Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Ion Gaztanaga 2005-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_NAMED_SEMAPHORE_HPP
|
Chris@16
|
12 #define BOOST_INTERPROCESS_NAMED_SEMAPHORE_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/exceptions.hpp>
|
Chris@16
|
26 #include <boost/interprocess/permissions.hpp>
|
Chris@16
|
27 #include <boost/interprocess/detail/interprocess_tester.hpp>
|
Chris@16
|
28 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
Chris@16
|
29
|
Chris@16
|
30 #if defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USES_POSIX_SEMAPHORES)
|
Chris@16
|
31 #include <boost/interprocess/sync/posix/named_semaphore.hpp>
|
Chris@16
|
32 //Experimental...
|
Chris@16
|
33 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
|
Chris@16
|
34 #include <boost/interprocess/sync/windows/named_semaphore.hpp>
|
Chris@16
|
35 #define BOOST_INTERPROCESS_USE_WINDOWS
|
Chris@16
|
36 #else
|
Chris@16
|
37 #include <boost/interprocess/sync/shm/named_semaphore.hpp>
|
Chris@16
|
38 #endif
|
Chris@16
|
39
|
Chris@16
|
40 //!\file
|
Chris@16
|
41 //!Describes a named semaphore class for inter-process synchronization
|
Chris@16
|
42
|
Chris@16
|
43 namespace boost {
|
Chris@16
|
44 namespace interprocess {
|
Chris@16
|
45
|
Chris@16
|
46 //!A semaphore with a global name, so it can be found from different
|
Chris@16
|
47 //!processes. Allows several resource sharing patterns and efficient
|
Chris@16
|
48 //!acknowledgment mechanisms.
|
Chris@16
|
49 class named_semaphore
|
Chris@16
|
50 {
|
Chris@101
|
51 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
52
|
Chris@16
|
53 //Non-copyable
|
Chris@16
|
54 named_semaphore();
|
Chris@16
|
55 named_semaphore(const named_semaphore &);
|
Chris@16
|
56 named_semaphore &operator=(const named_semaphore &);
|
Chris@101
|
57 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
58
|
Chris@16
|
59 public:
|
Chris@16
|
60 //!Creates a global semaphore with a name, and an initial count.
|
Chris@16
|
61 //!If the semaphore can't be created throws interprocess_exception
|
Chris@16
|
62 named_semaphore(create_only_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
|
Chris@16
|
63
|
Chris@16
|
64 //!Opens or creates a global semaphore with a name, and an initial count.
|
Chris@16
|
65 //!If the semaphore is created, this call is equivalent to
|
Chris@16
|
66 //!named_semaphore(create_only_t, ...)
|
Chris@16
|
67 //!If the semaphore is already created, this call is equivalent to
|
Chris@16
|
68 //!named_semaphore(open_only_t, ... )
|
Chris@16
|
69 //!and initialCount is ignored.
|
Chris@16
|
70 named_semaphore(open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
|
Chris@16
|
71
|
Chris@16
|
72 //!Opens a global semaphore with a name if that semaphore is previously.
|
Chris@16
|
73 //!created. If it is not previously created this function throws
|
Chris@16
|
74 //!interprocess_exception.
|
Chris@16
|
75 named_semaphore(open_only_t, const char *name);
|
Chris@16
|
76
|
Chris@16
|
77 //!Destroys *this and indicates that the calling process is finished using
|
Chris@16
|
78 //!the resource. The destructor function will deallocate
|
Chris@16
|
79 //!any system resources allocated by the system for use by this process for
|
Chris@16
|
80 //!this resource. The resource can still be opened again calling
|
Chris@16
|
81 //!the open constructor overload. To erase the resource from the system
|
Chris@16
|
82 //!use remove().
|
Chris@16
|
83 ~named_semaphore();
|
Chris@16
|
84
|
Chris@16
|
85 //!Increments the semaphore count. If there are processes/threads blocked waiting
|
Chris@16
|
86 //!for the semaphore, then one of these processes will return successfully from
|
Chris@16
|
87 //!its wait function. If there is an error an interprocess_exception exception is thrown.
|
Chris@16
|
88 void post();
|
Chris@16
|
89
|
Chris@16
|
90 //!Decrements the semaphore. If the semaphore value is not greater than zero,
|
Chris@16
|
91 //!then the calling process/thread blocks until it can decrement the counter.
|
Chris@16
|
92 //!If there is an error an interprocess_exception exception is thrown.
|
Chris@16
|
93 void wait();
|
Chris@16
|
94
|
Chris@16
|
95 //!Decrements the semaphore if the semaphore's value is greater than zero
|
Chris@16
|
96 //!and returns true. If the value is not greater than zero returns false.
|
Chris@16
|
97 //!If there is an error an interprocess_exception exception is thrown.
|
Chris@16
|
98 bool try_wait();
|
Chris@16
|
99
|
Chris@16
|
100 //!Decrements the semaphore if the semaphore's value is greater
|
Chris@16
|
101 //!than zero and returns true. Otherwise, waits for the semaphore
|
Chris@16
|
102 //!to the posted or the timeout expires. If the timeout expires, the
|
Chris@16
|
103 //!function returns false. If the semaphore is posted the function
|
Chris@16
|
104 //!returns true. If there is an error throws sem_exception
|
Chris@16
|
105 bool timed_wait(const boost::posix_time::ptime &abs_time);
|
Chris@16
|
106
|
Chris@16
|
107 //!Erases a named semaphore from the system.
|
Chris@16
|
108 //!Returns false on error. Never throws.
|
Chris@16
|
109 static bool remove(const char *name);
|
Chris@16
|
110
|
Chris@101
|
111 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
112 private:
|
Chris@16
|
113 friend class ipcdetail::interprocess_tester;
|
Chris@16
|
114 void dont_close_on_destruction();
|
Chris@16
|
115
|
Chris@16
|
116 #if defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USES_POSIX_SEMAPHORES)
|
Chris@16
|
117 typedef ipcdetail::posix_named_semaphore impl_t;
|
Chris@16
|
118 #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
|
Chris@16
|
119 #undef BOOST_INTERPROCESS_USE_WINDOWS
|
Chris@16
|
120 typedef ipcdetail::windows_named_semaphore impl_t;
|
Chris@16
|
121 #else
|
Chris@16
|
122 typedef ipcdetail::shm_named_semaphore impl_t;
|
Chris@16
|
123 #endif
|
Chris@16
|
124 impl_t m_sem;
|
Chris@101
|
125 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
126 };
|
Chris@16
|
127
|
Chris@101
|
128 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
129
|
Chris@16
|
130 inline named_semaphore::named_semaphore
|
Chris@16
|
131 (create_only_t, const char *name, unsigned int initialCount, const permissions &perm)
|
Chris@16
|
132 : m_sem(create_only, name, initialCount, perm)
|
Chris@16
|
133 {}
|
Chris@16
|
134
|
Chris@16
|
135 inline named_semaphore::named_semaphore
|
Chris@16
|
136 (open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm)
|
Chris@16
|
137 : m_sem(open_or_create, name, initialCount, perm)
|
Chris@16
|
138 {}
|
Chris@16
|
139
|
Chris@16
|
140 inline named_semaphore::named_semaphore(open_only_t, const char *name)
|
Chris@16
|
141 : m_sem(open_only, name)
|
Chris@16
|
142 {}
|
Chris@16
|
143
|
Chris@16
|
144 inline named_semaphore::~named_semaphore()
|
Chris@16
|
145 {}
|
Chris@16
|
146
|
Chris@16
|
147 inline void named_semaphore::dont_close_on_destruction()
|
Chris@16
|
148 { ipcdetail::interprocess_tester::dont_close_on_destruction(m_sem); }
|
Chris@16
|
149
|
Chris@16
|
150 inline void named_semaphore::wait()
|
Chris@16
|
151 { m_sem.wait(); }
|
Chris@16
|
152
|
Chris@16
|
153 inline void named_semaphore::post()
|
Chris@16
|
154 { m_sem.post(); }
|
Chris@16
|
155
|
Chris@16
|
156 inline bool named_semaphore::try_wait()
|
Chris@16
|
157 { return m_sem.try_wait(); }
|
Chris@16
|
158
|
Chris@16
|
159 inline bool named_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
|
Chris@101
|
160 { return m_sem.timed_wait(abs_time); }
|
Chris@16
|
161
|
Chris@16
|
162 inline bool named_semaphore::remove(const char *name)
|
Chris@16
|
163 { return impl_t::remove(name); }
|
Chris@16
|
164
|
Chris@101
|
165 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
166
|
Chris@16
|
167 } //namespace interprocess {
|
Chris@16
|
168 } //namespace boost {
|
Chris@16
|
169
|
Chris@16
|
170
|
Chris@16
|
171 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
172
|
Chris@16
|
173 #endif //BOOST_INTERPROCESS_NAMED_SEMAPHORE_HPP
|