file_lock.hpp 11 KB

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