summaryrefslogtreecommitdiff
path: root/thread.c
diff options
context:
space:
mode:
authorKlaus Schmidinger <vdr@tvdr.de>2004-12-19 10:58:20 +0100
committerKlaus Schmidinger <vdr@tvdr.de>2004-12-19 10:58:20 +0100
commit9a8ef2b6c26aefe51e33db804a75ff5cfdb592f4 (patch)
tree155b9cbb8af3dad7b82f7c9e7cb103bd30b8e0c9 /thread.c
parentf5b1a74056a865d406ba8efab4ae15c45eebf6b9 (diff)
downloadvdr-9a8ef2b6c26aefe51e33db804a75ff5cfdb592f4.tar.gz
vdr-9a8ef2b6c26aefe51e33db804a75ff5cfdb592f4.tar.bz2
Fixed a race condition in starting a thread
Diffstat (limited to 'thread.c')
-rw-r--r--thread.c50
1 files changed, 24 insertions, 26 deletions
diff --git a/thread.c b/thread.c
index ebb0943d..fe3359bf 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.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"
@@ -197,7 +197,8 @@ bool cThread::emergencyExitRequested = false;
cThread::cThread(const char *Description)
{
- parentTid = childTid = 0;
+ running = false;
+ childTid = 0;
description = NULL;
SetDescription(Description);
}
@@ -221,35 +222,35 @@ void cThread::SetDescription(const char *Description, ...)
void *cThread::StartThread(cThread *Thread)
{
- Thread->childTidMutex.Lock();
- Thread->childTid = pthread_self();
- Thread->childTidMutex.Unlock();
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();
if (Thread->description)
- dsyslog("%s thread ended (pid=%d, tid=%ld)", Thread->description, getpid(), Thread->childTid);
- Thread->childTidMutex.Lock();
- Thread->childTid = 0;
- Thread->childTidMutex.Unlock();
+ dsyslog("%s thread ended (pid=%d, tid=%ld)", Thread->description, getpid(), pthread_self());
+ Thread->running = false;
return NULL;
}
bool cThread::Start(void)
{
- if (!childTid) {
- parentTid = pthread_self();
- pthread_t Tid;
- pthread_create(&Tid, NULL, (void *(*) (void *))&StartThread, (void *)this);
- pthread_detach(Tid); // auto-reap
- pthread_setschedparam(Tid, SCHED_RR, 0);
+ if (!running) {
+ running = true;
+ if (pthread_create(&childTid, NULL, (void *(*) (void *))&StartThread, (void *)this) == 0) {
+ pthread_detach(childTid); // auto-reap
+ pthread_setschedparam(childTid, SCHED_RR, 0);
+ }
+ else {
+ LOG_ERROR;
+ running = false;
+ return false;
+ }
}
- return true; //XXX return value of pthread_create()???
+ return true;
}
bool cThread::Active(void)
{
- if (childTid) {
+ if (running) {
//
// Single UNIX Spec v2 says:
//
@@ -259,12 +260,12 @@ bool cThread::Active(void)
// As in kill(), if sig is zero, error checking is
// performed but no signal is actually sent.
//
- cMutexLock MutexLock(&childTidMutex);
int err;
if ((err = pthread_kill(childTid, 0)) != 0) {
if (err != ESRCH)
LOG_ERROR;
childTid = 0;
+ running = false;
}
else
return true;
@@ -274,7 +275,7 @@ bool cThread::Active(void)
void cThread::Cancel(int WaitSeconds)
{
- if (childTid) {
+ if (running) {
if (WaitSeconds > 0) {
for (time_t t0 = time(NULL) + WaitSeconds; time(NULL) < t0; ) {
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);
}
- childTidMutex.Lock();
- if (childTid) {
- pthread_cancel(childTid);
- childTid = 0;
- }
- childTidMutex.Unlock();
+ pthread_cancel(childTid);
+ childTid = 0;
+ running = false;
}
}