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 frameDeadlineSeconds); |
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. |
(...skipping 10 matching lines...) Expand all Loading... |
55 | 55 |
56 // The shared timer can be used to schedule a periodic callback which may | 56 // The shared timer can be used to schedule a periodic callback which may |
57 // get preempted by higher priority work. | 57 // get preempted by higher priority work. |
58 void setSharedTimerFiredFunction(void (*function)()); | 58 void setSharedTimerFiredFunction(void (*function)()); |
59 void setSharedTimerFireInterval(double); | 59 void setSharedTimerFireInterval(double); |
60 void stopSharedTimer(); | 60 void stopSharedTimer(); |
61 | 61 |
62 protected: | 62 protected: |
63 class MainThreadPendingTaskRunner; | 63 class MainThreadPendingTaskRunner; |
64 class MainThreadPendingHighPriorityTaskRunner; | 64 class MainThreadPendingHighPriorityTaskRunner; |
| 65 class MainThreadPendingIdleTaskRunner; |
65 friend class MainThreadPendingTaskRunner; | 66 friend class MainThreadPendingTaskRunner; |
66 friend class MainThreadPendingHighPriorityTaskRunner; | 67 friend class MainThreadPendingHighPriorityTaskRunner; |
| 68 friend class TracedIdleTask; |
67 | 69 |
68 enum SchedulerPolicy { | 70 enum SchedulerPolicy { |
69 Normal, | 71 Normal, |
70 CompositorPriority, | 72 CompositorPriority, |
71 }; | 73 }; |
72 | 74 |
73 Scheduler(); | 75 Scheduler(); |
74 virtual ~Scheduler(); | 76 virtual ~Scheduler(); |
75 | 77 |
76 void scheduleIdleTask(const TraceLocation&, const IdleTask&); | |
77 void postHighPriorityTaskInternal(const TraceLocation&, const Task&, const c
har* traceName); | 78 void postHighPriorityTaskInternal(const TraceLocation&, const Task&, const c
har* traceName); |
| 79 void postIdleTaskInternal(const TraceLocation&, const IdleTask&, const char*
traceName); |
78 | 80 |
79 static void sharedTimerAdapter(); | 81 static void sharedTimerAdapter(); |
80 | 82 |
81 // Start of main thread only members ----------------------------------- | 83 // Start of main thread only members ----------------------------------- |
82 | 84 |
83 // Only does work in CompositorPriority mode. Returns true if any work was d
one. | 85 // Only does work in CompositorPriority mode. Returns true if any work was d
one. |
84 bool runPendingHighPriorityTasksIfInCompositorPriority(); | 86 bool runPendingHighPriorityTasksIfInCompositorPriority(); |
85 | 87 |
86 // Returns true if any work was done. | 88 // Returns true if any work was done. |
87 bool swapQueuesAndRunPendingTasks(); | 89 bool swapQueuesAndRunPendingTasks(); |
88 | 90 |
89 void swapQueuesRunPendingTasksAndAllowHighPriorityTaskRunnerPosting(); | 91 void swapQueuesRunPendingTasksAndAllowHighPriorityTaskRunnerPosting(); |
90 | 92 |
91 // Returns true if any work was done. | 93 // Returns true if any work was done. |
92 bool executeHighPriorityTasks(Deque<TracedTask>&); | 94 bool executeHighPriorityTasks(Deque<TracedStandardTask>&); |
| 95 |
| 96 // Reposts all the tasks in idleTasks onto the idle task queue. |
| 97 void repostIdleTaskAtFront(const TracedIdleTask&); |
| 98 |
| 99 // Returns the time in CLOCK_MONOTONIC seconds until the current frame deadl
ine expires. |
| 100 double currentFrameDeadlineSeconds() const; |
| 101 |
| 102 // Returns true if the scheduler can run idle tasks at this time. |
| 103 bool canRunIdleTask() const; |
93 | 104 |
94 // Return the current SchedulerPolicy. | 105 // Return the current SchedulerPolicy. |
95 SchedulerPolicy schedulerPolicy() const; | 106 SchedulerPolicy schedulerPolicy() const; |
96 | 107 |
97 void maybeEnterNormalSchedulerPolicy(); | 108 void maybeEnterNormalSchedulerPolicy(); |
98 | 109 |
99 // Must be called while m_pendingTasksMutex is locked. | 110 // Must be called while m_pendingTasksMutex is locked. |
100 void maybePostMainThreadPendingHighPriorityTaskRunner(); | 111 void maybePostMainThreadPendingHighPriorityTaskRunner(); |
101 | 112 |
| 113 // Returns true if an idle task was posted to the main thread for execution. |
| 114 bool maybePostMainThreadPendingIdleTask(); |
| 115 |
102 void tickSharedTimer(); | 116 void tickSharedTimer(); |
103 | 117 |
104 void (*m_sharedTimerFunction)(); | 118 void (*m_sharedTimerFunction)(); |
105 | 119 |
106 // End of main thread only members ------------------------------------- | 120 // End of main thread only members ------------------------------------- |
107 | 121 |
108 bool hasPendingHighPriorityWork() const; | 122 bool hasPendingHighPriorityWork() const; |
109 | 123 |
110 void enterSchedulerPolicyLocked(SchedulerPolicy); | 124 void enterSchedulerPolicyLocked(SchedulerPolicy); |
111 | 125 |
112 void enterSchedulerPolicy(SchedulerPolicy); | 126 void enterSchedulerPolicy(SchedulerPolicy); |
113 | 127 |
114 static Scheduler* s_sharedScheduler; | 128 static Scheduler* s_sharedScheduler; |
115 | 129 |
116 WebThread* m_mainThread; | 130 WebThread* m_mainThread; |
117 | 131 |
118 // This mutex protects calls to the pending task queue, m_highPriorityTaskRu
nnerPosted and | 132 // This mutex protects calls to the pending task queue, m_highPriorityTaskRu
nnerPosted and |
119 // m_compositorPriorityPolicyEndTimeSeconds. | 133 // m_compositorPriorityPolicyEndTimeSeconds. |
120 Mutex m_pendingTasksMutex; | 134 Mutex m_pendingTasksMutex; |
121 DoubleBufferedDeque<TracedTask> m_pendingHighPriorityTasks; | 135 DoubleBufferedDeque<TracedStandardTask> m_pendingHighPriorityTasks; |
| 136 Deque<TracedIdleTask> m_pendingIdleTasks; |
122 double m_compositorPriorityPolicyEndTimeSeconds; | 137 double m_compositorPriorityPolicyEndTimeSeconds; |
123 | 138 |
| 139 bool m_currentFrameCommitted; |
| 140 double m_currentFrameDeadlineSeconds; |
| 141 |
124 // Declared volatile as it is atomically incremented. | 142 // Declared volatile as it is atomically incremented. |
125 volatile int m_highPriorityTaskCount; | 143 volatile int m_highPriorityTaskCount; |
126 | 144 |
127 bool m_highPriorityTaskRunnerPosted; | 145 bool m_highPriorityTaskRunnerPosted; |
128 | 146 |
129 // Don't access m_schedulerPolicy directly, use enterSchedulerPolicyLocked a
nd SchedulerPolicy instead. | 147 // Don't access m_schedulerPolicy directly, use enterSchedulerPolicyLocked a
nd SchedulerPolicy instead. |
130 volatile int m_schedulerPolicy; | 148 volatile int m_schedulerPolicy; |
131 }; | 149 }; |
132 | 150 |
133 } // namespace blink | 151 } // namespace blink |
134 | 152 |
135 #endif // Scheduler_h | 153 #endif // Scheduler_h |
OLD | NEW |