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