]> git.vomp.tv Git - vompserver.git/blob - thread.h
15 years that line of code has been waiting to crash
[vompserver.git] / thread.h
1 /*
2     Copyright 2004-2008 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 #ifndef THREAD_H
22 #define THREAD_H
23
24 #include <pthread.h>
25 #include <signal.h>
26
27 class Thread
28 {
29   protected:
30     // Override this method in derived classes
31     virtual void threadMethod()=0;
32     virtual void threadPostStopCleanup() {};
33
34     // Methods to use from outside the thread
35     int threadStart();    // start the thread. threadMethod() will be called in derived class
36     void threadStop();    // stop the thread nicely. thread will be stopped when it next calls threadCheckExit()
37     void threadCancel();  // stop thread immediately. thread will be stopped at the next cancellation point
38     void threadSignal();  // releases a thread that has called threadWaitForSignal
39     void threadSignalNoLock();  // same as above but without locking guarantees. probably not a good idea.
40     char threadIsActive(); // returns 1 if thread has been started but not stop() or cancel() 'd
41
42     // Methods to use from inside the thread
43     void threadCheckExit();      // terminates thread if threadStop() has been called
44     void threadWaitForSignal();  // pauses thread until threadSignal() is called
45     void threadDetach();         // Detaches the thread
46     void threadLock();           // locks the mutex used for internal cond/signal stuff
47     void threadUnlock();         // unlocks.
48
49     // Internal bits and pieces
50
51   private:
52     char threadActive;
53     pthread_t pthread;
54     pthread_cond_t threadCond;
55     pthread_mutex_t threadCondMutex;
56
57   public:
58     Thread();
59     virtual ~Thread() {};
60     void threadInternalStart2();
61 };
62
63 #endif