summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/scheduler/child/webthread_impl_for_worker_scheduler.cc
blob: 8b1777f7f692cc6566e6783c90142f4a1c395b93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2015 The Chromium 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 "third_party/blink/renderer/platform/scheduler/child/webthread_impl_for_worker_scheduler.h"

#include <memory>
#include "base/bind.h"
#include "base/location.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/waitable_event.h"
#include "base/time/default_tick_clock.h"
#include "third_party/blink/renderer/platform/scheduler/base/task_queue.h"
#include "third_party/blink/renderer/platform/scheduler/child/web_scheduler_impl.h"
#include "third_party/blink/renderer/platform/scheduler/child/worker_scheduler_proxy.h"
#include "third_party/blink/renderer/platform/scheduler/worker/worker_thread_scheduler.h"

namespace blink {
namespace scheduler {

WebThreadImplForWorkerScheduler::WebThreadImplForWorkerScheduler(
    const WebThreadCreationParams& params)
    : thread_(new base::Thread(params.name ? params.name : std::string())),
      thread_type_(params.thread_type),
      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();
}

void WebThreadImplForWorkerScheduler::Init() {
  base::WaitableEvent completion(
      base::WaitableEvent::ResetPolicy::AUTOMATIC,
      base::WaitableEvent::InitialState::NOT_SIGNALED);
  thread_task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&WebThreadImplForWorkerScheduler::InitOnThread,
                                base::Unretained(this), &completion));
  completion.Wait();
}

WebThreadImplForWorkerScheduler::~WebThreadImplForWorkerScheduler() {
  // We want to avoid blocking main thread when the thread was already
  // shut down, but calling ShutdownOnThread twice does not cause any problems.
  if (!was_shutdown_on_thread_.IsSet()) {
    base::WaitableEvent completion(
        base::WaitableEvent::ResetPolicy::AUTOMATIC,
        base::WaitableEvent::InitialState::NOT_SIGNALED);
    thread_task_runner_->PostTask(
        FROM_HERE,
        base::BindOnce(&WebThreadImplForWorkerScheduler::ShutdownOnThread,
                       base::Unretained(this), &completion));
    completion.Wait();
  }
  thread_->Stop();
}

void WebThreadImplForWorkerScheduler::InitOnThread(
    base::WaitableEvent* completion) {
  // TODO(alexclarke): Do we need to unify virtual time for workers and the
  // main thread?
  non_main_thread_scheduler_ = CreateNonMainThreadScheduler();
  non_main_thread_scheduler_->Init();
  task_queue_ = non_main_thread_scheduler_->DefaultTaskQueue();
  idle_task_runner_ = non_main_thread_scheduler_->IdleTaskRunner();
  web_scheduler_.reset(
      new WebSchedulerImpl(non_main_thread_scheduler_.get(),
                           non_main_thread_scheduler_->IdleTaskRunner(),
                           non_main_thread_scheduler_->DefaultTaskQueue()));
  base::MessageLoop::current()->AddDestructionObserver(this);
  completion->Signal();
}

void WebThreadImplForWorkerScheduler::ShutdownOnThread(
    base::WaitableEvent* completion) {
  was_shutdown_on_thread_.Set();

  task_queue_ = nullptr;
  idle_task_runner_ = nullptr;
  web_scheduler_ = nullptr;
  non_main_thread_scheduler_ = nullptr;

  if (completion)
    completion->Signal();
}

std::unique_ptr<NonMainThreadScheduler>
WebThreadImplForWorkerScheduler::CreateNonMainThreadScheduler() {
  return NonMainThreadScheduler::Create(thread_type_,
                                        worker_scheduler_proxy_.get());
}

void WebThreadImplForWorkerScheduler::WillDestroyCurrentMessageLoop() {
  ShutdownOnThread(nullptr);
}

blink::PlatformThreadId WebThreadImplForWorkerScheduler::ThreadId() const {
  return thread_->GetThreadId();
}

blink::WebScheduler* WebThreadImplForWorkerScheduler::Scheduler() const {
  return web_scheduler_.get();
}

SingleThreadIdleTaskRunner* WebThreadImplForWorkerScheduler::GetIdleTaskRunner()
    const {
  return idle_task_runner_.get();
}

scoped_refptr<base::SingleThreadTaskRunner>
WebThreadImplForWorkerScheduler::GetTaskRunner() const {
  return task_queue_;
}

void WebThreadImplForWorkerScheduler::AddTaskObserverInternal(
    base::MessageLoop::TaskObserver* observer) {
  non_main_thread_scheduler_->AddTaskObserver(observer);
}

void WebThreadImplForWorkerScheduler::RemoveTaskObserverInternal(
    base::MessageLoop::TaskObserver* observer) {
  non_main_thread_scheduler_->RemoveTaskObserver(observer);
}

}  // namespace scheduler
}  // namespace blink