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 _OSGBASETHREAD_H_
40 #define _OSGBASETHREAD_H_
45 #include "OSGBaseTypes.h"
46 #include "OSGMPBase.h"
48 #ifdef OSG_USE_PTHREADS
54 #if ! defined (OSG_USE_PTHREADS) && \
55 ! defined (OSG_USE_SPROC) && \
56 ! defined (OSG_USE_WINTHREADS)
57 #error "No threading model defined, check your system/compiler combination"
60 #if defined (OSG_USE_PTHREADS) && defined (OSG_USE_SPROC)
61 #error "PTHREAD and SPROC defined, check your system/compiler combination"
64 #if defined (OSG_USE_WINTHREADS) && defined (OSG_USE_SPROC)
65 #error "Winthreads and SPROC defined, check your system/compiler combination"
72 template <class MPFieldT
>
75 //---------------------------------------------------------------------------
77 //---------------------------------------------------------------------------
79 /*! \ingroup GrpBaseMultiThreading
80 \ingroup GrpLibOSGBase
83 class OSG_BASE_DLLMAPPING BaseThreadCommonBase
: public MPBase
85 /*========================== PUBLIC =================================*/
89 typedef void (*ThreadFuncF
)(void *pThreadArg
);
91 bool isInitialized(void);
93 /*========================= PROTECTED ===============================*/
97 typedef MPBase Inherited
;
102 /*---------------------------------------------------------------------*/
103 /*! \name Constructors */
106 BaseThreadCommonBase(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
109 /*---------------------------------------------------------------------*/
110 /*! \name Destructors */
113 virtual ~BaseThreadCommonBase(void);
116 /*========================== PRIVATE ================================*/
120 friend class ThreadManager
;
122 /*!\brief prohibit default function (move to 'public' if needed) */
123 BaseThreadCommonBase(const BaseThreadCommonBase
&source
);
124 /*!\brief prohibit default function (move to 'public' if needed) */
125 void operator =(const BaseThreadCommonBase
&source
);
131 //---------------------------------------------------------------------------
133 //---------------------------------------------------------------------------
135 #ifdef OSG_USE_PTHREADS
137 /*! \ingroup GrpBaseMultiThreading
138 \ingroup GrpLibOSGBase
141 class BasePThreadBase
: public BaseThreadCommonBase
143 /*========================== PUBLIC =================================*/
147 /*========================= PROTECTED ===============================*/
151 typedef BaseThreadCommonBase Inherited
;
153 #if defined(OSG_PTHREAD_ELF_TLS)
154 static __thread BaseThread
*_pLocalThread
;
156 static pthread_key_t _threadKey
;
159 /*---------------------------------------------------------------------*/
163 static void *threadFunc(void *pThreadArg
);
165 #if !defined(OSG_PTHREAD_ELF_TLS)
166 static void freeThread(void *pThread
);
170 /*---------------------------------------------------------------------*/
171 /*! \name Instance Variables */
174 void *_pThreadData
[3];
175 pthread_t
*_pThreadDesc
;
177 pthread_cond_t
*_pBlockCond
;
178 pthread_mutex_t
*_pBlockMutex
;
181 /*---------------------------------------------------------------------*/
182 /*! \name Constructors */
185 BasePThreadBase(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
188 /*---------------------------------------------------------------------*/
189 /*! \name Destructors */
192 virtual ~BasePThreadBase(void);
195 /*---------------------------------------------------------------------*/
196 /*! \name Construction */
199 virtual void init (void);
200 virtual void shutdown (void);
202 void setupThread (void);
203 void setupBlockCond(void);
205 virtual void doAutoInit (void);
208 /*---------------------------------------------------------------------*/
212 static BaseThread
*getCurrent(void);
215 /*---------------------------------------------------------------------*/
219 static void join(BasePThreadBase
*threadP
);
222 /*---------------------------------------------------------------------*/
226 bool runFunction(ThreadFuncF fThreadFunc
,
230 /*---------------------------------------------------------------------*/
231 /*! \name Blocking */
238 /*---------------------------------------------------------------------*/
244 void terminate(void);
248 /*---------------------------------------------------------------------*/
255 /*========================== PRIVATE ================================*/
259 friend class ThreadManager
;
261 /*!\brief prohibit default function (move to 'public' if needed) */
262 BasePThreadBase(const BasePThreadBase
&source
);
263 /*!\brief prohibit default function (move to 'public' if needed) */
264 void operator =(const BasePThreadBase
&source
);
267 typedef BasePThreadBase BaseThreadBase
;
269 #endif /* OSG_USE_PTHREADS */
274 //---------------------------------------------------------------------------
276 //---------------------------------------------------------------------------
280 /*! \ingroup GrpBaseMultiThreading
281 \ingroup GrpLibOSGBase
284 class BaseSprocBase
: public BaseThreadCommonBase
286 /*========================== PUBLIC =================================*/
290 /*========================= PROTECTED ===============================*/
294 typedef BaseThreadCommonBase Inherited
;
301 BaseThread
*_pThread
;
304 /*---------------------------------------------------------------------*/
308 static void threadFunc(void *pThreadArgP
);
311 /*---------------------------------------------------------------------*/
312 /*! \name Instance Variables */
315 void *_pThreadData
[3];
319 /*---------------------------------------------------------------------*/
320 /*! \name Constructors */
323 BaseSprocBase(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
326 /*---------------------------------------------------------------------*/
327 /*! \name Destructors */
330 virtual ~BaseSprocBase(void);
333 /*---------------------------------------------------------------------*/
334 /*! \name Construction */
337 virtual void init (void );
338 virtual void shutdown (void );
341 void setCurrentInternal(BaseThread
*pThread
);
344 /*---------------------------------------------------------------------*/
348 static BaseThread
*getCurrent(void);
351 /*---------------------------------------------------------------------*/
355 static void join(BaseSprocBase
*pThread
);
358 /*---------------------------------------------------------------------*/
362 bool runFunction(ThreadFuncF fThreadFunc
,
366 /*---------------------------------------------------------------------*/
367 /*! \name Blocking */
374 /*---------------------------------------------------------------------*/
380 void terminate(void);
384 /*---------------------------------------------------------------------*/
391 /*========================== PRIVATE ================================*/
395 friend class ThreadManager
;
397 /*!\brief prohibit default function (move to 'public' if needed) */
398 BaseSprocBase(const BaseSprocBase
&source
);
399 /*!\brief prohibit default function (move to 'public' if needed) */
400 void operator =(const BaseSprocBase
&source
);
403 typedef BaseSprocBase BaseThreadBase
;
405 #endif /* OSG_USE_SPROC */
410 //---------------------------------------------------------------------------
412 //---------------------------------------------------------------------------
414 #ifdef OSG_USE_WINTHREADS
416 #ifdef OSG_ENABLE_AUTOINIT_THREADS
417 static void doThreadCleanup(void);
420 /*! \ingroup GrpBaseMultiThreading
421 \ingroup GrpLibOSGBase
424 class BaseWinThreadBase
: public BaseThreadCommonBase
426 /*========================== PUBLIC =================================*/
430 /*========================= PROTECTED ===============================*/
434 typedef BaseThreadCommonBase Inherited
;
436 #if defined(OSG_WIN32_ASPECT_USE_LOCALSTORAGE)
437 static UInt32 _threadKey
;
439 static __declspec (thread
) BaseThread
*_pThreadLocal
;
442 /*---------------------------------------------------------------------*/
443 /*! \name Class Specific */
447 static void threadFunc(void *pThreadArg
);
449 #ifdef OSG_WIN32_ASPECT_USE_LOCALSTORAGE
451 static void freeThread(void );
455 /*---------------------------------------------------------------------*/
456 /*! \name Instance Variables */
459 void *_pThreadData
[3];
461 Handle _pThreadHandle
;
462 Handle _pExternalHandle
;
463 UInt32 _uiNativeThreadId
;
466 /*---------------------------------------------------------------------*/
467 /*! \name Constructors */
471 BaseWinThreadBase(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
474 /*---------------------------------------------------------------------*/
475 /*! \name Destructors */
479 virtual ~BaseWinThreadBase(void);
482 /*---------------------------------------------------------------------*/
483 /*! \name Construction */
487 virtual void init (void );
489 virtual void shutdown (void );
494 void setExternalHandle(Handle pExternalHandle
);
497 void setupThread (void );
500 virtual void doAutoInit (void );
503 /*---------------------------------------------------------------------*/
508 static BaseThread
*getCurrent(void);
511 static BaseThread
*getCurrentInternal(void);
514 /*---------------------------------------------------------------------*/
519 static void join(BaseWinThreadBase
*pThread
);
522 /*---------------------------------------------------------------------*/
527 bool runFunction(ThreadFuncF fThreadFunc
,
531 /*---------------------------------------------------------------------*/
532 /*! \name Blocking */
541 /*---------------------------------------------------------------------*/
549 void terminate(void);
554 /*---------------------------------------------------------------------*/
562 /*========================== PRIVATE ================================*/
565 friend class ThreadManager
;
566 #ifdef OSG_ENABLE_AUTOINIT_THREADS
567 friend void doThreadCleanup(void);
570 /*!\brief prohibit default function (move to 'public' if needed) */
571 BaseWinThreadBase(const BaseWinThreadBase
&source
);
572 /*!\brief prohibit default function (move to 'public' if needed) */
573 void operator =(const BaseWinThreadBase
&source
);
576 typedef BaseWinThreadBase BaseThreadBase
;
578 #endif /* OSG_USE_WINTHREADS */
583 //---------------------------------------------------------------------------
585 //---------------------------------------------------------------------------
587 /*! \ingroup GrpBaseMultiThreading
588 \ingroup GrpLibOSGBase
591 class OSG_BASE_DLLMAPPING BaseThread
: public BaseThreadBase
593 /*========================== PRIVATE ================================*/
597 typedef BaseThreadBase Inherited
;
599 /*========================== PUBLIC =================================*/
603 typedef MPThreadType Type
;
605 OSG_GEN_INTERNAL_MEMOBJPTR(BaseThread
);
607 /*---------------------------------------------------------------------*/
611 static ObjTransitPtr
get (const Char8
*szName
,
613 static BaseThread
*find (const Char8
*szName
);
614 static ObjTransitPtr
create ( void );
616 static const MPThreadType
&getClassType( void );
619 /*---------------------------------------------------------------------*/
623 static BaseThread
*getCurrent(void);
626 /*---------------------------------------------------------------------*/
630 static void join(BaseThread
*pThread
);
633 /*---------------------------------------------------------------------*/
639 bool runFunction(ThreadFuncF fThreadFunc
,
643 /*---------------------------------------------------------------------*/
644 /*! \name Blocking */
651 /*---------------------------------------------------------------------*/
657 void terminate(void);
661 /*---------------------------------------------------------------------*/
668 /*========================= PROTECTED ===============================*/
672 static MPThreadType _type
;
674 /*---------------------------------------------------------------------*/
675 /*! \name Class Specific */
678 static BaseThread
*create (const Char8
*szName
,
682 static void initThreading ( void );
683 static void terminateThreading( void );
685 static void runWorkProc ( void *pThread
);
688 /*---------------------------------------------------------------------*/
689 /*! \name Constructors */
692 BaseThread(const Char8
*szName
, UInt32 uiId
, bool bGlobal
);
695 /*---------------------------------------------------------------------*/
696 /*! \name Destructors */
699 virtual ~BaseThread(void);
702 /*---------------------------------------------------------------------*/
703 /*! \name Workproc */
706 virtual void workProc(void);
709 /*========================== PRIVATE ================================*/
713 friend class ThreadManager
;
714 friend class MPFieldStore
<BaseThread
>;
716 /*!\brief prohibit default function (move to 'public' if needed) */
717 BaseThread(const BaseThread
&source
);
718 /*!\brief prohibit default function (move to 'public' if needed) */
719 void operator =(const BaseThread
&source
);
722 OSG_GEN_MEMOBJPTR(BaseThread
);
726 #include "OSGBaseThread.inl"
728 #endif /* _OSGBASETHREAD_H_ */