changed: auto add updateData callback to stages so that stagedata can be updated...
[opensg.git] / Source / Base / Threading / OSGSemaphore.h
blobb87cc1cc8611d3f627ff61de9b2f8b5f33ab1809
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 _OSGSEMAPHORE_H_
40 #define _OSGSEMAPHORE_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 <semaphore.h>
56 #include <cerrno>
57 #endif
59 #if defined (OSG_USE_SPROC)
60 #include <semaphore.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 SemaphoreCommonBase : public MPBase
80 /*========================== PUBLIC =================================*/
82 public:
84 /*========================= PROTECTED ===============================*/
86 protected:
88 typedef MPBase Inherited;
90 UInt32 _uiSemaphoreId;
92 /*---------------------------------------------------------------------*/
93 /*! \name Constructors */
94 /*! \{ */
96 SemaphoreCommonBase(void);
97 SemaphoreCommonBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
99 /*! \} */
100 /*---------------------------------------------------------------------*/
101 /*! \name Destructor */
102 /*! \{ */
104 virtual ~SemaphoreCommonBase(void);
106 /*! \} */
107 /*========================== PRIVATE ================================*/
109 private:
111 /*!\brief prohibit default function (move to 'public' if needed) */
112 SemaphoreCommonBase (const SemaphoreCommonBase &source);
113 /*!\brief prohibit default function (move to 'public' if needed) */
114 void operator = (const SemaphoreCommonBase &source);
120 //---------------------------------------------------------------------------
121 // Class
122 //---------------------------------------------------------------------------
124 #if defined (OSG_USE_PTHREADS)
126 /*! \ingroup GrpBaseMultiThreading
127 \ingroup GrpLibOSGBase
130 class PThreadSemaphoreBase : public SemaphoreCommonBase
132 /*========================== PUBLIC =================================*/
134 public :
136 /*========================= PROTECTED ===============================*/
138 protected:
140 typedef SemaphoreCommonBase Inherited;
142 /*---------------------------------------------------------------------*/
143 /*! \name Constructors */
144 /*! \{ */
146 PThreadSemaphoreBase(void);
147 PThreadSemaphoreBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
149 /*! \} */
150 /*---------------------------------------------------------------------*/
151 /*! \name Destructor */
152 /*! \{ */
154 virtual ~PThreadSemaphoreBase(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 Semaphore */
173 /*! \{ */
175 void wait(void);
176 void post(void);
178 /*! \} */
179 /*========================== PRIVATE ================================*/
181 private:
183 sem_t _pLowLevelSemaphore;
185 /*!\brief prohibit default function (move to 'public' if needed) */
186 PThreadSemaphoreBase(const PThreadSemaphoreBase &source);
187 /*!\brief prohibit default function (move to 'public' if needed) */
188 void operator =(const PThreadSemaphoreBase &source);
191 typedef PThreadSemaphoreBase SemaphoreBase;
193 #endif /* OSG_USE_PTHREADS */
198 //---------------------------------------------------------------------------
199 // Class
200 //---------------------------------------------------------------------------
202 #if defined (OSG_USE_SPROC)
204 /*! \ingroup GrpBaseMultiThreading
205 \ingroup GrpLibOSGBase
208 class SprocSemaphoreBase : public SemaphoreCommonBase
210 /*========================== PUBLIC =================================*/
212 public :
214 /*========================= PROTECTED ===============================*/
216 protected:
218 typedef SemaphoreCommonBase Inherited;
220 /*---------------------------------------------------------------------*/
221 /*! \name Constructors */
222 /*! \{ */
224 SprocSemaphoreBase(void);
225 SprocSemaphoreBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
227 /*! \} */
228 /*---------------------------------------------------------------------*/
229 /*! \name Destructor */
230 /*! \{ */
232 virtual ~SprocSemaphoreBase(void);
234 /*! \} */
235 /*---------------------------------------------------------------------*/
236 /*! \name Construction */
237 /*! \{ */
239 bool init (void);
241 /*! \} */
242 /*---------------------------------------------------------------------*/
243 /*! \name Destruction */
244 /*! \{ */
246 void shutdown(void);
248 /*! \} */
249 /*---------------------------------------------------------------------*/
250 /*! \name Semaphore */
251 /*! \{ */
253 void wait(void);
254 void post(void);
256 /*! \} */
257 /*========================== PRIVATE ================================*/
259 private:
261 usema_t *_pLowLevelSema;
263 /*!\brief prohibit default function (move to 'public' if needed) */
264 SprocSemaphoreBase(const SprocSemaphoreBase &source);
265 /*!\brief prohibit default function (move to 'public' if needed) */
266 void operator =(const SprocSemaphoreBase &source);
269 typedef SprocSemaphoreBase SemaphoreBase;
271 #endif /* OSG_USE_SPROC */
276 //---------------------------------------------------------------------------
277 // Class
278 //---------------------------------------------------------------------------
280 #if defined (OSG_USE_WINTHREADS)
282 /*! \ingroup GrpBaseMultiThreading
283 \ingroup GrpLibOSGBase
286 class OSG_BASE_DLLMAPPING WinThreadSemaphoreBase : public SemaphoreCommonBase
288 /*========================== PUBLIC =================================*/
290 public :
292 /*========================= PROTECTED ===============================*/
294 protected:
296 typedef SemaphoreCommonBase Inherited;
298 /*---------------------------------------------------------------------*/
299 /*! \name Constructors */
300 /*! \{ */
302 WinThreadSemaphoreBase(void);
303 WinThreadSemaphoreBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
305 /*! \} */
306 /*---------------------------------------------------------------------*/
307 /*! \name Destructor */
308 /*! \{ */
310 virtual ~WinThreadSemaphoreBase(void);
312 /*! \} */
313 /*---------------------------------------------------------------------*/
314 /*! \name Construction */
315 /*! \{ */
317 bool init (void);
319 /*! \} */
320 /*---------------------------------------------------------------------*/
321 /*! \name Destruction */
322 /*! \{ */
324 void shutdown(void);
326 /*! \} */
327 /*---------------------------------------------------------------------*/
328 /*! \name Semaphore */
329 /*! \{ */
331 void wait(void);
332 void post(void);
334 /*! \} */
335 /*========================== PRIVATE ================================*/
336 private:
338 Handle _pSemaphore;
340 /*!\brief prohibit default function (move to 'public' if needed) */
341 WinThreadSemaphoreBase(const WinThreadSemaphoreBase &source);
342 /*!\brief prohibit default function (move to 'public' if needed) */
343 void operator =(const WinThreadSemaphoreBase &source);
346 typedef WinThreadSemaphoreBase SemaphoreBase;
348 #endif /* OSG_USE_WINTHREADS */
353 //---------------------------------------------------------------------------
354 // Class
355 //---------------------------------------------------------------------------
357 /*! \ingroup GrpBaseMultiThreading
358 \ingroup GrpLibOSGBase
361 class OSG_BASE_DLLMAPPING Semaphore : public SemaphoreBase
363 /*========================== PUBLIC =================================*/
365 public:
367 typedef MPSemaphoreType Type;
369 OSG_GEN_INTERNAL_MEMOBJPTR(Semaphore);
371 /*---------------------------------------------------------------------*/
372 /*! \name Get */
373 /*! \{ */
375 static ObjTransitPtr get (const Char8 *szName,
376 bool bGlobal);
377 static Semaphore *find (const Char8 *szName );
379 static ObjTransitPtr create ( void );
381 static const MPSemaphoreType &getClassType( void );
383 /*! \} */
384 /*---------------------------------------------------------------------*/
385 /*! \name Semaphore */
386 /*! \{ */
388 void wait(void);
389 void post(void);
391 /*! \} */
392 /*========================= PROTECTED ===============================*/
394 protected:
396 typedef SemaphoreBase Inherited;
398 static MPSemaphoreType _type;
400 /*---------------------------------------------------------------------*/
401 /*! \name Construction */
402 /*! \{ */
404 static Semaphore *create(const Char8 *szName, UInt32 uiId, bool bGlobal);
406 /*! \} */
407 /*---------------------------------------------------------------------*/
408 /*! \name Constructors */
409 /*! \{ */
411 Semaphore(void);
412 Semaphore(const Char8 *szName, UInt32 uiId, bool bGlobal);
414 /*! \} */
415 /*---------------------------------------------------------------------*/
416 /*! \name Destructor */
417 /*! \{ */
419 virtual ~Semaphore(void);
421 /*! \} */
422 /*========================== PRIVATE ================================*/
424 private:
426 friend class MPFieldStore<Semaphore>;
428 /*!\brief prohibit default function (move to 'public' if needed) */
429 Semaphore(const Semaphore &source);
430 /*!\brief prohibit default function (move to 'public' if needed) */
431 void operator =(const Semaphore &source);
434 OSG_GEN_MEMOBJPTR(Semaphore);
436 OSG_END_NAMESPACE
438 #include "OSGSemaphore.inl"
440 #endif /* _OSGSEMAPHORE_H_ */