1 //===-- Condition.cpp -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "lldb/Host/Condition.h"
13 #include "lldb/Host/TimeValue.h"
16 using namespace lldb_private;
20 //----------------------------------------------------------------------
21 // Default constructor
23 // The default constructor will initialize a new pthread condition
24 // and maintain the condition in the object state.
25 //----------------------------------------------------------------------
26 Condition::Condition () :
29 ::pthread_cond_init (&m_condition, NULL);
32 //----------------------------------------------------------------------
35 // Destroys the pthread condition that the object owns.
36 //----------------------------------------------------------------------
37 Condition::~Condition ()
39 ::pthread_cond_destroy (&m_condition);
42 //----------------------------------------------------------------------
43 // Unblock all threads waiting for a condition variable
44 //----------------------------------------------------------------------
46 Condition::Broadcast ()
48 return ::pthread_cond_broadcast (&m_condition);
51 //----------------------------------------------------------------------
52 // Unblocks one thread waiting for the condition variable
53 //----------------------------------------------------------------------
57 return ::pthread_cond_signal (&m_condition);
60 //----------------------------------------------------------------------
61 // The Wait() function atomically blocks the current thread
62 // waiting on the owned condition variable, and unblocks the mutex
63 // specified by "mutex". The waiting thread unblocks only after
64 // another thread calls Signal(), or Broadcast() with the same
65 // condition variable, or if "abstime" is valid (non-NULL) this
66 // function will return when the system time reaches the time
67 // specified in "abstime". If "abstime" is NULL this function will
68 // wait for an infinite amount of time for the condition variable
69 // to be signaled or broadcasted.
71 // The current thread re-acquires the lock on "mutex".
72 //----------------------------------------------------------------------
74 Condition::Wait (Mutex &mutex, const TimeValue *abstime, bool *timed_out)
79 if (abstime && abstime->IsValid())
81 struct timespec abstime_ts = abstime->GetAsTimeSpec();
82 err = ::pthread_cond_timedwait (&m_condition, mutex.GetMutex(), &abstime_ts);
85 err = ::pthread_cond_wait (&m_condition, mutex.GetMutex());
86 } while (err == EINTR);
88 if (timed_out != NULL)
101 //----------------------------------------------------------------------
102 // Get accessor to the pthread condition object
103 //----------------------------------------------------------------------
105 Condition::GetCondition()