file_lock.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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()
  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. //!Moves the ownership of "moved"'s file mapping object to *this.
  53. //!After the call, "moved" does not represent any file mapping object.
  54. //!Does not throw
  55. file_lock(BOOST_RV_REF(file_lock) moved)
  56. : m_file_hnd(file_handle_t(ipcdetail::invalid_file()))
  57. { this->swap(moved); }
  58. //!Moves the ownership of "moved"'s file mapping to *this.
  59. //!After the call, "moved" does not represent any file mapping.
  60. //!Does not throw
  61. file_lock &operator=(BOOST_RV_REF(file_lock) moved)
  62. {
  63. file_lock tmp(boost::move(moved));
  64. this->swap(tmp);
  65. return *this;
  66. }
  67. //!Closes a file lock. Does not throw.
  68. ~file_lock();
  69. //!Swaps two file_locks.
  70. //!Does not throw.
  71. void swap(file_lock &other)
  72. {
  73. file_handle_t tmp = m_file_hnd;
  74. m_file_hnd = other.m_file_hnd;
  75. other.m_file_hnd = tmp;
  76. }
  77. //Exclusive locking
  78. //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
  79. //! and if another thread has exclusive, or sharable ownership of
  80. //! the mutex, it waits until it can obtain the ownership.
  81. //!Throws: interprocess_exception on error.
  82. void lock();
  83. //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
  84. //! without waiting. If no other thread has exclusive, or sharable
  85. //! ownership of the mutex this succeeds.
  86. //!Returns: If it can acquire exclusive ownership immediately returns true.
  87. //! If it has to wait, returns false.
  88. //!Throws: interprocess_exception on error.
  89. bool try_lock();
  90. //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
  91. //! waiting if necessary until no other thread has exclusive, or sharable
  92. //! ownership of the mutex or abs_time is reached.
  93. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  94. //!Throws: interprocess_exception on error.
  95. bool timed_lock(const boost::posix_time::ptime &abs_time);
  96. //!Precondition: The thread must have exclusive ownership of the mutex.
  97. //!Effects: The calling thread releases the exclusive ownership of the mutex.
  98. //!Throws: An exception derived from interprocess_exception on error.
  99. void unlock();
  100. //Sharable locking
  101. //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
  102. //! and if another thread has exclusive ownership of the mutex, waits until
  103. //! it can obtain the ownership.
  104. //!Throws: interprocess_exception on error.
  105. void lock_sharable();
  106. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  107. //! without waiting. If no other thread has exclusive ownership of the
  108. //! mutex this succeeds.
  109. //!Returns: If it can acquire sharable ownership immediately returns true. If it
  110. //! has to wait, returns false.
  111. //!Throws: interprocess_exception on error.
  112. bool try_lock_sharable();
  113. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  114. //! waiting if necessary until no other thread has exclusive ownership of
  115. //! the mutex or abs_time is reached.
  116. //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
  117. //!Throws: interprocess_exception on error.
  118. bool timed_lock_sharable(const boost::posix_time::ptime &abs_time);
  119. //!Precondition: The thread must have sharable ownership of the mutex.
  120. //!Effects: The calling thread releases the sharable ownership of the mutex.
  121. //!Throws: An exception derived from interprocess_exception on error.
  122. void unlock_sharable();
  123. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  124. private:
  125. file_handle_t m_file_hnd;
  126. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  127. };
  128. inline file_lock::file_lock(const char *name)
  129. {
  130. m_file_hnd = ipcdetail::open_existing_file(name, read_write);
  131. if(m_file_hnd == ipcdetail::invalid_file()){
  132. error_info err(system_error_code());
  133. throw interprocess_exception(err);
  134. }
  135. }
  136. inline file_lock::~file_lock()
  137. {
  138. if(m_file_hnd != ipcdetail::invalid_file()){
  139. ipcdetail::close_file(m_file_hnd);
  140. m_file_hnd = ipcdetail::invalid_file();
  141. }
  142. }
  143. inline void file_lock::lock()
  144. {
  145. if(!ipcdetail::acquire_file_lock(m_file_hnd)){
  146. error_info err(system_error_code());
  147. throw interprocess_exception(err);
  148. }
  149. }
  150. inline bool file_lock::try_lock()
  151. {
  152. bool result;
  153. if(!ipcdetail::try_acquire_file_lock(m_file_hnd, result)){
  154. error_info err(system_error_code());
  155. throw interprocess_exception(err);
  156. }
  157. return result;
  158. }
  159. inline bool file_lock::timed_lock(const boost::posix_time::ptime &abs_time)
  160. { return ipcdetail::try_based_timed_lock(*this, abs_time); }
  161. inline void file_lock::unlock()
  162. {
  163. if(!ipcdetail::release_file_lock(m_file_hnd)){
  164. error_info err(system_error_code());
  165. throw interprocess_exception(err);
  166. }
  167. }
  168. inline void file_lock::lock_sharable()
  169. {
  170. if(!ipcdetail::acquire_file_lock_sharable(m_file_hnd)){
  171. error_info err(system_error_code());
  172. throw interprocess_exception(err);
  173. }
  174. }
  175. inline bool file_lock::try_lock_sharable()
  176. {
  177. bool result;
  178. if(!ipcdetail::try_acquire_file_lock_sharable(m_file_hnd, result)){
  179. error_info err(system_error_code());
  180. throw interprocess_exception(err);
  181. }
  182. return result;
  183. }
  184. inline bool file_lock::timed_lock_sharable(const boost::posix_time::ptime &abs_time)
  185. {
  186. ipcdetail::lock_to_sharable<file_lock> lsh(*this);
  187. return ipcdetail::try_based_timed_lock(lsh, abs_time);
  188. }
  189. inline void file_lock::unlock_sharable()
  190. {
  191. if(!ipcdetail::release_file_lock_sharable(m_file_hnd)){
  192. error_info err(system_error_code());
  193. throw interprocess_exception(err);
  194. }
  195. }
  196. } //namespace interprocess {
  197. } //namespace boost {
  198. #include <boost/interprocess/detail/config_end.hpp>
  199. #endif //BOOST_INTERPROCESS_FILE_LOCK_HPP