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_RECURSIVE_MUTEX_HPP
|
Chris@16
|
28 #define BOOST_INTERPROCESS_DETAIL_POSIX_RECURSIVE_MUTEX_HPP
|
Chris@16
|
29
|
Chris@16
|
30 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
31 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
32
|
Chris@16
|
33 #include <pthread.h>
|
Chris@16
|
34 #include <errno.h>
|
Chris@16
|
35 #include <boost/interprocess/sync/posix/pthread_helpers.hpp>
|
Chris@16
|
36 #include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
|
Chris@16
|
37 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
Chris@16
|
38 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
39 #ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS
|
Chris@16
|
40 # include <boost/interprocess/detail/os_thread_functions.hpp>
|
Chris@16
|
41 # include <boost/interprocess/sync/spin/wait.hpp>
|
Chris@16
|
42 #endif
|
Chris@16
|
43 #include <boost/assert.hpp>
|
Chris@16
|
44
|
Chris@16
|
45 namespace boost {
|
Chris@16
|
46 namespace interprocess {
|
Chris@16
|
47 namespace ipcdetail {
|
Chris@16
|
48
|
Chris@16
|
49 class posix_recursive_mutex
|
Chris@16
|
50 {
|
Chris@16
|
51 posix_recursive_mutex(const posix_recursive_mutex &);
|
Chris@16
|
52 posix_recursive_mutex &operator=(const posix_recursive_mutex &);
|
Chris@16
|
53 public:
|
Chris@16
|
54
|
Chris@16
|
55 posix_recursive_mutex();
|
Chris@16
|
56 ~posix_recursive_mutex();
|
Chris@16
|
57
|
Chris@16
|
58 void lock();
|
Chris@16
|
59 bool try_lock();
|
Chris@16
|
60 bool timed_lock(const boost::posix_time::ptime &abs_time);
|
Chris@16
|
61 void unlock();
|
Chris@16
|
62
|
Chris@16
|
63 private:
|
Chris@16
|
64 pthread_mutex_t m_mut;
|
Chris@16
|
65 };
|
Chris@16
|
66
|
Chris@16
|
67 inline posix_recursive_mutex::posix_recursive_mutex()
|
Chris@16
|
68 {
|
Chris@16
|
69 mutexattr_wrapper mut_attr(true);
|
Chris@16
|
70 mutex_initializer mut(m_mut, mut_attr);
|
Chris@16
|
71 mut.release();
|
Chris@16
|
72 }
|
Chris@16
|
73
|
Chris@16
|
74 inline posix_recursive_mutex::~posix_recursive_mutex()
|
Chris@16
|
75 {
|
Chris@16
|
76 int res = pthread_mutex_destroy(&m_mut);
|
Chris@16
|
77 BOOST_ASSERT(res == 0);(void)res;
|
Chris@16
|
78 }
|
Chris@16
|
79
|
Chris@16
|
80 inline void posix_recursive_mutex::lock()
|
Chris@16
|
81 {
|
Chris@16
|
82 if (pthread_mutex_lock(&m_mut) != 0)
|
Chris@16
|
83 throw lock_exception();
|
Chris@16
|
84 }
|
Chris@16
|
85
|
Chris@16
|
86 inline bool posix_recursive_mutex::try_lock()
|
Chris@16
|
87 {
|
Chris@16
|
88 int res = pthread_mutex_trylock(&m_mut);
|
Chris@16
|
89 if (!(res == 0 || res == EBUSY))
|
Chris@16
|
90 throw lock_exception();
|
Chris@16
|
91 return res == 0;
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 inline bool posix_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
|
Chris@16
|
95 {
|
Chris@16
|
96 if(abs_time == boost::posix_time::pos_infin){
|
Chris@16
|
97 this->lock();
|
Chris@16
|
98 return true;
|
Chris@16
|
99 }
|
Chris@16
|
100 #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
|
Chris@16
|
101
|
Chris@16
|
102 timespec ts = ptime_to_timespec(abs_time);
|
Chris@16
|
103 int res = pthread_mutex_timedlock(&m_mut, &ts);
|
Chris@16
|
104 if (res != 0 && res != ETIMEDOUT)
|
Chris@16
|
105 throw lock_exception();
|
Chris@16
|
106 return res == 0;
|
Chris@16
|
107
|
Chris@16
|
108 #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS
|
Chris@16
|
109
|
Chris@16
|
110 //Obtain current count and target time
|
Chris@16
|
111 boost::posix_time::ptime now = microsec_clock::universal_time();
|
Chris@16
|
112 spin_wait swait;
|
Chris@16
|
113 do{
|
Chris@16
|
114 if(this->try_lock()){
|
Chris@16
|
115 break;
|
Chris@16
|
116 }
|
Chris@16
|
117 now = microsec_clock::universal_time();
|
Chris@16
|
118
|
Chris@16
|
119 if(now >= abs_time){
|
Chris@16
|
120 return false;
|
Chris@16
|
121 }
|
Chris@16
|
122 // relinquish current time slice
|
Chris@16
|
123 swait.yield();
|
Chris@16
|
124 }while (true);
|
Chris@16
|
125 return true;
|
Chris@16
|
126
|
Chris@16
|
127 #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS
|
Chris@16
|
128 }
|
Chris@16
|
129
|
Chris@16
|
130 inline void posix_recursive_mutex::unlock()
|
Chris@16
|
131 {
|
Chris@16
|
132 int res = 0;
|
Chris@16
|
133 res = pthread_mutex_unlock(&m_mut);
|
Chris@16
|
134 BOOST_ASSERT(res == 0); (void)res;
|
Chris@16
|
135 }
|
Chris@16
|
136
|
Chris@16
|
137 } //namespace ipcdetail {
|
Chris@16
|
138 } //namespace interprocess {
|
Chris@16
|
139 } //namespace boost {
|
Chris@16
|
140
|
Chris@16
|
141 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
142
|
Chris@16
|
143 #endif //#ifndef BOOST_INTERPROCESS_DETAIL_POSIX_RECURSIVE_MUTEX_HPP
|