Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(624)

Unified Diff: third_party/WebKit/Source/core/workers/WorkerThread.cpp

Issue 2806623004: Worker: Introduce per-global-scope task scheduler (Closed)
Patch Set: remove unnecessary comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/workers/WorkerThread.cpp
diff --git a/third_party/WebKit/Source/core/workers/WorkerThread.cpp b/third_party/WebKit/Source/core/workers/WorkerThread.cpp
index 93adb13be205ca7e64b9f5ce1f05983dda4d5168..8966c10d921b29417784fb9969cec339b30ca480 100644
--- a/third_party/WebKit/Source/core/workers/WorkerThread.cpp
+++ b/third_party/WebKit/Source/core/workers/WorkerThread.cpp
@@ -31,6 +31,7 @@
#include "bindings/core/v8/Microtask.h"
#include "bindings/core/v8/ScriptSourceCode.h"
#include "bindings/core/v8/WorkerOrWorkletScriptController.h"
+#include "core/dom/TaskRunnerHelper.h"
#include "core/inspector/ConsoleMessageStorage.h"
#include "core/inspector/InspectorTaskRunner.h"
#include "core/inspector/WorkerInspectorController.h"
@@ -49,6 +50,8 @@
#include "platform/WebThreadSupportingGC.h"
#include "platform/heap/SafePoint.h"
#include "platform/heap/ThreadState.h"
+#include "platform/scheduler/child/webthread_impl_for_worker_scheduler.h"
+#include "platform/scheduler/child/worker_global_scope_scheduler.h"
#include "platform/weborigin/KURL.h"
#include "platform/wtf/Functional.h"
#include "platform/wtf/Noncopyable.h"
@@ -107,12 +110,22 @@ WorkerThread::~WorkerThread() {
void WorkerThread::Start(std::unique_ptr<WorkerThreadStartupData> startup_data,
ParentFrameTaskRunners* parent_frame_task_runners) {
DCHECK(IsMainThread());
-
if (requested_to_start_)
return;
requested_to_start_ = true;
parent_frame_task_runners_ = parent_frame_task_runners;
+
+ // Synchronously initialize the per-global-scope scheduler to prevent someone
+ // from posting a task to the thread before the scheduler is ready.
+ WaitableEvent waitable_event;
+ GetWorkerBackingThread().BackingThread().PostTask(
+ BLINK_FROM_HERE,
+ CrossThreadBind(&WorkerThread::InitializeSchedulerOnWorkerThread,
+ CrossThreadUnretained(this),
+ CrossThreadUnretained(&waitable_event)));
+ waitable_event.Wait();
+
GetWorkerBackingThread().BackingThread().PostTask(
BLINK_FROM_HERE, CrossThreadBind(&WorkerThread::InitializeOnWorkerThread,
CrossThreadUnretained(this),
@@ -190,33 +203,10 @@ bool WorkerThread::IsCurrentThread() {
return GetWorkerBackingThread().BackingThread().IsCurrentThread();
}
-void WorkerThread::PostTask(const WebTraceLocation& location,
- std::unique_ptr<WTF::Closure> task) {
- DCHECK(IsCurrentThread());
- if (IsInShutdown())
- return;
- GetWorkerBackingThread().BackingThread().PostTask(
- location,
- WTF::Bind(
- &WorkerThread::PerformTaskOnWorkerThread<WTF::kSameThreadAffinity>,
- WTF::Unretained(this), WTF::Passed(std::move(task))));
-}
-
-void WorkerThread::PostTask(const WebTraceLocation& location,
- std::unique_ptr<WTF::CrossThreadClosure> task) {
- if (IsInShutdown())
- return;
- GetWorkerBackingThread().BackingThread().PostTask(
- location,
- CrossThreadBind(
- &WorkerThread::PerformTaskOnWorkerThread<WTF::kCrossThreadAffinity>,
- CrossThreadUnretained(this), WTF::Passed(std::move(task))));
-}
-
void WorkerThread::AppendDebuggerTask(
std::unique_ptr<CrossThreadClosure> task) {
DCHECK(IsMainThread());
- if (IsInShutdown())
+ if (requested_to_terminate_)
return;
inspector_task_runner_->AppendTask(CrossThreadBind(
&WorkerThread::PerformDebuggerTaskOnWorkerThread,
@@ -226,10 +216,11 @@ void WorkerThread::AppendDebuggerTask(
if (GetIsolate() && thread_state_ != ThreadState::kReadyToShutdown)
inspector_task_runner_->InterruptAndRunAllTasksDontWait(GetIsolate());
}
- GetWorkerBackingThread().BackingThread().PostTask(
- BLINK_FROM_HERE,
- CrossThreadBind(&WorkerThread::PerformDebuggerTaskDontWaitOnWorkerThread,
- CrossThreadUnretained(this)));
+ TaskRunnerHelper::Get(TaskType::kUnthrottled, this)
+ ->PostTask(BLINK_FROM_HERE,
+ CrossThreadBind(
+ &WorkerThread::PerformDebuggerTaskDontWaitOnWorkerThread,
+ CrossThreadUnretained(this)));
}
void WorkerThread::StartRunningDebuggerTasksOnPauseOnWorkerThread() {
@@ -430,16 +421,17 @@ void WorkerThread::ForciblyTerminateExecution(const MutexLocker& lock,
forcible_termination_task_handle_.Cancel();
}
-bool WorkerThread::IsInShutdown() {
- // Check if we've started termination or shutdown sequence. Avoid acquiring
- // a lock here to avoid introducing a risk of deadlock. Note that accessing
- // |m_requestedToTerminate| on the main thread or |m_threadState| on the
- // worker thread is safe as the flag is set only on the thread.
- if (IsMainThread() && requested_to_terminate_)
- return true;
- if (IsCurrentThread() && thread_state_ == ThreadState::kReadyToShutdown)
- return true;
- return false;
+void WorkerThread::InitializeSchedulerOnWorkerThread(
+ WaitableEvent* waitable_event) {
+ DCHECK(IsCurrentThread());
+ DCHECK(!global_scope_scheduler_);
+ scheduler::WebThreadImplForWorkerScheduler& web_thread_for_worker =
+ static_cast<scheduler::WebThreadImplForWorkerScheduler&>(
+ GetWorkerBackingThread().BackingThread().PlatformThread());
+ global_scope_scheduler_ =
+ WTF::MakeUnique<scheduler::WorkerGlobalScopeScheduler>(
+ web_thread_for_worker.GetWorkerScheduler());
+ waitable_event->Signal();
}
void WorkerThread::InitializeOnWorkerThread(
@@ -536,6 +528,7 @@ void WorkerThread::PrepareForShutdownOnWorkerThread() {
worker_inspector_controller_.Clear();
}
GlobalScope()->Dispose();
+ global_scope_scheduler_->Dispose();
console_message_storage_.Clear();
GetWorkerBackingThread().BackingThread().RemoveTaskObserver(this);
}
@@ -564,22 +557,6 @@ void WorkerThread::PerformShutdownOnWorkerThread() {
shutdown_event_->Signal();
}
-template <WTF::FunctionThreadAffinity threadAffinity>
-void WorkerThread::PerformTaskOnWorkerThread(
- std::unique_ptr<Function<void(), threadAffinity>> task) {
- DCHECK(IsCurrentThread());
- if (thread_state_ != ThreadState::kRunning)
- return;
-
- {
- DEFINE_THREAD_SAFE_STATIC_LOCAL(
- CustomCountHistogram, scoped_us_counter,
- new CustomCountHistogram("WorkerThread.Task.Time", 0, 10000000, 50));
- ScopedUsHistogramTimer timer(scoped_us_counter);
- (*task)();
- }
-}
-
void WorkerThread::PerformDebuggerTaskOnWorkerThread(
std::unique_ptr<CrossThreadClosure> task) {
DCHECK(IsCurrentThread());

Powered by Google App Engine
This is Rietveld 408576698