BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstl_stopstate.h
Go to the documentation of this file.
1/// @file bslstl_stopstate.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_stopstate.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_STOPSTATE
9#define INCLUDED_BSLSTL_STOPSTATE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstl_stopstate bslstl_stopstate
15/// @brief Provide a stop state for `std`-compliant stop tokens.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_stopstate
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_stopstate-purpose"> Purpose</a>
25/// * <a href="#bslstl_stopstate-classes"> Classes </a>
26/// * <a href="#bslstl_stopstate-canonical-header"> Canonical Header </a>
27/// * <a href="#bslstl_stopstate-description"> Description </a>
28/// * <a href="#bslstl_stopstate-usage"> Usage </a>
29///
30/// # Purpose {#bslstl_stopstate-purpose}
31/// Provide a stop state for `std`-compliant stop tokens.
32///
33/// # Classes {#bslstl_stopstate-classes}
34///
35/// - bslstl::StopState: mechanism for requesting stops and invoking callbacks
36/// - bslstl::StopStateCallbackNode: base class for stop callbacks
37///
38/// # Canonical Header {#bslstl_stopstate-canonical-header}
39/// bsl_stop_token.h
40///
41/// @see bslstl_stopsource, bslstl_stoptoken, bslstl_stopcallback
42///
43/// # Description {#bslstl_stopstate-description}
44/// This component provides the `bslstl::StopState` and
45/// `bslstl::StopStateCallbackNode` classes, which are for internal use only
46/// and are used to implement `bsl::stop_source`, `bsl::stop_token`, and
47/// `bsl::stop_callback`. Please include `<bsl_stop_token.h>` instead.
48///
49/// ## Usage {#bslstl_stopstate-usage}
50///
51///
52/// This component is for internal usage only. Use `bsl::stop_source`,
53/// `bsl::stop_token`, and `bsl::stop_callback` instead.
54/// @}
55/** @} */
56/** @} */
57
58/** @addtogroup bsl
59 * @{
60 */
61/** @addtogroup bslstl
62 * @{
63 */
64/** @addtogroup bslstl_stopstate
65 * @{
66 */
67
68#include <bsla_nodiscard.h>
69
70#include <bsls_atomic.h>
71#include <bsls_bsllock.h>
72#include <bsls_exceptionutil.h>
73#include <bsls_keyword.h>
74
75
76namespace bslstl {
77
78 // ========================
79 // class StopState_ListNode
80 // ========================
81
82/// This component-private class represents a node in a doubly-linked list.
83///
84/// See @ref bslstl_stopstate
86 private:
87 // DATA
88 StopState_ListNode *d_prev_p;
89 StopState_ListNode *d_next_p; // set to null by `unlink`
90
91 // FRIENDS
92 friend class StopState;
93};
94
95 // ===========================
96 // class StopStateCallbackNode
97 // ===========================
98
100{
101 private:
102 // DATA
103
104 // flag indicating whether the callback has completed
105 bsls::AtomicBool d_finished;
106
107 // FRIENDS
108 friend class StopState;
109
110 public:
111 // MANIPULATORS
112
113 /// Invoke the callback stored in the derived class.
114 virtual void invoke() BSLS_NOTHROW_SPEC = 0;
115};
116
117 // ===============
118 // class StopState
119 // ===============
120
122 private:
123 // DATA
124
125 // mutex guarding access to this object's data members
126 bsls::BslLock d_stateMutex;
127
128 // head node for linked list of callbacks
129 StopState_ListNode d_head;
130
131 // thread ID of the thread that successfully called `requestStop`
132 unsigned long long d_stoppingThread;
133
134 // pointer to the node containing the callback currently being invoked;
135 // will be set to null if that callback deregisters its own node
136 StopStateCallbackNode *d_currentCallback_p;
137
138 // flag indicating whether a stop has been requested on this object
139 bsls::AtomicBool d_stopRequested;
140
141 // PRIVATE MANIPULATORS
142
143 /// Remove the specified `node` from the list of nodes registered to
144 /// this state and set its `d_next_p` pointer to null.
145 ///
146 /// \pre The behavior is undefined unless `d_stateMutex` is held by the calling thread and
147 /// `node` is registered to this state.
148 void unlink(StopState_ListNode *node);
149
150 private:
151 // NOT IMPLEMENTED
153 StopState& operator=(const StopState&) BSLS_KEYWORD_DELETED;
154
155 public:
156 // CREATORS
157
158 /// Create a `StopState` object that initially has no registered
159 /// callbacks and on which a stop has not been requested.
161
162 // MANIPULATORS
163
164 /// If a stop has already been requested, then invoke the callback of
165 /// the specified `node` and return `false`. Otherwise, add `node` to
166 /// the list of nodes registered to this state and return `true`.
167 ///
168 /// \pre The behavior is undefined if this method is called more than once for a
169 /// given `node`.
171
172 /// If the callback of the specified `node` is currently executing, wait
173 /// for it to complete. Otherwise, remove `node` from the list of nodes registered to this state.
174 ///
175 /// \pre The behavior is undefined if this method
176 /// is called more than once for a given `node` or if `node` was not
177 /// previously registered to this state by a call to `enregister` that
178 /// returned `true`.
180
181 /// If this `StopState` object has already had a stop requested, return
182 /// `false`. Otherwise, atomically mark this `StopState` object as
183 /// having had a stop requested, then execute all registered callbacks
184 /// in an unspecified order, and finally return `true`.
186
187 // ACCESSORS
188
189 /// Return `true` if a stop has been requested on this `StopState`
190 /// object, and `false` otherwise.
192};
193
194} // close package namespace
195
196#endif // INCLUDED_BSLSTL_STOPSTATE
197
198// ----------------------------------------------------------------------------
199// Copyright 2023 Bloomberg Finance L.P.
200//
201// Licensed under the Apache License, Version 2.0 (the "License");
202// you may not use this file except in compliance with the License.
203// You may obtain a copy of the License at
204//
205// http://www.apache.org/licenses/LICENSE-2.0
206//
207// Unless required by applicable law or agreed to in writing, software
208// distributed under the License is distributed on an "AS IS" BASIS,
209// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
210// See the License for the specific language governing permissions and
211// limitations under the License.
212// ----------------------------- END-OF-FILE ----------------------------------
213
214/** @} */
215/** @} */
216/** @} */
Definition bsls_atomic.h:1490
Definition bsls_bsllock.h:176
Definition bslstl_stopstate.h:100
friend class StopState
Definition bslstl_stopstate.h:108
virtual void invoke() BSLS_NOTHROW_SPEC=0
Invoke the callback stored in the derived class.
Definition bslstl_stopstate.h:85
Definition bslstl_stopstate.h:121
void deregister(StopStateCallbackNode *node)
BSLA_NODISCARD bool stopRequested() const
bool enregister(StopStateCallbackNode *node)
#define BSLA_NODISCARD
Definition bsla_nodiscard.h:320
#define BSLS_NOTHROW_SPEC
Definition bsls_exceptionutil.h:386
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_DELETED
Definition bsls_keyword.h:651
Definition bslstl_algorithm.h:84