BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmt_timedsemaphoreimpl_posixadv.h
Go to the documentation of this file.
1/// @file bslmt_timedsemaphoreimpl_posixadv.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmt_timedsemaphoreimpl_posixadv.h -*-C++-*-
8#ifndef INCLUDED_BSLMT_TIMEDSEMAPHOREIMPL_POSIXADV
9#define INCLUDED_BSLMT_TIMEDSEMAPHOREIMPL_POSIXADV
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmt_timedsemaphoreimpl_posixadv bslmt_timedsemaphoreimpl_posixadv
15/// @brief Provide "advanced" POSIX implementation of `bslmt::TimedSemaphore`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmt
19/// @{
20/// @addtogroup bslmt_timedsemaphoreimpl_posixadv
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmt_timedsemaphoreimpl_posixadv-purpose"> Purpose</a>
25/// * <a href="#bslmt_timedsemaphoreimpl_posixadv-classes"> Classes </a>
26/// * <a href="#bslmt_timedsemaphoreimpl_posixadv-description"> Description </a>
27/// * <a href="#bslmt_timedsemaphoreimpl_posixadv-supported-clock-types"> Supported Clock-Types </a>
28/// * <a href="#bslmt_timedsemaphoreimpl_posixadv-usage"> Usage </a>
29///
30/// # Purpose {#bslmt_timedsemaphoreimpl_posixadv-purpose}
31/// Provide "advanced" POSIX implementation of `bslmt::TimedSemaphore`.
32///
33/// # Classes {#bslmt_timedsemaphoreimpl_posixadv-classes}
34///
35/// - bslmt::TimedSemaphoreImpl<PosixAdvTimedSemaphore>: POSIXa specialization
36///
37/// @see bslmt_timedsemaphore
38///
39/// # Description {#bslmt_timedsemaphoreimpl_posixadv-description}
40/// This component provides an implementation of
41/// `bslmt::TimedSemaphore`,
42/// `bslmt::TimedSemaphoreImpl<PosixAdvTimedSemaphore>`, for conforming POSIX
43/// platforms via the template specialization:
44/// @code
45/// bslmt::TimedSemaphoreImpl<Platform::PosixAdvTimedSemaphore>
46/// @endcode
47/// This template class should not be used (directly) by client code. Clients
48/// should instead use `bslmt::TimedSemaphore`.
49///
50/// This implementation of `bslmt::TimedSemaphore` is preferred over that
51/// defined in @ref bslmt_timedsemaphoreimpl_pthread on platforms that support
52/// advanced realtime POSIX extensions (e.g., @ref sem_timedwait ).
53///
54/// ## Supported Clock-Types {#bslmt_timedsemaphoreimpl_posixadv-supported-clock-types}
55///
56///
57/// `bsls::SystemClockType` supplies the enumeration indicating the system clock
58/// on which timeouts supplied to other methods should be based. If the clock
59/// type indicated at construction is `bsls::SystemClockType::e_REALTIME`, the
60/// `absTime` argument passed to the `timedWait` method should be expressed as
61/// an *absolute* offset since 00:00:00 UTC, January 1, 1970 (which matches the
62/// epoch used in `bsls::SystemTime::now(bsls::SystemClockType::e_REALTIME)`.
63/// If the clock type indicated at construction is
64/// `bsls::SystemClockType::e_MONOTONIC`, the `absTime` argument passed to the
65/// `timedWait` method should be expressed as an *absolute* offset since the
66/// epoch of this clock (which matches the epoch used in
67/// `bsls::SystemTime::now(bsls::SystemClockType::e_MONOTONIC)`.
68///
69/// ## Usage {#bslmt_timedsemaphoreimpl_posixadv-usage}
70///
71///
72/// This component is an implementation detail of `bslmt` and is *not* intended
73/// for direct client use. It is subject to change without notice. As such, a
74/// usage example is not provided.
75/// @}
76/** @} */
77/** @} */
78
79/** @addtogroup bsl
80 * @{
81 */
82/** @addtogroup bslmt
83 * @{
84 */
85/** @addtogroup bslmt_timedsemaphoreimpl_posixadv
86 * @{
87 */
88
89#include <bslscm_version.h>
90
91#include <bslmt_platform.h>
92
93#ifdef BSLMT_PLATFORM_POSIXADV_TIMEDSEMAPHORE
94
95// Platform-specific implementation starts here.
96
97#include <bsls_assert.h>
99#include <bsls_timeinterval.h>
100
101#include <semaphore.h>
102
103
104namespace bslmt {
105
106template <class TIMED_SEMAPHORE_POLICY>
107class TimedSemaphoreImpl;
108
109 // ================================================
110 // class TimedSemaphoreImpl<PosixAdvTimedSemaphore>
111 // ================================================
112
113/// This class implements a timed semaphore in terms of POSIX operations.
114///
115/// \note Note that only certain platforms provide @ref sem_timedwait ; on those that
116/// do not, `TimedSemaphoreImpl<PthreadTimedSemaphore>` is used.
117template <>
118class TimedSemaphoreImpl<Platform::PosixAdvTimedSemaphore> {
119
120 // DATA
121 sem_t d_sem; // POSIX timed semaphore
122
123 bsls::SystemClockType::Enum d_clockType; // clock type used for 'absTime'
124 // in 'timedWait'
125
126 private:
127 // NOT IMPLEMENTED
128 TimedSemaphoreImpl(const TimedSemaphoreImpl&);
129 TimedSemaphoreImpl& operator=(const TimedSemaphoreImpl&);
130
131 public:
132 // TYPES
133
134 /// The value `timedWait` returns when a timeout occurs.
135 enum { e_TIMED_OUT = -1 };
136
137 // CREATORS
138
139 /// Create a timed semaphore initially having a count of 0. Optionally
140 /// specify a `clockType` indicating the type of the system clock
141 /// against which the `bsls::TimeInterval` `absTime` timeouts passed to
142 /// the `timedWait` method are to be interpreted (see {Supported
143 /// Clock-Types} in the component documentation). If `clockType` is not
144 /// specified then the realtime system clock is used. This method does
145 /// not return normally unless there are sufficient system resources to
146 /// construct the object.
147 explicit
148 TimedSemaphoreImpl(bsls::SystemClockType::Enum clockType
150
151 /// Create a timed semaphore initially having the specified `count`.
152 /// Optionally specify a `clockType` indicating the type of the system
153 /// clock against which the `bsls::TimeInterval` `absTime` timeouts
154 /// passed to the `timedWait` method are to be interpreted (see
155 /// {Supported Clock-Types} in the component documentation). If
156 /// `clockType` is not specified then the realtime system clock is used.
157 /// This method does not return normally unless there are sufficient
158 /// system resources to construct the object.
159 explicit
160 TimedSemaphoreImpl(int count,
163
164 /// Destroy this semaphore object.
165 ~TimedSemaphoreImpl();
166
167 // MANIPULATORS
168
169 /// Atomically increment the count of the semaphore.
170 void post();
171
172 /// Atomically increment the count by the specified `number` of the semaphore.
173 ///
174 /// \pre The behavior is undefined unless `number` is a positive
175 /// value.
176 void post(int number);
177
178 /// Block until the count of this semaphore is a positive value, or
179 /// until the specified `absTime` timeout expires. `absTime` is an
180 /// *absolute* time represented as an interval from some epoch, which is
181 /// determined by the clock indicated at construction (see {Supported
182 /// Clock-Types} in the component documentation). If the `absTime`
183 /// timeout did not expire before the count attained a positive value,
184 /// atomically decrement the count and return 0; otherwise, return a
185 /// non-zero value with no effect on the count.
186 int timedWait(const bsls::TimeInterval& absTime);
187
188 /// Decrement the count of this semaphore if it is positive and return
189 /// 0. Return a non-zero value otherwise.
190 int tryWait();
191
192 /// Block until the count is a positive value and atomically decrement
193 /// it.
194 void wait();
195
196 // ACCESSORS
197
198 /// Return the clock type used for timeouts.
199 bsls::SystemClockType::Enum clockType() const;
200};
201
202// ============================================================================
203// INLINE DEFINITIONS
204// ============================================================================
205
206 // ------------------------------------------------
207 // class TimedSemaphoreImpl<PosixAdvTimedSemaphore>
208 // ------------------------------------------------
209
210// CREATORS
211inline
212TimedSemaphoreImpl<bslmt::Platform::PosixAdvTimedSemaphore>::
213 TimedSemaphoreImpl(bsls::SystemClockType::Enum clockType)
214: d_clockType(clockType)
215{
216 int result = ::sem_init(&d_sem, 0, 0); (void)result;
217 BSLS_ASSERT_OPT(-1 != result);
218}
219
220inline
221TimedSemaphoreImpl<bslmt::Platform::PosixAdvTimedSemaphore>::
222 TimedSemaphoreImpl(int count, bsls::SystemClockType::Enum clockType)
223: d_clockType(clockType)
224{
225 int result = ::sem_init(&d_sem, 0, count); (void)result;
226 BSLS_ASSERT_OPT(-1 != result);
227}
228
229inline
230TimedSemaphoreImpl<bslmt::Platform::PosixAdvTimedSemaphore>::
231 ~TimedSemaphoreImpl()
232{
233 ::sem_destroy(&d_sem);
234}
235
236// MANIPULATORS
237inline
238void TimedSemaphoreImpl<bslmt::Platform::PosixAdvTimedSemaphore>::post()
239{
240 ::sem_post(&d_sem);
241}
242
243inline
244int TimedSemaphoreImpl<bslmt::Platform::PosixAdvTimedSemaphore>::
245 tryWait()
246{
247 return ::sem_trywait(&d_sem);
248}
249
250// ACCESSORS
251inline
253TimedSemaphoreImpl<bslmt::Platform::PosixAdvTimedSemaphore>::clockType() const
254{
255 return d_clockType;
256}
257
258} // close package namespace
259
260
261#endif // BSLMT_PLATFORM_POSIX_THREADS
262
263#endif
264
265// ----------------------------------------------------------------------------
266// Copyright 2023 Bloomberg Finance L.P.
267//
268// Licensed under the Apache License, Version 2.0 (the "License");
269// you may not use this file except in compliance with the License.
270// You may obtain a copy of the License at
271//
272// http://www.apache.org/licenses/LICENSE-2.0
273//
274// Unless required by applicable law or agreed to in writing, software
275// distributed under the License is distributed on an "AS IS" BASIS,
276// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
277// See the License for the specific language governing permissions and
278// limitations under the License.
279// ----------------------------- END-OF-FILE ----------------------------------
280
281/** @} */
282/** @} */
283/** @} */
Definition bsls_timeinterval.h:307
#define BSLS_ASSERT_OPT(X)
Definition bsls_assert.h:2045
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslmt_barrier.h:344
Enum
Definition bsls_systemclocktype.h:119
@ e_REALTIME
Definition bsls_systemclocktype.h:122