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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/include/module.h" |
| 14 | #include "rtc_base/checks.h" |
| 15 | #include "rtc_base/task_queue.h" |
| 16 | #include "rtc_base/timeutils.h" |
| 17 | #include "rtc_base/trace_event.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 20 | namespace { |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 21 | |
| 22 | // We use this constant internally to signal that a module has requested |
| 23 | // a callback right away. When this is set, no call to TimeUntilNextProcess |
| 24 | // should be made, but Process() should be called directly. |
| 25 | const int64_t kCallProcessImmediately = -1; |
| 26 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 27 | int64_t GetNextCallbackTime(Module* module, int64_t time_now) { |
| 28 | int64_t interval = module->TimeUntilNextProcess(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 29 | if (interval < 0) { |
pbos | 9e260f1 | 2015-08-20 01:23:48 -0700 | [diff] [blame] | 30 | // Falling behind, we should call the callback now. |
| 31 | return time_now; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 32 | } |
| 33 | return time_now + interval; |
| 34 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 35 | } // namespace |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 37 | ProcessThread::~ProcessThread() {} |
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 | // static |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 40 | std::unique_ptr<ProcessThread> ProcessThread::Create(const char* thread_name) { |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 41 | return std::unique_ptr<ProcessThread>(new ProcessThreadImpl(thread_name)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | } |
| 43 | |
stefan | 847855b | 2015-09-11 09:52:15 -0700 | [diff] [blame] | 44 | ProcessThreadImpl::ProcessThreadImpl(const char* thread_name) |
| 45 | : wake_up_(EventWrapper::Create()), |
| 46 | stop_(false), |
| 47 | 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() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 50 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 51 | RTC_DCHECK(!thread_.get()); |
| 52 | RTC_DCHECK(!stop_); |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 53 | |
| 54 | while (!queue_.empty()) { |
| 55 | delete queue_.front(); |
| 56 | queue_.pop(); |
| 57 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | } |
| 59 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 60 | void ProcessThreadImpl::Start() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 61 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 62 | RTC_DCHECK(!thread_.get()); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 63 | if (thread_.get()) |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 64 | return; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 65 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 66 | RTC_DCHECK(!stop_); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 67 | |
tommi | 0a2391f | 2017-03-21 02:31:51 -0700 | [diff] [blame] | 68 | for (ModuleCallback& m : modules_) |
| 69 | m.module->ProcessThreadAttached(this); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 70 | |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 71 | thread_.reset( |
| 72 | new rtc::PlatformThread(&ProcessThreadImpl::Run, this, thread_name_)); |
| 73 | thread_->Start(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | } |
| 75 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 76 | void ProcessThreadImpl::Stop() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 77 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 78 | if (!thread_.get()) |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 79 | return; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 80 | |
| 81 | { |
| 82 | rtc::CritScope lock(&lock_); |
| 83 | stop_ = true; |
| 84 | } |
| 85 | |
| 86 | wake_up_->Set(); |
| 87 | |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 88 | thread_->Stop(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 89 | stop_ = false; |
| 90 | |
Tommi | 7f375f0 | 2015-04-02 14:50:21 +0000 | [diff] [blame] | 91 | thread_.reset(); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 92 | for (ModuleCallback& m : modules_) |
| 93 | m.module->ProcessThreadAttached(nullptr); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | } |
| 95 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 96 | void ProcessThreadImpl::WakeUp(Module* module) { |
| 97 | // Allowed to be called on any thread. |
| 98 | { |
| 99 | rtc::CritScope lock(&lock_); |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 100 | for (ModuleCallback& m : modules_) { |
| 101 | if (m.module == module) |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 102 | m.next_callback = kCallProcessImmediately; |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 103 | } |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 104 | } |
| 105 | wake_up_->Set(); |
| 106 | } |
| 107 | |
tommi | 435f98b | 2016-05-28 14:57:15 -0700 | [diff] [blame] | 108 | void ProcessThreadImpl::PostTask(std::unique_ptr<rtc::QueuedTask> task) { |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 109 | // Allowed to be called on any thread. |
| 110 | { |
| 111 | rtc::CritScope lock(&lock_); |
| 112 | queue_.push(task.release()); |
| 113 | } |
| 114 | wake_up_->Set(); |
| 115 | } |
| 116 | |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 117 | void ProcessThreadImpl::RegisterModule(Module* module, |
| 118 | const rtc::Location& from) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 119 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
tommi | cf39dd5 | 2017-07-07 16:24:34 -0700 | [diff] [blame] | 120 | RTC_DCHECK(module) << from.ToString(); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 121 | |
kwiberg | 5377bc7 | 2016-10-04 13:46:56 -0700 | [diff] [blame] | 122 | #if RTC_DCHECK_IS_ON |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 123 | { |
| 124 | // Catch programmer error. |
| 125 | rtc::CritScope lock(&lock_); |
tommi | cf39dd5 | 2017-07-07 16:24:34 -0700 | [diff] [blame] | 126 | for (const ModuleCallback& mc : modules_) { |
| 127 | RTC_DCHECK(mc.module != module) |
| 128 | << "Already registered here: " << mc.location.ToString() << "\n" |
| 129 | << "Now attempting from here: " << from.ToString(); |
| 130 | } |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 131 | } |
| 132 | #endif |
| 133 | |
| 134 | // Now that we know the module isn't in the list, we'll call out to notify |
| 135 | // the module that it's attached to the worker thread. We don't hold |
| 136 | // the lock while we make this call. |
| 137 | if (thread_.get()) |
| 138 | module->ProcessThreadAttached(this); |
| 139 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 140 | { |
| 141 | rtc::CritScope lock(&lock_); |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 142 | modules_.push_back(ModuleCallback(module, from)); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 143 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 145 | // Wake the thread calling ProcessThreadImpl::Process() to update the |
| 146 | // waiting time. The waiting time for the just registered module may be |
| 147 | // shorter than all other registered modules. |
| 148 | wake_up_->Set(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 149 | } |
| 150 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 151 | void ProcessThreadImpl::DeRegisterModule(Module* module) { |
tommi | 0a2391f | 2017-03-21 02:31:51 -0700 | [diff] [blame] | 152 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 153 | RTC_DCHECK(module); |
Tommi | 7f375f0 | 2015-04-02 14:50:21 +0000 | [diff] [blame] | 154 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 155 | { |
| 156 | rtc::CritScope lock(&lock_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 157 | modules_.remove_if( |
| 158 | [&module](const ModuleCallback& m) { return m.module == module; }); |
Tommi | 7f375f0 | 2015-04-02 14:50:21 +0000 | [diff] [blame] | 159 | } |
tommi | 0a2391f | 2017-03-21 02:31:51 -0700 | [diff] [blame] | 160 | |
| 161 | // Notify the module that it's been detached. |
| 162 | module->ProcessThreadAttached(nullptr); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 163 | } |
| 164 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 165 | // static |
| 166 | bool ProcessThreadImpl::Run(void* obj) { |
| 167 | return static_cast<ProcessThreadImpl*>(obj)->Process(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | } |
| 169 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 170 | bool ProcessThreadImpl::Process() { |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 171 | TRACE_EVENT1("webrtc", "ProcessThreadImpl", "name", thread_name_); |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 172 | int64_t now = rtc::TimeMillis(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 173 | int64_t next_checkpoint = now + (1000 * 60); |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 174 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 175 | { |
| 176 | rtc::CritScope lock(&lock_); |
| 177 | if (stop_) |
| 178 | return false; |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 179 | for (ModuleCallback& m : modules_) { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 180 | // TODO(tommi): Would be good to measure the time TimeUntilNextProcess |
| 181 | // takes and dcheck if it takes too long (e.g. >=10ms). Ideally this |
| 182 | // operation should not require taking a lock, so querying all modules |
| 183 | // should run in a matter of nanoseconds. |
| 184 | if (m.next_callback == 0) |
| 185 | m.next_callback = GetNextCallbackTime(m.module, now); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 186 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 187 | if (m.next_callback <= now || |
| 188 | m.next_callback == kCallProcessImmediately) { |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 189 | { |
| 190 | TRACE_EVENT2("webrtc", "ModuleProcess", "function", |
| 191 | m.location.function_name(), "file", |
| 192 | m.location.file_and_line()); |
| 193 | m.module->Process(); |
| 194 | } |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 195 | // Use a new 'now' reference to calculate when the next callback |
| 196 | // should occur. We'll continue to use 'now' above for the baseline |
| 197 | // of calculating how long we should wait, to reduce variance. |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 198 | int64_t new_now = rtc::TimeMillis(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 199 | m.next_callback = GetNextCallbackTime(m.module, new_now); |
| 200 | } |
| 201 | |
| 202 | if (m.next_callback < next_checkpoint) |
| 203 | next_checkpoint = m.next_callback; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 204 | } |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 205 | |
| 206 | while (!queue_.empty()) { |
tommi | 435f98b | 2016-05-28 14:57:15 -0700 | [diff] [blame] | 207 | rtc::QueuedTask* task = queue_.front(); |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 208 | queue_.pop(); |
| 209 | lock_.Leave(); |
| 210 | task->Run(); |
| 211 | delete task; |
| 212 | lock_.Enter(); |
| 213 | } |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 216 | int64_t time_to_wait = next_checkpoint - rtc::TimeMillis(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 217 | if (time_to_wait > 0) |
| 218 | wake_up_->Wait(static_cast<unsigned long>(time_to_wait)); |
| 219 | |
| 220 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 221 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 222 | } // namespace webrtc |