fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Base / Threading / OSGBaseThread.h
blobbb73db6f8ddf5e22810ed1883b4824d641b707b8
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 _OSGBASETHREAD_H_
40 #define _OSGBASETHREAD_H_
41 #ifdef __sgi
42 #pragma once
43 #endif
45 #include "OSGBaseTypes.h"
46 #include "OSGMPBase.h"
48 #ifdef OSG_USE_PTHREADS
49 #include <pthread.h>
50 #endif
52 #include <utility>
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"
58 #endif
60 #if defined (OSG_USE_PTHREADS) && defined (OSG_USE_SPROC)
61 #error "PTHREAD and SPROC defined, check your system/compiler combination"
62 #endif
64 #if defined (OSG_USE_WINTHREADS) && defined (OSG_USE_SPROC)
65 #error "Winthreads and SPROC defined, check your system/compiler combination"
66 #endif
68 OSG_BEGIN_NAMESPACE
70 class BaseThread;
72 template <class MPFieldT>
73 class MPFieldStore;
75 //---------------------------------------------------------------------------
76 // Class
77 //---------------------------------------------------------------------------
79 /*! \ingroup GrpBaseMultiThreading
80 \ingroup GrpLibOSGBase
83 class OSG_BASE_DLLMAPPING BaseThreadCommonBase : public MPBase
85 /*========================== PUBLIC =================================*/
87 public:
89 typedef void (*ThreadFuncF)(void *pThreadArg);
91 bool isInitialized(void);
93 /*========================= PROTECTED ===============================*/
95 protected:
97 typedef MPBase Inherited;
99 UInt32 _uiThreadId;
100 bool _bInitialized;
102 /*---------------------------------------------------------------------*/
103 /*! \name Constructors */
104 /*! \{ */
106 BaseThreadCommonBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
108 /*! \} */
109 /*---------------------------------------------------------------------*/
110 /*! \name Destructors */
111 /*! \{ */
113 virtual ~BaseThreadCommonBase(void);
115 /*! \} */
116 /*========================== PRIVATE ================================*/
118 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 //---------------------------------------------------------------------------
132 // Class
133 //---------------------------------------------------------------------------
135 #ifdef OSG_USE_PTHREADS
137 /*! \ingroup GrpBaseMultiThreading
138 \ingroup GrpLibOSGBase
141 class BasePThreadBase : public BaseThreadCommonBase
143 /*========================== PUBLIC =================================*/
145 public:
147 /*========================= PROTECTED ===============================*/
149 protected:
151 typedef BaseThreadCommonBase Inherited;
153 #if defined(OSG_PTHREAD_ELF_TLS)
154 static __thread BaseThread *_pLocalThread;
155 #else
156 static pthread_key_t _threadKey;
157 #endif
159 /*---------------------------------------------------------------------*/
160 /*! \name Helper */
161 /*! \{ */
163 static void *threadFunc(void *pThreadArg);
165 #if !defined(OSG_PTHREAD_ELF_TLS)
166 static void freeThread(void *pThread );
167 #endif
169 /*! \} */
170 /*---------------------------------------------------------------------*/
171 /*! \name Instance Variables */
172 /*! \{ */
174 void *_pThreadData[3];
175 pthread_t *_pThreadDesc;
177 pthread_cond_t *_pBlockCond;
178 pthread_mutex_t *_pBlockMutex;
180 /*! \} */
181 /*---------------------------------------------------------------------*/
182 /*! \name Constructors */
183 /*! \{ */
185 BasePThreadBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
187 /*! \} */
188 /*---------------------------------------------------------------------*/
189 /*! \name Destructors */
190 /*! \{ */
192 virtual ~BasePThreadBase(void);
194 /*! \} */
195 /*---------------------------------------------------------------------*/
196 /*! \name Construction */
197 /*! \{ */
199 virtual void init (void);
200 virtual void shutdown (void);
202 void setupThread (void);
203 void setupBlockCond(void);
205 virtual void doAutoInit (void);
207 /*! \} */
208 /*---------------------------------------------------------------------*/
209 /*! \name Get */
210 /*! \{ */
212 static BaseThread *getCurrent(void);
214 /*! \} */
215 /*---------------------------------------------------------------------*/
216 /*! \name Join */
217 /*! \{ */
219 static void join(BasePThreadBase *threadP);
221 /*! \} */
222 /*---------------------------------------------------------------------*/
223 /*! \name Run */
224 /*! \{ */
226 bool runFunction(ThreadFuncF fThreadFunc,
227 void *pThreadArg );
229 /*! \} */
230 /*---------------------------------------------------------------------*/
231 /*! \name Blocking */
232 /*! \{ */
234 void block (void);
235 void unblock(void);
237 /*! \} */
238 /*---------------------------------------------------------------------*/
239 /*! \name Helper */
240 /*! \{ */
242 bool exists (void);
244 void terminate(void);
245 void kill (void);
247 /*! \} */
248 /*---------------------------------------------------------------------*/
249 /*! \name Dump */
250 /*! \{ */
252 void print(void);
254 /*! \} */
255 /*========================== PRIVATE ================================*/
257 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 //---------------------------------------------------------------------------
275 // Class
276 //---------------------------------------------------------------------------
278 #ifdef OSG_USE_SPROC
280 /*! \ingroup GrpBaseMultiThreading
281 \ingroup GrpLibOSGBase
284 class BaseSprocBase : public BaseThreadCommonBase
286 /*========================== PUBLIC =================================*/
288 public:
290 /*========================= PROTECTED ===============================*/
292 protected:
294 typedef BaseThreadCommonBase Inherited;
296 /*! \hideinhierarchy
299 struct ProcessData
301 BaseThread *_pThread;
304 /*---------------------------------------------------------------------*/
305 /*! \name Helper */
306 /*! \{ */
308 static void threadFunc(void *pThreadArgP);
310 /*! \} */
311 /*---------------------------------------------------------------------*/
312 /*! \name Instance Variables */
313 /*! \{ */
315 void *_pThreadData[3];
316 pid_t _pid;
318 /*! \} */
319 /*---------------------------------------------------------------------*/
320 /*! \name Constructors */
321 /*! \{ */
323 BaseSprocBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
325 /*! \} */
326 /*---------------------------------------------------------------------*/
327 /*! \name Destructors */
328 /*! \{ */
330 virtual ~BaseSprocBase(void);
332 /*! \} */
333 /*---------------------------------------------------------------------*/
334 /*! \name Construction */
335 /*! \{ */
337 virtual void init (void );
338 virtual void shutdown (void );
340 void setPid (void );
341 void setCurrentInternal(BaseThread *pThread);
343 /*! \} */
344 /*---------------------------------------------------------------------*/
345 /*! \name Get */
346 /*! \{ */
348 static BaseThread *getCurrent(void);
350 /*! \} */
351 /*---------------------------------------------------------------------*/
352 /*! \name Join */
353 /*! \{ */
355 static void join(BaseSprocBase *pThread);
357 /*! \} */
358 /*---------------------------------------------------------------------*/
359 /*! \name Run */
360 /*! \{ */
362 bool runFunction(ThreadFuncF fThreadFunc,
363 void *pThreadArg);
365 /*! \} */
366 /*---------------------------------------------------------------------*/
367 /*! \name Blocking */
368 /*! \{ */
370 void block (void);
371 void unblock (void);
373 /*! \} */
374 /*---------------------------------------------------------------------*/
375 /*! \name Helper */
376 /*! \{ */
378 bool exists (void);
380 void terminate(void);
381 void kill (void);
383 /*! \} */
384 /*---------------------------------------------------------------------*/
385 /*! \name Dump */
386 /*! \{ */
388 void print(void);
390 /*! \} */
391 /*========================== PRIVATE ================================*/
393 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 //---------------------------------------------------------------------------
411 // Class
412 //---------------------------------------------------------------------------
414 #ifdef OSG_USE_WINTHREADS
416 #ifdef OSG_ENABLE_AUTOINIT_THREADS
417 static void doThreadCleanup(void);
418 #endif
420 /*! \ingroup GrpBaseMultiThreading
421 \ingroup GrpLibOSGBase
424 class BaseWinThreadBase : public BaseThreadCommonBase
426 /*========================== PUBLIC =================================*/
428 public:
430 /*========================= PROTECTED ===============================*/
432 protected:
434 typedef BaseThreadCommonBase Inherited;
436 #if defined(OSG_WIN32_ASPECT_USE_LOCALSTORAGE)
437 static UInt32 _threadKey;
438 #else
439 static __declspec (thread) BaseThread *_pThreadLocal;
440 #endif
442 /*---------------------------------------------------------------------*/
443 /*! \name Class Specific */
444 /*! \{ */
446 OSG_BASE_DLLMAPPING
447 static void threadFunc(void *pThreadArg);
449 #ifdef OSG_WIN32_ASPECT_USE_LOCALSTORAGE
450 OSG_BASE_DLLMAPPING
451 static void freeThread(void );
452 #endif
454 /*! \} */
455 /*---------------------------------------------------------------------*/
456 /*! \name Instance Variables */
457 /*! \{ */
459 void *_pThreadData[3];
461 Handle _pThreadHandle;
462 Handle _pExternalHandle;
463 UInt32 _uiNativeThreadId;
465 /*! \} */
466 /*---------------------------------------------------------------------*/
467 /*! \name Constructors */
468 /*! \{ */
470 OSG_BASE_DLLMAPPING
471 BaseWinThreadBase(const Char8 *szName, UInt32 uiId, bool bGlobal);
473 /*! \} */
474 /*---------------------------------------------------------------------*/
475 /*! \name Destructors */
476 /*! \{ */
478 OSG_BASE_DLLMAPPING
479 virtual ~BaseWinThreadBase(void);
481 /*! \} */
482 /*---------------------------------------------------------------------*/
483 /*! \name Construction */
484 /*! \{ */
486 OSG_BASE_DLLMAPPING
487 virtual void init (void );
488 OSG_BASE_DLLMAPPING
489 virtual void shutdown (void );
491 OSG_BASE_DLLMAPPING
492 void setPid (void );
493 OSG_BASE_DLLMAPPING
494 void setExternalHandle(Handle pExternalHandle);
496 OSG_BASE_DLLMAPPING
497 void setupThread (void );
499 OSG_BASE_DLLMAPPING
500 virtual void doAutoInit (void );
502 /*! \} */
503 /*---------------------------------------------------------------------*/
504 /*! \name Get */
505 /*! \{ */
507 OSG_BASE_DLLMAPPING
508 static BaseThread *getCurrent(void);
510 OSG_BASE_DLLMAPPING
511 static BaseThread *getCurrentInternal(void);
513 /*! \} */
514 /*---------------------------------------------------------------------*/
515 /*! \name Join */
516 /*! \{ */
518 OSG_BASE_DLLMAPPING
519 static void join(BaseWinThreadBase *pThread);
521 /*! \} */
522 /*---------------------------------------------------------------------*/
523 /*! \name Run */
524 /*! \{ */
526 OSG_BASE_DLLMAPPING
527 bool runFunction(ThreadFuncF fThreadFunc,
528 void *pThreadArg);
530 /*! \} */
531 /*---------------------------------------------------------------------*/
532 /*! \name Blocking */
533 /*! \{ */
535 OSG_BASE_DLLMAPPING
536 void block (void);
537 OSG_BASE_DLLMAPPING
538 void unblock(void);
540 /*! \} */
541 /*---------------------------------------------------------------------*/
542 /*! \name Helper */
543 /*! \{ */
545 OSG_BASE_DLLMAPPING
546 bool exists (void);
548 OSG_BASE_DLLMAPPING
549 void terminate(void);
550 OSG_BASE_DLLMAPPING
551 void kill (void);
553 /*! \} */
554 /*---------------------------------------------------------------------*/
555 /*! \name Dump */
556 /*! \{ */
558 OSG_BASE_DLLMAPPING
559 void print(void);
561 /*! \} */
562 /*========================== PRIVATE ================================*/
563 private:
565 friend class ThreadManager;
566 #ifdef OSG_ENABLE_AUTOINIT_THREADS
567 friend void doThreadCleanup(void);
568 #endif
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 //---------------------------------------------------------------------------
584 // Class
585 //---------------------------------------------------------------------------
587 /*! \ingroup GrpBaseMultiThreading
588 \ingroup GrpLibOSGBase
591 class OSG_BASE_DLLMAPPING BaseThread : public BaseThreadBase
593 /*========================== PRIVATE ================================*/
595 private:
597 typedef BaseThreadBase Inherited;
599 /*========================== PUBLIC =================================*/
601 public:
603 typedef MPThreadType Type;
605 OSG_GEN_INTERNAL_MEMOBJPTR(BaseThread);
607 /*---------------------------------------------------------------------*/
608 /*! \name Get */
609 /*! \{ */
611 static ObjTransitPtr get (const Char8 *szName,
612 bool bGlobal);
613 static BaseThread *find (const Char8 *szName );
614 static ObjTransitPtr create ( void );
616 static const MPThreadType &getClassType( void );
618 /*! \} */
619 /*---------------------------------------------------------------------*/
620 /*! \name Get */
621 /*! \{ */
623 static BaseThread *getCurrent(void);
625 /*! \} */
626 /*---------------------------------------------------------------------*/
627 /*! \name Join */
628 /*! \{ */
630 static void join(BaseThread *pThread);
632 /*! \} */
633 /*---------------------------------------------------------------------*/
634 /*! \name Run */
635 /*! \{ */
637 void run (void );
639 bool runFunction(ThreadFuncF fThreadFunc,
640 void *pThreadArg);
642 /*! \} */
643 /*---------------------------------------------------------------------*/
644 /*! \name Blocking */
645 /*! \{ */
647 void block (void);
648 void unblock(void);
650 /*! \} */
651 /*---------------------------------------------------------------------*/
652 /*! \name Helper */
653 /*! \{ */
655 bool exists (void);
657 void terminate(void);
658 void kill (void);
660 /*! \} */
661 /*---------------------------------------------------------------------*/
662 /*! \name Dump */
663 /*! \{ */
665 void print(void);
667 /*! \} */
668 /*========================= PROTECTED ===============================*/
670 protected:
672 static MPThreadType _type;
674 /*---------------------------------------------------------------------*/
675 /*! \name Class Specific */
676 /*! \{ */
678 static BaseThread *create (const Char8 *szName,
679 UInt32 uiId,
680 bool bGlobal);
682 static void initThreading ( void );
683 static void terminateThreading( void );
685 static void runWorkProc ( void *pThread);
687 /*! \} */
688 /*---------------------------------------------------------------------*/
689 /*! \name Constructors */
690 /*! \{ */
692 BaseThread(const Char8 *szName, UInt32 uiId, bool bGlobal);
694 /*! \} */
695 /*---------------------------------------------------------------------*/
696 /*! \name Destructors */
697 /*! \{ */
699 virtual ~BaseThread(void);
701 /*! \} */
702 /*---------------------------------------------------------------------*/
703 /*! \name Workproc */
704 /*! \{ */
706 virtual void workProc(void);
708 /*! \} */
709 /*========================== PRIVATE ================================*/
711 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);
724 OSG_END_NAMESPACE
726 #include "OSGBaseThread.inl"
728 #endif /* _OSGBASETHREAD_H_ */