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