BDE 4.14.0 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/// `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 // NOT IMPLEMENTED
125 ConditionImpl(const ConditionImpl&);
126 ConditionImpl& operator=(const ConditionImpl&);
127
128 public:
129 // TYPES
130
131 /// The value `timedWait` returns when a timeout occurs.
132 enum { e_TIMED_OUT = -1 };
133
134 // CREATORS
135
136 /// Create a condition variable object. Optionally specify a
137 /// `clockType` indicating the type of the system clock against which
138 /// the `bsls::TimeInterval` `absTime` timeouts passed to the
139 /// `timedWait` method are to be interpreted (see {Supported
140 /// Clock-Types} in the component-level documentation). If `clockType`
141 /// is not specified then the realtime system clock is used. This
142 /// method does not return normally unless there are sufficient system
143 /// resources to construct the object.
144 explicit
145 ConditionImpl(bsls::SystemClockType::Enum clockType
147
148 /// Destroy condition variable this object.
149 ~ConditionImpl();
150
151 // MANIPULATORS
152
153 /// Signal this condition object; wake up all threads that are currently
154 /// waiting on this condition.
155 void broadcast();
156
157 /// Signal this condition object; wake up a single thread that is
158 /// currently waiting on this condition.
159 void signal();
160
161 /// Atomically unlock the specified `mutex` and suspend execution of the
162 /// current thread until this condition object is "signaled" (i.e., one
163 /// of the `signal` or `broadcast` methods is invoked on this object) or
164 /// until the specified `absTime` timeout expires, then re-acquire a
165 /// lock on the `mutex`. `absTime` is an *absolute* time represented as
166 /// an interval from some epoch, which is determined by the clock
167 /// indicated at construction (see {Supported Clock-Types} in the
168 /// component-level documentation), and is the earliest time at which
169 /// the timeout may occur. The `mutex` remains locked by the calling
170 /// thread upon returning from this function. Return 0 on success,
171 /// `e_TIMED_OUT` on timeout, and a non-zero value different from
172 /// `e_TIMED_OUT` if an error occurs. The behavior is undefined unless
173 /// `mutex` is locked by the calling thread prior to calling this
174 /// method. Note that spurious wakeups are rare but possible, i.e.,
175 /// this method may succeed (return 0) and return control to the thread
176 /// without the condition object being signaled. Also note that the
177 /// actual time of the timeout depends on many factors including system
178 /// scheduling and system timer resolution, and may be significantly
179 /// later than the time requested.
180 int timedWait(Mutex *mutex, const bsls::TimeInterval& absTime);
181
182 /// Atomically unlock the specified `mutex` and suspend execution of the
183 /// current thread until this condition object is "signaled" (i.e.,
184 /// either `signal` or `broadcast` is invoked on this object in another
185 /// thread), then re-acquire a lock on the `mutex`. Return 0 on
186 /// success, and a non-zero value otherwise. Spurious wakeups are rare
187 /// but possible; i.e., this method may succeed (return 0), and return
188 /// control to the thread without the condition object being signaled.
189 /// The behavior is undefined unless `mutex` is locked by the calling
190 /// thread prior to calling this method. Note that `mutex` remains
191 /// locked by the calling thread upon return from this function.
192 int wait(Mutex *mutex);
193
194 // ACCESSORS
195
196 /// Return the clock type used for timeouts.
197 bsls::SystemClockType::Enum clockType() const;
198};
199
200// ============================================================================
201// INLINE DEFINITIONS
202// ============================================================================
203
204 // -------------------------------------------
205 // class ConditionImpl<Platform::PosixThreads>
206 // -------------------------------------------
207
208// CREATORS
209inline
210ConditionImpl<bslmt::Platform::PosixThreads>::~ConditionImpl()
211{
212 pthread_cond_destroy(&d_cond);
213}
214
215// MANIPULATORS
216inline
217void ConditionImpl<bslmt::Platform::PosixThreads>::broadcast()
218{
219 pthread_cond_broadcast(&d_cond);
220}
221
222inline
223void ConditionImpl<bslmt::Platform::PosixThreads>::signal()
224{
225 pthread_cond_signal(&d_cond);
226}
227
228inline
229int ConditionImpl<bslmt::Platform::PosixThreads>::wait(Mutex *mutex)
230{
231 return pthread_cond_wait(&d_cond, &mutex->nativeMutex());
232}
233
234// ACCESSORS
235inline
237ConditionImpl<bslmt::Platform::PosixThreads>::clockType() const
238{
239 return d_clockType;
240}
241
242} // close package namespace
243
244
245#endif // BSLMT_PLATFORM_POSIX_THREADS
246
247#endif
248
249// ----------------------------------------------------------------------------
250// Copyright 2023 Bloomberg Finance L.P.
251//
252// Licensed under the Apache License, Version 2.0 (the "License");
253// you may not use this file except in compliance with the License.
254// You may obtain a copy of the License at
255//
256// http://www.apache.org/licenses/LICENSE-2.0
257//
258// Unless required by applicable law or agreed to in writing, software
259// distributed under the License is distributed on an "AS IS" BASIS,
260// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
261// See the License for the specific language governing permissions and
262// limitations under the License.
263// ----------------------------- END-OF-FILE ----------------------------------
264
265/** @} */
266/** @} */
267/** @} */
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