mutex.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifndef BOOST_INTERPROCESS_DETAIL_WINDOWS_MUTEX_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_WINDOWS_MUTEX_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. #include <boost/interprocess/detail/win32_api.hpp>
  22. #include <boost/interprocess/detail/windows_intermodule_singleton.hpp>
  23. #include <boost/interprocess/sync/windows/sync_utils.hpp>
  24. #include <boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp>
  25. #include <boost/interprocess/exceptions.hpp>
  26. namespace boost {
  27. namespace interprocess {
  28. namespace ipcdetail {
  29. class winapi_mutex
  30. {
  31. winapi_mutex(const winapi_mutex &);
  32. winapi_mutex &operator=(const winapi_mutex &);
  33. public:
  34. winapi_mutex();
  35. ~winapi_mutex();
  36. void lock();
  37. bool try_lock();
  38. template<class TimePoint> bool timed_lock(const TimePoint &abs_time);
  39. template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
  40. { return this->timed_lock(abs_time); }
  41. template<class Duration> bool try_lock_for(const Duration &dur)
  42. { return this->timed_lock(duration_to_ustime(dur)); }
  43. void unlock();
  44. void take_ownership(){};
  45. private:
  46. const sync_id id_;
  47. };
  48. inline winapi_mutex::winapi_mutex()
  49. : id_()
  50. {
  51. sync_handles &handles =
  52. windows_intermodule_singleton<sync_handles>::get();
  53. //Create mutex with the initial count
  54. bool open_or_created;
  55. (void)handles.obtain_mutex(this->id_, this, &open_or_created);
  56. //The mutex must be created, never opened
  57. BOOST_ASSERT(open_or_created);
  58. BOOST_ASSERT(open_or_created && winapi::get_last_error() != winapi::error_already_exists);
  59. (void)open_or_created;
  60. }
  61. inline winapi_mutex::~winapi_mutex()
  62. {
  63. sync_handles &handles =
  64. windows_intermodule_singleton<sync_handles>::get();
  65. handles.destroy_handle(this->id_, this);
  66. }
  67. inline void winapi_mutex::lock(void)
  68. {
  69. sync_handles &handles =
  70. windows_intermodule_singleton<sync_handles>::get();
  71. //This can throw
  72. winapi_mutex_functions mut(handles.obtain_mutex(this->id_, this));
  73. mut.lock();
  74. }
  75. inline bool winapi_mutex::try_lock(void)
  76. {
  77. sync_handles &handles =
  78. windows_intermodule_singleton<sync_handles>::get();
  79. //This can throw
  80. winapi_mutex_functions mut(handles.obtain_mutex(this->id_, this));
  81. return mut.try_lock();
  82. }
  83. template<class TimePoint>
  84. inline bool winapi_mutex::timed_lock(const TimePoint &abs_time)
  85. {
  86. sync_handles &handles =
  87. windows_intermodule_singleton<sync_handles>::get();
  88. //This can throw
  89. winapi_mutex_functions mut(handles.obtain_mutex(this->id_, this));
  90. return mut.timed_lock(abs_time);
  91. }
  92. inline void winapi_mutex::unlock(void)
  93. {
  94. sync_handles &handles =
  95. windows_intermodule_singleton<sync_handles>::get();
  96. //This can throw
  97. winapi_mutex_functions mut(handles.obtain_mutex(this->id_, this));
  98. return mut.unlock();
  99. }
  100. } //namespace ipcdetail {
  101. } //namespace interprocess {
  102. } //namespace boost {
  103. #include <boost/interprocess/detail/config_end.hpp>
  104. #endif //BOOST_INTERPROCESS_DETAIL_WINDOWS_MUTEX_HPP