webrtc_m130/test/time_controller/simulated_thread.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

115 lines
3.6 KiB
C++
Raw Permalink Normal View History

/*
* Copyright (c) 2020 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 "test/time_controller/simulated_thread.h"
#include <algorithm>
#include <utility>
namespace webrtc {
namespace {
// A socket server that does nothing. It's different from NullSocketServer in
// that it does allow sleep/wakeup. This avoids usage of an Event instance which
// otherwise would cause issues with the simulated Yeild behavior.
class DummySocketServer : public rtc::SocketServer {
public:
rtc::Socket* CreateSocket(int family, int type) override {
RTC_DCHECK_NOTREACHED();
return nullptr;
}
bool Wait(TimeDelta max_wait_duration, bool process_io) override {
RTC_CHECK(max_wait_duration.IsZero());
return true;
}
void WakeUp() override {}
};
} // namespace
SimulatedThread::SimulatedThread(
sim_time_impl::SimulatedTimeControllerImpl* handler,
absl::string_view name,
std::unique_ptr<rtc::SocketServer> socket_server)
: rtc::Thread(socket_server ? std::move(socket_server)
: std::make_unique<DummySocketServer>()),
handler_(handler),
name_(new char[name.size()]) {
std::copy_n(name.begin(), name.size(), name_);
}
SimulatedThread::~SimulatedThread() {
handler_->Unregister(this);
delete[] name_;
}
void SimulatedThread::RunReady(Timestamp at_time) {
CurrentThreadSetter set_current(this);
ProcessMessages(0);
int delay_ms = GetDelay();
MutexLock lock(&lock_);
if (delay_ms == kForever) {
next_run_time_ = Timestamp::PlusInfinity();
} else {
Use newer version of TimeDelta and TimeStamp factories in webrtc find . -type f \( -name "*.h" -o -name "*.cc" \) | xargs sed -i -e "s/TimeDelta::Micros<\(.*\)>()/TimeDelta::Micros(\1)/g" find . -type f \( -name "*.h" -o -name "*.cc" \) | xargs sed -i -e "s/TimeDelta::Millis<\(.*\)>()/TimeDelta::Millis(\1)/g" find . -type f \( -name "*.h" -o -name "*.cc" \) | xargs sed -i -e "s/TimeDelta::Seconds<\(.*\)>()/TimeDelta::Seconds(\1)/g" find . -type f \( -name "*.h" -o -name "*.cc" \) | xargs sed -i -e "s/TimeDelta::us/TimeDelta::Micros/g" find . -type f \( -name "*.h" -o -name "*.cc" \) | xargs sed -i -e "s/TimeDelta::ms/TimeDelta::Millis/g" find . -type f \( -name "*.h" -o -name "*.cc" \) | xargs sed -i -e "s/TimeDelta::seconds/TimeDelta::Seconds/g" find . -type f \( -name "*.h" -o -name "*.cc" \) | xargs sed -i -e "s/Timestamp::Micros<\(.*\)>()/Timestamp::Micros(\1)/g" find . -type f \( -name "*.h" -o -name "*.cc" \) | xargs sed -i -e "s/Timestamp::Millis<\(.*\)>()/Timestamp::Millis(\1)/g" find . -type f \( -name "*.h" -o -name "*.cc" \) | xargs sed -i -e "s/Timestamp::Seconds<\(.*\)>()/Timestamp::Seconds(\1)/g" find . -type f \( -name "*.h" -o -name "*.cc" \) | xargs sed -i -e "s/Timestamp::us/Timestamp::Micros/g" find . -type f \( -name "*.h" -o -name "*.cc" \) | xargs sed -i -e "s/Timestamp::ms/Timestamp::Millis/g" find . -type f \( -name "*.h" -o -name "*.cc" \) | xargs sed -i -e "s/Timestamp::seconds/Timestamp::Seconds/g" git cl format Bug: None Change-Id: I87469d2e4a38369654da839ab7c838215a7911e7 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/168402 Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> Cr-Commit-Position: refs/heads/master@{#30491}
2020-02-10 11:16:00 +01:00
next_run_time_ = at_time + TimeDelta::Millis(delay_ms);
}
}
void SimulatedThread::BlockingCallImpl(rtc::FunctionView<void()> functor,
const Location& /*location*/) {
if (IsQuitting())
return;
if (IsCurrent()) {
functor();
} else {
TaskQueueBase* yielding_from = TaskQueueBase::Current();
handler_->StartYield(yielding_from);
RunReady(Timestamp::MinusInfinity());
CurrentThreadSetter set_current(this);
functor();
handler_->StopYield(yielding_from);
}
}
void SimulatedThread::PostTaskImpl(absl::AnyInvocable<void() &&> task,
const PostTaskTraits& traits,
const Location& location) {
rtc::Thread::PostTaskImpl(std::move(task), traits, location);
MutexLock lock(&lock_);
next_run_time_ = Timestamp::MinusInfinity();
}
void SimulatedThread::PostDelayedTaskImpl(absl::AnyInvocable<void() &&> task,
TimeDelta delay,
const PostDelayedTaskTraits& traits,
const Location& location) {
rtc::Thread::PostDelayedTaskImpl(std::move(task), delay, traits, location);
MutexLock lock(&lock_);
next_run_time_ =
std::min(next_run_time_, Timestamp::Millis(rtc::TimeMillis()) + delay);
}
void SimulatedThread::Stop() {
Thread::Quit();
}
SimulatedMainThread::SimulatedMainThread(
sim_time_impl::SimulatedTimeControllerImpl* handler)
: SimulatedThread(handler, "main", nullptr), current_setter_(this) {}
SimulatedMainThread::~SimulatedMainThread() {
// Removes pending tasks in case they keep shared pointer references to
// objects whose destructor expects to run before the Thread destructor.
Stop();
DoDestroy();
}
} // namespace webrtc