CondVarPosix.h

Go to the documentation of this file.
00001 /****************** <VPR heading BEGIN do not edit this line> *****************
00002  *
00003  * VR Juggler Portable Runtime
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Patrick Hartling, Kevin Meinert, Carolina Cruz-Neira
00007  *
00008  * -----------------------------------------------------------------
00009  * File:          $RCSfile$
00010  * Date modified: $Date: 2005-01-17 22:34:01 -0600 (Mon, 17 Jan 2005) $
00011  * Version:       $Revision: 16635 $
00012  * -----------------------------------------------------------------
00013  *
00014  ****************** <VPR heading END do not edit this line> ******************/
00015 
00016 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00017  *
00018  * VR Juggler is (C) Copyright 1998-2005 by Iowa State University
00019  *
00020  * Original Authors:
00021  *   Allen Bierbaum, Christopher Just,
00022  *   Patrick Hartling, Kevin Meinert,
00023  *   Carolina Cruz-Neira, Albert Baker
00024  *
00025  * This library is free software; you can redistribute it and/or
00026  * modify it under the terms of the GNU Library General Public
00027  * License as published by the Free Software Foundation; either
00028  * version 2 of the License, or (at your option) any later version.
00029  *
00030  * This library is distributed in the hope that it will be useful,
00031  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00032  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00033  * Library General Public License for more details.
00034  *
00035  * You should have received a copy of the GNU Library General Public
00036  * License along with this library; if not, write to the
00037  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00038  * Boston, MA 02111-1307, USA.
00039  *
00040  *************** <auto-copyright.pl END do not edit this line> ***************/
00041 
00049 #ifndef _VPR_COND_VAR_POSIX_H_
00050 #define _VPR_COND_VAR_POSIX_H_
00051 
00052 #include <vpr/vprConfig.h>
00053 #include <stdlib.h>
00054 #include <pthread.h>
00055 #include <vpr/md/POSIX/Sync/MutexPosix.h>
00056 #include <vpr/Util/Interval.h>
00057 
00058 
00059 namespace vpr
00060 {
00061 
00068 class CondVarPosix
00069 {
00070 public:
00082    CondVarPosix(MutexPosix* mutex = NULL)
00083    {
00084       // Initialize the condition variable.
00085       pthread_cond_init(&mCondVar, NULL);
00086 
00087       // If the caller did not specify a mutex variable to use with
00088       // the condition variable, use mDefaultMutex.
00089       if ( mutex == NULL )
00090       {
00091          mutex = &mDefaultMutex;
00092       }
00093 
00094       mCondMutex = mutex;
00095    }
00096 
00102    ~CondVarPosix()
00103    {
00104       pthread_cond_destroy(&mCondVar);
00105    }
00106 
00119    vpr::ReturnStatus wait(vpr::Interval timeToWait = vpr::Interval::NoTimeout);
00120 
00132    vpr::ReturnStatus signal()
00133    {
00134       // ASSERT:  We have been locked
00135       if ( pthread_cond_signal(&mCondVar) == 0 )
00136       {
00137          return vpr::ReturnStatus(vpr::ReturnStatus::Succeed);
00138       }
00139       else
00140       {
00141          return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00142       }
00143    }
00144 
00158    vpr::ReturnStatus broadcast()
00159    {
00160       // ASSERT:  We have been locked
00161 
00162       // If not locked ...
00163       if ( mCondMutex->test() == 0 )
00164       {
00165          std::cerr << "vpr::CondVarPosix::broadcast: Mutex was not locked when "
00166                    << "broadcast called!!!\n";
00167       }
00168 
00169       if ( pthread_cond_broadcast(&mCondVar) == 0 )
00170       {
00171          return vpr::ReturnStatus(vpr::ReturnStatus::Succeed);
00172       }
00173       else
00174       {
00175          return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00176       }
00177    }
00178 
00192    vpr::ReturnStatus acquire()
00193    {
00194       return mCondMutex->acquire();
00195    }
00196 
00210    vpr::ReturnStatus tryAcquire()
00211    {
00212       return mCondMutex->tryAcquire();
00213    }
00214 
00221    vpr::ReturnStatus release()
00222    {
00223       return mCondMutex->release();
00224    }
00225 
00239    void setMutex(vpr::MutexPosix* mutex)
00240    {
00241       // NOT exactly correct, but just make sure not to leave it locked
00242       mutex->release();
00243       mCondMutex = mutex;
00244    }
00245 
00247    int test()
00248    {
00249       return mCondMutex->test();
00250    }
00251 
00258    void dump() const
00259    {
00260       std::cerr << "------------- vpr::CondVarPosix::Dump ---------\n"
00261                 << "Not Implemented yet.\n";
00262    }
00263 
00264 private:
00265    pthread_cond_t mCondVar;      
00266    MutexPosix*    mCondMutex;    
00267    MutexPosix     mDefaultMutex; 
00269    // = Prevent assignment and initialization.
00270    void operator= (const CondVarPosix&) {;}
00271    CondVarPosix(const CondVarPosix&) {;}
00272 };
00273 
00274 } // End of vpr namespace
00275 
00276 
00277 #endif  /* _VPR_COND_POSIX_H_ */

Generated on Thu Jan 4 10:52:09 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1