mutex.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/interprocess/detail/posix_time_types_wrk.hpp>
  22. #include <boost/assert.hpp>
  23. #include <boost/interprocess/detail/atomic.hpp>
  24. #include <boost/cstdint.hpp>
  25. #include <boost/interprocess/detail/os_thread_functions.hpp>
  26. #include <boost/interprocess/sync/detail/common_algorithms.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. bool timed_lock(const boost::posix_time::ptime &abs_time);
  40. void unlock();
  41. void take_ownership(){}
  42. private:
  43. volatile boost::uint32_t m_s;
  44. struct common_lock_wrapper
  45. {
  46. common_lock_wrapper(spin_mutex &sp)
  47. : m_sp(sp)
  48. {}
  49. void lock()
  50. {
  51. ipcdetail::try_based_lock(m_sp);
  52. }
  53. bool timed_lock(const boost::posix_time::ptime &abs_time)
  54. { return m_sp.timed_lock(abs_time); }
  55. spin_mutex &m_sp;
  56. };
  57. };
  58. inline spin_mutex::spin_mutex()
  59. : m_s(0)
  60. {
  61. //Note that this class is initialized to zero.
  62. //So zeroed memory can be interpreted as an
  63. //initialized mutex
  64. }
  65. inline spin_mutex::~spin_mutex()
  66. {
  67. //Trivial destructor
  68. }
  69. inline void spin_mutex::lock(void)
  70. {
  71. common_lock_wrapper clw(*this);
  72. ipcdetail::timeout_when_locking_aware_lock(clw);
  73. }
  74. inline bool spin_mutex::try_lock(void)
  75. {
  76. boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
  77. return m_s == 1 && prev_s == 0;
  78. }
  79. inline bool spin_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  80. { return ipcdetail::try_based_timed_lock(*this, abs_time); }
  81. inline void spin_mutex::unlock(void)
  82. { ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1); }
  83. } //namespace ipcdetail {
  84. } //namespace interprocess {
  85. } //namespace boost {
  86. #include <boost/interprocess/detail/config_end.hpp>
  87. #endif //BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP