1 |
/* |
2 |
* HT Editor |
3 |
* thread.h |
4 |
* |
5 |
* Copyright (C) 2003 Sebastian Biallas (sb@biallas.net) |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License version 2 as |
9 |
* published by the Free Software Foundation. |
10 |
* |
11 |
* This program 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 this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
20 |
|
21 |
#ifndef ___THREAD_H__ |
22 |
#define ___THREAD_H__ |
23 |
|
24 |
#include "system/systhread.h" |
25 |
|
26 |
enum ThreadState { |
27 |
threadInited, |
28 |
threadStarting, |
29 |
threadRunning, |
30 |
threadFinished, |
31 |
}; |
32 |
|
33 |
class Mutex; |
34 |
class Semaphore; |
35 |
|
36 |
class Thread { |
37 |
sys_thread mThread; |
38 |
bool mThreadValid; |
39 |
ThreadState mState; |
40 |
Semaphore * mSem; |
41 |
public: |
42 |
Thread(); |
43 |
virtual ~Thread(); |
44 |
/** |
45 |
* Call this to start the thread. |
46 |
* You can call this again, when the thread has finished, |
47 |
* but note that you will mostlikely get an other thread id. |
48 |
*/ |
49 |
void start(void *param); |
50 |
|
51 |
/** |
52 |
* |
53 |
*/ |
54 |
ThreadState getState(); |
55 |
|
56 |
/* |
57 |
* The following functions can only be called from *other* threads |
58 |
* (I.e. not from run()) |
59 |
*/ |
60 |
|
61 |
/** |
62 |
* Wait until the thread exits. |
63 |
* @returns return value from run() [FIXME: broken] |
64 |
*/ |
65 |
void *waitForExit(); |
66 |
/** |
67 |
* Waits until the thread is running (if thread has been started with start() before) |
68 |
*/ |
69 |
void waitForRunning(); |
70 |
|
71 |
/*protected: (I'd really love to make this protected, but I can't for some stupid reasons) */ |
72 |
|
73 |
/** |
74 |
* The body of the thread, must be implemented. |
75 |
* Don't call this directly but via start(). |
76 |
*/ |
77 |
virtual void * run() = 0; |
78 |
/** |
79 |
* Don't call this. |
80 |
*/ |
81 |
void setState(ThreadState ts); |
82 |
|
83 |
/* |
84 |
* These functions can only be called from the this thread |
85 |
* (I.e. from run()) |
86 |
*/ |
87 |
|
88 |
/** |
89 |
* Terminate current thread. |
90 |
* Equivalent to return in run() |
91 |
*/ |
92 |
void terminate(void *ret); |
93 |
}; |
94 |
|
95 |
/** |
96 |
* Use this if you dont want to use Thread. |
97 |
* Just takes a function pointer as argument. |
98 |
*/ |
99 |
class ThreadFunction: public Thread { |
100 |
sys_thread_function mFunc; |
101 |
void *mParam; |
102 |
public: |
103 |
ThreadFunction(sys_thread_function f, void *aParam); |
104 |
virtual void * run(); |
105 |
}; |
106 |
|
107 |
class Mutex { |
108 |
sys_mutex mutex; |
109 |
public: |
110 |
Mutex(); |
111 |
virtual ~Mutex(); |
112 |
void lock(); |
113 |
void unlock(); |
114 |
/** |
115 |
* Tries to lock the mutex. |
116 |
* @returns 0 if it wasn't locked (and is now locked) or EBUSY if it is already locked. |
117 |
*/ |
118 |
int trylock(); |
119 |
}; |
120 |
|
121 |
class Semaphore { |
122 |
sys_semaphore sem; |
123 |
public: |
124 |
Semaphore(); |
125 |
virtual ~Semaphore(); |
126 |
/** |
127 |
* Signals a random thread which is wait()ing for Semaphore. |
128 |
* You have to call lock() before. |
129 |
*/ |
130 |
void signal(); |
131 |
/** |
132 |
* Signals all threads which are wait()ing for Semaphore. |
133 |
* You have to call lock() before. |
134 |
*/ |
135 |
void signalAll(); |
136 |
|
137 |
/** |
138 |
* Locks Mutex for wait() |
139 |
*/ |
140 |
void lock(); |
141 |
|
142 |
/** |
143 |
* Wait for Semaphore until some other Thread signal()s it. |
144 |
* You have to call lock() before. |
145 |
*/ |
146 |
void wait(); |
147 |
|
148 |
/** |
149 |
* Locks Mutex for wait() |
150 |
*/ |
151 |
void unlock(); |
152 |
}; |
153 |
|
154 |
#endif |