Home | History | Annotate | Download | only in base
      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 #include "base/pending_task.h"
      6 
      7 #include "base/message_loop/message_loop.h"
      8 #include "base/tracked_objects.h"
      9 
     10 namespace base {
     11 
     12 PendingTask::PendingTask(const tracked_objects::Location& posted_from,
     13                          OnceClosure task)
     14     : PendingTask(posted_from, std::move(task), TimeTicks(), true) {}
     15 
     16 PendingTask::PendingTask(const tracked_objects::Location& posted_from,
     17                          OnceClosure task,
     18                          TimeTicks delayed_run_time,
     19                          bool nestable)
     20     : base::TrackingInfo(posted_from, delayed_run_time),
     21       task(std::move(task)),
     22       posted_from(posted_from),
     23       sequence_num(0),
     24       nestable(nestable),
     25       is_high_res(false) {
     26   const PendingTask* parent_task =
     27       MessageLoop::current() ? MessageLoop::current()->current_pending_task_
     28                              : nullptr;
     29   if (parent_task) {
     30     task_backtrace[0] = parent_task->posted_from.program_counter();
     31     std::copy(parent_task->task_backtrace.begin(),
     32               parent_task->task_backtrace.end() - 1,
     33               task_backtrace.begin() + 1);
     34   } else {
     35     task_backtrace.fill(nullptr);
     36   }
     37 }
     38 
     39 PendingTask::PendingTask(PendingTask&& other) = default;
     40 
     41 PendingTask::~PendingTask() {
     42 }
     43 
     44 PendingTask& PendingTask::operator=(PendingTask&& other) = default;
     45 
     46 bool PendingTask::operator<(const PendingTask& other) const {
     47   // Since the top of a priority queue is defined as the "greatest" element, we
     48   // need to invert the comparison here.  We want the smaller time to be at the
     49   // top of the heap.
     50 
     51   if (delayed_run_time < other.delayed_run_time)
     52     return false;
     53 
     54   if (delayed_run_time > other.delayed_run_time)
     55     return true;
     56 
     57   // If the times happen to match, then we use the sequence number to decide.
     58   // Compare the difference to support integer roll-over.
     59   return (sequence_num - other.sequence_num) > 0;
     60 }
     61 
     62 }  // namespace base
     63