Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(364)

Side by Side Diff: Source/platform/scheduler/Scheduler.h

Issue 595023002: Implement idle task scheduling. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Avoid reposting idle tasks Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.
45 void postIdleTask(const TraceLocation&, const IdleTask&); // For non-critica l tasks which may be reordered relative to other task types. 45 void postIdleTask(const TraceLocation&, const IdleTask&); // For non-critica l tasks which may be reordered relative to other task types.
Sami 2014/10/03 10:39:21 Let's also mention here that idle tasks can be sta
rmcilroy 2014/10/03 21:41:21 Done.
46 46
47 // Tells the scheduler that the system received an input event. This causes the scheduler to go into 47 // 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. 48 // Compositor Priority mode for a short duration.
49 void didReceiveInputEvent(); 49 void didReceiveInputEvent();
50 50
51 // Returns true if there is high priority work pending on the main thread 51 // 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. 52 // and the caller should yield to let the scheduler service that work.
53 // Can be called on any thread. 53 // Can be called on any thread.
54 bool shouldYieldForHighPriorityWork() const; 54 bool shouldYieldForHighPriorityWork() const;
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
88 // Only does work in CanRunIdleTask and there is a pending idle task. Return s true if any work was done.
89 bool runPendingIdleTaskIfCanRunIdleTask();
Sami 2014/10/03 10:39:22 This is a mouthful. How about runPendintIdleTaskIf
rmcilroy 2014/10/03 21:41:21 Made it maybeRunPendingIdleTask on Alex's suggesti
90
86 // Returns true if any work was done. 91 // Returns true if any work was done.
87 bool swapQueuesAndRunPendingTasks(); 92 bool swapQueuesAndRunPendingTasks();
88 93
89 void swapQueuesRunPendingTasksAndAllowHighPriorityTaskRunnerPosting(); 94 void swapQueuesRunPendingTasksAndAllowHighPriorityTaskRunnerPosting();
90 95
91 // Returns true if any work was done. 96 // Returns true if any work was done.
92 bool executeHighPriorityTasks(Deque<TracedTask>&); 97 bool executeHighPriorityTasks(Deque<TracedStandardTask>&);
98
99 // Returns the time in CLOCK_MONOTONIC seconds until the current frame deadl ine expires.
100 double currentFrameDeadlineSeconds() const;
Sami 2014/10/03 10:39:21 Please be explicit about which deadline this is ta
rmcilroy 2014/10/03 21:41:21 Done.
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
OLDNEW
« no previous file with comments | « no previous file | Source/platform/scheduler/Scheduler.cpp » ('j') | Source/platform/scheduler/Scheduler.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698