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"
45 class WorkerSharedTimer : public SharedTimer {
48 : m_sharedTimerFunction(0)
53 // SharedTimer interface.
54 virtual void setFiredFunction(void (*function)()) { m_sharedTimerFunction = function; }
55 virtual void setFireTime(double fireTime) { m_nextFireTime = fireTime; }
56 virtual void stop() { m_nextFireTime = 0; }
58 bool isActive() { return m_sharedTimerFunction && m_nextFireTime; }
59 double fireTime() { return m_nextFireTime; }
60 void fire() { m_sharedTimerFunction(); }
63 void (*m_sharedTimerFunction)();
64 double m_nextFireTime;
67 WorkerRunLoop::WorkerRunLoop()
68 : m_sharedTimer(new WorkerSharedTimer)
72 WorkerRunLoop::~WorkerRunLoop()
76 void WorkerRunLoop::run(WorkerContext* context)
79 ASSERT(context->thread());
80 ASSERT(context->thread()->threadID() == currentThread());
82 threadGlobalData().threadTimers().setSharedTimer(m_sharedTimer.get());
85 RefPtr<ScriptExecutionContext::Task> task;
86 MessageQueueWaitResult result;
88 if (m_sharedTimer->isActive())
89 result = m_messageQueue.waitForMessageTimed(task, m_sharedTimer->fireTime());
91 result = (m_messageQueue.waitForMessage(task) ? MessageQueueMessageReceived : MessageQueueTerminated);
93 if (result == MessageQueueTerminated)
96 if (result == MessageQueueMessageReceived)
97 task->performTask(context);
99 ASSERT(result == MessageQueueTimeout);
100 m_sharedTimer->fire();
104 threadGlobalData().threadTimers().setSharedTimer(0);
107 void WorkerRunLoop::terminate()
109 m_messageQueue.kill();
112 void WorkerRunLoop::postTask(PassRefPtr<ScriptExecutionContext::Task> task)
114 m_messageQueue.append(task);
117 } // namespace WebCore
119 #endif // ENABLE(WORKERS)