diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2018-05-15 10:20:33 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2018-05-15 10:28:57 +0000 |
commit | d17ea114e5ef69ad5d5d7413280a13e6428098aa (patch) | |
tree | 2c01a75df69f30d27b1432467cfe7c1467a498da /chromium/third_party/blink/renderer/platform/scheduler/child/webthread_impl_for_worker_scheduler.cc | |
parent | 8c5c43c7b138c9b4b0bf56d946e61d3bbc111bec (diff) | |
download | qtwebengine-chromium-d17ea114e5ef69ad5d5d7413280a13e6428098aa.tar.gz |
BASELINE: Update Chromium to 67.0.3396.47
Change-Id: Idcb1341782e417561a2473eeecc82642dafda5b7
Reviewed-by: Michal Klocek <michal.klocek@qt.io>
Diffstat (limited to 'chromium/third_party/blink/renderer/platform/scheduler/child/webthread_impl_for_worker_scheduler.cc')
-rw-r--r-- | chromium/third_party/blink/renderer/platform/scheduler/child/webthread_impl_for_worker_scheduler.cc | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/chromium/third_party/blink/renderer/platform/scheduler/child/webthread_impl_for_worker_scheduler.cc b/chromium/third_party/blink/renderer/platform/scheduler/child/webthread_impl_for_worker_scheduler.cc new file mode 100644 index 00000000000..8b1777f7f69 --- /dev/null +++ b/chromium/third_party/blink/renderer/platform/scheduler/child/webthread_impl_for_worker_scheduler.cc @@ -0,0 +1,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 |