Files
clang-p2996/clang-tools-extra/clangd/Cancellation.cpp
Sam McCall c8758406b5 [clangd] Simplify cancellation public API
Summary:
Task is no longer exposed:
 - task cancellation is hidden as a std::function
 - task creation returns the new context directly
 - checking is via free function only, with no way to avoid the context lookup
The implementation is essentially the same, but a bit terser as it's hidden.

isCancelled() is now safe to use outside any task (it returns false).
This will leave us free to sprinkle cancellation in e.g. TUScheduler without
needing elaborate test setup, and lets callers that don't cancel "just work".

Updated the docs to describe the new expected use pattern.
One thing I noticed: there's nothing async-specific about the cancellation.
Async tasks can be cancelled from any thread (typically the one that created
them), sync tasks can be cancelled from any *other* thread in the same way.
So the docs now refer to "long-running" tasks instead of async ones.

Updated usage in code complete, without any structural changes.
I didn't update all the names of the helpers in ClangdLSPServer (these will
likely be moved to JSONRPCDispatcher anyway).

Reviewers: ilya-biryukov, kadircet

Subscribers: ioeric, MaskRay, jkorous, arphaman, jfb, cfe-commits

Differential Revision: https://reviews.llvm.org/D51996

llvm-svn: 342130
2018-09-13 11:47:48 +00:00

35 lines
892 B
C++

//===--- Cancellation.cpp -----------------------------------------*-C++-*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "Cancellation.h"
#include <atomic>
namespace clang {
namespace clangd {
char CancelledError::ID = 0;
static Key<std::shared_ptr<std::atomic<bool>>> FlagKey;
std::pair<Context, Canceler> cancelableTask() {
auto Flag = std::make_shared<std::atomic<bool>>();
return {
Context::current().derive(FlagKey, Flag),
[Flag] { *Flag = true; },
};
}
bool isCancelled() {
if (auto *Flag = Context::current().get(FlagKey))
return **Flag;
return false; // Not in scope of a task.
}
} // namespace clangd
} // namespace clang