2 * Copyright (C) 2009 Google 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 are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "ScriptExecutionContext.h"
36 #include "SharedTimer.h"
37 #include "ThreadGlobalData.h"
38 #include "ThreadTimers.h"
39 #include "WorkerRunLoop.h"
40 #include "WorkerContext.h"
41 #include "WorkerThread.h"
42 #include <wtf/CurrentTime.h>
46 class WorkerSharedTimer : public SharedTimer {
49 : m_sharedTimerFunction(0)
54 // SharedTimer interface.
55 virtual void setFiredFunction(void (*function)()) { m_sharedTimerFunction = function; }
56 virtual void setFireInterval(double interval) { m_nextFireTime = interval + currentTime(); }
57 virtual void stop() { m_nextFireTime = 0; }
59 bool isActive() { return m_sharedTimerFunction && m_nextFireTime; }
60 double fireTime() { return m_nextFireTime; }
61 void fire() { m_sharedTimerFunction(); }
64 void (*m_sharedTimerFunction)();
65 double m_nextFireTime;
70 ModePredicate(const String& mode)
72 , m_defaultMode(mode == WorkerRunLoop::defaultMode())
76 bool isDefaultMode() const
81 bool operator()(WorkerRunLoop::Task* task) const
83 return m_defaultMode || m_mode == task->mode();
91 WorkerRunLoop::WorkerRunLoop()
92 : m_sharedTimer(adoptPtr(new WorkerSharedTimer))
98 WorkerRunLoop::~WorkerRunLoop()
100 ASSERT(!m_nestedCount);
103 String WorkerRunLoop::defaultMode()
109 WTF_MAKE_NONCOPYABLE(RunLoopSetup);
111 RunLoopSetup(WorkerRunLoop& runLoop)
114 if (!m_runLoop.m_nestedCount)
115 threadGlobalData().threadTimers().setSharedTimer(m_runLoop.m_sharedTimer.get());
116 m_runLoop.m_nestedCount++;
121 m_runLoop.m_nestedCount--;
122 if (!m_runLoop.m_nestedCount)
123 threadGlobalData().threadTimers().setSharedTimer(0);
126 WorkerRunLoop& m_runLoop;
129 void WorkerRunLoop::run(WorkerContext* context)
131 RunLoopSetup setup(*this);
132 ModePredicate modePredicate(defaultMode());
133 MessageQueueWaitResult result;
135 result = runInMode(context, modePredicate);
136 } while (result != MessageQueueTerminated);
139 MessageQueueWaitResult WorkerRunLoop::runInMode(WorkerContext* context, const String& mode)
141 RunLoopSetup setup(*this);
142 ModePredicate modePredicate(mode);
143 MessageQueueWaitResult result = runInMode(context, modePredicate);
147 MessageQueueWaitResult WorkerRunLoop::runInMode(WorkerContext* context, const ModePredicate& predicate)
150 ASSERT(context->thread());
151 ASSERT(context->thread()->threadID() == currentThread());
153 double absoluteTime = (predicate.isDefaultMode() && m_sharedTimer->isActive()) ? m_sharedTimer->fireTime() : MessageQueue<Task>::infiniteTime();
154 MessageQueueWaitResult result;
155 OwnPtr<WorkerRunLoop::Task> task = m_messageQueue.waitForMessageFilteredWithTimeout(result, predicate, absoluteTime);
157 // If the context is closing, don't execute any further JavaScript tasks (per section 4.1.1 of the Web Workers spec). However, there may be implementation cleanup tasks in the queue, so keep running through it.
160 case MessageQueueTerminated:
163 case MessageQueueMessageReceived:
164 task->performTask(context);
167 case MessageQueueTimeout:
168 if (!context->isClosing())
169 m_sharedTimer->fire();
176 void WorkerRunLoop::terminate()
178 m_messageQueue.kill();
181 void WorkerRunLoop::postTask(PassOwnPtr<ScriptExecutionContext::Task> task)
183 postTaskForMode(task, defaultMode());
186 void WorkerRunLoop::postTaskForMode(PassOwnPtr<ScriptExecutionContext::Task> task, const String& mode)
188 m_messageQueue.append(Task::create(task, mode.crossThreadString()));
191 PassOwnPtr<WorkerRunLoop::Task> WorkerRunLoop::Task::create(PassOwnPtr<ScriptExecutionContext::Task> task, const String& mode)
193 return adoptPtr(new Task(task, mode));
196 void WorkerRunLoop::Task::performTask(ScriptExecutionContext* context)
198 WorkerContext* workerContext = static_cast<WorkerContext *>(context);
199 if (!workerContext->isClosing() || m_task->isCleanupTask())
200 m_task->performTask(context);
203 WorkerRunLoop::Task::Task(PassOwnPtr<ScriptExecutionContext::Task> task, const String& mode)
205 , m_mode(mode.crossThreadString())
210 } // namespace WebCore
212 #endif // ENABLE(WORKERS)