1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31 #include "config.h"
32
33 #if ENABLE(WORKERS)
34
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
43 namespace WebCore {
44
45 class WorkerSharedTimer : public SharedTimer {
46 public:
WorkerSharedTimer()47 WorkerSharedTimer()
48 : m_sharedTimerFunction(0)
49 , m_nextFireTime(0)
50 {
51 }
52
53 // SharedTimer interface.
setFiredFunction(void (* function)())54 virtual void setFiredFunction(void (*function)()) { m_sharedTimerFunction = function; }
setFireTime(double fireTime)55 virtual void setFireTime(double fireTime) { m_nextFireTime = fireTime; }
stop()56 virtual void stop() { m_nextFireTime = 0; }
57
isActive()58 bool isActive() { return m_sharedTimerFunction && m_nextFireTime; }
fireTime()59 double fireTime() { return m_nextFireTime; }
fire()60 void fire() { m_sharedTimerFunction(); }
61
62 private:
63 void (*m_sharedTimerFunction)();
64 double m_nextFireTime;
65 };
66
67 class WorkerRunLoop::Task : public RefCounted<Task> {
68 public:
create(PassRefPtr<ScriptExecutionContext::Task> task,const String & mode)69 static PassRefPtr<Task> create(PassRefPtr<ScriptExecutionContext::Task> task, const String& mode)
70 {
71 return adoptRef(new Task(task, mode));
72 }
73
mode() const74 const String& mode() const { return m_mode; }
performTask(ScriptExecutionContext * context)75 void performTask(ScriptExecutionContext* context) { m_task->performTask(context); }
76
77 private:
Task(PassRefPtr<ScriptExecutionContext::Task> task,const String & mode)78 Task(PassRefPtr<ScriptExecutionContext::Task> task, const String& mode)
79 : m_task(task)
80 , m_mode(mode.copy())
81 {
82 }
83
84 RefPtr<ScriptExecutionContext::Task> m_task;
85 String m_mode;
86 };
87
88 class ModePredicate {
89 public:
ModePredicate(const String & mode)90 ModePredicate(const String& mode)
91 : m_mode(mode)
92 , m_defaultMode(mode == WorkerRunLoop::defaultMode())
93 {
94 }
95
isDefaultMode() const96 bool isDefaultMode() const
97 {
98 return m_defaultMode;
99 }
100
operator ()(PassRefPtr<WorkerRunLoop::Task> task) const101 bool operator()(PassRefPtr<WorkerRunLoop::Task> task) const
102 {
103 return m_defaultMode || m_mode == task->mode();
104 }
105
106 private:
107 String m_mode;
108 bool m_defaultMode;
109 };
110
WorkerRunLoop()111 WorkerRunLoop::WorkerRunLoop()
112 : m_sharedTimer(new WorkerSharedTimer)
113 , m_nestedCount(0)
114 , m_uniqueId(0)
115 {
116 }
117
~WorkerRunLoop()118 WorkerRunLoop::~WorkerRunLoop()
119 {
120 ASSERT(!m_nestedCount);
121 }
122
defaultMode()123 String WorkerRunLoop::defaultMode()
124 {
125 return String();
126 }
127
128 class RunLoopSetup : public Noncopyable {
129 public:
RunLoopSetup(WorkerRunLoop & runLoop)130 RunLoopSetup(WorkerRunLoop& runLoop)
131 : m_runLoop(runLoop)
132 {
133 if (!m_runLoop.m_nestedCount)
134 threadGlobalData().threadTimers().setSharedTimer(m_runLoop.m_sharedTimer.get());
135 m_runLoop.m_nestedCount++;
136 }
137
~RunLoopSetup()138 ~RunLoopSetup()
139 {
140 m_runLoop.m_nestedCount--;
141 if (!m_runLoop.m_nestedCount)
142 threadGlobalData().threadTimers().setSharedTimer(0);
143 }
144 private:
145 WorkerRunLoop& m_runLoop;
146 };
147
run(WorkerContext * context)148 void WorkerRunLoop::run(WorkerContext* context)
149 {
150 RunLoopSetup setup(*this);
151 ModePredicate modePredicate(defaultMode());
152 MessageQueueWaitResult result;
153 do {
154 result = runInMode(context, modePredicate);
155 } while (result != MessageQueueTerminated);
156 }
157
runInMode(WorkerContext * context,const String & mode)158 MessageQueueWaitResult WorkerRunLoop::runInMode(WorkerContext* context, const String& mode)
159 {
160 RunLoopSetup setup(*this);
161 ModePredicate modePredicate(mode);
162 MessageQueueWaitResult result = runInMode(context, modePredicate);
163 return result;
164 }
165
runInMode(WorkerContext * context,const ModePredicate & predicate)166 MessageQueueWaitResult WorkerRunLoop::runInMode(WorkerContext* context, const ModePredicate& predicate)
167 {
168 ASSERT(context);
169 ASSERT(context->thread());
170 ASSERT(context->thread()->threadID() == currentThread());
171
172 double absoluteTime = (predicate.isDefaultMode() && m_sharedTimer->isActive()) ? m_sharedTimer->fireTime() : MessageQueue<RefPtr<Task> >::infiniteTime();
173 RefPtr<Task> task;
174 MessageQueueWaitResult result = m_messageQueue.waitForMessageFilteredWithTimeout(task, predicate, absoluteTime);
175
176 switch (result) {
177 case MessageQueueTerminated:
178 break;
179
180 case MessageQueueMessageReceived:
181 task->performTask(context);
182 break;
183
184 case MessageQueueTimeout:
185 m_sharedTimer->fire();
186 break;
187 }
188
189 return result;
190 }
191
terminate()192 void WorkerRunLoop::terminate()
193 {
194 m_messageQueue.kill();
195 }
196
postTask(PassRefPtr<ScriptExecutionContext::Task> task)197 void WorkerRunLoop::postTask(PassRefPtr<ScriptExecutionContext::Task> task)
198 {
199 postTaskForMode(task, defaultMode());
200 }
201
postTaskForMode(PassRefPtr<ScriptExecutionContext::Task> task,const String & mode)202 void WorkerRunLoop::postTaskForMode(PassRefPtr<ScriptExecutionContext::Task> task, const String& mode)
203 {
204 m_messageQueue.append(Task::create(task, mode.copy()));
205 }
206
207 } // namespace WebCore
208
209 #endif // ENABLE(WORKERS)
210