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