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