1 // 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.
5 #ifndef BASE_PENDING_TASK_H_
6 #define BASE_PENDING_TASK_H_
10 #include "base/base_export.h"
11 #include "base/callback.h"
12 #include "base/containers/queue.h"
13 #include "base/location.h"
14 #include "base/optional.h"
15 #include "base/time/time.h"
19 enum class Nestable : uint8_t {
24 // Contains data about a pending task. Stored in TaskQueue and DelayedTaskQueue
25 // for use by classes that queue and execute tasks.
26 struct BASE_EXPORT PendingTask {
28 PendingTask(const Location& posted_from,
30 TimeTicks delayed_run_time = TimeTicks(),
31 Nestable nestable = Nestable::kNestable);
32 PendingTask(PendingTask&& other);
35 PendingTask& operator=(PendingTask&& other);
37 // Used to support sorting.
38 bool operator<(const PendingTask& other) const;
43 // The site this PendingTask was posted from.
46 // The time when the task should be run. This is null for an immediate task.
47 base::TimeTicks delayed_run_time;
49 // The time at which the task was queued. For SequenceManager tasks and
50 // ThreadPool non-delayed tasks, this happens at post time. For
51 // ThreadPool 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.
57 // 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 = {};
61 // The context of the IPC message that was being handled when this task was
62 // 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
64 // processed. This property is also propagated from one PendingTask to the
65 // 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
67 // B will inherit the |ipc_hash| of pending task A. In some sense this can be
68 // interpreted as a "root" task backtrace frame.
69 uint32_t ipc_hash = 0;
71 // Secondary sort key for run time.
74 bool task_backtrace_overflow = false;
76 // OK to dispatch from a nested loop.
79 // Needs high resolution timers.
80 bool is_high_res = false;
83 using TaskQueue = base::queue<PendingTask>;
85 // PendingTasks are sorted by their |delayed_run_time| property.
86 using DelayedTaskQueue = std::priority_queue<base::PendingTask>;
90 #endif // BASE_PENDING_TASK_H_