]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | ////////////////////////////////////////////////////////////////////////////// |
2 | // | |
3 | // (C) Copyright Ion Gaztanaga 2005-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_NAMED_SEMAPHORE_HPP | |
12 | #define BOOST_INTERPROCESS_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/exceptions.hpp> | |
26 | #include <boost/interprocess/permissions.hpp> | |
27 | #include <boost/interprocess/detail/interprocess_tester.hpp> | |
7c673cae FG |
28 | |
29 | #if defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USES_POSIX_SEMAPHORES) | |
30 | #include <boost/interprocess/sync/posix/named_semaphore.hpp> | |
31 | //Experimental... | |
32 | #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS) | |
33 | #include <boost/interprocess/sync/windows/named_semaphore.hpp> | |
1e59de90 | 34 | #define BOOST_INTERPROCESS_NAMED_SEMAPHORE_USE_WINAPI |
7c673cae | 35 | #else |
1e59de90 | 36 | #include <boost/interprocess/sync/shm/named_semaphore.hpp> |
7c673cae FG |
37 | #endif |
38 | ||
39 | //!\file | |
40 | //!Describes a named semaphore class for inter-process synchronization | |
41 | ||
42 | namespace boost { | |
43 | namespace interprocess { | |
44 | ||
45 | //!A semaphore with a global name, so it can be found from different | |
46 | //!processes. Allows several resource sharing patterns and efficient | |
47 | //!acknowledgment mechanisms. | |
48 | class named_semaphore | |
49 | { | |
50 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
51 | ||
52 | //Non-copyable | |
53 | named_semaphore(); | |
54 | named_semaphore(const named_semaphore &); | |
55 | named_semaphore &operator=(const named_semaphore &); | |
56 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
57 | ||
58 | public: | |
59 | //!Creates a global semaphore with a name, and an initial count. | |
60 | //!If the semaphore can't be created throws interprocess_exception | |
61 | named_semaphore(create_only_t, const char *name, unsigned int initialCount, const permissions &perm = permissions()); | |
62 | ||
63 | //!Opens or creates a global semaphore with a name, and an initial count. | |
64 | //!If the semaphore is created, this call is equivalent to | |
65 | //!named_semaphore(create_only_t, ...) | |
66 | //!If the semaphore is already created, this call is equivalent to | |
67 | //!named_semaphore(open_only_t, ... ) | |
68 | //!and initialCount is ignored. | |
69 | named_semaphore(open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm = permissions()); | |
70 | ||
71 | //!Opens a global semaphore with a name if that semaphore is previously. | |
72 | //!created. If it is not previously created this function throws | |
73 | //!interprocess_exception. | |
74 | named_semaphore(open_only_t, const char *name); | |
75 | ||
1e59de90 TL |
76 | #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
77 | ||
78 | //!Creates a global semaphore with a name, and an initial count. | |
79 | //!If the semaphore can't be created throws interprocess_exception | |
80 | //! | |
81 | //!Note: This function is only available on operating systems with | |
82 | //! native wchar_t APIs (e.g. Windows). | |
83 | named_semaphore(create_only_t, const wchar_t *name, unsigned int initialCount, const permissions &perm = permissions()); | |
84 | ||
85 | //!Opens or creates a global semaphore with a name, and an initial count. | |
86 | //!If the semaphore is created, this call is equivalent to | |
87 | //!named_semaphore(create_only_t, ...) | |
88 | //!If the semaphore is already created, this call is equivalent to | |
89 | //!named_semaphore(open_only_t, ... ) | |
90 | //!and initialCount is ignored. | |
91 | //! | |
92 | //!Note: This function is only available on operating systems with | |
93 | //! native wchar_t APIs (e.g. Windows). | |
94 | named_semaphore(open_or_create_t, const wchar_t *name, unsigned int initialCount, const permissions &perm = permissions()); | |
95 | ||
96 | //!Opens a global semaphore with a name if that semaphore is previously. | |
97 | //!created. If it is not previously created this function throws | |
98 | //!interprocess_exception. | |
99 | //! | |
100 | //!Note: This function is only available on operating systems with | |
101 | //! native wchar_t APIs (e.g. Windows). | |
102 | named_semaphore(open_only_t, const wchar_t *name); | |
103 | ||
104 | #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
105 | ||
7c673cae FG |
106 | //!Destroys *this and indicates that the calling process is finished using |
107 | //!the resource. The destructor function will deallocate | |
108 | //!any system resources allocated by the system for use by this process for | |
109 | //!this resource. The resource can still be opened again calling | |
110 | //!the open constructor overload. To erase the resource from the system | |
111 | //!use remove(). | |
112 | ~named_semaphore(); | |
113 | ||
114 | //!Increments the semaphore count. If there are processes/threads blocked waiting | |
115 | //!for the semaphore, then one of these processes will return successfully from | |
116 | //!its wait function. If there is an error an interprocess_exception exception is thrown. | |
117 | void post(); | |
118 | ||
119 | //!Decrements the semaphore. If the semaphore value is not greater than zero, | |
120 | //!then the calling process/thread blocks until it can decrement the counter. | |
121 | //!If there is an error an interprocess_exception exception is thrown. | |
122 | void wait(); | |
123 | ||
124 | //!Decrements the semaphore if the semaphore's value is greater than zero | |
125 | //!and returns true. If the value is not greater than zero returns false. | |
126 | //!If there is an error an interprocess_exception exception is thrown. | |
127 | bool try_wait(); | |
128 | ||
129 | //!Decrements the semaphore if the semaphore's value is greater | |
130 | //!than zero and returns true. Otherwise, waits for the semaphore | |
131 | //!to the posted or the timeout expires. If the timeout expires, the | |
132 | //!function returns false. If the semaphore is posted the function | |
133 | //!returns true. If there is an error throws sem_exception | |
1e59de90 TL |
134 | template<class TimePoint> |
135 | bool timed_wait(const TimePoint &abs_time); | |
7c673cae FG |
136 | |
137 | //!Erases a named semaphore from the system. | |
138 | //!Returns false on error. Never throws. | |
139 | static bool remove(const char *name); | |
140 | ||
1e59de90 TL |
141 | #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
142 | ||
143 | //!Erases a named semaphore from the system. | |
144 | //!Returns false on error. Never throws. | |
145 | //! | |
146 | //!Note: This function is only available on operating systems with | |
147 | //! native wchar_t APIs (e.g. Windows). | |
148 | static bool remove(const wchar_t *name); | |
149 | ||
150 | #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
151 | ||
7c673cae FG |
152 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
153 | private: | |
154 | friend class ipcdetail::interprocess_tester; | |
155 | void dont_close_on_destruction(); | |
156 | ||
157 | #if defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USES_POSIX_SEMAPHORES) | |
158 | typedef ipcdetail::posix_named_semaphore impl_t; | |
1e59de90 TL |
159 | #elif defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USE_WINAPI) |
160 | typedef ipcdetail::winapi_named_semaphore impl_t; | |
7c673cae FG |
161 | #else |
162 | typedef ipcdetail::shm_named_semaphore impl_t; | |
163 | #endif | |
164 | impl_t m_sem; | |
165 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
166 | }; | |
167 | ||
168 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
169 | ||
170 | inline named_semaphore::named_semaphore | |
171 | (create_only_t, const char *name, unsigned int initialCount, const permissions &perm) | |
172 | : m_sem(create_only, name, initialCount, perm) | |
173 | {} | |
174 | ||
175 | inline named_semaphore::named_semaphore | |
176 | (open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm) | |
177 | : m_sem(open_or_create, name, initialCount, perm) | |
178 | {} | |
179 | ||
180 | inline named_semaphore::named_semaphore(open_only_t, const char *name) | |
181 | : m_sem(open_only, name) | |
182 | {} | |
183 | ||
1e59de90 TL |
184 | #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
185 | ||
186 | inline named_semaphore::named_semaphore | |
187 | (create_only_t, const wchar_t *name, unsigned int initialCount, const permissions &perm) | |
188 | : m_sem(create_only, name, initialCount, perm) | |
189 | {} | |
190 | ||
191 | inline named_semaphore::named_semaphore | |
192 | (open_or_create_t, const wchar_t *name, unsigned int initialCount, const permissions &perm) | |
193 | : m_sem(open_or_create, name, initialCount, perm) | |
194 | {} | |
195 | ||
196 | inline named_semaphore::named_semaphore(open_only_t, const wchar_t *name) | |
197 | : m_sem(open_only, name) | |
198 | {} | |
199 | ||
200 | #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
201 | ||
7c673cae FG |
202 | inline named_semaphore::~named_semaphore() |
203 | {} | |
204 | ||
205 | inline void named_semaphore::dont_close_on_destruction() | |
206 | { ipcdetail::interprocess_tester::dont_close_on_destruction(m_sem); } | |
207 | ||
208 | inline void named_semaphore::wait() | |
209 | { m_sem.wait(); } | |
210 | ||
211 | inline void named_semaphore::post() | |
212 | { m_sem.post(); } | |
213 | ||
214 | inline bool named_semaphore::try_wait() | |
215 | { return m_sem.try_wait(); } | |
216 | ||
1e59de90 TL |
217 | template<class TimePoint> |
218 | inline bool named_semaphore::timed_wait(const TimePoint &abs_time) | |
7c673cae FG |
219 | { return m_sem.timed_wait(abs_time); } |
220 | ||
221 | inline bool named_semaphore::remove(const char *name) | |
222 | { return impl_t::remove(name); } | |
223 | ||
1e59de90 TL |
224 | #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
225 | ||
226 | inline bool named_semaphore::remove(const wchar_t *name) | |
227 | { return impl_t::remove(name); } | |
228 | ||
229 | #endif | |
230 | ||
7c673cae FG |
231 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
232 | ||
233 | } //namespace interprocess { | |
234 | } //namespace boost { | |
235 | ||
236 | ||
237 | #include <boost/interprocess/detail/config_end.hpp> | |
238 | ||
239 | #endif //BOOST_INTERPROCESS_NAMED_SEMAPHORE_HPP |