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 #include <pthread.h> 00026 #include "Lock.hxx" 00027 #include <errno.h> 00028 00029 // http://www.boost.org/libs/thread/doc/mutex_concept.html 00030 00031 namespace CLAM 00032 { 00033 00034 struct xtime; 00044 class Mutex 00045 { 00046 public: 00047 00048 friend class Hidden::LockOps<Mutex>; 00049 friend class Hidden::ScopedLock<Mutex>; 00050 00051 typedef Hidden::ScopedLock<Mutex> ScopedLock; 00052 00053 Mutex(); 00054 00055 ~Mutex(); 00056 00057 private: 00058 00059 struct ConditionVar 00060 { 00061 pthread_mutex_t* pmutex; 00062 }; 00063 00064 void DoLock(); 00065 00066 void DoUnlock(); 00067 00068 void DoLock( ConditionVar& state ); 00069 00070 void DoUnlock( ConditionVar& state ); 00071 00072 pthread_mutex_t mMutex; 00073 00074 }; 00075 00076 class TryMutex 00077 { 00078 public: 00079 friend class Hidden::LockOps<TryMutex>; 00080 00081 typedef Hidden::ScopedLock<TryMutex> ScopedLock; 00082 typedef Hidden::ScopedTryLock<TryMutex> ScopedTryLock; 00083 00084 TryMutex(); 00085 ~TryMutex(); 00086 00087 private: 00088 struct ConditionVar 00089 { 00090 pthread_mutex_t* pmutex; 00091 }; 00092 00093 void DoLock(); 00094 00095 bool DoTryLock(); 00096 00097 void DoUnlock(); 00098 00099 void DoLock( ConditionVar& state ); 00100 00101 void DoUnlock( ConditionVar& state ); 00102 00103 pthread_mutex_t mMutex; 00104 00105 }; 00106 00107 class TimedMutex 00108 { 00109 public: 00110 friend class Hidden::LockOps<TimedMutex>; 00111 00112 typedef Hidden::ScopedLock<TimedMutex> ScopedLock; 00113 typedef Hidden::ScopedTryLock<TimedMutex> ScopedTryLock; 00114 typedef Hidden::ScopedTimedLock<TimedMutex> ScopedTimedLock; 00115 00116 TimedMutex(); 00117 ~TimedMutex(); 00118 00119 private: 00120 struct ConditionVar 00121 { 00122 pthread_mutex_t* pmutex; 00123 }; 00124 00125 void DoLock(); 00126 00127 bool DoTryLock(); 00128 00129 bool DoTimedLock( const xtime& xt ); 00130 00131 void DoUnlock(); 00132 00133 void DoLock( ConditionVar& state ); 00134 00135 void DoUnlock( ConditionVar& state ); 00136 00137 pthread_mutex_t mMutex; 00138 00139 pthread_cond_t mCondition; 00140 00141 bool mLocked; 00142 00143 }; 00144 00145 } // end of namespace CLAM 00146 00147 #endif // Mutex.hxx 00148