| 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" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 virtual ~TimeDomain(); | 55 virtual ~TimeDomain(); |
| 56 | 56 |
| 57 // Returns a LazyNow that evaluate this TimeDomain's Now. Can be called from | 57 // Returns a LazyNow that evaluate this TimeDomain's Now. Can be called from |
| 58 // any thread. | 58 // any thread. |
| 59 // TODO(alexclarke): Make this main thread only. | 59 // TODO(alexclarke): Make this main thread only. |
| 60 virtual LazyNow CreateLazyNow() const = 0; | 60 virtual LazyNow CreateLazyNow() const = 0; |
| 61 | 61 |
| 62 // Evaluate this TimeDomain's Now. Can be called from any thread. | 62 // Evaluate this TimeDomain's Now. Can be called from any thread. |
| 63 virtual base::TimeTicks Now() const = 0; | 63 virtual base::TimeTicks Now() const = 0; |
| 64 | 64 |
| 65 // Computes the delay until the next task the TimeDomain is aware of, if any. | 65 // Some TimeDomains support virtual time, this method tells us to advance time |
| 66 // Note virtual time domains may return base::TimeDelta() if they have any | 66 // if possible and return true if time was advanced. |
| 67 // tasks that are eligible to run. | 67 virtual bool MaybeAdvanceTime() = 0; |
| 68 virtual base::Optional<base::TimeDelta> DelayTillNextTask( | |
| 69 LazyNow* lazy_now) = 0; | |
| 70 | 68 |
| 71 // Returns the name of this time domain for tracing. | 69 // Returns the name of this time domain for tracing. |
| 72 virtual const char* GetName() const = 0; | 70 virtual const char* GetName() const = 0; |
| 73 | 71 |
| 74 // If there is a scheduled delayed task, |out_time| is set to the scheduled | 72 // If there is a scheduled delayed task, |out_time| is set to the scheduled |
| 75 // runtime for the next one and it returns true. Returns false otherwise. | 73 // runtime for the next one and it returns true. Returns false otherwise. |
| 76 bool NextScheduledRunTime(base::TimeTicks* out_time) const; | 74 bool NextScheduledRunTime(base::TimeTicks* out_time) const; |
| 77 | 75 |
| 78 protected: | 76 protected: |
| 79 friend class internal::TaskQueueImpl; | 77 friend class internal::TaskQueueImpl; |
| 80 friend class TaskQueueManager; | 78 friend class TaskQueueManager; |
| 81 | 79 |
| 82 void AsValueInto(base::trace_event::TracedValue* state) const; | 80 void AsValueInto(base::trace_event::TracedValue* state) const; |
| 83 | 81 |
| 84 // Migrates |queue| from this time domain to |destination_time_domain|. | 82 // Migrates |queue| from this time domain to |destination_time_domain|. |
| 85 // Main-thread only. | 83 // Main-thread only. |
| 86 void MigrateQueue(internal::TaskQueueImpl* queue, | 84 void MigrateQueue(internal::TaskQueueImpl* queue, |
| 87 TimeDomain* destination_time_domain); | 85 TimeDomain* destination_time_domain); |
| 88 | 86 |
| 89 // If there is a scheduled delayed task, |out_task_queue| is set to the queue | 87 // If there is a scheduled delayed task, |out_task_queue| is set to the queue |
| 90 // the next task was posted to and it returns true. Returns false otherwise. | 88 // the next task was posted to and it returns true. Returns false otherwise. |
| 91 bool NextScheduledTaskQueue(TaskQueue** out_task_queue) const; | 89 bool NextScheduledTaskQueue(TaskQueue** out_task_queue) const; |
| 92 | 90 |
| 91 // Adds |queue| to the set of task queues that UpdateWorkQueues calls |
| 92 // UpdateWorkQueue on. |
| 93 void RegisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); |
| 94 |
| 93 // Schedules a call to TaskQueueImpl::WakeUpForDelayedWork when this | 95 // Schedules a call to TaskQueueImpl::WakeUpForDelayedWork when this |
| 94 // TimeDomain reaches |delayed_run_time|. This supersedes any previously | 96 // TimeDomain reaches |delayed_run_time|. This supersedes any previously |
| 95 // registered wakeup for |queue|. | 97 // registered wakeup for |queue|. |
| 96 void ScheduleDelayedWork(internal::TaskQueueImpl* queue, | 98 void ScheduleDelayedWork(internal::TaskQueueImpl* queue, |
| 97 base::TimeTicks delayed_run_time, | 99 base::TimeTicks delayed_run_time, |
| 98 base::TimeTicks now); | 100 base::TimeTicks now); |
| 99 | 101 |
| 100 // Registers the |queue|. | 102 // Registers the |queue|. |
| 101 void RegisterQueue(internal::TaskQueueImpl* queue); | 103 void RegisterQueue(internal::TaskQueueImpl* queue); |
| 102 | 104 |
| 105 // Removes |queue| from the set of task queues that UpdateWorkQueues calls |
| 106 // UpdateWorkQueue on. Returns true if |queue| was updatable. |
| 107 bool UnregisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); |
| 108 |
| 103 // Removes |queue| from all internal data structures. | 109 // Removes |queue| from all internal data structures. |
| 104 void UnregisterQueue(internal::TaskQueueImpl* queue); | 110 void UnregisterQueue(internal::TaskQueueImpl* queue); |
| 105 | 111 |
| 106 // Tells the time domain that |queue| went from having no immediate work to | 112 // Updates active queues associated with this TimeDomain. |
| 107 // having some. | 113 void UpdateWorkQueues(LazyNow lazy_now); |
| 108 void OnQueueHasImmediateWork(internal::TaskQueueImpl* queue); | |
| 109 | 114 |
| 110 // Called by the TaskQueueManager when the TimeDomain is registered. | 115 // Called by the TaskQueueManager when the TimeDomain is registered. |
| 111 virtual void OnRegisterWithTaskQueueManager( | 116 virtual void OnRegisterWithTaskQueueManager( |
| 112 TaskQueueManager* task_queue_manager) = 0; | 117 TaskQueueManager* task_queue_manager) = 0; |
| 113 | 118 |
| 114 // The implementaion will secedule task processing to run with |delay| with | 119 // The implementaion will secedule task processing to run with |delay| with |
| 115 // respect to the TimeDomain's time source. Always called on the main thread. | 120 // respect to the TimeDomain's time source. Always called on the main thread. |
| 116 // NOTE this is only called by ScheduleDelayedWork if the scheduled runtime | 121 // NOTE this is only called by ScheduleDelayedWork if the scheduled runtime |
| 117 // is sooner than any previously sheduled work or if there is no other | 122 // is sooner than any previously sheduled work or if there is no other |
| 118 // scheduled work. | 123 // scheduled work. |
| 119 virtual void RequestWakeup(base::TimeTicks now, base::TimeDelta delay) = 0; | 124 virtual void RequestWakeup(base::TimeTicks now, base::TimeDelta delay) = 0; |
| 120 | 125 |
| 121 // For implementation specific tracing. | 126 // For implementation specific tracing. |
| 122 virtual void AsValueIntoInternal( | 127 virtual void AsValueIntoInternal( |
| 123 base::trace_event::TracedValue* state) const = 0; | 128 base::trace_event::TracedValue* state) const = 0; |
| 124 | 129 |
| 125 // Call TaskQueueImpl::UpdateDelayedWorkQueue for each queue where the delay | 130 // Call TaskQueueImpl::UpdateDelayedWorkQueue for each queue where the delay |
| 126 // has elapsed. | 131 // has elapsed. |
| 127 void WakeupReadyDelayedQueues(LazyNow* lazy_now); | 132 void WakeupReadyDelayedQueues(LazyNow* lazy_now); |
| 128 | 133 |
| 129 size_t NumberOfScheduledWakeups() const { | 134 size_t NumberOfScheduledWakeups() const { |
| 130 return delayed_wakeup_queue_.size(); | 135 return delayed_wakeup_queue_.size(); |
| 131 } | 136 } |
| 132 | 137 |
| 133 private: | 138 private: |
| 139 void MoveNewlyUpdatableQueuesIntoUpdatableQueueSet(); |
| 140 |
| 134 struct DelayedWakeup { | 141 struct DelayedWakeup { |
| 135 base::TimeTicks time; | 142 base::TimeTicks time; |
| 136 internal::TaskQueueImpl* queue; | 143 internal::TaskQueueImpl* queue; |
| 137 | 144 |
| 138 bool operator<=(const DelayedWakeup& other) const { | 145 bool operator<=(const DelayedWakeup& other) const { |
| 139 if (time == other.time) | 146 if (time == other.time) |
| 140 return queue <= other.queue; | 147 return queue <= other.queue; |
| 141 return time < other.time; | 148 return time < other.time; |
| 142 } | 149 } |
| 143 | 150 |
| 144 void SetHeapHandle(HeapHandle handle) { | 151 void SetHeapHandle(HeapHandle handle) { |
| 145 DCHECK(handle.IsValid()); | 152 DCHECK(handle.IsValid()); |
| 146 queue->set_heap_handle(handle); | 153 queue->set_heap_handle(handle); |
| 147 } | 154 } |
| 148 | 155 |
| 149 void ClearHeapHandle() { | 156 void ClearHeapHandle() { |
| 150 DCHECK(queue->heap_handle().IsValid()); | 157 DCHECK(queue->heap_handle().IsValid()); |
| 151 queue->set_heap_handle(HeapHandle()); | 158 queue->set_heap_handle(HeapHandle()); |
| 152 | 159 |
| 153 DCHECK_NE(queue->scheduled_time_domain_wakeup(), base::TimeTicks()); | 160 DCHECK_NE(queue->scheduled_time_domain_wakeup(), base::TimeTicks()); |
| 154 queue->set_scheduled_time_domain_wakeup(base::TimeTicks()); | 161 queue->set_scheduled_time_domain_wakeup(base::TimeTicks()); |
| 155 } | 162 } |
| 156 }; | 163 }; |
| 157 | 164 |
| 158 IntrusiveHeap<DelayedWakeup> delayed_wakeup_queue_; | 165 IntrusiveHeap<DelayedWakeup> delayed_wakeup_queue_; |
| 159 | 166 |
| 167 // This lock guards only |newly_updatable_|. It's not expected to be heavily |
| 168 // contended. |
| 169 base::Lock newly_updatable_lock_; |
| 170 std::vector<internal::TaskQueueImpl*> newly_updatable_; |
| 171 |
| 172 // Set of task queues with avaliable work on the incoming queue. This should |
| 173 // only be accessed from the main thread. |
| 174 std::set<internal::TaskQueueImpl*> updatable_queue_set_; |
| 175 |
| 160 Observer* observer_; // NOT OWNED. | 176 Observer* observer_; // NOT OWNED. |
| 161 | 177 |
| 162 base::ThreadChecker main_thread_checker_; | 178 base::ThreadChecker main_thread_checker_; |
| 163 | 179 |
| 164 DISALLOW_COPY_AND_ASSIGN(TimeDomain); | 180 DISALLOW_COPY_AND_ASSIGN(TimeDomain); |
| 165 }; | 181 }; |
| 166 | 182 |
| 167 } // namespace scheduler | 183 } // namespace scheduler |
| 168 } // namespace blink | 184 } // namespace blink |
| 169 | 185 |
| 170 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TIME_DOMAIN_H_ | 186 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TIME_DOMAIN_H_ |
| OLD | NEW |