Jack2 1.9.6

JackPosixMutex.h

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 <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             // Use recursive mutex
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 } // namespace
00131 
00132 #endif