blob: ca319ad48640cbe28fcc79187022a9aeb4e1c180 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
xians@webrtc.org6bde7a82012-02-20 08:39:25 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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.org8b062002013-07-12 08:28:10 +000011#include "webrtc/modules/utility/source/process_thread_impl.h"
asapersson@webrtc.org8b2ec152014-04-11 07:59:43 +000012
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000013#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.com470e71d2011-07-07 08:21:25 +000017
18namespace webrtc {
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000019namespace {
tommi@webrtc.org3985f012015-02-27 13:36:34 +000020
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.
24const int64_t kCallProcessImmediately = -1;
25
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000026int64_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.com470e71d2011-07-07 08:21:25 +000037}
38
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000039ProcessThread::~ProcessThread() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000040
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000041// static
42rtc::scoped_ptr<ProcessThread> ProcessThread::Create() {
43 return rtc::scoped_ptr<ProcessThread>(new ProcessThreadImpl()).Pass();
niklase@google.com470e71d2011-07-07 08:21:25 +000044}
45
46ProcessThreadImpl::ProcessThreadImpl()
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000047 : wake_up_(EventWrapper::Create()), stop_(false) {
niklase@google.com470e71d2011-07-07 08:21:25 +000048}
49
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000050ProcessThreadImpl::~ProcessThreadImpl() {
51 DCHECK(thread_checker_.CalledOnValidThread());
52 DCHECK(!thread_.get());
53 DCHECK(!stop_);
tommi@webrtc.org03054482015-03-05 13:13:42 +000054
55 while (!queue_.empty()) {
56 delete queue_.front();
57 queue_.pop();
58 }
niklase@google.com470e71d2011-07-07 08:21:25 +000059}
60
tommi@webrtc.org3985f012015-02-27 13:36:34 +000061void ProcessThreadImpl::Start() {
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000062 DCHECK(thread_checker_.CalledOnValidThread());
tommi@webrtc.org3985f012015-02-27 13:36:34 +000063 DCHECK(!thread_.get());
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000064 if (thread_.get())
tommi@webrtc.org3985f012015-02-27 13:36:34 +000065 return;
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000066
67 DCHECK(!stop_);
68
Tommi842a4a62015-03-30 14:16:12 +000069 {
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.org3985f012015-02-27 13:36:34 +000078
tommi@webrtc.org361981f2015-03-19 14:44:18 +000079 thread_ = ThreadWrapper::CreateThread(
tommi@webrtc.org38492c52015-03-22 14:41:46 +000080 &ProcessThreadImpl::Run, this, "ProcessThread");
pbos@webrtc.org86639732015-03-13 00:06:21 +000081 CHECK(thread_->Start());
niklase@google.com470e71d2011-07-07 08:21:25 +000082}
83
tommi@webrtc.org3985f012015-02-27 13:36:34 +000084void ProcessThreadImpl::Stop() {
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000085 DCHECK(thread_checker_.CalledOnValidThread());
86 if(!thread_.get())
tommi@webrtc.org3985f012015-02-27 13:36:34 +000087 return;
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000088
89 {
90 rtc::CritScope lock(&lock_);
91 stop_ = true;
92 }
93
94 wake_up_->Set();
95
tommi@webrtc.org3985f012015-02-27 13:36:34 +000096 CHECK(thread_->Stop());
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000097 stop_ = false;
98
Tommi842a4a62015-03-30 14:16:12 +000099 // 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.
Tommi7f375f02015-04-02 14:50:21 +0000102 // Since DeRegisterModule also checks thread_, we also need to hold the
103 // lock for the .reset() operation.
Tommi842a4a62015-03-30 14:16:12 +0000104 // Once we've cleaned up those places, we can remove this lock.
105 rtc::CritScope lock(&lock_);
Tommi7f375f02015-04-02 14:50:21 +0000106 thread_.reset();
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000107 for (ModuleCallback& m : modules_)
108 m.module->ProcessThreadAttached(nullptr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000109}
110
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000111void ProcessThreadImpl::WakeUp(Module* module) {
112 // Allowed to be called on any thread.
113 {
114 rtc::CritScope lock(&lock_);
tommi@webrtc.org103f3282015-02-08 00:48:10 +0000115 for (ModuleCallback& m : modules_) {
116 if (m.module == module)
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000117 m.next_callback = kCallProcessImmediately;
tommi@webrtc.org103f3282015-02-08 00:48:10 +0000118 }
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000119 }
120 wake_up_->Set();
121}
122
tommi@webrtc.org03054482015-03-05 13:13:42 +0000123void 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.org3985f012015-02-27 13:36:34 +0000132void ProcessThreadImpl::RegisterModule(Module* module) {
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000133 // Allowed to be called on any thread.
Tommi7f375f02015-04-02 14:50:21 +0000134 // TODO(tommi): Disallow this ^^^
tommi@webrtc.org103f3282015-02-08 00:48:10 +0000135 DCHECK(module);
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000136
137#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
138 {
139 // Catch programmer error.
140 rtc::CritScope lock(&lock_);
141 for (const ModuleCallback& mc : modules_)
142 DCHECK(mc.module != module);
143 }
144#endif
145
146 // Now that we know the module isn't in the list, we'll call out to notify
147 // the module that it's attached to the worker thread. We don't hold
148 // the lock while we make this call.
149 if (thread_.get())
150 module->ProcessThreadAttached(this);
151
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000152 {
153 rtc::CritScope lock(&lock_);
tommi@webrtc.org103f3282015-02-08 00:48:10 +0000154 modules_.push_back(ModuleCallback(module));
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000155 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000156
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000157 // Wake the thread calling ProcessThreadImpl::Process() to update the
158 // waiting time. The waiting time for the just registered module may be
159 // shorter than all other registered modules.
160 wake_up_->Set();
niklase@google.com470e71d2011-07-07 08:21:25 +0000161}
162
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000163void ProcessThreadImpl::DeRegisterModule(Module* module) {
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000164 // Allowed to be called on any thread.
Tommi7f375f02015-04-02 14:50:21 +0000165 // TODO(tommi): Disallow this ^^^
tommi@webrtc.org103f3282015-02-08 00:48:10 +0000166 DCHECK(module);
Tommi7f375f02015-04-02 14:50:21 +0000167
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000168 {
169 rtc::CritScope lock(&lock_);
170 modules_.remove_if([&module](const ModuleCallback& m) {
171 return m.module == module;
172 });
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000173
Tommi7f375f02015-04-02 14:50:21 +0000174 // TODO(tommi): we currently need to hold the lock while calling out to
175 // ProcessThreadAttached. This is to make sure that the thread hasn't been
176 // destroyed while we attach the module. Once we can make sure
177 // DeRegisterModule isn't being called on arbitrary threads, we can move the
178 // |if (thread_.get())| check and ProcessThreadAttached() call outside the
179 // lock scope.
180
181 // Notify the module that it's been detached.
182 if (thread_.get())
183 module->ProcessThreadAttached(nullptr);
184 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000185}
186
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000187// static
188bool ProcessThreadImpl::Run(void* obj) {
189 return static_cast<ProcessThreadImpl*>(obj)->Process();
niklase@google.com470e71d2011-07-07 08:21:25 +0000190}
191
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000192bool ProcessThreadImpl::Process() {
193 int64_t now = TickTime::MillisecondTimestamp();
194 int64_t next_checkpoint = now + (1000 * 60);
tommi@webrtc.org03054482015-03-05 13:13:42 +0000195
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000196 {
197 rtc::CritScope lock(&lock_);
198 if (stop_)
199 return false;
tommi@webrtc.org103f3282015-02-08 00:48:10 +0000200 for (ModuleCallback& m : modules_) {
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000201 // TODO(tommi): Would be good to measure the time TimeUntilNextProcess
202 // takes and dcheck if it takes too long (e.g. >=10ms). Ideally this
203 // operation should not require taking a lock, so querying all modules
204 // should run in a matter of nanoseconds.
205 if (m.next_callback == 0)
206 m.next_callback = GetNextCallbackTime(m.module, now);
niklase@google.com470e71d2011-07-07 08:21:25 +0000207
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000208 if (m.next_callback <= now ||
209 m.next_callback == kCallProcessImmediately) {
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000210 m.module->Process();
211 // Use a new 'now' reference to calculate when the next callback
212 // should occur. We'll continue to use 'now' above for the baseline
213 // of calculating how long we should wait, to reduce variance.
tommi@webrtc.org103f3282015-02-08 00:48:10 +0000214 int64_t new_now = TickTime::MillisecondTimestamp();
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000215 m.next_callback = GetNextCallbackTime(m.module, new_now);
216 }
217
218 if (m.next_callback < next_checkpoint)
219 next_checkpoint = m.next_callback;
niklase@google.com470e71d2011-07-07 08:21:25 +0000220 }
tommi@webrtc.org03054482015-03-05 13:13:42 +0000221
222 while (!queue_.empty()) {
223 ProcessTask* task = queue_.front();
224 queue_.pop();
225 lock_.Leave();
226 task->Run();
227 delete task;
228 lock_.Enter();
229 }
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000230 }
231
tommi@webrtc.org103f3282015-02-08 00:48:10 +0000232 int64_t time_to_wait = next_checkpoint - TickTime::MillisecondTimestamp();
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000233 if (time_to_wait > 0)
234 wake_up_->Wait(static_cast<unsigned long>(time_to_wait));
235
236 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000237}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000238} // namespace webrtc