From d86c0cdbde7261deefc5771f41a14186bac9fd09 Mon Sep 17 00:00:00 2001 From: Byoungchan Lee Date: Tue, 16 Apr 2024 11:06:08 +0900 Subject: [PATCH] Extend use of poll() to Apple systems in addition to Linux and Fuchsia To address the limitations of select(), poll() is now used for Apple (MacOS and iOS) systems in the PhysicalSocketServer. Bug: webrtc:15421, webrtc:15908 Change-Id: Ic6703a08653ca608a714ea37ecbbfeaf29743c1f Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/316480 Reviewed-by: Harald Alvestrand Reviewed-by: Tomas Gunnarsson Commit-Queue: Daniel.L (Byoungchan) Lee Cr-Commit-Position: refs/heads/main@{#42075} --- rtc_base/physical_socket_server.cc | 18 +++++++++++++----- rtc_base/physical_socket_server.h | 7 ++----- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/rtc_base/physical_socket_server.cc b/rtc_base/physical_socket_server.cc index cd4f854aa8..787e942f60 100644 --- a/rtc_base/physical_socket_server.cc +++ b/rtc_base/physical_socket_server.cc @@ -98,15 +98,15 @@ int64_t GetSocketRecvTimestamp(int socket) { typedef char* SockOptArg; #endif -#if defined(WEBRTC_USE_EPOLL) +#if defined(WEBRTC_LINUX) // POLLRDHUP / EPOLLRDHUP are only defined starting with Linux 2.6.17. #if !defined(POLLRDHUP) #define POLLRDHUP 0x2000 -#endif +#endif // !defined(POLLRDHUP) #if !defined(EPOLLRDHUP) #define EPOLLRDHUP 0x2000 -#endif -#endif +#endif // !defined(EPOLLRDHUP) +#endif // defined(WEBRTC_LINUX) namespace { @@ -1489,7 +1489,15 @@ static void ProcessEvents(Dispatcher* dispatcher, static void ProcessPollEvents(Dispatcher* dispatcher, const pollfd& pfd) { bool readable = (pfd.revents & (POLLIN | POLLPRI)); bool writable = (pfd.revents & POLLOUT); - bool error = (pfd.revents & (POLLRDHUP | POLLERR | POLLHUP)); + + // Linux and Fuchsia define POLLRDHUP, which is set when the peer has + // disconnected. On other platforms, we only check for POLLHUP. +#if defined(WEBRTC_LINUX) || defined(WEBRTC_FUCHSIA) + constexpr short kEvents = POLLRDHUP | POLLERR | POLLHUP; +#else + constexpr short kEvents = POLLERR | POLLHUP; +#endif + bool error = (pfd.revents & kEvents); ProcessEvents(dispatcher, readable, writable, error, error); } diff --git a/rtc_base/physical_socket_server.h b/rtc_base/physical_socket_server.h index ec30281e90..b8cf0420af 100644 --- a/rtc_base/physical_socket_server.h +++ b/rtc_base/physical_socket_server.h @@ -23,7 +23,7 @@ #include #define WEBRTC_USE_EPOLL 1 -#elif defined(WEBRTC_FUCHSIA) +#elif defined(WEBRTC_FUCHSIA) || defined(WEBRTC_MAC) // Fuchsia implements select and poll but not epoll, and testing shows that poll // is faster than select. #include @@ -31,7 +31,7 @@ #define WEBRTC_USE_POLL 1 #else // On other POSIX systems, use select by default. -#endif // WEBRTC_LINUX, WEBRTC_FUCHSIA +#endif // WEBRTC_LINUX, WEBRTC_FUCHSIA, WEBRTC_MAC #endif // WEBRTC_POSIX #include @@ -125,9 +125,6 @@ class RTC_EXPORT PhysicalSocketServer : public SocketServer { const int epoll_fd_ = INVALID_SOCKET; #elif defined(WEBRTC_USE_POLL) - void AddPoll(Dispatcher* dispatcher, uint64_t key); - void RemovePoll(Dispatcher* dispatcher); - void UpdatePoll(Dispatcher* dispatcher, uint64_t key); bool WaitPoll(int cmsWait, bool process_io); #endif // WEBRTC_USE_EPOLL, WEBRTC_USE_POLL