]> git.vomp.tv Git - vompclient-marten.git/blob - threadp.cc
Motion Compensation, all implemented, all buggy
[vompclient-marten.git] / threadp.cc
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #include "threadp.h"
22
23 // Undeclared functions, only for use in this file to start the thread
24 void threadPInternalStart(void *arg)
25 {
26   // I don't want signals
27   sigset_t sigs;
28   sigfillset(&sigs);
29   pthread_sigmask(SIG_BLOCK, &sigs, NULL);
30
31   Thread *t = (Thread *)arg;
32   t->threadInternalStart2();
33 }
34
35 int ThreadP::threadStart()
36 {
37   pthread_cond_init(&threadCond, NULL);
38   pthread_mutex_init(&threadCondMutex, NULL);
39
40   threadActive = 1;
41   if (pthread_create(&pthread, NULL, (void*(*)(void*))threadPInternalStart, (void *)this) == -1) return 0;
42   return 1;
43 }
44
45 void ThreadP::threadStop()
46 {
47   threadActive = 0;
48   // Signal thread here in case it's waiting
49   threadSignal();
50   pthread_join(pthread, NULL);
51   this->threadPostStopCleanup();
52 }
53
54
55 void ThreadP::threadCancel()
56 {
57   threadActive = 0;
58   pthread_cancel(pthread);
59   pthread_join(pthread, NULL);
60   this->threadPostStopCleanup();
61 }
62
63
64 void ThreadP::threadCheckExit()
65 {
66   if (!threadActive) pthread_exit(NULL);
67 }
68
69 void ThreadP::threadLock()
70 {
71   pthread_mutex_lock(&threadCondMutex);
72 }
73
74 void ThreadP::threadUnlock()
75 {
76   pthread_mutex_unlock(&threadCondMutex);
77 }
78
79 void ThreadP::threadSignal()
80 {
81   pthread_mutex_lock(&threadCondMutex);
82   pthread_cond_signal(&threadCond);
83   pthread_mutex_unlock(&threadCondMutex);
84 }
85
86 void ThreadP::threadSignalNoLock()
87 {
88   pthread_cond_signal(&threadCond);
89 }
90
91 void ThreadP::threadWaitForSignal()
92 {
93   pthread_cond_wait(&threadCond, &threadCondMutex);
94 }
95
96 void ThreadP::threadWaitForSignalTimed(struct timespec* ts)
97 {
98   pthread_cond_timedwait(&threadCond, &threadCondMutex, ts);
99 }
100
101 void ThreadP::threadSetKillable()
102 {
103   pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
104   pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
105 }
106
107
108 pthread_t ThreadP::getThreadID() // returns the ID of this thread
109 {
110   return pthread;
111 }
112
113 // Static functions
114
115 void ThreadP::threadSuicide()
116 {
117   if(!pthread_detach(pthread_self()))
118   {
119     MILLISLEEP(1000);
120     if(!pthread_detach(pthread_self()))
121     {
122       MILLISLEEP(1000);
123       pthread_detach(pthread_self());
124     }
125   }
126
127   pthread_exit(NULL);
128 }
129
130 pthread_t ThreadP::thisThreadID() // returns the ID of the calling thread
131 {
132   return pthread_self();
133 }