2001-10-06 18:13:28 +04:00
|
|
|
#include "CMutex.h"
|
2001-10-14 22:29:43 +04:00
|
|
|
#include "CLog.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
//
|
|
|
|
// CMutex
|
|
|
|
//
|
|
|
|
|
|
|
|
CMutex::CMutex()
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
CMutex::CMutex(const CMutex&)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
CMutex::~CMutex()
|
|
|
|
{
|
|
|
|
fini();
|
|
|
|
}
|
|
|
|
|
|
|
|
CMutex& CMutex::operator=(const CMutex&)
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(CONFIG_PTHREADS)
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2001-10-14 22:29:43 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2001-10-06 18:13:28 +04:00
|
|
|
void CMutex::fini()
|
|
|
|
{
|
|
|
|
pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(m_mutex);
|
|
|
|
int status = pthread_mutex_destroy(mutex);
|
2001-10-14 22:29:43 +04:00
|
|
|
logc(status != 0, (CLOG_ERR "pthread_mutex_destroy status %d", status));
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(status == 0);
|
|
|
|
delete mutex;
|
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
void CMutex::lock() const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(m_mutex);
|
|
|
|
int status = pthread_mutex_lock(mutex);
|
|
|
|
|
|
|
|
switch (status) {
|
2002-04-29 18:40:01 +04:00
|
|
|
case 0:
|
2001-10-06 18:13:28 +04:00
|
|
|
// success
|
|
|
|
return;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
case EDEADLK:
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(0 && "lock already owned");
|
|
|
|
break;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
case EAGAIN:
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(0 && "too many recursive locks");
|
|
|
|
break;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
default:
|
2001-10-14 22:29:43 +04:00
|
|
|
log((CLOG_ERR "pthread_mutex_lock status %d", status));
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(0 && "unexpected error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
void CMutex::unlock() const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(m_mutex);
|
|
|
|
int status = pthread_mutex_unlock(mutex);
|
|
|
|
|
|
|
|
switch (status) {
|
2002-04-29 18:40:01 +04:00
|
|
|
case 0:
|
2001-10-06 18:13:28 +04:00
|
|
|
// success
|
|
|
|
return;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
case EPERM:
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(0 && "thread doesn't own a lock");
|
|
|
|
break;
|
|
|
|
|
2002-04-29 18:40:01 +04:00
|
|
|
default:
|
2001-10-14 22:29:43 +04:00
|
|
|
log((CLOG_ERR "pthread_mutex_unlock status %d", status));
|
2001-10-06 18:13:28 +04:00
|
|
|
assert(0 && "unexpected error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CONFIG_PTHREADS
|
|
|
|
|
|
|
|
#if defined(CONFIG_PLATFORM_WIN32)
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
void CMutex::init()
|
|
|
|
{
|
|
|
|
CRITICAL_SECTION* mutex = new CRITICAL_SECTION;
|
2001-10-26 02:17:17 +04:00
|
|
|
InitializeCriticalSection(mutex);
|
2001-10-06 18:13:28 +04:00
|
|
|
m_mutex = reinterpret_cast<void*>(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMutex::fini()
|
|
|
|
{
|
|
|
|
CRITICAL_SECTION* mutex = reinterpret_cast<CRITICAL_SECTION*>(m_mutex);
|
2001-10-26 02:17:17 +04:00
|
|
|
DeleteCriticalSection(mutex);
|
2001-10-06 18:13:28 +04:00
|
|
|
delete mutex;
|
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
void CMutex::lock() const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-26 02:17:17 +04:00
|
|
|
EnterCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(m_mutex));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2001-10-14 20:58:01 +04:00
|
|
|
void CMutex::unlock() const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-26 02:17:17 +04:00
|
|
|
LeaveCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(m_mutex));
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CONFIG_PLATFORM_WIN32
|