Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_CANCELABLE_TASK_H_ | 5 #ifndef V8_CANCELABLE_TASK_H_ |
| 6 #define V8_CANCELABLE_TASK_H_ | 6 #define V8_CANCELABLE_TASK_H_ |
| 7 | 7 |
| 8 #include "include/v8-platform.h" | 8 #include "include/v8-platform.h" |
| 9 #include "src/atomic-utils.h" | |
| 9 #include "src/base/macros.h" | 10 #include "src/base/macros.h" |
| 10 | 11 |
| 11 namespace v8 { | 12 namespace v8 { |
| 12 namespace internal { | 13 namespace internal { |
| 13 | 14 |
| 14 class Isolate; | 15 class Isolate; |
| 15 | 16 |
| 16 | 17 // {Cancelable} can be used to create tasks that can run on foreground and |
| 18 // background workers and can be canceled from any of those. | |
| 17 class Cancelable { | 19 class Cancelable { |
| 18 public: | 20 public: |
| 19 explicit Cancelable(Isolate* isolate); | 21 explicit Cancelable(Isolate* isolate); |
| 20 virtual ~Cancelable(); | 22 virtual ~Cancelable(); |
| 21 | 23 |
| 22 virtual void Cancel() { is_cancelled_ = true; } | 24 bool Cancel() { return status_.TrySetValue(kWaiting, kCanceled); } |
| 25 bool IsCanceled() { return status_.Value() == kCanceled; } | |
| 26 bool IsRunning() { return status_.Value() == kRunning; } | |
| 23 | 27 |
| 24 protected: | 28 protected: |
| 29 // Before actually running, the task needs to call {CanRun} to indicate that | |
| 30 // it cannot be canceled anymore. | |
| 31 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.
| |
| 32 | |
| 25 Isolate* isolate_; | 33 Isolate* isolate_; |
| 26 bool is_cancelled_; | |
| 27 | 34 |
| 28 private: | 35 private: |
| 36 enum Status { | |
| 37 kWaiting, | |
| 38 kCanceled, | |
| 39 kRunning, | |
| 40 kCanceledForShutdown, | |
| 41 }; | |
| 42 | |
| 43 bool CancelForShutdown() { | |
| 44 return status_.TrySetValue(kWaiting, kCanceledForShutdown); | |
| 45 } | |
| 46 | |
| 47 AtomicValue<Status> status_; | |
| 48 | |
| 49 friend class Isolate; | |
|
Hannes Payer (out of office)
2015/11/04 23:19:00
Why friends?
| |
| 50 | |
| 29 DISALLOW_COPY_AND_ASSIGN(Cancelable); | 51 DISALLOW_COPY_AND_ASSIGN(Cancelable); |
| 30 }; | 52 }; |
| 31 | 53 |
| 32 | 54 |
| 33 // Multiple inheritance can be used because Task is a pure interface. | 55 // Multiple inheritance can be used because Task is a pure interface. |
| 34 class CancelableTask : public Cancelable, public Task { | 56 class CancelableTask : public Cancelable, public Task { |
| 35 public: | 57 public: |
| 36 explicit CancelableTask(Isolate* isolate) : Cancelable(isolate) {} | 58 explicit CancelableTask(Isolate* isolate) : Cancelable(isolate) {} |
| 37 | 59 |
| 38 // Task overrides. | 60 // Task overrides. |
| 39 void Run() final { | 61 void Run() final { |
| 40 if (!is_cancelled_) { | 62 if (CanRun()) { |
| 41 RunInternal(); | 63 RunInternal(); |
| 42 } | 64 } |
| 43 } | 65 } |
| 44 | 66 |
| 45 virtual void RunInternal() = 0; | 67 virtual void RunInternal() = 0; |
| 46 | 68 |
| 47 private: | 69 private: |
| 48 DISALLOW_COPY_AND_ASSIGN(CancelableTask); | 70 DISALLOW_COPY_AND_ASSIGN(CancelableTask); |
| 49 }; | 71 }; |
| 50 | 72 |
| 51 | 73 |
| 52 // Multiple inheritance can be used because IdleTask is a pure interface. | 74 // Multiple inheritance can be used because IdleTask is a pure interface. |
| 53 class CancelableIdleTask : public Cancelable, public IdleTask { | 75 class CancelableIdleTask : public Cancelable, public IdleTask { |
| 54 public: | 76 public: |
| 55 explicit CancelableIdleTask(Isolate* isolate) : Cancelable(isolate) {} | 77 explicit CancelableIdleTask(Isolate* isolate) : Cancelable(isolate) {} |
| 56 | 78 |
| 57 // IdleTask overrides. | 79 // IdleTask overrides. |
| 58 void Run(double deadline_in_seconds) final { | 80 void Run(double deadline_in_seconds) final { |
| 59 if (!is_cancelled_) { | 81 if (CanRun()) { |
| 60 RunInternal(deadline_in_seconds); | 82 RunInternal(deadline_in_seconds); |
| 61 } | 83 } |
| 62 } | 84 } |
| 63 | 85 |
| 64 virtual void RunInternal(double deadline_in_seconds) = 0; | 86 virtual void RunInternal(double deadline_in_seconds) = 0; |
| 65 | 87 |
| 66 private: | 88 private: |
| 67 DISALLOW_COPY_AND_ASSIGN(CancelableIdleTask); | 89 DISALLOW_COPY_AND_ASSIGN(CancelableIdleTask); |
| 68 }; | 90 }; |
| 69 | 91 |
| 70 | 92 |
| 71 } // namespace internal | 93 } // namespace internal |
| 72 } // namespace v8 | 94 } // namespace v8 |
| 73 | 95 |
| 74 #endif // V8_CANCELABLE_TASK_H_ | 96 #endif // V8_CANCELABLE_TASK_H_ |
| OLD | NEW |