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"
38 #include "WorkerContext.h"
39 #include "WorkerMessagingProxy.h"
40 #include "WorkerTask.h"
43 #include <wtf/Noncopyable.h>
46 struct WorkerThreadStartupData : Noncopyable {
48 static std::auto_ptr<WorkerThreadStartupData> create(const KURL& scriptURL, const String& userAgent, const String& sourceCode)
50 return std::auto_ptr<WorkerThreadStartupData>(new WorkerThreadStartupData(scriptURL, userAgent, sourceCode));
57 WorkerThreadStartupData(const KURL& scriptURL, const String& userAgent, const String& sourceCode);
60 WorkerThreadStartupData::WorkerThreadStartupData(const KURL& scriptURL, const String& userAgent, const String& sourceCode)
61 : m_scriptURL(scriptURL.copy())
62 , m_userAgent(userAgent.copy())
63 , m_sourceCode(sourceCode.copy())
67 PassRefPtr<WorkerThread> WorkerThread::create(const KURL& scriptURL, const String& userAgent, const String& sourceCode, WorkerMessagingProxy* messagingProxy)
69 return adoptRef(new WorkerThread(scriptURL, userAgent, sourceCode, messagingProxy));
72 WorkerThread::WorkerThread(const KURL& scriptURL, const String& userAgent, const String& sourceCode, WorkerMessagingProxy* messagingProxy)
74 , m_messagingProxy(messagingProxy)
75 , m_startupData(WorkerThreadStartupData::create(scriptURL, userAgent, sourceCode))
79 WorkerThread::~WorkerThread()
83 bool WorkerThread::start()
85 // Mutex protection is necessary to ensure that m_threadID is initialized when the thread starts.
86 MutexLocker lock(m_threadCreationMutex);
91 m_threadID = createThread(WorkerThread::workerThreadStart, this, "WebCore::Worker");
96 void* WorkerThread::workerThreadStart(void* thread)
98 return static_cast<WorkerThread*>(thread)->workerThread();
101 void* WorkerThread::workerThread()
104 MutexLocker lock(m_threadCreationMutex);
105 m_workerContext = WorkerContext::create(m_startupData->m_scriptURL, m_startupData->m_userAgent, this);
106 if (m_runLoop.terminated()) {
107 // The worker was terminated before the thread had a chance to run. Since the context didn't exist yet,
108 // forbidExecution() couldn't be called from stop().
109 m_workerContext->script()->forbidExecution();
113 WorkerScriptController* script = m_workerContext->script();
114 script->evaluate(ScriptSourceCode(m_startupData->m_sourceCode, m_startupData->m_scriptURL));
115 // Free the startup data to cause its member variable deref's happen on the worker's thread (since
116 // all ref/derefs of these objects are happening on the thread at this point). Note that
117 // WorkerThread::~WorkerThread happens on a different thread where it was created.
118 m_startupData.clear();
120 m_messagingProxy->confirmWorkerThreadMessage(m_workerContext->hasPendingActivity()); // This wasn't really a message, but it counts as one for GC.
122 // Blocks until terminated.
123 m_runLoop.run(m_workerContext.get());
125 ThreadIdentifier threadID = m_threadID;
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)