Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:12:29

0001 // @(#)root/thread:$Id$
0002 // Author: Fons Rademakers   02/07/97
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_TThread
0013 #define ROOT_TThread
0014 
0015 
0016 //////////////////////////////////////////////////////////////////////////
0017 //                                                                      //
0018 // TThread                                                              //
0019 //                                                                      //
0020 // This class implements threads. A thread is an execution environment  //
0021 // much lighter than a process. A single process can have multiple      //
0022 // threads. The actual work is done via the TThreadImp class (either    //
0023 // TPosixThread or TWin32Thread).                                       //
0024 //                                                                      //
0025 //////////////////////////////////////////////////////////////////////////
0026 
0027 #include "TNamed.h"
0028 #include "TTimer.h"
0029 #include <stdarg.h>
0030 
0031 #ifdef R__LESS_INCLUDES
0032 class TCondition;
0033 #else
0034 #include "TCondition.h"
0035 #endif
0036 
0037 class TMutex;
0038 class TThreadImp;
0039 
0040 class TThread : public TNamed {
0041 
0042 friend class TThreadImp;
0043 friend class TPosixThread;
0044 friend class TThreadTimer;
0045 friend class TThreadCleaner;
0046 friend class TWin32Thread;
0047 friend class TThreadTearDownGuard;
0048 friend class TJoinHelper;
0049 
0050 public:
0051 
0052    typedef void *(*VoidRtnFunc_t)(void *);
0053    typedef void  (*VoidFunc_t)(void *);
0054 
0055    enum EPriority {
0056       kLowPriority,
0057       kNormalPriority,
0058       kHighPriority
0059    };
0060 
0061    enum EState {
0062       kInvalidState,            // thread was not created properly
0063       kNewState,                // thread object exists but hasn't started
0064       kRunningState,            // thread is running
0065       kTerminatedState,         // thread has terminated but storage has not
0066                                 // yet been reclaimed (i.e. waiting to be joined)
0067       kFinishedState,           // thread has finished
0068       kCancelingState,          // thread in process of canceling
0069       kCanceledState,           // thread has been canceled
0070       kDeletingState            // thread in process of deleting
0071    };
0072 
0073 private:
0074    TThread       *fNext;                  // pointer to next thread
0075    TThread       *fPrev;                  // pointer to prev thread
0076    TThread      **fHolder;                // pointer to holder of this (delete only)
0077    EPriority      fPriority;              // thread priority
0078    EState         fState;                 // thread state
0079    EState         fStateComing;           // coming thread state
0080    Long_t         fId;                    // thread id
0081    Longptr_t      fHandle;                // Win32 thread handle
0082    Bool_t         fDetached;              // kTRUE if thread is Detached
0083    Bool_t         fNamed;                 // kTRUE if thread is Named
0084    VoidRtnFunc_t  fFcnRetn;               // void* start function of thread
0085    VoidFunc_t     fFcnVoid;               // void  start function of thread
0086    void          *fThreadArg;             // thread start function arguments
0087    void          *fClean;                 // support of cleanup structure
0088    char           fComment[100];          // thread specific state comment
0089 
0090    static TThreadImp      *fgThreadImp;   // static pointer to thread implementation
0091    static std::atomic<char  *> volatile fgXAct; // Action name to do by main thread
0092    static void ** volatile fgXArr;        // pointer to control array of void pointers for action
0093    static volatile Int_t   fgXAnb;        // size of array above
0094    static volatile Int_t   fgXArt;        // return XA flag
0095    static Long_t           fgMainId;      // thread id of main thread
0096    static TThread         *fgMain;        // pointer to chain of TThread's
0097    static TMutex          *fgMainMutex;   // mutex to protect chain of threads
0098    static TMutex          *fgXActMutex;   // mutex to protect XAction
0099    static TCondition      *fgXActCondi;   // condition for XAction
0100 
0101    // Private Member functions
0102    void           Constructor();
0103    void           SetComment(const char *txt = nullptr)
0104                      { fComment[0] = 0; if (txt) { strncpy(fComment, txt, 99); fComment[99] = 0; } }
0105    void           DoError(Int_t level, const char *location, const char *fmt, va_list va) const override;
0106    void           ErrorHandler(int level, const char *location, const char *fmt, va_list ap) const;
0107    static void    Init();
0108    static void   *Function(void *ptr);
0109    static Int_t   XARequest(const char *xact, Int_t nb, void **ar, Int_t *iret);
0110    static void    AfterCancel(TThread *th);
0111    static void    **GetTls(Int_t k);
0112 
0113    TThread(const TThread&) = delete;
0114    TThread& operator=(const TThread&) = delete;
0115 
0116 public:
0117    TThread(VoidRtnFunc_t fn, void *arg = nullptr, EPriority pri = kNormalPriority);
0118    TThread(VoidFunc_t fn, void *arg = nullptr, EPriority pri = kNormalPriority);
0119    TThread(const char *thname, VoidRtnFunc_t fn, void *arg = nullptr, EPriority pri = kNormalPriority);
0120    TThread(const char *thname, VoidFunc_t fn, void *arg = nullptr, EPriority pri = kNormalPriority);
0121    TThread(Long_t id = 0);
0122    virtual ~TThread();
0123 
0124    Int_t            Kill();
0125    Int_t            Run(void *arg = nullptr, const int affinity = -1);
0126    void             SetPriority(EPriority pri);
0127    void             Delete(Option_t *option="") override { TObject::Delete(option); }
0128    EPriority        GetPriority() const { return fPriority; }
0129    EState           GetState() const { return fState; }
0130    Long_t           GetId() const { return fId; }
0131    static void      Ps();
0132    static void      ps() { Ps(); }
0133 
0134    static void      Initialize();
0135    static Bool_t    IsInitialized();
0136 
0137    Long_t           Join(void **ret = nullptr);
0138    static Long_t    Join(Long_t id, void **ret = nullptr);
0139 
0140    static Int_t     Exit(void *ret = nullptr);
0141    static Int_t     Exists();
0142    static TThread  *GetThread(Long_t id);
0143    static TThread  *GetThread(const char *name);
0144 
0145    static Int_t     Lock();                  //User's lock of main mutex
0146    static Int_t     TryLock();               //User's try lock of main mutex
0147    static Int_t     UnLock();                //User's unlock of main mutex
0148    static TThread  *Self();
0149    static Long_t    SelfId();
0150    static Int_t     Sleep(ULong_t secs, ULong_t nanos = 0);
0151    static Int_t     GetTime(ULong_t *absSec, ULong_t *absNanoSec);
0152 
0153    static Int_t     Delete(TThread *&th);
0154    static void    **Tsd(void *dflt, Int_t k);
0155 
0156    // Cancellation
0157    // there are two types of TThread cancellation:
0158    //    DEFERRED     - Cancellation only in user provided cancel-points
0159    //    ASYNCHRONOUS - In any point
0160    //    DEFERRED is more safe, it is DEFAULT.
0161    static Int_t     SetCancelOn();
0162    static Int_t     SetCancelOff();
0163    static Int_t     SetCancelAsynchronous();
0164    static Int_t     SetCancelDeferred();
0165    static Int_t     CancelPoint();
0166    static Int_t     Kill(Long_t id);
0167    static Int_t     Kill(const char *name);
0168    static Int_t     CleanUpPush(void *free, void *arg = nullptr);
0169    static Int_t     CleanUpPop(Int_t exe = 0);
0170    static Int_t     CleanUp();
0171 
0172    // XActions
0173    static void      Printf(const char *fmt, ...)   // format and print
0174 #if defined(__GNUC__)
0175    __attribute__((format(printf, 1, 2)))
0176 #endif
0177    ;
0178    static void      XAction();
0179 
0180    ClassDefOverride(TThread,0)  // Thread class
0181 };
0182 
0183 
0184 //////////////////////////////////////////////////////////////////////////
0185 //                                                                      //
0186 // TThreadCleaner                                                       //
0187 //                                                                      //
0188 //////////////////////////////////////////////////////////////////////////
0189 
0190 class TThreadCleaner {
0191 public:
0192    TThreadCleaner() { }
0193    ~TThreadCleaner();
0194 };
0195 
0196 
0197 //////////////////////////////////////////////////////////////////////////
0198 //                                                                      //
0199 // TThreadTimer                                                         //
0200 //                                                                      //
0201 //////////////////////////////////////////////////////////////////////////
0202 
0203 class TThreadTimer : public TTimer {
0204 public:
0205    // if this time is less or equal to kItimerResolution, TUnixSystem::DispatchOneEvent i
0206    // can not exit and have its caller react to the other TTimer's actions (like the request
0207    // to stop the event loop) until there is another type of event.
0208    TThreadTimer(Long_t ms = kItimerResolution + 10);
0209    Bool_t Notify() override;
0210 };
0211 
0212 #endif