BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmt_semaphoreimpl_darwin.h
Go to the documentation of this file.
1/// @file bslmt_semaphoreimpl_darwin.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmt_semaphoreimpl_darwin.h -*-C++-*-
8#ifndef INCLUDED_BSLMT_SEMAPHOREIMPL_DARWIN
9#define INCLUDED_BSLMT_SEMAPHOREIMPL_DARWIN
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmt_semaphoreimpl_darwin bslmt_semaphoreimpl_darwin
15/// @brief Provide a Darwin implementation of `bslmt::Semaphore`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmt
19/// @{
20/// @addtogroup bslmt_semaphoreimpl_darwin
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmt_semaphoreimpl_darwin-purpose"> Purpose</a>
25/// * <a href="#bslmt_semaphoreimpl_darwin-classes"> Classes </a>
26/// * <a href="#bslmt_semaphoreimpl_darwin-description"> Description </a>
27/// * <a href="#bslmt_semaphoreimpl_darwin-usage"> Usage </a>
28///
29/// # Purpose {#bslmt_semaphoreimpl_darwin-purpose}
30/// Provide a Darwin implementation of `bslmt::Semaphore`.
31///
32/// # Classes {#bslmt_semaphoreimpl_darwin-classes}
33///
34/// - bslmt::SemaphoreImpl<DarwinSemaphore>: semaphore specialization for Darwin
35///
36/// @see bslmt_semaphore
37///
38/// # Description {#bslmt_semaphoreimpl_darwin-description}
39/// This component provides an implementation of `bslmt::Semaphore`
40/// for POSIX threads ("pthreads") according to the POSIX support on Darwin
41/// platform, `bslmt::SemaphoreImpl<DarwinSemaphore>`, via the template
42/// specialization:
43/// @code
44/// bslmt::SemaphoreImpl<Platform::DarwinSemaphore>
45/// @endcode
46/// This template class should not be used (directly) by client code. Clients
47/// should instead use `bslmt::Semaphore`.
48///
49/// ## Usage {#bslmt_semaphoreimpl_darwin-usage}
50///
51///
52/// This component is an implementation detail of `bslmt` and is *not* intended
53/// for direct client use. It is subject to change without notice. As such, a
54/// usage example is not provided.
55/// @}
56/** @} */
57/** @} */
58
59/** @addtogroup bsl
60 * @{
61 */
62/** @addtogroup bslmt
63 * @{
64 */
65/** @addtogroup bslmt_semaphoreimpl_darwin
66 * @{
67 */
68
69#include <bslscm_version.h>
70
71#include <bsls_assert.h>
72#include <bsls_platform.h>
73
74#include <bslmt_platform.h>
75
76#ifdef BSLS_PLATFORM_OS_DARWIN
77#include <semaphore.h>
78
79
80namespace bslmt {
81
82template <class SEMAPHORE_POLICY>
83class SemaphoreImpl;
84
85 // ==============================================
86 // class SemaphoreImpl<Platform::DarwinSemaphore>
87 // ==============================================
88
89/// This class provides a full specialization of `SemaphoreImpl` for
90/// pthreads on Darwin. The implementation provided here defines an
91/// efficient proxy for the `sem_t` pthread type, and related operations.
92template <>
93class SemaphoreImpl<Platform::DarwinSemaphore> {
94
95 // DATA
96 sem_t *d_sem_p; // pointer to native semaphore handle
97 static const char *s_semaphorePrefix; // prefix for a unique semaphore name
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 SemaphoreImpl(int count);
111
112 /// Destroy a semaphore
113 ~SemaphoreImpl();
114
115 // MANIPULATORS
116
117 /// Atomically increment the count of this semaphore.
118 void post();
119
120 /// Atomically increment the count of this semaphore by the specified `number`.
121 ///
122 /// \pre 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::DarwinSemaphore>
145 // ----------------------------------------------
146
147// CREATORS
148inline
149SemaphoreImpl<bslmt::Platform::DarwinSemaphore>::~SemaphoreImpl()
150{
151 int result = ::sem_close(d_sem_p);
152
153 (void) result;
154 BSLS_ASSERT(result == 0);
155}
156
157// MANIPULATORS
158inline
159void SemaphoreImpl<bslmt::Platform::DarwinSemaphore>::post()
160{
161 int result = ::sem_post(d_sem_p);
162
163 (void) result;
164 BSLS_ASSERT(result == 0);
165}
166
167inline
168int SemaphoreImpl<bslmt::Platform::DarwinSemaphore>::tryWait()
169{
170 return ::sem_trywait(d_sem_p);
171
172}
173
174// ACCESSORS
175inline
176int SemaphoreImpl<bslmt::Platform::DarwinSemaphore>::getValue() const
177{
178 // Not implemented on Darwin, but sem_getvalue still returns success.
180 "`sem_getvalue` is optional in POSIX and not implemented on Darwin");
181 return 0;
182}
183
184} // close package namespace
185
186
187#endif // BSLS_PLATFORM_OS_DARWIN
188
189#endif
190
191// ----------------------------------------------------------------------------
192// Copyright 2023 Bloomberg Finance L.P.
193//
194// Licensed under the Apache License, Version 2.0 (the "License");
195// you may not use this file except in compliance with the License.
196// You may obtain a copy of the License at
197//
198// http://www.apache.org/licenses/LICENSE-2.0
199//
200// Unless required by applicable law or agreed to in writing, software
201// distributed under the License is distributed on an "AS IS" BASIS,
202// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
203// See the License for the specific language governing permissions and
204// limitations under the License.
205// ----------------------------- END-OF-FILE ----------------------------------
206
207/** @} */
208/** @} */
209/** @} */
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_ASSERT_UNREACHABLE(X)
Definition bsls_assert.h:1996
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslmt_barrier.h:344