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(); |
| 28 | // Currently some implementations erroneously return error codes from |
| 29 | // TimeUntilNextProcess(). So, as is, we correct that and log an error. |
| 30 | if (interval < 0) { |
| 31 | LOG(LS_ERROR) << "TimeUntilNextProcess returned an invalid value " |
| 32 | << interval; |
| 33 | interval = 0; |
| 34 | } |
| 35 | return time_now + interval; |
| 36 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | } |
| 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 |
| 42 | rtc::scoped_ptr<ProcessThread> ProcessThread::Create() { |
| 43 | return rtc::scoped_ptr<ProcessThread>(new ProcessThreadImpl()).Pass(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | ProcessThreadImpl::ProcessThreadImpl() |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 47 | : wake_up_(EventWrapper::Create()), stop_(false) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | } |
| 49 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 50 | ProcessThreadImpl::~ProcessThreadImpl() { |
| 51 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 52 | DCHECK(!thread_.get()); |
| 53 | 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() { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 62 | DCHECK(thread_checker_.CalledOnValidThread()); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 63 | 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 | |
| 67 | DCHECK(!stop_); |
| 68 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 69 | for (ModuleCallback& m : modules_) |
| 70 | m.module->ProcessThreadAttached(this); |
| 71 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 72 | thread_.reset(ThreadWrapper::CreateThread( |
| 73 | &ProcessThreadImpl::Run, this, kNormalPriority, "ProcessThread")); |
| 74 | unsigned int id; |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 75 | CHECK(thread_->Start(id)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | } |
| 77 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 78 | void ProcessThreadImpl::Stop() { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 79 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 80 | if(!thread_.get()) |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 81 | return; |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 82 | |
| 83 | { |
| 84 | rtc::CritScope lock(&lock_); |
| 85 | stop_ = true; |
| 86 | } |
| 87 | |
| 88 | wake_up_->Set(); |
| 89 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 90 | CHECK(thread_->Stop()); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 91 | thread_.reset(); |
| 92 | stop_ = false; |
| 93 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 94 | for (ModuleCallback& m : modules_) |
| 95 | m.module->ProcessThreadAttached(nullptr); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | } |
| 97 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 98 | void ProcessThreadImpl::WakeUp(Module* module) { |
| 99 | // Allowed to be called on any thread. |
| 100 | { |
| 101 | rtc::CritScope lock(&lock_); |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 102 | for (ModuleCallback& m : modules_) { |
| 103 | if (m.module == module) |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 104 | m.next_callback = kCallProcessImmediately; |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 105 | } |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 106 | } |
| 107 | wake_up_->Set(); |
| 108 | } |
| 109 | |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame^] | 110 | void ProcessThreadImpl::PostTask(rtc::scoped_ptr<ProcessTask> task) { |
| 111 | // Allowed to be called on any thread. |
| 112 | { |
| 113 | rtc::CritScope lock(&lock_); |
| 114 | queue_.push(task.release()); |
| 115 | } |
| 116 | wake_up_->Set(); |
| 117 | } |
| 118 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 119 | void ProcessThreadImpl::RegisterModule(Module* module) { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 120 | // Allowed to be called on any thread. |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 121 | DCHECK(module); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 122 | |
| 123 | #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
| 124 | { |
| 125 | // Catch programmer error. |
| 126 | rtc::CritScope lock(&lock_); |
| 127 | for (const ModuleCallback& mc : modules_) |
| 128 | DCHECK(mc.module != module); |
| 129 | } |
| 130 | #endif |
| 131 | |
| 132 | // Now that we know the module isn't in the list, we'll call out to notify |
| 133 | // the module that it's attached to the worker thread. We don't hold |
| 134 | // the lock while we make this call. |
| 135 | if (thread_.get()) |
| 136 | module->ProcessThreadAttached(this); |
| 137 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 138 | { |
| 139 | rtc::CritScope lock(&lock_); |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 140 | modules_.push_back(ModuleCallback(module)); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 141 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 143 | // Wake the thread calling ProcessThreadImpl::Process() to update the |
| 144 | // waiting time. The waiting time for the just registered module may be |
| 145 | // shorter than all other registered modules. |
| 146 | wake_up_->Set(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 147 | } |
| 148 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 149 | void ProcessThreadImpl::DeRegisterModule(Module* module) { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 150 | // Allowed to be called on any thread. |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 151 | DCHECK(module); |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 152 | { |
| 153 | rtc::CritScope lock(&lock_); |
| 154 | modules_.remove_if([&module](const ModuleCallback& m) { |
| 155 | return m.module == module; |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | // Notify the module that it's been detached, while not holding the lock. |
| 160 | if (thread_.get()) |
| 161 | module->ProcessThreadAttached(nullptr); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 162 | } |
| 163 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 164 | // static |
| 165 | bool ProcessThreadImpl::Run(void* obj) { |
| 166 | return static_cast<ProcessThreadImpl*>(obj)->Process(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 167 | } |
| 168 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 169 | bool ProcessThreadImpl::Process() { |
| 170 | int64_t now = TickTime::MillisecondTimestamp(); |
| 171 | int64_t next_checkpoint = now + (1000 * 60); |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame^] | 172 | |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 173 | { |
| 174 | rtc::CritScope lock(&lock_); |
| 175 | if (stop_) |
| 176 | return false; |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 177 | for (ModuleCallback& m : modules_) { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 178 | // TODO(tommi): Would be good to measure the time TimeUntilNextProcess |
| 179 | // takes and dcheck if it takes too long (e.g. >=10ms). Ideally this |
| 180 | // operation should not require taking a lock, so querying all modules |
| 181 | // should run in a matter of nanoseconds. |
| 182 | if (m.next_callback == 0) |
| 183 | m.next_callback = GetNextCallbackTime(m.module, now); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | |
tommi@webrtc.org | 3985f01 | 2015-02-27 13:36:34 +0000 | [diff] [blame] | 185 | if (m.next_callback <= now || |
| 186 | m.next_callback == kCallProcessImmediately) { |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 187 | m.module->Process(); |
| 188 | // Use a new 'now' reference to calculate when the next callback |
| 189 | // should occur. We'll continue to use 'now' above for the baseline |
| 190 | // of calculating how long we should wait, to reduce variance. |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 191 | int64_t new_now = TickTime::MillisecondTimestamp(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 192 | m.next_callback = GetNextCallbackTime(m.module, new_now); |
| 193 | } |
| 194 | |
| 195 | if (m.next_callback < next_checkpoint) |
| 196 | next_checkpoint = m.next_callback; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 197 | } |
tommi@webrtc.org | 0305448 | 2015-03-05 13:13:42 +0000 | [diff] [blame^] | 198 | |
| 199 | while (!queue_.empty()) { |
| 200 | ProcessTask* task = queue_.front(); |
| 201 | queue_.pop(); |
| 202 | lock_.Leave(); |
| 203 | task->Run(); |
| 204 | delete task; |
| 205 | lock_.Enter(); |
| 206 | } |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 207 | } |
| 208 | |
tommi@webrtc.org | 103f328 | 2015-02-08 00:48:10 +0000 | [diff] [blame] | 209 | int64_t time_to_wait = next_checkpoint - TickTime::MillisecondTimestamp(); |
tommi@webrtc.org | 0c3e12b | 2015-02-06 09:44:12 +0000 | [diff] [blame] | 210 | if (time_to_wait > 0) |
| 211 | wake_up_->Wait(static_cast<unsigned long>(time_to_wait)); |
| 212 | |
| 213 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 214 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 215 | } // namespace webrtc |