]>
Dogcows Code - chaz/yoink/blob - 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>
28 // The detach function detaches a separate thread by calling 'func' with
29 // the 'arg' parameter.
32 typedef SDL_Thread
* Thread
;
34 typedef boost::function
<int(void)> Function
;
37 inline int detach_(void* arg
)
39 //Function function = *(Function*)arg;
40 int code
= (*(Function
*)arg
)();
42 delete (Function
*)arg
;
46 inline Thread
detachFunction(const Function
& function
)
48 Function
* fcopy
= new Function(function
);
49 Thread thread
= SDL_CreateThread(detach_
, (void*)fcopy
);
51 if (thread
== 0) delete fcopy
;
56 inline int waitOnThread(Thread thread
)
59 SDL_WaitThread(thread
, &i
);
63 inline void killThread(Thread thread
)
65 SDL_KillThread(thread
);
70 // The identifier function returns a unique integer for the calling thread.
73 inline unsigned getThreadIdentifier()
75 return SDL_ThreadID();
78 inline unsigned getThreadIdentifier(Thread thread
)
80 return SDL_GetThreadID(thread
);
88 virtual ~AsyncTask() {}
90 virtual bool isDone() const = 0;
92 virtual void run() = 0;
93 virtual int wait() = 0;
103 Thread
thread() const { return mThread
; }
107 int code
= waitOnThread(mThread
);
119 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123 friend class Condition
;
129 mMutex
= SDL_CreateMutex();
133 SDL_DestroyMutex(mMutex
);
138 return (SDL_LockMutex(mMutex
) == 0);
142 return (SDL_UnlockMutex(mMutex
) == 0);
147 friend class Condition
;
158 if (mIsLocked
) release();
163 return (mIsLocked
= mMutex
->acquireLock());
167 return mMutex
->releaseLock();
171 bool isLocked() const
182 class ScopedLock
: public Lock
186 ScopedLock(Mutex
& mutex
) :
205 condition_
= SDL_CreateCond();
209 SDL_DestroyCond(condition_
);
212 bool wait(Mutex::Lock
& lock
)
214 return (SDL_CondWait(condition_
, lock
.mMutex
->mMutex
) == 0);
216 bool wait(Mutex::Lock
& lock
, unsigned ms
)
218 // TODO for consistency, this function should take seconds
219 return (SDL_CondWaitTimeout(condition_
,
220 lock
.mMutex
->mMutex
, ms
) == 0);
225 return (SDL_CondSignal(condition_
) == 0);
229 return (SDL_CondBroadcast(condition_
) == 0);
234 SDL_cond
* condition_
;
242 Semaphore(unsigned int value
)
244 semaphore_
= SDL_CreateSemaphore(value
);
248 SDL_DestroySemaphore(semaphore_
);
253 return (SDL_SemWait(semaphore_
) == 0);
257 return (SDL_SemPost(semaphore_
) == 0);
262 return (SDL_SemTryWait(semaphore_
) == 0);
264 bool tryLock(unsigned ms
)
266 // TODO for consistency, this function should take seconds
267 return (SDL_SemWaitTimeout(semaphore_
, ms
) == 0);
274 Lock(Semaphore
& semaphore
)
276 semaphore_
= &semaphore
;
281 if (mIsLocked
) release();
286 return (mIsLocked
= semaphore_
->acquireLock());
290 return semaphore_
->releaseLock(); mIsLocked
= false;
293 bool isLocked() const
300 Semaphore
* semaphore_
;
304 class ScopedLock
: public Lock
308 ScopedLock(Semaphore
& semaphore
) :
323 #endif // _MOOF_THREAD_HH_
This page took 0.048571 seconds and 4 git commands to generate.