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"
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/worker_scheduler_impl.h"
15 #include "third_party/WebKit/public/platform/WebTraceLocation.h"
19 WebThreadImplForWorkerScheduler::WebThreadImplForWorkerScheduler(
21 : thread_(new base::Thread(name
)) {
23 thread_task_runner_
= thread_
->task_runner();
25 base::WaitableEvent
completion(false, false);
26 thread_task_runner_
->PostTask(
27 FROM_HERE
, base::Bind(&WebThreadImplForWorkerScheduler::InitOnThread
,
28 base::Unretained(this), &completion
));
32 WebThreadImplForWorkerScheduler::~WebThreadImplForWorkerScheduler() {
33 base::WaitableEvent
completion(false, false);
34 // Restore the original task runner so that the thread can tear itself down.
35 thread_task_runner_
->PostTask(
37 base::Bind(&WebThreadImplForWorkerScheduler::RestoreTaskRunnerOnThread
,
38 base::Unretained(this), &completion
));
43 void WebThreadImplForWorkerScheduler::InitOnThread(
44 base::WaitableEvent
* completion
) {
45 task_runner_delegate_
=
46 SchedulerTaskRunnerDelegateImpl::Create(thread_
->message_loop());
47 worker_scheduler_
= WorkerScheduler::Create(task_runner_delegate_
);
48 worker_scheduler_
->Init();
49 task_runner_
= worker_scheduler_
->DefaultTaskRunner();
50 idle_task_runner_
= worker_scheduler_
->IdleTaskRunner();
51 web_scheduler_
.reset(new WebSchedulerImpl(
52 worker_scheduler_
.get(), worker_scheduler_
->IdleTaskRunner(),
53 worker_scheduler_
->DefaultTaskRunner(),
54 worker_scheduler_
->DefaultTaskRunner()));
55 base::MessageLoop::current()->AddDestructionObserver(this);
59 void WebThreadImplForWorkerScheduler::RestoreTaskRunnerOnThread(
60 base::WaitableEvent
* completion
) {
61 task_runner_delegate_
->RestoreDefaultTaskRunner();
65 void WebThreadImplForWorkerScheduler::WillDestroyCurrentMessageLoop() {
66 task_runner_
= nullptr;
67 idle_task_runner_
= nullptr;
68 web_scheduler_
.reset();
69 worker_scheduler_
.reset();
72 blink::PlatformThreadId
WebThreadImplForWorkerScheduler::threadId() const {
73 return thread_
->GetThreadId();
76 blink::WebScheduler
* WebThreadImplForWorkerScheduler::scheduler() const {
77 return web_scheduler_
.get();
80 base::SingleThreadTaskRunner
* WebThreadImplForWorkerScheduler::TaskRunner()
82 return task_runner_
.get();
85 SingleThreadIdleTaskRunner
* WebThreadImplForWorkerScheduler::IdleTaskRunner()
87 return idle_task_runner_
.get();
90 void WebThreadImplForWorkerScheduler::AddTaskObserverInternal(
91 base::MessageLoop::TaskObserver
* observer
) {
92 worker_scheduler_
->AddTaskObserver(observer
);
95 void WebThreadImplForWorkerScheduler::RemoveTaskObserverInternal(
96 base::MessageLoop::TaskObserver
* observer
) {
97 worker_scheduler_
->RemoveTaskObserver(observer
);
100 } // namespace scheduler