From bff717e4f9b057900ae716524d74e6bb97b2ef27 Mon Sep 17 00:00:00 2001 From: Tomas Gunnarsson Date: Wed, 18 Nov 2020 09:38:14 +0100 Subject: [PATCH] Remove dependency on AsyncInvoker in SctpTransport Bug: webrtc:11988 Change-Id: I996aa220a00b61fb5080803bffe7a37c6b90aaec Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/193866 Reviewed-by: Harald Alvestrand Commit-Queue: Tommi Cr-Commit-Position: refs/heads/master@{#32645} --- media/BUILD.gn | 2 ++ media/sctp/sctp_transport.cc | 29 +++++++++++-------- media/sctp/sctp_transport.h | 4 +-- .../sctp_transport_reliability_unittest.cc | 1 + pc/sctp_transport.cc | 2 ++ 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/media/BUILD.gn b/media/BUILD.gn index 212ccf9eae..f5f1dfa0e0 100644 --- a/media/BUILD.gn +++ b/media/BUILD.gn @@ -408,6 +408,8 @@ rtc_library("rtc_data") { "../rtc_base", "../rtc_base:rtc_base_approved", "../rtc_base/synchronization:mutex", + "../rtc_base/task_utils:pending_task_safety_flag", + "../rtc_base/task_utils:to_queued_task", "../rtc_base/third_party/sigslot", "../system_wrappers", ] diff --git a/media/sctp/sctp_transport.cc b/media/sctp/sctp_transport.cc index 7c2eee3b13..eb10ad20dc 100644 --- a/media/sctp/sctp_transport.cc +++ b/media/sctp/sctp_transport.cc @@ -46,6 +46,7 @@ constexpr int kSctpSuccessReturn = 1; #include "rtc_base/numerics/safe_conversions.h" #include "rtc_base/string_utils.h" #include "rtc_base/synchronization/mutex.h" +#include "rtc_base/task_utils/to_queued_task.h" #include "rtc_base/thread_annotations.h" #include "rtc_base/thread_checker.h" #include "rtc_base/trace_event.h" @@ -386,11 +387,12 @@ class SctpTransport::UsrSctpWrapper { VerboseLogPacket(data, length, SCTP_DUMP_OUTBOUND); // Note: We have to copy the data; the caller will delete it. rtc::CopyOnWriteBuffer buf(reinterpret_cast(data), length); - // TODO(deadbeef): Why do we need an AsyncInvoke here? We're already on the - // right thread and don't need to unwind the stack. - transport->invoker_.AsyncInvoke( - RTC_FROM_HERE, transport->network_thread_, - rtc::Bind(&SctpTransport::OnPacketFromSctpToNetwork, transport, buf)); + + transport->network_thread_->PostTask(ToQueuedTask( + transport->task_safety_, [transport, buf = std::move(buf)]() { + transport->OnPacketFromSctpToNetwork(buf); + })); + return 0; } @@ -497,6 +499,7 @@ SctpTransport::SctpTransport(rtc::Thread* network_thread, } SctpTransport::~SctpTransport() { + RTC_DCHECK_RUN_ON(network_thread_); // Close abruptly; no reset procedure. CloseSctpSocket(); // It's not strictly necessary to reset these fields to nullptr, @@ -1165,9 +1168,10 @@ int SctpTransport::OnDataOrNotificationFromSctp(void* data, // Copy and dispatch asynchronously rtc::CopyOnWriteBuffer notification(reinterpret_cast(data), length); - invoker_.AsyncInvoke( - RTC_FROM_HERE, network_thread_, - rtc::Bind(&SctpTransport::OnNotificationFromSctp, this, notification)); + network_thread_->PostTask(ToQueuedTask( + task_safety_, [this, notification = std::move(notification)]() { + OnNotificationFromSctp(notification); + })); return kSctpSuccessReturn; } @@ -1239,10 +1243,11 @@ int SctpTransport::OnDataOrNotificationFromSctp(void* data, // Dispatch the complete message. // The ownership of the packet transfers to |invoker_|. Using // CopyOnWriteBuffer is the most convenient way to do this. - invoker_.AsyncInvoke( - RTC_FROM_HERE, network_thread_, - rtc::Bind(&SctpTransport::OnDataFromSctpToTransport, this, params, - partial_incoming_message_)); + network_thread_->PostTask(webrtc::ToQueuedTask( + task_safety_, [this, params = std::move(params), + message = partial_incoming_message_]() { + OnDataFromSctpToTransport(params, message); + })); // Reset the message buffer partial_incoming_message_.Clear(); diff --git a/media/sctp/sctp_transport.h b/media/sctp/sctp_transport.h index 54542af6b3..44a895512e 100644 --- a/media/sctp/sctp_transport.h +++ b/media/sctp/sctp_transport.h @@ -22,10 +22,10 @@ #include "absl/types/optional.h" #include "api/transport/sctp_transport_factory_interface.h" -#include "rtc_base/async_invoker.h" #include "rtc_base/buffer.h" #include "rtc_base/constructor_magic.h" #include "rtc_base/copy_on_write_buffer.h" +#include "rtc_base/task_utils/pending_task_safety_flag.h" #include "rtc_base/third_party/sigslot/sigslot.h" #include "rtc_base/thread.h" // For SendDataParams/ReceiveDataParams. @@ -199,7 +199,7 @@ class SctpTransport : public SctpTransportInternal, // outgoing data to the network interface. rtc::Thread* network_thread_; // Helps pass inbound/outbound packets asynchronously to the network thread. - rtc::AsyncInvoker invoker_; + webrtc::ScopedTaskSafety task_safety_; // Underlying DTLS transport. rtc::PacketTransportInternal* transport_ = nullptr; diff --git a/media/sctp/sctp_transport_reliability_unittest.cc b/media/sctp/sctp_transport_reliability_unittest.cc index e5dbf2933d..80b7d61215 100644 --- a/media/sctp/sctp_transport_reliability_unittest.cc +++ b/media/sctp/sctp_transport_reliability_unittest.cc @@ -14,6 +14,7 @@ #include #include "media/sctp/sctp_transport_internal.h" +#include "rtc_base/async_invoker.h" #include "rtc_base/copy_on_write_buffer.h" #include "rtc_base/gunit.h" #include "rtc_base/logging.h" diff --git a/pc/sctp_transport.cc b/pc/sctp_transport.cc index ea1165f94a..9450469b8e 100644 --- a/pc/sctp_transport.cc +++ b/pc/sctp_transport.cc @@ -13,6 +13,8 @@ #include #include +#include "rtc_base/bind.h" + namespace webrtc { SctpTransport::SctpTransport(