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