| // Copyright 2015 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef COMPONENTS_SCHEDULER_BASE_TIME_DOMAIN_H_ |
| #define COMPONENTS_SCHEDULER_BASE_TIME_DOMAIN_H_ |
| |
| #include <map> |
| |
| #include "base/macros.h" |
| #include "base/memory/ref_counted.h" |
| #include "base/memory/weak_ptr.h" |
| #include "base/time/time.h" |
| #include "components/scheduler/base/lazy_now.h" |
| #include "components/scheduler/base/task_queue_impl.h" |
| #include "components/scheduler/scheduler_export.h" |
| |
| namespace scheduler { |
| namespace internal { |
| class TaskQueueImpl; |
| } // internal |
| class TaskQueueManager; |
| |
| class SCHEDULER_EXPORT TimeDomain : public base::RefCounted<TimeDomain> { |
| public: |
| TimeDomain(); |
| |
| // Returns a LazyNow that evaluate this TimeDomain's Now. Can be called from |
| // any thread. |
| // TODO(alexclarke): Make this main thread only. |
| virtual LazyNow CreateLazyNow() = 0; |
| |
| // Some TimeDomains support virtual time, this method tells us to advance time |
| // if possible and return true if time was advanced. |
| virtual bool MaybeAdvanceTime() = 0; |
| |
| // Returns the name of this time domain for tracing. |
| virtual const char* GetName() const = 0; |
| |
| // If there is a scheduled delayed task, |out_time| is set to the scheduled |
| // runtime for the next one and it returns true. Returns false otherwise. |
| bool NextScheduledRunTime(base::TimeTicks* out_time) const; |
| |
| protected: |
| friend class internal::TaskQueueImpl; |
| friend class TaskQueueManager; |
| friend class base::RefCounted<TimeDomain>; |
| |
| virtual ~TimeDomain(); |
| |
| void AsValueInto(base::trace_event::TracedValue* state) const; |
| |
| // Migrates |queue| from this time domain to |destination_time_domain|. |
| void MigrateQueue(internal::TaskQueueImpl* queue, |
| TimeDomain* destination_time_domain); |
| |
| // If there is a scheduled delayed task, |out_task_queue| is set to the queue |
| // the next task was posted to and it returns true. Returns false otherwise. |
| bool NextScheduledTaskQueue(TaskQueue** out_task_queue) const; |
| |
| // Adds |queue| to the set of task queues that UpdateWorkQueues calls |
| // UpdateWorkQueue on. |
| void RegisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); |
| |
| // Schedules a call to TaskQueueImpl::MoveReadyDelayedTasksToIncomingQueue |
| // when this TimeDomain reaches |delayed_run_time|. |
| void ScheduleDelayedWork(internal::TaskQueueImpl* queue, |
| base::TimeTicks delayed_run_time, |
| LazyNow* lazy_now); |
| |
| // Removes |queue| from the set of task queues that UpdateWorkQueues calls |
| // UpdateWorkQueue on. |
| void UnregisterAsUpdatableTaskQueue(internal::TaskQueueImpl* queue); |
| |
| // Removes |queue| from all internal data structures. |
| void UnregisterQueue(internal::TaskQueueImpl* queue); |
| |
| // Updates active queues associated with this TimeDomain. |
| void UpdateWorkQueues(bool should_trigger_wakeup, |
| const internal::TaskQueueImpl::Task* previous_task); |
| |
| // The implementaion will secedule task processing to run with |delay| with |
| // respect to the TimeDomain's time source. |
| virtual void RequestWakeup(base::TimeDelta delay) = 0; |
| |
| // For implementation specific tracing. |
| virtual void AsValueIntoInternal( |
| base::trace_event::TracedValue* state) const = 0; |
| |
| // Call TaskQueueImpl::MoveReadyDelayedTasksToIncomingQueue for each |
| // queue where the delay has elapsed. |
| void WakeupReadyDelayedQueues(LazyNow* lazy_now); |
| |
| private: |
| void MoveNewlyUpdatableQueuesIntoUpdatableQueueSet(); |
| |
| typedef std::multimap<base::TimeTicks, internal::TaskQueueImpl*> |
| DelayedWakeupMultimap; |
| |
| DelayedWakeupMultimap delayed_wakeup_multimap_; |
| |
| // This lock guards only |newly_updatable_|. It's not expected to be heavily |
| // contended. |
| base::Lock newly_updatable_lock_; |
| std::vector<internal::TaskQueueImpl*> newly_updatable_; |
| |
| // Set of task queues with avaliable work on the incoming queue. This should |
| // only be accessed from the main thread. |
| std::set<internal::TaskQueueImpl*> updatable_queue_set_; |
| |
| base::ThreadChecker main_thread_checker_; |
| base::WeakPtrFactory<TimeDomain> weak_factory_; |
| |
| DISALLOW_COPY_AND_ASSIGN(TimeDomain); |
| }; |
| |
| } // namespace scheduler |
| |
| #endif // COMPONENTS_SCHEDULER_BASE_TIME_DOMAIN_H_ |