blob: 7304e682a215b0089bc7dd3f06252ead787693a9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
/*
* thread.c: A simple thread base class
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
* $Id: thread.c 1.2 2000/10/08 16:45:50 kls Exp $
*/
#include "thread.h"
#include <signal.h>
#include <unistd.h>
// --- cThread ---------------------------------------------------------------
// The signal handler is necessary to be able to use SIGIO to wake up any
// pending 'select()' call.
bool cThread::signalHandlerInstalled = false;
cThread::cThread(void)
{
if (!signalHandlerInstalled) {
signal(SIGIO, SignalHandler);
signalHandlerInstalled = true;
}
pthread_mutex_init(&mutex, NULL);
running = false;
parentPid = lockingPid = 0;
locked = 0;
}
cThread::~cThread()
{
pthread_mutex_destroy(&mutex);
}
void cThread::SignalHandler(int signum)
{
signal(signum, SignalHandler);
}
void *cThread::StartThread(cThread *Thread)
{
Thread->Action();
return NULL;
}
bool cThread::Start(void)
{
if (!running) {
running = true;
parentPid = getpid();
pthread_create(&thread, NULL, &StartThread, (void *)this);
}
return true; //XXX return value of pthread_create()???
}
void cThread::Stop(void)
{
pthread_cancel(thread);
}
bool cThread::Lock(void)
{
if (!lockingPid || lockingPid != getpid()) {
pthread_mutex_lock(&mutex);
lockingPid = getpid();
}
locked++;
return true;
}
void cThread::Unlock(void)
{
if (!--locked) {
lockingPid = 0;
pthread_mutex_unlock(&mutex);
}
}
void cThread::WakeUp(void)
{
kill(parentPid, SIGIO); // makes any waiting 'select()' call return immediately
}
// --- cThreadLock -----------------------------------------------------------
cThreadLock::cThreadLock(cThread *Thread)
{
thread = Thread;
locked = Thread->Lock();
}
cThreadLock::~cThreadLock()
{
if (locked)
thread->Unlock();
}
bool cThreadLock::Locked(void)
{
return locked;
}
|