| 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 // Some TimeDomains support virtual time, this method tells us to advance time | |
| 54 // if possible and return true if time was advanced. | |
| 55 virtual bool MaybeAdvanceTime() = 0; | |
| 56 | |
| 57 // Returns the name of this time domain for tracing. | |
| 58 virtual const char* GetName() const = 0; | |
| 59 | |
| 60 // If there is a scheduled delayed task, |out_time| is set to the scheduled | |
| 61 // runtime for the next one and it returns true. Returns false otherwise. | |
| 62 bool NextScheduledRunTime(base::TimeTicks* out_time) const; | |
| 63 | |
| 64 protected: | |
| 65 friend class internal::TaskQueueImpl; | |
| 66 friend class TaskQueueManager; | |
| 67 | |
| 68 void AsValueInto(base::trace_event::TracedValue* state) const; | |
| 69 | |
| 70 // Migrates |queue| from this time domain to |destination_time_domain|. | |
| 71 void MigrateQueue(internal::TaskQueueImpl* queue, | |
| 72 TimeDomain* destination_time_domain); | |
| 73 | |
| 74 // If there is a scheduled delayed task, |out_task_queue| is set to the queue | |
| 75 // the next task was posted to and it returns true. Returns false otherwise. | |
| 76 bool NextScheduledTaskQueue(TaskQueue** out_task_queue) const; | |
| 77 | |
| 78 // Adds |queue| to the set of task queues that UpdateWorkQueues calls | |
| 79 // UpdateWorkQueue on. | |
| 80 void RegisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); | |
| 81 | |
| 82 // Schedules a call to TaskQueueImpl::MoveReadyDelayedTasksToDelayedWorkQueue | |
| 83 // when this TimeDomain reaches |delayed_run_time|. | |
| 84 void ScheduleDelayedWork(internal::TaskQueueImpl* queue, | |
| 85 base::TimeTicks delayed_run_time, | |
| 86 base::TimeTicks now); | |
| 87 | |
| 88 // Registers the |queue|. | |
| 89 void RegisterQueue(internal::TaskQueueImpl* queue); | |
| 90 | |
| 91 // Removes |queue| from the set of task queues that UpdateWorkQueues calls | |
| 92 // UpdateWorkQueue on. Returns true if |queue| was updatable. | |
| 93 bool UnregisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); | |
| 94 | |
| 95 // Removes |queue| from all internal data structures. | |
| 96 void UnregisterQueue(internal::TaskQueueImpl* queue); | |
| 97 | |
| 98 // Updates active queues associated with this TimeDomain. | |
| 99 void UpdateWorkQueues(bool should_trigger_wakeup, | |
| 100 const internal::TaskQueueImpl::Task* previous_task, | |
| 101 LazyNow lazy_now); | |
| 102 | |
| 103 // Called by the TaskQueueManager when the TimeDomain is registered. | |
| 104 virtual void OnRegisterWithTaskQueueManager( | |
| 105 TaskQueueManager* task_queue_manager) = 0; | |
| 106 | |
| 107 // The implementaion will secedule task processing to run with |delay| with | |
| 108 // respect to the TimeDomain's time source. Always called on the main thread. | |
| 109 // NOTE this is only called by ScheduleDelayedWork if the scheduled runtime | |
| 110 // is sooner than any previously sheduled work or if there is no other | |
| 111 // scheduled work. | |
| 112 virtual void RequestWakeup(base::TimeTicks now, base::TimeDelta delay) = 0; | |
| 113 | |
| 114 // For implementation specific tracing. | |
| 115 virtual void AsValueIntoInternal( | |
| 116 base::trace_event::TracedValue* state) const = 0; | |
| 117 | |
| 118 // Call TaskQueueImpl::UpdateDelayedWorkQueue for each queue where the delay | |
| 119 // has elapsed. | |
| 120 void WakeupReadyDelayedQueues( | |
| 121 LazyNow* lazy_now, | |
| 122 bool should_trigger_wakeup, | |
| 123 const internal::TaskQueueImpl::Task* previous_task); | |
| 124 | |
| 125 protected: | |
| 126 // Clears expired entries from |delayed_wakeup_multimap_|. Caution needs to be | |
| 127 // taken to ensure TaskQueueImpl::UpdateDelayedWorkQueue or | |
| 128 // TaskQueueImpl::Pump is called on the affected queues. | |
| 129 void ClearExpiredWakeups(); | |
| 130 | |
| 131 private: | |
| 132 void MoveNewlyUpdatableQueuesIntoUpdatableQueueSet(); | |
| 133 | |
| 134 typedef std::multimap<base::TimeTicks, internal::TaskQueueImpl*> | |
| 135 DelayedWakeupMultimap; | |
| 136 | |
| 137 DelayedWakeupMultimap delayed_wakeup_multimap_; | |
| 138 | |
| 139 // This lock guards only |newly_updatable_|. It's not expected to be heavily | |
| 140 // contended. | |
| 141 base::Lock newly_updatable_lock_; | |
| 142 std::vector<internal::TaskQueueImpl*> newly_updatable_; | |
| 143 | |
| 144 // Set of task queues with avaliable work on the incoming queue. This should | |
| 145 // only be accessed from the main thread. | |
| 146 std::set<internal::TaskQueueImpl*> updatable_queue_set_; | |
| 147 | |
| 148 Observer* observer_; // NOT OWNED. | |
| 149 | |
| 150 base::ThreadChecker main_thread_checker_; | |
| 151 | |
| 152 DISALLOW_COPY_AND_ASSIGN(TimeDomain); | |
| 153 }; | |
| 154 | |
| 155 } // namespace scheduler | |
| 156 | |
| 157 #endif // COMPONENTS_SCHEDULER_BASE_TIME_DOMAIN_H_ | |
| OLD | NEW |