named_semaphore.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #if defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USES_POSIX_SEMAPHORES)
  26. #include <boost/interprocess/sync/posix/named_semaphore.hpp>
  27. //Experimental...
  28. #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  29. #include <boost/interprocess/sync/windows/named_semaphore.hpp>
  30. #define BOOST_INTERPROCESS_NAMED_SEMAPHORE_USE_WINAPI
  31. #else
  32. #include <boost/interprocess/sync/shm/named_semaphore.hpp>
  33. #endif
  34. //!\file
  35. //!Describes a named semaphore class for inter-process synchronization
  36. namespace boost {
  37. namespace interprocess {
  38. //!A semaphore with a global name, so it can be found from different
  39. //!processes. Allows several resource sharing patterns and efficient
  40. //!acknowledgment mechanisms.
  41. class named_semaphore
  42. {
  43. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  44. //Non-copyable
  45. named_semaphore();
  46. named_semaphore(const named_semaphore &);
  47. named_semaphore &operator=(const named_semaphore &);
  48. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  49. public:
  50. //!Creates a global semaphore with a name, and an initial count.
  51. //!If the semaphore can't be created throws interprocess_exception
  52. named_semaphore(create_only_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
  53. //!Opens or creates a global semaphore with a name, and an initial count.
  54. //!If the semaphore is created, this call is equivalent to
  55. //!named_semaphore(create_only_t, ...)
  56. //!If the semaphore is already created, this call is equivalent to
  57. //!named_semaphore(open_only_t, ... )
  58. //!and initialCount is ignored.
  59. named_semaphore(open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
  60. //!Opens a global semaphore with a name if that semaphore is previously.
  61. //!created. If it is not previously created this function throws
  62. //!interprocess_exception.
  63. named_semaphore(open_only_t, const char *name);
  64. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  65. //!Creates a global semaphore with a name, and an initial count.
  66. //!If the semaphore can't be created throws interprocess_exception
  67. //!
  68. //!Note: This function is only available on operating systems with
  69. //! native wchar_t APIs (e.g. Windows).
  70. named_semaphore(create_only_t, const wchar_t *name, unsigned int initialCount, const permissions &perm = permissions());
  71. //!Opens or creates a global semaphore with a name, and an initial count.
  72. //!If the semaphore is created, this call is equivalent to
  73. //!named_semaphore(create_only_t, ...)
  74. //!If the semaphore is already created, this call is equivalent to
  75. //!named_semaphore(open_only_t, ... )
  76. //!and initialCount is ignored.
  77. //!
  78. //!Note: This function is only available on operating systems with
  79. //! native wchar_t APIs (e.g. Windows).
  80. named_semaphore(open_or_create_t, const wchar_t *name, unsigned int initialCount, const permissions &perm = permissions());
  81. //!Opens a global semaphore with a name if that semaphore is previously.
  82. //!created. If it is not previously created this function throws
  83. //!interprocess_exception.
  84. //!
  85. //!Note: This function is only available on operating systems with
  86. //! native wchar_t APIs (e.g. Windows).
  87. named_semaphore(open_only_t, const wchar_t *name);
  88. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  89. //!Destroys *this and indicates that the calling process is finished using
  90. //!the resource. The destructor function will deallocate
  91. //!any system resources allocated by the system for use by this process for
  92. //!this resource. The resource can still be opened again calling
  93. //!the open constructor overload. To erase the resource from the system
  94. //!use remove().
  95. ~named_semaphore();
  96. //!Increments the semaphore count. If there are processes/threads blocked waiting
  97. //!for the semaphore, then one of these processes will return successfully from
  98. //!its wait function. If there is an error an interprocess_exception exception is thrown.
  99. void post();
  100. //!Decrements the semaphore. If the semaphore value is not greater than zero,
  101. //!then the calling process/thread blocks until it can decrement the counter.
  102. //!If there is an error an interprocess_exception exception is thrown.
  103. void wait();
  104. //!Decrements the semaphore if the semaphore's value is greater than zero
  105. //!and returns true. If the value is not greater than zero returns false.
  106. //!If there is an error an interprocess_exception exception is thrown.
  107. bool try_wait();
  108. //!Decrements the semaphore if the semaphore's value is greater
  109. //!than zero and returns true. Otherwise, waits for the semaphore
  110. //!to the posted or the timeout expires. If the timeout expires, the
  111. //!function returns false. If the semaphore is posted the function
  112. //!returns true. If there is an error throws sem_exception
  113. template<class TimePoint>
  114. bool timed_wait(const TimePoint &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. template<class TimePoint>
  176. inline bool named_semaphore::timed_wait(const TimePoint &abs_time)
  177. { return m_sem.timed_wait(abs_time); }
  178. inline bool named_semaphore::remove(const char *name)
  179. { return impl_t::remove(name); }
  180. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  181. inline bool named_semaphore::remove(const wchar_t *name)
  182. { return impl_t::remove(name); }
  183. #endif
  184. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  185. } //namespace interprocess {
  186. } //namespace boost {
  187. #include <boost/interprocess/detail/config_end.hpp>
  188. #endif //BOOST_INTERPROCESS_NAMED_SEMAPHORE_HPP