| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 class WorkerSharedTimer; | 50 class WorkerSharedTimer; |
| 51 class WorkerThreadShutdownFinishTask; | 51 class WorkerThreadShutdownFinishTask; |
| 52 class WorkerThreadStartupData; | 52 class WorkerThreadStartupData; |
| 53 class WorkerThreadTask; | 53 class WorkerThreadTask; |
| 54 | 54 |
| 55 enum WorkerThreadStartMode { | 55 enum WorkerThreadStartMode { |
| 56 DontPauseWorkerGlobalScopeOnStart, | 56 DontPauseWorkerGlobalScopeOnStart, |
| 57 PauseWorkerGlobalScopeOnStart | 57 PauseWorkerGlobalScopeOnStart |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 // TODO(sadrul): Rename to WorkerScript. |
| 60 class CORE_EXPORT WorkerThread : public RefCounted<WorkerThread> { | 61 class CORE_EXPORT WorkerThread : public RefCounted<WorkerThread> { |
| 61 public: | 62 public: |
| 62 virtual ~WorkerThread(); | 63 virtual ~WorkerThread(); |
| 63 | 64 |
| 64 virtual void start(); | 65 virtual void start(); |
| 65 virtual void stop(); | 66 virtual void stop(); |
| 66 | 67 |
| 67 virtual PassOwnPtr<WebThreadSupportingGC> createWebThreadSupportingGC(); | 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 |
| 68 virtual void didStartRunLoop(); | 74 virtual void didStartRunLoop(); |
| 69 virtual void didStopRunLoop(); | 75 virtual void didStopRunLoop(); |
| 70 | 76 |
| 71 v8::Isolate* isolate() const { return m_isolate; } | 77 v8::Isolate* isolate() const { return m_isolate; } |
| 72 | 78 |
| 73 // Can be used to wait for this worker thread to shut down. | 79 // Can be used to wait for this worker thread to shut down. |
| 74 // (This is signalled on the main thread, so it's assumed to be waited on th
e worker context thread) | 80 // (This is signalled on the main thread, so it's assumed to be waited on th
e worker context thread) |
| 75 WebWaitableEvent* shutdownEvent() { return m_shutdownEvent.get(); } | 81 WebWaitableEvent* shutdownEvent() { return m_shutdownEvent.get(); } |
| 76 | 82 |
| 77 WebWaitableEvent* terminationEvent() { return m_terminationEvent.get(); } | 83 WebWaitableEvent* terminationEvent() { return m_terminationEvent.get(); } |
| 78 void terminateAndWait(); | 84 void terminateAndWait(); |
| 79 static void terminateAndWaitForAllWorkers(); | 85 static void terminateAndWaitForAllWorkers(); |
| 80 | 86 |
| 81 bool isCurrentThread() const; | 87 bool isCurrentThread(); |
| 82 WorkerLoaderProxy* workerLoaderProxy() const | 88 WorkerLoaderProxy* workerLoaderProxy() const |
| 83 { | 89 { |
| 84 RELEASE_ASSERT(m_workerLoaderProxy); | 90 RELEASE_ASSERT(m_workerLoaderProxy); |
| 85 return m_workerLoaderProxy.get(); | 91 return m_workerLoaderProxy.get(); |
| 86 } | 92 } |
| 87 | 93 |
| 88 WorkerReportingProxy& workerReportingProxy() const { return m_workerReportin
gProxy; } | 94 WorkerReportingProxy& workerReportingProxy() const { return m_workerReportin
gProxy; } |
| 89 | 95 |
| 90 void postTask(const WebTraceLocation&, PassOwnPtr<ExecutionContextTask>); | 96 void postTask(const WebTraceLocation&, PassOwnPtr<ExecutionContextTask>); |
| 91 void postDebuggerTask(const WebTraceLocation&, PassOwnPtr<ExecutionContextTa
sk>); | 97 void postDebuggerTask(const WebTraceLocation&, PassOwnPtr<ExecutionContextTa
sk>); |
| 92 | 98 |
| 93 enum WaitMode { WaitForMessage, DontWaitForMessage }; | 99 enum WaitMode { WaitForMessage, DontWaitForMessage }; |
| 94 MessageQueueWaitResult runDebuggerTask(WaitMode = WaitForMessage); | 100 MessageQueueWaitResult runDebuggerTask(WaitMode = WaitForMessage); |
| 95 | 101 |
| 96 // These methods should be called if the holder of the thread is | 102 // These methods should be called if the holder of the thread is |
| 97 // going to call runDebuggerTask in a loop. | 103 // going to call runDebuggerTask in a loop. |
| 98 void willEnterNestedLoop(); | 104 void willEnterNestedLoop(); |
| 99 void didLeaveNestedLoop(); | 105 void didLeaveNestedLoop(); |
| 100 | 106 |
| 101 WorkerGlobalScope* workerGlobalScope() const { return m_workerGlobalScope.ge
t(); } | 107 WorkerGlobalScope* workerGlobalScope() const { return m_workerGlobalScope.ge
t(); } |
| 102 bool terminated(); | 108 bool terminated(); |
| 103 | 109 |
| 104 // Number of active worker threads. | 110 // Number of active worker threads. |
| 105 static unsigned workerThreadCount(); | 111 static unsigned workerThreadCount(); |
| 106 | 112 |
| 107 PlatformThreadId platformThreadId() const; | 113 PlatformThreadId platformThreadId(); |
| 108 | 114 |
| 109 void interruptAndDispatchInspectorCommands(); | 115 void interruptAndDispatchInspectorCommands(); |
| 110 void setWorkerInspectorController(WorkerInspectorController*); | 116 void setWorkerInspectorController(WorkerInspectorController*); |
| 111 | 117 |
| 112 protected: | 118 protected: |
| 113 WorkerThread(const char* threadName, PassRefPtr<WorkerLoaderProxy>, WorkerRe
portingProxy&, PassOwnPtr<WorkerThreadStartupData>); | 119 WorkerThread(PassRefPtr<WorkerLoaderProxy>, WorkerReportingProxy&, PassOwnPt
r<WorkerThreadStartupData>); |
| 114 | 120 |
| 115 // Factory method for creating a new worker context for the thread. | 121 // Factory method for creating a new worker context for the thread. |
| 116 virtual PassRefPtrWillBeRawPtr<WorkerGlobalScope> createWorkerGlobalScope(Pa
ssOwnPtr<WorkerThreadStartupData>) = 0; | 122 virtual PassRefPtrWillBeRawPtr<WorkerGlobalScope> createWorkerGlobalScope(Pa
ssOwnPtr<WorkerThreadStartupData>) = 0; |
| 117 | 123 |
| 118 virtual void postInitialize() { } | 124 virtual void postInitialize() { } |
| 119 | 125 |
| 120 virtual v8::Isolate* initializeIsolate(); | 126 virtual v8::Isolate* initializeIsolate(); |
| 121 virtual void willDestroyIsolate(); | 127 virtual void willDestroyIsolate(); |
| 122 virtual void destroyIsolate(); | 128 virtual void destroyIsolate(); |
| 123 virtual void terminateV8Execution(); | 129 virtual void terminateV8Execution(); |
| 124 | 130 |
| 125 private: | 131 private: |
| 126 friend class WorkerSharedTimer; | 132 friend class WorkerSharedTimer; |
| 127 friend class WorkerThreadShutdownFinishTask; | 133 friend class WorkerThreadShutdownFinishTask; |
| 128 | 134 |
| 129 void stopInShutdownSequence(); | 135 void stopInShutdownSequence(); |
| 130 void stopInternal(); | 136 void stopInternal(); |
| 131 | 137 |
| 132 void initialize(); | 138 void initialize(); |
| 133 void cleanup(); | 139 void cleanup(); |
| 134 void idleHandler(); | 140 void idleHandler(); |
| 135 void postDelayedTask(PassOwnPtr<ExecutionContextTask>, long long delayMs); | 141 void postDelayedTask(PassOwnPtr<ExecutionContextTask>, long long delayMs); |
| 136 void postDelayedTask(const WebTraceLocation&, PassOwnPtr<ExecutionContextTas
k>, long long delayMs); | 142 void postDelayedTask(const WebTraceLocation&, PassOwnPtr<ExecutionContextTas
k>, long long delayMs); |
| 137 | 143 |
| 138 const char* m_threadName; | 144 bool m_started; |
| 139 bool m_terminated; | 145 bool m_terminated; |
| 140 MessageQueue<WorkerThreadTask> m_debuggerMessageQueue; | 146 MessageQueue<WorkerThreadTask> m_debuggerMessageQueue; |
| 141 OwnPtr<WebThread::TaskObserver> m_microtaskRunner; | 147 OwnPtr<WebThread::TaskObserver> m_microtaskRunner; |
| 142 | 148 |
| 143 RefPtr<WorkerLoaderProxy> m_workerLoaderProxy; | 149 RefPtr<WorkerLoaderProxy> m_workerLoaderProxy; |
| 144 WorkerReportingProxy& m_workerReportingProxy; | 150 WorkerReportingProxy& m_workerReportingProxy; |
| 145 | 151 |
| 146 RefPtrWillBePersistent<WorkerInspectorController> m_workerInspectorControlle
r; | 152 RefPtrWillBePersistent<WorkerInspectorController> m_workerInspectorControlle
r; |
| 147 Mutex m_workerInspectorControllerMutex; | 153 Mutex m_workerInspectorControllerMutex; |
| 148 | 154 |
| 149 Mutex m_threadCreationMutex; | 155 Mutex m_threadCreationMutex; |
| 150 RefPtrWillBePersistent<WorkerGlobalScope> m_workerGlobalScope; | 156 RefPtrWillBePersistent<WorkerGlobalScope> m_workerGlobalScope; |
| 151 OwnPtr<WorkerThreadStartupData> m_startupData; | 157 OwnPtr<WorkerThreadStartupData> m_startupData; |
| 152 | 158 |
| 153 v8::Isolate* m_isolate; | 159 v8::Isolate* m_isolate; |
| 154 OwnPtr<V8IsolateInterruptor> m_interruptor; | 160 OwnPtr<V8IsolateInterruptor> m_interruptor; |
| 155 | 161 |
| 156 // Used to signal thread shutdown. | 162 // Used to signal thread shutdown. |
| 157 OwnPtr<WebWaitableEvent> m_shutdownEvent; | 163 OwnPtr<WebWaitableEvent> m_shutdownEvent; |
| 158 | 164 |
| 159 // Used to signal thread termination. | 165 // Used to signal thread termination. |
| 160 OwnPtr<WebWaitableEvent> m_terminationEvent; | 166 OwnPtr<WebWaitableEvent> m_terminationEvent; |
| 161 | |
| 162 // FIXME: This has to be last because of crbug.com/401397 - the | |
| 163 // WorkerThread might get deleted before it had a chance to properly | |
| 164 // shut down. By deleting the WebThread first, we can guarantee that | |
| 165 // no pending tasks on the thread might want to access any of the other | |
| 166 // members during the WorkerThread's destruction. | |
| 167 OwnPtr<WebThreadSupportingGC> m_thread; | |
| 168 }; | 167 }; |
| 169 | 168 |
| 170 } // namespace blink | 169 } // namespace blink |
| 171 | 170 |
| 172 #endif // WorkerThread_h | 171 #endif // WorkerThread_h |
| OLD | NEW |