blob: 3cba11748c8018601d689f44017b0894d4846bc5 [file] [log] [blame]
eladalon413ee9a2017-08-22 04:02:52 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "test/single_threaded_task_queue.h"
eladalon413ee9a2017-08-22 04:02:52 -070012
13#include <utility>
14
Karl Wiberg918f50c2018-07-05 11:40:33 +020015#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010017#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/time_utils.h"
eladalon413ee9a2017-08-22 04:02:52 -070019
20namespace webrtc {
21namespace test {
22
23SingleThreadedTaskQueueForTesting::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
31SingleThreadedTaskQueueForTesting::QueuedTask::~QueuedTask() = default;
32
33SingleThreadedTaskQueueForTesting::SingleThreadedTaskQueueForTesting(
34 const char* name)
Niels Möllerc572ff32018-11-07 08:43:50 +010035 : thread_(Run, this, name), running_(true), next_task_id_(0) {
eladalon413ee9a2017-08-22 04:02:52 -070036 thread_.Start();
37}
38
39SingleThreadedTaskQueueForTesting::~SingleThreadedTaskQueueForTesting() {
40 RTC_DCHECK_RUN_ON(&owner_thread_checker_);
41 {
42 rtc::CritScope lock(&cs_);
43 running_ = false;
44 }
45 wake_up_.Set();
46 thread_.Stop();
47}
48
49SingleThreadedTaskQueueForTesting::TaskId
50SingleThreadedTaskQueueForTesting::PostTask(Task task) {
51 return PostDelayedTask(task, 0);
52}
53
54SingleThreadedTaskQueueForTesting::TaskId
55SingleThreadedTaskQueueForTesting::PostDelayedTask(Task task,
56 int64_t delay_ms) {
57 int64_t earliest_exec_time = rtc::TimeAfter(delay_ms);
58
59 rtc::CritScope lock(&cs_);
60
61 TaskId id = next_task_id_++;
62
63 // Insert after any other tasks with an earlier-or-equal target time.
64 auto it = tasks_.begin();
65 for (; it != tasks_.end(); it++) {
66 if (earliest_exec_time < (*it)->earliest_execution_time) {
67 break;
68 }
69 }
Karl Wiberg918f50c2018-07-05 11:40:33 +020070 tasks_.insert(it,
71 absl::make_unique<QueuedTask>(id, earliest_exec_time, task));
eladalon413ee9a2017-08-22 04:02:52 -070072
73 // This class is optimized for simplicty, not for performance. This will wake
74 // the thread up even if the next task in the queue is only scheduled for
75 // quite some time from now. In that case, the thread will just send itself
76 // back to sleep.
77 wake_up_.Set();
78
79 return id;
80}
81
82void SingleThreadedTaskQueueForTesting::SendTask(Task task) {
Tommi6e4791f2019-08-14 23:05:44 +020083 RTC_DCHECK(!IsCurrent());
Niels Möllerc572ff32018-11-07 08:43:50 +010084 rtc::Event done;
eladalon413ee9a2017-08-22 04:02:52 -070085 PostTask([&task, &done]() {
86 task();
87 done.Set();
88 });
Tommi6e4791f2019-08-14 23:05:44 +020089 // Give up after 30 seconds, warn after 10.
90 RTC_CHECK(done.Wait(30000, 10000));
eladalon413ee9a2017-08-22 04:02:52 -070091}
92
93bool 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
Tommi6e4791f2019-08-14 23:05:44 +0200104bool SingleThreadedTaskQueueForTesting::IsCurrent() {
105 return rtc::IsThreadRefEqual(thread_.GetThreadRef(), rtc::CurrentThreadRef());
106}
107
eladalon413ee9a2017-08-22 04:02:52 -0700108void SingleThreadedTaskQueueForTesting::Run(void* obj) {
109 static_cast<SingleThreadedTaskQueueForTesting*>(obj)->RunLoop();
110}
111
112void SingleThreadedTaskQueueForTesting::RunLoop() {
113 while (true) {
114 std::unique_ptr<QueuedTask> queued_task;
115
116 // An empty queue would lead to sleeping until the queue becoems non-empty.
Mirko Bonadeidca82bc2017-12-13 18:44:59 +0100117 // A queue where the earliest task is scheduled for later than now, will
eladalon413ee9a2017-08-22 04:02:52 -0700118 // lead to sleeping until the time of the next scheduled task (or until
119 // more tasks are scheduled).
120 int wait_time = rtc::Event::kForever;
121
122 {
123 rtc::CritScope lock(&cs_);
124 if (!running_) {
125 return;
126 }
127 if (!tasks_.empty()) {
128 int64_t remaining_delay_ms = rtc::TimeDiff(
129 tasks_.front()->earliest_execution_time, rtc::TimeMillis());
130 if (remaining_delay_ms <= 0) {
131 queued_task = std::move(tasks_.front());
132 tasks_.pop_front();
133 } else {
134 wait_time = rtc::saturated_cast<int>(remaining_delay_ms);
135 }
136 }
137 }
138
139 if (queued_task) {
140 queued_task->task();
141 } else {
142 wake_up_.Wait(wait_time);
143 }
144 }
145}
146
147} // namespace test
148} // namespace webrtc