JackMutex.h

00001  /*
00002 
00003   Copyright (C) 2006 Grame
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009 
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014 
00015   You should have received a copy of the GNU Lesser General Public
00016   License along with this library; if not, write to the Free Software
00017   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019   Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
00020   grame@grame.fr
00021 
00022 */
00023 
00024 #ifndef __JackMutex__
00025 #define __JackMutex__
00026 
00027 #ifdef WIN32
00028 #include <windows.h>
00029 #else
00030 #include <pthread.h>
00031 #endif
00032 
00033 #include<assert.h>
00034 
00035 namespace Jack 
00036 {
00037 
00038 class JackMutex 
00039 {
00040         
00041         private:
00042         
00043         #ifdef WIN32
00044                 HANDLE fMutex;
00045         #else
00046                 pthread_mutex_t fMutex;  
00047         #endif
00048 
00049         public:
00050 
00051         #ifdef WIN32
00052 
00053                 JackMutex()
00054                 {               
00055                         fMutex = CreateMutex(0, FALSE, 0);
00056                 }
00057                 virtual ~JackMutex()
00058                 {
00059                         CloseHandle(fMutex);
00060                 }
00061                 
00062                 void Lock()
00063                 {       
00064                          DWORD dwWaitResult = WaitForSingleObject(fMutex, INFINITE);
00065                 }
00066 
00067                 void Unlock()
00068                 {       
00069                         ReleaseMutex(fMutex);
00070                 }
00071 
00072         #else
00073 
00074                 JackMutex()
00075                 {
00076                         // Use recursive mutex
00077                         pthread_mutexattr_t mutex_attr;
00078                         assert(pthread_mutexattr_init(&mutex_attr) == 0);
00079                         assert(pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE) == 0);
00080                         assert(pthread_mutex_init(&fMutex, &mutex_attr) == 0);
00081                 }
00082                 virtual ~JackMutex()
00083                 {
00084                         pthread_mutex_destroy(&fMutex);
00085                 }
00086                 
00087                 void Lock()
00088                 {
00089                         pthread_mutex_lock(&fMutex);
00090                 }
00091 
00092                 void Unlock()
00093                 {
00094                         pthread_mutex_unlock(&fMutex);
00095                 }
00096 
00097         #endif
00098 };
00099 
00100 class JackLockAble
00101 {
00102 
00103         private:
00104 
00105                 JackMutex fMutex;
00106 
00107         public:
00108 
00109                 JackLockAble() {}
00110                 virtual ~JackLockAble() {}
00111 
00112                 void Lock()
00113                 {       
00114                         fMutex.Lock();
00115                 }
00116 
00117                 void Unlock()
00118                 {       
00119                         fMutex.Unlock();
00120                 }
00121                 
00122 };
00123 
00124 class JackLock
00125 {
00126         private:
00127         
00128                 JackLockAble* fObj;
00129                                 
00130         public:
00131         
00132                 JackLock(JackLockAble* obj):fObj(obj)
00133                 {       
00134                         fObj->Lock();
00135                 }
00136                 
00137                 JackLock(const JackLockAble* obj):fObj((JackLockAble*)obj)
00138                 {       
00139                         fObj->Lock();
00140                 }       
00141                 
00142                 virtual ~JackLock()
00143                 {
00144                         fObj->Unlock();
00145                 }
00146 };
00147 
00148 
00149 } // namespace
00150 
00151 #endif

Generated on Thu Feb 14 11:16:02 2008 for Jackdmp by  doxygen 1.5.1