recursive_mutex.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #ifndef BOOST_CONFIG_HPP
  29. # include <boost/config.hpp>
  30. #endif
  31. #
  32. #if defined(BOOST_HAS_PRAGMA_ONCE)
  33. # pragma once
  34. #endif
  35. #include <boost/interprocess/detail/config_begin.hpp>
  36. #include <boost/interprocess/detail/workaround.hpp>
  37. #include <pthread.h>
  38. #include <errno.h>
  39. #include <boost/interprocess/sync/posix/pthread_helpers.hpp>
  40. #include <boost/interprocess/sync/posix/timepoint_to_timespec.hpp>
  41. #include <boost/interprocess/timed_utils.hpp>
  42. #include <boost/interprocess/exceptions.hpp>
  43. #ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS
  44. # include <boost/interprocess/detail/os_thread_functions.hpp>
  45. # include <boost/interprocess/sync/detail/common_algorithms.hpp>
  46. #endif
  47. #include <boost/assert.hpp>
  48. namespace boost {
  49. namespace interprocess {
  50. namespace ipcdetail {
  51. class posix_recursive_mutex
  52. {
  53. posix_recursive_mutex(const posix_recursive_mutex &);
  54. posix_recursive_mutex &operator=(const posix_recursive_mutex &);
  55. public:
  56. posix_recursive_mutex();
  57. ~posix_recursive_mutex();
  58. void lock();
  59. bool try_lock();
  60. template<class TimePoint> bool timed_lock(const TimePoint &abs_time);
  61. void unlock();
  62. private:
  63. pthread_mutex_t m_mut;
  64. };
  65. inline posix_recursive_mutex::posix_recursive_mutex()
  66. {
  67. mutexattr_wrapper mut_attr(true);
  68. mutex_initializer mut(m_mut, mut_attr);
  69. mut.release();
  70. }
  71. inline posix_recursive_mutex::~posix_recursive_mutex()
  72. {
  73. int res = pthread_mutex_destroy(&m_mut);
  74. BOOST_ASSERT(res == 0);(void)res;
  75. }
  76. inline void posix_recursive_mutex::lock()
  77. {
  78. int res = pthread_mutex_lock(&m_mut);
  79. #ifdef BOOST_INTERPROCESS_POSIX_ROBUST_MUTEXES
  80. if (res == EOWNERDEAD)
  81. {
  82. //We can't inform the application and data might
  83. //corrupted, so be safe and mark the mutex as not recoverable
  84. //so applications can act accordingly.
  85. pthread_mutex_unlock(&m_mut);
  86. throw lock_exception(not_recoverable);
  87. }
  88. else if (res == ENOTRECOVERABLE)
  89. throw lock_exception(not_recoverable);
  90. #endif
  91. if (res != 0)
  92. throw lock_exception();
  93. }
  94. inline bool posix_recursive_mutex::try_lock()
  95. {
  96. int res = pthread_mutex_trylock(&m_mut);
  97. #ifdef BOOST_INTERPROCESS_POSIX_ROBUST_MUTEXES
  98. if (res == EOWNERDEAD)
  99. {
  100. //We can't inform the application and data might
  101. //corrupted, so be safe and mark the mutex as not recoverable
  102. //so applications can act accordingly.
  103. pthread_mutex_unlock(&m_mut);
  104. throw lock_exception(not_recoverable);
  105. }
  106. else if (res == ENOTRECOVERABLE)
  107. throw lock_exception(not_recoverable);
  108. #endif
  109. if (!(res == 0 || res == EBUSY))
  110. throw lock_exception();
  111. return (res == 0);
  112. }
  113. template<class TimePoint>
  114. inline bool posix_recursive_mutex::timed_lock(const TimePoint &abs_time)
  115. {
  116. #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
  117. //Posix does not support infinity absolute time so handle it here
  118. if(ipcdetail::is_pos_infinity(abs_time)){
  119. this->lock();
  120. return true;
  121. }
  122. timespec ts = timepoint_to_timespec(abs_time);
  123. int res = pthread_mutex_timedlock(&m_mut, &ts);
  124. #ifdef BOOST_INTERPROCESS_POSIX_ROBUST_MUTEXES
  125. if (res == EOWNERDEAD)
  126. {
  127. //We can't inform the application and data might
  128. //corrupted, so be safe and mark the mutex as not recoverable
  129. //so applications can act accordingly.
  130. pthread_mutex_unlock(&m_mut);
  131. throw lock_exception(not_recoverable);
  132. }
  133. else if (res == ENOTRECOVERABLE)
  134. throw lock_exception(not_recoverable);
  135. #endif
  136. if (res != 0 && res != ETIMEDOUT)
  137. throw lock_exception();
  138. return res == 0;
  139. #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS
  140. return ipcdetail::try_based_timed_lock(*this, abs_time);
  141. #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS
  142. }
  143. inline void posix_recursive_mutex::unlock()
  144. {
  145. int res = 0;
  146. res = pthread_mutex_unlock(&m_mut);
  147. BOOST_ASSERT(res == 0); (void)res;
  148. }
  149. } //namespace ipcdetail {
  150. } //namespace interprocess {
  151. } //namespace boost {
  152. #include <boost/interprocess/detail/config_end.hpp>
  153. #endif //#ifndef BOOST_INTERPROCESS_DETAIL_POSIX_RECURSIVE_MUTEX_HPP