Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "platform/scheduler/Scheduler.h" | 6 #include "platform/scheduler/Scheduler.h" |
| 7 | 7 |
| 8 #include "platform/PlatformThreadData.h" | |
| 8 #include "platform/Task.h" | 9 #include "platform/Task.h" |
| 10 #include "platform/ThreadTimers.h" | |
| 9 #include "platform/TraceEvent.h" | 11 #include "platform/TraceEvent.h" |
| 10 #include "platform/TraceLocation.h" | |
| 11 #include "public/platform/Platform.h" | 12 #include "public/platform/Platform.h" |
| 12 #include "public/platform/WebThread.h" | 13 #include "wtf/ThreadingPrimitives.h" |
| 13 | 14 |
| 14 namespace blink { | 15 namespace blink { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 class MainThreadTaskAdapter : public blink::WebThread::Task { | 19 // Can be created from any thread. |
| 20 // Note if the scheduler gets shutdown, this may be run after. | |
| 21 class MainThreadIdleTaskAdapter : public WebThread::Task { | |
| 19 public: | 22 public: |
| 20 explicit MainThreadTaskAdapter(const TraceLocation& location, const Schedule r::Task& task) | 23 MainThreadIdleTaskAdapter(const Scheduler::IdleTask& idleTask, double allott edTimeMs, const TraceLocation& location) |
| 21 : m_location(location) | 24 : m_idleTask(idleTask) |
| 22 , m_task(task) | 25 , m_allottedTimeMs(allottedTimeMs) |
| 26 , m_location(location) | |
| 23 { | 27 { |
| 24 } | 28 } |
| 25 | 29 |
| 26 // WebThread::Task implementation. | 30 // WebThread::Task implementation. |
| 27 virtual void run() OVERRIDE | 31 virtual void run() OVERRIDE |
| 28 { | 32 { |
| 29 TRACE_EVENT2("blink", "MainThreadTaskAdapter::run", | 33 TRACE_EVENT2("blink", "MainThreadIdleTaskAdapter::run", |
| 30 "src_file", m_location.fileName(), | 34 "src_file", m_location.fileName(), |
| 31 "src_func", m_location.functionName()); | 35 "src_func", m_location.functionName()); |
| 32 m_task(); | |
| 33 } | |
| 34 | |
| 35 private: | |
| 36 const TraceLocation m_location; | |
| 37 Scheduler::Task m_task; | |
| 38 }; | |
| 39 | |
| 40 class MainThreadIdleTaskAdapter : public blink::WebThread::Task { | |
| 41 public: | |
| 42 MainThreadIdleTaskAdapter(const Scheduler::IdleTask& idleTask, double allott edTimeMs) | |
| 43 : m_idleTask(idleTask) | |
| 44 , m_allottedTimeMs(allottedTimeMs) | |
| 45 { | |
| 46 } | |
| 47 | |
| 48 // WebThread::Task implementation. | |
| 49 virtual void run() OVERRIDE | |
| 50 { | |
| 51 TRACE_EVENT1("blink", "MainThreadIdleTaskAdapter::run", "allottedTime", m_allottedTimeMs); | |
| 52 m_idleTask(m_allottedTimeMs); | 36 m_idleTask(m_allottedTimeMs); |
| 53 } | 37 } |
| 54 | 38 |
| 55 private: | 39 private: |
| 56 Scheduler::IdleTask m_idleTask; | 40 Scheduler::IdleTask m_idleTask; |
| 57 double m_allottedTimeMs; | 41 double m_allottedTimeMs; |
| 42 TraceLocation m_location; | |
| 58 }; | 43 }; |
| 59 | 44 |
| 60 } | 45 } // namespace |
| 46 | |
| 47 // Typically only created from compositor or render threads. | |
| 48 // Note if the scheduler gets shutdown, this may be run after. | |
| 49 class Scheduler::MainThreadPendingHighPriorityTaskRunner : public WebThread::Tas k { | |
| 50 public: | |
| 51 MainThreadPendingHighPriorityTaskRunner() | |
| 52 { | |
| 53 ASSERT(Scheduler::shared()); | |
| 54 } | |
| 55 | |
| 56 // WebThread::Task implementation. | |
| 57 virtual void run() OVERRIDE | |
| 58 { | |
| 59 Scheduler* scheduler = Scheduler::shared(); | |
| 60 // FIXME: This check should't be necessary, tasks should not outlive bli nk. | |
|
Sami
2014/08/13 10:28:43
Should we have an assert here too to match the fun
alexclarke
2014/08/13 10:35:03
Done.
| |
| 61 if (!scheduler) | |
| 62 return; | |
| 63 scheduler->runHighPriorityTasks(); | |
| 64 } | |
| 65 }; | |
| 66 | |
| 67 | |
| 68 // Can be created from any thread. | |
| 69 // Note if the scheduler gets shutdown, this may be run after. | |
| 70 class Scheduler::MainThreadPendingTaskRunner : public WebThread::Task { | |
| 71 public: | |
| 72 MainThreadPendingTaskRunner( | |
| 73 const Scheduler::Task& task, const TraceLocation& location) | |
| 74 : m_task(task, location) | |
| 75 { | |
| 76 ASSERT(Scheduler::shared()); | |
| 77 } | |
| 78 | |
| 79 // WebThread::Task implementation. | |
| 80 virtual void run() OVERRIDE | |
| 81 { | |
| 82 Scheduler* scheduler = Scheduler::shared(); | |
| 83 // FIXME: This check should't be necessary, tasks should not outlive bli nk. | |
| 84 ASSERT(scheduler); | |
| 85 if (scheduler) | |
| 86 Scheduler::shared()->runHighPriorityTasks(); | |
| 87 m_task.run(); | |
| 88 } | |
| 89 | |
| 90 Scheduler::TracedTask m_task; | |
| 91 }; | |
| 61 | 92 |
| 62 Scheduler* Scheduler::s_sharedScheduler = nullptr; | 93 Scheduler* Scheduler::s_sharedScheduler = nullptr; |
| 63 | 94 |
| 64 void Scheduler::initializeOnMainThread() | 95 void Scheduler::initializeOnMainThread() |
| 65 { | 96 { |
| 66 s_sharedScheduler = new Scheduler(); | 97 s_sharedScheduler = new Scheduler(); |
| 67 } | 98 } |
| 68 | 99 |
| 69 void Scheduler::shutdown() | 100 void Scheduler::shutdown() |
| 70 { | 101 { |
| 71 delete s_sharedScheduler; | 102 delete s_sharedScheduler; |
| 72 s_sharedScheduler = nullptr; | 103 s_sharedScheduler = nullptr; |
| 73 } | 104 } |
| 74 | 105 |
| 75 Scheduler* Scheduler::shared() | 106 Scheduler* Scheduler::shared() |
| 76 { | 107 { |
| 77 return s_sharedScheduler; | 108 return s_sharedScheduler; |
| 78 } | 109 } |
| 79 | 110 |
| 80 Scheduler::Scheduler() | 111 Scheduler::Scheduler() |
| 81 : m_mainThread(blink::Platform::current()->currentThread()) | 112 : m_sharedTimerFunction(nullptr) |
| 82 , m_sharedTimerFunction(nullptr) | 113 , m_mainThread(blink::Platform::current()->currentThread()) |
| 114 , m_highPriorityTaskCount(0) | |
| 83 { | 115 { |
| 84 } | 116 } |
| 85 | 117 |
| 86 Scheduler::~Scheduler() | 118 Scheduler::~Scheduler() |
| 87 { | 119 { |
| 120 while (hasPendingHighPriorityWork()) { | |
| 121 runHighPriorityTasks(); | |
| 122 } | |
| 88 } | 123 } |
| 89 | 124 |
| 90 void Scheduler::scheduleTask(const TraceLocation& location, const Task& task) | 125 void Scheduler::scheduleIdleTask(const TraceLocation& location, const IdleTask& idleTask) |
| 91 { | |
| 92 m_mainThread->postTask(new MainThreadTaskAdapter(location, task)); | |
| 93 } | |
| 94 | |
| 95 void Scheduler::scheduleIdleTask(const IdleTask& idleTask) | |
| 96 { | 126 { |
| 97 // TODO: send a real allottedTime here. | 127 // TODO: send a real allottedTime here. |
| 98 m_mainThread->postTask(new MainThreadIdleTaskAdapter(idleTask, 0)); | 128 m_mainThread->postTask(new MainThreadIdleTaskAdapter(idleTask, 0, location)) ; |
| 99 } | 129 } |
| 100 | 130 |
| 101 void Scheduler::postTask(const TraceLocation& location, const Task& task) | 131 void Scheduler::postTask(const TraceLocation& location, const Task& task) |
| 102 { | 132 { |
| 103 scheduleTask(location, task); | 133 m_mainThread->postTask(new MainThreadPendingTaskRunner(task, location)); |
| 104 } | 134 } |
| 105 | 135 |
| 106 void Scheduler::postInputTask(const TraceLocation& location, const Task& task) | 136 void Scheduler::postInputTask(const TraceLocation& location, const Task& task) |
| 107 { | 137 { |
| 108 scheduleTask(location, task); | 138 Locker<Mutex> lock(m_pendingTasksMutex); |
| 139 m_pendingInputTasks.append(TracedTask(task, location)); | |
| 140 atomicIncrement(&m_highPriorityTaskCount); | |
| 141 m_mainThread->postTask(new MainThreadPendingHighPriorityTaskRunner()); | |
| 109 } | 142 } |
| 110 | 143 |
| 111 void Scheduler::postCompositorTask(const TraceLocation& location, const Task& ta sk) | 144 void Scheduler::postCompositorTask(const TraceLocation& location, const Task& ta sk) |
| 112 { | 145 { |
| 113 scheduleTask(location, task); | 146 Locker<Mutex> lock(m_pendingTasksMutex); |
| 147 m_pendingCompositorTasks.append(TracedTask(task, location)); | |
| 148 atomicIncrement(&m_highPriorityTaskCount); | |
| 149 m_mainThread->postTask(new MainThreadPendingHighPriorityTaskRunner()); | |
| 114 } | 150 } |
| 115 | 151 |
| 116 void Scheduler::postIdleTask(const IdleTask& idleTask) | 152 void Scheduler::postIdleTask(const TraceLocation& location, const IdleTask& idle Task) |
| 117 { | 153 { |
| 118 scheduleIdleTask(idleTask); | 154 scheduleIdleTask(location, idleTask); |
| 119 } | 155 } |
| 120 | 156 |
| 121 void Scheduler::tickSharedTimer() | 157 void Scheduler::tickSharedTimer() |
| 122 { | 158 { |
| 123 TRACE_EVENT0("blink", "Scheduler::tickSharedTimer"); | 159 TRACE_EVENT0("blink", "Scheduler::tickSharedTimer"); |
| 160 | |
| 161 // Run any high priority tasks that are queued up, otherwise the blink timer s will yield immediately. | |
| 162 runHighPriorityTasks(); | |
| 124 m_sharedTimerFunction(); | 163 m_sharedTimerFunction(); |
| 164 | |
| 165 // The blink timers may have just yielded, so run any high priority tasks th at where queued up | |
| 166 // while the blink timers were executing. | |
| 167 runHighPriorityTasks(); | |
| 168 } | |
| 169 | |
| 170 void Scheduler::runHighPriorityTasks() | |
| 171 { | |
| 172 ASSERT(isMainThread()); | |
| 173 TRACE_EVENT0("blink", "Scheduler::runHighPriorityTasks"); | |
| 174 | |
| 175 // These locks guard against another thread posing input or compositor tasks while we swap the buffers. | |
|
Sami
2014/08/13 10:28:43
s/posing/posting/
alexclarke
2014/08/13 10:35:03
Done.
| |
| 176 // One the buffers have been swapped we can safely access the returned deque without having to lock. | |
| 177 m_pendingTasksMutex.lock(); | |
| 178 Deque<TracedTask>& inputTasks = m_pendingInputTasks.swapBuffers(); | |
| 179 Deque<TracedTask>& compositorTasks = m_pendingCompositorTasks.swapBuffers(); | |
| 180 m_pendingTasksMutex.unlock(); | |
| 181 | |
| 182 int highPriorityTasksExecuted = 0; | |
| 183 while (!inputTasks.isEmpty()) { | |
| 184 inputTasks.takeFirst().run(); | |
| 185 highPriorityTasksExecuted++; | |
| 186 } | |
| 187 | |
| 188 while (!compositorTasks.isEmpty()) { | |
| 189 compositorTasks.takeFirst().run(); | |
| 190 highPriorityTasksExecuted++; | |
| 191 } | |
| 192 | |
| 193 int highPriorityTaskCount = atomicSubtract(&m_highPriorityTaskCount, highPri orityTasksExecuted); | |
| 194 ASSERT_UNUSED(highPriorityTaskCount, highPriorityTaskCount >= 0); | |
| 125 } | 195 } |
| 126 | 196 |
| 127 void Scheduler::sharedTimerAdapter() | 197 void Scheduler::sharedTimerAdapter() |
| 128 { | 198 { |
| 129 shared()->tickSharedTimer(); | 199 shared()->tickSharedTimer(); |
| 130 } | 200 } |
| 131 | 201 |
| 132 void Scheduler::setSharedTimerFiredFunction(void (*function)()) | 202 void Scheduler::setSharedTimerFiredFunction(void (*function)()) |
| 133 { | 203 { |
| 134 m_sharedTimerFunction = function; | 204 m_sharedTimerFunction = function; |
| 135 blink::Platform::current()->setSharedTimerFiredFunction(function ? &Schedule r::sharedTimerAdapter : nullptr); | 205 blink::Platform::current()->setSharedTimerFiredFunction(function ? &Schedule r::sharedTimerAdapter : nullptr); |
| 136 } | 206 } |
| 137 | 207 |
| 138 void Scheduler::setSharedTimerFireInterval(double interval) | 208 void Scheduler::setSharedTimerFireInterval(double interval) |
| 139 { | 209 { |
| 140 blink::Platform::current()->setSharedTimerFireInterval(interval); | 210 blink::Platform::current()->setSharedTimerFireInterval(interval); |
| 141 } | 211 } |
| 142 | 212 |
| 143 void Scheduler::stopSharedTimer() | 213 void Scheduler::stopSharedTimer() |
| 144 { | 214 { |
| 145 blink::Platform::current()->stopSharedTimer(); | 215 blink::Platform::current()->stopSharedTimer(); |
| 146 } | 216 } |
| 147 | 217 |
| 148 bool Scheduler::shouldYieldForHighPriorityWork() | 218 bool Scheduler::shouldYieldForHighPriorityWork() const |
| 149 { | 219 { |
| 150 return false; | 220 return hasPendingHighPriorityWork(); |
| 221 } | |
| 222 | |
| 223 bool Scheduler::hasPendingHighPriorityWork() const | |
| 224 { | |
| 225 // This method is expected to be run on the main thread, but the high priori ty tasks will be posted by | |
| 226 // other threads. We could use locks here, but this function is (sometimes) called a lot by | |
| 227 // ThreadTimers::sharedTimerFiredInternal so we decided to use atomics + bar rier loads here which | |
| 228 // should be cheaper. | |
| 229 // NOTE it's possible the barrier read is overkill here, since delayed yield ing isn't a big deal. | |
| 230 return acquireLoad(&m_highPriorityTaskCount) != 0; | |
| 231 } | |
| 232 | |
| 233 void Scheduler::TracedTask::run() | |
| 234 { | |
| 235 TRACE_EVENT2("blink", "TracedTask::run", | |
| 236 "src_file", m_location.fileName(), | |
| 237 "src_func", m_location.functionName()); | |
| 238 m_task(); | |
| 151 } | 239 } |
| 152 | 240 |
| 153 } // namespace blink | 241 } // namespace blink |
| OLD | NEW |