]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / interprocess / sync / windows / winapi_mutex_wrapper.hpp
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/sync/windows/winapi_wrapper_common.hpp>
28 #include <boost/interprocess/errors.hpp>
29 #include <boost/interprocess/exceptions.hpp>
30 #include <limits>
31
32 namespace boost {
33 namespace interprocess {
34 namespace ipcdetail {
35
36 class winapi_mutex_functions
37 {
38 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
39
40 //Non-copyable
41 winapi_mutex_functions(const winapi_mutex_functions &);
42 winapi_mutex_functions &operator=(const winapi_mutex_functions &);
43 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
44
45 public:
46 winapi_mutex_functions(void *mtx_hnd)
47 : m_mtx_hnd(mtx_hnd)
48 {}
49
50 void unlock()
51 { winapi::release_mutex(m_mtx_hnd); }
52
53 void lock()
54 { return winapi_wrapper_wait_for_single_object(m_mtx_hnd); }
55
56 bool try_lock()
57 { return winapi_wrapper_try_wait_for_single_object(m_mtx_hnd); }
58
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); }
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:
84 winapi_mutex_wrapper(void *mtx_hnd = 0)
85 : winapi_mutex_functions(mtx_hnd)
86 {}
87
88 ~winapi_mutex_wrapper()
89 { this->close(); }
90
91 void *release()
92 {
93 void *hnd = m_mtx_hnd;
94 m_mtx_hnd = 0;
95 return hnd;
96 }
97
98 void *handle() const
99 { return m_mtx_hnd; }
100
101 template<class CharT>
102 bool open_or_create(const CharT *name, const permissions &perm)
103 {
104 if(m_mtx_hnd == 0){
105 m_mtx_hnd = winapi::open_or_create_mutex
106 ( name
107 , false
108 , (winapi::interprocess_security_attributes*)perm.get_permissions()
109 );
110 return m_mtx_hnd != 0;
111 }
112 else{
113 return false;
114 }
115 }
116
117 void close()
118 {
119 if(m_mtx_hnd != 0){
120 winapi::close_handle(m_mtx_hnd);
121 m_mtx_hnd = 0;
122 }
123 }
124
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; }
127 };
128
129 } //namespace ipcdetail {
130 } //namespace interprocess {
131 } //namespace boost {
132
133 #include <boost/interprocess/detail/config_end.hpp>
134
135 #endif //BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_HPP