changed: auto add updateData callback to stages so that stagedata can be updated...
[opensg.git] / Source / Base / Threading / OSGThreadManager.h
blob5c2bf2c04d6269b13140f9b8b4070b1b706e369e
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 _OSGTHREADMANAGER_H_
40 #define _OSGTHREADMANAGER_H_
41 #ifdef __sgi
42 #pragma once
43 #endif
45 #include "OSGBaseFunctions.h"
46 #include "OSGLog.h"
48 #if defined(OSG_USE_SPROC)
49 #include <ulocks.h>
50 #endif
52 #include <map>
54 #include "OSGBaseThread.h"
55 #include "OSGBarrier.h"
56 #include "OSGCondVar.h"
57 #include "OSGLock.h"
58 #include "OSGSemaphore.h"
60 OSG_BEGIN_NAMESPACE
62 //---------------------------------------------------------------------------
63 // Class
64 //---------------------------------------------------------------------------
66 /*! \ingroup GrpBaseMultiThreading
67 \ingroup GrpLibOSGBase
68 \nohierarchy
71 template <class MPFieldT>
72 class MPFieldStore
74 /*========================== PUBLIC =================================*/
76 public:
78 typedef typename MPFieldT::Type MPFieldType;
80 /*---------------------------------------------------------------------*/
81 /*! \name Constructor */
82 /*! \{ */
84 MPFieldStore(void);
86 /*! \} */
87 /*---------------------------------------------------------------------*/
88 /*! \name Destructor */
89 /*! \{ */
91 virtual ~MPFieldStore(void);
93 /*! \} */
94 /*---------------------------------------------------------------------*/
95 /*! \name Get */
96 /*! \{ */
98 MPFieldT *getMPField (const Char8 *szName,
99 const Char8 *szTypeName,
100 bool bGlobal );
102 MPFieldT *findMPField (const Char8 *szName);
104 void removeMPField( MPFieldT *pField);
106 /*! \} */
107 /*---------------------------------------------------------------------*/
108 /*! \name Helper */
109 /*! \{ */
111 void clear(void);
113 /*! \} */
114 /*---------------------------------------------------------------------*/
115 /*! \name Find */
116 /*! \{ */
118 MPFieldType *findMPFieldType(const Char8 *szName) const;
120 /*! \} */
121 /*========================= PROTECTED ===============================*/
123 protected:
125 /*---------------------------------------------------------------------*/
126 /*! \name Types */
127 /*! \{ */
129 typedef std::map<std::string, MPFieldType *> MPFieldTypeMap;
130 typedef typename MPFieldTypeMap::iterator MPFieldTypeMapIt;
132 typedef typename MPFieldTypeMap::const_iterator MPFieldTypeMapCIt;
134 typedef std::map<std::string, MPFieldT *> MPFieldMap;
135 typedef typename MPFieldMap::iterator MPFieldMapIt;
137 typedef typename MPFieldMap::const_iterator MPFieldMapCIt;
139 /*! \} */
140 /*---------------------------------------------------------------------*/
142 MPFieldTypeMap _mFieldTypeMap;
143 MPFieldMap _mFieldMap;
145 UInt32 registerMPType(MPFieldType *pType);
147 /*========================== PRIVATE ================================*/
149 private:
151 friend class ThreadManager;
153 /*!\brief prohibit default function (move to 'public' if needed) */
154 MPFieldStore (const MPFieldStore &source);
155 /*!\brief prohibit default function (move to 'public' if needed) */
156 void operator =(const MPFieldStore &source);
162 //---------------------------------------------------------------------------
163 // Class
164 //---------------------------------------------------------------------------
166 /*! \ingroup GrpBaseMultiThreading
167 \ingroup GrpLibOSGBase
168 \nohierarchy
171 class OSG_BASE_DLLMAPPING ThreadManager
173 /*========================== PUBLIC =================================*/
175 public:
177 /*---------------------------------------------------------------------*/
178 /*! \name Types */
179 /*! \{ */
181 typedef MPFieldStore<BaseThread> ThreadStore;
182 typedef MPFieldStore<Barrier > BarrierStore;
183 typedef MPFieldStore<CondVar > CondVarStore;
184 typedef MPFieldStore<Lock > LockStore;
185 typedef MPFieldStore<LockPool > LockPoolStore;
186 typedef MPFieldStore<Semaphore > SemaphoreStore;
188 /*! \} */
189 /*---------------------------------------------------------------------*/
190 /*! \name Set / Get */
191 /*! \{ */
193 static void setAppThreadType(const Char8 *szAppThreadType);
195 static ThreadManager *the ( void );
197 static BaseThread *getAppThread ( void );
199 /*! \} */
200 /*---------------------------------------------------------------------*/
201 /*! \name Create Threading Element */
202 /*! \{ */
204 // bGlobal is passed as UInt32 because Char8 * gets autoconverted to
205 // bool and that would allow existing code to silently compile
207 BaseThreadTransitPtr getThread (const Char8 *szName,
208 UInt32 bGlobal,
209 const Char8 *szTypeName = "OSGThread");
210 BarrierTransitPtr getBarrier (const Char8 *szName,
211 UInt32 bGlobal,
212 const Char8 *szTypeName = "OSGBarrier");
213 CondVarTransitPtr getCondVar (const Char8 *szName,
214 UInt32 bGlobal,
215 const Char8 *szTypeName = "OSGCondVar");
216 LockTransitPtr getLock (const Char8 *szName,
217 UInt32 bGlobal,
218 const Char8 *szTypeName = "OSGLock");
219 LockPoolTransitPtr getLockPool (const Char8 *szName,
220 UInt32 bGlobal,
221 const Char8 *szTypeName = "OSGLockPool");
222 SemaphoreTransitPtr getSemaphore (const Char8 *szName,
223 UInt32 bGlobal,
224 const Char8 *szTypeName ="OSGSemaphore");
226 /*! \} */
227 /*---------------------------------------------------------------------*/
228 /*! \name Debug */
229 /*! \{ */
231 BaseThread *findThread (const Char8 *szName);
232 Barrier *findBarrier (const Char8 *szName);
233 CondVar *findCondVar (const Char8 *szName);
234 Lock *findLock (const Char8 *szName);
235 LockPool *findLockPool (const Char8 *szName);
236 Semaphore *findSemaphore(const Char8 *szName);
238 /*! \} */
239 /*---------------------------------------------------------------------*/
240 /*! \name Debug */
241 /*! \{ */
243 void remove(BaseThread *pThread );
244 void remove(Barrier *pBarrier );
245 void remove(CondVar *pCondVar );
246 void remove(Lock *pLock );
247 void remove(LockPool *pLockPool );
248 void remove(Semaphore *pSemaphore);
250 /*! \} */
251 /*---------------------------------------------------------------------*/
252 /*! \name Debug */
253 /*! \{ */
255 void dump(void);
257 #if defined(OSG_USE_SPROC)
258 /*! \} */
259 /*---------------------------------------------------------------------*/
260 /*! \name Set / Get */
261 /*! \{ */
263 usptr_t *getArena(void);
264 #endif
266 /*! \} */
267 /*========================= PROTECTED ===============================*/
269 protected:
271 static bool initialize (void ) ;
272 static bool terminate (void );
274 UInt32 registerThreadType (MPThreadType *pType );
275 UInt32 registerBarrierType (MPBarrierType *pType );
276 UInt32 registerCondVarType (MPCondVarType *pType );
277 UInt32 registerLockType (MPLockType *pType );
278 UInt32 registerLockPoolType (MPLockPoolType *pType );
279 UInt32 registerSemaphoreType(MPSemaphoreType *pType );
281 bool init (void );
282 bool shutdown (void );
284 /*---------------------------------------------------------------------*/
285 /*! \name Constructors */
286 /*! \{ */
288 ThreadManager(void);
290 /*! \} */
291 /*---------------------------------------------------------------------*/
292 /*! \name Destructor */
293 /*! \{ */
295 virtual ~ThreadManager(void);
297 /*! \} */
298 /*---------------------------------------------------------------------*/
299 /*! \name Destructor */
300 /*! \{ */
302 /*! \} */
303 /*========================== PRIVATE ================================*/
305 private:
307 /*---------------------------------------------------------------------*/
308 /*! \name Friend Classes */
309 /*! \{ */
311 friend class MPThreadType;
312 friend class MPBarrierType;
313 friend class MPCondVarType;
314 friend class MPLockType;
315 friend class MPLockPoolType;
316 friend class MPSemaphoreType;
318 friend class BaseThread;
319 friend class Barrier;
320 friend class CondVar;
321 friend class Lock;
322 friend class LockPool;
323 friend class Semaphore;
325 friend OSG_BASE_DLLMAPPING bool osgDoInit(Int32 argc,
326 Char8 **argv,
327 UInt16 major,
328 UInt16 minor,
329 UInt16 release,
330 bool debug,
331 bool dll,
332 bool mt );
333 friend OSG_BASE_DLLMAPPING bool osgExit (void );
335 /*! \} */
336 /*---------------------------------------------------------------------*/
337 /*! \name Class Variable */
338 /*! \{ */
340 static ThreadManager *_pThreadManager;
341 static BaseThreadRefPtr _pAppThread;
343 static bool _bShutdownInProgress;
345 /*! \} */
346 /*---------------------------------------------------------------------*/
347 /*! \name Instance Variables */
348 /*! \{ */
350 ThreadStore _sThreadStore;
351 BarrierStore _sBarrierStore;
352 CondVarStore _sCondVarStore;
353 LockStore _sLockStore;
354 LockPoolStore _sLockPoolStore;
355 SemaphoreStore _sSemaphoreStore;
357 Lock *_storePLock;
359 static Char8 *_szAppThreadType;
361 #if defined(OSG_USE_SPROC)
362 usptr_t *_pArena;
363 #endif
365 /*! \} */
366 /*---------------------------------------------------------------------*/
368 /*!\brief prohibit default function (move to 'public' if needed) */
369 ThreadManager (const ThreadManager &source);
370 /*!\brief prohibit default function (move to 'public' if needed) */
371 void operator =(const ThreadManager &source);
374 OSG_END_NAMESPACE
376 #include "OSGThreadManager.inl"
378 #endif /* _OSGTHREADMANAGER_H_ */