]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/include/boost/interprocess/sync/interprocess_recursive_mutex.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / interprocess / include / boost / interprocess / sync / interprocess_recursive_mutex.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10 //
11 // Parts of the pthread code come from Boost Threads code:
12 //
13 //////////////////////////////////////////////////////////////////////////////
14 //
15 // Copyright (C) 2001-2003
16 // William E. Kempf
17 //
18 // Permission to use, copy, modify, distribute and sell this software
19 // and its documentation for any purpose is hereby granted without fee,
20 // provided that the above copyright notice appear in all copies and
21 // that both that copyright notice and this permission notice appear
22 // in supporting documentation. William E. Kempf makes no representations
23 // about the suitability of this software for any purpose.
24 // It is provided "as is" without express or implied warranty.
25 //////////////////////////////////////////////////////////////////////////////
26
27 #ifndef BOOST_INTERPROCESS_RECURSIVE_MUTEX_HPP
28 #define BOOST_INTERPROCESS_RECURSIVE_MUTEX_HPP
29
30 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
31
32 #ifndef BOOST_CONFIG_HPP
33 # include <boost/config.hpp>
34 #endif
35 #
36 #if defined(BOOST_HAS_PRAGMA_ONCE)
37 # pragma once
38 #endif
39
40 #include <boost/interprocess/detail/config_begin.hpp>
41 #include <boost/interprocess/detail/workaround.hpp>
42 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
43 #include <boost/assert.hpp>
44
45 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && \
46 (defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED) && defined (BOOST_INTERPROCESS_POSIX_RECURSIVE_MUTEXES))
47 #include <boost/interprocess/sync/posix/recursive_mutex.hpp>
48 #define BOOST_INTERPROCESS_USE_POSIX
49 //Experimental...
50 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
51 #include <boost/interprocess/sync/windows/recursive_mutex.hpp>
52 #define BOOST_INTERPROCESS_USE_WINDOWS
53 #elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
54 #include <boost/interprocess/sync/spin/recursive_mutex.hpp>
55 #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
56 #endif
57
58 #if defined (BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
59 namespace boost {
60 namespace interprocess {
61 namespace ipcdetail{
62 namespace robust_emulation_helpers {
63
64 template<class T>
65 class mutex_traits;
66
67 }}}}
68
69 #endif
70
71 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
72
73 //!\file
74 //!Describes interprocess_recursive_mutex and shared_recursive_try_mutex classes
75
76 namespace boost {
77 namespace interprocess {
78
79 //!Wraps a interprocess_mutex that can be placed in shared memory and can be
80 //!shared between processes. Allows several locking calls by the same
81 //!process. Allows timed lock tries
82 class interprocess_recursive_mutex
83 {
84 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
85 //Non-copyable
86 interprocess_recursive_mutex(const interprocess_recursive_mutex &);
87 interprocess_recursive_mutex &operator=(const interprocess_recursive_mutex &);
88 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
89 public:
90 //!Constructor.
91 //!Throws interprocess_exception on error.
92 interprocess_recursive_mutex();
93
94 //!Destructor. If any process uses the mutex after the destructor is called
95 //!the result is undefined. Does not throw.
96 ~interprocess_recursive_mutex();
97
98 //!Effects: The calling thread tries to obtain ownership of the mutex, and
99 //! if another thread has ownership of the mutex, it waits until it can
100 //! obtain the ownership. If a thread takes ownership of the mutex the
101 //! mutex must be unlocked by the same mutex. The mutex must be unlocked
102 //! the same number of times it is locked.
103 //!Throws: interprocess_exception on error.
104 void lock();
105
106 //!Tries to lock the interprocess_mutex, returns false when interprocess_mutex
107 //!is already locked, returns true when success. The mutex must be unlocked
108 //!the same number of times it is locked.
109 //!Throws: interprocess_exception if a severe error is found
110 bool try_lock();
111
112 //!Tries to lock the interprocess_mutex, if interprocess_mutex can't be locked before
113 //!abs_time time, returns false. The mutex must be unlocked
114 //! the same number of times it is locked.
115 //!Throws: interprocess_exception if a severe error is found
116 bool timed_lock(const boost::posix_time::ptime &abs_time);
117
118 //!Effects: The calling thread releases the exclusive ownership of the mutex.
119 //! If the mutex supports recursive locking, the mutex must be unlocked the
120 //! same number of times it is locked.
121 //!Throws: interprocess_exception on error.
122 void unlock();
123 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
124 private:
125
126 #if defined (BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
127 #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
128 void take_ownership(){ mutex.take_ownership(); }
129 friend class ipcdetail::robust_emulation_helpers::mutex_traits<interprocess_recursive_mutex>;
130 ipcdetail::spin_recursive_mutex mutex;
131 #elif defined(BOOST_INTERPROCESS_USE_POSIX)
132 #undef BOOST_INTERPROCESS_USE_POSIX
133 ipcdetail::posix_recursive_mutex mutex;
134 #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
135 #undef BOOST_INTERPROCESS_USE_WINDOWS
136 ipcdetail::windows_recursive_mutex mutex;
137 #else
138 #error "Unknown platform for interprocess_mutex"
139 #endif
140 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
141 };
142
143 } //namespace interprocess {
144 } //namespace boost {
145
146 namespace boost {
147 namespace interprocess {
148
149 inline interprocess_recursive_mutex::interprocess_recursive_mutex(){}
150
151 inline interprocess_recursive_mutex::~interprocess_recursive_mutex(){}
152
153 inline void interprocess_recursive_mutex::lock()
154 {
155 #ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING
156 boost::posix_time::ptime wait_time
157 = microsec_clock::universal_time()
158 + boost::posix_time::milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS);
159 if (!mutex.timed_lock(wait_time)){
160 throw interprocess_exception(timeout_when_locking_error, "Interprocess mutex timeout when locking. Possible deadlock: owner died without unlocking?");
161 }
162 #else
163 mutex.lock();
164 #endif
165 }
166
167 inline bool interprocess_recursive_mutex::try_lock()
168 { return mutex.try_lock(); }
169
170 inline bool interprocess_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
171 { return mutex.timed_lock(abs_time); }
172
173 inline void interprocess_recursive_mutex::unlock()
174 { mutex.unlock(); }
175
176 } //namespace interprocess {
177 } //namespace boost {
178
179 #include <boost/interprocess/detail/config_end.hpp>
180
181 #endif //BOOST_INTERPROCESS_RECURSIVE_MUTEX_HPP