file_lock.hpp 9.7 KB

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