annotate DEPENDENCIES/generic/include/boost/interprocess/sync/posix/mutex.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1 //////////////////////////////////////////////////////////////////////////////
Chris@16 2 //
Chris@16 3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
Chris@16 4 // Software License, Version 1.0. (See accompanying file
Chris@16 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 6 //
Chris@16 7 // See http://www.boost.org/libs/interprocess for documentation.
Chris@16 8 //
Chris@16 9 //////////////////////////////////////////////////////////////////////////////
Chris@16 10 //
Chris@16 11 // Parts of the pthread code come from Boost Threads code:
Chris@16 12 //
Chris@16 13 //////////////////////////////////////////////////////////////////////////////
Chris@16 14 //
Chris@16 15 // Copyright (C) 2001-2003
Chris@16 16 // William E. Kempf
Chris@16 17 //
Chris@16 18 // Permission to use, copy, modify, distribute and sell this software
Chris@16 19 // and its documentation for any purpose is hereby granted without fee,
Chris@16 20 // provided that the above copyright notice appear in all copies and
Chris@16 21 // that both that copyright notice and this permission notice appear
Chris@16 22 // in supporting documentation. William E. Kempf makes no representations
Chris@16 23 // about the suitability of this software for any purpose.
Chris@16 24 // It is provided "as is" without express or implied warranty.
Chris@16 25 //////////////////////////////////////////////////////////////////////////////
Chris@16 26
Chris@16 27 #ifndef BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP
Chris@16 28 #define BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP
Chris@16 29
Chris@16 30 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
Chris@16 31 # pragma once
Chris@16 32 #endif
Chris@16 33
Chris@16 34 #include <boost/interprocess/detail/config_begin.hpp>
Chris@16 35 #include <boost/interprocess/detail/workaround.hpp>
Chris@16 36
Chris@16 37 #include <pthread.h>
Chris@16 38 #include <errno.h>
Chris@16 39 #include <boost/interprocess/exceptions.hpp>
Chris@16 40 #include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
Chris@16 41 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
Chris@16 42 #include <boost/interprocess/exceptions.hpp>
Chris@16 43 #include <boost/interprocess/sync/posix/pthread_helpers.hpp>
Chris@16 44
Chris@16 45 #ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS
Chris@16 46 # include <boost/interprocess/detail/os_thread_functions.hpp>
Chris@16 47 # include <boost/interprocess/sync/spin/wait.hpp>
Chris@16 48 #endif
Chris@16 49 #include <boost/assert.hpp>
Chris@16 50
Chris@16 51 namespace boost {
Chris@16 52 namespace interprocess {
Chris@16 53 namespace ipcdetail {
Chris@16 54
Chris@16 55 class posix_condition;
Chris@16 56
Chris@16 57 class posix_mutex
Chris@16 58 {
Chris@16 59 posix_mutex(const posix_mutex &);
Chris@16 60 posix_mutex &operator=(const posix_mutex &);
Chris@16 61 public:
Chris@16 62
Chris@16 63 posix_mutex();
Chris@16 64 ~posix_mutex();
Chris@16 65
Chris@16 66 void lock();
Chris@16 67 bool try_lock();
Chris@16 68 bool timed_lock(const boost::posix_time::ptime &abs_time);
Chris@16 69 void unlock();
Chris@16 70
Chris@16 71 friend class posix_condition;
Chris@16 72
Chris@16 73 private:
Chris@16 74 pthread_mutex_t m_mut;
Chris@16 75 };
Chris@16 76
Chris@16 77 inline posix_mutex::posix_mutex()
Chris@16 78 {
Chris@16 79 mutexattr_wrapper mut_attr;
Chris@16 80 mutex_initializer mut(m_mut, mut_attr);
Chris@16 81 mut.release();
Chris@16 82 }
Chris@16 83
Chris@16 84 inline posix_mutex::~posix_mutex()
Chris@16 85 {
Chris@16 86 int res = pthread_mutex_destroy(&m_mut);
Chris@16 87 BOOST_ASSERT(res == 0);(void)res;
Chris@16 88 }
Chris@16 89
Chris@16 90 inline void posix_mutex::lock()
Chris@16 91 {
Chris@16 92 if (pthread_mutex_lock(&m_mut) != 0)
Chris@16 93 throw lock_exception();
Chris@16 94 }
Chris@16 95
Chris@16 96 inline bool posix_mutex::try_lock()
Chris@16 97 {
Chris@16 98 int res = pthread_mutex_trylock(&m_mut);
Chris@16 99 if (!(res == 0 || res == EBUSY))
Chris@16 100 throw lock_exception();
Chris@16 101 return res == 0;
Chris@16 102 }
Chris@16 103
Chris@16 104 inline bool posix_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
Chris@16 105 {
Chris@16 106 if(abs_time == boost::posix_time::pos_infin){
Chris@16 107 this->lock();
Chris@16 108 return true;
Chris@16 109 }
Chris@16 110 #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
Chris@16 111
Chris@16 112 timespec ts = ptime_to_timespec(abs_time);
Chris@16 113 int res = pthread_mutex_timedlock(&m_mut, &ts);
Chris@16 114 if (res != 0 && res != ETIMEDOUT)
Chris@16 115 throw lock_exception();
Chris@16 116 return res == 0;
Chris@16 117
Chris@16 118 #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS
Chris@16 119
Chris@16 120 //Obtain current count and target time
Chris@16 121 boost::posix_time::ptime now = microsec_clock::universal_time();
Chris@16 122
Chris@16 123 spin_wait swait;
Chris@16 124 do{
Chris@16 125 if(this->try_lock()){
Chris@16 126 break;
Chris@16 127 }
Chris@16 128 now = microsec_clock::universal_time();
Chris@16 129
Chris@16 130 if(now >= abs_time){
Chris@16 131 return false;
Chris@16 132 }
Chris@16 133 // relinquish current time slice
Chris@16 134 swait.yield();
Chris@16 135 }while (true);
Chris@16 136 return true;
Chris@16 137
Chris@16 138 #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS
Chris@16 139 }
Chris@16 140
Chris@16 141 inline void posix_mutex::unlock()
Chris@16 142 {
Chris@16 143 int res = 0;
Chris@16 144 res = pthread_mutex_unlock(&m_mut);
Chris@16 145 (void)res;
Chris@16 146 BOOST_ASSERT(res == 0);
Chris@16 147 }
Chris@16 148
Chris@16 149 } //namespace ipcdetail {
Chris@16 150 } //namespace interprocess {
Chris@16 151 } //namespace boost {
Chris@16 152
Chris@16 153 #include <boost/interprocess/detail/config_end.hpp>
Chris@16 154
Chris@16 155 #endif //#ifndef BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP