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