blob: 64dc1da56f3c1195d4cbbef93d694efe0a821387 [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
5#include "base/pending_task.h"
6
ajwong8d51d072017-02-10 08:52:40 +09007#include "base/message_loop/message_loop.h"
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +09008
9namespace base {
10
Brett Wilson89388db2017-09-12 14:22:16 +090011PendingTask::PendingTask(const Location& posted_from,
tzike82b7e82016-10-14 23:34:58 +090012 OnceClosure task,
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090013 TimeTicks delayed_run_time,
Hajime Hoshi67c2dbe2017-10-11 21:56:07 +090014 Nestable nestable)
Brett Wilsonc7c59cb2017-09-08 09:47:49 +090015 : task(std::move(task)),
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090016 posted_from(posted_from),
Brett Wilsonc7c59cb2017-09-08 09:47:49 +090017 delayed_run_time(delayed_run_time),
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090018 sequence_num(0),
cpu410a98e2014-08-29 08:25:37 +090019 nestable(nestable),
ajwong8d51d072017-02-10 08:52:40 +090020 is_high_res(false) {
21 const PendingTask* parent_task =
22 MessageLoop::current() ? MessageLoop::current()->current_pending_task_
23 : nullptr;
24 if (parent_task) {
25 task_backtrace[0] = parent_task->posted_from.program_counter();
26 std::copy(parent_task->task_backtrace.begin(),
27 parent_task->task_backtrace.end() - 1,
28 task_backtrace.begin() + 1);
29 } else {
30 task_backtrace.fill(nullptr);
31 }
32}
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090033
tzikc74f6fe2016-07-08 05:20:06 +090034PendingTask::PendingTask(PendingTask&& other) = default;
vmpstrdb004792016-02-19 07:12:24 +090035
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090036PendingTask::~PendingTask() {
37}
38
tzikc74f6fe2016-07-08 05:20:06 +090039PendingTask& PendingTask::operator=(PendingTask&& other) = default;
40
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090041bool PendingTask::operator<(const PendingTask& other) const {
42 // Since the top of a priority queue is defined as the "greatest" element, we
43 // need to invert the comparison here. We want the smaller time to be at the
44 // top of the heap.
45
46 if (delayed_run_time < other.delayed_run_time)
47 return false;
48
49 if (delayed_run_time > other.delayed_run_time)
50 return true;
51
52 // If the times happen to match, then we use the sequence number to decide.
53 // Compare the difference to support integer roll-over.
54 return (sequence_num - other.sequence_num) > 0;
55}
56
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090057} // namespace base