tasks.h 4.2 KB

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