xsi_named_mutex.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2009-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_XSI_XSI_NAMED_MUTEX_HPP
  11. #define BOOST_INTERPROCESS_XSI_XSI_NAMED_MUTEX_HPP
  12. #include <boost/interprocess/detail/config_begin.hpp>
  13. #include <boost/interprocess/detail/workaround.hpp>
  14. #if defined(BOOST_INTERPROCESS_WINDOWS)
  15. #error "This header can't be used in Windows operating systems"
  16. #endif
  17. #include <boost/move/move.hpp>
  18. #include <boost/interprocess/creation_tags.hpp>
  19. #include <boost/interprocess/exceptions.hpp>
  20. #include <boost/interprocess/detail/utilities.hpp>
  21. #include <boost/interprocess/detail/os_file_functions.hpp>
  22. #include <boost/interprocess/interprocess_fwd.hpp>
  23. #include <boost/interprocess/exceptions.hpp>
  24. #include <boost/interprocess/sync/xsi/basic_xsi_semaphore.hpp>
  25. #include <cstddef>
  26. #include <boost/assert.hpp>
  27. #include <boost/cstdint.hpp>
  28. #include <string>
  29. #include <boost/assert.hpp>
  30. //!\file
  31. //!Describes a class representing a xsi-based named_mutex.
  32. namespace boost {
  33. namespace interprocess {
  34. //!A class that wraps a XSI (System V)-based named semaphore
  35. //!that undoes the operation if the process crashes.
  36. class xsi_named_mutex
  37. {
  38. /// @cond
  39. //Non-copyable and non-assignable
  40. xsi_named_mutex(xsi_named_mutex &);
  41. xsi_named_mutex &operator=(xsi_named_mutex &);
  42. /// @endcond
  43. public:
  44. BOOST_MOVABLE_BUT_NOT_COPYABLE(xsi_named_mutex)
  45. //!Default constructor.
  46. //!Represents an empty xsi_named_mutex.
  47. xsi_named_mutex();
  48. //!Tries to create a new XSI-based named mutex with a key obtained from a call to ftok (with path
  49. //!"path" and id "id"), and permissions "perm".
  50. //!If the named mutex previously exists, it tries to open it.
  51. //!Otherwise throws an error.
  52. xsi_named_mutex(open_or_create_t, const char *path, boost::uint8_t id, int perm = 0666)
  53. { this->priv_open_or_create(ipcdetail::DoOpenOrCreate, path, id, perm); }
  54. //!Moves the ownership of "moved"'s named mutex to *this.
  55. //!After the call, "moved" does not represent any named mutex
  56. //!Does not throw
  57. xsi_named_mutex(BOOST_RV_REF(xsi_named_mutex) moved)
  58. { this->swap(moved); }
  59. //!Moves the ownership of "moved"'s named mutex to *this.
  60. //!After the call, "moved" does not represent any named mutex.
  61. //!Does not throw
  62. xsi_named_mutex &operator=(BOOST_RV_REF(xsi_named_mutex) moved)
  63. {
  64. xsi_named_mutex tmp(boost::move(moved));
  65. this->swap(tmp);
  66. return *this;
  67. }
  68. //!Swaps two xsi_named_mutex. Does not throw
  69. void swap(xsi_named_mutex &other);
  70. //!Destroys *this. The named mutex is still valid after
  71. //!destruction. use remove() to destroy the named mutex.
  72. ~xsi_named_mutex();
  73. //!Returns the path used to construct the
  74. //!named mutex.
  75. const char *get_path() const;
  76. //!Returns access
  77. //!permissions
  78. int get_permissions() const;
  79. //!Returns the mapping handle.
  80. //!Never throws
  81. mapping_handle_t get_mapping_handle() const;
  82. //!Erases a XSI-based named mutex from the system.
  83. //!Returns false on error. Never throws
  84. bool remove();
  85. void lock();
  86. void unlock();
  87. /// @cond
  88. private:
  89. //!Closes a previously opened file mapping. Never throws.
  90. void priv_close();
  91. //!Closes a previously opened file mapping. Never throws.
  92. bool priv_open_or_create( ipcdetail::create_enum_t type
  93. , const char *path
  94. , boost::uint8_t id
  95. , int perm);
  96. int m_semid;
  97. key_t m_key;
  98. boost::uint8_t m_id;
  99. int m_perm;
  100. std::string m_path;
  101. /// @endcond
  102. };
  103. /// @cond
  104. inline xsi_named_mutex::xsi_named_mutex()
  105. : m_semid(-1), m_key(-1), m_id(0), m_perm(0), m_path()
  106. {}
  107. inline xsi_named_mutex::~xsi_named_mutex()
  108. { this->priv_close(); }
  109. inline const char *xsi_named_mutex::get_path() const
  110. { return m_path.c_str(); }
  111. inline void xsi_named_mutex::swap(xsi_named_mutex &other)
  112. {
  113. std::swap(m_key, other.m_key);
  114. std::swap(m_id, other.m_id);
  115. std::swap(m_semid, other.m_semid);
  116. std::swap(m_perm, other.m_perm);
  117. m_path.swap(other.m_path);
  118. }
  119. inline mapping_handle_t xsi_named_mutex::get_mapping_handle() const
  120. { mapping_handle_t mhnd = { m_semid, true}; return mhnd; }
  121. inline int xsi_named_mutex::get_permissions() const
  122. { return m_perm; }
  123. inline bool xsi_named_mutex::priv_open_or_create
  124. (ipcdetail::create_enum_t type, const char *path, boost::uint8_t id, int perm)
  125. {
  126. key_t key;
  127. if(path){
  128. key = ::ftok(path, id);
  129. if(((key_t)-1) == key){
  130. error_info err = system_error_code();
  131. throw interprocess_exception(err);
  132. }
  133. }
  134. else{
  135. key = IPC_PRIVATE;
  136. }
  137. perm &= 0x01FF;
  138. int semid;
  139. if(!xsi::simple_sem_open_or_create(key, 1, semid, perm)){
  140. error_info err = system_error_code();
  141. throw interprocess_exception(err);
  142. }
  143. m_perm = perm;
  144. m_semid = semid;
  145. m_path = path ? path : "";
  146. m_id = id;
  147. m_key = key;
  148. return true;
  149. }
  150. inline void xsi_named_mutex::priv_close()
  151. {
  152. }
  153. inline void xsi_named_mutex::lock()
  154. {
  155. if(!xsi::simple_sem_op(m_semid, -1)){
  156. error_info err = system_error_code();
  157. throw interprocess_exception(err);
  158. }
  159. }
  160. inline void xsi_named_mutex::unlock()
  161. {
  162. bool success = xsi::simple_sem_op(m_semid, 1);
  163. (void)success;
  164. BOOST_ASSERT(success);
  165. }
  166. inline bool xsi_named_mutex::remove()
  167. {
  168. if(m_semid != -1){
  169. int ret = ::semctl(m_semid, IPC_RMID, 0);
  170. if(-1 == ret)
  171. return false;
  172. //Now put it in default-constructed state
  173. m_semid = -1;
  174. m_key = -1;
  175. m_id = 0;
  176. m_perm = 0;
  177. m_path.clear();
  178. }
  179. return false;
  180. }
  181. ///@endcond
  182. } //namespace interprocess {
  183. } //namespace boost {
  184. #include <boost/interprocess/detail/config_end.hpp>
  185. #endif //BOOST_INTERPROCESS_XSI_XSI_NAMED_MUTEX_HPP