interprocess_semaphore.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_SEMAPHORE_HPP
  11. #define BOOST_INTERPROCESS_SEMAPHORE_HPP
  12. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  13. #ifndef BOOST_CONFIG_HPP
  14. # include <boost/config.hpp>
  15. #endif
  16. #
  17. #if defined(BOOST_HAS_PRAGMA_ONCE)
  18. # pragma once
  19. #endif
  20. #include <boost/interprocess/detail/config_begin.hpp>
  21. #include <boost/interprocess/detail/workaround.hpp>
  22. #include <boost/interprocess/creation_tags.hpp>
  23. #include <boost/interprocess/exceptions.hpp>
  24. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  25. #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && \
  26. (defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED) && defined(BOOST_INTERPROCESS_POSIX_UNNAMED_SEMAPHORES))
  27. #include <boost/interprocess/sync/posix/semaphore.hpp>
  28. #define BOOST_INTERPROCESS_USE_POSIX
  29. //Experimental...
  30. #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  31. #include <boost/interprocess/sync/windows/semaphore.hpp>
  32. #define BOOST_INTERPROCESS_USE_WINDOWS
  33. #elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  34. #include <boost/interprocess/sync/spin/semaphore.hpp>
  35. #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
  36. #endif
  37. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  38. //!\file
  39. //!Describes a interprocess_semaphore class for inter-process synchronization
  40. namespace boost {
  41. namespace interprocess {
  42. //!Wraps a interprocess_semaphore that can be placed in shared memory and can be
  43. //!shared between processes. Allows timed lock tries
  44. class interprocess_semaphore
  45. {
  46. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  47. //Non-copyable
  48. interprocess_semaphore(const interprocess_semaphore &);
  49. interprocess_semaphore &operator=(const interprocess_semaphore &);
  50. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  51. public:
  52. //!Creates a interprocess_semaphore with the given initial count.
  53. //!interprocess_exception if there is an error.*/
  54. interprocess_semaphore(unsigned int initialCount);
  55. //!Destroys the interprocess_semaphore.
  56. //!Does not throw
  57. ~interprocess_semaphore();
  58. //!Increments the interprocess_semaphore count. If there are processes/threads blocked waiting
  59. //!for the interprocess_semaphore, then one of these processes will return successfully from
  60. //!its wait function. If there is an error an interprocess_exception exception is thrown.
  61. void post();
  62. //!Decrements the interprocess_semaphore. If the interprocess_semaphore value is not greater than zero,
  63. //!then the calling process/thread blocks until it can decrement the counter.
  64. //!If there is an error an interprocess_exception exception is thrown.
  65. void wait();
  66. //!Decrements the interprocess_semaphore if the interprocess_semaphore's value is greater than zero
  67. //!and returns true. If the value is not greater than zero returns false.
  68. //!If there is an error an interprocess_exception exception is thrown.
  69. bool try_wait();
  70. //!Decrements the interprocess_semaphore if the interprocess_semaphore's value is greater
  71. //!than zero and returns true. Otherwise, waits for the interprocess_semaphore
  72. //!to the posted or the timeout expires. If the timeout expires, the
  73. //!function returns false. If the interprocess_semaphore is posted the function
  74. //!returns true. If there is an error throws sem_exception
  75. bool timed_wait(const boost::posix_time::ptime &abs_time);
  76. //!Returns the interprocess_semaphore count
  77. // int get_count() const;
  78. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  79. private:
  80. #if defined(BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
  81. #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
  82. ipcdetail::spin_semaphore m_sem;
  83. #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
  84. #undef BOOST_INTERPROCESS_USE_WINDOWS
  85. ipcdetail::windows_semaphore m_sem;
  86. #else
  87. #undef BOOST_INTERPROCESS_USE_POSIX
  88. ipcdetail::posix_semaphore m_sem;
  89. #endif //#if defined(BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
  90. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  91. };
  92. } //namespace interprocess {
  93. } //namespace boost {
  94. namespace boost {
  95. namespace interprocess {
  96. inline interprocess_semaphore::interprocess_semaphore(unsigned int initialCount)
  97. : m_sem(initialCount)
  98. {}
  99. inline interprocess_semaphore::~interprocess_semaphore(){}
  100. inline void interprocess_semaphore::wait()
  101. {
  102. #ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING
  103. boost::posix_time::ptime wait_time
  104. = microsec_clock::universal_time()
  105. + boost::posix_time::milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS);
  106. if (!m_sem.timed_wait(wait_time))
  107. {
  108. throw interprocess_exception(timeout_when_waiting_error, "Interprocess semaphore timeout when waiting. Possible deadlock: owner died without posting?");
  109. }
  110. #else
  111. m_sem.wait();
  112. #endif
  113. }
  114. inline bool interprocess_semaphore::try_wait()
  115. { return m_sem.try_wait(); }
  116. inline bool interprocess_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
  117. { return m_sem.timed_wait(abs_time); }
  118. inline void interprocess_semaphore::post()
  119. { m_sem.post(); }
  120. } //namespace interprocess {
  121. } //namespace boost {
  122. #include <boost/interprocess/detail/config_end.hpp>
  123. #endif //BOOST_INTERPROCESS_SEMAPHORE_HPP