recursive_mutex.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_RECURSIVE_MUTEX_HPP
  27. #define BOOST_INTERPROCESS_DETAIL_POSIX_RECURSIVE_MUTEX_HPP
  28. #include <boost/interprocess/detail/config_begin.hpp>
  29. #include <boost/interprocess/detail/workaround.hpp>
  30. #include <pthread.h>
  31. #include <errno.h>
  32. #include <boost/interprocess/sync/posix/pthread_helpers.hpp>
  33. #include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
  34. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  35. #include <boost/interprocess/exceptions.hpp>
  36. #ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS
  37. # include <boost/interprocess/detail/os_thread_functions.hpp>
  38. # include <boost/interprocess/sync/spin/wait.hpp>
  39. #endif
  40. #include <boost/assert.hpp>
  41. namespace boost {
  42. namespace interprocess {
  43. namespace ipcdetail {
  44. class posix_recursive_mutex
  45. {
  46. posix_recursive_mutex(const posix_recursive_mutex &);
  47. posix_recursive_mutex &operator=(const posix_recursive_mutex &);
  48. public:
  49. posix_recursive_mutex();
  50. ~posix_recursive_mutex();
  51. void lock();
  52. bool try_lock();
  53. bool timed_lock(const boost::posix_time::ptime &abs_time);
  54. void unlock();
  55. private:
  56. pthread_mutex_t m_mut;
  57. };
  58. inline posix_recursive_mutex::posix_recursive_mutex()
  59. {
  60. mutexattr_wrapper mut_attr(true);
  61. mutex_initializer mut(m_mut, mut_attr);
  62. mut.release();
  63. }
  64. inline posix_recursive_mutex::~posix_recursive_mutex()
  65. {
  66. int res = pthread_mutex_destroy(&m_mut);
  67. BOOST_ASSERT(res == 0);(void)res;
  68. }
  69. inline void posix_recursive_mutex::lock()
  70. {
  71. if (pthread_mutex_lock(&m_mut) != 0)
  72. throw lock_exception();
  73. }
  74. inline bool posix_recursive_mutex::try_lock()
  75. {
  76. int res = pthread_mutex_trylock(&m_mut);
  77. if (!(res == 0 || res == EBUSY))
  78. throw lock_exception();
  79. return res == 0;
  80. }
  81. inline bool posix_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  82. {
  83. if(abs_time == boost::posix_time::pos_infin){
  84. this->lock();
  85. return true;
  86. }
  87. #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
  88. timespec ts = ptime_to_timespec(abs_time);
  89. int res = pthread_mutex_timedlock(&m_mut, &ts);
  90. if (res != 0 && res != ETIMEDOUT)
  91. throw lock_exception();
  92. return res == 0;
  93. #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS
  94. //Obtain current count and target time
  95. boost::posix_time::ptime now = microsec_clock::universal_time();
  96. spin_wait swait;
  97. do{
  98. if(this->try_lock()){
  99. break;
  100. }
  101. now = microsec_clock::universal_time();
  102. if(now >= abs_time){
  103. return false;
  104. }
  105. // relinquish current time slice
  106. swait.yield();
  107. }while (true);
  108. return true;
  109. #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS
  110. }
  111. inline void posix_recursive_mutex::unlock()
  112. {
  113. int res = 0;
  114. res = pthread_mutex_unlock(&m_mut);
  115. BOOST_ASSERT(res == 0); (void)res;
  116. }
  117. } //namespace ipcdetail {
  118. } //namespace interprocess {
  119. } //namespace boost {
  120. #include <boost/interprocess/detail/config_end.hpp>
  121. #endif //#ifndef BOOST_INTERPROCESS_DETAIL_POSIX_RECURSIVE_MUTEX_HPP