BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmt_conditionimpl_pthread.h
Go to the documentation of this file.
1/// @file bslmt_conditionimpl_pthread.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmt_conditionimpl_pthread.h -*-C++-*-
8#ifndef INCLUDED_BSLMT_CONDITIONIMPL_PTHREAD
9#define INCLUDED_BSLMT_CONDITIONIMPL_PTHREAD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmt_conditionimpl_pthread bslmt_conditionimpl_pthread
15/// @brief Provide a POSIX implementation of `bslmt::Condition`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmt
19/// @{
20/// @addtogroup bslmt_conditionimpl_pthread
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmt_conditionimpl_pthread-purpose"> Purpose</a>
25/// * <a href="#bslmt_conditionimpl_pthread-classes"> Classes </a>
26/// * <a href="#bslmt_conditionimpl_pthread-description"> Description </a>
27/// * <a href="#bslmt_conditionimpl_pthread-supported-clock-types"> Supported Clock-Types </a>
28/// * <a href="#bslmt_conditionimpl_pthread-usage"> Usage </a>
29///
30/// # Purpose {#bslmt_conditionimpl_pthread-purpose}
31/// Provide a POSIX implementation of `bslmt::Condition`.
32///
33/// # Classes {#bslmt_conditionimpl_pthread-classes}
34///
35/// - bslmt::ConditionImpl<PosixThreads>: POSIX specialization
36///
37/// @see bslmt_condition
38///
39/// # Description {#bslmt_conditionimpl_pthread-description}
40/// This component provides an implementation of `bslmt::Condition`
41/// for POSIX threads ("pthreads"), `bslmt::ConditionImpl<PosixThreads>`, via
42/// the template specialization:
43/// @code
44/// bslmt::ConditionImpl<Platform::PosixThreads>
45/// @endcode
46/// This template class should not be used (directly) by client code. Clients
47/// should instead use `bslmt::Condition`.
48///
49/// ## Supported Clock-Types {#bslmt_conditionimpl_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_conditionimpl_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_conditionimpl_pthread
81 * @{
82 */
83
84#include <bslscm_version.h>
85
86#include <bslmt_mutex.h>
87#include <bslmt_platform.h>
88
89#include <bsls_platform.h>
91#include <bsls_timeinterval.h>
92
93#ifdef BSLMT_PLATFORM_POSIX_THREADS
94
95// Platform-specific implementation starts here.
96
97#include <bsl_ctime.h>
98#include <bsl_c_errno.h>
99
100#include <pthread.h>
101
102
103namespace bslmt {
104
105template <class THREAD_POLICY>
106class ConditionImpl;
107
108 // ===========================================
109 // class ConditionImpl<Platform::PosixThreads>
110 // ===========================================
111
112/// This class provides a full specialization of `Condition` for pthreads.
113/// The implementation provided here defines an efficient proxy for the
114/// @ref pthread_cond_t pthread type, and related operations.
115template <>
116class ConditionImpl<Platform::PosixThreads> {
117
118 // DATA
119 pthread_cond_t d_cond; // provides post/wait for
120 // condition
121
122 bsls::SystemClockType::Enum d_clockType; // clock type used in 'timedWait'
123
124 private:
125 // NOT IMPLEMENTED
126 ConditionImpl(const ConditionImpl&);
127 ConditionImpl& operator=(const ConditionImpl&);
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 condition variable object. Optionally specify a
138 /// `clockType` indicating the type of the system clock against which
139 /// the `bsls::TimeInterval` `absTime` timeouts passed to the
140 /// `timedWait` method are to be interpreted (see {Supported
141 /// Clock-Types} in the component-level documentation). If `clockType`
142 /// is not specified then the realtime system clock is used. This
143 /// method does not return normally unless there are sufficient system
144 /// resources to construct the object.
145 explicit
146 ConditionImpl(bsls::SystemClockType::Enum clockType
148
149 /// Destroy condition variable this object.
150 ~ConditionImpl();
151
152 // MANIPULATORS
153
154 /// Signal this condition object; wake up all threads that are currently
155 /// waiting on this condition.
156 void broadcast();
157
158 /// Signal this condition object; wake up a single thread that is
159 /// currently waiting on this condition.
160 void signal();
161
162 /// Atomically unlock the specified `mutex` and suspend execution of the
163 /// current thread until this condition object is "signaled" (i.e., one
164 /// of the `signal` or `broadcast` methods is invoked on this object) or
165 /// until the specified `absTime` timeout expires, then re-acquire a
166 /// lock on the `mutex`. `absTime` is an *absolute* time represented as
167 /// an interval from some epoch, which is determined by the clock
168 /// indicated at construction (see {Supported Clock-Types} in the
169 /// component-level documentation), and is the earliest time at which
170 /// the timeout may occur. The `mutex` remains locked by the calling
171 /// thread upon returning from this function. Return 0 on success,
172 /// `e_TIMED_OUT` on timeout, and a non-zero value different from `e_TIMED_OUT` if an error occurs.
173 ///
174 /// \pre The behavior is undefined unless
175 /// `mutex` is locked by the calling thread prior to calling this method.
176 ///
177 /// \note Note that spurious wakeups are rare but possible, i.e.,
178 /// this method may succeed (return 0) and return control to the thread
179 /// without the condition object being signaled. Also note that the
180 /// actual time of the timeout depends on many factors including system
181 /// scheduling and system timer resolution, and may be significantly
182 /// later than the time requested.
183 int timedWait(Mutex *mutex, const bsls::TimeInterval& absTime);
184
185 /// Atomically unlock the specified `mutex` and suspend execution of the
186 /// current thread until this condition object is "signaled" (i.e.,
187 /// either `signal` or `broadcast` is invoked on this object in another
188 /// thread), then re-acquire a lock on the `mutex`. Return 0 on
189 /// success, and a non-zero value otherwise. Spurious wakeups are rare
190 /// but possible; i.e., this method may succeed (return 0), and return
191 /// control to the thread without the condition object being signaled.
192 ///
193 /// \pre The behavior is undefined unless `mutex` is locked by the calling thread prior to calling this method.
194 ///
195 /// \note Note that `mutex` remains
196 /// locked by the calling thread upon return from this function.
197 int wait(Mutex *mutex);
198
199 // ACCESSORS
200
201 /// Return the clock type used for timeouts.
202 bsls::SystemClockType::Enum clockType() const;
203};
204
205// ============================================================================
206// INLINE DEFINITIONS
207// ============================================================================
208
209 // -------------------------------------------
210 // class ConditionImpl<Platform::PosixThreads>
211 // -------------------------------------------
212
213// CREATORS
214inline
215ConditionImpl<bslmt::Platform::PosixThreads>::~ConditionImpl()
216{
217 pthread_cond_destroy(&d_cond);
218}
219
220// MANIPULATORS
221inline
222void ConditionImpl<bslmt::Platform::PosixThreads>::broadcast()
223{
224 pthread_cond_broadcast(&d_cond);
225}
226
227inline
228void ConditionImpl<bslmt::Platform::PosixThreads>::signal()
229{
230 pthread_cond_signal(&d_cond);
231}
232
233inline
234int ConditionImpl<bslmt::Platform::PosixThreads>::wait(Mutex *mutex)
235{
236 return pthread_cond_wait(&d_cond, &mutex->nativeMutex());
237}
238
239// ACCESSORS
240inline
242ConditionImpl<bslmt::Platform::PosixThreads>::clockType() const
243{
244 return d_clockType;
245}
246
247} // close package namespace
248
249
250#endif // BSLMT_PLATFORM_POSIX_THREADS
251
252#endif
253
254// ----------------------------------------------------------------------------
255// Copyright 2023 Bloomberg Finance L.P.
256//
257// Licensed under the Apache License, Version 2.0 (the "License");
258// you may not use this file except in compliance with the License.
259// You may obtain a copy of the License at
260//
261// http://www.apache.org/licenses/LICENSE-2.0
262//
263// Unless required by applicable law or agreed to in writing, software
264// distributed under the License is distributed on an "AS IS" BASIS,
265// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
266// See the License for the specific language governing permissions and
267// limitations under the License.
268// ----------------------------- END-OF-FILE ----------------------------------
269
270/** @} */
271/** @} */
272/** @} */
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