1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_DETAIL_WINDOWS_MUTEX_HPP
12 #define BOOST_INTERPROCESS_DETAIL_WINDOWS_MUTEX_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
24 #include <boost/interprocess/detail/win32_api.hpp>
25 #include <boost/interprocess/detail/windows_intermodule_singleton.hpp>
26 #include <boost/interprocess/sync/windows/sync_utils.hpp>
27 #include <boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp>
28 #include <boost/interprocess/exceptions.hpp>
32 namespace interprocess {
37 winapi_mutex(const winapi_mutex &);
38 winapi_mutex &operator=(const winapi_mutex &);
46 template<class TimePoint> bool timed_lock(const TimePoint &abs_time);
48 template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
49 { return this->timed_lock(abs_time); }
51 template<class Duration> bool try_lock_for(const Duration &dur)
52 { return this->timed_lock(duration_to_ustime(dur)); }
55 void take_ownership(){};
61 inline winapi_mutex::winapi_mutex()
64 sync_handles &handles =
65 windows_intermodule_singleton<sync_handles>::get();
66 //Create mutex with the initial count
68 (void)handles.obtain_mutex(this->id_, this, &open_or_created);
69 //The mutex must be created, never opened
70 BOOST_ASSERT(open_or_created);
71 BOOST_ASSERT(open_or_created && winapi::get_last_error() != winapi::error_already_exists);
72 (void)open_or_created;
75 inline winapi_mutex::~winapi_mutex()
77 sync_handles &handles =
78 windows_intermodule_singleton<sync_handles>::get();
79 handles.destroy_handle(this->id_, this);
82 inline void winapi_mutex::lock(void)
84 sync_handles &handles =
85 windows_intermodule_singleton<sync_handles>::get();
87 winapi_mutex_functions mut(handles.obtain_mutex(this->id_, this));
91 inline bool winapi_mutex::try_lock(void)
93 sync_handles &handles =
94 windows_intermodule_singleton<sync_handles>::get();
96 winapi_mutex_functions mut(handles.obtain_mutex(this->id_, this));
97 return mut.try_lock();
100 template<class TimePoint>
101 inline bool winapi_mutex::timed_lock(const TimePoint &abs_time)
103 sync_handles &handles =
104 windows_intermodule_singleton<sync_handles>::get();
106 winapi_mutex_functions mut(handles.obtain_mutex(this->id_, this));
107 return mut.timed_lock(abs_time);
110 inline void winapi_mutex::unlock(void)
112 sync_handles &handles =
113 windows_intermodule_singleton<sync_handles>::get();
115 winapi_mutex_functions mut(handles.obtain_mutex(this->id_, this));
119 } //namespace ipcdetail {
120 } //namespace interprocess {
121 } //namespace boost {
123 #include <boost/interprocess/detail/config_end.hpp>
125 #endif //BOOST_INTERPROCESS_DETAIL_WINDOWS_MUTEX_HPP