1*6777b538SAndroid Build Coastguard Worker# What is this 2*6777b538SAndroid Build Coastguard WorkerThis file documents high level parts of the sequence manager. 3*6777b538SAndroid Build Coastguard Worker 4*6777b538SAndroid Build Coastguard WorkerThe sequence manager provides a set of prioritized FIFO task queues, which 5*6777b538SAndroid Build Coastguard Workerallows funneling multiple sequences of immediate and delayed tasks on a single 6*6777b538SAndroid Build Coastguard Workerunderlying sequence. 7*6777b538SAndroid Build Coastguard Worker 8*6777b538SAndroid Build Coastguard Worker## Work Queue and Task selection 9*6777b538SAndroid Build Coastguard WorkerBoth immediate tasks and delayed tasks are posted to a `TaskQueue` via an 10*6777b538SAndroid Build Coastguard Workerassociated `TaskRunner`. `TaskQueue`s use distinct primitive FIFO queues, called 11*6777b538SAndroid Build Coastguard Worker`WorkQueue`s, to manage immediate tasks and delayed tasks. Tasks eventually end 12*6777b538SAndroid Build Coastguard Workerup in their assigned `WorkQueue` which is made directly visible to 13*6777b538SAndroid Build Coastguard Worker`SequenceManager` through `TaskQueueSelector`. 14*6777b538SAndroid Build Coastguard Worker`SequenceManagerImpl::SelectNextTask()` uses 15*6777b538SAndroid Build Coastguard Worker`TaskQueueSelector::SelectWorkQueueToService()` to select the next work queue 16*6777b538SAndroid Build Coastguard Workerbased on various policy e.g. priority, from which 1 task is popped at a time. 17*6777b538SAndroid Build Coastguard Worker 18*6777b538SAndroid Build Coastguard Worker## Journey of a Task 19*6777b538SAndroid Build Coastguard WorkerTask queues have a mechanism to allow efficient cross-thread posting with the 20*6777b538SAndroid Build Coastguard Workeruse of 2 work queues, `immediate_incoming_queue` which is used when posting, and 21*6777b538SAndroid Build Coastguard Worker`immediate_work_queue` used to pop tasks from. An immediate task posted from the 22*6777b538SAndroid Build Coastguard Workermain thread is pushed on `immediate_incoming_queue` in 23*6777b538SAndroid Build Coastguard Worker`TaskQueueImpl::PostImmediateTaskImpl()`. If the work queue was empty, 24*6777b538SAndroid Build Coastguard Worker`SequenceManager` is notified and the `TaskQueue` is registered to do 25*6777b538SAndroid Build Coastguard Worker`ReloadEmptyImmediateWorkQueue()` before SequenceManager selects a task, which 26*6777b538SAndroid Build Coastguard Workermoves tasks from `immediate_incoming_queue` to `immediate_work_queue` in batch 27*6777b538SAndroid Build Coastguard Workerfor all registered `TaskQueue`s. The tasks then follow the regular work queue 28*6777b538SAndroid Build Coastguard Workerselection mechanism. 29*6777b538SAndroid Build Coastguard Worker 30*6777b538SAndroid Build Coastguard Worker## Journey of a WakeUp 31*6777b538SAndroid Build Coastguard WorkerA `WakeUp` represents a time at which a delayed task wants to run. 32*6777b538SAndroid Build Coastguard Worker 33*6777b538SAndroid Build Coastguard WorkerEach `TaskQueueImpl` maintains its own next wake-up as 34*6777b538SAndroid Build Coastguard Worker`main_thread_only().scheduled_wake_up`, associated with the earliest pending 35*6777b538SAndroid Build Coastguard Workerdelayed task. It communicates its wake up to the WakeUpQueue via 36*6777b538SAndroid Build Coastguard Worker`WakeUpQueue::SetNextWakeUpForQueue()`. The `WakeUpQueue` is responsible for 37*6777b538SAndroid Build Coastguard Workerdetermining the single next wake up time for the thread. This is accessed from 38*6777b538SAndroid Build Coastguard Worker`SequenceManagerImpl` and may determine the next run time if there's no 39*6777b538SAndroid Build Coastguard Workerimmediate work, which ultimately gets passed to the MessagePump, typically via 40*6777b538SAndroid Build Coastguard Worker`MessagePump::Delegate::NextWorkInfo` (returned by 41*6777b538SAndroid Build Coastguard Worker`ThreadControllerWithMessagePumpImpl::DoWork()`) or by 42*6777b538SAndroid Build Coastguard Worker`MessagePump::ScheduleDelayedWork()` (on rare occasions where the next WakeUp is 43*6777b538SAndroid Build Coastguard Workerscheduled on the main thread from outside a `DoWork()`). When a delayed run time 44*6777b538SAndroid Build Coastguard Workerassociated with a wake-up is reached, `WakeUpQueue` is notified through 45*6777b538SAndroid Build Coastguard Worker`WakeUpQueue::MoveReadyDelayedTasksToWorkQueues()` and in turn notifies all 46*6777b538SAndroid Build Coastguard Worker`TaskQueue`s whose wake-up can be resolved. This lets each `TaskQueue`s process 47*6777b538SAndroid Build Coastguard Workerripe delayed tasks. 48*6777b538SAndroid Build Coastguard Worker 49*6777b538SAndroid Build Coastguard Worker## Journey of a delayed Task 50*6777b538SAndroid Build Coastguard WorkerA delayed Task posted cross-thread generates an immediate Task to run 51*6777b538SAndroid Build Coastguard Worker`TaskQueueImpl::ScheduleDelayedWorkTask()` which eventually calls 52*6777b538SAndroid Build Coastguard Worker`TaskQueueImpl::PushOntoDelayedIncomingQueueFromMainThread()`, so that it can be 53*6777b538SAndroid Build Coastguard Workerenqueued on the main thread. A delayed Task posted from the main thread skips 54*6777b538SAndroid Build Coastguard Workerthis step and calls 55*6777b538SAndroid Build Coastguard Worker`TaskQueueImpl::PushOntoDelayedIncomingQueueFromMainThread()` directly. The Task 56*6777b538SAndroid Build Coastguard Workeris then pushed on `main_thread_only().delayed_incoming_queue` and possibly 57*6777b538SAndroid Build Coastguard Workerupdates the next task queue wake-up. Once the delayed run time is reached, 58*6777b538SAndroid Build Coastguard Workerpossibly because the wake-up is resolved, the delayed task is moved to 59*6777b538SAndroid Build Coastguard Worker`main_thread_only().delayed_work_queue` and follows the regular work queue 60*6777b538SAndroid Build Coastguard Workerselection mechanism. 61*6777b538SAndroid Build Coastguard Worker 62*6777b538SAndroid Build Coastguard Worker## TimeDomain and TickClock 63*6777b538SAndroid Build Coastguard Worker`SequenceManager` and related classes use a common `TickClock` that can be 64*6777b538SAndroid Build Coastguard Workerinjected by specifying a `TimeDomain`. A `TimeDomain` is a specialisation of 65*6777b538SAndroid Build Coastguard Worker`TickClock` that gets notified when the `MessagePump` is about to go idle via 66*6777b538SAndroid Build Coastguard WorkerTimeDomain::MaybeFastForwardToWakeUp(), and can use the signal to fast forward 67*6777b538SAndroid Build Coastguard Workerin time. This is used in `TaskEnvironment` to support `MOCK_TIME`, and in 68*6777b538SAndroid Build Coastguard Workerdevtools to support virtual time. 69