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 18 matching lines...) Expand all Loading... |
29 #include "core/workers/WorkerThread.h" | 29 #include "core/workers/WorkerThread.h" |
30 | 30 |
31 #include "bindings/core/v8/ScriptSourceCode.h" | 31 #include "bindings/core/v8/ScriptSourceCode.h" |
32 #include "core/inspector/InspectorInstrumentation.h" | 32 #include "core/inspector/InspectorInstrumentation.h" |
33 #include "core/inspector/WorkerInspectorController.h" | 33 #include "core/inspector/WorkerInspectorController.h" |
34 #include "core/workers/DedicatedWorkerGlobalScope.h" | 34 #include "core/workers/DedicatedWorkerGlobalScope.h" |
35 #include "core/workers/WorkerClients.h" | 35 #include "core/workers/WorkerClients.h" |
36 #include "core/workers/WorkerReportingProxy.h" | 36 #include "core/workers/WorkerReportingProxy.h" |
37 #include "core/workers/WorkerThreadStartupData.h" | 37 #include "core/workers/WorkerThreadStartupData.h" |
38 #include "platform/PlatformThreadData.h" | 38 #include "platform/PlatformThreadData.h" |
39 #include "platform/Task.h" | |
40 #include "platform/ThreadTimers.h" | |
41 #include "platform/heap/ThreadState.h" | 39 #include "platform/heap/ThreadState.h" |
42 #include "platform/weborigin/KURL.h" | 40 #include "platform/weborigin/KURL.h" |
43 #include "public/platform/Platform.h" | 41 #include "public/platform/Platform.h" |
44 #include "public/platform/WebThread.h" | |
45 #include "public/platform/WebWaitableEvent.h" | 42 #include "public/platform/WebWaitableEvent.h" |
46 #include "public/platform/WebWorkerRunLoop.h" | 43 #include "public/platform/WebWorkerRunLoop.h" |
47 #include "wtf/Noncopyable.h" | 44 #include "wtf/Noncopyable.h" |
48 #include "wtf/text/WTFString.h" | 45 #include "wtf/text/WTFString.h" |
49 | 46 |
50 #include <utility> | 47 #include <utility> |
51 | 48 |
52 namespace blink { | 49 namespace blink { |
53 | 50 |
54 namespace { | |
55 const int64 kShortIdleHandlerDelayMs = 1000; | |
56 const int64 kLongIdleHandlerDelayMs = 10*1000; | |
57 } | |
58 | |
59 static Mutex& threadSetMutex() | 51 static Mutex& threadSetMutex() |
60 { | 52 { |
61 AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex); | 53 AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex); |
62 return mutex; | 54 return mutex; |
63 } | 55 } |
64 | 56 |
65 static HashSet<WorkerThread*>& workerThreads() | 57 static HashSet<WorkerThread*>& workerThreads() |
66 { | 58 { |
67 DEFINE_STATIC_LOCAL(HashSet<WorkerThread*>, threads, ()); | 59 DEFINE_STATIC_LOCAL(HashSet<WorkerThread*>, threads, ()); |
68 return threads; | 60 return threads; |
69 } | 61 } |
70 | 62 |
71 unsigned WorkerThread::workerThreadCount() | 63 unsigned WorkerThread::workerThreadCount() |
72 { | 64 { |
73 MutexLocker lock(threadSetMutex()); | 65 MutexLocker lock(threadSetMutex()); |
74 return workerThreads().size(); | 66 return workerThreads().size(); |
75 } | 67 } |
76 | 68 |
77 class WorkerSharedTimer : public SharedTimer { | |
78 public: | |
79 explicit WorkerSharedTimer(blink::WebThread* webThread) | |
80 : m_thread(webThread) | |
81 , m_nextFireTime(0.0) | |
82 , m_running(false) | |
83 { } | |
84 | |
85 typedef void (*SharedTimerFunction)(); | |
86 virtual void setFiredFunction(SharedTimerFunction func) | |
87 { | |
88 m_sharedTimerFunction = func; | |
89 if (!m_sharedTimerFunction) | |
90 m_nextFireTime = 0.0; | |
91 } | |
92 | |
93 virtual void setFireInterval(double interval) | |
94 { | |
95 ASSERT(m_sharedTimerFunction); | |
96 | |
97 // See BlinkPlatformImpl::setSharedTimerFireInterval for explanation of | |
98 // why ceil is used in the interval calculation. | |
99 int64 delay = static_cast<int64>(ceil(interval * 1000)); | |
100 | |
101 if (delay < 0) { | |
102 delay = 0; | |
103 m_nextFireTime = 0.0; | |
104 } | |
105 | |
106 m_running = true; | |
107 m_nextFireTime = currentTime() + interval; | |
108 m_thread->postDelayedTask(new Task(WTF::bind(&WorkerSharedTimer::OnTimeo
ut, this)), delay); | |
109 } | |
110 | |
111 virtual void stop() | |
112 { | |
113 m_running = false; | |
114 } | |
115 | |
116 double nextFireTime() { return m_nextFireTime; } | |
117 | |
118 private: | |
119 void OnTimeout() | |
120 { | |
121 if (m_sharedTimerFunction && m_running) | |
122 m_sharedTimerFunction(); | |
123 } | |
124 | |
125 WebThread* m_thread; | |
126 SharedTimerFunction m_sharedTimerFunction; | |
127 double m_nextFireTime; | |
128 bool m_running; | |
129 }; | |
130 | |
131 class WorkerThreadTask : public blink::WebThread::Task { | |
132 WTF_MAKE_NONCOPYABLE(WorkerThreadTask); WTF_MAKE_FAST_ALLOCATED; | |
133 public: | |
134 static PassOwnPtr<WorkerThreadTask> create(const WorkerThread& workerThread,
PassOwnPtr<ExecutionContextTask> task, bool isInstrumented) | |
135 { | |
136 return adoptPtr(new WorkerThreadTask(workerThread, task, isInstrumented)
); | |
137 } | |
138 | |
139 virtual ~WorkerThreadTask() { } | |
140 | |
141 virtual void run() OVERRIDE | |
142 { | |
143 WorkerGlobalScope* workerGlobalScope = m_workerThread.workerGlobalScope(
); | |
144 if (m_isInstrumented) | |
145 InspectorInstrumentation::willPerformExecutionContextTask(workerGlob
alScope, m_task.get()); | |
146 if ((!workerGlobalScope->isClosing() && !m_workerThread.terminated()) ||
m_task->isCleanupTask()) | |
147 m_task->performTask(workerGlobalScope); | |
148 if (m_isInstrumented) | |
149 InspectorInstrumentation::didPerformExecutionContextTask(workerGloba
lScope); | |
150 } | |
151 | |
152 private: | |
153 WorkerThreadTask(const WorkerThread& workerThread, PassOwnPtr<ExecutionConte
xtTask> task, bool isInstrumented) | |
154 : m_workerThread(workerThread) | |
155 , m_task(task) | |
156 , m_isInstrumented(isInstrumented) | |
157 { | |
158 if (m_isInstrumented) | |
159 m_isInstrumented = !m_task->taskNameForInstrumentation().isEmpty(); | |
160 if (m_isInstrumented) | |
161 InspectorInstrumentation::didPostExecutionContextTask(m_workerThread
.workerGlobalScope(), m_task.get()); | |
162 } | |
163 | |
164 const WorkerThread& m_workerThread; | |
165 OwnPtr<ExecutionContextTask> m_task; | |
166 bool m_isInstrumented; | |
167 }; | |
168 | |
169 class RunDebuggerQueueTask FINAL : public ExecutionContextTask { | |
170 public: | |
171 static PassOwnPtr<RunDebuggerQueueTask> create(WorkerThread* thread) | |
172 { | |
173 return adoptPtr(new RunDebuggerQueueTask(thread)); | |
174 } | |
175 virtual void performTask(ExecutionContext* context) OVERRIDE | |
176 { | |
177 ASSERT(context->isWorkerGlobalScope()); | |
178 m_thread->runDebuggerTask(WorkerThread::DontWaitForMessage); | |
179 } | |
180 | |
181 private: | |
182 explicit RunDebuggerQueueTask(WorkerThread* thread) : m_thread(thread) { } | |
183 | |
184 WorkerThread* m_thread; | |
185 }; | |
186 | |
187 WorkerThread::WorkerThread(WorkerLoaderProxy& workerLoaderProxy, WorkerReporting
Proxy& workerReportingProxy, PassOwnPtrWillBeRawPtr<WorkerThreadStartupData> sta
rtupData) | 69 WorkerThread::WorkerThread(WorkerLoaderProxy& workerLoaderProxy, WorkerReporting
Proxy& workerReportingProxy, PassOwnPtrWillBeRawPtr<WorkerThreadStartupData> sta
rtupData) |
188 : m_terminated(false) | 70 : m_threadID(0) |
189 , m_workerLoaderProxy(workerLoaderProxy) | 71 , m_workerLoaderProxy(workerLoaderProxy) |
190 , m_workerReportingProxy(workerReportingProxy) | 72 , m_workerReportingProxy(workerReportingProxy) |
191 , m_startupData(startupData) | 73 , m_startupData(startupData) |
192 , m_shutdownEvent(adoptPtr(blink::Platform::current()->createWaitableEvent()
)) | 74 , m_shutdownEvent(adoptPtr(blink::Platform::current()->createWaitableEvent()
)) |
193 { | 75 { |
194 MutexLocker lock(threadSetMutex()); | 76 MutexLocker lock(threadSetMutex()); |
195 workerThreads().add(this); | 77 workerThreads().add(this); |
196 } | 78 } |
197 | 79 |
198 WorkerThread::~WorkerThread() | 80 WorkerThread::~WorkerThread() |
199 { | 81 { |
200 MutexLocker lock(threadSetMutex()); | 82 MutexLocker lock(threadSetMutex()); |
201 ASSERT(workerThreads().contains(this)); | 83 ASSERT(workerThreads().contains(this)); |
202 workerThreads().remove(this); | 84 workerThreads().remove(this); |
203 } | 85 } |
204 | 86 |
205 void WorkerThread::start() | 87 bool WorkerThread::start() |
206 { | 88 { |
207 if (m_thread) | 89 // Mutex protection is necessary to ensure that m_threadID is initialized wh
en the thread starts. |
208 return; | 90 MutexLocker lock(m_threadCreationMutex); |
209 | 91 |
210 m_thread = adoptPtr(blink::Platform::current()->createThread("WebCore: Worke
r")); | 92 if (m_threadID) |
211 m_thread->postTask(new Task(WTF::bind(&WorkerThread::initialize, this))); | 93 return true; |
| 94 |
| 95 m_threadID = createThread(WorkerThread::workerThreadStart, this, "WebCore: W
orker"); |
| 96 |
| 97 return m_threadID; |
| 98 } |
| 99 |
| 100 void WorkerThread::workerThreadStart(void* thread) |
| 101 { |
| 102 static_cast<WorkerThread*>(thread)->workerThread(); |
212 } | 103 } |
213 | 104 |
214 void WorkerThread::interruptAndDispatchInspectorCommands() | 105 void WorkerThread::interruptAndDispatchInspectorCommands() |
215 { | 106 { |
216 MutexLocker locker(m_workerInspectorControllerMutex); | 107 MutexLocker locker(m_workerInspectorControllerMutex); |
217 if (m_workerInspectorController) | 108 if (m_workerInspectorController) |
218 m_workerInspectorController->interruptAndDispatchInspectorCommands(); | 109 m_workerInspectorController->interruptAndDispatchInspectorCommands(); |
219 } | 110 } |
220 | 111 |
221 void WorkerThread::initialize() | 112 void WorkerThread::workerThread() |
222 { | 113 { |
223 KURL scriptURL = m_startupData->m_scriptURL; | 114 KURL scriptURL = m_startupData->m_scriptURL; |
224 String sourceCode = m_startupData->m_sourceCode; | 115 String sourceCode = m_startupData->m_sourceCode; |
225 WorkerThreadStartMode startMode = m_startupData->m_startMode; | 116 WorkerThreadStartMode startMode = m_startupData->m_startMode; |
226 | 117 |
227 { | 118 { |
228 MutexLocker lock(m_threadCreationMutex); | 119 MutexLocker lock(m_threadCreationMutex); |
229 | |
230 ThreadState::attach(); | 120 ThreadState::attach(); |
231 m_workerGlobalScope = createWorkerGlobalScope(m_startupData.release()); | 121 m_workerGlobalScope = createWorkerGlobalScope(m_startupData.release()); |
| 122 m_runLoop.setWorkerGlobalScope(workerGlobalScope()); |
232 | 123 |
233 m_sharedTimer = adoptPtr(new WorkerSharedTimer(m_thread.get())); | 124 if (m_runLoop.terminated()) { |
234 PlatformThreadData::current().threadTimers().setSharedTimer(m_sharedTime
r.get()); | |
235 | |
236 if (m_terminated) { | |
237 // The worker was terminated before the thread had a chance to run.
Since the context didn't exist yet, | 125 // The worker was terminated before the thread had a chance to run.
Since the context didn't exist yet, |
238 // forbidExecution() couldn't be called from stop(). | 126 // forbidExecution() couldn't be called from stop(). |
239 m_workerGlobalScope->script()->forbidExecution(); | 127 m_workerGlobalScope->script()->forbidExecution(); |
240 } | 128 } |
241 } | 129 } |
242 | 130 // The corresponding call to didStopWorkerRunLoop is in |
243 // The corresponding call to didStopWorkerThread is in | |
244 // ~WorkerScriptController. | 131 // ~WorkerScriptController. |
245 blink::Platform::current()->didStartWorkerThread(m_thread.get()); | 132 blink::Platform::current()->didStartWorkerRunLoop(blink::WebWorkerRunLoop(&m
_runLoop)); |
246 | 133 |
247 // Notify proxy that a new WorkerGlobalScope has been created and started. | 134 // Notify proxy that a new WorkerGlobalScope has been created and started. |
248 m_workerReportingProxy.workerGlobalScopeStarted(m_workerGlobalScope.get()); | 135 m_workerReportingProxy.workerGlobalScopeStarted(m_workerGlobalScope.get()); |
249 | 136 |
250 WorkerScriptController* script = m_workerGlobalScope->script(); | 137 WorkerScriptController* script = m_workerGlobalScope->script(); |
251 if (!script->isExecutionForbidden()) | 138 if (!script->isExecutionForbidden()) |
252 script->initializeContextIfNeeded(); | 139 script->initializeContextIfNeeded(); |
253 InspectorInstrumentation::willEvaluateWorkerScript(workerGlobalScope(), star
tMode); | 140 InspectorInstrumentation::willEvaluateWorkerScript(workerGlobalScope(), star
tMode); |
254 script->evaluate(ScriptSourceCode(sourceCode, scriptURL)); | 141 script->evaluate(ScriptSourceCode(sourceCode, scriptURL)); |
255 | 142 |
256 postInitialize(); | 143 runEventLoop(); |
257 | |
258 m_weakFactory = adoptPtr(new WeakPtrFactory<WorkerThread>(this)); | |
259 m_thread->postDelayedTask(new Task(WTF::bind(&WorkerThread::idleHandler, m_w
eakFactory->createWeakPtr())), kShortIdleHandlerDelayMs); | |
260 } | |
261 | |
262 void WorkerThread::cleanup() | |
263 { | |
264 m_weakFactory.release(); | |
265 | 144 |
266 // This should be called before we start the shutdown procedure. | 145 // This should be called before we start the shutdown procedure. |
267 workerReportingProxy().willDestroyWorkerGlobalScope(); | 146 workerReportingProxy().willDestroyWorkerGlobalScope(); |
268 | 147 |
| 148 ThreadIdentifier threadID = m_threadID; |
| 149 |
269 // The below assignment will destroy the context, which will in turn notify
messaging proxy. | 150 // The below assignment will destroy the context, which will in turn notify
messaging proxy. |
270 // We cannot let any objects survive past thread exit, because no other thre
ad will run GC or otherwise destroy them. | 151 // We cannot let any objects survive past thread exit, because no other thre
ad will run GC or otherwise destroy them. |
271 // If Oilpan is enabled, we detach of the context/global scope, with the fin
al heap cleanup below sweeping it out. | 152 // If Oilpan is enabled, we detach of the context/global scope, with the fin
al heap cleanup below sweeping it out. |
272 #if !ENABLE(OILPAN) | 153 #if !ENABLE(OILPAN) |
273 ASSERT(m_workerGlobalScope->hasOneRef()); | 154 ASSERT(m_workerGlobalScope->hasOneRef()); |
274 #endif | 155 #endif |
275 m_workerGlobalScope->dispose(); | 156 m_workerGlobalScope->dispose(); |
276 m_workerGlobalScope = nullptr; | 157 m_workerGlobalScope = nullptr; |
277 | 158 |
278 // Detach the ThreadState, cleaning out the thread's heap by | 159 // Detach the ThreadState, cleaning out the thread's heap by |
279 // performing a final GC. The cleanup operation will at the end | 160 // performing a final GC. The cleanup operation will at the end |
280 // assert that the heap is empty. If the heap does not become | 161 // assert that the heap is empty. If the heap does not become |
281 // empty, there are still pointers into the heap and those | 162 // empty, there are still pointers into the heap and those |
282 // pointers will be dangling after thread termination because we | 163 // pointers will be dangling after thread termination because we |
283 // are destroying the heap. It is important to detach while the | 164 // are destroying the heap. It is important to detach while the |
284 // thread is still valid. In particular, finalizers for objects in | 165 // thread is still valid. In particular, finalizers for objects in |
285 // the heap for this thread will need to access thread local data. | 166 // the heap for this thread will need to access thread local data. |
286 ThreadState::detach(); | 167 ThreadState::detach(); |
287 | 168 |
288 // Notify the proxy that the WorkerGlobalScope has been disposed of. | 169 // Notify the proxy that the WorkerGlobalScope has been disposed of. |
289 // This can free this thread object, hence it must not be touched afterwards
. | 170 // This can free this thread object, hence it must not be touched afterwards
. |
290 workerReportingProxy().workerGlobalScopeDestroyed(); | 171 workerReportingProxy().workerGlobalScopeDestroyed(); |
291 | 172 |
292 // Clean up PlatformThreadData before WTF::WTFThreadData goes away! | 173 // Clean up PlatformThreadData before WTF::WTFThreadData goes away! |
293 PlatformThreadData::current().destroy(); | 174 PlatformThreadData::current().destroy(); |
| 175 |
| 176 // The thread object may be already destroyed from notification now, don't t
ry to access "this". |
| 177 detachThread(threadID); |
| 178 } |
| 179 |
| 180 void WorkerThread::runEventLoop() |
| 181 { |
| 182 // Does not return until terminated. |
| 183 m_runLoop.run(); |
294 } | 184 } |
295 | 185 |
296 class WorkerThreadShutdownFinishTask : public ExecutionContextTask { | 186 class WorkerThreadShutdownFinishTask : public ExecutionContextTask { |
297 public: | 187 public: |
298 static PassOwnPtr<WorkerThreadShutdownFinishTask> create() | 188 static PassOwnPtr<WorkerThreadShutdownFinishTask> create() |
299 { | 189 { |
300 return adoptPtr(new WorkerThreadShutdownFinishTask()); | 190 return adoptPtr(new WorkerThreadShutdownFinishTask()); |
301 } | 191 } |
302 | 192 |
303 virtual void performTask(ExecutionContext *context) | 193 virtual void performTask(ExecutionContext *context) |
304 { | 194 { |
305 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); | 195 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); |
306 workerGlobalScope->clearInspector(); | 196 workerGlobalScope->clearInspector(); |
307 // It's not safe to call clearScript until all the cleanup tasks posted
by functions initiated by WorkerThreadShutdownStartTask have completed. | 197 // It's not safe to call clearScript until all the cleanup tasks posted
by functions initiated by WorkerThreadShutdownStartTask have completed. |
308 workerGlobalScope->clearScript(); | 198 workerGlobalScope->clearScript(); |
309 workerGlobalScope->thread()->webThread()->postTask(new Task(WTF::bind(&W
orkerThread::cleanup, workerGlobalScope->thread()))); | |
310 } | 199 } |
311 | 200 |
312 virtual bool isCleanupTask() const { return true; } | 201 virtual bool isCleanupTask() const { return true; } |
313 }; | 202 }; |
314 | 203 |
315 class WorkerThreadShutdownStartTask : public ExecutionContextTask { | 204 class WorkerThreadShutdownStartTask : public ExecutionContextTask { |
316 public: | 205 public: |
317 static PassOwnPtr<WorkerThreadShutdownStartTask> create() | 206 static PassOwnPtr<WorkerThreadShutdownStartTask> create() |
318 { | 207 { |
319 return adoptPtr(new WorkerThreadShutdownStartTask()); | 208 return adoptPtr(new WorkerThreadShutdownStartTask()); |
320 } | 209 } |
321 | 210 |
322 virtual void performTask(ExecutionContext *context) | 211 virtual void performTask(ExecutionContext *context) |
323 { | 212 { |
324 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); | 213 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); |
325 workerGlobalScope->stopFetch(); | 214 workerGlobalScope->stopFetch(); |
326 workerGlobalScope->stopActiveDOMObjects(); | 215 workerGlobalScope->stopActiveDOMObjects(); |
327 PlatformThreadData::current().threadTimers().setSharedTimer(nullptr); | |
328 | 216 |
329 // Event listeners would keep DOMWrapperWorld objects alive for too long
. Also, they have references to JS objects, | 217 // Event listeners would keep DOMWrapperWorld objects alive for too long
. Also, they have references to JS objects, |
330 // which become dangling once Heap is destroyed. | 218 // which become dangling once Heap is destroyed. |
331 workerGlobalScope->removeAllEventListeners(); | 219 workerGlobalScope->removeAllEventListeners(); |
332 | 220 |
333 // Stick a shutdown command at the end of the queue, so that we deal | 221 // Stick a shutdown command at the end of the queue, so that we deal |
334 // with all the cleanup tasks the databases post first. | 222 // with all the cleanup tasks the databases post first. |
335 workerGlobalScope->postTask(WorkerThreadShutdownFinishTask::create()); | 223 workerGlobalScope->postTask(WorkerThreadShutdownFinishTask::create()); |
336 } | 224 } |
337 | 225 |
338 virtual bool isCleanupTask() const { return true; } | 226 virtual bool isCleanupTask() const { return true; } |
339 }; | 227 }; |
340 | 228 |
341 void WorkerThread::stop() | 229 void WorkerThread::stop() |
342 { | 230 { |
343 // Prevent the deadlock between GC and an attempt to stop a thread. | 231 // Prevent the deadlock between GC and an attempt to stop a thread. |
344 ThreadState::SafePointScope safePointScope(ThreadState::HeapPointersOnStack)
; | 232 ThreadState::SafePointScope safePointScope(ThreadState::HeapPointersOnStack)
; |
345 | 233 |
346 // Protect against this method and initialize() racing each other. | 234 // Mutex protection is necessary because stop() can be called before the con
text is fully created. |
347 MutexLocker lock(m_threadCreationMutex); | 235 MutexLocker lock(m_threadCreationMutex); |
348 | 236 |
349 // If stop has already been called, just return. | |
350 if (m_terminated) | |
351 return; | |
352 | |
353 // Signal the thread to notify that the thread's stopping. | 237 // Signal the thread to notify that the thread's stopping. |
354 if (m_shutdownEvent) | 238 if (m_shutdownEvent) |
355 m_shutdownEvent->signal(); | 239 m_shutdownEvent->signal(); |
356 | 240 |
357 if (!m_workerGlobalScope) | 241 // Ensure that tasks are being handled by thread event loop. If script execu
tion weren't forbidden, a while(1) loop in JS could keep the thread alive foreve
r. |
| 242 if (m_workerGlobalScope) { |
| 243 m_workerGlobalScope->script()->scheduleExecutionTermination(); |
| 244 m_workerGlobalScope->wasRequestedToTerminate(); |
| 245 m_runLoop.postTaskAndTerminate(WorkerThreadShutdownStartTask::create()); |
358 return; | 246 return; |
359 | 247 } |
360 // Ensure that tasks are being handled by thread event loop. If script execu
tion weren't forbidden, a while(1) loop in JS could keep the thread alive foreve
r. | 248 m_runLoop.terminate(); |
361 m_workerGlobalScope->script()->scheduleExecutionTermination(); | |
362 m_workerGlobalScope->wasRequestedToTerminate(); | |
363 InspectorInstrumentation::didKillAllExecutionContextTasks(m_workerGlobalScop
e.get()); | |
364 postTask(WorkerThreadShutdownStartTask::create()); | |
365 m_terminated = true; | |
366 } | 249 } |
367 | 250 |
368 bool WorkerThread::isCurrentThread() const | 251 bool WorkerThread::isCurrentThread() const |
369 { | 252 { |
370 return m_thread && m_thread->isCurrentThread(); | 253 return m_threadID == currentThread(); |
371 } | |
372 | |
373 void WorkerThread::idleHandler() | |
374 { | |
375 int64 delay = kLongIdleHandlerDelayMs; | |
376 | |
377 // Do a script engine idle notification if the next event is distant enough. | |
378 const double kMinIdleTimespan = 0.3; | |
379 if (m_sharedTimer->nextFireTime() == 0.0 || m_sharedTimer->nextFireTime() >
currentTime() + kMinIdleTimespan) { | |
380 bool hasMoreWork = !m_workerGlobalScope->idleNotification(); | |
381 if (hasMoreWork) | |
382 delay = kShortIdleHandlerDelayMs; | |
383 } | |
384 | |
385 m_thread->postDelayedTask(new Task(WTF::bind(&WorkerThread::idleHandler, m_w
eakFactory->createWeakPtr())), delay); | |
386 } | 254 } |
387 | 255 |
388 void WorkerThread::postTask(PassOwnPtr<ExecutionContextTask> task) | 256 void WorkerThread::postTask(PassOwnPtr<ExecutionContextTask> task) |
389 { | 257 { |
390 m_thread->postTask(WorkerThreadTask::create(*this, task, true).leakPtr()); | 258 m_runLoop.postTask(task); |
391 } | 259 } |
392 | 260 |
393 void WorkerThread::postDebuggerTask(PassOwnPtr<ExecutionContextTask> task) | 261 void WorkerThread::postDebuggerTask(PassOwnPtr<ExecutionContextTask> task) |
394 { | 262 { |
395 m_debuggerMessageQueue.append(WorkerThreadTask::create(*this, task, false)); | 263 m_runLoop.postDebuggerTask(task); |
396 postTask(RunDebuggerQueueTask::create(this)); | |
397 } | 264 } |
398 | 265 |
399 MessageQueueWaitResult WorkerThread::runDebuggerTask(WaitMode waitMode) | 266 MessageQueueWaitResult WorkerThread::runDebuggerTask(WorkerRunLoop::WaitMode wai
tMode) |
400 { | 267 { |
401 ASSERT(isCurrentThread()); | 268 return m_runLoop.runDebuggerTask(waitMode); |
402 MessageQueueWaitResult result; | |
403 double absoluteTime = MessageQueue<blink::WebThread::Task>::infiniteTime(); | |
404 OwnPtr<blink::WebThread::Task> task; | |
405 { | |
406 if (waitMode == DontWaitForMessage) | |
407 absoluteTime = 0.0; | |
408 ThreadState::SafePointScope safePointScope(ThreadState::NoHeapPointersOn
Stack); | |
409 task = m_debuggerMessageQueue.waitForMessageWithTimeout(result, absolute
Time); | |
410 } | |
411 | |
412 if (result == MessageQueueMessageReceived) { | |
413 InspectorInstrumentation::willProcessTask(workerGlobalScope()); | |
414 task->run(); | |
415 InspectorInstrumentation::didProcessTask(workerGlobalScope()); | |
416 } | |
417 | |
418 return result; | |
419 } | |
420 | |
421 void WorkerThread::willEnterNestedLoop() | |
422 { | |
423 InspectorInstrumentation::willEnterNestedRunLoop(m_workerGlobalScope.get()); | |
424 } | |
425 | |
426 void WorkerThread::didLeaveNestedLoop() | |
427 { | |
428 InspectorInstrumentation::didLeaveNestedRunLoop(m_workerGlobalScope.get()); | |
429 } | 269 } |
430 | 270 |
431 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke
rInspectorController) | 271 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke
rInspectorController) |
432 { | 272 { |
433 MutexLocker locker(m_workerInspectorControllerMutex); | 273 MutexLocker locker(m_workerInspectorControllerMutex); |
434 m_workerInspectorController = workerInspectorController; | 274 m_workerInspectorController = workerInspectorController; |
435 } | 275 } |
436 | 276 |
437 } // namespace blink | 277 } // namespace blink |
OLD | NEW |