changed: gcc8 base update
[opensg.git] / Source / Base / Threading / OSGLock.cpp
blob8b000de090755593f5622bb9a6f668160df7221c
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2003 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
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. *
18 * *
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. *
23 * *
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. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 #include <cstdlib>
40 #include <cstdio>
42 #include "OSGConfig.h"
44 #include <iostream>
45 #include "OSGLock.h"
46 #include "OSGBaseFunctions.h"
47 #include "OSGThreadManager.h"
48 #include "OSGLog.h"
50 OSG_USING_NAMESPACE
52 //---------------------------------------------------------------------------
53 // Class
54 //---------------------------------------------------------------------------
56 /*--------------------------- Constructors --------------------------------*/
58 LockCommonBase::LockCommonBase(void) :
59 Inherited(NULL, true),
60 _uiLockId (0 )
64 LockCommonBase::LockCommonBase(const Char8 *szName,
65 UInt32 uiId,
66 bool bGlobal):
67 Inherited(szName, bGlobal),
68 _uiLockId (uiId )
72 /*---------------------------- Destructor ---------------------------------*/
74 LockCommonBase::~LockCommonBase(void)
80 #if defined (OSG_USE_PTHREADS)
82 //---------------------------------------------------------------------------
83 // Class
84 //---------------------------------------------------------------------------
86 /*--------------------------- Constructors --------------------------------*/
88 PThreadLockBase::PThreadLockBase(void):
89 Inherited (),
90 _pLowLevelLock()
94 PThreadLockBase::PThreadLockBase(const Char8 *szName,
95 UInt32 uiId,
96 bool bGlobal) :
97 Inherited (szName, uiId, bGlobal),
98 _pLowLevelLock()
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);
120 return true;
123 /*--------------------------- Destruction ---------------------------------*/
125 void PThreadLockBase::shutdown(void)
127 pthread_mutex_destroy(&(_pLowLevelLock));
131 #endif /* OSG_USE_PTHREADS */
136 #if defined (OSG_USE_SPROC)
138 //---------------------------------------------------------------------------
139 // Class
140 //---------------------------------------------------------------------------
142 /*--------------------------- Constructors --------------------------------*/
144 SprocLockBase::SprocLockBase(void):
145 Inherited ( ),
146 #ifdef OSG_SPROC_USE_LOCK
147 _pLowLevelLock(NULL)
148 #else
149 _pLowLevelSema(NULL)
150 #endif
154 SprocLockBase::SprocLockBase(const Char8 *szName,
155 UInt32 uiId ):
156 Inherited (szName, uiId),
157 _pLowLevelLock(NULL )
158 #ifdef OSG_SPROC_USE_LOCK
159 _pLowLevelLock(NULL )
160 #else
161 _pLowLevelSema(NULL )
162 #endif
166 /*---------------------------- Destructor ---------------------------------*/
168 SprocLockBase::~SprocLockBase(void)
172 /*--------------------------- Construction --------------------------------*/
174 bool SprocLockBase::init(void)
176 ThreadManager *pThreadManager = ThreadManager::the();
178 if(pThreadManager == NULL)
179 return false;
181 if(pThreadManager->getArena() == NULL)
182 return false;
184 #ifdef OSG_SPROC_USE_LOCK
185 _pLowLevelLock = usnewlock(pThreadManager->getArena());
187 if(_pLowLevelLock == NULL)
188 return false;
190 usinitlock(_pLowLevelLock);
192 #else
193 _pLowLevelSema = usnewsema(pThreadManager->getArena(), 1);
195 if(_pLowLevelSema == NULL)
196 return false;
198 usinitsema(_pLowLevelSema, 1);
199 usctlsema (_pLowLevelSema, CS_RECURSIVEON, NULL);
200 #endif
202 return true;
205 /*--------------------------- Destruction ---------------------------------*/
207 void SprocLockBase::shutdown(void)
209 ThreadManager *pThreadManager = ThreadManager::the();
211 if(pThreadManager == NULL)
212 return;
214 if(pThreadManager->getArena() == NULL)
215 return;
217 #ifdef OSG_SPROC_USE_LOCK
218 if(_pLowLevelLock != NULL)
220 usfreelock(_pLowLevelLock, pThreadManager->getArena());
222 _pLowLevelLock = NULL;
224 #else
225 if(_pLowLevelSema != NULL)
227 usfreesema(_pLowLevelSema, pThreadManager->getArena());
229 _pLowLevelSema = NULL;
231 #endif
234 /*------------------------------- Lock ------------------------------------*/
236 #endif /* OSG_USE_SPROC */
240 #if defined (OSG_USE_WINTHREADS)
242 //---------------------------------------------------------------------------
243 // Class
244 //---------------------------------------------------------------------------
246 /*--------------------------- Constructors --------------------------------*/
248 WinThreadLockBase::WinThreadLockBase(void) :
249 Inherited( )
250 #ifdef OSG_WINLOCK_USE_MUTEX
251 , _pMutex (NULL)
252 #endif
256 WinThreadLockBase::WinThreadLockBase(const Char8 *szName,
257 UInt32 uiId,
258 bool bGlobal) :
259 Inherited(szName, uiId, bGlobal)
260 #ifdef OSG_WINLOCK_USE_MUTEX
261 , _pMutex (NULL )
262 #endif
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
281 if(_pMutex == NULL)
283 return false;
286 return true;
287 #else
288 InitializeCriticalSection(&_pCriticalSection);
290 return true;
291 #endif
294 /*-------------------------- Destruction ----------------------------------*/
296 void WinThreadLockBase::shutdown(void)
298 #ifdef OSG_WINLOCK_USE_MUTEX
299 if(_pMutex != NULL)
301 CloseHandle(_pMutex);
302 _pMutex = NULL;
304 #else
305 DeleteCriticalSection(&_pCriticalSection);
306 #endif
309 #endif /* OSG_USE_WINTHREADS */
313 //---------------------------------------------------------------------------
314 // Class
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)
342 delete returnValue;
343 returnValue = NULL;
346 return returnValue;
349 /*--------------------------- Constructors --------------------------------*/
351 Lock::Lock(void) :
352 Inherited()
356 Lock::Lock(const Char8 *szName, UInt32 uiId, bool bGlobal) :
357 Inherited(szName, uiId, bGlobal)
361 /*---------------------------- Destructor ---------------------------------*/
363 Lock::~Lock(void)
365 _bGlobal = false;
367 ThreadManager::the()->remove(this);
369 shutdown();
374 //---------------------------------------------------------------------------
375 // Class
376 //---------------------------------------------------------------------------
378 MPLockPoolType LockPool::_type("OSGLockPool", "OSGMPBase", &LockPool::create);
380 /*-------------------------------------------------------------------------*/
381 /* Get */
383 LockPool::ObjTransitPtr LockPool::get(const Char8 *szName, bool bGlobal)
385 return ThreadManager::the()->getLockPool(szName,
386 bGlobal,
387 "OSGLockPool");
390 LockPool *LockPool::find(const Char8 *szName)
392 return ThreadManager::the()->findLockPool(szName);
395 /*------------------------------ Create -----------------------------------*/
397 LockPool *LockPool::create(const Char8 *szName,
398 UInt32 uiId,
399 bool bGlobal)
401 LockPool *returnValue = NULL;
403 returnValue = new LockPool(szName, uiId, bGlobal);
405 if(returnValue->init() == false)
407 returnValue = NULL;
410 return returnValue;
413 /*--------------------------- Constructors --------------------------------*/
415 LockPool::LockPool(const Char8 *szName,
416 UInt32 uiId,
417 bool bGlobal) :
418 Inherited(szName, uiId, bGlobal)
422 /*---------------------------- Destructor ---------------------------------*/
424 LockPool::~LockPool(void)
426 _bGlobal = false;
428 ThreadManager::the()->remove(this);
430 shutdown();
433 /*--------------------------- Construction --------------------------------*/
435 bool LockPool::init(void)
437 bool returnValue = true;
438 Char8 *pTmp;
440 pTmp = new Char8[strlen(_szName) + 6];
442 for(UInt32 i = 0; i < uiLockPoolSize; i++)
444 #ifdef OSG_DEBUG_LOCK_STAT
445 _pLockStats[i] = 0;
446 #endif
447 sprintf(pTmp, "%s%u\n", _szName, i);
449 osgStringDup(pTmp, _pLocks[i]._szName);
451 returnValue &= _pLocks[i].init();
454 delete [] pTmp;
456 return returnValue;
459 /*--------------------------- Destruction ---------------------------------*/
461 void LockPool::shutdown(void)