]>
Dogcows Code - chaz/yoink/blob - src/Moof/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 *******************************************************************************/
29 #ifndef _MOOF_THREAD_HH_
30 #define _MOOF_THREAD_HH_
34 * Light C++ wrapper around the SDL threads API.
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 inline int detach_(void* arg
)
56 //Function function = *(Function*)arg;
57 int code
= (*(Function
*)arg
)();
59 delete (Function
*)arg
;
63 inline Thread
detachFunction(const Function
& function
)
65 Function
* fcopy
= new Function(function
);
66 Thread thread
= SDL_CreateThread(detach_
, (void*)fcopy
);
68 if (thread
== 0) delete fcopy
;
73 inline int waitOnThread(Thread thread
)
76 SDL_WaitThread(thread
, &i
);
80 inline void killThread(Thread thread
)
82 SDL_KillThread(thread
);
87 // The identifier function returns a unique integer for the calling thread.
90 inline unsigned getThreadIdentifier()
92 return SDL_ThreadID();
95 inline unsigned getThreadIdentifier(Thread thread
)
97 return SDL_GetThreadID(thread
);
101 // =============================================================================
105 friend class Condition
;
110 mutex_
= SDL_CreateMutex();
114 SDL_DestroyMutex(mutex_
);
119 return (SDL_LockMutex(mutex_
) == 0);
123 return (SDL_UnlockMutex(mutex_
) == 0);
128 friend class Condition
;
138 if (isLocked_
) release();
143 return (isLocked_
= mutex_
->acquireLock());
147 return mutex_
->releaseLock();
151 bool isLocked() const
161 class ScopedLock
: public Lock
164 ScopedLock(Mutex
& mutex
) :
181 condition_
= SDL_CreateCond();
185 SDL_DestroyCond(condition_
);
188 bool wait(Mutex::Lock
& lock
)
190 return (SDL_CondWait(condition_
, lock
.mutex_
->mutex_
) == 0);
192 bool wait(Mutex::Lock
& lock
, unsigned ms
)
194 // TODO for consistency, this function should take seconds
195 return (SDL_CondWaitTimeout(condition_
, lock
.mutex_
->mutex_
, ms
) == 0);
200 return (SDL_CondSignal(condition_
) == 0);
204 return (SDL_CondBroadcast(condition_
) == 0);
208 SDL_cond
* condition_
;
215 Semaphore(unsigned int value
)
217 semaphore_
= SDL_CreateSemaphore(value
);
221 SDL_DestroySemaphore(semaphore_
);
226 return (SDL_SemWait(semaphore_
) == 0);
230 return (SDL_SemPost(semaphore_
) == 0);
235 return (SDL_SemTryWait(semaphore_
) == 0);
237 bool tryLock(unsigned ms
)
239 // TODO for consistency, this function should take seconds
240 return (SDL_SemWaitTimeout(semaphore_
, ms
) == 0);
246 Lock(Semaphore
& semaphore
)
248 semaphore_
= &semaphore
;
253 if (isLocked_
) release();
258 return (isLocked_
= semaphore_
->acquireLock());
262 return semaphore_
->releaseLock(); isLocked_
= false;
265 bool isLocked() const
271 Semaphore
* semaphore_
;
275 class ScopedLock
: public Lock
278 ScopedLock(Semaphore
& semaphore
) :
293 #endif // _MOOF_THREAD_HH_
295 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.047538 seconds and 5 git commands to generate.