]>
Dogcows Code - chaz/yoink/blob - src/thread.hh
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
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.
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.
27 *******************************************************************************/
34 * Light C++ wrapper around SDL threads.
37 #include <boost/function.hpp>
45 // The detach function detaches a separate thread by calling 'func' with
46 // the 'arg' parameter.
49 typedef SDL_Thread
* thread
;
51 typedef boost::function
<int(void)> function
;
54 int detach_(void* arg
)
56 function func
= *(function
*)arg
;
59 delete (function
*)arg
;
63 thread
detach(const function
& func
)
66 function
* fcopy
= new function(func
);
68 t
= SDL_CreateThread(detach_
, (void*)fcopy
);
69 if (t
== 0) delete fcopy
;
77 SDL_WaitThread(t
, &i
);
88 // The identifier function returns a unique integer for the calling thread.
91 unsigned int identifier()
93 return SDL_ThreadID();
96 unsigned int identifier(thread t
)
98 return SDL_GetThreadID(t
);
102 // =============================================================================
106 friend class condition
;
109 mutex() { mutex_
= SDL_CreateMutex(); }
110 ~mutex() { SDL_DestroyMutex(mutex_
); }
112 bool acquireLock() { return (SDL_LockMutex(mutex_
) == 0); }
113 bool releaseLock() { return (SDL_UnlockMutex(mutex_
) == 0); }
117 friend class condition
;
120 lock(mutex
& m
) { mutex_
= &m
; isLocked_
= false; }
121 ~lock() { if (isLocked_
) release(); }
123 bool acquire() { return (isLocked_
= mutex_
->acquireLock()); }
124 bool release() { return mutex_
->releaseLock(); isLocked_
= false; }
125 bool isLocked() const { return isLocked_
; }
132 class scoped_lock
: public lock
135 scoped_lock(mutex
& m
) : lock(m
) { acquire(); }
142 // =============================================================================
147 condition() { condition_
= SDL_CreateCond(); }
148 ~condition() { SDL_DestroyCond(condition_
); }
150 bool wait(mutex::lock
& l
)
152 return (SDL_CondWait(condition_
, l
.mutex_
->mutex_
) == 0);
154 bool wait(mutex::lock
& l
, unsigned ms
)
156 // TODO: For consistency, this function should take seconds, not ms.
157 return (SDL_CondWaitTimeout(condition_
, l
.mutex_
->mutex_
, ms
) == 0);
160 bool notify() { return (SDL_CondSignal(condition_
) == 0); }
161 bool notifyAll() { return (SDL_CondBroadcast(condition_
) == 0); }
164 SDL_cond
* condition_
;
167 // =============================================================================
172 semaphore(unsigned int value
) { semaphore_
= SDL_CreateSemaphore(value
); }
173 ~semaphore() { SDL_DestroySemaphore(semaphore_
); }
175 bool acquireLock() { return (SDL_SemWait(semaphore_
) == 0); }
176 bool tryLock() { return (SDL_SemTryWait(semaphore_
) == 0); }
177 bool tryLock(unsigned ms
)
179 // TODO: For consistency, this function should take seconds, not ms.
180 return (SDL_SemWaitTimeout(semaphore_
, ms
) == 0);
182 bool releaseLock() { return (SDL_SemPost(semaphore_
) == 0); }
187 lock(semaphore
& m
) { semaphore_
= &m
; isLocked_
= false; }
188 ~lock() { if (isLocked_
) release(); }
190 bool acquire() { return (isLocked_
= semaphore_
->acquireLock()); }
191 bool release() { return semaphore_
->releaseLock(); isLocked_
= false; }
192 bool isLocked() const { return isLocked_
; }
195 semaphore
* semaphore_
;
199 class scoped_lock
: public lock
202 scoped_lock(semaphore
& m
) : lock(m
) { acquire(); }
210 } // namespace thread
214 #endif // _THREAD_HH_
216 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.049166 seconds and 4 git commands to generate.