OLD | NEW |
(Empty) | |
| 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 |
| 11 #include "webrtc/test/single_threaded_task_queue.h" |
| 12 |
| 13 #include <utility> |
| 14 |
| 15 #include "webrtc/rtc_base/checks.h" |
| 16 #include "webrtc/rtc_base/ptr_util.h" |
| 17 #include "webrtc/rtc_base/safe_conversions.h" |
| 18 #include "webrtc/rtc_base/timeutils.h" |
| 19 |
| 20 namespace webrtc { |
| 21 namespace test { |
| 22 |
| 23 SingleThreadedTaskQueueForTesting::QueuedTask::QueuedTask( |
| 24 SingleThreadedTaskQueueForTesting::TaskId task_id, |
| 25 int64_t earliest_execution_time, |
| 26 SingleThreadedTaskQueueForTesting::Task task) |
| 27 : task_id(task_id), |
| 28 earliest_execution_time(earliest_execution_time), |
| 29 task(task) {} |
| 30 |
| 31 SingleThreadedTaskQueueForTesting::QueuedTask::~QueuedTask() = default; |
| 32 |
| 33 SingleThreadedTaskQueueForTesting::SingleThreadedTaskQueueForTesting( |
| 34 const char* name) |
| 35 : thread_(Run, this, name), |
| 36 running_(true), |
| 37 next_task_id_(0), |
| 38 wake_up_(false, false) { |
| 39 thread_.Start(); |
| 40 } |
| 41 |
| 42 SingleThreadedTaskQueueForTesting::~SingleThreadedTaskQueueForTesting() { |
| 43 RTC_DCHECK_RUN_ON(&owner_thread_checker_); |
| 44 { |
| 45 rtc::CritScope lock(&cs_); |
| 46 running_ = false; |
| 47 } |
| 48 wake_up_.Set(); |
| 49 thread_.Stop(); |
| 50 } |
| 51 |
| 52 SingleThreadedTaskQueueForTesting::TaskId |
| 53 SingleThreadedTaskQueueForTesting::PostTask(Task task) { |
| 54 return PostDelayedTask(task, 0); |
| 55 } |
| 56 |
| 57 SingleThreadedTaskQueueForTesting::TaskId |
| 58 SingleThreadedTaskQueueForTesting::PostDelayedTask(Task task, |
| 59 int64_t delay_ms) { |
| 60 int64_t earliest_exec_time = rtc::TimeAfter(delay_ms); |
| 61 |
| 62 rtc::CritScope lock(&cs_); |
| 63 |
| 64 TaskId id = next_task_id_++; |
| 65 |
| 66 // Insert after any other tasks with an earlier-or-equal target time. |
| 67 auto it = tasks_.begin(); |
| 68 for (; it != tasks_.end(); it++) { |
| 69 if (earliest_exec_time < (*it)->earliest_execution_time) { |
| 70 break; |
| 71 } |
| 72 } |
| 73 tasks_.insert(it, rtc::MakeUnique<QueuedTask>(id, earliest_exec_time, task)); |
| 74 |
| 75 // This class is optimized for simplicty, not for performance. This will wake |
| 76 // the thread up even if the next task in the queue is only scheduled for |
| 77 // quite some time from now. In that case, the thread will just send itself |
| 78 // back to sleep. |
| 79 wake_up_.Set(); |
| 80 |
| 81 return id; |
| 82 } |
| 83 |
| 84 void SingleThreadedTaskQueueForTesting::SendTask(Task task) { |
| 85 rtc::Event done(true, false); |
| 86 PostTask([&task, &done]() { |
| 87 task(); |
| 88 done.Set(); |
| 89 }); |
| 90 done.Wait(rtc::Event::kForever); |
| 91 } |
| 92 |
| 93 bool SingleThreadedTaskQueueForTesting::CancelTask(TaskId task_id) { |
| 94 rtc::CritScope lock(&cs_); |
| 95 for (auto it = tasks_.begin(); it != tasks_.end(); it++) { |
| 96 if ((*it)->task_id == task_id) { |
| 97 tasks_.erase(it); |
| 98 return true; |
| 99 } |
| 100 } |
| 101 return false; |
| 102 } |
| 103 |
| 104 void SingleThreadedTaskQueueForTesting::Run(void* obj) { |
| 105 static_cast<SingleThreadedTaskQueueForTesting*>(obj)->RunLoop(); |
| 106 } |
| 107 |
| 108 void SingleThreadedTaskQueueForTesting::RunLoop() { |
| 109 while (true) { |
| 110 std::unique_ptr<QueuedTask> queued_task; |
| 111 |
| 112 // An empty queue would lead to sleeping until the queue becoems non-empty. |
| 113 // A queue where the earliest task is shceduled for later than now, will |
| 114 // lead to sleeping until the time of the next scheduled task (or until |
| 115 // more tasks are scheduled). |
| 116 int wait_time = rtc::Event::kForever; |
| 117 |
| 118 { |
| 119 rtc::CritScope lock(&cs_); |
| 120 if (!running_) { |
| 121 return; |
| 122 } |
| 123 if (!tasks_.empty()) { |
| 124 int64_t remaining_delay_ms = rtc::TimeDiff( |
| 125 tasks_.front()->earliest_execution_time, rtc::TimeMillis()); |
| 126 if (remaining_delay_ms <= 0) { |
| 127 queued_task = std::move(tasks_.front()); |
| 128 tasks_.pop_front(); |
| 129 } else { |
| 130 wait_time = rtc::saturated_cast<int>(remaining_delay_ms); |
| 131 } |
| 132 } |
| 133 } |
| 134 |
| 135 if (queued_task) { |
| 136 queued_task->task(); |
| 137 } else { |
| 138 wake_up_.Wait(wait_time); |
| 139 } |
| 140 } |
| 141 } |
| 142 |
| 143 } // namespace test |
| 144 } // namespace webrtc |
OLD | NEW |