X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Fthread.hh;fp=src%2FMoof%2FThread.hh;h=09caefd3048800a0cab77380bbceb62743700e96;hp=2987a59b968716b7649c1eb5d180072f3c308e8a;hb=831f04d4bc19a390415ac0bbac4331c7a65509bc;hpb=299af4f2047e767e5d79501c26444473bda64c64 diff --git a/src/Moof/Thread.hh b/src/moof/thread.hh similarity index 72% rename from src/Moof/Thread.hh rename to src/moof/thread.hh index 2987a59..09caefd 100644 --- a/src/Moof/Thread.hh +++ b/src/moof/thread.hh @@ -10,7 +10,7 @@ **************************************************************************/ /** - * \file Thread.hh + * \file thread.hh * Light C++ wrapper around the SDL threads API. */ @@ -21,10 +21,10 @@ #include #include -#include +#include -namespace Mf { +namespace moof { /** @@ -34,31 +34,31 @@ namespace Mf { * running until the function returns. You don't need to keep the thread * object you want to wait() or kill() the thread later. */ -class Thread +class thread { public: - typedef boost::function Function; + typedef boost::function function; /** * Construct an invalid thread object which has no association with any * real thread. */ - Thread() : - mThread(0) {} + thread() : + thread_(0) {} /** * Execute a function in a new thread. * \param function The function to execute. * \return The new thread, or an invalid thread if an error occurred. */ - static Thread detach(const Function& function) + static thread detach(const function& function) { - Function* fcopy = new Function(function); - SDL_Thread* thread = SDL_CreateThread(&Thread::run, (void*)fcopy); + thread::function* fcopy = new thread::function(function); + SDL_Thread* thread = SDL_CreateThread(&thread::run, (void*)fcopy); if (thread == 0) delete fcopy; - return Thread(thread); + return moof::thread(thread); } @@ -70,8 +70,8 @@ public: int wait() { int i; - SDL_WaitThread(mThread, &i); - mThread = 0; + SDL_WaitThread(thread_, &i); + thread_ = 0; return i; } @@ -81,8 +81,8 @@ public: */ void kill() { - SDL_KillThread(mThread); - mThread = 0; + SDL_KillThread(thread_); + thread_ = 0; } /** @@ -90,9 +90,9 @@ public: * thread. * \return True if the thread is valid, false otherwise. */ - bool isValid() const + bool is_valid() const { - return mThread != 0; + return thread_ != 0; } @@ -102,14 +102,14 @@ public: */ uint32_t identifier() const { - return SDL_GetThreadID(mThread); + return SDL_GetThreadID(thread_); } /** * Get the unique identifier of the calling thread. * \return The identifier. */ - static uint32_t currentIdentifier() + static uint32_t current_identifier() { return SDL_ThreadID(); } @@ -117,18 +117,18 @@ public: private: - Thread(SDL_Thread* thread) : - mThread(thread) {} + thread(SDL_Thread* thread) : + thread_(thread) {} static int run(void* arg) { - int code = (*(Function*)arg)(); - delete (Function*)arg; + int code = (*(function*)arg)(); + delete (function*)arg; return code; } - SDL_Thread* mThread; + SDL_Thread* thread_; }; @@ -136,20 +136,20 @@ private: * An abstract class representing some task that is to be run * asynchronously. */ -class AsyncTask +class async_task { public: /** * Deconstruct the task. */ - virtual ~AsyncTask() {} + virtual ~async_task() {} /** * Get whether or not the task is done. * \return True if the task is done, false otherwise. */ - virtual bool isDone() const = 0; + virtual bool is_done() const = 0; /** * Begin the task. @@ -166,7 +166,7 @@ public: /** * An asynchronous task that is run to be executed in a separated thread. */ -class ThreadedTask +class threaded_task { public: @@ -174,7 +174,7 @@ public: * Get the thread object the task is executing in. * \return The thread. */ - const Thread& thread() const { return mThread; } + const class thread& thread() const { return thread_; } /** * Block the current thread until the task thread is finished. @@ -182,13 +182,13 @@ public: */ int wait() { - return mThread.wait(); + return thread_.wait(); } protected: - Thread mThread; + class thread thread_; }; @@ -196,22 +196,22 @@ protected: * A mutex to protect sensitive sections of code from threads which might * otherwise cause unpredictable results. */ -class Mutex +class mutex { public: /** * Construct a mutex. */ - Mutex() : - mMutex(SDL_CreateMutex()) {} + mutex() : + mutex_(SDL_CreateMutex()) {} /** * Deconstruct a mutex. */ - ~Mutex() + ~mutex() { - SDL_DestroyMutex(mMutex); + SDL_DestroyMutex(mutex_); } @@ -219,22 +219,22 @@ public: * Block until the calling thread can secure exclusive access to the * code protected by the mutex. * \return True if the lock was acquired, false otherwise. - * \see Lock + * \see lock */ - bool acquireLock() + bool acquire_lock() { - return (SDL_LockMutex(mMutex) == 0); + return (SDL_LockMutex(mutex_) == 0); } /** * Unlock the mutex. Call this after the sensitive block of code to * allow another thread to acquire the lock. * \return True if the mutex was unlocked, false otherwise. - * \see Lock + * \see lock */ - bool releaseLock() + bool release_lock() { - return (SDL_UnlockMutex(mMutex) == 0); + return (SDL_UnlockMutex(mutex_) == 0); } @@ -244,7 +244,7 @@ public: * deconstruction. Therefore, it's generally safer to use this method * since it makes it much more difficult to forget to unlock a mutex. */ - class Lock + class lock { public: @@ -252,17 +252,17 @@ public: * Construct a lock. * \param mutex The mutex. */ - explicit Lock(Mutex& mutex) : - mMutex(mutex), - mIsLocked(false) {} + explicit lock(mutex& mutex) : + mutex_(mutex), + is_locked_(false) {} /** * Deconstruct a lock. The lock is automagically released if it is * still locked. */ - ~Lock() + ~lock() { - if (mIsLocked) release(); + if (is_locked_) release(); } @@ -272,7 +272,7 @@ public: */ bool acquire() { - return (mIsLocked = mMutex.acquireLock()); + return (is_locked_ = mutex_.acquire_lock()); } /** @@ -281,8 +281,8 @@ public: */ bool release() { - bool result = mMutex.releaseLock(); - mIsLocked = false; + bool result = mutex_.release_lock(); + is_locked_ = false; return result; } @@ -291,25 +291,25 @@ public: * Get whether or not the mutex is locked. * \return True if the mutex is locked, false otherwise. */ - bool isLocked() const + bool is_locked() const { - return mIsLocked; + return is_locked_; } protected: - Mutex& mMutex; - bool mIsLocked; + mutex& mutex_; + bool is_locked_; - friend class Condition; + friend class condition; }; /** * This type of lock tries to acquire a lock on the mutex during * construction and releases the lock on deconstruction. */ - class ScopedLock : private Lock + class scoped_lock : private lock { public: @@ -317,8 +317,8 @@ public: * Construct a lock. * \param mutex The mutex. */ - explicit ScopedLock(Mutex& mutex) : - Lock(mutex) + explicit scoped_lock(mutex& mutex) : + lock(mutex) { acquire(); } @@ -327,42 +327,42 @@ public: * Get whether or not the mutex is locked. * \return True if the mutex is locked, false otherwise. */ - bool isLocked() const + bool is_locked() const { - return Lock::isLocked(); + return lock::is_locked(); } }; private: - SDL_mutex* mMutex; + SDL_mutex* mutex_; - friend class Condition; + friend class condition; }; /** * A class representing a condition variable. */ -class Condition +class condition { public: /** * Construct a condition. */ - Condition() + condition() { - mCondition = SDL_CreateCond(); + condition_ = SDL_CreateCond(); } /** * Deconstruct a condition. */ - ~Condition() + ~condition() { - SDL_DestroyCond(mCondition); + SDL_DestroyCond(condition_); } @@ -373,9 +373,9 @@ public: * \param mutex The mutex. * \return True if the thread was notified, false otherwise. */ - bool wait(Mutex& mutex) + bool wait(mutex& mutex) { - return (SDL_CondWait(mCondition, mutex.mMutex) == 0); + return (SDL_CondWait(condition_, mutex.mutex_) == 0); } /** @@ -385,9 +385,9 @@ public: * \param lock The lock. * \return True if the thread was notified, false otherwise. */ - bool wait(Mutex::Lock& lock) + bool wait(mutex::lock& lock) { - return (SDL_CondWait(mCondition, lock.mMutex.mMutex) == 0); + return (SDL_CondWait(condition_, lock.mutex_.mutex_) == 0); } /** @@ -399,10 +399,10 @@ public: * \param timeout Number of seconds to wait. * \return True if the thread was notified, false otherwise. */ - bool wait(Mutex& mutex, Scalar timeout) + bool wait(mutex& mutex, scalar timeout) { Uint32 ms = timeout * SCALAR(1000.0); - return (SDL_CondWaitTimeout(mCondition, mutex.mMutex, ms) == 0); + return (SDL_CondWaitTimeout(condition_, mutex.mutex_, ms) == 0); } /** @@ -414,11 +414,11 @@ public: * \param timeout Number of seconds to wait. * \return True if the thread was notified, false otherwise. */ - bool wait(Mutex::Lock& lock, Scalar timeout) + bool wait(mutex::lock& lock, scalar timeout) { Uint32 ms = timeout * SCALAR(1000.0); - return (SDL_CondWaitTimeout(mCondition, - lock.mMutex.mMutex, ms) == 0); + return (SDL_CondWaitTimeout(condition_, + lock.mutex_.mutex_, ms) == 0); } @@ -428,29 +428,29 @@ public: */ bool notify() { - return (SDL_CondSignal(mCondition) == 0); + return (SDL_CondSignal(condition_) == 0); } /** * Notify all other threads that are waiting on the condition. * \return True on success, false otherwise. */ - bool notifyAll() + bool notify_all() { - return (SDL_CondBroadcast(mCondition) == 0); + return (SDL_CondBroadcast(condition_) == 0); } private: - SDL_cond* mCondition; + SDL_cond* condition_; }; /** * A semaphore class. */ -class Semaphore +class semaphore { public: @@ -458,17 +458,17 @@ public: * Construct a semaphore. * \param value The initial value of the semaphore. */ - explicit Semaphore(uint32_t value) + explicit semaphore(uint32_t value) { - mSemaphore = SDL_CreateSemaphore(value); + semaphore_ = SDL_CreateSemaphore(value); } /** * Deconstruct a semaphore. */ - ~Semaphore() + ~semaphore() { - SDL_DestroySemaphore(mSemaphore); + SDL_DestroySemaphore(semaphore_); } @@ -476,11 +476,11 @@ public: * Block until the calling thread can secure exclusive access to the * code protected by the semaphore. * \return True if the lock was acquired, false otherwise. - * \see Lock + * \see lock */ - bool acquireLock() + bool acquire_lock() { - return (SDL_SemWait(mSemaphore) == 0); + return (SDL_SemWait(semaphore_) == 0); } /** @@ -489,21 +489,21 @@ public: * \param timeout Number of seconds to try. * \return True if the lock was acquired, false otherwise. */ - bool acquireLock(Scalar timeout) + bool acquire_lock(scalar timeout) { Uint32 ms = timeout * SCALAR(1000.0); - return (SDL_SemWaitTimeout(mSemaphore, ms) == 0); + return (SDL_SemWaitTimeout(semaphore_, ms) == 0); } /** * Unlock the semaphore. Call this after the sensitive block of code * to allow another thread to acquire the lock. * \return True if the semaphore was unlocked, false otherwise. - * \see Lock + * \see lock */ - bool releaseLock() + bool release_lock() { - return (SDL_SemPost(mSemaphore) == 0); + return (SDL_SemPost(semaphore_) == 0); } /** @@ -511,9 +511,9 @@ public: * immediately available. * \return True if the semaphore was locked, false otherwise. */ - bool tryLock() + bool try_lock() { - return (SDL_SemTryWait(mSemaphore) == 0); + return (SDL_SemTryWait(semaphore_) == 0); } /** @@ -523,7 +523,7 @@ public: * since it makes it much more difficult to forget to unlock a * semaphore. */ - class Lock + class lock { public: @@ -531,17 +531,17 @@ public: * Construct a lock. * \param semaphore The semaphore. */ - explicit Lock(Semaphore& semaphore) : - mSemaphore(semaphore), - mIsLocked(false) {} + explicit lock(semaphore& semaphore) : + semaphore_(semaphore), + is_locked_(false) {} /** * Deconstruct a lock. The lock is automagically released if it is * still locked. */ - ~Lock() + ~lock() { - if (mIsLocked) release(); + if (is_locked_) release(); } @@ -551,7 +551,7 @@ public: */ bool acquire() { - return (mIsLocked = mSemaphore.acquireLock()); + return (is_locked_ = semaphore_.acquire_lock()); } /** @@ -560,8 +560,8 @@ public: */ bool release() { - bool result = mSemaphore.releaseLock(); - mIsLocked = false; + bool result = semaphore_.release_lock(); + is_locked_ = false; return result; } @@ -569,23 +569,23 @@ public: * Get whether or not the semaphore is locked. * \return True if the semaphore is locked, false otherwise. */ - bool isLocked() const + bool is_locked() const { - return mIsLocked; + return is_locked_; } protected: - Semaphore& mSemaphore; - bool mIsLocked; + semaphore& semaphore_; + bool is_locked_; }; /** * This type of lock tries to acquire a lock on the semaphore during * construction and releases the lock on deconstruction. */ - class ScopedLock : private Lock + class scoped_lock : private lock { public: @@ -593,8 +593,8 @@ public: * Construct a lock. * \param semaphore The semaphore. */ - explicit ScopedLock(Semaphore& semaphore) : - Lock(semaphore) + explicit scoped_lock(semaphore& semaphore) : + lock(semaphore) { acquire(); } @@ -603,20 +603,20 @@ public: * Get whether or not the semaphore is locked. * \return True if the semaphore is locked, false otherwise. */ - bool isLocked() const + bool is_locked() const { - return Lock::isLocked(); + return lock::is_locked(); } }; private: - SDL_sem* mSemaphore; + SDL_sem* semaphore_; }; -} // namespace Mf +} // namespace moof #endif // _MOOF_THREAD_HH_