TThread.h

Go to the documentation of this file.
00001 // @(#)root/thread:$Id: TThread.h 34686 2010-07-31 19:52:20Z pcanal $
00002 // Author: Fons Rademakers   02/07/97
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TThread
00013 #define ROOT_TThread
00014 
00015 
00016 //////////////////////////////////////////////////////////////////////////
00017 //                                                                      //
00018 // TThread                                                              //
00019 //                                                                      //
00020 // This class implements threads. A thread is an execution environment  //
00021 // much lighter than a process. A single process can have multiple      //
00022 // threads. The actual work is done via the TThreadImp class (either    //
00023 // TPosixThread or TWin32Thread).                                       //
00024 //                                                                      //
00025 //////////////////////////////////////////////////////////////////////////
00026 
00027 #ifndef ROOT_TObject
00028 #include "TObject.h"
00029 #endif
00030 #ifndef ROOT_TMutex
00031 #include "TMutex.h"
00032 #endif
00033 #ifndef ROOT_TCondition
00034 #include "TCondition.h"
00035 #endif
00036 #ifndef ROOT_TSystem
00037 #include "TSystem.h"
00038 #endif
00039 #ifndef ROOT_TTimer
00040 #include "TTimer.h"
00041 #endif
00042 #ifndef ROOT_Varargs
00043 #include "Varargs.h"
00044 #endif
00045 
00046 class TThreadImp;
00047 
00048 
00049 class TThread : public TNamed {
00050 
00051 friend class TThreadImp;
00052 friend class TPosixThread;
00053 friend class TThreadTimer;
00054 friend class TThreadCleaner;
00055 friend class TWin32Thread;
00056 
00057 public:
00058 
00059    typedef void *(*VoidRtnFunc_t)(void *);
00060    typedef void  (*VoidFunc_t)(void *);
00061 
00062    enum EPriority {
00063       kLowPriority,
00064       kNormalPriority,
00065       kHighPriority
00066    };
00067 
00068    enum EState {
00069       kInvalidState,            // thread was not created properly
00070       kNewState,                // thread object exists but hasn't started
00071       kRunningState,            // thread is running
00072       kTerminatedState,         // thread has terminated but storage has not
00073                                 // yet been reclaimed (i.e. waiting to be joined)
00074       kFinishedState,           // thread has finished
00075       kCancelingState,          // thread in process of canceling
00076       kCanceledState,           // thread has been canceled
00077       kDeletingState            // thread in process of deleting
00078    };
00079 
00080 private:
00081    TThread       *fNext;                  // pointer to next thread
00082    TThread       *fPrev;                  // pointer to prev thread
00083    TThread      **fHolder;                // pointer to holder of this (delete only)
00084    EPriority      fPriority;              // thread priority
00085    EState         fState;                 // thread state
00086    EState         fStateComing;           // coming thread state
00087    Long_t         fId;                    // thread id
00088    Long_t         fHandle;                // Win32 thread handle
00089    Bool_t         fDetached;              // kTRUE if thread is Detached
00090    Bool_t         fNamed;                 // kTRUE if thread is Named
00091    VoidRtnFunc_t  fFcnRetn;               // void* start function of thread
00092    VoidFunc_t     fFcnVoid;               // void  start function of thread
00093    void          *fThreadArg;             // thread start function arguments
00094    void          *fClean;                 // support of cleanup structure
00095    void          *fTsd[20];               // thread specific data container
00096    char           fComment[100];          // thread specific state comment
00097 
00098    static TThreadImp      *fgThreadImp;   // static pointer to thread implementation
00099    static char  * volatile fgXAct;        // Action name to do by main thread
00100    static void ** volatile fgXArr;        // pointer to control array of void pointers for action
00101    static volatile Int_t   fgXAnb;        // size of array above
00102    static volatile Int_t   fgXArt;        // return XA flag
00103    static Long_t           fgMainId;      // thread id of main thread
00104    static TThread         *fgMain;        // pointer to chain of TThread's
00105    static TMutex          *fgMainMutex;   // mutex to protect chain of threads
00106    static TMutex          *fgXActMutex;   // mutex to protect XAction
00107    static TCondition      *fgXActCondi;   // condition for XAction
00108 
00109    // Private Member functions
00110    void           Constructor();
00111    void           SetComment(const char *txt = 0)
00112                      { fComment[0] = 0; if (txt) { strncpy(fComment, txt, 99); fComment[99] = 0; } }
00113    void           DoError(Int_t level, const char *location, const char *fmt, va_list va) const;
00114    void           ErrorHandler(int level, const char *location, const char *fmt, va_list ap) const;
00115    static void    Init();
00116    static void   *Function(void *ptr);
00117    static Int_t   XARequest(const char *xact, Int_t nb, void **ar, Int_t *iret);
00118    static void    AfterCancel(TThread *th);
00119 
00120    TThread(const TThread&);            // not implemented
00121    TThread& operator=(const TThread&); // not implemented
00122 
00123 public:
00124    TThread(VoidRtnFunc_t fn, void *arg = 0, EPriority pri = kNormalPriority);
00125    TThread(VoidFunc_t fn, void *arg = 0, EPriority pri = kNormalPriority);
00126    TThread(const char *thname, VoidRtnFunc_t fn, void *arg = 0, EPriority pri = kNormalPriority);
00127    TThread(const char *thname, VoidFunc_t fn, void *arg = 0, EPriority pri = kNormalPriority);
00128    TThread(Long_t id = 0);
00129    virtual ~TThread();
00130 
00131    Int_t            Kill();
00132    Int_t            Run(void *arg = 0);
00133    void             SetPriority(EPriority pri);
00134    void             Delete(Option_t *option="") { TObject::Delete(option); }
00135    EPriority        GetPriority() const { return fPriority; }
00136    EState           GetState() const { return fState; }
00137    Long_t           GetId() const { return fId; }
00138    static void      Ps();
00139    static void      ps() { Ps(); }
00140 
00141    static Bool_t    IsInitialized();
00142 
00143    Long_t           Join(void **ret = 0);
00144    static Long_t    Join(Long_t id, void **ret = 0);
00145 
00146    static Int_t     Exit(void *ret = 0);
00147    static Int_t     Exists();
00148    static TThread  *GetThread(Long_t id);
00149    static TThread  *GetThread(const char *name);
00150 
00151    static Int_t     Lock();                  //User's lock of main mutex
00152    static Int_t     TryLock();               //User's try lock of main mutex
00153    static Int_t     UnLock();                //User's unlock of main mutex
00154    static TThread  *Self();
00155    static Long_t    SelfId();
00156    static Int_t     Sleep(ULong_t secs, ULong_t nanos = 0);
00157    static Int_t     GetTime(ULong_t *absSec, ULong_t *absNanoSec);
00158 
00159    static Int_t     Delete(TThread *&th);
00160    static void    **Tsd(void *dflt, Int_t k);
00161 
00162    // Cancellation
00163    // there are two types of TThread cancellation:
00164    //    DEFERRED     - Cancellation only in user provided cancel-points
00165    //    ASYNCHRONOUS - In any point
00166    //    DEFERRED is more safe, it is DEFAULT.
00167    static Int_t     SetCancelOn();
00168    static Int_t     SetCancelOff();
00169    static Int_t     SetCancelAsynchronous();
00170    static Int_t     SetCancelDeferred();
00171    static Int_t     CancelPoint();
00172    static Int_t     Kill(Long_t id);
00173    static Int_t     Kill(const char *name);
00174    static Int_t     CleanUpPush(void *free, void *arg = 0);
00175    static Int_t     CleanUpPop(Int_t exe = 0);
00176    static Int_t     CleanUp();
00177 
00178    // XActions
00179    static void      Printf(const char *fmt, ...);   // format and print
00180    static void      XAction();
00181 
00182    ClassDef(TThread,0)  // Thread class
00183 };
00184 
00185 
00186 //////////////////////////////////////////////////////////////////////////
00187 //                                                                      //
00188 // TThreadCleaner                                                       //
00189 //                                                                      //
00190 //////////////////////////////////////////////////////////////////////////
00191 
00192 class TThreadCleaner {
00193 public:
00194    TThreadCleaner() { }
00195    ~TThreadCleaner();
00196 };
00197 
00198 
00199 //////////////////////////////////////////////////////////////////////////
00200 //                                                                      //
00201 // TThreadTimer                                                         //
00202 //                                                                      //
00203 //////////////////////////////////////////////////////////////////////////
00204 
00205 class TThreadTimer : public TTimer {
00206 public:
00207    TThreadTimer(Long_t ms = 10);
00208    Bool_t Notify();
00209 };
00210 
00211 #endif

Generated on Tue Jul 5 14:12:00 2011 for ROOT_528-00b_version by  doxygen 1.5.1