150
|
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
|