file_lock.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/detail/posix_time_types_wrk.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. bool timed_lock(const boost::posix_time::ptime &abs_time);
  122. //!Precondition: The thread must have exclusive ownership of the mutex.
  123. //!Effects: The calling thread releases the exclusive ownership of the mutex.
  124. //!Throws: An exception derived from interprocess_exception on error.
  125. void unlock();
  126. //Sharable locking
  127. //!Requires: The calling thread does not own the mutex.
  128. //!
  129. //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
  130. //! and if another thread has exclusive ownership of the mutex, waits until
  131. //! it can obtain the ownership.
  132. //!Throws: interprocess_exception on error.
  133. //!
  134. //!Note: A program may deadlock if the thread that owns a mutex object calls
  135. //! this function. If the implementation can detect the deadlock,
  136. //! an exception could be thrown.
  137. void lock_sharable();
  138. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  139. //! without waiting. If no other thread has exclusive ownership of the
  140. //! mutex this succeeds.
  141. //!Returns: If it can acquire sharable ownership immediately returns true. If it
  142. //! has to wait, returns false.
  143. //!Throws: interprocess_exception on error.
  144. bool try_lock_sharable();
  145. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  146. //! waiting if necessary until no other thread has exclusive ownership of
  147. //! the mutex or abs_time is reached.
  148. //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
  149. //!Throws: interprocess_exception on error.
  150. bool timed_lock_sharable(const boost::posix_time::ptime &abs_time);
  151. //!Precondition: The thread must have sharable ownership of the mutex.
  152. //!Effects: The calling thread releases the sharable ownership of the mutex.
  153. //!Throws: An exception derived from interprocess_exception on error.
  154. void unlock_sharable();
  155. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  156. private:
  157. file_handle_t m_file_hnd;
  158. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  159. };
  160. inline file_lock::file_lock(const char *name)
  161. {
  162. m_file_hnd = ipcdetail::open_existing_file(name, read_write);
  163. if(m_file_hnd == ipcdetail::invalid_file()){
  164. error_info err(system_error_code());
  165. throw interprocess_exception(err);
  166. }
  167. }
  168. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  169. inline file_lock::file_lock(const wchar_t *name)
  170. {
  171. m_file_hnd = ipcdetail::open_existing_file(name, read_write);
  172. if(m_file_hnd == ipcdetail::invalid_file()){
  173. error_info err(system_error_code());
  174. throw interprocess_exception(err);
  175. }
  176. }
  177. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  178. inline file_lock::~file_lock()
  179. {
  180. if(m_file_hnd != ipcdetail::invalid_file()){
  181. ipcdetail::close_file(m_file_hnd);
  182. m_file_hnd = ipcdetail::invalid_file();
  183. }
  184. }
  185. inline void file_lock::lock()
  186. {
  187. if(!ipcdetail::acquire_file_lock(m_file_hnd)){
  188. error_info err(system_error_code());
  189. throw interprocess_exception(err);
  190. }
  191. }
  192. inline bool file_lock::try_lock()
  193. {
  194. bool result;
  195. if(!ipcdetail::try_acquire_file_lock(m_file_hnd, result)){
  196. error_info err(system_error_code());
  197. throw interprocess_exception(err);
  198. }
  199. return result;
  200. }
  201. inline bool file_lock::timed_lock(const boost::posix_time::ptime &abs_time)
  202. { return ipcdetail::try_based_timed_lock(*this, abs_time); }
  203. inline void file_lock::unlock()
  204. {
  205. if(!ipcdetail::release_file_lock(m_file_hnd)){
  206. error_info err(system_error_code());
  207. throw interprocess_exception(err);
  208. }
  209. }
  210. inline void file_lock::lock_sharable()
  211. {
  212. if(!ipcdetail::acquire_file_lock_sharable(m_file_hnd)){
  213. error_info err(system_error_code());
  214. throw interprocess_exception(err);
  215. }
  216. }
  217. inline bool file_lock::try_lock_sharable()
  218. {
  219. bool result;
  220. if(!ipcdetail::try_acquire_file_lock_sharable(m_file_hnd, result)){
  221. error_info err(system_error_code());
  222. throw interprocess_exception(err);
  223. }
  224. return result;
  225. }
  226. inline bool file_lock::timed_lock_sharable(const boost::posix_time::ptime &abs_time)
  227. {
  228. ipcdetail::lock_to_sharable<file_lock> lsh(*this);
  229. return ipcdetail::try_based_timed_lock(lsh, abs_time);
  230. }
  231. inline void file_lock::unlock_sharable()
  232. {
  233. if(!ipcdetail::release_file_lock_sharable(m_file_hnd)){
  234. error_info err(system_error_code());
  235. throw interprocess_exception(err);
  236. }
  237. }
  238. } //namespace interprocess {
  239. } //namespace boost {
  240. #include <boost/interprocess/detail/config_end.hpp>
  241. #endif //BOOST_INTERPROCESS_FILE_LOCK_HPP