Logo ROOT  
Reference Guide
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
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
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
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
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
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
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
100{
101 return pthread_cond_broadcast(&fCond);
102}
unsigned long ULong_t
Definition: RtypesCore.h:53
long Long_t
Definition: RtypesCore.h:52
#define ClassImp(name)
Definition: Rtypes.h:361
virtual void SysError(const char *method, const char *msgfmt,...) const
Issue system error message.
Definition: TObject.cxx:905
virtual ~TPosixCondition()
TCondition dtor.
Int_t Broadcast()
Broadcast is like signal but wakes all threads which have called Wait().
TPosixCondition(TMutexImp *m)
Create Condition variable.
Int_t TimedWait(ULong_t secs, ULong_t nanoSecs=0)
TimedWait() is given an absolute time to wait until.
Int_t Wait()
Wait for the condition variable to be signalled.
Int_t Signal()
If one or more threads have called Wait(), Signal() wakes up at least one of them,...
pthread_cond_t fCond
TPosixMutex * fMutex
pthread_mutex_t fMutex
Definition: TPosixMutex.h:37
auto * m
Definition: textangle.C:8