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

http://hadesmem.googlecode.com/ · C++ Header · 148 lines · 70 code · 30 blank · 48 comment · 2 complexity · 43423de8ff9d909e84b9aff93bf0a172 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. #ifndef BOOST_INTERPROCESS_MUTEX_HPP
  15. #define BOOST_INTERPROCESS_MUTEX_HPP
  16. /// @cond
  17. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  18. # pragma once
  19. #endif
  20. #include <boost/interprocess/detail/config_begin.hpp>
  21. #include <boost/interprocess/detail/workaround.hpp>
  22. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  23. #include <boost/assert.hpp>
  24. #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
  25. #include <pthread.h>
  26. #include <errno.h>
  27. #include <boost/interprocess/sync/posix/pthread_helpers.hpp>
  28. #define BOOST_INTERPROCESS_USE_POSIX
  29. #else
  30. #include <boost/interprocess/sync/emulation/mutex.hpp>
  31. #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
  32. namespace boost {
  33. namespace interprocess {
  34. namespace detail{
  35. namespace robust_emulation_helpers {
  36. template<class T>
  37. class mutex_traits;
  38. }}}}
  39. #endif
  40. /// @endcond
  41. //!\file
  42. //!Describes a mutex class that can be placed in memory shared by
  43. //!several processes.
  44. namespace boost {
  45. namespace interprocess {
  46. class interprocess_condition;
  47. //!Wraps a interprocess_mutex that can be placed in shared memory and can be
  48. //!shared between processes. Allows timed lock tries
  49. class interprocess_mutex
  50. {
  51. /// @cond
  52. //Non-copyable
  53. interprocess_mutex(const interprocess_mutex &);
  54. interprocess_mutex &operator=(const interprocess_mutex &);
  55. friend class interprocess_condition;
  56. /// @endcond
  57. public:
  58. //!Constructor.
  59. //!Throws interprocess_exception on error.
  60. interprocess_mutex();
  61. //!Destructor. If any process uses the mutex after the destructor is called
  62. //!the result is undefined. Does not throw.
  63. ~interprocess_mutex();
  64. //!Effects: The calling thread tries to obtain ownership of the mutex, and
  65. //! if another thread has ownership of the mutex, it waits until it can
  66. //! obtain the ownership. If a thread takes ownership of the mutex the
  67. //! mutex must be unlocked by the same mutex.
  68. //!Throws: interprocess_exception on error.
  69. void lock();
  70. //!Effects: The calling thread tries to obtain ownership of the mutex, and
  71. //! if another thread has ownership of the mutex returns immediately.
  72. //!Returns: If the thread acquires ownership of the mutex, returns true, if
  73. //! the another thread has ownership of the mutex, returns false.
  74. //!Throws: interprocess_exception on error.
  75. bool try_lock();
  76. //!Effects: The calling thread will try to obtain exclusive ownership of the
  77. //! mutex if it can do so in until the specified time is reached. If the
  78. //! mutex supports recursive locking, the mutex must be unlocked the same
  79. //! number of times it is locked.
  80. //!Returns: If the thread acquires ownership of the mutex, returns true, if
  81. //! the timeout expires returns false.
  82. //!Throws: interprocess_exception on error.
  83. bool timed_lock(const boost::posix_time::ptime &abs_time);
  84. //!Effects: The calling thread releases the exclusive ownership of the mutex.
  85. //!Throws: interprocess_exception on error.
  86. void unlock();
  87. /// @cond
  88. private:
  89. #if defined(BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
  90. friend class detail::robust_emulation_helpers::mutex_traits<interprocess_mutex>;
  91. void take_ownership(){ mutex.take_ownership(); }
  92. detail::emulation_mutex mutex;
  93. #elif defined(BOOST_INTERPROCESS_USE_POSIX)
  94. pthread_mutex_t m_mut;
  95. #endif //#if (defined BOOST_INTERPROCESS_WINDOWS)
  96. /// @endcond
  97. };
  98. } //namespace interprocess {
  99. } //namespace boost {
  100. #ifdef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
  101. # undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
  102. namespace boost {
  103. namespace interprocess {
  104. inline interprocess_mutex::interprocess_mutex(){}
  105. inline interprocess_mutex::~interprocess_mutex(){}
  106. inline void interprocess_mutex::lock(){ mutex.lock(); }
  107. inline bool interprocess_mutex::try_lock(){ return mutex.try_lock(); }
  108. inline bool interprocess_mutex::timed_lock(const boost::posix_time::ptime &abs_time){ return mutex.timed_lock(abs_time); }
  109. inline void interprocess_mutex::unlock(){ mutex.unlock(); }
  110. } //namespace interprocess {
  111. } //namespace boost {
  112. #endif
  113. #ifdef BOOST_INTERPROCESS_USE_POSIX
  114. #include <boost/interprocess/sync/posix/interprocess_mutex.hpp>
  115. # undef BOOST_INTERPROCESS_USE_POSIX
  116. #endif
  117. #include <boost/interprocess/detail/config_end.hpp>
  118. #endif //BOOST_INTERPROCESS_MUTEX_HPP