Commit da66158f authored by Peter Marshall's avatar Peter Marshall Committed by Commit Bot

[platform] Implement delayed tasks in the default worker runner

This was unimplemented but is needed for Perfetto which posts delayed
tasks on worker threads e.g. drain the trace buffer into a file every x
seconds.

This is implemented by adding a second queue which holds the delayed
tasks in chronological order of 'next-to-execute'. We use an
std::multimap for the queue so that we can easily get the next delayed
task with begin().

The implementation will move delayed tasks into the main task queue
when their deadline expires.

Drive-by cleanup of the runner destructor which can just use = default.

Bug: v8:8339

Change-Id: I3629c5d6e15ced2fc47eb1b7519a2dbbf8461fce
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1521114
Commit-Queue: Peter Marshall <petermarshall@chromium.org>
Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60320}
parent 123ca158
...@@ -3428,6 +3428,8 @@ v8_component("v8_libplatform") { ...@@ -3428,6 +3428,8 @@ v8_component("v8_libplatform") {
"src/libplatform/default-platform.h", "src/libplatform/default-platform.h",
"src/libplatform/default-worker-threads-task-runner.cc", "src/libplatform/default-worker-threads-task-runner.cc",
"src/libplatform/default-worker-threads-task-runner.h", "src/libplatform/default-worker-threads-task-runner.h",
"src/libplatform/delayed-task-queue.cc",
"src/libplatform/delayed-task-queue.h",
"src/libplatform/task-queue.cc", "src/libplatform/task-queue.cc",
"src/libplatform/task-queue.h", "src/libplatform/task-queue.h",
"src/libplatform/tracing/trace-buffer.cc", "src/libplatform/tracing/trace-buffer.cc",
......
...@@ -99,14 +99,6 @@ void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) { ...@@ -99,14 +99,6 @@ void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) {
std::max(std::min(thread_pool_size, kMaxThreadPoolSize), 1); std::max(std::min(thread_pool_size, kMaxThreadPoolSize), 1);
} }
void DefaultPlatform::EnsureBackgroundTaskRunnerInitialized() {
base::MutexGuard guard(&lock_);
if (!worker_threads_task_runner_) {
worker_threads_task_runner_ =
std::make_shared<DefaultWorkerThreadsTaskRunner>(thread_pool_size_);
}
}
namespace { namespace {
double DefaultTimeFunction() { double DefaultTimeFunction() {
...@@ -116,6 +108,17 @@ double DefaultTimeFunction() { ...@@ -116,6 +108,17 @@ double DefaultTimeFunction() {
} // namespace } // namespace
void DefaultPlatform::EnsureBackgroundTaskRunnerInitialized() {
base::MutexGuard guard(&lock_);
if (!worker_threads_task_runner_) {
worker_threads_task_runner_ =
std::make_shared<DefaultWorkerThreadsTaskRunner>(
thread_pool_size_, time_function_for_testing_
? time_function_for_testing_
: DefaultTimeFunction);
}
}
void DefaultPlatform::SetTimeFunctionForTesting( void DefaultPlatform::SetTimeFunctionForTesting(
DefaultPlatform::TimeFunction time_function) { DefaultPlatform::TimeFunction time_function) {
base::MutexGuard guard(&lock_); base::MutexGuard guard(&lock_);
......
...@@ -4,23 +4,23 @@ ...@@ -4,23 +4,23 @@
#include "src/libplatform/default-worker-threads-task-runner.h" #include "src/libplatform/default-worker-threads-task-runner.h"
#include "src/base/platform/mutex.h" #include "src/libplatform/delayed-task-queue.h"
#include "src/libplatform/worker-thread.h"
namespace v8 { namespace v8 {
namespace platform { namespace platform {
DefaultWorkerThreadsTaskRunner::DefaultWorkerThreadsTaskRunner( DefaultWorkerThreadsTaskRunner::DefaultWorkerThreadsTaskRunner(
uint32_t thread_pool_size) { uint32_t thread_pool_size, TimeFunction time_function)
: queue_(time_function), time_function_(time_function) {
for (uint32_t i = 0; i < thread_pool_size; ++i) { for (uint32_t i = 0; i < thread_pool_size; ++i) {
thread_pool_.push_back(base::make_unique<WorkerThread>(&queue_)); thread_pool_.push_back(base::make_unique<WorkerThread>(this));
} }
} }
// NOLINTNEXTLINE DefaultWorkerThreadsTaskRunner::~DefaultWorkerThreadsTaskRunner() = default;
DefaultWorkerThreadsTaskRunner::~DefaultWorkerThreadsTaskRunner() {
// This destructor is needed because we have unique_ptr to the WorkerThreads, double DefaultWorkerThreadsTaskRunner::MonotonicallyIncreasingTime() {
// und the {WorkerThread} class is forward declared in the header file. return time_function_();
} }
void DefaultWorkerThreadsTaskRunner::Terminate() { void DefaultWorkerThreadsTaskRunner::Terminate() {
...@@ -41,13 +41,7 @@ void DefaultWorkerThreadsTaskRunner::PostDelayedTask(std::unique_ptr<Task> task, ...@@ -41,13 +41,7 @@ void DefaultWorkerThreadsTaskRunner::PostDelayedTask(std::unique_ptr<Task> task,
double delay_in_seconds) { double delay_in_seconds) {
base::MutexGuard guard(&lock_); base::MutexGuard guard(&lock_);
if (terminated_) return; if (terminated_) return;
if (delay_in_seconds == 0) { queue_.AppendDelayed(std::move(task), delay_in_seconds);
queue_.Append(std::move(task));
return;
}
// There is no use case for this function with non zero delay_in_second on a
// worker thread at the moment, but it is still part of the interface.
UNIMPLEMENTED();
} }
void DefaultWorkerThreadsTaskRunner::PostIdleTask( void DefaultWorkerThreadsTaskRunner::PostIdleTask(
...@@ -61,5 +55,24 @@ bool DefaultWorkerThreadsTaskRunner::IdleTasksEnabled() { ...@@ -61,5 +55,24 @@ bool DefaultWorkerThreadsTaskRunner::IdleTasksEnabled() {
return false; return false;
} }
std::unique_ptr<Task> DefaultWorkerThreadsTaskRunner::GetNext() {
return queue_.GetNext();
}
DefaultWorkerThreadsTaskRunner::WorkerThread::WorkerThread(
DefaultWorkerThreadsTaskRunner* runner)
: Thread(Options("V8 DefaultWorkerThreadsTaskRunner WorkerThread")),
runner_(runner) {
Start();
}
DefaultWorkerThreadsTaskRunner::WorkerThread::~WorkerThread() { Join(); }
void DefaultWorkerThreadsTaskRunner::WorkerThread::Run() {
while (std::unique_ptr<Task> task = runner_->GetNext()) {
task->Run();
}
}
} // namespace platform } // namespace platform
} // namespace v8 } // namespace v8
...@@ -5,24 +5,31 @@ ...@@ -5,24 +5,31 @@
#ifndef V8_LIBPLATFORM_DEFAULT_WORKER_THREADS_TASK_RUNNER_H_ #ifndef V8_LIBPLATFORM_DEFAULT_WORKER_THREADS_TASK_RUNNER_H_
#define V8_LIBPLATFORM_DEFAULT_WORKER_THREADS_TASK_RUNNER_H_ #define V8_LIBPLATFORM_DEFAULT_WORKER_THREADS_TASK_RUNNER_H_
#include <vector>
#include "include/libplatform/libplatform-export.h"
#include "include/v8-platform.h" #include "include/v8-platform.h"
#include "src/libplatform/task-queue.h" #include "src/base/platform/mutex.h"
#include "src/base/platform/platform.h"
#include "src/libplatform/delayed-task-queue.h"
namespace v8 { namespace v8 {
namespace platform { namespace platform {
class Thread;
class WorkerThread;
class V8_PLATFORM_EXPORT DefaultWorkerThreadsTaskRunner class V8_PLATFORM_EXPORT DefaultWorkerThreadsTaskRunner
: public NON_EXPORTED_BASE(TaskRunner) { : public NON_EXPORTED_BASE(TaskRunner) {
public: public:
DefaultWorkerThreadsTaskRunner(uint32_t thread_pool_size); using TimeFunction = double (*)();
DefaultWorkerThreadsTaskRunner(uint32_t thread_pool_size,
TimeFunction time_function);
~DefaultWorkerThreadsTaskRunner() override; ~DefaultWorkerThreadsTaskRunner() override;
void Terminate(); void Terminate();
double MonotonicallyIncreasingTime();
// v8::TaskRunner implementation. // v8::TaskRunner implementation.
void PostTask(std::unique_ptr<Task> task) override; void PostTask(std::unique_ptr<Task> task) override;
...@@ -34,12 +41,32 @@ class V8_PLATFORM_EXPORT DefaultWorkerThreadsTaskRunner ...@@ -34,12 +41,32 @@ class V8_PLATFORM_EXPORT DefaultWorkerThreadsTaskRunner
bool IdleTasksEnabled() override; bool IdleTasksEnabled() override;
private: private:
class WorkerThread : public base::Thread {
public:
explicit WorkerThread(DefaultWorkerThreadsTaskRunner* runner);
~WorkerThread() override;
// This thread attempts to get tasks in a loop from |runner_| and run them.
void Run() override;
private:
DefaultWorkerThreadsTaskRunner* runner_;
DISALLOW_COPY_AND_ASSIGN(WorkerThread);
};
// Called by the WorkerThread. Gets the next take (delayed or immediate) to be
// executed. Blocks if no task is available.
std::unique_ptr<Task> GetNext();
bool terminated_ = false; bool terminated_ = false;
base::Mutex lock_; base::Mutex lock_;
TaskQueue queue_; DelayedTaskQueue queue_;
std::vector<std::unique_ptr<WorkerThread>> thread_pool_; std::vector<std::unique_ptr<WorkerThread>> thread_pool_;
TimeFunction time_function_;
}; };
} // namespace platform } // namespace platform
} // namespace v8 } // namespace v8
#endif // V8_LIBPLATFORM_DEFAULT_WORKER_THREADS_TASK_RUNNER_H_ #endif // V8_LIBPLATFORM_DEFAULT_WORKER_THREADS_TASK_RUNNER_H_
// Copyright 2019 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/libplatform/delayed-task-queue.h"
#include "include/v8-platform.h"
#include "src/base/logging.h"
#include "src/base/platform/time.h"
namespace v8 {
namespace platform {
DelayedTaskQueue::DelayedTaskQueue(TimeFunction time_function)
: time_function_(time_function) {}
DelayedTaskQueue::~DelayedTaskQueue() {
base::MutexGuard guard(&lock_);
DCHECK(terminated_);
DCHECK(task_queue_.empty());
}
double DelayedTaskQueue::MonotonicallyIncreasingTime() {
return time_function_();
}
void DelayedTaskQueue::Append(std::unique_ptr<Task> task) {
base::MutexGuard guard(&lock_);
DCHECK(!terminated_);
task_queue_.push(std::move(task));
queues_condition_var_.NotifyOne();
}
void DelayedTaskQueue::AppendDelayed(std::unique_ptr<Task> task,
double delay_in_seconds) {
DCHECK_GE(delay_in_seconds, 0.0);
double deadline = MonotonicallyIncreasingTime() + delay_in_seconds;
{
base::MutexGuard guard(&lock_);
DCHECK(!terminated_);
delayed_task_queue_.emplace(deadline, std::move(task));
queues_condition_var_.NotifyOne();
}
}
std::unique_ptr<Task> DelayedTaskQueue::GetNext() {
base::MutexGuard guard(&lock_);
for (;;) {
// Move delayed tasks that have hit their deadline to the main queue.
std::unique_ptr<Task> task = PopTaskFromDelayedQueue();
while (task) {
task_queue_.push(std::move(task));
task = PopTaskFromDelayedQueue();
}
if (!task_queue_.empty()) {
std::unique_ptr<Task> result = std::move(task_queue_.front());
task_queue_.pop();
return result;
}
if (terminated_) {
queues_condition_var_.NotifyAll();
return nullptr;
}
if (task_queue_.empty() && !delayed_task_queue_.empty()) {
// Wait for the next delayed task or a newly posted task.
double now = MonotonicallyIncreasingTime();
double wait_in_seconds = delayed_task_queue_.begin()->first - now;
base::TimeDelta wait_delta = base::TimeDelta::FromMicroseconds(
base::TimeConstants::kMicrosecondsPerSecond * wait_in_seconds);
// WaitFor unfortunately doesn't care about our fake time and will wait
// the 'real' amount of time, based on whatever clock the system call
// uses.
bool notified = queues_condition_var_.WaitFor(&lock_, wait_delta);
USE(notified);
} else {
queues_condition_var_.Wait(&lock_);
}
}
}
// Gets the next task from the delayed queue for which the deadline has passed
// according to |time_function_|. Returns nullptr if no such task exists.
std::unique_ptr<Task> DelayedTaskQueue::PopTaskFromDelayedQueue() {
if (delayed_task_queue_.empty()) return nullptr;
double now = MonotonicallyIncreasingTime();
auto it = delayed_task_queue_.begin();
if (it->first > now) return nullptr;
std::unique_ptr<Task> result = std::move(it->second);
delayed_task_queue_.erase(it);
return result;
}
void DelayedTaskQueue::Terminate() {
base::MutexGuard guard(&lock_);
DCHECK(!terminated_);
terminated_ = true;
queues_condition_var_.NotifyAll();
}
} // namespace platform
} // namespace v8
// Copyright 2019 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.
#ifndef V8_LIBPLATFORM_DELAYED_TASK_QUEUE_H_
#define V8_LIBPLATFORM_DELAYED_TASK_QUEUE_H_
#include <map>
#include <queue>
#include "include/libplatform/libplatform-export.h"
#include "src/base/macros.h"
#include "src/base/platform/condition-variable.h"
#include "src/base/platform/mutex.h"
namespace v8 {
class Task;
namespace platform {
// DelayedTaskQueue provides queueing for immediate and delayed tasks. It does
// not provide any guarantees about ordering of tasks, except that immediate
// tasks will be run in the order that they are posted.
class V8_PLATFORM_EXPORT DelayedTaskQueue {
public:
using TimeFunction = double (*)();
explicit DelayedTaskQueue(TimeFunction time_function);
~DelayedTaskQueue();
double MonotonicallyIncreasingTime();
// Appends an immediate task to the queue. The queue takes ownership of
// |task|. Tasks appended via this method will be run in order. Thread-safe.
void Append(std::unique_ptr<Task> task);
// Appends a delayed task to the queue. There is no ordering guarantee
// provided regarding delayed tasks, both with respect to other delayed tasks
// and non-delayed tasks that were appended using Append(). Thread-safe.
void AppendDelayed(std::unique_ptr<Task> task, double delay_in_seconds);
// Returns the next task to process. Blocks if no task is available.
// Returns nullptr if the queue is terminated. Will return either an immediate
// task posted using Append() or a delayed task where the deadline has passed,
// according to the |time_function| provided in the constructor. Thread-safe.
std::unique_ptr<Task> GetNext();
// Terminate the queue.
void Terminate();
private:
std::unique_ptr<Task> PopTaskFromDelayedQueue();
base::ConditionVariable queues_condition_var_;
base::Mutex lock_;
std::queue<std::unique_ptr<Task>> task_queue_;
std::multimap<double, std::unique_ptr<Task>> delayed_task_queue_;
bool terminated_ = false;
TimeFunction time_function_;
DISALLOW_COPY_AND_ASSIGN(DelayedTaskQueue);
};
} // namespace platform
} // namespace v8
#endif // V8_LIBPLATFORM_DELAYED_TASK_QUEUE_H_
...@@ -15,12 +15,10 @@ WorkerThread::WorkerThread(TaskQueue* queue) ...@@ -15,12 +15,10 @@ WorkerThread::WorkerThread(TaskQueue* queue)
Start(); Start();
} }
WorkerThread::~WorkerThread() { WorkerThread::~WorkerThread() {
Join(); Join();
} }
void WorkerThread::Run() { void WorkerThread::Run() {
while (std::unique_ptr<Task> task = queue_->GetNext()) { while (std::unique_ptr<Task> task = queue_->GetNext()) {
task->Run(); task->Run();
......
...@@ -27,8 +27,6 @@ class V8_PLATFORM_EXPORT WorkerThread : public NON_EXPORTED_BASE(base::Thread) { ...@@ -27,8 +27,6 @@ class V8_PLATFORM_EXPORT WorkerThread : public NON_EXPORTED_BASE(base::Thread) {
void Run() override; void Run() override;
private: private:
friend class QuitTask;
TaskQueue* queue_; TaskQueue* queue_;
DISALLOW_COPY_AND_ASSIGN(WorkerThread); DISALLOW_COPY_AND_ASSIGN(WorkerThread);
......
...@@ -182,6 +182,7 @@ v8_source_set("unittests_sources") { ...@@ -182,6 +182,7 @@ v8_source_set("unittests_sources") {
"interpreter/interpreter-assembler-unittest.cc", "interpreter/interpreter-assembler-unittest.cc",
"interpreter/interpreter-assembler-unittest.h", "interpreter/interpreter-assembler-unittest.h",
"libplatform/default-platform-unittest.cc", "libplatform/default-platform-unittest.cc",
"libplatform/default-worker-threads-task-runner-unittest.cc",
"libplatform/task-queue-unittest.cc", "libplatform/task-queue-unittest.cc",
"libplatform/worker-thread-unittest.cc", "libplatform/worker-thread-unittest.cc",
"locked-queue-unittest.cc", "locked-queue-unittest.cc",
......
// Copyright 2019 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/libplatform/default-worker-threads-task-runner.h"
#include <vector>
#include "include/v8-platform.h"
#include "src/base/platform/platform.h"
#include "src/base/platform/semaphore.h"
#include "src/base/platform/time.h"
#include "testing/gtest-support.h"
namespace v8 {
namespace platform {
class TestTask : public v8::Task {
public:
explicit TestTask(std::function<void()> f) : f_(std::move(f)) {}
void Run() override { f_(); }
private:
std::function<void()> f_;
};
double RealTime() {
return base::TimeTicks::HighResolutionNow().ToInternalValue() /
static_cast<double>(base::Time::kMicrosecondsPerSecond);
}
TEST(DefaultWorkerThreadsTaskRunnerUnittest, PostTaskOrder) {
DefaultWorkerThreadsTaskRunner runner(1, RealTime);
std::vector<int> order;
base::Semaphore semaphore(0);
std::unique_ptr<TestTask> task1 =
base::make_unique<TestTask>([&] { order.push_back(1); });
std::unique_ptr<TestTask> task2 =
base::make_unique<TestTask>([&] { order.push_back(2); });
std::unique_ptr<TestTask> task3 = base::make_unique<TestTask>([&] {
order.push_back(3);
semaphore.Signal();
});
runner.PostTask(std::move(task1));
runner.PostTask(std::move(task2));
runner.PostTask(std::move(task3));
semaphore.Wait();
runner.Terminate();
ASSERT_EQ(3UL, order.size());
ASSERT_EQ(1, order[0]);
ASSERT_EQ(2, order[1]);
ASSERT_EQ(3, order[2]);
}
TEST(DefaultWorkerThreadsTaskRunnerUnittest, PostTaskOrderMultipleWorkers) {
DefaultWorkerThreadsTaskRunner runner(4, RealTime);
base::Mutex vector_lock;
std::vector<int> order;
std::atomic_int count{0};
std::unique_ptr<TestTask> task1 = base::make_unique<TestTask>([&] {
base::MutexGuard guard(&vector_lock);
order.push_back(1);
count++;
});
std::unique_ptr<TestTask> task2 = base::make_unique<TestTask>([&] {
base::MutexGuard guard(&vector_lock);
order.push_back(2);
count++;
});
std::unique_ptr<TestTask> task3 = base::make_unique<TestTask>([&] {
base::MutexGuard guard(&vector_lock);
order.push_back(3);
count++;
});
std::unique_ptr<TestTask> task4 = base::make_unique<TestTask>([&] {
base::MutexGuard guard(&vector_lock);
order.push_back(4);
count++;
});
std::unique_ptr<TestTask> task5 = base::make_unique<TestTask>([&] {
base::MutexGuard guard(&vector_lock);
order.push_back(5);
count++;
});
runner.PostTask(std::move(task1));
runner.PostTask(std::move(task2));
runner.PostTask(std::move(task3));
runner.PostTask(std::move(task4));
runner.PostTask(std::move(task5));
// We can't observe any ordering when there are multiple worker threads. The
// tasks are guaranteed to be dispatched to workers in the input order, but
// the workers are different threads and can be scheduled arbitrarily. Just
// check that all of the tasks were run once.
while (count != 5) {
}
runner.Terminate();
ASSERT_EQ(5UL, order.size());
ASSERT_EQ(1, std::count(order.begin(), order.end(), 1));
ASSERT_EQ(1, std::count(order.begin(), order.end(), 2));
ASSERT_EQ(1, std::count(order.begin(), order.end(), 3));
ASSERT_EQ(1, std::count(order.begin(), order.end(), 4));
ASSERT_EQ(1, std::count(order.begin(), order.end(), 5));
}
class FakeClock {
public:
static double time() { return time_.load(); }
static void set_time(double time) { time_.store(time); }
static void set_time_and_wake_up_runner(
double time, DefaultWorkerThreadsTaskRunner* runner) {
time_.store(time);
// PostTask will cause the condition variable WaitFor() call to be notified
// early, rather than waiting for the real amount of time. WaitFor() listens
// to the system clock and not our FakeClock.
runner->PostTask(base::make_unique<TestTask>([] {}));
}
private:
static std::atomic<double> time_;
};
std::atomic<double> FakeClock::time_{0.0};
TEST(DefaultWorkerThreadsTaskRunnerUnittest, PostDelayedTaskOrder) {
FakeClock::set_time(0.0);
DefaultWorkerThreadsTaskRunner runner(1, FakeClock::time);
std::vector<int> order;
base::Semaphore task1_semaphore(0);
base::Semaphore task3_semaphore(0);
std::unique_ptr<TestTask> task1 = base::make_unique<TestTask>([&] {
order.push_back(1);
task1_semaphore.Signal();
});
std::unique_ptr<TestTask> task2 =
base::make_unique<TestTask>([&] { order.push_back(2); });
std::unique_ptr<TestTask> task3 = base::make_unique<TestTask>([&] {
order.push_back(3);
task3_semaphore.Signal();
});
runner.PostDelayedTask(std::move(task1), 100);
runner.PostTask(std::move(task2));
runner.PostTask(std::move(task3));
FakeClock::set_time_and_wake_up_runner(99, &runner);
task3_semaphore.Wait();
ASSERT_EQ(2UL, order.size());
ASSERT_EQ(2, order[0]);
ASSERT_EQ(3, order[1]);
FakeClock::set_time_and_wake_up_runner(101, &runner);
task1_semaphore.Wait();
runner.Terminate();
ASSERT_EQ(3UL, order.size());
ASSERT_EQ(2, order[0]);
ASSERT_EQ(3, order[1]);
ASSERT_EQ(1, order[2]);
}
TEST(DefaultWorkerThreadsTaskRunnerUnittest, PostDelayedTaskOrder2) {
FakeClock::set_time(0.0);
DefaultWorkerThreadsTaskRunner runner(1, FakeClock::time);
std::vector<int> order;
base::Semaphore task1_semaphore(0);
base::Semaphore task2_semaphore(0);
base::Semaphore task3_semaphore(0);
std::unique_ptr<TestTask> task1 = base::make_unique<TestTask>([&] {
order.push_back(1);
task1_semaphore.Signal();
});
std::unique_ptr<TestTask> task2 = base::make_unique<TestTask>([&] {
order.push_back(2);
task2_semaphore.Signal();
});
std::unique_ptr<TestTask> task3 = base::make_unique<TestTask>([&] {
order.push_back(3);
task3_semaphore.Signal();
});
runner.PostDelayedTask(std::move(task1), 500);
runner.PostDelayedTask(std::move(task2), 100);
runner.PostDelayedTask(std::move(task3), 200);
FakeClock::set_time_and_wake_up_runner(101, &runner);
task2_semaphore.Wait();
ASSERT_EQ(1UL, order.size());
ASSERT_EQ(2, order[0]);
FakeClock::set_time_and_wake_up_runner(201, &runner);
task3_semaphore.Wait();
ASSERT_EQ(2UL, order.size());
ASSERT_EQ(2, order[0]);
ASSERT_EQ(3, order[1]);
FakeClock::set_time_and_wake_up_runner(501, &runner);
task1_semaphore.Wait();
runner.Terminate();
ASSERT_EQ(3UL, order.size());
ASSERT_EQ(2, order[0]);
ASSERT_EQ(3, order[1]);
ASSERT_EQ(1, order[2]);
}
TEST(DefaultWorkerThreadsTaskRunnerUnittest, PostAfterTerminate) {
FakeClock::set_time(0.0);
DefaultWorkerThreadsTaskRunner runner(1, FakeClock::time);
std::vector<int> order;
base::Semaphore task1_semaphore(0);
base::Semaphore task2_semaphore(0);
base::Semaphore task3_semaphore(0);
std::unique_ptr<TestTask> task1 = base::make_unique<TestTask>([&] {
order.push_back(1);
task1_semaphore.Signal();
});
std::unique_ptr<TestTask> task2 = base::make_unique<TestTask>([&] {
order.push_back(2);
task2_semaphore.Signal();
});
std::unique_ptr<TestTask> task3 = base::make_unique<TestTask>([&] {
order.push_back(3);
task3_semaphore.Signal();
});
runner.PostTask(std::move(task1));
runner.PostDelayedTask(std::move(task2), 100);
task1_semaphore.Wait();
ASSERT_EQ(1UL, order.size());
ASSERT_EQ(1, order[0]);
runner.Terminate();
FakeClock::set_time_and_wake_up_runner(201, &runner);
// OK, we can't actually prove that this never executes. But wait a bit at
// least.
bool signalled =
task2_semaphore.WaitFor(base::TimeDelta::FromMilliseconds(100));
ASSERT_FALSE(signalled);
ASSERT_EQ(1UL, order.size());
ASSERT_EQ(1, order[0]);
runner.PostTask(std::move(task3));
signalled = task3_semaphore.WaitFor(base::TimeDelta::FromMilliseconds(100));
ASSERT_FALSE(signalled);
ASSERT_EQ(1UL, order.size());
ASSERT_EQ(1, order[0]);
}
TEST(DefaultWorkerThreadsTaskRunnerUnittest, NoIdleTasks) {
DefaultWorkerThreadsTaskRunner runner(1, FakeClock::time);
ASSERT_FALSE(runner.IdleTasksEnabled());
runner.Terminate();
}
} // namespace platform
} // namespace v8
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment