BDE 4.14.0 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 `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/// Note that only certain platforms provide @ref sem_timedwait ; on those that
115/// do not, `TimedSemaphoreImpl<PthreadTimedSemaphore>` is used.
116template <>
117class TimedSemaphoreImpl<Platform::PosixAdvTimedSemaphore> {
118
119 // DATA
120 sem_t d_sem; // POSIX timed semaphore
121
122 bsls::SystemClockType::Enum d_clockType; // clock type used for 'absTime'
123 // in 'timedWait'
124
125 // NOT IMPLEMENTED
126 TimedSemaphoreImpl(const TimedSemaphoreImpl&);
127 TimedSemaphoreImpl& operator=(const TimedSemaphoreImpl&);
128
129 public:
130 // TYPES
131
132 /// The value `timedWait` returns when a timeout occurs.
133 enum { e_TIMED_OUT = -1 };
134
135 // CREATORS
136
137 /// Create a timed semaphore initially having a count of 0. Optionally
138 /// specify a `clockType` indicating the type of the system clock
139 /// against which the `bsls::TimeInterval` `absTime` timeouts passed to
140 /// the `timedWait` method are to be interpreted (see {Supported
141 /// Clock-Types} in the component documentation). If `clockType` is not
142 /// specified then the realtime system clock is used. This method does
143 /// not return normally unless there are sufficient system resources to
144 /// construct the object.
145 explicit
146 TimedSemaphoreImpl(bsls::SystemClockType::Enum clockType
148
149 /// Create a timed semaphore initially having the specified `count`.
150 /// Optionally specify a `clockType` indicating the type of the system
151 /// clock against which the `bsls::TimeInterval` `absTime` timeouts
152 /// passed to the `timedWait` method are to be interpreted (see
153 /// {Supported Clock-Types} in the component documentation). If
154 /// `clockType` is not specified then the realtime system clock is used.
155 /// This method does not return normally unless there are sufficient
156 /// system resources to construct the object.
157 explicit
158 TimedSemaphoreImpl(int count,
161
162 /// Destroy this semaphore object.
163 ~TimedSemaphoreImpl();
164
165 // MANIPULATORS
166
167 /// Atomically increment the count of the semaphore.
168 void post();
169
170 /// Atomically increment the count by the specified `number` of the
171 /// semaphore. The behavior is undefined unless `number` is a positive
172 /// value.
173 void post(int number);
174
175 /// Block until the count of this semaphore is a positive value, or
176 /// until the specified `absTime` timeout expires. `absTime` is an
177 /// *absolute* time represented as an interval from some epoch, which is
178 /// determined by the clock indicated at construction (see {Supported
179 /// Clock-Types} in the component documentation). If the `absTime`
180 /// timeout did not expire before the count attained a positive value,
181 /// atomically decrement the count and return 0; otherwise, return a
182 /// non-zero value with no effect on the count.
183 int timedWait(const bsls::TimeInterval& absTime);
184
185 /// Decrement the count of this semaphore if it is positive and return
186 /// 0. Return a non-zero value otherwise.
187 int tryWait();
188
189 /// Block until the count is a positive value and atomically decrement
190 /// it.
191 void wait();
192
193 // ACCESSORS
194
195 /// Return the clock type used for timeouts.
196 bsls::SystemClockType::Enum clockType() const;
197};
198
199// ============================================================================
200// INLINE DEFINITIONS
201// ============================================================================
202
203 // ------------------------------------------------
204 // class TimedSemaphoreImpl<PosixAdvTimedSemaphore>
205 // ------------------------------------------------
206
207// CREATORS
208inline
209TimedSemaphoreImpl<bslmt::Platform::PosixAdvTimedSemaphore>::
210 TimedSemaphoreImpl(bsls::SystemClockType::Enum clockType)
211: d_clockType(clockType)
212{
213 int result = ::sem_init(&d_sem, 0, 0); (void)result;
214 BSLS_ASSERT_OPT(-1 != result);
215}
216
217inline
218TimedSemaphoreImpl<bslmt::Platform::PosixAdvTimedSemaphore>::
219 TimedSemaphoreImpl(int count, bsls::SystemClockType::Enum clockType)
220: d_clockType(clockType)
221{
222 int result = ::sem_init(&d_sem, 0, count); (void)result;
223 BSLS_ASSERT_OPT(-1 != result);
224}
225
226inline
227TimedSemaphoreImpl<bslmt::Platform::PosixAdvTimedSemaphore>::
228 ~TimedSemaphoreImpl()
229{
230 ::sem_destroy(&d_sem);
231}
232
233// MANIPULATORS
234inline
235void TimedSemaphoreImpl<bslmt::Platform::PosixAdvTimedSemaphore>::post()
236{
237 ::sem_post(&d_sem);
238}
239
240inline
241int TimedSemaphoreImpl<bslmt::Platform::PosixAdvTimedSemaphore>::
242 tryWait()
243{
244 return ::sem_trywait(&d_sem);
245}
246
247// ACCESSORS
248inline
250TimedSemaphoreImpl<bslmt::Platform::PosixAdvTimedSemaphore>::clockType() const
251{
252 return d_clockType;
253}
254
255} // close package namespace
256
257
258#endif // BSLMT_PLATFORM_POSIX_THREADS
259
260#endif
261
262// ----------------------------------------------------------------------------
263// Copyright 2023 Bloomberg Finance L.P.
264//
265// Licensed under the Apache License, Version 2.0 (the "License");
266// you may not use this file except in compliance with the License.
267// You may obtain a copy of the License at
268//
269// http://www.apache.org/licenses/LICENSE-2.0
270//
271// Unless required by applicable law or agreed to in writing, software
272// distributed under the License is distributed on an "AS IS" BASIS,
273// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
274// See the License for the specific language governing permissions and
275// limitations under the License.
276// ----------------------------- END-OF-FILE ----------------------------------
277
278/** @} */
279/** @} */
280/** @} */
Definition bsls_timeinterval.h:301
#define BSLS_ASSERT_OPT(X)
Definition bsls_assert.h:1856
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslmt_barrier.h:344
Enum
Definition bsls_systemclocktype.h:117
@ e_REALTIME
Definition bsls_systemclocktype.h:120