5 #ifndef V8_CANCELABLE_TASK_H_ 6 #define V8_CANCELABLE_TASK_H_ 9 #include <unordered_map> 11 #include "include/v8-platform.h" 12 #include "src/base/macros.h" 13 #include "src/base/platform/condition-variable.h" 14 #include "src/globals.h" 28 enum class TryAbortResult { kTaskRemoved, kTaskRunning, kTaskAborted };
44 TryAbortResult TryAbort(Id
id);
55 TryAbortResult TryAbortAll();
62 bool canceled()
const {
return canceled_; }
67 void RemoveFinishedTask(Id
id);
73 std::unordered_map<Id, Cancelable*> cancelable_tasks_;
90 : parent_(parent), id_(parent->Register(
this)) {}
99 CancelableTaskManager::Id id() {
return id_; }
107 enum Status { kWaiting, kCanceled, kRunning };
109 bool TryRun(Status* previous =
nullptr) {
110 return CompareExchangeStatus(kWaiting, kRunning, previous);
118 bool Cancel() {
return CompareExchangeStatus(kWaiting, kCanceled); }
120 bool CompareExchangeStatus(Status expected, Status desired,
121 Status* previous =
nullptr) {
123 bool success = status_.compare_exchange_strong(expected, desired,
124 std::memory_order_acq_rel,
125 std::memory_order_acquire);
126 if (previous) *previous = expected;
131 std::atomic<Status> status_{kWaiting};
132 const CancelableTaskManager::Id id_;
139 NON_EXPORTED_BASE(public Task) {
151 virtual void RunInternal() = 0;
164 void Run(
double deadline_in_seconds)
final {
166 RunInternal(deadline_in_seconds);
170 virtual void RunInternal(
double deadline_in_seconds) = 0;
179 #endif // V8_CANCELABLE_TASK_H_