1 // Copyright 2018 The Chromium Authors 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 BASE_TASK_SEQUENCE_MANAGER_SEQUENCED_TASK_SOURCE_H_ 6 #define BASE_TASK_SEQUENCE_MANAGER_SEQUENCED_TASK_SOURCE_H_ 7 8 #include <optional> 9 10 #include "base/base_export.h" 11 #include "base/functional/callback_helpers.h" 12 #include "base/memory/raw_ptr_exclusion.h" 13 #include "base/pending_task.h" 14 #include "base/task/common/lazy_now.h" 15 #include "base/task/sequence_manager/task_queue.h" 16 #include "base/task/sequence_manager/tasks.h" 17 18 namespace perfetto { 19 class EventContext; 20 } 21 22 namespace base { 23 namespace sequence_manager { 24 namespace internal { 25 26 // Interface to pass tasks to ThreadController. 27 class SequencedTaskSource { 28 public: 29 enum class SelectTaskOption { kDefault, kSkipDelayedTask }; 30 31 using TaskExecutionTraceLogger = 32 RepeatingCallback<void(perfetto::EventContext&, const Task&)>; 33 34 struct BASE_EXPORT SelectedTask { 35 SelectedTask(const SelectedTask&); 36 SelectedTask(Task& task, 37 TaskExecutionTraceLogger task_execution_trace_logger, 38 TaskQueue::QueuePriority priority, 39 QueueName task_queue_name); 40 ~SelectedTask(); 41 42 // RAW_PTR_EXCLUSION: Performance reasons: based on this sampling profiler 43 // result on Mac. go/brp-mac-prof-diff-20230403 44 RAW_PTR_EXCLUSION Task& task; 45 // Callback to fill trace event arguments associated with the task 46 // execution. Can be null 47 TaskExecutionTraceLogger task_execution_trace_logger = 48 TaskExecutionTraceLogger(); 49 TaskQueue::QueuePriority priority; 50 QueueName task_queue_name; 51 }; 52 53 virtual ~SequencedTaskSource() = default; 54 55 // Controls whether a `SequencedTaskRunner` associated with this source can 56 // run a task synchronously in `RunOrPostTask`. Enable this to indicate that 57 // there isn't any pending or running work that has mutual exclusion or 58 // ordering expectations with tasks from this source, outside of 59 // `SelectNextTask()` or `OnBeginWork()` -> `OnIdle()` (those prevent tasks 60 // from running synchronously, irrespective of the state set here). 61 virtual void SetRunTaskSynchronouslyAllowed( 62 bool can_run_tasks_synchronously) = 0; 63 64 // Returns the next task to run from this source or nullopt if 65 // there're no more tasks ready to run. If a task is returned, 66 // DidRunTask() must be invoked before the next call to SelectNextTask(). 67 // |option| allows control on which kind of tasks can be selected. 68 virtual std::optional<SelectedTask> SelectNextTask( 69 LazyNow& lazy_now, 70 SelectTaskOption option = SelectTaskOption::kDefault) = 0; 71 72 // Notifies this source that the task previously obtained 73 // from SelectNextTask() has been completed. 74 virtual void DidRunTask(LazyNow& lazy_now) = 0; 75 76 // Returns a WakeUp for the next pending task, is_immediate() if the 77 // next task can run immediately, or nullopt if there are no more immediate or 78 // delayed tasks. |option| allows control on which kind of tasks can be 79 // selected. May delete canceled tasks. 80 virtual std::optional<WakeUp> GetPendingWakeUp( 81 LazyNow* lazy_now, 82 SelectTaskOption option = SelectTaskOption::kDefault) = 0; 83 84 // Return true if there are any pending tasks in the task source which require 85 // high resolution timing. 86 virtual bool HasPendingHighResolutionTasks() = 0; 87 88 // Indicates that work that has mutual exclusion expectations with tasks from 89 // this `SequencedTaskSource` will start running. 90 virtual void OnBeginWork() = 0; 91 92 // Called when we have run out of immediate work. If more immediate work 93 // becomes available as a result of any processing done by this callback, 94 // return true to schedule a future DoWork. 95 virtual bool OnIdle() = 0; 96 97 // Called prior to running `selected_task` to emit trace event data for it. 98 virtual void MaybeEmitTaskDetails( 99 perfetto::EventContext& ctx, 100 const SelectedTask& selected_task) const = 0; 101 }; 102 103 } // namespace internal 104 } // namespace sequence_manager 105 } // namespace base 106 107 #endif // BASE_TASK_SEQUENCE_MANAGER_SEQUENCED_TASK_SOURCE_H_ 108