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