File: prioritized_task_runner.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (105 lines) | stat: -rw-r--r-- 3,270 bytes parent folder | download | duplicates (10)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/base/prioritized_task_runner.h"

#include <algorithm>

#include "base/functional/bind.h"
#include "base/task/task_runner.h"
#include "base/task/thread_pool.h"

namespace net {

PrioritizedTaskRunner::Job::Job(const base::Location& from_here,
                                base::OnceClosure task,
                                base::OnceClosure reply,
                                uint32_t priority,
                                uint32_t task_count)
    : from_here(from_here),
      task(std::move(task)),
      reply(std::move(reply)),
      priority(priority),
      task_count(task_count) {}

PrioritizedTaskRunner::Job::Job() = default;

PrioritizedTaskRunner::Job::~Job() = default;
PrioritizedTaskRunner::Job::Job(Job&& other) = default;
PrioritizedTaskRunner::Job& PrioritizedTaskRunner::Job::operator=(Job&& other) =
    default;

PrioritizedTaskRunner::PrioritizedTaskRunner(
    const base::TaskTraits& task_traits)
    : task_traits_(task_traits) {}

void PrioritizedTaskRunner::PostTaskAndReply(const base::Location& from_here,
                                             base::OnceClosure task,
                                             base::OnceClosure reply,
                                             uint32_t priority) {
  Job job(from_here, std::move(task), std::move(reply), priority,
          task_count_++);
  task_jobs_.Push(std::move(job));

  scoped_refptr<base::TaskRunner> task_runner;
  if (task_runner_for_testing_) {
    task_runner = task_runner_for_testing_;
  } else {
    task_runner = base::ThreadPool::CreateSequencedTaskRunner(task_traits_);
  }

  task_runner->PostTaskAndReply(
      from_here,
      base::BindOnce(&PrioritizedTaskRunner::RunTaskAndPostReply, this),
      base::BindOnce(&PrioritizedTaskRunner::RunReply, this));
}

PrioritizedTaskRunner::~PrioritizedTaskRunner() = default;

void PrioritizedTaskRunner::RunTaskAndPostReply() {
  // Find the next job to run.
  Job job = task_jobs_.Pop();

  std::move(job.task).Run();

  // Add the job to the reply priority queue.
  reply_jobs_.Push(std::move(job));
}

void PrioritizedTaskRunner::RunReply() {
  // Find the next job to run.
  Job job = reply_jobs_.Pop();

  // Run the job.
  std::move(job.reply).Run();
}

struct PrioritizedTaskRunner::JobComparer {
  bool operator()(const Job& left, const Job& right) {
    if (left.priority == right.priority) {
      return left.task_count > right.task_count;
    }
    return left.priority > right.priority;
  }
};

PrioritizedTaskRunner::JobPriorityQueue::JobPriorityQueue() = default;
PrioritizedTaskRunner::JobPriorityQueue::~JobPriorityQueue() = default;

void PrioritizedTaskRunner::JobPriorityQueue::Push(Job job) {
  base::AutoLock auto_lock(lock_);
  heap_.push_back(std::move(job));
  std::push_heap(heap_.begin(), heap_.end(), JobComparer());
}

PrioritizedTaskRunner::Job PrioritizedTaskRunner::JobPriorityQueue::Pop() {
  base::AutoLock auto_lock(lock_);
  CHECK(!heap_.empty());
  std::pop_heap(heap_.begin(), heap_.end(), JobComparer());
  Job job = std::move(heap_.back());
  heap_.pop_back();
  return job;
}

}  // namespace net