clang-tools 19.0.0git
Cancellation.cpp
Go to the documentation of this file.
1//===--- Cancellation.cpp -----------------------------------------*-C++-*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include <atomic>
11
12namespace clang {
13namespace clangd {
14
15char CancelledError::ID = 0;
16
17// We don't want a cancelable scope to "shadow" an enclosing one.
19 std::shared_ptr<std::atomic<int>> Cancelled;
21};
23
24std::pair<Context, Canceler> cancelableTask(int Reason) {
25 assert(Reason != 0 && "Can't detect cancellation if Reason is zero");
26 CancelState State;
27 State.Cancelled = std::make_shared<std::atomic<int>>();
29 return {
31 [Reason, Flag(State.Cancelled)] { *Flag = Reason; },
32 };
33}
34
35int isCancelled(const Context &Ctx) {
36 for (const CancelState *State = Ctx.get(StateKey); State != nullptr;
37 State = State->Parent)
38 if (int Reason = State->Cancelled->load())
39 return Reason;
40 return 0;
41}
42
43} // namespace clangd
44} // namespace clang
A context is an immutable container for per-request data that must be propagated through layers that ...
Definition: Context.h:69
Context derive(const Key< Type > &Key, std::decay_t< Type > Value) const &
Derives a child context It is safe to move or destroy a parent context after calling derive().
Definition: Context.h:119
static const Context & current()
Returns the context for the current thread, creating it if needed.
Definition: Context.cpp:27
const Type * get(const Key< Type > &Key) const
Get data stored for a typed Key.
Definition: Context.h:98
Values in a Context are indexed by typed keys.
Definition: Context.h:40
static Key< CancelState > StateKey
std::pair< Context, Canceler > cancelableTask(int Reason)
Defines a new task whose cancellation may be requested.
int isCancelled(const Context &Ctx)
If the current context is within a cancelled task, returns the reason.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
const CancelState * Parent
std::shared_ptr< std::atomic< int > > Cancelled