]> Dogcows Code - chaz/yoink/blob - src/Moof/Thread.hh
improved new vorbisfile compatibility
[chaz/yoink] / src / Moof / Thread.hh
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #ifndef _MOOF_THREAD_HH_
30 #define _MOOF_THREAD_HH_
31
32 /**
33 * @file Thread.hh
34 * Light C++ wrapper around SDL threads.
35 */
36
37 #include <boost/function.hpp>
38
39 #include <SDL/SDL.h>
40
41
42 namespace Mf {
43
44 //
45 // The detach function detaches a separate thread by calling 'func' with
46 // the 'arg' parameter.
47 //
48
49 typedef SDL_Thread* Thread;
50
51 typedef boost::function<int(void)> Function;
52
53
54 int detach_(void* arg)
55 {
56 Function function = *(Function*)arg;
57 int ret = function();
58
59 delete (Function*)arg;
60 return ret;
61 }
62
63 Thread detachFunction(const Function& function)
64 {
65 Thread thread;
66 Function* fcopy = new Function(function);
67
68 thread = SDL_CreateThread(detach_, (void*)fcopy);
69 if (thread == 0) delete fcopy;
70 return thread;
71 }
72
73
74 int waitOnThread(Thread thread)
75 {
76 int i;
77 SDL_WaitThread(thread, &i);
78 return i;
79 }
80
81 void killThread(Thread thread)
82 {
83 SDL_KillThread(thread);
84 }
85
86
87 //
88 // The identifier function returns a unique integer for the calling thread.
89 //
90
91 unsigned int getThreadIdentifier()
92 {
93 return SDL_ThreadID();
94 }
95
96 unsigned int getThreadIdentifier(Thread thread)
97 {
98 return SDL_GetThreadID(thread);
99 }
100
101
102 // =============================================================================
103
104 class Mutex
105 {
106 friend class Condition;
107
108 public:
109 Mutex()
110 {
111 mutex_ = SDL_CreateMutex();
112 }
113 ~Mutex()
114 {
115 SDL_DestroyMutex(mutex_);
116 }
117
118 bool acquireLock()
119 {
120 return (SDL_LockMutex(mutex_) == 0);
121 }
122 bool releaseLock()
123 {
124 return (SDL_UnlockMutex(mutex_) == 0);
125 }
126
127 class Lock
128 {
129 friend class Condition;
130
131 public:
132 Lock(Mutex& mutex)
133 {
134 mutex_ = &mutex;
135 isLocked_ = false;
136 }
137 ~Lock()
138 {
139 if (isLocked_) release();
140 }
141
142 bool acquire()
143 {
144 return (isLocked_ = mutex_->acquireLock());
145 }
146 bool release()
147 {
148 return mutex_->releaseLock();
149 isLocked_ = false;
150 }
151
152 bool isLocked() const
153 {
154 return isLocked_;
155 }
156
157 protected:
158 Mutex* mutex_;
159 bool isLocked_;
160 };
161
162 class ScopedLock : public Lock
163 {
164 public:
165 ScopedLock(Mutex& mutex) :
166 Lock(mutex)
167 {
168 acquire();
169 }
170 };
171
172 private:
173 SDL_mutex* mutex_;
174 };
175
176
177 class Condition
178 {
179 public:
180 Condition()
181 {
182 condition_ = SDL_CreateCond();
183 }
184 ~Condition()
185 {
186 SDL_DestroyCond(condition_);
187 }
188
189 bool wait(Mutex::Lock& lock)
190 {
191 return (SDL_CondWait(condition_, lock.mutex_->mutex_) == 0);
192 }
193 bool wait(Mutex::Lock& lock, unsigned ms)
194 {
195 // TODO for consistency, this function should take seconds
196 return (SDL_CondWaitTimeout(condition_, lock.mutex_->mutex_, ms) == 0);
197 }
198
199 bool notify()
200 {
201 return (SDL_CondSignal(condition_) == 0);
202 }
203 bool notifyAll()
204 {
205 return (SDL_CondBroadcast(condition_) == 0);
206 }
207
208 private:
209 SDL_cond* condition_;
210 };
211
212
213 class Semaphore
214 {
215 public:
216 Semaphore(unsigned int value)
217 {
218 semaphore_ = SDL_CreateSemaphore(value);
219 }
220 ~Semaphore()
221 {
222 SDL_DestroySemaphore(semaphore_);
223 }
224
225 bool acquireLock()
226 {
227 return (SDL_SemWait(semaphore_) == 0);
228 }
229 bool releaseLock()
230 {
231 return (SDL_SemPost(semaphore_) == 0);
232 }
233
234 bool tryLock()
235 {
236 return (SDL_SemTryWait(semaphore_) == 0);
237 }
238 bool tryLock(unsigned ms)
239 {
240 // TODO for consistency, this function should take seconds
241 return (SDL_SemWaitTimeout(semaphore_, ms) == 0);
242 }
243
244 class Lock
245 {
246 public:
247 Lock(Semaphore& semaphore)
248 {
249 semaphore_ = &semaphore;
250 isLocked_ = false;
251 }
252 ~Lock()
253 {
254 if (isLocked_) release();
255 }
256
257 bool acquire()
258 {
259 return (isLocked_ = semaphore_->acquireLock());
260 }
261 bool release()
262 {
263 return semaphore_->releaseLock(); isLocked_ = false;
264 }
265
266 bool isLocked() const
267 {
268 return isLocked_;
269 }
270
271 protected:
272 Semaphore* semaphore_;
273 bool isLocked_;
274 };
275
276 class ScopedLock : public Lock
277 {
278 public:
279 ScopedLock(Semaphore& semaphore) :
280 Lock(semaphore)
281 {
282 acquire();
283 }
284 };
285
286 private:
287 SDL_sem* semaphore_;
288 };
289
290
291 } // namespace Mf
292
293
294 #endif // _MOOF_THREAD_HH_
295
296 /** vim: set ts=4 sw=4 tw=80: *************************************************/
297
This page took 0.046851 seconds and 4 git commands to generate.