| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (C) 2014 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 |
| 6 * are met: |
| 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright |
| 11 * notice, this list of conditions and the following disclaimer in the |
| 12 * documentation and/or other materials provided with the distribution. |
| 13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
| 14 * its contributors may be used to endorse or promote products derived |
| 15 * from this software without specific prior written permission. |
| 16 * |
| 17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 */ |
| 28 |
| 29 #include "config.h" |
| 30 #include "platform/scheduler/Scheduler.h" |
| 31 |
| 32 #include "platform/Task.h" |
| 33 #include "platform/TraceEvent.h" |
| 34 #include "public/platform/Platform.h" |
| 35 |
| 36 namespace WebCore { |
| 37 |
| 38 Scheduler* Scheduler::s_currentScheduler = 0; |
| 39 |
| 40 void Scheduler::initializeOnMainThread() |
| 41 { |
| 42 s_currentScheduler = new Scheduler(); |
| 43 } |
| 44 |
| 45 void Scheduler::shutdown() |
| 46 { |
| 47 delete s_currentScheduler; |
| 48 s_currentScheduler = nullptr; |
| 49 } |
| 50 |
| 51 Scheduler* Scheduler::current() |
| 52 { |
| 53 return s_currentScheduler; |
| 54 } |
| 55 |
| 56 Scheduler::Scheduler() |
| 57 : m_mainThread(blink::Platform::current()->currentThread()) |
| 58 , m_sharedTimerFunction(nullptr) |
| 59 , m_workScheduled(false) |
| 60 , m_hasPendingBeginFrameTask(false) |
| 61 { |
| 62 } |
| 63 |
| 64 Scheduler::~Scheduler() |
| 65 { |
| 66 runScheduledTasks(); |
| 67 ASSERT(!chooseNextTask()); |
| 68 } |
| 69 |
| 70 void Scheduler::scheduleTask(TaskQueue* queue, blink::WebThread::Task* task) |
| 71 { |
| 72 MutexLocker lock(m_taskQueueLock); |
| 73 queue->push_back(task); |
| 74 if (m_workScheduled) |
| 75 return; |
| 76 m_workScheduled = true; |
| 77 m_mainThread->postTask(new Task(WTF::bind(&Scheduler::runScheduledTasks, thi
s))); |
| 78 } |
| 79 |
| 80 void Scheduler::runScheduledTasks() |
| 81 { |
| 82 TRACE_EVENT0("blink", "Scheduler::runScheduledTasks"); |
| 83 { |
| 84 MutexLocker lock(m_taskQueueLock); |
| 85 m_workScheduled = false; |
| 86 m_hasPendingBeginFrameTask = false; |
| 87 } |
| 88 while (OwnPtr<blink::WebThread::Task> task = chooseNextTask()) |
| 89 task->run(); |
| 90 } |
| 91 |
| 92 PassOwnPtr<blink::WebThread::Task> Scheduler::chooseNextTask() |
| 93 { |
| 94 MutexLocker lock(m_taskQueueLock); |
| 95 TaskQueue* queues[] = { |
| 96 &m_pendingInputTasks, |
| 97 &m_pendingCompositorTasks, |
| 98 &m_pendingTasks, |
| 99 }; |
| 100 |
| 101 for (size_t i = 0; i < arraysize(queues); i++) { |
| 102 if (!queues[i]->size()) |
| 103 continue; |
| 104 OwnPtr<blink::WebThread::Task> task = adoptPtr(queues[i]->front()); |
| 105 queues[i]->pop_front(); |
| 106 return task.release(); |
| 107 } |
| 108 return nullptr; |
| 109 } |
| 110 |
| 111 void Scheduler::runSharedTimer() |
| 112 { |
| 113 TRACE_EVENT0("blink", "Scheduler::runSharedTimer"); |
| 114 if (shouldYieldForHighPriorityWork()) { |
| 115 postTask(new Task(WTF::bind(&Scheduler::runSharedTimer, this))); |
| 116 return; |
| 117 } |
| 118 if (m_sharedTimerFunction) |
| 119 m_sharedTimerFunction(); |
| 120 } |
| 121 |
| 122 void Scheduler::postTask(blink::WebThread::Task* task) |
| 123 { |
| 124 scheduleTask(&m_pendingTasks, task); |
| 125 } |
| 126 |
| 127 void Scheduler::postInputTask(blink::WebThread::Task* task) |
| 128 { |
| 129 scheduleTask(&m_pendingInputTasks, task); |
| 130 } |
| 131 |
| 132 void Scheduler::postCompositorTask(blink::WebThread::Task* task) |
| 133 { |
| 134 scheduleTask(&m_pendingCompositorTasks, task); |
| 135 } |
| 136 |
| 137 void Scheduler::postBeginFrameTask(blink::WebThread::Task* task, double frameTim
e, double deadline, double interval) |
| 138 { |
| 139 { |
| 140 MutexLocker lock(m_taskQueueLock); |
| 141 m_hasPendingBeginFrameTask = true; |
| 142 } |
| 143 postCompositorTask(task); |
| 144 } |
| 145 |
| 146 void Scheduler::sharedTimerAdapter() |
| 147 { |
| 148 current()->runSharedTimer(); |
| 149 } |
| 150 |
| 151 void Scheduler::setSharedTimerFiredFunction(void (*function)()) |
| 152 { |
| 153 m_sharedTimerFunction = function; |
| 154 blink::Platform::current()->setSharedTimerFiredFunction(function ? &Schedule
r::sharedTimerAdapter : nullptr); |
| 155 } |
| 156 |
| 157 void Scheduler::setSharedTimerFireInterval(double interval) |
| 158 { |
| 159 blink::Platform::current()->setSharedTimerFireInterval(interval); |
| 160 } |
| 161 |
| 162 void Scheduler::stopSharedTimer() |
| 163 { |
| 164 blink::Platform::current()->stopSharedTimer(); |
| 165 } |
| 166 |
| 167 bool Scheduler::shouldYieldForHighPriorityWork() |
| 168 { |
| 169 // TODO: Use atomics. |
| 170 MutexLocker lock(m_taskQueueLock); |
| 171 bool yield = m_pendingInputTasks.size() || m_hasPendingBeginFrameTask; |
| 172 TRACE_EVENT_INSTANT1("blink", "Scheduler::shouldYieldForHighPriorityWork", "
yield", yield); |
| 173 return yield; |
| 174 } |
| 175 |
| 176 } |
| OLD | NEW |