clang-tools 10.0.0

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
9 #include "Cancellation.h"
10 #include <atomic>
11
12 namespace clang {
13 namespace clangd {
14
15 char CancelledError::ID = 0;
16 static Key<std::shared_ptr<std::atomic<bool>>> FlagKey;
17
18 std::pair<Context, Canceler> cancelableTask() {
19 auto Flag = std::make_shared<std::atomic<bool>>();
20 return {
21 Context::current().derive(FlagKey, Flag),
22 [Flag] { *Flag = true; },
23 };
24 }
25
26 bool isCancelled(const Context &Ctx) {
27 if (auto *Flag = Ctx.get(FlagKey))
28 return **Flag;
29 return false; // Not in scope of a task.
30 }
31
32 } // namespace clangd
33 } // namespace clang
clang::clangd::CancelledError::ID
static char ID
Definition: Cancellation.h:86
clang::clangd::Key
Values in a Context are indexed by typed keys.
Definition: Context.h:40
clang::clangd::isCancelled
bool isCancelled(const Context &Ctx)
True if the current context is within a cancelable task which was cancelled.
Definition: Cancellation.cpp:26
Cancellation.h
clang::clangd::Context::get
const Type * get(const Key< Type > &Key) const
Get data stored for a typed Key.
Definition: Context.h:100
Ctx
Context Ctx
Definition: TUScheduler.cpp:237
clang::clangd::Context::current
static const Context & current()
Returns the context for the current thread, creating it if needed.
Definition: Context.cpp:27
clang::clangd::FlagKey
static Key< std::shared_ptr< std::atomic< bool > > > FlagKey
Definition: Cancellation.cpp:16
clang::clangd::cancelableTask
std::pair< Context, Canceler > cancelableTask()
Defines a new task whose cancellation may be requested.
Definition: Cancellation.cpp:18
clang::clangd::Context
A context is an immutable container for per-request data that must be propagated through layers that ...
Definition: Context.h:69
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::Context::derive
Context derive(const Key< Type > &Key, typename std::decay< Type >::type Value) const &
Derives a child context It is safe to move or destroy a parent context after calling derive()...
Definition: Context.h:121