From 70e60380562fb25a7acce9b47c75842d60722dd0 Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Sun, 24 Oct 2004 10:34:20 +0200 Subject: Added some checks when cancelling a thread and removed the usleep() in cThread::Start(); removed 'running' from cThread --- CONTRIBUTORS | 2 ++ HISTORY | 4 ++++ thread.c | 29 +++++++++++++++-------------- thread.h | 3 +-- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 4e05f37c..a7e0d386 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -742,6 +742,8 @@ Ludwig Nussel for reporting a problem on systems that have UTF-8 enabled for pointing out a flaw in the the description of cRingBufferLinear for reporting a bug in cRingBufferLinear::Get() in case the buffer wraps around + for adding some checks when cancelling a thread and removing the usleep() in + cThread::Start() Thomas Koch for his support in keeping the Premiere World channels up to date in 'channels.conf' diff --git a/HISTORY b/HISTORY index c4668ff4..d19847e2 100644 --- a/HISTORY +++ b/HISTORY @@ -3065,3 +3065,7 @@ Video Disk Recorder Revision History small packet. - Removed the signal handler and WakeUp() call from cThread (it is no longer needed). +- Added some checks when cancelling a thread and removed the usleep() in + cThread::Start() (suggested by Ludwig Nussel). Also removed 'running' from + cThread and using only childTid to indicate whether a thread is actually + running. diff --git a/thread.c b/thread.c index a646b3f1..730fa733 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.33 2004/10/24 09:47:57 kls Exp $ + * $Id: thread.c 1.34 2004/10/24 10:27:47 kls Exp $ */ #include "thread.h" @@ -189,7 +189,6 @@ bool cThread::emergencyExitRequested = false; cThread::cThread(const char *Description) { - running = false; parentTid = childTid = 0; description = NULL; SetDescription(Description); @@ -226,13 +225,11 @@ void *cThread::StartThread(cThread *Thread) bool cThread::Start(void) { - if (!running) { - running = true; + if (!childTid) { parentTid = pthread_self(); pthread_create(&childTid, NULL, (void *(*) (void *))&StartThread, (void *)this); pthread_detach(childTid); // auto-reap pthread_setschedparam(childTid, SCHED_RR, 0); - usleep(10000); // otherwise calling Active() immediately after Start() causes a "pure virtual method called" error } return true; //XXX return value of pthread_create()??? } @@ -263,16 +260,20 @@ bool cThread::Active(void) void cThread::Cancel(int WaitSeconds) { - running = false; - if (WaitSeconds > 0) { - for (time_t t0 = time(NULL) + WaitSeconds; time(NULL) < t0; ) { - if (!Active()) - return; - usleep(10000); - } - esyslog("ERROR: thread %ld won't end (waited %d seconds) - cancelling it...", childTid, WaitSeconds); + if (childTid) { + if (WaitSeconds > 0) { + for (time_t t0 = time(NULL) + WaitSeconds; time(NULL) < t0; ) { + if (!Active()) + return; + usleep(10000); + } + esyslog("ERROR: thread %ld won't end (waited %d seconds) - cancelling it...", childTid, WaitSeconds); + } + if (childTid) { + pthread_cancel(childTid); + childTid = 0; + } } - pthread_cancel(childTid); } bool cThread::EmergencyExit(bool Request) diff --git a/thread.h b/thread.h index d57f0b6b..da49bcdd 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.22 2004/10/24 09:46:36 kls Exp $ + * $Id: thread.h 1.23 2004/10/24 10:28:48 kls Exp $ */ #ifndef __THREAD_H @@ -71,7 +71,6 @@ class cThread { private: pthread_t parentTid, childTid; cMutex mutex; - bool running; char *description; static bool emergencyExitRequested; static void *StartThread(cThread *Thread); -- cgit v1.2.3