10
bool Mutex::Init(bool initial_owner)
16
pthread_mutexattr_t mutexAttr;
18
pthread_mutexattr_init(&mutexAttr);
19
pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE);
20
pthread_mutexattr_setpshared(&mutexAttr, PTHREAD_PROCESS_PRIVATE);
22
if(!pthread_mutex_init(&mutex, &mutexAttr)) {
23
if(initial_owner) res = (Wait() == WAIT_OBJECT);
27
if(!res) pthread_mutex_destroy(&mutex);
28
pthread_mutexattr_destroy(&mutexAttr);
30
if(res) IPCObject::Init();
34
WaitResult Mutex::Wait(int time_out)
39
if(pthread_mutex_lock(&mutex)) return WAIT_ERROR;
41
locker = pthread_self();
47
struct timespec delay = {0, 1000000};
49
while((res = pthread_mutex_trylock(&mutex)) == EBUSY) {
50
if(!time_out) return WAIT_TIMEOUT;
52
nanosleep(&delay, NULL);
57
if(res) return WAIT_ERROR;
59
locker = pthread_self();
69
if(locker != pthread_self()) return false;
71
if(pthread_mutex_unlock(&mutex)) return false;
82
if(!IsValid()) return true;
86
if(pthread_mutex_destroy(&mutex)) return false;