mutex.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif
  15. #include <boost/interprocess/detail/config_begin.hpp>
  16. #include <boost/interprocess/detail/workaround.hpp>
  17. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  18. #include <boost/interprocess/detail/win32_api.hpp>
  19. #include <boost/interprocess/detail/windows_intermodule_singleton.hpp>
  20. #include <boost/interprocess/sync/windows/sync_utils.hpp>
  21. #include <boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp>
  22. #include <boost/interprocess/exceptions.hpp>
  23. namespace boost {
  24. namespace interprocess {
  25. namespace ipcdetail {
  26. class windows_mutex
  27. {
  28. windows_mutex(const windows_mutex &);
  29. windows_mutex &operator=(const windows_mutex &);
  30. public:
  31. windows_mutex();
  32. ~windows_mutex();
  33. void lock();
  34. bool try_lock();
  35. bool timed_lock(const boost::posix_time::ptime &abs_time);
  36. void unlock();
  37. void take_ownership(){};
  38. private:
  39. const sync_id id_;
  40. };
  41. inline windows_mutex::windows_mutex()
  42. : id_(this)
  43. {
  44. sync_handles &handles =
  45. windows_intermodule_singleton<sync_handles>::get();
  46. //Create mutex with the initial count
  47. bool open_or_created;
  48. (void)handles.obtain_mutex(this->id_, &open_or_created);
  49. //The mutex must be created, never opened
  50. assert(open_or_created);
  51. assert(open_or_created && winapi::get_last_error() != winapi::error_already_exists);
  52. (void)open_or_created;
  53. }
  54. inline windows_mutex::~windows_mutex()
  55. {
  56. sync_handles &handles =
  57. windows_intermodule_singleton<sync_handles>::get();
  58. handles.destroy_handle(this->id_);
  59. }
  60. inline void windows_mutex::lock(void)
  61. {
  62. sync_handles &handles =
  63. windows_intermodule_singleton<sync_handles>::get();
  64. //This can throw
  65. winapi_mutex_functions mut(handles.obtain_mutex(this->id_));
  66. mut.lock();
  67. }
  68. inline bool windows_mutex::try_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_));
  74. return mut.try_lock();
  75. }
  76. inline bool windows_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  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_));
  82. return mut.timed_lock(abs_time);
  83. }
  84. inline void windows_mutex::unlock(void)
  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_));
  90. return mut.unlock();
  91. }
  92. } //namespace ipcdetail {
  93. } //namespace interprocess {
  94. } //namespace boost {
  95. #include <boost/interprocess/detail/config_end.hpp>
  96. #endif //BOOST_INTERPROCESS_DETAIL_WINDOWS_MUTEX_HPP