Roll src/third_party/WebKit 605a979:06cb9e9 (svn 202556:202558)
[chromium-blink-merge.git] / components / scheduler / child / webthread_impl_for_worker_scheduler.cc
blob4f960783ee5f8d46e77296c75aac4b1db3673381
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/scheduler/child/webthread_impl_for_worker_scheduler.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/synchronization/waitable_event.h"
11 #include "components/scheduler/child/scheduler_task_runner_delegate_impl.h"
12 #include "components/scheduler/child/task_queue.h"
13 #include "components/scheduler/child/web_scheduler_impl.h"
14 #include "components/scheduler/child/web_task_runner_impl.h"
15 #include "components/scheduler/child/worker_scheduler_impl.h"
16 #include "third_party/WebKit/public/platform/WebTraceLocation.h"
18 namespace scheduler {
20 WebThreadImplForWorkerScheduler::WebThreadImplForWorkerScheduler(
21 const char* name)
22 : thread_(new base::Thread(name)) {
23 thread_->Start();
24 thread_task_runner_ = thread_->task_runner();
26 base::WaitableEvent completion(false, false);
27 thread_task_runner_->PostTask(
28 FROM_HERE, base::Bind(&WebThreadImplForWorkerScheduler::InitOnThread,
29 base::Unretained(this), &completion));
30 completion.Wait();
33 WebThreadImplForWorkerScheduler::~WebThreadImplForWorkerScheduler() {
34 base::WaitableEvent completion(false, false);
35 // Restore the original task runner so that the thread can tear itself down.
36 thread_task_runner_->PostTask(
37 FROM_HERE,
38 base::Bind(&WebThreadImplForWorkerScheduler::RestoreTaskRunnerOnThread,
39 base::Unretained(this), &completion));
40 completion.Wait();
41 thread_->Stop();
44 void WebThreadImplForWorkerScheduler::InitOnThread(
45 base::WaitableEvent* completion) {
46 task_runner_delegate_ =
47 SchedulerTaskRunnerDelegateImpl::Create(thread_->message_loop());
48 worker_scheduler_ = WorkerScheduler::Create(task_runner_delegate_);
49 worker_scheduler_->Init();
50 task_runner_ = worker_scheduler_->DefaultTaskRunner();
51 idle_task_runner_ = worker_scheduler_->IdleTaskRunner();
52 web_scheduler_.reset(new WebSchedulerImpl(
53 worker_scheduler_.get(), worker_scheduler_->IdleTaskRunner(),
54 worker_scheduler_->DefaultTaskRunner(),
55 worker_scheduler_->DefaultTaskRunner()));
56 base::MessageLoop::current()->AddDestructionObserver(this);
57 web_task_runner_ = make_scoped_ptr(new WebTaskRunnerImpl(task_runner_));
58 completion->Signal();
61 void WebThreadImplForWorkerScheduler::RestoreTaskRunnerOnThread(
62 base::WaitableEvent* completion) {
63 task_runner_delegate_->RestoreDefaultTaskRunner();
64 completion->Signal();
67 void WebThreadImplForWorkerScheduler::WillDestroyCurrentMessageLoop() {
68 task_runner_ = nullptr;
69 idle_task_runner_ = nullptr;
70 web_scheduler_.reset();
71 worker_scheduler_.reset();
74 blink::PlatformThreadId WebThreadImplForWorkerScheduler::threadId() const {
75 return thread_->GetThreadId();
78 blink::WebScheduler* WebThreadImplForWorkerScheduler::scheduler() const {
79 return web_scheduler_.get();
82 base::SingleThreadTaskRunner* WebThreadImplForWorkerScheduler::TaskRunner()
83 const {
84 return task_runner_.get();
87 SingleThreadIdleTaskRunner* WebThreadImplForWorkerScheduler::IdleTaskRunner()
88 const {
89 return idle_task_runner_.get();
92 blink::WebTaskRunner* WebThreadImplForWorkerScheduler::taskRunner() {
93 return web_task_runner_.get();
96 void WebThreadImplForWorkerScheduler::AddTaskObserverInternal(
97 base::MessageLoop::TaskObserver* observer) {
98 worker_scheduler_->AddTaskObserver(observer);
101 void WebThreadImplForWorkerScheduler::RemoveTaskObserverInternal(
102 base::MessageLoop::TaskObserver* observer) {
103 worker_scheduler_->RemoveTaskObserver(observer);
106 } // namespace scheduler