| 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 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 | |
| 186 WorkerThread::WorkerThread(WorkerLoaderProxy& workerLoaderProxy, WorkerReporting
Proxy& workerReportingProxy, PassOwnPtrWillBeRawPtr<WorkerThreadStartupData> sta
rtupData) | 69 WorkerThread::WorkerThread(WorkerLoaderProxy& workerLoaderProxy, WorkerReporting
Proxy& workerReportingProxy, PassOwnPtrWillBeRawPtr<WorkerThreadStartupData> sta
rtupData) |
| 187 : m_terminated(false) | 70 : m_threadID(0) |
| 188 , m_workerLoaderProxy(workerLoaderProxy) | 71 , m_workerLoaderProxy(workerLoaderProxy) |
| 189 , m_workerReportingProxy(workerReportingProxy) | 72 , m_workerReportingProxy(workerReportingProxy) |
| 190 , m_startupData(startupData) | 73 , m_startupData(startupData) |
| 191 , m_shutdownEvent(adoptPtr(blink::Platform::current()->createWaitableEvent()
)) | 74 , m_shutdownEvent(adoptPtr(blink::Platform::current()->createWaitableEvent()
)) |
| 192 { | 75 { |
| 193 MutexLocker lock(threadSetMutex()); | 76 MutexLocker lock(threadSetMutex()); |
| 194 workerThreads().add(this); | 77 workerThreads().add(this); |
| 195 } | 78 } |
| 196 | 79 |
| 197 WorkerThread::~WorkerThread() | 80 WorkerThread::~WorkerThread() |
| 198 { | 81 { |
| 199 MutexLocker lock(threadSetMutex()); | 82 MutexLocker lock(threadSetMutex()); |
| 200 ASSERT(workerThreads().contains(this)); | 83 ASSERT(workerThreads().contains(this)); |
| 201 workerThreads().remove(this); | 84 workerThreads().remove(this); |
| 202 } | 85 } |
| 203 | 86 |
| 204 void WorkerThread::start() | 87 bool WorkerThread::start() |
| 205 { | 88 { |
| 206 if (m_thread) | 89 // Mutex protection is necessary to ensure that m_threadID is initialized wh
en the thread starts. |
| 207 return; | 90 MutexLocker lock(m_threadCreationMutex); |
| 208 | 91 |
| 209 m_thread = adoptPtr(blink::Platform::current()->createThread("WebCore: Worke
r")); | 92 if (m_threadID) |
| 210 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(); |
| 211 } | 103 } |
| 212 | 104 |
| 213 void WorkerThread::interruptAndDispatchInspectorCommands() | 105 void WorkerThread::interruptAndDispatchInspectorCommands() |
| 214 { | 106 { |
| 215 MutexLocker locker(m_workerInspectorControllerMutex); | 107 MutexLocker locker(m_workerInspectorControllerMutex); |
| 216 if (m_workerInspectorController) | 108 if (m_workerInspectorController) |
| 217 m_workerInspectorController->interruptAndDispatchInspectorCommands(); | 109 m_workerInspectorController->interruptAndDispatchInspectorCommands(); |
| 218 } | 110 } |
| 219 | 111 |
| 220 void WorkerThread::initialize() | 112 void WorkerThread::workerThread() |
| 221 { | 113 { |
| 222 KURL scriptURL = m_startupData->m_scriptURL; | 114 KURL scriptURL = m_startupData->m_scriptURL; |
| 223 String sourceCode = m_startupData->m_sourceCode; | 115 String sourceCode = m_startupData->m_sourceCode; |
| 224 WorkerThreadStartMode startMode = m_startupData->m_startMode; | 116 WorkerThreadStartMode startMode = m_startupData->m_startMode; |
| 225 | 117 |
| 226 { | 118 { |
| 227 MutexLocker lock(m_threadCreationMutex); | 119 MutexLocker lock(m_threadCreationMutex); |
| 228 | |
| 229 ThreadState::attach(); | 120 ThreadState::attach(); |
| 230 m_workerGlobalScope = createWorkerGlobalScope(m_startupData.release()); | 121 m_workerGlobalScope = createWorkerGlobalScope(m_startupData.release()); |
| 122 m_runLoop.setWorkerGlobalScope(workerGlobalScope()); |
| 231 | 123 |
| 232 m_sharedTimer = adoptPtr(new WorkerSharedTimer(m_thread.get())); | 124 if (m_runLoop.terminated()) { |
| 233 PlatformThreadData::current().threadTimers().setSharedTimer(m_sharedTime
r.get()); | |
| 234 | |
| 235 if (m_terminated) { | |
| 236 // 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, |
| 237 // forbidExecution() couldn't be called from stop(). | 126 // forbidExecution() couldn't be called from stop(). |
| 238 m_workerGlobalScope->script()->forbidExecution(); | 127 m_workerGlobalScope->script()->forbidExecution(); |
| 239 } | 128 } |
| 240 } | 129 } |
| 241 | 130 // The corresponding call to didStopWorkerRunLoop is in |
| 242 // The corresponding call to didStopWorkerThread is in | |
| 243 // ~WorkerScriptController. | 131 // ~WorkerScriptController. |
| 244 blink::Platform::current()->didStartWorkerThread(m_thread.get()); | 132 blink::Platform::current()->didStartWorkerRunLoop(blink::WebWorkerRunLoop(&m
_runLoop)); |
| 245 | 133 |
| 246 // Notify proxy that a new WorkerGlobalScope has been created and started. | 134 // Notify proxy that a new WorkerGlobalScope has been created and started. |
| 247 m_workerReportingProxy.workerGlobalScopeStarted(m_workerGlobalScope.get()); | 135 m_workerReportingProxy.workerGlobalScopeStarted(m_workerGlobalScope.get()); |
| 248 | 136 |
| 249 WorkerScriptController* script = m_workerGlobalScope->script(); | 137 WorkerScriptController* script = m_workerGlobalScope->script(); |
| 250 if (!script->isExecutionForbidden()) | 138 if (!script->isExecutionForbidden()) |
| 251 script->initializeContextIfNeeded(); | 139 script->initializeContextIfNeeded(); |
| 252 InspectorInstrumentation::willEvaluateWorkerScript(workerGlobalScope(), star
tMode); | 140 InspectorInstrumentation::willEvaluateWorkerScript(workerGlobalScope(), star
tMode); |
| 253 script->evaluate(ScriptSourceCode(sourceCode, scriptURL)); | 141 script->evaluate(ScriptSourceCode(sourceCode, scriptURL)); |
| 254 | 142 |
| 255 postInitialize(); | 143 runEventLoop(); |
| 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(); | |
| 264 | 144 |
| 265 // This should be called before we start the shutdown procedure. | 145 // This should be called before we start the shutdown procedure. |
| 266 workerReportingProxy().willDestroyWorkerGlobalScope(); | 146 workerReportingProxy().willDestroyWorkerGlobalScope(); |
| 267 | 147 |
| 148 ThreadIdentifier threadID = m_threadID; |
| 149 |
| 268 // 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. |
| 269 // 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. |
| 270 // 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. |
| 271 #if !ENABLE(OILPAN) | 153 #if !ENABLE(OILPAN) |
| 272 ASSERT(m_workerGlobalScope->hasOneRef()); | 154 ASSERT(m_workerGlobalScope->hasOneRef()); |
| 273 #endif | 155 #endif |
| 274 m_workerGlobalScope->dispose(); | 156 m_workerGlobalScope->dispose(); |
| 275 m_workerGlobalScope = nullptr; | 157 m_workerGlobalScope = nullptr; |
| 276 | 158 |
| 277 // Detach the ThreadState, cleaning out the thread's heap by | 159 // Detach the ThreadState, cleaning out the thread's heap by |
| 278 // performing a final GC. The cleanup operation will at the end | 160 // performing a final GC. The cleanup operation will at the end |
| 279 // 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 |
| 280 // empty, there are still pointers into the heap and those | 162 // empty, there are still pointers into the heap and those |
| 281 // pointers will be dangling after thread termination because we | 163 // pointers will be dangling after thread termination because we |
| 282 // are destroying the heap. It is important to detach while the | 164 // are destroying the heap. It is important to detach while the |
| 283 // thread is still valid. In particular, finalizers for objects in | 165 // thread is still valid. In particular, finalizers for objects in |
| 284 // 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. |
| 285 ThreadState::detach(); | 167 ThreadState::detach(); |
| 286 | 168 |
| 287 // Notify the proxy that the WorkerGlobalScope has been disposed of. | 169 // Notify the proxy that the WorkerGlobalScope has been disposed of. |
| 288 // 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
. |
| 289 workerReportingProxy().workerGlobalScopeDestroyed(); | 171 workerReportingProxy().workerGlobalScopeDestroyed(); |
| 290 | 172 |
| 291 // Clean up PlatformThreadData before WTF::WTFThreadData goes away! | 173 // Clean up PlatformThreadData before WTF::WTFThreadData goes away! |
| 292 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(); |
| 293 } | 184 } |
| 294 | 185 |
| 295 class WorkerThreadShutdownFinishTask : public ExecutionContextTask { | 186 class WorkerThreadShutdownFinishTask : public ExecutionContextTask { |
| 296 public: | 187 public: |
| 297 static PassOwnPtr<WorkerThreadShutdownFinishTask> create() | 188 static PassOwnPtr<WorkerThreadShutdownFinishTask> create() |
| 298 { | 189 { |
| 299 return adoptPtr(new WorkerThreadShutdownFinishTask()); | 190 return adoptPtr(new WorkerThreadShutdownFinishTask()); |
| 300 } | 191 } |
| 301 | 192 |
| 302 virtual void performTask(ExecutionContext *context) | 193 virtual void performTask(ExecutionContext *context) |
| 303 { | 194 { |
| 304 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); | 195 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); |
| 305 workerGlobalScope->clearInspector(); | 196 workerGlobalScope->clearInspector(); |
| 306 // 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. |
| 307 workerGlobalScope->clearScript(); | 198 workerGlobalScope->clearScript(); |
| 308 workerGlobalScope->thread()->webThread()->postTask(new Task(WTF::bind(&W
orkerThread::cleanup, workerGlobalScope->thread()))); | |
| 309 } | 199 } |
| 310 | 200 |
| 311 virtual bool isCleanupTask() const { return true; } | 201 virtual bool isCleanupTask() const { return true; } |
| 312 }; | 202 }; |
| 313 | 203 |
| 314 class WorkerThreadShutdownStartTask : public ExecutionContextTask { | 204 class WorkerThreadShutdownStartTask : public ExecutionContextTask { |
| 315 public: | 205 public: |
| 316 static PassOwnPtr<WorkerThreadShutdownStartTask> create() | 206 static PassOwnPtr<WorkerThreadShutdownStartTask> create() |
| 317 { | 207 { |
| 318 return adoptPtr(new WorkerThreadShutdownStartTask()); | 208 return adoptPtr(new WorkerThreadShutdownStartTask()); |
| 319 } | 209 } |
| 320 | 210 |
| 321 virtual void performTask(ExecutionContext *context) | 211 virtual void performTask(ExecutionContext *context) |
| 322 { | 212 { |
| 323 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); | 213 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); |
| 324 workerGlobalScope->stopFetch(); | 214 workerGlobalScope->stopFetch(); |
| 325 workerGlobalScope->stopActiveDOMObjects(); | 215 workerGlobalScope->stopActiveDOMObjects(); |
| 326 PlatformThreadData::current().threadTimers().setSharedTimer(nullptr); | |
| 327 | 216 |
| 328 // 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, |
| 329 // which become dangling once Heap is destroyed. | 218 // which become dangling once Heap is destroyed. |
| 330 workerGlobalScope->removeAllEventListeners(); | 219 workerGlobalScope->removeAllEventListeners(); |
| 331 | 220 |
| 332 // 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 |
| 333 // with all the cleanup tasks the databases post first. | 222 // with all the cleanup tasks the databases post first. |
| 334 workerGlobalScope->postTask(WorkerThreadShutdownFinishTask::create()); | 223 workerGlobalScope->postTask(WorkerThreadShutdownFinishTask::create()); |
| 335 } | 224 } |
| 336 | 225 |
| 337 virtual bool isCleanupTask() const { return true; } | 226 virtual bool isCleanupTask() const { return true; } |
| 338 }; | 227 }; |
| 339 | 228 |
| 340 void WorkerThread::stop() | 229 void WorkerThread::stop() |
| 341 { | 230 { |
| 342 // 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. |
| 343 ThreadState::SafePointScope safePointScope(ThreadState::HeapPointersOnStack)
; | 232 ThreadState::SafePointScope safePointScope(ThreadState::HeapPointersOnStack)
; |
| 344 | 233 |
| 345 // 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. |
| 346 MutexLocker lock(m_threadCreationMutex); | 235 MutexLocker lock(m_threadCreationMutex); |
| 347 | 236 |
| 348 // If stop has already been called, just return. | |
| 349 if (m_terminated) | |
| 350 return; | |
| 351 | |
| 352 // Signal the thread to notify that the thread's stopping. | 237 // Signal the thread to notify that the thread's stopping. |
| 353 if (m_shutdownEvent) | 238 if (m_shutdownEvent) |
| 354 m_shutdownEvent->signal(); | 239 m_shutdownEvent->signal(); |
| 355 | 240 |
| 356 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()); |
| 357 return; | 246 return; |
| 358 | 247 } |
| 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. | 248 m_runLoop.terminate(); |
| 360 m_workerGlobalScope->script()->scheduleExecutionTermination(); | |
| 361 m_workerGlobalScope->wasRequestedToTerminate(); | |
| 362 InspectorInstrumentation::didKillAllExecutionContextTasks(m_workerGlobalScop
e.get()); | |
| 363 postTask(WorkerThreadShutdownStartTask::create()); | |
| 364 m_terminated = true; | |
| 365 } | 249 } |
| 366 | 250 |
| 367 bool WorkerThread::isCurrentThread() const | 251 bool WorkerThread::isCurrentThread() const |
| 368 { | 252 { |
| 369 return m_thread && m_thread->isCurrentThread(); | 253 return m_threadID == currentThread(); |
| 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); | |
| 385 } | 254 } |
| 386 | 255 |
| 387 void WorkerThread::postTask(PassOwnPtr<ExecutionContextTask> task) | 256 void WorkerThread::postTask(PassOwnPtr<ExecutionContextTask> task) |
| 388 { | 257 { |
| 389 m_thread->postTask(WorkerThreadTask::create(*this, task, true).leakPtr()); | 258 m_runLoop.postTask(task); |
| 390 } | 259 } |
| 391 | 260 |
| 392 void WorkerThread::postDebuggerTask(PassOwnPtr<ExecutionContextTask> task) | 261 void WorkerThread::postDebuggerTask(PassOwnPtr<ExecutionContextTask> task) |
| 393 { | 262 { |
| 394 m_debuggerMessageQueue.append(WorkerThreadTask::create(*this, task, false)); | 263 m_runLoop.postDebuggerTask(task); |
| 395 postTask(RunDebuggerQueueTask::create(this)); | |
| 396 } | 264 } |
| 397 | 265 |
| 398 MessageQueueWaitResult WorkerThread::runDebuggerTask(WaitMode waitMode) | 266 MessageQueueWaitResult WorkerThread::runDebuggerTask(WorkerRunLoop::WaitMode wai
tMode) |
| 399 { | 267 { |
| 400 ASSERT(isCurrentThread()); | 268 return m_runLoop.runDebuggerTask(waitMode); |
| 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()); | |
| 428 } | 269 } |
| 429 | 270 |
| 430 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke
rInspectorController) | 271 void WorkerThread::setWorkerInspectorController(WorkerInspectorController* worke
rInspectorController) |
| 431 { | 272 { |
| 432 MutexLocker locker(m_workerInspectorControllerMutex); | 273 MutexLocker locker(m_workerInspectorControllerMutex); |
| 433 m_workerInspectorController = workerInspectorController; | 274 m_workerInspectorController = workerInspectorController; |
| 434 } | 275 } |
| 435 | 276 |
| 436 } // namespace blink | 277 } // namespace blink |
| OLD | NEW |