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" |
39 #include "platform/heap/ThreadState.h" | 41 #include "platform/heap/ThreadState.h" |
40 #include "platform/weborigin/KURL.h" | 42 #include "platform/weborigin/KURL.h" |
41 #include "public/platform/Platform.h" | 43 #include "public/platform/Platform.h" |
| 44 #include "public/platform/WebThread.h" |
42 #include "public/platform/WebWaitableEvent.h" | 45 #include "public/platform/WebWaitableEvent.h" |
43 #include "public/platform/WebWorkerRunLoop.h" | 46 #include "public/platform/WebWorkerRunLoop.h" |
44 #include "wtf/Noncopyable.h" | 47 #include "wtf/Noncopyable.h" |
45 #include "wtf/text/WTFString.h" | 48 #include "wtf/text/WTFString.h" |
46 | 49 |
47 #include <utility> | 50 #include <utility> |
48 | 51 |
49 namespace blink { | 52 namespace blink { |
50 | 53 |
| 54 namespace { |
| 55 const int64 kShortIdleHandlerDelayMs = 1000; |
| 56 const int64 kLongIdleHandlerDelayMs = 10*1000; |
| 57 } |
| 58 |
51 static Mutex& threadSetMutex() | 59 static Mutex& threadSetMutex() |
52 { | 60 { |
53 AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex); | 61 AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex); |
54 return mutex; | 62 return mutex; |
55 } | 63 } |
56 | 64 |
57 static HashSet<WorkerThread*>& workerThreads() | 65 static HashSet<WorkerThread*>& workerThreads() |
58 { | 66 { |
59 DEFINE_STATIC_LOCAL(HashSet<WorkerThread*>, threads, ()); | 67 DEFINE_STATIC_LOCAL(HashSet<WorkerThread*>, threads, ()); |
60 return threads; | 68 return threads; |
61 } | 69 } |
62 | 70 |
63 unsigned WorkerThread::workerThreadCount() | 71 unsigned WorkerThread::workerThreadCount() |
64 { | 72 { |
65 MutexLocker lock(threadSetMutex()); | 73 MutexLocker lock(threadSetMutex()); |
66 return workerThreads().size(); | 74 return workerThreads().size(); |
67 } | 75 } |
68 | 76 |
| 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 |
69 WorkerThread::WorkerThread(WorkerLoaderProxy& workerLoaderProxy, WorkerReporting
Proxy& workerReportingProxy, PassOwnPtrWillBeRawPtr<WorkerThreadStartupData> sta
rtupData) | 187 WorkerThread::WorkerThread(WorkerLoaderProxy& workerLoaderProxy, WorkerReporting
Proxy& workerReportingProxy, PassOwnPtrWillBeRawPtr<WorkerThreadStartupData> sta
rtupData) |
70 : m_threadID(0) | 188 : m_terminated(false) |
71 , m_workerLoaderProxy(workerLoaderProxy) | 189 , m_workerLoaderProxy(workerLoaderProxy) |
72 , m_workerReportingProxy(workerReportingProxy) | 190 , m_workerReportingProxy(workerReportingProxy) |
73 , m_startupData(startupData) | 191 , m_startupData(startupData) |
74 , m_shutdownEvent(adoptPtr(blink::Platform::current()->createWaitableEvent()
)) | 192 , m_shutdownEvent(adoptPtr(blink::Platform::current()->createWaitableEvent()
)) |
75 { | 193 { |
76 MutexLocker lock(threadSetMutex()); | 194 MutexLocker lock(threadSetMutex()); |
77 workerThreads().add(this); | 195 workerThreads().add(this); |
78 } | 196 } |
79 | 197 |
80 WorkerThread::~WorkerThread() | 198 WorkerThread::~WorkerThread() |
81 { | 199 { |
82 MutexLocker lock(threadSetMutex()); | 200 MutexLocker lock(threadSetMutex()); |
83 ASSERT(workerThreads().contains(this)); | 201 ASSERT(workerThreads().contains(this)); |
84 workerThreads().remove(this); | 202 workerThreads().remove(this); |
85 } | 203 } |
86 | 204 |
87 bool WorkerThread::start() | 205 void WorkerThread::start() |
88 { | 206 { |
89 // Mutex protection is necessary to ensure that m_threadID is initialized wh
en the thread starts. | 207 if (m_thread) |
90 MutexLocker lock(m_threadCreationMutex); | 208 return; |
91 | 209 |
92 if (m_threadID) | 210 m_thread = adoptPtr(blink::Platform::current()->createThread("WebCore: Worke
r")); |
93 return true; | 211 m_thread->postTask(new Task(WTF::bind(&WorkerThread::initialize, this))); |
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(); | |
103 } | 212 } |
104 | 213 |
105 void WorkerThread::interruptAndDispatchInspectorCommands() | 214 void WorkerThread::interruptAndDispatchInspectorCommands() |
106 { | 215 { |
107 MutexLocker locker(m_workerInspectorControllerMutex); | 216 MutexLocker locker(m_workerInspectorControllerMutex); |
108 if (m_workerInspectorController) | 217 if (m_workerInspectorController) |
109 m_workerInspectorController->interruptAndDispatchInspectorCommands(); | 218 m_workerInspectorController->interruptAndDispatchInspectorCommands(); |
110 } | 219 } |
111 | 220 |
112 void WorkerThread::workerThread() | 221 void WorkerThread::initialize() |
113 { | 222 { |
114 KURL scriptURL = m_startupData->m_scriptURL; | 223 KURL scriptURL = m_startupData->m_scriptURL; |
115 String sourceCode = m_startupData->m_sourceCode; | 224 String sourceCode = m_startupData->m_sourceCode; |
116 WorkerThreadStartMode startMode = m_startupData->m_startMode; | 225 WorkerThreadStartMode startMode = m_startupData->m_startMode; |
117 | 226 |
118 { | 227 { |
119 MutexLocker lock(m_threadCreationMutex); | 228 MutexLocker lock(m_threadCreationMutex); |
| 229 |
120 ThreadState::attach(); | 230 ThreadState::attach(); |
121 m_workerGlobalScope = createWorkerGlobalScope(m_startupData.release()); | 231 m_workerGlobalScope = createWorkerGlobalScope(m_startupData.release()); |
122 m_runLoop.setWorkerGlobalScope(workerGlobalScope()); | |
123 | 232 |
124 if (m_runLoop.terminated()) { | 233 m_sharedTimer = adoptPtr(new WorkerSharedTimer(m_thread.get())); |
| 234 PlatformThreadData::current().threadTimers().setSharedTimer(m_sharedTime
r.get()); |
| 235 |
| 236 if (m_terminated) { |
125 // The worker was terminated before the thread had a chance to run.
Since the context didn't exist yet, | 237 // The worker was terminated before the thread had a chance to run.
Since the context didn't exist yet, |
126 // forbidExecution() couldn't be called from stop(). | 238 // forbidExecution() couldn't be called from stop(). |
127 m_workerGlobalScope->script()->forbidExecution(); | 239 m_workerGlobalScope->script()->forbidExecution(); |
128 } | 240 } |
129 } | 241 } |
130 // The corresponding call to didStopWorkerRunLoop is in | 242 |
| 243 // The corresponding call to didStopWorkerThread is in |
131 // ~WorkerScriptController. | 244 // ~WorkerScriptController. |
132 blink::Platform::current()->didStartWorkerRunLoop(blink::WebWorkerRunLoop(&m
_runLoop)); | 245 blink::Platform::current()->didStartWorkerThread(m_thread.get()); |
133 | 246 |
134 // Notify proxy that a new WorkerGlobalScope has been created and started. | 247 // Notify proxy that a new WorkerGlobalScope has been created and started. |
135 m_workerReportingProxy.workerGlobalScopeStarted(m_workerGlobalScope.get()); | 248 m_workerReportingProxy.workerGlobalScopeStarted(m_workerGlobalScope.get()); |
136 | 249 |
137 WorkerScriptController* script = m_workerGlobalScope->script(); | 250 WorkerScriptController* script = m_workerGlobalScope->script(); |
138 if (!script->isExecutionForbidden()) | 251 if (!script->isExecutionForbidden()) |
139 script->initializeContextIfNeeded(); | 252 script->initializeContextIfNeeded(); |
140 InspectorInstrumentation::willEvaluateWorkerScript(workerGlobalScope(), star
tMode); | 253 InspectorInstrumentation::willEvaluateWorkerScript(workerGlobalScope(), star
tMode); |
141 script->evaluate(ScriptSourceCode(sourceCode, scriptURL)); | 254 script->evaluate(ScriptSourceCode(sourceCode, scriptURL)); |
142 | 255 |
143 runEventLoop(); | 256 postInitialize(); |
| 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(); |
144 | 265 |
145 // This should be called before we start the shutdown procedure. | 266 // This should be called before we start the shutdown procedure. |
146 workerReportingProxy().willDestroyWorkerGlobalScope(); | 267 workerReportingProxy().willDestroyWorkerGlobalScope(); |
147 | 268 |
148 ThreadIdentifier threadID = m_threadID; | |
149 | |
150 // The below assignment will destroy the context, which will in turn notify
messaging proxy. | 269 // The below assignment will destroy the context, which will in turn notify
messaging proxy. |
151 // We cannot let any objects survive past thread exit, because no other thre
ad will run GC or otherwise destroy them. | 270 // We cannot let any objects survive past thread exit, because no other thre
ad will run GC or otherwise destroy them. |
152 // If Oilpan is enabled, we detach of the context/global scope, with the fin
al heap cleanup below sweeping it out. | 271 // If Oilpan is enabled, we detach of the context/global scope, with the fin
al heap cleanup below sweeping it out. |
153 #if !ENABLE(OILPAN) | 272 #if !ENABLE(OILPAN) |
154 ASSERT(m_workerGlobalScope->hasOneRef()); | 273 ASSERT(m_workerGlobalScope->hasOneRef()); |
155 #endif | 274 #endif |
156 m_workerGlobalScope->dispose(); | 275 m_workerGlobalScope->dispose(); |
157 m_workerGlobalScope = nullptr; | 276 m_workerGlobalScope = nullptr; |
158 | 277 |
159 // Detach the ThreadState, cleaning out the thread's heap by | 278 // Detach the ThreadState, cleaning out the thread's heap by |
160 // performing a final GC. The cleanup operation will at the end | 279 // performing a final GC. The cleanup operation will at the end |
161 // assert that the heap is empty. If the heap does not become | 280 // assert that the heap is empty. If the heap does not become |
162 // empty, there are still pointers into the heap and those | 281 // empty, there are still pointers into the heap and those |
163 // pointers will be dangling after thread termination because we | 282 // pointers will be dangling after thread termination because we |
164 // are destroying the heap. It is important to detach while the | 283 // are destroying the heap. It is important to detach while the |
165 // thread is still valid. In particular, finalizers for objects in | 284 // thread is still valid. In particular, finalizers for objects in |
166 // the heap for this thread will need to access thread local data. | 285 // the heap for this thread will need to access thread local data. |
167 ThreadState::detach(); | 286 ThreadState::detach(); |
168 | 287 |
169 // Notify the proxy that the WorkerGlobalScope has been disposed of. | 288 // Notify the proxy that the WorkerGlobalScope has been disposed of. |
170 // This can free this thread object, hence it must not be touched afterwards
. | 289 // This can free this thread object, hence it must not be touched afterwards
. |
171 workerReportingProxy().workerGlobalScopeDestroyed(); | 290 workerReportingProxy().workerGlobalScopeDestroyed(); |
172 | 291 |
173 // Clean up PlatformThreadData before WTF::WTFThreadData goes away! | 292 // Clean up PlatformThreadData before WTF::WTFThreadData goes away! |
174 PlatformThreadData::current().destroy(); | 293 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(); | |
184 } | 294 } |
185 | 295 |
186 class WorkerThreadShutdownFinishTask : public ExecutionContextTask { | 296 class WorkerThreadShutdownFinishTask : public ExecutionContextTask { |
187 public: | 297 public: |
188 static PassOwnPtr<WorkerThreadShutdownFinishTask> create() | 298 static PassOwnPtr<WorkerThreadShutdownFinishTask> create() |
189 { | 299 { |
190 return adoptPtr(new WorkerThreadShutdownFinishTask()); | 300 return adoptPtr(new WorkerThreadShutdownFinishTask()); |
191 } | 301 } |
192 | 302 |
193 virtual void performTask(ExecutionContext *context) | 303 virtual void performTask(ExecutionContext *context) |
194 { | 304 { |
195 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); | 305 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); |
196 workerGlobalScope->clearInspector(); | 306 workerGlobalScope->clearInspector(); |
197 // It's not safe to call clearScript until all the cleanup tasks posted
by functions initiated by WorkerThreadShutdownStartTask have completed. | 307 // It's not safe to call clearScript until all the cleanup tasks posted
by functions initiated by WorkerThreadShutdownStartTask have completed. |
198 workerGlobalScope->clearScript(); | 308 workerGlobalScope->clearScript(); |
| 309 workerGlobalScope->thread()->webThread()->postTask(new Task(WTF::bind(&W
orkerThread::cleanup, workerGlobalScope->thread()))); |
199 } | 310 } |
200 | 311 |
201 virtual bool isCleanupTask() const { return true; } | 312 virtual bool isCleanupTask() const { return true; } |
202 }; | 313 }; |
203 | 314 |
204 class WorkerThreadShutdownStartTask : public ExecutionContextTask { | 315 class WorkerThreadShutdownStartTask : public ExecutionContextTask { |
205 public: | 316 public: |
206 static PassOwnPtr<WorkerThreadShutdownStartTask> create() | 317 static PassOwnPtr<WorkerThreadShutdownStartTask> create() |
207 { | 318 { |
208 return adoptPtr(new WorkerThreadShutdownStartTask()); | 319 return adoptPtr(new WorkerThreadShutdownStartTask()); |
209 } | 320 } |
210 | 321 |
211 virtual void performTask(ExecutionContext *context) | 322 virtual void performTask(ExecutionContext *context) |
212 { | 323 { |
213 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); | 324 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); |
214 workerGlobalScope->stopFetch(); | 325 workerGlobalScope->stopFetch(); |
215 workerGlobalScope->stopActiveDOMObjects(); | 326 workerGlobalScope->stopActiveDOMObjects(); |
| 327 PlatformThreadData::current().threadTimers().setSharedTimer(nullptr); |
216 | 328 |
217 // Event listeners would keep DOMWrapperWorld objects alive for too long
. Also, they have references to JS objects, | 329 // Event listeners would keep DOMWrapperWorld objects alive for too long
. Also, they have references to JS objects, |
218 // which become dangling once Heap is destroyed. | 330 // which become dangling once Heap is destroyed. |
219 workerGlobalScope->removeAllEventListeners(); | 331 workerGlobalScope->removeAllEventListeners(); |
220 | 332 |
221 // Stick a shutdown command at the end of the queue, so that we deal | 333 // Stick a shutdown command at the end of the queue, so that we deal |
222 // with all the cleanup tasks the databases post first. | 334 // with all the cleanup tasks the databases post first. |
223 workerGlobalScope->postTask(WorkerThreadShutdownFinishTask::create()); | 335 workerGlobalScope->postTask(WorkerThreadShutdownFinishTask::create()); |
224 } | 336 } |
225 | 337 |
226 virtual bool isCleanupTask() const { return true; } | 338 virtual bool isCleanupTask() const { return true; } |
227 }; | 339 }; |
228 | 340 |
229 void WorkerThread::stop() | 341 void WorkerThread::stop() |
230 { | 342 { |
231 // Prevent the deadlock between GC and an attempt to stop a thread. | 343 // Prevent the deadlock between GC and an attempt to stop a thread. |
232 ThreadState::SafePointScope safePointScope(ThreadState::HeapPointersOnStack)
; | 344 ThreadState::SafePointScope safePointScope(ThreadState::HeapPointersOnStack)
; |
233 | 345 |
234 // Mutex protection is necessary because stop() can be called before the con
text is fully created. | 346 // Protect against this method and initialize() racing each other. |
235 MutexLocker lock(m_threadCreationMutex); | 347 MutexLocker lock(m_threadCreationMutex); |
236 | 348 |
| 349 // If stop has already been called, just return. |
| 350 if (m_terminated) |
| 351 return; |
| 352 |
237 // Signal the thread to notify that the thread's stopping. | 353 // Signal the thread to notify that the thread's stopping. |
238 if (m_shutdownEvent) | 354 if (m_shutdownEvent) |
239 m_shutdownEvent->signal(); | 355 m_shutdownEvent->signal(); |
240 | 356 |
| 357 if (!m_workerGlobalScope) |
| 358 return; |
| 359 |
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. | 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. |
242 if (m_workerGlobalScope) { | 361 m_workerGlobalScope->script()->scheduleExecutionTermination(); |
243 m_workerGlobalScope->script()->scheduleExecutionTermination(); | 362 m_workerGlobalScope->wasRequestedToTerminate(); |
244 m_workerGlobalScope->wasRequestedToTerminate(); | 363 InspectorInstrumentation::didKillAllExecutionContextTasks(m_workerGlobalScop
e.get()); |
245 m_runLoop.postTaskAndTerminate(WorkerThreadShutdownStartTask::create()); | 364 postTask(WorkerThreadShutdownStartTask::create()); |
246 return; | 365 m_terminated = true; |
247 } | |
248 m_runLoop.terminate(); | |
249 } | 366 } |
250 | 367 |
251 bool WorkerThread::isCurrentThread() const | 368 bool WorkerThread::isCurrentThread() const |
252 { | 369 { |
253 return m_threadID == currentThread(); | 370 return m_thread && m_thread->isCurrentThread(); |
| 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); |
254 } | 386 } |
255 | 387 |
256 void WorkerThread::postTask(PassOwnPtr<ExecutionContextTask> task) | 388 void WorkerThread::postTask(PassOwnPtr<ExecutionContextTask> task) |
257 { | 389 { |
258 m_runLoop.postTask(task); | 390 m_thread->postTask(WorkerThreadTask::create(*this, task, true).leakPtr()); |
259 } | 391 } |
260 | 392 |
261 void WorkerThread::postDebuggerTask(PassOwnPtr<ExecutionContextTask> task) | 393 void WorkerThread::postDebuggerTask(PassOwnPtr<ExecutionContextTask> task) |
262 { | 394 { |
263 m_runLoop.postDebuggerTask(task); | 395 m_debuggerMessageQueue.append(WorkerThreadTask::create(*this, task, false)); |
| 396 postTask(RunDebuggerQueueTask::create(this)); |
264 } | 397 } |
265 | 398 |
266 MessageQueueWaitResult WorkerThread::runDebuggerTask(WorkerRunLoop::WaitMode wai
tMode) | 399 MessageQueueWaitResult WorkerThread::runDebuggerTask(WaitMode waitMode) |
267 { | 400 { |
268 return m_runLoop.runDebuggerTask(waitMode); | 401 ASSERT(isCurrentThread()); |
| 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()); |
269 } | 429 } |
270 | 430 |
271 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke
rInspectorController) | 431 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke
rInspectorController) |
272 { | 432 { |
273 MutexLocker locker(m_workerInspectorControllerMutex); | 433 MutexLocker locker(m_workerInspectorControllerMutex); |
274 m_workerInspectorController = workerInspectorController; | 434 m_workerInspectorController = workerInspectorController; |
275 } | 435 } |
276 | 436 |
277 } // namespace blink | 437 } // namespace blink |
OLD | NEW |