mutex.hpp 3.5 KB

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