From 75da451418ba6092424f1f7e87f75da72e99a3f5 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Mon, 16 Oct 2006 23:20:59 +0000 Subject: Use more accurate sleeps in poll() to ensure that we don't end up busy waiting for the last fraction of a second with repeated poll(..., 0). (refs #3) --- lib/server/SocketStreamTLS.cpp | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'lib/server') diff --git a/lib/server/SocketStreamTLS.cpp b/lib/server/SocketStreamTLS.cpp index af4ad460..58dc5754 100644 --- a/lib/server/SocketStreamTLS.cpp +++ b/lib/server/SocketStreamTLS.cpp @@ -23,6 +23,7 @@ #include "SSLLib.h" #include "ServerException.h" #include "TLSContext.h" +#include "BoxTime.h" #include "MemLeakFindOn.h" @@ -244,20 +245,30 @@ bool SocketStreamTLS::WaitWhenRetryRequired(int SSLErrorCode, int Timeout) break; } p.revents = 0; - switch(::poll(&p, 1, (Timeout == IOStream::TimeOutInfinite)?INFTIM:Timeout)) + + int64_t start, end; + start = BoxTimeToMilliSeconds(GetCurrentBoxTime()); + end = start + Timeout; + int result; + + do { - case -1: - // error - if(errno == EINTR) - { - // Signal. Do "time out" - return false; - } - else + int64_t now = BoxTimeToMilliSeconds(GetCurrentBoxTime()); + int poll_timeout = (int)(end - now); + if (poll_timeout < 0) poll_timeout = 0; + if (Timeout == IOStream::TimeOutInfinite) { - // Bad! - THROW_EXCEPTION(ServerException, SocketPollError) + poll_timeout = INFTIM; } + result = ::poll(&p, 1, poll_timeout); + } + while(result == -1 && errno == EINTR); + + switch(result) + { + case -1: + // error - Bad! + THROW_EXCEPTION(ServerException, SocketPollError) break; case 0: -- cgit v1.2.3