00001 // @(#)root/thread:$Id: TPosixCondition.cxx 29797 2009-08-17 14:35:51Z rdm $ 00002 // Author: Fons Rademakers 01/07/97 00003 00004 /************************************************************************* 00005 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. * 00006 * All rights reserved. * 00007 * * 00008 * For the licensing terms see $ROOTSYS/LICENSE. * 00009 * For the list of contributors see $ROOTSYS/README/CREDITS. * 00010 *************************************************************************/ 00011 00012 ////////////////////////////////////////////////////////////////////////// 00013 // // 00014 // TPosixCondition // 00015 // // 00016 // This class provides an interface to the posix condition variable // 00017 // routines. // 00018 // // 00019 ////////////////////////////////////////////////////////////////////////// 00020 00021 #include "TPosixCondition.h" 00022 #include "TPosixMutex.h" 00023 #include "PosixThreadInc.h" 00024 00025 #include <errno.h> 00026 00027 00028 ClassImp(TPosixCondition) 00029 00030 //______________________________________________________________________________ 00031 TPosixCondition::TPosixCondition(TMutexImp *m) 00032 { 00033 // Create Condition variable. Ctor must be given a pointer to an 00034 // existing mutex. The condition variable is then linked to the mutex, 00035 // so that there is an implicit unlock and lock around Wait() and 00036 // TimedWait(). 00037 00038 fMutex = (TPosixMutex *) m; 00039 00040 int rc = pthread_cond_init(&fCond, 0); 00041 00042 if (rc) 00043 SysError("TPosixCondition", "pthread_cond_init error"); 00044 } 00045 00046 //______________________________________________________________________________ 00047 TPosixCondition::~TPosixCondition() 00048 { 00049 // TCondition dtor. 00050 00051 int rc = pthread_cond_destroy(&fCond); 00052 00053 if (rc) 00054 SysError("~TPosixCondition", "pthread_cond_destroy error"); 00055 } 00056 00057 //______________________________________________________________________________ 00058 Int_t TPosixCondition::Wait() 00059 { 00060 // Wait for the condition variable to be signalled. The mutex is 00061 // implicitely released before waiting and locked again after waking up. 00062 // If Wait() is called by multiple threads, a signal may wake up more 00063 // than one thread. See POSIX threads documentation for details. 00064 00065 return pthread_cond_wait(&fCond, &(fMutex->fMutex)); 00066 } 00067 00068 //______________________________________________________________________________ 00069 Int_t TPosixCondition::TimedWait(ULong_t secs, ULong_t nanoSecs) 00070 { 00071 // TimedWait() is given an absolute time to wait until. To wait for a 00072 // relative time from now, use TThread::GetTime(). See POSIX threads 00073 // documentation for why absolute times are better than relative. 00074 // Returns 0 if successfully signalled, 1 if time expired. 00075 00076 timespec rqts = { secs, nanoSecs }; 00077 00078 int rc = pthread_cond_timedwait(&fCond, &(fMutex->fMutex), &rqts); 00079 00080 if (rc == ETIMEDOUT) 00081 rc = 1; 00082 00083 return rc; 00084 } 00085 00086 //______________________________________________________________________________ 00087 Int_t TPosixCondition::Signal() 00088 { 00089 // If one or more threads have called Wait(), Signal() wakes up at least 00090 // one of them, possibly more. See POSIX threads documentation for details. 00091 00092 return pthread_cond_signal(&fCond); 00093 } 00094 00095 00096 //______________________________________________________________________________ 00097 Int_t TPosixCondition::Broadcast() 00098 { 00099 // Broadcast is like signal but wakes all threads which have called Wait(). 00100 00101 return pthread_cond_broadcast(&fCond); 00102 }