| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_SCHEDULER_BASE_TIME_DOMAIN_H_ | |
| 6 #define COMPONENTS_SCHEDULER_BASE_TIME_DOMAIN_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/memory/weak_ptr.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "components/scheduler/base/lazy_now.h" | |
| 17 #include "components/scheduler/base/task_queue_impl.h" | |
| 18 #include "components/scheduler/scheduler_export.h" | |
| 19 | |
| 20 namespace scheduler { | |
| 21 namespace internal { | |
| 22 class TaskQueueImpl; | |
| 23 } // internal | |
| 24 class TaskQueueManager; | |
| 25 class TaskQueueManagerDelegate; | |
| 26 | |
| 27 class SCHEDULER_EXPORT TimeDomain { | |
| 28 public: | |
| 29 class SCHEDULER_EXPORT Observer { | |
| 30 public: | |
| 31 virtual ~Observer() {} | |
| 32 | |
| 33 // Called when an empty TaskQueue registered with this TimeDomain has a task | |
| 34 // enqueued. | |
| 35 virtual void OnTimeDomainHasImmediateWork() = 0; | |
| 36 | |
| 37 // Called when a TaskQueue registered with this TimeDomain has a delayed | |
| 38 // task enqueued. | |
| 39 virtual void OnTimeDomainHasDelayedWork() = 0; | |
| 40 }; | |
| 41 | |
| 42 explicit TimeDomain(Observer* observer); | |
| 43 virtual ~TimeDomain(); | |
| 44 | |
| 45 // Returns a LazyNow that evaluate this TimeDomain's Now. Can be called from | |
| 46 // any thread. | |
| 47 // TODO(alexclarke): Make this main thread only. | |
| 48 virtual LazyNow CreateLazyNow() const = 0; | |
| 49 | |
| 50 // Evaluate this TimeDomain's Now. Can be called from any thread. | |
| 51 virtual base::TimeTicks Now() const = 0; | |
| 52 | |
| 53 // Computes a runtime which is >= |time_domain_now| + |delay|. This is used to | |
| 54 // allow the TimeDomain to decide if the real or virtual time should be used | |
| 55 // when computing the task run time. This can be called from any thread. | |
| 56 virtual base::TimeTicks ComputeDelayedRunTime( | |
| 57 base::TimeTicks time_domain_now, | |
| 58 base::TimeDelta delay) const = 0; | |
| 59 | |
| 60 // Some TimeDomains support virtual time, this method tells us to advance time | |
| 61 // if possible and return true if time was advanced. | |
| 62 virtual bool MaybeAdvanceTime() = 0; | |
| 63 | |
| 64 // Returns the name of this time domain for tracing. | |
| 65 virtual const char* GetName() const = 0; | |
| 66 | |
| 67 // If there is a scheduled delayed task, |out_time| is set to the scheduled | |
| 68 // runtime for the next one and it returns true. Returns false otherwise. | |
| 69 bool NextScheduledRunTime(base::TimeTicks* out_time) const; | |
| 70 | |
| 71 protected: | |
| 72 friend class internal::TaskQueueImpl; | |
| 73 friend class TaskQueueManager; | |
| 74 | |
| 75 void AsValueInto(base::trace_event::TracedValue* state) const; | |
| 76 | |
| 77 // Migrates |queue| from this time domain to |destination_time_domain|. | |
| 78 void MigrateQueue(internal::TaskQueueImpl* queue, | |
| 79 TimeDomain* destination_time_domain); | |
| 80 | |
| 81 // If there is a scheduled delayed task, |out_task_queue| is set to the queue | |
| 82 // the next task was posted to and it returns true. Returns false otherwise. | |
| 83 bool NextScheduledTaskQueue(TaskQueue** out_task_queue) const; | |
| 84 | |
| 85 // Adds |queue| to the set of task queues that UpdateWorkQueues calls | |
| 86 // UpdateWorkQueue on. | |
| 87 void RegisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); | |
| 88 | |
| 89 // Schedules a call to TaskQueueImpl::MoveReadyDelayedTasksToDelayedWorkQueue | |
| 90 // when this TimeDomain reaches |delayed_run_time|. | |
| 91 void ScheduleDelayedWork(internal::TaskQueueImpl* queue, | |
| 92 base::TimeTicks delayed_run_time, | |
| 93 base::TimeTicks now); | |
| 94 | |
| 95 // Registers the |queue|. | |
| 96 void RegisterQueue(internal::TaskQueueImpl* queue); | |
| 97 | |
| 98 // Removes |queue| from the set of task queues that UpdateWorkQueues calls | |
| 99 // UpdateWorkQueue on. | |
| 100 void UnregisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); | |
| 101 | |
| 102 // Removes |queue| from all internal data structures. | |
| 103 void UnregisterQueue(internal::TaskQueueImpl* queue); | |
| 104 | |
| 105 // Updates active queues associated with this TimeDomain. | |
| 106 void UpdateWorkQueues(bool should_trigger_wakeup, | |
| 107 const internal::TaskQueueImpl::Task* previous_task, | |
| 108 LazyNow lazy_now); | |
| 109 | |
| 110 // Called by the TaskQueueManager when the TimeDomain is registered. | |
| 111 virtual void OnRegisterWithTaskQueueManager( | |
| 112 TaskQueueManager* task_queue_manager) = 0; | |
| 113 | |
| 114 // 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. | |
| 116 // 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 | |
| 118 // scheduled work. | |
| 119 virtual void RequestWakeup(base::TimeTicks now, base::TimeDelta delay) = 0; | |
| 120 | |
| 121 // For implementation specific tracing. | |
| 122 virtual void AsValueIntoInternal( | |
| 123 base::trace_event::TracedValue* state) const = 0; | |
| 124 | |
| 125 // Call TaskQueueImpl::UpdateDelayedWorkQueue for each queue where the delay | |
| 126 // has elapsed. | |
| 127 void WakeupReadyDelayedQueues( | |
| 128 LazyNow* lazy_now, | |
| 129 bool should_trigger_wakeup, | |
| 130 const internal::TaskQueueImpl::Task* previous_task); | |
| 131 | |
| 132 protected: | |
| 133 // Clears expired entries from |delayed_wakeup_multimap_|. Caution needs to be | |
| 134 // taken to ensure TaskQueueImpl::UpdateDelayedWorkQueue or | |
| 135 // TaskQueueImpl::Pump is called on the affected queues. | |
| 136 void ClearExpiredWakeups(); | |
| 137 | |
| 138 private: | |
| 139 void MoveNewlyUpdatableQueuesIntoUpdatableQueueSet(); | |
| 140 | |
| 141 typedef std::multimap<base::TimeTicks, internal::TaskQueueImpl*> | |
| 142 DelayedWakeupMultimap; | |
| 143 | |
| 144 DelayedWakeupMultimap delayed_wakeup_multimap_; | |
| 145 | |
| 146 // This lock guards only |newly_updatable_|. It's not expected to be heavily | |
| 147 // contended. | |
| 148 base::Lock newly_updatable_lock_; | |
| 149 std::vector<internal::TaskQueueImpl*> newly_updatable_; | |
| 150 | |
| 151 // Set of task queues with avaliable work on the incoming queue. This should | |
| 152 // only be accessed from the main thread. | |
| 153 std::set<internal::TaskQueueImpl*> updatable_queue_set_; | |
| 154 | |
| 155 Observer* observer_; // NOT OWNED. | |
| 156 | |
| 157 base::ThreadChecker main_thread_checker_; | |
| 158 | |
| 159 DISALLOW_COPY_AND_ASSIGN(TimeDomain); | |
| 160 }; | |
| 161 | |
| 162 } // namespace scheduler | |
| 163 | |
| 164 #endif // COMPONENTS_SCHEDULER_BASE_TIME_DOMAIN_H_ | |
| OLD | NEW |