blob: 3332cd6dc6a64603173a04e71bc1c6dc90405435 [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"
tommi435f98b2016-05-28 14:57:15 -070014#include "webrtc/base/task_queue.h"
Niels Möllerd28db7f2016-05-10 16:31:47 +020015#include "webrtc/base/timeutils.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010016#include "webrtc/modules/include/module.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010017#include "webrtc/system_wrappers/include/logging.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018
19namespace webrtc {
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000020namespace {
tommi@webrtc.org3985f012015-02-27 13:36:34 +000021
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.
25const int64_t kCallProcessImmediately = -1;
26
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000027int64_t GetNextCallbackTime(Module* module, int64_t time_now) {
28 int64_t interval = module->TimeUntilNextProcess();
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000029 if (interval < 0) {
pbos9e260f12015-08-20 01:23:48 -070030 // Falling behind, we should call the callback now.
31 return time_now;
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000032 }
33 return time_now + interval;
34}
niklase@google.com470e71d2011-07-07 08:21:25 +000035}
36
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000037ProcessThread::~ProcessThread() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000038
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000039// static
kwiberg84be5112016-04-27 01:19:58 -070040std::unique_ptr<ProcessThread> ProcessThread::Create(
stefan847855b2015-09-11 09:52:15 -070041 const char* thread_name) {
kwiberg84be5112016-04-27 01:19:58 -070042 return std::unique_ptr<ProcessThread>(new ProcessThreadImpl(thread_name));
niklase@google.com470e71d2011-07-07 08:21:25 +000043}
44
stefan847855b2015-09-11 09:52:15 -070045ProcessThreadImpl::ProcessThreadImpl(const char* thread_name)
46 : wake_up_(EventWrapper::Create()),
47 stop_(false),
48 thread_name_(thread_name) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000049
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000050ProcessThreadImpl::~ProcessThreadImpl() {
henrikg91d6ede2015-09-17 00:24:34 -070051 RTC_DCHECK(thread_checker_.CalledOnValidThread());
52 RTC_DCHECK(!thread_.get());
53 RTC_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() {
henrikg91d6ede2015-09-17 00:24:34 -070062 RTC_DCHECK(thread_checker_.CalledOnValidThread());
63 RTC_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
henrikg91d6ede2015-09-17 00:24:34 -070067 RTC_DCHECK(!stop_);
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000068
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
Peter Boström8c38e8b2015-11-26 17:45:47 +010079 thread_.reset(
80 new rtc::PlatformThread(&ProcessThreadImpl::Run, this, thread_name_));
81 thread_->Start();
niklase@google.com470e71d2011-07-07 08:21:25 +000082}
83
tommi@webrtc.org3985f012015-02-27 13:36:34 +000084void ProcessThreadImpl::Stop() {
henrikg91d6ede2015-09-17 00:24:34 -070085 RTC_DCHECK(thread_checker_.CalledOnValidThread());
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000086 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
Peter Boström8c38e8b2015-11-26 17:45:47 +010096 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
tommi435f98b2016-05-28 14:57:15 -0700123void ProcessThreadImpl::PostTask(std::unique_ptr<rtc::QueuedTask> task) {
tommi@webrtc.org03054482015-03-05 13:13:42 +0000124 // 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) {
henrikg91d6ede2015-09-17 00:24:34 -0700133 RTC_DCHECK(thread_checker_.CalledOnValidThread());
134 RTC_DCHECK(module);
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000135
kwiberg5377bc72016-10-04 13:46:56 -0700136#if RTC_DCHECK_IS_ON
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000137 {
138 // Catch programmer error.
139 rtc::CritScope lock(&lock_);
140 for (const ModuleCallback& mc : modules_)
henrikg91d6ede2015-09-17 00:24:34 -0700141 RTC_DCHECK(mc.module != module);
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000142 }
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.org0c3e12b2015-02-06 09:44:12 +0000151 {
152 rtc::CritScope lock(&lock_);
tommi@webrtc.org103f3282015-02-08 00:48:10 +0000153 modules_.push_back(ModuleCallback(module));
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000154 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000155
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000156 // 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.com470e71d2011-07-07 08:21:25 +0000160}
161
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000162void ProcessThreadImpl::DeRegisterModule(Module* module) {
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000163 // Allowed to be called on any thread.
Tommi7f375f02015-04-02 14:50:21 +0000164 // TODO(tommi): Disallow this ^^^
henrikg91d6ede2015-09-17 00:24:34 -0700165 RTC_DCHECK(module);
Tommi7f375f02015-04-02 14:50:21 +0000166
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000167 {
168 rtc::CritScope lock(&lock_);
169 modules_.remove_if([&module](const ModuleCallback& m) {
170 return m.module == module;
171 });
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000172
Tommi7f375f02015-04-02 14:50:21 +0000173 // 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.com470e71d2011-07-07 08:21:25 +0000184}
185
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000186// static
187bool ProcessThreadImpl::Run(void* obj) {
188 return static_cast<ProcessThreadImpl*>(obj)->Process();
niklase@google.com470e71d2011-07-07 08:21:25 +0000189}
190
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000191bool ProcessThreadImpl::Process() {
Niels Möllerd28db7f2016-05-10 16:31:47 +0200192 int64_t now = rtc::TimeMillis();
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000193 int64_t next_checkpoint = now + (1000 * 60);
tommi@webrtc.org03054482015-03-05 13:13:42 +0000194
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000195 {
196 rtc::CritScope lock(&lock_);
197 if (stop_)
198 return false;
tommi@webrtc.org103f3282015-02-08 00:48:10 +0000199 for (ModuleCallback& m : modules_) {
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000200 // 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.com470e71d2011-07-07 08:21:25 +0000206
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000207 if (m.next_callback <= now ||
208 m.next_callback == kCallProcessImmediately) {
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000209 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.
Niels Möllerd28db7f2016-05-10 16:31:47 +0200213 int64_t new_now = rtc::TimeMillis();
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000214 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.com470e71d2011-07-07 08:21:25 +0000219 }
tommi@webrtc.org03054482015-03-05 13:13:42 +0000220
221 while (!queue_.empty()) {
tommi435f98b2016-05-28 14:57:15 -0700222 rtc::QueuedTask* task = queue_.front();
tommi@webrtc.org03054482015-03-05 13:13:42 +0000223 queue_.pop();
224 lock_.Leave();
225 task->Run();
226 delete task;
227 lock_.Enter();
228 }
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000229 }
230
Niels Möllerd28db7f2016-05-10 16:31:47 +0200231 int64_t time_to_wait = next_checkpoint - rtc::TimeMillis();
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000232 if (time_to_wait > 0)
233 wake_up_->Wait(static_cast<unsigned long>(time_to_wait));
234
235 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000236}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000237} // namespace webrtc