 Chromium Code Reviews
 Chromium Code Reviews Issue 2806623004:
  Worker: Introduce per-global-scope task scheduler  (Closed)
    
  
    Issue 2806623004:
  Worker: Introduce per-global-scope task scheduler  (Closed) 
  | 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..623dee6079206ef9f197eb1036e7bb98ce7bb1cc 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" | 
| @@ -107,12 +108,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 +201,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 +214,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 +419,12 @@ 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()); | 
| 
kinuko
2017/04/17 05:01:11
nit: DCHECK(!global_scope_scheduler_)
 
nhiroki
2017/04/17 11:03:46
Done.
 | 
| + global_scope_scheduler_ = | 
| + GetWorkerBackingThread().CreateGlobalScopeScheduler(); | 
| + waitable_event->Signal(); | 
| } | 
| void WorkerThread::InitializeOnWorkerThread( | 
| @@ -536,6 +521,7 @@ void WorkerThread::PrepareForShutdownOnWorkerThread() { | 
| worker_inspector_controller_.Clear(); | 
| } | 
| GlobalScope()->Dispose(); | 
| + global_scope_scheduler_->Dispose(); | 
| console_message_storage_.Clear(); | 
| GetWorkerBackingThread().BackingThread().RemoveTaskObserver(this); | 
| } | 
| @@ -564,22 +550,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)); | 
| 
kinuko
2017/04/17 05:01:11
Are we deprecating this histogram?
 
nhiroki
2017/04/17 11:03:46
Oh, good catch.
I moved this into WorkerScheduler
 | 
| - ScopedUsHistogramTimer timer(scoped_us_counter); | 
| - (*task)(); | 
| - } | 
| -} | 
| - | 
| void WorkerThread::PerformDebuggerTaskOnWorkerThread( | 
| std::unique_ptr<CrossThreadClosure> task) { | 
| DCHECK(IsCurrentThread()); |