00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __JackPosixMutex__
00023 #define __JackPosixMutex__
00024
00025 #include <pthread.h>
00026 #include <stdio.h>
00027 #include <assert.h>
00028 #include "JackError.h"
00029
00030 namespace Jack
00031 {
00037 class JackBasePosixMutex
00038 {
00039
00040 protected:
00041
00042 pthread_mutex_t fMutex;
00043
00044 public:
00045
00046 JackBasePosixMutex()
00047 {
00048 pthread_mutex_init(&fMutex, NULL);
00049 }
00050
00051 virtual ~JackBasePosixMutex()
00052 {
00053 pthread_mutex_destroy(&fMutex);
00054 }
00055
00056 void Lock()
00057 {
00058 int res = pthread_mutex_lock(&fMutex);
00059 if (res != 0)
00060 jack_error("JackBasePosixMutex::Lock res = %d", res);
00061 }
00062
00063 bool Trylock()
00064 {
00065 return (pthread_mutex_trylock(&fMutex) == 0);
00066 }
00067
00068 void Unlock()
00069 {
00070 int res = pthread_mutex_unlock(&fMutex);
00071 if (res != 0)
00072 jack_error("JackBasePosixMutex::Unlock res = %d", res);
00073 }
00074
00075 };
00076
00077 class JackPosixMutex
00078 {
00079
00080 protected:
00081
00082 pthread_mutex_t fMutex;
00083
00084 public:
00085
00086 JackPosixMutex()
00087 {
00088
00089 pthread_mutexattr_t mutex_attr;
00090 int res;
00091 res = pthread_mutexattr_init(&mutex_attr);
00092 assert(res == 0);
00093 res = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
00094 assert(res == 0);
00095 res = pthread_mutex_init(&fMutex, &mutex_attr);
00096 assert(res == 0);
00097 res = pthread_mutexattr_destroy(&mutex_attr);
00098 assert(res == 0);
00099 }
00100
00101 virtual ~JackPosixMutex()
00102 {
00103 pthread_mutex_destroy(&fMutex);
00104 }
00105
00106 bool Lock()
00107 {
00108 int res = pthread_mutex_lock(&fMutex);
00109 if (res != 0)
00110 jack_error("JackPosixMutex::Lock res = %d", res);
00111 return (res == 0);
00112 }
00113
00114 bool Trylock()
00115 {
00116 return (pthread_mutex_trylock(&fMutex) == 0);
00117 }
00118
00119 bool Unlock()
00120 {
00121 int res = pthread_mutex_unlock(&fMutex);
00122 if (res != 0)
00123 jack_error("JackPosixMutex::Unlock res = %d", res);
00124 return (res == 0);
00125 }
00126
00127 };
00128
00129
00130 }
00131
00132 #endif