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/detail/posix_time_types_wrk.hpp>
  24. #include <boost/interprocess/sync/detail/common_algorithms.hpp>
  25. #include <boost/interprocess/sync/detail/locks.hpp>
  26. #include <boost/cstdint.hpp>
  27. namespace boost {
  28. namespace interprocess {
  29. namespace ipcdetail {
  30. class spin_semaphore
  31. {
  32. spin_semaphore(const spin_semaphore &);
  33. spin_semaphore &operator=(const spin_semaphore &);
  34. public:
  35. spin_semaphore(unsigned int initialCount);
  36. ~spin_semaphore();
  37. void post();
  38. void wait();
  39. bool try_wait();
  40. bool timed_wait(const boost::posix_time::ptime &abs_time);
  41. // int get_count() const;
  42. private:
  43. volatile boost::uint32_t m_count;
  44. };
  45. inline spin_semaphore::~spin_semaphore()
  46. {}
  47. inline spin_semaphore::spin_semaphore(unsigned int initialCount)
  48. { ipcdetail::atomic_write32(&this->m_count, boost::uint32_t(initialCount)); }
  49. inline void spin_semaphore::post()
  50. {
  51. ipcdetail::atomic_inc32(&m_count);
  52. }
  53. inline void spin_semaphore::wait()
  54. {
  55. ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
  56. return ipcdetail::try_based_lock(lw);
  57. }
  58. inline bool spin_semaphore::try_wait()
  59. {
  60. return ipcdetail::atomic_add_unless32(&m_count, boost::uint32_t(-1), boost::uint32_t(0));
  61. }
  62. inline bool spin_semaphore::timed_wait(const boost::posix_time::ptime &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