pending_task.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/location.h"
  10. #include "base/task/delay_policy.h"
  11. #include "base/time/time.h"
  12. namespace base {
  13. enum class Nestable : uint8_t {
  14. kNonNestable,
  15. kNestable,
  16. };
  17. // Contains data about a pending task. Stored in TaskQueue and DelayedTaskQueue
  18. // for use by classes that queue and execute tasks.
  19. struct BASE_EXPORT PendingTask {
  20. PendingTask();
  21. PendingTask(const Location& posted_from,
  22. OnceClosure task,
  23. TimeTicks queue_time = TimeTicks(),
  24. TimeTicks delayed_run_time = TimeTicks(),
  25. TimeDelta leeway = TimeDelta(),
  26. subtle::DelayPolicy delay_policy =
  27. subtle::DelayPolicy::kFlexibleNoSooner);
  28. PendingTask(PendingTask&& other);
  29. ~PendingTask();
  30. PendingTask& operator=(PendingTask&& other);
  31. // Returns the time at which this task should run. This is |delayed_run_time|
  32. // for a delayed task, |queue_time| otherwise.
  33. base::TimeTicks GetDesiredExecutionTime() const;
  34. TimeTicks earliest_delayed_run_time() const;
  35. TimeTicks latest_delayed_run_time() const;
  36. // The task to run.
  37. OnceClosure task;
  38. // The site this PendingTask was posted from.
  39. Location posted_from;
  40. // The time at which the task was queued, which happens at post time. For
  41. // deferred non-nestable tasks, this is reset when the nested loop exits and
  42. // the deferred tasks are pushed back at the front of the queue. This is not
  43. // set for immediate SequenceManager tasks unless SetAddQueueTimeToTasks(true)
  44. // was called. This defaults to a null TimeTicks if the task hasn't been
  45. // inserted in a sequence yet.
  46. TimeTicks queue_time;
  47. // The time when the task should be run. This is null for an immediate task.
  48. base::TimeTicks delayed_run_time;
  49. // |leeway| and |delay_policy| determine the preferred time range for running
  50. // the delayed task. A larger leeway provides more freedom to run the task at
  51. // an optimal time for power consumption. These fields are ignored for an
  52. // immediate (non-delayed) task.
  53. TimeDelta leeway;
  54. subtle::DelayPolicy delay_policy = subtle::DelayPolicy::kFlexibleNoSooner;
  55. // Chain of symbols of the parent tasks which led to this one being posted.
  56. static constexpr size_t kTaskBacktraceLength = 4;
  57. std::array<const void*, kTaskBacktraceLength> task_backtrace = {};
  58. // The context of the IPC message that was being handled when this task was
  59. // posted. This is a hash of the IPC message name that is set within the scope
  60. // of an IPC handler and when symbolized uniquely identifies the message being
  61. // processed. This property is not propagated from one PendingTask to the
  62. // next. For example, if pending task A was posted while handling an IPC,
  63. // and pending task B was posted from within pending task A, then pending task
  64. // B will not inherit the |ipc_hash| of pending task A.
  65. uint32_t ipc_hash = 0;
  66. const char* ipc_interface_name = nullptr;
  67. // Secondary sort key for run time.
  68. int sequence_num = 0;
  69. bool task_backtrace_overflow = false;
  70. };
  71. } // namespace base
  72. #endif // BASE_PENDING_TASK_H_