prioritized_task_runner.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2018 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. #include "net/base/prioritized_task_runner.h"
  5. #include <algorithm>
  6. #include "base/bind.h"
  7. #include "base/task/task_runner.h"
  8. #include "base/task/task_runner_util.h"
  9. #include "base/task/thread_pool.h"
  10. namespace net {
  11. PrioritizedTaskRunner::Job::Job(const base::Location& from_here,
  12. base::OnceClosure task,
  13. base::OnceClosure reply,
  14. uint32_t priority,
  15. uint32_t task_count)
  16. : from_here(from_here),
  17. task(std::move(task)),
  18. reply(std::move(reply)),
  19. priority(priority),
  20. task_count(task_count) {}
  21. PrioritizedTaskRunner::Job::Job() = default;
  22. PrioritizedTaskRunner::Job::~Job() = default;
  23. PrioritizedTaskRunner::Job::Job(Job&& other) = default;
  24. PrioritizedTaskRunner::Job& PrioritizedTaskRunner::Job::operator=(Job&& other) =
  25. default;
  26. PrioritizedTaskRunner::PrioritizedTaskRunner(
  27. const base::TaskTraits& task_traits)
  28. : task_traits_(task_traits) {}
  29. void PrioritizedTaskRunner::PostTaskAndReply(const base::Location& from_here,
  30. base::OnceClosure task,
  31. base::OnceClosure reply,
  32. uint32_t priority) {
  33. Job job(from_here, std::move(task), std::move(reply), priority,
  34. task_count_++);
  35. {
  36. base::AutoLock lock(task_job_heap_lock_);
  37. task_job_heap_.push_back(std::move(job));
  38. std::push_heap(task_job_heap_.begin(), task_job_heap_.end(), JobComparer());
  39. }
  40. scoped_refptr<base::TaskRunner> task_runner;
  41. if (task_runner_for_testing_) {
  42. task_runner = task_runner_for_testing_;
  43. } else {
  44. task_runner = base::ThreadPool::CreateSequencedTaskRunner(task_traits_);
  45. }
  46. task_runner->PostTaskAndReply(
  47. from_here,
  48. base::BindOnce(&PrioritizedTaskRunner::RunTaskAndPostReply, this),
  49. base::BindOnce(&PrioritizedTaskRunner::RunReply, this));
  50. }
  51. PrioritizedTaskRunner::~PrioritizedTaskRunner() = default;
  52. void PrioritizedTaskRunner::RunTaskAndPostReply() {
  53. // Find the next job to run.
  54. Job job;
  55. {
  56. base::AutoLock lock(task_job_heap_lock_);
  57. std::pop_heap(task_job_heap_.begin(), task_job_heap_.end(), JobComparer());
  58. job = std::move(task_job_heap_.back());
  59. task_job_heap_.pop_back();
  60. }
  61. std::move(job.task).Run();
  62. // Add the job to the reply priority queue.
  63. base::AutoLock reply_lock(reply_job_heap_lock_);
  64. reply_job_heap_.push_back(std::move(job));
  65. std::push_heap(reply_job_heap_.begin(), reply_job_heap_.end(), JobComparer());
  66. }
  67. void PrioritizedTaskRunner::RunReply() {
  68. // Find the next job to run.
  69. Job job;
  70. {
  71. base::AutoLock lock(reply_job_heap_lock_);
  72. std::pop_heap(reply_job_heap_.begin(), reply_job_heap_.end(),
  73. JobComparer());
  74. job = std::move(reply_job_heap_.back());
  75. reply_job_heap_.pop_back();
  76. }
  77. // Run the job.
  78. std::move(job.reply).Run();
  79. }
  80. } // namespace net