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