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

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

Issue 2806623004: Worker: Introduce per-global-scope task scheduler (Closed)
Patch Set: rebase 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..e0ae3b078b48a8796acff7f9d59159beba9483d4 100644
--- a/third_party/WebKit/Source/core/workers/WorkerThread.cpp
+++ b/third_party/WebKit/Source/core/workers/WorkerThread.cpp
@@ -107,16 +107,21 @@ 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::InitializeOnWorkerThread,
CrossThreadUnretained(this),
- WTF::Passed(std::move(startup_data))));
+ WTF::Passed(std::move(startup_data)),
+ CrossThreadUnretained(&waitable_event)));
+ waitable_event.Wait();
}
void WorkerThread::Terminate() {
@@ -190,33 +195,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 +208,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)));
+ worker_task_runners_->Get(TaskType::kUnthrottled)
+ ->PostTask(BLINK_FROM_HERE,
+ CrossThreadBind(
+ &WorkerThread::PerformDebuggerTaskDontWaitOnWorkerThread,
+ CrossThreadUnretained(this)));
}
void WorkerThread::StartRunningDebuggerTasksOnPauseOnWorkerThread() {
@@ -430,20 +413,9 @@ 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::InitializeOnWorkerThread(
- std::unique_ptr<WorkerThreadStartupData> startup_data) {
+ std::unique_ptr<WorkerThreadStartupData> startup_data,
+ WaitableEvent* global_scope_creation_event) {
DCHECK(IsCurrentThread());
DCHECK_EQ(ThreadState::kNotStarted, thread_state_);
@@ -473,7 +445,12 @@ void WorkerThread::InitializeOnWorkerThread(
}
console_message_storage_ = new ConsoleMessageStorage();
+ global_scope_scheduler_ =
+ GetWorkerBackingThread().CreateGlobalScopeScheduler();
global_scope_ = CreateWorkerGlobalScope(std::move(startup_data));
+ worker_task_runners_ = WorkerTaskRunners::Create(global_scope_.Get());
+ global_scope_creation_event->Signal();
+
worker_reporting_proxy_.DidCreateWorkerGlobalScope(GlobalScope());
worker_inspector_controller_ = WorkerInspectorController::Create(this);
@@ -536,6 +513,7 @@ void WorkerThread::PrepareForShutdownOnWorkerThread() {
worker_inspector_controller_.Clear();
}
GlobalScope()->Dispose();
+ global_scope_scheduler_->Dispose();
console_message_storage_.Clear();
GetWorkerBackingThread().BackingThread().RemoveTaskObserver(this);
}
@@ -564,22 +542,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