mirror of
https://github.com/debauchee/barrier.git
synced 2026-05-08 23:14:20 +08:00
Refactored some platform dependent code into a new library,
lib/arch. This should make porting easier. Will probably continue to refactor a little more, moving platform dependent event handling stuff into lib/platform.
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
*/
|
||||
|
||||
#include "CMutex.h"
|
||||
#include "CLog.h"
|
||||
#include "CArch.h"
|
||||
|
||||
//
|
||||
// CMutex
|
||||
@@ -21,17 +21,17 @@
|
||||
|
||||
CMutex::CMutex()
|
||||
{
|
||||
init();
|
||||
m_mutex = ARCH->newMutex();
|
||||
}
|
||||
|
||||
CMutex::CMutex(const CMutex&)
|
||||
{
|
||||
init();
|
||||
m_mutex = ARCH->newMutex();
|
||||
}
|
||||
|
||||
CMutex::~CMutex()
|
||||
{
|
||||
fini();
|
||||
ARCH->closeMutex(m_mutex);
|
||||
}
|
||||
|
||||
CMutex&
|
||||
@@ -40,111 +40,14 @@ CMutex::operator=(const CMutex&)
|
||||
return *this;
|
||||
}
|
||||
|
||||
#if HAVE_PTHREAD
|
||||
|
||||
#include <pthread.h>
|
||||
#include <cerrno>
|
||||
|
||||
void
|
||||
CMutex::init()
|
||||
{
|
||||
pthread_mutex_t* mutex = new pthread_mutex_t;
|
||||
int status = pthread_mutex_init(mutex, NULL);
|
||||
assert(status == 0);
|
||||
// status = pthread_mutexattr_settype(mutex, PTHREAD_MUTEX_RECURSIVE);
|
||||
// assert(status == 0);
|
||||
m_mutex = reinterpret_cast<void*>(mutex);
|
||||
}
|
||||
|
||||
void
|
||||
CMutex::fini()
|
||||
{
|
||||
pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(m_mutex);
|
||||
int status = pthread_mutex_destroy(mutex);
|
||||
LOGC(status != 0, (CLOG_ERR "pthread_mutex_destroy status %d", status));
|
||||
assert(status == 0);
|
||||
delete mutex;
|
||||
}
|
||||
|
||||
void
|
||||
CMutex::lock() const
|
||||
{
|
||||
pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(m_mutex);
|
||||
int status = pthread_mutex_lock(mutex);
|
||||
|
||||
switch (status) {
|
||||
case 0:
|
||||
// success
|
||||
return;
|
||||
|
||||
case EDEADLK:
|
||||
assert(0 && "lock already owned");
|
||||
break;
|
||||
|
||||
case EAGAIN:
|
||||
assert(0 && "too many recursive locks");
|
||||
break;
|
||||
|
||||
default:
|
||||
LOG((CLOG_ERR "pthread_mutex_lock status %d", status));
|
||||
assert(0 && "unexpected error");
|
||||
}
|
||||
ARCH->lockMutex(m_mutex);
|
||||
}
|
||||
|
||||
void
|
||||
CMutex::unlock() const
|
||||
{
|
||||
pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(m_mutex);
|
||||
int status = pthread_mutex_unlock(mutex);
|
||||
|
||||
switch (status) {
|
||||
case 0:
|
||||
// success
|
||||
return;
|
||||
|
||||
case EPERM:
|
||||
assert(0 && "thread doesn't own a lock");
|
||||
break;
|
||||
|
||||
default:
|
||||
LOG((CLOG_ERR "pthread_mutex_unlock status %d", status));
|
||||
assert(0 && "unexpected error");
|
||||
}
|
||||
ARCH->unlockMutex(m_mutex);
|
||||
}
|
||||
|
||||
#endif // HAVE_PTHREAD
|
||||
|
||||
#if WINDOWS_LIKE
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
void
|
||||
CMutex::init()
|
||||
{
|
||||
CRITICAL_SECTION* mutex = new CRITICAL_SECTION;
|
||||
InitializeCriticalSection(mutex);
|
||||
m_mutex = reinterpret_cast<void*>(mutex);
|
||||
}
|
||||
|
||||
void
|
||||
CMutex::fini()
|
||||
{
|
||||
CRITICAL_SECTION* mutex = reinterpret_cast<CRITICAL_SECTION*>(m_mutex);
|
||||
DeleteCriticalSection(mutex);
|
||||
delete mutex;
|
||||
}
|
||||
|
||||
void
|
||||
CMutex::lock() const
|
||||
{
|
||||
EnterCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(m_mutex));
|
||||
}
|
||||
|
||||
void
|
||||
CMutex::unlock() const
|
||||
{
|
||||
LeaveCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(m_mutex));
|
||||
}
|
||||
|
||||
#endif // WINDOWS_LIKE
|
||||
|
||||
Reference in New Issue
Block a user