blob: b288f28d09f8a62044c419b3e18676e6558c3771 [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
7#include "base/tracked_objects.h"
8
9namespace base {
10
ksakamoto@chromium.org518c2112014-07-22 20:32:40 +090011#if _MSC_VER >= 1700
12// This a temporary fix for compiling on VS2012. http://crbug.com/154744
13PendingTask::PendingTask() : sequence_num(-1), nestable(false) {
14}
15#endif
16
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090017PendingTask::PendingTask(const tracked_objects::Location& posted_from,
18 const base::Closure& task)
19 : base::TrackingInfo(posted_from, TimeTicks()),
20 task(task),
21 posted_from(posted_from),
22 sequence_num(0),
ksakamoto@chromium.org518c2112014-07-22 20:32:40 +090023 nestable(true) {
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090024}
25
26PendingTask::PendingTask(const tracked_objects::Location& posted_from,
27 const base::Closure& task,
28 TimeTicks delayed_run_time,
29 bool nestable)
30 : base::TrackingInfo(posted_from, delayed_run_time),
31 task(task),
32 posted_from(posted_from),
33 sequence_num(0),
ksakamoto@chromium.org518c2112014-07-22 20:32:40 +090034 nestable(nestable) {
jhawkins@chromium.org2af12f22011-11-16 08:36:30 +090035}
36
37PendingTask::~PendingTask() {
38}
39
40bool PendingTask::operator<(const PendingTask& other) const {
41 // Since the top of a priority queue is defined as the "greatest" element, we
42 // need to invert the comparison here. We want the smaller time to be at the
43 // top of the heap.
44
45 if (delayed_run_time < other.delayed_run_time)
46 return false;
47
48 if (delayed_run_time > other.delayed_run_time)
49 return true;
50
51 // If the times happen to match, then we use the sequence number to decide.
52 // Compare the difference to support integer roll-over.
53 return (sequence_num - other.sequence_num) > 0;
54}
55
56void TaskQueue::Swap(TaskQueue* queue) {
57 c.swap(queue->c); // Calls std::deque::swap.
58}
59
60} // namespace base