1 |
// Truck Computer Dooom! |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
/* Menes - C++ High-Level Utility Library |
8 |
* Copyright (C) 2002-2004 Jay Freeman (saurik) |
9 |
*/ |
10 |
|
11 |
/* |
12 |
* Redistribution and use in source and binary |
13 |
* forms, with or without modification, are permitted |
14 |
* provided that the following conditions are met: |
15 |
* |
16 |
* 1. Redistributions of source code must retain the |
17 |
* above copyright notice, this list of conditions |
18 |
* and the following disclaimer. |
19 |
* 2. Redistributions in binary form must reproduce the |
20 |
* above copyright notice, this list of conditions |
21 |
* and the following disclaimer in the documentation |
22 |
* and/or other materials provided with the |
23 |
* distribution. |
24 |
* 3. The name of the author may not be used to endorse |
25 |
* or promote products derived from this software |
26 |
* without specific prior written permission. |
27 |
* |
28 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' |
29 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, |
30 |
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
31 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
32 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
33 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
34 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
35 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
36 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
37 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
38 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR |
39 |
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
40 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
41 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
42 |
*/ |
43 |
|
44 |
#ifndef _threads_hpp_ |
45 |
#define _threads_hpp_ |
46 |
|
47 |
#include "posix.hpp" |
48 |
|
49 |
#include <pthread.h> |
50 |
#include <semaphore.h> |
51 |
|
52 |
namespace Pthreads |
53 |
{ |
54 |
|
55 |
typedef Posix::Error Error; |
56 |
|
57 |
inline void CheckError(int status) |
58 |
{ |
59 |
if (status != 0) |
60 |
throw Error(status); |
61 |
} |
62 |
|
63 |
class Thread |
64 |
{ |
65 |
::pthread_t thread; |
66 |
mutable bool joinable; |
67 |
public: |
68 |
typedef void *(*Function)(void *); |
69 |
|
70 |
Thread(Function function, void *argument = NULL); |
71 |
~Thread(); |
72 |
|
73 |
void Join() const; |
74 |
}; |
75 |
|
76 |
class CriticalSection |
77 |
{ |
78 |
protected: |
79 |
mutable pthread_mutex_t mutex; |
80 |
|
81 |
public: |
82 |
CriticalSection() |
83 |
{ |
84 |
CheckError(::pthread_mutex_init(&mutex, NULL)); |
85 |
} |
86 |
|
87 |
~CriticalSection() |
88 |
{ |
89 |
CheckError(::pthread_mutex_destroy(&mutex)); |
90 |
} |
91 |
|
92 |
void Lock() |
93 |
{ |
94 |
CheckError(::pthread_mutex_lock(&mutex)); |
95 |
} |
96 |
|
97 |
void Unlock() |
98 |
{ |
99 |
CheckError(::pthread_mutex_unlock(&mutex)); |
100 |
} |
101 |
|
102 |
operator pthread_mutex_t *() const |
103 |
{ |
104 |
return &mutex; |
105 |
} |
106 |
}; |
107 |
|
108 |
class ThreadMutex |
109 |
{ |
110 |
protected: |
111 |
CriticalSection *section; |
112 |
//ext::IndirectCount count; |
113 |
|
114 |
public: |
115 |
ThreadMutex() : section(new CriticalSection) |
116 |
{ |
117 |
} |
118 |
|
119 |
~ThreadMutex() |
120 |
{ |
121 |
//if (count.Alone()) |
122 |
delete section; |
123 |
} |
124 |
|
125 |
void Lock() |
126 |
{ |
127 |
section->Lock(); |
128 |
} |
129 |
|
130 |
void Unlock() |
131 |
{ |
132 |
section->Unlock(); |
133 |
} |
134 |
|
135 |
operator pthread_mutex_t *() const |
136 |
{ |
137 |
return *section; |
138 |
} |
139 |
}; |
140 |
|
141 |
class ThreadCondition |
142 |
{ |
143 |
public: |
144 |
typedef ThreadMutex Mutex; |
145 |
|
146 |
protected: |
147 |
mutable pthread_cond_t cond; |
148 |
Mutex &lock; |
149 |
//ext::IndirectCount count; |
150 |
|
151 |
public: |
152 |
explicit ThreadCondition(Mutex &lock) : lock(lock) |
153 |
{ |
154 |
CheckError(::pthread_cond_init(&cond, NULL)); |
155 |
} |
156 |
|
157 |
~ThreadCondition() |
158 |
{ |
159 |
//if (count.Alone()) |
160 |
CheckError(::pthread_cond_destroy(&cond)); |
161 |
} |
162 |
|
163 |
void Signal() |
164 |
{ |
165 |
CheckError(::pthread_cond_signal(&cond)); |
166 |
} |
167 |
|
168 |
void Broadcast() |
169 |
{ |
170 |
CheckError(::pthread_cond_broadcast(&cond)); |
171 |
} |
172 |
|
173 |
void Wait() |
174 |
{ |
175 |
CheckError(::pthread_cond_wait(&cond, lock)); |
176 |
} |
177 |
|
178 |
operator pthread_cond_t *() const |
179 |
{ |
180 |
return &cond; |
181 |
} |
182 |
}; |
183 |
|
184 |
class Semaphore |
185 |
{ |
186 |
protected: |
187 |
sem_t handle; |
188 |
|
189 |
operator sem_t() const; |
190 |
|
191 |
private: |
192 |
Semaphore(const Semaphore &rhs); |
193 |
|
194 |
Semaphore &operator =(const Semaphore &rhs); |
195 |
|
196 |
public: |
197 |
explicit Semaphore(unsigned initial = 0) |
198 |
{ |
199 |
Posix::CheckError(::sem_init(&handle, 0, initial)); |
200 |
} |
201 |
|
202 |
~Semaphore() |
203 |
{ |
204 |
Posix::CheckError(::sem_destroy(&handle)); |
205 |
} |
206 |
|
207 |
void Acquire() |
208 |
{ |
209 |
Posix::CheckError(::sem_wait(&handle)); |
210 |
} |
211 |
|
212 |
void Release(unsigned number = 1) |
213 |
{ |
214 |
Posix::CheckError(::sem_post(&handle)); |
215 |
} |
216 |
}; |
217 |
|
218 |
} |
219 |
|
220 |
#endif//_threads_hpp_ |