From 9f7166a8915596fca3656d4b2627dd0bcc27ddaa Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Fri, 6 May 2005 14:43:17 +0200 Subject: Fixed timeout handling in cRwLock::Lock() --- CONTRIBUTORS | 1 + HISTORY | 4 ++++ thread.c | 46 +++++++++++++++++++++++----------------------- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 5e02cc37..2ddbf747 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -765,6 +765,7 @@ Sascha Volkenandt for reporting a problem when starting replay of a recording that has no Dolby Digital audio after switching to a channel that has DD and selecting the DD audio track + for reporting a bug in timeout handling in cRwLock::Lock() Malcolm Caldwell for modifying LOF handling to allow for C-band reception diff --git a/HISTORY b/HISTORY index 71c9bd1d..32ab89fb 100644 --- a/HISTORY +++ b/HISTORY @@ -3496,3 +3496,7 @@ Video Disk Recorder Revision History actually stored. - Fixed handling lifetime when deciding whether to delete a recording (thanks to Udo Richter). +- Fixed timeout handling in cRwLock::Lock() (thanks to Sascha Volkenandt for reporting + this one). +- Since there are several places in thread.c where a timeout value is calculated, + this has been put into a separate function. diff --git a/thread.c b/thread.c index eb009f14..013b2179 100644 --- a/thread.c +++ b/thread.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: thread.c 1.41 2005/01/14 13:59:48 kls Exp $ + * $Id: thread.c 1.42 2005/05/06 14:39:15 kls Exp $ */ #include "thread.h" @@ -17,6 +17,22 @@ #include #include "tools.h" +static bool GetAbsTime(struct timespec *Abstime, int MillisecondsFromNow) +{ + struct timeval now; + if (gettimeofday(&now, NULL) == 0) { // get current time + now.tv_usec += MillisecondsFromNow * 1000; // add the timeout + while (now.tv_usec >= 1000000) { // take care of an overflow + now.tv_sec++; + now.tv_usec -= 1000000; + } + Abstime->tv_sec = now.tv_sec; // seconds + Abstime->tv_nsec = now.tv_usec * 1000; // nano seconds + return true; + } + return false; +} + // --- cCondWait ------------------------------------------------------------- cCondWait::cCondWait(void) @@ -44,15 +60,8 @@ bool cCondWait::Wait(int TimeoutMs) pthread_mutex_lock(&mutex); if (!signaled) { if (TimeoutMs) { - struct timeval now; - if (gettimeofday(&now, NULL) == 0) { // get current time - now.tv_usec += TimeoutMs * 1000; // add the timeout - int sec = now.tv_usec / 1000000; - now.tv_sec += sec; - now.tv_usec -= sec * 1000000; - struct timespec abstime; // build timespec for timedwait - abstime.tv_sec = now.tv_sec; // seconds - abstime.tv_nsec = now.tv_usec * 1000; // nano seconds + struct timespec abstime; + if (GetAbsTime(&abstime, TimeoutMs)) { while (!signaled) { if (pthread_cond_timedwait(&cond, &mutex, &abstime) == ETIMEDOUT) break; @@ -105,17 +114,8 @@ bool cCondVar::TimedWait(cMutex &Mutex, int TimeoutMs) bool r = true; // true = condition signaled false = timeout if (Mutex.locked) { - struct timeval now; // unfortunately timedwait needs the absolute time, not the delta :-( - if (gettimeofday(&now, NULL) == 0) { // get current time - now.tv_usec += TimeoutMs * 1000; // add the timeout - while (now.tv_usec >= 1000000) { // take care of an overflow - now.tv_sec++; - now.tv_usec -= 1000000; - } - struct timespec abstime; // build timespec for timedwait - abstime.tv_sec = now.tv_sec; // seconds - abstime.tv_nsec = now.tv_usec * 1000; // nano seconds - + struct timespec abstime; + if (GetAbsTime(&abstime, TimeoutMs)) { int locked = Mutex.locked; Mutex.locked = 0; // have to clear the locked count here, as pthread_cond_timedwait // does an implizit unlock of the mutex. @@ -150,8 +150,8 @@ bool cRwLock::Lock(bool Write, int TimeoutMs) int Result = 0; struct timespec abstime; if (TimeoutMs) { - abstime.tv_sec = TimeoutMs / 1000; - abstime.tv_nsec = (TimeoutMs % 1000) * 1000000; + if (!GetAbsTime(&abstime, TimeoutMs)) + TimeoutMs = 0; } if (Write) Result = TimeoutMs ? pthread_rwlock_timedwrlock(&rwlock, &abstime) : pthread_rwlock_wrlock(&rwlock); -- cgit v1.2.3