annotate DEPENDENCIES/generic/include/boost/interprocess/detail/robust_emulation.hpp @ 16:2665513ce2d3

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