blob: a609a9108f64e745cb6252b4b096be4b42ec38ba [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
Hajime Hoshi64853ef2017-10-11 12:56:0719enum class Nestable {
20 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
Brett Wilsonb57e3dd2017-09-08 00:47:4946 // The time when the task should be run.
47 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
50 // TaskScheduler non-delayed tasks, this happens at post time. For
51 // TaskScheduler delayed tasks, this happens some time after the task's delay
52 // 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 = {};
60 bool task_backtrace_overflow = false;
ajwong4f13f742017-02-09 23:52:4061
[email protected]dd1f9fe2011-11-15 23:36:3062 // Secondary sort key for run time.
Gabriel Charetteae4f9ce2018-04-05 19:00:0163 int sequence_num = 0;
[email protected]dd1f9fe2011-11-15 23:36:3064
65 // OK to dispatch from a nested loop.
Hajime Hoshi64853ef2017-10-11 12:56:0766 Nestable nestable;
cpuee8907952014-08-28 23:25:3767
68 // Needs high resolution timers.
Gabriel Charetteae4f9ce2018-04-05 19:00:0169 bool is_high_res = false;
[email protected]dd1f9fe2011-11-15 23:36:3070};
71
Brett Wilson3d88e7762017-08-14 19:54:3872using TaskQueue = base::queue<PendingTask>;
[email protected]dd1f9fe2011-11-15 23:36:3073
[email protected]262060ff2011-11-17 23:26:5374// PendingTasks are sorted by their |delayed_run_time| property.
danakj5d792152016-06-15 20:47:4775using DelayedTaskQueue = std::priority_queue<base::PendingTask>;
[email protected]262060ff2011-11-17 23:26:5376
[email protected]dd1f9fe2011-11-15 23:36:3077} // namespace base
78
danakj0a448602015-03-10 00:31:1679#endif // BASE_PENDING_TASK_H_