clang-tools  8.0.0
Cancellation.cpp
Go to the documentation of this file.
1 //===--- Cancellation.cpp -----------------------------------------*-C++-*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Cancellation.h"
11 #include <atomic>
12 
13 namespace clang {
14 namespace clangd {
15 
16 char CancelledError::ID = 0;
18 
19 std::pair<Context, Canceler> cancelableTask() {
20  auto Flag = std::make_shared<std::atomic<bool>>();
21  return {
22  Context::current().derive(FlagKey, Flag),
23  [Flag] { *Flag = true; },
24  };
25 }
26 
27 bool isCancelled(const Context &Ctx) {
28  if (auto *Flag = Ctx.get(FlagKey))
29  return **Flag;
30  return false; // Not in scope of a task.
31 }
32 
33 } // namespace clangd
34 } // namespace clang
Values in a Context are indexed by typed keys.
Definition: Context.h:41
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:101
Context Ctx
static const Context & current()
Returns the context for the current thread, creating it if needed.
Definition: Context.cpp:28
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:70
===– 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:122