mutex.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. };
  45. inline spin_mutex::spin_mutex()
  46. : m_s(0)
  47. {
  48. //Note that this class is initialized to zero.
  49. //So zeroed memory can be interpreted as an
  50. //initialized mutex
  51. }
  52. inline spin_mutex::~spin_mutex()
  53. {
  54. //Trivial destructor
  55. }
  56. inline void spin_mutex::lock(void)
  57. {
  58. #ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING
  59. boost::posix_time::ptime wait_time
  60. = microsec_clock::universal_time()
  61. + boost::posix_time::milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS);
  62. if (!timed_lock(wait_time))
  63. {
  64. throw interprocess_exception(timeout_when_locking_error
  65. , "Interprocess mutex timeout when locking. Possible deadlock: "
  66. "owner died without unlocking?");
  67. }
  68. #else
  69. return ipcdetail::try_based_lock(*this);
  70. #endif
  71. }
  72. inline bool spin_mutex::try_lock(void)
  73. {
  74. boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
  75. return m_s == 1 && prev_s == 0;
  76. }
  77. inline bool spin_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  78. { return ipcdetail::try_based_timed_lock(*this, abs_time); }
  79. inline void spin_mutex::unlock(void)
  80. { ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1); }
  81. } //namespace ipcdetail {
  82. } //namespace interprocess {
  83. } //namespace boost {
  84. #include <boost/interprocess/detail/config_end.hpp>
  85. #endif //BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP