blob: 5c2f0abd1f3439d72ea2de0b75a10930c273a5ee [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/utility/source/process_thread_impl.h"
asapersson@webrtc.org8b2ec152014-04-11 07:59:43 +000012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <string>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/include/module.h"
16#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/trace_event.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000019
20namespace webrtc {
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000021namespace {
tommi@webrtc.org3985f012015-02-27 13:36:34 +000022
23// We use this constant internally to signal that a module has requested
24// a callback right away. When this is set, no call to TimeUntilNextProcess
25// should be made, but Process() should be called directly.
26const int64_t kCallProcessImmediately = -1;
27
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000028int64_t GetNextCallbackTime(Module* module, int64_t time_now) {
29 int64_t interval = module->TimeUntilNextProcess();
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000030 if (interval < 0) {
pbos9e260f12015-08-20 01:23:48 -070031 // Falling behind, we should call the callback now.
32 return time_now;
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000033 }
34 return time_now + interval;
35}
Yves Gerey665174f2018-06-19 15:03:05 +020036} // namespace
niklase@google.com470e71d2011-07-07 08:21:25 +000037
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000038ProcessThread::~ProcessThread() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000039
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000040// static
Yves Gerey665174f2018-06-19 15:03:05 +020041std::unique_ptr<ProcessThread> ProcessThread::Create(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)
Niels Möllerc572ff32018-11-07 08:43:50 +010046 : stop_(false), thread_name_(thread_name) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000047
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000048ProcessThreadImpl::~ProcessThreadImpl() {
henrikg91d6ede2015-09-17 00:24:34 -070049 RTC_DCHECK(thread_checker_.CalledOnValidThread());
50 RTC_DCHECK(!thread_.get());
51 RTC_DCHECK(!stop_);
tommi@webrtc.org03054482015-03-05 13:13:42 +000052
53 while (!queue_.empty()) {
54 delete queue_.front();
55 queue_.pop();
56 }
niklase@google.com470e71d2011-07-07 08:21:25 +000057}
58
tommi@webrtc.org3985f012015-02-27 13:36:34 +000059void ProcessThreadImpl::Start() {
henrikg91d6ede2015-09-17 00:24:34 -070060 RTC_DCHECK(thread_checker_.CalledOnValidThread());
61 RTC_DCHECK(!thread_.get());
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000062 if (thread_.get())
tommi@webrtc.org3985f012015-02-27 13:36:34 +000063 return;
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000064
henrikg91d6ede2015-09-17 00:24:34 -070065 RTC_DCHECK(!stop_);
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000066
tommi0a2391f2017-03-21 02:31:51 -070067 for (ModuleCallback& m : modules_)
68 m.module->ProcessThreadAttached(this);
tommi@webrtc.org3985f012015-02-27 13:36:34 +000069
Peter Boström8c38e8b2015-11-26 17:45:47 +010070 thread_.reset(
71 new rtc::PlatformThread(&ProcessThreadImpl::Run, this, thread_name_));
72 thread_->Start();
niklase@google.com470e71d2011-07-07 08:21:25 +000073}
74
tommi@webrtc.org3985f012015-02-27 13:36:34 +000075void ProcessThreadImpl::Stop() {
henrikg91d6ede2015-09-17 00:24:34 -070076 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Yves Gerey665174f2018-06-19 15:03:05 +020077 if (!thread_.get())
tommi@webrtc.org3985f012015-02-27 13:36:34 +000078 return;
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000079
80 {
81 rtc::CritScope lock(&lock_);
82 stop_ = true;
83 }
84
Niels Möller2c16cc62018-10-29 09:47:51 +010085 wake_up_.Set();
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000086
Peter Boström8c38e8b2015-11-26 17:45:47 +010087 thread_->Stop();
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000088 stop_ = false;
89
Tommi7f375f02015-04-02 14:50:21 +000090 thread_.reset();
tommi@webrtc.org3985f012015-02-27 13:36:34 +000091 for (ModuleCallback& m : modules_)
92 m.module->ProcessThreadAttached(nullptr);
niklase@google.com470e71d2011-07-07 08:21:25 +000093}
94
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +000095void ProcessThreadImpl::WakeUp(Module* module) {
96 // Allowed to be called on any thread.
97 {
98 rtc::CritScope lock(&lock_);
tommi@webrtc.org103f3282015-02-08 00:48:10 +000099 for (ModuleCallback& m : modules_) {
100 if (m.module == module)
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000101 m.next_callback = kCallProcessImmediately;
tommi@webrtc.org103f3282015-02-08 00:48:10 +0000102 }
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000103 }
Niels Möller2c16cc62018-10-29 09:47:51 +0100104 wake_up_.Set();
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000105}
106
Danil Chapovalov959e9b62019-01-14 14:29:18 +0100107void ProcessThreadImpl::PostTask(std::unique_ptr<QueuedTask> task) {
tommi@webrtc.org03054482015-03-05 13:13:42 +0000108 // Allowed to be called on any thread.
109 {
110 rtc::CritScope lock(&lock_);
111 queue_.push(task.release());
112 }
Niels Möller2c16cc62018-10-29 09:47:51 +0100113 wake_up_.Set();
tommi@webrtc.org03054482015-03-05 13:13:42 +0000114}
115
tommidea489f2017-03-03 03:20:24 -0800116void ProcessThreadImpl::RegisterModule(Module* module,
117 const rtc::Location& from) {
henrikg91d6ede2015-09-17 00:24:34 -0700118 RTC_DCHECK(thread_checker_.CalledOnValidThread());
tommicf39dd52017-07-07 16:24:34 -0700119 RTC_DCHECK(module) << from.ToString();
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000120
kwiberg5377bc72016-10-04 13:46:56 -0700121#if RTC_DCHECK_IS_ON
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000122 {
123 // Catch programmer error.
124 rtc::CritScope lock(&lock_);
tommicf39dd52017-07-07 16:24:34 -0700125 for (const ModuleCallback& mc : modules_) {
126 RTC_DCHECK(mc.module != module)
127 << "Already registered here: " << mc.location.ToString() << "\n"
128 << "Now attempting from here: " << from.ToString();
129 }
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000130 }
131#endif
132
133 // Now that we know the module isn't in the list, we'll call out to notify
134 // the module that it's attached to the worker thread. We don't hold
135 // the lock while we make this call.
136 if (thread_.get())
137 module->ProcessThreadAttached(this);
138
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000139 {
140 rtc::CritScope lock(&lock_);
tommidea489f2017-03-03 03:20:24 -0800141 modules_.push_back(ModuleCallback(module, from));
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000142 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000143
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000144 // Wake the thread calling ProcessThreadImpl::Process() to update the
145 // waiting time. The waiting time for the just registered module may be
146 // shorter than all other registered modules.
Niels Möller2c16cc62018-10-29 09:47:51 +0100147 wake_up_.Set();
niklase@google.com470e71d2011-07-07 08:21:25 +0000148}
149
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000150void ProcessThreadImpl::DeRegisterModule(Module* module) {
tommi0a2391f2017-03-21 02:31:51 -0700151 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700152 RTC_DCHECK(module);
Tommi7f375f02015-04-02 14:50:21 +0000153
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000154 {
155 rtc::CritScope lock(&lock_);
Yves Gerey665174f2018-06-19 15:03:05 +0200156 modules_.remove_if(
157 [&module](const ModuleCallback& m) { return m.module == module; });
Tommi7f375f02015-04-02 14:50:21 +0000158 }
tommi0a2391f2017-03-21 02:31:51 -0700159
160 // Notify the module that it's been detached.
161 module->ProcessThreadAttached(nullptr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000162}
163
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000164// static
165bool ProcessThreadImpl::Run(void* obj) {
166 return static_cast<ProcessThreadImpl*>(obj)->Process();
niklase@google.com470e71d2011-07-07 08:21:25 +0000167}
168
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000169bool ProcessThreadImpl::Process() {
tommidea489f2017-03-03 03:20:24 -0800170 TRACE_EVENT1("webrtc", "ProcessThreadImpl", "name", thread_name_);
Niels Möllerd28db7f2016-05-10 16:31:47 +0200171 int64_t now = rtc::TimeMillis();
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000172 int64_t next_checkpoint = now + (1000 * 60);
tommi@webrtc.org03054482015-03-05 13:13:42 +0000173
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000174 {
175 rtc::CritScope lock(&lock_);
176 if (stop_)
177 return false;
tommi@webrtc.org103f3282015-02-08 00:48:10 +0000178 for (ModuleCallback& m : modules_) {
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000179 // TODO(tommi): Would be good to measure the time TimeUntilNextProcess
180 // takes and dcheck if it takes too long (e.g. >=10ms). Ideally this
181 // operation should not require taking a lock, so querying all modules
182 // should run in a matter of nanoseconds.
183 if (m.next_callback == 0)
184 m.next_callback = GetNextCallbackTime(m.module, now);
niklase@google.com470e71d2011-07-07 08:21:25 +0000185
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000186 if (m.next_callback <= now ||
187 m.next_callback == kCallProcessImmediately) {
tommidea489f2017-03-03 03:20:24 -0800188 {
189 TRACE_EVENT2("webrtc", "ModuleProcess", "function",
190 m.location.function_name(), "file",
191 m.location.file_and_line());
192 m.module->Process();
193 }
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000194 // Use a new 'now' reference to calculate when the next callback
195 // should occur. We'll continue to use 'now' above for the baseline
196 // of calculating how long we should wait, to reduce variance.
Niels Möllerd28db7f2016-05-10 16:31:47 +0200197 int64_t new_now = rtc::TimeMillis();
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000198 m.next_callback = GetNextCallbackTime(m.module, new_now);
199 }
200
201 if (m.next_callback < next_checkpoint)
202 next_checkpoint = m.next_callback;
niklase@google.com470e71d2011-07-07 08:21:25 +0000203 }
tommi@webrtc.org03054482015-03-05 13:13:42 +0000204
205 while (!queue_.empty()) {
Danil Chapovalov959e9b62019-01-14 14:29:18 +0100206 QueuedTask* task = queue_.front();
tommi@webrtc.org03054482015-03-05 13:13:42 +0000207 queue_.pop();
208 lock_.Leave();
209 task->Run();
210 delete task;
211 lock_.Enter();
212 }
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000213 }
214
Niels Möllerd28db7f2016-05-10 16:31:47 +0200215 int64_t time_to_wait = next_checkpoint - rtc::TimeMillis();
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000216 if (time_to_wait > 0)
Niels Möller2c16cc62018-10-29 09:47:51 +0100217 wake_up_.Wait(static_cast<int>(time_to_wait));
tommi@webrtc.org0c3e12b2015-02-06 09:44:12 +0000218
219 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000220}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000221} // namespace webrtc