| 6 | #define BASE_PENDING_TASK_H_ |
| 7 |
| 8 | #include <array> |
| 9 |
| 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" |
| 16 |
| 17 | namespace base { |
| 18 |
| 19 | enum class Nestable : uint8_t { |
| 20 | kNonNestable, |
| 21 | kNestable, |
| 22 | }; |
| 23 |
| 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 { |
| 27 | PendingTask(); |
| 28 | PendingTask(const Location& posted_from, |
| 29 | OnceClosure task, |
| 30 | TimeTicks delayed_run_time = TimeTicks(), |
| 31 | Nestable nestable = Nestable::kNestable); |
| 32 | PendingTask(PendingTask&& other); |
| 33 | ~PendingTask(); |
| 34 |
| 35 | PendingTask& operator=(PendingTask&& other); |
| 36 |
| 37 | // Used to support sorting. |
| 38 | bool operator<(const PendingTask& other) const; |
| 39 |
| 40 | // The task to run. |
| 41 | OnceClosure task; |
| 42 |
| 43 | // The site this PendingTask was posted from. |
| 44 | Location posted_from; |
| 45 |
| 46 | // The time when the task should be run. This is null for an immediate task. |
| 47 | base::TimeTicks delayed_run_time; |
| 48 |
| 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. |
| 55 | TimeTicks queue_time; |
| 56 |
| 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 = {}; |
| 60 |
| 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; |
| 70 |
| 71 | // Secondary sort key for run time. |
| 72 | int sequence_num = 0; |
| 73 |
| 74 | bool task_backtrace_overflow = false; |
| 75 |
| 76 | // OK to dispatch from a nested loop. |
| 77 | Nestable nestable; |
| 78 |
| 79 | // Needs high resolution timers. |
| 80 | bool is_high_res = false; |
| 81 | }; |
| 82 |
| 83 | using TaskQueue = base::queue<PendingTask>; |
| 84 |
| 85 | // PendingTasks are sorted by their |delayed_run_time| property. |
| 86 | using DelayedTaskQueue = std::priority_queue<base::PendingTask>; |
| 87 |
| 88 | } // namespace base |
| 89 |
| 90 | #endif // BASE_PENDING_TASK_H_ |
| 91 |