Condition.cxx

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 #include "Condition.hxx"
00023 #include "Thread.hxx"
00024 #include "ErrSystem.hxx"
00025 #include "Assert.hxx"
00026 #include "xtime.hxx"
00027 #include <errno.h>
00028 #include <cstdio>
00029 #include <sstream>
00030 
00031 namespace CLAM
00032 {
00033 
00034         Condition::Condition()
00035         {
00036                 int res  = 0;
00037 
00038                 res = pthread_cond_init( &mCondition, 0 );
00039 
00040                 CLAM_ASSERT( res == 0, "pthread_cond_init call failed" );
00041         }
00042 
00043         Condition::~Condition()
00044         {
00045                 int res = 0;
00046 
00047                 res = pthread_cond_destroy( &mCondition );
00048 
00049                 CLAM_ASSERT( res==0, "pthread_cond_destroy call failed" );
00050         }
00051 
00052         void Condition::NotifyAll()
00053         {
00054                 int res = 0;
00055 
00056                 res = pthread_cond_broadcast( &mCondition );
00057 
00058                 CLAM_ASSERT( res==0, "pthread_cond_broadcast call failed!" );
00059         }
00060 
00061         void Condition::NotifyOne()
00062         {
00063                 int res = 0;
00064 
00065                 res = pthread_cond_signal( &mCondition );
00066 
00067                 CLAM_ASSERT( res == 0, "pthread_cond_signal call failed" );
00068         }
00069 
00070         void Condition::DoWait( pthread_mutex_t* pmutex )
00071         {
00072                 int res = 0;
00073 
00074                 res = pthread_cond_wait( &mCondition, pmutex );
00075 
00076                 CLAM_ASSERT( res == 0, "pthread_cond_wait call failed" );
00077         }
00078 
00079         bool Condition::DoTimedWait( const xtime& xt, pthread_mutex_t* pmutex )
00080         {
00081                 timespec ts;
00082 
00083                 to_timespec( xt, ts );
00084 
00085                 int res = 0;
00086 
00087                 res = pthread_cond_timedwait( &mCondition, pmutex, &ts );
00088 
00089                 std::ostringstream os;
00090                 os << "Something strange has happened. PThread returned from timed wait with result code " << res << std::flush;
00091                 CLAM_ASSERT( res == 0 || res == ETIMEDOUT , os.str().c_str() );
00092 
00093                 return res != ETIMEDOUT;
00094         }
00095 
00096 } // end of namespace CLAM
00097 
Generated by  doxygen 1.6.3