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