Reason for revert:
This CL breaks FYI bots with a compile error.
Sample error:
jingle/glue/thread_wrapper.cc -o obj/jingle/jingle_glue/thread_wrapper.o
In file included from ../../jingle/glue/thread_wrapper.cc:5:
In file included from ../../jingle/glue/thread_wrapper.h:16:
In file included from ../../base/message_loop/message_loop.h:17:
In file included from ../../base/memory/ref_counted.h:19:
../../base/logging.h:598:1: error: call to 'MakeCheckOpString' is ambiguous
DEFINE_CHECK_OP_IMPL(EQ, ==)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../base/logging.h:592:17: note: expanded from macro 'DEFINE_CHECK_OP_IMPL'
else return MakeCheckOpString(v1, v2, names); \
^~~~~~~~~~~~~~~~~
../../jingle/glue/thread_wrapper.cc:46:3: note: in instantiation of function template specialization 'logging::CheckEQImpl<rtc::Thread *, jingle_glue::JingleThreadWrapper *>' requested here
DCHECK_EQ(rtc::Thread::Current(), current());
^
../../base/logging.h:748:31: note: expanded from macro 'DCHECK_EQ'
#define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
^
../../base/logging.h:721:18: note: expanded from macro 'DCHECK_OP'
::logging::Check##name##Impl((val1), (val2), \
^
<scratch space>:102:1: note: expanded from here
CheckEQImpl
^
../../base/logging.h:555:14: note: candidate function [with t1 = rtc::Thread *, t2 = jingle_glue::JingleThreadWrapper *]
std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
^
../../third_party/webrtc/base/checks.h:122:14: note: candidate function [with t1 = rtc::Thread *, t2 = jingle_glue::JingleThreadWrapper *]
std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
^
In file included from ../../jingle/glue/thread_wrapper.cc:5:
In file included from ../../jingle/glue/thread_wrapper.h:16:
In file included from ../../base/message_loop/message_loop.h:17:
In file included from ../../base/memory/ref_counted.h:19:
../../base/logging.h:598:1: error: call to 'MakeCheckOpString' is ambiguous
DEFINE_CHECK_OP_IMPL(EQ, ==)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../base/logging.h:592:17: note: expanded from macro 'DEFINE_CHECK_OP_IMPL'
else return MakeCheckOpString(v1, v2, names); \
^~~~~~~~~~~~~~~~~
../../jingle/glue/thread_wrapper.cc:81:3: note: in instantiation of function template specialization 'logging::CheckEQImpl<jingle_glue::JingleThreadWrapper *, jingle_glue::JingleThreadWrapper *>' requested here
DCHECK_EQ(this, JingleThreadWrapper::current());
^
../../base/logging.h:748:31: note: expanded from macro 'DCHECK_EQ'
#define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
^
../../base/logging.h:721:18: note: expanded from macro 'DCHECK_OP'
::logging::Check##name##Impl((val1), (val2), \
^
<scratch space>:5:1: note: expanded from here
CheckEQImpl
^
../../base/logging.h:555:14: note: candidate function [with t1 = jingle_glue::JingleThreadWrapper *, t2 = jingle_glue::JingleThreadWrapper *]
std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
^
../../third_party/webrtc/base/checks.h:122:14: note: candidate function [with t1 = jingle_glue::JingleThreadWrapper *, t2 = jingle_glue::JingleThreadWrapper *]
std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
^
In file included from ../../jingle/glue/thread_wrapper.cc:5:
In file included from ../../jingle/glue/thread_wrapper.h:16:
In file included from ../../base/message_loop/message_loop.h:17:
In file included from ../../base/memory/ref_counted.h:19:
../../base/logging.h:598:1: error: call to 'MakeCheckOpString' is ambiguous
DEFINE_CHECK_OP_IMPL(EQ, ==)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../base/logging.h:592:17: note: expanded from macro 'DEFINE_CHECK_OP_IMPL'
else return MakeCheckOpString(v1, v2, names); \
^~~~~~~~~~~~~~~~~
../../jingle/glue/thread_wrapper.cc:82:3: note: in instantiation of function template specialization 'logging::CheckEQImpl<jingle_glue::JingleThreadWrapper *, rtc::Thread *>' requested here
DCHECK_EQ(this, rtc::Thread::Current());
^
../../base/logging.h:748:31: note: expanded from macro 'DCHECK_EQ'
#define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
^
../../base/logging.h:721:18: note: expanded from macro 'DCHECK_OP'
::logging::Check##name##Impl((val1), (val2), \
^
<scratch space>:12:1: note: expanded from here
CheckEQImpl
^
../../base/logging.h:555:14: note: candidate function [with t1 = jingle_glue::JingleThreadWrapper *, t2 = rtc::Thread *]
std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
^
../../third_party/webrtc/base/checks.h:122:14: note: candidate function [with t1 = jingle_glue::JingleThreadWrapper *, t2 = rtc::Thread *]
std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
^
3 errors generated.
Original issue's description:
> Test RTC_DCHECK_IS_ON instead of checking DCHECK_ALWAYS_ON everywhere
>
> The former is always defined (by webrtc/base/checks.h) to either 0 or
> 1, whereas the latter isn't necessarily defined.
>
> NOTRY=true
> BUG=webrtc:6451
>
> Committed: https://crrev.com/ab0b929321d37669165d5795268fa10a8c97ec5b
> Cr-Commit-Position: refs/heads/master@{#14474}
TBR=ossu@webrtc.org,kwiberg@webrtc.org
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=webrtc:6451
Review-Url: https://codereview.webrtc.org/2384083004
Cr-Commit-Position: refs/heads/master@{#14480}
238 lines
7.0 KiB
C++
238 lines
7.0 KiB
C++
/*
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#include "webrtc/modules/utility/source/process_thread_impl.h"
|
|
|
|
#include "webrtc/base/checks.h"
|
|
#include "webrtc/base/task_queue.h"
|
|
#include "webrtc/base/timeutils.h"
|
|
#include "webrtc/modules/include/module.h"
|
|
#include "webrtc/system_wrappers/include/logging.h"
|
|
|
|
namespace webrtc {
|
|
namespace {
|
|
|
|
// We use this constant internally to signal that a module has requested
|
|
// a callback right away. When this is set, no call to TimeUntilNextProcess
|
|
// should be made, but Process() should be called directly.
|
|
const int64_t kCallProcessImmediately = -1;
|
|
|
|
int64_t GetNextCallbackTime(Module* module, int64_t time_now) {
|
|
int64_t interval = module->TimeUntilNextProcess();
|
|
if (interval < 0) {
|
|
// Falling behind, we should call the callback now.
|
|
return time_now;
|
|
}
|
|
return time_now + interval;
|
|
}
|
|
}
|
|
|
|
ProcessThread::~ProcessThread() {}
|
|
|
|
// static
|
|
std::unique_ptr<ProcessThread> ProcessThread::Create(
|
|
const char* thread_name) {
|
|
return std::unique_ptr<ProcessThread>(new ProcessThreadImpl(thread_name));
|
|
}
|
|
|
|
ProcessThreadImpl::ProcessThreadImpl(const char* thread_name)
|
|
: wake_up_(EventWrapper::Create()),
|
|
stop_(false),
|
|
thread_name_(thread_name) {}
|
|
|
|
ProcessThreadImpl::~ProcessThreadImpl() {
|
|
RTC_DCHECK(thread_checker_.CalledOnValidThread());
|
|
RTC_DCHECK(!thread_.get());
|
|
RTC_DCHECK(!stop_);
|
|
|
|
while (!queue_.empty()) {
|
|
delete queue_.front();
|
|
queue_.pop();
|
|
}
|
|
}
|
|
|
|
void ProcessThreadImpl::Start() {
|
|
RTC_DCHECK(thread_checker_.CalledOnValidThread());
|
|
RTC_DCHECK(!thread_.get());
|
|
if (thread_.get())
|
|
return;
|
|
|
|
RTC_DCHECK(!stop_);
|
|
|
|
{
|
|
// TODO(tommi): Since DeRegisterModule is currently being called from
|
|
// different threads in some cases (ChannelOwner), we need to lock access to
|
|
// the modules_ collection even on the controller thread.
|
|
// Once we've cleaned up those places, we can remove this lock.
|
|
rtc::CritScope lock(&lock_);
|
|
for (ModuleCallback& m : modules_)
|
|
m.module->ProcessThreadAttached(this);
|
|
}
|
|
|
|
thread_.reset(
|
|
new rtc::PlatformThread(&ProcessThreadImpl::Run, this, thread_name_));
|
|
thread_->Start();
|
|
}
|
|
|
|
void ProcessThreadImpl::Stop() {
|
|
RTC_DCHECK(thread_checker_.CalledOnValidThread());
|
|
if(!thread_.get())
|
|
return;
|
|
|
|
{
|
|
rtc::CritScope lock(&lock_);
|
|
stop_ = true;
|
|
}
|
|
|
|
wake_up_->Set();
|
|
|
|
thread_->Stop();
|
|
stop_ = false;
|
|
|
|
// TODO(tommi): Since DeRegisterModule is currently being called from
|
|
// different threads in some cases (ChannelOwner), we need to lock access to
|
|
// the modules_ collection even on the controller thread.
|
|
// Since DeRegisterModule also checks thread_, we also need to hold the
|
|
// lock for the .reset() operation.
|
|
// Once we've cleaned up those places, we can remove this lock.
|
|
rtc::CritScope lock(&lock_);
|
|
thread_.reset();
|
|
for (ModuleCallback& m : modules_)
|
|
m.module->ProcessThreadAttached(nullptr);
|
|
}
|
|
|
|
void ProcessThreadImpl::WakeUp(Module* module) {
|
|
// Allowed to be called on any thread.
|
|
{
|
|
rtc::CritScope lock(&lock_);
|
|
for (ModuleCallback& m : modules_) {
|
|
if (m.module == module)
|
|
m.next_callback = kCallProcessImmediately;
|
|
}
|
|
}
|
|
wake_up_->Set();
|
|
}
|
|
|
|
void ProcessThreadImpl::PostTask(std::unique_ptr<rtc::QueuedTask> task) {
|
|
// Allowed to be called on any thread.
|
|
{
|
|
rtc::CritScope lock(&lock_);
|
|
queue_.push(task.release());
|
|
}
|
|
wake_up_->Set();
|
|
}
|
|
|
|
void ProcessThreadImpl::RegisterModule(Module* module) {
|
|
RTC_DCHECK(thread_checker_.CalledOnValidThread());
|
|
RTC_DCHECK(module);
|
|
|
|
#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
|
|
{
|
|
// Catch programmer error.
|
|
rtc::CritScope lock(&lock_);
|
|
for (const ModuleCallback& mc : modules_)
|
|
RTC_DCHECK(mc.module != module);
|
|
}
|
|
#endif
|
|
|
|
// Now that we know the module isn't in the list, we'll call out to notify
|
|
// the module that it's attached to the worker thread. We don't hold
|
|
// the lock while we make this call.
|
|
if (thread_.get())
|
|
module->ProcessThreadAttached(this);
|
|
|
|
{
|
|
rtc::CritScope lock(&lock_);
|
|
modules_.push_back(ModuleCallback(module));
|
|
}
|
|
|
|
// Wake the thread calling ProcessThreadImpl::Process() to update the
|
|
// waiting time. The waiting time for the just registered module may be
|
|
// shorter than all other registered modules.
|
|
wake_up_->Set();
|
|
}
|
|
|
|
void ProcessThreadImpl::DeRegisterModule(Module* module) {
|
|
// Allowed to be called on any thread.
|
|
// TODO(tommi): Disallow this ^^^
|
|
RTC_DCHECK(module);
|
|
|
|
{
|
|
rtc::CritScope lock(&lock_);
|
|
modules_.remove_if([&module](const ModuleCallback& m) {
|
|
return m.module == module;
|
|
});
|
|
|
|
// TODO(tommi): we currently need to hold the lock while calling out to
|
|
// ProcessThreadAttached. This is to make sure that the thread hasn't been
|
|
// destroyed while we attach the module. Once we can make sure
|
|
// DeRegisterModule isn't being called on arbitrary threads, we can move the
|
|
// |if (thread_.get())| check and ProcessThreadAttached() call outside the
|
|
// lock scope.
|
|
|
|
// Notify the module that it's been detached.
|
|
if (thread_.get())
|
|
module->ProcessThreadAttached(nullptr);
|
|
}
|
|
}
|
|
|
|
// static
|
|
bool ProcessThreadImpl::Run(void* obj) {
|
|
return static_cast<ProcessThreadImpl*>(obj)->Process();
|
|
}
|
|
|
|
bool ProcessThreadImpl::Process() {
|
|
int64_t now = rtc::TimeMillis();
|
|
int64_t next_checkpoint = now + (1000 * 60);
|
|
|
|
{
|
|
rtc::CritScope lock(&lock_);
|
|
if (stop_)
|
|
return false;
|
|
for (ModuleCallback& m : modules_) {
|
|
// TODO(tommi): Would be good to measure the time TimeUntilNextProcess
|
|
// takes and dcheck if it takes too long (e.g. >=10ms). Ideally this
|
|
// operation should not require taking a lock, so querying all modules
|
|
// should run in a matter of nanoseconds.
|
|
if (m.next_callback == 0)
|
|
m.next_callback = GetNextCallbackTime(m.module, now);
|
|
|
|
if (m.next_callback <= now ||
|
|
m.next_callback == kCallProcessImmediately) {
|
|
m.module->Process();
|
|
// Use a new 'now' reference to calculate when the next callback
|
|
// should occur. We'll continue to use 'now' above for the baseline
|
|
// of calculating how long we should wait, to reduce variance.
|
|
int64_t new_now = rtc::TimeMillis();
|
|
m.next_callback = GetNextCallbackTime(m.module, new_now);
|
|
}
|
|
|
|
if (m.next_callback < next_checkpoint)
|
|
next_checkpoint = m.next_callback;
|
|
}
|
|
|
|
while (!queue_.empty()) {
|
|
rtc::QueuedTask* task = queue_.front();
|
|
queue_.pop();
|
|
lock_.Leave();
|
|
task->Run();
|
|
delete task;
|
|
lock_.Enter();
|
|
}
|
|
}
|
|
|
|
int64_t time_to_wait = next_checkpoint - rtc::TimeMillis();
|
|
if (time_to_wait > 0)
|
|
wake_up_->Wait(static_cast<unsigned long>(time_to_wait));
|
|
|
|
return true;
|
|
}
|
|
} // namespace webrtc
|