BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmt_semaphore.h
Go to the documentation of this file.
1/// @file bslmt_semaphore.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmt_semaphore.h -*-C++-*-
8#ifndef INCLUDED_BSLMT_SEMAPHORE
9#define INCLUDED_BSLMT_SEMAPHORE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmt_semaphore bslmt_semaphore
15/// @brief Provide a semaphore class.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmt
19/// @{
20/// @addtogroup bslmt_semaphore
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmt_semaphore-purpose"> Purpose</a>
25/// * <a href="#bslmt_semaphore-classes"> Classes </a>
26/// * <a href="#bslmt_semaphore-description"> Description </a>
27/// * <a href="#bslmt_semaphore-usage"> Usage </a>
28/// * <a href="#bslmt_semaphore-example-1-basic-usage"> Example 1: Basic Usage </a>
29///
30/// # Purpose {#bslmt_semaphore-purpose}
31/// Provide a semaphore class.
32///
33/// # Classes {#bslmt_semaphore-classes}
34///
35/// - bslmt::Semaphore: semaphore class
36///
37/// @see bslmt_timedsemaphore
38///
39/// # Description {#bslmt_semaphore-description}
40/// This component defines a portable and efficient thread
41/// synchronization primitive. In particular, `bslmt::Semaphore` is an
42/// efficient synchronization primitive that enables sharing of a counted number
43/// of resources or exclusive access. The usage model of this facility is
44/// modeled on POSIX semaphores and Windows semaphores.
45///
46/// ## Usage {#bslmt_semaphore-usage}
47///
48///
49/// This section illustrates intended use of this component.
50///
51/// ### Example 1: Basic Usage {#bslmt_semaphore-example-1-basic-usage}
52///
53///
54/// This example illustrates a very simple queue where potential clients can
55/// push integers to a queue, and later retrieve the integer values from the
56/// queue in FIFO order. It illustrates two potential uses of semaphores: to
57/// enforce exclusive access, and to allow resource sharing.
58/// @code
59/// /// FIFO queue of integer values.
60/// class IntQueue {
61///
62/// // DATA
63/// bsl::deque<int> d_queue; // underlying queue
64/// bslmt::Semaphore d_mutexSem; // mutual-access semaphore
65/// bslmt::Semaphore d_resourceSem; // resource-availability semaphore
66///
67/// private:
68/// // NOT IMPLEMENTED
69/// IntQueue(const IntQueue&);
70/// IntQueue& operator=(const IntQueue&);
71///
72/// public:
73/// // CREATORS
74///
75/// /// Create an `IntQueue` object. Optionally specified a
76/// /// `basicAllocator` used to supply memory. If `basicAllocator` is
77/// /// 0, the currently installed default allocator is used.
78/// explicit IntQueue(bslma::Allocator *basicAllocator = 0);
79///
80/// /// Destroy this `IntQueue` object.
81/// ~IntQueue();
82///
83/// // MANIPULATORS
84///
85/// /// Retrieve an integer from this `IntQueue` object. Integer values
86/// /// are obtained from the queue in FIFO order.
87/// int getInt();
88///
89/// /// Push the specified `value` to this `IntQueue` object.
90/// void pushInt(int value);
91/// };
92/// @endcode
93/// Note that the `IntQueue` constructor increments the count of the semaphore
94/// to 1 so that values can be pushed into the queue immediately following
95/// construction:
96/// @code
97/// // CREATORS
98/// IntQueue::IntQueue(bslma::Allocator *basicAllocator)
99/// : d_queue(basicAllocator)
100/// {
101/// d_mutexSem.post();
102/// }
103///
104/// IntQueue::~IntQueue()
105/// {
106/// d_mutexSem.wait(); // Wait for potential modifier.
107/// }
108///
109/// // MANIPULATORS
110/// int IntQueue::getInt()
111/// {
112/// // Waiting for resources.
113/// d_resourceSem.wait();
114///
115/// // `d_mutexSem` is used for exclusive access.
116/// d_mutexSem.wait(); // lock
117/// const int ret = d_queue.back();
118/// d_queue.pop_back();
119/// d_mutexSem.post(); // unlock
120///
121/// return ret;
122/// }
123///
124/// void IntQueue::pushInt(int value)
125/// {
126/// d_mutexSem.wait();
127/// d_queue.push_front(value);
128/// d_mutexSem.post();
129///
130/// d_resourceSem.post(); // Signal we have resources available.
131/// }
132/// @endcode
133/// @}
134/** @} */
135/** @} */
136
137/** @addtogroup bsl
138 * @{
139 */
140/** @addtogroup bslmt
141 * @{
142 */
143/** @addtogroup bslmt_semaphore
144 * @{
145 */
146
147#include <bslscm_version.h>
148
149#include <bslmt_platform.h>
153
154
155namespace bslmt {
156
157template <class SEMAPHORE_POLICY>
159
160 // ===============
161 // class Semaphore
162 // ===============
163
164/// This class implements a portable semaphore type for thread
165/// synchronization. It forwards all requests to an appropriate
166/// platform-specific implementation.
167///
168/// See @ref bslmt_semaphore
170
171 // DATA
172 SemaphoreImpl<Platform::SemaphorePolicy> d_impl; // platform-specific
173 // implementation
174
175 private:
176 // NOT IMPLEMENTED
177 Semaphore(const Semaphore&);
178 Semaphore& operator=(const Semaphore&);
179
180 public:
181 // CREATORS
182
183 /// Create a semaphore initially having a count of 0. This
184 /// method does not return normally unless there are sufficient system
185 /// resources to construct the object.
186 Semaphore();
187
188 /// Create a semaphore initially having the specified `count`. This
189 /// method does not return normally unless there are sufficient system
190 /// resources to construct the object.
191 explicit
192 Semaphore(int count);
193
194 /// Destroy this semaphore.
195 ~Semaphore();
196
197 // MANIPULATORS
198
199 /// Atomically increment the count of this semaphore.
200 void post();
201
202 /// Atomically increase the count of this semaphore by the specified `value`.
203 ///
204 /// \pre The behavior is undefined unless `value > 0`.
205 void post(int value);
206
207 /// If the count of this semaphore is positive, atomically decrement the
208 /// count and return 0; otherwise, return a non-zero value with no
209 /// effect on the count.
210 int tryWait();
211
212 /// Block until the count of this semaphore is a positive value, then
213 /// atomically decrement the count and return.
214 void wait();
215
216 // ACCESSORS
217
218 /// Return the value of the current count of this semaphore.
219 int getValue() const;
220};
221
222// ============================================================================
223// INLINE DEFINITIONS
224// ============================================================================
225
226 // ---------------
227 // class Semaphore
228 // ---------------
229
230// CREATORS
231inline
233: d_impl(0)
234{
235}
236
237inline
239: d_impl(count)
240{
241}
242
243inline
247
248inline
250{
251 d_impl.post();
252}
253
254inline
255void Semaphore::post(int value)
256{
257 d_impl.post(value);
258}
259
260inline
262{
263 return d_impl.tryWait();
264}
265
266inline
268{
269 d_impl.wait();
270}
271
272// ACCESSORS
273inline
275{
276 return d_impl.getValue();
277}
278
279} // close package namespace
280
281
282#endif
283
284// ----------------------------------------------------------------------------
285// Copyright 2015 Bloomberg Finance L.P.
286//
287// Licensed under the Apache License, Version 2.0 (the "License");
288// you may not use this file except in compliance with the License.
289// You may obtain a copy of the License at
290//
291// http://www.apache.org/licenses/LICENSE-2.0
292//
293// Unless required by applicable law or agreed to in writing, software
294// distributed under the License is distributed on an "AS IS" BASIS,
295// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
296// See the License for the specific language governing permissions and
297// limitations under the License.
298// ----------------------------- END-OF-FILE ----------------------------------
299
300/** @} */
301/** @} */
302/** @} */
Definition bslmt_semaphore.h:158
Definition bslmt_semaphore.h:169
int tryWait()
Definition bslmt_semaphore.h:261
~Semaphore()
Destroy this semaphore.
Definition bslmt_semaphore.h:244
int getValue() const
Return the value of the current count of this semaphore.
Definition bslmt_semaphore.h:274
void wait()
Definition bslmt_semaphore.h:267
Semaphore()
Definition bslmt_semaphore.h:232
void post()
Atomically increment the count of this semaphore.
Definition bslmt_semaphore.h:249
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslmt_barrier.h:344