xref: /aosp_15_r20/external/cronet/base/pending_task.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 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_PENDING_TASK_H_
6 #define BASE_PENDING_TASK_H_
7 
8 #include <array>
9 
10 #include "base/base_export.h"
11 #include "base/functional/callback.h"
12 #include "base/location.h"
13 #include "base/task/delay_policy.h"
14 #include "base/time/time.h"
15 
16 namespace base {
17 
18 enum class Nestable : uint8_t {
19   kNonNestable,
20   kNestable,
21 };
22 
23 // Copyable data part of PendingTask.
24 struct BASE_EXPORT TaskMetadata {
25   TaskMetadata();
26   explicit TaskMetadata(const Location& posted_from,
27                         TimeTicks queue_time = TimeTicks(),
28                         TimeTicks delayed_run_time = TimeTicks(),
29                         TimeDelta leeway = TimeDelta(),
30                         subtle::DelayPolicy delay_policy =
31                             subtle::DelayPolicy::kFlexibleNoSooner);
32   TaskMetadata(TaskMetadata&& other);
33   TaskMetadata(const TaskMetadata& other);
34   ~TaskMetadata();
35 
36   TaskMetadata& operator=(TaskMetadata&& other);
37   TaskMetadata& operator=(const TaskMetadata& other);
38 
39   // Returns the time at which this task should run. This is |delayed_run_time|
40   // for a delayed task, |queue_time| otherwise.
41   base::TimeTicks GetDesiredExecutionTime() const;
42 
43   TimeTicks earliest_delayed_run_time() const;
44   TimeTicks latest_delayed_run_time() const;
45 
46   // The site this PendingTask was posted from.
47   Location posted_from;
48 
49   // The time at which the task was queued, which happens at post time. For
50   // deferred non-nestable tasks, this is reset when the nested loop exits and
51   // the deferred tasks are pushed back at the front of the queue. This is not
52   // set for immediate SequenceManager tasks unless SetAddQueueTimeToTasks(true)
53   // was called. This defaults to a null TimeTicks if the task hasn't been
54   // inserted in a sequence yet.
55   TimeTicks queue_time;
56 
57   // The time when the task should be run. This is null for an immediate task.
58   base::TimeTicks delayed_run_time;
59 
60   // |leeway| and |delay_policy| determine the preferred time range for running
61   // the delayed task. A larger leeway provides more freedom to run the task at
62   // an optimal time for power consumption. These fields are ignored for an
63   // immediate (non-delayed) task.
64   TimeDelta leeway;
65   subtle::DelayPolicy delay_policy = subtle::DelayPolicy::kFlexibleNoSooner;
66 
67   // Chain of symbols of the parent tasks which led to this one being posted.
68   static constexpr size_t kTaskBacktraceLength = 4;
69   std::array<const void*, kTaskBacktraceLength> task_backtrace = {};
70 
71   // The context of the IPC message that was being handled when this task was
72   // posted. This is a hash of the IPC message name that is set within the scope
73   // of an IPC handler and when symbolized uniquely identifies the message being
74   // processed. This property is not propagated from one PendingTask to the
75   // next. For example, if pending task A was posted while handling an IPC,
76   // and pending task B was posted from within pending task A, then pending task
77   // B will not inherit the |ipc_hash| of pending task A.
78   uint32_t ipc_hash = 0;
79   const char* ipc_interface_name = nullptr;
80 
81   // Secondary sort key for run time.
82   int sequence_num = 0;
83 
84   bool task_backtrace_overflow = false;
85 };
86 
87 // Contains data about a pending task. Stored in TaskQueue and DelayedTaskQueue
88 // for use by classes that queue and execute tasks.
89 struct BASE_EXPORT PendingTask : public TaskMetadata {
90   PendingTask();
91   PendingTask(const Location& posted_from,
92               OnceClosure task,
93               TimeTicks queue_time = TimeTicks(),
94               TimeTicks delayed_run_time = TimeTicks(),
95               TimeDelta leeway = TimeDelta(),
96               subtle::DelayPolicy delay_policy =
97                   subtle::DelayPolicy::kFlexibleNoSooner);
98   PendingTask(const TaskMetadata& metadata, OnceClosure task);
99   PendingTask(PendingTask&& other);
100   ~PendingTask();
101 
102   PendingTask& operator=(PendingTask&& other);
103 
104   // The task to run.
105   OnceClosure task;
106 };
107 
108 }  // namespace base
109 
110 #endif  // BASE_PENDING_TASK_H_
111