recursive_mutex.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/ptime_to_timespec.hpp>
  41. #include <boost/interprocess/detail/posix_time_types_wrk.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. bool timed_lock(const boost::posix_time::ptime &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. if (pthread_mutex_lock(&m_mut) != 0)
  79. throw lock_exception();
  80. }
  81. inline bool posix_recursive_mutex::try_lock()
  82. {
  83. int res = pthread_mutex_trylock(&m_mut);
  84. if (!(res == 0 || res == EBUSY))
  85. throw lock_exception();
  86. return res == 0;
  87. }
  88. inline bool posix_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  89. {
  90. #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
  91. //Posix does not support infinity absolute time so handle it here
  92. if(abs_time.is_pos_infinity()){
  93. this->lock();
  94. return true;
  95. }
  96. timespec ts = ptime_to_timespec(abs_time);
  97. int res = pthread_mutex_timedlock(&m_mut, &ts);
  98. if (res != 0 && res != ETIMEDOUT)
  99. throw lock_exception();
  100. return res == 0;
  101. #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS
  102. return ipcdetail::try_based_timed_lock(*this, abs_time);
  103. #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS
  104. }
  105. inline void posix_recursive_mutex::unlock()
  106. {
  107. int res = 0;
  108. res = pthread_mutex_unlock(&m_mut);
  109. BOOST_ASSERT(res == 0); (void)res;
  110. }
  111. } //namespace ipcdetail {
  112. } //namespace interprocess {
  113. } //namespace boost {
  114. #include <boost/interprocess/detail/config_end.hpp>
  115. #endif //#ifndef BOOST_INTERPROCESS_DETAIL_POSIX_RECURSIVE_MUTEX_HPP