Mutex.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #ifndef __MUTEX__
00023 #define __MUTEX__
00024 
00025 #if USE_PTHREADS != 1
00026 #error USE_PTHREADS was not set to 1 in your settings.cfg file, but you are including files that require this. Please fix your settings.cfg
00027 #endif
00028 
00029 #include <pthread.h>
00030 #include "Lock.hxx"
00031 #include <errno.h>
00032 
00033 // http://www.boost.org/libs/thread/doc/mutex_concept.html
00034 
00035 namespace CLAM
00036 {
00037 
00038 struct xtime;
00048 class Mutex
00049 {
00050 public:
00051 
00052         friend class Hidden::LockOps<Mutex>;
00053         friend class Hidden::ScopedLock<Mutex>;
00054 
00055         typedef Hidden::ScopedLock<Mutex> ScopedLock;
00056 
00057         Mutex();
00058 
00059         ~Mutex();
00060 
00061 private:
00062         
00063         struct ConditionVar
00064         {
00065                 pthread_mutex_t* pmutex;
00066         };
00067 
00068         void DoLock();
00069         
00070         void DoUnlock();
00071         
00072         void DoLock( ConditionVar& state );
00073 
00074         void DoUnlock( ConditionVar& state );
00075 
00076         pthread_mutex_t mMutex;
00077 
00078 };
00079 
00080 class TryMutex
00081 {
00082 public:
00083         friend class Hidden::LockOps<TryMutex>;
00084 
00085         typedef Hidden::ScopedLock<TryMutex> ScopedLock;
00086         typedef Hidden::ScopedTryLock<TryMutex> ScopedTryLock;
00087 
00088         TryMutex();
00089         ~TryMutex();
00090 
00091 private:        
00092         struct ConditionVar
00093         {
00094                 pthread_mutex_t* pmutex;
00095         };
00096 
00097         void DoLock();
00098 
00099         bool DoTryLock();
00100 
00101         void DoUnlock();
00102         
00103         void DoLock( ConditionVar& state );
00104 
00105         void DoUnlock( ConditionVar& state );
00106 
00107         pthread_mutex_t mMutex;
00108 
00109 };
00110 
00111 class TimedMutex
00112 {
00113 public:
00114         friend class Hidden::LockOps<TimedMutex>;
00115 
00116         typedef Hidden::ScopedLock<TimedMutex> ScopedLock;
00117         typedef Hidden::ScopedTryLock<TimedMutex> ScopedTryLock;
00118         typedef Hidden::ScopedTimedLock<TimedMutex> ScopedTimedLock;
00119 
00120         TimedMutex();
00121         ~TimedMutex();
00122 
00123 private:
00124         struct ConditionVar
00125         {
00126                 pthread_mutex_t* pmutex;
00127         };
00128 
00129         void DoLock();
00130 
00131         bool DoTryLock();
00132 
00133         bool DoTimedLock( const xtime& xt );
00134 
00135         void DoUnlock();
00136         
00137         void DoLock( ConditionVar& state );
00138 
00139         void DoUnlock( ConditionVar& state );
00140 
00141         pthread_mutex_t mMutex;
00142         
00143         pthread_cond_t  mCondition;
00144 
00145         bool mLocked;
00146 
00147 };
00148 
00149 } // end of namespace CLAM
00150 
00151 #endif // Mutex.hxx
00152 

Generated on Tue Aug 12 22:33:43 2008 for CLAM by  doxygen 1.5.5