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

OSGThreadManager.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 <stdlib.h>
00040 #include <stdio.h>
00041 #include <errno.h>
00042 
00043 #include "OSGConfig.h"
00044 
00045 #include <iostream>
00046 
00047 #include "OSGThreadManager.h"
00048 
00049 OSG_USING_NAMESPACE
00050 
00051 ThreadManager *ThreadManager::_pThreadManager      = NULL;
00052 BaseThread    *ThreadManager::_pAppThread          = NULL;
00053 
00054 bool           ThreadManager::_bShutdownInProgress = false;
00055 UInt32         ThreadManager::_uiNumAspects        = OSG_NUM_ASPECTS;
00056 
00057 Char8         *ThreadManager::_szAppThreadType     = NULL;
00058 
00059 #ifdef OSG_RUNTIME_NUM_ASPECTS 
00060 bool           ThreadManager::_bNumAspectSet       = false;
00061 #endif
00062 
00063 
00064 /*-------------------------- Get / Set ------------------------------------*/
00065 
00066 void ThreadManager::setAppThreadType(const Char8 *szAppThreadType)
00067 {
00068     stringDup(szAppThreadType, _szAppThreadType);
00069 }
00070 
00071 
00072 ThreadManager *ThreadManager::the(void)
00073 {
00074     if(_pThreadManager == NULL)
00075         _pThreadManager = new ThreadManager();
00076 
00077     return _pThreadManager;
00078 }
00079 
00080 
00081 BaseThread *ThreadManager::getAppThread(void)
00082 {
00083     return _pAppThread;
00084 }
00085 
00086 
00087 void ThreadManager::setNumAspects(UInt32 OSG_CHECK_ARG(uiNumAspects))
00088 {
00089 #if defined(OSG_RUNTIME_NUM_ASPECTS)    
00090     if(_bNumAspectSet == false && uiNumAspects > 1)
00091     {
00092 #if defined(OSG_ASPECT_USE_PTHREADSELF) || \
00093     defined(OSG_ASPECT_USE_CUSTOMSELF)
00094         UInt32  i;
00095         UInt16 *pAspect = new UInt16[uiNumAspects];
00096 
00097         for(i = 0; i < _uiNumAspects; i++)
00098             pAspect[i] = Thread::_pAspects[i];
00099 
00100         for(i = _uiNumAspects; i < uiNumAspects; i++)
00101             Thread::_pAspects[i] = 0;
00102 
00103         delete [] Thread::_pAspects;
00104 
00105         Thread::_pAspectsA = pAspect;
00106 #endif
00107         _uiNumAspects  = uiNumAspects;
00108         _bNumAspectSet = true;        
00109     }    
00110 #endif
00111 }
00112 
00113 
00114 UInt32 ThreadManager::getNumAspects(void)
00115 {
00116     return _uiNumAspects;
00117 }
00118 
00119 /*-------------------- Create Threading Elements --------------------------*/
00120 
00121 BaseThread *ThreadManager::getThread(const Char8 *szName,
00122                                      const Char8 *szTypeName)
00123 {
00124     BaseThread *returnValue = NULL;
00125 
00126     _storePLock->aquire();
00127 
00128     returnValue = _sThreadStore.getMPField(szName, szTypeName);
00129 
00130     _storePLock->release();
00131 
00132     return returnValue;
00133 }
00134 
00135 Barrier *ThreadManager::getBarrier(const Char8 *szName,
00136                                    const Char8 *szTypeName)
00137 {
00138     Barrier *returnValue = NULL;
00139 
00140     _storePLock->aquire();
00141 
00142     returnValue = _sBarrierStore.getMPField(szName, szTypeName);
00143 
00144     _storePLock->release();
00145 
00146     return returnValue;
00147 }
00148 
00149 Lock *ThreadManager::getLock(const Char8 *szName,
00150                              const Char8 *szTypeName)
00151 {
00152     Lock *returnValue = NULL;
00153 
00154     _storePLock->aquire();
00155 
00156     returnValue = _sLockStore.getMPField(szName, szTypeName);
00157 
00158     _storePLock->release();
00159 
00160     return returnValue;
00161 }
00162 
00163 LockPool *ThreadManager::getLockPool(const Char8 *szName,
00164                                      const Char8 *szTypeName)
00165 {
00166     LockPool *returnValue = NULL;
00167 
00168     _storePLock->aquire();
00169 
00170     returnValue = _sLockPoolStore.getMPField(szName, szTypeName);
00171 
00172     _storePLock->release();
00173 
00174     return returnValue;
00175 }
00176 
00177 BaseThread  *ThreadManager::findThread(const Char8 *szName)
00178 {
00179     BaseThread *returnValue = NULL;
00180 
00181     _storePLock->aquire();
00182     
00183     returnValue = _sThreadStore.findMPField(szName);
00184 
00185     _storePLock->release();
00186 
00187     return returnValue;
00188 }
00189 
00190 Barrier *ThreadManager::findBarrier(const Char8 *szName)
00191 {
00192     Barrier *returnValue = NULL;
00193 
00194     _storePLock->aquire();
00195     
00196     returnValue = _sBarrierStore.findMPField(szName);
00197 
00198     _storePLock->release();
00199 
00200     return returnValue;
00201 }
00202 
00203 Lock *ThreadManager::findLock(const Char8 *szName)
00204 {
00205     Lock *returnValue = NULL;
00206 
00207     _storePLock->aquire();
00208     
00209     returnValue = _sLockStore.findMPField(szName);
00210 
00211     _storePLock->release();
00212 
00213     return returnValue;
00214 }
00215 
00216 LockPool *ThreadManager::findLockPool(const Char8 *szName)
00217 {
00218     LockPool *returnValue = NULL;
00219 
00220     _storePLock->aquire();
00221     
00222     returnValue = _sLockPoolStore.findMPField(szName);
00223 
00224     _storePLock->release();
00225 
00226     return returnValue;
00227 }
00228 
00229 /*------------------------------- Get -------------------------------------*/
00230 
00231 #if defined(OSG_USE_SPROC)
00232 usptr_t *ThreadManager::getArena(void)
00233 {
00234     return _pArena;
00235 }
00236 #endif
00237 
00238 /*------------------------------ Helper -----------------------------------*/
00239 
00240 bool ThreadManager::initialize(void)
00241 {
00242     return the()->init();
00243 }
00244 
00245 bool ThreadManager::terminate (void)
00246 {
00247     return the()->shutdown();
00248 }
00249 
00250 void ThreadManager::removeThread(BaseThread *pThread)
00251 {
00252     if(_bShutdownInProgress == true)
00253         return;
00254 
00255     _storePLock->aquire();
00256 
00257     _sThreadStore.removeMPField(pThread);
00258     
00259     _storePLock->release();
00260 }
00261 
00262 void ThreadManager::removeBarrier(Barrier *pBarrier)
00263 {
00264     if(_bShutdownInProgress == true)
00265         return;
00266 
00267     _storePLock->aquire();
00268 
00269     _sBarrierStore.removeMPField(pBarrier);
00270     
00271     _storePLock->release();
00272 }
00273 
00274 void ThreadManager::removeLock(Lock *pLock)
00275 {
00276     if(_bShutdownInProgress == true)
00277         return;
00278 
00279     _storePLock->aquire();
00280 
00281     _sLockStore.removeMPField(pLock);
00282     
00283     _storePLock->release();
00284 }
00285 
00286 void ThreadManager::removeLockPool(LockPool *pLockPool)
00287 {
00288     if(_bShutdownInProgress == true)
00289         return;
00290 
00291     _storePLock->aquire();
00292 
00293     _sLockPoolStore.removeMPField(pLockPool);
00294     
00295     _storePLock->release();
00296 }
00297 
00298 UInt32 ThreadManager::registerThreadType(MPThreadType *pType)
00299 {
00300     return _sThreadStore.registerMPType(pType);
00301 }
00302 
00303 UInt32 ThreadManager::registerBarrierType(MPBarrierType *pType)
00304 {
00305     return _sBarrierStore.registerMPType(pType);
00306 }
00307 
00308 UInt32 ThreadManager::registerLockType(MPLockType *pType)
00309 {
00310     return _sLockStore.registerMPType(pType);
00311 }
00312 
00313 UInt32 ThreadManager::registerLockPoolType(MPLockPoolType *pType)
00314 {
00315     return _sLockPoolStore.registerMPType(pType);
00316 }
00317 
00318 
00319 #ifdef __sgi
00320 #pragma set woff 1209
00321 #endif
00322 
00323 bool ThreadManager::init(void)
00324 {
00325     bool returnValue = true;
00326 
00327     FDEBUG(("OSGThreadManager init\n"))
00328 
00329 #if defined(OSG_USE_SPROC)
00330     usconfig(CONF_AUTOGROW,   1);
00331     usconfig(CONF_INITUSERS, 20);
00332     usconfig(CONF_INITSIZE, 10 * 1048576);
00333     usconfig(CONF_CHMOD, 0666);
00334 
00335     _pArena = usinit("/dev/zero");
00336 
00337     if(_pArena == NULL)
00338     {
00339         SFATAL << "OSGTM : could not initialize arena " << errno << std::endl;
00340         returnValue = false;
00341     }
00342     else
00343     {
00344         SINFO << "OSGTM : got arena " << _pArena << std::endl;
00345     }
00346 #endif
00347 
00348 #ifdef darwin
00349     Lock      ::getClassType();
00350     BaseThread::getClassType();
00351     Barrier   ::getClassType();
00352 #endif
00353 
00354     _storePLock = _sLockStore.getMPField("OSGTMStoreLock", "OSGLock");
00355     
00356     if(_storePLock == NULL)
00357     {
00358         SFATAL << "OSGTM : could not get table lock" << std::endl;
00359 
00360         returnValue = false;
00361     }
00362     else
00363     {
00364         SINFO << "OSGTM : got table lock " << _storePLock << std::endl;
00365     }
00366 
00367     if(_szAppThreadType == NULL)
00368     {
00369         FINFO(("OSGTM : create -OSGBaseThread- app thread\n"))
00370 
00371         _pAppThread = getThread("OSGAppThread", "OSGBaseThread");
00372     }
00373     else
00374     {
00375         FINFO(("OSGTM : create -%s- app thread\n", _szAppThreadType))
00376         _pAppThread = getThread("OSGAppThread", _szAppThreadType);
00377     }
00378 
00379     FFASSERT((_pAppThread != NULL), 1, 
00380              ("OSGTM : could not get application thread \n");)
00381              
00382 
00383     FINFO(("OSGTM : got application thread %p\n", _pAppThread))
00384 
00385     _pAppThread->init();
00386 
00387     return returnValue;
00388 }
00389 
00390 #ifdef __sgi
00391 #pragma reset woff 1209
00392 #endif
00393 
00394 bool ThreadManager::shutdown(void)
00395 {
00396     _bShutdownInProgress = true;
00397 
00398     _sThreadStore.clear();
00399     _sBarrierStore.clear();
00400     _sLockStore.clear();
00401     _sLockPoolStore.clear();
00402 
00403 #ifdef CHECK
00404 #if defined (OSG_ASPECT_USE_LOCALSTORAGE)
00405     Thread::freeAspect();
00406     Thread::freeThread();
00407     Thread::freeChangeList();
00408 #endif
00409 #endif
00410 
00411 #if defined(OSG_USE_SPROC)
00412     if(_pArena != NULL)
00413         usdetach(_pArena);
00414 #endif
00415 
00416     return true;
00417 }
00418 
00419 /*--------------------------- Constructors --------------------------------*/
00420 
00421 ThreadManager::ThreadManager(void) :
00422     _sThreadStore  (),
00423     _sBarrierStore (),
00424     _sLockStore    (),
00425     _sLockPoolStore(),
00426 
00427     _storePLock    (NULL)
00428 {
00429 #if defined(OSG_USE_SPROC)
00430     _pArena = NULL;
00431 #endif
00432 }
00433 
00434 /*---------------------------- Destructo ----------------------------------*/
00435 
00436 ThreadManager::~ThreadManager(void)
00437 {
00438 }
00439 
00440 
00441 /*-------------------------------------------------------------------------*/
00442 /*                              cvs id's                                   */
00443 
00444 #ifdef __sgi
00445 #pragma set woff 1174
00446 #endif
00447 
00448 #ifdef OSG_LINUX_ICC
00449 #pragma warning( disable : 177 )
00450 #endif
00451 
00452 namespace
00453 {
00454     static Char8 cvsid_cpp[] = "@(#)$Id: $";
00455     static Char8 cvsid_hpp[] = OSGTHREADMANAGER_HEADER_CVSID;
00456     static Char8 cvsid_inl[] = OSGTHREADMANAGER_INLINE_CVSID;
00457 }

Generated on Thu Aug 25 04:11:39 2005 for OpenSG by  doxygen 1.4.3