Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Examples  

CondVarGeneric.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: CondVarGeneric.h,v $
00010  * Date modified: $Date: 2004/02/03 21:26:03 $
00011  * Version:       $Revision: 1.18 $
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-2003 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 
00042 #ifndef _VPR_CondVarGeneric_h_
00043 #define _VPR_CondVarGeneric_h_
00044 
00045 #include <vpr/vprConfig.h>
00046 
00047 #include <iostream>
00048 #include <iomanip>
00049 
00050 #ifdef VPR_OS_Win32
00051 #  include <process.h>
00052 #endif
00053 
00054 #include <sys/types.h>
00055 
00056 #ifdef HAVE_UNISTD_H
00057 #  include <unistd.h>
00058 #endif
00059 
00060 #include <vpr/Sync/Semaphore.h>
00061 #include <vpr/Sync/Mutex.h>
00062 #include <vpr/Util/Debug.h>
00063 #include <vpr/Util/Interval.h>
00064 
00065 
00066 namespace vpr
00067 {
00068 
00082 class VPR_CLASS_API CondVarGeneric
00083 {
00084 public:
00091    CondVarGeneric(Mutex* mutex = NULL)
00092       : mWaiters(0)
00093       , mCondMutex(NULL)
00094    {
00095       if ( mutex == NULL )
00096       {
00097          mutex = &mDefaultMutex;
00098       }
00099 
00100       mCondMutex = mutex;
00101 
00102       std::cerr << "------------------------------------\n"
00103                 << "  vpr::CondVarGeneric: DOES NOT WORK\n"
00104                 << "------------------------------------\n";
00105    }
00106 
00113    vpr::ReturnStatus wait(vpr::Interval time_to_wait = vpr::Interval::NoTimeout);
00114 
00120    vpr::ReturnStatus signal()
00121    {
00122       std::cerr << std::setw(5) << getpid() << "  Signal" << std::endl;
00123       // ASSERT:  We have been locked
00124       if ( mCondMutex->test() == 0 )    // Not locked
00125       {
00126          std::cerr << " vpr::CondVarGeneric::signal: Mutex was not locked when signal called!!!" << std::endl;
00127       }
00128 
00129       if ( mWaiters > 0 )
00130       {
00131          return mSema.release();
00132       }
00133       else
00134       {
00135          return vpr::ReturnStatus();
00136       }
00137    }
00138 
00143    vpr::ReturnStatus broadcast()
00144    {
00145       // ASSERT:  We have been locked
00146       if ( mCondMutex->test() == 0 )    // Not locked
00147       {
00148          std::cerr << " vpr::CondVarGeneric::broadcast: Mutex was not locked when broadcase called!!!" << std::endl;
00149       }
00150 
00151       for ( int i = mWaiters;i>0;i-- )
00152       {
00153          mSema.release();
00154       }
00155 
00156       return vpr::ReturnStatus();
00157    }
00158 
00160    vpr::ReturnStatus acquire()
00161    {
00162       return mCondMutex->acquire();
00163    }
00164 
00166    vpr::ReturnStatus tryAcquire()
00167    {
00168       return mCondMutex->tryAcquire();
00169    }
00170 
00172    vpr::ReturnStatus release()
00173    {
00174       return mCondMutex->release();
00175    }
00176 
00182    void setMutex(Mutex* mutex)
00183    {
00184       mutex->release();       // NOT exactly correct, but just make sure not to leave it locked
00185       mCondMutex = mutex;
00186    }
00187 
00189    int test()
00190    {
00191       return mCondMutex->test();
00192    }
00193 
00194    void dump() const
00195    {
00196       vprDEBUG_BEGIN(vprDBG_ALL,0)
00197          << "------------- vpr::CondVarGeneric::Dump ---------\n"
00198          << vprDEBUG_FLUSH;
00199       vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL) << "mWaiters: "
00200                                                 << mWaiters << std::endl
00201                                                 << vprDEBUG_FLUSH;
00202       mCondMutex->dump();
00203       vprDEBUG_END(vprDBG_ALL,0) << "-----------------------------------\n"
00204                                  << vprDEBUG_FLUSH;
00205    }
00206 
00207 private:
00208    // --- These make up the "condition variable" ---- ///
00209    Semaphore mSema;     
00210    long mWaiters;       
00212    Mutex* mCondMutex;   
00213    Mutex mDefaultMutex; 
00215    // = Prevent assignment and initialization.
00216    void operator= (const CondVarGeneric&)
00217    {
00218       ;
00219    }
00220 
00221    CondVarGeneric(const CondVarGeneric& c)
00222    {
00223       ;
00224    }
00225 };
00226 
00227 } // End of vpr namespace
00228 
00229 
00230 #endif

Generated on Sun May 2 14:43:01 2004 for VR Juggler Portable Runtime by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002