| 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 13 matching lines...) Expand all Loading... |
| 24 } // internal | 24 } // internal |
| 25 class TaskQueueManager; | 25 class TaskQueueManager; |
| 26 | 26 |
| 27 // The TimeDomain's job is to wake task queues up when their next delayed tasks | 27 // The TimeDomain's job is to wake task queues up when their next delayed tasks |
| 28 // are due to fire. TaskQueues request a wake up via ScheduleDelayedWork, when | 28 // are due to fire. TaskQueues request a wake up via ScheduleDelayedWork, when |
| 29 // the wake up is due the TimeDomain calls TaskQueue::WakeUpForDelayedWork. | 29 // the wake up is due the TimeDomain calls TaskQueue::WakeUpForDelayedWork. |
| 30 // The TimeDomain communicates with the TaskQueueManager to actually schedule | 30 // The TimeDomain communicates with the TaskQueueManager to actually schedule |
| 31 // the wake-ups on the underlying base::MessageLoop. Various levels of de-duping | 31 // the wake-ups on the underlying base::MessageLoop. Various levels of de-duping |
| 32 // are employed to prevent unnecessary posting of TaskQueueManager::DoWork. | 32 // are employed to prevent unnecessary posting of TaskQueueManager::DoWork. |
| 33 // | 33 // |
| 34 // Note the TimeDomain only knows about the first wakeup per queue, it's the | 34 // Note the TimeDomain only knows about the first wake-up per queue, it's the |
| 35 // responsibility of TaskQueueImpl to keep the time domain up to date if this | 35 // responsibility of TaskQueueImpl to keep the time domain up to date if this |
| 36 // changes. | 36 // changes. |
| 37 class BLINK_PLATFORM_EXPORT TimeDomain { | 37 class BLINK_PLATFORM_EXPORT TimeDomain { |
| 38 public: | 38 public: |
| 39 class BLINK_PLATFORM_EXPORT Observer { | 39 TimeDomain(); |
| 40 public: | |
| 41 virtual ~Observer() {} | |
| 42 | |
| 43 // Called when an empty TaskQueue registered with this TimeDomain has a task | |
| 44 // enqueued. | |
| 45 // |task_queue| - task queue which has immediate work scheduled. | |
| 46 virtual void OnTimeDomainHasImmediateWork(TaskQueue* task_queue) = 0; | |
| 47 | |
| 48 // Called when a TaskQueue registered with this TimeDomain has a delayed | |
| 49 // task enqueued. | |
| 50 // |task_queue| - task queue which has delayed work scheduled. | |
| 51 virtual void OnTimeDomainHasDelayedWork(TaskQueue* task_queue) = 0; | |
| 52 }; | |
| 53 | |
| 54 explicit TimeDomain(Observer* observer); | |
| 55 virtual ~TimeDomain(); | 40 virtual ~TimeDomain(); |
| 56 | 41 |
| 57 // Returns a LazyNow that evaluate this TimeDomain's Now. Can be called from | 42 // Returns a LazyNow that evaluate this TimeDomain's Now. Can be called from |
| 58 // any thread. | 43 // any thread. |
| 59 // TODO(alexclarke): Make this main thread only. | 44 // TODO(alexclarke): Make this main thread only. |
| 60 virtual LazyNow CreateLazyNow() const = 0; | 45 virtual LazyNow CreateLazyNow() const = 0; |
| 61 | 46 |
| 62 // Evaluate this TimeDomain's Now. Can be called from any thread. | 47 // Evaluate this TimeDomain's Now. Can be called from any thread. |
| 63 virtual base::TimeTicks Now() const = 0; | 48 virtual base::TimeTicks Now() const = 0; |
| 64 | 49 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 79 protected: | 64 protected: |
| 80 friend class internal::TaskQueueImpl; | 65 friend class internal::TaskQueueImpl; |
| 81 friend class TaskQueueManager; | 66 friend class TaskQueueManager; |
| 82 | 67 |
| 83 void AsValueInto(base::trace_event::TracedValue* state) const; | 68 void AsValueInto(base::trace_event::TracedValue* state) const; |
| 84 | 69 |
| 85 // If there is a scheduled delayed task, |out_task_queue| is set to the queue | 70 // If there is a scheduled delayed task, |out_task_queue| is set to the queue |
| 86 // the next task was posted to and it returns true. Returns false otherwise. | 71 // the next task was posted to and it returns true. Returns false otherwise. |
| 87 bool NextScheduledTaskQueue(TaskQueue** out_task_queue) const; | 72 bool NextScheduledTaskQueue(TaskQueue** out_task_queue) const; |
| 88 | 73 |
| 89 // Notifies the time domain observer (if any) that |queue| has incoming | |
| 90 // immediate work. | |
| 91 void OnQueueHasImmediateWork(internal::TaskQueueImpl* queue); | |
| 92 | |
| 93 // Schedules a call to TaskQueueImpl::WakeUpForDelayedWork when this | 74 // Schedules a call to TaskQueueImpl::WakeUpForDelayedWork when this |
| 94 // TimeDomain reaches |delayed_run_time|. This supersedes any previously | 75 // TimeDomain reaches |delayed_run_time|. This supersedes any previously |
| 95 // registered wakeup for |queue|. | 76 // registered wake-up for |queue|. |
| 96 void ScheduleDelayedWork(internal::TaskQueueImpl* queue, | 77 void ScheduleDelayedWork(internal::TaskQueueImpl* queue, |
| 97 internal::TaskQueueImpl::DelayedWakeUp wake_up, | 78 internal::TaskQueueImpl::DelayedWakeUp wake_up, |
| 98 base::TimeTicks now); | 79 base::TimeTicks now); |
| 99 | 80 |
| 100 // Cancels any scheduled calls to TaskQueueImpl::WakeUpForDelayedWork for | 81 // Cancels any scheduled calls to TaskQueueImpl::WakeUpForDelayedWork for |
| 101 // |queue|. | 82 // |queue|. |
| 102 void CancelDelayedWork(internal::TaskQueueImpl* queue); | 83 void CancelDelayedWork(internal::TaskQueueImpl* queue); |
| 103 | 84 |
| 104 // Registers the |queue|. | 85 // Registers the |queue|. |
| 105 void RegisterQueue(internal::TaskQueueImpl* queue); | 86 void RegisterQueue(internal::TaskQueueImpl* queue); |
| 106 | 87 |
| 107 // Removes |queue| from all internal data structures. | 88 // Removes |queue| from all internal data structures. |
| 108 void UnregisterQueue(internal::TaskQueueImpl* queue); | 89 void UnregisterQueue(internal::TaskQueueImpl* queue); |
| 109 | 90 |
| 110 // Called by the TaskQueueManager when the TimeDomain is registered. | 91 // Called by the TaskQueueManager when the TimeDomain is registered. |
| 111 virtual void OnRegisterWithTaskQueueManager( | 92 virtual void OnRegisterWithTaskQueueManager( |
| 112 TaskQueueManager* task_queue_manager) = 0; | 93 TaskQueueManager* task_queue_manager) = 0; |
| 113 | 94 |
| 114 // The implementation will schedule task processing to run at time |run_time| | 95 // The implementation will schedule task processing to run at time |run_time| |
| 115 // within the TimeDomain's time line. Only called from the main thread. | 96 // within the TimeDomain's time line. Only called from the main thread. |
| 116 // NOTE this is only called by ScheduleDelayedWork if the scheduled runtime | 97 // 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 | 98 // is sooner than any previously sheduled work or if there is no other |
| 118 // scheduled work. | 99 // scheduled work. |
| 119 virtual void RequestWakeupAt(base::TimeTicks now, | 100 virtual void RequestWakeUpAt(base::TimeTicks now, |
| 120 base::TimeTicks run_time) = 0; | 101 base::TimeTicks run_time) = 0; |
| 121 | 102 |
| 122 // The implementation will cancel a wake up previously requested by | 103 // The implementation will cancel a wake up previously requested by |
| 123 // RequestWakeupAt. It's expected this will be a NOP for most virtual time | 104 // RequestWakeUpAt. It's expected this will be a NOP for most virtual time |
| 124 // domains. | 105 // domains. |
| 125 virtual void CancelWakeupAt(base::TimeTicks run_time) = 0; | 106 virtual void CancelWakeUpAt(base::TimeTicks run_time) = 0; |
| 126 | 107 |
| 127 // For implementation specific tracing. | 108 // For implementation specific tracing. |
| 128 virtual void AsValueIntoInternal( | 109 virtual void AsValueIntoInternal( |
| 129 base::trace_event::TracedValue* state) const = 0; | 110 base::trace_event::TracedValue* state) const = 0; |
| 130 | 111 |
| 131 // Call TaskQueueImpl::UpdateDelayedWorkQueue for each queue where the delay | 112 // Call TaskQueueImpl::UpdateDelayedWorkQueue for each queue where the delay |
| 132 // has elapsed. | 113 // has elapsed. |
| 133 void WakeupReadyDelayedQueues(LazyNow* lazy_now); | 114 void WakeUpReadyDelayedQueues(LazyNow* lazy_now); |
| 134 | 115 |
| 135 size_t NumberOfScheduledWakeups() const { | 116 size_t NumberOfScheduledWakeUps() const { |
| 136 return delayed_wakeup_queue_.size(); | 117 return delayed_wake_up_queue_.size(); |
| 137 } | 118 } |
| 138 | 119 |
| 139 private: | 120 private: |
| 140 struct ScheduledDelayedWakeUp { | 121 struct ScheduledDelayedWakeUp { |
| 141 internal::TaskQueueImpl::DelayedWakeUp wake_up; | 122 internal::TaskQueueImpl::DelayedWakeUp wake_up; |
| 142 internal::TaskQueueImpl* queue; | 123 internal::TaskQueueImpl* queue; |
| 143 | 124 |
| 144 bool operator<=(const ScheduledDelayedWakeUp& other) const { | 125 bool operator<=(const ScheduledDelayedWakeUp& other) const { |
| 145 return wake_up <= other.wake_up; | 126 return wake_up <= other.wake_up; |
| 146 } | 127 } |
| 147 | 128 |
| 148 void SetHeapHandle(HeapHandle handle) { | 129 void SetHeapHandle(HeapHandle handle) { |
| 149 DCHECK(handle.IsValid()); | 130 DCHECK(handle.IsValid()); |
| 150 queue->set_heap_handle(handle); | 131 queue->set_heap_handle(handle); |
| 151 } | 132 } |
| 152 | 133 |
| 153 void ClearHeapHandle() { | 134 void ClearHeapHandle() { |
| 154 DCHECK(queue->heap_handle().IsValid()); | 135 DCHECK(queue->heap_handle().IsValid()); |
| 155 queue->set_heap_handle(HeapHandle()); | 136 queue->set_heap_handle(HeapHandle()); |
| 156 | 137 |
| 157 DCHECK_NE(queue->scheduled_time_domain_wakeup(), base::TimeTicks()); | 138 DCHECK_NE(queue->scheduled_time_domain_wake_up(), base::TimeTicks()); |
| 158 queue->set_scheduled_time_domain_wakeup(base::TimeTicks()); | 139 queue->set_scheduled_time_domain_wake_up(base::TimeTicks()); |
| 159 } | 140 } |
| 160 }; | 141 }; |
| 161 | 142 |
| 162 IntrusiveHeap<ScheduledDelayedWakeUp> delayed_wakeup_queue_; | 143 IntrusiveHeap<ScheduledDelayedWakeUp> delayed_wake_up_queue_; |
| 163 | |
| 164 Observer* const observer_; // NOT OWNED. | |
| 165 | 144 |
| 166 base::ThreadChecker main_thread_checker_; | 145 base::ThreadChecker main_thread_checker_; |
| 167 | 146 |
| 168 DISALLOW_COPY_AND_ASSIGN(TimeDomain); | 147 DISALLOW_COPY_AND_ASSIGN(TimeDomain); |
| 169 }; | 148 }; |
| 170 | 149 |
| 171 } // namespace scheduler | 150 } // namespace scheduler |
| 172 } // namespace blink | 151 } // namespace blink |
| 173 | 152 |
| 174 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TIME_DOMAIN_H_ | 153 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_TIME_DOMAIN_H_ |
| OLD | NEW |