Chromium Code Reviews| 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/AtomicRingbuffer.h" | |
| 11 #include "wtf/Deque.h" | |
| 9 #include "wtf/Functional.h" | 12 #include "wtf/Functional.h" |
| 10 #include "wtf/Noncopyable.h" | 13 #include "wtf/Noncopyable.h" |
| 14 #include "wtf/ThreadingPrimitives.h" | |
| 11 | 15 |
| 12 namespace blink { | 16 namespace blink { |
| 13 class WebThread; | 17 class WebThread; |
| 14 } | 18 } |
| 15 | 19 |
| 16 namespace blink { | 20 namespace blink { |
| 17 | 21 |
| 18 class TraceLocation; | 22 class TraceLocation; |
| 19 | 23 |
| 20 // The scheduler is an opinionated gateway for arranging work to be run on the | 24 // 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 | 25 // main thread. It decides which tasks get priority over others based on a |
| 22 // scheduling policy and the overall system state. | 26 // scheduling policy and the overall system state. |
| 23 class PLATFORM_EXPORT Scheduler { | 27 class PLATFORM_EXPORT Scheduler { |
| 24 WTF_MAKE_NONCOPYABLE(Scheduler); | 28 WTF_MAKE_NONCOPYABLE(Scheduler); |
| 25 public: | 29 public: |
| 26 typedef Function<void()> Task; | 30 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. | 31 // 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; | 32 typedef Function<void(double allottedTimeMs)> IdleTask; |
| 29 | 33 |
| 30 static Scheduler* shared(); | 34 static Scheduler* shared(); |
| 31 static void initializeOnMainThread(); | 35 static void initializeOnMainThread(); |
| 32 static void shutdown(); | 36 static void shutdown(); |
| 33 | 37 |
| 34 // The following entrypoints are used to schedule different types of tasks | 38 // 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. | 39 // to be run on the main thread. They can be called from any thread. |
| 36 void postInputTask(const TraceLocation&, const Task&); | 40 void postInputTask(const TraceLocation&, const Task&); |
| 37 void postCompositorTask(const TraceLocation&, const Task&); | 41 void postCompositorTask(const TraceLocation&, const Task&); |
| 38 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 IdleTask&); // For non-critical tasks which may be r eordered relative to other task types. |
| 40 | 43 |
| 44 // For generic (low priority) tasks, can only be called from the main thread . | |
| 45 void postTask(const TraceLocation&, const Task&); | |
| 46 | |
| 41 // Returns true if there is high priority work pending on the main thread | 47 // 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. | 48 // and the caller should yield to let the scheduler service that work. |
| 43 // Can be called on the main thread. | 49 // Can be called on the main thread. |
| 44 bool shouldYieldForHighPriorityWork(); | 50 bool shouldYieldForHighPriorityWork(); |
| 45 | 51 |
| 46 // The shared timer can be used to schedule a periodic callback which may | 52 // The shared timer can be used to schedule a periodic callback which may |
| 47 // get preempted by higher priority work. | 53 // get preempted by higher priority work. |
| 48 void setSharedTimerFiredFunction(void (*function)()); | 54 void setSharedTimerFiredFunction(void (*function)()); |
| 49 void setSharedTimerFireInterval(double); | 55 void setSharedTimerFireInterval(double); |
| 50 void stopSharedTimer(); | 56 void stopSharedTimer(); |
| 51 | 57 |
| 58 void runPendingTasks(); | |
| 59 void allowMainThreadTaskPosting(); | |
| 60 | |
| 52 private: | 61 private: |
| 53 Scheduler(); | 62 Scheduler(); |
| 54 ~Scheduler(); | 63 ~Scheduler(); |
| 55 | 64 |
| 56 void scheduleTask(const TraceLocation&, const Task&); | |
| 57 void scheduleIdleTask(const IdleTask&); | 65 void scheduleIdleTask(const IdleTask&); |
| 58 | 66 |
| 59 static void sharedTimerAdapter(); | 67 static void sharedTimerAdapter(); |
| 60 void tickSharedTimer(); | 68 void tickSharedTimer(); |
| 61 | 69 |
| 62 static Scheduler* s_sharedScheduler; | 70 static Scheduler* s_sharedScheduler; |
| 63 blink::WebThread* m_mainThread; | 71 blink::WebThread* m_mainThread; |
| 64 | 72 |
| 73 class TracedTask { | |
| 74 public: | |
| 75 TracedTask(Task task, TraceLocation location) : m_task(task), m_location (location) { } | |
|
Sami
2014/08/06 10:19:52
nit: Initializer style.
alexclarke
2014/08/07 12:08:06
Done.
| |
| 76 | |
| 77 void run(); | |
| 78 | |
| 79 private: | |
| 80 Task m_task; | |
| 81 TraceLocation m_location; | |
| 82 }; | |
| 83 | |
| 84 // The input and compositor task queues can be posted to from any thread. | |
| 85 AtomicRingBuffer<TracedTask, 128> m_pendingInputTasks; | |
|
Sami
2014/08/06 10:19:52
One pattern we could steal from cc/ is to put memb
alexclarke
2014/08/07 12:08:06
I quite like that pattern, however I don't think w
| |
| 86 AtomicRingBuffer<TracedTask, 128> m_pendingCompositorTasks; | |
| 87 | |
| 88 // Blink tasks can only be posted on the main thread. | |
| 89 WTF::Deque<TracedTask> m_pendingBlinkTasks; | |
|
Sami
2014/08/06 10:19:52
m_pendingLowPriorityTasks or something?
alexclarke
2014/08/07 12:08:06
Done.
| |
| 90 | |
| 91 volatile int m_mainThreadTaskPosted; | |
| 92 volatile int m_blinkShouldNotYield; | |
| 93 | |
| 94 void setBlinkShouldYield(); | |
|
Sami
2014/08/06 10:19:52
How about setHighPriorityWorkPending to match shou
alexclarke
2014/08/07 12:08:06
I've decided not to use atomics, nailing down all
| |
| 95 void clearBlinkShouldYield(); | |
| 96 void maybePostTaskLoopOnMainThread(); | |
| 97 | |
| 65 void (*m_sharedTimerFunction)(); | 98 void (*m_sharedTimerFunction)(); |
| 66 }; | 99 }; |
| 67 | 100 |
| 68 } // namespace blink | 101 } // namespace blink |
| 69 | 102 |
| 70 #endif // Scheduler_h | 103 #endif // Scheduler_h |
| OLD | NEW |