pending_task.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.
  4. #ifndef BASE_PENDING_TASK_H_
  5. #define BASE_PENDING_TASK_H_
  6. #include <array>
  7. #include "base/base_export.h"
  8. #include "base/callback.h"
  9. #include "base/containers/queue.h"
  10. #include "base/location.h"
  11. #include "base/optional.h"
  12. #include "base/time/time.h"
  13. namespace base {
  14. enum class Nestable : uint8_t {
  15. kNonNestable,
  16. kNestable,
  17. };
  18. // Contains data about a pending task. Stored in TaskQueue and DelayedTaskQueue
  19. // for use by classes that queue and execute tasks.
  20. struct BASE_EXPORT PendingTask {
  21. PendingTask();
  22. PendingTask(const Location& posted_from,
  23. OnceClosure task,
  24. TimeTicks delayed_run_time = TimeTicks(),
  25. Nestable nestable = Nestable::kNestable);
  26. PendingTask(PendingTask&& other);
  27. ~PendingTask();
  28. PendingTask& operator=(PendingTask&& other);
  29. // Used to support sorting.
  30. bool operator<(const PendingTask& other) const;
  31. // The task to run.
  32. OnceClosure task;
  33. // The site this PendingTask was posted from.
  34. Location posted_from;
  35. // The time when the task should be run. This is null for an immediate task.
  36. base::TimeTicks delayed_run_time;
  37. // The time at which the task was queued. For SequenceManager tasks and
  38. // ThreadPool non-delayed tasks, this happens at post time. For
  39. // ThreadPool delayed tasks, this happens some time after the task's delay
  40. // has expired. This is not set for SequenceManager tasks if
  41. // SetAddQueueTimeToTasks(true) wasn't call. This defaults to a null TimeTicks
  42. // if the task hasn't been inserted in a sequence yet.
  43. TimeTicks queue_time;
  44. // Chain of symbols of the parent tasks which led to this one being posted.
  45. static constexpr size_t kTaskBacktraceLength = 4;
  46. std::array<const void*, kTaskBacktraceLength> task_backtrace = {};
  47. // The context of the IPC message that was being handled when this task was
  48. // posted. This is a hash of the IPC message name that is set within the scope
  49. // of an IPC handler and when symbolized uniquely identifies the message being
  50. // processed. This property is not propagated from one PendingTask to the
  51. // next. For example, if pending task A was posted while handling an IPC,
  52. // and pending task B was posted from within pending task A, then pending task
  53. // B will not inherit the |ipc_hash| of pending task A.
  54. uint32_t ipc_hash = 0;
  55. const char* ipc_interface_name = nullptr;
  56. // Secondary sort key for run time.
  57. int sequence_num = 0;
  58. bool task_backtrace_overflow = false;
  59. // OK to dispatch from a nested loop.
  60. Nestable nestable;
  61. // Needs high resolution timers.
  62. bool is_high_res = false;
  63. };
  64. using TaskQueue = base::queue<PendingTask>;
  65. // PendingTasks are sorted by their |delayed_run_time| property.
  66. using DelayedTaskQueue = std::priority_queue<base::PendingTask>;
  67. } // namespace base
  68. #endif // BASE_PENDING_TASK_H_