| 6 | #define BASE_TASK_SEQUENCE_MANAGER_TASKS_H_ |
| 7 |
| 8 | #include "base/pending_task.h" |
| 9 | #include "base/sequenced_task_runner.h" |
| 10 | #include "base/task/sequence_manager/enqueue_order.h" |
| 11 |
| 12 | namespace base { |
| 13 | namespace sequence_manager { |
| 14 |
| 15 | using TaskType = uint8_t; |
| 16 | constexpr TaskType kTaskTypeNone = 0; |
| 17 |
| 18 | namespace internal { |
| 19 |
| 20 | enum class WakeUpResolution { kLow, kHigh }; |
| 21 |
| 22 | // Wrapper around PostTask method arguments and the assigned task type. |
| 23 | // Eventually it becomes a PendingTask once accepted by a TaskQueueImpl. |
| 24 | struct BASE_EXPORT PostedTask { |
| 25 | explicit PostedTask(scoped_refptr<SequencedTaskRunner> task_runner, |
| 26 | OnceClosure callback = OnceClosure(), |
| 27 | Location location = Location(), |
| 28 | TimeDelta delay = TimeDelta(), |
| 29 | Nestable nestable = Nestable::kNestable, |
| 30 | TaskType task_type = kTaskTypeNone); |
| 31 | PostedTask(PostedTask&& move_from) noexcept; |
| 32 | ~PostedTask(); |
| 33 |
| 34 | OnceClosure callback; |
| 35 | Location location; |
| 36 | TimeDelta delay; |
| 37 | Nestable nestable; |
| 38 | TaskType task_type; |
| 39 | // The task runner this task is running on. Can be used by task runners that |
| 40 | // support posting back to the "current sequence". |
| 41 | scoped_refptr<SequencedTaskRunner> task_runner; |
| 42 | // The time at which the task was queued. |
| 43 | TimeTicks queue_time; |
| 44 |
| 45 | DISALLOW_COPY_AND_ASSIGN(PostedTask); |
| 46 | }; |
| 47 |
| 48 | // Represents a time at which a task wants to run. Tasks scheduled for the |
| 49 | // same point in time will be ordered by their sequence numbers. |
| 50 | struct DelayedWakeUp { |
| 51 | TimeTicks time; |
| 52 | int sequence_num; |
| 53 |
| 54 | bool operator!=(const DelayedWakeUp& other) const { |
| 55 | return time != other.time || other.sequence_num != sequence_num; |
| 56 | } |
| 57 |
| 58 | bool operator==(const DelayedWakeUp& other) const { |
| 59 | return !(*this != other); |
| 60 | } |
| 61 |
| 62 | bool operator<=(const DelayedWakeUp& other) const { |
| 63 | if (time == other.time) { |
| 64 | // Debug gcc builds can compare an element against itself. |
| 65 | DCHECK(sequence_num != other.sequence_num || this == &other); |
| 66 | // |sequence_num| is int and might wrap around to a negative number when |
| 67 | // casted from EnqueueOrder. This way of comparison handles that properly. |
| 68 | return (sequence_num - other.sequence_num) <= 0; |
| 69 | } |
| 70 | return time < other.time; |
| 71 | } |
| 72 | }; |
| 73 |
| 74 | } // namespace internal |
| 75 |
| 76 | // PendingTask with extra metadata for SequenceManager. |
| 77 | struct BASE_EXPORT Task : public PendingTask { |
| 78 | Task(internal::PostedTask posted_task, |
| 79 | TimeTicks delayed_run_time, |
| 80 | EnqueueOrder sequence_order, |
| 81 | EnqueueOrder enqueue_order = EnqueueOrder(), |
| 82 | internal::WakeUpResolution wake_up_resolution = |
| 83 | internal::WakeUpResolution::kLow); |
| 84 | Task(Task&& move_from); |
| 85 | ~Task(); |
| 86 | Task& operator=(Task&& other); |
| 87 |
| 88 | internal::DelayedWakeUp delayed_wake_up() const { |
| 89 | return internal::DelayedWakeUp{delayed_run_time, sequence_num}; |
| 90 | } |
| 91 |
| 92 | // SequenceManager is particularly sensitive to enqueue order, |
| 93 | // so we have accessors for safety. |
| 94 | EnqueueOrder enqueue_order() const { |
| 95 | DCHECK(enqueue_order_); |
| 96 | return enqueue_order_; |
| 97 | } |
| 98 |
| 99 | void set_enqueue_order(EnqueueOrder enqueue_order) { |
| 100 | DCHECK(!enqueue_order_); |
| 101 | enqueue_order_ = enqueue_order; |
| 102 | } |
| 103 |
| 104 | bool enqueue_order_set() const { return enqueue_order_; } |
| 105 |
| 106 | TaskType task_type; |
| 107 |
| 108 | // The task runner this task is running on. Can be used by task runners that |
| 109 | // support posting back to the "current sequence". |
| 110 | scoped_refptr<SequencedTaskRunner> task_runner; |
| 111 |
| 112 | #if DCHECK_IS_ON() |
| 113 | bool cross_thread_; |
| 114 | #endif |
| 115 |
| 116 | private: |
| 117 | // Similar to |sequence_num|, but ultimately the |enqueue_order| is what |
| 118 | // the scheduler uses for task ordering. For immediate tasks |enqueue_order| |
| 119 | // is set when posted, but for delayed tasks it's not defined until they are |
| 120 | // enqueued. This is because otherwise delayed tasks could run before |
| 121 | // an immediate task posted after the delayed task. |
| 122 | EnqueueOrder enqueue_order_; |
| 123 | }; |
| 124 |
| 125 | } // namespace sequence_manager |
| 126 | } // namespace base |
| 127 |
| 128 | #endif // BASE_TASK_SEQUENCE_MANAGER_TASKS_H_ |
| 129 |