Chromium Code Reviews| Index: src/cancelable-task.h |
| diff --git a/src/cancelable-task.h b/src/cancelable-task.h |
| index bae5b580cd2330897f0e9d5aa71d415cfecccd1d..b640e8f8f547f7679ab033312c2c8db2ea11c922 100644 |
| --- a/src/cancelable-task.h |
| +++ b/src/cancelable-task.h |
| @@ -6,26 +6,83 @@ |
| #define V8_CANCELABLE_TASK_H_ |
| #include "include/v8-platform.h" |
| +#include "src/atomic-utils.h" |
| #include "src/base/macros.h" |
| +#include "src/base/platform/condition-variable.h" |
| +#include "src/hashmap.h" |
| namespace v8 { |
| namespace internal { |
| +class Cancelable; |
| class Isolate; |
| +// Keeps track of cancelable fore- and background tasks. |
| +class CancelableTaskManager { |
| + public: |
| + CancelableTaskManager(); |
| + |
| + // Registers a new {Cancelable} task. |
|
Hannes Payer (out of office)
2015/11/12 22:01:50
Specify what the return value is.
Michael Lippautz
2015/11/12 22:15:21
Done.
|
| + uint32_t Register(Cancelable* task); |
| + |
| + // Remove a given task by id if it is still managed. Only returns true if the |
| + // task has not been started yet and could be canceled. |
|
Hannes Payer (out of office)
2015/11/12 22:01:50
It is not clear to me what the semantics of this A
Michael Lippautz
2015/11/12 22:15:21
Done. Hope the more elaborate comment makes it cle
|
| + bool Remove(uint32_t id); |
| + |
| + // Cancel all registered tasks and wait for tasks that are already running. |
| + void CancelAndWait(); |
| + |
| + private: |
| + // To mitigate the ABA problem, the api refers to tasks through an id. |
| + uint32_t task_id_counter_; |
| + |
| + // A set of cancelable tasks that are currently registered. |
| + HashMap cancelable_tasks_; |
| + |
| + // Mutex and condition variable enabling concurrent register and removing, as |
| + // well as waiting for background tasks on {CancelAndWait}. |
| + base::ConditionVariable cancelable_tasks_barrier_; |
| + base::Mutex mutex_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(CancelableTaskManager); |
| +}; |
| + |
| + |
| class Cancelable { |
| public: |
| - explicit Cancelable(Isolate* isolate); |
| + explicit Cancelable(CancelableTaskManager* parent); |
| virtual ~Cancelable(); |
| - virtual void Cancel() { is_cancelled_ = true; } |
| + // Never invoke after handing over the task to the platform! |
|
Hannes Payer (out of office)
2015/11/12 22:01:50
Please explain why.
Michael Lippautz
2015/11/12 22:15:21
Done.
|
| + uint32_t id() { return id_; } |
| + |
| + // Always invoke indirectly through the connected {CancelableTaskManager}. |
|
Hannes Payer (out of office)
2015/11/12 22:01:50
Should this really be public then? Or should it be
Michael Lippautz
2015/11/12 22:15:21
Done.
|
| + bool Cancel() { |
| + if (status_.TrySetValue(kWaiting, kCanceled)) { |
| + return true; |
| + } |
| + cancel_counter_.Increment(1); |
| + return false; |
| + } |
| protected: |
| - Isolate* isolate_; |
| - bool is_cancelled_; |
| + bool TryRun() { return status_.TrySetValue(kWaiting, kRunning); } |
| + bool IsRunning() { return status_.Value() == kRunning; } |
| + intptr_t CancelAttempts() { return cancel_counter_.Value(); } |
| private: |
| + enum Status { |
|
Hannes Payer (out of office)
2015/11/12 22:01:50
Please explain the states.
|
| + kWaiting, |
| + kCanceled, |
| + kRunning, |
| + }; |
| + |
| + CancelableTaskManager* parent_; |
| + AtomicValue<Status> status_; |
| + uint32_t id_; |
| + AtomicNumber<intptr_t> cancel_counter_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(Cancelable); |
| }; |
| @@ -33,18 +90,21 @@ class Cancelable { |
| // Multiple inheritance can be used because Task is a pure interface. |
| class CancelableTask : public Cancelable, public Task { |
| public: |
| - explicit CancelableTask(Isolate* isolate) : Cancelable(isolate) {} |
| + explicit CancelableTask(Isolate* isolate); |
| // Task overrides. |
| void Run() final { |
| - if (!is_cancelled_) { |
| + if (TryRun()) { |
| RunInternal(); |
| } |
| } |
| virtual void RunInternal() = 0; |
| + Isolate* isolate() { return isolate_; } |
| + |
| private: |
| + Isolate* isolate_; |
| DISALLOW_COPY_AND_ASSIGN(CancelableTask); |
| }; |
| @@ -52,18 +112,21 @@ class CancelableTask : public Cancelable, public Task { |
| // Multiple inheritance can be used because IdleTask is a pure interface. |
| class CancelableIdleTask : public Cancelable, public IdleTask { |
| public: |
| - explicit CancelableIdleTask(Isolate* isolate) : Cancelable(isolate) {} |
| + explicit CancelableIdleTask(Isolate* isolate); |
| // IdleTask overrides. |
| void Run(double deadline_in_seconds) final { |
| - if (!is_cancelled_) { |
| + if (TryRun()) { |
| RunInternal(deadline_in_seconds); |
| } |
| } |
| virtual void RunInternal(double deadline_in_seconds) = 0; |
| + Isolate* isolate() { return isolate_; } |
| + |
| private: |
| + Isolate* isolate_; |
| DISALLOW_COPY_AND_ASSIGN(CancelableIdleTask); |
| }; |