Home | History | Annotate | Download | only in common
      1 //===-- Condition.cpp -------------------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include <errno.h>
     11 
     12 #include "lldb/Host/Condition.h"
     13 #include "lldb/Host/TimeValue.h"
     14 
     15 
     16 using namespace lldb_private;
     17 
     18 //----------------------------------------------------------------------
     19 // Default constructor
     20 //
     21 // The default constructor will initialize a new pthread condition
     22 // and maintain the condition in the object state.
     23 //----------------------------------------------------------------------
     24 Condition::Condition () :
     25     m_condition()
     26 {
     27     ::pthread_cond_init (&m_condition, NULL);
     28 }
     29 
     30 //----------------------------------------------------------------------
     31 // Destructor
     32 //
     33 // Destroys the pthread condition that the object owns.
     34 //----------------------------------------------------------------------
     35 Condition::~Condition ()
     36 {
     37     ::pthread_cond_destroy (&m_condition);
     38 }
     39 
     40 //----------------------------------------------------------------------
     41 // Unblock all threads waiting for a condition variable
     42 //----------------------------------------------------------------------
     43 int
     44 Condition::Broadcast ()
     45 {
     46     return ::pthread_cond_broadcast (&m_condition);
     47 }
     48 
     49 //----------------------------------------------------------------------
     50 // Get accessor to the pthread condition object
     51 //----------------------------------------------------------------------
     52 pthread_cond_t *
     53 Condition::GetCondition ()
     54 {
     55     return &m_condition;
     56 }
     57 
     58 //----------------------------------------------------------------------
     59 // Unblocks one thread waiting for the condition variable
     60 //----------------------------------------------------------------------
     61 int
     62 Condition::Signal ()
     63 {
     64     return ::pthread_cond_signal (&m_condition);
     65 }
     66 
     67 //----------------------------------------------------------------------
     68 // The Wait() function atomically blocks the current thread
     69 // waiting on the owned condition variable, and unblocks the mutex
     70 // specified by "mutex".  The waiting thread unblocks only after
     71 // another thread calls Signal(), or Broadcast() with the same
     72 // condition variable, or if "abstime" is valid (non-NULL) this
     73 // function will return when the system time reaches the time
     74 // specified in "abstime". If "abstime" is NULL this function will
     75 // wait for an infinite amount of time for the condition variable
     76 // to be signaled or broadcasted.
     77 //
     78 // The current thread re-acquires the lock on "mutex".
     79 //----------------------------------------------------------------------
     80 int
     81 Condition::Wait (Mutex &mutex, const TimeValue *abstime, bool *timed_out)
     82 {
     83     int err = 0;
     84     do
     85     {
     86         if (abstime && abstime->IsValid())
     87         {
     88             struct timespec abstime_ts = abstime->GetAsTimeSpec();
     89             err = ::pthread_cond_timedwait (&m_condition, mutex.GetMutex(), &abstime_ts);
     90         }
     91         else
     92             err = ::pthread_cond_wait (&m_condition, mutex.GetMutex());
     93     } while (err == EINTR);
     94 
     95     if (timed_out != NULL)
     96     {
     97         if (err == ETIMEDOUT)
     98             *timed_out = true;
     99         else
    100             *timed_out = false;
    101     }
    102 
    103 
    104     return err;
    105 }
    106 
    107