changed: gcc8 base update
[opensg.git] / Source / Base / Threading / OSGLock.h
blobc18fa84349c365a323e803101a0581dc868457b1
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 #ifndef _OSGLOCK_H_
40 #define _OSGLOCK_H_
41 #ifdef __sgi
42 #pragma once
43 #endif
45 #include "OSGBaseTypes.h"
46 #include "OSGMPBase.h"
48 #if ! defined (OSG_USE_PTHREADS) && \
49 ! defined (OSG_USE_SPROC) && \
50 ! defined (OSG_USE_WINTHREADS)
51 #error "No threading model defined, check your system/compiler combination"
52 #endif
54 #if defined (OSG_USE_PTHREADS)
55 #include <pthread.h>
56 #include <cerrno>
57 #endif
59 #if defined (OSG_USE_SPROC)
60 #include <ulocks.h>
61 #endif
63 #include <boost/mpl/if.hpp>
65 OSG_BEGIN_NAMESPACE
67 static const UInt32 uiLockPoolSize = 32;
69 template <class MPFieldT>
70 class MPFieldStore;
72 //---------------------------------------------------------------------------
73 // Class
74 //---------------------------------------------------------------------------
76 /*! \ingroup GrpBaseMultiThreading
77 \ingroup GrpLibOSGBase
80 class OSG_BASE_DLLMAPPING LockCommonBase : public MPBase
82 /*========================== PUBLIC =================================*/
84 public:
86 /*========================= PROTECTED ===============================*/
88 protected:
90 typedef MPBase Inherited;
92 UInt32 _uiLockId;
94 /*---------------------------------------------------------------------*/
95 /*! \name Constructors */
96 /*! \{ */
98 LockCommonBase(void);
99 LockCommonBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
101 /*! \} */
102 /*---------------------------------------------------------------------*/
103 /*! \name Destructor */
104 /*! \{ */
106 virtual ~LockCommonBase(void);
108 /*! \} */
109 /*========================== PRIVATE ================================*/
111 private:
113 /*!\brief prohibit default function (move to 'public' if needed) */
114 LockCommonBase (const LockCommonBase &source);
115 /*!\brief prohibit default function (move to 'public' if needed) */
116 void operator = (const LockCommonBase &source);
122 //---------------------------------------------------------------------------
123 // Class
124 //---------------------------------------------------------------------------
126 #if defined (OSG_USE_PTHREADS)
128 /*! \ingroup GrpBaseMultiThreading
129 \ingroup GrpLibOSGBase
132 class PThreadLockBase : public LockCommonBase
134 /*========================== PUBLIC =================================*/
136 public :
138 /*========================= PROTECTED ===============================*/
140 protected:
142 typedef LockCommonBase Inherited;
144 /*---------------------------------------------------------------------*/
145 /*! \name Constructors */
146 /*! \{ */
148 PThreadLockBase(void);
149 PThreadLockBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
151 /*! \} */
152 /*---------------------------------------------------------------------*/
153 /*! \name Destructor */
154 /*! \{ */
156 virtual ~PThreadLockBase(void);
158 /*! \} */
159 /*---------------------------------------------------------------------*/
160 /*! \name Construction */
161 /*! \{ */
163 bool init (void);
165 /*! \} */
166 /*---------------------------------------------------------------------*/
167 /*! \name Destruction */
168 /*! \{ */
170 void shutdown(void);
172 /*! \} */
173 /*---------------------------------------------------------------------*/
174 /*! \name Lock */
175 /*! \{ */
177 void acquire(void);
178 void release(void);
179 bool request(void);
181 /*! \} */
182 /*========================== PRIVATE ================================*/
184 private:
186 friend class LockPool;
188 pthread_mutex_t _pLowLevelLock;
190 /*!\brief prohibit default function (move to 'public' if needed) */
191 PThreadLockBase(const PThreadLockBase &source);
192 /*!\brief prohibit default function (move to 'public' if needed) */
193 void operator =(const PThreadLockBase &source);
196 typedef PThreadLockBase LockBase;
198 #endif /* OSG_USE_PTHREADS */
203 //---------------------------------------------------------------------------
204 // Class
205 //---------------------------------------------------------------------------
207 #if defined (OSG_USE_SPROC)
209 /*! \ingroup GrpBaseMultiThreading
210 \ingroup GrpLibOSGBase
213 class SprocLockBase : public LockCommonBase
215 /*========================== PUBLIC =================================*/
217 public :
219 /*========================= PROTECTED ===============================*/
221 protected:
223 typedef LockCommonBase Inherited;
225 /*---------------------------------------------------------------------*/
226 /*! \name Constructors */
227 /*! \{ */
229 SprocLockBase(void);
230 SprocLockBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
232 /*! \} */
233 /*---------------------------------------------------------------------*/
234 /*! \name Destructor */
235 /*! \{ */
237 virtual ~SprocLockBase(void);
239 /*! \} */
240 /*---------------------------------------------------------------------*/
241 /*! \name Construction */
242 /*! \{ */
244 bool init (void);
246 /*! \} */
247 /*---------------------------------------------------------------------*/
248 /*! \name Destruction */
249 /*! \{ */
251 void shutdown(void);
253 /*! \} */
254 /*---------------------------------------------------------------------*/
255 /*! \name Lock */
256 /*! \{ */
258 void acquire(void);
259 void release(void);
260 bool request(void);
262 /*! \} */
263 /*========================== PRIVATE ================================*/
265 private:
267 friend class LockPool;
269 #define OSG_SPROC_USE_LOCK
270 #ifdef OSG_SPROC_USE_LOCK
271 ulock_t _pLowLevelLock;
272 #else
273 usema_t *_pLowLevelSema;
274 #endif
276 /*!\brief prohibit default function (move to 'public' if needed) */
277 SprocLockBase(const SprocLockBase &source);
278 /*!\brief prohibit default function (move to 'public' if needed) */
279 void operator =(const SprocLockBase &source);
282 typedef SprocLockBase LockBase;
284 #endif /* OSG_USE_SPROC */
289 //---------------------------------------------------------------------------
290 // Class
291 //---------------------------------------------------------------------------
293 #if defined (OSG_USE_WINTHREADS)
295 #if _WIN32_WINNT < 0x0400
296 #define OSG_WINLOCK_USE_MUTEX
297 #endif
299 /*! \ingroup GrpBaseMultiThreading
300 \ingroup GrpLibOSGBase
303 class OSG_BASE_DLLMAPPING WinThreadLockBase : public LockCommonBase
305 /*========================== PUBLIC =================================*/
307 public :
309 /*========================= PROTECTED ===============================*/
311 protected:
313 typedef LockCommonBase Inherited;
315 /*---------------------------------------------------------------------*/
316 /*! \name Constructors */
317 /*! \{ */
319 WinThreadLockBase(void);
320 WinThreadLockBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
322 /*! \} */
323 /*---------------------------------------------------------------------*/
324 /*! \name Destructor */
325 /*! \{ */
327 virtual ~WinThreadLockBase(void);
329 /*! \} */
330 /*---------------------------------------------------------------------*/
331 /*! \name Construction */
332 /*! \{ */
334 bool init (void);
336 /*! \} */
337 /*---------------------------------------------------------------------*/
338 /*! \name Destruction */
339 /*! \{ */
341 void shutdown(void);
343 /*! \} */
344 /*---------------------------------------------------------------------*/
345 /*! \name Lock */
346 /*! \{ */
348 void acquire(void);
349 void release(void);
350 bool request(void);
352 /*! \} */
353 /*========================== PRIVATE ================================*/
354 private:
356 friend class LockPool;
358 #ifdef OSG_WINLOCK_USE_MUTEX
359 Handle _pMutex;
360 #else
361 CRITICAL_SECTION _pCriticalSection;
362 #endif
364 /*!\brief prohibit default function (move to 'public' if needed) */
365 WinThreadLockBase(const WinThreadLockBase &source);
366 /*!\brief prohibit default function (move to 'public' if needed) */
367 void operator =(const WinThreadLockBase &source);
370 typedef WinThreadLockBase LockBase;
372 #endif /* OSG_USE_WINTHREADS */
377 //---------------------------------------------------------------------------
378 // Class
379 //---------------------------------------------------------------------------
381 /*! \ingroup GrpBaseMultiThreading
382 \ingroup GrpLibOSGBase
385 class OSG_BASE_DLLMAPPING Lock : public LockBase
387 /*========================== PUBLIC =================================*/
389 public:
391 typedef MPLockType Type;
393 OSG_GEN_INTERNAL_MEMOBJPTR(Lock);
395 /*---------------------------------------------------------------------*/
396 /*! \name Get */
397 /*! \{ */
399 static ObjTransitPtr get (const Char8 *szName,
400 bool bGlobal);
401 static Lock *find (const Char8 *szName );
403 static ObjTransitPtr create ( void );
405 static const MPLockType &getClassType( void );
407 /*! \} */
408 /*---------------------------------------------------------------------*/
409 /*! \name Lock */
410 /*! \{ */
412 void acquire(void);
413 void release(void);
414 bool request(void);
416 /*! \} */
417 /*========================= PROTECTED ===============================*/
419 protected:
421 typedef LockBase Inherited;
423 static MPLockType _type;
425 /*---------------------------------------------------------------------*/
426 /*! \name Construction */
427 /*! \{ */
429 static Lock *create(const Char8 *szName, UInt32 uiId, bool bGlobal);
431 /*! \} */
432 /*---------------------------------------------------------------------*/
433 /*! \name Constructors */
434 /*! \{ */
436 Lock(void);
437 Lock(const Char8 *szName, UInt32 uiId, bool bGlobal);
439 /*! \} */
440 /*---------------------------------------------------------------------*/
441 /*! \name Destructor */
442 /*! \{ */
444 virtual ~Lock(void);
446 /*! \} */
447 /*========================== PRIVATE ================================*/
449 private:
451 friend class LockPool;
452 friend class MPFieldStore<Lock>;
454 /*!\brief prohibit default function (move to 'public' if needed) */
455 Lock(const Lock &source);
456 /*!\brief prohibit default function (move to 'public' if needed) */
457 void operator =(const Lock &source);
460 OSG_GEN_MEMOBJPTR(Lock);
464 //---------------------------------------------------------------------------
465 // Class
466 //---------------------------------------------------------------------------
468 /*! \ingroup GrpBaseMultiThreading
469 \ingroup GrpLibOSGBase
472 class OSG_BASE_DLLMAPPING LockPool : public LockCommonBase
474 /*========================== PUBLIC =================================*/
476 public:
478 typedef MPLockPoolType Type;
480 OSG_GEN_INTERNAL_MEMOBJPTR(LockPool);
482 /*---------------------------------------------------------------------*/
483 /*! \name Get */
484 /*! \{ */
486 static ObjTransitPtr get (const Char8 *szName,
487 bool bGlobal);
488 static LockPool *find (const Char8 *szName );
489 static ObjTransitPtr create ( void );
490 static const MPLockPoolType &getClassType( void );
492 /*! \} */
493 /*---------------------------------------------------------------------*/
494 /*! \name Lock */
495 /*! \{ */
497 void acquire(void *keyP);
498 void release(void *keyP);
499 bool request(void *keyP);
501 /*! \} */
502 /*========================= PROTECTED ===============================*/
504 protected:
506 typedef LockCommonBase Inherited;
508 static MPLockPoolType _type;
510 Lock _pLocks [uiLockPoolSize];
512 #ifdef OSG_DEBUG_LOCK_STAT
513 UInt32 _pLockStats[uiLockPoolSize];
514 #endif
516 /*! \nohierarchy
519 struct ErrorCouldNotMatchSize {};
521 typedef
522 boost::mpl::if_<boost::mpl::bool_<(sizeof(void *) == sizeof(UInt32))>,
523 UInt32,
524 ErrorCouldNotMatchSize>::type NumericalKeyType32;
526 typedef
527 boost::mpl::if_<boost::mpl::bool_<(sizeof(void *) == sizeof(UInt64))>,
528 UInt64,
529 NumericalKeyType32>::type NumericalKeyType;
531 /*---------------------------------------------------------------------*/
532 /*! \name Create */
533 /*! \{ */
535 /*! \} */
536 /*---------------------------------------------------------------------*/
537 /*! \name Constructor */
538 /*! \{ */
540 static const NumericalKeyType uiLockPoolMask = 0x0f80;
542 static LockPool *create(const Char8 *szName, UInt32 uiId, bool bGlobal);
544 /*! \} */
545 /*---------------------------------------------------------------------*/
546 /*! \name Constructor */
547 /*! \{ */
549 LockPool(const Char8 *szName, UInt32 uiId, bool bGlobal);
551 /*! \} */
552 /*---------------------------------------------------------------------*/
553 /*! \name Destructor */
554 /*! \{ */
556 virtual ~LockPool(void);
558 /*! \} */
559 /*---------------------------------------------------------------------*/
560 /*! \name Construction */
561 /*! \{ */
563 bool init(void);
565 /*! \} */
566 /*---------------------------------------------------------------------*/
567 /*! \name Destruction */
568 /*! \{ */
570 void shutdown(void);
572 /*! \} */
573 /*========================== PRIVATE ================================*/
575 private:
577 friend class MPFieldStore<LockPool>;
579 /*!\brief prohibit default function (move to 'public' if needed) */
580 LockPool(const LockPool &source);
581 /*!\brief prohibit default function (move to 'public' if needed) */
582 void operator =(const LockPool &source);
585 OSG_GEN_MEMOBJPTR(LockPool);
587 OSG_END_NAMESPACE
589 #include "OSGLock.inl"
591 #endif /* _OSGLOCK_H_ */