]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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_WINDOWS_NAMED_SEMAPHORE_HPP | |
12 | #define BOOST_INTERPROCESS_WINDOWS_NAMED_SEMAPHORE_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/interprocess_tester.hpp> | |
7c673cae FG |
27 | #include <boost/interprocess/sync/windows/named_sync.hpp> |
28 | #include <boost/interprocess/sync/windows/winapi_semaphore_wrapper.hpp> | |
29 | ||
30 | namespace boost { | |
31 | namespace interprocess { | |
32 | namespace ipcdetail { | |
33 | ||
34 | ||
35 | ||
1e59de90 | 36 | class winapi_named_semaphore |
7c673cae FG |
37 | { |
38 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
39 | ||
40 | //Non-copyable | |
1e59de90 TL |
41 | winapi_named_semaphore(); |
42 | winapi_named_semaphore(const winapi_named_semaphore &); | |
43 | winapi_named_semaphore &operator=(const winapi_named_semaphore &); | |
7c673cae FG |
44 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
45 | ||
46 | public: | |
1e59de90 | 47 | winapi_named_semaphore(create_only_t, const char *name, unsigned int initialCount, const permissions &perm = permissions()); |
7c673cae | 48 | |
1e59de90 | 49 | winapi_named_semaphore(open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm = permissions()); |
7c673cae | 50 | |
1e59de90 | 51 | winapi_named_semaphore(open_only_t, const char *name); |
7c673cae | 52 | |
1e59de90 TL |
53 | winapi_named_semaphore(create_only_t, const wchar_t *name, unsigned int initialCount, const permissions &perm = permissions()); |
54 | ||
55 | winapi_named_semaphore(open_or_create_t, const wchar_t *name, unsigned int initialCount, const permissions &perm = permissions()); | |
56 | ||
57 | winapi_named_semaphore(open_only_t, const wchar_t *name); | |
58 | ||
59 | ~winapi_named_semaphore(); | |
7c673cae FG |
60 | |
61 | void post(); | |
62 | void wait(); | |
63 | bool try_wait(); | |
1e59de90 | 64 | template<class TimePoint> bool timed_wait(const TimePoint &abs_time); |
7c673cae FG |
65 | |
66 | static bool remove(const char *name); | |
1e59de90 | 67 | static bool remove(const wchar_t *name); |
7c673cae FG |
68 | |
69 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
70 | private: | |
71 | friend class interprocess_tester; | |
72 | void dont_close_on_destruction(); | |
73 | winapi_semaphore_wrapper m_sem_wrapper; | |
74 | windows_named_sync m_named_sync; | |
75 | ||
76 | class named_sem_callbacks : public windows_named_sync_interface | |
77 | { | |
78 | public: | |
79 | typedef __int64 sem_count_t; | |
80 | named_sem_callbacks(winapi_semaphore_wrapper &sem_wrapper, sem_count_t sem_cnt) | |
81 | : m_sem_wrapper(sem_wrapper), m_sem_count(sem_cnt) | |
82 | {} | |
83 | ||
1e59de90 | 84 | virtual std::size_t get_data_size() const BOOST_OVERRIDE |
7c673cae FG |
85 | { return sizeof(sem_count_t); } |
86 | ||
1e59de90 | 87 | virtual const void *buffer_with_final_data_to_file() BOOST_OVERRIDE |
7c673cae FG |
88 | { return &m_sem_count; } |
89 | ||
1e59de90 | 90 | virtual const void *buffer_with_init_data_to_file() BOOST_OVERRIDE |
7c673cae FG |
91 | { return &m_sem_count; } |
92 | ||
1e59de90 | 93 | virtual void *buffer_to_store_init_data_from_file() BOOST_OVERRIDE |
7c673cae FG |
94 | { return &m_sem_count; } |
95 | ||
1e59de90 | 96 | virtual bool open(create_enum_t, const char *id_name) BOOST_OVERRIDE |
7c673cae FG |
97 | { |
98 | std::string aux_str = "Global\\bipc.sem."; | |
99 | aux_str += id_name; | |
100 | // | |
101 | permissions sem_perm; | |
102 | sem_perm.set_unrestricted(); | |
103 | bool created; | |
104 | return m_sem_wrapper.open_or_create | |
105 | ( aux_str.c_str(), static_cast<long>(m_sem_count) | |
106 | , winapi_semaphore_wrapper::MaxCount, sem_perm, created); | |
107 | } | |
108 | ||
1e59de90 TL |
109 | virtual bool open(create_enum_t, const wchar_t *id_name) BOOST_OVERRIDE |
110 | { | |
111 | std::wstring aux_str = L"Global\\bipc.sem."; | |
112 | aux_str += id_name; | |
113 | // | |
114 | permissions sem_perm; | |
115 | sem_perm.set_unrestricted(); | |
116 | bool created; | |
117 | return m_sem_wrapper.open_or_create | |
118 | ( aux_str.c_str(), static_cast<long>(m_sem_count) | |
119 | , winapi_semaphore_wrapper::MaxCount, sem_perm, created); | |
120 | } | |
121 | ||
122 | virtual void close() BOOST_OVERRIDE | |
7c673cae FG |
123 | { |
124 | m_sem_wrapper.close(); | |
125 | } | |
126 | ||
1e59de90 | 127 | virtual ~named_sem_callbacks() BOOST_OVERRIDE |
7c673cae FG |
128 | {} |
129 | ||
130 | private: | |
7c673cae | 131 | winapi_semaphore_wrapper& m_sem_wrapper; |
1e59de90 | 132 | sem_count_t m_sem_count; |
7c673cae FG |
133 | }; |
134 | ||
135 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
136 | }; | |
137 | ||
1e59de90 | 138 | inline winapi_named_semaphore::~winapi_named_semaphore() |
7c673cae FG |
139 | { |
140 | named_sem_callbacks callbacks(m_sem_wrapper, m_sem_wrapper.value()); | |
141 | m_named_sync.close(callbacks); | |
142 | } | |
143 | ||
1e59de90 | 144 | inline void winapi_named_semaphore::dont_close_on_destruction() |
7c673cae FG |
145 | {} |
146 | ||
1e59de90 | 147 | inline winapi_named_semaphore::winapi_named_semaphore |
7c673cae FG |
148 | (create_only_t, const char *name, unsigned int initial_count, const permissions &perm) |
149 | : m_sem_wrapper() | |
150 | { | |
151 | named_sem_callbacks callbacks(m_sem_wrapper, initial_count); | |
152 | m_named_sync.open_or_create(DoCreate, name, perm, callbacks); | |
153 | } | |
154 | ||
1e59de90 | 155 | inline winapi_named_semaphore::winapi_named_semaphore |
7c673cae FG |
156 | (open_or_create_t, const char *name, unsigned int initial_count, const permissions &perm) |
157 | : m_sem_wrapper() | |
158 | { | |
159 | named_sem_callbacks callbacks(m_sem_wrapper, initial_count); | |
160 | m_named_sync.open_or_create(DoOpenOrCreate, name, perm, callbacks); | |
161 | } | |
162 | ||
1e59de90 TL |
163 | inline winapi_named_semaphore::winapi_named_semaphore(open_only_t, const char *name) |
164 | : m_sem_wrapper() | |
165 | { | |
166 | named_sem_callbacks callbacks(m_sem_wrapper, 0); | |
167 | m_named_sync.open_or_create(DoOpen, name, permissions(), callbacks); | |
168 | } | |
169 | ||
170 | inline winapi_named_semaphore::winapi_named_semaphore | |
171 | (create_only_t, const wchar_t *name, unsigned int initial_count, const permissions &perm) | |
172 | : m_sem_wrapper() | |
173 | { | |
174 | named_sem_callbacks callbacks(m_sem_wrapper, initial_count); | |
175 | m_named_sync.open_or_create(DoCreate, name, perm, callbacks); | |
176 | } | |
177 | ||
178 | inline winapi_named_semaphore::winapi_named_semaphore | |
179 | (open_or_create_t, const wchar_t *name, unsigned int initial_count, const permissions &perm) | |
180 | : m_sem_wrapper() | |
181 | { | |
182 | named_sem_callbacks callbacks(m_sem_wrapper, initial_count); | |
183 | m_named_sync.open_or_create(DoOpenOrCreate, name, perm, callbacks); | |
184 | } | |
185 | ||
186 | inline winapi_named_semaphore::winapi_named_semaphore(open_only_t, const wchar_t *name) | |
7c673cae FG |
187 | : m_sem_wrapper() |
188 | { | |
189 | named_sem_callbacks callbacks(m_sem_wrapper, 0); | |
190 | m_named_sync.open_or_create(DoOpen, name, permissions(), callbacks); | |
191 | } | |
192 | ||
1e59de90 | 193 | inline void winapi_named_semaphore::post() |
7c673cae FG |
194 | { |
195 | m_sem_wrapper.post(); | |
196 | } | |
197 | ||
1e59de90 | 198 | inline void winapi_named_semaphore::wait() |
7c673cae FG |
199 | { |
200 | m_sem_wrapper.wait(); | |
201 | } | |
202 | ||
1e59de90 | 203 | inline bool winapi_named_semaphore::try_wait() |
7c673cae FG |
204 | { |
205 | return m_sem_wrapper.try_wait(); | |
206 | } | |
207 | ||
1e59de90 TL |
208 | template<class TimePoint> |
209 | inline bool winapi_named_semaphore::timed_wait(const TimePoint &abs_time) | |
7c673cae FG |
210 | { |
211 | return m_sem_wrapper.timed_wait(abs_time); | |
212 | } | |
213 | ||
1e59de90 TL |
214 | inline bool winapi_named_semaphore::remove(const char *name) |
215 | { | |
216 | return windows_named_sync::remove(name); | |
217 | } | |
218 | ||
219 | inline bool winapi_named_semaphore::remove(const wchar_t *name) | |
7c673cae FG |
220 | { |
221 | return windows_named_sync::remove(name); | |
222 | } | |
223 | ||
224 | } //namespace ipcdetail { | |
225 | } //namespace interprocess { | |
226 | } //namespace boost { | |
227 | ||
228 | #include <boost/interprocess/detail/config_end.hpp> | |
229 | ||
230 | #endif //BOOST_INTERPROCESS_WINDOWS_NAMED_SEMAPHORE_HPP |