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 | |
pbos@webrtc.org | 8b06200 | 2013-07-12 08:28:10 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/utility/source/process_thread_impl.h" |
asapersson@webrtc.org | 8b2ec15 | 2014-04-11 07:59:43 +0000 | [diff] [blame] | 12 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 13 | #include "webrtc/base/checks.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 14 | #include "webrtc/modules/include/module.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 15 | #include "webrtc/system_wrappers/include/logging.h" |
| 16 | #include "webrtc/system_wrappers/include/tick_util.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 19 | namespace { |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 20 | |
| 21 | // We use this constant internally to signal that a module has requested |
| 22 | // a callback right away. When this is set, no call to TimeUntilNextProcess |
| 23 | // should be made, but Process() should be called directly. |
| 24 | const int64_t kCallProcessImmediately = -1; |
| 25 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 26 | int64_t GetNextCallbackTime(Module* module, int64_t time_now) { |
| 27 | int64_t interval = module->TimeUntilNextProcess(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 28 | if (interval < 0) { |
pbos | 9e260f1 | 2015-08-20 01:23:48 -0700 | [diff] [blame] | 29 | // Falling behind, we should call the callback now. |
| 30 | return time_now; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 31 | } |
| 32 | return time_now + interval; |
| 33 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | } |
| 35 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 36 | ProcessThread::~ProcessThread() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 38 | // static |
kwiberg | 2c27a06 | 2016-04-26 08:37:53 -0700 | [diff] [blame] | 39 | std::unique_ptr<ProcessThread> ProcessThread::Create( |
stefan | 847855b | 2015-09-11 09:52:15 -0700 | [diff] [blame] | 40 | const char* thread_name) { |
kwiberg | 2c27a06 | 2016-04-26 08:37:53 -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 | 842a4a6 | 2015-03-30 14:16:12 +0000 | [diff] [blame] | 68 | { |
| 69 | // TODO(tommi): Since DeRegisterModule is currently being called from |
| 70 | // different threads in some cases (ChannelOwner), we need to lock access to |
| 71 | // the modules_ collection even on the controller thread. |
| 72 | // Once we've cleaned up those places, we can remove this lock. |
| 73 | rtc::CritScope lock(&lock_); |
| 74 | for (ModuleCallback& m : modules_) |
| 75 | m.module->ProcessThreadAttached(this); |
| 76 | } |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 77 | |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 78 | thread_.reset( |
| 79 | new rtc::PlatformThread(&ProcessThreadImpl::Run, this, thread_name_)); |
| 80 | thread_->Start(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | } |
| 82 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 83 | void ProcessThreadImpl::Stop() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 84 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 85 | if(!thread_.get()) |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 86 | return; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 87 | |
| 88 | { |
| 89 | rtc::CritScope lock(&lock_); |
| 90 | stop_ = true; |
| 91 | } |
| 92 | |
| 93 | wake_up_->Set(); |
| 94 | |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 95 | thread_->Stop(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 96 | stop_ = false; |
| 97 | |
Tommi | 842a4a6 | 2015-03-30 14:16:12 +0000 | [diff] [blame] | 98 | // TODO(tommi): Since DeRegisterModule is currently being called from |
| 99 | // different threads in some cases (ChannelOwner), we need to lock access to |
| 100 | // the modules_ collection even on the controller thread. |
Tommi | 7f375f0 | 2015-04-02 14:50:21 +0000 | [diff] [blame] | 101 | // Since DeRegisterModule also checks thread_, we also need to hold the |
| 102 | // lock for the .reset() operation. |
Tommi | 842a4a6 | 2015-03-30 14:16:12 +0000 | [diff] [blame] | 103 | // Once we've cleaned up those places, we can remove this lock. |
| 104 | rtc::CritScope lock(&lock_); |
Tommi | 7f375f0 | 2015-04-02 14:50:21 +0000 | [diff] [blame] | 105 | thread_.reset(); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 106 | for (ModuleCallback& m : modules_) |
| 107 | m.module->ProcessThreadAttached(nullptr); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 108 | } |
| 109 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 110 | void ProcessThreadImpl::WakeUp(Module* module) { |
| 111 | // Allowed to be called on any thread. |
| 112 | { |
| 113 | rtc::CritScope lock(&lock_); |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 114 | for (ModuleCallback& m : modules_) { |
| 115 | if (m.module == module) |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 116 | m.next_callback = kCallProcessImmediately; |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 117 | } |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 118 | } |
| 119 | wake_up_->Set(); |
| 120 | } |
| 121 | |
kwiberg | 2c27a06 | 2016-04-26 08:37:53 -0700 | [diff] [blame] | 122 | void ProcessThreadImpl::PostTask(std::unique_ptr<ProcessTask> task) { |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 123 | // Allowed to be called on any thread. |
| 124 | { |
| 125 | rtc::CritScope lock(&lock_); |
| 126 | queue_.push(task.release()); |
| 127 | } |
| 128 | wake_up_->Set(); |
| 129 | } |
| 130 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 131 | void ProcessThreadImpl::RegisterModule(Module* module) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 132 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 133 | RTC_DCHECK(module); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 134 | |
| 135 | #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
| 136 | { |
| 137 | // Catch programmer error. |
| 138 | rtc::CritScope lock(&lock_); |
| 139 | for (const ModuleCallback& mc : modules_) |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 140 | RTC_DCHECK(mc.module != module); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 141 | } |
| 142 | #endif |
| 143 | |
| 144 | // Now that we know the module isn't in the list, we'll call out to notify |
| 145 | // the module that it's attached to the worker thread. We don't hold |
| 146 | // the lock while we make this call. |
| 147 | if (thread_.get()) |
| 148 | module->ProcessThreadAttached(this); |
| 149 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 150 | { |
| 151 | rtc::CritScope lock(&lock_); |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 152 | modules_.push_back(ModuleCallback(module)); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 153 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 155 | // Wake the thread calling ProcessThreadImpl::Process() to update the |
| 156 | // waiting time. The waiting time for the just registered module may be |
| 157 | // shorter than all other registered modules. |
| 158 | wake_up_->Set(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | } |
| 160 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 161 | void ProcessThreadImpl::DeRegisterModule(Module* module) { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 162 | // Allowed to be called on any thread. |
Tommi | 7f375f0 | 2015-04-02 14:50:21 +0000 | [diff] [blame] | 163 | // TODO(tommi): Disallow this ^^^ |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 164 | RTC_DCHECK(module); |
Tommi | 7f375f0 | 2015-04-02 14:50:21 +0000 | [diff] [blame] | 165 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 166 | { |
| 167 | rtc::CritScope lock(&lock_); |
| 168 | modules_.remove_if([&module](const ModuleCallback& m) { |
| 169 | return m.module == module; |
| 170 | }); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 171 | |
Tommi | 7f375f0 | 2015-04-02 14:50:21 +0000 | [diff] [blame] | 172 | // TODO(tommi): we currently need to hold the lock while calling out to |
| 173 | // ProcessThreadAttached. This is to make sure that the thread hasn't been |
| 174 | // destroyed while we attach the module. Once we can make sure |
| 175 | // DeRegisterModule isn't being called on arbitrary threads, we can move the |
| 176 | // |if (thread_.get())| check and ProcessThreadAttached() call outside the |
| 177 | // lock scope. |
| 178 | |
| 179 | // Notify the module that it's been detached. |
| 180 | if (thread_.get()) |
| 181 | module->ProcessThreadAttached(nullptr); |
| 182 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 183 | } |
| 184 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 185 | // static |
| 186 | bool ProcessThreadImpl::Run(void* obj) { |
| 187 | return static_cast<ProcessThreadImpl*>(obj)->Process(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 188 | } |
| 189 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 190 | bool ProcessThreadImpl::Process() { |
| 191 | int64_t now = TickTime::MillisecondTimestamp(); |
| 192 | int64_t next_checkpoint = now + (1000 * 60); |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 193 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 194 | { |
| 195 | rtc::CritScope lock(&lock_); |
| 196 | if (stop_) |
| 197 | return false; |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 198 | for (ModuleCallback& m : modules_) { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 199 | // TODO(tommi): Would be good to measure the time TimeUntilNextProcess |
| 200 | // takes and dcheck if it takes too long (e.g. >=10ms). Ideally this |
| 201 | // operation should not require taking a lock, so querying all modules |
| 202 | // should run in a matter of nanoseconds. |
| 203 | if (m.next_callback == 0) |
| 204 | m.next_callback = GetNextCallbackTime(m.module, now); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 206 | if (m.next_callback <= now || |
| 207 | m.next_callback == kCallProcessImmediately) { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 208 | m.module->Process(); |
| 209 | // Use a new 'now' reference to calculate when the next callback |
| 210 | // should occur. We'll continue to use 'now' above for the baseline |
| 211 | // of calculating how long we should wait, to reduce variance. |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 212 | int64_t new_now = TickTime::MillisecondTimestamp(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 213 | m.next_callback = GetNextCallbackTime(m.module, new_now); |
| 214 | } |
| 215 | |
| 216 | if (m.next_callback < next_checkpoint) |
| 217 | next_checkpoint = m.next_callback; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 218 | } |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame] | 219 | |
| 220 | while (!queue_.empty()) { |
| 221 | ProcessTask* task = queue_.front(); |
| 222 | queue_.pop(); |
| 223 | lock_.Leave(); |
| 224 | task->Run(); |
| 225 | delete task; |
| 226 | lock_.Enter(); |
| 227 | } |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 228 | } |
| 229 | |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 230 | int64_t time_to_wait = next_checkpoint - TickTime::MillisecondTimestamp(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 231 | if (time_to_wait > 0) |
| 232 | wake_up_->Wait(static_cast<unsigned long>(time_to_wait)); |
| 233 | |
| 234 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 235 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 236 | } // namespace webrtc |