BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmt_timedcompletionguard.h
Go to the documentation of this file.
1/// @file bslmt_timedcompletionguard.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmt_timedcompletionguard.h -*-C++-*-
8#ifndef INCLUDED_BSLMT_TIMEDCOMPLETIONGUARD
9#define INCLUDED_BSLMT_TIMEDCOMPLETIONGUARD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmt_timedcompletionguard bslmt_timedcompletionguard
15/// @brief Provide guard to verify work completion within a set duration.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmt
19/// @{
20/// @addtogroup bslmt_timedcompletionguard
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmt_timedcompletionguard-purpose"> Purpose</a>
25/// * <a href="#bslmt_timedcompletionguard-classes"> Classes </a>
26/// * <a href="#bslmt_timedcompletionguard-description"> Description </a>
27/// * <a href="#bslmt_timedcompletionguard-usage"> Usage </a>
28/// * <a href="#bslmt_timedcompletionguard-example-1-guarding-work-on-a-thread"> Example 1: Guarding Work on a Thread </a>
29///
30/// # Purpose {#bslmt_timedcompletionguard-purpose}
31/// Provide guard to verify work completion within a set duration.
32///
33/// # Classes {#bslmt_timedcompletionguard-classes}
34///
35/// - bslmt::TimedCompletionGuard: guard to verify completion within a duration
36///
37/// # Description {#bslmt_timedcompletionguard-description}
38/// This component provides a thread-enabled guard,
39/// `bslmt::TimedCompletionGuard`, which invokes a handler displaying provided
40/// diagnostic text if the guard is not destroyed or released within a specified
41/// duration.
42///
43/// ## Usage {#bslmt_timedcompletionguard-usage}
44///
45///
46/// This section illustrates intended use of this component.
47///
48/// ### Example 1: Guarding Work on a Thread {#bslmt_timedcompletionguard-example-1-guarding-work-on-a-thread}
49///
50///
51/// In the following example a `bslmt::TimedCompletionGuard` is used as a
52/// testing aid verify work done on a thread completes in a specified amount of
53/// time. The work is done in two parts, but the actual tasks are not relevant
54/// to the example and is elided.
55///
56/// First, define the two thread functions containing the work to be done:
57/// @code
58/// /// Do some work based upon the specified `arg`.
59/// void *myWorkPart1(void *arg)
60/// {
61/// // // do some stuff...
62/// (void)arg;
63/// return 0;
64/// }
65///
66/// /// Do some other work based upon the specified `arg`.
67/// void *myWorkPart2(void *arg)
68/// {
69/// // do some other stuff...
70/// (void)arg;
71/// return 0;
72/// }
73/// @endcode
74/// Next, we create and configure `tcg` to allow both parts of the work one
75/// second to complete and verify the guard was started successfully:
76/// @code
77/// bslmt::TimedCompletionGuard tcg;
78///
79/// assert(0 == tcg.guard(bsls::TimeInterval(1, 0), "first part"));
80/// @endcode
81/// Then, we create a thread to execute the first part of the work, and join the
82/// created thread when the work completes:
83/// @code
84/// {
85/// bslmt::ThreadUtil::Handle workerHandle;
86///
87/// assert(0 == bslmt::ThreadUtil::create(&workerHandle, myWorkPart1, 0));
88///
89/// bslmt::ThreadUtil::join(workerHandle);
90/// }
91/// @endcode
92/// Next, we update the text displayed if the duration should expire to reflect
93/// the second portion of the work:
94/// @code
95/// tcg.updateText("second part");
96/// @endcode
97/// Now, we use a thread to execute the second part of the work:
98/// @code
99/// {
100/// bslmt::ThreadUtil::Handle workerHandle;
101///
102/// assert(0 == bslmt::ThreadUtil::create(&workerHandle, myWorkPart2, 0));
103///
104/// bslmt::ThreadUtil::join(workerHandle);
105/// }
106/// @endcode
107/// Finally, we release the guard:
108/// @code
109/// tcg.release();
110/// @endcode
111/// @}
112/** @} */
113/** @} */
114
115/** @addtogroup bsl
116 * @{
117 */
118/** @addtogroup bslmt
119 * @{
120 */
121/** @addtogroup bslmt_timedcompletionguard
122 * @{
123 */
124
125#include <bslscm_version.h>
126
127#include <bslma_allocator.h>
128
129#include <bslmt_condition.h>
130#include <bslmt_lockguard.h>
131#include <bslmt_mutex.h>
132#include <bslmt_threadutil.h>
133
134#include <bsls_annotation.h>
135#include <bsls_timeinterval.h>
136#include <bsls_systemclocktype.h>
137
138#include <bsl_string.h>
139#include <bsl_string_view.h>
140
141
142namespace bslmt {
143
144 // ==========================
145 // class TimedCompletionGuard
146 // ==========================
147
148/// This class implements a guard for work completion within a specified
149/// duration.
150///
151/// See @ref bslmt_timedcompletionguard
153 public:
154 // TYPES
155
156 /// `Handler` is an alias for a pointer to a function returning `void`,
157 /// and taking, as a parameter, a null-terminated string, which is the
158 /// structure of the handler function supported by this class -- e.g.,
159 /// @code
160 /// void myHandler(const char *text);
161 /// @endcode
162 typedef void (*Handler)(const char *);
163
164 private:
165
166 // CLASS DATA
167 static const bsls::TimeInterval k_DISABLED; // value used in
168 // `d_expiration` to indicate
169 // thread should return from
170 // `guardThreadFunction`
171
172 static const bsls::TimeInterval k_WAITING; // value used in
173 // `d_expiration` to indicate
174 // thread is waiting for an
175 // expiration value
176
177 // PRIVATE CLASS METHODS
178
179 /// Thread function that implements the guard functionality on the
180 /// specified `arg`, which is a pointer to a `ThreadCompletionGuard`
181 /// object.
182 static void *guardThreadFunction(void *arg);
183
184 // DATA
185 mutable bslmt::Mutex d_handleMutex; // mutex protecting
186 // `d_threadHandle`,
187 // synchronizes all public
188 // methods
189
190 bslmt::Mutex d_dataMutex; // mutex protecting all other
191 // data, and used with
192 // `d_condition`
193
194 bslmt::Condition d_condition; // condition variable used to
195 // signal when the work is
196 // complete
197
198 bslmt::ThreadUtil::Handle d_threadHandle; // thread handle
199
200 bsls::TimeInterval d_expiration; // expiration time
201
202 bsl::string d_text; // text to display at expiration
203
204 Handler d_handler; // handler function for
205 // expiration
206
207 private:
208 // NOT IMPLEMENTED
211
212 public:
213 // TYPES
214 typedef bsl::allocator<> allocator_type; // allocator-aware trait
215
216 // CLASS METHODS
217
218 /// (Default Handler) Emulate the invocation of the standard `assert`
219 /// macro with a `false` argument, using the expression `text` to
220 /// generate a helpful output message and then, after logging, unconditionally aborting.
221 ///
222 /// \note Note that this handler function is the
223 /// default installed assertion handler.
225 static void failByAbort(const char *text);
226
227 // CREATORS
228
229 /// Create the guard. Optionally specify a `handler` function that is
230 /// invoked when a `set` duration expires. If `handler` is not specified,
231 /// `failByAbort` is used. Optionally specify a `basicAllocator` used to
232 /// supply memory. If `basicAllocator` is not specified, the currently
233 /// installed default allocator is used.
235 explicit TimedCompletionGuard(const allocator_type& basicAllocator);
237 Handler function,
238 const allocator_type& basicAllocator = allocator_type());
239
240 /// Invoke `release` and destroy this guard.
242
243 // MANIPULATORS
244
245 /// Configure a thread to, at the expiration of the specified `duration`,
246 /// invoke the handler assigned at object creation with the specified
247 /// `text`, unless the `release` method is invoked or this guard is
248 /// rescheduled with a subsequent call to `guard`. Return 0 on success,
249 /// and a non-zero value if a thread is not available and could not be created.
250 ///
251 /// \pre The behavior is undefined unless `duration` is a positive
252 /// value.
253 int guard(const bsls::TimeInterval& duration,
254 const bsl::string_view& text);
255
256 /// If a thread is waiting to alert at expiration of the `set` duration,
257 /// join the thread, thus preventing any alerting.
258 void release();
259
260 /// Use the specified `text` for alerting when the current duration
261 /// expires. Return 0 on success, and a non-zero value if
262 /// `false == isGuarding()`.
263 int updateText(const bsl::string_view& text);
264
265 // ACCESSORS
266
267 /// Return the allocator used to supply memory.
269
270
271 /// Return `true` if this guard is currently configured with a duration to
272 /// guard, and `false` otherwise.
273 bool isGuarding() const;
274};
275
276// ============================================================================
277// INLINE DEFINITIONS
278// ============================================================================
279
280 // --------------------------
281 // class TimedCompletionGuard
282 // --------------------------
283
284// CREATORS
285inline
287: d_handleMutex()
288, d_dataMutex()
289, d_condition(bsls::SystemClockType::e_MONOTONIC)
290, d_threadHandle(bslmt::ThreadUtil::invalidHandle())
291, d_expiration(k_DISABLED)
292, d_text()
293, d_handler(failByAbort)
294{
295}
296
297inline
299 const allocator_type& basicAllocator)
300: d_handleMutex()
301, d_dataMutex()
302, d_condition(bsls::SystemClockType::e_MONOTONIC)
303, d_threadHandle(bslmt::ThreadUtil::invalidHandle())
304, d_expiration(k_DISABLED)
305, d_text(basicAllocator)
306, d_handler(failByAbort)
307{
308}
309
310inline
312 Handler function,
313 const allocator_type& basicAllocator)
314: d_handleMutex()
315, d_dataMutex()
316, d_condition(bsls::SystemClockType::e_MONOTONIC)
317, d_threadHandle(bslmt::ThreadUtil::invalidHandle())
318, d_expiration(k_DISABLED)
319, d_text(basicAllocator)
320, d_handler(function)
321{
322}
323
324inline
329
330// ACCESSORS
331
332inline
338
339inline
341{
342 bslmt::LockGuard<bslmt::Mutex> guardHandle(&d_handleMutex);
343
344 return k_DISABLED != d_expiration && k_WAITING != d_expiration;
345}
346
347} // close package namespace
348
349
350#endif
351
352// ----------------------------------------------------------------------------
353// Copyright 2025 Bloomberg Finance L.P.
354//
355// Licensed under the Apache License, Version 2.0 (the "License");
356// you may not use this file except in compliance with the License.
357// You may obtain a copy of the License at
358//
359// http://www.apache.org/licenses/LICENSE-2.0
360//
361// Unless required by applicable law or agreed to in writing, software
362// distributed under the License is distributed on an "AS IS" BASIS,
363// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
364// See the License for the specific language governing permissions and
365// limitations under the License.
366// ----------------------------- END-OF-FILE ----------------------------------
367
368/** @} */
369/** @} */
370/** @} */
Definition bslma_bslallocator.h:588
Definition bslstl_stringview.h:471
Definition bslstl_string.h:1252
allocator_type get_allocator() const BSLS_KEYWORD_NOEXCEPT
Return the allocator used by this string to supply memory.
Definition bslstl_string.h:7423
Definition bslmt_condition.h:220
Definition bslmt_lockguard.h:234
Definition bslmt_mutex.h:317
Definition bslmt_timedcompletionguard.h:152
~TimedCompletionGuard()
Invoke release and destroy this guard.
Definition bslmt_timedcompletionguard.h:325
bool isGuarding() const
Definition bslmt_timedcompletionguard.h:340
static BSLS_ANNOTATION_NORETURN void failByAbort(const char *text)
void(* Handler)(const char *)
Definition bslmt_timedcompletionguard.h:162
int updateText(const bsl::string_view &text)
bsl::allocator allocator_type
Definition bslmt_timedcompletionguard.h:214
TimedCompletionGuard()
Definition bslmt_timedcompletionguard.h:286
allocator_type get_allocator() const
Return the allocator used to supply memory.
Definition bslmt_timedcompletionguard.h:333
int guard(const bsls::TimeInterval &duration, const bsl::string_view &text)
Definition bsls_timeinterval.h:307
#define BSLS_ANNOTATION_NORETURN
Definition bsls_annotation.h:378
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslmt_barrier.h:344
Definition bdlt_iso8601util.h:707
Definition bslmt_threadutil.h:379
Imp::Handle Handle
Definition bslmt_threadutil.h:389