Chromium Code Reviews| Index: src/cancelable-task.h |
| diff --git a/src/cancelable-task.h b/src/cancelable-task.h |
| index bae5b580cd2330897f0e9d5aa71d415cfecccd1d..53c44a091e9bd3dc10f1fb7d51537220f9c08a6a 100644 |
| --- a/src/cancelable-task.h |
| +++ b/src/cancelable-task.h |
| @@ -6,6 +6,7 @@ |
| #define V8_CANCELABLE_TASK_H_ |
| #include "include/v8-platform.h" |
| +#include "src/atomic-utils.h" |
| #include "src/base/macros.h" |
| namespace v8 { |
| @@ -13,19 +14,40 @@ namespace internal { |
| class Isolate; |
| - |
| +// {Cancelable} can be used to create tasks that can run on foreground and |
| +// background workers and can be canceled from any of those. |
| class Cancelable { |
| public: |
| explicit Cancelable(Isolate* isolate); |
| virtual ~Cancelable(); |
| - virtual void Cancel() { is_cancelled_ = true; } |
| + bool Cancel() { return status_.TrySetValue(kWaiting, kCanceled); } |
| + bool IsCanceled() { return status_.Value() == kCanceled; } |
| + bool IsRunning() { return status_.Value() == kRunning; } |
| protected: |
| + // Before actually running, the task needs to call {CanRun} to indicate that |
| + // it cannot be canceled anymore. |
| + bool CanRun() { return status_.TrySetValue(kWaiting, kRunning); } |
|
Hannes Payer (out of office)
2015/11/04 23:19:00
I think TryRun matches better the semantics.
|
| + |
| Isolate* isolate_; |
| - bool is_cancelled_; |
| private: |
| + enum Status { |
| + kWaiting, |
| + kCanceled, |
| + kRunning, |
| + kCanceledForShutdown, |
| + }; |
| + |
| + bool CancelForShutdown() { |
| + return status_.TrySetValue(kWaiting, kCanceledForShutdown); |
| + } |
| + |
| + AtomicValue<Status> status_; |
| + |
| + friend class Isolate; |
|
Hannes Payer (out of office)
2015/11/04 23:19:00
Why friends?
|
| + |
| DISALLOW_COPY_AND_ASSIGN(Cancelable); |
| }; |
| @@ -37,7 +59,7 @@ class CancelableTask : public Cancelable, public Task { |
| // Task overrides. |
| void Run() final { |
| - if (!is_cancelled_) { |
| + if (CanRun()) { |
| RunInternal(); |
| } |
| } |
| @@ -56,7 +78,7 @@ class CancelableIdleTask : public Cancelable, public IdleTask { |
| // IdleTask overrides. |
| void Run(double deadline_in_seconds) final { |
| - if (!is_cancelled_) { |
| + if (CanRun()) { |
| RunInternal(deadline_in_seconds); |
| } |
| } |