Logo ROOT  
Reference Guide
Go to the documentation of this file.
1// @(#)root/thread:$Id$
2// Author: Fons Rademakers 02/07/97
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 *************************************************************************/
12#ifndef ROOT_TThread
13#define ROOT_TThread
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// //
27#include "TObject.h"
28#include "TCondition.h"
29#include "TTimer.h"
30#include "Varargs.h"
32class TMutex;
33class TThreadImp;
36class TThread : public TNamed {
38friend class TThreadImp;
39friend class TPosixThread;
40friend class TThreadTimer;
41friend class TThreadCleaner;
42friend class TWin32Thread;
44friend class TJoinHelper;
48 typedef void *(*VoidRtnFunc_t)(void *);
49 typedef void (*VoidFunc_t)(void *);
51 enum EPriority {
55 };
57 enum EState {
58 kInvalidState, // thread was not created properly
59 kNewState, // thread object exists but hasn't started
60 kRunningState, // thread is running
61 kTerminatedState, // thread has terminated but storage has not
62 // yet been reclaimed (i.e. waiting to be joined)
63 kFinishedState, // thread has finished
64 kCancelingState, // thread in process of canceling
65 kCanceledState, // thread has been canceled
66 kDeletingState // thread in process of deleting
67 };
70 TThread *fNext; // pointer to next thread
71 TThread *fPrev; // pointer to prev thread
72 TThread **fHolder; // pointer to holder of this (delete only)
73 EPriority fPriority; // thread priority
74 EState fState; // thread state
75 EState fStateComing; // coming thread state
76 Long_t fId; // thread id
77 Long_t fHandle; // Win32 thread handle
78 Bool_t fDetached; // kTRUE if thread is Detached
79 Bool_t fNamed; // kTRUE if thread is Named
80 VoidRtnFunc_t fFcnRetn; // void* start function of thread
81 VoidFunc_t fFcnVoid; // void start function of thread
82 void *fThreadArg; // thread start function arguments
83 void *fClean; // support of cleanup structure
84 char fComment[100]; // thread specific state comment
86 static TThreadImp *fgThreadImp; // static pointer to thread implementation
87 static char * volatile fgXAct; // Action name to do by main thread
88 static void ** volatile fgXArr; // pointer to control array of void pointers for action
89 static volatile Int_t fgXAnb; // size of array above
90 static volatile Int_t fgXArt; // return XA flag
91 static Long_t fgMainId; // thread id of main thread
92 static TThread *fgMain; // pointer to chain of TThread's
93 static TMutex *fgMainMutex; // mutex to protect chain of threads
94 static TMutex *fgXActMutex; // mutex to protect XAction
95 static TCondition *fgXActCondi; // condition for XAction
97 // Private Member functions
98 void Constructor();
99 void SetComment(const char *txt = 0)
100 { fComment[0] = 0; if (txt) { strncpy(fComment, txt, 99); fComment[99] = 0; } }
101 void DoError(Int_t level, const char *location, const char *fmt, va_list va) const;
102 void ErrorHandler(int level, const char *location, const char *fmt, va_list ap) const;
103 static void Init();
104 static void *Function(void *ptr);
105 static Int_t XARequest(const char *xact, Int_t nb, void **ar, Int_t *iret);
106 static void AfterCancel(TThread *th);
107 static void **GetTls(Int_t k);
109 TThread(const TThread&); // not implemented
110 TThread& operator=(const TThread&); // not implemented
113 TThread(VoidRtnFunc_t fn, void *arg = 0, EPriority pri = kNormalPriority);
114 TThread(VoidFunc_t fn, void *arg = 0, EPriority pri = kNormalPriority);
115 TThread(const char *thname, VoidRtnFunc_t fn, void *arg = 0, EPriority pri = kNormalPriority);
116 TThread(const char *thname, VoidFunc_t fn, void *arg = 0, EPriority pri = kNormalPriority);
117 TThread(Long_t id = 0);
118 virtual ~TThread();
120 Int_t Kill();
121 Int_t Run(void *arg = 0);
122 void SetPriority(EPriority pri);
123 void Delete(Option_t *option="") { TObject::Delete(option); }
124 EPriority GetPriority() const { return fPriority; }
125 EState GetState() const { return fState; }
126 Long_t GetId() const { return fId; }
127 static void Ps();
128 static void ps() { Ps(); }
130 static void Initialize();
131 static Bool_t IsInitialized();
133 Long_t Join(void **ret = 0);
134 static Long_t Join(Long_t id, void **ret = 0);
136 static Int_t Exit(void *ret = 0);
137 static Int_t Exists();
138 static TThread *GetThread(Long_t id);
139 static TThread *GetThread(const char *name);
141 static Int_t Lock(); //User's lock of main mutex
142 static Int_t TryLock(); //User's try lock of main mutex
143 static Int_t UnLock(); //User's unlock of main mutex
144 static TThread *Self();
145 static Long_t SelfId();
146 static Int_t Sleep(ULong_t secs, ULong_t nanos = 0);
147 static Int_t GetTime(ULong_t *absSec, ULong_t *absNanoSec);
149 static Int_t Delete(TThread *&th);
150 static void **Tsd(void *dflt, Int_t k);
152 // Cancellation
153 // there are two types of TThread cancellation:
154 // DEFERRED - Cancellation only in user provided cancel-points
155 // ASYNCHRONOUS - In any point
156 // DEFERRED is more safe, it is DEFAULT.
157 static Int_t SetCancelOn();
158 static Int_t SetCancelOff();
160 static Int_t SetCancelDeferred();
161 static Int_t CancelPoint();
162 static Int_t Kill(Long_t id);
163 static Int_t Kill(const char *name);
164 static Int_t CleanUpPush(void *free, void *arg = 0);
165 static Int_t CleanUpPop(Int_t exe = 0);
166 static Int_t CleanUp();
168 // XActions
169 static void Printf(const char *fmt, ...) // format and print
170#if defined(__GNUC__) && !defined(__CINT__)
171 __attribute__((format(printf, 1, 2)))
173 ;
174 static void XAction();
176 ClassDef(TThread,0) // Thread class
181// //
182// TThreadCleaner //
183// //
194// //
195// TThreadTimer //
196// //
199class TThreadTimer : public TTimer {
201 // if this time is less or equal to kItimerResolution, TUnixSystem::DispatchOneEvent i
202 // can not exit and have its caller react to the other TTimer's actions (like the request
203 // to stop the event loop) until there is another type of event.
205 Bool_t Notify();
int Int_t
Definition: RtypesCore.h:43
unsigned long ULong_t
Definition: RtypesCore.h:53
long Long_t
Definition: RtypesCore.h:52
bool Bool_t
Definition: RtypesCore.h:61
const char Option_t
Definition: RtypesCore.h:64
#define ClassDef(name, id)
Definition: Rtypes.h:322
@ 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
Call user clean up routines.
Definition: TThread.cxx:1218
TThreadTimer(Long_t ms=kItimerResolution+10)
Create thread timer.
Definition: TThread.cxx:1192
Bool_t Notify()
Periodically execute the TThread::XAxtion() method in the main thread.
Definition: TThread.cxx:1200
VoidRtnFunc_t fFcnRetn
Definition: TThread.h:80
static TThread * GetThread(Long_t id)
Static method to find a thread by id.
Definition: TThread.cxx:462
static Int_t CleanUpPop(Int_t exe=0)
Static method which pops thread cleanup method off stack.
Definition: TThread.cxx:693
static char *volatile fgXAct
Definition: TThread.h:87
static Int_t CancelPoint()
Static method to set a cancellation point.
Definition: TThread.cxx:672
static Int_t Sleep(ULong_t secs, ULong_t nanos=0)
Static method to sleep the calling thread.
Definition: TThread.cxx:746
static void **volatile fgXArr
Definition: TThread.h:88
static Int_t TryLock()
Static method to try to lock the main thread mutex.
Definition: TThread.cxx:776
void SetPriority(EPriority pri)
Set thread priority.
Definition: TThread.cxx:454
static Int_t Exists()
Static method to check if threads exist.
Definition: TThread.cxx:438
static void ps()
Definition: TThread.h:128
TThread * fPrev
Definition: TThread.h:71
static void Ps()
Static method listing the existing threads.
Definition: TThread.cxx:839
char fComment[100]
Definition: TThread.h:84
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:903
static TThreadImp * fgThreadImp
Definition: TThread.h:86
static Int_t UnLock()
Static method to unlock the main thread mutex.
Definition: TThread.cxx:784
Int_t Kill()
Kill this thread.
Definition: TThread.cxx:586
static TMutex * fgMainMutex
Definition: TThread.h:93
EState fState
Definition: TThread.h:74
EState fStateComing
Definition: TThread.h:75
static volatile Int_t fgXAnb
Definition: TThread.h:89
Long_t fHandle
Definition: TThread.h:77
static Long_t fgMainId
Definition: TThread.h:91
EState GetState() const
Definition: TThread.h:125
static void XAction()
Static method called via the thread timer to execute in the main thread certain commands.
Definition: TThread.cxx:1071
virtual ~TThread()
Cleanup the thread.
Definition: TThread.cxx:389
EPriority GetPriority() const
Definition: TThread.h:124
Bool_t fDetached
Definition: TThread.h:78
static Int_t SetCancelAsynchronous()
Static method to set the cancellation response type of the calling thread to asynchronous,...
Definition: TThread.cxx:653
static Int_t CleanUp()
Static method to cleanup the calling thread.
Definition: TThread.cxx:704
void ErrorHandler(int level, const char *location, const char *fmt, va_list ap) const
Thread specific error handler function.
Definition: TThread.cxx:956
static Int_t GetTime(ULong_t *absSec, ULong_t *absNanoSec)
Static method to get the current time.
Definition: TThread.cxx:757
static Long_t SelfId()
Static method returning the id for the current thread.
Definition: TThread.cxx:548
void Constructor()
Common thread constructor.
Definition: TThread.cxx:363
static Int_t SetCancelOn()
Static method to turn on thread cancellation.
Definition: TThread.cxx:643
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:890
void DoError(Int_t level, const char *location, const char *fmt, va_list va) const
Interface to ErrorHandler.
Definition: TThread.cxx:1000
Long_t fId
Definition: TThread.h:76
static Int_t CleanUpPush(void *free, void *arg=0)
Static method which pushes thread cleanup method on stack.
Definition: TThread.cxx:681
Bool_t fNamed
Definition: TThread.h:79
static volatile Int_t fgXArt
Definition: TThread.h:90
Long_t GetId() const
Definition: TThread.h:126
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:1021
VoidFunc_t fFcnVoid
Definition: TThread.h:81
Long_t Join(void **ret=0)
Join this thread.
Definition: TThread.cxx:509
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:793
friend class TThreadTearDownGuard
Definition: TThread.h:43
EPriority fPriority
Definition: TThread.h:73
static void Printf(const char *fmt,...)
Static method providing a thread safe printf. Appends a newline.
Definition: TThread.cxx:920
static Int_t Lock()
Static method to lock the main thread mutex.
Definition: TThread.cxx:768
void(* VoidFunc_t)(void *)
Definition: TThread.h:49
Definition: TThread.h:57
@ kRunningState
Definition: TThread.h:60
@ kNewState
Definition: TThread.h:59
@ kCanceledState
Definition: TThread.h:65
@ kCancelingState
Definition: TThread.h:64
@ kFinishedState
Definition: TThread.h:63
@ kTerminatedState
Definition: TThread.h:61
@ kDeletingState
Definition: TThread.h:66
@ kInvalidState
Definition: TThread.h:58
static Int_t SetCancelOff()
Static method to turn off thread cancellation.
Definition: TThread.cxx:634
static void Init()
Initialize global state and variables once.
Definition: TThread.cxx:317
static Int_t SetCancelDeferred()
Static method to set the cancellation response type of the calling thread to deferred,...
Definition: TThread.cxx:663
static void AfterCancel(TThread *th)
Static method which is called after the thread has been canceled.
Definition: TThread.cxx:725
static TThread * fgMain
Definition: TThread.h:92
static TMutex * fgXActMutex
Definition: TThread.h:94
Definition: TThread.h:51
@ kHighPriority
Definition: TThread.h:54
@ kLowPriority
Definition: TThread.h:52
@ kNormalPriority
Definition: TThread.h:53
static Bool_t IsInitialized()
Return true, if the TThread objects have been initialize.
Definition: TThread.cxx:307
static TThread * Self()
Static method returning pointer to current thread.
Definition: TThread.cxx:494
TThread(const TThread &)
static void Initialize()
Initialize the Thread package.
Definition: TThread.cxx:298
void SetComment(const char *txt=0)
Definition: TThread.h:99
void * fClean
Definition: TThread.h:83
TThread & operator=(const TThread &)
static TCondition * fgXActCondi
Definition: TThread.h:95
friend class TJoinHelper
Definition: TThread.h:44
void Delete(Option_t *option="")
Delete this object.
Definition: TThread.h:123
static Int_t Exit(void *ret=0)
Static method which terminates the execution of the calling thread.
Definition: TThread.cxx:738
TThread ** fHolder
Definition: TThread.h:72
TThread * fNext
Definition: TThread.h:70
Int_t Run(void *arg=0)
Start the thread.
Definition: TThread.cxx:562
void *(* VoidRtnFunc_t)(void *)
Definition: TThread.h:48
void * fThreadArg
Definition: TThread.h:82
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:51
static constexpr double ms