00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef _OSGBASETHREAD_H_
00040 #define _OSGBASETHREAD_H_
00041 #ifdef __sgi
00042 #pragma once
00043 #endif
00044
00045 #include <OSGConfig.h>
00046 #include <OSGBase.h>
00047 #include <OSGBaseTypes.h>
00048 #include <OSGMPBase.h>
00049
00050 #ifdef OSG_USE_PTHREADS
00051 #include <pthread.h>
00052 #endif
00053
00054 #include <utility>
00055
00056 #if ! defined (OSG_USE_PTHREADS) && \
00057 ! defined (OSG_USE_SPROC) && \
00058 ! defined (OSG_USE_WINTHREADS)
00059 #error "No threading model defined, check your system/compiler combination"
00060 #endif
00061
00062 #if defined (OSG_USE_PTHREADS) && defined (OSG_USE_SPROC)
00063 #error "PTHREAD and SPROC defined, check your system/compiler combination"
00064 #endif
00065
00066 #if defined (OSG_USE_WINTHREADS) && defined (OSG_USE_SPROC)
00067 #error "Winthreads and SPROC defined, check your system/compiler combination"
00068 #endif
00069
00070 OSG_BEGIN_NAMESPACE
00071
00072 class BaseThread;
00073
00074 template <class MPFieldT>
00075 class MPFieldStore;
00076
00077
00078
00079
00080
00084 class OSG_BASE_DLLMAPPING BaseThreadCommonBase : public MPBase
00085 {
00086
00087
00088 public:
00089
00090 typedef void (*ThreadFuncF)(void *pThreadArg);
00091
00092 bool isInitialized(void);
00093
00094
00095
00096 protected:
00097
00098 typedef MPBase Inherited;
00099
00100 UInt32 _uiThreadId;
00101 bool _bInitialized;
00102
00103
00107 BaseThreadCommonBase(const Char8 *szName, UInt32 uiId);
00108
00110
00114 virtual ~BaseThreadCommonBase(void);
00115
00117
00118
00119 private:
00120
00121 friend class ThreadManager;
00122
00124 BaseThreadCommonBase(const BaseThreadCommonBase &source);
00126 void operator =(const BaseThreadCommonBase &source);
00127 };
00128
00129
00130
00131
00132
00133
00134
00135
00136 #ifdef OSG_USE_PTHREADS
00137
00141 class BasePThreadBase : public BaseThreadCommonBase
00142 {
00143
00144
00145 public:
00146
00147
00148
00149 protected:
00150
00151 typedef BaseThreadCommonBase Inherited;
00152
00153 #if defined(OSG_PTHREAD_ELF_TLS)
00154 static __thread BaseThread *_pLocalThread;
00155 #else
00156 static pthread_key_t _threadKey;
00157 #endif
00158
00159
00163 static void *threadFunc(void *pThreadArg);
00164
00165 #if !defined(OSG_PTHREAD_ELF_TLS)
00166 static void freeThread(void *pThread );
00167 #endif
00168
00170
00174 void *_pThreadData[3];
00175 pthread_t *_pThreadDesc;
00176
00177 pthread_cond_t *_pBlockCond;
00178 pthread_mutex_t *_pBlockMutex;
00179
00181
00185 BasePThreadBase(const Char8 *szName, UInt32 uiId);
00186
00188
00192 virtual ~BasePThreadBase(void);
00193
00195
00199 virtual void init (void);
00200
00201 void setupThread (void);
00202 void setupBlockCond(void);
00203
00205
00209 static BaseThread *getCurrent(void);
00210
00212
00216 static void join(BasePThreadBase *threadP);
00217
00219
00223 bool runFunction(ThreadFuncF fThreadFunc,
00224 void *pThreadArg );
00225
00227
00231 void block (void);
00232 void unblock(void);
00233
00235
00239 bool exists (void);
00240
00241 void terminate(void);
00242 void kill (void);
00243
00245
00249 void print(void);
00250
00252
00253
00254 private:
00255
00256 friend class ThreadManager;
00257
00259 BasePThreadBase(const BasePThreadBase &source);
00261 void operator =(const BasePThreadBase &source);
00262 };
00263
00264 typedef BasePThreadBase BaseThreadBase;
00265
00266 #endif
00267
00268
00269
00270
00271
00272
00273
00274
00275 #ifdef OSG_USE_SPROC
00276
00280 class BaseSprocBase : public BaseThreadCommonBase
00281 {
00282
00283
00284 public:
00285
00286
00287
00288 protected:
00289
00290 typedef BaseThreadCommonBase Inherited;
00291
00295 struct ProcessData
00296 {
00297 BaseThread *_pThread;
00298 };
00299
00300
00304 static void threadFunc(void *pThreadArgP);
00305
00307
00311 void *_pThreadData[3];
00312 pid_t _pid;
00313
00315
00319 BaseSprocBase(const Char8 *szName, UInt32 uiId);
00320
00322
00326 virtual ~BaseSprocBase(void);
00327
00329
00333 virtual void init (void);
00334
00335 void setPid (void);
00336 void setCurrentInternal(BaseThread *pThread);
00337
00339
00343 static BaseThread *getCurrent(void);
00344
00346
00350 static void join(BaseSprocBase *pThread);
00351
00353
00357 bool runFunction(ThreadFuncF fThreadFunc,
00358 void *pThreadArg);
00359
00361
00365 void block (void);
00366 void unblock (void);
00367
00369
00373 bool exists (void);
00374
00375 void terminate(void);
00376 void kill (void);
00377
00379
00383 void print(void);
00384
00386
00387
00388 private:
00389
00390 friend class ThreadManager;
00391
00393 BaseSprocBase(const BaseSprocBase &source);
00395 void operator =(const BaseSprocBase &source);
00396 };
00397
00398 typedef BaseSprocBase BaseThreadBase;
00399
00400 #endif
00401
00402
00403
00404
00405
00406
00407
00408
00409 #ifdef OSG_USE_WINTHREADS
00410
00414 class OSG_BASE_DLLMAPPING BaseWinThreadBase : public BaseThreadCommonBase
00415 {
00416
00417
00418 public:
00419
00420
00421
00422 protected:
00423
00424 typedef BaseThreadCommonBase Inherited;
00425
00426 #if defined(OSG_ASPECT_USE_LOCALSTORAGE)
00427 static UInt32 _threadKey;
00428 #endif
00429 #if defined(OSG_ASPECT_USE_DECLSPEC)
00430 static __declspec (thread) BaseThread *_pThreadLocal;
00431 #endif
00432
00433
00437 static void threadFunc(void *pThreadArg);
00438
00439 #ifdef OSG_ASPECT_USE_LOCALSTORAGE
00440 static void freeThread(void );
00441 #endif
00442
00444
00448 void *_pThreadData[3];
00449
00450 Handle _pThreadHandle;
00451 Handle _pExternalHandle;
00452 UInt32 _uiNativeThreadId;
00453
00455
00459 BaseWinThreadBase(const Char8 *szName, UInt32 uiId);
00460
00462
00466 virtual ~BaseWinThreadBase(void);
00467
00469
00473 virtual void init (void);
00474
00475 void setPid (void);
00476 void setExternalHandle(Handle pExternalHandle);
00477
00478 void setupThread (void);
00479
00481
00485 static BaseThread *getCurrent(void);
00486
00488
00492 static void join(BaseWinThreadBase *pThread);
00493
00495
00499 bool runFunction(ThreadFuncF fThreadFunc,
00500 void *pThreadArg);
00501
00503
00507 void block (void);
00508 void unblock(void);
00509
00511
00515 bool exists (void);
00516
00517 void terminate(void);
00518 void kill (void);
00519
00521
00525 void print(void);
00526
00528
00529 private:
00530
00531 friend class ThreadManager;
00532
00534 BaseWinThreadBase(const BaseWinThreadBase &source);
00536 void operator =(const BaseWinThreadBase &source);
00537 };
00538
00539 typedef BaseWinThreadBase BaseThreadBase;
00540
00541 #endif
00542
00543
00544
00545
00546
00547
00548
00549
00553 class OSG_BASE_DLLMAPPING BaseThread : public BaseThreadBase
00554 {
00555
00556
00557 private:
00558
00559 typedef BaseThreadBase Inherited;
00560
00561
00562
00563 public:
00564
00565 typedef MPThreadType Type;
00566
00567
00571 static BaseThread *get (const Char8 *szName);
00572 static BaseThread *find (const Char8 *szName);
00573 static BaseThread *create ( void );
00574
00575 static const MPThreadType &getClassType( void );
00576
00578
00582 static BaseThread *getCurrent(void);
00583
00585
00589 static void join(BaseThread *pThread);
00590
00592
00596 void run (void );
00597
00598 bool runFunction(ThreadFuncF fThreadFunc,
00599 void *pThreadArg);
00600
00602
00606 void block (void);
00607 void unblock(void);
00608
00610
00614 bool exists (void);
00615
00616 void terminate(void);
00617 void kill (void);
00618
00620
00624 void print(void);
00625
00627
00628
00629 protected:
00630
00631 static MPThreadType _type;
00632
00633
00637 static BaseThread *create (const Char8 *szName, UInt32 uiId);
00638
00639 static void initThreading( void );
00640
00641 static void runWorkProc ( void *pThread );
00642
00644
00648 BaseThread(const Char8 *szName, UInt32 uiId);
00649
00651
00655 virtual ~BaseThread(void);
00656
00658
00662 virtual void workProc(void);
00663
00665
00666
00667 private:
00668
00669 friend class ThreadManager;
00670 friend class MPFieldStore<BaseThread>;
00671
00673 BaseThread(const BaseThread &source);
00675 void operator =(const BaseThread &source);
00676 };
00677
00678 OSG_END_NAMESPACE
00679
00680 #define OSGBASETHREAD_HEADER_CVSID "@(#)$Id: $"
00681
00682 #endif