named_semaphore.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_USE_WINDOWS
  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. //!Destroys *this and indicates that the calling process is finished using
  66. //!the resource. The destructor function will deallocate
  67. //!any system resources allocated by the system for use by this process for
  68. //!this resource. The resource can still be opened again calling
  69. //!the open constructor overload. To erase the resource from the system
  70. //!use remove().
  71. ~named_semaphore();
  72. //!Increments the semaphore count. If there are processes/threads blocked waiting
  73. //!for the semaphore, then one of these processes will return successfully from
  74. //!its wait function. If there is an error an interprocess_exception exception is thrown.
  75. void post();
  76. //!Decrements the semaphore. If the semaphore value is not greater than zero,
  77. //!then the calling process/thread blocks until it can decrement the counter.
  78. //!If there is an error an interprocess_exception exception is thrown.
  79. void wait();
  80. //!Decrements the semaphore if the semaphore's value is greater than zero
  81. //!and returns true. If the value is not greater than zero returns false.
  82. //!If there is an error an interprocess_exception exception is thrown.
  83. bool try_wait();
  84. //!Decrements the semaphore if the semaphore's value is greater
  85. //!than zero and returns true. Otherwise, waits for the semaphore
  86. //!to the posted or the timeout expires. If the timeout expires, the
  87. //!function returns false. If the semaphore is posted the function
  88. //!returns true. If there is an error throws sem_exception
  89. bool timed_wait(const boost::posix_time::ptime &abs_time);
  90. //!Erases a named semaphore from the system.
  91. //!Returns false on error. Never throws.
  92. static bool remove(const char *name);
  93. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  94. private:
  95. friend class ipcdetail::interprocess_tester;
  96. void dont_close_on_destruction();
  97. #if defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USES_POSIX_SEMAPHORES)
  98. typedef ipcdetail::posix_named_semaphore impl_t;
  99. #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
  100. #undef BOOST_INTERPROCESS_USE_WINDOWS
  101. typedef ipcdetail::windows_named_semaphore impl_t;
  102. #else
  103. typedef ipcdetail::shm_named_semaphore impl_t;
  104. #endif
  105. impl_t m_sem;
  106. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  107. };
  108. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  109. inline named_semaphore::named_semaphore
  110. (create_only_t, const char *name, unsigned int initialCount, const permissions &perm)
  111. : m_sem(create_only, name, initialCount, perm)
  112. {}
  113. inline named_semaphore::named_semaphore
  114. (open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm)
  115. : m_sem(open_or_create, name, initialCount, perm)
  116. {}
  117. inline named_semaphore::named_semaphore(open_only_t, const char *name)
  118. : m_sem(open_only, name)
  119. {}
  120. inline named_semaphore::~named_semaphore()
  121. {}
  122. inline void named_semaphore::dont_close_on_destruction()
  123. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_sem); }
  124. inline void named_semaphore::wait()
  125. { m_sem.wait(); }
  126. inline void named_semaphore::post()
  127. { m_sem.post(); }
  128. inline bool named_semaphore::try_wait()
  129. { return m_sem.try_wait(); }
  130. inline bool named_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
  131. { return m_sem.timed_wait(abs_time); }
  132. inline bool named_semaphore::remove(const char *name)
  133. { return impl_t::remove(name); }
  134. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  135. } //namespace interprocess {
  136. } //namespace boost {
  137. #include <boost/interprocess/detail/config_end.hpp>
  138. #endif //BOOST_INTERPROCESS_NAMED_SEMAPHORE_HPP