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 \*---------------------------------------------------------------------------*/
39 #ifndef _OSGCONDVAR_H_
40 #define _OSGCONDVAR_H_
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 template <class MPFieldT
>
70 //---------------------------------------------------------------------------
72 //---------------------------------------------------------------------------
74 /*! \ingroup GrpBaseMultiThreading
75 \ingroup GrpLibOSGBase
78 class OSG_BASE_DLLMAPPING CondVarCommonBase
: public MPBase
80 /*========================== PUBLIC =================================*/
84 /*========================= PROTECTED ===============================*/
88 typedef MPBase Inherited
;
92 /*---------------------------------------------------------------------*/
93 /*! \name Constructors */
96 CondVarCommonBase(void);
97 CondVarCommonBase(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
100 /*---------------------------------------------------------------------*/
101 /*! \name Destructor */
104 virtual ~CondVarCommonBase(void);
107 /*========================== PRIVATE ================================*/
111 /*!\brief prohibit default function (move to 'public' if needed) */
112 CondVarCommonBase (const CondVarCommonBase
&source
);
113 /*!\brief prohibit default function (move to 'public' if needed) */
114 void operator = (const CondVarCommonBase
&source
);
120 //---------------------------------------------------------------------------
122 //---------------------------------------------------------------------------
124 #if defined (OSG_USE_PTHREADS)
126 /*! \ingroup GrpBaseMultiThreading
127 \ingroup GrpLibOSGBase
130 class PThreadCondVarBase
: public CondVarCommonBase
132 /*========================== PUBLIC =================================*/
136 /*========================= PROTECTED ===============================*/
140 typedef CondVarCommonBase Inherited
;
142 /*---------------------------------------------------------------------*/
143 /*! \name Constructors */
146 PThreadCondVarBase(void);
147 PThreadCondVarBase(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
150 /*---------------------------------------------------------------------*/
151 /*! \name Destructor */
154 virtual ~PThreadCondVarBase(void);
157 /*---------------------------------------------------------------------*/
158 /*! \name Construction */
164 /*---------------------------------------------------------------------*/
165 /*! \name Destruction */
171 /*---------------------------------------------------------------------*/
178 bool wait(const Int32 timeToWait
= -1);
183 /*========================== PRIVATE ================================*/
187 // XXX: We should make it so we can pass in a mutex some way.
189 pthread_mutex_t _pLowLevelLock
;
190 pthread_cond_t _pLowLevelCondVar
;
192 /*!\brief prohibit default function (move to 'public' if needed) */
193 PThreadCondVarBase(const PThreadCondVarBase
&source
);
194 /*!\brief prohibit default function (move to 'public' if needed) */
195 void operator =(const PThreadCondVarBase
&source
);
198 typedef PThreadCondVarBase CondVarBase
;
200 #endif /* OSG_USE_PTHREADS */
205 //---------------------------------------------------------------------------
207 //---------------------------------------------------------------------------
209 #if defined (OSG_USE_SPROC)
211 /*! \ingroup GrpBaseMultiThreading
212 \ingroup GrpLibOSGBase
215 class SprocCondVarBase
: public CondVarCommonBase
217 /*========================== PUBLIC =================================*/
221 /*========================= PROTECTED ===============================*/
225 typedef CondVarCommonBase Inherited
;
227 /*---------------------------------------------------------------------*/
228 /*! \name Constructors */
231 SprocCondVarBase(void);
232 SprocCondVarBase(const Char8
*szName
, UInt32 uiId
);
235 /*---------------------------------------------------------------------*/
236 /*! \name Destructor */
239 virtual ~SprocCondVarBase(void);
242 /*---------------------------------------------------------------------*/
243 /*! \name Construction */
249 /*---------------------------------------------------------------------*/
250 /*! \name Destruction */
256 /*---------------------------------------------------------------------*/
263 bool wait(const Int32 timeToWait
= -1);
268 /*========================== PRIVATE ================================*/
272 #define OSG_SPROC_USE_LOCK
273 #ifdef OSG_SPROC_USE_LOCK
274 ulock_t _pLowLevelLock
;
276 usema_t
*_pLowLevelSema
;
279 /*!\brief prohibit default function (move to 'public' if needed) */
280 SprocCondVarBase(const SprocCondVarBase
&source
);
281 /*!\brief prohibit default function (move to 'public' if needed) */
282 void operator =(const SprocCondVarBase
&source
);
285 typedef SprocCondVarBase CondVarBase
;
287 #endif /* OSG_USE_SPROC */
292 //---------------------------------------------------------------------------
294 //---------------------------------------------------------------------------
296 #if defined (OSG_USE_WINTHREADS)
298 /*! \ingroup GrpBaseMultiThreading
299 \ingroup GrpLibOSGBase
305 // Number of waiting threads.
308 // Serialize access to <waiters_count_>.
309 CRITICAL_SECTION waiters_count_lock_
;
311 // Semaphore used to queue up threads waiting for the condition to
315 // An auto-reset event used by the broadcast/signal thread to wait
316 // for all the waiting thread(s) to wake up and be released from the
318 HANDLE waiters_done_
;
320 // Keeps track of whether we were broadcasting or signaling. This
321 // allows us to optimize the code if we're just signaling.
322 size_t was_broadcast_
;
325 typedef HANDLE pthread_mutex_t
;
327 /*! \ingroup GrpBaseMultiThreading
329 int pthread_cond_init(pthread_cond_t
*cv
, void* dummy
);
331 /*! \ingroup GrpBaseMultiThreading
333 int pthread_cond_wait(pthread_cond_t
*cv
,
334 pthread_mutex_t
*external_mutex
);
336 /*! \ingroup GrpBaseMultiThreading
338 int pthread_cond_signal(pthread_cond_t
*cv
);
340 /*! \ingroup GrpBaseMultiThreading
342 int pthread_cond_broadcast(pthread_cond_t
*cv
);
344 /*! \ingroup GrpBaseMultiThreading
345 \ingroup GrpLibOSGBase
348 class OSG_BASE_DLLMAPPING WinThreadCondVarBase
: public CondVarCommonBase
350 /*========================== PUBLIC =================================*/
354 /*========================= PROTECTED ===============================*/
358 typedef CondVarCommonBase Inherited
;
360 /*---------------------------------------------------------------------*/
361 /*! \name Constructors */
364 WinThreadCondVarBase(void);
365 WinThreadCondVarBase(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
368 /*---------------------------------------------------------------------*/
369 /*! \name Destructor */
372 virtual ~WinThreadCondVarBase(void);
375 /*---------------------------------------------------------------------*/
376 /*! \name Construction */
382 /*---------------------------------------------------------------------*/
383 /*! \name Destruction */
389 /*---------------------------------------------------------------------*/
396 bool wait(const Int32 timeToWait
= -1);
401 /*========================== PRIVATE ================================*/
407 // Number of waiting threads.
409 CRITICAL_SECTION waiters_count_lock_
;
410 // Serialize access to <waiters_count_>.
413 // Semaphore used to queue up threads waiting for the condition to
416 HANDLE waiters_done_
;
417 // An auto-reset event used by the broadcast/signal thread to wait
418 // for all the waiting thread(s) to wake up and be released from the
421 // Keeps track of whether we were broadcasting or signaling. This
422 // allows us to optimize the code if we're just signaling.
423 size_t was_broadcast_
;
426 /*!\brief prohibit default function (move to 'public' if needed) */
427 WinThreadCondVarBase(const WinThreadCondVarBase
&source
);
428 /*!\brief prohibit default function (move to 'public' if needed) */
429 void operator =(const WinThreadCondVarBase
&source
);
432 typedef WinThreadCondVarBase CondVarBase
;
434 #endif /* OSG_USE_WINTHREADS */
439 //---------------------------------------------------------------------------
441 //---------------------------------------------------------------------------
443 /*! \ingroup GrpBaseMultiThreading
444 \ingroup GrpLibOSGBase
447 class OSG_BASE_DLLMAPPING CondVar
: public CondVarBase
449 /*========================== PUBLIC =================================*/
453 typedef MPCondVarType Type
;
455 OSG_GEN_INTERNAL_MEMOBJPTR(CondVar
);
457 /*---------------------------------------------------------------------*/
461 static ObjTransitPtr
get (const Char8
*szName
,
463 static CondVar
*find (const Char8
*szName
);
465 static ObjTransitPtr
create ( void );
467 static const MPCondVarType
&getClassType( void );
470 /*---------------------------------------------------------------------*/
477 bool wait(const Int32 timeToWait
= -1);
482 /*========================= PROTECTED ===============================*/
486 typedef CondVarBase Inherited
;
488 static MPCondVarType _type
;
490 /*---------------------------------------------------------------------*/
491 /*! \name Construction */
494 static CondVar
*create(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
497 /*---------------------------------------------------------------------*/
498 /*! \name Constructors */
502 CondVar(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
505 /*---------------------------------------------------------------------*/
506 /*! \name Destructor */
509 virtual ~CondVar(void);
512 /*========================== PRIVATE ================================*/
516 friend class MPFieldStore
<CondVar
>;
518 /*!\brief prohibit default function (move to 'public' if needed) */
519 CondVar(const CondVar
&source
);
520 /*!\brief prohibit default function (move to 'public' if needed) */
521 void operator =(const CondVar
&source
);
524 OSG_GEN_MEMOBJPTR(CondVar
);
528 #include "OSGCondVar.inl"
530 #endif /* _OSGCONDVAR_H_ */