]> Dogcows Code - chaz/yoink/blob - src/thread.hh
big batch of progress
[chaz/yoink] / src / 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 _THREAD_HH_
30 #define _THREAD_HH_
31
32 /**
33 * @file thread.hh
34 * Light C++ wrapper around SDL threads.
35 */
36
37 #include <boost/function.hpp>
38 #include <SDL/SDL.h>
39
40
41 namespace dc {
42 namespace thread {
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 func = *(function*)arg;
57 int ret = func();
58
59 delete (function*)arg;
60 return ret;
61 }
62
63 thread detach(const function& func)
64 {
65 thread t;
66 function* fcopy = new function(func);
67
68 t = SDL_CreateThread(detach_, (void*)fcopy);
69 if (t == 0) delete fcopy;
70 return t;
71 }
72
73
74 int wait(thread t)
75 {
76 int i;
77 SDL_WaitThread(t, &i);
78 return i;
79 }
80
81 void kill(thread t)
82 {
83 SDL_KillThread(t);
84 }
85
86
87 //
88 // The identifier function returns a unique integer for the calling thread.
89 //
90
91 unsigned int identifier()
92 {
93 return SDL_ThreadID();
94 }
95
96 unsigned int identifier(thread t)
97 {
98 return SDL_GetThreadID(t);
99 }
100
101
102 // =============================================================================
103
104 class mutex
105 {
106 friend class condition;
107
108 public:
109 mutex() { mutex_ = SDL_CreateMutex(); }
110 ~mutex() { SDL_DestroyMutex(mutex_); }
111
112 bool acquireLock() { return (SDL_LockMutex(mutex_) == 0); }
113 bool releaseLock() { return (SDL_UnlockMutex(mutex_) == 0); }
114
115 class lock
116 {
117 friend class condition;
118
119 public:
120 lock(mutex& m) { mutex_ = &m; isLocked_ = false; }
121 ~lock() { if (isLocked_) release(); }
122
123 bool acquire() { return (isLocked_ = mutex_->acquireLock()); }
124 bool release() { return mutex_->releaseLock(); isLocked_ = false; }
125 bool isLocked() const { return isLocked_; }
126
127 protected:
128 mutex* mutex_;
129 bool isLocked_;
130 };
131
132 class scoped_lock : public lock
133 {
134 public:
135 scoped_lock(mutex& m) : lock(m) { acquire(); }
136 };
137
138 private:
139 SDL_mutex* mutex_;
140 };
141
142 // =============================================================================
143
144 class condition
145 {
146 public:
147 condition() { condition_ = SDL_CreateCond(); }
148 ~condition() { SDL_DestroyCond(condition_); }
149
150 bool wait(mutex::lock& l)
151 {
152 return (SDL_CondWait(condition_, l.mutex_->mutex_) == 0);
153 }
154 bool wait(mutex::lock& l, unsigned ms)
155 {
156 // TODO: For consistency, this function should take seconds, not ms.
157 return (SDL_CondWaitTimeout(condition_, l.mutex_->mutex_, ms) == 0);
158 }
159
160 bool notify() { return (SDL_CondSignal(condition_) == 0); }
161 bool notifyAll() { return (SDL_CondBroadcast(condition_) == 0); }
162
163 private:
164 SDL_cond* condition_;
165 };
166
167 // =============================================================================
168
169 class semaphore
170 {
171 public:
172 semaphore(unsigned int value) { semaphore_ = SDL_CreateSemaphore(value); }
173 ~semaphore() { SDL_DestroySemaphore(semaphore_); }
174
175 bool acquireLock() { return (SDL_SemWait(semaphore_) == 0); }
176 bool tryLock() { return (SDL_SemTryWait(semaphore_) == 0); }
177 bool tryLock(unsigned ms)
178 {
179 // TODO: For consistency, this function should take seconds, not ms.
180 return (SDL_SemWaitTimeout(semaphore_, ms) == 0);
181 }
182 bool releaseLock() { return (SDL_SemPost(semaphore_) == 0); }
183
184 class lock
185 {
186 public:
187 lock(semaphore& m) { semaphore_ = &m; isLocked_ = false; }
188 ~lock() { if (isLocked_) release(); }
189
190 bool acquire() { return (isLocked_ = semaphore_->acquireLock()); }
191 bool release() { return semaphore_->releaseLock(); isLocked_ = false; }
192 bool isLocked() const { return isLocked_; }
193
194 protected:
195 semaphore* semaphore_;
196 bool isLocked_;
197 };
198
199 class scoped_lock : public lock
200 {
201 public:
202 scoped_lock(semaphore& m) : lock(m) { acquire(); }
203 };
204
205 private:
206 SDL_sem* semaphore_;
207 };
208
209
210 } // namespace thread
211 } // namespace dc
212
213
214 #endif // _THREAD_HH_
215
216 /** vim: set ts=4 sw=4 tw=80: *************************************************/
217
This page took 0.040491 seconds and 5 git commands to generate.