BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmt_semaphoreimpl_pthread.h
Go to the documentation of this file.
1/// @file bslmt_semaphoreimpl_pthread.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmt_semaphoreimpl_pthread.h -*-C++-*-
8#ifndef INCLUDED_BSLMT_SEMAPHOREIMPL_PTHREAD
9#define INCLUDED_BSLMT_SEMAPHOREIMPL_PTHREAD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmt_semaphoreimpl_pthread bslmt_semaphoreimpl_pthread
15/// @brief Provide a POSIX implementation of `bslmt::Semaphore`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmt
19/// @{
20/// @addtogroup bslmt_semaphoreimpl_pthread
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmt_semaphoreimpl_pthread-purpose"> Purpose</a>
25/// * <a href="#bslmt_semaphoreimpl_pthread-classes"> Classes </a>
26/// * <a href="#bslmt_semaphoreimpl_pthread-description"> Description </a>
27/// * <a href="#bslmt_semaphoreimpl_pthread-usage"> Usage </a>
28///
29/// # Purpose {#bslmt_semaphoreimpl_pthread-purpose}
30/// Provide a POSIX implementation of `bslmt::Semaphore`.
31///
32/// # Classes {#bslmt_semaphoreimpl_pthread-classes}
33///
34/// - bslmt::SemaphoreImpl<PosixSemaphore>: POSIX specialization
35///
36/// @see bslmt_semaphore
37///
38/// # Description {#bslmt_semaphoreimpl_pthread-description}
39/// This component provides an implementation of `bslmt::Semaphore`
40/// for POSIX threads ("pthreads"), `bslmt::SemaphoreImpl<PosixSemaphore>`, via
41/// the template specialization:
42/// @code
43/// bslmt::SemaphoreImpl<Platform::PosixThreads>
44/// @endcode
45/// This template class should not be used (directly) by client code. Clients
46/// should instead use `bslmt::Semaphore`.
47///
48/// ## Usage {#bslmt_semaphoreimpl_pthread-usage}
49///
50///
51/// This component is an implementation detail of `bslmt` and is *not* intended
52/// for direct client use. It is subject to change without notice. As such, a
53/// usage example is not provided.
54/// @}
55/** @} */
56/** @} */
57
58/** @addtogroup bsl
59 * @{
60 */
61/** @addtogroup bslmt
62 * @{
63 */
64/** @addtogroup bslmt_semaphoreimpl_pthread
65 * @{
66 */
67
68#include <bslscm_version.h>
69
70#include <bslmt_platform.h>
71
72#if defined(BSLMT_PLATFORM_POSIX_SEMAPHORE)
73
74// Platform-specific implementation starts here.
75
76#include <bsls_assert.h>
77
78#include <semaphore.h>
79
80
81namespace bslmt {
82
83template <class SEMAPHORE_POLICY>
84class SemaphoreImpl;
85
86 // =============================================
87 // class SemaphoreImpl<Platform::PosixSemaphore>
88 // =============================================
89
90/// This class provides a full specialization of `SemaphoreImpl` for
91/// pthreads. The implementation provided here defines an efficient proxy
92/// for the `sem_t` pthread type, and related operations.
93template <>
94class SemaphoreImpl<Platform::PosixSemaphore> {
95
96 // DATA
97 sem_t d_sem; // native semaphore handle
98
99 private:
100 // NOT IMPLEMENTED
101 SemaphoreImpl(const SemaphoreImpl&);
102 SemaphoreImpl& operator=(const SemaphoreImpl&);
103
104 public:
105 // CREATORS
106
107 /// Create a semaphore initialized to the specified `count`. This
108 /// method does not return normally unless there are sufficient system
109 /// resources to construct the object.
110 explicit
111 SemaphoreImpl(int count);
112
113 /// Destroy a semaphore
114 ~SemaphoreImpl();
115
116 // MANIPULATORS
117
118 /// Atomically increment the count of this semaphore.
119 void post();
120
121 /// Atomically increment the count of this semaphore by the specified
122 /// `number`. The behavior is undefined unless `number > 0`.
123 void post(int number);
124
125 /// Decrement the count of this semaphore if it is positive and return
126 /// 0. Return a non-zero value otherwise.
127 int tryWait();
128
129 /// Block until the count of this semaphore is a positive value and
130 /// atomically decrement it.
131 void wait();
132
133 // ACCESSORS
134
135 /// Return the current value of this semaphore.
136 int getValue() const;
137};
138
139// ============================================================================
140// INLINE DEFINITIONS
141// ============================================================================
142
143 // ---------------------------------------------
144 // class SemaphoreImpl<Platform::PosixSemaphore>
145 // ---------------------------------------------
146
147// CREATORS
148inline
149SemaphoreImpl<bslmt::Platform::PosixSemaphore>::~SemaphoreImpl()
150{
151 int result = ::sem_destroy(&d_sem);
152
153 (void) result;
154 BSLS_ASSERT(result == 0);
155}
156
157// MANIPULATORS
158inline
159void SemaphoreImpl<bslmt::Platform::PosixSemaphore>::post()
160{
161 int result = ::sem_post(&d_sem);
162
163 (void) result;
164 BSLS_ASSERT(result == 0);
165}
166
167inline
168int SemaphoreImpl<bslmt::Platform::PosixSemaphore>::tryWait()
169{
170 return ::sem_trywait(&d_sem);
171}
172
173// ACCESSORS
174inline
175int SemaphoreImpl<bslmt::Platform::PosixSemaphore>::getValue() const
176{
177 int value = 0;
178 int result = ::sem_getvalue(const_cast<sem_t *>(&d_sem), &value);
179
180 (void) result;
181 BSLS_ASSERT_SAFE(result == 0);
182
183 return value;
184}
185
186} // close package namespace
187
188
189#endif // defined(BSLMT_PLATFORM_POSIX_SEMAPHORE)
190
191#endif
192
193// ----------------------------------------------------------------------------
194// Copyright 2015 Bloomberg Finance L.P.
195//
196// Licensed under the Apache License, Version 2.0 (the "License");
197// you may not use this file except in compliance with the License.
198// You may obtain a copy of the License at
199//
200// http://www.apache.org/licenses/LICENSE-2.0
201//
202// Unless required by applicable law or agreed to in writing, software
203// distributed under the License is distributed on an "AS IS" BASIS,
204// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
205// See the License for the specific language governing permissions and
206// limitations under the License.
207// ----------------------------- END-OF-FILE ----------------------------------
208
209/** @} */
210/** @} */
211/** @} */
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslmt_barrier.h:344