robust_emulation.hpp 14 KB

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