file_lock.hpp 9.4 KB

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