named_semaphore.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_NAMED_SEMAPHORE_HPP
  11. #define BOOST_INTERPROCESS_NAMED_SEMAPHORE_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/creation_tags.hpp>
  22. #include <boost/interprocess/exceptions.hpp>
  23. #include <boost/interprocess/permissions.hpp>
  24. #include <boost/interprocess/detail/interprocess_tester.hpp>
  25. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  26. #if defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USES_POSIX_SEMAPHORES)
  27. #include <boost/interprocess/sync/posix/named_semaphore.hpp>
  28. //Experimental...
  29. #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  30. #include <boost/interprocess/sync/windows/named_semaphore.hpp>
  31. #define BOOST_INTERPROCESS_NAMED_SEMAPHORE_USE_WINAPI
  32. #else
  33. #include <boost/interprocess/sync/shm/named_semaphore.hpp>
  34. #endif
  35. //!\file
  36. //!Describes a named semaphore class for inter-process synchronization
  37. namespace boost {
  38. namespace interprocess {
  39. //!A semaphore with a global name, so it can be found from different
  40. //!processes. Allows several resource sharing patterns and efficient
  41. //!acknowledgment mechanisms.
  42. class named_semaphore
  43. {
  44. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  45. //Non-copyable
  46. named_semaphore();
  47. named_semaphore(const named_semaphore &);
  48. named_semaphore &operator=(const named_semaphore &);
  49. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  50. public:
  51. //!Creates a global semaphore with a name, and an initial count.
  52. //!If the semaphore can't be created throws interprocess_exception
  53. named_semaphore(create_only_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
  54. //!Opens or creates a global semaphore with a name, and an initial count.
  55. //!If the semaphore is created, this call is equivalent to
  56. //!named_semaphore(create_only_t, ...)
  57. //!If the semaphore is already created, this call is equivalent to
  58. //!named_semaphore(open_only_t, ... )
  59. //!and initialCount is ignored.
  60. named_semaphore(open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
  61. //!Opens a global semaphore with a name if that semaphore is previously.
  62. //!created. If it is not previously created this function throws
  63. //!interprocess_exception.
  64. named_semaphore(open_only_t, const char *name);
  65. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  66. //!Creates a global semaphore with a name, and an initial count.
  67. //!If the semaphore can't be created throws interprocess_exception
  68. //!
  69. //!Note: This function is only available on operating systems with
  70. //! native wchar_t APIs (e.g. Windows).
  71. named_semaphore(create_only_t, const wchar_t *name, unsigned int initialCount, const permissions &perm = permissions());
  72. //!Opens or creates a global semaphore with a name, and an initial count.
  73. //!If the semaphore is created, this call is equivalent to
  74. //!named_semaphore(create_only_t, ...)
  75. //!If the semaphore is already created, this call is equivalent to
  76. //!named_semaphore(open_only_t, ... )
  77. //!and initialCount is ignored.
  78. //!
  79. //!Note: This function is only available on operating systems with
  80. //! native wchar_t APIs (e.g. Windows).
  81. named_semaphore(open_or_create_t, const wchar_t *name, unsigned int initialCount, const permissions &perm = permissions());
  82. //!Opens a global semaphore with a name if that semaphore is previously.
  83. //!created. If it is not previously created this function throws
  84. //!interprocess_exception.
  85. //!
  86. //!Note: This function is only available on operating systems with
  87. //! native wchar_t APIs (e.g. Windows).
  88. named_semaphore(open_only_t, const wchar_t *name);
  89. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  90. //!Destroys *this and indicates that the calling process is finished using
  91. //!the resource. The destructor function will deallocate
  92. //!any system resources allocated by the system for use by this process for
  93. //!this resource. The resource can still be opened again calling
  94. //!the open constructor overload. To erase the resource from the system
  95. //!use remove().
  96. ~named_semaphore();
  97. //!Increments the semaphore count. If there are processes/threads blocked waiting
  98. //!for the semaphore, then one of these processes will return successfully from
  99. //!its wait function. If there is an error an interprocess_exception exception is thrown.
  100. void post();
  101. //!Decrements the semaphore. If the semaphore value is not greater than zero,
  102. //!then the calling process/thread blocks until it can decrement the counter.
  103. //!If there is an error an interprocess_exception exception is thrown.
  104. void wait();
  105. //!Decrements the semaphore if the semaphore's value is greater than zero
  106. //!and returns true. If the value is not greater than zero returns false.
  107. //!If there is an error an interprocess_exception exception is thrown.
  108. bool try_wait();
  109. //!Decrements the semaphore if the semaphore's value is greater
  110. //!than zero and returns true. Otherwise, waits for the semaphore
  111. //!to the posted or the timeout expires. If the timeout expires, the
  112. //!function returns false. If the semaphore is posted the function
  113. //!returns true. If there is an error throws sem_exception
  114. bool timed_wait(const boost::posix_time::ptime &abs_time);
  115. //!Erases a named semaphore from the system.
  116. //!Returns false on error. Never throws.
  117. static bool remove(const char *name);
  118. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  119. //!Erases a named semaphore from the system.
  120. //!Returns false on error. Never throws.
  121. //!
  122. //!Note: This function is only available on operating systems with
  123. //! native wchar_t APIs (e.g. Windows).
  124. static bool remove(const wchar_t *name);
  125. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  126. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  127. private:
  128. friend class ipcdetail::interprocess_tester;
  129. void dont_close_on_destruction();
  130. #if defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USES_POSIX_SEMAPHORES)
  131. typedef ipcdetail::posix_named_semaphore impl_t;
  132. #elif defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USE_WINAPI)
  133. typedef ipcdetail::winapi_named_semaphore impl_t;
  134. #else
  135. typedef ipcdetail::shm_named_semaphore impl_t;
  136. #endif
  137. impl_t m_sem;
  138. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  139. };
  140. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  141. inline named_semaphore::named_semaphore
  142. (create_only_t, const char *name, unsigned int initialCount, const permissions &perm)
  143. : m_sem(create_only, name, initialCount, perm)
  144. {}
  145. inline named_semaphore::named_semaphore
  146. (open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm)
  147. : m_sem(open_or_create, name, initialCount, perm)
  148. {}
  149. inline named_semaphore::named_semaphore(open_only_t, const char *name)
  150. : m_sem(open_only, name)
  151. {}
  152. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  153. inline named_semaphore::named_semaphore
  154. (create_only_t, const wchar_t *name, unsigned int initialCount, const permissions &perm)
  155. : m_sem(create_only, name, initialCount, perm)
  156. {}
  157. inline named_semaphore::named_semaphore
  158. (open_or_create_t, const wchar_t *name, unsigned int initialCount, const permissions &perm)
  159. : m_sem(open_or_create, name, initialCount, perm)
  160. {}
  161. inline named_semaphore::named_semaphore(open_only_t, const wchar_t *name)
  162. : m_sem(open_only, name)
  163. {}
  164. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  165. inline named_semaphore::~named_semaphore()
  166. {}
  167. inline void named_semaphore::dont_close_on_destruction()
  168. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_sem); }
  169. inline void named_semaphore::wait()
  170. { m_sem.wait(); }
  171. inline void named_semaphore::post()
  172. { m_sem.post(); }
  173. inline bool named_semaphore::try_wait()
  174. { return m_sem.try_wait(); }
  175. inline bool named_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
  176. { return m_sem.timed_wait(abs_time); }
  177. inline bool named_semaphore::remove(const char *name)
  178. { return impl_t::remove(name); }
  179. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  180. inline bool named_semaphore::remove(const wchar_t *name)
  181. { return impl_t::remove(name); }
  182. #endif
  183. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  184. } //namespace interprocess {
  185. } //namespace boost {
  186. #include <boost/interprocess/detail/config_end.hpp>
  187. #endif //BOOST_INTERPROCESS_NAMED_SEMAPHORE_HPP