/* ----------------------------------------------------------------------------- GSFramework Copyright 2001-2013 Emmanuel Julien. All Rights Reserved. ----------------------------------------------------------------------------- */ #include "thread/mutex.h" #include "alloc/ialloc.h" using namespace GS::Threading; #if __PLATFORM_WINDOWS__ #define WIN32_LEAN_AND_MEAN #include //------------------------------------------------------------------------------ bool Mutex::TryLock() { return asbool(TryEnterCriticalSection((LPCRITICAL_SECTION)mutex)); } void Mutex::Lock() { if (mutex) EnterCriticalSection((LPCRITICAL_SECTION)mutex); } void Mutex::Unlock() { if (mutex) LeaveCriticalSection((LPCRITICAL_SECTION)mutex); } Mutex::Mutex() { mutex = new CRITICAL_SECTION; InitializeCriticalSection((LPCRITICAL_SECTION)mutex); } Mutex::~Mutex() { DeleteCriticalSection((LPCRITICAL_SECTION)mutex); _safe_delete(mutex); } //------------------------------------------------------------------------------ #elif __PLATFORM_POSIX__ #include //------------------------------------------------------------------------------ bool Mutex::TryLock() { return pthread_mutex_trylock(&mutex) != EBUSY ? true : false; } void Mutex::Lock() { pthread_mutex_lock(&mutex); } void Mutex::Unlock() { pthread_mutex_unlock(&mutex); } Mutex::Mutex() { pthread_mutex_init(&mutex, NULL); } Mutex::~Mutex() { pthread_mutex_destroy(&mutex); } //------------------------------------------------------------------------------ #elif __PLATFORM_NINTENDO_WII__ //------------------------------------------------------------------------------ bool Mutex::TryLock() { return OSTryLockMutex(&mutex); } void Mutex::Lock() { OSLockMutex(&mutex); } void Mutex::Unlock() { OSUnlockMutex(&mutex); } Mutex::Mutex() { OSInitMutex(&mutex); } Mutex::~Mutex() {} //------------------------------------------------------------------------------ #else //------------------------------------------------------------------------------ bool Mutex::TryLock() { return true; } void Mutex::Lock() {} void Mutex::Unlock() {} Mutex::Mutex() {} Mutex::~Mutex() {} //------------------------------------------------------------------------------ #endif