Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGBarrier.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 #include "OSGConfig.h"
00040 
00041 #include <stdlib.h>
00042 #include <stdio.h>
00043 
00044 
00045 #include <iostream>
00046 
00047 #include "OSGBarrier.h"
00048 #include "OSGBaseFunctions.h"
00049 
00050 #include "OSGThreadManager.h"
00051 
00052 OSG_USING_NAMESPACE
00053 
00054 //---------------------------------------------------------------------------
00055 //  Class
00056 //---------------------------------------------------------------------------
00057 
00058 
00059 /*--------------------------- Constructors --------------------------------*/
00060 
00061 BarrierCommonBase::BarrierCommonBase(const Char8  *szName,
00062                                            UInt32  uiId  ):
00063      Inherited   (szName),
00064     _uiBarrierId (uiId  ),
00065     _uiNumWaitFor(     1)
00066 {
00067 }
00068 
00069 /*---------------------------- Destructor ---------------------------------*/
00070 
00071 BarrierCommonBase::~BarrierCommonBase(void)
00072 {
00073 }
00074 
00075 
00076 
00077 
00078 #if defined (OSG_USE_PTHREADS)
00079 
00080 //---------------------------------------------------------------------------
00081 //  Class
00082 //---------------------------------------------------------------------------
00083 
00084 /*--------------------------- Constructors --------------------------------*/
00085 
00086 PThreadBarrierBase::PThreadBarrierBase(const Char8  *szName,
00087                                              UInt32  uiId  ) :
00088      Inherited        (szName, 
00089                        uiId  ),
00090 
00091     _pLockOne         (      ),
00092     _uiCount          (0     ),
00093     _uiCurrentCond    (0     )
00094 {
00095 }
00096 
00097 /*---------------------------- Destructor ---------------------------------*/
00098 
00099 PThreadBarrierBase::~PThreadBarrierBase(void)
00100 {
00101 }
00102 
00103 /*--------------------------- Construction --------------------------------*/
00104 
00105 bool PThreadBarrierBase::init(void)
00106 {
00107     pthread_cond_init (&(_pWakeupCondition[0]), NULL);
00108     pthread_cond_init (&(_pWakeupCondition[1]), NULL);
00109     pthread_mutex_init(&(_pLockOne),            NULL);
00110 
00111     _uiCount       = 0;
00112     _uiCurrentCond = 0;
00113 
00114     return true;
00115 }
00116 
00117 /*---------------------------- Destruction --------------------------------*/
00118 
00119 void PThreadBarrierBase::shutdown(void)
00120 {
00121     pthread_cond_destroy (&(_pWakeupCondition[0]));
00122     pthread_cond_destroy (&(_pWakeupCondition[1]));
00123     pthread_mutex_destroy(&(_pLockOne));
00124 }
00125 
00126 #endif /* OSG_USE_PTHREADS */
00127 
00128 
00129 
00130 #if defined (OSG_USE_SPROC)
00131 
00132 //---------------------------------------------------------------------------
00133 //  Class
00134 //---------------------------------------------------------------------------
00135 
00136 /*--------------------------- Constructors --------------------------------*/
00137 
00138 SprocBarrierBase::SprocBarrierBase(const Char8  *szName,
00139                                          UInt32  uiId  ) :
00140      Inherited(szName, uiId),
00141 
00142     _pBarrier (NULL)
00143 {
00144 }
00145 
00146 /*---------------------------- Destructor ---------------------------------*/
00147 
00148 SprocBarrierBase::~SprocBarrierBase(void)
00149 {
00150 }
00151 
00152 /*--------------------------- Construction --------------------------------*/
00153 
00154 bool SprocBarrierBase::init(void)
00155 {
00156     ThreadManager *pThreadManager = ThreadManager::the();
00157 
00158     if(pThreadManager == NULL)
00159         return false;
00160 
00161     if(pThreadManager->getArena() == NULL)
00162         return false;
00163 
00164     _pBarrier = new_barrier(pThreadManager->getArena());
00165 
00166     if(_pBarrier == NULL)
00167         return false;
00168 
00169     init_barrier(_pBarrier);
00170 
00171     return true;
00172 }
00173 
00174 /*---------------------------- Destruction --------------------------------*/
00175 
00176 void SprocBarrierBase::shutdown(void)
00177 {
00178     if(_pBarrier != NULL)
00179         free_barrier(_pBarrier);
00180 }
00181 
00182 
00183 
00184 #endif /* OSG_USE_SPROC */
00185 
00186 
00187 
00188 #if defined (OSG_USE_WINTHREADS)
00189 
00190 //---------------------------------------------------------------------------
00191 //  Class
00192 //---------------------------------------------------------------------------
00193 
00194 /*--------------------------- Constructors --------------------------------*/
00195 
00196 WinThreadBarrierBase::WinThreadBarrierBase(const Char8  *szName,
00197                                                  UInt32  uiId  ) :
00198      Inherited   (szName, uiId),
00199 
00200     _pMutex1     (NULL),
00201     _pBarrierSema(NULL),
00202     _uiNumWaiters(0   )
00203 
00204 {
00205 }
00206 
00207 /*---------------------------- Destructor ---------------------------------*/
00208 
00209 WinThreadBarrierBase::~WinThreadBarrierBase(void)
00210 {
00211 }
00212 
00213 /*--------------------------- Construction --------------------------------*/
00214 
00215 bool WinThreadBarrierBase::init(void)
00216 {
00217     Char8 *pTmp;
00218 
00219     pTmp = new Char8[strlen(_szName) + 5];
00220 
00221     sprintf(pTmp, "%sM1", _szName);
00222 
00223     _pMutex1 = CreateMutex(NULL,   // no security attributes
00224                            FALSE,  // initially not owned
00225                            pTmp);  // name of mutex
00226 
00227     if(_pMutex1 == NULL)
00228     {
00229         fprintf(stderr, "Create mutex1 failed\n");
00230         return false;
00231     }
00232 
00233     sprintf(pTmp, "%sS", _szName);
00234 
00235     _pBarrierSema = CreateSemaphore(NULL, 0, 42, pTmp);
00236 
00237     if(_pBarrierSema == NULL)
00238     {
00239         CloseHandle(_pMutex1);
00240 
00241         fprintf(stderr, "Create semaphore failed\n");
00242         return false;
00243     }
00244 
00245     delete [] pTmp;
00246 
00247     return true;
00248 }
00249 
00250 /*---------------------------- Destruction --------------------------------*/
00251 
00252 void WinThreadBarrierBase::shutdown(void)
00253 {
00254     if(_pMutex1 != NULL)
00255         CloseHandle(_pMutex1);
00256 
00257     if(_pBarrierSema != NULL)
00258         CloseHandle(_pBarrierSema);
00259 }
00260 
00261 #endif /* OSG_USE_WINTHREADS */
00262 
00263 
00264 
00265 
00266 //---------------------------------------------------------------------------
00267 //  Class
00268 //---------------------------------------------------------------------------
00269 
00270 MPBarrierType Barrier::_type("OSGBarrier", "OSGMPBase", &Barrier::create);
00271 
00272 
00273 /*--------------------------- Constructors --------------------------------*/
00274 
00275 Barrier::Barrier(const Char8  *szName,
00276                        UInt32  uiId  ) :
00277     Inherited(szName, uiId)
00278 {
00279 }
00280 
00281 /*---------------------------- Destructor ---------------------------------*/
00282 
00283 Barrier::~Barrier(void)
00284 {
00285     ThreadManager::the()->removeBarrier(this);
00286 
00287     shutdown();
00288 }
00289 
00290 /*-------------------------------- Get ------------------------------------*/
00291 
00292 Barrier *Barrier::get(const Char8 *szName)
00293 {
00294     return ThreadManager::the()->getBarrier(szName, "OSGBarrier");
00295 }
00296 
00297 Barrier *Barrier::find(const Char8 *szName)
00298 {
00299     return ThreadManager::the()->findBarrier(szName);
00300 }
00301 
00302 
00303 /*------------------------------ Create -----------------------------------*/
00304 
00305 Barrier *Barrier::create (const Char8  *szName,
00306                                 UInt32  uiId  )
00307 {
00308     Barrier *returnValue = NULL;
00309 
00310     returnValue = new Barrier(szName, uiId);
00311 
00312     if(returnValue->init() == false)
00313     {
00314         delete returnValue;
00315         returnValue = NULL;
00316    }
00317 
00318     return returnValue;
00319 }
00320 
00321 /*-------------------------------------------------------------------------*/
00322 /*                              cvs id's                                   */
00323 
00324 #ifdef __sgi
00325 #pragma set woff 1174
00326 #endif
00327 
00328 #ifdef OSG_LINUX_ICC
00329 #pragma warning( disable : 177 )
00330 #endif
00331 
00332 namespace
00333 {
00334     static Char8 cvsid_cpp[] = "@(#)$Id: $";
00335     static Char8 cvsid_hpp[] = OSGBARRIER_HEADER_CVSID;
00336 }
00337 

Generated on Thu Aug 25 04:01:09 2005 for OpenSG by  doxygen 1.4.3