// @(#)root/thread:$Id$
// Author: Fons Rademakers   01/07/97

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TPosixCondition                                                      //
//                                                                      //
// This class provides an interface to the posix condition variable     //
// routines.                                                            //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TPosixCondition.h"
#include "TPosixMutex.h"
#include "PosixThreadInc.h"

#include <errno.h>


ClassImp(TPosixCondition)

//______________________________________________________________________________
TPosixCondition::TPosixCondition(TMutexImp *m)
{
   // Create Condition variable. Ctor must be given a pointer to an
   // existing mutex. The condition variable is then linked to the mutex,
   // so that there is an implicit unlock and lock around Wait() and
   // TimedWait().

   fMutex = (TPosixMutex *) m;

   int rc = pthread_cond_init(&fCond, 0);

   if (rc)
      SysError("TPosixCondition", "pthread_cond_init error");
}

//______________________________________________________________________________
TPosixCondition::~TPosixCondition()
{
   // TCondition dtor.

   int rc = pthread_cond_destroy(&fCond);

   if (rc)
      SysError("~TPosixCondition", "pthread_cond_destroy error");
}

//______________________________________________________________________________
Int_t TPosixCondition::Wait()
{
   // Wait for the condition variable to be signalled. The mutex is
   // implicitely released before waiting and locked again after waking up.
   // If Wait() is called by multiple threads, a signal may wake up more
   // than one thread. See POSIX threads documentation for details.

   return pthread_cond_wait(&fCond, &(fMutex->fMutex));
}

//______________________________________________________________________________
Int_t TPosixCondition::TimedWait(ULong_t secs, ULong_t nanoSecs)
{
   // TimedWait() is given an absolute time to wait until. To wait for a
   // relative time from now, use TThread::GetTime(). See POSIX threads
   // documentation for why absolute times are better than relative.
   // Returns 0 if successfully signalled, 1 if time expired.

   timespec rqts = { (Long_t)secs, (Long_t)nanoSecs };

   int rc = pthread_cond_timedwait(&fCond, &(fMutex->fMutex), &rqts);

   if (rc == ETIMEDOUT)
      rc = 1;

   return rc;
}

//______________________________________________________________________________
Int_t TPosixCondition::Signal()
{
   // If one or more threads have called Wait(), Signal() wakes up at least
   // one of them, possibly more. See POSIX threads documentation for details.

   return pthread_cond_signal(&fCond);
}


//______________________________________________________________________________
Int_t TPosixCondition::Broadcast()
{
   // Broadcast is like signal but wakes all threads which have called Wait().

   return pthread_cond_broadcast(&fCond);
}
 TPosixCondition.cxx:1
 TPosixCondition.cxx:2
 TPosixCondition.cxx:3
 TPosixCondition.cxx:4
 TPosixCondition.cxx:5
 TPosixCondition.cxx:6
 TPosixCondition.cxx:7
 TPosixCondition.cxx:8
 TPosixCondition.cxx:9
 TPosixCondition.cxx:10
 TPosixCondition.cxx:11
 TPosixCondition.cxx:12
 TPosixCondition.cxx:13
 TPosixCondition.cxx:14
 TPosixCondition.cxx:15
 TPosixCondition.cxx:16
 TPosixCondition.cxx:17
 TPosixCondition.cxx:18
 TPosixCondition.cxx:19
 TPosixCondition.cxx:20
 TPosixCondition.cxx:21
 TPosixCondition.cxx:22
 TPosixCondition.cxx:23
 TPosixCondition.cxx:24
 TPosixCondition.cxx:25
 TPosixCondition.cxx:26
 TPosixCondition.cxx:27
 TPosixCondition.cxx:28
 TPosixCondition.cxx:29
 TPosixCondition.cxx:30
 TPosixCondition.cxx:31
 TPosixCondition.cxx:32
 TPosixCondition.cxx:33
 TPosixCondition.cxx:34
 TPosixCondition.cxx:35
 TPosixCondition.cxx:36
 TPosixCondition.cxx:37
 TPosixCondition.cxx:38
 TPosixCondition.cxx:39
 TPosixCondition.cxx:40
 TPosixCondition.cxx:41
 TPosixCondition.cxx:42
 TPosixCondition.cxx:43
 TPosixCondition.cxx:44
 TPosixCondition.cxx:45
 TPosixCondition.cxx:46
 TPosixCondition.cxx:47
 TPosixCondition.cxx:48
 TPosixCondition.cxx:49
 TPosixCondition.cxx:50
 TPosixCondition.cxx:51
 TPosixCondition.cxx:52
 TPosixCondition.cxx:53
 TPosixCondition.cxx:54
 TPosixCondition.cxx:55
 TPosixCondition.cxx:56
 TPosixCondition.cxx:57
 TPosixCondition.cxx:58
 TPosixCondition.cxx:59
 TPosixCondition.cxx:60
 TPosixCondition.cxx:61
 TPosixCondition.cxx:62
 TPosixCondition.cxx:63
 TPosixCondition.cxx:64
 TPosixCondition.cxx:65
 TPosixCondition.cxx:66
 TPosixCondition.cxx:67
 TPosixCondition.cxx:68
 TPosixCondition.cxx:69
 TPosixCondition.cxx:70
 TPosixCondition.cxx:71
 TPosixCondition.cxx:72
 TPosixCondition.cxx:73
 TPosixCondition.cxx:74
 TPosixCondition.cxx:75
 TPosixCondition.cxx:76
 TPosixCondition.cxx:77
 TPosixCondition.cxx:78
 TPosixCondition.cxx:79
 TPosixCondition.cxx:80
 TPosixCondition.cxx:81
 TPosixCondition.cxx:82
 TPosixCondition.cxx:83
 TPosixCondition.cxx:84
 TPosixCondition.cxx:85
 TPosixCondition.cxx:86
 TPosixCondition.cxx:87
 TPosixCondition.cxx:88
 TPosixCondition.cxx:89
 TPosixCondition.cxx:90
 TPosixCondition.cxx:91
 TPosixCondition.cxx:92
 TPosixCondition.cxx:93
 TPosixCondition.cxx:94
 TPosixCondition.cxx:95
 TPosixCondition.cxx:96
 TPosixCondition.cxx:97
 TPosixCondition.cxx:98
 TPosixCondition.cxx:99
 TPosixCondition.cxx:100
 TPosixCondition.cxx:101
 TPosixCondition.cxx:102