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