Skip to content

Commit b7f7d67

Browse files
Matt LoringMylesBorins
Matt Loring
authored andcommitted
deps: backport bca8409 from upstream V8
Original commit message: Make CancelableTask ids unique They were only limited to 32 bit when using the internal Hashmap. Since this has changed alreay some time ago, we can switch to 64 bit ids and check that we never overflow. Bug: Change-Id: Ia6c6d02d6b5e555c6941185a79427dc4aa2a1d62 Reviewed-on: https://chromium-review.googlesource.com/598229 Commit-Queue: Michael Lippautz <[email protected]> Reviewed-by: Ulan Degenbaev <[email protected]> Cr-Commit-Position: refs/heads/master@{#47085} PR-URL: #14001 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent a67e7f9 commit b7f7d67

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

deps/v8/src/cancelable-task.cc

+6-7
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,17 @@ Cancelable::~Cancelable() {
2929
CancelableTaskManager::CancelableTaskManager()
3030
: task_id_counter_(0), canceled_(false) {}
3131

32-
uint32_t CancelableTaskManager::Register(Cancelable* task) {
32+
CancelableTaskManager::Id CancelableTaskManager::Register(Cancelable* task) {
3333
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);
3737
CHECK(!canceled_);
3838
cancelable_tasks_[id] = task;
3939
return id;
4040
}
4141

42-
43-
void CancelableTaskManager::RemoveFinishedTask(uint32_t id) {
42+
void CancelableTaskManager::RemoveFinishedTask(CancelableTaskManager::Id id) {
4443
base::LockGuard<base::Mutex> guard(&mutex_);
4544
size_t removed = cancelable_tasks_.erase(id);
4645
USE(removed);
@@ -49,7 +48,7 @@ void CancelableTaskManager::RemoveFinishedTask(uint32_t id) {
4948
}
5049

5150
CancelableTaskManager::TryAbortResult CancelableTaskManager::TryAbort(
52-
uint32_t id) {
51+
CancelableTaskManager::Id id) {
5352
base::LockGuard<base::Mutex> guard(&mutex_);
5453
auto entry = cancelable_tasks_.find(id);
5554
if (entry != cancelable_tasks_.end()) {

deps/v8/src/cancelable-task.h

+10-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#ifndef V8_CANCELABLE_TASK_H_
66
#define V8_CANCELABLE_TASK_H_
77

8-
#include <map>
8+
#include <unordered_map>
99

1010
#include "include/v8-platform.h"
1111
#include "src/base/atomic-utils.h"
@@ -24,12 +24,14 @@ class Isolate;
2424
// from any fore- and background task/thread.
2525
class V8_EXPORT_PRIVATE CancelableTaskManager {
2626
public:
27+
using Id = uint64_t;
28+
2729
CancelableTaskManager();
2830

2931
// Registers a new cancelable {task}. Returns the unique {id} of the task that
3032
// can be used to try to abort a task by calling {Abort}.
3133
// Must not be called after CancelAndWait.
32-
uint32_t Register(Cancelable* task);
34+
Id Register(Cancelable* task);
3335

3436
// Try to abort running a task identified by {id}. The possible outcomes are:
3537
// (1) The task is already finished running or was canceled before and
@@ -39,7 +41,7 @@ class V8_EXPORT_PRIVATE CancelableTaskManager {
3941
// removed.
4042
//
4143
enum TryAbortResult { kTaskRemoved, kTaskRunning, kTaskAborted };
42-
TryAbortResult TryAbort(uint32_t id);
44+
TryAbortResult TryAbort(Id id);
4345

4446
// Cancels all remaining registered tasks and waits for tasks that are
4547
// already running. This disallows subsequent Register calls.
@@ -59,13 +61,13 @@ class V8_EXPORT_PRIVATE CancelableTaskManager {
5961
private:
6062
// Only called by {Cancelable} destructor. The task is done with executing,
6163
// but needs to be removed.
62-
void RemoveFinishedTask(uint32_t id);
64+
void RemoveFinishedTask(Id id);
6365

6466
// To mitigate the ABA problem, the api refers to tasks through an id.
65-
uint32_t task_id_counter_;
67+
Id task_id_counter_;
6668

6769
// 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_;
6971

7072
// Mutex and condition variable enabling concurrent register and removing, as
7173
// well as waiting for background tasks on {CancelAndWait}.
@@ -89,7 +91,7 @@ class V8_EXPORT_PRIVATE Cancelable {
8991
// a platform. This step transfers ownership to the platform, which destroys
9092
// the task after running it. Since the exact time is not known, we cannot
9193
// access the object after handing it to a platform.
92-
uint32_t id() { return id_; }
94+
CancelableTaskManager::Id id() { return id_; }
9395

9496
protected:
9597
bool TryRun() { return status_.TrySetValue(kWaiting, kRunning); }
@@ -120,7 +122,7 @@ class V8_EXPORT_PRIVATE Cancelable {
120122

121123
CancelableTaskManager* parent_;
122124
base::AtomicValue<Status> status_;
123-
uint32_t id_;
125+
CancelableTaskManager::Id id_;
124126

125127
// The counter is incremented for failing tries to cancel a task. This can be
126128
// used by the task itself as an indication how often external entities tried

deps/v8/src/heap/item-parallel-job.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ class ItemParallelJob {
133133
const size_t num_tasks = tasks_.size();
134134
const size_t num_items = items_.size();
135135
const size_t items_per_task = (num_items + num_tasks - 1) / num_tasks;
136-
uint32_t* task_ids = new uint32_t[num_tasks];
136+
CancelableTaskManager::Id* task_ids =
137+
new CancelableTaskManager::Id[num_tasks];
137138
size_t start_index = 0;
138139
Task* main_task = nullptr;
139140
Task* task = nullptr;

deps/v8/src/heap/mark-compact.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ class MarkCompactCollector final : public MarkCompactCollectorBase {
471471

472472
Heap* const heap_;
473473
int num_tasks_;
474-
uint32_t task_ids_[kMaxSweeperTasks];
474+
CancelableTaskManager::Id task_ids_[kMaxSweeperTasks];
475475
base::Semaphore pending_sweeper_tasks_semaphore_;
476476
base::Mutex mutex_;
477477
SweptList swept_list_[kAllocationSpaces];

deps/v8/test/unittests/cancelable-tasks-unittest.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ TEST(CancelableTask, RemoveBeforeCancelAndWait) {
180180
ResultType result1 = 0;
181181
TestTask* task1 = new TestTask(&manager, &result1, TestTask::kCheckNotRun);
182182
ThreadedRunner runner1(task1);
183-
uint32_t id = task1->id();
183+
CancelableTaskManager::Id id = task1->id();
184184
EXPECT_EQ(id, 1u);
185185
EXPECT_TRUE(manager.TryAbort(id));
186186
runner1.Start();
@@ -195,7 +195,7 @@ TEST(CancelableTask, RemoveAfterCancelAndWait) {
195195
ResultType result1 = 0;
196196
TestTask* task1 = new TestTask(&manager, &result1);
197197
ThreadedRunner runner1(task1);
198-
uint32_t id = task1->id();
198+
CancelableTaskManager::Id id = task1->id();
199199
EXPECT_EQ(id, 1u);
200200
runner1.Start();
201201
runner1.Join();

0 commit comments

Comments
 (0)