1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2003 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
42 #include "OSGConfig.h"
46 #include "OSGBaseFunctions.h"
47 #include "OSGThreadManager.h"
52 //---------------------------------------------------------------------------
54 //---------------------------------------------------------------------------
56 /*--------------------------- Constructors --------------------------------*/
58 LockCommonBase::LockCommonBase(void) :
59 Inherited(NULL
, true),
64 LockCommonBase::LockCommonBase(const Char8
*szName
,
67 Inherited(szName
, bGlobal
),
72 /*---------------------------- Destructor ---------------------------------*/
74 LockCommonBase::~LockCommonBase(void)
80 #if defined (OSG_USE_PTHREADS)
82 //---------------------------------------------------------------------------
84 //---------------------------------------------------------------------------
86 /*--------------------------- Constructors --------------------------------*/
88 PThreadLockBase::PThreadLockBase(void):
94 PThreadLockBase::PThreadLockBase(const Char8
*szName
,
97 Inherited (szName
, uiId
, bGlobal
),
102 /*---------------------------- Destructor ---------------------------------*/
104 PThreadLockBase::~PThreadLockBase(void)
108 /*--------------------------- Construction --------------------------------*/
110 bool PThreadLockBase::init(void)
112 pthread_mutexattr_t lockAttr
;
114 pthread_mutexattr_init(&lockAttr
);
116 pthread_mutexattr_settype(&lockAttr
, PTHREAD_MUTEX_RECURSIVE
);
118 pthread_mutex_init(&(_pLowLevelLock
), &lockAttr
);
123 /*--------------------------- Destruction ---------------------------------*/
125 void PThreadLockBase::shutdown(void)
127 pthread_mutex_destroy(&(_pLowLevelLock
));
131 #endif /* OSG_USE_PTHREADS */
136 #if defined (OSG_USE_SPROC)
138 //---------------------------------------------------------------------------
140 //---------------------------------------------------------------------------
142 /*--------------------------- Constructors --------------------------------*/
144 SprocLockBase::SprocLockBase(void):
146 #ifdef OSG_SPROC_USE_LOCK
154 SprocLockBase::SprocLockBase(const Char8
*szName
,
156 Inherited (szName
, uiId
),
157 _pLowLevelLock(NULL
)
158 #ifdef OSG_SPROC_USE_LOCK
159 _pLowLevelLock(NULL
)
161 _pLowLevelSema(NULL
)
166 /*---------------------------- Destructor ---------------------------------*/
168 SprocLockBase::~SprocLockBase(void)
172 /*--------------------------- Construction --------------------------------*/
174 bool SprocLockBase::init(void)
176 ThreadManager
*pThreadManager
= ThreadManager::the();
178 if(pThreadManager
== NULL
)
181 if(pThreadManager
->getArena() == NULL
)
184 #ifdef OSG_SPROC_USE_LOCK
185 _pLowLevelLock
= usnewlock(pThreadManager
->getArena());
187 if(_pLowLevelLock
== NULL
)
190 usinitlock(_pLowLevelLock
);
193 _pLowLevelSema
= usnewsema(pThreadManager
->getArena(), 1);
195 if(_pLowLevelSema
== NULL
)
198 usinitsema(_pLowLevelSema
, 1);
199 usctlsema (_pLowLevelSema
, CS_RECURSIVEON
, NULL
);
205 /*--------------------------- Destruction ---------------------------------*/
207 void SprocLockBase::shutdown(void)
209 ThreadManager
*pThreadManager
= ThreadManager::the();
211 if(pThreadManager
== NULL
)
214 if(pThreadManager
->getArena() == NULL
)
217 #ifdef OSG_SPROC_USE_LOCK
218 if(_pLowLevelLock
!= NULL
)
220 usfreelock(_pLowLevelLock
, pThreadManager
->getArena());
222 _pLowLevelLock
= NULL
;
225 if(_pLowLevelSema
!= NULL
)
227 usfreesema(_pLowLevelSema
, pThreadManager
->getArena());
229 _pLowLevelSema
= NULL
;
234 /*------------------------------- Lock ------------------------------------*/
236 #endif /* OSG_USE_SPROC */
240 #if defined (OSG_USE_WINTHREADS)
242 //---------------------------------------------------------------------------
244 //---------------------------------------------------------------------------
246 /*--------------------------- Constructors --------------------------------*/
248 WinThreadLockBase::WinThreadLockBase(void) :
250 #ifdef OSG_WINLOCK_USE_MUTEX
256 WinThreadLockBase::WinThreadLockBase(const Char8
*szName
,
259 Inherited(szName
, uiId
, bGlobal
)
260 #ifdef OSG_WINLOCK_USE_MUTEX
266 /*---------------------------- Destructor ---------------------------------*/
268 WinThreadLockBase::~WinThreadLockBase(void)
272 /*-------------------------- Construction ---------------------------------*/
274 bool WinThreadLockBase::init(void)
276 #ifdef OSG_WINLOCK_USE_MUTEX
277 _pMutex
= CreateMutex( NULL
, // no security attributes
278 FALSE
, // initially not owned
279 _szName
); // name of mutex
288 InitializeCriticalSection(&_pCriticalSection
);
294 /*-------------------------- Destruction ----------------------------------*/
296 void WinThreadLockBase::shutdown(void)
298 #ifdef OSG_WINLOCK_USE_MUTEX
301 CloseHandle(_pMutex
);
305 DeleteCriticalSection(&_pCriticalSection
);
309 #endif /* OSG_USE_WINTHREADS */
313 //---------------------------------------------------------------------------
315 //---------------------------------------------------------------------------
317 MPLockType
Lock::_type("OSGLock", "OSGMPBase", &Lock::create
);
319 /*------------------------------- Get -------------------------------------*/
321 Lock::ObjTransitPtr
Lock::get(const Char8
*szName
, bool bGlobal
)
323 return ThreadManager::the()->getLock(szName
, bGlobal
, "OSGLock");
326 Lock
*Lock::find(const Char8
*szName
)
328 return ThreadManager::the()->findLock(szName
);
332 /*------------------------------ Create -----------------------------------*/
334 Lock
*Lock::create(const Char8
*szName
, UInt32 uiId
, bool bGlobal
)
336 Lock
*returnValue
= NULL
;
338 returnValue
= new Lock(szName
, uiId
, bGlobal
);
340 if(returnValue
->init() == false)
349 /*--------------------------- Constructors --------------------------------*/
356 Lock::Lock(const Char8
*szName
, UInt32 uiId
, bool bGlobal
) :
357 Inherited(szName
, uiId
, bGlobal
)
361 /*---------------------------- Destructor ---------------------------------*/
367 ThreadManager::the()->remove(this);
374 //---------------------------------------------------------------------------
376 //---------------------------------------------------------------------------
378 MPLockPoolType
LockPool::_type("OSGLockPool", "OSGMPBase", &LockPool::create
);
380 /*-------------------------------------------------------------------------*/
383 LockPool::ObjTransitPtr
LockPool::get(const Char8
*szName
, bool bGlobal
)
385 return ThreadManager::the()->getLockPool(szName
,
390 LockPool
*LockPool::find(const Char8
*szName
)
392 return ThreadManager::the()->findLockPool(szName
);
395 /*------------------------------ Create -----------------------------------*/
397 LockPool
*LockPool::create(const Char8
*szName
,
401 LockPool
*returnValue
= NULL
;
403 returnValue
= new LockPool(szName
, uiId
, bGlobal
);
405 if(returnValue
->init() == false)
413 /*--------------------------- Constructors --------------------------------*/
415 LockPool::LockPool(const Char8
*szName
,
418 Inherited(szName
, uiId
, bGlobal
)
422 /*---------------------------- Destructor ---------------------------------*/
424 LockPool::~LockPool(void)
428 ThreadManager::the()->remove(this);
433 /*--------------------------- Construction --------------------------------*/
435 bool LockPool::init(void)
437 bool returnValue
= true;
440 pTmp
= new Char8
[strlen(_szName
) + 6];
442 for(UInt32 i
= 0; i
< uiLockPoolSize
; i
++)
444 #ifdef OSG_DEBUG_LOCK_STAT
447 sprintf(pTmp
, "%s%u\n", _szName
, i
);
449 osgStringDup(pTmp
, _pLocks
[i
]._szName
);
451 returnValue
&= _pLocks
[i
].init();
459 /*--------------------------- Destruction ---------------------------------*/
461 void LockPool::shutdown(void)