Take FunctionView rather than any functor reference in the rtc::Thread::Invoke
to generate less versions of the function template and FunctorMessageHandler helper thus producing less binary size Bug: None Change-Id: Idbd6fb1e1f23b9b2dc4e4306a74ef11e74ba94cd Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/161044 Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> Cr-Commit-Position: refs/heads/master@{#29962}
This commit is contained in:
parent
39cf3c723e
commit
89313451d8
@ -87,6 +87,7 @@ rtc_library("rtc_pc_base") {
|
||||
"../api:array_view",
|
||||
"../api:audio_options_api",
|
||||
"../api:call_api",
|
||||
"../api:function_view",
|
||||
"../api:ice_transport_factory",
|
||||
"../api:libjingle_peerconnection_api",
|
||||
"../api:rtc_error",
|
||||
|
||||
@ -19,6 +19,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "api/call/audio_sink.h"
|
||||
#include "api/function_view.h"
|
||||
#include "api/jsep.h"
|
||||
#include "api/rtp_receiver_interface.h"
|
||||
#include "api/transport/media/media_transport_config.h"
|
||||
@ -259,8 +260,9 @@ class BaseChannel : public ChannelInterface,
|
||||
void OnMessage(rtc::Message* pmsg) override;
|
||||
|
||||
// Helper function template for invoking methods on the worker thread.
|
||||
template <class T, class FunctorT>
|
||||
T InvokeOnWorker(const rtc::Location& posted_from, const FunctorT& functor) {
|
||||
template <class T>
|
||||
T InvokeOnWorker(const rtc::Location& posted_from,
|
||||
rtc::FunctionView<T()> functor) {
|
||||
return worker_thread_->Invoke<T>(posted_from, functor);
|
||||
}
|
||||
|
||||
|
||||
@ -785,6 +785,7 @@ rtc_library("rtc_base") {
|
||||
":checks",
|
||||
":stringutils",
|
||||
"../api:array_view",
|
||||
"../api:function_view",
|
||||
"../api:scoped_refptr",
|
||||
"../api/task_queue",
|
||||
"network:sent_packet",
|
||||
|
||||
@ -469,11 +469,22 @@ bool Thread::PopSendMessageFromThread(const Thread* source, _SendMessage* msg) {
|
||||
}
|
||||
|
||||
void Thread::InvokeInternal(const Location& posted_from,
|
||||
MessageHandler* handler) {
|
||||
rtc::FunctionView<void()> functor) {
|
||||
TRACE_EVENT2("webrtc", "Thread::Invoke", "src_file_and_line",
|
||||
posted_from.file_and_line(), "src_func",
|
||||
posted_from.function_name());
|
||||
Send(posted_from, handler);
|
||||
|
||||
class FunctorMessageHandler : public MessageHandler {
|
||||
public:
|
||||
explicit FunctorMessageHandler(rtc::FunctionView<void()> functor)
|
||||
: functor_(functor) {}
|
||||
void OnMessage(Message* msg) override { functor_(); }
|
||||
|
||||
private:
|
||||
rtc::FunctionView<void()> functor_;
|
||||
} handler(functor);
|
||||
|
||||
Send(posted_from, &handler);
|
||||
}
|
||||
|
||||
void Thread::QueuedTaskHandler::OnMessage(Message* msg) {
|
||||
|
||||
@ -21,6 +21,7 @@
|
||||
#if defined(WEBRTC_POSIX)
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
#include "api/function_view.h"
|
||||
#include "api/task_queue/queued_task.h"
|
||||
#include "api/task_queue/task_queue_base.h"
|
||||
#include "rtc_base/constructor_magic.h"
|
||||
@ -214,12 +215,20 @@ class RTC_LOCKABLE RTC_EXPORT Thread : public MessageQueue,
|
||||
// See ScopedDisallowBlockingCalls for details.
|
||||
// NOTE: Blocking invokes are DISCOURAGED, consider if what you're doing can
|
||||
// be achieved with PostTask() and callbacks instead.
|
||||
template <class ReturnT, class FunctorT>
|
||||
ReturnT Invoke(const Location& posted_from, FunctorT&& functor) {
|
||||
FunctorMessageHandler<ReturnT, FunctorT> handler(
|
||||
std::forward<FunctorT>(functor));
|
||||
InvokeInternal(posted_from, &handler);
|
||||
return handler.MoveResult();
|
||||
template <
|
||||
class ReturnT,
|
||||
typename = typename std::enable_if<!std::is_void<ReturnT>::value>::type>
|
||||
ReturnT Invoke(const Location& posted_from, FunctionView<ReturnT()> functor) {
|
||||
ReturnT result;
|
||||
InvokeInternal(posted_from, [functor, &result] { result = functor(); });
|
||||
return result;
|
||||
}
|
||||
|
||||
template <
|
||||
class ReturnT,
|
||||
typename = typename std::enable_if<std::is_void<ReturnT>::value>::type>
|
||||
void Invoke(const Location& posted_from, FunctionView<void()> functor) {
|
||||
InvokeInternal(posted_from, functor);
|
||||
}
|
||||
|
||||
// Posts a task to invoke the functor on |this| thread asynchronously, i.e.
|
||||
@ -369,7 +378,8 @@ class RTC_LOCKABLE RTC_EXPORT Thread : public MessageQueue,
|
||||
// Returns true if there is such a message.
|
||||
bool PopSendMessageFromThread(const Thread* source, _SendMessage* msg);
|
||||
|
||||
void InvokeInternal(const Location& posted_from, MessageHandler* handler);
|
||||
void InvokeInternal(const Location& posted_from,
|
||||
rtc::FunctionView<void()> functor);
|
||||
|
||||
std::list<_SendMessage> sendlist_;
|
||||
std::string name_;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user