/Src/Dependencies/Boost/boost/interprocess/sync/emulation/mutex.hpp

http://hadesmem.googlecode.com/ · C++ Header · 114 lines · 78 code · 20 blank · 16 comment · 13 complexity · 31337534ff1fe5a0844afef718f46eee MD5 · raw file

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2009. 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_EMULATION_MUTEX_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_EMULATION_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 detail {
  25. class emulation_mutex
  26. {
  27. emulation_mutex(const emulation_mutex &);
  28. emulation_mutex &operator=(const emulation_mutex &);
  29. public:
  30. emulation_mutex();
  31. ~emulation_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 emulation_mutex::emulation_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 emulation_mutex::~emulation_mutex()
  48. {
  49. //Trivial destructor
  50. }
  51. inline void emulation_mutex::lock(void)
  52. {
  53. do{
  54. boost::uint32_t prev_s = detail::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. detail::thread_yield();
  60. }while (true);
  61. }
  62. inline bool emulation_mutex::try_lock(void)
  63. {
  64. boost::uint32_t prev_s = detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
  65. return m_s == 1 && prev_s == 0;
  66. }
  67. inline bool emulation_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. if(now >= abs_time) return false;
  76. do{
  77. if(this->try_lock()){
  78. break;
  79. }
  80. now = microsec_clock::universal_time();
  81. if(now >= abs_time){
  82. return false;
  83. }
  84. // relinquish current time slice
  85. detail::thread_yield();
  86. }while (true);
  87. return true;
  88. }
  89. inline void emulation_mutex::unlock(void)
  90. { detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1); }
  91. } //namespace detail {
  92. } //namespace interprocess {
  93. } //namespace boost {
  94. #endif //BOOST_INTERPROCESS_DETAIL_EMULATION_MUTEX_HPP