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"
14 #include "lldb/Host/windows/windows.h"
17 using namespace lldb_private;
19 //----------------------------------------------------------------------
20 // Default constructor
22 // The default constructor will initialize a new pthread condition
23 // and maintain the condition in the object state.
24 //----------------------------------------------------------------------
25 Condition::Condition () :
28 m_condition = static_cast<PCONDITION_VARIABLE>(malloc(sizeof(CONDITION_VARIABLE)));
29 InitializeConditionVariable(static_cast<PCONDITION_VARIABLE>(m_condition));
32 //----------------------------------------------------------------------
35 // Destroys the pthread condition that the object owns.
36 //----------------------------------------------------------------------
37 Condition::~Condition ()
42 //----------------------------------------------------------------------
43 // Unblock all threads waiting for a condition variable
44 //----------------------------------------------------------------------
46 Condition::Broadcast ()
48 WakeAllConditionVariable(static_cast<PCONDITION_VARIABLE>(m_condition));
52 //----------------------------------------------------------------------
53 // Unblocks one thread waiting for the condition variable
54 //----------------------------------------------------------------------
58 WakeConditionVariable(static_cast<PCONDITION_VARIABLE>(m_condition));
62 //----------------------------------------------------------------------
63 // The Wait() function atomically blocks the current thread
64 // waiting on the owned condition variable, and unblocks the mutex
65 // specified by "mutex". The waiting thread unblocks only after
66 // another thread calls Signal(), or Broadcast() with the same
67 // condition variable, or if "abstime" is valid (non-NULL) this
68 // function will return when the system time reaches the time
69 // specified in "abstime". If "abstime" is NULL this function will
70 // wait for an infinite amount of time for the condition variable
71 // to be signaled or broadcasted.
73 // The current thread re-acquires the lock on "mutex".
74 //----------------------------------------------------------------------
76 Condition::Wait (Mutex &mutex, const TimeValue *abstime, bool *timed_out)
78 DWORD wait = INFINITE;
79 if (abstime != NULL) {
80 int wval = (*abstime - TimeValue::Now()) / 1000000;
81 if (wval < 0) wval = 0;
86 int err = SleepConditionVariableCS(static_cast<PCONDITION_VARIABLE>(m_condition), static_cast<PCRITICAL_SECTION>(mutex.m_mutex), wait);
88 if (timed_out != NULL)
90 if ((err == 0) && GetLastError() == ERROR_TIMEOUT)