/Src/Dependencies/Boost/boost/interprocess/sync/posix/interprocess_mutex.hpp

http://hadesmem.googlecode.com/ · C++ Header · 111 lines · 68 code · 16 blank · 27 comment · 19 complexity · 5f4201bdcd5b378907347a1d28c03c17 MD5 · raw file

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2009. 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. #include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
  27. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  28. #include <boost/interprocess/exceptions.hpp>
  29. #ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS
  30. # include <boost/interprocess/detail/os_thread_functions.hpp>
  31. #endif
  32. #include <boost/assert.hpp>
  33. namespace boost {
  34. namespace interprocess {
  35. inline interprocess_mutex::interprocess_mutex()
  36. {
  37. detail::mutexattr_wrapper mut_attr;
  38. detail::mutex_initializer mut(m_mut, mut_attr);
  39. mut.release();
  40. }
  41. inline interprocess_mutex::~interprocess_mutex()
  42. {
  43. int res = pthread_mutex_destroy(&m_mut);
  44. BOOST_ASSERT(res == 0);(void)res;
  45. }
  46. inline void interprocess_mutex::lock()
  47. {
  48. if (pthread_mutex_lock(&m_mut) != 0)
  49. throw lock_exception();
  50. }
  51. inline bool interprocess_mutex::try_lock()
  52. {
  53. int res = pthread_mutex_trylock(&m_mut);
  54. if (!(res == 0 || res == EBUSY))
  55. throw lock_exception();
  56. return res == 0;
  57. }
  58. inline bool interprocess_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  59. {
  60. if(abs_time == boost::posix_time::pos_infin){
  61. this->lock();
  62. return true;
  63. }
  64. #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
  65. timespec ts = detail::ptime_to_timespec(abs_time);
  66. int res = pthread_mutex_timedlock(&m_mut, &ts);
  67. if (res != 0 && res != ETIMEDOUT)
  68. throw lock_exception();
  69. return res == 0;
  70. #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS
  71. //Obtain current count and target time
  72. boost::posix_time::ptime now = microsec_clock::universal_time();
  73. if(now >= abs_time) return false;
  74. do{
  75. if(this->try_lock()){
  76. break;
  77. }
  78. now = microsec_clock::universal_time();
  79. if(now >= abs_time){
  80. return false;
  81. }
  82. // relinquish current time slice
  83. detail::thread_yield();
  84. }while (true);
  85. return true;
  86. #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS
  87. }
  88. inline void interprocess_mutex::unlock()
  89. {
  90. int res = 0;
  91. res = pthread_mutex_unlock(&m_mut);
  92. BOOST_ASSERT(res == 0);
  93. }
  94. } //namespace interprocess {
  95. } //namespace boost {