Logo ROOT  
Reference Guide
TThread.cxx
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//////////////////////////////////////////////////////////////////////////
13// //
14// TThread //
15// //
16// This class implements threads. A thread is an execution environment //
17// much lighter than a process. A single process can have multiple //
18// threads. The actual work is done via the TThreadImp class (either //
19// TPosixThread or TWin32Thread). //
20// //
21//////////////////////////////////////////////////////////////////////////
22
23#include "RConfigure.h"
24
25#include "TThread.h"
26#include "TThreadImp.h"
27#include "TThreadFactory.h"
28#include "TROOT.h"
29#include "TCondition.h"
30#include "TApplication.h"
31#include "TVirtualPad.h"
32#include "TMethodCall.h"
33#include "TMutex.h"
34#include "TTimeStamp.h"
35#include "TInterpreter.h"
36#include "TError.h"
37#include "TSystem.h"
38#include "Varargs.h"
39#include "ThreadLocalStorage.h"
40#include "TThreadSlots.h"
41#include "TRWMutexImp.h"
42#include "snprintf.h"
43
46TThread *TThread::fgMain = nullptr;
48std::atomic<char *> volatile TThread::fgXAct{nullptr};
51void **volatile TThread::fgXArr = nullptr;
52volatile Int_t TThread::fgXAnb = 0;
53volatile Int_t TThread::fgXArt = 0;
54
55static void CINT_alloc_lock() { gGlobalMutex->Lock(); }
57
58static TMutex *gMainInternalMutex = nullptr;
59
62
64
65extern "C" void ROOT_TThread_Initialize()
66{
68};
69
70//------------------------------------------------------------------------------
71
72// Set gGlobalMutex to 0 when Thread library gets unloaded
74public:
77 // Note: we could insert here a wait for all thread to be finished.
78 // this is questionable though as we need to balance between fixing a
79 // user error (the thread was let lose and the caller did not explicit wait)
80 // and the risk that we can not terminate a failing process.
81
84 gGlobalMutex = 0;
85 delete m;
88 delete imp;
89 }
90};
92
93//------------------------------------------------------------------------------
94
96private:
99 void **fRet;
104
105 static void* JoinFunc(void *p);
106
107public:
108 TJoinHelper(TThread *th, void **ret);
109 ~TJoinHelper();
110
111 Int_t Join();
112};
113
114////////////////////////////////////////////////////////////////////////////////
115/// Constructor of Thread helper class.
116
118 : fT(th), fRet(ret), fRc(0), fM(new TMutex), fC(new TCondition(fM)), fJoined(kFALSE)
119{
120 fH = new TThread("JoinHelper", JoinFunc, this);
121}
122
123////////////////////////////////////////////////////////////////////////////////
124/// Destructor.
125
127{
128 delete fC;
129 delete fM;
130 delete fH;
131}
132
133////////////////////////////////////////////////////////////////////////////////
134/// Static method which runs in a separate thread to handle thread
135/// joins without blocking the main thread.
136/// Return a value (zero) so that it makes a joinable thread.
137
139{
140 TJoinHelper *jp = (TJoinHelper*)p;
141
142 jp->fRc = jp->fT->Join(jp->fRet);
143
144 jp->fM->Lock();
145 jp->fJoined = kTRUE;
146 jp->fC->Signal();
147 jp->fM->UnLock();
148
149 TThread::Exit(0);
150
151 return 0;
152}
153
154////////////////////////////////////////////////////////////////////////////////
155/// Thread join function.
156
158{
159 fM->Lock();
160 fH->Run();
161
162 while (kTRUE) {
163 // TimedWaitRelative will release the mutex (i.e. equivalent to fM->Unlock),
164 // then block on the condition variable. Upon return it will lock the mutex.
165 int r = fC->TimedWaitRelative(100); // 100 ms
166
167 // From the man page from pthread_ond_timedwait:
168
169 // When using condition variables there is always a Boolean predicate
170 // involving shared variables associated with each condition wait that
171 // is true if the thread should proceed. Spurious wakeups from the
172 // pthread_cond_timedwait() or pthread_cond_wait() functions may occur.
173 // Since the return from pthread_cond_timedwait() or pthread_cond_wait()
174 // does not imply anything about the value of this predicate, the
175 // predicate should be re-evaluated upon such return.
176
177 if (r == 0 || r == 1) {
178 // If we received the signal or timed out, let's check the value
179 if (fJoined) break;
180 } else {
181 // If any other error occured, there is no point in trying again
182 break;
183 }
184
186 }
187
188 fM->UnLock();
189
190 // And wait for the help to finish to avoid the risk that it is still
191 // running when the main tread is finished (and the thread library unloaded!)
193
194 return fRc;
195}
196
197
198//------------------------------------------------------------------------------
199
201
202
203////////////////////////////////////////////////////////////////////////////////
204/// Create a thread. Specify the function or static class method
205/// to be executed by the thread and a pointer to the argument structure.
206/// The user function should return a void*. To start the thread call Run().
207
208TThread::TThread(VoidRtnFunc_t fn, void *arg, EPriority pri)
209 : TNamed("<anon>", "")
210{
212 fFcnVoid = 0;
213 fFcnRetn = fn;
214 fPriority = pri;
215 fThreadArg = arg;
216 Constructor();
217 fNamed = kFALSE;
218}
219
220////////////////////////////////////////////////////////////////////////////////
221/// Create a detached thread. Specify the function or static class method
222/// to be executed by the thread and a pointer to the argument structure.
223/// To start the thread call Run().
224
225TThread::TThread(VoidFunc_t fn, void *arg, EPriority pri)
226 : TNamed("<anon>", "")
227{
229 fFcnRetn = 0;
230 fFcnVoid = fn;
231 fPriority = pri;
232 fThreadArg = arg;
233 Constructor();
234 fNamed = kFALSE;
235}
236
237////////////////////////////////////////////////////////////////////////////////
238/// Create thread with a name. Specify the function or static class method
239/// to be executed by the thread and a pointer to the argument structure.
240/// The user function should return a void*. To start the thread call Run().
241
242TThread::TThread(const char *thname, VoidRtnFunc_t fn, void *arg,
243 EPriority pri) : TNamed(thname, "")
244{
246 fFcnVoid = 0;
247 fFcnRetn = fn;
248 fPriority = pri;
249 fThreadArg = arg;
250 Constructor();
251 fNamed = kTRUE;
252}
253
254////////////////////////////////////////////////////////////////////////////////
255/// Create a detached thread with a name. Specify the function or static
256/// class method to be executed by the thread and a pointer to the argument
257/// structure. To start the thread call Run().
258
259TThread::TThread(const char *thname, VoidFunc_t fn, void *arg,
260 EPriority pri) : TNamed(thname, "")
261{
263 fFcnRetn = 0;
264 fFcnVoid = fn;
265 fPriority = pri;
266 fThreadArg = arg;
267 Constructor();
268 fNamed = kTRUE;
269}
270
271////////////////////////////////////////////////////////////////////////////////
272/// Create a TThread for a already running thread.
273
275{
277 fFcnRetn = 0;
278 fFcnVoid = 0;
280 fThreadArg = 0;
281 Constructor();
282
283 // Changing the id must be protected as it will be look at by multiple
284 // threads (see TThread::GetThread)
286 fNamed = kFALSE;
287 fId = (id ? id : SelfId());
290
291 if (gDebug)
292 Info("TThread::TThread", "TThread attached to running thread");
293}
294
295////////////////////////////////////////////////////////////////////////////////
296/// Initialize the Thread package. This initializes the TThread and ROOT
297/// global mutexes to make parts of ROOT thread safe/aware. This call is
298/// implicit in case a TThread is created.
299
301{
302 Init();
303}
304
305////////////////////////////////////////////////////////////////////////////////
306/// Return true, if the TThread objects have been initialize. If false,
307/// the process is (from ROOT's point of view) single threaded.
308
310{
311 if (fgThreadImp)
312 return kTRUE;
313 return kFALSE;
314}
315
316////////////////////////////////////////////////////////////////////////////////
317/// Initialize global state and variables once.
318
320{
321 if (fgThreadImp || fgIsTearDown) return;
322
323#if !defined (_REENTRANT) && !defined (WIN32)
324 // Not having it means (See TVirtualMutext.h) that the LOCKGUARD macro are empty.
325 ::Fatal("Init","_REENTRANT must be #define-d for TThread to work properly.");
326#endif
327
328 // 'Insure' gROOT is created before initializing the Thread safe behavior
329 // (to make sure we do not have two attempting to create it).
331
334
336 fgMainMutex = new TMutex(kTRUE);
339
340
341 // Create the single global mutex
343 // We need to make sure that gCling is initialized.
346
347 // To avoid deadlocks, gInterpreterMutex and gROOTMutex need
348 // to point at the same instance.
349 // Both are now deprecated in favor of ROOT::gCoreMutex
350 {
352 if (!ROOT::gCoreMutex) {
353 // To avoid dead locks, caused by shared library opening and/or static initialization
354 // taking the same lock as 'tls_get_addr_tail', we can not use UniqueLockRecurseCount.
355#ifdef R__HAS_TBB
357#else
359#endif
360 }
363 }
364}
365
366////////////////////////////////////////////////////////////////////////////////
367/// Common thread constructor.
368
370{
371 fHolder = 0;
372 fClean = 0;
374
375 fId = -1;
376 fHandle= 0;
377 if (!fgThreadImp) Init();
378
379 SetComment("Constructor: MainInternalMutex Locking");
381 SetComment("Constructor: MainInternalMutex Locked");
382
383 if (fgMain) fgMain->fPrev = this;
384 fNext = fgMain; fPrev = 0; fgMain = this;
385
387 SetComment();
388
389 // thread is set up in initialisation routine or Run().
390}
391
392////////////////////////////////////////////////////////////////////////////////
393/// Cleanup the thread.
394
396{
397 if (gDebug)
398 Info("TThread::~TThread", "thread deleted");
399
400 // Disconnect thread instance
401
402 SetComment("Destructor: MainInternalMutex Locking");
404 SetComment("Destructor: MainInternalMutex Locked");
405
406 if (fPrev) fPrev->fNext = fNext;
407 if (fNext) fNext->fPrev = fPrev;
408 if (fgMain == this) fgMain = fNext;
409
411 SetComment();
412 if (fHolder) *fHolder = 0;
413}
414
415////////////////////////////////////////////////////////////////////////////////
416/// Static method to delete the specified thread.
417/// Returns -1 in case the thread was running and has been killed. Returns
418/// 0 in case the thread has been Delete and Cleaned up. The th pointer is
419/// not valid anymore in that case.
420
422{
423 if (!th) return 0;
424 th->fHolder = &th;
425
426 if (th->fState == kRunningState) { // Cancel if running
428
429 if (gDebug)
430 th->Info("TThread::Delete", "deleting thread");
431
432 th->Kill();
433 return -1;
434 }
435
436 CleanUp();
437 return 0;
438}
439
440////////////////////////////////////////////////////////////////////////////////
441/// Static method to check if threads exist.
442/// Returns the number of running threads.
443
445{
447
448 Int_t num = 0;
449 for (TThread *l = fgMain; l; l = l->fNext)
450 num++; //count threads
451
453
454 return num;
455}
456
457////////////////////////////////////////////////////////////////////////////////
458/// Set thread priority.
459
461{
462 fPriority = pri;
463}
464
465////////////////////////////////////////////////////////////////////////////////
466/// Static method to find a thread by id.
467
469{
470 TThread *myTh;
471
473
474 for (myTh = fgMain; myTh && (myTh->fId != id); myTh = myTh->fNext) { }
475
477
478 return myTh;
479}
480
481////////////////////////////////////////////////////////////////////////////////
482/// Static method to find a thread by name.
483
485{
486 TThread *myTh;
487
489
490 for (myTh = fgMain; myTh && (strcmp(name, myTh->GetName())); myTh = myTh->fNext) { }
491
493
494 return myTh;
495}
496
497////////////////////////////////////////////////////////////////////////////////
498/// Static method returning pointer to current thread.
499
501{
502 TTHREAD_TLS(TThread*) self = 0;
503
504 if (!self || fgIsTearDown) {
505 if (fgIsTearDown) self = 0;
506 self = GetThread(SelfId());
507 }
508 return self;
509}
510
511
512////////////////////////////////////////////////////////////////////////////////
513/// Join this thread.
514
516{
517 if (fId == -1) {
518 Error("Join", "thread not running");
519 return -1;
520 }
521
522 if (fDetached) {
523 Error("Join", "cannot join detached thread");
524 return -1;
525 }
526
527 if (SelfId() != fgMainId)
528 return fgThreadImp->Join(this, ret);
529
530 // do not block the main thread, use helper thread
531 TJoinHelper helper(this, ret);
532
533 return helper.Join();
534}
535
536////////////////////////////////////////////////////////////////////////////////
537/// Static method to join a thread by id.
538
540{
541 TThread *myTh = GetThread(jid);
542
543 if (!myTh) {
544 ::Error("TThread::Join", "cannot find thread 0x%lx", jid);
545 return -1L;
546 }
547
548 return myTh->Join(ret);
549}
550
551////////////////////////////////////////////////////////////////////////////////
552/// Static method returning the id for the current thread.
553
555{
556 if (fgIsTearDown) return -1;
557 if (!fgThreadImp) Init();
558
559 return fgThreadImp->SelfId();
560}
561
562////////////////////////////////////////////////////////////////////////////////
563/// Start the thread. This starts the static method TThread::Function()
564/// which calls the user function specified in the TThread ctor with
565/// the arg argument.
566/// If affinity is specified (>=0), a CPU affinity will be associated
567/// with the current thread.
568/// Returns 0 on success, otherwise an error number will
569/// be returned.
570
571Int_t TThread::Run(void *arg, const int affinity)
572{
573 if (arg) fThreadArg = arg;
574
575 SetComment("Run: MainInternalMutex locking");
577 SetComment("Run: MainMutex locked");
578
579 int iret = fgThreadImp->Run(this, affinity);
580
582
583 if (gDebug)
584 Info("TThread::Run", "thread run requested");
585
587 SetComment();
588 return iret;
589}
590
591////////////////////////////////////////////////////////////////////////////////
592/// Kill this thread. Returns 0 on success, otherwise an error number will
593/// be returned.
594
596{
598 if (gDebug)
599 Warning("TThread::Kill", "thread is not running");
600 return 13;
601 } else {
603 return fgThreadImp->Kill(this);
604 }
605}
606
607////////////////////////////////////////////////////////////////////////////////
608/// Static method to kill the thread by id. Returns 0 on success, otherwise
609/// an error number will be returned.
610
612{
613 TThread *th = GetThread(id);
614 if (th) {
615 return fgThreadImp->Kill(th);
616 } else {
617 if (gDebug)
618 ::Warning("TThread::Kill(Long_t)", "thread 0x%lx not found", id);
619 return 13;
620 }
621}
622
623////////////////////////////////////////////////////////////////////////////////
624/// Static method to kill thread by name. Returns 0 on success, otherwise
625/// an error number will be returned.
626
628{
629 TThread *th = GetThread(name);
630 if (th) {
631 return fgThreadImp->Kill(th);
632 } else {
633 if (gDebug)
634 ::Warning("TThread::Kill(const char*)", "thread %s not found", name);
635 return 13;
636 }
637}
638
639////////////////////////////////////////////////////////////////////////////////
640/// Static method to turn off thread cancellation. Returns 0 on success,
641/// otherwise an error number will be returned.
642
644{
645 return fgThreadImp ? fgThreadImp->SetCancelOff() : -1;
646}
647
648////////////////////////////////////////////////////////////////////////////////
649/// Static method to turn on thread cancellation. Returns 0 on success,
650/// otherwise an error number will be returned.
651
653{
654 return fgThreadImp ? fgThreadImp->SetCancelOn() : -1;
655}
656
657////////////////////////////////////////////////////////////////////////////////
658/// Static method to set the cancellation response type of the calling thread
659/// to asynchronous, i.e. cancel as soon as the cancellation request
660/// is received.
661
663{
665}
666
667////////////////////////////////////////////////////////////////////////////////
668/// Static method to set the cancellation response type of the calling thread
669/// to deferred, i.e. cancel only at next cancellation point.
670/// Returns 0 on success, otherwise an error number will be returned.
671
673{
675}
676
677////////////////////////////////////////////////////////////////////////////////
678/// Static method to set a cancellation point. Returns 0 on success, otherwise
679/// an error number will be returned.
680
682{
683 return fgThreadImp ? fgThreadImp->CancelPoint() : -1;
684}
685
686////////////////////////////////////////////////////////////////////////////////
687/// Static method which pushes thread cleanup method on stack.
688/// Returns 0 in case of success and -1 in case of error.
689
691{
692 TThread *th = Self();
693 if (th)
694 return fgThreadImp->CleanUpPush(&(th->fClean), free, arg);
695 return -1;
696}
697
698////////////////////////////////////////////////////////////////////////////////
699/// Static method which pops thread cleanup method off stack.
700/// Returns 0 in case of success and -1 in case of error.
701
703{
704 TThread *th = Self();
705 if (th)
706 return fgThreadImp->CleanUpPop(&(th->fClean), exe);
707 return -1;
708}
709
710////////////////////////////////////////////////////////////////////////////////
711/// Static method to cleanup the calling thread.
712
714{
715 TThread *th = Self();
716 if (!th) return 13;
717
718 fgThreadImp->CleanUp(&(th->fClean));
720 if (fgXActMutex)
722
724
725 if (th->fHolder)
726 delete th;
727
728 return 0;
729}
730
731////////////////////////////////////////////////////////////////////////////////
732/// Static method which is called after the thread has been canceled.
733
735{
736 if (th) {
738 if (gDebug)
739 th->Info("TThread::AfterCancel", "thread is canceled");
740 } else
741 ::Error("TThread::AfterCancel", "zero thread pointer passed");
742}
743
744////////////////////////////////////////////////////////////////////////////////
745/// Static method which terminates the execution of the calling thread.
746
748{
749 return fgThreadImp ? fgThreadImp->Exit(ret) : -1;
750}
751
752////////////////////////////////////////////////////////////////////////////////
753/// Static method to sleep the calling thread.
754
756{
757 UInt_t ms = UInt_t(secs * 1000) + UInt_t(nanos / 1000000);
758 if (gSystem) gSystem->Sleep(ms);
759 return 0;
760}
761
762////////////////////////////////////////////////////////////////////////////////
763/// Static method to get the current time. Returns
764/// the number of seconds.
765
767{
768 TTimeStamp t;
769 if (absSec) *absSec = t.GetSec();
770 if (absNanoSec) *absNanoSec = t.GetNanoSec();
771 return t.GetSec();
772}
773
774////////////////////////////////////////////////////////////////////////////////
775/// Static method to lock the main thread mutex.
776
778{
779 return (fgMainMutex ? fgMainMutex->Lock() : 0);
780}
781
782////////////////////////////////////////////////////////////////////////////////
783/// Static method to try to lock the main thread mutex.
784
786{
787 return (fgMainMutex ? fgMainMutex->TryLock() : 0);
788}
789
790////////////////////////////////////////////////////////////////////////////////
791/// Static method to unlock the main thread mutex.
792
794{
795 return (fgMainMutex ? fgMainMutex->UnLock() : 0);
796}
797
798////////////////////////////////////////////////////////////////////////////////
799/// Static method which is called by the system thread function and
800/// which in turn calls the actual user function.
801
802void *TThread::Function(void *ptr)
803{
804 TThread *th;
805 void *ret, *arg;
806
807 TThreadCleaner dummy;
808
809 th = (TThread *)ptr;
810
811 // Default cancel state is OFF
812 // Default cancel type is DEFERRED
813 // User can change it by call SetCancelOn() and SetCancelAsynchronous()
814 SetCancelOff();
816 CleanUpPush((void *)&AfterCancel, th); // Enable standard cancelling function
817
818 if (gDebug)
819 th->Info("TThread::Function", "thread is running");
820
821 arg = th->fThreadArg;
822 th->fState = kRunningState;
823
824 if (th->fDetached) {
825 //Detached, non joinable thread
826 (th->fFcnVoid)(arg);
827 ret = 0;
829 } else {
830 //UnDetached, joinable thread
831 ret = (th->fFcnRetn)(arg);
833 }
834
835 CleanUpPop(1); // Disable standard canceling function
836
837 if (gDebug)
838 th->Info("TThread::Function", "thread has finished");
839
840 TThread::Exit(ret);
841
842 return ret;
843}
844
845////////////////////////////////////////////////////////////////////////////////
846/// Static method listing the existing threads.
847
849{
850 TThread *l;
851 int i;
852
853 if (!fgMain) {
854 ::Info("TThread::Ps", "no threads have been created");
855 return;
856 }
857
859
860 int num = 0;
861 for (l = fgMain; l; l = l->fNext)
862 num++;
863
864 char cbuf[256];
865 printf(" Thread State\n");
866 for (l = fgMain; l; l = l->fNext) { // loop over threads
867 memset(cbuf, ' ', sizeof(cbuf));
868 snprintf(cbuf, sizeof(cbuf), "%3d %s:0x%lx", num--, l->GetName(), l->fId);
869 i = (int)strlen(cbuf);
870 if (i < 30)
871 cbuf[i] = ' ';
872 cbuf[30] = 0;
873 printf("%30s", cbuf);
874
875 switch (l->fState) { // print states
876 case kNewState: printf("Idle "); break;
877 case kRunningState: printf("Running "); break;
878 case kTerminatedState: printf("Terminated "); break;
879 case kFinishedState: printf("Finished "); break;
880 case kCancelingState: printf("Canceling "); break;
881 case kCanceledState: printf("Canceled "); break;
882 case kDeletingState: printf("Deleting "); break;
883 default: printf("Invalid ");
884 }
885 if (l->fComment[0]) printf(" // %s", l->fComment);
886 printf("\n");
887 } // end of loop
888
890}
891
892////////////////////////////////////////////////////////////////////////////////
893/// Static method returning a pointer to thread specific data container
894/// of the calling thread.
895/// k should be between 0 and kMaxUserThreadSlot for user application.
896/// (and between kMaxUserThreadSlot and kMaxThreadSlot for ROOT libraries).
897/// See ROOT::EThreadSlotReservation
898
899void **TThread::Tsd(void *dflt, Int_t k)
900{
901 if (TThread::SelfId() == fgMainId) { //Main thread
902 return (void**)dflt;
903 } else {
904 return GetTls(k);
905 }
906}
907
908////////////////////////////////////////////////////////////////////////////////
909/// Static method that initializes the TLS array of a thread and returns the
910/// reference to a given position in that array.
911
913 TTHREAD_TLS_ARRAY(void*, ROOT::kMaxThreadSlot, tls);
914
915 // In order for the thread 'gDirectory' value to be properly
916 // initialized we set it now (otherwise it defaults
917 // to zero which is 'unexpected')
918 // We initialize it to gROOT rather than gDirectory, since
919 // TFile are currently expected to not be shared by two threads.
920 if (k == ROOT::kDirectoryThreadSlot && tls[k] == nullptr)
921 tls[k] = gROOT;
922
923 return &(tls[k]);
924}
925
926////////////////////////////////////////////////////////////////////////////////
927/// Static method providing a thread safe printf. Appends a newline.
928
929void TThread::Printf(const char *va_(fmt), ...)
930{
931 va_list ap;
932 va_start(ap,va_(fmt));
933
934 Int_t buf_size = 2048;
935 char *buf;
936
937again:
938 buf = new char[buf_size];
939
940 int n = vsnprintf(buf, buf_size, va_(fmt), ap);
941 // old vsnprintf's return -1 if string is truncated new ones return
942 // total number of characters that would have been written
943 if (n == -1 || n >= buf_size) {
944 buf_size *= 2;
945 delete [] buf;
946 goto again;
947 }
948
949 va_end(ap);
950
951 void *arr[2];
952 arr[1] = (void*) buf;
953 if (XARequest("PRTF", 2, arr, 0)) {
954 delete [] buf;
955 return;
956 }
957
958 printf("%s\n", buf);
959 fflush(stdout);
960
961 delete [] buf;
962}
963
964////////////////////////////////////////////////////////////////////////////////
965/// Thread specific error handler function.
966/// It calls the user set error handler in the main thread.
967
968void TThread::ErrorHandler(int level, const char *location, const char *fmt,
969 va_list ap) const
970{
971 Int_t buf_size = 2048;
972 char *buf, *bp;
973
974again:
975 buf = new char[buf_size];
976
977 int n = vsnprintf(buf, buf_size, fmt, ap);
978 // old vsnprintf's return -1 if string is truncated new ones return
979 // total number of characters that would have been written
980 if (n == -1 || n >= buf_size) {
981 buf_size *= 2;
982 delete [] buf;
983 goto again;
984 }
985 if (level >= kSysError && level < kFatal) {
986 char *buf1 = new char[buf_size + strlen(gSystem->GetError()) + 5];
987 sprintf(buf1, "%s (%s)", buf, gSystem->GetError());
988 bp = buf1;
989 delete [] buf;
990 } else
991 bp = buf;
992
993 void *arr[4];
994 arr[1] = (void*) Longptr_t(level);
995 arr[2] = (void*) location;
996 arr[3] = (void*) bp;
997 if (XARequest("ERRO", 4, arr, 0)) return;
998
999 if (level != kFatal)
1000 ::GetErrorHandler()(level, level >= gErrorAbortLevel, location, bp);
1001 else
1002 ::GetErrorHandler()(level, kTRUE, location, bp);
1003
1004 delete [] bp;
1005}
1006
1007////////////////////////////////////////////////////////////////////////////////
1008/// Interface to ErrorHandler. User has to specify the class name as
1009/// part of the location, just like for the global Info(), Warning() and
1010/// Error() functions.
1011
1012void TThread::DoError(int level, const char *location, const char *fmt,
1013 va_list va) const
1014{
1015 char *loc = 0;
1016
1017 if (location) {
1018 loc = new char[strlen(location) + strlen(GetName()) + 32];
1019 sprintf(loc, "%s %s:0x%lx", location, GetName(), fId);
1020 } else {
1021 loc = new char[strlen(GetName()) + 32];
1022 sprintf(loc, "%s:0x%lx", GetName(), fId);
1023 }
1024
1025 ErrorHandler(level, loc, fmt, va);
1026
1027 delete [] loc;
1028}
1029
1030////////////////////////////////////////////////////////////////////////////////
1031/// Static method used to allow commands to be executed by the main thread.
1032
1033Int_t TThread::XARequest(const char *xact, Int_t nb, void **ar, Int_t *iret)
1034{
1035 if (!gApplication || !gApplication->IsRunning()) return 0;
1036
1037 // The first time, create the related static vars
1038 if (!fgXActMutex && gGlobalMutex) {
1039 gGlobalMutex->Lock();
1040 if (!fgXActMutex) {
1041 fgXActMutex = new TMutex(kTRUE);
1042 fgXActCondi = new TCondition;
1043 new TThreadTimer;
1044 }
1046 }
1047
1048 TThread *th = Self();
1049 if (th && th->fId != fgMainId) { // we are in the thread
1050 th->SetComment("XARequest: XActMutex Locking");
1051 fgXActMutex->Lock();
1052 th->SetComment("XARequest: XActMutex Locked");
1053
1055 TMutexImp *condmutex = fgXActCondi->GetMutex()->fMutexImp;
1056
1057 // Lock now, so the XAction signal will wait
1058 // and never come before the wait
1059 condmutex->Lock();
1060
1061 fgXAnb = nb;
1062 fgXArr = ar;
1063 fgXArt = 0;
1064 fgXAct = (char*) xact;
1065 th->SetComment(fgXAct);
1066
1067 if (condimp) condimp->Wait();
1068 condmutex->UnLock();
1069
1070 if (iret) *iret = fgXArt;
1072 th->SetComment();
1073 return 1997;
1074 } else //we are in the main thread
1075 return 0;
1076}
1077
1078////////////////////////////////////////////////////////////////////////////////
1079/// Static method called via the thread timer to execute in the main
1080/// thread certain commands. This to avoid sophisticated locking and
1081/// possible deadlocking.
1082
1084{
1086 TMutexImp *condmutex = fgXActCondi->GetMutex()->fMutexImp;
1087 condmutex->Lock();
1088
1089 char const acts[] = "PRTF CUPD CANV CDEL PDCD METH ERRO";
1090 enum { kPRTF = 0, kCUPD = 5, kCANV = 10, kCDEL = 15,
1091 kPDCD = 20, kMETH = 25, kERRO = 30 };
1092 int iact = strstr(acts, fgXAct) - acts;
1093 char *cmd = 0;
1094
1095 switch (iact) {
1096
1097 case kPRTF:
1098 printf("%s\n", (const char*)fgXArr[1]);
1099 fflush(stdout);
1100 break;
1101
1102 case kERRO:
1103 {
1104 int level = (int)Longptr_t(fgXArr[1]);
1105 const char *location = (const char*)fgXArr[2];
1106 char *mess = (char*)fgXArr[3];
1107 if (level != kFatal)
1108 GetErrorHandler()(level, level >= gErrorAbortLevel, location, mess);
1109 else
1110 GetErrorHandler()(level, kTRUE, location, mess);
1111 delete [] mess;
1112 }
1113 break;
1114
1115 case kCUPD:
1116 //((TCanvas *)fgXArr[1])->Update();
1117 union CastFromFuncToVoidPtr_t {
1118 void (*fFuncPtr)(void*);
1119 void* fVoidPtr;
1120 } castFromFuncToVoidPtr;
1121 castFromFuncToVoidPtr.fVoidPtr = fgXArr[2];
1122 (*castFromFuncToVoidPtr.fFuncPtr)(fgXArr[1]); // aka TCanvas::Update()
1123 break;
1124
1125 case kCANV:
1126
1127 switch(fgXAnb) { // Over TCanvas constructors
1128
1129 case 2:
1130 //((TCanvas*)fgXArr[1])->Constructor();
1131 cmd = Form("((TCanvas *)0x%zx)->Constructor();",(size_t)fgXArr[1]);
1132 gROOT->ProcessLine(cmd);
1133 break;
1134
1135 case 5:
1136 //((TCanvas*)fgXArr[1])->Constructor(
1137 // (char*)fgXArr[2],
1138 // (char*)fgXArr[3],
1139 // *((Int_t*)(fgXArr[4])));
1140 cmd = Form("((TCanvas *)0x%zx)->Constructor((char*)0x%zx,(char*)0x%zx,*((Int_t*)(0x%zx)));",(size_t)fgXArr[1],(size_t)fgXArr[2],(size_t)fgXArr[3],(size_t)fgXArr[4]);
1141 gROOT->ProcessLine(cmd);
1142 break;
1143 case 6:
1144 //((TCanvas*)fgXArr[1])->Constructor(
1145 // (char*)fgXArr[2],
1146 // (char*)fgXArr[3],
1147 // *((Int_t*)(fgXArr[4])),
1148 // *((Int_t*)(fgXArr[5])));
1149 cmd = Form("((TCanvas *)0x%zx)->Constructor((char*)0x%zx,(char*)0x%zx,*((Int_t*)(0x%zx)),*((Int_t*)(0x%zx)));",(size_t)fgXArr[1],(size_t)fgXArr[2],(size_t)fgXArr[3],(size_t)fgXArr[4],(size_t)fgXArr[5]);
1150 gROOT->ProcessLine(cmd);
1151 break;
1152
1153 case 8:
1154 //((TCanvas*)fgXArr[1])->Constructor(
1155 // (char*)fgXArr[2],
1156 // (char*)fgXArr[3],
1157 // *((Int_t*)(fgXArr[4])),
1158 // *((Int_t*)(fgXArr[5])),
1159 // *((Int_t*)(fgXArr[6])),
1160 // *((Int_t*)(fgXArr[7])));
1161 cmd = Form("((TCanvas *)0x%zx)->Constructor((char*)0x%zx,(char*)0x%zx,*((Int_t*)(0x%zx)),*((Int_t*)(0x%zx)),*((Int_t*)(0x%zx)),*((Int_t*)(0x%zx)));",(size_t)fgXArr[1],(size_t)fgXArr[2],(size_t)fgXArr[3],(size_t)fgXArr[4],(size_t)fgXArr[5],(size_t)fgXArr[6],(size_t)fgXArr[7]);
1162 gROOT->ProcessLine(cmd);
1163 break;
1164
1165 }
1166 break;
1167
1168 case kCDEL:
1169 //((TCanvas*)fgXArr[1])->Destructor();
1170 cmd = Form("((TCanvas *)0x%zx)->Destructor();",(size_t)fgXArr[1]);
1171 gROOT->ProcessLine(cmd);
1172 break;
1173
1174 case kPDCD:
1175 ((TVirtualPad*) fgXArr[1])->Divide( *((Int_t*)(fgXArr[2])),
1176 *((Int_t*)(fgXArr[3])),
1177 *((Float_t*)(fgXArr[4])),
1178 *((Float_t*)(fgXArr[5])),
1179 *((Int_t*)(fgXArr[6])));
1180 break;
1181 case kMETH:
1182 ((TMethodCall *) fgXArr[1])->Execute((void*)(fgXArr[2]),(const char*)(fgXArr[3]));
1183 break;
1184
1185 default:
1186 ::Error("TThread::XAction", "wrong case");
1187 }
1188
1189 fgXAct = 0;
1190 if (condimp) condimp->Signal();
1191 condmutex->UnLock();
1192}
1193
1194
1195//////////////////////////////////////////////////////////////////////////
1196// //
1197// TThreadTimer //
1198// //
1199//////////////////////////////////////////////////////////////////////////
1200
1201////////////////////////////////////////////////////////////////////////////////
1202/// Create thread timer.
1203
1205{
1206 gSystem->AddTimer(this);
1207}
1208
1209////////////////////////////////////////////////////////////////////////////////
1210/// Periodically execute the TThread::XAction() method in the main thread.
1211
1213{
1215 Reset();
1216
1217 return kFALSE;
1218}
1219
1220
1221//////////////////////////////////////////////////////////////////////////
1222// //
1223// TThreadCleaner //
1224// //
1225//////////////////////////////////////////////////////////////////////////
1226
1227////////////////////////////////////////////////////////////////////////////////
1228/// Call user clean up routines.
1229
1231{
1233}
typedef void(GLAPIENTRYP _GLUfuncptr)(void)
ROOT::R::TRInterface & r
Definition: Object.C:4
int Int_t
Definition: RtypesCore.h:45
long Longptr_t
Definition: RtypesCore.h:82
unsigned int UInt_t
Definition: RtypesCore.h:46
const Bool_t kFALSE
Definition: RtypesCore.h:101
unsigned long ULong_t
Definition: RtypesCore.h:55
long Long_t
Definition: RtypesCore.h:54
bool Bool_t
Definition: RtypesCore.h:63
float Float_t
Definition: RtypesCore.h:57
const Bool_t kTRUE
Definition: RtypesCore.h:100
#define ClassImp(name)
Definition: Rtypes.h:364
R__EXTERN TApplication * gApplication
Definition: TApplication.h:165
ErrorHandlerFunc_t GetErrorHandler()
Returns the current error handler function.
Definition: TError.cxx:102
constexpr Int_t kFatal
Definition: TError.h:49
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:187
R__EXTERN Int_t gErrorAbortLevel
Definition: TError.h:128
constexpr Int_t kSysError
Definition: TError.h:48
XFontStruct * id
Definition: TGX11.cxx:109
char name[80]
Definition: TGX11.cxx:110
R__EXTERN TVirtualMutex * gInterpreterMutex
Definition: TInterpreter.h:44
R__EXTERN TInterpreter * gCling
Definition: TInterpreter.h:563
Int_t gDebug
Definition: TROOT.cxx:592
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:61
#define gROOT
Definition: TROOT.h:404
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
R__EXTERN TThreadFactory * gThreadFactory
R__EXTERN void **(* gThreadTsd)(void *, Int_t)
Definition: TThreadSlots.h:40
static TMutex * gMainInternalMutex
Definition: TThread.cxx:58
static void ThreadInternalUnLock()
Definition: TThread.cxx:61
static void CINT_alloc_lock()
Definition: TThread.cxx:55
static TThreadTearDownGuard gTearDownGuard
Definition: TThread.cxx:91
static Bool_t fgIsTearDown(kFALSE)
static void ThreadInternalLock()
Definition: TThread.cxx:60
void ROOT_TThread_Initialize()
Definition: TThread.cxx:65
static void CINT_alloc_unlock()
Definition: TThread.cxx:56
R__EXTERN TVirtualMutex * gGlobalMutex
Definition: TVirtualMutex.h:27
#define R__LOCKGUARD(mutex)
R__EXTERN Int_t(* gThreadXAR)(const char *xact, Int_t nb, void **ar, Int_t *iret)
Definition: TVirtualPad.h:290
#define va_(arg)
Definition: Varargs.h:41
#define free
Definition: civetweb.c:1539
#define snprintf
Definition: civetweb.c:1540
Bool_t IsRunning() const
Definition: TApplication.h:147
virtual Int_t Wait()=0
virtual Int_t Signal()=0
TConditionImp * fConditionImp
Definition: TCondition.h:37
Int_t Signal()
Definition: TCondition.h:53
Int_t TimedWaitRelative(ULong_t ms)
Wait to be signaled or till the timer times out.
Definition: TCondition.cxx:114
TMutex * GetMutex() const
Get internally created mutex.
Definition: TCondition.cxx:65
virtual void SetAllocunlockfunc(void(*)()) const
Definition: TInterpreter.h:265
virtual void SetAlloclockfunc(void(*)()) const
Definition: TInterpreter.h:264
static TInterpreter * Instance()
returns gInterpreter global
TJoinHelper(TThread *th, void **ret)
Constructor of Thread helper class.
Definition: TThread.cxx:117
TMutex * fM
Definition: TThread.cxx:101
Int_t Join()
Thread join function.
Definition: TThread.cxx:157
Bool_t fJoined
Definition: TThread.cxx:103
Long_t fRc
Definition: TThread.cxx:100
TThread * fH
Definition: TThread.cxx:98
void ** fRet
Definition: TThread.cxx:99
~TJoinHelper()
Destructor.
Definition: TThread.cxx:126
TCondition * fC
Definition: TThread.cxx:102
static void * JoinFunc(void *p)
Static method which runs in a separate thread to handle thread joins without blocking the main thread...
Definition: TThread.cxx:138
TThread * fT
Definition: TThread.cxx:97
Method or function calling interface.
Definition: TMethodCall.h:37
virtual Int_t Lock()=0
virtual Int_t UnLock()=0
Definition: TMutex.h:30
Int_t UnLock()
Unlock the mutex.
Definition: TMutex.cxx:68
Int_t CleanUp()
Clean up of mutex.
Definition: TMutex.cxx:76
Int_t Lock()
Lock the mutex.
Definition: TMutex.cxx:46
Int_t TryLock()
Try to lock mutex.
Definition: TMutex.cxx:57
TMutexImp * fMutexImp
Definition: TMutex.h:36
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:879
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:893
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition: TObject.cxx:921
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:867
virtual void AddTimer(TTimer *t)
Add timer to list of system timers.
Definition: TSystem.cxx:474
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition: TSystem.cxx:440
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition: TSystem.cxx:419
virtual const char * GetError()
Return system error string.
Definition: TSystem.cxx:253
~TThreadCleaner()
Call user clean up routines.
Definition: TThread.cxx:1230
virtual TThreadImp * CreateThreadImp()=0
virtual Int_t Join(TThread *th, void **ret)=0
virtual Int_t SetCancelOff()=0
virtual Int_t Exit(void *ret)=0
virtual Int_t CleanUpPush(void **main, void *free, void *arg)=0
virtual Long_t SelfId()=0
virtual Int_t CancelPoint()=0
virtual Int_t SetCancelDeferred()=0
virtual Int_t Kill(TThread *th)=0
virtual Int_t CleanUpPop(void **main, Int_t exe)=0
virtual Int_t SetCancelAsynchronous()=0
virtual Int_t SetCancelOn()=0
virtual Int_t CleanUp(void **main)=0
virtual Int_t Run(TThread *th, const int affinity=-1)=0
TThreadTimer(Long_t ms=kItimerResolution+10)
Create thread timer.
Definition: TThread.cxx:1204
Bool_t Notify()
Periodically execute the TThread::XAction() method in the main thread.
Definition: TThread.cxx:1212
VoidRtnFunc_t fFcnRetn
Definition: TThread.h:84
static TThread * GetThread(Long_t id)
Static method to find a thread by id.
Definition: TThread.cxx:468
static Int_t CleanUpPop(Int_t exe=0)
Static method which pops thread cleanup method off stack.
Definition: TThread.cxx:702
static Int_t CancelPoint()
Static method to set a cancellation point.
Definition: TThread.cxx:681
static Int_t Sleep(ULong_t secs, ULong_t nanos=0)
Static method to sleep the calling thread.
Definition: TThread.cxx:755
static void **volatile fgXArr
Definition: TThread.h:92
static Int_t CleanUpPush(void *free, void *arg=nullptr)
Static method which pushes thread cleanup method on stack.
Definition: TThread.cxx:690
static Int_t TryLock()
Static method to try to lock the main thread mutex.
Definition: TThread.cxx:785
void SetPriority(EPriority pri)
Set thread priority.
Definition: TThread.cxx:460
static Int_t Exists()
Static method to check if threads exist.
Definition: TThread.cxx:444
TThread * fPrev
Definition: TThread.h:75
static void Ps()
Static method listing the existing threads.
Definition: TThread.cxx:848
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:912
static TThreadImp * fgThreadImp
Definition: TThread.h:90
static Int_t UnLock()
Static method to unlock the main thread mutex.
Definition: TThread.cxx:793
Int_t Kill()
Kill this thread.
Definition: TThread.cxx:595
static TMutex * fgMainMutex
Definition: TThread.h:97
EState fState
Definition: TThread.h:78
static volatile Int_t fgXAnb
Definition: TThread.h:93
static Long_t fgMainId
Definition: TThread.h:95
Long_t Join(void **ret=nullptr)
Join this thread.
Definition: TThread.cxx:515
static void XAction()
Static method called via the thread timer to execute in the main thread certain commands.
Definition: TThread.cxx:1083
virtual ~TThread()
Cleanup the thread.
Definition: TThread.cxx:395
Bool_t fDetached
Definition: TThread.h:82
static Int_t SetCancelAsynchronous()
Static method to set the cancellation response type of the calling thread to asynchronous,...
Definition: TThread.cxx:662
static Int_t CleanUp()
Static method to cleanup the calling thread.
Definition: TThread.cxx:713
void ErrorHandler(int level, const char *location, const char *fmt, va_list ap) const
Thread specific error handler function.
Definition: TThread.cxx:968
static Int_t GetTime(ULong_t *absSec, ULong_t *absNanoSec)
Static method to get the current time.
Definition: TThread.cxx:766
static Long_t SelfId()
Static method returning the id for the current thread.
Definition: TThread.cxx:554
void Constructor()
Common thread constructor.
Definition: TThread.cxx:369
static Int_t SetCancelOn()
Static method to turn on thread cancellation.
Definition: TThread.cxx:652
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:899
void DoError(Int_t level, const char *location, const char *fmt, va_list va) const
Interface to ErrorHandler.
Definition: TThread.cxx:1012
Long_t fId
Definition: TThread.h:80
Bool_t fNamed
Definition: TThread.h:83
Int_t Run(void *arg=nullptr, const int affinity=-1)
Start the thread.
Definition: TThread.cxx:571
static volatile Int_t fgXArt
Definition: TThread.h:94
void SetComment(const char *txt=nullptr)
Definition: TThread.h:103
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:1033
static std::atomic< char * > volatile fgXAct
Definition: TThread.h:91
VoidFunc_t fFcnVoid
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:802
EPriority fPriority
Definition: TThread.h:77
static void Printf(const char *fmt,...)
Static method providing a thread safe printf. Appends a newline.
Definition: TThread.cxx:929
static Int_t Lock()
Static method to lock the main thread mutex.
Definition: TThread.cxx:777
@ kRunningState
Definition: TThread.h:64
@ kNewState
Definition: TThread.h:63
@ kCanceledState
Definition: TThread.h:69
@ kCancelingState
Definition: TThread.h:68
@ kFinishedState
Definition: TThread.h:67
@ kTerminatedState
Definition: TThread.h:65
@ kDeletingState
Definition: TThread.h:70
@ kInvalidState
Definition: TThread.h:62
friend class TThreadTimer
Definition: TThread.h:44
static Int_t SetCancelOff()
Static method to turn off thread cancellation.
Definition: TThread.cxx:643
static void Init()
Initialize global state and variables once.
Definition: TThread.cxx:319
static Int_t SetCancelDeferred()
Static method to set the cancellation response type of the calling thread to deferred,...
Definition: TThread.cxx:672
Longptr_t fHandle
Definition: TThread.h:81
static void AfterCancel(TThread *th)
Static method which is called after the thread has been canceled.
Definition: TThread.cxx:734
static TThread * fgMain
Definition: TThread.h:96
static TMutex * fgXActMutex
Definition: TThread.h:98
EPriority
Definition: TThread.h:55
@ kNormalPriority
Definition: TThread.h:57
static Bool_t IsInitialized()
Return true, if the TThread objects have been initialize.
Definition: TThread.cxx:309
static TThread * Self()
Static method returning pointer to current thread.
Definition: TThread.cxx:500
static Int_t Exit(void *ret=nullptr)
Static method which terminates the execution of the calling thread.
Definition: TThread.cxx:747
static void Initialize()
Initialize the Thread package.
Definition: TThread.cxx:300
void * fClean
Definition: TThread.h:87
static TCondition * fgXActCondi
Definition: TThread.h:99
void Delete(Option_t *option="")
Delete this object.
Definition: TThread.h:127
TThread(const TThread &)=delete
TThread ** fHolder
Definition: TThread.h:76
TThread * fNext
Definition: TThread.h:74
void * fThreadArg
Definition: TThread.h:86
The TTimeStamp encapsulates seconds and ns since EPOCH.
Definition: TTimeStamp.h:71
time_t GetSec() const
Definition: TTimeStamp.h:135
Int_t GetNanoSec() const
Definition: TTimeStamp.h:136
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:51
void Reset()
Reset the timer.
Definition: TTimer.cxx:157
This class implements a mutex interface.
Definition: TVirtualMutex.h:32
virtual Int_t UnLock()=0
virtual Int_t Lock()=0
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:51
const Int_t n
Definition: legend1.C:16
R__EXTERN TVirtualRWMutex * gCoreMutex
@ kDirectoryThreadSlot
Definition: TThreadSlots.h:31
@ kMaxThreadSlot
Definition: TThreadSlots.h:35
TROOT * GetROOT()
Definition: TROOT.cxx:464
static constexpr double ms
auto * m
Definition: textangle.C:8
auto * l
Definition: textangle.C:4