]> git.vomp.tv Git - vompserver.git/blob - thread.c
Bootp server
[vompserver.git] / thread.c
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "thread.h"
22
23 // Undeclared functions, only for use in this file to start the thread
24 void threadInternalStart(void *arg)
25 {
26   // I don't want signals
27   sigset_t sigset;
28   sigfillset(&sigset);
29   pthread_sigmask(SIG_BLOCK, &sigset, NULL);
30
31   Thread *t = (Thread *)arg;
32   t->threadInternalStart2();
33 }
34
35 void Thread::threadInternalStart2()
36 {
37   threadMethod();
38 }
39
40 Thread::Thread()
41 {
42   threadActive = 0;
43 }
44
45 int Thread::threadStart()
46 {
47   pthread_cond_init(&threadCond, NULL);
48   pthread_mutex_init(&threadCondMutex, NULL);
49
50   threadActive = 1;
51   if (pthread_create(&pthread, NULL, (void*(*)(void*))threadInternalStart, (void *)this) == -1) return 0;
52   return 1;
53 }
54
55 void Thread::threadStop()
56 {
57   threadActive = 0;
58   // Signal thread here in case it's waiting
59   threadSignal();
60   pthread_join(pthread, NULL);
61 }
62
63 void Thread::threadCancel()
64 {
65   threadActive = 0;
66   pthread_cancel(pthread);
67   pthread_join(pthread, NULL);
68 }
69
70 void Thread::threadCheckExit()
71 {
72   if (!threadActive) pthread_exit(NULL);
73 }
74
75 char Thread::threadIsActive()
76 {
77   return threadActive;
78 }
79
80 void Thread::threadSignal()
81 {
82   pthread_mutex_lock(&threadCondMutex);
83   pthread_cond_signal(&threadCond);
84   pthread_mutex_unlock(&threadCondMutex);
85 }
86
87 void Thread::threadSignalNoLock()
88 {
89   pthread_cond_signal(&threadCond);
90 }
91
92 void Thread::threadWaitForSignal()
93 {
94   pthread_mutex_lock(&threadCondMutex);
95   pthread_cond_wait(&threadCond, &threadCondMutex);
96   pthread_mutex_unlock(&threadCondMutex);
97 }