Commit 053b619c authored by Alexander Timin's avatar Alexander Timin Committed by Commit Bot

[scheduler] Use WorkerSchedulerProxy in DedicatedWorkerThread.

Create newly-introduced WorkerSchedulerProxy in DedicatedWorkerThread
and plumb it to WorkerScheduler via WebThreadCreationParams.

BUG=776416
R=kinuko@chromium.org, haraken@chromium.org

Change-Id: I48de4ee3986b1c53dd22c3b9da82ac7bb9a3ee54
Reviewed-on: https://chromium-review.googlesource.com/957046Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Alexander Timin <altimin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#543362}
parent a1ccbb60
......@@ -373,24 +373,21 @@ WebString BlinkPlatformImpl::UserAgent() {
std::unique_ptr<blink::WebThread> BlinkPlatformImpl::CreateThread(
const blink::WebThreadCreationParams& params) {
std::unique_ptr<blink::scheduler::WebThreadBase> thread =
blink::scheduler::WebThreadBase::CreateWorkerThread(
params.name, base::Thread::Options());
blink::scheduler::WebThreadBase::CreateWorkerThread(params);
thread->Init();
WaitUntilWebThreadTLSUpdate(thread.get());
return std::move(thread);
}
std::unique_ptr<blink::WebThread> BlinkPlatformImpl::CreateWebAudioThread() {
base::Thread::Options thread_options;
blink::WebThreadCreationParams params(blink::WebThreadType::kWebAudioThread);
// WebAudio uses a thread with |DISPLAY| priority to avoid glitch when the
// system is under the high pressure. Note that the main browser thread also
// runs with same priority. (see: crbug.com/734539)
thread_options.priority = base::ThreadPriority::DISPLAY;
params.thread_options.priority = base::ThreadPriority::DISPLAY;
std::unique_ptr<blink::scheduler::WebThreadBase> thread =
blink::scheduler::WebThreadBase::CreateWorkerThread(
"WebAudio Rendering Thread", thread_options);
blink::scheduler::WebThreadBase::CreateWorkerThread(params);
thread->Init();
WaitUntilWebThreadTLSUpdate(thread.get());
return std::move(thread);
......
......@@ -1293,12 +1293,13 @@ void RenderThreadImpl::SetResourceDispatcherDelegate(
}
void RenderThreadImpl::InitializeCompositorThread() {
base::Thread::Options options;
blink::WebThreadCreationParams params(
blink::WebThreadType::kCompositorThread);
#if defined(OS_ANDROID)
options.priority = base::ThreadPriority::DISPLAY;
params.thread_options.priority = base::ThreadPriority::DISPLAY;
#endif
compositor_thread_ =
blink::scheduler::WebThreadBase::CreateCompositorThread(options);
blink::scheduler::WebThreadBase::CreateCompositorThread(params);
blink_platform_impl_->SetCompositorThread(compositor_thread_.get());
compositor_task_runner_ = compositor_thread_->GetTaskRunner();
compositor_task_runner_->PostTask(
......
/*
* Copyright (C) 2009 Google Inc. All rights reserved.
*
......@@ -35,6 +34,9 @@
#include <utility>
#include "base/memory/ptr_util.h"
#include "core/dom/Document.h"
#include "core/dom/ExecutionContext.h"
#include "core/frame/LocalFrame.h"
#include "core/workers/DedicatedWorkerGlobalScope.h"
#include "core/workers/DedicatedWorkerObjectProxy.h"
#include "core/workers/GlobalScopeCreationParams.h"
......@@ -42,6 +44,20 @@
namespace blink {
namespace {
WebFrameScheduler* GetFrameScheduler(
ThreadableLoadingContext* loading_context) {
// |loading_context| can be null in unittests.
if (!loading_context)
return nullptr;
return ToDocument(loading_context->GetExecutionContext())
->GetFrame()
->FrameScheduler();
}
} // namespace
std::unique_ptr<DedicatedWorkerThread> DedicatedWorkerThread::Create(
ThreadableLoadingContext* loading_context,
DedicatedWorkerObjectProxy& worker_object_proxy) {
......@@ -54,7 +70,8 @@ DedicatedWorkerThread::DedicatedWorkerThread(
DedicatedWorkerObjectProxy& worker_object_proxy)
: WorkerThread(loading_context, worker_object_proxy),
worker_backing_thread_(WorkerBackingThread::Create(
WebThreadCreationParams(GetThreadType()))),
WebThreadCreationParams(GetThreadType())
.SetFrameScheduler(GetFrameScheduler(loading_context)))),
worker_object_proxy_(worker_object_proxy) {}
DedicatedWorkerThread::~DedicatedWorkerThread() = default;
......
......@@ -17,7 +17,9 @@
namespace blink {
WebThreadCreationParams::WebThreadCreationParams(WebThreadType thread_type)
: thread_type(thread_type), name(GetNameForThreadType(thread_type)) {}
: thread_type(thread_type),
name(GetNameForThreadType(thread_type)),
frame_scheduler(nullptr) {}
WebThreadCreationParams& WebThreadCreationParams::SetThreadName(
const char* thread_name) {
......@@ -25,6 +27,12 @@ WebThreadCreationParams& WebThreadCreationParams::SetThreadName(
return *this;
}
WebThreadCreationParams& WebThreadCreationParams::SetFrameScheduler(
WebFrameScheduler* scheduler) {
frame_scheduler = scheduler;
return *this;
}
#if defined(OS_WIN)
static_assert(sizeof(blink::PlatformThreadId) >= sizeof(DWORD),
"size of platform thread id is too small");
......
......@@ -107,8 +107,8 @@ namespace {
class WebThreadForCompositor : public WebThreadImplForWorkerScheduler {
public:
explicit WebThreadForCompositor(base::Thread::Options options)
: WebThreadImplForWorkerScheduler("Compositor", options) {
explicit WebThreadForCompositor(const WebThreadCreationParams& params)
: WebThreadImplForWorkerScheduler(params) {
Init();
}
~WebThreadForCompositor() override = default;
......@@ -127,14 +127,13 @@ class WebThreadForCompositor : public WebThreadImplForWorkerScheduler {
} // namespace
std::unique_ptr<WebThreadBase> WebThreadBase::CreateWorkerThread(
const char* name,
base::Thread::Options options) {
return std::make_unique<WebThreadImplForWorkerScheduler>(name, options);
const WebThreadCreationParams& params) {
return std::make_unique<WebThreadImplForWorkerScheduler>(params);
}
std::unique_ptr<WebThreadBase> WebThreadBase::CreateCompositorThread(
base::Thread::Options options) {
return std::make_unique<WebThreadForCompositor>(options);
const WebThreadCreationParams& params) {
return std::make_unique<WebThreadForCompositor>(params);
}
std::unique_ptr<WebThreadBase> WebThreadBase::InitializeUtilityThread() {
......
......@@ -13,19 +13,19 @@
#include "platform/scheduler/base/task_queue.h"
#include "platform/scheduler/child/web_scheduler_impl.h"
#include "platform/scheduler/child/worker_scheduler_impl.h"
#include "platform/scheduler/child/worker_scheduler_proxy.h"
namespace blink {
namespace scheduler {
WebThreadImplForWorkerScheduler::WebThreadImplForWorkerScheduler(
const char* name)
: WebThreadImplForWorkerScheduler(name, base::Thread::Options()) {}
WebThreadImplForWorkerScheduler::WebThreadImplForWorkerScheduler(
const char* name,
base::Thread::Options options)
: thread_(new base::Thread(name ? name : std::string())) {
bool started = thread_->StartWithOptions(options);
const WebThreadCreationParams& params)
: thread_(new base::Thread(params.name ? params.name : std::string())),
worker_scheduler_proxy_(
params.frame_scheduler
? std::make_unique<WorkerSchedulerProxy>(params.frame_scheduler)
: nullptr) {
bool started = thread_->StartWithOptions(params.thread_options);
CHECK(started);
thread_task_runner_ = thread_->task_runner();
}
......@@ -86,7 +86,7 @@ void WebThreadImplForWorkerScheduler::ShutdownOnThread(
std::unique_ptr<WorkerScheduler>
WebThreadImplForWorkerScheduler::CreateWorkerScheduler() {
return WorkerScheduler::Create();
return WorkerScheduler::Create(worker_scheduler_proxy_.get());
}
void WebThreadImplForWorkerScheduler::WillDestroyCurrentMessageLoop() {
......
......@@ -25,14 +25,14 @@ class SingleThreadIdleTaskRunner;
class TaskQueue;
class WebSchedulerImpl;
class WorkerScheduler;
class WorkerSchedulerProxy;
class PLATFORM_EXPORT WebThreadImplForWorkerScheduler
: public WebThreadBase,
public base::MessageLoop::DestructionObserver {
public:
explicit WebThreadImplForWorkerScheduler(const char* name);
WebThreadImplForWorkerScheduler(const char* name,
base::Thread::Options options);
explicit WebThreadImplForWorkerScheduler(
const WebThreadCreationParams& params);
~WebThreadImplForWorkerScheduler() override;
// WebThread implementation.
......@@ -52,11 +52,15 @@ class PLATFORM_EXPORT WebThreadImplForWorkerScheduler
}
protected:
virtual std::unique_ptr<WorkerScheduler> CreateWorkerScheduler();
base::Thread* GetThread() const { return thread_.get(); }
private:
virtual std::unique_ptr<scheduler::WorkerScheduler> CreateWorkerScheduler();
scheduler::WorkerSchedulerProxy* worker_scheduler_proxy() const {
return worker_scheduler_proxy_.get();
}
private:
void AddTaskObserverInternal(
base::MessageLoop::TaskObserver* observer) override;
void RemoveTaskObserverInternal(
......@@ -66,6 +70,7 @@ class PLATFORM_EXPORT WebThreadImplForWorkerScheduler
void ShutdownOnThread(base::WaitableEvent* completion);
std::unique_ptr<base::Thread> thread_;
std::unique_ptr<scheduler::WorkerSchedulerProxy> worker_scheduler_proxy_;
std::unique_ptr<scheduler::WorkerScheduler> worker_scheduler_;
std::unique_ptr<scheduler::WebSchedulerImpl> web_scheduler_;
scoped_refptr<base::SingleThreadTaskRunner> thread_task_runner_;
......
......@@ -74,7 +74,8 @@ class WebThreadImplForWorkerSchedulerTest : public ::testing::Test {
~WebThreadImplForWorkerSchedulerTest() override = default;
void SetUp() override {
thread_.reset(new WebThreadImplForWorkerScheduler("test thread"));
thread_.reset(new WebThreadImplForWorkerScheduler(
WebThreadCreationParams(WebThreadType::kTestThread)));
thread_->Init();
}
......
......@@ -19,10 +19,10 @@ WorkerScheduler::WorkerScheduler(std::unique_ptr<WorkerSchedulerHelper> helper)
WorkerScheduler::~WorkerScheduler() = default;
// static
std::unique_ptr<WorkerScheduler> WorkerScheduler::Create() {
// TODO(altimin): Plumb WorkerSchedulerProxy to the constructor.
std::unique_ptr<WorkerScheduler> WorkerScheduler::Create(
WorkerSchedulerProxy* proxy) {
return base::WrapUnique(new WorkerSchedulerImpl(
TaskQueueManager::TakeOverCurrentThread(), nullptr /* proxy */));
TaskQueueManager::TakeOverCurrentThread(), proxy));
}
scoped_refptr<WorkerTaskQueue> WorkerScheduler::CreateTaskRunner() {
......
......@@ -19,12 +19,13 @@
namespace blink {
namespace scheduler {
class WorkerSchedulerProxy;
class PLATFORM_EXPORT WorkerScheduler : public ChildScheduler {
public:
~WorkerScheduler() override;
static std::unique_ptr<WorkerScheduler> Create();
static std::unique_ptr<WorkerScheduler> Create(WorkerSchedulerProxy* proxy);
// Blink should use WorkerScheduler::DefaultTaskQueue instead of
// ChildScheduler::DefaultTaskRunner.
......
......@@ -44,16 +44,16 @@ class WorkerSchedulerImplForTest : public WorkerSchedulerImpl {
class WebThreadImplForWorkerSchedulerForTest
: public WebThreadImplForWorkerScheduler {
public:
explicit WebThreadImplForWorkerSchedulerForTest(
WorkerSchedulerProxy* proxy,
WebThreadImplForWorkerSchedulerForTest(WebFrameScheduler* frame_scheduler,
WaitableEvent* throtting_state_changed)
: WebThreadImplForWorkerScheduler("Test Worker", base::Thread::Options()),
proxy_(proxy),
: WebThreadImplForWorkerScheduler(
WebThreadCreationParams(WebThreadType::kTestThread)
.SetFrameScheduler(frame_scheduler)),
throtting_state_changed_(throtting_state_changed) {}
std::unique_ptr<WorkerScheduler> CreateWorkerScheduler() override {
std::unique_ptr<WorkerScheduler> CreateWorkerScheduler() {
auto scheduler = std::make_unique<WorkerSchedulerImplForTest>(
TaskQueueManager::TakeOverCurrentThread(), proxy_,
TaskQueueManager::TakeOverCurrentThread(), worker_scheduler_proxy(),
throtting_state_changed_);
scheduler_ = scheduler.get();
return scheduler;
......@@ -62,17 +62,16 @@ class WebThreadImplForWorkerSchedulerForTest
WorkerSchedulerImplForTest* GetWorkerScheduler() { return scheduler_; }
private:
WorkerSchedulerProxy* proxy_; // NOW OWNED
WaitableEvent* throtting_state_changed_; // NOT OWNED
WorkerSchedulerImplForTest* scheduler_ = nullptr; // NOT OWNED
};
std::unique_ptr<WebThreadImplForWorkerSchedulerForTest> CreateWorkerThread(
WorkerSchedulerProxy* proxy,
WebFrameScheduler* frame_scheduler,
WaitableEvent* throtting_state_changed) {
std::unique_ptr<WebThreadImplForWorkerSchedulerForTest> thread =
std::make_unique<WebThreadImplForWorkerSchedulerForTest>(
proxy, throtting_state_changed);
frame_scheduler, throtting_state_changed);
thread->Init();
return thread;
}
......@@ -115,11 +114,8 @@ class WorkerSchedulerProxyTest : public ::testing::Test {
TEST_F(WorkerSchedulerProxyTest, VisibilitySignalReceived) {
WaitableEvent throtting_state_changed;
std::unique_ptr<WorkerSchedulerProxy> proxy =
std::make_unique<WorkerSchedulerProxy>(frame_scheduler_.get());
auto worker_thread =
CreateWorkerThread(proxy.get(), &throtting_state_changed);
CreateWorkerThread(frame_scheduler_.get(), &throtting_state_changed);
DCHECK(worker_thread->GetWorkerScheduler()->throttling_state() ==
WebFrameScheduler::ThrottlingState::kNotThrottled);
......@@ -142,11 +138,8 @@ TEST_F(WorkerSchedulerProxyTest, VisibilitySignalReceived) {
TEST_F(WorkerSchedulerProxyTest, FrameSchedulerDestroyed) {
WaitableEvent throtting_state_changed;
std::unique_ptr<WorkerSchedulerProxy> proxy =
std::make_unique<WorkerSchedulerProxy>(frame_scheduler_.get());
auto worker_thread =
CreateWorkerThread(proxy.get(), &throtting_state_changed);
CreateWorkerThread(frame_scheduler_.get(), &throtting_state_changed);
DCHECK(worker_thread->GetWorkerScheduler()->throttling_state() ==
WebFrameScheduler::ThrottlingState::kNotThrottled);
......@@ -160,18 +153,14 @@ TEST_F(WorkerSchedulerProxyTest, FrameSchedulerDestroyed) {
mock_main_thread_task_runner_->RunUntilIdle();
worker_thread.reset();
proxy.reset();
mock_main_thread_task_runner_->RunUntilIdle();
}
TEST_F(WorkerSchedulerProxyTest, ThreadDestroyed) {
WaitableEvent throtting_state_changed;
std::unique_ptr<WorkerSchedulerProxy> proxy =
std::make_unique<WorkerSchedulerProxy>(frame_scheduler_.get());
auto worker_thread =
CreateWorkerThread(proxy.get(), &throtting_state_changed);
CreateWorkerThread(frame_scheduler_.get(), &throtting_state_changed);
DCHECK(worker_thread->GetWorkerScheduler()->throttling_state() ==
WebFrameScheduler::ThrottlingState::kNotThrottled);
......@@ -182,7 +171,6 @@ TEST_F(WorkerSchedulerProxyTest, ThreadDestroyed) {
WebFrameScheduler::ThrottlingState::kThrottled);
worker_thread.reset();
proxy.reset();
mock_main_thread_task_runner_->RunUntilIdle();
page_scheduler_->SetPageVisible(true);
......
......@@ -66,8 +66,7 @@ std::unique_ptr<WebThread>
TestingPlatformSupportWithMockScheduler::CreateThread(
const WebThreadCreationParams& params) {
std::unique_ptr<scheduler::WebThreadBase> thread =
scheduler::WebThreadBase::CreateWorkerThread(params.name,
base::Thread::Options());
scheduler::WebThreadBase::CreateWorkerThread(params);
thread->Init();
WaitableEvent event;
thread->GetTaskRunner()->PostTask(
......
......@@ -12,6 +12,7 @@ include_rules = [
"+base/optional.h",
"+base/single_thread_task_runner.h",
"+base/strings",
"+base/threading/thread.h",
"+base/time",
"+base/trace_event",
"-bindings",
......
......@@ -30,6 +30,7 @@
#include "base/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread.h"
#include <stdint.h>
......@@ -39,6 +40,7 @@ class TaskTimeObserver;
}
class WebScheduler;
class WebFrameScheduler;
// Always an integer value.
typedef uintptr_t PlatformThreadId;
......@@ -48,8 +50,14 @@ struct BLINK_PLATFORM_EXPORT WebThreadCreationParams {
WebThreadCreationParams& SetThreadName(const char* name);
// Sets a scheduler for the frame which was responsible for the creation
// of this thread.
WebThreadCreationParams& SetFrameScheduler(WebFrameScheduler*);
WebThreadType thread_type;
const char* name;
WebFrameScheduler* frame_scheduler; // NOT OWNED
base::Thread::Options thread_options;
};
// Provides an interface to an embedder-defined thread implementation.
......
......@@ -26,10 +26,9 @@ class BLINK_PLATFORM_EXPORT WebThreadBase : public WebThread {
~WebThreadBase() override;
static std::unique_ptr<WebThreadBase> CreateWorkerThread(
const char* name,
base::Thread::Options options);
const WebThreadCreationParams& params);
static std::unique_ptr<WebThreadBase> CreateCompositorThread(
base::Thread::Options options);
const WebThreadCreationParams& params);
// Must be called on utility thread.
static std::unique_ptr<WebThreadBase> InitializeUtilityThread();
......
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