clang-tools  10.0.0svn
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;
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
Values in a Context are indexed by typed keys.
Definition: Context.h:40
bool isCancelled(const Context &Ctx)
True if the current context is within a cancelable task which was cancelled.
const Type * get(const Key< Type > &Key) const
Get data stored for a typed Key.
Definition: Context.h:100
Context Ctx
static const Context & current()
Returns the context for the current thread, creating it if needed.
Definition: Context.cpp:27
static Key< std::shared_ptr< std::atomic< bool > > > FlagKey
std::pair< Context, Canceler > cancelableTask()
Defines a new task whose cancellation may be requested.
A context is an immutable container for per-request data that must be propagated through layers that ...
Definition: Context.h:69
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
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