Use monotonic clock for PhysicalSocketServer timeouts.

Bug: webrtc:9684
Change-Id: Ia50e2d8f8100364ca8047b9b6cf55674206d8d8b
Reviewed-on: https://webrtc-review.googlesource.com/96680
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24522}
This commit is contained in:
Niels Möller
2018-08-29 09:55:44 +02:00
committed by Commit Bot
parent 1606d5eee8
commit 689b5874d4

View File

@ -1390,21 +1390,15 @@ bool PhysicalSocketServer::WaitSelect(int cmsWait, bool process_io) {
struct timeval* ptvWait = nullptr; struct timeval* ptvWait = nullptr;
struct timeval tvWait; struct timeval tvWait;
struct timeval tvStop; int64_t stop_us;
if (cmsWait != kForever) { if (cmsWait != kForever) {
// Calculate wait timeval // Calculate wait timeval
tvWait.tv_sec = cmsWait / 1000; tvWait.tv_sec = cmsWait / 1000;
tvWait.tv_usec = (cmsWait % 1000) * 1000; tvWait.tv_usec = (cmsWait % 1000) * 1000;
ptvWait = &tvWait; ptvWait = &tvWait;
// Calculate when to return in a timeval // Calculate when to return
gettimeofday(&tvStop, nullptr); stop_us = rtc::TimeMicros() + cmsWait * 1000;
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;
}
} }
// Zero all fd_sets. Don't need to do this inside the loop since // 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) { if (ptvWait) {
ptvWait->tv_sec = 0; ptvWait->tv_sec = 0;
ptvWait->tv_usec = 0; ptvWait->tv_usec = 0;
struct timeval tvT; int64_t time_left_us = stop_us - rtc::TimeMicros();
gettimeofday(&tvT, nullptr); if (time_left_us > 0) {
if ((tvStop.tv_sec > tvT.tv_sec) || ptvWait->tv_sec = time_left_us / rtc::kNumMicrosecsPerSec;
((tvStop.tv_sec == tvT.tv_sec) && (tvStop.tv_usec > tvT.tv_usec))) { ptvWait->tv_usec = time_left_us % rtc::kNumMicrosecsPerSec;
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;
}
} }
} }
} }