ROOT  6.06/09
Reference Guide
TSemaphore.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 // TSemaphore //
15 // //
16 // This class implements a counting semaphore. Use a semaphore //
17 // to synchronize threads. //
18 // //
19 //////////////////////////////////////////////////////////////////////////
20 
21 #include "TSemaphore.h"
22 
24 
25 ////////////////////////////////////////////////////////////////////////////////
26 /// Create counting semaphore.
27 
28 TSemaphore::TSemaphore(UInt_t initial) : fCond(&fMutex)
29 {
30  fValue = initial;
31 }
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 /// If semaphore value is > 0 then decrement it and carry on. If it's
35 /// already 0 then block. If millisec > 0, apply a relative timeout
36 /// of millisec milliseconds. Returns 0 in case of success, or mutex errno.
37 
39 {
40  Int_t rc = 0;
41 
42  if ((rc = fMutex.Lock())) {
43  Error("Wait","Lock returns %d [%ld]", rc, TThread::SelfId());
44  return rc;
45  }
46 
47  while (fValue == 0) {
48 
49  int crc = (millisec > 0) ? fCond.TimedWaitRelative(millisec)
50  : fCond.Wait();
51 
52  if (crc != 0) {
53  if (crc == 1 && gDebug > 0) {
54  Info("Wait", "TCondition::Wait() returns %d [%ld]",
55  crc, TThread::SelfId());
56  } else if (crc != 1) {
57  Error("Wait", "TCondition::Wait() returns %d [%ld]",
58  crc, TThread::SelfId());
59  }
60  if ((rc = fMutex.UnLock()))
61  Error("Wait", "UnLock on error returns %d [%ld]",
62  rc, TThread::SelfId());
63  return crc;
64  }
65  }
66 
67  fValue--;
68 
69  if ((rc = fMutex.UnLock())) {
70  Error("Wait", "UnLock returns %d [%ld]", rc, TThread::SelfId());
71  return rc;
72  }
73 
74  return 0;
75 }
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// If semaphore value is > 0 then decrement it and return 0. If it's
79 /// already 0 then return 1 or mutex errno.
80 
82 {
83  int r = fMutex.Lock();
84  if (r) { Error("TryWait","Lock returns %d [%ld]", r, TThread::SelfId()); return r; }
85 
86  if (fValue == 0) {
87  r = fMutex.UnLock();
88  if (r) Error("TryWait","UnLock on fail returns %d [%ld]", r, TThread::SelfId());
89  return 1;
90  }
91 
92  fValue--;
93 
94  r = fMutex.UnLock();
95  if (r) { Error("TryWait","UnLock returns %d [%ld]", r, TThread::SelfId()); return r; }
96 
97  return 0;
98 }
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// If any threads are blocked in Wait(), wake one of them up and
102 /// increment the value of the semaphore. Returns 0 in case of success, or
103 /// mutex errno.
104 
106 {
107  int r = fMutex.Lock();
108  if (r) { Error("Post","Lock returns %d [%ld]", r, TThread::SelfId()); return r; }
109 
110  Bool_t doSignal = fValue == 0;
111  fValue++;
112 
113  r = fMutex.UnLock();
114  if (r) { Error("Post","UnLock returns %d [%ld]", r, TThread::SelfId()); return r; }
115 
116  if (doSignal) fCond.Signal();
117 
118  return 0;
119 }
Int_t TryWait()
If semaphore value is > 0 then decrement it and return 0.
Definition: TSemaphore.cxx:81
Int_t Wait(Int_t millisec=0)
If semaphore value is > 0 then decrement it and carry on.
Definition: TSemaphore.cxx:38
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:892
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Int_t UnLock()
Unlock the mutex.
Definition: TMutex.cxx:68
static Long_t SelfId()
Static method returning the id for the current thread.
Definition: TThread.cxx:537
TMutex fMutex
Definition: TSemaphore.h:39
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
Int_t Wait()
Wait to be signaled.
Definition: TCondition.cxx:74
TCondition fCond
Definition: TSemaphore.h:40
ROOT::R::TRInterface & r
Definition: Object.C:4
PyObject * fValue
unsigned int UInt_t
Definition: RtypesCore.h:42
Int_t Post()
If any threads are blocked in Wait(), wake one of them up and increment the value of the semaphore...
Definition: TSemaphore.cxx:105
Int_t fValue
Definition: TSemaphore.h:41
Int_t Lock()
Lock the mutex.
Definition: TMutex.cxx:46
R__EXTERN Int_t gDebug
Definition: Rtypes.h:128
Int_t TimedWaitRelative(ULong_t ms)
Wait to be signaled or till the timer times out.
Definition: TCondition.cxx:113
ClassImp(TSemaphore) TSemaphore
Create counting semaphore.
Definition: TSemaphore.cxx:23
Int_t Signal()
Definition: TCondition.h:57