2015-11-14 01:20:07 +00:00
|
|
|
// Copyright 2015 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "src/base/atomicops.h"
|
|
|
|
#include "src/base/platform/platform.h"
|
|
|
|
#include "src/cancelable-task.h"
|
2018-11-09 15:51:43 +00:00
|
|
|
#include "testing/gmock/include/gmock/gmock.h"
|
2015-11-14 01:20:07 +00:00
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
using ResultType = std::atomic<CancelableTaskManager::Id>;
|
|
|
|
|
2018-11-09 15:51:43 +00:00
|
|
|
class CancelableTaskManagerTest;
|
|
|
|
|
2015-11-14 01:20:07 +00:00
|
|
|
class TestTask : public Task, public Cancelable {
|
|
|
|
public:
|
2018-11-09 15:51:43 +00:00
|
|
|
enum Mode { kDoNothing, kWaitTillCancelTriggered, kCheckNotRun };
|
2015-11-14 01:20:07 +00:00
|
|
|
|
2018-11-09 15:51:43 +00:00
|
|
|
TestTask(CancelableTaskManagerTest* test, ResultType* result, Mode mode);
|
2015-11-14 01:20:07 +00:00
|
|
|
|
2018-11-09 15:51:43 +00:00
|
|
|
// Task override.
|
|
|
|
void Run() final;
|
2015-11-14 01:20:07 +00:00
|
|
|
|
|
|
|
private:
|
2018-11-08 16:44:32 +00:00
|
|
|
ResultType* const result_;
|
|
|
|
const Mode mode_;
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelableTaskManagerTest* const test_;
|
2015-11-14 01:20:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SequentialRunner {
|
|
|
|
public:
|
2018-11-08 16:44:32 +00:00
|
|
|
explicit SequentialRunner(std::unique_ptr<TestTask> task)
|
|
|
|
: task_(std::move(task)), task_id_(task_->id()) {}
|
2015-11-14 01:20:07 +00:00
|
|
|
|
|
|
|
void Run() {
|
|
|
|
task_->Run();
|
2018-11-08 16:44:32 +00:00
|
|
|
task_.reset();
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
CancelableTaskManager::Id task_id() const { return task_id_; }
|
|
|
|
|
2015-11-14 01:20:07 +00:00
|
|
|
private:
|
2018-11-08 16:44:32 +00:00
|
|
|
std::unique_ptr<TestTask> task_;
|
|
|
|
const CancelableTaskManager::Id task_id_;
|
2015-11-14 01:20:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadedRunner final : public base::Thread {
|
|
|
|
public:
|
2018-11-08 16:44:32 +00:00
|
|
|
explicit ThreadedRunner(std::unique_ptr<TestTask> task)
|
|
|
|
: Thread(Options("runner thread")),
|
|
|
|
task_(std::move(task)),
|
|
|
|
task_id_(task_->id()) {}
|
2015-11-14 01:20:07 +00:00
|
|
|
|
2018-09-14 15:34:02 +00:00
|
|
|
void Run() override {
|
2015-11-14 01:20:07 +00:00
|
|
|
task_->Run();
|
2018-11-08 16:44:32 +00:00
|
|
|
task_.reset();
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
CancelableTaskManager::Id task_id() const { return task_id_; }
|
|
|
|
|
2015-11-14 01:20:07 +00:00
|
|
|
private:
|
2018-11-08 16:44:32 +00:00
|
|
|
std::unique_ptr<TestTask> task_;
|
|
|
|
const CancelableTaskManager::Id task_id_;
|
2015-11-14 01:20:07 +00:00
|
|
|
};
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
class CancelableTaskManagerTest : public ::testing::Test {
|
|
|
|
public:
|
|
|
|
CancelableTaskManager* manager() { return &manager_; }
|
2015-11-14 01:20:07 +00:00
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
std::unique_ptr<TestTask> NewTask(
|
|
|
|
ResultType* result, TestTask::Mode mode = TestTask::kDoNothing) {
|
2018-11-09 15:51:43 +00:00
|
|
|
return base::make_unique<TestTask>(this, result, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CancelAndWait() {
|
|
|
|
cancel_triggered_.store(true);
|
|
|
|
manager_.CancelAndWait();
|
2018-11-08 16:44:32 +00:00
|
|
|
}
|
2015-11-14 01:20:07 +00:00
|
|
|
|
2018-11-09 15:51:43 +00:00
|
|
|
TryAbortResult TryAbortAll() {
|
|
|
|
cancel_triggered_.store(true);
|
|
|
|
return manager_.TryAbortAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cancel_triggered() const { return cancel_triggered_.load(); }
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
private:
|
|
|
|
CancelableTaskManager manager_;
|
2018-11-09 15:51:43 +00:00
|
|
|
std::atomic<bool> cancel_triggered_{false};
|
2018-11-08 16:44:32 +00:00
|
|
|
};
|
2015-11-14 01:20:07 +00:00
|
|
|
|
2018-11-09 15:51:43 +00:00
|
|
|
TestTask::TestTask(CancelableTaskManagerTest* test, ResultType* result,
|
|
|
|
Mode mode)
|
|
|
|
: Cancelable(test->manager()), result_(result), mode_(mode), test_(test) {}
|
|
|
|
|
|
|
|
void TestTask::Run() {
|
|
|
|
if (!TryRun()) return;
|
|
|
|
|
|
|
|
result_->store(id());
|
|
|
|
|
|
|
|
switch (mode_) {
|
|
|
|
case kWaitTillCancelTriggered:
|
|
|
|
// Simple busy wait until the main thread tried to cancel.
|
|
|
|
while (!test_->cancel_triggered()) {
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kCheckNotRun:
|
|
|
|
// Check that we never execute {RunInternal}.
|
|
|
|
EXPECT_TRUE(false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 01:20:07 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, EmptyCancelableTaskManager) {
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelAndWait();
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, SequentialCancelAndWait) {
|
|
|
|
ResultType result1{0};
|
|
|
|
SequentialRunner runner1(NewTask(&result1, TestTask::kCheckNotRun));
|
|
|
|
EXPECT_EQ(0u, result1);
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelAndWait();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(0u, result1);
|
|
|
|
runner1.Run();
|
|
|
|
EXPECT_EQ(0u, result1);
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, SequentialMultipleTasks) {
|
|
|
|
ResultType result1{0};
|
|
|
|
ResultType result2{0};
|
|
|
|
SequentialRunner runner1(NewTask(&result1));
|
|
|
|
SequentialRunner runner2(NewTask(&result2));
|
|
|
|
EXPECT_EQ(1u, runner1.task_id());
|
|
|
|
EXPECT_EQ(2u, runner2.task_id());
|
|
|
|
|
|
|
|
EXPECT_EQ(0u, result1);
|
|
|
|
runner1.Run();
|
|
|
|
EXPECT_EQ(1u, result1);
|
|
|
|
|
|
|
|
EXPECT_EQ(0u, result2);
|
|
|
|
runner2.Run();
|
|
|
|
EXPECT_EQ(2u, result2);
|
|
|
|
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelAndWait();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(TryAbortResult::kTaskRemoved, manager()->TryAbort(1));
|
|
|
|
EXPECT_EQ(TryAbortResult::kTaskRemoved, manager()->TryAbort(2));
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, ThreadedMultipleTasksStarted) {
|
|
|
|
ResultType result1{0};
|
|
|
|
ResultType result2{0};
|
2018-11-09 15:51:43 +00:00
|
|
|
ThreadedRunner runner1(NewTask(&result1, TestTask::kWaitTillCancelTriggered));
|
|
|
|
ThreadedRunner runner2(NewTask(&result2, TestTask::kWaitTillCancelTriggered));
|
2015-11-14 01:20:07 +00:00
|
|
|
runner1.Start();
|
|
|
|
runner2.Start();
|
|
|
|
// Busy wait on result to make sure both tasks are done.
|
2018-11-08 16:44:32 +00:00
|
|
|
while (result1.load() == 0 || result2.load() == 0) {
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelAndWait();
|
2015-11-14 01:20:07 +00:00
|
|
|
runner1.Join();
|
|
|
|
runner2.Join();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(1u, result1);
|
|
|
|
EXPECT_EQ(2u, result2);
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, ThreadedMultipleTasksNotRun) {
|
|
|
|
ResultType result1{0};
|
|
|
|
ResultType result2{0};
|
|
|
|
ThreadedRunner runner1(NewTask(&result1, TestTask::kCheckNotRun));
|
|
|
|
ThreadedRunner runner2(NewTask(&result2, TestTask::kCheckNotRun));
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelAndWait();
|
2015-11-14 01:20:07 +00:00
|
|
|
// Tasks are canceled, hence the runner will bail out and not update result.
|
|
|
|
runner1.Start();
|
|
|
|
runner2.Start();
|
|
|
|
runner1.Join();
|
|
|
|
runner2.Join();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(0u, result1);
|
|
|
|
EXPECT_EQ(0u, result2);
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, RemoveBeforeCancelAndWait) {
|
|
|
|
ResultType result1{0};
|
|
|
|
ThreadedRunner runner1(NewTask(&result1, TestTask::kCheckNotRun));
|
|
|
|
CancelableTaskManager::Id id = runner1.task_id();
|
|
|
|
EXPECT_EQ(1u, id);
|
|
|
|
EXPECT_EQ(TryAbortResult::kTaskAborted, manager()->TryAbort(id));
|
2015-11-14 01:20:07 +00:00
|
|
|
runner1.Start();
|
|
|
|
runner1.Join();
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelAndWait();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(0u, result1);
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, RemoveAfterCancelAndWait) {
|
|
|
|
ResultType result1{0};
|
|
|
|
ThreadedRunner runner1(NewTask(&result1));
|
|
|
|
CancelableTaskManager::Id id = runner1.task_id();
|
|
|
|
EXPECT_EQ(1u, id);
|
2015-11-14 01:20:07 +00:00
|
|
|
runner1.Start();
|
|
|
|
runner1.Join();
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelAndWait();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(TryAbortResult::kTaskRemoved, manager()->TryAbort(id));
|
|
|
|
EXPECT_EQ(1u, result1);
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, RemoveUnmanagedId) {
|
|
|
|
EXPECT_EQ(TryAbortResult::kTaskRemoved, manager()->TryAbort(1));
|
|
|
|
EXPECT_EQ(TryAbortResult::kTaskRemoved, manager()->TryAbort(2));
|
2018-11-09 15:51:43 +00:00
|
|
|
CancelAndWait();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(TryAbortResult::kTaskRemoved, manager()->TryAbort(1));
|
|
|
|
EXPECT_EQ(TryAbortResult::kTaskRemoved, manager()->TryAbort(3));
|
2015-11-14 01:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, EmptyTryAbortAll) {
|
2018-11-09 15:51:43 +00:00
|
|
|
EXPECT_EQ(TryAbortResult::kTaskRemoved, TryAbortAll());
|
[wasm] Reschedule compilation tasks
At the moment, the whole WebAssembly compilation may run in a single
background task. On a low-end device, this can mean that the background
thread is busy for seconds and thereby blocks other tasks, see e.g.
https://crbug.com/914757.
With this CL we re-schedule compilation tasks after every 50ms. These
50ms are an arbitrary number. I don't want to introduce too much
overhead, but since this is in the background we also don't have to
make tasks super short.
Tasks which are going to compile with TurboFan will be posted with
lower priority.
This change requires changes in the CancelableTaskManager. At the
moment it is not possible that a background task posts a new task
which is managed by the same task manager as itself. The problem is
about how to deal with another thread which calls CancelAndWait
concurrently. At the moment, if a new task gets posted after the call
to CancelAndWait, then `CHECK(!canceled_)` in
CancelableTaskManager::Register will fail. If we used a lock to
synchronize the calls to CancelAndWait and Register, then there would
be a deadlock, where the thread which calls CancelAndWait waits for
the task which wants to call Register, but at the same time blocks that
task by holding the lock.
With the change here, posting a task after the call to CancelAndWait
will just immediately cancel the new task. This matches the behavior
you would get if CancelAndWait is called right after calling Register.
Bug: chromium:914757
Change-Id: I6d57aba161db8a915ec0d745658e0c28d25219a8
Reviewed-on: https://chromium-review.googlesource.com/c/1411884
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58898}
2019-01-17 17:47:03 +00:00
|
|
|
CancelAndWait();
|
2017-01-04 13:45:56 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, ThreadedMultipleTasksNotRunTryAbortAll) {
|
|
|
|
ResultType result1{0};
|
|
|
|
ResultType result2{0};
|
|
|
|
ThreadedRunner runner1(NewTask(&result1, TestTask::kCheckNotRun));
|
|
|
|
ThreadedRunner runner2(NewTask(&result2, TestTask::kCheckNotRun));
|
2018-11-09 15:51:43 +00:00
|
|
|
EXPECT_EQ(TryAbortResult::kTaskAborted, TryAbortAll());
|
2017-01-04 13:45:56 +00:00
|
|
|
// Tasks are canceled, hence the runner will bail out and not update result.
|
|
|
|
runner1.Start();
|
|
|
|
runner2.Start();
|
|
|
|
runner1.Join();
|
|
|
|
runner2.Join();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(0u, result1);
|
|
|
|
EXPECT_EQ(0u, result2);
|
[wasm] Reschedule compilation tasks
At the moment, the whole WebAssembly compilation may run in a single
background task. On a low-end device, this can mean that the background
thread is busy for seconds and thereby blocks other tasks, see e.g.
https://crbug.com/914757.
With this CL we re-schedule compilation tasks after every 50ms. These
50ms are an arbitrary number. I don't want to introduce too much
overhead, but since this is in the background we also don't have to
make tasks super short.
Tasks which are going to compile with TurboFan will be posted with
lower priority.
This change requires changes in the CancelableTaskManager. At the
moment it is not possible that a background task posts a new task
which is managed by the same task manager as itself. The problem is
about how to deal with another thread which calls CancelAndWait
concurrently. At the moment, if a new task gets posted after the call
to CancelAndWait, then `CHECK(!canceled_)` in
CancelableTaskManager::Register will fail. If we used a lock to
synchronize the calls to CancelAndWait and Register, then there would
be a deadlock, where the thread which calls CancelAndWait waits for
the task which wants to call Register, but at the same time blocks that
task by holding the lock.
With the change here, posting a task after the call to CancelAndWait
will just immediately cancel the new task. This matches the behavior
you would get if CancelAndWait is called right after calling Register.
Bug: chromium:914757
Change-Id: I6d57aba161db8a915ec0d745658e0c28d25219a8
Reviewed-on: https://chromium-review.googlesource.com/c/1411884
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58898}
2019-01-17 17:47:03 +00:00
|
|
|
CancelAndWait();
|
2017-01-04 13:45:56 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:44:32 +00:00
|
|
|
TEST_F(CancelableTaskManagerTest, ThreadedMultipleTasksStartedTryAbortAll) {
|
|
|
|
ResultType result1{0};
|
|
|
|
ResultType result2{0};
|
2018-11-09 15:51:43 +00:00
|
|
|
ThreadedRunner runner1(NewTask(&result1, TestTask::kWaitTillCancelTriggered));
|
|
|
|
ThreadedRunner runner2(NewTask(&result2, TestTask::kWaitTillCancelTriggered));
|
2017-01-04 13:45:56 +00:00
|
|
|
runner1.Start();
|
|
|
|
// Busy wait on result to make sure task1 is done.
|
2018-11-08 16:44:32 +00:00
|
|
|
while (result1.load() == 0) {
|
2017-01-04 13:45:56 +00:00
|
|
|
}
|
2018-11-09 15:51:43 +00:00
|
|
|
// If the task saw that we triggered the cancel and finished *before* the
|
|
|
|
// actual cancel happened, we get {kTaskAborted}. Otherwise, we get
|
|
|
|
// {kTaskRunning}.
|
|
|
|
EXPECT_THAT(TryAbortAll(),
|
|
|
|
testing::AnyOf(testing::Eq(TryAbortResult::kTaskAborted),
|
|
|
|
testing::Eq(TryAbortResult::kTaskRunning)));
|
2017-01-04 13:45:56 +00:00
|
|
|
runner2.Start();
|
|
|
|
runner1.Join();
|
|
|
|
runner2.Join();
|
2018-11-08 16:44:32 +00:00
|
|
|
EXPECT_EQ(1u, result1);
|
|
|
|
EXPECT_EQ(0u, result2);
|
[wasm] Reschedule compilation tasks
At the moment, the whole WebAssembly compilation may run in a single
background task. On a low-end device, this can mean that the background
thread is busy for seconds and thereby blocks other tasks, see e.g.
https://crbug.com/914757.
With this CL we re-schedule compilation tasks after every 50ms. These
50ms are an arbitrary number. I don't want to introduce too much
overhead, but since this is in the background we also don't have to
make tasks super short.
Tasks which are going to compile with TurboFan will be posted with
lower priority.
This change requires changes in the CancelableTaskManager. At the
moment it is not possible that a background task posts a new task
which is managed by the same task manager as itself. The problem is
about how to deal with another thread which calls CancelAndWait
concurrently. At the moment, if a new task gets posted after the call
to CancelAndWait, then `CHECK(!canceled_)` in
CancelableTaskManager::Register will fail. If we used a lock to
synchronize the calls to CancelAndWait and Register, then there would
be a deadlock, where the thread which calls CancelAndWait waits for
the task which wants to call Register, but at the same time blocks that
task by holding the lock.
With the change here, posting a task after the call to CancelAndWait
will just immediately cancel the new task. This matches the behavior
you would get if CancelAndWait is called right after calling Register.
Bug: chromium:914757
Change-Id: I6d57aba161db8a915ec0d745658e0c28d25219a8
Reviewed-on: https://chromium-review.googlesource.com/c/1411884
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58898}
2019-01-17 17:47:03 +00:00
|
|
|
CancelAndWait();
|
2017-01-04 13:45:56 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 01:20:07 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|