file_lock.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. #include <boost/interprocess/exceptions.hpp>
  22. #include <boost/interprocess/timed_utils.hpp>
  23. #include <boost/interprocess/detail/os_file_functions.hpp>
  24. #include <boost/interprocess/detail/os_thread_functions.hpp>
  25. #include <boost/interprocess/sync/detail/common_algorithms.hpp>
  26. #include <boost/interprocess/sync/detail/locks.hpp>
  27. #include <boost/move/utility_core.hpp>
  28. //!\file
  29. //!Describes a class that wraps file locking capabilities.
  30. namespace boost {
  31. namespace interprocess {
  32. //!A file lock, is a mutual exclusion utility similar to a mutex using a
  33. //!file. A file lock has sharable and exclusive locking capabilities and
  34. //!can be used with scoped_lock and sharable_lock classes.
  35. //!A file lock can't guarantee synchronization between threads of the same
  36. //!process so just use file locks to synchronize threads from different processes.
  37. class file_lock
  38. {
  39. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  40. //Non-copyable
  41. BOOST_MOVABLE_BUT_NOT_COPYABLE(file_lock)
  42. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  43. public:
  44. //!Constructs an empty file mapping.
  45. //!Does not throw
  46. file_lock() BOOST_NOEXCEPT
  47. : m_file_hnd(file_handle_t(ipcdetail::invalid_file()))
  48. {}
  49. //!Opens a file lock. Throws interprocess_exception if the file does not
  50. //!exist or there are no operating system resources.
  51. file_lock(const char *name);
  52. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  53. //!Opens a file lock. Throws interprocess_exception if the file does not
  54. //!exist or there are no operating system resources.
  55. //!
  56. //!Note: This function is only available on operating systems with
  57. //! native wchar_t APIs (e.g. Windows).
  58. file_lock(const wchar_t *name);
  59. #endif
  60. //!Moves the ownership of "moved"'s file mapping object to *this.
  61. //!After the call, "moved" does not represent any file mapping object.
  62. //!Does not throw
  63. file_lock(BOOST_RV_REF(file_lock) moved) BOOST_NOEXCEPT
  64. : m_file_hnd(file_handle_t(ipcdetail::invalid_file()))
  65. { this->swap(moved); }
  66. //!Moves the ownership of "moved"'s file mapping to *this.
  67. //!After the call, "moved" does not represent any file mapping.
  68. //!Does not throw
  69. file_lock &operator=(BOOST_RV_REF(file_lock) moved) BOOST_NOEXCEPT
  70. {
  71. file_lock tmp(boost::move(moved));
  72. this->swap(tmp);
  73. return *this;
  74. }
  75. //!Closes a file lock. Does not throw.
  76. ~file_lock();
  77. //!Swaps two file_locks.
  78. //!Does not throw.
  79. void swap(file_lock &other) BOOST_NOEXCEPT
  80. {
  81. file_handle_t tmp = m_file_hnd;
  82. m_file_hnd = other.m_file_hnd;
  83. other.m_file_hnd = tmp;
  84. }
  85. //Exclusive locking
  86. //!Requires: The calling thread does not own the mutex.
  87. //!
  88. //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
  89. //! and if another thread has exclusive, or sharable ownership of
  90. //! the mutex, it waits until it can obtain the ownership.
  91. //!Throws: interprocess_exception on error.
  92. //!
  93. //!Note: A program may deadlock if the thread that has ownership calls
  94. //! this function. If the implementation can detect the deadlock,
  95. //! an exception could be thrown.
  96. void lock();
  97. //!Requires: The calling thread does not own the mutex.
  98. //!
  99. //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
  100. //! without waiting. If no other thread has exclusive, or sharable
  101. //! ownership of the mutex this succeeds.
  102. //!Returns: If it can acquire exclusive ownership immediately returns true.
  103. //! If it has to wait, returns false.
  104. //!Throws: interprocess_exception on error.
  105. //!
  106. //!Note: A program may deadlock if the thread that has ownership calls
  107. //! this function. If the implementation can detect the deadlock,
  108. //! an exception could be thrown.
  109. bool try_lock();
  110. //!Requires: The calling thread does not own the mutex.
  111. //!
  112. //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
  113. //! waiting if necessary until no other thread has exclusive, or sharable
  114. //! ownership of the mutex or abs_time is reached.
  115. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  116. //!Throws: interprocess_exception on error.
  117. //!
  118. //!Note: A program may deadlock if the thread that has ownership calls
  119. //! this function. If the implementation can detect the deadlock,
  120. //! an exception could be thrown.
  121. template<class TimePoint>
  122. bool timed_lock(const TimePoint &abs_time);
  123. //!Same as `timed_lock`, but this function is modeled after the
  124. //!standard library interface.
  125. template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
  126. { return this->timed_lock(abs_time); }
  127. //!Same as `timed_lock`, but this function is modeled after the
  128. //!standard library interface.
  129. template<class Duration> bool try_lock_for(const Duration &dur)
  130. { return this->timed_lock(ipcdetail::duration_to_ustime(dur)); }
  131. //!Precondition: The thread must have exclusive ownership of the mutex.
  132. //!Effects: The calling thread releases the exclusive ownership of the mutex.
  133. //!Throws: An exception derived from interprocess_exception on error.
  134. void unlock();
  135. //Sharable locking
  136. //!Requires: The calling thread does not own the mutex.
  137. //!
  138. //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
  139. //! and if another thread has exclusive ownership of the mutex, waits until
  140. //! it can obtain the ownership.
  141. //!Throws: interprocess_exception on error.
  142. //!
  143. //!Note: A program may deadlock if the thread that owns a mutex object calls
  144. //! this function. If the implementation can detect the deadlock,
  145. //! an exception could be thrown.
  146. void lock_sharable();
  147. //!Same as `lock_sharable` but with a std-compatible interface
  148. //!
  149. void lock_shared()
  150. { this->lock_sharable(); }
  151. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  152. //! without waiting. If no other thread has exclusive ownership of the
  153. //! mutex this succeeds.
  154. //!Returns: If it can acquire sharable ownership immediately returns true. If it
  155. //! has to wait, returns false.
  156. //!Throws: interprocess_exception on error.
  157. bool try_lock_sharable();
  158. //!Same as `try_lock_sharable` but with a std-compatible interface
  159. //!
  160. bool try_lock_shared()
  161. { return this->try_lock_sharable(); }
  162. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  163. //! waiting if necessary until no other thread has exclusive ownership of
  164. //! the mutex or abs_time is reached.
  165. //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
  166. //!Throws: interprocess_exception on error.
  167. template<class TimePoint>
  168. bool timed_lock_sharable(const TimePoint &abs_time);
  169. //!Same as `timed_lock_sharable`, but this function is modeled after the
  170. //!standard library interface.
  171. template<class TimePoint> bool try_lock_shared_until(const TimePoint &abs_time)
  172. { return this->timed_lock_sharable(abs_time); }
  173. //!Same as `timed_lock_sharable`, but this function is modeled after the
  174. //!standard library interface.
  175. template<class Duration> bool try_lock_shared_for(const Duration &dur)
  176. { return this->timed_lock_sharable(ipcdetail::duration_to_ustime(dur)); }
  177. //!Precondition: The thread must have sharable ownership of the mutex.
  178. //!Effects: The calling thread releases the sharable ownership of the mutex.
  179. //!Throws: An exception derived from interprocess_exception on error.
  180. void unlock_sharable();
  181. //!Same as `unlock_sharable` but with a std-compatible interface
  182. //!
  183. void unlock_shared()
  184. { this->unlock_sharable(); }
  185. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  186. private:
  187. file_handle_t m_file_hnd;
  188. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  189. };
  190. inline file_lock::file_lock(const char *name)
  191. {
  192. m_file_hnd = ipcdetail::open_existing_file(name, read_write);
  193. if(m_file_hnd == ipcdetail::invalid_file()){
  194. error_info err(system_error_code());
  195. throw interprocess_exception(err);
  196. }
  197. }
  198. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  199. inline file_lock::file_lock(const wchar_t *name)
  200. {
  201. m_file_hnd = ipcdetail::open_existing_file(name, read_write);
  202. if(m_file_hnd == ipcdetail::invalid_file()){
  203. error_info err(system_error_code());
  204. throw interprocess_exception(err);
  205. }
  206. }
  207. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  208. inline file_lock::~file_lock()
  209. {
  210. if(m_file_hnd != ipcdetail::invalid_file()){
  211. ipcdetail::close_file(m_file_hnd);
  212. m_file_hnd = ipcdetail::invalid_file();
  213. }
  214. }
  215. inline void file_lock::lock()
  216. {
  217. if(!ipcdetail::acquire_file_lock(m_file_hnd)){
  218. error_info err(system_error_code());
  219. throw interprocess_exception(err);
  220. }
  221. }
  222. inline bool file_lock::try_lock()
  223. {
  224. bool result;
  225. if(!ipcdetail::try_acquire_file_lock(m_file_hnd, result)){
  226. error_info err(system_error_code());
  227. throw interprocess_exception(err);
  228. }
  229. return result;
  230. }
  231. template<class TimePoint>
  232. inline bool file_lock::timed_lock(const TimePoint &abs_time)
  233. { return ipcdetail::try_based_timed_lock(*this, abs_time); }
  234. inline void file_lock::unlock()
  235. {
  236. if(!ipcdetail::release_file_lock(m_file_hnd)){
  237. error_info err(system_error_code());
  238. throw interprocess_exception(err);
  239. }
  240. }
  241. inline void file_lock::lock_sharable()
  242. {
  243. if(!ipcdetail::acquire_file_lock_sharable(m_file_hnd)){
  244. error_info err(system_error_code());
  245. throw interprocess_exception(err);
  246. }
  247. }
  248. inline bool file_lock::try_lock_sharable()
  249. {
  250. bool result;
  251. if(!ipcdetail::try_acquire_file_lock_sharable(m_file_hnd, result)){
  252. error_info err(system_error_code());
  253. throw interprocess_exception(err);
  254. }
  255. return result;
  256. }
  257. template<class TimePoint>
  258. inline bool file_lock::timed_lock_sharable(const TimePoint &abs_time)
  259. {
  260. ipcdetail::lock_to_sharable<file_lock> lsh(*this);
  261. return ipcdetail::try_based_timed_lock(lsh, abs_time);
  262. }
  263. inline void file_lock::unlock_sharable()
  264. {
  265. if(!ipcdetail::release_file_lock_sharable(m_file_hnd)){
  266. error_info err(system_error_code());
  267. throw interprocess_exception(err);
  268. }
  269. }
  270. } //namespace interprocess {
  271. } //namespace boost {
  272. #include <boost/interprocess/detail/config_end.hpp>
  273. #endif //BOOST_INTERPROCESS_FILE_LOCK_HPP