22 #ifndef __JackPosixMutex__
23 #define __JackPosixMutex__
25 #include "JackError.h"
26 #include "JackException.h"
42 pthread_mutex_t fMutex;
49 int res = pthread_mutex_init(&fMutex, NULL);
50 ThrowIf(res != 0,
JackException(
"JackBasePosixMutex: could not init the mutex"));
55 pthread_mutex_destroy(&fMutex);
60 pthread_t current_thread = pthread_self();
62 if (!pthread_equal(current_thread, fOwner)) {
63 int res = pthread_mutex_lock(&fMutex);
65 fOwner = current_thread;
68 jack_error(
"JackBasePosixMutex::Lock res = %d", res);
72 jack_error(
"JackBasePosixMutex::Lock mutex already locked by thread = %d", current_thread);
79 pthread_t current_thread = pthread_self();
81 if (!pthread_equal(current_thread, fOwner)) {
82 int res = pthread_mutex_trylock(&fMutex);
84 fOwner = current_thread;
90 jack_error(
"JackBasePosixMutex::Trylock mutex already locked by thread = %d", current_thread);
97 if (pthread_equal(pthread_self(), fOwner)) {
99 int res = pthread_mutex_unlock(&fMutex);
103 jack_error(
"JackBasePosixMutex::Unlock res = %d", res);
107 jack_error(
"JackBasePosixMutex::Unlock mutex not locked by thread = %d owner %d", pthread_self(), fOwner);
118 pthread_mutex_t fMutex;
125 pthread_mutexattr_t mutex_attr;
127 res = pthread_mutexattr_init(&mutex_attr);
128 ThrowIf(res != 0,
JackException(
"JackBasePosixMutex: could not init the mutex attribute"));
129 res = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
130 ThrowIf(res != 0,
JackException(
"JackBasePosixMutex: could not settype the mutex"));
131 res = pthread_mutex_init(&fMutex, &mutex_attr);
132 ThrowIf(res != 0,
JackException(
"JackBasePosixMutex: could not init the mutex"));
133 pthread_mutexattr_destroy(&mutex_attr);
138 pthread_mutex_destroy(&fMutex);
143 int res = pthread_mutex_lock(&fMutex);
145 jack_log(
"JackPosixMutex::Lock res = %d", res);
152 return (pthread_mutex_trylock(&fMutex) == 0);
157 int res = pthread_mutex_unlock(&fMutex);
159 jack_log(
"JackPosixMutex::Unlock res = %d", res);