mutex.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_MUTEX_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_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/assert.hpp>
  22. #include <boost/interprocess/detail/atomic.hpp>
  23. #include <boost/cstdint.hpp>
  24. #include <boost/interprocess/detail/os_thread_functions.hpp>
  25. #include <boost/interprocess/sync/detail/common_algorithms.hpp>
  26. #include <boost/interprocess/timed_utils.hpp>
  27. namespace boost {
  28. namespace interprocess {
  29. namespace ipcdetail {
  30. class spin_mutex
  31. {
  32. spin_mutex(const spin_mutex &);
  33. spin_mutex &operator=(const spin_mutex &);
  34. public:
  35. spin_mutex();
  36. ~spin_mutex();
  37. void lock();
  38. bool try_lock();
  39. template<class TimePoint>
  40. bool timed_lock(const TimePoint &abs_time);
  41. template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
  42. { return this->timed_lock(abs_time); }
  43. template<class Duration> bool try_lock_for(const Duration &dur)
  44. { return this->timed_lock(duration_to_ustime(dur)); }
  45. void unlock();
  46. void take_ownership(){}
  47. private:
  48. volatile boost::uint32_t m_s;
  49. struct common_lock_wrapper
  50. {
  51. common_lock_wrapper(spin_mutex &sp)
  52. : m_sp(sp)
  53. {}
  54. void lock()
  55. {
  56. ipcdetail::try_based_lock(m_sp);
  57. }
  58. template<class TimePoint>
  59. bool timed_lock(const TimePoint &abs_time)
  60. { return m_sp.timed_lock(abs_time); }
  61. spin_mutex &m_sp;
  62. };
  63. };
  64. inline spin_mutex::spin_mutex()
  65. : m_s(0)
  66. {
  67. //Note that this class is initialized to zero.
  68. //So zeroed memory can be interpreted as an
  69. //initialized mutex
  70. }
  71. inline spin_mutex::~spin_mutex()
  72. {
  73. //Trivial destructor
  74. }
  75. inline void spin_mutex::lock(void)
  76. {
  77. common_lock_wrapper clw(*this);
  78. ipcdetail::timeout_when_locking_aware_lock(clw);
  79. }
  80. inline bool spin_mutex::try_lock(void)
  81. {
  82. boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
  83. return m_s == 1 && prev_s == 0;
  84. }
  85. template<class TimePoint>
  86. inline bool spin_mutex::timed_lock(const TimePoint &abs_time)
  87. { return ipcdetail::try_based_timed_lock(*this, abs_time); }
  88. inline void spin_mutex::unlock(void)
  89. { ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1); }
  90. } //namespace ipcdetail {
  91. } //namespace interprocess {
  92. } //namespace boost {
  93. #include <boost/interprocess/detail/config_end.hpp>
  94. #endif //BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP