1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_NAMED_SEMAPHORE_HPP
12 #define BOOST_INTERPROCESS_NAMED_SEMAPHORE_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/exceptions.hpp>
26 #include <boost/interprocess/permissions.hpp>
27 #include <boost/interprocess/detail/interprocess_tester.hpp>
29 #if defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USES_POSIX_SEMAPHORES)
30 #include <boost/interprocess/sync/posix/named_semaphore.hpp>
32 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
33 #include <boost/interprocess/sync/windows/named_semaphore.hpp>
34 #define BOOST_INTERPROCESS_NAMED_SEMAPHORE_USE_WINAPI
36 #include <boost/interprocess/sync/shm/named_semaphore.hpp>
40 //!Describes a named semaphore class for inter-process synchronization
43 namespace interprocess {
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.
50 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
54 named_semaphore(const named_semaphore &);
55 named_semaphore &operator=(const named_semaphore &);
56 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
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());
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());
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);
76 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
78 //!Creates a global semaphore with a name, and an initial count.
79 //!If the semaphore can't be created throws interprocess_exception
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());
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.
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());
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.
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);
104 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
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
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.
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.
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.
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
134 template<class TimePoint>
135 bool timed_wait(const TimePoint &abs_time);
137 //!Erases a named semaphore from the system.
138 //!Returns false on error. Never throws.
139 static bool remove(const char *name);
141 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
143 //!Erases a named semaphore from the system.
144 //!Returns false on error. Never throws.
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);
150 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
152 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
154 friend class ipcdetail::interprocess_tester;
155 void dont_close_on_destruction();
157 #if defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USES_POSIX_SEMAPHORES)
158 typedef ipcdetail::posix_named_semaphore impl_t;
159 #elif defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USE_WINAPI)
160 typedef ipcdetail::winapi_named_semaphore impl_t;
162 typedef ipcdetail::shm_named_semaphore impl_t;
165 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
168 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
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)
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)
180 inline named_semaphore::named_semaphore(open_only_t, const char *name)
181 : m_sem(open_only, name)
184 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
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)
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)
196 inline named_semaphore::named_semaphore(open_only_t, const wchar_t *name)
197 : m_sem(open_only, name)
200 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
202 inline named_semaphore::~named_semaphore()
205 inline void named_semaphore::dont_close_on_destruction()
206 { ipcdetail::interprocess_tester::dont_close_on_destruction(m_sem); }
208 inline void named_semaphore::wait()
211 inline void named_semaphore::post()
214 inline bool named_semaphore::try_wait()
215 { return m_sem.try_wait(); }
217 template<class TimePoint>
218 inline bool named_semaphore::timed_wait(const TimePoint &abs_time)
219 { return m_sem.timed_wait(abs_time); }
221 inline bool named_semaphore::remove(const char *name)
222 { return impl_t::remove(name); }
224 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
226 inline bool named_semaphore::remove(const wchar_t *name)
227 { return impl_t::remove(name); }
231 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
233 } //namespace interprocess {
234 } //namespace boost {
237 #include <boost/interprocess/detail/config_end.hpp>
239 #endif //BOOST_INTERPROCESS_NAMED_SEMAPHORE_HPP