mutex.hpp 3.0 KB

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