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/TraceLocation.h" |
| 10 #include "platform/scheduler/DoubleBufferedDeque.h" |
9 #include "wtf/Functional.h" | 11 #include "wtf/Functional.h" |
10 #include "wtf/Noncopyable.h" | 12 #include "wtf/Noncopyable.h" |
| 13 #include "wtf/ThreadingPrimitives.h" |
11 | 14 |
12 namespace blink { | 15 namespace blink { |
13 class WebThread; | 16 class WebThread; |
14 } | 17 } |
15 | 18 |
16 namespace blink { | 19 namespace blink { |
17 | 20 |
18 class TraceLocation; | 21 class TraceLocation; |
19 | 22 |
20 // The scheduler is an opinionated gateway for arranging work to be run on the | 23 // The scheduler is an opinionated gateway for arranging work to be run on the |
21 // main thread. It decides which tasks get priority over others based on a | 24 // main thread. It decides which tasks get priority over others based on a |
22 // scheduling policy and the overall system state. | 25 // scheduling policy and the overall system state. |
23 class PLATFORM_EXPORT Scheduler { | 26 class PLATFORM_EXPORT Scheduler { |
24 WTF_MAKE_NONCOPYABLE(Scheduler); | 27 WTF_MAKE_NONCOPYABLE(Scheduler); |
25 public: | 28 public: |
26 typedef Function<void()> Task; | 29 typedef Function<void()> Task; |
27 // An IdleTask is passed an allotted time in CLOCK_MONOTONIC milliseconds an
d is expected to complete within this timeframe. | 30 // An IdleTask is passed an allotted time in CLOCK_MONOTONIC milliseconds an
d is expected to complete within this timeframe. |
28 typedef Function<void(double allottedTimeMs)> IdleTask; | 31 typedef Function<void(double allottedTimeMs)> IdleTask; |
29 | 32 |
30 static Scheduler* shared(); | 33 static Scheduler* shared(); |
31 static void initializeOnMainThread(); | 34 static void initializeOnMainThread(); |
32 static void shutdown(); | 35 static void shutdown(); |
33 | 36 |
34 // The following entrypoints are used to schedule different types of tasks | 37 // The following entrypoints are used to schedule different types of tasks |
35 // to be run on the main thread. They can be called from any thread. | 38 // to be run on the main thread. They can be called from any thread. |
36 void postInputTask(const TraceLocation&, const Task&); | 39 void postInputTask(const TraceLocation&, const Task&); |
37 void postCompositorTask(const TraceLocation&, const Task&); | 40 void postCompositorTask(const TraceLocation&, const Task&); |
38 void postTask(const TraceLocation&, const Task&); // For generic (low priori
ty) tasks. | 41 void postTask(const TraceLocation&, const Task&); // For generic (low priori
ty) tasks. |
39 void postIdleTask(const IdleTask&); // For non-critical tasks which may be r
eordered relative to other task types. | 42 void postIdleTask(const TraceLocation&, const IdleTask&); // For non-critica
l tasks which may be reordered relative to other task types. |
40 | 43 |
41 // Returns true if there is high priority work pending on the main thread | 44 // Returns true if there is high priority work pending on the main thread |
42 // and the caller should yield to let the scheduler service that work. | 45 // and the caller should yield to let the scheduler service that work. |
43 // Can be called on the main thread. | 46 // Can be called on any thread. |
44 bool shouldYieldForHighPriorityWork(); | 47 bool shouldYieldForHighPriorityWork(); |
45 | 48 |
46 // The shared timer can be used to schedule a periodic callback which may | 49 // The shared timer can be used to schedule a periodic callback which may |
47 // get preempted by higher priority work. | 50 // get preempted by higher priority work. |
48 void setSharedTimerFiredFunction(void (*function)()); | 51 void setSharedTimerFiredFunction(void (*function)()); |
49 void setSharedTimerFireInterval(double); | 52 void setSharedTimerFireInterval(double); |
50 void stopSharedTimer(); | 53 void stopSharedTimer(); |
51 | 54 |
52 private: | 55 private: |
| 56 class MainThreadPendingTaskRunner; |
| 57 class MainThreadPendingHighPriorityTaskRunner; |
| 58 friend class MainThreadPendingTaskRunner; |
| 59 friend class MainThreadPendingHighPriorityTaskRunner; |
| 60 |
53 Scheduler(); | 61 Scheduler(); |
54 ~Scheduler(); | 62 ~Scheduler(); |
55 | 63 |
56 void scheduleTask(const TraceLocation&, const Task&); | 64 void scheduleIdleTask(const TraceLocation&, const IdleTask&); |
57 void scheduleIdleTask(const IdleTask&); | |
58 | 65 |
59 static void sharedTimerAdapter(); | 66 static void sharedTimerAdapter(); |
60 void tickSharedTimer(); | 67 void tickSharedTimer(); |
61 | 68 |
62 static Scheduler* s_sharedScheduler; | 69 static Scheduler* s_sharedScheduler; |
63 blink::WebThread* m_mainThread; | |
64 | 70 |
| 71 class TracedTask { |
| 72 public: |
| 73 TracedTask(const Task& task, const TraceLocation& location) |
| 74 : m_task(task) |
| 75 , m_location(location) { } |
| 76 |
| 77 void run(); |
| 78 |
| 79 private: |
| 80 Task m_task; |
| 81 TraceLocation m_location; |
| 82 }; |
| 83 |
| 84 void maybePostMainThreadPendingHighPriorityTaskRunner(); |
| 85 void runHighPriorityTasks(); |
| 86 |
| 87 void incrementMainThreadTaskRunnerCount(); |
| 88 void decrementMainThreadTaskRunnerCount(); |
| 89 |
| 90 // These members can be accessed from any thread. |
| 91 WebThread* m_mainThread; |
65 void (*m_sharedTimerFunction)(); | 92 void (*m_sharedTimerFunction)(); |
| 93 volatile int m_mainThreadTaskRunnerCount; |
| 94 |
| 95 // This mutex protects calls to the pending task queues. |
| 96 Mutex m_pendingTasksMutex; |
| 97 DoubleBufferedDeque<TracedTask> m_pendingInputTasks; |
| 98 DoubleBufferedDeque<TracedTask> m_pendingCompositorTasks; |
| 99 |
| 100 volatile int m_highPriotityTaskCount; |
66 }; | 101 }; |
67 | 102 |
68 } // namespace blink | 103 } // namespace blink |
69 | 104 |
70 #endif // Scheduler_h | 105 #endif // Scheduler_h |
OLD | NEW |