Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Ion Gaztanaga 2010-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_ROBUST_EMULATION_HPP
|
Chris@16
|
12 #define BOOST_INTERPROCESS_ROBUST_EMULATION_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/sync/interprocess_mutex.hpp>
|
Chris@16
|
25 #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
|
Chris@16
|
26 #include <boost/interprocess/detail/atomic.hpp>
|
Chris@16
|
27 #include <boost/interprocess/detail/os_file_functions.hpp>
|
Chris@101
|
28 #include <boost/interprocess/detail/shared_dir_helpers.hpp>
|
Chris@16
|
29 #include <boost/interprocess/detail/intermodule_singleton.hpp>
|
Chris@101
|
30 #include <boost/interprocess/detail/portable_intermodule_singleton.hpp>
|
Chris@16
|
31 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
32 #include <boost/interprocess/sync/spin/wait.hpp>
|
Chris@101
|
33 #include <boost/interprocess/sync/detail/common_algorithms.hpp>
|
Chris@16
|
34 #include <string>
|
Chris@16
|
35
|
Chris@16
|
36 namespace boost{
|
Chris@16
|
37 namespace interprocess{
|
Chris@16
|
38 namespace ipcdetail{
|
Chris@16
|
39
|
Chris@16
|
40 namespace robust_emulation_helpers {
|
Chris@16
|
41
|
Chris@16
|
42 template<class T>
|
Chris@16
|
43 class mutex_traits
|
Chris@16
|
44 {
|
Chris@16
|
45 public:
|
Chris@16
|
46 static void take_ownership(T &t)
|
Chris@16
|
47 { t.take_ownership(); }
|
Chris@16
|
48 };
|
Chris@16
|
49
|
Chris@16
|
50 inline void remove_if_can_lock_file(const char *file_path)
|
Chris@16
|
51 {
|
Chris@16
|
52 file_handle_t fhnd = open_existing_file(file_path, read_write);
|
Chris@16
|
53
|
Chris@16
|
54 if(fhnd != invalid_file()){
|
Chris@16
|
55 bool acquired;
|
Chris@16
|
56 if(try_acquire_file_lock(fhnd, acquired) && acquired){
|
Chris@16
|
57 delete_file(file_path);
|
Chris@16
|
58 }
|
Chris@16
|
59 close_file(fhnd);
|
Chris@16
|
60 }
|
Chris@16
|
61 }
|
Chris@16
|
62
|
Chris@16
|
63 inline const char *robust_lock_subdir_path()
|
Chris@16
|
64 { return "robust"; }
|
Chris@16
|
65
|
Chris@16
|
66 inline const char *robust_lock_prefix()
|
Chris@16
|
67 { return "lck"; }
|
Chris@16
|
68
|
Chris@16
|
69 inline void robust_lock_path(std::string &s)
|
Chris@16
|
70 {
|
Chris@101
|
71 get_shared_dir(s);
|
Chris@16
|
72 s += "/";
|
Chris@16
|
73 s += robust_lock_subdir_path();
|
Chris@16
|
74 }
|
Chris@16
|
75
|
Chris@16
|
76 inline void create_and_get_robust_lock_file_path(std::string &s, OS_process_id_t pid)
|
Chris@16
|
77 {
|
Chris@16
|
78 intermodule_singleton_helpers::create_tmp_subdir_and_get_pid_based_filepath
|
Chris@16
|
79 (robust_lock_subdir_path(), robust_lock_prefix(), pid, s);
|
Chris@16
|
80 }
|
Chris@16
|
81
|
Chris@16
|
82 //This class will be a intermodule_singleton. The constructor will create
|
Chris@16
|
83 //a lock file, the destructor will erase it.
|
Chris@16
|
84 //
|
Chris@16
|
85 //We should take in care that another process might be erasing unlocked
|
Chris@16
|
86 //files while creating this one, so there are some race conditions we must
|
Chris@16
|
87 //take in care to guarantee some robustness.
|
Chris@16
|
88 class robust_mutex_lock_file
|
Chris@16
|
89 {
|
Chris@16
|
90 file_handle_t fd;
|
Chris@16
|
91 std::string fname;
|
Chris@16
|
92 public:
|
Chris@16
|
93 robust_mutex_lock_file()
|
Chris@16
|
94 {
|
Chris@16
|
95 permissions p;
|
Chris@16
|
96 p.set_unrestricted();
|
Chris@16
|
97 //Remove old lock files of other processes
|
Chris@16
|
98 remove_old_robust_lock_files();
|
Chris@16
|
99 //Create path and obtain lock file path for this process
|
Chris@16
|
100 create_and_get_robust_lock_file_path(fname, get_current_process_id());
|
Chris@16
|
101
|
Chris@16
|
102 //Now try to open or create the lock file
|
Chris@16
|
103 fd = create_or_open_file(fname.c_str(), read_write, p);
|
Chris@16
|
104 //If we can't open or create it, then something unrecoverable has happened
|
Chris@16
|
105 if(fd == invalid_file()){
|
Chris@16
|
106 throw interprocess_exception(other_error, "Robust emulation robust_mutex_lock_file constructor failed: could not open or create file");
|
Chris@16
|
107 }
|
Chris@16
|
108
|
Chris@16
|
109 //Now we must take in care a race condition with another process
|
Chris@16
|
110 //calling "remove_old_robust_lock_files()". No other threads from this
|
Chris@16
|
111 //process will be creating the lock file because intermodule_singleton
|
Chris@16
|
112 //guarantees this. So let's loop acquiring the lock and checking if we
|
Chris@16
|
113 //can't exclusively create the file (if the file is erased by another process
|
Chris@16
|
114 //then this exclusive open would fail). If the file can't be exclusively created
|
Chris@16
|
115 //then we have correctly open/create and lock the file. If the file can
|
Chris@16
|
116 //be exclusively created, then close previous locked file and try again.
|
Chris@16
|
117 while(1){
|
Chris@16
|
118 bool acquired;
|
Chris@16
|
119 if(!try_acquire_file_lock(fd, acquired) || !acquired ){
|
Chris@16
|
120 throw interprocess_exception(other_error, "Robust emulation robust_mutex_lock_file constructor failed: try_acquire_file_lock");
|
Chris@16
|
121 }
|
Chris@16
|
122 //Creating exclusively must fail with already_exists_error
|
Chris@16
|
123 //to make sure we've locked the file and no one has
|
Chris@16
|
124 //deleted it between creation and locking
|
Chris@16
|
125 file_handle_t fd2 = create_new_file(fname.c_str(), read_write, p);
|
Chris@16
|
126 if(fd2 != invalid_file()){
|
Chris@16
|
127 close_file(fd);
|
Chris@16
|
128 fd = fd2;
|
Chris@16
|
129 continue;
|
Chris@16
|
130 }
|
Chris@16
|
131 //If exclusive creation fails with expected error go ahead
|
Chris@16
|
132 else if(error_info(system_error_code()).get_error_code() == already_exists_error){ //must already exist
|
Chris@16
|
133 //Leak descriptor to mantain the file locked until the process dies
|
Chris@16
|
134 break;
|
Chris@16
|
135 }
|
Chris@16
|
136 //If exclusive creation fails with unexpected error throw an unrecoverable error
|
Chris@16
|
137 else{
|
Chris@16
|
138 close_file(fd);
|
Chris@16
|
139 throw interprocess_exception(other_error, "Robust emulation robust_mutex_lock_file constructor failed: create_file filed with unexpected error");
|
Chris@16
|
140 }
|
Chris@16
|
141 }
|
Chris@16
|
142 }
|
Chris@16
|
143
|
Chris@16
|
144 ~robust_mutex_lock_file()
|
Chris@16
|
145 {
|
Chris@16
|
146 //The destructor is guaranteed by intermodule_singleton to be
|
Chris@16
|
147 //executed serialized between all threads from current process,
|
Chris@16
|
148 //so we just need to close and unlink the file.
|
Chris@16
|
149 close_file(fd);
|
Chris@16
|
150 //If some other process deletes the file before us after
|
Chris@16
|
151 //closing it there should not be any problem.
|
Chris@16
|
152 delete_file(fname.c_str());
|
Chris@16
|
153 }
|
Chris@16
|
154
|
Chris@16
|
155 private:
|
Chris@16
|
156 //This functor is execute for all files in the lock file directory
|
Chris@16
|
157 class other_process_lock_remover
|
Chris@16
|
158 {
|
Chris@16
|
159 public:
|
Chris@16
|
160 void operator()(const char *filepath, const char *filename)
|
Chris@16
|
161 {
|
Chris@16
|
162 std::string pid_str;
|
Chris@16
|
163 //If the lock file is not our own lock file, then try to do the cleanup
|
Chris@16
|
164 if(!intermodule_singleton_helpers::check_if_filename_complies_with_pid
|
Chris@16
|
165 (filename, robust_lock_prefix(), get_current_process_id(), pid_str)){
|
Chris@16
|
166 remove_if_can_lock_file(filepath);
|
Chris@16
|
167 }
|
Chris@16
|
168 }
|
Chris@16
|
169 };
|
Chris@16
|
170
|
Chris@16
|
171 bool remove_old_robust_lock_files()
|
Chris@16
|
172 {
|
Chris@16
|
173 std::string refcstrRootDirectory;
|
Chris@16
|
174 robust_lock_path(refcstrRootDirectory);
|
Chris@16
|
175 return for_each_file_in_dir(refcstrRootDirectory.c_str(), other_process_lock_remover());
|
Chris@16
|
176 }
|
Chris@16
|
177 };
|
Chris@16
|
178
|
Chris@16
|
179 } //namespace robust_emulation_helpers {
|
Chris@16
|
180
|
Chris@16
|
181 //This is the mutex class. Mutex should follow mutex concept
|
Chris@16
|
182 //with an additonal "take_ownership()" function to take ownership of the
|
Chris@16
|
183 //mutex when robust_spin_mutex determines the previous owner was dead.
|
Chris@16
|
184 template<class Mutex>
|
Chris@16
|
185 class robust_spin_mutex
|
Chris@16
|
186 {
|
Chris@16
|
187 public:
|
Chris@16
|
188 static const boost::uint32_t correct_state = 0;
|
Chris@16
|
189 static const boost::uint32_t fixing_state = 1;
|
Chris@16
|
190 static const boost::uint32_t broken_state = 2;
|
Chris@16
|
191
|
Chris@16
|
192 typedef robust_emulation_helpers::mutex_traits<Mutex> mutex_traits_t;
|
Chris@16
|
193
|
Chris@16
|
194 robust_spin_mutex();
|
Chris@16
|
195 void lock();
|
Chris@16
|
196 bool try_lock();
|
Chris@16
|
197 bool timed_lock(const boost::posix_time::ptime &abs_time);
|
Chris@16
|
198 void unlock();
|
Chris@16
|
199 void consistent();
|
Chris@16
|
200 bool previous_owner_dead();
|
Chris@16
|
201
|
Chris@16
|
202 private:
|
Chris@16
|
203 static const unsigned int spin_threshold = 100u;
|
Chris@16
|
204 bool lock_own_unique_file();
|
Chris@16
|
205 bool robust_check();
|
Chris@16
|
206 bool check_if_owner_dead_and_take_ownership_atomically();
|
Chris@16
|
207 bool is_owner_dead(boost::uint32_t own);
|
Chris@16
|
208 void owner_to_filename(boost::uint32_t own, std::string &s);
|
Chris@16
|
209 //The real mutex
|
Chris@16
|
210 Mutex mtx;
|
Chris@16
|
211 //The pid of the owner
|
Chris@16
|
212 volatile boost::uint32_t owner;
|
Chris@16
|
213 //The state of the mutex (correct, fixing, broken)
|
Chris@16
|
214 volatile boost::uint32_t state;
|
Chris@16
|
215 };
|
Chris@16
|
216
|
Chris@16
|
217 template<class Mutex>
|
Chris@16
|
218 inline robust_spin_mutex<Mutex>::robust_spin_mutex()
|
Chris@16
|
219 : mtx(), owner(get_invalid_process_id()), state(correct_state)
|
Chris@16
|
220 {}
|
Chris@16
|
221
|
Chris@16
|
222 template<class Mutex>
|
Chris@16
|
223 inline void robust_spin_mutex<Mutex>::lock()
|
Chris@101
|
224 { try_based_lock(*this); }
|
Chris@16
|
225
|
Chris@16
|
226 template<class Mutex>
|
Chris@16
|
227 inline bool robust_spin_mutex<Mutex>::try_lock()
|
Chris@16
|
228 {
|
Chris@16
|
229 //Same as lock() but without spinning
|
Chris@16
|
230 if(atomic_read32(&this->state) == broken_state){
|
Chris@16
|
231 throw interprocess_exception(lock_error, "Broken id");
|
Chris@16
|
232 }
|
Chris@16
|
233
|
Chris@16
|
234 if(!this->lock_own_unique_file()){
|
Chris@16
|
235 throw interprocess_exception(lock_error, "Broken id");
|
Chris@16
|
236 }
|
Chris@16
|
237
|
Chris@16
|
238 if (mtx.try_lock()){
|
Chris@16
|
239 atomic_write32(&this->owner, get_current_process_id());
|
Chris@16
|
240 return true;
|
Chris@16
|
241 }
|
Chris@16
|
242 else{
|
Chris@16
|
243 if(!this->robust_check()){
|
Chris@16
|
244 return false;
|
Chris@16
|
245 }
|
Chris@16
|
246 else{
|
Chris@16
|
247 return true;
|
Chris@16
|
248 }
|
Chris@16
|
249 }
|
Chris@16
|
250 }
|
Chris@16
|
251
|
Chris@16
|
252 template<class Mutex>
|
Chris@16
|
253 inline bool robust_spin_mutex<Mutex>::timed_lock
|
Chris@16
|
254 (const boost::posix_time::ptime &abs_time)
|
Chris@101
|
255 { return try_based_timed_lock(*this, abs_time); }
|
Chris@16
|
256
|
Chris@16
|
257 template<class Mutex>
|
Chris@16
|
258 inline void robust_spin_mutex<Mutex>::owner_to_filename(boost::uint32_t own, std::string &s)
|
Chris@16
|
259 {
|
Chris@16
|
260 robust_emulation_helpers::create_and_get_robust_lock_file_path(s, own);
|
Chris@16
|
261 }
|
Chris@16
|
262
|
Chris@16
|
263 template<class Mutex>
|
Chris@16
|
264 inline bool robust_spin_mutex<Mutex>::robust_check()
|
Chris@16
|
265 {
|
Chris@16
|
266 //If the old owner was dead, and we've acquired ownership, mark
|
Chris@16
|
267 //the mutex as 'fixing'. This means that a "consistent()" is needed
|
Chris@16
|
268 //to avoid marking the mutex as "broken" when the mutex is unlocked.
|
Chris@16
|
269 if(!this->check_if_owner_dead_and_take_ownership_atomically()){
|
Chris@16
|
270 return false;
|
Chris@16
|
271 }
|
Chris@16
|
272 atomic_write32(&this->state, fixing_state);
|
Chris@16
|
273 return true;
|
Chris@16
|
274 }
|
Chris@16
|
275
|
Chris@16
|
276 template<class Mutex>
|
Chris@16
|
277 inline bool robust_spin_mutex<Mutex>::check_if_owner_dead_and_take_ownership_atomically()
|
Chris@16
|
278 {
|
Chris@16
|
279 boost::uint32_t cur_owner = get_current_process_id();
|
Chris@16
|
280 boost::uint32_t old_owner = atomic_read32(&this->owner), old_owner2;
|
Chris@16
|
281 //The cas loop guarantees that only one thread from this or another process
|
Chris@16
|
282 //will succeed taking ownership
|
Chris@16
|
283 do{
|
Chris@16
|
284 //Check if owner is dead
|
Chris@16
|
285 if(!this->is_owner_dead(old_owner)){
|
Chris@16
|
286 return false;
|
Chris@16
|
287 }
|
Chris@16
|
288 //If it's dead, try to mark this process as the owner in the owner field
|
Chris@16
|
289 old_owner2 = old_owner;
|
Chris@16
|
290 old_owner = atomic_cas32(&this->owner, cur_owner, old_owner);
|
Chris@16
|
291 }while(old_owner2 != old_owner);
|
Chris@16
|
292 //If success, we fix mutex internals to assure our ownership
|
Chris@16
|
293 mutex_traits_t::take_ownership(mtx);
|
Chris@16
|
294 return true;
|
Chris@16
|
295 }
|
Chris@16
|
296
|
Chris@16
|
297 template<class Mutex>
|
Chris@16
|
298 inline bool robust_spin_mutex<Mutex>::is_owner_dead(boost::uint32_t own)
|
Chris@16
|
299 {
|
Chris@16
|
300 //If owner is an invalid id, then it's clear it's dead
|
Chris@16
|
301 if(own == (boost::uint32_t)get_invalid_process_id()){
|
Chris@16
|
302 return true;
|
Chris@16
|
303 }
|
Chris@16
|
304
|
Chris@16
|
305 //Obtain the lock filename of the owner field
|
Chris@16
|
306 std::string file;
|
Chris@16
|
307 this->owner_to_filename(own, file);
|
Chris@16
|
308
|
Chris@16
|
309 //Now the logic is to open and lock it
|
Chris@16
|
310 file_handle_t fhnd = open_existing_file(file.c_str(), read_write);
|
Chris@16
|
311
|
Chris@16
|
312 if(fhnd != invalid_file()){
|
Chris@16
|
313 //If we can open the file, lock it.
|
Chris@16
|
314 bool acquired;
|
Chris@16
|
315 if(try_acquire_file_lock(fhnd, acquired) && acquired){
|
Chris@16
|
316 //If locked, just delete the file
|
Chris@16
|
317 delete_file(file.c_str());
|
Chris@16
|
318 close_file(fhnd);
|
Chris@16
|
319 return true;
|
Chris@16
|
320 }
|
Chris@16
|
321 //If not locked, the owner is suppossed to be still alive
|
Chris@16
|
322 close_file(fhnd);
|
Chris@16
|
323 }
|
Chris@16
|
324 else{
|
Chris@16
|
325 //If the lock file does not exist then the owner is dead (a previous cleanup)
|
Chris@16
|
326 //function has deleted the file. If there is another reason, then this is
|
Chris@16
|
327 //an unrecoverable error
|
Chris@16
|
328 if(error_info(system_error_code()).get_error_code() == not_found_error){
|
Chris@16
|
329 return true;
|
Chris@16
|
330 }
|
Chris@16
|
331 }
|
Chris@16
|
332 return false;
|
Chris@16
|
333 }
|
Chris@16
|
334
|
Chris@16
|
335 template<class Mutex>
|
Chris@16
|
336 inline void robust_spin_mutex<Mutex>::consistent()
|
Chris@16
|
337 {
|
Chris@16
|
338 //This function supposes the previous state was "fixing"
|
Chris@16
|
339 //and the current process holds the mutex
|
Chris@16
|
340 if(atomic_read32(&this->state) != fixing_state &&
|
Chris@16
|
341 atomic_read32(&this->owner) != (boost::uint32_t)get_current_process_id()){
|
Chris@16
|
342 throw interprocess_exception(lock_error, "Broken id");
|
Chris@16
|
343 }
|
Chris@16
|
344 //If that's the case, just update mutex state
|
Chris@16
|
345 atomic_write32(&this->state, correct_state);
|
Chris@16
|
346 }
|
Chris@16
|
347
|
Chris@16
|
348 template<class Mutex>
|
Chris@16
|
349 inline bool robust_spin_mutex<Mutex>::previous_owner_dead()
|
Chris@16
|
350 {
|
Chris@16
|
351 //Notifies if a owner recovery has been performed in the last lock()
|
Chris@16
|
352 return atomic_read32(&this->state) == fixing_state;
|
Chris@101
|
353 }
|
Chris@16
|
354
|
Chris@16
|
355 template<class Mutex>
|
Chris@16
|
356 inline void robust_spin_mutex<Mutex>::unlock()
|
Chris@16
|
357 {
|
Chris@16
|
358 //If in "fixing" state, unlock and mark the mutex as unrecoverable
|
Chris@16
|
359 //so next locks will fail and all threads will be notified that the
|
Chris@16
|
360 //data protected by the mutex was not recoverable.
|
Chris@16
|
361 if(atomic_read32(&this->state) == fixing_state){
|
Chris@16
|
362 atomic_write32(&this->state, broken_state);
|
Chris@16
|
363 }
|
Chris@16
|
364 //Write an invalid owner to minimize pid reuse possibility
|
Chris@16
|
365 atomic_write32(&this->owner, get_invalid_process_id());
|
Chris@16
|
366 mtx.unlock();
|
Chris@16
|
367 }
|
Chris@16
|
368
|
Chris@16
|
369 template<class Mutex>
|
Chris@16
|
370 inline bool robust_spin_mutex<Mutex>::lock_own_unique_file()
|
Chris@16
|
371 {
|
Chris@16
|
372 //This function forces instantiation of the singleton
|
Chris@16
|
373 robust_emulation_helpers::robust_mutex_lock_file* dummy =
|
Chris@16
|
374 &ipcdetail::intermodule_singleton
|
Chris@16
|
375 <robust_emulation_helpers::robust_mutex_lock_file>::get();
|
Chris@16
|
376 return dummy != 0;
|
Chris@16
|
377 }
|
Chris@16
|
378
|
Chris@16
|
379 } //namespace ipcdetail{
|
Chris@16
|
380 } //namespace interprocess{
|
Chris@16
|
381 } //namespace boost{
|
Chris@16
|
382
|
Chris@16
|
383 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
384
|
Chris@16
|
385 #endif
|