BDE 4.14.0 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 // NOT IMPLEMENTED
122 TimedSemaphoreImpl(const TimedSemaphoreImpl&);
123 TimedSemaphoreImpl& operator=(const TimedSemaphoreImpl&);
124
125 // PRIVATE MANIPULATORS
126
127 /// Block until the count of this semaphore is potentially a positive
128 /// value, or until the specified `absTime` timeout expires. `absTime`
129 /// is an *absolute* time represented as an interval from some epoch,
130 /// which is determined by the clock indicated at construction (see
131 /// {Supported Clock-Types} in the component documentation). Return 0
132 /// if the timeout did not expire, -1 if a timeout occurred, and -2 on
133 /// error.
134 int timedWaitImp(const bsls::TimeInterval& absTime);
135
136 public:
137 // TYPES
138
139 /// The value `timedWait` returns when a timeout occurs.
140 enum { e_TIMED_OUT = 1 };
141
142 // CREATORS
143
144 /// Create a timed semaphore initially having a count of 0. Optionally
145 /// specify a `clockType` indicating the type of the system clock
146 /// against which the `bsls::TimeInterval` `absTime` timeouts passed to
147 /// the `timedWait` method are to be interpreted (see {Supported
148 /// Clock-Types} in the component documentation). If `clockType` is not
149 /// specified then the realtime system clock is used. This method does
150 /// not return normally unless there are sufficient system resources to
151 /// construct the object.
152 explicit
153 TimedSemaphoreImpl(bsls::SystemClockType::Enum clockType
155
156 /// Create a timed semaphore initially having the specified `count`.
157 /// Optionally specify a `clockType` indicating the type of the system
158 /// clock against which the `bsls::TimeInterval` `absTime` timeouts
159 /// passed to the `timedWait` method are to be interpreted (see
160 /// {Supported Clock-Types} in the component documentation). If
161 /// `clockType` is not specified then the realtime system clock is used.
162 /// This method does not return normally unless there are sufficient
163 /// system resources to construct the object.
164 explicit
165 TimedSemaphoreImpl(int count,
168
169 /// Destroy this semaphore object.
170 ~TimedSemaphoreImpl();
171
172 // MANIPULATORS
173
174 /// Atomically increment the count of the semaphore.
175 void post();
176
177 /// Atomically increment the count by the specified `number` of the
178 /// semaphore. The behavior is undefined unless `number` is a positive
179 /// value.
180 void post(int number);
181
182 /// Block until the count of this semaphore is a positive value, or
183 /// until the specified `absTime` timeout expires. `absTime` is an
184 /// *absolute* time represented as an interval from some epoch, which is
185 /// determined by the clock indicated at construction (see {Supported
186 /// Clock-Types} in the component documentation). If the `absTime`
187 /// timeout did not expire before the count attained a positive value,
188 /// atomically decrement the count and return 0; otherwise, return a
189 /// non-zero value with no effect on the count.
190 int timedWait(const bsls::TimeInterval& absTime);
191
192 /// Decrement the count of this semaphore if it is positive and return
193 /// 0. Return a non-zero value otherwise.
194 int tryWait();
195
196 /// Block until the count is a positive value and atomically decrement
197 /// it.
198 void wait();
199
200 // ACCESSORS
201
202 /// Return the clock type used for timeouts.
203 bsls::SystemClockType::Enum clockType() const;
204};
205
206} // close package namespace
207
208
209#endif // BSLMT_PLATFORM_POSIX_THREADS
210
211#endif
212
213// ----------------------------------------------------------------------------
214// Copyright 2023 Bloomberg Finance L.P.
215//
216// Licensed under the Apache License, Version 2.0 (the "License");
217// you may not use this file except in compliance with the License.
218// You may obtain a copy of the License at
219//
220// http://www.apache.org/licenses/LICENSE-2.0
221//
222// Unless required by applicable law or agreed to in writing, software
223// distributed under the License is distributed on an "AS IS" BASIS,
224// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
225// See the License for the specific language governing permissions and
226// limitations under the License.
227// ----------------------------- END-OF-FILE ----------------------------------
228
229/** @} */
230/** @} */
231/** @} */
Definition bsls_atomic.h:743
Definition bsls_timeinterval.h:301
#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