| OLD | NEW |
| 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 14 matching lines...) Expand all Loading... |
| 25 */ | 25 */ |
| 26 | 26 |
| 27 #ifndef WorkerThread_h | 27 #ifndef WorkerThread_h |
| 28 #define WorkerThread_h | 28 #define WorkerThread_h |
| 29 | 29 |
| 30 #include "core/CoreExport.h" | 30 #include "core/CoreExport.h" |
| 31 #include "core/dom/ExecutionContextTask.h" | 31 #include "core/dom/ExecutionContextTask.h" |
| 32 #include "core/frame/csp/ContentSecurityPolicy.h" | 32 #include "core/frame/csp/ContentSecurityPolicy.h" |
| 33 #include "core/workers/WorkerGlobalScope.h" | 33 #include "core/workers/WorkerGlobalScope.h" |
| 34 #include "core/workers/WorkerLoaderProxy.h" | 34 #include "core/workers/WorkerLoaderProxy.h" |
| 35 #include "core/workers/WorkerOrWorkletThread.h" |
| 35 #include "wtf/Forward.h" | 36 #include "wtf/Forward.h" |
| 36 #include "wtf/Functional.h" | 37 #include "wtf/Functional.h" |
| 37 #include "wtf/OwnPtr.h" | 38 #include "wtf/OwnPtr.h" |
| 38 #include "wtf/PassRefPtr.h" | 39 #include "wtf/PassRefPtr.h" |
| 39 #include <v8.h> | 40 #include <v8.h> |
| 40 | 41 |
| 41 namespace blink { | 42 namespace blink { |
| 42 | 43 |
| 43 class InspectorTaskRunner; | 44 class InspectorTaskRunner; |
| 44 class WaitableEvent; | 45 class WaitableEvent; |
| 45 class WorkerBackingThread; | 46 class WorkerBackingThread; |
| 46 class WorkerGlobalScope; | 47 class WorkerGlobalScope; |
| 47 class WorkerInspectorController; | 48 class WorkerInspectorController; |
| 48 class WorkerReportingProxy; | 49 class WorkerReportingProxy; |
| 49 class WorkerThreadStartupData; | 50 class WorkerThreadStartupData; |
| 50 | 51 |
| 51 enum WorkerThreadStartMode { | |
| 52 DontPauseWorkerGlobalScopeOnStart, | |
| 53 PauseWorkerGlobalScopeOnStart | |
| 54 }; | |
| 55 | |
| 56 // WorkerThread is a kind of WorkerBackingThread client. Each worker mechanism | 52 // WorkerThread is a kind of WorkerBackingThread client. Each worker mechanism |
| 57 // can access the lower thread infrastructure via an implementation of this | 53 // can access the lower thread infrastructure via an implementation of this |
| 58 // abstract class. Multiple WorkerThreads can share one WorkerBackingThread. | 54 // abstract class. Multiple WorkerThreads can share one WorkerBackingThread. |
| 59 // See WorkerBackingThread.h for more details. | 55 // See WorkerBackingThread.h for more details. |
| 60 // | 56 // |
| 61 // WorkerThread start and termination must be initiated on the main thread and | 57 // WorkerThread start and termination must be initiated on the main thread and |
| 62 // an actual task is executed on the worker thread. | 58 // an actual task is executed on the worker thread. |
| 63 class CORE_EXPORT WorkerThread { | 59 class CORE_EXPORT WorkerThread : public WorkerOrWorkletThread { |
| 64 public: | 60 public: |
| 65 // Represents how this thread is terminated. | |
| 66 enum class ExitCode { | |
| 67 NotTerminated, | |
| 68 GracefullyTerminated, | |
| 69 SyncForciblyTerminated, | |
| 70 AsyncForciblyTerminated, | |
| 71 }; | |
| 72 | |
| 73 virtual ~WorkerThread(); | |
| 74 | |
| 75 // Called on the main thread. | 61 // Called on the main thread. |
| 76 void start(PassOwnPtr<WorkerThreadStartupData>); | 62 void start(PassOwnPtr<WorkerThreadStartupData>); |
| 77 void terminate(); | |
| 78 | |
| 79 // Called on the main thread. Internally calls terminateInternal() and wait | |
| 80 // (by *blocking* the calling thread) until the worker(s) is/are shut down. | |
| 81 void terminateAndWait(); | |
| 82 static void terminateAndWaitForAllWorkers(); | |
| 83 | |
| 84 virtual WorkerBackingThread& workerBackingThread() = 0; | |
| 85 virtual bool shouldAttachThreadDebugger() const { return true; } | |
| 86 v8::Isolate* isolate(); | |
| 87 | |
| 88 // Can be used to wait for this worker thread to terminate. | |
| 89 // (This is signaled on the main thread, so it's assumed to be waited on | |
| 90 // the worker context thread) | |
| 91 WaitableEvent* terminationEvent() { return m_terminationEvent.get(); } | |
| 92 | |
| 93 bool isCurrentThread(); | |
| 94 | 63 |
| 95 WorkerLoaderProxy* workerLoaderProxy() const | 64 WorkerLoaderProxy* workerLoaderProxy() const |
| 96 { | 65 { |
| 97 RELEASE_ASSERT(m_workerLoaderProxy); | 66 RELEASE_ASSERT(m_workerLoaderProxy); |
| 98 return m_workerLoaderProxy.get(); | 67 return m_workerLoaderProxy.get(); |
| 99 } | 68 } |
| 100 | 69 |
| 101 WorkerReportingProxy& workerReportingProxy() const { return m_workerReportin
gProxy; } | 70 // Factory method for creating a new worker context for the thread. |
| 102 | 71 // Called on the worker thread. |
| 103 void postTask(const WebTraceLocation&, std::unique_ptr<ExecutionContextTask>
); | 72 virtual WorkerGlobalScope* createWorkerGlobalScope(WorkerThreadStartupData*)
= 0; |
| 104 void appendDebuggerTask(std::unique_ptr<CrossThreadClosure>); | |
| 105 | |
| 106 // Runs only debugger tasks while paused in debugger, called on the worker | |
| 107 // thread. | |
| 108 void startRunningDebuggerTasksOnPause(); | |
| 109 void stopRunningDebuggerTasksOnPause(); | |
| 110 bool isRunningDebuggerTasksOnPause() const { return m_pausedInDebugger; } | |
| 111 | |
| 112 // Can be called only on the worker thread, WorkerGlobalScope is not thread | |
| 113 // safe. | |
| 114 WorkerGlobalScope* workerGlobalScope(); | |
| 115 | |
| 116 // Returns true once one of the terminate* methods is called. | |
| 117 bool terminated(); | |
| 118 | |
| 119 // Number of active worker threads. | |
| 120 static unsigned workerThreadCount(); | |
| 121 | |
| 122 PlatformThreadId platformThreadId(); | |
| 123 | |
| 124 ExitCode getExitCode(); | |
| 125 | 73 |
| 126 protected: | 74 protected: |
| 127 WorkerThread(PassRefPtr<WorkerLoaderProxy>, WorkerReportingProxy&); | 75 WorkerThread(PassRefPtr<WorkerLoaderProxy>, WorkerReportingProxy&); |
| 128 | 76 |
| 129 // Factory method for creating a new worker context for the thread. | |
| 130 // Called on the worker thread. | |
| 131 virtual WorkerGlobalScope* createWorkerGlobalScope(PassOwnPtr<WorkerThreadSt
artupData>) = 0; | |
| 132 | |
| 133 // Called on the worker thread. | 77 // Called on the worker thread. |
| 134 virtual void postInitialize() { } | 78 virtual void postInitialize() { } |
| 135 | 79 |
| 136 private: | 80 private: |
| 137 friend class WorkerThreadTest; | 81 friend class WorkerThreadTest; |
| 138 | 82 |
| 139 class ForceTerminationTask; | |
| 140 class WorkerMicrotaskRunner; | |
| 141 | |
| 142 enum class TerminationMode { | |
| 143 // Synchronously terminate the worker execution. Please be careful to | |
| 144 // use this mode, because after the synchronous termination any V8 APIs | |
| 145 // may suddenly start to return empty handles and it may cause crashes. | |
| 146 Forcible, | |
| 147 | |
| 148 // Don't synchronously terminate the worker execution. Instead, schedule | |
| 149 // a task to terminate it in case that the shutdown sequence does not | |
| 150 // start on the worker thread in a certain time period. | |
| 151 Graceful, | |
| 152 }; | |
| 153 | |
| 154 std::unique_ptr<CrossThreadClosure> createWorkerThreadTask(std::unique_ptr<E
xecutionContextTask>, bool isInstrumented); | |
| 155 | |
| 156 void terminateInternal(TerminationMode); | |
| 157 void forciblyTerminateExecution(); | |
| 158 | |
| 159 void initializeOnWorkerThread(PassOwnPtr<WorkerThreadStartupData>); | 83 void initializeOnWorkerThread(PassOwnPtr<WorkerThreadStartupData>); |
| 160 void prepareForShutdownOnWorkerThread(); | |
| 161 void performShutdownOnWorkerThread(); | |
| 162 void performTaskOnWorkerThread(std::unique_ptr<ExecutionContextTask>, bool i
sInstrumented); | |
| 163 void runDebuggerTaskOnWorkerThread(std::unique_ptr<CrossThreadClosure>); | |
| 164 void runDebuggerTaskDontWaitOnWorkerThread(); | |
| 165 | |
| 166 void setForceTerminationDelayInMsForTesting(long long forceTerminationDelayI
nMs) { m_forceTerminationDelayInMs = forceTerminationDelayInMs; } | |
| 167 | |
| 168 bool m_started = false; | |
| 169 bool m_terminated = false; | |
| 170 bool m_readyToShutdown = false; | |
| 171 bool m_pausedInDebugger = false; | |
| 172 bool m_runningDebuggerTask = false; | |
| 173 ExitCode m_exitCode = ExitCode::NotTerminated; | |
| 174 | |
| 175 long long m_forceTerminationDelayInMs; | |
| 176 | |
| 177 OwnPtr<InspectorTaskRunner> m_inspectorTaskRunner; | |
| 178 OwnPtr<WorkerMicrotaskRunner> m_microtaskRunner; | |
| 179 | |
| 180 RefPtr<WorkerLoaderProxy> m_workerLoaderProxy; | 84 RefPtr<WorkerLoaderProxy> m_workerLoaderProxy; |
| 181 WorkerReportingProxy& m_workerReportingProxy; | |
| 182 | |
| 183 // This lock protects |m_workerGlobalScope|, |m_terminated|, | |
| 184 // |m_readyToShutdown|, |m_runningDebuggerTask|, |m_exitCode| and | |
| 185 // |m_microtaskRunner|. | |
| 186 Mutex m_threadStateMutex; | |
| 187 | |
| 188 Persistent<WorkerGlobalScope> m_workerGlobalScope; | |
| 189 | |
| 190 // Signaled when the thread starts termination on the main thread. | |
| 191 OwnPtr<WaitableEvent> m_terminationEvent; | |
| 192 | |
| 193 // Signaled when the thread completes termination on the worker thread. | |
| 194 OwnPtr<WaitableEvent> m_shutdownEvent; | |
| 195 | |
| 196 // Scheduled when termination starts with TerminationMode::Force, and | |
| 197 // cancelled when the worker thread is gracefully shut down. | |
| 198 OwnPtr<ForceTerminationTask> m_scheduledForceTerminationTask; | |
| 199 }; | 85 }; |
| 200 | 86 |
| 201 } // namespace blink | 87 } // namespace blink |
| 202 | 88 |
| 203 #endif // WorkerThread_h | 89 #endif // WorkerThread_h |
| OLD | NEW |