ROOT  6.06/09
Reference Guide
TThread.h
Go to the documentation of this file.
1 // @(#)root/thread:$Id$
2 // Author: Fons Rademakers 02/07/97
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TThread
13 #define ROOT_TThread
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TThread //
19 // //
20 // This class implements threads. A thread is an execution environment //
21 // much lighter than a process. A single process can have multiple //
22 // threads. The actual work is done via the TThreadImp class (either //
23 // TPosixThread or TWin32Thread). //
24 // //
25 //////////////////////////////////////////////////////////////////////////
26 
27 #ifndef ROOT_TObject
28 #include "TObject.h"
29 #endif
30 #ifndef ROOT_TMutex
31 #include "TMutex.h"
32 #endif
33 #ifndef ROOT_TCondition
34 #include "TCondition.h"
35 #endif
36 #ifndef ROOT_TSystem
37 #include "TSystem.h"
38 #endif
39 #ifndef ROOT_TTimer
40 #include "TTimer.h"
41 #endif
42 #ifndef ROOT_Varargs
43 #include "Varargs.h"
44 #endif
45 
46 class TThreadImp;
47 
48 
49 class TThread : public TNamed {
50 
51 friend class TThreadImp;
52 friend class TPosixThread;
53 friend class TThreadTimer;
54 friend class TThreadCleaner;
55 friend class TWin32Thread;
56 friend class TThreadTearDownGuard;
57 friend class TJoinHelper;
58 
59 public:
60 
61  typedef void *(*VoidRtnFunc_t)(void *);
62  typedef void (*VoidFunc_t)(void *);
63 
64  enum EPriority {
68  };
69 
70  enum EState {
71  kInvalidState, // thread was not created properly
72  kNewState, // thread object exists but hasn't started
73  kRunningState, // thread is running
74  kTerminatedState, // thread has terminated but storage has not
75  // yet been reclaimed (i.e. waiting to be joined)
76  kFinishedState, // thread has finished
77  kCancelingState, // thread in process of canceling
78  kCanceledState, // thread has been canceled
79  kDeletingState // thread in process of deleting
80  };
81 
82 private:
83  TThread *fNext; // pointer to next thread
84  TThread *fPrev; // pointer to prev thread
85  TThread **fHolder; // pointer to holder of this (delete only)
86  EPriority fPriority; // thread priority
87  EState fState; // thread state
88  EState fStateComing; // coming thread state
89  Long_t fId; // thread id
90  Long_t fHandle; // Win32 thread handle
91  Bool_t fDetached; // kTRUE if thread is Detached
92  Bool_t fNamed; // kTRUE if thread is Named
93  VoidRtnFunc_t fFcnRetn; // void* start function of thread
94  VoidFunc_t fFcnVoid; // void start function of thread
95  void *fThreadArg; // thread start function arguments
96  void *fClean; // support of cleanup structure
97  char fComment[100]; // thread specific state comment
98 
99  static TThreadImp *fgThreadImp; // static pointer to thread implementation
100  static char * volatile fgXAct; // Action name to do by main thread
101  static void ** volatile fgXArr; // pointer to control array of void pointers for action
102  static volatile Int_t fgXAnb; // size of array above
103  static volatile Int_t fgXArt; // return XA flag
104  static Long_t fgMainId; // thread id of main thread
105  static TThread *fgMain; // pointer to chain of TThread's
106  static TMutex *fgMainMutex; // mutex to protect chain of threads
107  static TMutex *fgXActMutex; // mutex to protect XAction
108  static TCondition *fgXActCondi; // condition for XAction
109 
110  // Private Member functions
111  void Constructor();
112  void SetComment(const char *txt = 0)
113  { fComment[0] = 0; if (txt) { strncpy(fComment, txt, 99); fComment[99] = 0; } }
114  void DoError(Int_t level, const char *location, const char *fmt, va_list va) const;
115  void ErrorHandler(int level, const char *location, const char *fmt, va_list ap) const;
116  static void Init();
117  static void *Function(void *ptr);
118  static Int_t XARequest(const char *xact, Int_t nb, void **ar, Int_t *iret);
119  static void AfterCancel(TThread *th);
120  static void **GetTls(Int_t k);
121 
122  TThread(const TThread&); // not implemented
123  TThread& operator=(const TThread&); // not implemented
124 
125 public:
126  TThread(VoidRtnFunc_t fn, void *arg = 0, EPriority pri = kNormalPriority);
127  TThread(VoidFunc_t fn, void *arg = 0, EPriority pri = kNormalPriority);
128  TThread(const char *thname, VoidRtnFunc_t fn, void *arg = 0, EPriority pri = kNormalPriority);
129  TThread(const char *thname, VoidFunc_t fn, void *arg = 0, EPriority pri = kNormalPriority);
130  TThread(Long_t id = 0);
131  virtual ~TThread();
132 
133  Int_t Kill();
134  Int_t Run(void *arg = 0);
135  void SetPriority(EPriority pri);
136  void Delete(Option_t *option="") { TObject::Delete(option); }
137  EPriority GetPriority() const { return fPriority; }
138  EState GetState() const { return fState; }
139  Long_t GetId() const { return fId; }
140  static void Ps();
141  static void ps() { Ps(); }
142 
143  static void Initialize();
144  static Bool_t IsInitialized();
145 
146  Long_t Join(void **ret = 0);
147  static Long_t Join(Long_t id, void **ret = 0);
148 
149  static Int_t Exit(void *ret = 0);
150  static Int_t Exists();
151  static TThread *GetThread(Long_t id);
152  static TThread *GetThread(const char *name);
153 
154  static Int_t Lock(); //User's lock of main mutex
155  static Int_t TryLock(); //User's try lock of main mutex
156  static Int_t UnLock(); //User's unlock of main mutex
157  static TThread *Self();
158  static Long_t SelfId();
159  static Int_t Sleep(ULong_t secs, ULong_t nanos = 0);
160  static Int_t GetTime(ULong_t *absSec, ULong_t *absNanoSec);
161 
162  static Int_t Delete(TThread *&th);
163  static void **Tsd(void *dflt, Int_t k);
164 
165  // Cancellation
166  // there are two types of TThread cancellation:
167  // DEFERRED - Cancellation only in user provided cancel-points
168  // ASYNCHRONOUS - In any point
169  // DEFERRED is more safe, it is DEFAULT.
170  static Int_t SetCancelOn();
171  static Int_t SetCancelOff();
172  static Int_t SetCancelAsynchronous();
173  static Int_t SetCancelDeferred();
174  static Int_t CancelPoint();
175  static Int_t Kill(Long_t id);
176  static Int_t Kill(const char *name);
177  static Int_t CleanUpPush(void *free, void *arg = 0);
178  static Int_t CleanUpPop(Int_t exe = 0);
179  static Int_t CleanUp();
180 
181  // XActions
182  static void Printf(const char *fmt, ...) // format and print
183 #if defined(__GNUC__) && !defined(__CINT__)
184  __attribute__((format(printf, 1, 2)))
185 #endif
186  ;
187  static void XAction();
188 
189  ClassDef(TThread,0) // Thread class
190 };
191 
192 
193 //////////////////////////////////////////////////////////////////////////
194 // //
195 // TThreadCleaner //
196 // //
197 //////////////////////////////////////////////////////////////////////////
198 
200 public:
202  ~TThreadCleaner();
203 };
204 
205 
206 //////////////////////////////////////////////////////////////////////////
207 // //
208 // TThreadTimer //
209 // //
210 //////////////////////////////////////////////////////////////////////////
211 
212 class TThreadTimer : public TTimer {
213 public:
214  // if this time is less or equal to kItimerResolution, TUnixSystem::DispatchOneEvent i
215  // can not exit and have its caller react to the other TTimer's actions (like the request
216  // to stop the event loop) until there is another type of event.
218  Bool_t Notify();
219 };
220 
221 #endif
static void ** GetTls(Int_t k)
Static method that initializes the TLS array of a thread and returns the reference to a given positio...
Definition: TThread.cxx:892
static void AfterCancel(TThread *th)
Static method which is called after the thread has been canceled.
Definition: TThread.cxx:714
static Int_t SetCancelDeferred()
Static method to set the cancellation response type of the calling thread to deferred, i.e.
Definition: TThread.cxx:652
Long_t GetId() const
Definition: TThread.h:139
Definition: TMutex.h:37
void *(* VoidRtnFunc_t)(void *)
Definition: TThread.h:61
static Bool_t IsInitialized()
Return true, if the TThread objects have been initialize.
Definition: TThread.cxx:304
TThread & operator=(const TThread &)
EState
Definition: TThread.h:70
friend class TJoinHelper
Definition: TThread.h:57
static void XAction()
Static method called via the thread timer to execute in the main thread certain commands.
Definition: TThread.cxx:1060
EState GetState() const
Definition: TThread.h:138
EState fStateComing
Definition: TThread.h:88
static TCondition * fgXActCondi
Definition: TThread.h:108
void ErrorHandler(int level, const char *location, const char *fmt, va_list ap) const
Thread specific error handler function.
Definition: TThread.cxx:945
static TThread * fgMain
Definition: TThread.h:105
friend class TThreadTearDownGuard
Definition: TThread.h:56
static void Initialize()
Initialize the Thread package.
Definition: TThread.cxx:295
const char Option_t
Definition: RtypesCore.h:62
void Constructor()
Common thread constructor.
Definition: TThread.cxx:352
void * fThreadArg
Definition: TThread.h:95
static Int_t SetCancelOff()
Static method to turn off thread cancellation.
Definition: TThread.cxx:623
EPriority GetPriority() const
Definition: TThread.h:137
static void Printf(const char *fmt,...)
Static method providing a thread safe printf. Appends a newline.
Definition: TThread.cxx:909
static Int_t SetCancelOn()
Static method to turn on thread cancellation.
Definition: TThread.cxx:632
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
static TThreadImp * fgThreadImp
Definition: TThread.h:99
TThread * fPrev
Definition: TThread.h:84
static Int_t TryLock()
Static method to try to lock the main thread mutex.
Definition: TThread.cxx:765
EState fState
Definition: TThread.h:87
static std::string format(double x, double y, int digits, int width)
static Int_t CleanUpPop(Int_t exe=0)
Static method which pops thread cleanup method off stack.
Definition: TThread.cxx:682
Long_t fHandle
Definition: TThread.h:90
static volatile Int_t fgXArt
Definition: TThread.h:103
TThread(const TThread &)
static Int_t SetCancelAsynchronous()
Static method to set the cancellation response type of the calling thread to asynchronous, i.e.
Definition: TThread.cxx:642
#define ClassDef(name, id)
Definition: Rtypes.h:254
Vc_ALWAYS_INLINE void free(T *p)
Frees memory that was allocated with Vc::malloc.
Definition: memory.h:94
void SetComment(const char *txt=0)
Definition: TThread.h:112
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
~TThreadCleaner()
Call user clean up routines.
Definition: TThread.cxx:1203
static Int_t XARequest(const char *xact, Int_t nb, void **ar, Int_t *iret)
Static method used to allow commands to be executed by the main thread.
Definition: TThread.cxx:1010
static Long_t SelfId()
Static method returning the id for the current thread.
Definition: TThread.cxx:537
void DoError(Int_t level, const char *location, const char *fmt, va_list va) const
Interface to ErrorHandler.
Definition: TThread.cxx:989
Int_t Run(void *arg=0)
Start the thread.
Definition: TThread.cxx:551
VoidFunc_t fFcnVoid
Definition: TThread.h:94
virtual ~TThread()
Cleanup the thread.
Definition: TThread.cxx:378
virtual void Delete(Option_t *option="")
Delete this object.
Definition: TObject.cxx:228
static TThread * GetThread(Long_t id)
Static method to find a thread by id.
Definition: TThread.cxx:451
EPriority fPriority
Definition: TThread.h:86
static TMutex * fgMainMutex
Definition: TThread.h:106
static Int_t Exists()
Static method to check if threads exist.
Definition: TThread.cxx:427
static void Init()
Initialize global state and variables once.
Definition: TThread.cxx:314
static char *volatile fgXAct
Definition: TThread.h:100
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:57
static Long_t fgMainId
Definition: TThread.h:104
static Int_t CleanUpPush(void *free, void *arg=0)
Static method which pushes thread cleanup method on stack.
Definition: TThread.cxx:670
static Int_t CancelPoint()
Static method to set a cancellation point.
Definition: TThread.cxx:661
Long_t Join(void **ret=0)
Join this thread.
Definition: TThread.cxx:498
Int_t Kill()
Kill this thread.
Definition: TThread.cxx:575
static Int_t Lock()
Static method to lock the main thread mutex.
Definition: TThread.cxx:757
VoidRtnFunc_t fFcnRetn
Definition: TThread.h:93
static void Ps()
Static method listing the existing threads.
Definition: TThread.cxx:828
static TMutex * fgXActMutex
Definition: TThread.h:107
Bool_t Notify()
Periodically execute the TThread::XAxtion() method in the main thread.
Definition: TThread.cxx:1185
static void **volatile fgXArr
Definition: TThread.h:101
static Int_t CleanUp()
Static method to cleanup the calling thread.
Definition: TThread.cxx:693
long Long_t
Definition: RtypesCore.h:50
static void ** Tsd(void *dflt, Int_t k)
Static method returning a pointer to thread specific data container of the calling thread...
Definition: TThread.cxx:879
static Int_t UnLock()
Static method to unlock the main thread mutex.
Definition: TThread.cxx:773
EPriority
Definition: TThread.h:64
static TThread * Self()
Static method returning pointer to current thread.
Definition: TThread.cxx:483
TThreadTimer(Long_t ms=kItimerResolution+10)
Create thread timer.
Definition: TThread.cxx:1177
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
unsigned long ULong_t
Definition: RtypesCore.h:51
void SetPriority(EPriority pri)
Set thread priority.
Definition: TThread.cxx:443
Long_t fId
Definition: TThread.h:89
void * fClean
Definition: TThread.h:96
static Int_t GetTime(ULong_t *absSec, ULong_t *absNanoSec)
Static method to get the current time.
Definition: TThread.cxx:746
#define name(a, b)
Definition: linkTestLib0.cpp:5
Bool_t fDetached
Definition: TThread.h:91
typedef void((*Func_t)())
Bool_t fNamed
Definition: TThread.h:92
static Int_t Exit(void *ret=0)
Static method which terminates the execution of the calling thread.
Definition: TThread.cxx:727
void(* VoidFunc_t)(void *)
Definition: TThread.h:62
TThread * fNext
Definition: TThread.h:83
static Int_t Sleep(ULong_t secs, ULong_t nanos=0)
Static method to sleep the calling thread.
Definition: TThread.cxx:735
static void ps()
Definition: TThread.h:141
TThread ** fHolder
Definition: TThread.h:85
static void * Function(void *ptr)
Static method which is called by the system thread function and which in turn calls the actual user f...
Definition: TThread.cxx:782
static volatile Int_t fgXAnb
Definition: TThread.h:102
void Delete(Option_t *option="")
Delete this object.
Definition: TThread.h:136