From 6d17602e704f997dbab0afd3b070b6c827257a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Tue, 9 Feb 2021 14:44:48 +0100 Subject: [PATCH] Replace some RecursiveCriticalSection with Mutex, in PhysicalSocketServer. The one remaining RecursiveCriticalSection likely needs a bit more care. Bug: webrtc:11567 Change-Id: Ie81085969197bed03ac8e2d269b58653b86095e0 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/206468 Reviewed-by: Mirko Bonadei Reviewed-by: Markus Handell Commit-Queue: Niels Moller Cr-Commit-Position: refs/heads/master@{#33206} --- rtc_base/BUILD.gn | 1 + rtc_base/physical_socket_server.cc | 17 +++++++++-------- rtc_base/physical_socket_server.h | 5 +++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index 983488f893..65572aa913 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -809,6 +809,7 @@ rtc_library("threading") { "../api:function_view", "../api:scoped_refptr", "../api/task_queue", + "synchronization:mutex", "synchronization:sequence_checker", "system:no_unique_address", "system:rtc_export", diff --git a/rtc_base/physical_socket_server.cc b/rtc_base/physical_socket_server.cc index adf3fab507..ee611a1cf7 100644 --- a/rtc_base/physical_socket_server.cc +++ b/rtc_base/physical_socket_server.cc @@ -48,6 +48,7 @@ #include "rtc_base/logging.h" #include "rtc_base/network_monitor.h" #include "rtc_base/null_socket_server.h" +#include "rtc_base/synchronization/mutex.h" #include "rtc_base/time_utils.h" #if defined(WEBRTC_LINUX) @@ -273,12 +274,12 @@ int PhysicalSocket::DoConnect(const SocketAddress& connect_addr) { } int PhysicalSocket::GetError() const { - CritScope cs(&crit_); + webrtc::MutexLock lock(&mutex_); return error_; } void PhysicalSocket::SetError(int error) { - CritScope cs(&crit_); + webrtc::MutexLock lock(&mutex_); error_ = error; } @@ -934,7 +935,7 @@ class EventDispatcher : public Dispatcher { } virtual void Signal() { - CritScope cs(&crit_); + webrtc::MutexLock lock(&mutex_); if (!fSignaled_) { const uint8_t b[1] = {0}; const ssize_t res = write(afd_[1], b, sizeof(b)); @@ -949,7 +950,7 @@ class EventDispatcher : public Dispatcher { // It is not possible to perfectly emulate an auto-resetting event with // pipes. This simulates it by resetting before the event is handled. - CritScope cs(&crit_); + webrtc::MutexLock lock(&mutex_); if (fSignaled_) { uint8_t b[4]; // Allow for reading more than 1 byte, but expect 1. const ssize_t res = read(afd_[0], b, sizeof(b)); @@ -965,10 +966,10 @@ class EventDispatcher : public Dispatcher { bool IsDescriptorClosed() override { return false; } private: - PhysicalSocketServer* ss_; - int afd_[2]; - bool fSignaled_; - RecursiveCriticalSection crit_; + PhysicalSocketServer* const ss_; + int afd_[2]; // Assigned in constructor only. + bool fSignaled_ RTC_GUARDED_BY(mutex_); + webrtc::Mutex mutex_; }; #endif // WEBRTC_POSIX diff --git a/rtc_base/physical_socket_server.h b/rtc_base/physical_socket_server.h index 5a09aacc9b..f83bf52487 100644 --- a/rtc_base/physical_socket_server.h +++ b/rtc_base/physical_socket_server.h @@ -25,6 +25,7 @@ #include "rtc_base/async_resolver_interface.h" #include "rtc_base/deprecated/recursive_critical_section.h" #include "rtc_base/socket_server.h" +#include "rtc_base/synchronization/mutex.h" #include "rtc_base/system/rtc_export.h" #include "rtc_base/thread_annotations.h" @@ -203,8 +204,8 @@ class PhysicalSocket : public AsyncSocket, public sigslot::has_slots<> { SOCKET s_; bool udp_; int family_ = 0; - RecursiveCriticalSection crit_; - int error_ RTC_GUARDED_BY(crit_); + mutable webrtc::Mutex mutex_; + int error_ RTC_GUARDED_BY(mutex_); ConnState state_; AsyncResolver* resolver_;