| 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 #ifndef Scheduler_h | 5 #ifndef Scheduler_h |
| 6 #define Scheduler_h | 6 #define Scheduler_h |
| 7 | 7 |
| 8 #include "platform/PlatformExport.h" | 8 #include "platform/PlatformExport.h" |
| 9 #include "platform/scheduler/TracedTask.h" | 9 #include "platform/scheduler/TracedTask.h" |
| 10 #include "wtf/DoubleBufferedDeque.h" | 10 #include "wtf/DoubleBufferedDeque.h" |
| 11 #include "wtf/Functional.h" | 11 #include "wtf/Functional.h" |
| 12 #include "wtf/Noncopyable.h" | 12 #include "wtf/Noncopyable.h" |
| 13 #include "wtf/ThreadingPrimitives.h" | 13 #include "wtf/ThreadingPrimitives.h" |
| 14 | 14 |
| 15 namespace blink { | 15 namespace blink { |
| 16 class WebThread; | 16 class WebThread; |
| 17 struct WebBeginFrameArgs; | 17 struct WebBeginFrameArgs; |
| 18 | 18 |
| 19 // The scheduler is an opinionated gateway for arranging work to be run on the | 19 // The scheduler is an opinionated gateway for arranging work to be run on the |
| 20 // main thread. It decides which tasks get priority over others based on a | 20 // main thread. It decides which tasks get priority over others based on a |
| 21 // scheduling policy and the overall system state. | 21 // scheduling policy and the overall system state. |
| 22 class PLATFORM_EXPORT Scheduler { | 22 class PLATFORM_EXPORT Scheduler { |
| 23 WTF_MAKE_NONCOPYABLE(Scheduler); | 23 WTF_MAKE_NONCOPYABLE(Scheduler); |
| 24 public: | 24 public: |
| 25 typedef Function<void()> Task; | 25 typedef Function<void()> Task; |
| 26 // An IdleTask is passed an allotted time in CLOCK_MONOTONIC milliseconds an
d is expected to complete within this timeframe. | 26 // An IdleTask is passed a deadline in CLOCK_MONOTONIC seconds and is expect
ed to complete before this deadline. |
| 27 typedef Function<void(double allottedTimeMs)> IdleTask; | 27 typedef Function<void(double deadlineSeconds)> IdleTask; |
| 28 | 28 |
| 29 static Scheduler* shared(); | 29 static Scheduler* shared(); |
| 30 static void initializeOnMainThread(); | 30 static void initializeOnMainThread(); |
| 31 static void shutdown(); | 31 static void shutdown(); |
| 32 | 32 |
| 33 // Called to notify about the start of a new frame. | 33 // Called to notify about the start of a new frame. |
| 34 void willBeginFrame(const WebBeginFrameArgs&); | 34 void willBeginFrame(double estimatedNextBeginFrameSeconds); |
| 35 | 35 |
| 36 // Called to notify that a previously begun frame was committed. | 36 // Called to notify that a previously begun frame was committed. |
| 37 void didCommitFrameToCompositor(); | 37 void didCommitFrameToCompositor(); |
| 38 | 38 |
| 39 // The following entrypoints are used to schedule different types of tasks | 39 // The following entrypoints are used to schedule different types of tasks |
| 40 // to be run on the main thread. They can be called from any thread. | 40 // to be run on the main thread. They can be called from any thread. |
| 41 void postInputTask(const TraceLocation&, const Task&); | 41 void postInputTask(const TraceLocation&, const Task&); |
| 42 void postCompositorTask(const TraceLocation&, const Task&); | 42 void postCompositorTask(const TraceLocation&, const Task&); |
| 43 void postIpcTask(const TraceLocation&, const Task&); | 43 void postIpcTask(const TraceLocation&, const Task&); |
| 44 void postTask(const TraceLocation&, const Task&); // For generic (low priori
ty) tasks. | 44 void postTask(const TraceLocation&, const Task&); // For generic (low priori
ty) tasks. |
| 45 void postIdleTask(const TraceLocation&, const IdleTask&); // For non-critica
l tasks which may be reordered relative to other task types. | 45 // For non-critical tasks which may be reordered relative to other task type
s and may be starved |
| 46 // for an arbitrarily long time if no idle time is available. |
| 47 void postIdleTask(const TraceLocation&, const IdleTask&); |
| 46 | 48 |
| 47 // Tells the scheduler that the system received an input event. This causes
the scheduler to go into | 49 // Tells the scheduler that the system received an input event. This causes
the scheduler to go into |
| 48 // Compositor Priority mode for a short duration. | 50 // Compositor Priority mode for a short duration. |
| 49 void didReceiveInputEvent(); | 51 void didReceiveInputEvent(); |
| 50 | 52 |
| 51 // Returns true if there is high priority work pending on the main thread | 53 // Returns true if there is high priority work pending on the main thread |
| 52 // and the caller should yield to let the scheduler service that work. | 54 // and the caller should yield to let the scheduler service that work. |
| 53 // Can be called on any thread. | 55 // Can be called on any thread. |
| 54 bool shouldYieldForHighPriorityWork() const; | 56 bool shouldYieldForHighPriorityWork() const; |
| 55 | 57 |
| 56 // The shared timer can be used to schedule a periodic callback which may | 58 // The shared timer can be used to schedule a periodic callback which may |
| 57 // get preempted by higher priority work. | 59 // get preempted by higher priority work. |
| 58 void setSharedTimerFiredFunction(void (*function)()); | 60 void setSharedTimerFiredFunction(void (*function)()); |
| 59 void setSharedTimerFireInterval(double); | 61 void setSharedTimerFireInterval(double); |
| 60 void stopSharedTimer(); | 62 void stopSharedTimer(); |
| 61 | 63 |
| 64 // Returns the deadline, in CLOCK_MONOTONIC seconds, which an idle task shou
ld |
| 65 // finish by for the current frame. |
| 66 double currentFrameDeadlineForIdleTasks() const; |
| 67 |
| 62 protected: | 68 protected: |
| 63 class MainThreadPendingTaskRunner; | 69 class MainThreadPendingTaskRunner; |
| 64 class MainThreadPendingHighPriorityTaskRunner; | 70 class MainThreadPendingHighPriorityTaskRunner; |
| 71 class MainThreadPendingIdleTaskRunner; |
| 65 friend class MainThreadPendingTaskRunner; | 72 friend class MainThreadPendingTaskRunner; |
| 66 friend class MainThreadPendingHighPriorityTaskRunner; | 73 friend class MainThreadPendingHighPriorityTaskRunner; |
| 67 | 74 |
| 68 enum SchedulerPolicy { | 75 enum SchedulerPolicy { |
| 69 Normal, | 76 Normal, |
| 70 CompositorPriority, | 77 CompositorPriority, |
| 71 }; | 78 }; |
| 72 | 79 |
| 73 Scheduler(); | 80 Scheduler(); |
| 74 virtual ~Scheduler(); | 81 virtual ~Scheduler(); |
| 75 | 82 |
| 76 void scheduleIdleTask(const TraceLocation&, const IdleTask&); | |
| 77 void postHighPriorityTaskInternal(const TraceLocation&, const Task&, const c
har* traceName); | 83 void postHighPriorityTaskInternal(const TraceLocation&, const Task&, const c
har* traceName); |
| 84 void postIdleTaskInternal(const TraceLocation&, const IdleTask&, const char*
traceName); |
| 78 void didRunHighPriorityTask(); | 85 void didRunHighPriorityTask(); |
| 79 | 86 |
| 80 static void sharedTimerAdapter(); | 87 static void sharedTimerAdapter(); |
| 81 | 88 |
| 82 // Start of main thread only members ----------------------------------- | 89 // Start of main thread only members ----------------------------------- |
| 83 | 90 |
| 84 // Only does work in CompositorPriority mode. Returns true if any work was d
one. | 91 // Only does work in CompositorPriority mode. Returns true if any work was d
one. |
| 85 bool runPendingHighPriorityTasksIfInCompositorPriority(); | 92 bool runPendingHighPriorityTasksIfInCompositorPriority(); |
| 86 | 93 |
| 87 // Returns true if any work was done. | 94 // Only does work if canRunIdleTask. Returns true if any work was done. |
| 88 bool executeHighPriorityTasks(Deque<TracedTask>&); | 95 bool maybeRunPendingIdleTask(); |
| 96 |
| 97 PassOwnPtr<internal::TracedIdleTask> takeFirstPendingIdleTask(); |
| 98 |
| 99 // Returns true if the scheduler can run idle tasks at this time. |
| 100 bool canRunIdleTask() const; |
| 89 | 101 |
| 90 // Return the current SchedulerPolicy. | 102 // Return the current SchedulerPolicy. |
| 91 SchedulerPolicy schedulerPolicy() const; | 103 SchedulerPolicy schedulerPolicy() const; |
| 92 | 104 |
| 93 void updatePolicy(); | 105 void updatePolicy(); |
| 94 | 106 |
| 107 // Returns true if an idle task was posted to the main thread for execution. |
| 108 bool maybePostMainThreadPendingIdleTask(); |
| 109 |
| 95 void tickSharedTimer(); | 110 void tickSharedTimer(); |
| 96 | 111 |
| 97 void (*m_sharedTimerFunction)(); | 112 void (*m_sharedTimerFunction)(); |
| 98 | 113 |
| 99 // End of main thread only members ------------------------------------- | 114 // End of main thread only members ------------------------------------- |
| 100 | 115 |
| 101 void enterSchedulerPolicyLocked(SchedulerPolicy); | 116 void enterSchedulerPolicyLocked(SchedulerPolicy); |
| 102 void enterSchedulerPolicy(SchedulerPolicy); | 117 void enterSchedulerPolicy(SchedulerPolicy); |
| 103 | 118 |
| 104 static Scheduler* s_sharedScheduler; | 119 static Scheduler* s_sharedScheduler; |
| 105 | 120 |
| 106 WebThread* m_mainThread; | 121 WebThread* m_mainThread; |
| 107 | 122 |
| 123 // This mutex protects calls to the pending idle task queue. |
| 124 Mutex m_pendingIdleTasksMutex; |
| 125 Deque<OwnPtr<internal::TracedIdleTask>> m_pendingIdleTasks; |
| 126 |
| 127 bool m_currentFrameCommitted; |
| 128 double m_estimatedNextBeginFrameSeconds; |
| 108 // Declared volatile as it is atomically incremented. | 129 // Declared volatile as it is atomically incremented. |
| 109 volatile int m_highPriorityTaskCount; | 130 volatile int m_highPriorityTaskCount; |
| 110 | 131 |
| 111 bool m_highPriorityTaskRunnerPosted; | 132 bool m_highPriorityTaskRunnerPosted; |
| 112 | 133 |
| 113 Mutex m_policyStateMutex; | 134 Mutex m_policyStateMutex; |
| 114 double m_compositorPriorityPolicyEndTimeSeconds; | 135 double m_compositorPriorityPolicyEndTimeSeconds; |
| 115 | 136 |
| 116 // Don't access m_schedulerPolicy directly, use enterSchedulerPolicyLocked a
nd schedulerPolicy instead. | 137 // Don't access m_schedulerPolicy directly, use enterSchedulerPolicyLocked a
nd schedulerPolicy instead. |
| 117 volatile int m_schedulerPolicy; | 138 volatile int m_schedulerPolicy; |
| 118 }; | 139 }; |
| 119 | 140 |
| 120 } // namespace blink | 141 } // namespace blink |
| 121 | 142 |
| 122 #endif // Scheduler_h | 143 #endif // Scheduler_h |
| OLD | NEW |