| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 * | |
| 25 */ | |
| 26 | |
| 27 #ifndef WorkerThread_h | |
| 28 #define WorkerThread_h | |
| 29 | |
| 30 #include "core/CoreExport.h" | |
| 31 #include "core/dom/ExecutionContextTask.h" | |
| 32 #include "core/frame/csp/ContentSecurityPolicy.h" | |
| 33 #include "core/workers/WorkerGlobalScope.h" | |
| 34 #include "core/workers/WorkerLoaderProxy.h" | |
| 35 #include "platform/WebThreadSupportingGC.h" | |
| 36 #include "platform/weborigin/SecurityOrigin.h" | |
| 37 #include "wtf/Forward.h" | |
| 38 #include "wtf/MessageQueue.h" | |
| 39 #include "wtf/OwnPtr.h" | |
| 40 #include "wtf/PassRefPtr.h" | |
| 41 #include "wtf/RefCounted.h" | |
| 42 #include <v8.h> | |
| 43 | |
| 44 namespace blink { | |
| 45 | |
| 46 class WebWaitableEvent; | |
| 47 class WorkerGlobalScope; | |
| 48 class WorkerInspectorController; | |
| 49 class WorkerReportingProxy; | |
| 50 class WorkerSharedTimer; | |
| 51 class WorkerThreadShutdownFinishTask; | |
| 52 class WorkerThreadStartupData; | |
| 53 class WorkerThreadTask; | |
| 54 | |
| 55 enum WorkerThreadStartMode { | |
| 56 DontPauseWorkerGlobalScopeOnStart, | |
| 57 PauseWorkerGlobalScopeOnStart | |
| 58 }; | |
| 59 | |
| 60 // TODO(sadrul): Rename to WorkerScript. | |
| 61 class CORE_EXPORT WorkerThread : public RefCounted<WorkerThread> { | |
| 62 public: | |
| 63 virtual ~WorkerThread(); | |
| 64 | |
| 65 virtual void start(); | |
| 66 virtual void stop(); | |
| 67 | |
| 68 // Returns the thread this worker runs on. Some implementations can create | |
| 69 // a new thread on the first call (e.g. shared, dedicated workers), whereas | |
| 70 // some implementations can use an existing thread that is already being | |
| 71 // used by other workers (e.g. compositor workers). | |
| 72 virtual WebThreadSupportingGC& backingThread() = 0; | |
| 73 | |
| 74 virtual void didStartRunLoop(); | |
| 75 virtual void didStopRunLoop(); | |
| 76 | |
| 77 v8::Isolate* isolate() const { return m_isolate; } | |
| 78 | |
| 79 // Can be used to wait for this worker thread to shut down. | |
| 80 // (This is signalled on the main thread, so it's assumed to be waited on th
e worker context thread) | |
| 81 WebWaitableEvent* shutdownEvent() { return m_shutdownEvent.get(); } | |
| 82 | |
| 83 WebWaitableEvent* terminationEvent() { return m_terminationEvent.get(); } | |
| 84 void terminateAndWait(); | |
| 85 static void terminateAndWaitForAllWorkers(); | |
| 86 | |
| 87 bool isCurrentThread(); | |
| 88 WorkerLoaderProxy* workerLoaderProxy() const | |
| 89 { | |
| 90 RELEASE_ASSERT(m_workerLoaderProxy); | |
| 91 return m_workerLoaderProxy.get(); | |
| 92 } | |
| 93 | |
| 94 WorkerReportingProxy& workerReportingProxy() const { return m_workerReportin
gProxy; } | |
| 95 | |
| 96 void postTask(const WebTraceLocation&, PassOwnPtr<ExecutionContextTask>); | |
| 97 void postDebuggerTask(const WebTraceLocation&, PassOwnPtr<ExecutionContextTa
sk>); | |
| 98 | |
| 99 enum WaitMode { WaitForMessage, DontWaitForMessage }; | |
| 100 MessageQueueWaitResult runDebuggerTask(WaitMode = WaitForMessage); | |
| 101 | |
| 102 // These methods should be called if the holder of the thread is | |
| 103 // going to call runDebuggerTask in a loop. | |
| 104 void willEnterNestedLoop(); | |
| 105 void didLeaveNestedLoop(); | |
| 106 | |
| 107 WorkerGlobalScope* workerGlobalScope() const { return m_workerGlobalScope.ge
t(); } | |
| 108 bool terminated(); | |
| 109 | |
| 110 // Number of active worker threads. | |
| 111 static unsigned workerThreadCount(); | |
| 112 | |
| 113 PlatformThreadId platformThreadId(); | |
| 114 | |
| 115 void interruptAndDispatchInspectorCommands(); | |
| 116 void setWorkerInspectorController(WorkerInspectorController*); | |
| 117 | |
| 118 protected: | |
| 119 WorkerThread(PassRefPtr<WorkerLoaderProxy>, WorkerReportingProxy&, PassOwnPt
r<WorkerThreadStartupData>); | |
| 120 | |
| 121 // Factory method for creating a new worker context for the thread. | |
| 122 virtual PassRefPtrWillBeRawPtr<WorkerGlobalScope> createWorkerGlobalScope(Pa
ssOwnPtr<WorkerThreadStartupData>) = 0; | |
| 123 | |
| 124 virtual void postInitialize() { } | |
| 125 | |
| 126 virtual v8::Isolate* initializeIsolate(); | |
| 127 virtual void willDestroyIsolate(); | |
| 128 virtual void destroyIsolate(); | |
| 129 virtual void terminateV8Execution(); | |
| 130 | |
| 131 private: | |
| 132 friend class WorkerSharedTimer; | |
| 133 friend class WorkerThreadShutdownFinishTask; | |
| 134 | |
| 135 void stopInShutdownSequence(); | |
| 136 void stopInternal(); | |
| 137 | |
| 138 void initialize(); | |
| 139 void cleanup(); | |
| 140 void idleHandler(); | |
| 141 void postDelayedTask(PassOwnPtr<ExecutionContextTask>, long long delayMs); | |
| 142 void postDelayedTask(const WebTraceLocation&, PassOwnPtr<ExecutionContextTas
k>, long long delayMs); | |
| 143 | |
| 144 bool m_started; | |
| 145 bool m_terminated; | |
| 146 MessageQueue<WorkerThreadTask> m_debuggerMessageQueue; | |
| 147 OwnPtr<WebThread::TaskObserver> m_microtaskRunner; | |
| 148 | |
| 149 RefPtr<WorkerLoaderProxy> m_workerLoaderProxy; | |
| 150 WorkerReportingProxy& m_workerReportingProxy; | |
| 151 | |
| 152 RefPtrWillBePersistent<WorkerInspectorController> m_workerInspectorControlle
r; | |
| 153 Mutex m_workerInspectorControllerMutex; | |
| 154 | |
| 155 Mutex m_threadCreationMutex; | |
| 156 RefPtrWillBePersistent<WorkerGlobalScope> m_workerGlobalScope; | |
| 157 OwnPtr<WorkerThreadStartupData> m_startupData; | |
| 158 | |
| 159 v8::Isolate* m_isolate; | |
| 160 OwnPtr<V8IsolateInterruptor> m_interruptor; | |
| 161 | |
| 162 // Used to signal thread shutdown. | |
| 163 OwnPtr<WebWaitableEvent> m_shutdownEvent; | |
| 164 | |
| 165 // Used to signal thread termination. | |
| 166 OwnPtr<WebWaitableEvent> m_terminationEvent; | |
| 167 }; | |
| 168 | |
| 169 } // namespace blink | |
| 170 | |
| 171 #endif // WorkerThread_h | |
| OLD | NEW |