blob: cc30eacfa7599fdb5df6ad0001e7e05d5fde80b4 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2011 The Chromium Authors
[email protected]dd1f9fe2011-11-15 23:36:302// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
danakj0a448602015-03-10 00:31:165#ifndef BASE_PENDING_TASK_H_
6#define BASE_PENDING_TASK_H_
[email protected]dd1f9fe2011-11-15 23:36:307
ajwong4f13f742017-02-09 23:52:408#include <array>
[email protected]dd1f9fe2011-11-15 23:36:309
[email protected]c360bae72011-11-18 06:08:0210#include "base/base_export.h"
Avi Drissman63e1f992023-01-13 18:54:4311#include "base/functional/callback.h"
[email protected]dd1f9fe2011-11-15 23:36:3012#include "base/location.h"
Etienne Pierre-doray6fcfc7872022-04-26 15:56:0013#include "base/task/delay_policy.h"
[email protected]8f9a3a52013-06-28 15:14:1814#include "base/time/time.h"
[email protected]dd1f9fe2011-11-15 23:36:3015
16namespace base {
17
Alex Clarke1ac1f982019-03-29 08:48:3718enum class Nestable : uint8_t {
Hajime Hoshi64853ef2017-10-11 12:56:0719 kNonNestable,
20 kNestable,
21};
22
Etienne Pierre-dorayf3425c72023-11-18 06:47:1323// Copyable data part of PendingTask.
24struct 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();
[email protected]dd1f9fe2011-11-15 23:36:3035
Etienne Pierre-dorayf3425c72023-11-18 06:47:1336 TaskMetadata& operator=(TaskMetadata&& other);
37 TaskMetadata& operator=(const TaskMetadata& other);
tzikb6769d52016-07-07 20:20:0638
Etienne Pierre-doray69f30c82021-04-27 20:50:3739 // 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
Etienne Pierre-doray6fcfc7872022-04-26 15:56:0043 TimeTicks earliest_delayed_run_time() const;
44 TimeTicks latest_delayed_run_time() const;
45
[email protected]dd1f9fe2011-11-15 23:36:3046 // The site this PendingTask was posted from.
Brett Wilson8e88b312017-09-12 05:22:1647 Location posted_from;
[email protected]dd1f9fe2011-11-15 23:36:3048
Etienne Pierre-doray69f30c82021-04-27 20:50:3749 // 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
Francois Doray118223f42019-12-04 17:00:2857 // The time when the task should be run. This is null for an immediate task.
Brett Wilsonb57e3dd2017-09-08 00:47:4958 base::TimeTicks delayed_run_time;
59
Etienne Pierre-doray6fcfc7872022-04-26 15:56:0060 // |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
Alan Cutter9b0e1ab2019-03-21 04:22:1667 // 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 = {};
ajwong4f13f742017-02-09 23:52:4070
Chris Hamilton60dcc26b2019-04-24 16:59:5171 // The context of the IPC message that was being handled when this task was
Chris Hamilton888085312019-05-30 00:53:3072 // 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
Harkiran Bolaria875dd0d2020-09-15 18:10:4674 // processed. This property is not propagated from one PendingTask to the
Chris Hamilton60dcc26b2019-04-24 16:59:5175 // 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
Harkiran Bolaria875dd0d2020-09-15 18:10:4677 // B will not inherit the |ipc_hash| of pending task A.
Chris Hamilton888085312019-05-30 00:53:3078 uint32_t ipc_hash = 0;
Harkiran Bolaria875dd0d2020-09-15 18:10:4679 const char* ipc_interface_name = nullptr;
Chris Hamilton60dcc26b2019-04-24 16:59:5180
[email protected]dd1f9fe2011-11-15 23:36:3081 // Secondary sort key for run time.
Gabriel Charetteae4f9ce2018-04-05 19:00:0182 int sequence_num = 0;
[email protected]dd1f9fe2011-11-15 23:36:3083
Alex Clarke1ac1f982019-03-29 08:48:3784 bool task_backtrace_overflow = false;
[email protected]dd1f9fe2011-11-15 23:36:3085};
86
Etienne Pierre-dorayf3425c72023-11-18 06:47:1387// Contains data about a pending task. Stored in TaskQueue and DelayedTaskQueue
88// for use by classes that queue and execute tasks.
89struct 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
[email protected]dd1f9fe2011-11-15 23:36:30108} // namespace base
109
danakj0a448602015-03-10 00:31:16110#endif // BASE_PENDING_TASK_H_