Logo ROOT   6.12/07
Reference Guide
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 
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// Create a condition variable. The actual condition implementation
32 /// will be provided via the TThreadFactory. If no external mutex is
33 /// provided one will be created. Use GetMutex() to get this mutex
34 /// and use it before calling Signal() or Broadcast().
35 
37 {
38  fPrivateMutex = (m == 0);
39  if (fPrivateMutex) {
40  fMutex = new TMutex();
41  } else {
42  fMutex = m;
43  }
44 
46 
47  if (!fConditionImp)
48  Error("TCondition", "could not create TConditionImp");
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Clean up condition variable.
53 
55 {
56  delete fConditionImp;
57  if (fPrivateMutex) delete fMutex;
58 }
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// Get internally created mutex. Use it to lock resources
62 /// before calling Signal() or Broadcast(). Returns 0 if
63 /// external mutex was provided in TCondition ctor.
64 
66 {
67  if (fPrivateMutex)
68  return fMutex;
69  return 0;
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// Wait to be signaled.
74 
76 {
77  if (!fConditionImp) return -1;
78 
79  Int_t iret;
80  if (fPrivateMutex) fMutex->Lock();
81  iret = fConditionImp->Wait();
82  if (fPrivateMutex) fMutex->UnLock();
83  return iret;
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// Wait to be signaled or till the timer times out.
88 /// This method is given an absolute time since the beginning of
89 /// the EPOCH (use TThread::GetTime() to get this absolute time).
90 /// To wait for a relative time from now, use
91 /// TCondition::TimedWaitRelative(ULong_t ms).
92 /// Returns 0 if successfully signalled, 1 if time expired and -1 in
93 /// case of error.
94 
96 {
97  if (!fConditionImp) return -1;
98 
99  Int_t iret;
100  if (fPrivateMutex) fMutex->Lock();
101  iret = fConditionImp->TimedWait(secs, nanoSec);
102  if (fPrivateMutex) fMutex->UnLock();
103  return iret;
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// Wait to be signaled or till the timer times out.
108 /// This method is given a relative time from now.
109 /// To wait for an absolute time since the beginning of the EPOCH, use
110 /// TCondition::TimedWait(ULong_t secs, ULong_t nanoSec).
111 /// Returns 0 if successfully signalled, 1 if time expired and -1 in
112 /// case of error.
113 
115 {
116  if (!fConditionImp) return -1;
117 
118  ULong_t absSec, absNanoSec;
119  TThread::GetTime(&absSec, &absNanoSec);
120 
121  ULong_t dsec = ms/1000;
122  absSec += dsec;
123  absNanoSec += (ms - dsec*1000) * 1000000;
124  if (absNanoSec > 999999999) {
125  absSec += 1;
126  absNanoSec -= 1000000000;
127  }
128 
129  return TimedWait(absSec, absNanoSec);
130 }
virtual Int_t Wait()=0
Definition: TMutex.h:30
auto * m
Definition: textangle.C:8
virtual ~TCondition()
Clean up condition variable.
Definition: TCondition.cxx:54
TMutex * fMutex
Definition: TCondition.h:38
Bool_t fPrivateMutex
Definition: TCondition.h:39
int Int_t
Definition: RtypesCore.h:41
R__EXTERN TThreadFactory * gThreadFactory
Int_t TimedWait(ULong_t secs, ULong_t nanoSecs)
Wait to be signaled or till the timer times out.
Definition: TCondition.cxx:95
Int_t UnLock()
Unlock the mutex.
Definition: TMutex.cxx:68
Int_t Wait()
Wait to be signaled.
Definition: TCondition.cxx:75
TMutexImp * fMutexImp
Definition: TMutex.h:36
TConditionImp * fConditionImp
Definition: TCondition.h:37
TMutex * GetMutex() const
Get internally created mutex.
Definition: TCondition.cxx:65
virtual Int_t TimedWait(ULong_t secs, ULong_t nanoSecs=0)=0
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
static constexpr double ms
#define ClassImp(name)
Definition: Rtypes.h:359
TCondition(const TCondition &)
unsigned long ULong_t
Definition: RtypesCore.h:51
Int_t Lock()
Lock the mutex.
Definition: TMutex.cxx:46
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:756
Int_t TimedWaitRelative(ULong_t ms)
Wait to be signaled or till the timer times out.
Definition: TCondition.cxx:114