mutex.hpp 2.9 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. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif
  15. #include <boost/interprocess/detail/config_begin.hpp>
  16. #include <boost/interprocess/detail/workaround.hpp>
  17. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  18. #include <boost/assert.hpp>
  19. #include <boost/interprocess/detail/atomic.hpp>
  20. #include <boost/cstdint.hpp>
  21. #include <boost/interprocess/detail/os_thread_functions.hpp>
  22. #include <boost/interprocess/sync/spin/wait.hpp>
  23. namespace boost {
  24. namespace interprocess {
  25. namespace ipcdetail {
  26. class spin_mutex
  27. {
  28. spin_mutex(const spin_mutex &);
  29. spin_mutex &operator=(const spin_mutex &);
  30. public:
  31. spin_mutex();
  32. ~spin_mutex();
  33. void lock();
  34. bool try_lock();
  35. bool timed_lock(const boost::posix_time::ptime &abs_time);
  36. void unlock();
  37. void take_ownership(){};
  38. private:
  39. volatile boost::uint32_t m_s;
  40. };
  41. inline spin_mutex::spin_mutex()
  42. : m_s(0)
  43. {
  44. //Note that this class is initialized to zero.
  45. //So zeroed memory can be interpreted as an
  46. //initialized mutex
  47. }
  48. inline spin_mutex::~spin_mutex()
  49. {
  50. //Trivial destructor
  51. }
  52. inline void spin_mutex::lock(void)
  53. {
  54. spin_wait swait;
  55. do{
  56. boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
  57. if (m_s == 1 && prev_s == 0){
  58. break;
  59. }
  60. // relinquish current timeslice
  61. swait.yield();
  62. }while (true);
  63. }
  64. inline bool spin_mutex::try_lock(void)
  65. {
  66. boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
  67. return m_s == 1 && prev_s == 0;
  68. }
  69. inline bool spin_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  70. {
  71. if(abs_time == boost::posix_time::pos_infin){
  72. this->lock();
  73. return true;
  74. }
  75. //Obtain current count and target time
  76. boost::posix_time::ptime now = microsec_clock::universal_time();
  77. spin_wait swait;
  78. do{
  79. if(this->try_lock()){
  80. break;
  81. }
  82. now = microsec_clock::universal_time();
  83. if(now >= abs_time){
  84. return false;
  85. }
  86. // relinquish current time slice
  87. swait.yield();
  88. }while (true);
  89. return true;
  90. }
  91. inline void spin_mutex::unlock(void)
  92. { ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1); }
  93. } //namespace ipcdetail {
  94. } //namespace interprocess {
  95. } //namespace boost {
  96. #include <boost/interprocess/detail/config_end.hpp>
  97. #endif //BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP