Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 11 | #include "rtc_base/task_queue_stdlib.h" |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 12 | |
| 13 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 15 | #include <algorithm> |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 16 | #include <map> |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 17 | #include <memory> |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 18 | #include <queue> |
| 19 | #include <utility> |
| 20 | |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 21 | #include "absl/functional/any_invocable.h" |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 22 | #include "absl/strings/string_view.h" |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 23 | #include "api/task_queue/task_queue_base.h" |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 24 | #include "api/units/time_delta.h" |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 26 | #include "rtc_base/event.h" |
| 27 | #include "rtc_base/logging.h" |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 28 | #include "rtc_base/numerics/divide_round.h" |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 29 | #include "rtc_base/platform_thread.h" |
Markus Handell | 18523c3 | 2020-07-08 17:55:58 +0200 | [diff] [blame] | 30 | #include "rtc_base/synchronization/mutex.h" |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 31 | #include "rtc_base/thread_annotations.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 32 | #include "rtc_base/time_utils.h" |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 33 | |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 34 | namespace webrtc { |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 35 | namespace { |
| 36 | |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 37 | rtc::ThreadPriority TaskQueuePriorityToThreadPriority( |
| 38 | TaskQueueFactory::Priority priority) { |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 39 | switch (priority) { |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 40 | case TaskQueueFactory::Priority::HIGH: |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 41 | return rtc::ThreadPriority::kRealtime; |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 42 | case TaskQueueFactory::Priority::LOW: |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 43 | return rtc::ThreadPriority::kLow; |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 44 | case TaskQueueFactory::Priority::NORMAL: |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 45 | return rtc::ThreadPriority::kNormal; |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 46 | } |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 47 | } |
| 48 | |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 49 | class TaskQueueStdlib final : public TaskQueueBase { |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 50 | public: |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 51 | TaskQueueStdlib(absl::string_view queue_name, rtc::ThreadPriority priority); |
| 52 | ~TaskQueueStdlib() override = default; |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 53 | |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 54 | void Delete() override; |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 55 | void PostTask(absl::AnyInvocable<void() &&> task) override; |
| 56 | void PostDelayedTask(absl::AnyInvocable<void() &&> task, |
| 57 | TimeDelta delay) override; |
| 58 | void PostDelayedHighPrecisionTask(absl::AnyInvocable<void() &&> task, |
| 59 | TimeDelta delay) override; |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 60 | |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 61 | private: |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 62 | using OrderId = uint64_t; |
| 63 | |
| 64 | struct DelayedEntryTimeout { |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 65 | int64_t next_fire_at_us{}; |
| 66 | OrderId order{}; |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 67 | |
| 68 | bool operator<(const DelayedEntryTimeout& o) const { |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 69 | return std::tie(next_fire_at_us, order) < |
| 70 | std::tie(o.next_fire_at_us, o.order); |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 71 | } |
| 72 | }; |
| 73 | |
| 74 | struct NextTask { |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 75 | bool final_task = false; |
| 76 | absl::AnyInvocable<void() &&> run_task; |
Markus Handell | 6df49b9 | 2022-08-17 13:39:59 +0000 | [diff] [blame^] | 77 | // TODO(bugs.webrtc.org/14366): While transitioning to TimeDelta, WebRTC and |
| 78 | // Chromium has a different idea about what type rtc::Event::kForever is. |
| 79 | // Code can't assume rtc::Event::kForever is the same type as timed wait |
| 80 | // arguments. |
| 81 | // Change `sleep_time_ms` to be explicit type, default value |
| 82 | // `rtc::Event::kForever` once transition is complete. |
| 83 | absl::optional<int64_t> sleep_time_ms; |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 84 | }; |
| 85 | |
Tommi | 76d9c18 | 2022-04-22 15:48:37 +0200 | [diff] [blame] | 86 | static rtc::PlatformThread InitializeThread(TaskQueueStdlib* me, |
| 87 | absl::string_view queue_name, |
| 88 | rtc::ThreadPriority priority); |
| 89 | |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 90 | NextTask GetNextTask(); |
| 91 | |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 92 | void ProcessTasks(); |
| 93 | |
| 94 | void NotifyWake(); |
| 95 | |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 96 | // Signaled whenever a new task is pending. |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 97 | rtc::Event flag_notify_; |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 98 | |
Markus Handell | 18523c3 | 2020-07-08 17:55:58 +0200 | [diff] [blame] | 99 | Mutex pending_lock_; |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 100 | |
| 101 | // Indicates if the worker thread needs to shutdown now. |
Tommi | 76d9c18 | 2022-04-22 15:48:37 +0200 | [diff] [blame] | 102 | bool thread_should_quit_ RTC_GUARDED_BY(pending_lock_) = false; |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 103 | |
| 104 | // Holds the next order to use for the next task to be |
| 105 | // put into one of the pending queues. |
Tommi | 76d9c18 | 2022-04-22 15:48:37 +0200 | [diff] [blame] | 106 | OrderId thread_posting_order_ RTC_GUARDED_BY(pending_lock_) = 0; |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 107 | |
| 108 | // The list of all pending tasks that need to be processed in the |
| 109 | // FIFO queue ordering on the worker thread. |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 110 | std::queue<std::pair<OrderId, absl::AnyInvocable<void() &&>>> pending_queue_ |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 111 | RTC_GUARDED_BY(pending_lock_); |
| 112 | |
| 113 | // The list of all pending tasks that need to be processed at a future |
| 114 | // time based upon a delay. On the off change the delayed task should |
| 115 | // happen at exactly the same time interval as another task then the |
| 116 | // task is processed based on FIFO ordering. std::priority_queue was |
| 117 | // considered but rejected due to its inability to extract the |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 118 | // move-only value out of the queue without the presence of a hack. |
| 119 | std::map<DelayedEntryTimeout, absl::AnyInvocable<void() &&>> delayed_queue_ |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 120 | RTC_GUARDED_BY(pending_lock_); |
Markus Handell | 49cb459 | 2021-06-22 10:02:26 +0200 | [diff] [blame] | 121 | |
| 122 | // Contains the active worker thread assigned to processing |
| 123 | // tasks (including delayed tasks). |
| 124 | // Placing this last ensures the thread doesn't touch uninitialized attributes |
| 125 | // throughout it's lifetime. |
| 126 | rtc::PlatformThread thread_; |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 127 | }; |
| 128 | |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 129 | TaskQueueStdlib::TaskQueueStdlib(absl::string_view queue_name, |
| 130 | rtc::ThreadPriority priority) |
Tommi | 76d9c18 | 2022-04-22 15:48:37 +0200 | [diff] [blame] | 131 | : flag_notify_(/*manual_reset=*/false, /*initially_signaled=*/false), |
| 132 | thread_(InitializeThread(this, queue_name, priority)) {} |
| 133 | |
| 134 | // static |
| 135 | rtc::PlatformThread TaskQueueStdlib::InitializeThread( |
| 136 | TaskQueueStdlib* me, |
| 137 | absl::string_view queue_name, |
| 138 | rtc::ThreadPriority priority) { |
| 139 | rtc::Event started; |
| 140 | auto thread = rtc::PlatformThread::SpawnJoinable( |
| 141 | [&started, me] { |
| 142 | CurrentTaskQueueSetter set_current(me); |
| 143 | started.Set(); |
| 144 | me->ProcessTasks(); |
| 145 | }, |
| 146 | queue_name, rtc::ThreadAttributes().SetPriority(priority)); |
| 147 | started.Wait(rtc::Event::kForever); |
| 148 | return thread; |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 149 | } |
| 150 | |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 151 | void TaskQueueStdlib::Delete() { |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 152 | RTC_DCHECK(!IsCurrent()); |
| 153 | |
| 154 | { |
Markus Handell | 18523c3 | 2020-07-08 17:55:58 +0200 | [diff] [blame] | 155 | MutexLock lock(&pending_lock_); |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 156 | thread_should_quit_ = true; |
| 157 | } |
| 158 | |
| 159 | NotifyWake(); |
| 160 | |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 161 | delete this; |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 162 | } |
| 163 | |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 164 | void TaskQueueStdlib::PostTask(absl::AnyInvocable<void() &&> task) { |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 165 | { |
Markus Handell | 18523c3 | 2020-07-08 17:55:58 +0200 | [diff] [blame] | 166 | MutexLock lock(&pending_lock_); |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 167 | pending_queue_.push( |
| 168 | std::make_pair(++thread_posting_order_, std::move(task))); |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | NotifyWake(); |
| 172 | } |
| 173 | |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 174 | void TaskQueueStdlib::PostDelayedTask(absl::AnyInvocable<void() &&> task, |
| 175 | TimeDelta delay) { |
| 176 | DelayedEntryTimeout delayed_entry; |
| 177 | delayed_entry.next_fire_at_us = rtc::TimeMicros() + delay.us(); |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 178 | |
| 179 | { |
Markus Handell | 18523c3 | 2020-07-08 17:55:58 +0200 | [diff] [blame] | 180 | MutexLock lock(&pending_lock_); |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 181 | delayed_entry.order = ++thread_posting_order_; |
| 182 | delayed_queue_[delayed_entry] = std::move(task); |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | NotifyWake(); |
| 186 | } |
| 187 | |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 188 | void TaskQueueStdlib::PostDelayedHighPrecisionTask( |
| 189 | absl::AnyInvocable<void() &&> task, |
| 190 | TimeDelta delay) { |
| 191 | PostDelayedTask(std::move(task), delay); |
| 192 | } |
| 193 | |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 194 | TaskQueueStdlib::NextTask TaskQueueStdlib::GetNextTask() { |
Tommi | 76d9c18 | 2022-04-22 15:48:37 +0200 | [diff] [blame] | 195 | NextTask result; |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 196 | |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 197 | const int64_t tick_us = rtc::TimeMicros(); |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 198 | |
Markus Handell | 18523c3 | 2020-07-08 17:55:58 +0200 | [diff] [blame] | 199 | MutexLock lock(&pending_lock_); |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 200 | |
| 201 | if (thread_should_quit_) { |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 202 | result.final_task = true; |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 203 | return result; |
| 204 | } |
| 205 | |
| 206 | if (delayed_queue_.size() > 0) { |
| 207 | auto delayed_entry = delayed_queue_.begin(); |
| 208 | const auto& delay_info = delayed_entry->first; |
| 209 | auto& delay_run = delayed_entry->second; |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 210 | if (tick_us >= delay_info.next_fire_at_us) { |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 211 | if (pending_queue_.size() > 0) { |
| 212 | auto& entry = pending_queue_.front(); |
| 213 | auto& entry_order = entry.first; |
| 214 | auto& entry_run = entry.second; |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 215 | if (entry_order < delay_info.order) { |
| 216 | result.run_task = std::move(entry_run); |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 217 | pending_queue_.pop(); |
| 218 | return result; |
| 219 | } |
| 220 | } |
| 221 | |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 222 | result.run_task = std::move(delay_run); |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 223 | delayed_queue_.erase(delayed_entry); |
| 224 | return result; |
| 225 | } |
| 226 | |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 227 | result.sleep_time_ms = |
| 228 | DivideRoundUp(delay_info.next_fire_at_us - tick_us, 1'000); |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | if (pending_queue_.size() > 0) { |
| 232 | auto& entry = pending_queue_.front(); |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 233 | result.run_task = std::move(entry.second); |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 234 | pending_queue_.pop(); |
| 235 | } |
| 236 | |
| 237 | return result; |
| 238 | } |
| 239 | |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 240 | void TaskQueueStdlib::ProcessTasks() { |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 241 | while (true) { |
| 242 | auto task = GetNextTask(); |
| 243 | |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 244 | if (task.final_task) |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 245 | break; |
| 246 | |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 247 | if (task.run_task) { |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 248 | // process entry immediately then try again |
Danil Chapovalov | ba57001 | 2022-07-19 18:36:31 +0200 | [diff] [blame] | 249 | std::move(task.run_task)(); |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 250 | |
Tommi | 76d9c18 | 2022-04-22 15:48:37 +0200 | [diff] [blame] | 251 | // Attempt to run more tasks before going to sleep. |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 252 | continue; |
| 253 | } |
| 254 | |
Markus Handell | 6df49b9 | 2022-08-17 13:39:59 +0000 | [diff] [blame^] | 255 | // TODO(bugs.webrtc.org/14366): While transitioning to TimeDelta, WebRTC and |
| 256 | // Chromium has a different idea about what type rtc::Event::kForever is. |
| 257 | // Code can't assume rtc::Event::kForever is the same type as timed wait |
| 258 | // arguments. |
| 259 | // Simplify after transitioning is complete. |
| 260 | if (task.sleep_time_ms.has_value()) |
| 261 | flag_notify_.Wait(task.sleep_time_ms.value()); |
| 262 | else |
| 263 | flag_notify_.Wait(rtc::Event::kForever); |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 264 | } |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 265 | } |
| 266 | |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 267 | void TaskQueueStdlib::NotifyWake() { |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 268 | // The queue holds pending tasks to complete. Either tasks are to be |
| 269 | // executed immediately or tasks are to be run at some future delayed time. |
| 270 | // For immediate tasks the task queue's thread is busy running the task and |
| 271 | // the thread will not be waiting on the flag_notify_ event. If no immediate |
| 272 | // tasks are available but a delayed task is pending then the thread will be |
| 273 | // waiting on flag_notify_ with a delayed time-out of the nearest timed task |
| 274 | // to run. If no immediate or pending tasks are available, the thread will |
| 275 | // wait on flag_notify_ until signaled that a task has been added (or the |
| 276 | // thread to be told to shutdown). |
| 277 | |
| 278 | // In all cases, when a new immediate task, delayed task, or request to |
| 279 | // shutdown the thread is added the flag_notify_ is signaled after. If the |
| 280 | // thread was waiting then the thread will wake up immediately and re-assess |
| 281 | // what task needs to be run next (i.e. run a task now, wait for the nearest |
| 282 | // timed delayed task, or shutdown the thread). If the thread was not waiting |
| 283 | // then the thread will remained signaled to wake up the next time any |
| 284 | // attempt to wait on the flag_notify_ event occurs. |
| 285 | |
| 286 | // Any immediate or delayed pending task (or request to shutdown the thread) |
| 287 | // must always be added to the queue prior to signaling flag_notify_ to wake |
| 288 | // up the possibly sleeping thread. This prevents a race condition where the |
| 289 | // thread is notified to wake up but the task queue's thread finds nothing to |
| 290 | // do so it waits once again to be signaled where such a signal may never |
| 291 | // happen. |
| 292 | flag_notify_.Set(); |
| 293 | } |
| 294 | |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 295 | class TaskQueueStdlibFactory final : public TaskQueueFactory { |
| 296 | public: |
| 297 | std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue( |
| 298 | absl::string_view name, |
| 299 | Priority priority) const override { |
| 300 | return std::unique_ptr<TaskQueueBase, TaskQueueDeleter>( |
| 301 | new TaskQueueStdlib(name, TaskQueuePriorityToThreadPriority(priority))); |
| 302 | } |
| 303 | }; |
| 304 | |
| 305 | } // namespace |
| 306 | |
| 307 | std::unique_ptr<TaskQueueFactory> CreateTaskQueueStdlibFactory() { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 308 | return std::make_unique<TaskQueueStdlibFactory>(); |
Robin Raymond | 22027b9 | 2018-11-23 09:07:50 -0500 | [diff] [blame] | 309 | } |
| 310 | |
Danil Chapovalov | fa52efa | 2019-02-21 11:13:58 +0100 | [diff] [blame] | 311 | } // namespace webrtc |