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: 47 WorkerSharedTimer() 48 : m_sharedTimerFunction(0) 49 , m_nextFireTime(0) 50 { 51 } 52 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; } 57 58 bool isActive() { return m_sharedTimerFunction && m_nextFireTime; } 59 double fireTime() { return m_nextFireTime; } 60 void fire() { m_sharedTimerFunction(); } 61 62 private: 63 void (*m_sharedTimerFunction)(); 64 double m_nextFireTime; 65 }; 66 67 class ModePredicate { 68 public: 69 ModePredicate(const String& mode) 70 : m_mode(mode) 71 , m_defaultMode(mode == WorkerRunLoop::defaultMode()) 72 { 73 } 74 75 bool isDefaultMode() const 76 { 77 return m_defaultMode; 78 } 79 80 bool operator()(WorkerRunLoop::Task* task) const 81 { 82 return m_defaultMode || m_mode == task->mode(); 83 } 84 85 private: 86 String m_mode; 87 bool m_defaultMode; 88 }; 89 90 WorkerRunLoop::WorkerRunLoop() 91 : m_sharedTimer(new WorkerSharedTimer) 92 , m_nestedCount(0) 93 , m_uniqueId(0) 94 { 95 } 96 97 WorkerRunLoop::~WorkerRunLoop() 98 { 99 ASSERT(!m_nestedCount); 100 } 101 102 String WorkerRunLoop::defaultMode() 103 { 104 return String(); 105 } 106 107 class RunLoopSetup { 108 WTF_MAKE_NONCOPYABLE(RunLoopSetup); 109 public: 110 RunLoopSetup(WorkerRunLoop& runLoop) 111 : m_runLoop(runLoop) 112 { 113 if (!m_runLoop.m_nestedCount) 114 threadGlobalData().threadTimers().setSharedTimer(m_runLoop.m_sharedTimer.get()); 115 m_runLoop.m_nestedCount++; 116 } 117 118 ~RunLoopSetup() 119 { 120 m_runLoop.m_nestedCount--; 121 if (!m_runLoop.m_nestedCount) 122 threadGlobalData().threadTimers().setSharedTimer(0); 123 } 124 private: 125 WorkerRunLoop& m_runLoop; 126 }; 127 128 void WorkerRunLoop::run(WorkerContext* context) 129 { 130 RunLoopSetup setup(*this); 131 ModePredicate modePredicate(defaultMode()); 132 MessageQueueWaitResult result; 133 do { 134 result = runInMode(context, modePredicate); 135 } while (result != MessageQueueTerminated); 136 } 137 138 MessageQueueWaitResult WorkerRunLoop::runInMode(WorkerContext* context, const String& mode) 139 { 140 RunLoopSetup setup(*this); 141 ModePredicate modePredicate(mode); 142 MessageQueueWaitResult result = runInMode(context, modePredicate); 143 return result; 144 } 145 146 MessageQueueWaitResult WorkerRunLoop::runInMode(WorkerContext* context, const ModePredicate& predicate) 147 { 148 ASSERT(context); 149 ASSERT(context->thread()); 150 ASSERT(context->thread()->threadID() == currentThread()); 151 152 double absoluteTime = (predicate.isDefaultMode() && m_sharedTimer->isActive()) ? m_sharedTimer->fireTime() : MessageQueue<Task>::infiniteTime(); 153 MessageQueueWaitResult result; 154 OwnPtr<WorkerRunLoop::Task> task = m_messageQueue.waitForMessageFilteredWithTimeout(result, predicate, absoluteTime); 155 156 // 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. 157 158 switch (result) { 159 case MessageQueueTerminated: 160 break; 161 162 case MessageQueueMessageReceived: 163 task->performTask(context); 164 break; 165 166 case MessageQueueTimeout: 167 if (!context->isClosing()) 168 m_sharedTimer->fire(); 169 break; 170 } 171 172 return result; 173 } 174 175 void WorkerRunLoop::terminate() 176 { 177 m_messageQueue.kill(); 178 } 179 180 void WorkerRunLoop::postTask(PassOwnPtr<ScriptExecutionContext::Task> task) 181 { 182 postTaskForMode(task, defaultMode()); 183 } 184 185 void WorkerRunLoop::postTaskForMode(PassOwnPtr<ScriptExecutionContext::Task> task, const String& mode) 186 { 187 m_messageQueue.append(Task::create(task, mode.crossThreadString())); 188 } 189 190 PassOwnPtr<WorkerRunLoop::Task> WorkerRunLoop::Task::create(PassOwnPtr<ScriptExecutionContext::Task> task, const String& mode) 191 { 192 return new Task(task, mode); 193 } 194 195 void WorkerRunLoop::Task::performTask(ScriptExecutionContext* context) 196 { 197 WorkerContext* workerContext = static_cast<WorkerContext *>(context); 198 if (!workerContext->isClosing() || m_task->isCleanupTask()) 199 m_task->performTask(context); 200 } 201 202 WorkerRunLoop::Task::Task(PassOwnPtr<ScriptExecutionContext::Task> task, const String& mode) 203 : m_task(task) 204 , m_mode(mode.crossThreadString()) 205 { 206 } 207 208 209 } // namespace WebCore 210 211 #endif // ENABLE(WORKERS) 212