blob: fa534ab40ba29364a52737ca45ed9908b82a2fb7 [file] [log] [blame]
[email protected]dd1f9fe2011-11-15 23:36:301// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// 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"
[email protected]dd1f9fe2011-11-15 23:36:3011#include "base/callback.h"
Brett Wilson3d88e7762017-08-14 19:54:3812#include "base/containers/queue.h"
[email protected]dd1f9fe2011-11-15 23:36:3013#include "base/location.h"
Erik Chena95335192018-07-30 17:48:2214#include "base/optional.h"
[email protected]8f9a3a52013-06-28 15:14:1815#include "base/time/time.h"
[email protected]dd1f9fe2011-11-15 23:36:3016
17namespace base {
18
Alex Clarke1ac1f982019-03-29 08:48:3719enum class Nestable : uint8_t {
Hajime Hoshi64853ef2017-10-11 12:56:0720 kNonNestable,
21 kNestable,
22};
23
[email protected]dd1f9fe2011-11-15 23:36:3024// Contains data about a pending task. Stored in TaskQueue and DelayedTaskQueue
25// for use by classes that queue and execute tasks.
Brett Wilsonb57e3dd2017-09-08 00:47:4926struct BASE_EXPORT PendingTask {
Jesse McKenna9cf11372018-11-01 19:38:3827 PendingTask();
Brett Wilson8e88b312017-09-12 05:22:1628 PendingTask(const Location& posted_from,
tzik739ffe32016-10-14 14:34:5829 OnceClosure task,
Hajime Hoshi64853ef2017-10-11 12:56:0730 TimeTicks delayed_run_time = TimeTicks(),
31 Nestable nestable = Nestable::kNestable);
tzikb6769d52016-07-07 20:20:0632 PendingTask(PendingTask&& other);
[email protected]dd1f9fe2011-11-15 23:36:3033 ~PendingTask();
34
tzikb6769d52016-07-07 20:20:0635 PendingTask& operator=(PendingTask&& other);
36
[email protected]dd1f9fe2011-11-15 23:36:3037 // Used to support sorting.
38 bool operator<(const PendingTask& other) const;
39
40 // The task to run.
tzik739ffe32016-10-14 14:34:5841 OnceClosure task;
[email protected]dd1f9fe2011-11-15 23:36:3042
43 // The site this PendingTask was posted from.
Brett Wilson8e88b312017-09-12 05:22:1644 Location posted_from;
[email protected]dd1f9fe2011-11-15 23:36:3045
Francois Doray118223f42019-12-04 17:00:2846 // The time when the task should be run. This is null for an immediate task.
Brett Wilsonb57e3dd2017-09-08 00:47:4947 base::TimeTicks delayed_run_time;
48
Etienne Pierre-doray2d079032019-02-07 15:18:2349 // The time at which the task was queued. For SequenceManager tasks and
Gabriel Charette52fa3ae2019-04-15 21:44:3750 // ThreadPool non-delayed tasks, this happens at post time. For
51 // ThreadPool delayed tasks, this happens some time after the task's delay
Etienne Pierre-doray2d079032019-02-07 15:18:2352 // has expired. This is not set for SequenceManager tasks if
53 // SetAddQueueTimeToTasks(true) wasn't call. This defaults to a null TimeTicks
54 // if the task hasn't been inserted in a sequence yet.
Karolina Soltysfe88c9e2018-11-06 15:27:0155 TimeTicks queue_time;
Erik Chena95335192018-07-30 17:48:2256
Alan Cutter9b0e1ab2019-03-21 04:22:1657 // Chain of symbols of the parent tasks which led to this one being posted.
58 static constexpr size_t kTaskBacktraceLength = 4;
59 std::array<const void*, kTaskBacktraceLength> task_backtrace = {};
ajwong4f13f742017-02-09 23:52:4060
Chris Hamilton60dcc26b2019-04-24 16:59:5161 // The context of the IPC message that was being handled when this task was
Chris Hamilton888085312019-05-30 00:53:3062 // posted. This is a hash of the IPC message name that is set within the scope
63 // of an IPC handler and when symbolized uniquely identifies the message being
Harkiran Bolaria875dd0d2020-09-15 18:10:4664 // processed. This property is not propagated from one PendingTask to the
Chris Hamilton60dcc26b2019-04-24 16:59:5165 // next. For example, if pending task A was posted while handling an IPC,
66 // and pending task B was posted from within pending task A, then pending task
Harkiran Bolaria875dd0d2020-09-15 18:10:4667 // B will not inherit the |ipc_hash| of pending task A.
Chris Hamilton888085312019-05-30 00:53:3068 uint32_t ipc_hash = 0;
Harkiran Bolaria875dd0d2020-09-15 18:10:4669 const char* ipc_interface_name = nullptr;
Chris Hamilton60dcc26b2019-04-24 16:59:5170
[email protected]dd1f9fe2011-11-15 23:36:3071 // Secondary sort key for run time.
Gabriel Charetteae4f9ce2018-04-05 19:00:0172 int sequence_num = 0;
[email protected]dd1f9fe2011-11-15 23:36:3073
Alex Clarke1ac1f982019-03-29 08:48:3774 bool task_backtrace_overflow = false;
75
[email protected]dd1f9fe2011-11-15 23:36:3076 // OK to dispatch from a nested loop.
Hajime Hoshi64853ef2017-10-11 12:56:0777 Nestable nestable;
cpuee8907952014-08-28 23:25:3778
79 // Needs high resolution timers.
Gabriel Charetteae4f9ce2018-04-05 19:00:0180 bool is_high_res = false;
[email protected]dd1f9fe2011-11-15 23:36:3081};
82
Brett Wilson3d88e7762017-08-14 19:54:3883using TaskQueue = base::queue<PendingTask>;
[email protected]dd1f9fe2011-11-15 23:36:3084
[email protected]262060ff2011-11-17 23:26:5385// PendingTasks are sorted by their |delayed_run_time| property.
danakj5d792152016-06-15 20:47:4786using DelayedTaskQueue = std::priority_queue<base::PendingTask>;
[email protected]262060ff2011-11-17 23:26:5387
[email protected]dd1f9fe2011-11-15 23:36:3088} // namespace base
89
danakj0a448602015-03-10 00:31:1690#endif // BASE_PENDING_TASK_H_