mutex.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. //
  11. // Parts of the pthread code come from Boost Threads code:
  12. //
  13. //////////////////////////////////////////////////////////////////////////////
  14. //
  15. // Copyright (C) 2001-2003
  16. // William E. Kempf
  17. //
  18. // Permission to use, copy, modify, distribute and sell this software
  19. // and its documentation for any purpose is hereby granted without fee,
  20. // provided that the above copyright notice appear in all copies and
  21. // that both that copyright notice and this permission notice appear
  22. // in supporting documentation. William E. Kempf makes no representations
  23. // about the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied warranty.
  25. //////////////////////////////////////////////////////////////////////////////
  26. #ifndef BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP
  27. #define BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP
  28. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  29. # pragma once
  30. #endif
  31. #include <boost/interprocess/detail/config_begin.hpp>
  32. #include <boost/interprocess/detail/workaround.hpp>
  33. #include <pthread.h>
  34. #include <errno.h>
  35. #include <boost/interprocess/exceptions.hpp>
  36. #include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
  37. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  38. #include <boost/interprocess/exceptions.hpp>
  39. #include <boost/interprocess/sync/posix/pthread_helpers.hpp>
  40. #ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS
  41. # include <boost/interprocess/detail/os_thread_functions.hpp>
  42. # include <boost/interprocess/sync/spin/wait.hpp>
  43. #endif
  44. #include <boost/assert.hpp>
  45. namespace boost {
  46. namespace interprocess {
  47. namespace ipcdetail {
  48. class posix_condition;
  49. class posix_mutex
  50. {
  51. posix_mutex(const posix_mutex &);
  52. posix_mutex &operator=(const posix_mutex &);
  53. public:
  54. posix_mutex();
  55. ~posix_mutex();
  56. void lock();
  57. bool try_lock();
  58. bool timed_lock(const boost::posix_time::ptime &abs_time);
  59. void unlock();
  60. friend class posix_condition;
  61. private:
  62. pthread_mutex_t m_mut;
  63. };
  64. inline posix_mutex::posix_mutex()
  65. {
  66. mutexattr_wrapper mut_attr;
  67. mutex_initializer mut(m_mut, mut_attr);
  68. mut.release();
  69. }
  70. inline posix_mutex::~posix_mutex()
  71. {
  72. int res = pthread_mutex_destroy(&m_mut);
  73. BOOST_ASSERT(res == 0);(void)res;
  74. }
  75. inline void posix_mutex::lock()
  76. {
  77. if (pthread_mutex_lock(&m_mut) != 0)
  78. throw lock_exception();
  79. }
  80. inline bool posix_mutex::try_lock()
  81. {
  82. int res = pthread_mutex_trylock(&m_mut);
  83. if (!(res == 0 || res == EBUSY))
  84. throw lock_exception();
  85. return res == 0;
  86. }
  87. inline bool posix_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  88. {
  89. if(abs_time == boost::posix_time::pos_infin){
  90. this->lock();
  91. return true;
  92. }
  93. #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
  94. timespec ts = ptime_to_timespec(abs_time);
  95. int res = pthread_mutex_timedlock(&m_mut, &ts);
  96. if (res != 0 && res != ETIMEDOUT)
  97. throw lock_exception();
  98. return res == 0;
  99. #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS
  100. //Obtain current count and target time
  101. boost::posix_time::ptime now = microsec_clock::universal_time();
  102. spin_wait swait;
  103. do{
  104. if(this->try_lock()){
  105. break;
  106. }
  107. now = microsec_clock::universal_time();
  108. if(now >= abs_time){
  109. return false;
  110. }
  111. // relinquish current time slice
  112. swait.yield();
  113. }while (true);
  114. return true;
  115. #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS
  116. }
  117. inline void posix_mutex::unlock()
  118. {
  119. int res = 0;
  120. res = pthread_mutex_unlock(&m_mut);
  121. (void)res;
  122. BOOST_ASSERT(res == 0);
  123. }
  124. } //namespace ipcdetail {
  125. } //namespace interprocess {
  126. } //namespace boost {
  127. #include <boost/interprocess/detail/config_end.hpp>
  128. #endif //#ifndef BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP