Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Ion Gaztanaga 2011-2012. Distributed under the Boost
|
Chris@16
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 //
|
Chris@16
|
7 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
8 //
|
Chris@16
|
9 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_HPP
|
Chris@16
|
12 #define BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_HPP
|
Chris@16
|
13
|
Chris@101
|
14 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
15 # include <boost/config.hpp>
|
Chris@101
|
16 #endif
|
Chris@101
|
17 #
|
Chris@101
|
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@16
|
19 # pragma once
|
Chris@16
|
20 #endif
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
23 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
24 #include <boost/interprocess/creation_tags.hpp>
|
Chris@16
|
25 #include <boost/interprocess/permissions.hpp>
|
Chris@16
|
26 #include <boost/interprocess/detail/win32_api.hpp>
|
Chris@16
|
27 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
Chris@101
|
28 #include <boost/interprocess/sync/windows/winapi_wrapper_common.hpp>
|
Chris@16
|
29 #include <boost/interprocess/errors.hpp>
|
Chris@16
|
30 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
31 #include <limits>
|
Chris@16
|
32
|
Chris@16
|
33 namespace boost {
|
Chris@16
|
34 namespace interprocess {
|
Chris@16
|
35 namespace ipcdetail {
|
Chris@16
|
36
|
Chris@16
|
37 class winapi_mutex_functions
|
Chris@16
|
38 {
|
Chris@101
|
39 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
40
|
Chris@16
|
41 //Non-copyable
|
Chris@16
|
42 winapi_mutex_functions(const winapi_mutex_functions &);
|
Chris@16
|
43 winapi_mutex_functions &operator=(const winapi_mutex_functions &);
|
Chris@101
|
44 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
45
|
Chris@16
|
46 public:
|
Chris@16
|
47 winapi_mutex_functions(void *mtx_hnd)
|
Chris@16
|
48 : m_mtx_hnd(mtx_hnd)
|
Chris@16
|
49 {}
|
Chris@16
|
50
|
Chris@16
|
51 void unlock()
|
Chris@101
|
52 { winapi::release_mutex(m_mtx_hnd); }
|
Chris@16
|
53
|
Chris@16
|
54 void lock()
|
Chris@101
|
55 { return winapi_wrapper_wait_for_single_object(m_mtx_hnd); }
|
Chris@16
|
56
|
Chris@16
|
57 bool try_lock()
|
Chris@101
|
58 { return winapi_wrapper_try_wait_for_single_object(m_mtx_hnd); }
|
Chris@16
|
59
|
Chris@16
|
60 bool timed_lock(const boost::posix_time::ptime &abs_time)
|
Chris@101
|
61 { return winapi_wrapper_timed_wait_for_single_object(m_mtx_hnd, abs_time); }
|
Chris@16
|
62
|
Chris@101
|
63 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
64 protected:
|
Chris@16
|
65 void *m_mtx_hnd;
|
Chris@101
|
66 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
67 };
|
Chris@16
|
68
|
Chris@16
|
69 //Swappable mutex wrapper
|
Chris@16
|
70 class winapi_mutex_wrapper
|
Chris@16
|
71 : public winapi_mutex_functions
|
Chris@16
|
72 {
|
Chris@101
|
73 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
74
|
Chris@16
|
75 //Non-copyable
|
Chris@16
|
76 winapi_mutex_wrapper(const winapi_mutex_wrapper &);
|
Chris@16
|
77 winapi_mutex_wrapper &operator=(const winapi_mutex_wrapper &);
|
Chris@101
|
78 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@101
|
79
|
Chris@101
|
80 //Note that Windows API does not return winapi::invalid_handle_value
|
Chris@101
|
81 //when failing to create/open a mutex, but a nullptr
|
Chris@16
|
82
|
Chris@16
|
83 public:
|
Chris@101
|
84 winapi_mutex_wrapper(void *mtx_hnd = 0)
|
Chris@16
|
85 : winapi_mutex_functions(mtx_hnd)
|
Chris@16
|
86 {}
|
Chris@16
|
87
|
Chris@16
|
88 ~winapi_mutex_wrapper()
|
Chris@16
|
89 { this->close(); }
|
Chris@16
|
90
|
Chris@16
|
91 void *release()
|
Chris@16
|
92 {
|
Chris@16
|
93 void *hnd = m_mtx_hnd;
|
Chris@101
|
94 m_mtx_hnd = 0;
|
Chris@16
|
95 return hnd;
|
Chris@16
|
96 }
|
Chris@16
|
97
|
Chris@16
|
98 void *handle() const
|
Chris@16
|
99 { return m_mtx_hnd; }
|
Chris@16
|
100
|
Chris@16
|
101 bool open_or_create(const char *name, const permissions &perm)
|
Chris@16
|
102 {
|
Chris@101
|
103 if(m_mtx_hnd == 0){
|
Chris@16
|
104 m_mtx_hnd = winapi::open_or_create_mutex
|
Chris@16
|
105 ( name
|
Chris@16
|
106 , false
|
Chris@16
|
107 , (winapi::interprocess_security_attributes*)perm.get_permissions()
|
Chris@16
|
108 );
|
Chris@101
|
109 return m_mtx_hnd != 0;
|
Chris@16
|
110 }
|
Chris@16
|
111 else{
|
Chris@16
|
112 return false;
|
Chris@16
|
113 }
|
Chris@101
|
114 }
|
Chris@16
|
115
|
Chris@16
|
116 void close()
|
Chris@16
|
117 {
|
Chris@101
|
118 if(m_mtx_hnd != 0){
|
Chris@16
|
119 winapi::close_handle(m_mtx_hnd);
|
Chris@101
|
120 m_mtx_hnd = 0;
|
Chris@16
|
121 }
|
Chris@16
|
122 }
|
Chris@16
|
123
|
Chris@16
|
124 void swap(winapi_mutex_wrapper &other)
|
Chris@16
|
125 { void *tmp = m_mtx_hnd; m_mtx_hnd = other.m_mtx_hnd; other.m_mtx_hnd = tmp; }
|
Chris@16
|
126 };
|
Chris@16
|
127
|
Chris@16
|
128 } //namespace ipcdetail {
|
Chris@16
|
129 } //namespace interprocess {
|
Chris@16
|
130 } //namespace boost {
|
Chris@16
|
131
|
Chris@16
|
132 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
133
|
Chris@16
|
134 #endif //BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_HPP
|