10#include <condition_variable>
24class CancellationRegistration;
26CancellationRegistration register_cancellation_callback(
27 CancellationToken token, std::function<
void()> callback);
38 std::atomic_bool cancelled{
false};
40 std::condition_variable cv;
41 std::size_t next_callback_id = 1;
42 std::vector<std::pair<std::size_t, std::function<void()>>> callbacks;
54 : state_(std::move(other.state_)), id_(other.id_) {
62 state_ = std::move(other.state_);
71 bool active()
const noexcept {
return state_ !=
nullptr && id_ != 0; }
79 : state_(std::move(state)), id_(
id) {}
81 void unregister()
noexcept {
82 if (!state_ || id_ == 0) {
85 std::lock_guard<std::mutex> lock(state_->mutex);
86 for (
auto iter = state_->callbacks.begin(); iter != state_->callbacks.end();
88 if (iter->first == id_) {
89 state_->callbacks.erase(iter);
97 std::shared_ptr<CancellationState> state_;
118 return state_ !=
nullptr &&
119 state_->cancelled.load(std::memory_order_acquire);
128 if (!state_)
return false;
130 if (state_->cancelled.load(std::memory_order_acquire))
return true;
131 std::unique_lock<std::mutex> lock(state_->mutex);
132 state_->cv.wait(lock, [
this]() {
133 return state_->cancelled.load(std::memory_order_acquire);
141 if (!state_)
return false;
142 if (state_->cancelled.load(std::memory_order_acquire))
return true;
143 std::unique_lock<std::mutex> lock(state_->mutex);
144 if (state_->cv.wait_for(lock, timeout, [
this]() {
145 return state_->cancelled.load(std::memory_order_acquire);
155 if (!state_)
return false;
156 if (state_->cancelled.load(std::memory_order_acquire))
return true;
157 std::unique_lock<std::mutex> lock(state_->mutex);
158 if (state_->cv.wait_until(lock, deadline, [
this]() {
159 return state_->cancelled.load(std::memory_order_acquire);
170 std::function<
void()> callback);
173 : state_(std::move(state)) {}
175 std::shared_ptr<CancellationState> state_;
180inline CancellationRegistration register_cancellation_callback(
181 CancellationToken token, std::function<
void()> callback) {
182 if (!callback || !token.state_) {
183 return CancellationRegistration{};
186 bool run_now =
false;
189 std::lock_guard<std::mutex> lock(token.state_->mutex);
190 if (token.state_->cancelled.load(std::memory_order_acquire)) {
193 id = token.state_->next_callback_id++;
194 token.state_->callbacks.emplace_back(
id, std::move(callback));
203 return CancellationRegistration{};
206 return CancellationRegistration{std::move(token.state_),
id};
223 if (state_ !=
nullptr) {
224 const bool already_cancelled =
225 state_->cancelled.exchange(
true, std::memory_order_acq_rel);
226 std::vector<std::function<void()>> callbacks;
227 if (!already_cancelled) {
228 std::lock_guard<std::mutex> lock(state_->mutex);
229 for (
auto& callback : state_->callbacks) {
230 callbacks.push_back(std::move(callback.second));
232 state_->callbacks.clear();
234 state_->cv.notify_all();
235 for (
auto& callback : callbacks) {
246 return state_ !=
nullptr &&
247 state_->cancelled.load(std::memory_order_acquire);
251 std::shared_ptr<CancellationState> state_;
Owner side of a cooperative cancellation token.
Definition cancellation.hpp:212
CancellationToken token() const noexcept
Returns a token sharing this source's cancellation state.
Definition cancellation.hpp:217
bool cancelled() const noexcept
Returns true when cancellation has been requested.
Definition cancellation.hpp:245
void cancel() const noexcept
Requests cancellation for all tokens created from this source.
Definition cancellation.hpp:222
Copyable token observed by cancellation-aware SDK operations.
Definition cancellation.hpp:104
static CancellationToken none()
Constructs a detached token that is never cancelled.
Definition cancellation.hpp:110
bool wait_for_cancel(std::chrono::steady_clock::time_point deadline) const
Block until cancellation is requested or deadline is reached.
Definition cancellation.hpp:154
bool wait_for_cancel() const
Block until cancellation is requested.
Definition cancellation.hpp:127
CancellationToken()
Constructs a detached token (same as none()).
Definition cancellation.hpp:113
bool wait_for_cancel(std::chrono::milliseconds timeout) const
Block until cancellation is requested or timeout expires.
Definition cancellation.hpp:140
bool cancellable() const noexcept
Returns true when this token is attached to a cancellation source.
Definition cancellation.hpp:123
bool cancelled() const noexcept
Returns true after the associated source has requested cancellation.
Definition cancellation.hpp:117
Definition cancellation.hpp:47
mcp::CancellationToken CancellationToken
Copyable cooperative cancellation token for server handlers.
Definition context.hpp:22
Shared state backing a cancellation token/source pair.
Definition cancellation.hpp:37