[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 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 | |||||
danakj | 0a44860 | 2015-03-10 00:31:16 | [diff] [blame] | 5 | #ifndef BASE_PENDING_TASK_H_ |
6 | #define BASE_PENDING_TASK_H_ | ||||
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 7 | |
ajwong | 4f13f74 | 2017-02-09 23:52:40 | [diff] [blame] | 8 | #include <array> |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 9 | |
[email protected] | c360bae7 | 2011-11-18 06:08:02 | [diff] [blame] | 10 | #include "base/base_export.h" |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 11 | #include "base/callback.h" |
Brett Wilson | 3d88e776 | 2017-08-14 19:54:38 | [diff] [blame] | 12 | #include "base/containers/queue.h" |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 13 | #include "base/location.h" |
Erik Chen | a9533519 | 2018-07-30 17:48:22 | [diff] [blame] | 14 | #include "base/optional.h" |
[email protected] | 8f9a3a5 | 2013-06-28 15:14:18 | [diff] [blame] | 15 | #include "base/time/time.h" |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 16 | |
17 | namespace base { | ||||
18 | |||||
Hajime Hoshi | 64853ef | 2017-10-11 12:56:07 | [diff] [blame] | 19 | enum class Nestable { |
20 | kNonNestable, | ||||
21 | kNestable, | ||||
22 | }; | ||||
23 | |||||
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 24 | // Contains data about a pending task. Stored in TaskQueue and DelayedTaskQueue |
25 | // for use by classes that queue and execute tasks. | ||||
Brett Wilson | b57e3dd | 2017-09-08 00:47:49 | [diff] [blame] | 26 | struct BASE_EXPORT PendingTask { |
Jesse McKenna | 9cf1137 | 2018-11-01 19:38:38 | [diff] [blame^] | 27 | PendingTask(); |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 28 | PendingTask(const Location& posted_from, |
tzik | 739ffe3 | 2016-10-14 14:34:58 | [diff] [blame] | 29 | OnceClosure task, |
Hajime Hoshi | 64853ef | 2017-10-11 12:56:07 | [diff] [blame] | 30 | TimeTicks delayed_run_time = TimeTicks(), |
31 | Nestable nestable = Nestable::kNestable); | ||||
tzik | b6769d5 | 2016-07-07 20:20:06 | [diff] [blame] | 32 | PendingTask(PendingTask&& other); |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 33 | ~PendingTask(); |
34 | |||||
tzik | b6769d5 | 2016-07-07 20:20:06 | [diff] [blame] | 35 | PendingTask& operator=(PendingTask&& other); |
36 | |||||
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 37 | // Used to support sorting. |
38 | bool operator<(const PendingTask& other) const; | ||||
39 | |||||
40 | // The task to run. | ||||
tzik | 739ffe3 | 2016-10-14 14:34:58 | [diff] [blame] | 41 | OnceClosure task; |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 42 | |
43 | // The site this PendingTask was posted from. | ||||
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 44 | Location posted_from; |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 45 | |
Brett Wilson | b57e3dd | 2017-09-08 00:47:49 | [diff] [blame] | 46 | // The time when the task should be run. |
47 | base::TimeTicks delayed_run_time; | ||||
48 | |||||
Erik Chen | a9533519 | 2018-07-30 17:48:22 | [diff] [blame] | 49 | // The time at which the task was queued. Only set if the task was posted to a |
50 | // MessageLoop with SetAddQueueTimeToTasks(true). | ||||
51 | Optional<TimeTicks> queue_time; | ||||
52 | |||||
Gabriel Charette | 4036f82 | 2018-07-06 18:16:45 | [diff] [blame] | 53 | // Chain of up-to-four symbols of the parent tasks which led to this one being |
54 | // posted. | ||||
55 | std::array<const void*, 4> task_backtrace = {}; | ||||
ajwong | 4f13f74 | 2017-02-09 23:52:40 | [diff] [blame] | 56 | |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 57 | // Secondary sort key for run time. |
Gabriel Charette | ae4f9ce | 2018-04-05 19:00:01 | [diff] [blame] | 58 | int sequence_num = 0; |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 59 | |
60 | // OK to dispatch from a nested loop. | ||||
Hajime Hoshi | 64853ef | 2017-10-11 12:56:07 | [diff] [blame] | 61 | Nestable nestable; |
cpu | ee890795 | 2014-08-28 23:25:37 | [diff] [blame] | 62 | |
63 | // Needs high resolution timers. | ||||
Gabriel Charette | ae4f9ce | 2018-04-05 19:00:01 | [diff] [blame] | 64 | bool is_high_res = false; |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 65 | }; |
66 | |||||
Brett Wilson | 3d88e776 | 2017-08-14 19:54:38 | [diff] [blame] | 67 | using TaskQueue = base::queue<PendingTask>; |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 68 | |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 69 | // PendingTasks are sorted by their |delayed_run_time| property. |
danakj | 5d79215 | 2016-06-15 20:47:47 | [diff] [blame] | 70 | using DelayedTaskQueue = std::priority_queue<base::PendingTask>; |
[email protected] | 262060ff | 2011-11-17 23:26:53 | [diff] [blame] | 71 | |
[email protected] | dd1f9fe | 2011-11-15 23:36:30 | [diff] [blame] | 72 | } // namespace base |
73 | |||||
danakj | 0a44860 | 2015-03-10 00:31:16 | [diff] [blame] | 74 | #endif // BASE_PENDING_TASK_H_ |