• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/base_export.h"
9 #include "base/functional/callback_helpers.h"
10 #include "base/memory/raw_ptr_exclusion.h"
11 #include "base/pending_task.h"
12 #include "base/task/common/lazy_now.h"
13 #include "base/task/sequence_manager/task_queue.h"
14 #include "base/task/sequence_manager/tasks.h"
15 #include "third_party/abseil-cpp/absl/types/optional.h"
16 
17 namespace perfetto {
18 class EventContext;
19 }
20 
21 namespace base {
22 namespace sequence_manager {
23 namespace internal {
24 
25 // Interface to pass tasks to ThreadController.
26 class SequencedTaskSource {
27  public:
28   enum class SelectTaskOption { kDefault, kSkipDelayedTask };
29 
30   using TaskExecutionTraceLogger =
31       RepeatingCallback<void(perfetto::EventContext&, const Task&)>;
32 
33   struct BASE_EXPORT SelectedTask {
34     SelectedTask(const SelectedTask&);
35     SelectedTask(Task& task,
36                  TaskExecutionTraceLogger task_execution_trace_logger,
37                  TaskQueue::QueuePriority priority,
38                  QueueName task_queue_name);
39     ~SelectedTask();
40 
41     // `task` is not a raw_ref<> for performance reasons: based on this sampling
42     // profiler result on Mac. go/brp-mac-prof-diff-20230403
43     RAW_PTR_EXCLUSION Task& task;
44     // Callback to fill trace event arguments associated with the task
45     // execution. Can be null
46     TaskExecutionTraceLogger task_execution_trace_logger =
47         TaskExecutionTraceLogger();
48     TaskQueue::QueuePriority priority;
49     QueueName task_queue_name;
50   };
51 
52   virtual ~SequencedTaskSource() = default;
53 
54   // Returns the next task to run from this source or nullopt if
55   // there're no more tasks ready to run. If a task is returned,
56   // DidRunTask() must be invoked before the next call to SelectNextTask().
57   // |option| allows control on which kind of tasks can be selected.
58   virtual absl::optional<SelectedTask> SelectNextTask(
59       LazyNow& lazy_now,
60       SelectTaskOption option = SelectTaskOption::kDefault) = 0;
61 
62   // Notifies this source that the task previously obtained
63   // from SelectNextTask() has been completed.
64   virtual void DidRunTask(LazyNow& lazy_now) = 0;
65 
66   // Removes all canceled delayed tasks from the front of the queue. After
67   // calling this, GetPendingWakeUp() is guaranteed to return a ready time for a
68   // non-canceled task.
69   virtual void RemoveAllCanceledDelayedTasksFromFront(LazyNow* lazy_now) = 0;
70 
71   // Returns a WakeUp for the next pending task, is_immediate() if the
72   // next task can run immediately, or nullopt if there are no more immediate or
73   // delayed tasks. |option| allows control on which kind of tasks can be
74   // selected.
75   virtual absl::optional<WakeUp> GetPendingWakeUp(
76       LazyNow* lazy_now,
77       SelectTaskOption option = SelectTaskOption::kDefault) const = 0;
78 
79   // Return true if there are any pending tasks in the task source which require
80   // high resolution timing.
81   virtual bool HasPendingHighResolutionTasks() = 0;
82 
83   // Called when we have run out of immediate work.  If more immediate work
84   // becomes available as a result of any processing done by this callback,
85   // return true to schedule a future DoWork.
86   virtual bool OnSystemIdle() = 0;
87 
88   // Called prior to running `selected_task` to emit trace event data for it.
89   virtual void MaybeEmitTaskDetails(
90       perfetto::EventContext& ctx,
91       const SelectedTask& selected_task) const = 0;
92 };
93 
94 }  // namespace internal
95 }  // namespace sequence_manager
96 }  // namespace base
97 
98 #endif  // BASE_TASK_SEQUENCE_MANAGER_SEQUENCED_TASK_SOURCE_H_
99