1 ////////////////////////////////////////////////////////////////////////////// 2 // 3 // (C) Copyright Ion Gaztanaga 2011-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_WINAPI_MUTEX_WRAPPER_HPP 12 #define BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_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/creation_tags.hpp> 25 #include <boost/interprocess/permissions.hpp> 26 #include <boost/interprocess/detail/win32_api.hpp> 27 #include <boost/interprocess/detail/posix_time_types_wrk.hpp> 28 #include <boost/interprocess/sync/windows/winapi_wrapper_common.hpp> 29 #include <boost/interprocess/errors.hpp> 30 #include <boost/interprocess/exceptions.hpp> 31 #include <limits> 32 33 namespace boost { 34 namespace interprocess { 35 namespace ipcdetail { 36 37 class winapi_mutex_functions 38 { 39 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 40 41 //Non-copyable 42 winapi_mutex_functions(const winapi_mutex_functions &); 43 winapi_mutex_functions &operator=(const winapi_mutex_functions &); 44 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 45 46 public: winapi_mutex_functions(void * mtx_hnd)47 winapi_mutex_functions(void *mtx_hnd) 48 : m_mtx_hnd(mtx_hnd) 49 {} 50 unlock()51 void unlock() 52 { winapi::release_mutex(m_mtx_hnd); } 53 lock()54 void lock() 55 { return winapi_wrapper_wait_for_single_object(m_mtx_hnd); } 56 try_lock()57 bool try_lock() 58 { return winapi_wrapper_try_wait_for_single_object(m_mtx_hnd); } 59 timed_lock(const boost::posix_time::ptime & abs_time)60 bool timed_lock(const boost::posix_time::ptime &abs_time) 61 { return winapi_wrapper_timed_wait_for_single_object(m_mtx_hnd, abs_time); } 62 63 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 64 protected: 65 void *m_mtx_hnd; 66 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 67 }; 68 69 //Swappable mutex wrapper 70 class winapi_mutex_wrapper 71 : public winapi_mutex_functions 72 { 73 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 74 75 //Non-copyable 76 winapi_mutex_wrapper(const winapi_mutex_wrapper &); 77 winapi_mutex_wrapper &operator=(const winapi_mutex_wrapper &); 78 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 79 80 //Note that Windows API does not return winapi::invalid_handle_value 81 //when failing to create/open a mutex, but a nullptr 82 83 public: winapi_mutex_wrapper(void * mtx_hnd=0)84 winapi_mutex_wrapper(void *mtx_hnd = 0) 85 : winapi_mutex_functions(mtx_hnd) 86 {} 87 ~winapi_mutex_wrapper()88 ~winapi_mutex_wrapper() 89 { this->close(); } 90 release()91 void *release() 92 { 93 void *hnd = m_mtx_hnd; 94 m_mtx_hnd = 0; 95 return hnd; 96 } 97 handle() const98 void *handle() const 99 { return m_mtx_hnd; } 100 open_or_create(const char * name,const permissions & perm)101 bool open_or_create(const char *name, const permissions &perm) 102 { 103 if(m_mtx_hnd == 0){ 104 m_mtx_hnd = winapi::open_or_create_mutex 105 ( name 106 , false 107 , (winapi::interprocess_security_attributes*)perm.get_permissions() 108 ); 109 return m_mtx_hnd != 0; 110 } 111 else{ 112 return false; 113 } 114 } 115 close()116 void close() 117 { 118 if(m_mtx_hnd != 0){ 119 winapi::close_handle(m_mtx_hnd); 120 m_mtx_hnd = 0; 121 } 122 } 123 swap(winapi_mutex_wrapper & other)124 void swap(winapi_mutex_wrapper &other) 125 { void *tmp = m_mtx_hnd; m_mtx_hnd = other.m_mtx_hnd; other.m_mtx_hnd = tmp; } 126 }; 127 128 } //namespace ipcdetail { 129 } //namespace interprocess { 130 } //namespace boost { 131 132 #include <boost/interprocess/detail/config_end.hpp> 133 134 #endif //BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_HPP 135