named_semaphore.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_SHM_NAMED_SEMAPHORE_HPP
  11. #define BOOST_INTERPROCESS_SHM_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/shared_memory_object.hpp>
  26. #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
  27. #include <boost/interprocess/sync/interprocess_semaphore.hpp>
  28. #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
  29. namespace boost {
  30. namespace interprocess {
  31. namespace ipcdetail {
  32. class shm_named_semaphore
  33. {
  34. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  35. //Non-copyable
  36. shm_named_semaphore();
  37. shm_named_semaphore(const shm_named_semaphore &);
  38. shm_named_semaphore &operator=(const shm_named_semaphore &);
  39. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  40. public:
  41. shm_named_semaphore(create_only_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
  42. shm_named_semaphore(open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
  43. shm_named_semaphore(open_only_t, const char *name);
  44. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  45. shm_named_semaphore(create_only_t, const wchar_t *name, unsigned int initialCount, const permissions &perm = permissions());
  46. shm_named_semaphore(open_or_create_t, const wchar_t *name, unsigned int initialCount, const permissions &perm = permissions());
  47. shm_named_semaphore(open_only_t, const wchar_t *name);
  48. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  49. ~shm_named_semaphore();
  50. void post();
  51. void wait();
  52. bool try_wait();
  53. template<class TimePoint> bool timed_wait(const TimePoint &abs_time);
  54. static bool remove(const char *name);
  55. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  56. static bool remove(const wchar_t *name);
  57. #endif
  58. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  59. private:
  60. friend class interprocess_tester;
  61. void dont_close_on_destruction();
  62. interprocess_semaphore *semaphore() const
  63. { return static_cast<interprocess_semaphore*>(m_shmem.get_user_address()); }
  64. typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
  65. open_create_impl_t m_shmem;
  66. typedef named_creation_functor<interprocess_semaphore, unsigned> construct_func_t;
  67. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  68. };
  69. inline shm_named_semaphore::~shm_named_semaphore()
  70. {}
  71. inline void shm_named_semaphore::dont_close_on_destruction()
  72. { interprocess_tester::dont_close_on_destruction(m_shmem); }
  73. inline shm_named_semaphore::shm_named_semaphore
  74. (create_only_t, const char *name, unsigned int initialCount, const permissions &perm)
  75. : m_shmem (create_only
  76. ,name
  77. ,sizeof(interprocess_semaphore) +
  78. open_create_impl_t::ManagedOpenOrCreateUserOffset
  79. ,read_write
  80. ,0
  81. ,construct_func_t(DoCreate, initialCount)
  82. ,perm)
  83. {}
  84. inline shm_named_semaphore::shm_named_semaphore
  85. (open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm)
  86. : m_shmem (open_or_create
  87. ,name
  88. ,sizeof(interprocess_semaphore) +
  89. open_create_impl_t::ManagedOpenOrCreateUserOffset
  90. ,read_write
  91. ,0
  92. ,construct_func_t(DoOpenOrCreate, initialCount)
  93. ,perm)
  94. {}
  95. inline shm_named_semaphore::shm_named_semaphore
  96. (open_only_t, const char *name)
  97. : m_shmem (open_only
  98. ,name
  99. ,read_write
  100. ,0
  101. ,construct_func_t(DoOpen, 0))
  102. {}
  103. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  104. inline shm_named_semaphore::shm_named_semaphore
  105. (create_only_t, const wchar_t *name, unsigned int initialCount, const permissions &perm)
  106. : m_shmem (create_only
  107. ,name
  108. ,sizeof(interprocess_semaphore) +
  109. open_create_impl_t::ManagedOpenOrCreateUserOffset
  110. ,read_write
  111. ,0
  112. ,construct_func_t(DoCreate, initialCount)
  113. ,perm)
  114. {}
  115. inline shm_named_semaphore::shm_named_semaphore
  116. (open_or_create_t, const wchar_t *name, unsigned int initialCount, const permissions &perm)
  117. : m_shmem (open_or_create
  118. ,name
  119. ,sizeof(interprocess_semaphore) +
  120. open_create_impl_t::ManagedOpenOrCreateUserOffset
  121. ,read_write
  122. ,0
  123. ,construct_func_t(DoOpenOrCreate, initialCount)
  124. ,perm)
  125. {}
  126. inline shm_named_semaphore::shm_named_semaphore
  127. (open_only_t, const wchar_t *name)
  128. : m_shmem (open_only
  129. ,name
  130. ,read_write
  131. ,0
  132. ,construct_func_t(DoOpen, 0))
  133. {}
  134. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  135. inline void shm_named_semaphore::post()
  136. { semaphore()->post(); }
  137. inline void shm_named_semaphore::wait()
  138. { semaphore()->wait(); }
  139. inline bool shm_named_semaphore::try_wait()
  140. { return semaphore()->try_wait(); }
  141. template<class TimePoint>
  142. inline bool shm_named_semaphore::timed_wait(const TimePoint &abs_time)
  143. { return semaphore()->timed_wait(abs_time); }
  144. inline bool shm_named_semaphore::remove(const char *name)
  145. { return shared_memory_object::remove(name); }
  146. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  147. inline bool shm_named_semaphore::remove(const wchar_t *name)
  148. { return shared_memory_object::remove(name); }
  149. #endif
  150. } //namespace ipcdetail {
  151. } //namespace interprocess {
  152. } //namespace boost {
  153. #include <boost/interprocess/detail/config_end.hpp>
  154. #endif //BOOST_INTERPROCESS_SHM_NAMED_SEMAPHORE_HPP