1
0
mirror of https://github.com/VDR4Arch/vdr.git synced 2023-10-10 13:36:52 +02:00

Fixed a race condition in starting a thread

This commit is contained in:
Klaus Schmidinger 2004-12-19 10:58:20 +01:00
parent f5b1a74056
commit 9a8ef2b6c2
4 changed files with 30 additions and 29 deletions

View File

@ -897,6 +897,7 @@ Reinhard Nissl <rnissl@gmx.de>
to clear the transfer buffer to avoid overflows to clear the transfer buffer to avoid overflows
for reporting a high CPU load in still picture mode after removing the usleep() for reporting a high CPU load in still picture mode after removing the usleep()
call from cDvbPlayer::Action() call from cDvbPlayer::Action()
for reporting a race condition in starting a thread
Richard Robson <richard_robson@beeb.net> Richard Robson <richard_robson@beeb.net>
for reporting freezing replay if a timer starts while in Transfer Mode from the for reporting freezing replay if a timer starts while in Transfer Mode from the

View File

@ -3207,3 +3207,5 @@ Video Disk Recorder Revision History
- Switched the character set to iso8859-15 for English, German and Finnish (thanks - Switched the character set to iso8859-15 for English, German and Finnish (thanks
to Andreas Brugger for reporting the missing Euro sign in iso8859-1). to Andreas Brugger for reporting the missing Euro sign in iso8859-1).
- Added 'channels.conf.terr' entries for Lübeck (thanks to Stefan Hußfeldt). - Added 'channels.conf.terr' entries for Lübeck (thanks to Stefan Hußfeldt).
- Fixed a race condition in starting a thread (thanks to Reinhard Nissl for
reporting this one).

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * how to reach the author.
* *
* $Id: thread.c 1.39 2004/11/26 14:16:07 kls Exp $ * $Id: thread.c 1.40 2004/12/19 10:43:14 kls Exp $
*/ */
#include "thread.h" #include "thread.h"
@ -197,7 +197,8 @@ bool cThread::emergencyExitRequested = false;
cThread::cThread(const char *Description) cThread::cThread(const char *Description)
{ {
parentTid = childTid = 0; running = false;
childTid = 0;
description = NULL; description = NULL;
SetDescription(Description); SetDescription(Description);
} }
@ -221,35 +222,35 @@ void cThread::SetDescription(const char *Description, ...)
void *cThread::StartThread(cThread *Thread) void *cThread::StartThread(cThread *Thread)
{ {
Thread->childTidMutex.Lock();
Thread->childTid = pthread_self();
Thread->childTidMutex.Unlock();
if (Thread->description) if (Thread->description)
dsyslog("%s thread started (pid=%d, tid=%ld)", Thread->description, getpid(), Thread->childTid); dsyslog("%s thread started (pid=%d, tid=%ld)", Thread->description, getpid(), pthread_self());
Thread->Action(); Thread->Action();
if (Thread->description) if (Thread->description)
dsyslog("%s thread ended (pid=%d, tid=%ld)", Thread->description, getpid(), Thread->childTid); dsyslog("%s thread ended (pid=%d, tid=%ld)", Thread->description, getpid(), pthread_self());
Thread->childTidMutex.Lock(); Thread->running = false;
Thread->childTid = 0;
Thread->childTidMutex.Unlock();
return NULL; return NULL;
} }
bool cThread::Start(void) bool cThread::Start(void)
{ {
if (!childTid) { if (!running) {
parentTid = pthread_self(); running = true;
pthread_t Tid; if (pthread_create(&childTid, NULL, (void *(*) (void *))&StartThread, (void *)this) == 0) {
pthread_create(&Tid, NULL, (void *(*) (void *))&StartThread, (void *)this); pthread_detach(childTid); // auto-reap
pthread_detach(Tid); // auto-reap pthread_setschedparam(childTid, SCHED_RR, 0);
pthread_setschedparam(Tid, SCHED_RR, 0); }
else {
LOG_ERROR;
running = false;
return false;
}
} }
return true; //XXX return value of pthread_create()??? return true;
} }
bool cThread::Active(void) bool cThread::Active(void)
{ {
if (childTid) { if (running) {
// //
// Single UNIX Spec v2 says: // Single UNIX Spec v2 says:
// //
@ -259,12 +260,12 @@ bool cThread::Active(void)
// As in kill(), if sig is zero, error checking is // As in kill(), if sig is zero, error checking is
// performed but no signal is actually sent. // performed but no signal is actually sent.
// //
cMutexLock MutexLock(&childTidMutex);
int err; int err;
if ((err = pthread_kill(childTid, 0)) != 0) { if ((err = pthread_kill(childTid, 0)) != 0) {
if (err != ESRCH) if (err != ESRCH)
LOG_ERROR; LOG_ERROR;
childTid = 0; childTid = 0;
running = false;
} }
else else
return true; return true;
@ -274,7 +275,7 @@ bool cThread::Active(void)
void cThread::Cancel(int WaitSeconds) void cThread::Cancel(int WaitSeconds)
{ {
if (childTid) { if (running) {
if (WaitSeconds > 0) { if (WaitSeconds > 0) {
for (time_t t0 = time(NULL) + WaitSeconds; time(NULL) < t0; ) { for (time_t t0 = time(NULL) + WaitSeconds; time(NULL) < t0; ) {
if (!Active()) if (!Active())
@ -283,12 +284,9 @@ void cThread::Cancel(int WaitSeconds)
} }
esyslog("ERROR: thread %ld won't end (waited %d seconds) - canceling it...", childTid, WaitSeconds); esyslog("ERROR: thread %ld won't end (waited %d seconds) - canceling it...", childTid, WaitSeconds);
} }
childTidMutex.Lock(); pthread_cancel(childTid);
if (childTid) { childTid = 0;
pthread_cancel(childTid); running = false;
childTid = 0;
}
childTidMutex.Unlock();
} }
} }

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * how to reach the author.
* *
* $Id: thread.h 1.25 2004/11/26 13:33:26 kls Exp $ * $Id: thread.h 1.26 2004/12/19 10:43:10 kls Exp $
*/ */
#ifndef __THREAD_H #ifndef __THREAD_H
@ -73,8 +73,8 @@ public:
class cThread { class cThread {
friend class cThreadLock; friend class cThreadLock;
private: private:
pthread_t parentTid, childTid; bool running;
cMutex childTidMutex; pthread_t childTid;
cMutex mutex; cMutex mutex;
char *description; char *description;
static bool emergencyExitRequested; static bool emergencyExitRequested;