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