2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "core/workers/WorkerThread.h"
31 #include "bindings/v8/ScriptSourceCode.h"
32 #include "core/inspector/InspectorInstrumentation.h"
33 #include "core/workers/DedicatedWorkerGlobalScope.h"
34 #include "core/workers/WorkerClients.h"
35 #include "core/workers/WorkerReportingProxy.h"
36 #include "core/workers/WorkerThreadStartupData.h"
37 #include "platform/PlatformThreadData.h"
38 #include "platform/heap/ThreadState.h"
39 #include "platform/weborigin/KURL.h"
40 #include "public/platform/Platform.h"
41 #include "public/platform/WebWaitableEvent.h"
42 #include "public/platform/WebWorkerRunLoop.h"
43 #include "wtf/Noncopyable.h"
44 #include "wtf/text/WTFString.h"
50 static Mutex& threadSetMutex()
52 AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
56 static HashSet<WorkerThread*>& workerThreads()
58 DEFINE_STATIC_LOCAL(HashSet<WorkerThread*>, threads, ());
62 unsigned WorkerThread::workerThreadCount()
64 MutexLocker lock(threadSetMutex());
65 return workerThreads().size();
68 WorkerThread::WorkerThread(WorkerLoaderProxy& workerLoaderProxy, WorkerReportingProxy& workerReportingProxy, PassOwnPtrWillBeRawPtr<WorkerThreadStartupData> startupData)
70 , m_workerLoaderProxy(workerLoaderProxy)
71 , m_workerReportingProxy(workerReportingProxy)
72 , m_startupData(startupData)
73 , m_notificationClient(0)
74 , m_shutdownEvent(adoptPtr(blink::Platform::current()->createWaitableEvent()))
76 MutexLocker lock(threadSetMutex());
77 workerThreads().add(this);
80 WorkerThread::~WorkerThread()
82 MutexLocker lock(threadSetMutex());
83 ASSERT(workerThreads().contains(this));
84 workerThreads().remove(this);
87 bool WorkerThread::start()
89 // Mutex protection is necessary to ensure that m_threadID is initialized when the thread starts.
90 MutexLocker lock(m_threadCreationMutex);
95 m_threadID = createThread(WorkerThread::workerThreadStart, this, "WebCore: Worker");
100 void WorkerThread::workerThreadStart(void* thread)
102 static_cast<WorkerThread*>(thread)->workerThread();
105 void WorkerThread::workerThread()
107 KURL scriptURL = m_startupData->m_scriptURL;
108 String sourceCode = m_startupData->m_sourceCode;
109 WorkerThreadStartMode startMode = m_startupData->m_startMode;
112 MutexLocker lock(m_threadCreationMutex);
113 ThreadState::attach();
114 m_workerGlobalScope = createWorkerGlobalScope(m_startupData.release());
115 m_runLoop.setWorkerGlobalScope(workerGlobalScope());
117 if (m_runLoop.terminated()) {
118 // The worker was terminated before the thread had a chance to run. Since the context didn't exist yet,
119 // forbidExecution() couldn't be called from stop().
120 m_workerGlobalScope->script()->forbidExecution();
123 // The corresponding call to didStopWorkerRunLoop is in
124 // ~WorkerScriptController.
125 blink::Platform::current()->didStartWorkerRunLoop(blink::WebWorkerRunLoop(&m_runLoop));
127 // Notify proxy that a new WorkerGlobalScope has been created and started.
128 m_workerReportingProxy.workerGlobalScopeStarted(m_workerGlobalScope.get());
130 WorkerScriptController* script = m_workerGlobalScope->script();
131 InspectorInstrumentation::willEvaluateWorkerScript(workerGlobalScope(), startMode);
132 script->evaluate(ScriptSourceCode(sourceCode, scriptURL));
136 // This should be called before we start the shutdown procedure.
137 workerReportingProxy().willDestroyWorkerGlobalScope();
139 ThreadIdentifier threadID = m_threadID;
141 // The below assignment will destroy the context, which will in turn notify messaging proxy.
142 // We cannot let any objects survive past thread exit, because no other thread will run GC or otherwise destroy them.
143 // If Oilpan is enabled, we detach of the context/global scope, with the final heap cleanup below sweeping it out.
145 ASSERT(m_workerGlobalScope->hasOneRef());
147 m_workerGlobalScope->dispose();
148 m_workerGlobalScope = nullptr;
150 // Detach the ThreadState, cleaning out the thread's heap by
151 // performing a final GC. The cleanup operation will at the end
152 // assert that the heap is empty. If the heap does not become
153 // empty, there are still pointers into the heap and those
154 // pointers will be dangling after thread termination because we
155 // are destroying the heap. It is important to detach while the
156 // thread is still valid. In particular, finalizers for objects in
157 // the heap for this thread will need to access thread local data.
158 ThreadState::detach();
160 // Notify the proxy that the WorkerGlobalScope has been disposed of.
161 // This can free this thread object, hence it must not be touched afterwards.
162 workerReportingProxy().workerGlobalScopeDestroyed();
164 // Clean up PlatformThreadData before WTF::WTFThreadData goes away!
165 PlatformThreadData::current().destroy();
167 // The thread object may be already destroyed from notification now, don't try to access "this".
168 detachThread(threadID);
171 void WorkerThread::runEventLoop()
173 // Does not return until terminated.
177 class WorkerThreadShutdownFinishTask : public ExecutionContextTask {
179 static PassOwnPtr<WorkerThreadShutdownFinishTask> create()
181 return adoptPtr(new WorkerThreadShutdownFinishTask());
184 virtual void performTask(ExecutionContext *context)
186 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context);
187 workerGlobalScope->clearInspector();
188 // It's not safe to call clearScript until all the cleanup tasks posted by functions initiated by WorkerThreadShutdownStartTask have completed.
189 workerGlobalScope->clearScript();
192 virtual bool isCleanupTask() const { return true; }
195 class WorkerThreadShutdownStartTask : public ExecutionContextTask {
197 static PassOwnPtr<WorkerThreadShutdownStartTask> create()
199 return adoptPtr(new WorkerThreadShutdownStartTask());
202 virtual void performTask(ExecutionContext *context)
204 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context);
206 workerGlobalScope->stopActiveDOMObjects();
208 // Event listeners would keep DOMWrapperWorld objects alive for too long. Also, they have references to JS objects,
209 // which become dangling once Heap is destroyed.
210 workerGlobalScope->removeAllEventListeners();
212 // Stick a shutdown command at the end of the queue, so that we deal
213 // with all the cleanup tasks the databases post first.
214 workerGlobalScope->postTask(WorkerThreadShutdownFinishTask::create());
217 virtual bool isCleanupTask() const { return true; }
220 void WorkerThread::stop()
222 // Prevent the deadlock between GC and an attempt to stop a thread.
223 ThreadState::SafePointScope safePointScope(ThreadState::HeapPointersOnStack);
225 // Mutex protection is necessary because stop() can be called before the context is fully created.
226 MutexLocker lock(m_threadCreationMutex);
228 // Signal the thread to notify that the thread's stopping.
230 m_shutdownEvent->signal();
232 // Ensure that tasks are being handled by thread event loop. If script execution weren't forbidden, a while(1) loop in JS could keep the thread alive forever.
233 if (m_workerGlobalScope) {
234 m_workerGlobalScope->script()->scheduleExecutionTermination();
235 m_workerGlobalScope->wasRequestedToTerminate();
236 m_runLoop.postTaskAndTerminate(WorkerThreadShutdownStartTask::create());
239 m_runLoop.terminate();
242 bool WorkerThread::isCurrentThread() const
244 return m_threadID == currentThread();
247 } // namespace WebCore