]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/include/boost/interprocess/sync/named_sharable_mutex.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / interprocess / include / boost / interprocess / sync / named_sharable_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 #ifndef BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP
12 #define BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP
13
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
16 #endif
17 #
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 # pragma once
20 #endif
21
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
24 #include <boost/interprocess/creation_tags.hpp>
25 #include <boost/interprocess/exceptions.hpp>
26 #include <boost/interprocess/shared_memory_object.hpp>
27 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
28 #include <boost/interprocess/sync/interprocess_sharable_mutex.hpp>
29 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
30 #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
31 #include <boost/interprocess/permissions.hpp>
32
33 //!\file
34 //!Describes a named sharable mutex class for inter-process synchronization
35
36 namespace boost {
37 namespace interprocess {
38
39 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
40 namespace ipcdetail{ class interprocess_tester; }
41 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
42
43 class named_condition;
44
45 //!A sharable mutex with a global name, so it can be found from different
46 //!processes. This mutex can't be placed in shared memory, and
47 //!each process should have it's own named sharable mutex.
48 class named_sharable_mutex
49 {
50 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
51 //Non-copyable
52 named_sharable_mutex();
53 named_sharable_mutex(const named_sharable_mutex &);
54 named_sharable_mutex &operator=(const named_sharable_mutex &);
55 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
56 public:
57
58 //!Creates a global sharable mutex with a name.
59 //!If the sharable mutex can't be created throws interprocess_exception
60 named_sharable_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
61
62 //!Opens or creates a global sharable mutex with a name.
63 //!If the sharable mutex is created, this call is equivalent to
64 //!named_sharable_mutex(create_only_t, ...)
65 //!If the sharable mutex is already created, this call is equivalent to
66 //!named_sharable_mutex(open_only_t, ... ).
67 named_sharable_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
68
69 //!Opens a global sharable mutex with a name if that sharable mutex
70 //!is previously.
71 //!created. If it is not previously created this function throws
72 //!interprocess_exception.
73 named_sharable_mutex(open_only_t open_only, const char *name);
74
75 //!Destroys *this and indicates that the calling process is finished using
76 //!the resource. The destructor function will deallocate
77 //!any system resources allocated by the system for use by this process for
78 //!this resource. The resource can still be opened again calling
79 //!the open constructor overload. To erase the resource from the system
80 //!use remove().
81 ~named_sharable_mutex();
82
83 //Exclusive locking
84
85 //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
86 //! and if another thread has exclusive or sharable ownership of
87 //! the mutex, it waits until it can obtain the ownership.
88 //!Throws: interprocess_exception on error.
89 void lock();
90
91 //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
92 //! without waiting. If no other thread has exclusive or sharable
93 //! ownership of the mutex this succeeds.
94 //!Returns: If it can acquire exclusive ownership immediately returns true.
95 //! If it has to wait, returns false.
96 //!Throws: interprocess_exception on error.
97 bool try_lock();
98
99 //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
100 //! waiting if necessary until no other thread has exclusive, or sharable
101 //! ownership of the mutex or abs_time is reached.
102 //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
103 //!Throws: interprocess_exception on error.
104 bool timed_lock(const boost::posix_time::ptime &abs_time);
105
106 //!Precondition: The thread must have exclusive ownership of the mutex.
107 //!Effects: The calling thread releases the exclusive ownership of the mutex.
108 //!Throws: An exception derived from interprocess_exception on error.
109 void unlock();
110
111 //Sharable locking
112
113 //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
114 //! and if another thread has exclusive ownership of the mutex,
115 //! waits until it can obtain the ownership.
116 //!Throws: interprocess_exception on error.
117 void lock_sharable();
118
119 //!Effects: The calling thread tries to acquire sharable ownership of the mutex
120 //! without waiting. If no other thread has exclusive ownership
121 //! of the mutex this succeeds.
122 //!Returns: If it can acquire sharable ownership immediately returns true. If it
123 //! has to wait, returns false.
124 //!Throws: interprocess_exception on error.
125 bool try_lock_sharable();
126
127 //!Effects: The calling thread tries to acquire sharable ownership of the mutex
128 //! waiting if necessary until no other thread has exclusive
129 //! ownership of the mutex or abs_time is reached.
130 //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
131 //!Throws: interprocess_exception on error.
132 bool timed_lock_sharable(const boost::posix_time::ptime &abs_time);
133
134 //!Precondition: The thread must have sharable ownership of the mutex.
135 //!Effects: The calling thread releases the sharable ownership of the mutex.
136 //!Throws: An exception derived from interprocess_exception on error.
137 void unlock_sharable();
138
139 //!Erases a named sharable mutex from the system.
140 //!Returns false on error. Never throws.
141 static bool remove(const char *name);
142
143 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
144 private:
145 friend class ipcdetail::interprocess_tester;
146 void dont_close_on_destruction();
147
148 interprocess_sharable_mutex *mutex() const
149 { return static_cast<interprocess_sharable_mutex*>(m_shmem.get_user_address()); }
150
151 typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
152 open_create_impl_t m_shmem;
153 typedef ipcdetail::named_creation_functor<interprocess_sharable_mutex> construct_func_t;
154 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
155 };
156
157 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
158
159 inline named_sharable_mutex::~named_sharable_mutex()
160 {}
161
162 inline named_sharable_mutex::named_sharable_mutex
163 (create_only_t, const char *name, const permissions &perm)
164 : m_shmem (create_only
165 ,name
166 ,sizeof(interprocess_sharable_mutex) +
167 open_create_impl_t::ManagedOpenOrCreateUserOffset
168 ,read_write
169 ,0
170 ,construct_func_t(ipcdetail::DoCreate)
171 ,perm)
172 {}
173
174 inline named_sharable_mutex::named_sharable_mutex
175 (open_or_create_t, const char *name, const permissions &perm)
176 : m_shmem (open_or_create
177 ,name
178 ,sizeof(interprocess_sharable_mutex) +
179 open_create_impl_t::ManagedOpenOrCreateUserOffset
180 ,read_write
181 ,0
182 ,construct_func_t(ipcdetail::DoOpenOrCreate)
183 ,perm)
184 {}
185
186 inline named_sharable_mutex::named_sharable_mutex
187 (open_only_t, const char *name)
188 : m_shmem (open_only
189 ,name
190 ,read_write
191 ,0
192 ,construct_func_t(ipcdetail::DoOpen))
193 {}
194
195 inline void named_sharable_mutex::dont_close_on_destruction()
196 { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); }
197
198 inline void named_sharable_mutex::lock()
199 { this->mutex()->lock(); }
200
201 inline void named_sharable_mutex::unlock()
202 { this->mutex()->unlock(); }
203
204 inline bool named_sharable_mutex::try_lock()
205 { return this->mutex()->try_lock(); }
206
207 inline bool named_sharable_mutex::timed_lock
208 (const boost::posix_time::ptime &abs_time)
209 { return this->mutex()->timed_lock(abs_time); }
210
211 inline void named_sharable_mutex::lock_sharable()
212 { this->mutex()->lock_sharable(); }
213
214 inline void named_sharable_mutex::unlock_sharable()
215 { this->mutex()->unlock_sharable(); }
216
217 inline bool named_sharable_mutex::try_lock_sharable()
218 { return this->mutex()->try_lock_sharable(); }
219
220 inline bool named_sharable_mutex::timed_lock_sharable
221 (const boost::posix_time::ptime &abs_time)
222 { return this->mutex()->timed_lock_sharable(abs_time); }
223
224 inline bool named_sharable_mutex::remove(const char *name)
225 { return shared_memory_object::remove(name); }
226
227 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
228
229 } //namespace interprocess {
230 } //namespace boost {
231
232 #include <boost/interprocess/detail/config_end.hpp>
233
234 #endif //BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP