mutex.hpp 4.1 KB

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