• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #ifndef BASE_PENDING_TASK_H_
6 #define BASE_PENDING_TASK_H_
7 
8 #include <array>
9 
10 #include "base/base_export.h"
11 #include "base/callback.h"
12 #include "base/containers/queue.h"
13 #include "base/location.h"
14 #include "base/time/time.h"
15 
16 namespace base {
17 
18 enum class Nestable {
19   kNonNestable,
20   kNestable,
21 };
22 
23 // Contains data about a pending task. Stored in TaskQueue and DelayedTaskQueue
24 // for use by classes that queue and execute tasks.
25 struct BASE_EXPORT PendingTask {
26   PendingTask(const Location& posted_from,
27               OnceClosure task,
28               TimeTicks delayed_run_time = TimeTicks(),
29               Nestable nestable = Nestable::kNestable);
30   PendingTask(PendingTask&& other);
31   ~PendingTask();
32 
33   PendingTask& operator=(PendingTask&& other);
34 
35   // Used to support sorting.
36   bool operator<(const PendingTask& other) const;
37 
38   // The task to run.
39   OnceClosure task;
40 
41   // The site this PendingTask was posted from.
42   Location posted_from;
43 
44   // The time when the task should be run.
45   base::TimeTicks delayed_run_time;
46 
47   // Chain of up-to-four symbols of the parent tasks which led to this one being
48   // posted.
49   std::array<const void*, 4> task_backtrace = {};
50 
51   // Secondary sort key for run time.
52   int sequence_num = 0;
53 
54   // OK to dispatch from a nested loop.
55   Nestable nestable;
56 
57   // Needs high resolution timers.
58   bool is_high_res = false;
59 };
60 
61 using TaskQueue = base::queue<PendingTask>;
62 
63 // PendingTasks are sorted by their |delayed_run_time| property.
64 using DelayedTaskQueue = std::priority_queue<base::PendingTask>;
65 
66 }  // namespace base
67 
68 #endif  // BASE_PENDING_TASK_H_
69