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

Side by Side Diff: third_party/WebKit/Source/core/workers/WorkerThread.h

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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 19 matching lines...) Expand all
30 #include <memory> 30 #include <memory>
31 31
32 #include "core/CoreExport.h" 32 #include "core/CoreExport.h"
33 #include "core/frame/csp/ContentSecurityPolicy.h" 33 #include "core/frame/csp/ContentSecurityPolicy.h"
34 #include "core/workers/ParentFrameTaskRunners.h" 34 #include "core/workers/ParentFrameTaskRunners.h"
35 #include "core/workers/WorkerLoaderProxy.h" 35 #include "core/workers/WorkerLoaderProxy.h"
36 #include "core/workers/WorkerThreadLifecycleObserver.h" 36 #include "core/workers/WorkerThreadLifecycleObserver.h"
37 #include "platform/LifecycleNotifier.h" 37 #include "platform/LifecycleNotifier.h"
38 #include "platform/WaitableEvent.h" 38 #include "platform/WaitableEvent.h"
39 #include "platform/WebTaskRunner.h" 39 #include "platform/WebTaskRunner.h"
40 #include "platform/scheduler/child/worker_global_scope_scheduler.h"
40 #include "platform/wtf/Forward.h" 41 #include "platform/wtf/Forward.h"
41 #include "platform/wtf/Functional.h" 42 #include "platform/wtf/Functional.h"
42 #include "platform/wtf/PassRefPtr.h" 43 #include "platform/wtf/PassRefPtr.h"
43 #include "public/platform/WebThread.h" 44 #include "public/platform/WebThread.h"
44 #include "v8/include/v8.h" 45 #include "v8/include/v8.h"
45 46
46 namespace blink { 47 namespace blink {
47 48
48 class ConsoleMessageStorage; 49 class ConsoleMessageStorage;
49 class InspectorTaskRunner; 50 class InspectorTaskRunner;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 // 86 //
86 // WorkerThread start and termination must be initiated on the main thread and 87 // WorkerThread start and termination must be initiated on the main thread and
87 // an actual task is executed on the worker thread. 88 // an actual task is executed on the worker thread.
88 // 89 //
89 // When termination starts, (debugger) tasks on WorkerThread are handled as 90 // When termination starts, (debugger) tasks on WorkerThread are handled as
90 // follows: 91 // follows:
91 // - A running task may finish unless a forcible termination task interrupts. 92 // - A running task may finish unless a forcible termination task interrupts.
92 // If the running task is for debugger, it's guaranteed to finish without 93 // If the running task is for debugger, it's guaranteed to finish without
93 // any interruptions. 94 // any interruptions.
94 // - Queued tasks never run. 95 // - Queued tasks never run.
95 // - postTask() and appendDebuggerTask() reject posting new tasks.
96 class CORE_EXPORT WorkerThread : public WebThread::TaskObserver { 96 class CORE_EXPORT WorkerThread : public WebThread::TaskObserver {
97 public: 97 public:
98 // Represents how this thread is terminated. Used for UMA. Append only. 98 // Represents how this thread is terminated. Used for UMA. Append only.
99 enum class ExitCode { 99 enum class ExitCode {
100 kNotTerminated, 100 kNotTerminated,
101 kGracefullyTerminated, 101 kGracefullyTerminated,
102 kSyncForciblyTerminated, 102 kSyncForciblyTerminated,
103 kAsyncForciblyTerminated, 103 kAsyncForciblyTerminated,
104 kLastEnum, 104 kLastEnum,
105 }; 105 };
(...skipping 24 matching lines...) Expand all
130 130
131 WorkerLoaderProxy* GetWorkerLoaderProxy() const { 131 WorkerLoaderProxy* GetWorkerLoaderProxy() const {
132 RELEASE_ASSERT(worker_loader_proxy_); 132 RELEASE_ASSERT(worker_loader_proxy_);
133 return worker_loader_proxy_.Get(); 133 return worker_loader_proxy_.Get();
134 } 134 }
135 135
136 WorkerReportingProxy& GetWorkerReportingProxy() const { 136 WorkerReportingProxy& GetWorkerReportingProxy() const {
137 return worker_reporting_proxy_; 137 return worker_reporting_proxy_;
138 } 138 }
139 139
140 void PostTask(const WebTraceLocation&, std::unique_ptr<WTF::Closure>);
141 void PostTask(const WebTraceLocation&,
142 std::unique_ptr<WTF::CrossThreadClosure>);
143 void AppendDebuggerTask(std::unique_ptr<CrossThreadClosure>); 140 void AppendDebuggerTask(std::unique_ptr<CrossThreadClosure>);
144 141
145 // Runs only debugger tasks while paused in debugger. 142 // Runs only debugger tasks while paused in debugger.
146 void StartRunningDebuggerTasksOnPauseOnWorkerThread(); 143 void StartRunningDebuggerTasksOnPauseOnWorkerThread();
147 void StopRunningDebuggerTasksOnPauseOnWorkerThread(); 144 void StopRunningDebuggerTasksOnPauseOnWorkerThread();
148 145
149 // Can be called only on the worker thread, WorkerOrWorkletGlobalScope 146 // Can be called only on the worker thread, WorkerOrWorkletGlobalScope
150 // and WorkerInspectorController are not thread safe. 147 // and WorkerInspectorController are not thread safe.
151 WorkerOrWorkletGlobalScope* GlobalScope(); 148 WorkerOrWorkletGlobalScope* GlobalScope();
152 WorkerInspectorController* GetWorkerInspectorController(); 149 WorkerInspectorController* GetWorkerInspectorController();
(...skipping 16 matching lines...) Expand all
169 PlatformThreadId GetPlatformThreadId(); 166 PlatformThreadId GetPlatformThreadId();
170 167
171 bool IsForciblyTerminated(); 168 bool IsForciblyTerminated();
172 169
173 void WaitForShutdownForTesting() { shutdown_event_->Wait(); } 170 void WaitForShutdownForTesting() { shutdown_event_->Wait(); }
174 171
175 ParentFrameTaskRunners* GetParentFrameTaskRunners() const { 172 ParentFrameTaskRunners* GetParentFrameTaskRunners() const {
176 return parent_frame_task_runners_.Get(); 173 return parent_frame_task_runners_.Get();
177 } 174 }
178 175
176 scheduler::WorkerGlobalScopeScheduler* GetGlobalScopeScheduler() const {
177 return global_scope_scheduler_.get();
178 }
179
179 protected: 180 protected:
180 WorkerThread(PassRefPtr<WorkerLoaderProxy>, WorkerReportingProxy&); 181 WorkerThread(PassRefPtr<WorkerLoaderProxy>, WorkerReportingProxy&);
181 182
182 // Factory method for creating a new worker context for the thread. 183 // Factory method for creating a new worker context for the thread.
183 // Called on the worker thread. 184 // Called on the worker thread.
184 virtual WorkerOrWorkletGlobalScope* CreateWorkerGlobalScope( 185 virtual WorkerOrWorkletGlobalScope* CreateWorkerGlobalScope(
185 std::unique_ptr<WorkerThreadStartupData>) = 0; 186 std::unique_ptr<WorkerThreadStartupData>) = 0;
186 187
187 // Returns true when this WorkerThread owns the associated 188 // Returns true when this WorkerThread owns the associated
188 // WorkerBackingThread exclusively. If this function returns true, the 189 // WorkerBackingThread exclusively. If this function returns true, the
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 // thread. This task is expected to run when the shutdown sequence does not 237 // thread. This task is expected to run when the shutdown sequence does not
237 // start in a certain time period because of an inifite loop in the JS 238 // start in a certain time period because of an inifite loop in the JS
238 // execution context etc. When the shutdown sequence is started before this 239 // execution context etc. When the shutdown sequence is started before this
239 // task runs, the task is simply cancelled. 240 // task runs, the task is simply cancelled.
240 void MayForciblyTerminateExecution(); 241 void MayForciblyTerminateExecution();
241 242
242 // Forcibly terminates the worker execution. This must be called with 243 // Forcibly terminates the worker execution. This must be called with
243 // |m_threadStateMutex| acquired. 244 // |m_threadStateMutex| acquired.
244 void ForciblyTerminateExecution(const MutexLocker&, ExitCode); 245 void ForciblyTerminateExecution(const MutexLocker&, ExitCode);
245 246
246 // Returns true if termination or shutdown sequence has started. This is 247 void InitializeSchedulerOnWorkerThread(WaitableEvent*);
247 // thread safe.
248 // Note that this returns false when the sequence has already started but it
249 // hasn't been notified to the calling thread.
250 bool IsInShutdown();
251
252 void InitializeOnWorkerThread(std::unique_ptr<WorkerThreadStartupData>); 248 void InitializeOnWorkerThread(std::unique_ptr<WorkerThreadStartupData>);
253 void PrepareForShutdownOnWorkerThread(); 249 void PrepareForShutdownOnWorkerThread();
254 void PerformShutdownOnWorkerThread(); 250 void PerformShutdownOnWorkerThread();
255 template <WTF::FunctionThreadAffinity threadAffinity> 251 template <WTF::FunctionThreadAffinity threadAffinity>
256 void PerformTaskOnWorkerThread( 252 void PerformTaskOnWorkerThread(
257 std::unique_ptr<Function<void(), threadAffinity>> task); 253 std::unique_ptr<Function<void(), threadAffinity>> task);
258 void PerformDebuggerTaskOnWorkerThread(std::unique_ptr<CrossThreadClosure>); 254 void PerformDebuggerTaskOnWorkerThread(std::unique_ptr<CrossThreadClosure>);
259 void PerformDebuggerTaskDontWaitOnWorkerThread(); 255 void PerformDebuggerTaskDontWaitOnWorkerThread();
260 256
261 // These must be called with |m_threadStateMutex| acquired. 257 // These must be called with |m_threadStateMutex| acquired.
(...skipping 25 matching lines...) Expand all
287 283
288 ThreadState thread_state_ = ThreadState::kNotStarted; 284 ThreadState thread_state_ = ThreadState::kNotStarted;
289 ExitCode exit_code_ = ExitCode::kNotTerminated; 285 ExitCode exit_code_ = ExitCode::kNotTerminated;
290 286
291 long long forcible_termination_delay_in_ms_; 287 long long forcible_termination_delay_in_ms_;
292 288
293 std::unique_ptr<InspectorTaskRunner> inspector_task_runner_; 289 std::unique_ptr<InspectorTaskRunner> inspector_task_runner_;
294 290
295 RefPtr<WorkerLoaderProxy> worker_loader_proxy_; 291 RefPtr<WorkerLoaderProxy> worker_loader_proxy_;
296 WorkerReportingProxy& worker_reporting_proxy_; 292 WorkerReportingProxy& worker_reporting_proxy_;
293
297 CrossThreadPersistent<ParentFrameTaskRunners> parent_frame_task_runners_; 294 CrossThreadPersistent<ParentFrameTaskRunners> parent_frame_task_runners_;
298 295
296 // Tasks managed by this scheduler are canceled when the global scope is
297 // closed.
298 std::unique_ptr<scheduler::WorkerGlobalScopeScheduler>
299 global_scope_scheduler_;
300
299 // This lock protects |m_globalScope|, |m_requestedToTerminate|, 301 // This lock protects |m_globalScope|, |m_requestedToTerminate|,
300 // |m_threadState|, |m_runningDebuggerTask| and |m_exitCode|. 302 // |m_threadState|, |m_runningDebuggerTask| and |m_exitCode|.
301 Mutex thread_state_mutex_; 303 Mutex thread_state_mutex_;
302 304
303 CrossThreadPersistent<ConsoleMessageStorage> console_message_storage_; 305 CrossThreadPersistent<ConsoleMessageStorage> console_message_storage_;
304 CrossThreadPersistent<WorkerOrWorkletGlobalScope> global_scope_; 306 CrossThreadPersistent<WorkerOrWorkletGlobalScope> global_scope_;
305 CrossThreadPersistent<WorkerInspectorController> worker_inspector_controller_; 307 CrossThreadPersistent<WorkerInspectorController> worker_inspector_controller_;
306 308
307 // Signaled when the thread completes termination on the worker thread. 309 // Signaled when the thread completes termination on the worker thread.
308 std::unique_ptr<WaitableEvent> shutdown_event_; 310 std::unique_ptr<WaitableEvent> shutdown_event_;
309 311
310 // Used to cancel a scheduled forcible termination task. See 312 // Used to cancel a scheduled forcible termination task. See
311 // mayForciblyTerminateExecution() for details. 313 // mayForciblyTerminateExecution() for details.
312 TaskHandle forcible_termination_task_handle_; 314 TaskHandle forcible_termination_task_handle_;
313 315
314 // Created on the main thread heap, but will be accessed cross-thread 316 // Created on the main thread heap, but will be accessed cross-thread
315 // when worker thread posts tasks. 317 // when worker thread posts tasks.
316 CrossThreadPersistent<WorkerThreadLifecycleContext> 318 CrossThreadPersistent<WorkerThreadLifecycleContext>
317 worker_thread_lifecycle_context_; 319 worker_thread_lifecycle_context_;
318 }; 320 };
319 321
320 } // namespace blink 322 } // namespace blink
321 323
322 #endif // WorkerThread_h 324 #endif // WorkerThread_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698