You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "test/single_threaded_task_queue.h"
  11. #include <memory>
  12. #include <utility>
  13. #include "rtc_base/checks.h"
  14. #include "rtc_base/numerics/safe_conversions.h"
  15. #include "rtc_base/time_utils.h"
  16. namespace webrtc {
  17. namespace test {
  18. DEPRECATED_SingleThreadedTaskQueueForTesting::StoredTask::StoredTask(
  19. DEPRECATED_SingleThreadedTaskQueueForTesting::TaskId task_id,
  20. std::unique_ptr<QueuedTask> task)
  21. : task_id(task_id), task(std::move(task)) {}
  22. DEPRECATED_SingleThreadedTaskQueueForTesting::StoredTask::~StoredTask() =
  23. default;
  24. DEPRECATED_SingleThreadedTaskQueueForTesting::
  25. DEPRECATED_SingleThreadedTaskQueueForTesting(const char* name)
  26. : thread_(Run, this, name), running_(true), next_task_id_(0) {
  27. thread_.Start();
  28. }
  29. DEPRECATED_SingleThreadedTaskQueueForTesting::
  30. ~DEPRECATED_SingleThreadedTaskQueueForTesting() {
  31. Stop();
  32. }
  33. DEPRECATED_SingleThreadedTaskQueueForTesting::TaskId
  34. DEPRECATED_SingleThreadedTaskQueueForTesting::PostDelayed(
  35. std::unique_ptr<QueuedTask> task,
  36. int64_t delay_ms) {
  37. int64_t earliest_exec_time = rtc::TimeAfter(delay_ms);
  38. rtc::CritScope lock(&cs_);
  39. if (!running_)
  40. return kInvalidTaskId;
  41. TaskId id = next_task_id_++;
  42. // Insert after any other tasks with an earlier-or-equal target time.
  43. // Note: multimap has promise "The order of the key-value pairs whose keys
  44. // compare equivalent is the order of insertion and does not change."
  45. tasks_.emplace(std::piecewise_construct,
  46. std::forward_as_tuple(earliest_exec_time),
  47. std::forward_as_tuple(id, std::move(task)));
  48. // This class is optimized for simplicty, not for performance. This will wake
  49. // the thread up even if the next task in the queue is only scheduled for
  50. // quite some time from now. In that case, the thread will just send itself
  51. // back to sleep.
  52. wake_up_.Set();
  53. return id;
  54. }
  55. bool DEPRECATED_SingleThreadedTaskQueueForTesting::CancelTask(TaskId task_id) {
  56. rtc::CritScope lock(&cs_);
  57. for (auto it = tasks_.begin(); it != tasks_.end(); it++) {
  58. if (it->second.task_id == task_id) {
  59. tasks_.erase(it);
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. bool DEPRECATED_SingleThreadedTaskQueueForTesting::IsCurrent() {
  66. return rtc::IsThreadRefEqual(thread_.GetThreadRef(), rtc::CurrentThreadRef());
  67. }
  68. bool DEPRECATED_SingleThreadedTaskQueueForTesting::IsRunning() {
  69. RTC_DCHECK_RUN_ON(&owner_thread_checker_);
  70. // We could check the |running_| flag here, but this is equivalent for the
  71. // purposes of this function.
  72. return thread_.IsRunning();
  73. }
  74. bool DEPRECATED_SingleThreadedTaskQueueForTesting::HasPendingTasks() const {
  75. rtc::CritScope lock(&cs_);
  76. return !tasks_.empty();
  77. }
  78. void DEPRECATED_SingleThreadedTaskQueueForTesting::Stop() {
  79. RTC_DCHECK_RUN_ON(&owner_thread_checker_);
  80. if (!thread_.IsRunning())
  81. return;
  82. {
  83. rtc::CritScope lock(&cs_);
  84. running_ = false;
  85. }
  86. wake_up_.Set();
  87. thread_.Stop();
  88. }
  89. void DEPRECATED_SingleThreadedTaskQueueForTesting::Run(void* obj) {
  90. static_cast<DEPRECATED_SingleThreadedTaskQueueForTesting*>(obj)->RunLoop();
  91. }
  92. void DEPRECATED_SingleThreadedTaskQueueForTesting::RunLoop() {
  93. CurrentTaskQueueSetter set_current(this);
  94. while (true) {
  95. std::unique_ptr<QueuedTask> queued_task;
  96. // An empty queue would lead to sleeping until the queue becoems non-empty.
  97. // A queue where the earliest task is scheduled for later than now, will
  98. // lead to sleeping until the time of the next scheduled task (or until
  99. // more tasks are scheduled).
  100. int wait_time = rtc::Event::kForever;
  101. {
  102. rtc::CritScope lock(&cs_);
  103. if (!running_) {
  104. return;
  105. }
  106. if (!tasks_.empty()) {
  107. auto next_delayed_task = tasks_.begin();
  108. int64_t earliest_exec_time = next_delayed_task->first;
  109. int64_t remaining_delay_ms =
  110. rtc::TimeDiff(earliest_exec_time, rtc::TimeMillis());
  111. if (remaining_delay_ms <= 0) {
  112. queued_task = std::move(next_delayed_task->second.task);
  113. tasks_.erase(next_delayed_task);
  114. } else {
  115. wait_time = rtc::saturated_cast<int>(remaining_delay_ms);
  116. }
  117. }
  118. }
  119. if (queued_task) {
  120. if (!queued_task->Run()) {
  121. queued_task.release();
  122. }
  123. } else {
  124. wake_up_.Wait(wait_time);
  125. }
  126. }
  127. }
  128. void DEPRECATED_SingleThreadedTaskQueueForTesting::Delete() {
  129. Stop();
  130. delete this;
  131. }
  132. } // namespace test
  133. } // namespace webrtc