From 689b5874d4fb5ee1767aef0d05e0a929ac2ea46e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Wed, 29 Aug 2018 09:55:44 +0200 Subject: [PATCH] Use monotonic clock for PhysicalSocketServer timeouts. Bug: webrtc:9684 Change-Id: Ia50e2d8f8100364ca8047b9b6cf55674206d8d8b Reviewed-on: https://webrtc-review.googlesource.com/96680 Reviewed-by: Karl Wiberg Commit-Queue: Niels Moller Cr-Commit-Position: refs/heads/master@{#24522} --- rtc_base/physicalsocketserver.cc | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/rtc_base/physicalsocketserver.cc b/rtc_base/physicalsocketserver.cc index ca78499179..9a3fc4dfd4 100644 --- a/rtc_base/physicalsocketserver.cc +++ b/rtc_base/physicalsocketserver.cc @@ -1390,21 +1390,15 @@ bool PhysicalSocketServer::WaitSelect(int cmsWait, bool process_io) { struct timeval* ptvWait = nullptr; struct timeval tvWait; - struct timeval tvStop; + int64_t stop_us; if (cmsWait != kForever) { // Calculate wait timeval tvWait.tv_sec = cmsWait / 1000; tvWait.tv_usec = (cmsWait % 1000) * 1000; ptvWait = &tvWait; - // Calculate when to return in a timeval - gettimeofday(&tvStop, nullptr); - tvStop.tv_sec += tvWait.tv_sec; - tvStop.tv_usec += tvWait.tv_usec; - if (tvStop.tv_usec >= 1000000) { - tvStop.tv_usec -= 1000000; - tvStop.tv_sec += 1; - } + // Calculate when to return + stop_us = rtc::TimeMicros() + cmsWait * 1000; } // Zero all fd_sets. Don't need to do this inside the loop since @@ -1501,17 +1495,10 @@ bool PhysicalSocketServer::WaitSelect(int cmsWait, bool process_io) { if (ptvWait) { ptvWait->tv_sec = 0; ptvWait->tv_usec = 0; - struct timeval tvT; - gettimeofday(&tvT, nullptr); - if ((tvStop.tv_sec > tvT.tv_sec) || - ((tvStop.tv_sec == tvT.tv_sec) && (tvStop.tv_usec > tvT.tv_usec))) { - ptvWait->tv_sec = tvStop.tv_sec - tvT.tv_sec; - ptvWait->tv_usec = tvStop.tv_usec - tvT.tv_usec; - if (ptvWait->tv_usec < 0) { - RTC_DCHECK(ptvWait->tv_sec > 0); - ptvWait->tv_usec += 1000000; - ptvWait->tv_sec -= 1; - } + int64_t time_left_us = stop_us - rtc::TimeMicros(); + if (time_left_us > 0) { + ptvWait->tv_sec = time_left_us / rtc::kNumMicrosecsPerSec; + ptvWait->tv_usec = time_left_us % rtc::kNumMicrosecsPerSec; } } }