2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
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.
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.
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.
23 // Undeclared functions, only for use in this file to start the thread
24 void threadPInternalStart(void *arg)
26 // I don't want signals
29 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
31 Thread *t = (Thread *)arg;
32 t->threadInternalStart2();
35 int ThreadP::threadStart()
37 pthread_cond_init(&threadCond, NULL);
38 pthread_mutex_init(&threadCondMutex, NULL);
41 if (pthread_create(&pthread, NULL, (void*(*)(void*))threadPInternalStart, (void *)this) == -1) return 0;
45 void ThreadP::threadStop()
47 if (threadActive) { // we need this, on some implementations this will fail, if already stopped
49 // Signal thread here in case it's waiting
51 pthread_join(pthread, NULL);
52 this->threadPostStopCleanup();
57 void ThreadP::threadCancel()
61 pthread_cancel(pthread);
62 pthread_join(pthread, NULL);
63 this->threadPostStopCleanup();
68 void ThreadP::threadCheckExit()
70 if (!threadActive) pthread_exit(NULL);
73 void ThreadP::threadLock()
75 pthread_mutex_lock(&threadCondMutex);
78 void ThreadP::threadUnlock()
80 pthread_mutex_unlock(&threadCondMutex);
83 void ThreadP::threadSignal()
85 pthread_mutex_lock(&threadCondMutex);
86 pthread_cond_signal(&threadCond);
87 pthread_mutex_unlock(&threadCondMutex);
90 void ThreadP::threadSignalNoLock()
92 pthread_cond_signal(&threadCond);
95 void ThreadP::threadWaitForSignal()
97 pthread_cond_wait(&threadCond, &threadCondMutex);
100 void ThreadP::threadWaitForSignalTimed(struct timespec* ts)
102 pthread_cond_timedwait(&threadCond, &threadCondMutex, ts);
105 void ThreadP::threadSetKillable()
107 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
108 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
112 pthread_t ThreadP::getThreadID() // returns the ID of this thread
119 void ThreadP::threadSuicide()
121 if(!pthread_detach(pthread_self()))
124 if(!pthread_detach(pthread_self()))
127 pthread_detach(pthread_self());
134 pthread_t ThreadP::thisThreadID() // returns the ID of the calling thread
136 return pthread_self();