BDE 4.14.0 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/// // NOT IMPLEMENTED
68/// IntQueue(const IntQueue&);
69/// IntQueue& operator=(const IntQueue&);
70///
71/// public:
72/// // CREATORS
73///
74/// /// Create an `IntQueue` object. Optionally specified a
75/// /// `basicAllocator` used to supply memory. If `basicAllocator` is
76/// /// 0, the currently installed default allocator is used.
77/// explicit IntQueue(bslma::Allocator *basicAllocator = 0);
78///
79/// /// Destroy this `IntQueue` object.
80/// ~IntQueue();
81///
82/// // MANIPULATORS
83///
84/// /// Retrieve an integer from this `IntQueue` object. Integer values
85/// /// are obtained from the queue in FIFO order.
86/// int getInt();
87///
88/// /// Push the specified `value` to this `IntQueue` object.
89/// void pushInt(int value);
90/// };
91/// @endcode
92/// Note that the `IntQueue` constructor increments the count of the semaphore
93/// to 1 so that values can be pushed into the queue immediately following
94/// construction:
95/// @code
96/// // CREATORS
97/// IntQueue::IntQueue(bslma::Allocator *basicAllocator)
98/// : d_queue(basicAllocator)
99/// {
100/// d_mutexSem.post();
101/// }
102///
103/// IntQueue::~IntQueue()
104/// {
105/// d_mutexSem.wait(); // Wait for potential modifier.
106/// }
107///
108/// // MANIPULATORS
109/// int IntQueue::getInt()
110/// {
111/// // Waiting for resources.
112/// d_resourceSem.wait();
113///
114/// // `d_mutexSem` is used for exclusive access.
115/// d_mutexSem.wait(); // lock
116/// const int ret = d_queue.back();
117/// d_queue.pop_back();
118/// d_mutexSem.post(); // unlock
119///
120/// return ret;
121/// }
122///
123/// void IntQueue::pushInt(int value)
124/// {
125/// d_mutexSem.wait();
126/// d_queue.push_front(value);
127/// d_mutexSem.post();
128///
129/// d_resourceSem.post(); // Signal we have resources available.
130/// }
131/// @endcode
132/// @}
133/** @} */
134/** @} */
135
136/** @addtogroup bsl
137 * @{
138 */
139/** @addtogroup bslmt
140 * @{
141 */
142/** @addtogroup bslmt_semaphore
143 * @{
144 */
145
146#include <bslscm_version.h>
147
148#include <bslmt_platform.h>
152
153
154namespace bslmt {
155
156template <class SEMAPHORE_POLICY>
158
159 // ===============
160 // class Semaphore
161 // ===============
162
163/// This class implements a portable semaphore type for thread
164/// synchronization. It forwards all requests to an appropriate
165/// platform-specific implementation.
166///
167/// See @ref bslmt_semaphore
169
170 // DATA
171 SemaphoreImpl<Platform::SemaphorePolicy> d_impl; // platform-specific
172 // implementation
173
174 // NOT IMPLEMENTED
175 Semaphore(const Semaphore&);
176 Semaphore& operator=(const Semaphore&);
177
178 public:
179 // CREATORS
180
181 /// Create a semaphore initially having a count of 0. This
182 /// method does not return normally unless there are sufficient system
183 /// resources to construct the object.
184 Semaphore();
185
186 /// Create a semaphore initially having the specified `count`. This
187 /// method does not return normally unless there are sufficient system
188 /// resources to construct the object.
189 explicit
190 Semaphore(int count);
191
192 /// Destroy this semaphore.
193 ~Semaphore();
194
195 // MANIPULATORS
196
197 /// Atomically increment the count of this semaphore.
198 void post();
199
200 /// Atomically increase the count of this semaphore by the specified
201 /// `value`. The behavior is undefined unless `value > 0`.
202 void post(int value);
203
204 /// If the count of this semaphore is positive, atomically decrement the
205 /// count and return 0; otherwise, return a non-zero value with no
206 /// effect on the count.
207 int tryWait();
208
209 /// Block until the count of this semaphore is a positive value, then
210 /// atomically decrement the count and return.
211 void wait();
212
213 // ACCESSORS
214
215 /// Return the value of the current count of this semaphore.
216 int getValue() const;
217};
218
219// ============================================================================
220// INLINE DEFINITIONS
221// ============================================================================
222
223 // ---------------
224 // class Semaphore
225 // ---------------
226
227// CREATORS
228inline
230: d_impl(0)
231{
232}
233
234inline
236: d_impl(count)
237{
238}
239
240inline
244
245inline
247{
248 d_impl.post();
249}
250
251inline
252void Semaphore::post(int value)
253{
254 d_impl.post(value);
255}
256
257inline
259{
260 return d_impl.tryWait();
261}
262
263inline
265{
266 d_impl.wait();
267}
268
269// ACCESSORS
270inline
272{
273 return d_impl.getValue();
274}
275
276} // close package namespace
277
278
279#endif
280
281// ----------------------------------------------------------------------------
282// Copyright 2015 Bloomberg Finance L.P.
283//
284// Licensed under the Apache License, Version 2.0 (the "License");
285// you may not use this file except in compliance with the License.
286// You may obtain a copy of the License at
287//
288// http://www.apache.org/licenses/LICENSE-2.0
289//
290// Unless required by applicable law or agreed to in writing, software
291// distributed under the License is distributed on an "AS IS" BASIS,
292// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
293// See the License for the specific language governing permissions and
294// limitations under the License.
295// ----------------------------- END-OF-FILE ----------------------------------
296
297/** @} */
298/** @} */
299/** @} */
Definition bslmt_semaphore.h:157
Definition bslmt_semaphore.h:168
int tryWait()
Definition bslmt_semaphore.h:258
~Semaphore()
Destroy this semaphore.
Definition bslmt_semaphore.h:241
int getValue() const
Return the value of the current count of this semaphore.
Definition bslmt_semaphore.h:271
void wait()
Definition bslmt_semaphore.h:264
Semaphore()
Definition bslmt_semaphore.h:229
void post()
Atomically increment the count of this semaphore.
Definition bslmt_semaphore.h:246
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslmt_barrier.h:344