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 \*---------------------------------------------------------------------------*/
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"
54 #if defined (OSG_USE_PTHREADS)
59 #if defined (OSG_USE_SPROC)
63 #include <boost/mpl/if.hpp>
67 static const UInt32 uiLockPoolSize
= 32;
69 template <class MPFieldT
>
72 //---------------------------------------------------------------------------
74 //---------------------------------------------------------------------------
76 /*! \ingroup GrpBaseMultiThreading
77 \ingroup GrpLibOSGBase
80 class OSG_BASE_DLLMAPPING LockCommonBase
: public MPBase
82 /*========================== PUBLIC =================================*/
86 /*========================= PROTECTED ===============================*/
90 typedef MPBase Inherited
;
94 /*---------------------------------------------------------------------*/
95 /*! \name Constructors */
99 LockCommonBase(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
102 /*---------------------------------------------------------------------*/
103 /*! \name Destructor */
106 virtual ~LockCommonBase(void);
109 /*========================== 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 //---------------------------------------------------------------------------
124 //---------------------------------------------------------------------------
126 #if defined (OSG_USE_PTHREADS)
128 /*! \ingroup GrpBaseMultiThreading
129 \ingroup GrpLibOSGBase
132 class PThreadLockBase
: public LockCommonBase
134 /*========================== PUBLIC =================================*/
138 /*========================= PROTECTED ===============================*/
142 typedef LockCommonBase Inherited
;
144 /*---------------------------------------------------------------------*/
145 /*! \name Constructors */
148 PThreadLockBase(void);
149 PThreadLockBase(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
152 /*---------------------------------------------------------------------*/
153 /*! \name Destructor */
156 virtual ~PThreadLockBase(void);
159 /*---------------------------------------------------------------------*/
160 /*! \name Construction */
166 /*---------------------------------------------------------------------*/
167 /*! \name Destruction */
173 /*---------------------------------------------------------------------*/
182 /*========================== 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 //---------------------------------------------------------------------------
205 //---------------------------------------------------------------------------
207 #if defined (OSG_USE_SPROC)
209 /*! \ingroup GrpBaseMultiThreading
210 \ingroup GrpLibOSGBase
213 class SprocLockBase
: public LockCommonBase
215 /*========================== PUBLIC =================================*/
219 /*========================= PROTECTED ===============================*/
223 typedef LockCommonBase Inherited
;
225 /*---------------------------------------------------------------------*/
226 /*! \name Constructors */
230 SprocLockBase(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
233 /*---------------------------------------------------------------------*/
234 /*! \name Destructor */
237 virtual ~SprocLockBase(void);
240 /*---------------------------------------------------------------------*/
241 /*! \name Construction */
247 /*---------------------------------------------------------------------*/
248 /*! \name Destruction */
254 /*---------------------------------------------------------------------*/
263 /*========================== PRIVATE ================================*/
267 friend class LockPool
;
269 #define OSG_SPROC_USE_LOCK
270 #ifdef OSG_SPROC_USE_LOCK
271 ulock_t _pLowLevelLock
;
273 usema_t
*_pLowLevelSema
;
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 //---------------------------------------------------------------------------
291 //---------------------------------------------------------------------------
293 #if defined (OSG_USE_WINTHREADS)
295 #if _WIN32_WINNT < 0x0400
296 #define OSG_WINLOCK_USE_MUTEX
299 /*! \ingroup GrpBaseMultiThreading
300 \ingroup GrpLibOSGBase
303 class OSG_BASE_DLLMAPPING WinThreadLockBase
: public LockCommonBase
305 /*========================== PUBLIC =================================*/
309 /*========================= PROTECTED ===============================*/
313 typedef LockCommonBase Inherited
;
315 /*---------------------------------------------------------------------*/
316 /*! \name Constructors */
319 WinThreadLockBase(void);
320 WinThreadLockBase(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
323 /*---------------------------------------------------------------------*/
324 /*! \name Destructor */
327 virtual ~WinThreadLockBase(void);
330 /*---------------------------------------------------------------------*/
331 /*! \name Construction */
337 /*---------------------------------------------------------------------*/
338 /*! \name Destruction */
344 /*---------------------------------------------------------------------*/
353 /*========================== PRIVATE ================================*/
356 friend class LockPool
;
358 #ifdef OSG_WINLOCK_USE_MUTEX
361 CRITICAL_SECTION _pCriticalSection
;
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 //---------------------------------------------------------------------------
379 //---------------------------------------------------------------------------
381 /*! \ingroup GrpBaseMultiThreading
382 \ingroup GrpLibOSGBase
385 class OSG_BASE_DLLMAPPING Lock
: public LockBase
387 /*========================== PUBLIC =================================*/
391 typedef MPLockType Type
;
393 OSG_GEN_INTERNAL_MEMOBJPTR(Lock
);
395 /*---------------------------------------------------------------------*/
399 static ObjTransitPtr
get (const Char8
*szName
,
401 static Lock
*find (const Char8
*szName
);
403 static ObjTransitPtr
create ( void );
405 static const MPLockType
&getClassType( void );
408 /*---------------------------------------------------------------------*/
417 /*========================= PROTECTED ===============================*/
421 typedef LockBase Inherited
;
423 static MPLockType _type
;
425 /*---------------------------------------------------------------------*/
426 /*! \name Construction */
429 static Lock
*create(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
432 /*---------------------------------------------------------------------*/
433 /*! \name Constructors */
437 Lock(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
440 /*---------------------------------------------------------------------*/
441 /*! \name Destructor */
447 /*========================== 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 //---------------------------------------------------------------------------
466 //---------------------------------------------------------------------------
468 /*! \ingroup GrpBaseMultiThreading
469 \ingroup GrpLibOSGBase
472 class OSG_BASE_DLLMAPPING LockPool
: public LockCommonBase
474 /*========================== PUBLIC =================================*/
478 typedef MPLockPoolType Type
;
480 OSG_GEN_INTERNAL_MEMOBJPTR(LockPool
);
482 /*---------------------------------------------------------------------*/
486 static ObjTransitPtr
get (const Char8
*szName
,
488 static LockPool
*find (const Char8
*szName
);
489 static ObjTransitPtr
create ( void );
490 static const MPLockPoolType
&getClassType( void );
493 /*---------------------------------------------------------------------*/
497 void acquire(void *keyP
);
498 void release(void *keyP
);
499 bool request(void *keyP
);
502 /*========================= PROTECTED ===============================*/
506 typedef LockCommonBase Inherited
;
508 static MPLockPoolType _type
;
510 Lock _pLocks
[uiLockPoolSize
];
512 #ifdef OSG_DEBUG_LOCK_STAT
513 UInt32 _pLockStats
[uiLockPoolSize
];
519 struct ErrorCouldNotMatchSize
{};
522 boost::mpl::if_
<boost::mpl::bool_
<(sizeof(void *) == sizeof(UInt32
))>,
524 ErrorCouldNotMatchSize
>::type NumericalKeyType32
;
527 boost::mpl::if_
<boost::mpl::bool_
<(sizeof(void *) == sizeof(UInt64
))>,
529 NumericalKeyType32
>::type NumericalKeyType
;
531 /*---------------------------------------------------------------------*/
536 /*---------------------------------------------------------------------*/
537 /*! \name Constructor */
540 static const NumericalKeyType uiLockPoolMask
= 0x0f80;
542 static LockPool
*create(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
545 /*---------------------------------------------------------------------*/
546 /*! \name Constructor */
549 LockPool(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
552 /*---------------------------------------------------------------------*/
553 /*! \name Destructor */
556 virtual ~LockPool(void);
559 /*---------------------------------------------------------------------*/
560 /*! \name Construction */
566 /*---------------------------------------------------------------------*/
567 /*! \name Destruction */
573 /*========================== 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
);
589 #include "OSGLock.inl"
591 #endif /* _OSGLOCK_H_ */