Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Unified Diff: src/cancelable-task.h

Issue 1409993012: Add {CancelableTaskManager} to handle {Cancelable} concurrent tasks. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fixed concurrent version including unittests Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/cancelable-task.cc » ('j') | src/cancelable-task.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/cancelable-task.h
diff --git a/src/cancelable-task.h b/src/cancelable-task.h
index bae5b580cd2330897f0e9d5aa71d415cfecccd1d..4a5c1f4c80910b0ef35e7a8f7a0bf76f500ae383 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.
+ uint32_t Register(Cancelable* task);
+
+ // Remove a given task. Returns whether the task was present at the time of
+ // calling {Remove}.
+ 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!
+ uint32_t id() { return id_; }
+
+ // Always invoke indirectly through the connected {CancelableTaskManager}.
+ 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 {
+ 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);
};
« no previous file with comments | « no previous file | src/cancelable-task.cc » ('j') | src/cancelable-task.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698