fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Base / Threading / OSGBarrier.h
blob4d52a31f00d040e99f8a5dc677b8d530036a487c
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 _OSGBARRIER_H_
40 #define _OSGBARRIER_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 #endif /* OSG_USE_PTHREADS */
58 #if defined (OSG_USE_SPROC)
59 #include <ulocks.h>
60 #endif /* OSG_USE_SPROC */
62 OSG_BEGIN_NAMESPACE
64 template <class MPFieldT>
65 class MPFieldStore;
67 //---------------------------------------------------------------------------
68 // Class
69 //---------------------------------------------------------------------------
71 /*! \ingroup GrpBaseMultiThreading
72 \ingroup GrpLibOSGBase
75 class OSG_BASE_DLLMAPPING BarrierCommonBase : public MPBase
77 /*========================== PUBLIC =================================*/
79 public:
81 /*========================= PROTECTED ===============================*/
83 protected:
85 typedef MPBase Inherited;
87 UInt32 _uiBarrierId;
88 volatile UInt32 _uiNumWaitFor;
90 /*---------------------------------------------------------------------*/
91 /*! \name Constructors */
92 /*! \{ */
94 BarrierCommonBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
96 /*! \} */
97 /*---------------------------------------------------------------------*/
98 /*! \name Destructor */
99 /*! \{ */
101 virtual ~BarrierCommonBase(void);
103 /*! \} */
104 /*---------------------------------------------------------------------*/
105 /*! \name Set */
106 /*! \{ */
108 void setNumWaitFor(UInt32 uiNumWaitFor);
110 /*! \} */
111 /*========================== PRIVATE ================================*/
113 private:
115 /*!\brief prohibit default function (move to 'public' if needed) */
116 BarrierCommonBase(const BarrierCommonBase &source);
117 /*!\brief prohibit default function (move to 'public' if needed) */
118 void operator =(const BarrierCommonBase &source);
124 //---------------------------------------------------------------------------
125 // Class
126 //---------------------------------------------------------------------------
128 #if defined (OSG_USE_PTHREADS)
130 /*! \ingroup GrpBaseMultiThreading
131 \ingroup GrpLibOSGBase
134 class PThreadBarrierBase : public BarrierCommonBase
136 /*========================== PUBLIC =================================*/
138 public:
140 /*========================= PROTECTED ===============================*/
142 protected:
144 typedef BarrierCommonBase Inherited;
146 /*---------------------------------------------------------------------*/
147 /*! \name Constructors */
148 /*! \{ */
150 PThreadBarrierBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
152 /*! \} */
153 /*---------------------------------------------------------------------*/
154 /*! \name Destructor */
155 /*! \{ */
157 virtual ~PThreadBarrierBase(void);
159 /*! \} */
160 /*---------------------------------------------------------------------*/
161 /*! \name Construction */
162 /*! \{ */
164 bool init ( void );
166 /*! \} */
167 /*---------------------------------------------------------------------*/
168 /*! \name Destruction */
169 /*! \{ */
171 void shutdown( void );
173 /*! \} */
174 /*---------------------------------------------------------------------*/
175 /*! \name Enter */
176 /*! \{ */
178 void enter (void );
179 void enter (UInt32 uiNumWaitFor);
181 UInt32 getNumWaiting(void );
183 /*! \} */
184 /*========================== PRIVATE ================================*/
186 private:
188 pthread_mutex_t _pLockOne;
189 pthread_cond_t _pWakeupCondition[2];
190 UInt32 _uiCount;
191 UInt32 _uiCurrentCond;
193 /*!\brief prohibit default function (move to 'public' if needed) */
194 PThreadBarrierBase(const PThreadBarrierBase &source);
195 /*!\brief prohibit default function (move to 'public' if needed) */
196 void operator =(const PThreadBarrierBase &source);
199 typedef PThreadBarrierBase BarrierBase;
201 #endif /* OSG_USE_PTHREADS */
206 //---------------------------------------------------------------------------
207 // Class
208 //---------------------------------------------------------------------------
210 #if defined (OSG_USE_SPROC)
212 /*! \ingroup GrpBaseMultiThreading
213 \ingroup GrpLibOSGBase
216 class SprocBarrierBase : public BarrierCommonBase
218 /*========================== PUBLIC =================================*/
220 public:
222 /*========================= PROTECTED ===============================*/
224 protected:
226 typedef BarrierCommonBase Inherited;
228 /*---------------------------------------------------------------------*/
229 /*! \name Constructors */
230 /*! \{ */
232 SprocBarrierBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
234 /*! \} */
235 /*---------------------------------------------------------------------*/
236 /*! \name Destructor */
237 /*! \{ */
239 virtual ~SprocBarrierBase(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 Enter */
258 /*! \{ */
260 void enter (void );
261 void enter (UInt32 uiNumWaitFor);
263 UInt32 getNumWaiting(void );
265 /*! \} */
266 /*========================== PRIVATE ================================*/
268 private:
270 barrier_t *_pBarrier;
272 /*!\brief prohibit default function (move to 'public' if needed) */
273 SprocBarrierBase(const SprocBarrierBase &source);
274 /*!\brief prohibit default function (move to 'public' if needed) */
275 void operator =(const SprocBarrierBase &source);
278 typedef SprocBarrierBase BarrierBase;
280 #endif /* OSG_USE_SPROC */
285 //---------------------------------------------------------------------------
286 // Class
287 //---------------------------------------------------------------------------
289 #if defined (OSG_USE_WINTHREADS)
291 /*! \ingroup GrpBaseMultiThreading
292 \ingroup GrpLibOSGBase
295 class OSG_BASE_DLLMAPPING WinThreadBarrierBase : public BarrierCommonBase
297 /*========================== PUBLIC =================================*/
299 public:
301 /*========================= PROTECTED ===============================*/
303 protected:
305 /*---------------------------------------------------------------------*/
306 /*! \name Constructors */
307 /*! \{ */
309 WinThreadBarrierBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
311 /*! \} */
312 /*---------------------------------------------------------------------*/
313 /*! \name Destructor */
314 /*! \{ */
316 virtual ~WinThreadBarrierBase(void);
318 /*! \} */
319 /*---------------------------------------------------------------------*/
320 /*! \name Construction */
321 /*! \{ */
323 bool init (void );
325 /*! \} */
326 /*---------------------------------------------------------------------*/
327 /*! \name Destruction */
328 /*! \{ */
330 void shutdown(void );
332 /*! \} */
333 /*---------------------------------------------------------------------*/
334 /*! \name Enter */
335 /*! \{ */
337 void enter (void );
338 void enter (UInt32 uiNumWaitFor);
340 UInt32 getNumWaiting(void );
342 /*! \} */
343 /*========================== PRIVATE ================================*/
345 private:
347 typedef BarrierCommonBase Inherited;
349 Handle _pMutex1;
350 Handle _pBarrierSema[2];
351 UInt32 _uiCount;
352 UInt32 _uiCurrentCond;
354 /*!\brief prohibit default function (move to 'public' if needed) */
355 WinThreadBarrierBase(const WinThreadBarrierBase &source);
356 /*!\brief prohibit default function (move to 'public' if needed) */
357 void operator =(const WinThreadBarrierBase &source);
360 typedef WinThreadBarrierBase BarrierBase;
362 #endif /* OSG_USE_WINTHREADS */
367 //---------------------------------------------------------------------------
368 // Class
369 //---------------------------------------------------------------------------
371 /*! \ingroup GrpBaseMultiThreading
372 \ingroup GrpLibOSGBase
375 class OSG_BASE_DLLMAPPING Barrier : public BarrierBase
377 /*========================== PUBLIC =================================*/
379 public:
381 typedef MPBarrierType Type;
383 OSG_GEN_INTERNAL_MEMOBJPTR(Barrier);
385 /*---------------------------------------------------------------------*/
386 /*! \name Get */
387 /*! \{ */
389 static ObjTransitPtr get (const Char8 *szName,
390 bool bGlobal);
391 static Barrier *find (const Char8 *szName );
393 static ObjTransitPtr create ( void );
395 static const MPBarrierType &getClassType( void );
397 /*! \} */
398 /*---------------------------------------------------------------------*/
399 /*! \name Enter */
400 /*! \{ */
402 void setNumWaitFor(UInt32 uiNumWaitFor);
403 UInt32 getNumWaiting(void );
405 /*! \} */
406 /*---------------------------------------------------------------------*/
407 /*! \name Enter */
408 /*! \{ */
410 void enter(void );
411 void enter(UInt32 uiNumWaitFor);
413 /*! \} */
414 /*========================= PROTECTED ===============================*/
416 protected:
418 typedef BarrierBase Inherited;
420 static MPBarrierType _type;
422 /*---------------------------------------------------------------------*/
423 /*! \name Constructors */
424 /*! \{ */
426 Barrier(const Char8 *szName, UInt32 uiId, bool bGlobal);
428 /*! \} */
429 /*---------------------------------------------------------------------*/
430 /*! \name Destructor */
431 /*! \{ */
433 virtual ~Barrier(void);
435 /*! \} */
436 /*---------------------------------------------------------------------*/
437 /*! \name Construction */
438 /*! \{ */
440 static Barrier *create(const Char8 *szName,
441 UInt32 uiId,
442 bool bGlobal);
444 /*! \} */
445 /*========================== PRIVATE ================================*/
447 private:
449 friend class MPFieldStore<Barrier>;
451 /*!\brief prohibit default function (move to 'public' if needed) */
452 Barrier(const Barrier &source);
453 /*!\brief prohibit default function (move to 'public' if needed) */
454 void operator =(const Barrier &source);
457 OSG_GEN_MEMOBJPTR(Barrier);
459 OSG_END_NAMESPACE
461 #include "OSGBarrier.inl"
463 #endif /* _OSGBARRIER_H_ */