]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/interprocess/sync/shm/named_mutex.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / interprocess / sync / shm / named_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_SHM_NAMED_MUTEX_HPP
12 #define BOOST_INTERPROCESS_SHM_NAMED_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/detail/interprocess_tester.hpp>
27 #include <boost/interprocess/permissions.hpp>
28
29 #include <boost/interprocess/shared_memory_object.hpp>
30 #include <boost/interprocess/sync/interprocess_mutex.hpp>
31 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
32 #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
33
34 //!\file
35 //!Describes a named mutex class for inter-process synchronization
36
37 namespace boost {
38 namespace interprocess {
39 namespace ipcdetail {
40
41 class named_condition;
42
43 //!A mutex with a global name, so it can be found from different
44 //!processes. This mutex can't be placed in shared memory, and
45 //!each process should have it's own named mutex.
46 class shm_named_mutex
47 {
48 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
49
50 //Non-copyable
51 shm_named_mutex();
52 shm_named_mutex(const shm_named_mutex &);
53 shm_named_mutex &operator=(const shm_named_mutex &);
54 friend class named_condition;
55 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
56
57 public:
58 //!Creates a global interprocess_mutex with a name.
59 //!Throws interprocess_exception on error.
60 shm_named_mutex(create_only_t, const char *name, const permissions &perm = permissions());
61
62 //!Opens or creates a global mutex with a name.
63 //!If the mutex is created, this call is equivalent to
64 //!shm_named_mutex(create_only_t, ... )
65 //!If the mutex is already created, this call is equivalent
66 //!shm_named_mutex(open_only_t, ... )
67 //!Does not throw
68 shm_named_mutex(open_or_create_t, const char *name, const permissions &perm = permissions());
69
70 //!Opens a global mutex with a name if that mutex is previously
71 //!created. If it is not previously created this function throws
72 //!interprocess_exception.
73 shm_named_mutex(open_only_t, const char *name);
74
75 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
76
77 //!Creates a global interprocess_mutex with a name.
78 //!Throws interprocess_exception on error.
79 shm_named_mutex(create_only_t, const wchar_t *name, const permissions &perm = permissions());
80
81 //!Opens or creates a global mutex with a name.
82 //!If the mutex is created, this call is equivalent to
83 //!shm_named_mutex(create_only_t, ... )
84 //!If the mutex is already created, this call is equivalent
85 //!shm_named_mutex(open_only_t, ... )
86 //!Does not throw
87 shm_named_mutex(open_or_create_t, const wchar_t *name, const permissions &perm = permissions());
88
89 //!Opens a global mutex with a name if that mutex is previously
90 //!created. If it is not previously created this function throws
91 //!interprocess_exception.
92 shm_named_mutex(open_only_t, const wchar_t *name);
93
94 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
95
96 //!Destroys *this and indicates that the calling process is finished using
97 //!the resource. The destructor function will deallocate
98 //!any system resources allocated by the system for use by this process for
99 //!this resource. The resource can still be opened again calling
100 //!the open constructor overload. To erase the resource from the system
101 //!use remove().
102 ~shm_named_mutex();
103
104 //!Unlocks a previously locked
105 //!interprocess_mutex.
106 void unlock();
107
108 //!Locks interprocess_mutex, sleeps when interprocess_mutex is already locked.
109 //!Throws interprocess_exception if a severe error is found
110 void lock();
111
112 //!Tries to lock the interprocess_mutex, returns false when interprocess_mutex
113 //!is already locked, returns true when success.
114 //!Throws interprocess_exception if a severe error is found
115 bool try_lock();
116
117 //!Tries to lock the interprocess_mutex until time abs_time,
118 //!Returns false when timeout expires, returns true when locks.
119 //!Throws interprocess_exception if a severe error is found
120 template<class TimePoint>
121 bool timed_lock(const TimePoint &abs_time);
122
123 template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
124 { return this->timed_lock(abs_time); }
125
126 template<class Duration> bool try_lock_for(const Duration &dur)
127 { return this->timed_lock(duration_to_ustime(dur)); }
128
129 //!Erases a named mutex from the system.
130 //!Returns false on error. Never throws.
131 static bool remove(const char *name);
132
133 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
134
135 //!Erases a named mutex from the system.
136 //!Returns false on error. Never throws.
137 static bool remove(const wchar_t *name);
138
139 #endif
140
141 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
142 typedef interprocess_mutex internal_mutex_type;
143 interprocess_mutex &internal_mutex()
144 { return *static_cast<interprocess_mutex*>(m_shmem.get_user_address()); }
145
146 private:
147 friend class ipcdetail::interprocess_tester;
148 void dont_close_on_destruction();
149 typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
150 open_create_impl_t m_shmem;
151 typedef ipcdetail::named_creation_functor<interprocess_mutex> construct_func_t;
152 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
153 };
154
155 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
156
157 inline void shm_named_mutex::dont_close_on_destruction()
158 { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); }
159
160 inline shm_named_mutex::~shm_named_mutex()
161 {}
162
163 inline shm_named_mutex::shm_named_mutex(create_only_t, const char *name, const permissions &perm)
164 : m_shmem (create_only_t()
165 ,name
166 ,sizeof(interprocess_mutex) +
167 open_create_impl_t::ManagedOpenOrCreateUserOffset
168 ,read_write
169 ,0
170 ,construct_func_t(ipcdetail::DoCreate)
171 ,perm)
172 {}
173
174 inline shm_named_mutex::shm_named_mutex(open_or_create_t, const char *name, const permissions &perm)
175 : m_shmem (open_or_create_t()
176 ,name
177 ,sizeof(interprocess_mutex) +
178 open_create_impl_t::ManagedOpenOrCreateUserOffset
179 ,read_write
180 ,0
181 ,construct_func_t(ipcdetail::DoOpenOrCreate)
182 ,perm)
183 {}
184
185 inline shm_named_mutex::shm_named_mutex(open_only_t, const char *name)
186 : m_shmem (open_only_t()
187 ,name
188 ,read_write
189 ,0
190 ,construct_func_t(ipcdetail::DoOpen))
191 {}
192
193 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
194
195 inline shm_named_mutex::shm_named_mutex(create_only_t, const wchar_t *name, const permissions &perm)
196 : m_shmem (create_only_t()
197 ,name
198 ,sizeof(interprocess_mutex) +
199 open_create_impl_t::ManagedOpenOrCreateUserOffset
200 ,read_write
201 ,0
202 ,construct_func_t(ipcdetail::DoCreate)
203 ,perm)
204 {}
205
206 inline shm_named_mutex::shm_named_mutex(open_or_create_t, const wchar_t *name, const permissions &perm)
207 : m_shmem (open_or_create_t()
208 ,name
209 ,sizeof(interprocess_mutex) +
210 open_create_impl_t::ManagedOpenOrCreateUserOffset
211 ,read_write
212 ,0
213 ,construct_func_t(ipcdetail::DoOpenOrCreate)
214 ,perm)
215 {}
216
217 inline shm_named_mutex::shm_named_mutex(open_only_t, const wchar_t *name)
218 : m_shmem (open_only_t()
219 ,name
220 ,read_write
221 ,0
222 ,construct_func_t(ipcdetail::DoOpen))
223 {}
224
225 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
226
227 inline void shm_named_mutex::lock()
228 { this->internal_mutex().lock(); }
229
230 inline void shm_named_mutex::unlock()
231 { this->internal_mutex().unlock(); }
232
233 inline bool shm_named_mutex::try_lock()
234 { return this->internal_mutex().try_lock(); }
235
236 template<class TimePoint>
237 inline bool shm_named_mutex::timed_lock(const TimePoint &abs_time)
238 { return this->internal_mutex().timed_lock(abs_time); }
239
240 inline bool shm_named_mutex::remove(const char *name)
241 { return shared_memory_object::remove(name); }
242
243 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
244
245 inline bool shm_named_mutex::remove(const wchar_t *name)
246 { return shared_memory_object::remove(name); }
247
248 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
249
250 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
251
252 } //namespace ipcdetail {
253 } //namespace interprocess {
254 } //namespace boost {
255
256 #include <boost/interprocess/detail/config_end.hpp>
257
258 #endif //BOOST_INTERPROCESS_SHM_NAMED_MUTEX_HPP