| 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/PlatformThreadData.h" |
| 9 #include "platform/Task.h" | 9 #include "platform/Task.h" |
| 10 #include "platform/ThreadTimers.h" | 10 #include "platform/ThreadTimers.h" |
| 11 #include "platform/TraceEvent.h" | 11 #include "platform/TraceEvent.h" |
| 12 #include "public/platform/Platform.h" | 12 #include "public/platform/Platform.h" |
| 13 #include "wtf/MainThread.h" | 13 #include "wtf/MainThread.h" |
| 14 #include "wtf/ThreadingPrimitives.h" | |
| 15 | |
| 16 | 14 |
| 17 namespace blink { | 15 namespace blink { |
| 18 | 16 |
| 19 namespace { | 17 namespace { |
| 20 | 18 |
| 21 // The time we should stay in CompositorPriority mode for, after a touch event. | 19 // The time we should stay in CompositorPriority mode for, after a touch event. |
| 22 double kLowSchedulerPolicyAfterTouchTimeSeconds = 0.1; | 20 double kLowSchedulerPolicyAfterTouchTimeSeconds = 0.1; |
| 23 | 21 |
| 24 // Can be created from any thread. | 22 // Can be created from any thread. |
| 25 // Note if the scheduler gets shutdown, this may be run after. | 23 // Note if the scheduler gets shutdown, this may be run after. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 scheduler->swapQueuesRunPendingTasksAndAllowHighPriorityTaskRunnerPostin
g(); | 69 scheduler->swapQueuesRunPendingTasksAndAllowHighPriorityTaskRunnerPostin
g(); |
| 72 } | 70 } |
| 73 }; | 71 }; |
| 74 | 72 |
| 75 | 73 |
| 76 // Can be created from any thread. | 74 // Can be created from any thread. |
| 77 // Note if the scheduler gets shutdown, this may be run after. | 75 // Note if the scheduler gets shutdown, this may be run after. |
| 78 class Scheduler::MainThreadPendingTaskRunner : public WebThread::Task { | 76 class Scheduler::MainThreadPendingTaskRunner : public WebThread::Task { |
| 79 public: | 77 public: |
| 80 MainThreadPendingTaskRunner( | 78 MainThreadPendingTaskRunner( |
| 81 const Scheduler::Task& task, const TraceLocation& location) | 79 const Scheduler::Task& task, const TraceLocation& location, const char*
traceName) |
| 82 : m_task(task, location) | 80 : m_task(task, location, traceName) |
| 83 { | 81 { |
| 84 ASSERT(Scheduler::shared()); | 82 ASSERT(Scheduler::shared()); |
| 85 } | 83 } |
| 86 | 84 |
| 87 // WebThread::Task implementation. | 85 // WebThread::Task implementation. |
| 88 virtual void run() OVERRIDE | 86 virtual void run() OVERRIDE |
| 89 { | 87 { |
| 90 Scheduler* scheduler = Scheduler::shared(); | 88 Scheduler* scheduler = Scheduler::shared(); |
| 91 // FIXME: This check should't be necessary, tasks should not outlive bli
nk. | 89 // FIXME: This check should't be necessary, tasks should not outlive bli
nk. |
| 92 ASSERT(scheduler); | 90 ASSERT(scheduler); |
| 93 if (scheduler) | 91 if (scheduler) |
| 94 Scheduler::shared()->runPendingHighPriorityTasksIfInCompositorPriori
ty(); | 92 Scheduler::shared()->runPendingHighPriorityTasksIfInCompositorPriori
ty(); |
| 95 m_task.run(); | 93 m_task.run(); |
| 96 } | 94 } |
| 97 | 95 |
| 98 Scheduler::TracedTask m_task; | 96 TracedTask m_task; |
| 99 }; | 97 }; |
| 100 | 98 |
| 101 Scheduler* Scheduler::s_sharedScheduler = nullptr; | 99 Scheduler* Scheduler::s_sharedScheduler = nullptr; |
| 102 | 100 |
| 103 void Scheduler::initializeOnMainThread() | 101 void Scheduler::initializeOnMainThread() |
| 104 { | 102 { |
| 105 s_sharedScheduler = new Scheduler(); | 103 s_sharedScheduler = new Scheduler(); |
| 106 } | 104 } |
| 107 | 105 |
| 108 void Scheduler::shutdown() | 106 void Scheduler::shutdown() |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 { | 140 { |
| 143 // TODO: Trigger the frame deadline immediately. | 141 // TODO: Trigger the frame deadline immediately. |
| 144 } | 142 } |
| 145 | 143 |
| 146 void Scheduler::scheduleIdleTask(const TraceLocation& location, const IdleTask&
idleTask) | 144 void Scheduler::scheduleIdleTask(const TraceLocation& location, const IdleTask&
idleTask) |
| 147 { | 145 { |
| 148 // TODO: send a real allottedTime here. | 146 // TODO: send a real allottedTime here. |
| 149 m_mainThread->postTask(new MainThreadIdleTaskAdapter(idleTask, 0, location))
; | 147 m_mainThread->postTask(new MainThreadIdleTaskAdapter(idleTask, 0, location))
; |
| 150 } | 148 } |
| 151 | 149 |
| 152 void Scheduler::postTask(const TraceLocation& location, const Task& task) | 150 void Scheduler::postHighPriorityTaskInternal(const TraceLocation& location, cons
t Task& task, const char* traceName) |
| 153 { | |
| 154 m_mainThread->postTask(new MainThreadPendingTaskRunner(task, location)); | |
| 155 } | |
| 156 | |
| 157 void Scheduler::postInputTask(const TraceLocation& location, const Task& task) | |
| 158 { | 151 { |
| 159 Locker<Mutex> lock(m_pendingTasksMutex); | 152 Locker<Mutex> lock(m_pendingTasksMutex); |
| 160 m_pendingHighPriorityTasks.append(TracedTask(task, location)); | 153 |
| 154 m_pendingHighPriorityTasks.append(TracedTask(task, location, traceName)); |
| 161 atomicIncrement(&m_highPriorityTaskCount); | 155 atomicIncrement(&m_highPriorityTaskCount); |
| 162 maybePostMainThreadPendingHighPriorityTaskRunner(); | 156 maybePostMainThreadPendingHighPriorityTaskRunner(); |
| 163 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("blink.scheduler"), "PendingHighPri
orityTasks", m_highPriorityTaskCount); | 157 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("blink.scheduler"), "PendingHighPri
orityTasks", m_highPriorityTaskCount); |
| 164 } | 158 } |
| 165 | 159 |
| 160 void Scheduler::postTask(const TraceLocation& location, const Task& task) |
| 161 { |
| 162 m_mainThread->postTask(new MainThreadPendingTaskRunner(task, location, "Sche
duler::MainThreadTask")); |
| 163 } |
| 164 |
| 165 void Scheduler::postInputTask(const TraceLocation& location, const Task& task) |
| 166 { |
| 167 postHighPriorityTaskInternal(location, task, "Scheduler::InputTask"); |
| 168 } |
| 169 |
| 166 void Scheduler::didReceiveInputEvent() | 170 void Scheduler::didReceiveInputEvent() |
| 167 { | 171 { |
| 168 enterSchedulerPolicy(CompositorPriority); | 172 enterSchedulerPolicy(CompositorPriority); |
| 169 } | 173 } |
| 170 | 174 |
| 171 void Scheduler::postCompositorTask(const TraceLocation& location, const Task& ta
sk) | 175 void Scheduler::postCompositorTask(const TraceLocation& location, const Task& ta
sk) |
| 172 { | 176 { |
| 173 Locker<Mutex> lock(m_pendingTasksMutex); | 177 postHighPriorityTaskInternal(location, task, "Scheduler::CompositorTask"); |
| 174 m_pendingHighPriorityTasks.append(TracedTask(task, location)); | |
| 175 atomicIncrement(&m_highPriorityTaskCount); | |
| 176 maybePostMainThreadPendingHighPriorityTaskRunner(); | |
| 177 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("blink.scheduler"), "PendingHighPri
orityTasks", m_highPriorityTaskCount); | |
| 178 } | 178 } |
| 179 | 179 |
| 180 void Scheduler::maybePostMainThreadPendingHighPriorityTaskRunner() | 180 void Scheduler::maybePostMainThreadPendingHighPriorityTaskRunner() |
| 181 { | 181 { |
| 182 ASSERT(m_pendingTasksMutex.locked()); | 182 ASSERT(m_pendingTasksMutex.locked()); |
| 183 if (m_highPriorityTaskRunnerPosted) | 183 if (m_highPriorityTaskRunnerPosted) |
| 184 return; | 184 return; |
| 185 m_mainThread->postTask(new MainThreadPendingHighPriorityTaskRunner()); | 185 m_mainThread->postTask(new MainThreadPendingHighPriorityTaskRunner()); |
| 186 m_highPriorityTaskRunnerPosted = true; | 186 m_highPriorityTaskRunnerPosted = true; |
| 187 } | 187 } |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 void Scheduler::enterSchedulerPolicyLocked(SchedulerPolicy schedulerPolicy) | 324 void Scheduler::enterSchedulerPolicyLocked(SchedulerPolicy schedulerPolicy) |
| 325 { | 325 { |
| 326 ASSERT(m_pendingTasksMutex.locked()); | 326 ASSERT(m_pendingTasksMutex.locked()); |
| 327 if (schedulerPolicy == CompositorPriority) | 327 if (schedulerPolicy == CompositorPriority) |
| 328 m_compositorPriorityPolicyEndTimeSeconds = Platform::current()->monotoni
callyIncreasingTime() + kLowSchedulerPolicyAfterTouchTimeSeconds; | 328 m_compositorPriorityPolicyEndTimeSeconds = Platform::current()->monotoni
callyIncreasingTime() + kLowSchedulerPolicyAfterTouchTimeSeconds; |
| 329 | 329 |
| 330 releaseStore(&m_schedulerPolicy, schedulerPolicy); | 330 releaseStore(&m_schedulerPolicy, schedulerPolicy); |
| 331 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("blink.scheduler"), "SchedulerPolic
y", schedulerPolicy); | 331 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("blink.scheduler"), "SchedulerPolic
y", schedulerPolicy); |
| 332 } | 332 } |
| 333 | 333 |
| 334 void Scheduler::TracedTask::run() | |
| 335 { | |
| 336 TRACE_EVENT2("blink", "TracedTask::run", | |
| 337 "src_file", m_location.fileName(), | |
| 338 "src_func", m_location.functionName()); | |
| 339 m_task(); | |
| 340 } | |
| 341 | |
| 342 } // namespace blink | 334 } // namespace blink |
| OLD | NEW |