]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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_RECURSIVE_MUTEX_HPP | |
12 | #define BOOST_INTERPROCESS_NAMED_RECURSIVE_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> | |
7c673cae | 25 | #include <boost/interprocess/permissions.hpp> |
1e59de90 | 26 | |
7c673cae FG |
27 | #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS) |
28 | #include <boost/interprocess/sync/windows/named_recursive_mutex.hpp> | |
1e59de90 | 29 | #define BOOST_INTERPROCESS_NAMED_RECURSIVE_MUTEX_USE_WINAPI |
7c673cae FG |
30 | #else |
31 | #include <boost/interprocess/sync/shm/named_recursive_mutex.hpp> | |
32 | #endif | |
33 | ||
34 | //!\file | |
35 | //!Describes a named named_recursive_mutex class for inter-process synchronization | |
36 | ||
37 | namespace boost { | |
38 | namespace interprocess { | |
39 | ||
40 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
41 | namespace ipcdetail{ class interprocess_tester; } | |
42 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
43 | ||
44 | //!A recursive mutex with a global name, so it can be found from different | |
45 | //!processes. This mutex can't be placed in shared memory, and | |
46 | //!each process should have it's own named_recursive_mutex. | |
47 | class named_recursive_mutex | |
48 | { | |
49 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
50 | //Non-copyable | |
51 | named_recursive_mutex(); | |
52 | named_recursive_mutex(const named_recursive_mutex &); | |
53 | named_recursive_mutex &operator=(const named_recursive_mutex &); | |
54 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
55 | public: | |
56 | ||
57 | //!Creates a global recursive_mutex with a name. | |
58 | //!If the recursive_mutex can't be created throws interprocess_exception | |
1e59de90 TL |
59 | named_recursive_mutex(create_only_t, const char *name, const permissions &perm = permissions()); |
60 | ||
61 | //!Opens or creates a global recursive_mutex with a name. | |
62 | //!If the recursive_mutex is created, this call is equivalent to | |
63 | //!named_recursive_mutex(create_only_t, ... ) | |
64 | //!If the recursive_mutex is already created, this call is equivalent | |
65 | //!named_recursive_mutex(open_only_t, ... ) | |
66 | //!Does not throw | |
67 | named_recursive_mutex(open_or_create_t, const char *name, const permissions &perm = permissions()); | |
68 | ||
69 | //!Opens a global recursive_mutex with a name if that recursive_mutex is previously | |
70 | //!created. If it is not previously created this function throws | |
71 | //!interprocess_exception. | |
72 | named_recursive_mutex(open_only_t, const char *name); | |
73 | ||
74 | #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
75 | ||
76 | //!Creates a global recursive_mutex with a name. | |
77 | //!If the recursive_mutex can't be created throws interprocess_exception | |
78 | //! | |
79 | //!Note: This function is only available on operating systems with | |
80 | //! native wchar_t APIs (e.g. Windows). | |
81 | named_recursive_mutex(create_only_t, const wchar_t *name, const permissions &perm = permissions()); | |
7c673cae FG |
82 | |
83 | //!Opens or creates a global recursive_mutex with a name. | |
84 | //!If the recursive_mutex is created, this call is equivalent to | |
85 | //!named_recursive_mutex(create_only_t, ... ) | |
86 | //!If the recursive_mutex is already created, this call is equivalent | |
87 | //!named_recursive_mutex(open_only_t, ... ) | |
88 | //!Does not throw | |
1e59de90 TL |
89 | //! |
90 | //!Note: This function is only available on operating systems with | |
91 | //! native wchar_t APIs (e.g. Windows). | |
92 | named_recursive_mutex(open_or_create_t, const wchar_t *name, const permissions &perm = permissions()); | |
7c673cae FG |
93 | |
94 | //!Opens a global recursive_mutex with a name if that recursive_mutex is previously | |
95 | //!created. If it is not previously created this function throws | |
96 | //!interprocess_exception. | |
1e59de90 TL |
97 | //! |
98 | //!Note: This function is only available on operating systems with | |
99 | //! native wchar_t APIs (e.g. Windows). | |
100 | named_recursive_mutex(open_only_t, const wchar_t *name); | |
101 | ||
102 | #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
7c673cae FG |
103 | |
104 | //!Destroys *this and indicates that the calling process is finished using | |
105 | //!the resource. The destructor function will deallocate | |
106 | //!any system resources allocated by the system for use by this process for | |
107 | //!this resource. The resource can still be opened again calling | |
108 | //!the open constructor overload. To erase the resource from the system | |
109 | //!use remove(). | |
110 | ~named_recursive_mutex(); | |
111 | ||
112 | //!Unlocks a previously locked | |
113 | //!named_recursive_mutex. | |
114 | void unlock(); | |
115 | ||
116 | //!Locks named_recursive_mutex, sleeps when named_recursive_mutex is already locked. | |
117 | //!Throws interprocess_exception if a severe error is found. | |
1e59de90 TL |
118 | //! |
119 | //!Note: A program shall not deadlock if the thread that has ownership calls | |
120 | //! this function. | |
7c673cae FG |
121 | void lock(); |
122 | ||
123 | //!Tries to lock the named_recursive_mutex, returns false when named_recursive_mutex | |
124 | //!is already locked, returns true when success. | |
125 | //!Throws interprocess_exception if a severe error is found. | |
1e59de90 TL |
126 | //! |
127 | //!Note: A program shall not deadlock if the thread that has ownership calls | |
128 | //! this function. | |
7c673cae FG |
129 | bool try_lock(); |
130 | ||
131 | //!Tries to lock the named_recursive_mutex until time abs_time, | |
132 | //!Returns false when timeout expires, returns true when locks. | |
133 | //!Throws interprocess_exception if a severe error is found | |
1e59de90 TL |
134 | //! |
135 | //!Note: A program shall not deadlock if the thread that has ownership calls | |
136 | //! this function. | |
137 | template<class TimePoint> | |
138 | bool timed_lock(const TimePoint &abs_time); | |
139 | ||
140 | //!Same as `timed_lock`, but this function is modeled after the | |
141 | //!standard library interface. | |
142 | template<class TimePoint> bool try_lock_until(const TimePoint &abs_time) | |
143 | { return this->timed_lock(abs_time); } | |
144 | ||
145 | //!Same as `timed_lock`, but this function is modeled after the | |
146 | //!standard library interface. | |
147 | template<class Duration> bool try_lock_for(const Duration &dur) | |
148 | { return this->timed_lock(ipcdetail::duration_to_ustime(dur)); } | |
7c673cae FG |
149 | |
150 | //!Erases a named recursive mutex | |
151 | //!from the system | |
152 | static bool remove(const char *name); | |
153 | ||
1e59de90 TL |
154 | #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
155 | //!Erases a named recursive mutex | |
156 | //!from the system | |
157 | //! | |
158 | //!Note: This function is only available on operating systems with | |
159 | //! native wchar_t APIs (e.g. Windows). | |
160 | static bool remove(const wchar_t *name); | |
161 | #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
162 | ||
7c673cae FG |
163 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
164 | private: | |
165 | friend class ipcdetail::interprocess_tester; | |
166 | void dont_close_on_destruction(); | |
167 | ||
1e59de90 TL |
168 | #if defined(BOOST_INTERPROCESS_NAMED_RECURSIVE_MUTEX_USE_WINAPI) |
169 | typedef ipcdetail::winapi_named_recursive_mutex impl_t; | |
7c673cae FG |
170 | #else |
171 | typedef ipcdetail::shm_named_recursive_mutex impl_t; | |
172 | #endif | |
173 | impl_t m_mut; | |
174 | ||
175 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
176 | }; | |
177 | ||
178 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
179 | ||
180 | inline named_recursive_mutex::~named_recursive_mutex() | |
181 | {} | |
182 | ||
183 | inline void named_recursive_mutex::dont_close_on_destruction() | |
184 | { ipcdetail::interprocess_tester::dont_close_on_destruction(m_mut); } | |
185 | ||
186 | inline named_recursive_mutex::named_recursive_mutex(create_only_t, const char *name, const permissions &perm) | |
187 | : m_mut (create_only, name, perm) | |
188 | {} | |
189 | ||
190 | inline named_recursive_mutex::named_recursive_mutex(open_or_create_t, const char *name, const permissions &perm) | |
191 | : m_mut (open_or_create, name, perm) | |
192 | {} | |
193 | ||
194 | inline named_recursive_mutex::named_recursive_mutex(open_only_t, const char *name) | |
195 | : m_mut (open_only, name) | |
196 | {} | |
197 | ||
1e59de90 TL |
198 | #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
199 | ||
200 | inline named_recursive_mutex::named_recursive_mutex(create_only_t, const wchar_t *name, const permissions &perm) | |
201 | : m_mut (create_only, name, perm) | |
202 | {} | |
203 | ||
204 | inline named_recursive_mutex::named_recursive_mutex(open_or_create_t, const wchar_t *name, const permissions &perm) | |
205 | : m_mut (open_or_create, name, perm) | |
206 | {} | |
207 | ||
208 | inline named_recursive_mutex::named_recursive_mutex(open_only_t, const wchar_t *name) | |
209 | : m_mut (open_only, name) | |
210 | {} | |
211 | ||
212 | #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
213 | ||
7c673cae FG |
214 | inline void named_recursive_mutex::lock() |
215 | { m_mut.lock(); } | |
216 | ||
217 | inline void named_recursive_mutex::unlock() | |
218 | { m_mut.unlock(); } | |
219 | ||
220 | inline bool named_recursive_mutex::try_lock() | |
221 | { return m_mut.try_lock(); } | |
222 | ||
1e59de90 TL |
223 | template<class TimePoint> |
224 | inline bool named_recursive_mutex::timed_lock(const TimePoint &abs_time) | |
7c673cae FG |
225 | { return m_mut.timed_lock(abs_time); } |
226 | ||
227 | inline bool named_recursive_mutex::remove(const char *name) | |
228 | { return impl_t::remove(name); } | |
229 | ||
1e59de90 TL |
230 | #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
231 | ||
232 | inline bool named_recursive_mutex::remove(const wchar_t *name) | |
233 | { return impl_t::remove(name); } | |
234 | ||
235 | #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
236 | ||
7c673cae FG |
237 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
238 | ||
239 | } //namespace interprocess { | |
240 | } //namespace boost { | |
241 | ||
242 | #include <boost/interprocess/detail/config_end.hpp> | |
243 | ||
244 | #endif //BOOST_INTERPROCESS_NAMED_RECURSIVE_MUTEX_HPP |