Jack2
1.9.8
|
00001 /* 00002 Copyright (C) 2006 Grame 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Lesser General Public 00006 License as published by the Free Software Foundation; either 00007 version 2.1 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Lesser General Public License for more details. 00013 00014 You should have received a copy of the GNU Lesser General Public 00015 License along with this library; if not, write to the Free Software 00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France 00019 grame@grame.fr 00020 */ 00021 00022 #ifndef __JackPosixMutex__ 00023 #define __JackPosixMutex__ 00024 00025 #include "JackError.h" 00026 #include "JackException.h" 00027 #include <pthread.h> 00028 #include <stdio.h> 00029 #include <assert.h> 00030 00031 namespace Jack 00032 { 00037 class JackBasePosixMutex 00038 { 00039 00040 protected: 00041 00042 pthread_mutex_t fMutex; 00043 pthread_t fOwner; 00044 00045 public: 00046 00047 JackBasePosixMutex():fOwner(0) 00048 { 00049 int res = pthread_mutex_init(&fMutex, NULL); 00050 ThrowIf(res != 0, JackException("JackBasePosixMutex: could not init the mutex")); 00051 } 00052 00053 virtual ~JackBasePosixMutex() 00054 { 00055 pthread_mutex_destroy(&fMutex); 00056 } 00057 00058 bool Lock() 00059 { 00060 pthread_t current_thread = pthread_self(); 00061 00062 if (!pthread_equal(current_thread, fOwner)) { 00063 int res = pthread_mutex_lock(&fMutex); 00064 if (res == 0) { 00065 fOwner = current_thread; 00066 return true; 00067 } else { 00068 jack_error("JackBasePosixMutex::Lock res = %d", res); 00069 return false; 00070 } 00071 } else { 00072 jack_error("JackBasePosixMutex::Lock mutex already locked by thread = %d", current_thread); 00073 return false; 00074 } 00075 } 00076 00077 bool Trylock() 00078 { 00079 pthread_t current_thread = pthread_self(); 00080 00081 if (!pthread_equal(current_thread, fOwner)) { 00082 int res = pthread_mutex_trylock(&fMutex); 00083 if (res == 0) { 00084 fOwner = current_thread; 00085 return true; 00086 } else { 00087 return false; 00088 } 00089 } else { 00090 jack_error("JackBasePosixMutex::Trylock mutex already locked by thread = %d", current_thread); 00091 return false; 00092 } 00093 } 00094 00095 bool Unlock() 00096 { 00097 if (pthread_equal(pthread_self(), fOwner)) { 00098 fOwner = 0; 00099 int res = pthread_mutex_unlock(&fMutex); 00100 if (res == 0) { 00101 return true; 00102 } else { 00103 jack_error("JackBasePosixMutex::Unlock res = %d", res); 00104 return false; 00105 } 00106 } else { 00107 jack_error("JackBasePosixMutex::Unlock mutex not locked by thread = %d owner %d", pthread_self(), fOwner); 00108 return false; 00109 } 00110 } 00111 00112 }; 00113 00114 class JackPosixMutex 00115 { 00116 protected: 00117 00118 pthread_mutex_t fMutex; 00119 00120 public: 00121 00122 JackPosixMutex() 00123 { 00124 // Use recursive mutex 00125 pthread_mutexattr_t mutex_attr; 00126 int res; 00127 res = pthread_mutexattr_init(&mutex_attr); 00128 ThrowIf(res != 0, JackException("JackBasePosixMutex: could not init the mutex attribute")); 00129 res = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE); 00130 ThrowIf(res != 0, JackException("JackBasePosixMutex: could not settype the mutex")); 00131 res = pthread_mutex_init(&fMutex, &mutex_attr); 00132 ThrowIf(res != 0, JackException("JackBasePosixMutex: could not init the mutex")); 00133 pthread_mutexattr_destroy(&mutex_attr); 00134 } 00135 00136 virtual ~JackPosixMutex() 00137 { 00138 pthread_mutex_destroy(&fMutex); 00139 } 00140 00141 bool Lock() 00142 { 00143 int res = pthread_mutex_lock(&fMutex); 00144 if (res != 0) { 00145 jack_log("JackPosixMutex::Lock res = %d", res); 00146 } 00147 return (res == 0); 00148 } 00149 00150 bool Trylock() 00151 { 00152 return (pthread_mutex_trylock(&fMutex) == 0); 00153 } 00154 00155 bool Unlock() 00156 { 00157 int res = pthread_mutex_unlock(&fMutex); 00158 if (res != 0) { 00159 jack_log("JackPosixMutex::Unlock res = %d", res); 00160 } 00161 return (res == 0); 00162 } 00163 00164 }; 00165 00166 00167 } // namespace 00168 00169 #endif