]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Host/windows/Condition.cpp
Vendor import of lldb trunk r256945:
[FreeBSD/FreeBSD.git] / source / Host / windows / Condition.cpp
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 #include "lldb/Host/windows/windows.h"
15
16
17 using namespace lldb_private;
18
19 //----------------------------------------------------------------------
20 // Default constructor
21 //
22 // The default constructor will initialize a new pthread condition
23 // and maintain the condition in the object state.
24 //----------------------------------------------------------------------
25 Condition::Condition () :
26     m_condition()
27 {
28     m_condition = static_cast<PCONDITION_VARIABLE>(malloc(sizeof(CONDITION_VARIABLE)));
29     InitializeConditionVariable(static_cast<PCONDITION_VARIABLE>(m_condition));
30 }
31
32 //----------------------------------------------------------------------
33 // Destructor
34 //
35 // Destroys the pthread condition that the object owns.
36 //----------------------------------------------------------------------
37 Condition::~Condition ()
38 {
39     free(m_condition);
40 }
41
42 //----------------------------------------------------------------------
43 // Unblock all threads waiting for a condition variable
44 //----------------------------------------------------------------------
45 int
46 Condition::Broadcast ()
47 {
48     WakeAllConditionVariable(static_cast<PCONDITION_VARIABLE>(m_condition));
49     return 0;
50 }
51
52 //----------------------------------------------------------------------
53 // Unblocks one thread waiting for the condition variable
54 //----------------------------------------------------------------------
55 int
56 Condition::Signal ()
57 {
58     WakeConditionVariable(static_cast<PCONDITION_VARIABLE>(m_condition));
59     return 0;
60 }
61
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.
72 //
73 // The current thread re-acquires the lock on "mutex".
74 //----------------------------------------------------------------------
75 int
76 Condition::Wait (Mutex &mutex, const TimeValue *abstime, bool *timed_out)
77 {
78     DWORD wait = INFINITE;
79     if (abstime != NULL) {
80         int wval = (*abstime - TimeValue::Now()) / 1000000;
81         if (wval < 0) wval = 0;
82
83         wait = wval;
84     }
85
86     int err = SleepConditionVariableCS(static_cast<PCONDITION_VARIABLE>(m_condition), static_cast<PCRITICAL_SECTION>(mutex.m_mutex), wait);
87
88     if (timed_out != NULL)
89     {
90         if ((err == 0) && GetLastError() == ERROR_TIMEOUT)
91             *timed_out = true;
92         else
93             *timed_out = false;
94     }
95
96     return err == 0;
97 }
98