mutex.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. namespace boost {
  23. namespace interprocess {
  24. namespace ipcdetail {
  25. class spin_mutex
  26. {
  27. spin_mutex(const spin_mutex &);
  28. spin_mutex &operator=(const spin_mutex &);
  29. public:
  30. spin_mutex();
  31. ~spin_mutex();
  32. void lock();
  33. bool try_lock();
  34. bool timed_lock(const boost::posix_time::ptime &abs_time);
  35. void unlock();
  36. void take_ownership(){};
  37. private:
  38. volatile boost::uint32_t m_s;
  39. };
  40. inline spin_mutex::spin_mutex()
  41. : m_s(0)
  42. {
  43. //Note that this class is initialized to zero.
  44. //So zeroed memory can be interpreted as an
  45. //initialized mutex
  46. }
  47. inline spin_mutex::~spin_mutex()
  48. {
  49. //Trivial destructor
  50. }
  51. inline void spin_mutex::lock(void)
  52. {
  53. do{
  54. boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
  55. if (m_s == 1 && prev_s == 0){
  56. break;
  57. }
  58. // relinquish current timeslice
  59. ipcdetail::thread_yield();
  60. }while (true);
  61. }
  62. inline bool spin_mutex::try_lock(void)
  63. {
  64. boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
  65. return m_s == 1 && prev_s == 0;
  66. }
  67. inline bool spin_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  68. {
  69. if(abs_time == boost::posix_time::pos_infin){
  70. this->lock();
  71. return true;
  72. }
  73. //Obtain current count and target time
  74. boost::posix_time::ptime now = microsec_clock::universal_time();
  75. do{
  76. if(this->try_lock()){
  77. break;
  78. }
  79. now = microsec_clock::universal_time();
  80. if(now >= abs_time){
  81. return false;
  82. }
  83. // relinquish current time slice
  84. ipcdetail::thread_yield();
  85. }while (true);
  86. return true;
  87. }
  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