]>
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.
22 #include <boost/bind.hpp>
23 #include <boost/function.hpp>
26 #include <moof/math.hh>
33 * Represents a thread which may be running. You cannot instantiate a
34 * thread object directly; new threads are created by detaching functions
35 * using the detach() method. Once a thread is detached, it will continue
36 * running until the function returns. You don't need to keep the thread
37 * object you want to wait() or kill() the thread later.
43 typedef boost::function
<int(void)> function
;
47 * Construct an invalid thread object which has no association with any
54 * Execute a function in a new thread.
55 * \param function The function to execute.
56 * \return The new thread, or an invalid thread if an error occurred.
58 static thread
detach(const function
& function
)
60 thread::function
* fcopy
= new thread::function(function
);
61 SDL_Thread
* thread
= SDL_CreateThread(&thread::run
, (void*)fcopy
);
62 if (thread
== 0) delete fcopy
;
63 return moof::thread(thread
);
68 * Wait for the thread to terminate, getting its return value. The
69 * thread will be invalidated.
70 * \return The integer value returned by the detached function.
75 SDL_WaitThread(thread_
, &i
);
81 * Forcefully kill the thread without giving it a chance to clean up
82 * after itself. The thread will be invalidated. Don't use this.
86 SDL_KillThread(thread_
);
91 * Get whether or not the thread object is associated with a real
93 * \return True if the thread is valid, false otherwise.
102 * Get a unique identifier for this thread, if it is valid.
103 * \return The identifier.
105 uint32_t identifier() const
107 return SDL_GetThreadID(thread_
);
111 * Get the unique identifier of the calling thread.
112 * \return The identifier.
114 static uint32_t current_identifier()
116 return SDL_ThreadID();
122 thread(SDL_Thread
* thread
) :
125 static int run(void* arg
)
127 int code
= (*(function
*)arg
)();
128 delete (function
*)arg
;
138 * An abstract class representing some task that is to be run
146 * Deconstruct the task.
148 virtual ~async_task() {}
151 * Get whether or not the task is done.
152 * \return True if the task is done, false otherwise.
154 virtual bool is_done() const = 0;
159 virtual void run() = 0;
162 * Block the current thread until the task is finished.
163 * \return A value representing the state of the finished task.
165 virtual int wait() = 0;
169 * An asynchronous task that is run to be executed in a separated thread.
176 * Get the thread object the task is executing in.
177 * \return The thread.
179 const class thread
& thread() const { return thread_
; }
182 * Block the current thread until the task thread is finished.
183 * \return The integer value returned by the task function.
187 return thread_
.wait();
193 class thread thread_
;
198 * A mutex to protect sensitive sections of code from threads which might
199 * otherwise cause unpredictable results.
209 mutex_(SDL_CreateMutex()) {}
212 * Deconstruct a mutex.
216 SDL_DestroyMutex(mutex_
);
221 * Block until the calling thread can secure exclusive access to the
222 * code protected by the mutex.
223 * \return True if the lock was acquired, false otherwise.
228 return (SDL_LockMutex(mutex_
) == 0);
232 * Unlock the mutex. Call this after the sensitive block of code to
233 * allow another thread to acquire the lock.
234 * \return True if the mutex was unlocked, false otherwise.
239 return (SDL_UnlockMutex(mutex_
) == 0);
244 * As an alternative method for locking, objects of this class will
245 * automagically release the lock if it is still locked at
246 * deconstruction. Therefore, it's generally safer to use this method
247 * since it makes it much more difficult to forget to unlock a mutex.
255 * \param mutex The mutex.
257 explicit lock(mutex
& mutex
, bool lock
= true) :
261 if (lock
) if (!acquire()) throw "mutex lock not acquired";
265 * Deconstruct a lock. The lock is automagically released if it is
270 if (is_locked_
) release();
275 * Try to acquire a lock on the mutex.
276 * \return True if the mutex was locked, false otherwise.
280 return (is_locked_
= mutex_
.acquire_lock());
285 * \return True if the mutex was unlocked, false otherwise.
289 bool result
= mutex_
.release_lock();
296 * Get whether or not the mutex is locked.
297 * \return True if the mutex is locked, false otherwise.
299 bool is_locked() const
310 friend class condition
;
318 friend class condition
;
323 * A class representing a condition variable.
330 * Construct a condition.
334 condition_
= SDL_CreateCond();
338 * Deconstruct a condition.
342 SDL_DestroyCond(condition_
);
347 * Unlock the mutex and wait for another thread to notify the thread,
348 * at which point the mutex will be re-locked and the method will
350 * \param mutex The mutex.
351 * \return True if the thread was notified, false otherwise.
353 bool wait(mutex
& mutex
)
355 return (SDL_CondWait(condition_
, mutex
.mutex_
) == 0);
359 * Unlock the mutex associated with a lock and wait for another thread
360 * to notify the thread, at which point the lock will be re-locked and
361 * the method will return.
362 * \param lock The lock.
363 * \return True if the thread was notified, false otherwise.
365 bool wait(mutex::lock
& lock
)
367 return (SDL_CondWait(condition_
, lock
.mutex_
.mutex_
) == 0);
371 * Unlock the mutex and wait for another thread to notify the thread,
372 * at which point the mutex will be re-locked and the method will
373 * return. If the thread was not notified before a certain number of
374 * seconds, the method will return anyway.
375 * \param mutex The mutex.
376 * \param timeout Number of seconds to wait.
377 * \return True if the thread was notified, false otherwise.
379 bool wait(mutex
& mutex
, scalar timeout
)
381 Uint32 ms
= timeout
* SCALAR(1000.0);
382 return (SDL_CondWaitTimeout(condition_
, mutex
.mutex_
, ms
) == 0);
386 * Unlock the mutex associated with a lock and wait for another thread
387 * to notify the thread, at which point the lock will be re-locked and
388 * the method will return. If the thread was not notified before a
389 * certain number of seconds, the method will return anyway.
390 * \param lock The lock.
391 * \param timeout Number of seconds to wait.
392 * \return True if the thread was notified, false otherwise.
394 bool wait(mutex::lock
& lock
, scalar timeout
)
396 Uint32 ms
= timeout
* SCALAR(1000.0);
397 return (SDL_CondWaitTimeout(condition_
,
398 lock
.mutex_
.mutex_
, ms
) == 0);
403 * Notify one other thread that is waiting on the condition.
404 * \return True on success, false otherwise.
408 return (SDL_CondSignal(condition_
) == 0);
412 * Notify all other threads that are waiting on the condition.
413 * \return True on success, false otherwise.
417 return (SDL_CondBroadcast(condition_
) == 0);
423 SDL_cond
* condition_
;
435 * Construct a semaphore.
436 * \param value The initial value of the semaphore.
438 explicit semaphore(uint32_t value
)
440 semaphore_
= SDL_CreateSemaphore(value
);
444 * Deconstruct a semaphore.
448 SDL_DestroySemaphore(semaphore_
);
453 * Block until the calling thread can secure exclusive access to the
454 * code protected by the semaphore.
455 * \return True if the lock was acquired, false otherwise.
460 return (SDL_SemWait(semaphore_
) == 0);
464 * Block until the calling thread can secure exclusive access to the
465 * code protected by the semaphore, or until the timeout expires.
466 * \param timeout Number of seconds to try.
467 * \return True if the lock was acquired, false otherwise.
469 bool acquire_lock(scalar timeout
)
471 Uint32 ms
= timeout
* SCALAR(1000.0);
472 return (SDL_SemWaitTimeout(semaphore_
, ms
) == 0);
476 * Unlock the semaphore. Call this after the sensitive block of code
477 * to allow another thread to acquire the lock.
478 * \return True if the semaphore was unlocked, false otherwise.
483 return (SDL_SemPost(semaphore_
) == 0);
487 * Try to lock the semaphore, but don't block if the lock is not
488 * immediately available.
489 * \return True if the semaphore was locked, false otherwise.
493 return (SDL_SemTryWait(semaphore_
) == 0);
497 * As an alternative method for locking, objects of this class will
498 * automagically release the lock if it is still locked at
499 * deconstruction. Therefore, it's generally safer to use this method
500 * since it makes it much more difficult to forget to unlock a
509 * \param semaphore The semaphore.
511 explicit lock(semaphore
& semaphore
, bool lock
= true) :
512 semaphore_(semaphore
),
515 if (lock
) if (!acquire()) throw "semaphore lock not acquired";
519 * Deconstruct a lock. The lock is automagically released if it is
524 if (is_locked_
) release();
529 * Try to acquire a lock on the semaphore.
530 * \return True if the semaphore was locked, false otherwise.
534 return (is_locked_
= semaphore_
.acquire_lock());
539 * \return True if the semaphore was unlocked, false otherwise.
543 bool result
= semaphore_
.release_lock();
549 * Get whether or not the semaphore is locked.
550 * \return True if the semaphore is locked, false otherwise.
552 bool is_locked() const
560 semaphore
& semaphore_
;
572 #define MOOF_DECLARE_MUTEX(M) moof::mutex M
573 #define MOOF_MUTEX_LOCK(M) moof::mutex::lock lock_##M(M)
575 #define MOOF_DECLARE_MUTEX(M)
576 #define MOOF_MUTEX_LOCK(M)
582 #endif // _MOOF_THREAD_HH_
This page took 0.063687 seconds and 4 git commands to generate.