1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_HPP
12 #define BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_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/creation_tags.hpp>
25 #include <boost/interprocess/permissions.hpp>
26 #include <boost/interprocess/detail/win32_api.hpp>
27 #include <boost/interprocess/sync/windows/winapi_wrapper_common.hpp>
28 #include <boost/interprocess/errors.hpp>
29 #include <boost/interprocess/exceptions.hpp>
33 namespace interprocess {
36 class winapi_mutex_functions
38 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
41 winapi_mutex_functions(const winapi_mutex_functions &);
42 winapi_mutex_functions &operator=(const winapi_mutex_functions &);
43 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
46 winapi_mutex_functions(void *mtx_hnd)
51 { winapi::release_mutex(m_mtx_hnd); }
54 { return winapi_wrapper_wait_for_single_object(m_mtx_hnd); }
57 { return winapi_wrapper_try_wait_for_single_object(m_mtx_hnd); }
59 template<class TimePoint>
60 bool timed_lock(const TimePoint &abs_time)
61 { return winapi_wrapper_timed_wait_for_single_object(m_mtx_hnd, abs_time); }
63 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
66 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
69 //Swappable mutex wrapper
70 class winapi_mutex_wrapper
71 : public winapi_mutex_functions
73 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
76 winapi_mutex_wrapper(const winapi_mutex_wrapper &);
77 winapi_mutex_wrapper &operator=(const winapi_mutex_wrapper &);
78 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
80 //Note that Windows API does not return winapi::invalid_handle_value
81 //when failing to create/open a mutex, but a nullptr
84 winapi_mutex_wrapper(void *mtx_hnd = 0)
85 : winapi_mutex_functions(mtx_hnd)
88 ~winapi_mutex_wrapper()
93 void *hnd = m_mtx_hnd;
101 template<class CharT>
102 bool open_or_create(const CharT *name, const permissions &perm)
105 m_mtx_hnd = winapi::open_or_create_mutex
108 , (winapi::interprocess_security_attributes*)perm.get_permissions()
110 return m_mtx_hnd != 0;
120 winapi::close_handle(m_mtx_hnd);
125 void swap(winapi_mutex_wrapper &other)
126 { void *tmp = m_mtx_hnd; m_mtx_hnd = other.m_mtx_hnd; other.m_mtx_hnd = tmp; }
129 } //namespace ipcdetail {
130 } //namespace interprocess {
131 } //namespace boost {
133 #include <boost/interprocess/detail/config_end.hpp>
135 #endif //BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_HPP