blob: 8c7854b14cb0188c6f4b13c350d08066d84f730e [file] [log] [blame]
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +09001// 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
danakj0393da32015-03-10 09:31:16 +09005#ifndef BASE_PENDING_TASK_H_
6#define BASE_PENDING_TASK_H_
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +09007
ajwong8d51d072017-02-10 08:52:40 +09008#include <array>
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +09009
rsleevi@chromium.org3f19acb2011-11-18 15:08:02 +090010#include "base/base_export.h"
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090011#include "base/callback.h"
Brett Wilson94ac4d12017-08-15 04:54:38 +090012#include "base/containers/queue.h"
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090013#include "base/location.h"
avi@chromium.orgb45ec932013-06-29 00:14:18 +090014#include "base/time/time.h"
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090015
16namespace base {
17
Hajime Hoshi67c2dbe2017-10-11 21:56:07 +090018enum class Nestable {
19 kNonNestable,
20 kNestable,
21};
22
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090023// Contains data about a pending task. Stored in TaskQueue and DelayedTaskQueue
24// for use by classes that queue and execute tasks.
Brett Wilsonc7c59cb2017-09-08 09:47:49 +090025struct BASE_EXPORT PendingTask {
Brett Wilson89388db2017-09-12 14:22:16 +090026 PendingTask(const Location& posted_from,
tzike82b7e82016-10-14 23:34:58 +090027 OnceClosure task,
Hajime Hoshi67c2dbe2017-10-11 21:56:07 +090028 TimeTicks delayed_run_time = TimeTicks(),
29 Nestable nestable = Nestable::kNestable);
tzikc74f6fe2016-07-08 05:20:06 +090030 PendingTask(PendingTask&& other);
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090031 ~PendingTask();
32
tzikc74f6fe2016-07-08 05:20:06 +090033 PendingTask& operator=(PendingTask&& other);
34
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090035 // Used to support sorting.
36 bool operator<(const PendingTask& other) const;
37
38 // The task to run.
tzike82b7e82016-10-14 23:34:58 +090039 OnceClosure task;
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090040
41 // The site this PendingTask was posted from.
Brett Wilson89388db2017-09-12 14:22:16 +090042 Location posted_from;
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090043
Brett Wilsonc7c59cb2017-09-08 09:47:49 +090044 // The time when the task should be run.
45 base::TimeTicks delayed_run_time;
46
ajwong8d51d072017-02-10 08:52:40 +090047 // Task backtrace.
48 std::array<const void*, 4> task_backtrace;
49
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090050 // Secondary sort key for run time.
51 int sequence_num;
52
53 // OK to dispatch from a nested loop.
Hajime Hoshi67c2dbe2017-10-11 21:56:07 +090054 Nestable nestable;
cpu410a98e2014-08-29 08:25:37 +090055
56 // Needs high resolution timers.
57 bool is_high_res;
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090058};
59
Brett Wilson94ac4d12017-08-15 04:54:38 +090060using TaskQueue = base::queue<PendingTask>;
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090061
jhawkins@chromium.org303de232011-11-18 08:26:53 +090062// PendingTasks are sorted by their |delayed_run_time| property.
danakjb82557d2016-06-16 05:47:47 +090063using DelayedTaskQueue = std::priority_queue<base::PendingTask>;
jhawkins@chromium.org303de232011-11-18 08:26:53 +090064
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090065} // namespace base
66
danakj0393da32015-03-10 09:31:16 +090067#endif // BASE_PENDING_TASK_H_