From 9f51fcad223572c459e7fc0705dfba44da38aaad Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Sun, 24 Oct 2004 11:12:05 +0200 Subject: [PATCH] Added cCondWait::Sleep() and using it to replace all usleep() calls --- CONTRIBUTORS | 1 + HISTORY | 2 ++ diseqc.c | 5 +++-- dvbdevice.c | 4 ++-- sections.c | 5 +++-- thread.c | 12 +++++++++--- thread.h | 6 +++++- 7 files changed, 25 insertions(+), 10 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index a7e0d386..86f1e289 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -257,6 +257,7 @@ Werner Fink for modifying handling of audio packets in cDvbPlayer for better sync with external AC3 replay for changing thread handling to make it work with NPTL ("Native Posix Thread Library") + for suggesting to replace usleep() calls with a pthread_cond_timedwait() based wait Rolf Hakenes for providing 'libdtv' and adapting the EIT mechanisms to it diff --git a/HISTORY b/HISTORY index d19847e2..659a59af 100644 --- a/HISTORY +++ b/HISTORY @@ -3069,3 +3069,5 @@ Video Disk Recorder Revision History cThread::Start() (suggested by Ludwig Nussel). Also removed 'running' from cThread and using only childTid to indicate whether a thread is actually running. +- Added cCondWait::Sleep() and using it to replace all usleep() calls (based + on a suggestion by Werner Fink). diff --git a/diseqc.c b/diseqc.c index 0766db8a..801e1cd8 100644 --- a/diseqc.c +++ b/diseqc.c @@ -4,12 +4,13 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: diseqc.c 1.2 2002/12/07 13:44:56 kls Exp $ + * $Id: diseqc.c 1.3 2004/10/24 11:04:56 kls Exp $ */ #include "diseqc.h" #include #include "sources.h" +#include "thread.h" // -- cDiseqc ---------------------------------------------------------------- @@ -61,7 +62,7 @@ char *cDiseqc::Wait(char *s) int n = strtol(s, &p, 10); if (!errno && p != s && n >= 0) { if (!parsing) - usleep(n * 1000); + cCondWait::SleepMs(n); return p; } esyslog("ERROR: illegal value for wait time in '%s'", s - 1); diff --git a/dvbdevice.c b/dvbdevice.c index bfe2aaf6..3f38a0a4 100644 --- a/dvbdevice.c +++ b/dvbdevice.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: dvbdevice.c 1.99 2004/10/24 08:50:15 kls Exp $ + * $Id: dvbdevice.c 1.100 2004/10/24 11:06:37 kls Exp $ */ #include "dvbdevice.h" @@ -1075,7 +1075,7 @@ void cDvbDevice::StillPicture(const uchar *Data, int Length) #define MIN_IFRAME 400000 for (int i = MIN_IFRAME / Length + 1; i > 0; i--) { safe_write(fd_video, Data, Length); - usleep(1); // allows the buffer to be displayed in case the progress display is active + cCondWait::SleepMs(1); // allows the buffer to be displayed in case the progress display is active } #endif } diff --git a/sections.c b/sections.c index 64923367..7c42a6ef 100644 --- a/sections.c +++ b/sections.c @@ -4,13 +4,14 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: sections.c 1.9 2004/10/16 13:45:02 kls Exp $ + * $Id: sections.c 1.10 2004/10/24 11:05:12 kls Exp $ */ #include "sections.h" #include #include "channels.h" #include "device.h" +#include "thread.h" // --- cFilterHandle---------------------------------------------------------- @@ -185,7 +186,7 @@ void cSectionHandler::Action(void) if (poll(pfd, NumFilters, 1000) > 0) { bool DeviceHasLock = device->HasLock(); if (!DeviceHasLock) - usleep(100000); + cCondWait::SleepMs(100); for (int i = 0; i < NumFilters; i++) { if (pfd[i].revents & POLLIN) { cFilterHandle *fh = NULL; diff --git a/thread.c b/thread.c index 730fa733..144563df 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.34 2004/10/24 10:27:47 kls Exp $ + * $Id: thread.c 1.35 2004/10/24 11:05:56 kls Exp $ */ #include "thread.h" @@ -32,6 +32,12 @@ cCondWait::~cCondWait() pthread_mutex_destroy(&mutex); } +void cCondWait::SleepMs(int TimeoutMs) +{ + cCondWait w; + w.Wait(TimeoutMs); +} + bool cCondWait::Wait(int TimeoutMs) { pthread_mutex_lock(&mutex); @@ -265,7 +271,7 @@ void cThread::Cancel(int WaitSeconds) for (time_t t0 = time(NULL) + WaitSeconds; time(NULL) < t0; ) { if (!Active()) return; - usleep(10000); + cCondWait::SleepMs(10); } esyslog("ERROR: thread %ld won't end (waited %d seconds) - cancelling it...", childTid, WaitSeconds); } @@ -433,7 +439,7 @@ int cPipe::Close(void) else if (ret == pid) break; i--; - usleep(100000); + cCondWait::SleepMs(100); } if (!i) { kill(pid, SIGKILL); diff --git a/thread.h b/thread.h index da49bcdd..2cc623bf 100644 --- a/thread.h +++ b/thread.h @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: thread.h 1.23 2004/10/24 10:28:48 kls Exp $ + * $Id: thread.h 1.24 2004/10/24 11:00:32 kls Exp $ */ #ifndef __THREAD_H @@ -22,6 +22,10 @@ private: public: cCondWait(void); ~cCondWait(); + static void SleepMs(int TimeoutMs); + ///< Creates a cCondWait object and uses it to sleep for TimeoutMs + ///< milliseconds, immediately giving up the calling thread's time + ///< slice and thus avoiding a "busy wait". bool Wait(int TimeoutMs = 0); ///< Waits at most TimeoutMs milliseconds for a call to Signal(), or ///< forever if TimeoutMs is 0.