semaphore.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_DETAIL_SPIN_SEMAPHORE_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_SPIN_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/detail/atomic.hpp>
  22. #include <boost/interprocess/detail/os_thread_functions.hpp>
  23. #include <boost/interprocess/sync/detail/common_algorithms.hpp>
  24. #include <boost/interprocess/sync/detail/locks.hpp>
  25. #include <boost/cstdint.hpp>
  26. namespace boost {
  27. namespace interprocess {
  28. namespace ipcdetail {
  29. class spin_semaphore
  30. {
  31. spin_semaphore(const spin_semaphore &);
  32. spin_semaphore &operator=(const spin_semaphore &);
  33. public:
  34. spin_semaphore(unsigned int initialCount);
  35. ~spin_semaphore();
  36. void post();
  37. void wait();
  38. bool try_wait();
  39. template<class TimePoint> bool timed_wait(const TimePoint &abs_time);
  40. // int get_count() const;
  41. private:
  42. volatile boost::uint32_t m_count;
  43. };
  44. inline spin_semaphore::~spin_semaphore()
  45. {}
  46. inline spin_semaphore::spin_semaphore(unsigned int initialCount)
  47. { ipcdetail::atomic_write32(&this->m_count, boost::uint32_t(initialCount)); }
  48. inline void spin_semaphore::post()
  49. {
  50. ipcdetail::atomic_inc32(&m_count);
  51. }
  52. inline void spin_semaphore::wait()
  53. {
  54. ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
  55. return ipcdetail::try_based_lock(lw);
  56. }
  57. inline bool spin_semaphore::try_wait()
  58. {
  59. return ipcdetail::atomic_add_unless32(&m_count, boost::uint32_t(-1), boost::uint32_t(0));
  60. }
  61. template<class TimePoint>
  62. inline bool spin_semaphore::timed_wait(const TimePoint &abs_time)
  63. {
  64. ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
  65. return ipcdetail::try_based_timed_lock(lw, abs_time);
  66. }
  67. //inline int spin_semaphore::get_count() const
  68. //{
  69. //return (int)ipcdetail::atomic_read32(&m_count);
  70. //}
  71. } //namespace ipcdetail {
  72. } //namespace interprocess {
  73. } //namespace boost {
  74. #include <boost/interprocess/detail/config_end.hpp>
  75. #endif //BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP