mutex.hpp 5.4 KB

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