Condition.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 __CONDITION__
00023 #define __CONDITION__
00024 
00025 #include "ErrSystem.hxx"
00026 #include "Lock.hxx"
00027 #include <pthread.h>
00028 
00029 namespace CLAM
00030 {
00031 
00032         struct xtime;
00033 
00034         class Condition
00035         {
00036         public:
00037                 Condition();
00038                 ~Condition();
00039 
00040                 void NotifyOne();
00041                 void NotifyAll();
00042                 
00043                 template <typename L>
00044                 void Wait( L& lock )
00045                 {
00046                         if ( !lock )
00047                                 throw LockError("Invalid lock");
00048                         
00049                         DoWait( lock.mMutex );
00050                 }
00051 
00052                 template <typename L, typename Pr >
00053                 void Wait( L& lock, Pr predicate )
00054                 {
00055                         if ( !lock )
00056                                 throw LockError("Invalid lock");
00057 
00058                         while ( !predicate() )
00059                                 DoWait( lock.mMutex );
00060                 }
00061 
00062                 template <typename L>
00063                 bool TimedWait( L& lock, const xtime& xt )
00064                 {
00065                         if (!lock )
00066                                 throw LockError();
00067 
00068                         return DoTimedWait( lock.mMutex, xt );
00069                 }
00070 
00071                 template <typename L, typename Pr>
00072                 bool TimedWait( L& lock, const xtime& xt, Pr predicate )
00073                 {
00074                         if ( !lock )
00075                                 throw LockError();
00076 
00077                         while( !predicate() )
00078                                 {
00079                                         if (!DoTimedWait( lock.mMutex, xt ) )
00080                                                 return false;
00081                                 }
00082 
00083                         return true;
00084                 }
00085 
00086         private:
00087                 
00088                 template <typename M>
00089                 void DoWait( M& mutex )
00090                 {
00091                         typedef typename Hidden::LockOps<M> LockOps;
00092                         typename LockOps::LockState state;
00093 
00094                         LockOps::Unlock( mutex, state );
00095                         DoWait( state.pmutex );
00096                         LockOps::Lock( mutex, state );
00097                 }
00098 
00099                 template <typename M>
00100                 bool DoTimedWait( M& mutex, const xtime& xt )
00101                 {
00102                         typedef typename Hidden::LockOps<M> LockOps;
00103                         typename LockOps::LockState state;
00104                         
00105                         LockOps::Unlock( mutex, state );
00106 
00107                         bool ret = false;
00108 
00109                         ret = DoTimedWait( xt, state.pmutex );
00110 
00111                         LockOps::Lock( mutex, state );
00112 
00113                         return ret;
00114                 }
00115 
00116                 void DoWait( pthread_mutex_t* pMutex );
00117 
00118                 bool DoTimedWait( const xtime& xt, pthread_mutex_t* pMutex);
00119 
00120                 pthread_cond_t mCondition;
00121         };
00122 
00123 } // end of namespace CLAM
00124 
00125 #endif // Condition.hxx
00126 
Generated by  doxygen 1.6.3