SemaphoreSGI.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 
00042 #ifndef _VPR_SemaphoreSGI_h_
00043 #define _VPR_SemaphoreSGI_h_
00044 
00045 #include <vpr/vprConfig.h>
00046 #include <ulocks.h>
00047 
00048 #include <vpr/md/SPROC/SharedMem/MemPool.h>
00049 #include <vpr/Util/ReturnStatus.h>
00050 
00051 
00052 namespace vpr
00053 {
00054 
00063 class SemaphoreSGI
00064 {
00065 public:
00075    SemaphoreSGI(int initialValue = 1)
00076    {
00077       // BUG:
00078       if (semaphorePool == NULL)
00079       {
00080          semaphorePool = new MemPoolSGI(65536, 32,
00081                                         "/var/tmp/memSemaphorePoolSGIXXXXXX");
00082          attachedCounter = static_cast<int*>(semaphorePool->allocate(sizeof(int)));
00083          *attachedCounter = 0;
00084       }
00085       *attachedCounter = *attachedCounter + 1;      // Track how many semaphores are allocated
00086 
00087 //      DebugLock.acquire();
00088 //      vprDEBUG << " vpr::SemaphoreSGI::SemaphoreSGI: attachedCounter: "
00089 //               << *attachedCounter << endl << vprDEBUG_FLUSH;
00090 //      DebugLock.release();
00091 
00092       // ----- Allocate the semaphore ----- //
00093       sema = usnewsema(semaphorePool->getArena(), initialValue);
00094    }
00095 
00096    ~SemaphoreSGI()
00097    {
00098       // ---- Delete the semaphore --- //
00099       usfreesema(sema, semaphorePool->getArena());
00100 
00101       // ---- Deal with the pool --- //
00102       *attachedCounter = *attachedCounter - 1;     // Track how many Semaphore are allocated
00103 
00104 //      DebugLock.acquire();
00105 //      vprDEBUG << "vpr::SemaphoreSGI::~SemaphoreSGI: attachedCounter: "
00106 //               << *attachedCounter << endl << vprDEBUG_FLUSH;
00107 //      DebugLock.release();
00108 
00109       if (*attachedCounter == 0)
00110       {
00111          semaphorePool->deallocate(attachedCounter);
00112          attachedCounter = NULL;
00113          delete semaphorePool;
00114          semaphorePool = NULL;
00115       }
00116    }
00117 
00129    vpr::ReturnStatus acquire() const
00130    {
00131       vpr::ReturnStatus status;
00132 
00133       if ( uspsema(sema) < 0 )
00134       {
00135          std::cerr << "vpr::SemphoreSGI::ERROR:" << std::endl;
00136          status.setCode(vpr::ReturnStatus::Fail);
00137       }
00138 
00139       return status;
00140    }
00141 
00155    vpr::ReturnStatus acquireRead() const
00156    {
00157       return this->acquire();     // No special "read" semaphore -- For now
00158    }
00159 
00173    vpr::ReturnStatus acquireWrite() const
00174    {
00175       return this->acquire();     // No special "write" semaphore -- For now
00176    }
00177 
00190    vpr::ReturnStatus tryAcquire() const
00191    {
00192       if ( uscpsema(sema) == 1 )
00193       {
00194          return vpr::ReturnStatus();
00195       }
00196       else
00197       {
00198          return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00199       }
00200    }
00201 
00214    vpr::ReturnStatus tryAcquireRead() const
00215    {
00216       return this->tryAcquire();
00217    }
00218 
00231    vpr::ReturnStatus tryAcquireWrite () const
00232    {
00233       return this->tryAcquire();
00234    }
00235 
00247    vpr::ReturnStatus release() const
00248    {
00249       vpr::ReturnStatus status;
00250 
00251       if( usvsema(sema) < 0 )
00252       {
00253          std::cerr << "vpr::SemaphoreSGI::ERROR:" << std::endl;
00254          status.setCode(vpr::ReturnStatus::Fail);
00255       }
00256 
00257       return status;
00258    }
00259 
00274    vpr::ReturnStatus reset(int val)
00275    {
00276       if ( usinitsema(sema, val) == 0 )
00277       {
00278          return vpr::ReturnStatus();
00279       }
00280       else
00281       {
00282          return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00283       }
00284    }
00285 
00297    void dump(FILE* dest = stderr,
00298              const char* message = "\n------ Semaphore Status -----\n") const
00299    {
00300       usdumpsema(sema, dest, message);
00301    }
00302 
00303 protected:
00304    usema_t* sema;
00305 
00306    // = Prevent assignment and initialization.
00307    void operator= (const SemaphoreSGI &) {;}
00308    SemaphoreSGI (const SemaphoreSGI &) {}
00309 
00310    // Problem here.  Fork will not like these.
00311    static MemPoolSGI* semaphorePool;
00312    static int* attachedCounter;
00313 };
00314 
00315 } // End of vpr namespace
00316 
00317 
00318 #endif /* _VPR_SemaphoreSGI_h_ */

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