Logo ROOT  
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 #include "TNamed.h"
28 #include "TTimer.h"
29 #include <stdarg.h>
30 
31 #ifdef R__LESS_INCLUDES
32 class TCondition;
33 #else
34 #include "TCondition.h"
35 #endif
36 
37 class TMutex;
38 class TThreadImp;
39 
40 class TThread : public TNamed {
41 
42 friend class TThreadImp;
43 friend class TPosixThread;
44 friend class TThreadTimer;
45 friend class TThreadCleaner;
46 friend class TWin32Thread;
47 friend class TThreadTearDownGuard;
48 friend class TJoinHelper;
49 
50 public:
51 
52  typedef void *(*VoidRtnFunc_t)(void *);
53  typedef void (*VoidFunc_t)(void *);
54 
55  enum EPriority {
59  };
60 
61  enum EState {
62  kInvalidState, // thread was not created properly
63  kNewState, // thread object exists but hasn't started
64  kRunningState, // thread is running
65  kTerminatedState, // thread has terminated but storage has not
66  // yet been reclaimed (i.e. waiting to be joined)
67  kFinishedState, // thread has finished
68  kCancelingState, // thread in process of canceling
69  kCanceledState, // thread has been canceled
70  kDeletingState // thread in process of deleting
71  };
72 
73 private:
74  TThread *fNext; // pointer to next thread
75  TThread *fPrev; // pointer to prev thread
76  TThread **fHolder; // pointer to holder of this (delete only)
77  EPriority fPriority; // thread priority
78  EState fState; // thread state
79  EState fStateComing; // coming thread state
80  Long_t fId; // thread id
81  Long_t fHandle; // Win32 thread handle
82  Bool_t fDetached; // kTRUE if thread is Detached
83  Bool_t fNamed; // kTRUE if thread is Named
84  VoidRtnFunc_t fFcnRetn; // void* start function of thread
85  VoidFunc_t fFcnVoid; // void start function of thread
86  void *fThreadArg; // thread start function arguments
87  void *fClean; // support of cleanup structure
88  char fComment[100]; // thread specific state comment
89 
90  static TThreadImp *fgThreadImp; // static pointer to thread implementation
91  static char * volatile fgXAct; // Action name to do by main thread
92  static void ** volatile fgXArr; // pointer to control array of void pointers for action
93  static volatile Int_t fgXAnb; // size of array above
94  static volatile Int_t fgXArt; // return XA flag
95  static Long_t fgMainId; // thread id of main thread
96  static TThread *fgMain; // pointer to chain of TThread's
97  static TMutex *fgMainMutex; // mutex to protect chain of threads
98  static TMutex *fgXActMutex; // mutex to protect XAction
99  static TCondition *fgXActCondi; // condition for XAction
100 
101  // Private Member functions
102  void Constructor();
103  void SetComment(const char *txt = 0)
104  { fComment[0] = 0; if (txt) { strncpy(fComment, txt, 99); fComment[99] = 0; } }
105  void DoError(Int_t level, const char *location, const char *fmt, va_list va) const;
106  void ErrorHandler(int level, const char *location, const char *fmt, va_list ap) const;
107  static void Init();
108  static void *Function(void *ptr);
109  static Int_t XARequest(const char *xact, Int_t nb, void **ar, Int_t *iret);
110  static void AfterCancel(TThread *th);
111  static void **GetTls(Int_t k);
112 
113  TThread(const TThread&); // not implemented
114  TThread& operator=(const TThread&); // not implemented
115 
116 public:
117  TThread(VoidRtnFunc_t fn, void *arg = 0, EPriority pri = kNormalPriority);
118  TThread(VoidFunc_t fn, void *arg = 0, EPriority pri = kNormalPriority);
119  TThread(const char *thname, VoidRtnFunc_t fn, void *arg = 0, EPriority pri = kNormalPriority);
120  TThread(const char *thname, VoidFunc_t fn, void *arg = 0, EPriority pri = kNormalPriority);
121  TThread(Long_t id = 0);
122  virtual ~TThread();
123 
124  Int_t Kill();
125  Int_t Run(void *arg = 0);
126  void SetPriority(EPriority pri);
127  void Delete(Option_t *option="") { TObject::Delete(option); }
128  EPriority GetPriority() const { return fPriority; }
129  EState GetState() const { return fState; }
130  Long_t GetId() const { return fId; }
131  static void Ps();
132  static void ps() { Ps(); }
133 
134  static void Initialize();
135  static Bool_t IsInitialized();
136 
137  Long_t Join(void **ret = 0);
138  static Long_t Join(Long_t id, void **ret = 0);
139 
140  static Int_t Exit(void *ret = 0);
141  static Int_t Exists();
142  static TThread *GetThread(Long_t id);
143  static TThread *GetThread(const char *name);
144 
145  static Int_t Lock(); //User's lock of main mutex
146  static Int_t TryLock(); //User's try lock of main mutex
147  static Int_t UnLock(); //User's unlock of main mutex
148  static TThread *Self();
149  static Long_t SelfId();
150  static Int_t Sleep(ULong_t secs, ULong_t nanos = 0);
151  static Int_t GetTime(ULong_t *absSec, ULong_t *absNanoSec);
152 
153  static Int_t Delete(TThread *&th);
154  static void **Tsd(void *dflt, Int_t k);
155 
156  // Cancellation
157  // there are two types of TThread cancellation:
158  // DEFERRED - Cancellation only in user provided cancel-points
159  // ASYNCHRONOUS - In any point
160  // DEFERRED is more safe, it is DEFAULT.
161  static Int_t SetCancelOn();
162  static Int_t SetCancelOff();
163  static Int_t SetCancelAsynchronous();
164  static Int_t SetCancelDeferred();
165  static Int_t CancelPoint();
166  static Int_t Kill(Long_t id);
167  static Int_t Kill(const char *name);
168  static Int_t CleanUpPush(void *free, void *arg = 0);
169  static Int_t CleanUpPop(Int_t exe = 0);
170  static Int_t CleanUp();
171 
172  // XActions
173  static void Printf(const char *fmt, ...) // format and print
174 #if defined(__GNUC__) && !defined(__CINT__)
175  __attribute__((format(printf, 1, 2)))
176 #endif
177  ;
178  static void XAction();
179 
180  ClassDef(TThread,0) // Thread class
181 };
182 
183 
184 //////////////////////////////////////////////////////////////////////////
185 // //
186 // TThreadCleaner //
187 // //
188 //////////////////////////////////////////////////////////////////////////
189 
191 public:
193  ~TThreadCleaner();
194 };
195 
196 
197 //////////////////////////////////////////////////////////////////////////
198 // //
199 // TThreadTimer //
200 // //
201 //////////////////////////////////////////////////////////////////////////
202 
203 class TThreadTimer : public TTimer {
204 public:
205  // if this time is less or equal to kItimerResolution, TUnixSystem::DispatchOneEvent i
206  // can not exit and have its caller react to the other TTimer's actions (like the request
207  // to stop the event loop) until there is another type of event.
209  Bool_t Notify();
210 };
211 
212 #endif
TThread::fNext
TThread * fNext
Definition: TThread.h:74
TThread
Definition: TThread.h:40
TThread::TJoinHelper
friend class TJoinHelper
Definition: TThread.h:48
Option_t
const char Option_t
Definition: RtypesCore.h:66
TThread::Tsd
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:892
TThread::fFcnVoid
VoidFunc_t fFcnVoid
Definition: TThread.h:85
TThread::SetPriority
void SetPriority(EPriority pri)
Set thread priority.
Definition: TThread.cxx:456
TThread::ps
static void ps()
Definition: TThread.h:132
TThread::Exit
static Int_t Exit(void *ret=0)
Static method which terminates the execution of the calling thread.
Definition: TThread.cxx:740
TThread::DoError
void DoError(Int_t level, const char *location, const char *fmt, va_list va) const
Interface to ErrorHandler.
Definition: TThread.cxx:1002
TCondition.h
TThreadTimer::Notify
Bool_t Notify()
Periodically execute the TThread::XAxtion() method in the main thread.
Definition: TThread.cxx:1202
TNamed.h
TThread::GetThread
static TThread * GetThread(Long_t id)
Static method to find a thread by id.
Definition: TThread.cxx:464
TThread::fPrev
TThread * fPrev
Definition: TThread.h:75
TThread::fState
EState fState
Definition: TThread.h:78
TThread::fNamed
Bool_t fNamed
Definition: TThread.h:83
TThread::CancelPoint
static Int_t CancelPoint()
Static method to set a cancellation point.
Definition: TThread.cxx:674
TThread::TryLock
static Int_t TryLock()
Static method to try to lock the main thread mutex.
Definition: TThread.cxx:778
TThread::fgMainMutex
static TMutex * fgMainMutex
Definition: TThread.h:97
TThread::fPriority
EPriority fPriority
Definition: TThread.h:77
Int_t
int Int_t
Definition: RtypesCore.h:45
TThread::fStateComing
EState fStateComing
Definition: TThread.h:79
TCondition
Definition: TCondition.h:32
TThread::fgXArr
static void **volatile fgXArr
Definition: TThread.h:92
TThreadTimer
Definition: TThread.h:203
TThread::UnLock
static Int_t UnLock()
Static method to unlock the main thread mutex.
Definition: TThread.cxx:786
TThread::EState
EState
Definition: TThread.h:61
TThread::Ps
static void Ps()
Static method listing the existing threads.
Definition: TThread.cxx:841
TTimer.h
TThread::kCanceledState
@ kCanceledState
Definition: TThread.h:69
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
TThread::fgMain
static TThread * fgMain
Definition: TThread.h:96
bool
TThread::kLowPriority
@ kLowPriority
Definition: TThread.h:56
TThread::fgXAnb
static volatile Int_t fgXAnb
Definition: TThread.h:93
TThread::SetComment
void SetComment(const char *txt=0)
Definition: TThread.h:103
TThread::Delete
void Delete(Option_t *option="")
Delete this object.
Definition: TThread.h:127
TThread::Join
Long_t Join(void **ret=0)
Join this thread.
Definition: TThread.cxx:511
TThread::Run
Int_t Run(void *arg=0)
Start the thread.
Definition: TThread.cxx:564
TThread::~TThread
virtual ~TThread()
Cleanup the thread.
Definition: TThread.cxx:391
TThread::fgXActMutex
static TMutex * fgXActMutex
Definition: TThread.h:98
TThread::GetId
Long_t GetId() const
Definition: TThread.h:130
TThread::GetTls
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:905
TThread::SetCancelDeferred
static Int_t SetCancelDeferred()
Static method to set the cancellation response type of the calling thread to deferred,...
Definition: TThread.cxx:665
TThread::fComment
char fComment[100]
Definition: TThread.h:88
TTimer
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:51
TThread::kDeletingState
@ kDeletingState
Definition: TThread.h:70
TThread::CleanUpPush
static Int_t CleanUpPush(void *free, void *arg=0)
Static method which pushes thread cleanup method on stack.
Definition: TThread.cxx:683
TThread::TThreadTearDownGuard
friend class TThreadTearDownGuard
Definition: TThread.h:47
TThread::fId
Long_t fId
Definition: TThread.h:80
TThread::fgXActCondi
static TCondition * fgXActCondi
Definition: TThread.h:99
TThread::Self
static TThread * Self()
Static method returning pointer to current thread.
Definition: TThread.cxx:496
TThread::kNormalPriority
@ kNormalPriority
Definition: TThread.h:57
TThread::kTerminatedState
@ kTerminatedState
Definition: TThread.h:65
TThread::AfterCancel
static void AfterCancel(TThread *th)
Static method which is called after the thread has been canceled.
Definition: TThread.cxx:727
TThread::Function
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:795
TThread::SetCancelAsynchronous
static Int_t SetCancelAsynchronous()
Static method to set the cancellation response type of the calling thread to asynchronous,...
Definition: TThread.cxx:655
TNamed
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
TThread::fHolder
TThread ** fHolder
Definition: TThread.h:76
TThread::Exists
static Int_t Exists()
Static method to check if threads exist.
Definition: TThread.cxx:440
TThread::XAction
static void XAction()
Static method called via the thread timer to execute in the main thread certain commands.
Definition: TThread.cxx:1073
TThread::GetTime
static Int_t GetTime(ULong_t *absSec, ULong_t *absNanoSec)
Static method to get the current time.
Definition: TThread.cxx:759
Long_t
long Long_t
Definition: RtypesCore.h:54
TWin32Thread
Definition: TWin32Thread.h:30
TThread::fgXArt
static volatile Int_t fgXArt
Definition: TThread.h:94
kItimerResolution
@ kItimerResolution
Definition: Rtypes.h:62
TThread::fDetached
Bool_t fDetached
Definition: TThread.h:82
TThread::GetPriority
EPriority GetPriority() const
Definition: TThread.h:128
TThread::kInvalidState
@ kInvalidState
Definition: TThread.h:62
TThreadCleaner::~TThreadCleaner
~TThreadCleaner()
Call user clean up routines.
Definition: TThread.cxx:1220
TThread::fgXAct
static char *volatile fgXAct
Definition: TThread.h:91
TThread::SetCancelOn
static Int_t SetCancelOn()
Static method to turn on thread cancellation.
Definition: TThread.cxx:645
TThread::fFcnRetn
VoidRtnFunc_t fFcnRetn
Definition: TThread.h:84
ULong_t
unsigned long ULong_t
Definition: RtypesCore.h:55
TThread::operator=
TThread & operator=(const TThread &)
TThread::fgMainId
static Long_t fgMainId
Definition: TThread.h:95
TThread::ErrorHandler
void ErrorHandler(int level, const char *location, const char *fmt, va_list ap) const
Thread specific error handler function.
Definition: TThread.cxx:958
TThreadImp
Definition: TThreadImp.h:31
void
typedef void((*Func_t)())
TThread::Sleep
static Int_t Sleep(ULong_t secs, ULong_t nanos=0)
Static method to sleep the calling thread.
Definition: TThread.cxx:748
TThread::Initialize
static void Initialize()
Initialize the Thread package.
Definition: TThread.cxx:300
TThread::kHighPriority
@ kHighPriority
Definition: TThread.h:58
TThread::fThreadArg
void * fThreadArg
Definition: TThread.h:86
TThread::kCancelingState
@ kCancelingState
Definition: TThread.h:68
TThread::VoidFunc_t
void(* VoidFunc_t)(void *)
Definition: TThread.h:53
TThread::GetState
EState GetState() const
Definition: TThread.h:129
TThread::Init
static void Init()
Initialize global state and variables once.
Definition: TThread.cxx:319
TThread::SetCancelOff
static Int_t SetCancelOff()
Static method to turn off thread cancellation.
Definition: TThread.cxx:636
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TThread::fHandle
Long_t fHandle
Definition: TThread.h:81
TThread::VoidRtnFunc_t
void *(* VoidRtnFunc_t)(void *)
Definition: TThread.h:52
TThread::Printf
static void Printf(const char *fmt,...)
Static method providing a thread safe printf. Appends a newline.
Definition: TThread.cxx:922
TThread::IsInitialized
static Bool_t IsInitialized()
Return true, if the TThread objects have been initialize.
Definition: TThread.cxx:309
name
char name[80]
Definition: TGX11.cxx:110
TPosixThread
Definition: TPosixThread.h:35
TThread::kFinishedState
@ kFinishedState
Definition: TThread.h:67
TObject::Delete
virtual void Delete(Option_t *option="")
Delete this object.
Definition: TObject.cxx:171
TThread::SelfId
static Long_t SelfId()
Static method returning the id for the current thread.
Definition: TThread.cxx:550
TThread::fgThreadImp
static TThreadImp * fgThreadImp
Definition: TThread.h:90
TThread::EPriority
EPriority
Definition: TThread.h:55
TGeant4Unit::ms
static constexpr double ms
Definition: TGeant4SystemOfUnits.h:163
TThread::XARequest
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:1023
TThread::Lock
static Int_t Lock()
Static method to lock the main thread mutex.
Definition: TThread.cxx:770
TThread::kRunningState
@ kRunningState
Definition: TThread.h:64
free
#define free
Definition: civetweb.c:1539
TThread::Constructor
void Constructor()
Common thread constructor.
Definition: TThread.cxx:365
TThread::TThread
TThread(const TThread &)
TMutex
Definition: TMutex.h:30
TThreadTimer::TThreadTimer
TThreadTimer(Long_t ms=kItimerResolution+10)
Create thread timer.
Definition: TThread.cxx:1194
TThread::Kill
Int_t Kill()
Kill this thread.
Definition: TThread.cxx:588
TThreadCleaner
Definition: TThread.h:190
TThread::CleanUp
static Int_t CleanUp()
Static method to cleanup the calling thread.
Definition: TThread.cxx:706
TThread::kNewState
@ kNewState
Definition: TThread.h:63
int
TThread::fClean
void * fClean
Definition: TThread.h:87
TThread::CleanUpPop
static Int_t CleanUpPop(Int_t exe=0)
Static method which pops thread cleanup method off stack.
Definition: TThread.cxx:695
TThreadCleaner::TThreadCleaner
TThreadCleaner()
Definition: TThread.h:192