mutex.hpp 3.9 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_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/ptime_to_timespec.hpp>
  41. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  42. #include <boost/interprocess/exceptions.hpp>
  43. #include <boost/interprocess/sync/posix/pthread_helpers.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. bool timed_lock(const boost::posix_time::ptime &abs_time);
  63. void unlock();
  64. friend class posix_condition;
  65. private:
  66. pthread_mutex_t m_mut;
  67. };
  68. inline posix_mutex::posix_mutex()
  69. {
  70. mutexattr_wrapper mut_attr;
  71. mutex_initializer mut(m_mut, mut_attr);
  72. mut.release();
  73. }
  74. inline posix_mutex::~posix_mutex()
  75. {
  76. int res = pthread_mutex_destroy(&m_mut);
  77. BOOST_ASSERT(res == 0);(void)res;
  78. }
  79. inline void posix_mutex::lock()
  80. {
  81. if (pthread_mutex_lock(&m_mut) != 0)
  82. throw lock_exception();
  83. }
  84. inline bool posix_mutex::try_lock()
  85. {
  86. int res = pthread_mutex_trylock(&m_mut);
  87. if (!(res == 0 || res == EBUSY))
  88. throw lock_exception();
  89. return res == 0;
  90. }
  91. inline bool posix_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  92. {
  93. #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
  94. //Posix does not support infinity absolute time so handle it here
  95. if(abs_time.is_pos_infinity()){
  96. this->lock();
  97. return true;
  98. }
  99. timespec ts = ptime_to_timespec(abs_time);
  100. int res = pthread_mutex_timedlock(&m_mut, &ts);
  101. if (res != 0 && res != ETIMEDOUT)
  102. throw lock_exception();
  103. return res == 0;
  104. #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS
  105. return ipcdetail::try_based_timed_lock(*this, abs_time);
  106. #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS
  107. }
  108. inline void posix_mutex::unlock()
  109. {
  110. int res = 0;
  111. res = pthread_mutex_unlock(&m_mut);
  112. (void)res;
  113. BOOST_ASSERT(res == 0);
  114. }
  115. } //namespace ipcdetail {
  116. } //namespace interprocess {
  117. } //namespace boost {
  118. #include <boost/interprocess/detail/config_end.hpp>
  119. #endif //#ifndef BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP