-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathTThread.cxx
More file actions
1227 lines (973 loc) · 35.4 KB
/
TThread.cxx
File metadata and controls
1227 lines (973 loc) · 35.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @(#)root/thread:$Id$
// Author: Fons Rademakers 02/07/97
/*************************************************************************
* Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
/** \class TThread
\legacy{TThread}
This class implements threads. A thread is an execution environment
much lighter than a process. A single process can have multiple
threads. The actual work is done via the TThreadImp class (either
TPosixThread or TWin32Thread).
**/
#include "RConfigure.h"
#include "TThread.h"
#include "TThreadImp.h"
#include "TThreadFactory.h"
#include "TROOT.h"
#include "TCondition.h"
#include "TApplication.h"
#include "TVirtualPad.h"
#include "TMethodCall.h"
#include "TMutex.h"
#include "TTimeStamp.h"
#include "TInterpreter.h"
#include "TError.h"
#include "TSystem.h"
#include "Varargs.h"
#include "ThreadLocalStorage.h"
#include "TThreadSlots.h"
#include "TRWMutexImp.h"
#include "snprintf.h"
TThreadImp *TThread::fgThreadImp = nullptr;
Long_t TThread::fgMainId = 0;
TThread *TThread::fgMain = nullptr;
TMutex *TThread::fgMainMutex;
std::atomic<char *> volatile TThread::fgXAct{nullptr};
TMutex *TThread::fgXActMutex = nullptr;
TCondition *TThread::fgXActCondi = nullptr;
void **volatile TThread::fgXArr = nullptr;
volatile Int_t TThread::fgXAnb = 0;
volatile Int_t TThread::fgXArt = 0;
static void CINT_alloc_lock() { gGlobalMutex->Lock(); }
static void CINT_alloc_unlock() { gGlobalMutex->UnLock(); }
static TMutex *gMainInternalMutex = nullptr;
static void ThreadInternalLock() { if (gMainInternalMutex) gMainInternalMutex->Lock(); }
static void ThreadInternalUnLock() { if (gMainInternalMutex) gMainInternalMutex->UnLock(); }
static Bool_t fgIsTearDown(kFALSE);
extern "C" void ROOT_TThread_Initialize()
{
TThread::Initialize();
};
//------------------------------------------------------------------------------
// Set gGlobalMutex to 0 when Thread library gets unloaded
class TThreadTearDownGuard {
public:
TThreadTearDownGuard() { fgIsTearDown = kFALSE; }
~TThreadTearDownGuard() {
// Note: we could insert here a wait for all thread to be finished.
// this is questionable though as we need to balance between fixing a
// user error (the thread was let lose and the caller did not explicit wait)
// and the risk that we can not terminate a failing process.
fgIsTearDown = kTRUE;
TVirtualMutex *m = gGlobalMutex;
gGlobalMutex = nullptr;
delete m;
TThreadImp *imp = TThread::fgThreadImp;
TThread::fgThreadImp = nullptr;
delete imp;
}
};
static TThreadTearDownGuard gTearDownGuard;
//------------------------------------------------------------------------------
class TJoinHelper {
private:
TThread *fT;
TThread *fH;
void **fRet;
Long_t fRc;
TMutex *fM;
TCondition *fC;
Bool_t fJoined;
static void* JoinFunc(void *p);
public:
TJoinHelper(TThread *th, void **ret);
~TJoinHelper();
Int_t Join();
};
////////////////////////////////////////////////////////////////////////////////
/// Constructor of Thread helper class.
TJoinHelper::TJoinHelper(TThread *th, void **ret)
: fT(th), fRet(ret), fRc(0), fM(new TMutex), fC(new TCondition(fM)), fJoined(kFALSE)
{
fH = new TThread("JoinHelper", JoinFunc, this);
}
////////////////////////////////////////////////////////////////////////////////
/// Destructor.
TJoinHelper::~TJoinHelper()
{
delete fC;
delete fM;
delete fH;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method which runs in a separate thread to handle thread
/// joins without blocking the main thread.
/// Return a value (zero) so that it makes a joinable thread.
void* TJoinHelper::JoinFunc(void *p)
{
TJoinHelper *jp = (TJoinHelper*)p;
jp->fRc = jp->fT->Join(jp->fRet);
jp->fM->Lock();
jp->fJoined = kTRUE;
jp->fC->Signal();
jp->fM->UnLock();
TThread::Exit(nullptr);
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
/// Thread join function.
Int_t TJoinHelper::Join()
{
fM->Lock();
fH->Run();
while (kTRUE) {
// TimedWaitRelative will release the mutex (i.e. equivalent to fM->Unlock),
// then block on the condition variable. Upon return it will lock the mutex.
int r = fC->TimedWaitRelative(100); // 100 ms
// From the man page from pthread_ond_timedwait:
// When using condition variables there is always a Boolean predicate
// involving shared variables associated with each condition wait that
// is true if the thread should proceed. Spurious wakeups from the
// pthread_cond_timedwait() or pthread_cond_wait() functions may occur.
// Since the return from pthread_cond_timedwait() or pthread_cond_wait()
// does not imply anything about the value of this predicate, the
// predicate should be re-evaluated upon such return.
if (r == 0 || r == 1) {
// If we received the signal or timed out, let's check the value
if (fJoined) break;
} else {
// If any other error occurred, there is no point in trying again
break;
}
gSystem->ProcessEvents();
}
fM->UnLock();
// And wait for the help to finish to avoid the risk that it is still
// running when the main tread is finished (and the thread library unloaded!)
TThread::fgThreadImp->Join(fH, nullptr);
return fRc;
}
//------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// Create a thread. Specify the function or static class method
/// to be executed by the thread and a pointer to the argument structure.
/// The user function should return a void*. To start the thread call Run().
TThread::TThread(VoidRtnFunc_t fn, void *arg, EPriority pri)
: TNamed("<anon>", "")
{
fDetached = kFALSE;
fFcnVoid = nullptr;
fFcnRetn = fn;
fPriority = pri;
fThreadArg = arg;
Constructor();
fNamed = kFALSE;
}
////////////////////////////////////////////////////////////////////////////////
/// Create a detached thread. Specify the function or static class method
/// to be executed by the thread and a pointer to the argument structure.
/// To start the thread call Run().
TThread::TThread(VoidFunc_t fn, void *arg, EPriority pri)
: TNamed("<anon>", "")
{
fDetached = kTRUE;
fFcnRetn = nullptr;
fFcnVoid = fn;
fPriority = pri;
fThreadArg = arg;
Constructor();
fNamed = kFALSE;
}
////////////////////////////////////////////////////////////////////////////////
/// Create thread with a name. Specify the function or static class method
/// to be executed by the thread and a pointer to the argument structure.
/// The user function should return a void*. To start the thread call Run().
TThread::TThread(const char *thname, VoidRtnFunc_t fn, void *arg,
EPriority pri) : TNamed(thname, "")
{
fDetached = kFALSE;
fFcnVoid = nullptr;
fFcnRetn = fn;
fPriority = pri;
fThreadArg = arg;
Constructor();
fNamed = kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Create a detached thread with a name. Specify the function or static
/// class method to be executed by the thread and a pointer to the argument
/// structure. To start the thread call Run().
TThread::TThread(const char *thname, VoidFunc_t fn, void *arg,
EPriority pri) : TNamed(thname, "")
{
fDetached = kTRUE;
fFcnRetn = nullptr;
fFcnVoid = fn;
fPriority = pri;
fThreadArg = arg;
Constructor();
fNamed = kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Create a TThread for a already running thread.
TThread::TThread(Long_t id)
{
fDetached = kTRUE;
fFcnRetn = nullptr;
fFcnVoid = nullptr;
fPriority = kNormalPriority;
fThreadArg = nullptr;
Constructor();
// Changing the id must be protected as it will be look at by multiple
// threads (see TThread::GetThread)
ThreadInternalLock();
fNamed = kFALSE;
fId = (id ? id : SelfId());
fState = kRunningState;
ThreadInternalUnLock();
if (gDebug)
Info("TThread::TThread", "TThread attached to running thread");
}
////////////////////////////////////////////////////////////////////////////////
/// Initialize the Thread package. This initializes the TThread and ROOT
/// global mutexes to make parts of ROOT thread safe/aware. This call is
/// implicit in case a TThread is created.
void TThread::Initialize()
{
Init();
}
////////////////////////////////////////////////////////////////////////////////
/// Return true, if the TThread objects have been initialize. If false,
/// the process is (from ROOT's point of view) single threaded.
Bool_t TThread::IsInitialized()
{
if (fgThreadImp)
return kTRUE;
return kFALSE;
}
////////////////////////////////////////////////////////////////////////////////
/// Initialize global state and variables once.
void TThread::Init()
{
if (fgThreadImp || fgIsTearDown) return;
// 'Insure' gROOT is created before initializing the Thread safe behavior
// (to make sure we do not have two attempting to create it).
ROOT::GetROOT();
fgThreadImp = gThreadFactory->CreateThreadImp();
gMainInternalMutex = new TMutex(kTRUE);
fgMainId = fgThreadImp->SelfId();
fgMainMutex = new TMutex(kTRUE);
gThreadTsd = TThread::Tsd;
gThreadXAR = TThread::XARequest;
// Create the single global mutex
gGlobalMutex = new TMutex(kTRUE);
// We need to make sure that gCling is initialized.
TInterpreter::Instance()->SetAlloclockfunc(CINT_alloc_lock);
gCling->SetAllocunlockfunc(CINT_alloc_unlock);
// To avoid deadlocks, gInterpreterMutex and gROOTMutex need
// to point at the same instance.
// Both are now deprecated in favor of ROOT::gCoreMutex
{
R__LOCKGUARD(gGlobalMutex);
if (!ROOT::gCoreMutex) {
// To avoid dead locks, caused by shared library opening and/or static initialization
// taking the same lock as 'tls_get_addr_tail', we can not use UniqueLockRecurseCount.
#ifdef ROOT_CORE_THREAD_TBB
ROOT::gCoreMutex = new ROOT::TRWMutexImp<std::mutex, ROOT::Internal::RecurseCountsTBBUnique>();
#else
ROOT::gCoreMutex = new ROOT::TRWMutexImp<std::mutex, ROOT::Internal::RecurseCounts>();
#endif
}
gInterpreterMutex = ROOT::gCoreMutex;
gROOTMutex = gInterpreterMutex;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Common thread constructor.
void TThread::Constructor()
{
fHolder = nullptr;
fClean = nullptr;
fState = kNewState;
fId = -1;
fHandle= 0;
if (!fgThreadImp) Init();
SetComment("Constructor: MainInternalMutex Locking");
ThreadInternalLock();
SetComment("Constructor: MainInternalMutex Locked");
if (fgMain) fgMain->fPrev = this;
fNext = fgMain;
fPrev = nullptr;
fgMain = this;
ThreadInternalUnLock();
SetComment();
// thread is set up in initialisation routine or Run().
}
////////////////////////////////////////////////////////////////////////////////
/// Cleanup the thread.
TThread::~TThread()
{
if (gDebug)
Info("TThread::~TThread", "thread deleted");
// Disconnect thread instance
SetComment("Destructor: MainInternalMutex Locking");
ThreadInternalLock();
SetComment("Destructor: MainInternalMutex Locked");
if (fPrev) fPrev->fNext = fNext;
if (fNext) fNext->fPrev = fPrev;
if (fgMain == this) fgMain = fNext;
ThreadInternalUnLock();
SetComment();
if (fHolder)
*fHolder = nullptr;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to delete the specified thread.
/// Returns -1 in case the thread was running and has been killed. Returns
/// 0 in case the thread has been Delete and Cleaned up. The th pointer is
/// not valid anymore in that case.
Int_t TThread::Delete(TThread *&th)
{
if (!th) return 0;
th->fHolder = &th;
if (th->fState == kRunningState) { // Cancel if running
th->fState = kDeletingState;
if (gDebug)
th->Info("TThread::Delete", "deleting thread");
th->Kill();
return -1;
}
CleanUp();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to check if threads exist.
/// Returns the number of running threads.
Int_t TThread::Exists()
{
ThreadInternalLock();
Int_t num = 0;
for (TThread *l = fgMain; l; l = l->fNext)
num++; //count threads
ThreadInternalUnLock();
return num;
}
////////////////////////////////////////////////////////////////////////////////
/// Set thread priority.
void TThread::SetPriority(EPriority pri)
{
fPriority = pri;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to find a thread by id.
TThread *TThread::GetThread(Long_t id)
{
TThread *myTh;
ThreadInternalLock();
for (myTh = fgMain; myTh && (myTh->fId != id); myTh = myTh->fNext) { }
ThreadInternalUnLock();
return myTh;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to find a thread by name.
TThread *TThread::GetThread(const char *name)
{
TThread *myTh;
ThreadInternalLock();
for (myTh = fgMain; myTh && (strcmp(name, myTh->GetName())); myTh = myTh->fNext) { }
ThreadInternalUnLock();
return myTh;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method returning pointer to current thread.
TThread *TThread::Self()
{
TTHREAD_TLS(TThread *) self = nullptr;
if (!self || fgIsTearDown) {
if (fgIsTearDown)
self = nullptr;
self = GetThread(SelfId());
}
return self;
}
////////////////////////////////////////////////////////////////////////////////
/// Join this thread.
Long_t TThread::Join(void **ret)
{
if (fId == -1) {
Error("Join", "thread not running");
return -1;
}
if (fDetached) {
Error("Join", "cannot join detached thread");
return -1;
}
if (SelfId() != fgMainId)
return fgThreadImp->Join(this, ret);
// do not block the main thread, use helper thread
TJoinHelper helper(this, ret);
return helper.Join();
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to join a thread by id.
Long_t TThread::Join(Long_t jid, void **ret)
{
TThread *myTh = GetThread(jid);
if (!myTh) {
::Error("TThread::Join", "cannot find thread 0x%lx", jid);
return -1L;
}
return myTh->Join(ret);
}
////////////////////////////////////////////////////////////////////////////////
/// Static method returning the id for the current thread.
Long_t TThread::SelfId()
{
if (fgIsTearDown) return -1;
if (!fgThreadImp) Init();
return fgThreadImp->SelfId();
}
////////////////////////////////////////////////////////////////////////////////
/// Start the thread. This starts the static method TThread::Function()
/// which calls the user function specified in the TThread ctor with
/// the arg argument.
/// If affinity is specified (>=0), a CPU affinity will be associated
/// with the current thread.
/// Returns 0 on success, otherwise an error number will
/// be returned.
Int_t TThread::Run(void *arg, const int affinity)
{
if (arg) fThreadArg = arg;
SetComment("Run: MainInternalMutex locking");
ThreadInternalLock();
SetComment("Run: MainMutex locked");
int iret = fgThreadImp->Run(this, affinity);
fState = iret ? kInvalidState : kRunningState;
if (gDebug)
Info("TThread::Run", "thread run requested");
ThreadInternalUnLock();
SetComment();
return iret;
}
////////////////////////////////////////////////////////////////////////////////
/// Kill this thread. Returns 0 on success, otherwise an error number will
/// be returned.
Int_t TThread::Kill()
{
if (fState != kRunningState && fState != kDeletingState) {
if (gDebug)
Warning("TThread::Kill", "thread is not running");
return 13;
} else {
if (fState == kRunningState ) fState = kCancelingState;
return fgThreadImp->Kill(this);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to kill the thread by id. Returns 0 on success, otherwise
/// an error number will be returned.
Int_t TThread::Kill(Long_t id)
{
TThread *th = GetThread(id);
if (th) {
return fgThreadImp->Kill(th);
} else {
if (gDebug)
::Warning("TThread::Kill(Long_t)", "thread 0x%lx not found", id);
return 13;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to kill thread by name. Returns 0 on success, otherwise
/// an error number will be returned.
Int_t TThread::Kill(const char *name)
{
TThread *th = GetThread(name);
if (th) {
return fgThreadImp->Kill(th);
} else {
if (gDebug)
::Warning("TThread::Kill(const char*)", "thread %s not found", name);
return 13;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to turn off thread cancellation. Returns 0 on success,
/// otherwise an error number will be returned.
Int_t TThread::SetCancelOff()
{
return fgThreadImp ? fgThreadImp->SetCancelOff() : -1;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to turn on thread cancellation. Returns 0 on success,
/// otherwise an error number will be returned.
Int_t TThread::SetCancelOn()
{
return fgThreadImp ? fgThreadImp->SetCancelOn() : -1;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to set the cancellation response type of the calling thread
/// to asynchronous, i.e. cancel as soon as the cancellation request
/// is received.
Int_t TThread::SetCancelAsynchronous()
{
return fgThreadImp ? fgThreadImp->SetCancelAsynchronous() : -1;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to set the cancellation response type of the calling thread
/// to deferred, i.e. cancel only at next cancellation point.
/// Returns 0 on success, otherwise an error number will be returned.
Int_t TThread::SetCancelDeferred()
{
return fgThreadImp ? fgThreadImp->SetCancelDeferred() : -1;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to set a cancellation point. Returns 0 on success, otherwise
/// an error number will be returned.
Int_t TThread::CancelPoint()
{
return fgThreadImp ? fgThreadImp->CancelPoint() : -1;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method which pushes thread cleanup method on stack.
/// Returns 0 in case of success and -1 in case of error.
Int_t TThread::CleanUpPush(void *free, void *arg)
{
TThread *th = Self();
if (th)
return fgThreadImp->CleanUpPush(&(th->fClean), free, arg);
return -1;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method which pops thread cleanup method off stack.
/// Returns 0 in case of success and -1 in case of error.
Int_t TThread::CleanUpPop(Int_t exe)
{
TThread *th = Self();
if (th)
return fgThreadImp->CleanUpPop(&(th->fClean), exe);
return -1;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to cleanup the calling thread.
Int_t TThread::CleanUp()
{
TThread *th = Self();
if (!th) return 13;
fgThreadImp->CleanUp(&(th->fClean));
fgMainMutex->CleanUp();
if (fgXActMutex)
fgXActMutex->CleanUp();
gMainInternalMutex->CleanUp();
if (th->fHolder)
delete th;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method which is called after the thread has been canceled.
void TThread::AfterCancel(TThread *th)
{
if (th) {
th->fState = kCanceledState;
if (gDebug)
th->Info("TThread::AfterCancel", "thread is canceled");
} else
::Error("TThread::AfterCancel", "zero thread pointer passed");
}
////////////////////////////////////////////////////////////////////////////////
/// Static method which terminates the execution of the calling thread.
Int_t TThread::Exit(void *ret)
{
return fgThreadImp ? fgThreadImp->Exit(ret) : -1;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to sleep the calling thread.
Int_t TThread::Sleep(ULong_t secs, ULong_t nanos)
{
UInt_t ms = UInt_t(secs * 1000) + UInt_t(nanos / 1000000);
if (gSystem) gSystem->Sleep(ms);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to get the current time. Returns
/// the number of seconds.
Int_t TThread::GetTime(ULong_t *absSec, ULong_t *absNanoSec)
{
TTimeStamp t;
if (absSec) *absSec = t.GetSec();
if (absNanoSec) *absNanoSec = t.GetNanoSec();
return t.GetSec();
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to lock the main thread mutex.
Int_t TThread::Lock()
{
return (fgMainMutex ? fgMainMutex->Lock() : 0);
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to try to lock the main thread mutex.
Int_t TThread::TryLock()
{
return (fgMainMutex ? fgMainMutex->TryLock() : 0);
}
////////////////////////////////////////////////////////////////////////////////
/// Static method to unlock the main thread mutex.
Int_t TThread::UnLock()
{
return (fgMainMutex ? fgMainMutex->UnLock() : 0);
}
////////////////////////////////////////////////////////////////////////////////
/// Static method which is called by the system thread function and
/// which in turn calls the actual user function.
void *TThread::Function(void *ptr)
{
TThread *th;
void *ret, *arg;
TThreadCleaner dummy;
th = (TThread *)ptr;
// Default cancel state is OFF
// Default cancel type is DEFERRED
// User can change it by call SetCancelOn() and SetCancelAsynchronous()
SetCancelOff();
SetCancelDeferred();
CleanUpPush((void *)&AfterCancel, th); // Enable standard cancelling function
if (gDebug)
th->Info("TThread::Function", "thread is running");
arg = th->fThreadArg;
th->fState = kRunningState;
if (th->fDetached) {
//Detached, non joinable thread
(th->fFcnVoid)(arg);
ret = nullptr;
th->fState = kFinishedState;
} else {
//UnDetached, joinable thread
ret = (th->fFcnRetn)(arg);
th->fState = kTerminatedState;
}
CleanUpPop(1); // Disable standard canceling function
if (gDebug)
th->Info("TThread::Function", "thread has finished");
TThread::Exit(ret);
return ret;
}
////////////////////////////////////////////////////////////////////////////////
/// Static method listing the existing threads.
void TThread::Ps()
{
TThread *l;
int i;
if (!fgMain) {
::Info("TThread::Ps", "no threads have been created");
return;
}
ThreadInternalLock();
int num = 0;
for (l = fgMain; l; l = l->fNext)
num++;
char cbuf[256];
printf(" Thread State\n");
for (l = fgMain; l; l = l->fNext) { // loop over threads
memset(cbuf, ' ', sizeof(cbuf));
snprintf(cbuf, sizeof(cbuf), "%3d %s:0x%lx", num--, l->GetName(), l->fId);
i = (int)strlen(cbuf);
if (i < 30)
cbuf[i] = ' ';
cbuf[30] = 0;
printf("%30s", cbuf);
switch (l->fState) { // print states
case kNewState: printf("Idle "); break;
case kRunningState: printf("Running "); break;
case kTerminatedState: printf("Terminated "); break;
case kFinishedState: printf("Finished "); break;
case kCancelingState: printf("Canceling "); break;
case kCanceledState: printf("Canceled "); break;
case kDeletingState: printf("Deleting "); break;
default: printf("Invalid ");
}
if (l->fComment[0]) printf(" // %s", l->fComment);
printf("\n");
} // end of loop
ThreadInternalUnLock();
}
////////////////////////////////////////////////////////////////////////////////
/// Static method returning a pointer to thread specific data container
/// of the calling thread.
/// k should be between 0 and kMaxUserThreadSlot for user application.
/// (and between kMaxUserThreadSlot and kMaxThreadSlot for ROOT libraries).
/// See ROOT::EThreadSlotReservation
void **TThread::Tsd(void *dflt, Int_t k)
{
if (TThread::SelfId() == fgMainId) { //Main thread
return (void**)dflt;
} else {
return GetTls(k);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Static method that initializes the TLS array of a thread and returns the
/// reference to a given position in that array.
void **TThread::GetTls(Int_t k) {
TTHREAD_TLS_ARRAY(void*, ROOT::kMaxThreadSlot, tls);
return &(tls[k]);
}
////////////////////////////////////////////////////////////////////////////////
/// Static method providing a thread safe printf. Appends a newline.
void TThread::Printf(const char *va_(fmt), ...)
{
va_list ap;
va_start(ap,va_(fmt));
Int_t buf_size = 2048;
char *buf;
again:
buf = new char[buf_size];
int n = vsnprintf(buf, buf_size, va_(fmt), ap);
// old vsnprintf's return -1 if string is truncated new ones return
// total number of characters that would have been written
if (n == -1 || n >= buf_size) {
buf_size *= 2;
delete [] buf;
goto again;
}
va_end(ap);
void *arr[2];
arr[1] = (void*) buf;
if (XARequest("PRTF", 2, arr, nullptr)) {
delete [] buf;
return;
}
printf("%s\n", buf);
fflush(stdout);
delete [] buf;
}
////////////////////////////////////////////////////////////////////////////////
/// Thread specific error handler function.
/// It calls the user set error handler in the main thread.
void TThread::ErrorHandler(int level, const char *location, const char *fmt,
va_list ap) const
{
Int_t buf_size = 2048;
char *buf, *bp;
again:
buf = new char[buf_size];
int n = vsnprintf(buf, buf_size, fmt, ap);
// old vsnprintf's return -1 if string is truncated new ones return
// total number of characters that would have been written
if (n == -1 || n >= buf_size) {
buf_size *= 2;
delete [] buf;
goto again;
}
if (level >= kSysError && level < kFatal) {
const std::size_t bufferSize = buf_size + strlen(gSystem->GetError()) + 5;
char *buf1 = new char[bufferSize];
snprintf(buf1, bufferSize, "%s (%s)", buf, gSystem->GetError());
bp = buf1;
delete [] buf;
} else
bp = buf;
void *arr[4];
arr[1] = (void*) Longptr_t(level);
arr[2] = (void*) location;
arr[3] = (void*) bp;
if (XARequest("ERRO", 4, arr, nullptr))
return;
if (level != kFatal)
::GetErrorHandler()(level, level >= gErrorAbortLevel, location, bp);
else
::GetErrorHandler()(level, kTRUE, location, bp);
delete [] bp;
}
////////////////////////////////////////////////////////////////////////////////
/// Interface to ErrorHandler. User has to specify the class name as