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" | 9 #include "platform/TraceLocation.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 } | 17 } |
| 18 | 18 |
| 19 namespace blink { | 19 namespace blink { |
| 20 | 20 |
| 21 typedef long long unsigned FlowTraceID; | |
|
Sami
2014/08/20 14:20:57
Probably better to use int64_t for this. Actually,
picksi1
2014/08/29 14:11:28
Changed to an int so keep atomicincrement happy
| |
| 22 | |
| 21 // 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 |
| 22 // 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 |
| 23 // scheduling policy and the overall system state. | 25 // scheduling policy and the overall system state. |
| 24 class PLATFORM_EXPORT Scheduler { | 26 class PLATFORM_EXPORT Scheduler { |
| 25 WTF_MAKE_NONCOPYABLE(Scheduler); | 27 WTF_MAKE_NONCOPYABLE(Scheduler); |
| 26 public: | 28 public: |
| 27 typedef Function<void()> Task; | 29 typedef Function<void()> Task; |
| 28 // 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. |
| 29 typedef Function<void(double allottedTimeMs)> IdleTask; | 31 typedef Function<void(double allottedTimeMs)> IdleTask; |
| 30 | 32 |
| 31 static Scheduler* shared(); | 33 static Scheduler* shared(); |
| 32 static void initializeOnMainThread(); | 34 static void initializeOnMainThread(); |
| 33 static void shutdown(); | 35 static void shutdown(); |
| 34 | 36 |
| 35 // The following entrypoints are used to schedule different types of tasks | 37 // The following entrypoints are used to schedule different types of tasks |
| 36 // 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. |
| 37 void postInputTask(const TraceLocation&, const Task&); | 39 void postInputTask(const TraceLocation&, const Task&); |
| 38 void postCompositorTask(const TraceLocation&, const Task&); | 40 void postCompositorTask(const TraceLocation&, const Task&); |
| 39 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. |
| 42 | |
| 40 void postIdleTask(const TraceLocation&, const IdleTask&); // For non-critica l tasks which may be reordered relative to other task types. | 43 void postIdleTask(const TraceLocation&, const IdleTask&); // For non-critica l tasks which may be reordered relative to other task types. |
| 41 | 44 |
| 42 // Returns true if there is high priority work pending on the main thread | 45 // Returns true if there is high priority work pending on the main thread |
| 43 // and the caller should yield to let the scheduler service that work. | 46 // and the caller should yield to let the scheduler service that work. |
| 44 // Can be called on any thread. | 47 // Can be called on any thread. |
| 45 bool shouldYieldForHighPriorityWork() const; | 48 bool shouldYieldForHighPriorityWork() const; |
| 46 | 49 |
| 47 // The shared timer can be used to schedule a periodic callback which may | 50 // The shared timer can be used to schedule a periodic callback which may |
| 48 // get preempted by higher priority work. | 51 // get preempted by higher priority work. |
| 49 void setSharedTimerFiredFunction(void (*function)()); | 52 void setSharedTimerFiredFunction(void (*function)()); |
| 50 void setSharedTimerFireInterval(double); | 53 void setSharedTimerFireInterval(double); |
| 51 void stopSharedTimer(); | 54 void stopSharedTimer(); |
| 52 | 55 |
| 53 private: | 56 private: |
| 54 class MainThreadPendingTaskRunner; | 57 class MainThreadPendingTaskRunner; |
| 55 class MainThreadPendingHighPriorityTaskRunner; | 58 class MainThreadPendingHighPriorityTaskRunner; |
| 56 friend class MainThreadPendingTaskRunner; | 59 friend class MainThreadPendingTaskRunner; |
| 57 friend class MainThreadPendingHighPriorityTaskRunner; | 60 friend class MainThreadPendingHighPriorityTaskRunner; |
| 58 | 61 |
| 59 Scheduler(); | 62 Scheduler(); |
| 60 ~Scheduler(); | 63 ~Scheduler(); |
| 61 | 64 |
| 62 void scheduleIdleTask(const TraceLocation&, const IdleTask&); | 65 void scheduleIdleTask(const TraceLocation&, const IdleTask&); |
| 63 | |
| 64 static void sharedTimerAdapter(); | 66 static void sharedTimerAdapter(); |
| 65 void tickSharedTimer(); | 67 void tickSharedTimer(); |
| 66 | 68 |
| 69 | |
| 67 bool hasPendingHighPriorityWork() const; | 70 bool hasPendingHighPriorityWork() const; |
| 68 void runHighPriorityTasks(); | 71 void runHighPriorityTasks(); |
| 69 | 72 |
| 70 static Scheduler* s_sharedScheduler; | 73 static Scheduler* s_sharedScheduler; |
| 71 | 74 |
| 72 class TracedTask { | 75 class TracedTask { |
| 73 public: | 76 public: |
| 74 TracedTask(const Task& task, const TraceLocation& location) | 77 |
| 75 : m_task(task) | 78 enum Type { |
| 76 , m_location(location) { } | 79 Compositor, |
| 80 Input, | |
| 81 MainThread, | |
| 82 TypeCount | |
| 83 }; | |
| 84 | |
| 85 TracedTask(const Task&, const TraceLocation&, Type); | |
| 86 static void setTypeName(Type, char const*); | |
| 77 | 87 |
| 78 void run(); | 88 void run(); |
| 79 | 89 |
| 90 FlowTraceID m_FlowTraceID; | |
|
Sami
2014/08/20 14:20:57
s/m_FlowTraceID/m_flowTraceID/
picksi1
2014/08/29 14:11:28
This is no longer static in latest version
| |
| 91 | |
| 80 private: | 92 private: |
| 81 Task m_task; | 93 Task m_task; |
| 82 TraceLocation m_location; | 94 TraceLocation m_location; |
| 95 Type m_type; | |
|
Sami
2014/08/20 14:20:57
Instead of having a type enum, could we just pass
picksi1
2014/08/29 14:11:28
Done.
| |
| 96 static FlowTraceID s_NextFlowTraceID; | |
| 97 static char const* s_typeNames[Type::TypeCount]; | |
| 83 }; | 98 }; |
| 84 | 99 |
| 85 // Should only be accessed from the main thread. | 100 // Should only be accessed from the main thread. |
| 86 void (*m_sharedTimerFunction)(); | 101 void (*m_sharedTimerFunction)(); |
| 87 | 102 |
| 88 // These members can be accessed from any thread. | 103 // These members can be accessed from any thread. |
| 89 WebThread* m_mainThread; | 104 WebThread* m_mainThread; |
| 90 | 105 |
| 91 // This mutex protects calls to the pending task queues. | 106 // This mutex protects calls to the pending task queues. |
| 92 Mutex m_pendingTasksMutex; | 107 Mutex m_pendingTasksMutex; |
| 93 DoubleBufferedDeque<TracedTask> m_pendingInputTasks; | 108 DoubleBufferedDeque<TracedTask> m_pendingInputTasks; |
| 94 DoubleBufferedDeque<TracedTask> m_pendingCompositorTasks; | 109 DoubleBufferedDeque<TracedTask> m_pendingCompositorTasks; |
| 95 | 110 |
| 96 volatile int m_highPriorityTaskCount; | 111 volatile int m_highPriorityTaskCount; |
| 97 }; | 112 }; |
| 98 | 113 |
| 99 } // namespace blink | 114 } // namespace blink |
| 100 | 115 |
| 101 #endif // Scheduler_h | 116 #endif // Scheduler_h |
| OLD | NEW |