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 #ifndef _WIN32
19
20 //----------------------------------------------------------------------
21 // Default constructor
22 //
23 // The default constructor will initialize a new pthread condition
24 // and maintain the condition in the object state.
25 //----------------------------------------------------------------------
Condition()26 Condition::Condition () :
27 m_condition()
28 {
29 ::pthread_cond_init (&m_condition, NULL);
30 }
31
32 //----------------------------------------------------------------------
33 // Destructor
34 //
35 // Destroys the pthread condition that the object owns.
36 //----------------------------------------------------------------------
~Condition()37 Condition::~Condition ()
38 {
39 ::pthread_cond_destroy (&m_condition);
40 }
41
42 //----------------------------------------------------------------------
43 // Unblock all threads waiting for a condition variable
44 //----------------------------------------------------------------------
45 int
Broadcast()46 Condition::Broadcast ()
47 {
48 return ::pthread_cond_broadcast (&m_condition);
49 }
50
51 //----------------------------------------------------------------------
52 // Unblocks one thread waiting for the condition variable
53 //----------------------------------------------------------------------
54 int
Signal()55 Condition::Signal ()
56 {
57 return ::pthread_cond_signal (&m_condition);
58 }
59
60 /* convert struct timeval to ms(milliseconds) */
tv2ms(struct timeval a)61 static unsigned long int tv2ms(struct timeval a) {
62 return ((a.tv_sec * 1000) + (a.tv_usec / 1000));
63 }
64
65 //----------------------------------------------------------------------
66 // The Wait() function atomically blocks the current thread
67 // waiting on the owned condition variable, and unblocks the mutex
68 // specified by "mutex". The waiting thread unblocks only after
69 // another thread calls Signal(), or Broadcast() with the same
70 // condition variable, or if "abstime" is valid (non-NULL) this
71 // function will return when the system time reaches the time
72 // specified in "abstime". If "abstime" is NULL this function will
73 // wait for an infinite amount of time for the condition variable
74 // to be signaled or broadcasted.
75 //
76 // The current thread re-acquires the lock on "mutex".
77 //----------------------------------------------------------------------
78 int
Wait(Mutex & mutex,const TimeValue * abstime,bool * timed_out)79 Condition::Wait (Mutex &mutex, const TimeValue *abstime, bool *timed_out)
80 {
81 int err = 0;
82 do
83 {
84 if (abstime && abstime->IsValid())
85 {
86 struct timespec abstime_ts = abstime->GetAsTimeSpec();
87 err = ::pthread_cond_timedwait (&m_condition, mutex.GetMutex(), &abstime_ts);
88 }
89 else
90 err = ::pthread_cond_wait (&m_condition, mutex.GetMutex());
91 } while (err == EINTR);
92
93 if (timed_out != NULL)
94 {
95 if (err == ETIMEDOUT)
96 *timed_out = true;
97 else
98 *timed_out = false;
99 }
100
101 return err;
102 }
103
104 #endif
105
106 //----------------------------------------------------------------------
107 // Get accessor to the pthread condition object
108 //----------------------------------------------------------------------
109 lldb::condition_t *
GetCondition()110 Condition::GetCondition()
111 {
112 return &m_condition;
113 }
114