Kadir Cetinkaya | b910a10 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 1 | //===--- Cancellation.h -------------------------------------------*-C++-*-===// |
| 2 | // |
Chandler Carruth | b1ace23 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Kadir Cetinkaya | b910a10 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Sam McCall | f0874bf | 2018-09-13 11:47:48 +0000 | [diff] [blame] | 8 | // Cancellation mechanism for long-running tasks. |
Kadir Cetinkaya | b910a10 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 9 | // |
Sam McCall | f0874bf | 2018-09-13 11:47:48 +0000 | [diff] [blame] | 10 | // This manages interactions between: |
Kadir Cetinkaya | b910a10 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 11 | // |
Sam McCall | f0874bf | 2018-09-13 11:47:48 +0000 | [diff] [blame] | 12 | // 1. Client code that starts some long-running work, and maybe cancels later. |
| 13 | // |
| 14 | // std::pair<Context, Canceler> Task = cancelableTask(); |
| 15 | // { |
| 16 | // WithContext Cancelable(std::move(Task.first)); |
| 17 | // Expected |
| 18 | // deepThoughtAsync([](int answer){ errs() << answer; }); |
Kadir Cetinkaya | b910a10 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 19 | // } |
Sam McCall | f0874bf | 2018-09-13 11:47:48 +0000 | [diff] [blame] | 20 | // // ...some time later... |
| 21 | // if (User.fellAsleep()) |
| 22 | // Task.second(); |
Kadir Cetinkaya | b910a10 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 23 | // |
Sam McCall | f0874bf | 2018-09-13 11:47:48 +0000 | [diff] [blame] | 24 | // (This example has an asynchronous computation, but synchronous examples |
| 25 | // work similarly - the Canceler should be invoked from another thread). |
Kadir Cetinkaya | b910a10 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 26 | // |
Sam McCall | f0874bf | 2018-09-13 11:47:48 +0000 | [diff] [blame] | 27 | // 2. Library code that executes long-running work, and can exit early if the |
| 28 | // result is not needed. |
Kadir Cetinkaya | b910a10 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 29 | // |
Sam McCall | f0874bf | 2018-09-13 11:47:48 +0000 | [diff] [blame] | 30 | // void deepThoughtAsync(std::function<void(int)> Callback) { |
| 31 | // runAsync([Callback]{ |
| 32 | // int A = ponder(6); |
| 33 | // if (isCancelled()) |
| 34 | // return; |
| 35 | // int B = ponder(9); |
| 36 | // if (isCancelled()) |
| 37 | // return; |
| 38 | // Callback(A * B); |
| 39 | // }); |
| 40 | // } |
Kadir Cetinkaya | b910a10 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 41 | // |
Sam McCall | f0874bf | 2018-09-13 11:47:48 +0000 | [diff] [blame] | 42 | // (A real example may invoke the callback with an error on cancellation, |
| 43 | // the CancelledError is provided for this purpose). |
| 44 | // |
| 45 | // Cancellation has some caveats: |
| 46 | // - the work will only stop when/if the library code next checks for it. |
| 47 | // Code outside clangd such as Sema will not do this. |
| 48 | // - it's inherently racy: client code must be prepared to accept results |
| 49 | // even after requesting cancellation. |
| 50 | // - it's Context-based, so async work must be dispatched to threads in |
| 51 | // ways that preserve the context. (Like runAsync() or TUScheduler). |
| 52 | // |
| 53 | // FIXME: We could add timestamps to isCancelled() and CancelledError. |
| 54 | // Measuring the start -> cancel -> acknowledge -> finish timeline would |
| 55 | // help find where libraries' cancellation should be improved. |
Kadir Cetinkaya | b910a10 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 56 | |
| 57 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CANCELLATION_H |
| 58 | #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CANCELLATION_H |
| 59 | |
| 60 | #include "Context.h" |
| 61 | #include "llvm/Support/Error.h" |
Sam McCall | f0874bf | 2018-09-13 11:47:48 +0000 | [diff] [blame] | 62 | #include <functional> |
Kadir Cetinkaya | b910a10 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 63 | #include <system_error> |
| 64 | |
| 65 | namespace clang { |
| 66 | namespace clangd { |
| 67 | |
Sam McCall | f0874bf | 2018-09-13 11:47:48 +0000 | [diff] [blame] | 68 | /// A canceller requests cancellation of a task, when called. |
| 69 | /// Calling it again has no effect. |
| 70 | using Canceler = std::function<void()>; |
Kadir Cetinkaya | b910a10 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 71 | |
Sam McCall | f0874bf | 2018-09-13 11:47:48 +0000 | [diff] [blame] | 72 | /// Defines a new task whose cancellation may be requested. |
| 73 | /// The returned Context defines the scope of the task. |
| 74 | /// When the context is active, isCancelled() is false until the Canceler is |
| 75 | /// invoked, and true afterwards. |
| 76 | std::pair<Context, Canceler> cancelableTask(); |
Kadir Cetinkaya | b910a10 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 77 | |
Sam McCall | f0874bf | 2018-09-13 11:47:48 +0000 | [diff] [blame] | 78 | /// True if the current context is within a cancelable task which was cancelled. |
| 79 | /// Always false if there is no active cancelable task. |
| 80 | /// This isn't free (context lookup) - don't call it in a tight loop. |
Sam McCall | 227e72b | 2018-11-22 10:22:16 +0000 | [diff] [blame] | 81 | bool isCancelled(const Context &Ctx = Context::current()); |
Kadir Cetinkaya | b910a10 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 82 | |
Sam McCall | f0874bf | 2018-09-13 11:47:48 +0000 | [diff] [blame] | 83 | /// Conventional error when no result is returned due to cancellation. |
Kadir Cetinkaya | b910a10 | 2018-08-24 13:09:41 +0000 | [diff] [blame] | 84 | class CancelledError : public llvm::ErrorInfo<CancelledError> { |
| 85 | public: |
| 86 | static char ID; |
| 87 | |
| 88 | void log(llvm::raw_ostream &OS) const override { |
| 89 | OS << "Task was cancelled."; |
| 90 | } |
| 91 | std::error_code convertToErrorCode() const override { |
| 92 | return std::make_error_code(std::errc::operation_canceled); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | } // namespace clangd |
| 97 | } // namespace clang |
| 98 | |
| 99 | #endif |