Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TCondition.cxx
Go to the documentation of this file.
1// @(#)root/thread:$Id$
2// Author: Fons Rademakers 01/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// TCondition //
15// //
16// This class implements a condition variable. Use a condition variable //
17// to signal threads. The actual work is done via the TConditionImp //
18// class (either TPosixCondition or TWin32Condition). //
19// //
20//////////////////////////////////////////////////////////////////////////
21
22#include "TCondition.h"
23#include "TMutex.h"
24#include "TThread.h"
25#include "TThreadFactory.h"
26
27
28
29////////////////////////////////////////////////////////////////////////////////
30/// Create a condition variable. The actual condition implementation
31/// will be provided via the TThreadFactory. If no external mutex is
32/// provided one will be created. Use GetMutex() to get this mutex
33/// and use it before calling Signal() or Broadcast().
34
36{
37 fPrivateMutex = (m == nullptr);
38 if (fPrivateMutex) {
39 fMutex = new TMutex();
40 } else {
41 fMutex = m;
42 }
43
45
46 if (!fConditionImp)
47 Error("TCondition", "could not create TConditionImp");
48}
49
50////////////////////////////////////////////////////////////////////////////////
51/// Clean up condition variable.
52
54{
55 delete fConditionImp;
56 if (fPrivateMutex) delete fMutex;
57}
58
59////////////////////////////////////////////////////////////////////////////////
60/// Get internally created mutex. Use it to lock resources
61/// before calling Signal() or Broadcast(). Returns 0 if
62/// external mutex was provided in TCondition ctor.
63
65{
66 if (fPrivateMutex)
67 return fMutex;
68 return nullptr;
69}
70
71////////////////////////////////////////////////////////////////////////////////
72/// Wait to be signaled.
73
75{
76 if (!fConditionImp) return -1;
77
78 Int_t iret;
82 return iret;
83}
84
85////////////////////////////////////////////////////////////////////////////////
86/// Wait to be signaled or till the timer times out.
87/// This method is given an absolute time since the beginning of
88/// the EPOCH (use TThread::GetTime() to get this absolute time).
89/// To wait for a relative time from now, use
90/// TCondition::TimedWaitRelative(ULong_t ms).
91/// Returns 0 if successfully signalled, 1 if time expired and -1 in
92/// case of error.
93
104
105////////////////////////////////////////////////////////////////////////////////
106/// Wait to be signaled or till the timer times out.
107/// This method is given a relative time from now.
108/// To wait for an absolute time since the beginning of the EPOCH, use
109/// TCondition::TimedWait(ULong_t secs, ULong_t nanoSec).
110/// Returns 0 if successfully signalled, 1 if time expired and -1 in
111/// case of error.
112
114{
115 if (!fConditionImp) return -1;
116
119
120 ULong_t dsec = ms/1000;
121 absSec += dsec;
122 absNanoSec += (ms - dsec*1000) * 1000000;
123 if (absNanoSec > 999999999) {
124 absSec += 1;
125 absNanoSec -= 1000000000;
126 }
127
128 return TimedWait(absSec, absNanoSec);
129}
unsigned long ULong_t
Unsigned long integer 4 bytes (unsigned long). Size depends on architecture.
Definition RtypesCore.h:69
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
R__EXTERN TThreadFactory * gThreadFactory
virtual Int_t Wait()=0
virtual Int_t TimedWait(ULong_t secs, ULong_t nanoSecs=0)=0
virtual ~TCondition()
Clean up condition variable.
Bool_t fPrivateMutex
Definition TCondition.h:39
Int_t TimedWait(ULong_t secs, ULong_t nanoSecs)
Wait to be signaled or till the timer times out.
TConditionImp * fConditionImp
Definition TCondition.h:37
Int_t TimedWaitRelative(ULong_t ms)
Wait to be signaled or till the timer times out.
TMutex * GetMutex() const
Get internally created mutex.
Int_t Wait()
Wait to be signaled.
TMutex * fMutex
Definition TCondition.h:38
TCondition(const TCondition &)=delete
Int_t UnLock() override
Unlock the mutex.
Definition TMutex.cxx:67
TMutexImp * fMutexImp
Definition TMutex.h:36
Int_t Lock() override
Lock the mutex.
Definition TMutex.cxx:45
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
virtual TConditionImp * CreateConditionImp(TMutexImp *m)=0
static Int_t GetTime(ULong_t *absSec, ULong_t *absNanoSec)
Static method to get the current time.
Definition TThread.cxx:764
TMarker m
Definition textangle.C:8