]>
Dogcows Code - chaz/yoink/blob - src/Moof/Thread.hh
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
12 #ifndef _MOOF_THREAD_HH_
13 #define _MOOF_THREAD_HH_
17 * Light C++ wrapper around the SDL threads API.
20 #include <boost/function.hpp>
27 // The detach function detaches a separate thread by calling 'func' with
28 // the 'arg' parameter.
31 typedef SDL_Thread
* Thread
;
33 typedef boost::function
<int(void)> Function
;
36 inline int detach_(void* arg
)
38 //Function function = *(Function*)arg;
39 int code
= (*(Function
*)arg
)();
41 delete (Function
*)arg
;
45 inline Thread
detachFunction(const Function
& function
)
47 Function
* fcopy
= new Function(function
);
48 Thread thread
= SDL_CreateThread(detach_
, (void*)fcopy
);
50 if (thread
== 0) delete fcopy
;
55 inline int waitOnThread(Thread thread
)
58 SDL_WaitThread(thread
, &i
);
62 inline void killThread(Thread thread
)
64 SDL_KillThread(thread
);
69 // The identifier function returns a unique integer for the calling thread.
72 inline unsigned getThreadIdentifier()
74 return SDL_ThreadID();
77 inline unsigned getThreadIdentifier(Thread thread
)
79 return SDL_GetThreadID(thread
);
83 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87 friend class Condition
;
93 mMutex
= SDL_CreateMutex();
97 SDL_DestroyMutex(mMutex
);
102 return (SDL_LockMutex(mMutex
) == 0);
106 return (SDL_UnlockMutex(mMutex
) == 0);
111 friend class Condition
;
122 if (mIsLocked
) release();
127 return (mIsLocked
= mMutex
->acquireLock());
131 return mMutex
->releaseLock();
135 bool isLocked() const
146 class ScopedLock
: public Lock
150 ScopedLock(Mutex
& mutex
) :
169 condition_
= SDL_CreateCond();
173 SDL_DestroyCond(condition_
);
176 bool wait(Mutex::Lock
& lock
)
178 return (SDL_CondWait(condition_
, lock
.mMutex
->mMutex
) == 0);
180 bool wait(Mutex::Lock
& lock
, unsigned ms
)
182 // TODO for consistency, this function should take seconds
183 return (SDL_CondWaitTimeout(condition_
,
184 lock
.mMutex
->mMutex
, ms
) == 0);
189 return (SDL_CondSignal(condition_
) == 0);
193 return (SDL_CondBroadcast(condition_
) == 0);
198 SDL_cond
* condition_
;
206 Semaphore(unsigned int value
)
208 semaphore_
= SDL_CreateSemaphore(value
);
212 SDL_DestroySemaphore(semaphore_
);
217 return (SDL_SemWait(semaphore_
) == 0);
221 return (SDL_SemPost(semaphore_
) == 0);
226 return (SDL_SemTryWait(semaphore_
) == 0);
228 bool tryLock(unsigned ms
)
230 // TODO for consistency, this function should take seconds
231 return (SDL_SemWaitTimeout(semaphore_
, ms
) == 0);
238 Lock(Semaphore
& semaphore
)
240 semaphore_
= &semaphore
;
245 if (mIsLocked
) release();
250 return (mIsLocked
= semaphore_
->acquireLock());
254 return semaphore_
->releaseLock(); mIsLocked
= false;
257 bool isLocked() const
264 Semaphore
* semaphore_
;
268 class ScopedLock
: public Lock
272 ScopedLock(Semaphore
& semaphore
) :
287 #endif // _MOOF_THREAD_HH_
This page took 0.043417 seconds and 4 git commands to generate.