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