Make MessageHandler cleanup optional.
As documented in webrtc:11908 this cleanup is fairly invasive and
when a part of a frequently executed code path, can be quite costly
in terms of performance overhead. This is currently the case with
synchronous calls between threads (Thread) as well with our proxy
api classes.
With this CL, all code in WebRTC should now either be using MessageHandlerAutoCleanup
or calling MessageHandler(false) explicitly.
Next steps will be to update external code to either depend on the
AutoCleanup variant, or call MessageHandler(false).
Changing the proxy classes to use TaskQueue set of concepts instead of
MessageHandler. This avoids the perf overhead related to the cleanup
above as well as incompatibility with the thread policy checks in
Thread that some current external users of the proxies would otherwise
run into (if we were to use Thread::Send() for synchronous call).
Following this we'll move the cleanup step into the AutoCleanup class
and an RTC_DCHECK that all calls to the MessageHandler are setting
the flag to false, before eventually removing the flag and make
MessageHandler pure virtual.
Bug: webrtc:11908
Change-Id: Idf4ff9bcc8438cb8c583777e282005e0bc511c8f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/183442
Reviewed-by: Artem Titov <titovartem@webrtc.org>
Commit-Queue: Tommi <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#32049}
diff --git a/api/proxy.cc b/api/proxy.cc
index e668285..67318e7 100644
--- a/api/proxy.cc
+++ b/api/proxy.cc
@@ -10,28 +10,3 @@
#include "api/proxy.h"
-namespace webrtc {
-namespace internal {
-
-SynchronousMethodCall::SynchronousMethodCall(rtc::MessageHandler* proxy)
- : proxy_(proxy) {}
-
-SynchronousMethodCall::~SynchronousMethodCall() = default;
-
-void SynchronousMethodCall::Invoke(const rtc::Location& posted_from,
- rtc::Thread* t) {
- if (t->IsCurrent()) {
- proxy_->OnMessage(nullptr);
- } else {
- t->Post(posted_from, this, 0);
- e_.Wait(rtc::Event::kForever);
- }
-}
-
-void SynchronousMethodCall::OnMessage(rtc::Message*) {
- proxy_->OnMessage(nullptr);
- e_.Set();
-}
-
-} // namespace internal
-} // namespace webrtc
diff --git a/api/proxy.h b/api/proxy.h
index 0e5d622..05f7414 100644
--- a/api/proxy.h
+++ b/api/proxy.h
@@ -59,6 +59,8 @@
#include <utility>
#include "api/scoped_refptr.h"
+#include "api/task_queue/queued_task.h"
+#include "api/task_queue/task_queue_base.h"
#include "rtc_base/event.h"
#include "rtc_base/message_handler.h"
#include "rtc_base/ref_counted_object.h"
@@ -96,27 +98,8 @@
void moved_result() {}
};
-namespace internal {
-
-class RTC_EXPORT SynchronousMethodCall : public rtc::MessageData,
- public rtc::MessageHandler {
- public:
- explicit SynchronousMethodCall(rtc::MessageHandler* proxy);
- ~SynchronousMethodCall() override;
-
- void Invoke(const rtc::Location& posted_from, rtc::Thread* t);
-
- private:
- void OnMessage(rtc::Message*) override;
-
- rtc::Event e_;
- rtc::MessageHandler* proxy_;
-};
-
-} // namespace internal
-
template <typename C, typename R, typename... Args>
-class MethodCall : public rtc::Message, public rtc::MessageHandler {
+class MethodCall : public QueuedTask {
public:
typedef R (C::*Method)(Args...);
MethodCall(C* c, Method m, Args&&... args)
@@ -125,12 +108,21 @@
args_(std::forward_as_tuple(std::forward<Args>(args)...)) {}
R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
- internal::SynchronousMethodCall(this).Invoke(posted_from, t);
+ if (t->IsCurrent()) {
+ Invoke(std::index_sequence_for<Args...>());
+ } else {
+ t->PostTask(std::unique_ptr<QueuedTask>(this));
+ event_.Wait(rtc::Event::kForever);
+ }
return r_.moved_result();
}
private:
- void OnMessage(rtc::Message*) { Invoke(std::index_sequence_for<Args...>()); }
+ bool Run() override {
+ Invoke(std::index_sequence_for<Args...>());
+ event_.Set();
+ return false;
+ }
template <size_t... Is>
void Invoke(std::index_sequence<Is...>) {
@@ -141,10 +133,11 @@
Method m_;
ReturnType<R> r_;
std::tuple<Args&&...> args_;
+ rtc::Event event_;
};
template <typename C, typename R, typename... Args>
-class ConstMethodCall : public rtc::Message, public rtc::MessageHandler {
+class ConstMethodCall : public QueuedTask {
public:
typedef R (C::*Method)(Args...) const;
ConstMethodCall(const C* c, Method m, Args&&... args)
@@ -153,12 +146,21 @@
args_(std::forward_as_tuple(std::forward<Args>(args)...)) {}
R Marshal(const rtc::Location& posted_from, rtc::Thread* t) {
- internal::SynchronousMethodCall(this).Invoke(posted_from, t);
+ if (t->IsCurrent()) {
+ Invoke(std::index_sequence_for<Args...>());
+ } else {
+ t->PostTask(std::unique_ptr<QueuedTask>(this));
+ event_.Wait(rtc::Event::kForever);
+ }
return r_.moved_result();
}
private:
- void OnMessage(rtc::Message*) { Invoke(std::index_sequence_for<Args...>()); }
+ bool Run() override {
+ Invoke(std::index_sequence_for<Args...>());
+ event_.Set();
+ return false;
+ }
template <size_t... Is>
void Invoke(std::index_sequence<Is...>) {
@@ -169,6 +171,7 @@
Method m_;
ReturnType<R> r_;
std::tuple<Args&&...> args_;
+ rtc::Event event_;
};
// Helper macros to reduce code duplication.