mirror of
https://github.com/vdr-projects/vdr.git
synced 2025-03-01 10:50:46 +00:00
Fixed a race condition in starting a thread
This commit is contained in:
50
thread.c
50
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user