Jack2
1.9.10
|
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 #include "JackPosixMutex.h" 00023 #include "JackError.h" 00024 00025 namespace Jack 00026 { 00027 00028 JackBasePosixMutex::JackBasePosixMutex(const char* name) 00029 :fOwner(0) 00030 { 00031 int res = pthread_mutex_init(&fMutex, NULL); 00032 ThrowIf(res != 0, JackException("JackBasePosixMutex: could not init the mutex")); 00033 } 00034 00035 JackBasePosixMutex::~JackBasePosixMutex() 00036 { 00037 pthread_mutex_destroy(&fMutex); 00038 } 00039 00040 bool JackBasePosixMutex::Lock() 00041 { 00042 pthread_t current_thread = pthread_self(); 00043 00044 if (!pthread_equal(current_thread, fOwner)) { 00045 int res = pthread_mutex_lock(&fMutex); 00046 if (res == 0) { 00047 fOwner = current_thread; 00048 return true; 00049 } else { 00050 jack_error("JackBasePosixMutex::Lock res = %d", res); 00051 return false; 00052 } 00053 } else { 00054 return false; 00055 } 00056 } 00057 00058 bool JackBasePosixMutex::Trylock() 00059 { 00060 pthread_t current_thread = pthread_self(); 00061 00062 if (!pthread_equal(current_thread, fOwner)) { 00063 int res = pthread_mutex_trylock(&fMutex); 00064 if (res == 0) { 00065 fOwner = current_thread; 00066 return true; 00067 } else { 00068 return false; 00069 } 00070 } else { 00071 return false; 00072 } 00073 } 00074 00075 bool JackBasePosixMutex::Unlock() 00076 { 00077 if (pthread_equal(pthread_self(), fOwner)) { 00078 fOwner = 0; 00079 int res = pthread_mutex_unlock(&fMutex); 00080 if (res == 0) { 00081 return true; 00082 } else { 00083 jack_error("JackBasePosixMutex::Unlock res = %d", res); 00084 return false; 00085 } 00086 } else { 00087 return false; 00088 } 00089 } 00090 00091 JackPosixMutex::JackPosixMutex(const char* name) 00092 { 00093 // Use recursive mutex 00094 pthread_mutexattr_t mutex_attr; 00095 int res; 00096 res = pthread_mutexattr_init(&mutex_attr); 00097 ThrowIf(res != 0, JackException("JackBasePosixMutex: could not init the mutex attribute")); 00098 res = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE); 00099 ThrowIf(res != 0, JackException("JackBasePosixMutex: could not settype the mutex")); 00100 res = pthread_mutex_init(&fMutex, &mutex_attr); 00101 ThrowIf(res != 0, JackException("JackBasePosixMutex: could not init the mutex")); 00102 pthread_mutexattr_destroy(&mutex_attr); 00103 } 00104 00105 JackPosixMutex::~JackPosixMutex() 00106 { 00107 pthread_mutex_destroy(&fMutex); 00108 } 00109 00110 bool JackPosixMutex::Lock() 00111 { 00112 int res = pthread_mutex_lock(&fMutex); 00113 if (res != 0) { 00114 jack_log("JackPosixMutex::Lock res = %d", res); 00115 } 00116 return (res == 0); 00117 } 00118 00119 bool JackPosixMutex::Trylock() 00120 { 00121 return (pthread_mutex_trylock(&fMutex) == 0); 00122 } 00123 00124 bool JackPosixMutex::Unlock() 00125 { 00126 int res = pthread_mutex_unlock(&fMutex); 00127 if (res != 0) { 00128 jack_log("JackPosixMutex::Unlock res = %d", res); 00129 } 00130 return (res == 0); 00131 } 00132 00133 00134 } // namespace