1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89

base / task / sequence_manager / task_order.cc [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/task/sequence_manager/task_order.h"

#include <functional>

#include "base/task/sequence_manager/enqueue_order.h"
#include "base/task/sequence_manager/tasks.h"

namespace base {
namespace sequence_manager {

namespace {

// Returns true iff `task_order1` Comparator{} `task_order2`. Used to
// implement other comparison operators.
template <typename Comparator>
static bool Compare(const base::sequence_manager::TaskOrder& task_order1,
                    const base::sequence_manager::TaskOrder& task_order2) {
  Comparator cmp{};

  if (task_order1.enqueue_order() != task_order2.enqueue_order())
    return cmp(task_order1.enqueue_order(), task_order2.enqueue_order());

  if (task_order1.delayed_run_time() != task_order2.delayed_run_time())
    return cmp(task_order1.delayed_run_time(), task_order2.delayed_run_time());

  // If the times happen to match, then we use the sequence number to decide.
  // Compare the difference to support integer roll-over.
  return cmp(task_order1.sequence_num() - task_order2.sequence_num(), 0);
}

}  // namespace

// Static
TaskOrder TaskOrder::CreateForTesting(EnqueueOrder enqueue_order,
                                      TimeTicks delayed_run_time,
                                      int sequence_num) {
  return TaskOrder(enqueue_order, delayed_run_time, sequence_num);
}

// Static
TaskOrder TaskOrder::CreateForTesting(EnqueueOrder enqueue_order) {
  return TaskOrder(enqueue_order, TimeTicks(), 0);
}

TaskOrder::TaskOrder(EnqueueOrder enqueue_order,
                     TimeTicks delayed_run_time,
                     int sequence_num)
    : enqueue_order_(enqueue_order),
      delayed_run_time_(delayed_run_time),
      sequence_num_(sequence_num) {}

TaskOrder::TaskOrder(const TaskOrder& other) = default;

TaskOrder& TaskOrder::operator=(const TaskOrder& other) = default;

TaskOrder::~TaskOrder() = default;

bool TaskOrder::operator>(const TaskOrder& other) const {
  return Compare<std::greater<>>(*this, other);
}

bool TaskOrder::operator<(const TaskOrder& other) const {
  return Compare<std::less<>>(*this, other);
}

bool TaskOrder::operator<=(const TaskOrder& other) const {
  return Compare<std::less_equal<>>(*this, other);
}

bool TaskOrder::operator>=(const TaskOrder& other) const {
  return Compare<std::greater_equal<>>(*this, other);
}

bool TaskOrder::operator==(const TaskOrder& other) const {
  return enqueue_order_ == other.enqueue_order_ &&
         delayed_run_time_ == other.delayed_run_time_ &&
         sequence_num_ == other.sequence_num_;
}

bool TaskOrder::operator!=(const TaskOrder& other) const {
  return !(*this == other);
}

}  // namespace sequence_manager
}  // namespace base