Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TPosixCondition.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 // TPosixCondition //
15 // //
16 // This class provides an interface to the posix condition variable //
17 // routines. //
18 // //
19 //////////////////////////////////////////////////////////////////////////
20 
21 #include "TPosixCondition.h"
22 #include "TPosixMutex.h"
23 #include "PosixThreadInc.h"
24 
25 #include <errno.h>
26 
27 
28 ClassImp(TPosixCondition);
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// Create Condition variable. Ctor must be given a pointer to an
32 /// existing mutex. The condition variable is then linked to the mutex,
33 /// so that there is an implicit unlock and lock around Wait() and
34 /// TimedWait().
35 
36 TPosixCondition::TPosixCondition(TMutexImp *m)
37 {
38  fMutex = (TPosixMutex *) m;
39 
40  int rc = pthread_cond_init(&fCond, 0);
41 
42  if (rc)
43  SysError("TPosixCondition", "pthread_cond_init error");
44 }
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 /// TCondition dtor.
48 
49 TPosixCondition::~TPosixCondition()
50 {
51  int rc = pthread_cond_destroy(&fCond);
52 
53  if (rc)
54  SysError("~TPosixCondition", "pthread_cond_destroy error");
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// Wait for the condition variable to be signalled. The mutex is
59 /// implicitely released before waiting and locked again after waking up.
60 /// If Wait() is called by multiple threads, a signal may wake up more
61 /// than one thread. See POSIX threads documentation for details.
62 
63 Int_t TPosixCondition::Wait()
64 {
65  return pthread_cond_wait(&fCond, &(fMutex->fMutex));
66 }
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 /// TimedWait() is given an absolute time to wait until. To wait for a
70 /// relative time from now, use TThread::GetTime(). See POSIX threads
71 /// documentation for why absolute times are better than relative.
72 /// Returns 0 if successfully signalled, 1 if time expired.
73 
74 Int_t TPosixCondition::TimedWait(ULong_t secs, ULong_t nanoSecs)
75 {
76  timespec rqts = { (Long_t)secs, (Long_t)nanoSecs };
77 
78  int rc = pthread_cond_timedwait(&fCond, &(fMutex->fMutex), &rqts);
79 
80  if (rc == ETIMEDOUT)
81  rc = 1;
82 
83  return rc;
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// If one or more threads have called Wait(), Signal() wakes up at least
88 /// one of them, possibly more. See POSIX threads documentation for details.
89 
90 Int_t TPosixCondition::Signal()
91 {
92  return pthread_cond_signal(&fCond);
93 }
94 
95 
96 ////////////////////////////////////////////////////////////////////////////////
97 /// Broadcast is like signal but wakes all threads which have called Wait().
98 
99 Int_t TPosixCondition::Broadcast()
100 {
101  return pthread_cond_broadcast(&fCond);
102 }