File indexing completed on 2025-01-18 10:12:29
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef ROOT_TThread
0013 #define ROOT_TThread
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
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,
0063 kNewState,
0064 kRunningState,
0065 kTerminatedState,
0066
0067 kFinishedState,
0068 kCancelingState,
0069 kCanceledState,
0070 kDeletingState
0071 };
0072
0073 private:
0074 TThread *fNext;
0075 TThread *fPrev;
0076 TThread **fHolder;
0077 EPriority fPriority;
0078 EState fState;
0079 EState fStateComing;
0080 Long_t fId;
0081 Longptr_t fHandle;
0082 Bool_t fDetached;
0083 Bool_t fNamed;
0084 VoidRtnFunc_t fFcnRetn;
0085 VoidFunc_t fFcnVoid;
0086 void *fThreadArg;
0087 void *fClean;
0088 char fComment[100];
0089
0090 static TThreadImp *fgThreadImp;
0091 static std::atomic<char *> volatile fgXAct;
0092 static void ** volatile fgXArr;
0093 static volatile Int_t fgXAnb;
0094 static volatile Int_t fgXArt;
0095 static Long_t fgMainId;
0096 static TThread *fgMain;
0097 static TMutex *fgMainMutex;
0098 static TMutex *fgXActMutex;
0099 static TCondition *fgXActCondi;
0100
0101
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();
0146 static Int_t TryLock();
0147 static Int_t UnLock();
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
0157
0158
0159
0160
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
0173 static void Printf(const char *fmt, ...)
0174 #if defined(__GNUC__)
0175 __attribute__((format(printf, 1, 2)))
0176 #endif
0177 ;
0178 static void XAction();
0179
0180 ClassDefOverride(TThread,0)
0181 };
0182
0183
0184
0185
0186
0187
0188
0189
0190 class TThreadCleaner {
0191 public:
0192 TThreadCleaner() { }
0193 ~TThreadCleaner();
0194 };
0195
0196
0197
0198
0199
0200
0201
0202
0203 class TThreadTimer : public TTimer {
0204 public:
0205
0206
0207
0208 TThreadTimer(Long_t ms = kItimerResolution + 10);
0209 Bool_t Notify() override;
0210 };
0211
0212 #endif