| 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 COMPONENTS_SCHEDULER_BASE_TIME_DOMAIN_H_ | 5 #ifndef COMPONENTS_SCHEDULER_BASE_TIME_DOMAIN_H_ |
| 6 #define COMPONENTS_SCHEDULER_BASE_TIME_DOMAIN_H_ | 6 #define COMPONENTS_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" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "components/scheduler/base/lazy_now.h" | 15 #include "components/scheduler/base/lazy_now.h" |
| 16 #include "components/scheduler/base/task_queue_impl.h" | 16 #include "components/scheduler/base/task_queue_impl.h" |
| 17 #include "components/scheduler/scheduler_export.h" | 17 #include "components/scheduler/scheduler_export.h" |
| 18 | 18 |
| 19 namespace scheduler { | 19 namespace scheduler { |
| 20 namespace internal { | 20 namespace internal { |
| 21 class TaskQueueImpl; | 21 class TaskQueueImpl; |
| 22 } // internal | 22 } // internal |
| 23 class TaskQueueManager; | 23 class TaskQueueManager; |
| 24 class TaskQueueManagerDelegate; | 24 class TaskQueueManagerDelegate; |
| 25 | 25 |
| 26 class SCHEDULER_EXPORT TimeDomain { | 26 class SCHEDULER_EXPORT TimeDomain : public base::RefCounted<TimeDomain> { |
| 27 public: | 27 public: |
| 28 class SCHEDULER_EXPORT Observer { | 28 TimeDomain(); |
| 29 public: | |
| 30 virtual ~Observer() {} | |
| 31 | |
| 32 // Called when an empty TaskQueue registered with this TimeDomain has a task | |
| 33 // enqueued. | |
| 34 virtual void OnTimeDomainHasImmediateWork() = 0; | |
| 35 | |
| 36 // Called when a TaskQueue registered with this TimeDomain has a delayed | |
| 37 // task enqueued and no other delayed tasks associated with this TimeDomain | |
| 38 // are pending. | |
| 39 virtual void OnTimeDomainHasDelayedWork() = 0; | |
| 40 }; | |
| 41 | |
| 42 explicit TimeDomain(Observer* observer); | |
| 43 virtual ~TimeDomain(); | |
| 44 | 29 |
| 45 // Returns a LazyNow that evaluate this TimeDomain's Now. Can be called from | 30 // Returns a LazyNow that evaluate this TimeDomain's Now. Can be called from |
| 46 // any thread. | 31 // any thread. |
| 47 // TODO(alexclarke): Make this main thread only. | 32 // TODO(alexclarke): Make this main thread only. |
| 48 virtual LazyNow CreateLazyNow() = 0; | 33 virtual LazyNow CreateLazyNow() = 0; |
| 49 | 34 |
| 50 // Some TimeDomains support virtual time, this method tells us to advance time | 35 // Some TimeDomains support virtual time, this method tells us to advance time |
| 51 // if possible and return true if time was advanced. | 36 // if possible and return true if time was advanced. |
| 52 virtual bool MaybeAdvanceTime() = 0; | 37 virtual bool MaybeAdvanceTime() = 0; |
| 53 | 38 |
| 54 // Returns the name of this time domain for tracing. | 39 // Returns the name of this time domain for tracing. |
| 55 virtual const char* GetName() const = 0; | 40 virtual const char* GetName() const = 0; |
| 56 | 41 |
| 57 // If there is a scheduled delayed task, |out_time| is set to the scheduled | 42 // If there is a scheduled delayed task, |out_time| is set to the scheduled |
| 58 // runtime for the next one and it returns true. Returns false otherwise. | 43 // runtime for the next one and it returns true. Returns false otherwise. |
| 59 bool NextScheduledRunTime(base::TimeTicks* out_time) const; | 44 bool NextScheduledRunTime(base::TimeTicks* out_time) const; |
| 60 | 45 |
| 61 protected: | 46 protected: |
| 62 friend class internal::TaskQueueImpl; | 47 friend class internal::TaskQueueImpl; |
| 63 friend class TaskQueueManager; | 48 friend class TaskQueueManager; |
| 49 friend class base::RefCounted<TimeDomain>; |
| 50 |
| 51 virtual ~TimeDomain(); |
| 64 | 52 |
| 65 void AsValueInto(base::trace_event::TracedValue* state) const; | 53 void AsValueInto(base::trace_event::TracedValue* state) const; |
| 66 | 54 |
| 67 // Migrates |queue| from this time domain to |destination_time_domain|. | 55 // Migrates |queue| from this time domain to |destination_time_domain|. |
| 68 void MigrateQueue(internal::TaskQueueImpl* queue, | 56 void MigrateQueue(internal::TaskQueueImpl* queue, |
| 69 TimeDomain* destination_time_domain); | 57 TimeDomain* destination_time_domain); |
| 70 | 58 |
| 71 // If there is a scheduled delayed task, |out_task_queue| is set to the queue | 59 // If there is a scheduled delayed task, |out_task_queue| is set to the queue |
| 72 // the next task was posted to and it returns true. Returns false otherwise. | 60 // the next task was posted to and it returns true. Returns false otherwise. |
| 73 bool NextScheduledTaskQueue(TaskQueue** out_task_queue) const; | 61 bool NextScheduledTaskQueue(TaskQueue** out_task_queue) const; |
| 74 | 62 |
| 75 // Adds |queue| to the set of task queues that UpdateWorkQueues calls | 63 // Adds |queue| to the set of task queues that UpdateWorkQueues calls |
| 76 // UpdateWorkQueue on. | 64 // UpdateWorkQueue on. |
| 77 void RegisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); | 65 void RegisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); |
| 78 | 66 |
| 79 // Schedules a call to TaskQueueImpl::MoveReadyDelayedTasksToIncomingQueue | 67 // Schedules a call to TaskQueueImpl::MoveReadyDelayedTasksToIncomingQueue |
| 80 // when this TimeDomain reaches |delayed_run_time|. | 68 // when this TimeDomain reaches |delayed_run_time|. |
| 81 void ScheduleDelayedWork(internal::TaskQueueImpl* queue, | 69 void ScheduleDelayedWork(internal::TaskQueueImpl* queue, |
| 82 base::TimeTicks delayed_run_time, | 70 base::TimeTicks delayed_run_time, |
| 83 LazyNow* lazy_now); | 71 LazyNow* lazy_now); |
| 84 | 72 |
| 85 // Registers the |queue|. | |
| 86 void RegisterQueue(internal::TaskQueueImpl* queue); | |
| 87 | |
| 88 // Removes |queue| from the set of task queues that UpdateWorkQueues calls | 73 // Removes |queue| from the set of task queues that UpdateWorkQueues calls |
| 89 // UpdateWorkQueue on. | 74 // UpdateWorkQueue on. |
| 90 void UnregisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); | 75 void UnregisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); |
| 91 | 76 |
| 92 // Removes |queue| from all internal data structures. | 77 // Removes |queue| from all internal data structures. |
| 93 void UnregisterQueue(internal::TaskQueueImpl* queue); | 78 void UnregisterQueue(internal::TaskQueueImpl* queue); |
| 94 | 79 |
| 95 // Updates active queues associated with this TimeDomain. | 80 // Updates active queues associated with this TimeDomain. |
| 96 void UpdateWorkQueues(bool should_trigger_wakeup, | 81 void UpdateWorkQueues(bool should_trigger_wakeup, |
| 97 const internal::TaskQueueImpl::Task* previous_task); | 82 const internal::TaskQueueImpl::Task* previous_task); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 123 | 108 |
| 124 // This lock guards only |newly_updatable_|. It's not expected to be heavily | 109 // This lock guards only |newly_updatable_|. It's not expected to be heavily |
| 125 // contended. | 110 // contended. |
| 126 base::Lock newly_updatable_lock_; | 111 base::Lock newly_updatable_lock_; |
| 127 std::vector<internal::TaskQueueImpl*> newly_updatable_; | 112 std::vector<internal::TaskQueueImpl*> newly_updatable_; |
| 128 | 113 |
| 129 // Set of task queues with avaliable work on the incoming queue. This should | 114 // Set of task queues with avaliable work on the incoming queue. This should |
| 130 // only be accessed from the main thread. | 115 // only be accessed from the main thread. |
| 131 std::set<internal::TaskQueueImpl*> updatable_queue_set_; | 116 std::set<internal::TaskQueueImpl*> updatable_queue_set_; |
| 132 | 117 |
| 133 std::set<internal::TaskQueueImpl*> registered_task_queues_; | |
| 134 | |
| 135 Observer* observer_; | |
| 136 | |
| 137 base::ThreadChecker main_thread_checker_; | 118 base::ThreadChecker main_thread_checker_; |
| 138 | 119 |
| 139 DISALLOW_COPY_AND_ASSIGN(TimeDomain); | 120 DISALLOW_COPY_AND_ASSIGN(TimeDomain); |
| 140 }; | 121 }; |
| 141 | 122 |
| 142 } // namespace scheduler | 123 } // namespace scheduler |
| 143 | 124 |
| 144 #endif // COMPONENTS_SCHEDULER_BASE_TIME_DOMAIN_H_ | 125 #endif // COMPONENTS_SCHEDULER_BASE_TIME_DOMAIN_H_ |
| OLD | NEW |