| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 2601a51 commit 19a5021
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,18 +29,17 @@ Cancelable::~Cancelable() { | |||
| 29 | 29 | CancelableTaskManager::CancelableTaskManager() | |
| 30 | 30 | : task_id_counter_(0), canceled_(false) {} | |
| 31 | 31 | ||
| 32 | - uint32_t CancelableTaskManager::Register(Cancelable* task) { | ||
| 32 | + CancelableTaskManager::Id CancelableTaskManager::Register(Cancelable* task) { | ||
| 33 | 33 | base::LockGuard<base::Mutex> guard(&mutex_); | |
| 34 | - uint32_t id = ++task_id_counter_; | ||
| 35 | - // The loop below is just used when task_id_counter_ overflows. | ||
| 36 | - while (cancelable_tasks_.count(id) > 0) ++id; | ||
| 34 | + CancelableTaskManager::Id id = ++task_id_counter_; | ||
| 35 | + // Id overflows are not supported. | ||
| 36 | + CHECK_NE(0, id); | ||
| 37 | 37 | CHECK(!canceled_); | |
| 38 | 38 | cancelable_tasks_[id] = task; | |
| 39 | 39 | return id; | |
| 40 | 40 | } | |
| 41 | 41 | ||
| 42 | - | ||
| 43 | - void CancelableTaskManager::RemoveFinishedTask(uint32_t id) { | ||
| 42 | + void CancelableTaskManager::RemoveFinishedTask(CancelableTaskManager::Id id) { | ||
| 44 | 43 | base::LockGuard<base::Mutex> guard(&mutex_); | |
| 45 | 44 | size_t removed = cancelable_tasks_.erase(id); | |
| 46 | 45 | USE(removed); | |
@@ -49,7 +48,7 @@ void CancelableTaskManager::RemoveFinishedTask(uint32_t id) { | |||
| 49 | 48 | } | |
| 50 | 49 | ||
| 51 | 50 | CancelableTaskManager::TryAbortResult CancelableTaskManager::TryAbort( | |
| 52 | - uint32_t id) { | ||
| 51 | + CancelableTaskManager::Id id) { | ||
| 53 | 52 | base::LockGuard<base::Mutex> guard(&mutex_); | |
| 54 | 53 | auto entry = cancelable_tasks_.find(id); | |
| 55 | 54 | if (entry != cancelable_tasks_.end()) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,7 +5,7 @@ | |||
| 5 | 5 | #ifndef V8_CANCELABLE_TASK_H_ | |
| 6 | 6 | #define V8_CANCELABLE_TASK_H_ | |
| 7 | 7 | ||
| 8 | - #include <map> | ||
| 8 | + #include <unordered_map> | ||
| 9 | 9 | ||
| 10 | 10 | #include "include/v8-platform.h" | |
| 11 | 11 | #include "src/base/atomic-utils.h" | |
@@ -24,12 +24,14 @@ class Isolate; | |||
| 24 | 24 | // from any fore- and background task/thread. | |
| 25 | 25 | class V8_EXPORT_PRIVATE CancelableTaskManager { | |
| 26 | 26 | public: | |
| 27 | + using Id = uint64_t; | ||
| 28 | + | ||
| 27 | 29 | CancelableTaskManager(); | |
| 28 | 30 | ||
| 29 | 31 | // Registers a new cancelable {task}. Returns the unique {id} of the task that | |
| 30 | 32 | // can be used to try to abort a task by calling {Abort}. | |
| 31 | 33 | // Must not be called after CancelAndWait. | |
| 32 | - uint32_t Register(Cancelable* task); | ||
| 34 | + Id Register(Cancelable* task); | ||
| 33 | 35 | ||
| 34 | 36 | // Try to abort running a task identified by {id}. The possible outcomes are: | |
| 35 | 37 | // (1) The task is already finished running or was canceled before and | |
@@ -39,7 +41,7 @@ class V8_EXPORT_PRIVATE CancelableTaskManager { | |||
| 39 | 41 | // removed. | |
| 40 | 42 | // | |
| 41 | 43 | enum TryAbortResult { kTaskRemoved, kTaskRunning, kTaskAborted }; | |
| 42 | - TryAbortResult TryAbort(uint32_t id); | ||
| 44 | + TryAbortResult TryAbort(Id id); | ||
| 43 | 45 | ||
| 44 | 46 | // Cancels all remaining registered tasks and waits for tasks that are | |
| 45 | 47 | // already running. This disallows subsequent Register calls. | |
@@ -59,13 +61,13 @@ class V8_EXPORT_PRIVATE CancelableTaskManager { | |||
| 59 | 61 | private: | |
| 60 | 62 | // Only called by {Cancelable} destructor. The task is done with executing, | |
| 61 | 63 | // but needs to be removed. | |
| 62 | - void RemoveFinishedTask(uint32_t id); | ||
| 64 | + void RemoveFinishedTask(Id id); | ||
| 63 | 65 | ||
| 64 | 66 | // To mitigate the ABA problem, the api refers to tasks through an id. | |
| 65 | - uint32_t task_id_counter_; | ||
| 67 | + Id task_id_counter_; | ||
| 66 | 68 | ||
| 67 | 69 | // A set of cancelable tasks that are currently registered. | |
| 68 | - std::map<uint32_t, Cancelable*> cancelable_tasks_; | ||
| 70 | + std::unordered_map<Id, Cancelable*> cancelable_tasks_; | ||
| 69 | 71 | ||
| 70 | 72 | // Mutex and condition variable enabling concurrent register and removing, as | |
| 71 | 73 | // well as waiting for background tasks on {CancelAndWait}. | |
@@ -89,7 +91,7 @@ class V8_EXPORT_PRIVATE Cancelable { | |||
| 89 | 91 | // a platform. This step transfers ownership to the platform, which destroys | |
| 90 | 92 | // the task after running it. Since the exact time is not known, we cannot | |
| 91 | 93 | // access the object after handing it to a platform. | |
| 92 | - uint32_t id() { return id_; } | ||
| 94 | + CancelableTaskManager::Id id() { return id_; } | ||
| 93 | 95 | ||
| 94 | 96 | protected: | |
| 95 | 97 | bool TryRun() { return status_.TrySetValue(kWaiting, kRunning); } | |
@@ -120,7 +122,7 @@ class V8_EXPORT_PRIVATE Cancelable { | |||
| 120 | 122 | ||
| 121 | 123 | CancelableTaskManager* parent_; | |
| 122 | 124 | base::AtomicValue<Status> status_; | |
| 123 | - uint32_t id_; | ||
| 125 | + CancelableTaskManager::Id id_; | ||
| 124 | 126 | ||
| 125 | 127 | // The counter is incremented for failing tries to cancel a task. This can be | |
| 126 | 128 | // used by the task itself as an indication how often external entities tried | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -136,7 +136,8 @@ class ItemParallelJob { | |||
| 136 | 136 | const size_t num_tasks = tasks_.size(); | |
| 137 | 137 | const size_t num_items = items_.size(); | |
| 138 | 138 | const size_t items_per_task = (num_items + num_tasks - 1) / num_tasks; | |
| 139 | - uint32_t* task_ids = new uint32_t[num_tasks]; | ||
| 139 | + CancelableTaskManager::Id* task_ids = | ||
| 140 | + new CancelableTaskManager::Id[num_tasks]; | ||
| 140 | 141 | size_t start_index = 0; | |
| 141 | 142 | Task* main_task = nullptr; | |
| 142 | 143 | Task* task = nullptr; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -553,7 +553,7 @@ class MarkCompactCollector final : public MarkCompactCollectorBase { | |||
| 553 | 553 | ||
| 554 | 554 | Heap* const heap_; | |
| 555 | 555 | int num_tasks_; | |
| 556 | - uint32_t task_ids_[kMaxSweeperTasks]; | ||
| 556 | + CancelableTaskManager::Id task_ids_[kMaxSweeperTasks]; | ||
| 557 | 557 | base::Semaphore pending_sweeper_tasks_semaphore_; | |
| 558 | 558 | base::Mutex mutex_; | |
| 559 | 559 | SweptList swept_list_[kAllocationSpaces]; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -180,7 +180,7 @@ TEST(CancelableTask, RemoveBeforeCancelAndWait) { | |||
| 180 | 180 | ResultType result1 = 0; | |
| 181 | 181 | TestTask* task1 = new TestTask(&manager, &result1, TestTask::kCheckNotRun); | |
| 182 | 182 | ThreadedRunner runner1(task1); | |
| 183 | - uint32_t id = task1->id(); | ||
| 183 | + CancelableTaskManager::Id id = task1->id(); | ||
| 184 | 184 | EXPECT_EQ(id, 1u); | |
| 185 | 185 | EXPECT_TRUE(manager.TryAbort(id)); | |
| 186 | 186 | runner1.Start(); | |
@@ -195,7 +195,7 @@ TEST(CancelableTask, RemoveAfterCancelAndWait) { | |||
| 195 | 195 | ResultType result1 = 0; | |
| 196 | 196 | TestTask* task1 = new TestTask(&manager, &result1); | |
| 197 | 197 | ThreadedRunner runner1(task1); | |
| 198 | - uint32_t id = task1->id(); | ||
| 198 | + CancelableTaskManager::Id id = task1->id(); | ||
| 199 | 199 | EXPECT_EQ(id, 1u); | |
| 200 | 200 | runner1.Start(); | |
| 201 | 201 | runner1.Join(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments