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.
31 #include "WorkerThread.h"
34 #include "PlatformString.h"
35 #include "ScriptSourceCode.h"
36 #include "ScriptValue.h"
37 #include "WorkerContext.h"
38 #include "WorkerObjectProxy.h"
41 #include <wtf/Noncopyable.h>
44 struct WorkerThreadStartupData : Noncopyable {
46 static std::auto_ptr<WorkerThreadStartupData> create(const KURL& scriptURL, const String& userAgent, const String& sourceCode)
48 return std::auto_ptr<WorkerThreadStartupData>(new WorkerThreadStartupData(scriptURL, userAgent, sourceCode));
55 WorkerThreadStartupData(const KURL& scriptURL, const String& userAgent, const String& sourceCode);
58 WorkerThreadStartupData::WorkerThreadStartupData(const KURL& scriptURL, const String& userAgent, const String& sourceCode)
59 : m_scriptURL(scriptURL.copy())
60 , m_userAgent(userAgent.copy())
61 , m_sourceCode(sourceCode.copy())
65 PassRefPtr<WorkerThread> WorkerThread::create(const KURL& scriptURL, const String& userAgent, const String& sourceCode, WorkerLoaderProxy& workerLoaderProxy, WorkerObjectProxy& workerObjectProxy)
67 return adoptRef(new WorkerThread(scriptURL, userAgent, sourceCode, workerLoaderProxy, workerObjectProxy));
70 WorkerThread::WorkerThread(const KURL& scriptURL, const String& userAgent, const String& sourceCode, WorkerLoaderProxy& workerLoaderProxy, WorkerObjectProxy& workerObjectProxy)
72 , m_workerLoaderProxy(workerLoaderProxy)
73 , m_workerObjectProxy(workerObjectProxy)
74 , m_startupData(WorkerThreadStartupData::create(scriptURL, userAgent, sourceCode))
78 WorkerThread::~WorkerThread()
82 bool WorkerThread::start()
84 // Mutex protection is necessary to ensure that m_threadID is initialized when the thread starts.
85 MutexLocker lock(m_threadCreationMutex);
90 m_threadID = createThread(WorkerThread::workerThreadStart, this, "WebCore: Worker");
95 void* WorkerThread::workerThreadStart(void* thread)
97 return static_cast<WorkerThread*>(thread)->workerThread();
100 void* WorkerThread::workerThread()
103 MutexLocker lock(m_threadCreationMutex);
104 m_workerContext = WorkerContext::create(m_startupData->m_scriptURL, m_startupData->m_userAgent, this);
105 if (m_runLoop.terminated()) {
106 // The worker was terminated before the thread had a chance to run. Since the context didn't exist yet,
107 // forbidExecution() couldn't be called from stop().
108 m_workerContext->script()->forbidExecution();
112 WorkerScriptController* script = m_workerContext->script();
113 script->evaluate(ScriptSourceCode(m_startupData->m_sourceCode, m_startupData->m_scriptURL));
114 // Free the startup data to cause its member variable deref's happen on the worker's thread (since
115 // all ref/derefs of these objects are happening on the thread at this point). Note that
116 // WorkerThread::~WorkerThread happens on a different thread where it was created.
117 m_startupData.clear();
119 m_workerObjectProxy.reportPendingActivity(m_workerContext->hasPendingActivity());
121 // Blocks until terminated.
122 m_runLoop.run(m_workerContext.get());
124 ThreadIdentifier threadID = m_threadID;
126 m_workerContext->stopActiveDOMObjects();
127 m_workerContext->clearScript();
128 ASSERT(m_workerContext->hasOneRef());
129 // The below assignment will destroy the context, which will in turn notify messaging proxy.
130 // We cannot let any objects survive past thread exit, because no other thread will run GC or otherwise destroy them.
133 // The thread object may be already destroyed from notification now, don't try to access "this".
134 detachThread(threadID);
139 void WorkerThread::stop()
141 // Mutex protection is necessary because stop() can be called before the context is fully created.
142 MutexLocker lock(m_threadCreationMutex);
144 // 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.
146 m_workerContext->script()->forbidExecution();
148 // FIXME: Rudely killing the thread won't work when we allow nested workers, because they will try to post notifications of their destruction.
149 m_runLoop.terminate();
152 } // namespace WebCore
154 #endif // ENABLE(WORKERS)