changed: gcc8 base update
[opensg.git] / Source / Base / Threading / OSGCondVar.h
blob7e33b304a52b66e49e30e8fe85ac584be45acd3e
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 _OSGCONDVAR_H_
40 #define _OSGCONDVAR_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 template <class MPFieldT>
68 class MPFieldStore;
70 //---------------------------------------------------------------------------
71 // Class
72 //---------------------------------------------------------------------------
74 /*! \ingroup GrpBaseMultiThreading
75 \ingroup GrpLibOSGBase
78 class OSG_BASE_DLLMAPPING CondVarCommonBase : public MPBase
80 /*========================== PUBLIC =================================*/
82 public:
84 /*========================= PROTECTED ===============================*/
86 protected:
88 typedef MPBase Inherited;
90 UInt32 _uiCondVarId;
92 /*---------------------------------------------------------------------*/
93 /*! \name Constructors */
94 /*! \{ */
96 CondVarCommonBase(void);
97 CondVarCommonBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
99 /*! \} */
100 /*---------------------------------------------------------------------*/
101 /*! \name Destructor */
102 /*! \{ */
104 virtual ~CondVarCommonBase(void);
106 /*! \} */
107 /*========================== PRIVATE ================================*/
109 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 //---------------------------------------------------------------------------
121 // Class
122 //---------------------------------------------------------------------------
124 #if defined (OSG_USE_PTHREADS)
126 /*! \ingroup GrpBaseMultiThreading
127 \ingroup GrpLibOSGBase
130 class PThreadCondVarBase : public CondVarCommonBase
132 /*========================== PUBLIC =================================*/
134 public :
136 /*========================= PROTECTED ===============================*/
138 protected:
140 typedef CondVarCommonBase Inherited;
142 /*---------------------------------------------------------------------*/
143 /*! \name Constructors */
144 /*! \{ */
146 PThreadCondVarBase(void);
147 PThreadCondVarBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
149 /*! \} */
150 /*---------------------------------------------------------------------*/
151 /*! \name Destructor */
152 /*! \{ */
154 virtual ~PThreadCondVarBase(void);
156 /*! \} */
157 /*---------------------------------------------------------------------*/
158 /*! \name Construction */
159 /*! \{ */
161 bool init (void);
163 /*! \} */
164 /*---------------------------------------------------------------------*/
165 /*! \name Destruction */
166 /*! \{ */
168 void shutdown(void);
170 /*! \} */
171 /*---------------------------------------------------------------------*/
172 /*! \name CondVar */
173 /*! \{ */
175 void acquire(void);
176 void release(void);
177 bool request(void);
178 bool wait(const Int32 timeToWait = -1);
179 void signal();
180 void broadcast();
182 /*! \} */
183 /*========================== PRIVATE ================================*/
185 private:
187 // XXX: We should make it so we can pass in a mutex some way.
188 //CondVarCommonBase
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 //---------------------------------------------------------------------------
206 // Class
207 //---------------------------------------------------------------------------
209 #if defined (OSG_USE_SPROC)
211 /*! \ingroup GrpBaseMultiThreading
212 \ingroup GrpLibOSGBase
215 class SprocCondVarBase : public CondVarCommonBase
217 /*========================== PUBLIC =================================*/
219 public :
221 /*========================= PROTECTED ===============================*/
223 protected:
225 typedef CondVarCommonBase Inherited;
227 /*---------------------------------------------------------------------*/
228 /*! \name Constructors */
229 /*! \{ */
231 SprocCondVarBase(void);
232 SprocCondVarBase(const Char8 *szName, UInt32 uiId );
234 /*! \} */
235 /*---------------------------------------------------------------------*/
236 /*! \name Destructor */
237 /*! \{ */
239 virtual ~SprocCondVarBase(void);
241 /*! \} */
242 /*---------------------------------------------------------------------*/
243 /*! \name Construction */
244 /*! \{ */
246 bool init (void);
248 /*! \} */
249 /*---------------------------------------------------------------------*/
250 /*! \name Destruction */
251 /*! \{ */
253 void shutdown(void);
255 /*! \} */
256 /*---------------------------------------------------------------------*/
257 /*! \name CondVar */
258 /*! \{ */
260 void acquire(void);
261 void release(void);
262 bool request(void);
263 bool wait(const Int32 timeToWait = -1);
264 void signal();
265 void broadcast();
267 /*! \} */
268 /*========================== PRIVATE ================================*/
270 private:
272 #define OSG_SPROC_USE_LOCK
273 #ifdef OSG_SPROC_USE_LOCK
274 ulock_t _pLowLevelLock;
275 #else
276 usema_t *_pLowLevelSema;
277 #endif
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 //---------------------------------------------------------------------------
293 // Class
294 //---------------------------------------------------------------------------
296 #if defined (OSG_USE_WINTHREADS)
298 /*! \ingroup GrpBaseMultiThreading
299 \ingroup GrpLibOSGBase
300 \nohierarchy
303 typedef struct
305 // Number of waiting threads.
306 int waiters_count_;
308 // Serialize access to <waiters_count_>.
309 CRITICAL_SECTION waiters_count_lock_;
311 // Semaphore used to queue up threads waiting for the condition to
312 // become signaled.
313 HANDLE sema_;
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
317 // semaphore.
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_;
323 } pthread_cond_t;
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 =================================*/
352 public :
354 /*========================= PROTECTED ===============================*/
356 protected:
358 typedef CondVarCommonBase Inherited;
360 /*---------------------------------------------------------------------*/
361 /*! \name Constructors */
362 /*! \{ */
364 WinThreadCondVarBase(void);
365 WinThreadCondVarBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
367 /*! \} */
368 /*---------------------------------------------------------------------*/
369 /*! \name Destructor */
370 /*! \{ */
372 virtual ~WinThreadCondVarBase(void);
374 /*! \} */
375 /*---------------------------------------------------------------------*/
376 /*! \name Construction */
377 /*! \{ */
379 bool init (void);
381 /*! \} */
382 /*---------------------------------------------------------------------*/
383 /*! \name Destruction */
384 /*! \{ */
386 void shutdown(void);
388 /*! \} */
389 /*---------------------------------------------------------------------*/
390 /*! \name CondVar */
391 /*! \{ */
393 void acquire(void);
394 void release(void);
395 bool request(void);
396 bool wait(const Int32 timeToWait = -1);
397 void signal();
398 void broadcast();
400 /*! \} */
401 /*========================== PRIVATE ================================*/
402 private:
404 Handle _pMutex;
406 int waiters_count_;
407 // Number of waiting threads.
409 CRITICAL_SECTION waiters_count_lock_;
410 // Serialize access to <waiters_count_>.
412 HANDLE sema_;
413 // Semaphore used to queue up threads waiting for the condition to
414 // become signaled.
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
419 // semaphore.
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 //---------------------------------------------------------------------------
440 // Class
441 //---------------------------------------------------------------------------
443 /*! \ingroup GrpBaseMultiThreading
444 \ingroup GrpLibOSGBase
447 class OSG_BASE_DLLMAPPING CondVar : public CondVarBase
449 /*========================== PUBLIC =================================*/
451 public:
453 typedef MPCondVarType Type;
455 OSG_GEN_INTERNAL_MEMOBJPTR(CondVar);
457 /*---------------------------------------------------------------------*/
458 /*! \name Get */
459 /*! \{ */
461 static ObjTransitPtr get (const Char8 *szName,
462 bool bGlobal);
463 static CondVar *find (const Char8 *szName );
465 static ObjTransitPtr create ( void );
467 static const MPCondVarType &getClassType( void );
469 /*! \} */
470 /*---------------------------------------------------------------------*/
471 /*! \name CondVar */
472 /*! \{ */
474 void acquire(void);
475 void release(void);
476 bool request(void);
477 bool wait(const Int32 timeToWait = -1);
478 void signal();
479 void broadcast();
481 /*! \} */
482 /*========================= PROTECTED ===============================*/
484 protected:
486 typedef CondVarBase Inherited;
488 static MPCondVarType _type;
490 /*---------------------------------------------------------------------*/
491 /*! \name Construction */
492 /*! \{ */
494 static CondVar *create(const Char8 *szName, UInt32 uiId, bool bGlobal);
496 /*! \} */
497 /*---------------------------------------------------------------------*/
498 /*! \name Constructors */
499 /*! \{ */
501 CondVar(void);
502 CondVar(const Char8 *szName, UInt32 uiId, bool bGlobal);
504 /*! \} */
505 /*---------------------------------------------------------------------*/
506 /*! \name Destructor */
507 /*! \{ */
509 virtual ~CondVar(void);
511 /*! \} */
512 /*========================== PRIVATE ================================*/
514 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);
526 OSG_END_NAMESPACE
528 #include "OSGCondVar.inl"
530 #endif /* _OSGCONDVAR_H_ */