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 "ThreadGlobalData.h"
37 #include "ThreadTimers.h"
38 #include "WorkerRunLoop.h"
39 #include "WorkerContext.h"
40 #include "WorkerThread.h"
44 class WorkerSharedTimer : public SharedTimer {
47 : m_sharedTimerFunction(0)
52 // SharedTimer interface.
53 virtual void setFiredFunction(void (*function)()) { m_sharedTimerFunction = function; }
54 virtual void setFireTime(double fireTime) { m_nextFireTime = fireTime; }
55 virtual void stop() { m_nextFireTime = 0; }
57 bool isActive() { return m_sharedTimerFunction && m_nextFireTime; }
58 double fireTime() { return m_nextFireTime; }
59 void fire() { m_sharedTimerFunction(); }
62 void (*m_sharedTimerFunction)();
63 double m_nextFireTime;
66 WorkerRunLoop::WorkerRunLoop()
67 : m_sharedTimer(new WorkerSharedTimer)
71 WorkerRunLoop::~WorkerRunLoop()
75 void WorkerRunLoop::run(WorkerContext* context)
78 ASSERT(context->thread());
79 ASSERT(context->thread()->threadID() == currentThread());
81 threadGlobalData().threadTimers().setSharedTimer(m_sharedTimer.get());
84 RefPtr<ScriptExecutionContext::Task> task;
85 MessageQueueWaitResult result;
87 if (m_sharedTimer->isActive())
88 result = m_messageQueue.waitForMessageTimed(task, m_sharedTimer->fireTime());
90 result = (m_messageQueue.waitForMessage(task) ? MessageQueueMessageReceived : MessageQueueTerminated);
92 if (result == MessageQueueTerminated)
95 if (result == MessageQueueMessageReceived)
96 task->performTask(context);
98 ASSERT(result == MessageQueueTimeout);
99 m_sharedTimer->fire();
103 threadGlobalData().threadTimers().setSharedTimer(0);
106 void WorkerRunLoop::terminate()
108 m_messageQueue.kill();
111 void WorkerRunLoop::postTask(PassRefPtr<ScriptExecutionContext::Task> task)
113 m_messageQueue.append(task);
116 } // namespace WebCore
118 #endif // ENABLE(WORKERS)