BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmt_timedsemaphoreimpl_pthread.h
Go to the documentation of this file.
1/// @file bslmt_timedsemaphoreimpl_pthread.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmt_timedsemaphoreimpl_pthread.h -*-C++-*-
8#ifndef INCLUDED_BSLMT_TIMEDSEMAPHOREIMPL_PTHREAD
9#define INCLUDED_BSLMT_TIMEDSEMAPHOREIMPL_PTHREAD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmt_timedsemaphoreimpl_pthread bslmt_timedsemaphoreimpl_pthread
15/// @brief Provide a POSIX implementation of `bslmt::TimedSemaphore`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmt
19/// @{
20/// @addtogroup bslmt_timedsemaphoreimpl_pthread
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmt_timedsemaphoreimpl_pthread-purpose"> Purpose</a>
25/// * <a href="#bslmt_timedsemaphoreimpl_pthread-classes"> Classes </a>
26/// * <a href="#bslmt_timedsemaphoreimpl_pthread-description"> Description </a>
27/// * <a href="#bslmt_timedsemaphoreimpl_pthread-supported-clock-types"> Supported Clock-Types </a>
28/// * <a href="#bslmt_timedsemaphoreimpl_pthread-usage"> Usage </a>
29///
30/// # Purpose {#bslmt_timedsemaphoreimpl_pthread-purpose}
31/// Provide a POSIX implementation of `bslmt::TimedSemaphore`.
32///
33/// # Classes {#bslmt_timedsemaphoreimpl_pthread-classes}
34///
35/// - bslmt::TimedSemaphoreImpl<PthreadTimedSemaphore>: POSIX specialization
36///
37/// @see bslmt_timedsemaphore
38///
39/// # Description {#bslmt_timedsemaphoreimpl_pthread-description}
40/// This component provides an implementation of
41/// `bslmt::TimedSemaphore`, `bslmt::TimedSemaphoreImpl<PthreadTimedSemaphore>`,
42/// for POSIX threads ("pthreads") via the template specialization:
43/// @code
44/// bslmt::TimedSemaphoreImpl<Platform::PosixThreads>
45/// @endcode
46/// This template class should not be used (directly) by client code. Clients
47/// should instead use `bslmt::TimedSemaphore`.
48///
49/// ## Supported Clock-Types {#bslmt_timedsemaphoreimpl_pthread-supported-clock-types}
50///
51///
52/// `bsls::SystemClockType` supplies the enumeration indicating the system clock
53/// on which timeouts supplied to other methods should be based. If the clock
54/// type indicated at construction is `bsls::SystemClockType::e_REALTIME`, the
55/// `absTime` argument passed to the `timedWait` method should be expressed as
56/// an *absolute* offset since 00:00:00 UTC, January 1, 1970 (which matches the
57/// epoch used in `bsls::SystemTime::now(bsls::SystemClockType::e_REALTIME)`.
58/// If the clock type indicated at construction is
59/// `bsls::SystemClockType::e_MONOTONIC`, the `absTime` argument passed to the
60/// `timedWait` method should be expressed as an *absolute* offset since the
61/// epoch of this clock (which matches the epoch used in
62/// `bsls::SystemTime::now(bsls::SystemClockType::e_MONOTONIC)`.
63///
64/// ## Usage {#bslmt_timedsemaphoreimpl_pthread-usage}
65///
66///
67/// This component is an implementation detail of `bslmt` and is *not* intended
68/// for direct client use. It is subject to change without notice. As such, a
69/// usage example is not provided.
70/// @}
71/** @} */
72/** @} */
73
74/** @addtogroup bsl
75 * @{
76 */
77/** @addtogroup bslmt
78 * @{
79 */
80/** @addtogroup bslmt_timedsemaphoreimpl_pthread
81 * @{
82 */
83
84#include <bslscm_version.h>
85
86#include <bslmt_platform.h>
87
88#ifdef BSLMT_PLATFORM_POSIX_THREADS
89
90// Platform specific implementation starts here.
91
92#include <bsls_atomic.h>
93#include <bsls_platform.h>
95#include <bsls_timeinterval.h>
96
97#include <pthread.h>
98
99
100namespace bslmt {
101
102template <class TIMED_SEMAPHORE_POLICY>
103class TimedSemaphoreImpl;
104
105 // ===============================================
106 // class TimedSemaphoreImpl<PthreadTimedSemaphore>
107 // ===============================================
108
109/// This class implements a portable semaphore type for thread
110/// synchronization.
111template<>
112class TimedSemaphoreImpl<Platform::PthreadTimedSemaphore> {
113
114 // DATA
115 bsls::AtomicInt d_resources; // resources count
116 bsls::AtomicInt d_waiters; // number of threads waiting
117 pthread_mutex_t d_lock; // lock
118 pthread_cond_t d_condition; // condition
119 bsls::SystemClockType::Enum d_clockType; // clock type
120
121 private:
122 // NOT IMPLEMENTED
123 TimedSemaphoreImpl(const TimedSemaphoreImpl&);
124 TimedSemaphoreImpl& operator=(const TimedSemaphoreImpl&);
125
126 // PRIVATE MANIPULATORS
127
128 /// Block until the count of this semaphore is potentially a positive
129 /// value, or until the specified `absTime` timeout expires. `absTime`
130 /// is an *absolute* time represented as an interval from some epoch,
131 /// which is determined by the clock indicated at construction (see
132 /// {Supported Clock-Types} in the component documentation). Return 0
133 /// if the timeout did not expire, -1 if a timeout occurred, and -2 on
134 /// error.
135 int timedWaitImp(const bsls::TimeInterval& absTime);
136
137 public:
138 // TYPES
139
140 /// The value `timedWait` returns when a timeout occurs.
141 enum { e_TIMED_OUT = 1 };
142
143 // CREATORS
144
145 /// Create a timed semaphore initially having a count of 0. Optionally
146 /// specify a `clockType` indicating the type of the system clock
147 /// against which the `bsls::TimeInterval` `absTime` timeouts passed to
148 /// the `timedWait` method are to be interpreted (see {Supported
149 /// Clock-Types} in the component documentation). If `clockType` is not
150 /// specified then the realtime system clock is used. This method does
151 /// not return normally unless there are sufficient system resources to
152 /// construct the object.
153 explicit
154 TimedSemaphoreImpl(bsls::SystemClockType::Enum clockType
156
157 /// Create a timed semaphore initially having the specified `count`.
158 /// Optionally specify a `clockType` indicating the type of the system
159 /// clock against which the `bsls::TimeInterval` `absTime` timeouts
160 /// passed to the `timedWait` method are to be interpreted (see
161 /// {Supported Clock-Types} in the component documentation). If
162 /// `clockType` is not specified then the realtime system clock is used.
163 /// This method does not return normally unless there are sufficient
164 /// system resources to construct the object.
165 explicit
166 TimedSemaphoreImpl(int count,
169
170 /// Destroy this semaphore object.
171 ~TimedSemaphoreImpl();
172
173 // MANIPULATORS
174
175 /// Atomically increment the count of the semaphore.
176 void post();
177
178 /// Atomically increment the count by the specified `number` of the semaphore.
179 ///
180 /// \pre The behavior is undefined unless `number` is a positive
181 /// value.
182 void post(int number);
183
184 /// Block until the count of this semaphore is a positive value, or
185 /// until the specified `absTime` timeout expires. `absTime` is an
186 /// *absolute* time represented as an interval from some epoch, which is
187 /// determined by the clock indicated at construction (see {Supported
188 /// Clock-Types} in the component documentation). If the `absTime`
189 /// timeout did not expire before the count attained a positive value,
190 /// atomically decrement the count and return 0; otherwise, return a
191 /// non-zero value with no effect on the count.
192 int timedWait(const bsls::TimeInterval& absTime);
193
194 /// Decrement the count of this semaphore if it is positive and return
195 /// 0. Return a non-zero value otherwise.
196 int tryWait();
197
198 /// Block until the count is a positive value and atomically decrement
199 /// it.
200 void wait();
201
202 // ACCESSORS
203
204 /// Return the clock type used for timeouts.
205 bsls::SystemClockType::Enum clockType() const;
206};
207
208} // close package namespace
209
210
211#endif // BSLMT_PLATFORM_POSIX_THREADS
212
213#endif
214
215// ----------------------------------------------------------------------------
216// Copyright 2023 Bloomberg Finance L.P.
217//
218// Licensed under the Apache License, Version 2.0 (the "License");
219// you may not use this file except in compliance with the License.
220// You may obtain a copy of the License at
221//
222// http://www.apache.org/licenses/LICENSE-2.0
223//
224// Unless required by applicable law or agreed to in writing, software
225// distributed under the License is distributed on an "AS IS" BASIS,
226// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
227// See the License for the specific language governing permissions and
228// limitations under the License.
229// ----------------------------- END-OF-FILE ----------------------------------
230
231/** @} */
232/** @} */
233/** @} */
Definition bsls_atomic.h:744
Definition bsls_timeinterval.h:307
#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