OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TIME_DOMAIN_H_ | 5 #ifndef THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TIME_DOMAIN_H_ |
6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TIME_DOMAIN_H_ | 6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TIME_DOMAIN_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "platform/scheduler/base/lazy_now.h" | 16 #include "platform/scheduler/base/lazy_now.h" |
17 #include "platform/scheduler/base/task_queue_impl.h" | 17 #include "platform/scheduler/base/task_queue_impl.h" |
18 | 18 |
19 namespace blink { | 19 namespace blink { |
20 namespace scheduler { | 20 namespace scheduler { |
21 namespace internal { | 21 namespace internal { |
22 class TaskQueueImpl; | 22 class TaskQueueImpl; |
23 } // internal | 23 } // internal |
24 class TaskQueueManager; | 24 class TaskQueueManager; |
25 class TaskQueueManagerDelegate; | 25 class TaskQueueManagerDelegate; |
26 | 26 |
27 // The TimeDomain's job is to keep track of moments when delayed tasks have been | 27 // The TimeDomain's job is to wake task queues up when their next delayed tasks |
28 // scheduled to fire and to notify their TaskQueues via UpdateDelayedWorkQueue. | 28 // are due to fire. TaskQueues request a wake up via ScheduleDelayedWork, when |
29 // the wake up is due the TimeDomain calls TaskQueue::WakeUpForDelayedWork. | |
30 // The TimeDomain communicates with the TaskQueueManager to actually schedule | |
31 // the wake-ups on the underlying base::MessageLoop. Various levels of de-duping | |
32 // are employed to prevent unnecessary posting of TaskQueueManager::DoWork. | |
29 // | 33 // |
30 // The time domain keeps track of the next wakeup required to pump delayed tasks | 34 // Note the TimeDomain only knows about the first wakeup per queue, it's the |
31 // and issues |RequestWakeup| calls to the subclass as needed. Where possible | 35 // responsibility of TaskQueueImpl to keep the time domain up to date if this |
32 // it tried to de-dupe these wakeups. Ideally it would be possible to cancel | 36 // changes. |
33 // them, but that's not currently supported by the base message loop. | |
34 // | |
35 // The clock itself is provided by subclasses of the TimeDomain and it may be | |
36 // the real wall clock or a synthetic (virtual) time base. | |
37 class BLINK_PLATFORM_EXPORT TimeDomain { | 37 class BLINK_PLATFORM_EXPORT TimeDomain { |
38 public: | 38 public: |
39 class BLINK_PLATFORM_EXPORT Observer { | 39 class BLINK_PLATFORM_EXPORT Observer { |
40 public: | 40 public: |
41 virtual ~Observer() {} | 41 virtual ~Observer() {} |
42 | 42 |
43 // Called when an empty TaskQueue registered with this TimeDomain has a task | 43 // Called when an empty TaskQueue registered with this TimeDomain has a task |
44 // enqueued. | 44 // enqueued. |
45 virtual void OnTimeDomainHasImmediateWork() = 0; | 45 virtual void OnTimeDomainHasImmediateWork() = 0; |
46 | 46 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 TimeDomain* destination_time_domain); | 82 TimeDomain* destination_time_domain); |
83 | 83 |
84 // If there is a scheduled delayed task, |out_task_queue| is set to the queue | 84 // If there is a scheduled delayed task, |out_task_queue| is set to the queue |
85 // the next task was posted to and it returns true. Returns false otherwise. | 85 // the next task was posted to and it returns true. Returns false otherwise. |
86 bool NextScheduledTaskQueue(TaskQueue** out_task_queue) const; | 86 bool NextScheduledTaskQueue(TaskQueue** out_task_queue) const; |
87 | 87 |
88 // Adds |queue| to the set of task queues that UpdateWorkQueues calls | 88 // Adds |queue| to the set of task queues that UpdateWorkQueues calls |
89 // UpdateWorkQueue on. | 89 // UpdateWorkQueue on. |
90 void RegisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); | 90 void RegisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); |
91 | 91 |
92 // Schedules a call to TaskQueueImpl::MoveReadyDelayedTasksToDelayedWorkQueue | 92 // Schedules a call to TaskQueueImpl::WakeUpForDelayedWork when this |
93 // when this TimeDomain reaches |delayed_run_time|. | 93 // TimeDomain reaches |delayed_run_time|. This supersedes any previously |
94 // registered wakeup for |queue|. | |
94 void ScheduleDelayedWork(internal::TaskQueueImpl* queue, | 95 void ScheduleDelayedWork(internal::TaskQueueImpl* queue, |
95 base::TimeTicks delayed_run_time, | 96 base::TimeTicks delayed_run_time, |
96 base::TimeTicks now); | 97 base::TimeTicks now); |
97 | 98 |
98 // Cancels a call to TaskQueueImpl::MoveReadyDelayedTasksToDelayedWorkQueue | |
99 // previously requested with ScheduleDelayedWork. Note this only works if | |
100 // delayed_run_time is _not_ the next scheduled run time. | |
101 void CancelDelayedWork(internal::TaskQueueImpl* queue, | |
102 base::TimeTicks delayed_run_time); | |
103 | |
104 // Registers the |queue|. | 99 // Registers the |queue|. |
105 void RegisterQueue(internal::TaskQueueImpl* queue); | 100 void RegisterQueue(internal::TaskQueueImpl* queue); |
106 | 101 |
107 // Removes |queue| from the set of task queues that UpdateWorkQueues calls | 102 // Removes |queue| from the set of task queues that UpdateWorkQueues calls |
108 // UpdateWorkQueue on. Returns true if |queue| was updatable. | 103 // UpdateWorkQueue on. Returns true if |queue| was updatable. |
109 bool UnregisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); | 104 bool UnregisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); |
110 | 105 |
111 // Removes |queue| from all internal data structures. | 106 // Removes |queue| from all internal data structures. |
112 void UnregisterQueue(internal::TaskQueueImpl* queue); | 107 void UnregisterQueue(internal::TaskQueueImpl* queue); |
113 | 108 |
(...skipping 12 matching lines...) Expand all Loading... | |
126 virtual void RequestWakeup(base::TimeTicks now, base::TimeDelta delay) = 0; | 121 virtual void RequestWakeup(base::TimeTicks now, base::TimeDelta delay) = 0; |
127 | 122 |
128 // For implementation specific tracing. | 123 // For implementation specific tracing. |
129 virtual void AsValueIntoInternal( | 124 virtual void AsValueIntoInternal( |
130 base::trace_event::TracedValue* state) const = 0; | 125 base::trace_event::TracedValue* state) const = 0; |
131 | 126 |
132 // Call TaskQueueImpl::UpdateDelayedWorkQueue for each queue where the delay | 127 // Call TaskQueueImpl::UpdateDelayedWorkQueue for each queue where the delay |
133 // has elapsed. | 128 // has elapsed. |
134 void WakeupReadyDelayedQueues(LazyNow* lazy_now); | 129 void WakeupReadyDelayedQueues(LazyNow* lazy_now); |
135 | 130 |
136 protected: | |
137 // Clears expired entries from |delayed_wakeup_multimap_|. Caution needs to be | |
138 // taken to ensure TaskQueueImpl::UpdateDelayedWorkQueue is called on the | |
139 // affected queues. | |
140 void ClearExpiredWakeups(); | |
141 | |
142 private: | 131 private: |
143 void MoveNewlyUpdatableQueuesIntoUpdatableQueueSet(); | 132 void MoveNewlyUpdatableQueuesIntoUpdatableQueueSet(); |
144 | 133 |
145 typedef std::multimap<base::TimeTicks, internal::TaskQueueImpl*> | 134 using DelayedWakeupMultimap = |
146 DelayedWakeupMultimap; | 135 std::multimap<base::TimeTicks, internal::TaskQueueImpl*>; |
147 | 136 |
148 DelayedWakeupMultimap delayed_wakeup_multimap_; | 137 DelayedWakeupMultimap delayed_wakeup_multimap_; |
149 | 138 |
139 // This map makes it easy to remove a queue from |delayed_wakeup_multimap_|. | |
140 // NOTE inserting or removing elements from a std::map does not invalidate any | |
141 // iterators. | |
142 using QueueToDelayedWakeupMultimapIteratorMap = | |
143 std::map<internal::TaskQueueImpl*, DelayedWakeupMultimap::iterator>; | |
altimin
2016/09/23 10:04:15
nit: unordered_map?
alex clarke (OOO till 29th)
2016/09/23 11:08:04
Yeah probably a small win, I expect boost::flat_ma
| |
144 | |
145 QueueToDelayedWakeupMultimapIteratorMap | |
146 queue_to_delayed_wakeup_multimap_iterator_map_; | |
147 | |
150 // This lock guards only |newly_updatable_|. It's not expected to be heavily | 148 // This lock guards only |newly_updatable_|. It's not expected to be heavily |
151 // contended. | 149 // contended. |
152 base::Lock newly_updatable_lock_; | 150 base::Lock newly_updatable_lock_; |
153 std::vector<internal::TaskQueueImpl*> newly_updatable_; | 151 std::vector<internal::TaskQueueImpl*> newly_updatable_; |
154 | 152 |
155 // Set of task queues with avaliable work on the incoming queue. This should | 153 // Set of task queues with avaliable work on the incoming queue. This should |
156 // only be accessed from the main thread. | 154 // only be accessed from the main thread. |
157 std::set<internal::TaskQueueImpl*> updatable_queue_set_; | 155 std::set<internal::TaskQueueImpl*> updatable_queue_set_; |
158 | 156 |
159 Observer* observer_; // NOT OWNED. | 157 Observer* observer_; // NOT OWNED. |
160 | 158 |
161 base::ThreadChecker main_thread_checker_; | 159 base::ThreadChecker main_thread_checker_; |
162 | 160 |
163 DISALLOW_COPY_AND_ASSIGN(TimeDomain); | 161 DISALLOW_COPY_AND_ASSIGN(TimeDomain); |
164 }; | 162 }; |
165 | 163 |
166 } // namespace scheduler | 164 } // namespace scheduler |
167 } // namespace blink | 165 } // namespace blink |
168 | 166 |
169 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TIME_DOMAIN_H_ | 167 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TIME_DOMAIN_H_ |
OLD | NEW |