changed: auto add updateData callback to stages so that stagedata can be updated...
[opensg.git] / Source / Base / Threading / OSGLock.inl
blobe3fb9273cbc4e60b1ef2cc7ff6991af7b1727692
1 /*---------------------------------------------------------------------------*\
2  *                                OpenSG                                     *
3  *                                                                           *
4  *                                                                           *
5  *           Copyright (C) 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 OSG_BEGIN_NAMESPACE
41 #if defined (OSG_USE_PTHREADS)
43 /*------------------------------- Lock ------------------------------------*/
45 inline
46 void PThreadLockBase::acquire(void)
48     pthread_mutex_lock(&(_pLowLevelLock));
51 inline
52 void PThreadLockBase::release(void)
54     pthread_mutex_unlock(&(_pLowLevelLock));
57 inline
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 ------------------------------------*/
70 inline
71 void SprocLockBase::acquire(void)
73 #ifdef OSG_SPROC_USE_LOCK
74     if(_pLowLevelLock != NULL)
75         ussetlock(_pLowLevelLock);
76 #else
77     if(_pLowLevelSema != NULL)
78         uspsema(_pLowLevelSema);
79 #endif
82 inline
83 void SprocLockBase::release(void)
85 #ifdef OSG_SPROC_USE_LOCK
86     if(_pLowLevelLock != NULL)
87         usunsetlock(_pLowLevelLock);
88 #else
89     if(_pLowLevelSema != NULL)
90         usvsema(_pLowLevelSema);
91 #endif
94 inline
95 bool SprocLockBase::request(void)
97     bool  returnValue = false;
98     Int32 rc          = 0;
100 #ifdef OSG_SPROC_USE_LOCK
101     if(_pLowLevelLock != NULL)
102         rc = uscsetlock(_pLowLevelLock, 0);
103 #else
104     if(_pLowLevelSema != NULL)
105         rc = uscpsema(_pLowLevelSema);
106 #endif
108     returnValue = (rc == 1);
110     return returnValue;
113 #endif /* OSG_USE_SPROC */
116 #if defined (OSG_USE_WINTHREADS)
118 /*------------------------------- Lock ------------------------------------*/
120 inline
121 void WinThreadLockBase::acquire(void)
123 #if defined(OSG_GV_BETA) && defined(OSG_DBG_LCK)
124     fprintf(stderr, "Lock::acquire %p\n", this);
125 #endif
127 #ifdef OSG_WINLOCK_USE_MUTEX
128     WaitForSingleObject(_pMutex, INFINITE);
129 #else
130     EnterCriticalSection(&_pCriticalSection);
131 #endif
134 inline
135 void WinThreadLockBase::release(void)
137 #if defined(OSG_GV_BETA) && defined(OSG_DBG_LCK)
138     fprintf(stderr, "Lock::release %p\n", this);
139 #endif
141 #ifdef OSG_WINLOCK_USE_MUTEX
142     ReleaseMutex(_pMutex);
143 #else
144     LeaveCriticalSection(&_pCriticalSection);
145 #endif
148 inline
149 bool WinThreadLockBase::request(void)
151 #ifdef OSG_WINLOCK_USE_MUTEX
152     DWORD rc;
153     rc = WaitForSingleObject(_pMutex, 0);
155     if(rc == WAIT_OBJECT_0)
156     {
157         return true;
158     }
159     else
160     {
161         return false;
162     }
163 #else
164     return (TryEnterCriticalSection(&_pCriticalSection) != FALSE);
165 #endif
168 #endif /* OSG_USE_WINTHREADS */
173 inline
174 Lock::ObjTransitPtr Lock::create(void)
176     return Lock::get(NULL, false);
179 inline
180 const MPLockType &Lock::getClassType(void)
182     return _type;
185 /*------------------------------- Lock ------------------------------------*/
187 inline
188 void Lock::acquire(void)
190     Inherited::acquire();
193 inline
194 void Lock::release(void)
196     Inherited::release();
199 inline
200 bool Lock::request(void)
202     return Inherited::request();
205 inline
206 LockPool::ObjTransitPtr LockPool::create(void)
208     return LockPool::get(NULL, false);
211 inline
212 const MPLockPoolType &LockPool::getClassType(void)
214     return _type;
217 /*------------------------------ Lock -------------------------------------*/
219 #if defined(OSG_WIN32_ICL) || defined(OSG_LINUX_ICC)
220 #pragma warning (disable : 171)
221 #endif
223 inline
224 void LockPool::acquire(void *keyP)
226     _pLocks[(NumericalKeyType(keyP) & uiLockPoolMask) >> 7].acquire();
229 inline
230 void LockPool::release(void *keyP)
232     _pLocks[(NumericalKeyType(keyP) & uiLockPoolMask) >> 7].release();
235 inline
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)
243 #endif
245 OSG_END_NAMESPACE