niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
xians@webrtc.org | 6bde7a8 | 2012-02-20 08:39:25 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/utility/source/process_thread_impl.h" |
asapersson@webrtc.org | 8b2ec15 | 2014-04-11 07:59:43 +0000 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <string> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "modules/include/module.h" |
| 16 | #include "rtc_base/checks.h" |
Danil Chapovalov | 14273de | 2020-02-27 13:37:43 +0100 | [diff] [blame] | 17 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/trace_event.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 22 | namespace { |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 23 | |
| 24 | // We use this constant internally to signal that a module has requested |
| 25 | // a callback right away. When this is set, no call to TimeUntilNextProcess |
| 26 | // should be made, but Process() should be called directly. |
| 27 | const int64_t kCallProcessImmediately = -1; |
| 28 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 29 | int64_t GetNextCallbackTime(Module* module, int64_t time_now) { |
| 30 | int64_t interval = module->TimeUntilNextProcess(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 31 | if (interval < 0) { |
pbos | 9e260f1 | 2015-08-20 01:23:48 -0700 | [diff] [blame] | 32 | // Falling behind, we should call the callback now. |
| 33 | return time_now; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 34 | } |
| 35 | return time_now + interval; |
| 36 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 37 | } // namespace |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 39 | ProcessThread::~ProcessThread() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 41 | // static |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 42 | std::unique_ptr<ProcessThread> ProcessThread::Create(const char* thread_name) { |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 43 | return std::unique_ptr<ProcessThread>(new ProcessThreadImpl(thread_name)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | } |
| 45 | |
stefan | 847855b | 2015-09-11 09:52:15 -0700 | [diff] [blame] | 46 | ProcessThreadImpl::ProcessThreadImpl(const char* thread_name) |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 47 | : stop_(false), thread_name_(thread_name) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 49 | ProcessThreadImpl::~ProcessThreadImpl() { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 50 | RTC_DCHECK(thread_checker_.IsCurrent()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 51 | RTC_DCHECK(!stop_); |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 52 | |
Danil Chapovalov | 14273de | 2020-02-27 13:37:43 +0100 | [diff] [blame] | 53 | while (!delayed_tasks_.empty()) { |
| 54 | delete delayed_tasks_.top().task; |
| 55 | delayed_tasks_.pop(); |
| 56 | } |
| 57 | |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 58 | while (!queue_.empty()) { |
| 59 | delete queue_.front(); |
| 60 | queue_.pop(); |
| 61 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Danil Chapovalov | 14273de | 2020-02-27 13:37:43 +0100 | [diff] [blame] | 64 | void ProcessThreadImpl::Delete() { |
| 65 | RTC_LOG(LS_WARNING) << "Process thread " << thread_name_ |
| 66 | << " is destroyed as a TaskQueue."; |
| 67 | Stop(); |
| 68 | delete this; |
| 69 | } |
| 70 | |
Niels Möller | 2bfddf7 | 2021-02-22 10:36:29 +0100 | [diff] [blame] | 71 | // Doesn't need locking, because the contending thread isn't running. |
| 72 | void ProcessThreadImpl::Start() RTC_NO_THREAD_SAFETY_ANALYSIS { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 73 | RTC_DCHECK(thread_checker_.IsCurrent()); |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 74 | RTC_DCHECK(thread_.empty()); |
| 75 | if (!thread_.empty()) |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 76 | return; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 77 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 78 | RTC_DCHECK(!stop_); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 79 | |
tommi | 0a2391f | 2017-03-21 02:31:51 -0700 | [diff] [blame] | 80 | for (ModuleCallback& m : modules_) |
| 81 | m.module->ProcessThreadAttached(this); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 82 | |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 83 | thread_ = rtc::PlatformThread::SpawnJoinable( |
| 84 | [this] { |
| 85 | CurrentTaskQueueSetter set_current(this); |
| 86 | while (Process()) { |
| 87 | } |
| 88 | }, |
| 89 | thread_name_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | } |
| 91 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 92 | void ProcessThreadImpl::Stop() { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 93 | RTC_DCHECK(thread_checker_.IsCurrent()); |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 94 | if (thread_.empty()) |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 95 | return; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 96 | |
| 97 | { |
Niels Möller | 2bfddf7 | 2021-02-22 10:36:29 +0100 | [diff] [blame] | 98 | // Need to take lock, for synchronization with `thread_`. |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 99 | MutexLock lock(&mutex_); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 100 | stop_ = true; |
| 101 | } |
| 102 | |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 103 | wake_up_.Set(); |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 104 | thread_.Finalize(); |
Niels Möller | 2bfddf7 | 2021-02-22 10:36:29 +0100 | [diff] [blame] | 105 | |
| 106 | StopNoLocks(); |
| 107 | } |
| 108 | |
| 109 | // No locking needed, since this is called after the contending thread is |
| 110 | // stopped. |
| 111 | void ProcessThreadImpl::StopNoLocks() RTC_NO_THREAD_SAFETY_ANALYSIS { |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 112 | RTC_DCHECK(thread_.empty()); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 113 | stop_ = false; |
| 114 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 115 | for (ModuleCallback& m : modules_) |
| 116 | m.module->ProcessThreadAttached(nullptr); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | } |
| 118 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 119 | void ProcessThreadImpl::WakeUp(Module* module) { |
| 120 | // Allowed to be called on any thread. |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 121 | auto holds_mutex = [this] { |
| 122 | if (!IsCurrent()) { |
| 123 | return false; |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 124 | } |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 125 | RTC_DCHECK_RUN_ON(this); |
| 126 | return holds_mutex_; |
| 127 | }; |
| 128 | if (holds_mutex()) { |
| 129 | // Avoid locking if called on the ProcessThread, via a module's Process), |
| 130 | WakeUpNoLocks(module); |
| 131 | } else { |
| 132 | MutexLock lock(&mutex_); |
| 133 | WakeUpInternal(module); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 134 | } |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 135 | wake_up_.Set(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 138 | // Must be called only indirectly from Process, which already holds the lock. |
| 139 | void ProcessThreadImpl::WakeUpNoLocks(Module* module) |
| 140 | RTC_NO_THREAD_SAFETY_ANALYSIS { |
| 141 | RTC_DCHECK_RUN_ON(this); |
| 142 | WakeUpInternal(module); |
| 143 | } |
| 144 | |
| 145 | void ProcessThreadImpl::WakeUpInternal(Module* module) { |
| 146 | for (ModuleCallback& m : modules_) { |
| 147 | if (m.module == module) |
| 148 | m.next_callback = kCallProcessImmediately; |
| 149 | } |
| 150 | } |
| 151 | |
Danil Chapovalov | 959e9b6 | 2019-01-14 14:29:18 +0100 | [diff] [blame] | 152 | void ProcessThreadImpl::PostTask(std::unique_ptr<QueuedTask> task) { |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 153 | // Allowed to be called on any thread, except from a module's Process method. |
| 154 | if (IsCurrent()) { |
| 155 | RTC_DCHECK_RUN_ON(this); |
| 156 | RTC_DCHECK(!holds_mutex_) << "Calling ProcessThread::PostTask from " |
| 157 | "Module::Process is not supported"; |
| 158 | } |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 159 | { |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 160 | MutexLock lock(&mutex_); |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 161 | queue_.push(task.release()); |
| 162 | } |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 163 | wake_up_.Set(); |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Danil Chapovalov | 14273de | 2020-02-27 13:37:43 +0100 | [diff] [blame] | 166 | void ProcessThreadImpl::PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 167 | uint32_t milliseconds) { |
| 168 | int64_t run_at_ms = rtc::TimeMillis() + milliseconds; |
| 169 | bool recalculate_wakeup_time; |
| 170 | { |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 171 | MutexLock lock(&mutex_); |
Danil Chapovalov | 14273de | 2020-02-27 13:37:43 +0100 | [diff] [blame] | 172 | recalculate_wakeup_time = |
| 173 | delayed_tasks_.empty() || run_at_ms < delayed_tasks_.top().run_at_ms; |
| 174 | delayed_tasks_.emplace(run_at_ms, std::move(task)); |
| 175 | } |
| 176 | if (recalculate_wakeup_time) { |
| 177 | wake_up_.Set(); |
| 178 | } |
| 179 | } |
| 180 | |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 181 | void ProcessThreadImpl::RegisterModule(Module* module, |
| 182 | const rtc::Location& from) { |
Markus Handell | fccb052 | 2021-06-02 13:28:10 +0200 | [diff] [blame] | 183 | TRACE_EVENT0("webrtc", "ProcessThreadImpl::RegisterModule"); |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 184 | RTC_DCHECK(thread_checker_.IsCurrent()); |
tommi | cf39dd5 | 2017-07-07 16:24:34 -0700 | [diff] [blame] | 185 | RTC_DCHECK(module) << from.ToString(); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 186 | |
kwiberg | 5377bc7 | 2016-10-04 13:46:56 -0700 | [diff] [blame] | 187 | #if RTC_DCHECK_IS_ON |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 188 | { |
| 189 | // Catch programmer error. |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 190 | MutexLock lock(&mutex_); |
tommi | cf39dd5 | 2017-07-07 16:24:34 -0700 | [diff] [blame] | 191 | for (const ModuleCallback& mc : modules_) { |
| 192 | RTC_DCHECK(mc.module != module) |
Jonas Olsson | b2b2031 | 2020-01-14 12:11:31 +0100 | [diff] [blame] | 193 | << "Already registered here: " << mc.location.ToString() |
| 194 | << "\n" |
| 195 | "Now attempting from here: " |
| 196 | << from.ToString(); |
tommi | cf39dd5 | 2017-07-07 16:24:34 -0700 | [diff] [blame] | 197 | } |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 198 | } |
| 199 | #endif |
| 200 | |
| 201 | // Now that we know the module isn't in the list, we'll call out to notify |
| 202 | // the module that it's attached to the worker thread. We don't hold |
| 203 | // the lock while we make this call. |
Markus Handell | ad5037b | 2021-05-07 15:02:36 +0200 | [diff] [blame] | 204 | if (!thread_.empty()) |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 205 | module->ProcessThreadAttached(this); |
| 206 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 207 | { |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 208 | MutexLock lock(&mutex_); |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 209 | modules_.push_back(ModuleCallback(module, from)); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 210 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 211 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 212 | // Wake the thread calling ProcessThreadImpl::Process() to update the |
| 213 | // waiting time. The waiting time for the just registered module may be |
| 214 | // shorter than all other registered modules. |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 215 | wake_up_.Set(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 216 | } |
| 217 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 218 | void ProcessThreadImpl::DeRegisterModule(Module* module) { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 219 | RTC_DCHECK(thread_checker_.IsCurrent()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 220 | RTC_DCHECK(module); |
Tommi | 7f375f0 | 2015-04-02 14:50:21 +0000 | [diff] [blame] | 221 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 222 | { |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 223 | MutexLock lock(&mutex_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 224 | modules_.remove_if( |
| 225 | [&module](const ModuleCallback& m) { return m.module == module; }); |
Tommi | 7f375f0 | 2015-04-02 14:50:21 +0000 | [diff] [blame] | 226 | } |
tommi | 0a2391f | 2017-03-21 02:31:51 -0700 | [diff] [blame] | 227 | |
| 228 | // Notify the module that it's been detached. |
| 229 | module->ProcessThreadAttached(nullptr); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 230 | } |
| 231 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 232 | bool ProcessThreadImpl::Process() { |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 233 | TRACE_EVENT1("webrtc", "ProcessThreadImpl", "name", thread_name_); |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 234 | int64_t now = rtc::TimeMillis(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 235 | int64_t next_checkpoint = now + (1000 * 60); |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 236 | RTC_DCHECK_RUN_ON(this); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 237 | { |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 238 | MutexLock lock(&mutex_); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 239 | if (stop_) |
| 240 | return false; |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 241 | for (ModuleCallback& m : modules_) { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 242 | // TODO(tommi): Would be good to measure the time TimeUntilNextProcess |
| 243 | // takes and dcheck if it takes too long (e.g. >=10ms). Ideally this |
| 244 | // operation should not require taking a lock, so querying all modules |
| 245 | // should run in a matter of nanoseconds. |
| 246 | if (m.next_callback == 0) |
| 247 | m.next_callback = GetNextCallbackTime(m.module, now); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 248 | |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 249 | // Set to true for the duration of the calls to modules' Process(). |
| 250 | holds_mutex_ = true; |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 251 | if (m.next_callback <= now || |
| 252 | m.next_callback == kCallProcessImmediately) { |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 253 | { |
| 254 | TRACE_EVENT2("webrtc", "ModuleProcess", "function", |
| 255 | m.location.function_name(), "file", |
Steve Anton | c5d7c52 | 2019-12-03 10:14:05 -0800 | [diff] [blame] | 256 | m.location.file_name()); |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 257 | m.module->Process(); |
| 258 | } |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 259 | // Use a new 'now' reference to calculate when the next callback |
| 260 | // should occur. We'll continue to use 'now' above for the baseline |
| 261 | // of calculating how long we should wait, to reduce variance. |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 262 | int64_t new_now = rtc::TimeMillis(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 263 | m.next_callback = GetNextCallbackTime(m.module, new_now); |
| 264 | } |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 265 | holds_mutex_ = false; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 266 | |
| 267 | if (m.next_callback < next_checkpoint) |
| 268 | next_checkpoint = m.next_callback; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 269 | } |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 270 | |
Danil Chapovalov | 14273de | 2020-02-27 13:37:43 +0100 | [diff] [blame] | 271 | while (!delayed_tasks_.empty() && delayed_tasks_.top().run_at_ms <= now) { |
| 272 | queue_.push(delayed_tasks_.top().task); |
| 273 | delayed_tasks_.pop(); |
| 274 | } |
| 275 | |
| 276 | if (!delayed_tasks_.empty()) { |
| 277 | next_checkpoint = |
| 278 | std::min(next_checkpoint, delayed_tasks_.top().run_at_ms); |
| 279 | } |
| 280 | |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 281 | while (!queue_.empty()) { |
Danil Chapovalov | 959e9b6 | 2019-01-14 14:29:18 +0100 | [diff] [blame] | 282 | QueuedTask* task = queue_.front(); |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 283 | queue_.pop(); |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 284 | mutex_.Unlock(); |
Danil Chapovalov | 14273de | 2020-02-27 13:37:43 +0100 | [diff] [blame] | 285 | if (task->Run()) { |
| 286 | delete task; |
| 287 | } |
Niels Möller | 4785402 | 2021-03-01 11:31:33 +0100 | [diff] [blame] | 288 | mutex_.Lock(); |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 289 | } |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 292 | int64_t time_to_wait = next_checkpoint - rtc::TimeMillis(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 293 | if (time_to_wait > 0) |
Niels Möller | 2c16cc6 | 2018-10-29 09:47:51 +0100 | [diff] [blame] | 294 | wake_up_.Wait(static_cast<int>(time_to_wait)); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 295 | |
| 296 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 297 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 298 | } // namespace webrtc |