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@101
|
30 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
31 # include <boost/config.hpp>
|
Chris@101
|
32 #endif
|
Chris@101
|
33 #
|
Chris@101
|
34 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@101
|
35 # pragma once
|
Chris@101
|
36 #endif
|
Chris@101
|
37
|
Chris@16
|
38 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
39 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
40
|
Chris@16
|
41 #include <pthread.h>
|
Chris@16
|
42 #include <errno.h>
|
Chris@16
|
43 #include <boost/interprocess/sync/posix/pthread_helpers.hpp>
|
Chris@16
|
44 #include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
|
Chris@16
|
45 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
Chris@16
|
46 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
47 #ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS
|
Chris@16
|
48 # include <boost/interprocess/detail/os_thread_functions.hpp>
|
Chris@101
|
49 # include <boost/interprocess/sync/detail/common_algorithms.hpp>
|
Chris@16
|
50 #endif
|
Chris@16
|
51 #include <boost/assert.hpp>
|
Chris@16
|
52
|
Chris@16
|
53 namespace boost {
|
Chris@16
|
54 namespace interprocess {
|
Chris@16
|
55 namespace ipcdetail {
|
Chris@16
|
56
|
Chris@16
|
57 class posix_recursive_mutex
|
Chris@16
|
58 {
|
Chris@16
|
59 posix_recursive_mutex(const posix_recursive_mutex &);
|
Chris@16
|
60 posix_recursive_mutex &operator=(const posix_recursive_mutex &);
|
Chris@16
|
61 public:
|
Chris@16
|
62
|
Chris@16
|
63 posix_recursive_mutex();
|
Chris@16
|
64 ~posix_recursive_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 private:
|
Chris@16
|
72 pthread_mutex_t m_mut;
|
Chris@16
|
73 };
|
Chris@16
|
74
|
Chris@16
|
75 inline posix_recursive_mutex::posix_recursive_mutex()
|
Chris@16
|
76 {
|
Chris@16
|
77 mutexattr_wrapper mut_attr(true);
|
Chris@16
|
78 mutex_initializer mut(m_mut, mut_attr);
|
Chris@16
|
79 mut.release();
|
Chris@16
|
80 }
|
Chris@16
|
81
|
Chris@16
|
82 inline posix_recursive_mutex::~posix_recursive_mutex()
|
Chris@16
|
83 {
|
Chris@16
|
84 int res = pthread_mutex_destroy(&m_mut);
|
Chris@16
|
85 BOOST_ASSERT(res == 0);(void)res;
|
Chris@16
|
86 }
|
Chris@16
|
87
|
Chris@16
|
88 inline void posix_recursive_mutex::lock()
|
Chris@16
|
89 {
|
Chris@16
|
90 if (pthread_mutex_lock(&m_mut) != 0)
|
Chris@16
|
91 throw lock_exception();
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 inline bool posix_recursive_mutex::try_lock()
|
Chris@16
|
95 {
|
Chris@16
|
96 int res = pthread_mutex_trylock(&m_mut);
|
Chris@16
|
97 if (!(res == 0 || res == EBUSY))
|
Chris@16
|
98 throw lock_exception();
|
Chris@16
|
99 return res == 0;
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 inline bool posix_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
|
Chris@16
|
103 {
|
Chris@101
|
104 #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
|
Chris@101
|
105 //Posix does not support infinity absolute time so handle it here
|
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
|
Chris@16
|
111 timespec ts = ptime_to_timespec(abs_time);
|
Chris@16
|
112 int res = pthread_mutex_timedlock(&m_mut, &ts);
|
Chris@16
|
113 if (res != 0 && res != ETIMEDOUT)
|
Chris@16
|
114 throw lock_exception();
|
Chris@16
|
115 return res == 0;
|
Chris@16
|
116
|
Chris@16
|
117 #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS
|
Chris@16
|
118
|
Chris@101
|
119 return ipcdetail::try_based_timed_lock(*this, abs_time);
|
Chris@16
|
120
|
Chris@16
|
121 #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS
|
Chris@16
|
122 }
|
Chris@16
|
123
|
Chris@16
|
124 inline void posix_recursive_mutex::unlock()
|
Chris@16
|
125 {
|
Chris@16
|
126 int res = 0;
|
Chris@16
|
127 res = pthread_mutex_unlock(&m_mut);
|
Chris@16
|
128 BOOST_ASSERT(res == 0); (void)res;
|
Chris@16
|
129 }
|
Chris@16
|
130
|
Chris@16
|
131 } //namespace ipcdetail {
|
Chris@16
|
132 } //namespace interprocess {
|
Chris@16
|
133 } //namespace boost {
|
Chris@16
|
134
|
Chris@16
|
135 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
136
|
Chris@16
|
137 #endif //#ifndef BOOST_INTERPROCESS_DETAIL_POSIX_RECURSIVE_MUTEX_HPP
|