2011-09-27 17:48:46 +00:00
|
|
|
// This code is in the public domain -- castano@gmail.com
|
|
|
|
|
|
|
|
#include "Mutex.h"
|
|
|
|
|
|
|
|
#if NV_OS_WIN32
|
|
|
|
|
|
|
|
#include "Win32.h"
|
|
|
|
|
2013-06-07 17:53:55 +00:00
|
|
|
#elif NV_OS_USE_PTHREAD
|
2011-09-27 17:48:46 +00:00
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h> // EBUSY
|
|
|
|
|
|
|
|
#endif // NV_OS
|
|
|
|
|
|
|
|
using namespace nv;
|
|
|
|
|
|
|
|
|
|
|
|
#if NV_OS_WIN32
|
|
|
|
|
|
|
|
struct Mutex::Private {
|
2011-09-27 18:12:32 +00:00
|
|
|
CRITICAL_SECTION mutex;
|
2011-09-27 17:48:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Mutex::Mutex () : m(new Private)
|
|
|
|
{
|
2011-09-27 18:12:32 +00:00
|
|
|
InitializeCriticalSection(&m->mutex);
|
2011-09-27 17:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Mutex::~Mutex ()
|
|
|
|
{
|
2011-09-27 18:12:32 +00:00
|
|
|
DeleteCriticalSection(&m->mutex);
|
2011-09-27 17:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Mutex::lock()
|
|
|
|
{
|
2011-09-27 18:12:32 +00:00
|
|
|
EnterCriticalSection(&m->mutex);
|
2011-09-27 17:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Mutex::tryLock()
|
|
|
|
{
|
2011-09-27 18:12:32 +00:00
|
|
|
return TryEnterCriticalSection(&m->mutex) != 0;
|
2011-09-27 17:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Mutex::unlock()
|
|
|
|
{
|
2011-09-27 18:12:32 +00:00
|
|
|
LeaveCriticalSection(&m->mutex);
|
2011-09-27 17:48:46 +00:00
|
|
|
}
|
|
|
|
|
2013-06-07 17:53:55 +00:00
|
|
|
#elif NV_OS_USE_PTHREAD
|
2011-09-27 17:48:46 +00:00
|
|
|
|
|
|
|
struct Mutex::Private {
|
2011-09-27 18:12:32 +00:00
|
|
|
pthread_mutex_t mutex;
|
2011-09-27 17:48:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Mutex::Mutex () : m(new Private)
|
|
|
|
{
|
2011-09-27 18:12:32 +00:00
|
|
|
int result = pthread_mutex_init(&m->mutex , NULL);
|
|
|
|
nvDebugCheck(result == 0);
|
2011-09-27 17:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Mutex::~Mutex ()
|
|
|
|
{
|
2011-09-27 18:12:32 +00:00
|
|
|
int result = pthread_mutex_destroy(&m->mutex);
|
|
|
|
nvDebugCheck(result == 0);
|
2011-09-27 17:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Mutex::lock()
|
|
|
|
{
|
2011-09-27 18:12:32 +00:00
|
|
|
int result = pthread_mutex_lock(&m->mutex);
|
|
|
|
nvDebugCheck(result == 0);
|
2011-09-27 17:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Mutex::tryLock()
|
|
|
|
{
|
2011-09-27 18:12:32 +00:00
|
|
|
int result = pthread_mutex_trylock(&m->mutex);
|
|
|
|
nvDebugCheck(result == 0 || result == EBUSY);
|
|
|
|
return result == 0;
|
2011-09-27 17:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Mutex::unlock()
|
|
|
|
{
|
2011-09-27 18:12:32 +00:00
|
|
|
int result = pthread_mutex_unlock(&m->mutex);
|
|
|
|
nvDebugCheck(result == 0);
|
2011-09-27 17:48:46 +00:00
|
|
|
}
|
|
|
|
|
2011-09-27 18:12:32 +00:00
|
|
|
#endif // NV_OS_UNIX
|