ROOT  6.06/09
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 "TThreadFactory.h"
25 
26 
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 == 0);
38  if (fPrivateMutex) {
39  fMutex = new TMutex();
40  } else {
41  fMutex = m;
42  }
43 
44  fConditionImp = gThreadFactory->CreateConditionImp(fMutex->fMutexImp);
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 0;
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Wait to be signaled.
73 
75 {
76  if (!fConditionImp) return -1;
77 
78  Int_t iret;
79  if (fPrivateMutex) fMutex->Lock();
80  iret = fConditionImp->Wait();
81  if (fPrivateMutex) fMutex->UnLock();
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 
95 {
96  if (!fConditionImp) return -1;
97 
98  Int_t iret;
99  if (fPrivateMutex) fMutex->Lock();
100  iret = fConditionImp->TimedWait(secs, nanoSec);
101  if (fPrivateMutex) fMutex->UnLock();
102  return iret;
103 }
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 
117  ULong_t absSec, absNanoSec;
118  TThread::GetTime(&absSec, &absNanoSec);
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 }
virtual Int_t Wait()=0
Definition: TMutex.h:37
virtual ~TCondition()
Clean up condition variable.
Definition: TCondition.cxx:53
TMutex * fMutex
Definition: TCondition.h:42
Bool_t fPrivateMutex
Definition: TCondition.h:43
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:94
Int_t UnLock()
Unlock the mutex.
Definition: TMutex.cxx:68
void Error(const char *location, const char *msgfmt,...)
Int_t Wait()
Wait to be signaled.
Definition: TCondition.cxx:74
TMutex * GetMutex() const
Get internally created mutex.
Definition: TCondition.cxx:64
TConditionImp * fConditionImp
Definition: TCondition.h:41
virtual Int_t TimedWait(ULong_t secs, ULong_t nanoSecs=0)=0
TMarker * m
Definition: textangle.C:8
ClassImp(TCondition) TCondition
Create a condition variable.
Definition: TCondition.cxx:27
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:746
Int_t TimedWaitRelative(ULong_t ms)
Wait to be signaled or till the timer times out.
Definition: TCondition.cxx:113