Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
|
Chris@16
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 //
|
Chris@16
|
7 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
8 //
|
Chris@16
|
9 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_INTERPROCESS_FILE_LOCK_HPP
|
Chris@16
|
12 #define BOOST_INTERPROCESS_FILE_LOCK_HPP
|
Chris@16
|
13
|
Chris@101
|
14 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
15 # include <boost/config.hpp>
|
Chris@101
|
16 #endif
|
Chris@101
|
17 #
|
Chris@101
|
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@16
|
19 # pragma once
|
Chris@16
|
20 #endif
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
23 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
24 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
25 #include <boost/interprocess/detail/os_file_functions.hpp>
|
Chris@16
|
26 #include <boost/interprocess/detail/os_thread_functions.hpp>
|
Chris@16
|
27 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
Chris@101
|
28 #include <boost/interprocess/sync/detail/common_algorithms.hpp>
|
Chris@101
|
29 #include <boost/interprocess/sync/detail/locks.hpp>
|
Chris@101
|
30 #include <boost/move/utility_core.hpp>
|
Chris@16
|
31
|
Chris@16
|
32 //!\file
|
Chris@16
|
33 //!Describes a class that wraps file locking capabilities.
|
Chris@16
|
34
|
Chris@16
|
35 namespace boost {
|
Chris@16
|
36 namespace interprocess {
|
Chris@16
|
37
|
Chris@16
|
38
|
Chris@16
|
39 //!A file lock, is a mutual exclusion utility similar to a mutex using a
|
Chris@16
|
40 //!file. A file lock has sharable and exclusive locking capabilities and
|
Chris@16
|
41 //!can be used with scoped_lock and sharable_lock classes.
|
Chris@16
|
42 //!A file lock can't guarantee synchronization between threads of the same
|
Chris@16
|
43 //!process so just use file locks to synchronize threads from different processes.
|
Chris@16
|
44 class file_lock
|
Chris@16
|
45 {
|
Chris@101
|
46 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
47 //Non-copyable
|
Chris@16
|
48 BOOST_MOVABLE_BUT_NOT_COPYABLE(file_lock)
|
Chris@101
|
49 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
50
|
Chris@16
|
51 public:
|
Chris@16
|
52 //!Constructs an empty file mapping.
|
Chris@16
|
53 //!Does not throw
|
Chris@16
|
54 file_lock()
|
Chris@16
|
55 : m_file_hnd(file_handle_t(ipcdetail::invalid_file()))
|
Chris@16
|
56 {}
|
Chris@16
|
57
|
Chris@16
|
58 //!Opens a file lock. Throws interprocess_exception if the file does not
|
Chris@16
|
59 //!exist or there are no operating system resources.
|
Chris@16
|
60 file_lock(const char *name);
|
Chris@16
|
61
|
Chris@16
|
62 //!Moves the ownership of "moved"'s file mapping object to *this.
|
Chris@16
|
63 //!After the call, "moved" does not represent any file mapping object.
|
Chris@16
|
64 //!Does not throw
|
Chris@16
|
65 file_lock(BOOST_RV_REF(file_lock) moved)
|
Chris@16
|
66 : m_file_hnd(file_handle_t(ipcdetail::invalid_file()))
|
Chris@16
|
67 { this->swap(moved); }
|
Chris@16
|
68
|
Chris@16
|
69 //!Moves the ownership of "moved"'s file mapping to *this.
|
Chris@16
|
70 //!After the call, "moved" does not represent any file mapping.
|
Chris@16
|
71 //!Does not throw
|
Chris@16
|
72 file_lock &operator=(BOOST_RV_REF(file_lock) moved)
|
Chris@16
|
73 {
|
Chris@16
|
74 file_lock tmp(boost::move(moved));
|
Chris@16
|
75 this->swap(tmp);
|
Chris@16
|
76 return *this;
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 //!Closes a file lock. Does not throw.
|
Chris@16
|
80 ~file_lock();
|
Chris@16
|
81
|
Chris@16
|
82 //!Swaps two file_locks.
|
Chris@16
|
83 //!Does not throw.
|
Chris@16
|
84 void swap(file_lock &other)
|
Chris@16
|
85 {
|
Chris@16
|
86 file_handle_t tmp = m_file_hnd;
|
Chris@16
|
87 m_file_hnd = other.m_file_hnd;
|
Chris@16
|
88 other.m_file_hnd = tmp;
|
Chris@16
|
89 }
|
Chris@16
|
90
|
Chris@16
|
91 //Exclusive locking
|
Chris@16
|
92
|
Chris@16
|
93 //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
|
Chris@16
|
94 //! and if another thread has exclusive, or sharable ownership of
|
Chris@16
|
95 //! the mutex, it waits until it can obtain the ownership.
|
Chris@16
|
96 //!Throws: interprocess_exception on error.
|
Chris@16
|
97 void lock();
|
Chris@16
|
98
|
Chris@16
|
99 //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
|
Chris@16
|
100 //! without waiting. If no other thread has exclusive, or sharable
|
Chris@16
|
101 //! ownership of the mutex this succeeds.
|
Chris@16
|
102 //!Returns: If it can acquire exclusive ownership immediately returns true.
|
Chris@16
|
103 //! If it has to wait, returns false.
|
Chris@16
|
104 //!Throws: interprocess_exception on error.
|
Chris@16
|
105 bool try_lock();
|
Chris@16
|
106
|
Chris@16
|
107 //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
|
Chris@16
|
108 //! waiting if necessary until no other thread has exclusive, or sharable
|
Chris@16
|
109 //! ownership of the mutex or abs_time is reached.
|
Chris@16
|
110 //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
|
Chris@16
|
111 //!Throws: interprocess_exception on error.
|
Chris@16
|
112 bool timed_lock(const boost::posix_time::ptime &abs_time);
|
Chris@16
|
113
|
Chris@16
|
114 //!Precondition: The thread must have exclusive ownership of the mutex.
|
Chris@16
|
115 //!Effects: The calling thread releases the exclusive ownership of the mutex.
|
Chris@16
|
116 //!Throws: An exception derived from interprocess_exception on error.
|
Chris@16
|
117 void unlock();
|
Chris@16
|
118
|
Chris@16
|
119 //Sharable locking
|
Chris@16
|
120
|
Chris@16
|
121 //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
|
Chris@16
|
122 //! and if another thread has exclusive ownership of the mutex, waits until
|
Chris@16
|
123 //! it can obtain the ownership.
|
Chris@16
|
124 //!Throws: interprocess_exception on error.
|
Chris@16
|
125 void lock_sharable();
|
Chris@16
|
126
|
Chris@16
|
127 //!Effects: The calling thread tries to acquire sharable ownership of the mutex
|
Chris@16
|
128 //! without waiting. If no other thread has exclusive ownership of the
|
Chris@16
|
129 //! mutex this succeeds.
|
Chris@16
|
130 //!Returns: If it can acquire sharable ownership immediately returns true. If it
|
Chris@16
|
131 //! has to wait, returns false.
|
Chris@16
|
132 //!Throws: interprocess_exception on error.
|
Chris@16
|
133 bool try_lock_sharable();
|
Chris@16
|
134
|
Chris@16
|
135 //!Effects: The calling thread tries to acquire sharable ownership of the mutex
|
Chris@16
|
136 //! waiting if necessary until no other thread has exclusive ownership of
|
Chris@16
|
137 //! the mutex or abs_time is reached.
|
Chris@16
|
138 //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
|
Chris@16
|
139 //!Throws: interprocess_exception on error.
|
Chris@16
|
140 bool timed_lock_sharable(const boost::posix_time::ptime &abs_time);
|
Chris@16
|
141
|
Chris@16
|
142 //!Precondition: The thread must have sharable ownership of the mutex.
|
Chris@16
|
143 //!Effects: The calling thread releases the sharable ownership of the mutex.
|
Chris@16
|
144 //!Throws: An exception derived from interprocess_exception on error.
|
Chris@16
|
145 void unlock_sharable();
|
Chris@101
|
146 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
147 private:
|
Chris@16
|
148 file_handle_t m_file_hnd;
|
Chris@16
|
149
|
Chris@101
|
150 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
151 };
|
Chris@16
|
152
|
Chris@16
|
153 inline file_lock::file_lock(const char *name)
|
Chris@16
|
154 {
|
Chris@16
|
155 m_file_hnd = ipcdetail::open_existing_file(name, read_write);
|
Chris@16
|
156
|
Chris@16
|
157 if(m_file_hnd == ipcdetail::invalid_file()){
|
Chris@16
|
158 error_info err(system_error_code());
|
Chris@16
|
159 throw interprocess_exception(err);
|
Chris@16
|
160 }
|
Chris@16
|
161 }
|
Chris@16
|
162
|
Chris@16
|
163 inline file_lock::~file_lock()
|
Chris@16
|
164 {
|
Chris@16
|
165 if(m_file_hnd != ipcdetail::invalid_file()){
|
Chris@16
|
166 ipcdetail::close_file(m_file_hnd);
|
Chris@16
|
167 m_file_hnd = ipcdetail::invalid_file();
|
Chris@16
|
168 }
|
Chris@16
|
169 }
|
Chris@16
|
170
|
Chris@16
|
171 inline void file_lock::lock()
|
Chris@16
|
172 {
|
Chris@16
|
173 if(!ipcdetail::acquire_file_lock(m_file_hnd)){
|
Chris@16
|
174 error_info err(system_error_code());
|
Chris@16
|
175 throw interprocess_exception(err);
|
Chris@16
|
176 }
|
Chris@16
|
177 }
|
Chris@16
|
178
|
Chris@16
|
179 inline bool file_lock::try_lock()
|
Chris@16
|
180 {
|
Chris@16
|
181 bool result;
|
Chris@16
|
182 if(!ipcdetail::try_acquire_file_lock(m_file_hnd, result)){
|
Chris@16
|
183 error_info err(system_error_code());
|
Chris@16
|
184 throw interprocess_exception(err);
|
Chris@16
|
185 }
|
Chris@16
|
186 return result;
|
Chris@16
|
187 }
|
Chris@16
|
188
|
Chris@16
|
189 inline bool file_lock::timed_lock(const boost::posix_time::ptime &abs_time)
|
Chris@101
|
190 { return ipcdetail::try_based_timed_lock(*this, abs_time); }
|
Chris@16
|
191
|
Chris@16
|
192 inline void file_lock::unlock()
|
Chris@16
|
193 {
|
Chris@16
|
194 if(!ipcdetail::release_file_lock(m_file_hnd)){
|
Chris@16
|
195 error_info err(system_error_code());
|
Chris@16
|
196 throw interprocess_exception(err);
|
Chris@16
|
197 }
|
Chris@16
|
198 }
|
Chris@16
|
199
|
Chris@16
|
200 inline void file_lock::lock_sharable()
|
Chris@16
|
201 {
|
Chris@16
|
202 if(!ipcdetail::acquire_file_lock_sharable(m_file_hnd)){
|
Chris@16
|
203 error_info err(system_error_code());
|
Chris@16
|
204 throw interprocess_exception(err);
|
Chris@16
|
205 }
|
Chris@16
|
206 }
|
Chris@16
|
207
|
Chris@16
|
208 inline bool file_lock::try_lock_sharable()
|
Chris@16
|
209 {
|
Chris@16
|
210 bool result;
|
Chris@16
|
211 if(!ipcdetail::try_acquire_file_lock_sharable(m_file_hnd, result)){
|
Chris@16
|
212 error_info err(system_error_code());
|
Chris@16
|
213 throw interprocess_exception(err);
|
Chris@16
|
214 }
|
Chris@16
|
215 return result;
|
Chris@16
|
216 }
|
Chris@16
|
217
|
Chris@16
|
218 inline bool file_lock::timed_lock_sharable(const boost::posix_time::ptime &abs_time)
|
Chris@16
|
219 {
|
Chris@101
|
220 ipcdetail::lock_to_sharable<file_lock> lsh(*this);
|
Chris@101
|
221 return ipcdetail::try_based_timed_lock(lsh, abs_time);
|
Chris@16
|
222 }
|
Chris@16
|
223
|
Chris@16
|
224 inline void file_lock::unlock_sharable()
|
Chris@16
|
225 {
|
Chris@16
|
226 if(!ipcdetail::release_file_lock_sharable(m_file_hnd)){
|
Chris@16
|
227 error_info err(system_error_code());
|
Chris@16
|
228 throw interprocess_exception(err);
|
Chris@16
|
229 }
|
Chris@16
|
230 }
|
Chris@16
|
231
|
Chris@16
|
232 } //namespace interprocess {
|
Chris@16
|
233 } //namespace boost {
|
Chris@16
|
234
|
Chris@16
|
235 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
236
|
Chris@16
|
237 #endif //BOOST_INTERPROCESS_FILE_LOCK_HPP
|