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_RECURSIVE_MUTEX_HPP
|
Chris@16
|
28 #define BOOST_INTERPROCESS_RECURSIVE_MUTEX_HPP
|
Chris@16
|
29
|
Chris@101
|
30 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
31
|
Chris@101
|
32 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
33 # include <boost/config.hpp>
|
Chris@101
|
34 #endif
|
Chris@101
|
35 #
|
Chris@101
|
36 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@16
|
37 # pragma once
|
Chris@16
|
38 #endif
|
Chris@16
|
39
|
Chris@16
|
40 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
41 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
42 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
Chris@16
|
43 #include <boost/assert.hpp>
|
Chris@16
|
44
|
Chris@16
|
45 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && \
|
Chris@16
|
46 (defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED) && defined (BOOST_INTERPROCESS_POSIX_RECURSIVE_MUTEXES))
|
Chris@16
|
47 #include <boost/interprocess/sync/posix/recursive_mutex.hpp>
|
Chris@16
|
48 #define BOOST_INTERPROCESS_USE_POSIX
|
Chris@16
|
49 //Experimental...
|
Chris@16
|
50 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
|
Chris@16
|
51 #include <boost/interprocess/sync/windows/recursive_mutex.hpp>
|
Chris@16
|
52 #define BOOST_INTERPROCESS_USE_WINDOWS
|
Chris@16
|
53 #elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
54 #include <boost/interprocess/sync/spin/recursive_mutex.hpp>
|
Chris@16
|
55 #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
|
Chris@16
|
56 #endif
|
Chris@16
|
57
|
Chris@16
|
58 #if defined (BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
|
Chris@16
|
59 namespace boost {
|
Chris@16
|
60 namespace interprocess {
|
Chris@16
|
61 namespace ipcdetail{
|
Chris@16
|
62 namespace robust_emulation_helpers {
|
Chris@16
|
63
|
Chris@16
|
64 template<class T>
|
Chris@16
|
65 class mutex_traits;
|
Chris@16
|
66
|
Chris@16
|
67 }}}}
|
Chris@16
|
68
|
Chris@16
|
69 #endif
|
Chris@16
|
70
|
Chris@101
|
71 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
72
|
Chris@16
|
73 //!\file
|
Chris@16
|
74 //!Describes interprocess_recursive_mutex and shared_recursive_try_mutex classes
|
Chris@16
|
75
|
Chris@16
|
76 namespace boost {
|
Chris@16
|
77 namespace interprocess {
|
Chris@16
|
78
|
Chris@16
|
79 //!Wraps a interprocess_mutex that can be placed in shared memory and can be
|
Chris@16
|
80 //!shared between processes. Allows several locking calls by the same
|
Chris@16
|
81 //!process. Allows timed lock tries
|
Chris@16
|
82 class interprocess_recursive_mutex
|
Chris@16
|
83 {
|
Chris@101
|
84 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
85 //Non-copyable
|
Chris@16
|
86 interprocess_recursive_mutex(const interprocess_recursive_mutex &);
|
Chris@16
|
87 interprocess_recursive_mutex &operator=(const interprocess_recursive_mutex &);
|
Chris@101
|
88 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
89 public:
|
Chris@16
|
90 //!Constructor.
|
Chris@16
|
91 //!Throws interprocess_exception on error.
|
Chris@16
|
92 interprocess_recursive_mutex();
|
Chris@16
|
93
|
Chris@16
|
94 //!Destructor. If any process uses the mutex after the destructor is called
|
Chris@16
|
95 //!the result is undefined. Does not throw.
|
Chris@16
|
96 ~interprocess_recursive_mutex();
|
Chris@16
|
97
|
Chris@16
|
98 //!Effects: The calling thread tries to obtain ownership of the mutex, and
|
Chris@16
|
99 //! if another thread has ownership of the mutex, it waits until it can
|
Chris@16
|
100 //! obtain the ownership. If a thread takes ownership of the mutex the
|
Chris@16
|
101 //! mutex must be unlocked by the same mutex. The mutex must be unlocked
|
Chris@16
|
102 //! the same number of times it is locked.
|
Chris@16
|
103 //!Throws: interprocess_exception on error.
|
Chris@16
|
104 void lock();
|
Chris@16
|
105
|
Chris@16
|
106 //!Tries to lock the interprocess_mutex, returns false when interprocess_mutex
|
Chris@16
|
107 //!is already locked, returns true when success. The mutex must be unlocked
|
Chris@16
|
108 //!the same number of times it is locked.
|
Chris@16
|
109 //!Throws: interprocess_exception if a severe error is found
|
Chris@16
|
110 bool try_lock();
|
Chris@16
|
111
|
Chris@16
|
112 //!Tries to lock the interprocess_mutex, if interprocess_mutex can't be locked before
|
Chris@16
|
113 //!abs_time time, returns false. The mutex must be unlocked
|
Chris@16
|
114 //! the same number of times it is locked.
|
Chris@16
|
115 //!Throws: interprocess_exception if a severe error is found
|
Chris@16
|
116 bool timed_lock(const boost::posix_time::ptime &abs_time);
|
Chris@16
|
117
|
Chris@16
|
118 //!Effects: The calling thread releases the exclusive ownership of the mutex.
|
Chris@16
|
119 //! If the mutex supports recursive locking, the mutex must be unlocked the
|
Chris@16
|
120 //! same number of times it is locked.
|
Chris@16
|
121 //!Throws: interprocess_exception on error.
|
Chris@16
|
122 void unlock();
|
Chris@101
|
123 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
124 private:
|
Chris@16
|
125
|
Chris@16
|
126 #if defined (BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
|
Chris@16
|
127 #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
|
Chris@16
|
128 void take_ownership(){ mutex.take_ownership(); }
|
Chris@16
|
129 friend class ipcdetail::robust_emulation_helpers::mutex_traits<interprocess_recursive_mutex>;
|
Chris@16
|
130 ipcdetail::spin_recursive_mutex mutex;
|
Chris@16
|
131 #elif defined(BOOST_INTERPROCESS_USE_POSIX)
|
Chris@16
|
132 #undef BOOST_INTERPROCESS_USE_POSIX
|
Chris@16
|
133 ipcdetail::posix_recursive_mutex mutex;
|
Chris@16
|
134 #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
|
Chris@16
|
135 #undef BOOST_INTERPROCESS_USE_WINDOWS
|
Chris@16
|
136 ipcdetail::windows_recursive_mutex mutex;
|
Chris@16
|
137 #else
|
Chris@16
|
138 #error "Unknown platform for interprocess_mutex"
|
Chris@16
|
139 #endif
|
Chris@101
|
140 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
141 };
|
Chris@16
|
142
|
Chris@16
|
143 } //namespace interprocess {
|
Chris@16
|
144 } //namespace boost {
|
Chris@16
|
145
|
Chris@16
|
146 namespace boost {
|
Chris@16
|
147 namespace interprocess {
|
Chris@16
|
148
|
Chris@16
|
149 inline interprocess_recursive_mutex::interprocess_recursive_mutex(){}
|
Chris@16
|
150
|
Chris@16
|
151 inline interprocess_recursive_mutex::~interprocess_recursive_mutex(){}
|
Chris@16
|
152
|
Chris@16
|
153 inline void interprocess_recursive_mutex::lock()
|
Chris@16
|
154 {
|
Chris@16
|
155 #ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING
|
Chris@16
|
156 boost::posix_time::ptime wait_time
|
Chris@16
|
157 = boost::posix_time::microsec_clock::universal_time()
|
Chris@16
|
158 + boost::posix_time::milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS);
|
Chris@16
|
159 if (!mutex.timed_lock(wait_time)){
|
Chris@16
|
160 throw interprocess_exception(timeout_when_locking_error, "Interprocess mutex timeout when locking. Possible deadlock: owner died without unlocking?");
|
Chris@16
|
161 }
|
Chris@16
|
162 #else
|
Chris@16
|
163 mutex.lock();
|
Chris@16
|
164 #endif
|
Chris@16
|
165 }
|
Chris@16
|
166
|
Chris@16
|
167 inline bool interprocess_recursive_mutex::try_lock()
|
Chris@16
|
168 { return mutex.try_lock(); }
|
Chris@16
|
169
|
Chris@16
|
170 inline bool interprocess_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
|
Chris@16
|
171 { return mutex.timed_lock(abs_time); }
|
Chris@16
|
172
|
Chris@16
|
173 inline void interprocess_recursive_mutex::unlock()
|
Chris@16
|
174 { mutex.unlock(); }
|
Chris@16
|
175
|
Chris@16
|
176 } //namespace interprocess {
|
Chris@16
|
177 } //namespace boost {
|
Chris@16
|
178
|
Chris@16
|
179 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
180
|
Chris@16
|
181 #endif //BOOST_INTERPROCESS_RECURSIVE_MUTEX_HPP
|