Chromium Code Reviews| Index: src/cancelable-task.h |
| diff --git a/src/cancelable-task.h b/src/cancelable-task.h |
| index bae5b580cd2330897f0e9d5aa71d415cfecccd1d..7eae8e30891b401578c34485dde4a55022ff39fb 100644 |
| --- a/src/cancelable-task.h |
| +++ b/src/cancelable-task.h |
| @@ -6,26 +6,98 @@ |
| #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 tasks. It is possible to register and remove tasks |
| +// from any fore- and background task/thread. |
| +class CancelableTaskManager { |
| + public: |
| + CancelableTaskManager(); |
| + |
| + // Registers a new cancelable {task}. Returns the unique {id} of the task that |
| + // can be used to try to abort running a task by calling {Abort}. |
|
Hannes Payer (out of office)
2015/11/13 20:03:22
drop the running, it's cleaner
Michael Lippautz
2015/11/13 22:51:03
Done.
|
| + uint32_t Register(Cancelable* task); |
| + |
| + // Try to abort running a task identified by {id}. The possible outcomes are: |
|
Hannes Payer (out of office)
2015/11/13 20:03:22
task _remove one whitespace_ identified
Michael Lippautz
2015/11/13 22:51:03
Done.
|
| + // (1) The task is already finished running and thus has been removed from |
| + // the manager. |
| + // (2) The task is currently running and cannot be canceled anymore. |
| + // (3) The task is not yet running (or finished) and is canceled and removed. |
|
Hannes Payer (out of office)
2015/11/13 20:03:22
"so it is"
Michael Lippautz
2015/11/13 22:51:03
Done.
|
| + // |
| + // Returns {false} for (1) and (2), and {true} for (3). |
| + bool TryAbort(uint32_t id); |
| + |
| + // Cancels all remaining registered tasks and waits 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! The reason is |
| + // that {Cancelable} is used in combination with {v8::Task} and handed to |
| + // a platform. This step transfers ownership to the platform, which destroys |
| + // the task after running it. Since the exact time is not known, we cannot |
| + // access the object after handing it to a platform. |
| + uint32_t id() { return id_; } |
| 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: |
|
Hannes Payer (out of office)
2015/11/13 20:03:22
Please describe the states in a comment.
Michael Lippautz
2015/11/13 22:51:03
Done.
|
| + enum Status { |
| + kWaiting, |
| + kCanceled, |
| + kRunning, |
| + }; |
| + |
| + // Use {CancelableTaskManager} to abort a task that has not yet been |
| + // executed. |
| + bool Cancel() { |
| + if (status_.TrySetValue(kWaiting, kCanceled)) { |
| + return true; |
| + } |
| + cancel_counter_.Increment(1); |
| + return false; |
| + } |
| + |
| + CancelableTaskManager* parent_; |
| + AtomicValue<Status> status_; |
| + uint32_t id_; |
| + AtomicNumber<intptr_t> cancel_counter_; |
|
Hannes Payer (out of office)
2015/11/13 20:03:22
Let's describe the purpose of this counter.
Michael Lippautz
2015/11/13 22:51:03
Done.
|
| + |
| + friend class CancelableTaskManager; |
| + |
| DISALLOW_COPY_AND_ASSIGN(Cancelable); |
| }; |
| @@ -33,18 +105,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 +127,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); |
| }; |