1 | 1 |
new file mode 100755 |
... | ... |
@@ -0,0 +1,284 @@ |
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_FILE_LOCK_HPP |
|
12 |
+#define BOOST_INTERPROCESS_FILE_LOCK_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/exceptions.hpp> |
|
25 |
+#include <boost/interprocess/detail/os_file_functions.hpp> |
|
26 |
+#include <boost/interprocess/detail/os_thread_functions.hpp> |
|
27 |
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp> |
|
28 |
+#include <boost/interprocess/sync/detail/common_algorithms.hpp> |
|
29 |
+#include <boost/interprocess/sync/detail/locks.hpp> |
|
30 |
+#include <boost/move/utility_core.hpp> |
|
31 |
+ |
|
32 |
+//!\file |
|
33 |
+//!Describes a class that wraps file locking capabilities. |
|
34 |
+ |
|
35 |
+namespace boost { |
|
36 |
+namespace interprocess { |
|
37 |
+ |
|
38 |
+ |
|
39 |
+//!A file lock, is a mutual exclusion utility similar to a mutex using a |
|
40 |
+//!file. A file lock has sharable and exclusive locking capabilities and |
|
41 |
+//!can be used with scoped_lock and sharable_lock classes. |
|
42 |
+//!A file lock can't guarantee synchronization between threads of the same |
|
43 |
+//!process so just use file locks to synchronize threads from different processes. |
|
44 |
+class file_lock |
|
45 |
+{ |
|
46 |
+ #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
|
47 |
+ //Non-copyable |
|
48 |
+ BOOST_MOVABLE_BUT_NOT_COPYABLE(file_lock) |
|
49 |
+ #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
|
50 |
+ |
|
51 |
+ public: |
|
52 |
+ //!Constructs an empty file mapping. |
|
53 |
+ //!Does not throw |
|
54 |
+ file_lock() BOOST_NOEXCEPT |
|
55 |
+ : m_file_hnd(file_handle_t(ipcdetail::invalid_file())) |
|
56 |
+ {} |
|
57 |
+ |
|
58 |
+ //!Opens a file lock. Throws interprocess_exception if the file does not |
|
59 |
+ //!exist or there are no operating system resources. |
|
60 |
+ file_lock(const char *name); |
|
61 |
+ |
|
62 |
+ #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
|
63 |
+ //!Opens a file lock. Throws interprocess_exception if the file does not |
|
64 |
+ //!exist or there are no operating system resources. |
|
65 |
+ //! |
|
66 |
+ //!Note: This function is only available on operating systems with |
|
67 |
+ //! native wchar_t APIs (e.g. Windows). |
|
68 |
+ file_lock(const wchar_t *name); |
|
69 |
+ #endif |
|
70 |
+ |
|
71 |
+ //!Moves the ownership of "moved"'s file mapping object to *this. |
|
72 |
+ //!After the call, "moved" does not represent any file mapping object. |
|
73 |
+ //!Does not throw |
|
74 |
+ file_lock(BOOST_RV_REF(file_lock) moved) BOOST_NOEXCEPT |
|
75 |
+ : m_file_hnd(file_handle_t(ipcdetail::invalid_file())) |
|
76 |
+ { this->swap(moved); } |
|
77 |
+ |
|
78 |
+ //!Moves the ownership of "moved"'s file mapping to *this. |
|
79 |
+ //!After the call, "moved" does not represent any file mapping. |
|
80 |
+ //!Does not throw |
|
81 |
+ file_lock &operator=(BOOST_RV_REF(file_lock) moved) BOOST_NOEXCEPT |
|
82 |
+ { |
|
83 |
+ file_lock tmp(boost::move(moved)); |
|
84 |
+ this->swap(tmp); |
|
85 |
+ return *this; |
|
86 |
+ } |
|
87 |
+ |
|
88 |
+ //!Closes a file lock. Does not throw. |
|
89 |
+ ~file_lock(); |
|
90 |
+ |
|
91 |
+ //!Swaps two file_locks. |
|
92 |
+ //!Does not throw. |
|
93 |
+ void swap(file_lock &other) BOOST_NOEXCEPT |
|
94 |
+ { |
|
95 |
+ file_handle_t tmp = m_file_hnd; |
|
96 |
+ m_file_hnd = other.m_file_hnd; |
|
97 |
+ other.m_file_hnd = tmp; |
|
98 |
+ } |
|
99 |
+ |
|
100 |
+ //Exclusive locking |
|
101 |
+ |
|
102 |
+ //!Requires: The calling thread does not own the mutex. |
|
103 |
+ //! |
|
104 |
+ //!Effects: The calling thread tries to obtain exclusive ownership of the mutex, |
|
105 |
+ //! and if another thread has exclusive, or sharable ownership of |
|
106 |
+ //! the mutex, it waits until it can obtain the ownership. |
|
107 |
+ //!Throws: interprocess_exception on error. |
|
108 |
+ //! |
|
109 |
+ //!Note: A program may deadlock if the thread that has ownership calls |
|
110 |
+ //! this function. If the implementation can detect the deadlock, |
|
111 |
+ //! an exception could be thrown. |
|
112 |
+ void lock(); |
|
113 |
+ |
|
114 |
+ //!Requires: The calling thread does not own the mutex. |
|
115 |
+ //! |
|
116 |
+ //!Effects: The calling thread tries to acquire exclusive ownership of the mutex |
|
117 |
+ //! without waiting. If no other thread has exclusive, or sharable |
|
118 |
+ //! ownership of the mutex this succeeds. |
|
119 |
+ //!Returns: If it can acquire exclusive ownership immediately returns true. |
|
120 |
+ //! If it has to wait, returns false. |
|
121 |
+ //!Throws: interprocess_exception on error. |
|
122 |
+ //! |
|
123 |
+ //!Note: A program may deadlock if the thread that has ownership calls |
|
124 |
+ //! this function. If the implementation can detect the deadlock, |
|
125 |
+ //! an exception could be thrown. |
|
126 |
+ bool try_lock(); |
|
127 |
+ |
|
128 |
+ //!Requires: The calling thread does not own the mutex. |
|
129 |
+ //! |
|
130 |
+ //!Effects: The calling thread tries to acquire exclusive ownership of the mutex |
|
131 |
+ //! waiting if necessary until no other thread has exclusive, or sharable |
|
132 |
+ //! ownership of the mutex or abs_time is reached. |
|
133 |
+ //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false. |
|
134 |
+ //!Throws: interprocess_exception on error. |
|
135 |
+ //! |
|
136 |
+ //!Note: A program may deadlock if the thread that has ownership calls |
|
137 |
+ //! this function. If the implementation can detect the deadlock, |
|
138 |
+ //! an exception could be thrown. |
|
139 |
+ bool timed_lock(const boost::posix_time::ptime &abs_time); |
|
140 |
+ |
|
141 |
+ //!Precondition: The thread must have exclusive ownership of the mutex. |
|
142 |
+ //!Effects: The calling thread releases the exclusive ownership of the mutex. |
|
143 |
+ //!Throws: An exception derived from interprocess_exception on error. |
|
144 |
+ void unlock(); |
|
145 |
+ |
|
146 |
+ //Sharable locking |
|
147 |
+ |
|
148 |
+ //!Requires: The calling thread does not own the mutex. |
|
149 |
+ //! |
|
150 |
+ //!Effects: The calling thread tries to obtain sharable ownership of the mutex, |
|
151 |
+ //! and if another thread has exclusive ownership of the mutex, waits until |
|
152 |
+ //! it can obtain the ownership. |
|
153 |
+ //!Throws: interprocess_exception on error. |
|
154 |
+ //! |
|
155 |
+ //!Note: A program may deadlock if the thread that owns a mutex object calls |
|
156 |
+ //! this function. If the implementation can detect the deadlock, |
|
157 |
+ //! an exception could be thrown. |
|
158 |
+ void lock_sharable(); |
|
159 |
+ |
|
160 |
+ //!Effects: The calling thread tries to acquire sharable ownership of the mutex |
|
161 |
+ //! without waiting. If no other thread has exclusive ownership of the |
|
162 |
+ //! mutex this succeeds. |
|
163 |
+ //!Returns: If it can acquire sharable ownership immediately returns true. If it |
|
164 |
+ //! has to wait, returns false. |
|
165 |
+ //!Throws: interprocess_exception on error. |
|
166 |
+ bool try_lock_sharable(); |
|
167 |
+ |
|
168 |
+ //!Effects: The calling thread tries to acquire sharable ownership of the mutex |
|
169 |
+ //! waiting if necessary until no other thread has exclusive ownership of |
|
170 |
+ //! the mutex or abs_time is reached. |
|
171 |
+ //!Returns: If acquires sharable ownership, returns true. Otherwise returns false. |
|
172 |
+ //!Throws: interprocess_exception on error. |
|
173 |
+ bool timed_lock_sharable(const boost::posix_time::ptime &abs_time); |
|
174 |
+ |
|
175 |
+ //!Precondition: The thread must have sharable ownership of the mutex. |
|
176 |
+ //!Effects: The calling thread releases the sharable ownership of the mutex. |
|
177 |
+ //!Throws: An exception derived from interprocess_exception on error. |
|
178 |
+ void unlock_sharable(); |
|
179 |
+ #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
|
180 |
+ private: |
|
181 |
+ file_handle_t m_file_hnd; |
|
182 |
+ |
|
183 |
+ #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
|
184 |
+}; |
|
185 |
+ |
|
186 |
+inline file_lock::file_lock(const char *name) |
|
187 |
+{ |
|
188 |
+ m_file_hnd = ipcdetail::open_existing_file(name, read_write); |
|
189 |
+ |
|
190 |
+ if(m_file_hnd == ipcdetail::invalid_file()){ |
|
191 |
+ error_info err(system_error_code()); |
|
192 |
+ throw interprocess_exception(err); |
|
193 |
+ } |
|
194 |
+} |
|
195 |
+ |
|
196 |
+#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
|
197 |
+ |
|
198 |
+inline file_lock::file_lock(const wchar_t *name) |
|
199 |
+{ |
|
200 |
+ m_file_hnd = ipcdetail::open_existing_file(name, read_write); |
|
201 |
+ |
|
202 |
+ if(m_file_hnd == ipcdetail::invalid_file()){ |
|
203 |
+ error_info err(system_error_code()); |
|
204 |
+ throw interprocess_exception(err); |
|
205 |
+ } |
|
206 |
+} |
|
207 |
+ |
|
208 |
+#endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
|
209 |
+ |
|
210 |
+inline file_lock::~file_lock() |
|
211 |
+{ |
|
212 |
+ if(m_file_hnd != ipcdetail::invalid_file()){ |
|
213 |
+ ipcdetail::close_file(m_file_hnd); |
|
214 |
+ m_file_hnd = ipcdetail::invalid_file(); |
|
215 |
+ } |
|
216 |
+} |
|
217 |
+ |
|
218 |
+inline void file_lock::lock() |
|
219 |
+{ |
|
220 |
+ if(!ipcdetail::acquire_file_lock(m_file_hnd)){ |
|
221 |
+ error_info err(system_error_code()); |
|
222 |
+ throw interprocess_exception(err); |
|
223 |
+ } |
|
224 |
+} |
|
225 |
+ |
|
226 |
+inline bool file_lock::try_lock() |
|
227 |
+{ |
|
228 |
+ bool result; |
|
229 |
+ if(!ipcdetail::try_acquire_file_lock(m_file_hnd, result)){ |
|
230 |
+ error_info err(system_error_code()); |
|
231 |
+ throw interprocess_exception(err); |
|
232 |
+ } |
|
233 |
+ return result; |
|
234 |
+} |
|
235 |
+ |
|
236 |
+inline bool file_lock::timed_lock(const boost::posix_time::ptime &abs_time) |
|
237 |
+{ return ipcdetail::try_based_timed_lock(*this, abs_time); } |
|
238 |
+ |
|
239 |
+inline void file_lock::unlock() |
|
240 |
+{ |
|
241 |
+ if(!ipcdetail::release_file_lock(m_file_hnd)){ |
|
242 |
+ error_info err(system_error_code()); |
|
243 |
+ throw interprocess_exception(err); |
|
244 |
+ } |
|
245 |
+} |
|
246 |
+ |
|
247 |
+inline void file_lock::lock_sharable() |
|
248 |
+{ |
|
249 |
+ if(!ipcdetail::acquire_file_lock_sharable(m_file_hnd)){ |
|
250 |
+ error_info err(system_error_code()); |
|
251 |
+ throw interprocess_exception(err); |
|
252 |
+ } |
|
253 |
+} |
|
254 |
+ |
|
255 |
+inline bool file_lock::try_lock_sharable() |
|
256 |
+{ |
|
257 |
+ bool result; |
|
258 |
+ if(!ipcdetail::try_acquire_file_lock_sharable(m_file_hnd, result)){ |
|
259 |
+ error_info err(system_error_code()); |
|
260 |
+ throw interprocess_exception(err); |
|
261 |
+ } |
|
262 |
+ return result; |
|
263 |
+} |
|
264 |
+ |
|
265 |
+inline bool file_lock::timed_lock_sharable(const boost::posix_time::ptime &abs_time) |
|
266 |
+{ |
|
267 |
+ ipcdetail::lock_to_sharable<file_lock> lsh(*this); |
|
268 |
+ return ipcdetail::try_based_timed_lock(lsh, abs_time); |
|
269 |
+} |
|
270 |
+ |
|
271 |
+inline void file_lock::unlock_sharable() |
|
272 |
+{ |
|
273 |
+ if(!ipcdetail::release_file_lock_sharable(m_file_hnd)){ |
|
274 |
+ error_info err(system_error_code()); |
|
275 |
+ throw interprocess_exception(err); |
|
276 |
+ } |
|
277 |
+} |
|
278 |
+ |
|
279 |
+} //namespace interprocess { |
|
280 |
+} //namespace boost { |
|
281 |
+ |
|
282 |
+#include <boost/interprocess/detail/config_end.hpp> |
|
283 |
+ |
|
284 |
+#endif //BOOST_INTERPROCESS_FILE_LOCK_HPP |
1 | 1 |
deleted file mode 100755 |
... | ... |
@@ -1,284 +0,0 @@ |
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_FILE_LOCK_HPP |
|
12 |
-#define BOOST_INTERPROCESS_FILE_LOCK_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/exceptions.hpp> |
|
25 |
-#include <boost/interprocess/detail/os_file_functions.hpp> |
|
26 |
-#include <boost/interprocess/detail/os_thread_functions.hpp> |
|
27 |
-#include <boost/interprocess/detail/posix_time_types_wrk.hpp> |
|
28 |
-#include <boost/interprocess/sync/detail/common_algorithms.hpp> |
|
29 |
-#include <boost/interprocess/sync/detail/locks.hpp> |
|
30 |
-#include <boost/move/utility_core.hpp> |
|
31 |
- |
|
32 |
-//!\file |
|
33 |
-//!Describes a class that wraps file locking capabilities. |
|
34 |
- |
|
35 |
-namespace boost { |
|
36 |
-namespace interprocess { |
|
37 |
- |
|
38 |
- |
|
39 |
-//!A file lock, is a mutual exclusion utility similar to a mutex using a |
|
40 |
-//!file. A file lock has sharable and exclusive locking capabilities and |
|
41 |
-//!can be used with scoped_lock and sharable_lock classes. |
|
42 |
-//!A file lock can't guarantee synchronization between threads of the same |
|
43 |
-//!process so just use file locks to synchronize threads from different processes. |
|
44 |
-class file_lock |
|
45 |
-{ |
|
46 |
- #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
|
47 |
- //Non-copyable |
|
48 |
- BOOST_MOVABLE_BUT_NOT_COPYABLE(file_lock) |
|
49 |
- #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
|
50 |
- |
|
51 |
- public: |
|
52 |
- //!Constructs an empty file mapping. |
|
53 |
- //!Does not throw |
|
54 |
- file_lock() BOOST_NOEXCEPT |
|
55 |
- : m_file_hnd(file_handle_t(ipcdetail::invalid_file())) |
|
56 |
- {} |
|
57 |
- |
|
58 |
- //!Opens a file lock. Throws interprocess_exception if the file does not |
|
59 |
- //!exist or there are no operating system resources. |
|
60 |
- file_lock(const char *name); |
|
61 |
- |
|
62 |
- #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
|
63 |
- //!Opens a file lock. Throws interprocess_exception if the file does not |
|
64 |
- //!exist or there are no operating system resources. |
|
65 |
- //! |
|
66 |
- //!Note: This function is only available on operating systems with |
|
67 |
- //! native wchar_t APIs (e.g. Windows). |
|
68 |
- file_lock(const wchar_t *name); |
|
69 |
- #endif |
|
70 |
- |
|
71 |
- //!Moves the ownership of "moved"'s file mapping object to *this. |
|
72 |
- //!After the call, "moved" does not represent any file mapping object. |
|
73 |
- //!Does not throw |
|
74 |
- file_lock(BOOST_RV_REF(file_lock) moved) BOOST_NOEXCEPT |
|
75 |
- : m_file_hnd(file_handle_t(ipcdetail::invalid_file())) |
|
76 |
- { this->swap(moved); } |
|
77 |
- |
|
78 |
- //!Moves the ownership of "moved"'s file mapping to *this. |
|
79 |
- //!After the call, "moved" does not represent any file mapping. |
|
80 |
- //!Does not throw |
|
81 |
- file_lock &operator=(BOOST_RV_REF(file_lock) moved) BOOST_NOEXCEPT |
|
82 |
- { |
|
83 |
- file_lock tmp(boost::move(moved)); |
|
84 |
- this->swap(tmp); |
|
85 |
- return *this; |
|
86 |
- } |
|
87 |
- |
|
88 |
- //!Closes a file lock. Does not throw. |
|
89 |
- ~file_lock(); |
|
90 |
- |
|
91 |
- //!Swaps two file_locks. |
|
92 |
- //!Does not throw. |
|
93 |
- void swap(file_lock &other) BOOST_NOEXCEPT |
|
94 |
- { |
|
95 |
- file_handle_t tmp = m_file_hnd; |
|
96 |
- m_file_hnd = other.m_file_hnd; |
|
97 |
- other.m_file_hnd = tmp; |
|
98 |
- } |
|
99 |
- |
|
100 |
- //Exclusive locking |
|
101 |
- |
|
102 |
- //!Requires: The calling thread does not own the mutex. |
|
103 |
- //! |
|
104 |
- //!Effects: The calling thread tries to obtain exclusive ownership of the mutex, |
|
105 |
- //! and if another thread has exclusive, or sharable ownership of |
|
106 |
- //! the mutex, it waits until it can obtain the ownership. |
|
107 |
- //!Throws: interprocess_exception on error. |
|
108 |
- //! |
|
109 |
- //!Note: A program may deadlock if the thread that has ownership calls |
|
110 |
- //! this function. If the implementation can detect the deadlock, |
|
111 |
- //! an exception could be thrown. |
|
112 |
- void lock(); |
|
113 |
- |
|
114 |
- //!Requires: The calling thread does not own the mutex. |
|
115 |
- //! |
|
116 |
- //!Effects: The calling thread tries to acquire exclusive ownership of the mutex |
|
117 |
- //! without waiting. If no other thread has exclusive, or sharable |
|
118 |
- //! ownership of the mutex this succeeds. |
|
119 |
- //!Returns: If it can acquire exclusive ownership immediately returns true. |
|
120 |
- //! If it has to wait, returns false. |
|
121 |
- //!Throws: interprocess_exception on error. |
|
122 |
- //! |
|
123 |
- //!Note: A program may deadlock if the thread that has ownership calls |
|
124 |
- //! this function. If the implementation can detect the deadlock, |
|
125 |
- //! an exception could be thrown. |
|
126 |
- bool try_lock(); |
|
127 |
- |
|
128 |
- //!Requires: The calling thread does not own the mutex. |
|
129 |
- //! |
|
130 |
- //!Effects: The calling thread tries to acquire exclusive ownership of the mutex |
|
131 |
- //! waiting if necessary until no other thread has exclusive, or sharable |
|
132 |
- //! ownership of the mutex or abs_time is reached. |
|
133 |
- //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false. |
|
134 |
- //!Throws: interprocess_exception on error. |
|
135 |
- //! |
|
136 |
- //!Note: A program may deadlock if the thread that has ownership calls |
|
137 |
- //! this function. If the implementation can detect the deadlock, |
|
138 |
- //! an exception could be thrown. |
|
139 |
- bool timed_lock(const boost::posix_time::ptime &abs_time); |
|
140 |
- |
|
141 |
- //!Precondition: The thread must have exclusive ownership of the mutex. |
|
142 |
- //!Effects: The calling thread releases the exclusive ownership of the mutex. |
|
143 |
- //!Throws: An exception derived from interprocess_exception on error. |
|
144 |
- void unlock(); |
|
145 |
- |
|
146 |
- //Sharable locking |
|
147 |
- |
|
148 |
- //!Requires: The calling thread does not own the mutex. |
|
149 |
- //! |
|
150 |
- //!Effects: The calling thread tries to obtain sharable ownership of the mutex, |
|
151 |
- //! and if another thread has exclusive ownership of the mutex, waits until |
|
152 |
- //! it can obtain the ownership. |
|
153 |
- //!Throws: interprocess_exception on error. |
|
154 |
- //! |
|
155 |
- //!Note: A program may deadlock if the thread that owns a mutex object calls |
|
156 |
- //! this function. If the implementation can detect the deadlock, |
|
157 |
- //! an exception could be thrown. |
|
158 |
- void lock_sharable(); |
|
159 |
- |
|
160 |
- //!Effects: The calling thread tries to acquire sharable ownership of the mutex |
|
161 |
- //! without waiting. If no other thread has exclusive ownership of the |
|
162 |
- //! mutex this succeeds. |
|
163 |
- //!Returns: If it can acquire sharable ownership immediately returns true. If it |
|
164 |
- //! has to wait, returns false. |
|
165 |
- //!Throws: interprocess_exception on error. |
|
166 |
- bool try_lock_sharable(); |
|
167 |
- |
|
168 |
- //!Effects: The calling thread tries to acquire sharable ownership of the mutex |
|
169 |
- //! waiting if necessary until no other thread has exclusive ownership of |
|
170 |
- //! the mutex or abs_time is reached. |
|
171 |
- //!Returns: If acquires sharable ownership, returns true. Otherwise returns false. |
|
172 |
- //!Throws: interprocess_exception on error. |
|
173 |
- bool timed_lock_sharable(const boost::posix_time::ptime &abs_time); |
|
174 |
- |
|
175 |
- //!Precondition: The thread must have sharable ownership of the mutex. |
|
176 |
- //!Effects: The calling thread releases the sharable ownership of the mutex. |
|
177 |
- //!Throws: An exception derived from interprocess_exception on error. |
|
178 |
- void unlock_sharable(); |
|
179 |
- #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
|
180 |
- private: |
|
181 |
- file_handle_t m_file_hnd; |
|
182 |
- |
|
183 |
- #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
|
184 |
-}; |
|
185 |
- |
|
186 |
-inline file_lock::file_lock(const char *name) |
|
187 |
-{ |
|
188 |
- m_file_hnd = ipcdetail::open_existing_file(name, read_write); |
|
189 |
- |
|
190 |
- if(m_file_hnd == ipcdetail::invalid_file()){ |
|
191 |
- error_info err(system_error_code()); |
|
192 |
- throw interprocess_exception(err); |
|
193 |
- } |
|
194 |
-} |
|
195 |
- |
|
196 |
-#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
|
197 |
- |
|
198 |
-inline file_lock::file_lock(const wchar_t *name) |
|
199 |
-{ |
|
200 |
- m_file_hnd = ipcdetail::open_existing_file(name, read_write); |
|
201 |
- |
|
202 |
- if(m_file_hnd == ipcdetail::invalid_file()){ |
|
203 |
- error_info err(system_error_code()); |
|
204 |
- throw interprocess_exception(err); |
|
205 |
- } |
|
206 |
-} |
|
207 |
- |
|
208 |
-#endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
|
209 |
- |
|
210 |
-inline file_lock::~file_lock() |
|
211 |
-{ |
|
212 |
- if(m_file_hnd != ipcdetail::invalid_file()){ |
|
213 |
- ipcdetail::close_file(m_file_hnd); |
|
214 |
- m_file_hnd = ipcdetail::invalid_file(); |
|
215 |
- } |
|
216 |
-} |
|
217 |
- |
|
218 |
-inline void file_lock::lock() |
|
219 |
-{ |
|
220 |
- if(!ipcdetail::acquire_file_lock(m_file_hnd)){ |
|
221 |
- error_info err(system_error_code()); |
|
222 |
- throw interprocess_exception(err); |
|
223 |
- } |
|
224 |
-} |
|
225 |
- |
|
226 |
-inline bool file_lock::try_lock() |
|
227 |
-{ |
|
228 |
- bool result; |
|
229 |
- if(!ipcdetail::try_acquire_file_lock(m_file_hnd, result)){ |
|
230 |
- error_info err(system_error_code()); |
|
231 |
- throw interprocess_exception(err); |
|
232 |
- } |
|
233 |
- return result; |
|
234 |
-} |
|
235 |
- |
|
236 |
-inline bool file_lock::timed_lock(const boost::posix_time::ptime &abs_time) |
|
237 |
-{ return ipcdetail::try_based_timed_lock(*this, abs_time); } |
|
238 |
- |
|
239 |
-inline void file_lock::unlock() |
|
240 |
-{ |
|
241 |
- if(!ipcdetail::release_file_lock(m_file_hnd)){ |
|
242 |
- error_info err(system_error_code()); |
|
243 |
- throw interprocess_exception(err); |
|
244 |
- } |
|
245 |
-} |
|
246 |
- |
|
247 |
-inline void file_lock::lock_sharable() |
|
248 |
-{ |
|
249 |
- if(!ipcdetail::acquire_file_lock_sharable(m_file_hnd)){ |
|
250 |
- error_info err(system_error_code()); |
|
251 |
- throw interprocess_exception(err); |
|
252 |
- } |
|
253 |
-} |
|
254 |
- |
|
255 |
-inline bool file_lock::try_lock_sharable() |
|
256 |
-{ |
|
257 |
- bool result; |
|
258 |
- if(!ipcdetail::try_acquire_file_lock_sharable(m_file_hnd, result)){ |
|
259 |
- error_info err(system_error_code()); |
|
260 |
- throw interprocess_exception(err); |
|
261 |
- } |
|
262 |
- return result; |
|
263 |
-} |
|
264 |
- |
|
265 |
-inline bool file_lock::timed_lock_sharable(const boost::posix_time::ptime &abs_time) |
|
266 |
-{ |
|
267 |
- ipcdetail::lock_to_sharable<file_lock> lsh(*this); |
|
268 |
- return ipcdetail::try_based_timed_lock(lsh, abs_time); |
|
269 |
-} |
|
270 |
- |
|
271 |
-inline void file_lock::unlock_sharable() |
|
272 |
-{ |
|
273 |
- if(!ipcdetail::release_file_lock_sharable(m_file_hnd)){ |
|
274 |
- error_info err(system_error_code()); |
|
275 |
- throw interprocess_exception(err); |
|
276 |
- } |
|
277 |
-} |
|
278 |
- |
|
279 |
-} //namespace interprocess { |
|
280 |
-} //namespace boost { |
|
281 |
- |
|
282 |
-#include <boost/interprocess/detail/config_end.hpp> |
|
283 |
- |
|
284 |
-#endif //BOOST_INTERPROCESS_FILE_LOCK_HPP |
1 | 1 |
new file mode 100755 |
... | ... |
@@ -0,0 +1,284 @@ |
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_FILE_LOCK_HPP |
|
12 |
+#define BOOST_INTERPROCESS_FILE_LOCK_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/exceptions.hpp> |
|
25 |
+#include <boost/interprocess/detail/os_file_functions.hpp> |
|
26 |
+#include <boost/interprocess/detail/os_thread_functions.hpp> |
|
27 |
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp> |
|
28 |
+#include <boost/interprocess/sync/detail/common_algorithms.hpp> |
|
29 |
+#include <boost/interprocess/sync/detail/locks.hpp> |
|
30 |
+#include <boost/move/utility_core.hpp> |
|
31 |
+ |
|
32 |
+//!\file |
|
33 |
+//!Describes a class that wraps file locking capabilities. |
|
34 |
+ |
|
35 |
+namespace boost { |
|
36 |
+namespace interprocess { |
|
37 |
+ |
|
38 |
+ |
|
39 |
+//!A file lock, is a mutual exclusion utility similar to a mutex using a |
|
40 |
+//!file. A file lock has sharable and exclusive locking capabilities and |
|
41 |
+//!can be used with scoped_lock and sharable_lock classes. |
|
42 |
+//!A file lock can't guarantee synchronization between threads of the same |
|
43 |
+//!process so just use file locks to synchronize threads from different processes. |
|
44 |
+class file_lock |
|
45 |
+{ |
|
46 |
+ #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
|
47 |
+ //Non-copyable |
|
48 |
+ BOOST_MOVABLE_BUT_NOT_COPYABLE(file_lock) |
|
49 |
+ #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
|
50 |
+ |
|
51 |
+ public: |
|
52 |
+ //!Constructs an empty file mapping. |
|
53 |
+ //!Does not throw |
|
54 |
+ file_lock() BOOST_NOEXCEPT |
|
55 |
+ : m_file_hnd(file_handle_t(ipcdetail::invalid_file())) |
|
56 |
+ {} |
|
57 |
+ |
|
58 |
+ //!Opens a file lock. Throws interprocess_exception if the file does not |
|
59 |
+ //!exist or there are no operating system resources. |
|
60 |
+ file_lock(const char *name); |
|
61 |
+ |
|
62 |
+ #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
|
63 |
+ //!Opens a file lock. Throws interprocess_exception if the file does not |
|
64 |
+ //!exist or there are no operating system resources. |
|
65 |
+ //! |
|
66 |
+ //!Note: This function is only available on operating systems with |
|
67 |
+ //! native wchar_t APIs (e.g. Windows). |
|
68 |
+ file_lock(const wchar_t *name); |
|
69 |
+ #endif |
|
70 |
+ |
|
71 |
+ //!Moves the ownership of "moved"'s file mapping object to *this. |
|
72 |
+ //!After the call, "moved" does not represent any file mapping object. |
|
73 |
+ //!Does not throw |
|
74 |
+ file_lock(BOOST_RV_REF(file_lock) moved) BOOST_NOEXCEPT |
|
75 |
+ : m_file_hnd(file_handle_t(ipcdetail::invalid_file())) |
|
76 |
+ { this->swap(moved); } |
|
77 |
+ |
|
78 |
+ //!Moves the ownership of "moved"'s file mapping to *this. |
|
79 |
+ //!After the call, "moved" does not represent any file mapping. |
|
80 |
+ //!Does not throw |
|
81 |
+ file_lock &operator=(BOOST_RV_REF(file_lock) moved) BOOST_NOEXCEPT |
|
82 |
+ { |
|
83 |
+ file_lock tmp(boost::move(moved)); |
|
84 |
+ this->swap(tmp); |
|
85 |
+ return *this; |
|
86 |
+ } |
|
87 |
+ |
|
88 |
+ //!Closes a file lock. Does not throw. |
|
89 |
+ ~file_lock(); |
|
90 |
+ |
|
91 |
+ //!Swaps two file_locks. |
|
92 |
+ //!Does not throw. |
|
93 |
+ void swap(file_lock &other) BOOST_NOEXCEPT |
|
94 |
+ { |
|
95 |
+ file_handle_t tmp = m_file_hnd; |
|
96 |
+ m_file_hnd = other.m_file_hnd; |
|
97 |
+ other.m_file_hnd = tmp; |
|
98 |
+ } |
|
99 |
+ |
|
100 |
+ //Exclusive locking |
|
101 |
+ |
|
102 |
+ //!Requires: The calling thread does not own the mutex. |
|
103 |
+ //! |
|
104 |
+ //!Effects: The calling thread tries to obtain exclusive ownership of the mutex, |
|
105 |
+ //! and if another thread has exclusive, or sharable ownership of |
|
106 |
+ //! the mutex, it waits until it can obtain the ownership. |
|
107 |
+ //!Throws: interprocess_exception on error. |
|
108 |
+ //! |
|
109 |
+ //!Note: A program may deadlock if the thread that has ownership calls |
|
110 |
+ //! this function. If the implementation can detect the deadlock, |
|
111 |
+ //! an exception could be thrown. |
|
112 |
+ void lock(); |
|
113 |
+ |
|
114 |
+ //!Requires: The calling thread does not own the mutex. |
|
115 |
+ //! |
|
116 |
+ //!Effects: The calling thread tries to acquire exclusive ownership of the mutex |
|
117 |
+ //! without waiting. If no other thread has exclusive, or sharable |
|
118 |
+ //! ownership of the mutex this succeeds. |
|
119 |
+ //!Returns: If it can acquire exclusive ownership immediately returns true. |
|
120 |
+ //! If it has to wait, returns false. |
|
121 |
+ //!Throws: interprocess_exception on error. |
|
122 |
+ //! |
|
123 |
+ //!Note: A program may deadlock if the thread that has ownership calls |
|
124 |
+ //! this function. If the implementation can detect the deadlock, |
|
125 |
+ //! an exception could be thrown. |
|
126 |
+ bool try_lock(); |
|
127 |
+ |
|
128 |
+ //!Requires: The calling thread does not own the mutex. |
|
129 |
+ //! |
|
130 |
+ //!Effects: The calling thread tries to acquire exclusive ownership of the mutex |
|
131 |
+ //! waiting if necessary until no other thread has exclusive, or sharable |
|
132 |
+ //! ownership of the mutex or abs_time is reached. |
|
133 |
+ //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false. |
|
134 |
+ //!Throws: interprocess_exception on error. |
|
135 |
+ //! |
|
136 |
+ //!Note: A program may deadlock if the thread that has ownership calls |
|
137 |
+ //! this function. If the implementation can detect the deadlock, |
|
138 |
+ //! an exception could be thrown. |
|
139 |
+ bool timed_lock(const boost::posix_time::ptime &abs_time); |
|
140 |
+ |
|
141 |
+ //!Precondition: The thread must have exclusive ownership of the mutex. |
|
142 |
+ //!Effects: The calling thread releases the exclusive ownership of the mutex. |
|
143 |
+ //!Throws: An exception derived from interprocess_exception on error. |
|
144 |
+ void unlock(); |
|
145 |
+ |
|
146 |
+ //Sharable locking |
|
147 |
+ |
|
148 |
+ //!Requires: The calling thread does not own the mutex. |
|
149 |
+ //! |
|
150 |
+ //!Effects: The calling thread tries to obtain sharable ownership of the mutex, |
|
151 |
+ //! and if another thread has exclusive ownership of the mutex, waits until |
|
152 |
+ //! it can obtain the ownership. |
|
153 |
+ //!Throws: interprocess_exception on error. |
|
154 |
+ //! |
|
155 |
+ //!Note: A program may deadlock if the thread that owns a mutex object calls |
|
156 |
+ //! this function. If the implementation can detect the deadlock, |
|
157 |
+ //! an exception could be thrown. |
|
158 |
+ void lock_sharable(); |
|
159 |
+ |
|
160 |
+ //!Effects: The calling thread tries to acquire sharable ownership of the mutex |
|
161 |
+ //! without waiting. If no other thread has exclusive ownership of the |
|
162 |
+ //! mutex this succeeds. |
|
163 |
+ //!Returns: If it can acquire sharable ownership immediately returns true. If it |
|
164 |
+ //! has to wait, returns false. |
|
165 |
+ //!Throws: interprocess_exception on error. |
|
166 |
+ bool try_lock_sharable(); |
|
167 |
+ |
|
168 |
+ //!Effects: The calling thread tries to acquire sharable ownership of the mutex |
|
169 |
+ //! waiting if necessary until no other thread has exclusive ownership of |
|
170 |
+ //! the mutex or abs_time is reached. |
|
171 |
+ //!Returns: If acquires sharable ownership, returns true. Otherwise returns false. |
|
172 |
+ //!Throws: interprocess_exception on error. |
|
173 |
+ bool timed_lock_sharable(const boost::posix_time::ptime &abs_time); |
|
174 |
+ |
|
175 |
+ //!Precondition: The thread must have sharable ownership of the mutex. |
|
176 |
+ //!Effects: The calling thread releases the sharable ownership of the mutex. |
|
177 |
+ //!Throws: An exception derived from interprocess_exception on error. |
|
178 |
+ void unlock_sharable(); |
|
179 |
+ #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
|
180 |
+ private: |
|
181 |
+ file_handle_t m_file_hnd; |
|
182 |
+ |
|
183 |
+ #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
|
184 |
+}; |
|
185 |
+ |
|
186 |
+inline file_lock::file_lock(const char *name) |
|
187 |
+{ |
|
188 |
+ m_file_hnd = ipcdetail::open_existing_file(name, read_write); |
|
189 |
+ |
|
190 |
+ if(m_file_hnd == ipcdetail::invalid_file()){ |
|
191 |
+ error_info err(system_error_code()); |
|
192 |
+ throw interprocess_exception(err); |
|
193 |
+ } |
|
194 |
+} |
|
195 |
+ |
|
196 |
+#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
|
197 |
+ |
|
198 |
+inline file_lock::file_lock(const wchar_t *name) |
|
199 |
+{ |
|
200 |
+ m_file_hnd = ipcdetail::open_existing_file(name, read_write); |
|
201 |
+ |
|
202 |
+ if(m_file_hnd == ipcdetail::invalid_file()){ |
|
203 |
+ error_info err(system_error_code()); |
|
204 |
+ throw interprocess_exception(err); |
|
205 |
+ } |
|
206 |
+} |
|
207 |
+ |
|
208 |
+#endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
|
209 |
+ |
|
210 |
+inline file_lock::~file_lock() |
|
211 |
+{ |
|
212 |
+ if(m_file_hnd != ipcdetail::invalid_file()){ |
|
213 |
+ ipcdetail::close_file(m_file_hnd); |
|
214 |
+ m_file_hnd = ipcdetail::invalid_file(); |
|
215 |
+ } |
|
216 |
+} |
|
217 |
+ |
|
218 |
+inline void file_lock::lock() |
|
219 |
+{ |
|
220 |
+ if(!ipcdetail::acquire_file_lock(m_file_hnd)){ |
|
221 |
+ error_info err(system_error_code()); |
|
222 |
+ throw interprocess_exception(err); |
|
223 |
+ } |
|
224 |
+} |
|
225 |
+ |
|
226 |
+inline bool file_lock::try_lock() |
|
227 |
+{ |
|
228 |
+ bool result; |
|
229 |
+ if(!ipcdetail::try_acquire_file_lock(m_file_hnd, result)){ |
|
230 |
+ error_info err(system_error_code()); |
|
231 |
+ throw interprocess_exception(err); |
|
232 |
+ } |
|
233 |
+ return result; |
|
234 |
+} |
|
235 |
+ |
|
236 |
+inline bool file_lock::timed_lock(const boost::posix_time::ptime &abs_time) |
|
237 |
+{ return ipcdetail::try_based_timed_lock(*this, abs_time); } |
|
238 |
+ |
|
239 |
+inline void file_lock::unlock() |
|
240 |
+{ |
|
241 |
+ if(!ipcdetail::release_file_lock(m_file_hnd)){ |
|
242 |
+ error_info err(system_error_code()); |
|
243 |
+ throw interprocess_exception(err); |
|
244 |
+ } |
|
245 |
+} |
|
246 |
+ |
|
247 |
+inline void file_lock::lock_sharable() |
|
248 |
+{ |
|
249 |
+ if(!ipcdetail::acquire_file_lock_sharable(m_file_hnd)){ |
|
250 |
+ error_info err(system_error_code()); |
|
251 |
+ throw interprocess_exception(err); |
|
252 |
+ } |
|
253 |
+} |
|
254 |
+ |
|
255 |
+inline bool file_lock::try_lock_sharable() |
|
256 |
+{ |
|
257 |
+ bool result; |
|
258 |
+ if(!ipcdetail::try_acquire_file_lock_sharable(m_file_hnd, result)){ |
|
259 |
+ error_info err(system_error_code()); |
|
260 |
+ throw interprocess_exception(err); |
|
261 |
+ } |
|
262 |
+ return result; |
|
263 |
+} |
|
264 |
+ |
|
265 |
+inline bool file_lock::timed_lock_sharable(const boost::posix_time::ptime &abs_time) |
|
266 |
+{ |
|
267 |
+ ipcdetail::lock_to_sharable<file_lock> lsh(*this); |
|
268 |
+ return ipcdetail::try_based_timed_lock(lsh, abs_time); |
|
269 |
+} |
|
270 |
+ |
|
271 |
+inline void file_lock::unlock_sharable() |
|
272 |
+{ |
|
273 |
+ if(!ipcdetail::release_file_lock_sharable(m_file_hnd)){ |
|
274 |
+ error_info err(system_error_code()); |
|
275 |
+ throw interprocess_exception(err); |
|
276 |
+ } |
|
277 |
+} |
|
278 |
+ |
|
279 |
+} //namespace interprocess { |
|
280 |
+} //namespace boost { |
|
281 |
+ |
|
282 |
+#include <boost/interprocess/detail/config_end.hpp> |
|
283 |
+ |
|
284 |
+#endif //BOOST_INTERPROCESS_FILE_LOCK_HPP |