| 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/macros.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "base/time/time.h" | |
| 14 #include "components/scheduler/base/lazy_now.h" | |
| 15 #include "components/scheduler/base/task_queue_impl.h" | |
| 16 #include "components/scheduler/scheduler_export.h" | |
| 17 | |
| 18 namespace scheduler { | |
| 19 namespace internal { | |
| 20 class TaskQueueImpl; | |
| 21 } // internal | |
| 22 class TaskQueueManager; | |
| 23 | |
| 24 class SCHEDULER_EXPORT TimeDomain : public base::RefCounted<TimeDomain> { | |
| 25 public: | |
| 26 TimeDomain(); | |
| 27 | |
| 28 // Returns a LazyNow that evaluate this TimeDomain's Now. Can be called from | |
| 29 // any thread. | |
| 30 // TODO(alexclarke): Make this main thread only. | |
| 31 virtual LazyNow CreateLazyNow() = 0; | |
| 32 | |
| 33 // Some TimeDomains support virtual time, this method tells us to advance time | |
| 34 // if possible and return true if time was advanced. | |
| 35 virtual bool MaybeAdvanceTime() = 0; | |
| 36 | |
| 37 // Returns the name of this time domain for tracing. | |
| 38 virtual const char* GetName() const = 0; | |
| 39 | |
| 40 // If there is a scheduled delayed task, |out_time| is set to the scheduled | |
| 41 // runtime for the next one and it returns true. Returns false otherwise. | |
| 42 bool NextScheduledRunTime(base::TimeTicks* out_time) const; | |
| 43 | |
| 44 protected: | |
| 45 friend class internal::TaskQueueImpl; | |
| 46 friend class TaskQueueManager; | |
| 47 friend class base::RefCounted<TimeDomain>; | |
| 48 | |
| 49 virtual ~TimeDomain(); | |
| 50 | |
| 51 void AsValueInto(base::trace_event::TracedValue* state) const; | |
| 52 | |
| 53 // Migrates |queue| from this time domain to |destination_time_domain|. | |
| 54 void MigrateQueue(internal::TaskQueueImpl* queue, | |
| 55 TimeDomain* destination_time_domain); | |
| 56 | |
| 57 // If there is a scheduled delayed task, |out_task_queue| is set to the queue | |
| 58 // the next task was posted to and it returns true. Returns false otherwise. | |
| 59 bool NextScheduledTaskQueue(TaskQueue** out_task_queue) const; | |
| 60 | |
| 61 // Adds |queue| to the set of task queues that UpdateWorkQueues calls | |
| 62 // UpdateWorkQueue on. | |
| 63 void RegisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); | |
| 64 | |
| 65 // Schedules a call to TaskQueueImpl::MoveReadyDelayedTasksToIncomingQueue | |
| 66 // when this TimeDomain reaches |delayed_run_time|. | |
| 67 void ScheduleDelayedWork(internal::TaskQueueImpl* queue, | |
| 68 base::TimeTicks delayed_run_time, | |
| 69 LazyNow* lazy_now); | |
| 70 | |
| 71 // Removes |queue| from the set of task queues that UpdateWorkQueues calls | |
| 72 // UpdateWorkQueue on. | |
| 73 void UnregisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); | |
| 74 | |
| 75 // Removes |queue| from all internal data structures. | |
| 76 void UnregisterQueue(internal::TaskQueueImpl* queue); | |
| 77 | |
| 78 // Updates active queues associated with this TimeDomain. | |
| 79 void UpdateWorkQueues(bool should_trigger_wakeup, | |
| 80 const internal::TaskQueueImpl::Task* previous_task); | |
| 81 | |
| 82 // The implementaion will secedule task processing to run with |delay| with | |
| 83 // respect to the TimeDomain's time source. | |
| 84 virtual void RequestWakeup(base::TimeDelta delay) = 0; | |
| 85 | |
| 86 // For implementation specific tracing. | |
| 87 virtual void AsValueIntoInternal( | |
| 88 base::trace_event::TracedValue* state) const = 0; | |
| 89 | |
| 90 // Call TaskQueueImpl::MoveReadyDelayedTasksToIncomingQueue for each | |
| 91 // queue where the delay has elapsed. | |
| 92 void WakeupReadyDelayedQueues(LazyNow* lazy_now); | |
| 93 | |
| 94 private: | |
| 95 void MoveNewlyUpdatableQueuesIntoUpdatableQueueSet(); | |
| 96 | |
| 97 typedef std::multimap<base::TimeTicks, internal::TaskQueueImpl*> | |
| 98 DelayedWakeupMultimap; | |
| 99 | |
| 100 DelayedWakeupMultimap delayed_wakeup_multimap_; | |
| 101 | |
| 102 // This lock guards only |newly_updatable_|. It's not expected to be heavily | |
| 103 // contended. | |
| 104 base::Lock newly_updatable_lock_; | |
| 105 std::vector<internal::TaskQueueImpl*> newly_updatable_; | |
| 106 | |
| 107 // Set of task queues with avaliable work on the incoming queue. This should | |
| 108 // only be accessed from the main thread. | |
| 109 std::set<internal::TaskQueueImpl*> updatable_queue_set_; | |
| 110 | |
| 111 base::ThreadChecker main_thread_checker_; | |
| 112 base::WeakPtrFactory<TimeDomain> weak_factory_; | |
| 113 | |
| 114 DISALLOW_COPY_AND_ASSIGN(TimeDomain); | |
| 115 }; | |
| 116 | |
| 117 } // namespace scheduler | |
| 118 | |
| 119 #endif // COMPONENTS_SCHEDULER_BASE_TIME_DOMAIN_H_ | |
| OLD | NEW |