named_semaphore.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif
  15. #include <boost/interprocess/detail/config_begin.hpp>
  16. #include <boost/interprocess/detail/workaround.hpp>
  17. #include <boost/interprocess/creation_tags.hpp>
  18. #include <boost/interprocess/exceptions.hpp>
  19. #include <boost/interprocess/permissions.hpp>
  20. #include <boost/interprocess/detail/interprocess_tester.hpp>
  21. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  22. #if defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USES_POSIX_SEMAPHORES)
  23. #include <boost/interprocess/sync/posix/named_semaphore.hpp>
  24. //Experimental...
  25. #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  26. #include <boost/interprocess/sync/windows/named_semaphore.hpp>
  27. #define BOOST_INTERPROCESS_USE_WINDOWS
  28. #else
  29. #include <boost/interprocess/sync/shm/named_semaphore.hpp>
  30. #endif
  31. //!\file
  32. //!Describes a named semaphore class for inter-process synchronization
  33. namespace boost {
  34. namespace interprocess {
  35. //!A semaphore with a global name, so it can be found from different
  36. //!processes. Allows several resource sharing patterns and efficient
  37. //!acknowledgment mechanisms.
  38. class named_semaphore
  39. {
  40. /// @cond
  41. //Non-copyable
  42. named_semaphore();
  43. named_semaphore(const named_semaphore &);
  44. named_semaphore &operator=(const named_semaphore &);
  45. /// @endcond
  46. public:
  47. //!Creates a global semaphore with a name, and an initial count.
  48. //!If the semaphore can't be created throws interprocess_exception
  49. named_semaphore(create_only_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
  50. //!Opens or creates a global semaphore with a name, and an initial count.
  51. //!If the semaphore is created, this call is equivalent to
  52. //!named_semaphore(create_only_t, ...)
  53. //!If the semaphore is already created, this call is equivalent to
  54. //!named_semaphore(open_only_t, ... )
  55. //!and initialCount is ignored.
  56. named_semaphore(open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
  57. //!Opens a global semaphore with a name if that semaphore is previously.
  58. //!created. If it is not previously created this function throws
  59. //!interprocess_exception.
  60. named_semaphore(open_only_t, const char *name);
  61. //!Destroys *this and indicates that the calling process is finished using
  62. //!the resource. The destructor function will deallocate
  63. //!any system resources allocated by the system for use by this process for
  64. //!this resource. The resource can still be opened again calling
  65. //!the open constructor overload. To erase the resource from the system
  66. //!use remove().
  67. ~named_semaphore();
  68. //!Increments the semaphore count. If there are processes/threads blocked waiting
  69. //!for the semaphore, then one of these processes will return successfully from
  70. //!its wait function. If there is an error an interprocess_exception exception is thrown.
  71. void post();
  72. //!Decrements the semaphore. If the semaphore value is not greater than zero,
  73. //!then the calling process/thread blocks until it can decrement the counter.
  74. //!If there is an error an interprocess_exception exception is thrown.
  75. void wait();
  76. //!Decrements the semaphore if the semaphore's value is greater than zero
  77. //!and returns true. If the value is not greater than zero returns false.
  78. //!If there is an error an interprocess_exception exception is thrown.
  79. bool try_wait();
  80. //!Decrements the semaphore if the semaphore's value is greater
  81. //!than zero and returns true. Otherwise, waits for the semaphore
  82. //!to the posted or the timeout expires. If the timeout expires, the
  83. //!function returns false. If the semaphore is posted the function
  84. //!returns true. If there is an error throws sem_exception
  85. bool timed_wait(const boost::posix_time::ptime &abs_time);
  86. //!Erases a named semaphore from the system.
  87. //!Returns false on error. Never throws.
  88. static bool remove(const char *name);
  89. /// @cond
  90. private:
  91. friend class ipcdetail::interprocess_tester;
  92. void dont_close_on_destruction();
  93. #if defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USES_POSIX_SEMAPHORES)
  94. typedef ipcdetail::posix_named_semaphore impl_t;
  95. #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
  96. #undef BOOST_INTERPROCESS_USE_WINDOWS
  97. typedef ipcdetail::windows_named_semaphore impl_t;
  98. #else
  99. typedef ipcdetail::shm_named_semaphore impl_t;
  100. #endif
  101. impl_t m_sem;
  102. /// @endcond
  103. };
  104. /// @cond
  105. inline named_semaphore::named_semaphore
  106. (create_only_t, const char *name, unsigned int initialCount, const permissions &perm)
  107. : m_sem(create_only, name, initialCount, perm)
  108. {}
  109. inline named_semaphore::named_semaphore
  110. (open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm)
  111. : m_sem(open_or_create, name, initialCount, perm)
  112. {}
  113. inline named_semaphore::named_semaphore(open_only_t, const char *name)
  114. : m_sem(open_only, name)
  115. {}
  116. inline named_semaphore::~named_semaphore()
  117. {}
  118. inline void named_semaphore::dont_close_on_destruction()
  119. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_sem); }
  120. inline void named_semaphore::wait()
  121. { m_sem.wait(); }
  122. inline void named_semaphore::post()
  123. { m_sem.post(); }
  124. inline bool named_semaphore::try_wait()
  125. { return m_sem.try_wait(); }
  126. inline bool named_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
  127. {
  128. if(abs_time == boost::posix_time::pos_infin){
  129. this->wait();
  130. return true;
  131. }
  132. return m_sem.timed_wait(abs_time);
  133. }
  134. inline bool named_semaphore::remove(const char *name)
  135. { return impl_t::remove(name); }
  136. /// @endcond
  137. } //namespace interprocess {
  138. } //namespace boost {
  139. #include <boost/interprocess/detail/config_end.hpp>
  140. #endif //BOOST_INTERPROCESS_NAMED_SEMAPHORE_HPP