mutex.hpp 3.3 KB

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