BDE 4.14.0 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 // NOT IMPLEMENTED
100 SemaphoreImpl(const SemaphoreImpl&);
101 SemaphoreImpl& operator=(const SemaphoreImpl&);
102
103 public:
104 // CREATORS
105
106 /// Create a semaphore initialized to the specified `count`. This
107 /// method does not return normally unless there are sufficient system
108 /// resources to construct the object.
109 SemaphoreImpl(int count);
110
111 /// Destroy a semaphore
112 ~SemaphoreImpl();
113
114 // MANIPULATORS
115
116 /// Atomically increment the count of this semaphore.
117 void post();
118
119 /// Atomically increment the count of this semaphore by the specified
120 /// `number`. The behavior is undefined unless `number > 0`.
121 void post(int number);
122
123 /// Decrement the count of this semaphore if it is positive and return
124 /// 0. Return a non-zero value otherwise.
125 int tryWait();
126
127 /// Block until the count of this semaphore is a positive value and
128 /// atomically decrement it.
129 void wait();
130
131 // ACCESSORS
132
133 /// Return the current value of this semaphore.
134 int getValue() const;
135};
136
137// ============================================================================
138// INLINE DEFINITIONS
139// ============================================================================
140
141 // ----------------------------------------------
142 // class SemaphoreImpl<Platform::DarwinSemaphore>
143 // ----------------------------------------------
144
145// CREATORS
146inline
147SemaphoreImpl<bslmt::Platform::DarwinSemaphore>::~SemaphoreImpl()
148{
149 int result = ::sem_close(d_sem_p);
150
151 (void) result;
152 BSLS_ASSERT(result == 0);
153}
154
155// MANIPULATORS
156inline
157void SemaphoreImpl<bslmt::Platform::DarwinSemaphore>::post()
158{
159 int result = ::sem_post(d_sem_p);
160
161 (void) result;
162 BSLS_ASSERT(result == 0);
163}
164
165inline
166int SemaphoreImpl<bslmt::Platform::DarwinSemaphore>::tryWait()
167{
168 return ::sem_trywait(d_sem_p);
169
170}
171
172// ACCESSORS
173inline
174int SemaphoreImpl<bslmt::Platform::DarwinSemaphore>::getValue() const
175{
176 // Not implemented on Darwin, but sem_getvalue still returns success.
177 BSLS_ASSERT(false &&
178 "sem_getvalue is optional in POSIX and not implemented on Darwin");
179 return 0;
180}
181
182} // close package namespace
183
184
185#endif // BSLS_PLATFORM_OS_DARWIN
186
187#endif
188
189// ----------------------------------------------------------------------------
190// Copyright 2023 Bloomberg Finance L.P.
191//
192// Licensed under the Apache License, Version 2.0 (the "License");
193// you may not use this file except in compliance with the License.
194// You may obtain a copy of the License at
195//
196// http://www.apache.org/licenses/LICENSE-2.0
197//
198// Unless required by applicable law or agreed to in writing, software
199// distributed under the License is distributed on an "AS IS" BASIS,
200// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201// See the License for the specific language governing permissions and
202// limitations under the License.
203// ----------------------------- END-OF-FILE ----------------------------------
204
205/** @} */
206/** @} */
207/** @} */
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslmt_barrier.h:344