1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 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 \*---------------------------------------------------------------------------*/
41 #if defined (OSG_USE_PTHREADS)
43 /*------------------------------- Lock ------------------------------------*/
46 void PThreadLockBase::acquire(void)
48 pthread_mutex_lock(&(_pLowLevelLock));
52 void PThreadLockBase::release(void)
54 pthread_mutex_unlock(&(_pLowLevelLock));
58 bool PThreadLockBase::request(void)
60 return (pthread_mutex_trylock(&(_pLowLevelLock)) != EBUSY);
63 #endif /* OSG_USE_PTHREADS */
66 #if defined (OSG_USE_SPROC)
68 /*------------------------------- Lock ------------------------------------*/
71 void SprocLockBase::acquire(void)
73 #ifdef OSG_SPROC_USE_LOCK
74 if(_pLowLevelLock != NULL)
75 ussetlock(_pLowLevelLock);
77 if(_pLowLevelSema != NULL)
78 uspsema(_pLowLevelSema);
83 void SprocLockBase::release(void)
85 #ifdef OSG_SPROC_USE_LOCK
86 if(_pLowLevelLock != NULL)
87 usunsetlock(_pLowLevelLock);
89 if(_pLowLevelSema != NULL)
90 usvsema(_pLowLevelSema);
95 bool SprocLockBase::request(void)
97 bool returnValue = false;
100 #ifdef OSG_SPROC_USE_LOCK
101 if(_pLowLevelLock != NULL)
102 rc = uscsetlock(_pLowLevelLock, 0);
104 if(_pLowLevelSema != NULL)
105 rc = uscpsema(_pLowLevelSema);
108 returnValue = (rc == 1);
113 #endif /* OSG_USE_SPROC */
116 #if defined (OSG_USE_WINTHREADS)
118 /*------------------------------- Lock ------------------------------------*/
121 void WinThreadLockBase::acquire(void)
123 #if defined(OSG_GV_BETA) && defined(OSG_DBG_LCK)
124 fprintf(stderr, "Lock::acquire %p\n", this);
127 #ifdef OSG_WINLOCK_USE_MUTEX
128 WaitForSingleObject(_pMutex, INFINITE);
130 EnterCriticalSection(&_pCriticalSection);
135 void WinThreadLockBase::release(void)
137 #if defined(OSG_GV_BETA) && defined(OSG_DBG_LCK)
138 fprintf(stderr, "Lock::release %p\n", this);
141 #ifdef OSG_WINLOCK_USE_MUTEX
142 ReleaseMutex(_pMutex);
144 LeaveCriticalSection(&_pCriticalSection);
149 bool WinThreadLockBase::request(void)
151 #ifdef OSG_WINLOCK_USE_MUTEX
153 rc = WaitForSingleObject(_pMutex, 0);
155 if(rc == WAIT_OBJECT_0)
164 return (TryEnterCriticalSection(&_pCriticalSection) != FALSE);
168 #endif /* OSG_USE_WINTHREADS */
174 Lock::ObjTransitPtr Lock::create(void)
176 return Lock::get(NULL, false);
180 const MPLockType &Lock::getClassType(void)
185 /*------------------------------- Lock ------------------------------------*/
188 void Lock::acquire(void)
190 Inherited::acquire();
194 void Lock::release(void)
196 Inherited::release();
200 bool Lock::request(void)
202 return Inherited::request();
206 LockPool::ObjTransitPtr LockPool::create(void)
208 return LockPool::get(NULL, false);
212 const MPLockPoolType &LockPool::getClassType(void)
217 /*------------------------------ Lock -------------------------------------*/
219 #if defined(OSG_WIN32_ICL) || defined(OSG_LINUX_ICC)
220 #pragma warning (disable : 171)
224 void LockPool::acquire(void *keyP)
226 _pLocks[(NumericalKeyType(keyP) & uiLockPoolMask) >> 7].acquire();
230 void LockPool::release(void *keyP)
232 _pLocks[(NumericalKeyType(keyP) & uiLockPoolMask) >> 7].release();
236 bool LockPool::request(void *keyP)
238 return _pLocks[(NumericalKeyType(keyP) & uiLockPoolMask) >> 7].request();
241 #if defined(OSG_WIN32_ICL) || defined(OSG_LINUX_ICC)
242 #pragma warning (error : 171)