Use Timeout<> in the Listener class

Summary:
Communication classes use the Timeout<> class to specify the timeout. Listener
class was converted to chrono some time ago, but it used a different meaning for
a timeout of zero (Listener: infinite wait, Communication: no wait). Instead,
Listener provided separate functions which performed a non-blocking event read.

This converts the Listener class to the new Timeout class, to improve
consistency. It also allows us to get merge the different GetNextEvent*** and
WaitForEvent*** functions into one. No functional change intended.

Reviewers: jingham, clayborg, zturner

Subscribers: lldb-commits

Differential Revision: https://reviews.llvm.org/D27136

llvm-svn: 288238
diff --git a/lldb/source/Core/Communication.cpp b/lldb/source/Core/Communication.cpp
index 113a847..3d3abea 100644
--- a/lldb/source/Core/Communication.cpp
+++ b/lldb/source/Core/Communication.cpp
@@ -115,8 +115,6 @@
 size_t Communication::Read(void *dst, size_t dst_len,
                            const Timeout<std::micro> &timeout,
                            ConnectionStatus &status, Error *error_ptr) {
-  using std::chrono::microseconds;
-
   lldb_private::LogIfAnyCategoriesSet(
       LIBLLDB_LOG_COMMUNICATION,
       "%p Communication::Read (dst = %p, dst_len = %" PRIu64
@@ -143,9 +141,7 @@
     listener_sp->StartListeningForEvents(
         this, eBroadcastBitReadThreadGotBytes | eBroadcastBitReadThreadDidExit);
     EventSP event_sp;
-    microseconds listener_timeout =
-        timeout ? microseconds(*timeout) : microseconds(0);
-    while (listener_sp->WaitForEvent(listener_timeout, event_sp)) {
+    while (listener_sp->GetEvent(event_sp, timeout)) {
       const uint32_t event_type = event_sp->GetType();
       if (event_type & eBroadcastBitReadThreadGotBytes) {
         return GetCachedBytes(dst, dst_len);
@@ -386,7 +382,7 @@
 
   // Wait for the synchronization event.
   EventSP event_sp;
-  listener_sp->WaitForEvent(std::chrono::microseconds(0), event_sp);
+  listener_sp->GetEvent(event_sp, llvm::None);
 }
 
 void Communication::SetConnection(Connection *connection) {
diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp
index e66fb23..8f888a5 100644
--- a/lldb/source/Core/Debugger.cpp
+++ b/lldb/source/Core/Debugger.cpp
@@ -1535,7 +1535,7 @@
   bool done = false;
   while (!done) {
     EventSP event_sp;
-    if (listener_sp->WaitForEvent(std::chrono::microseconds(0), event_sp)) {
+    if (listener_sp->GetEvent(event_sp, llvm::None)) {
       if (event_sp) {
         Broadcaster *broadcaster = event_sp->GetBroadcaster();
         if (broadcaster) {
@@ -1616,7 +1616,7 @@
     // to wait an infinite amount of time for it (nullptr timeout as the first
     // parameter)
     lldb::EventSP event_sp;
-    listener_sp->WaitForEvent(std::chrono::microseconds(0), event_sp);
+    listener_sp->GetEvent(event_sp, llvm::None);
   }
   return m_event_handler_thread.IsJoinable();
 }
diff --git a/lldb/source/Core/IOHandler.cpp b/lldb/source/Core/IOHandler.cpp
index e123fdb..ed85c59 100644
--- a/lldb/source/Core/IOHandler.cpp
+++ b/lldb/source/Core/IOHandler.cpp
@@ -1847,7 +1847,7 @@
           // Just a timeout from using halfdelay(), check for events
           EventSP event_sp;
           while (listener_sp->PeekAtNextEvent()) {
-            listener_sp->GetNextEvent(event_sp);
+            listener_sp->GetEvent(event_sp, std::chrono::seconds(0));
 
             if (event_sp) {
               Broadcaster *broadcaster = event_sp->GetBroadcaster();
diff --git a/lldb/source/Core/Listener.cpp b/lldb/source/Core/Listener.cpp
index 216a302..3adb677 100644
--- a/lldb/source/Core/Listener.cpp
+++ b/lldb/source/Core/Listener.cpp
@@ -345,44 +345,17 @@
   return nullptr;
 }
 
-bool Listener::GetNextEventInternal(
-    Broadcaster *broadcaster,             // nullptr for any broadcaster
-    const ConstString *broadcaster_names, // nullptr for any event
-    uint32_t num_broadcaster_names, uint32_t event_type_mask,
-    EventSP &event_sp) {
-  std::unique_lock<std::mutex> guard(m_events_mutex);
-  return FindNextEventInternal(guard, broadcaster, broadcaster_names,
-                               num_broadcaster_names, event_type_mask, event_sp,
-                               true);
-}
-
-bool Listener::GetNextEvent(EventSP &event_sp) {
-  return GetNextEventInternal(nullptr, nullptr, 0, 0, event_sp);
-}
-
-bool Listener::GetNextEventForBroadcaster(Broadcaster *broadcaster,
-                                          EventSP &event_sp) {
-  return GetNextEventInternal(broadcaster, nullptr, 0, 0, event_sp);
-}
-
-bool Listener::GetNextEventForBroadcasterWithType(Broadcaster *broadcaster,
-                                                  uint32_t event_type_mask,
-                                                  EventSP &event_sp) {
-  return GetNextEventInternal(broadcaster, nullptr, 0, event_type_mask,
-                              event_sp);
-}
-
-bool Listener::WaitForEventsInternal(
-    const std::chrono::microseconds &timeout,
+bool Listener::GetEventInternal(
+    const Timeout<std::micro> &timeout,
     Broadcaster *broadcaster,             // nullptr for any broadcaster
     const ConstString *broadcaster_names, // nullptr for any event
     uint32_t num_broadcaster_names, uint32_t event_type_mask,
     EventSP &event_sp) {
   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS));
   if (log != nullptr)
-    log->Printf("%p Listener::WaitForEventsInternal (timeout = %llu us) for %s",
-                static_cast<void *>(this),
-                static_cast<unsigned long long>(timeout.count()),
+    log->Printf("%p Listener::GetEventInternal (timeout = %llu us) for %s",
+                static_cast<void *>(this), static_cast<unsigned long long>(
+                                               timeout ? timeout->count() : -1),
                 m_name.c_str());
 
   std::unique_lock<std::mutex> lock(m_events_mutex);
@@ -394,23 +367,22 @@
       return true;
     } else {
       std::cv_status result = std::cv_status::no_timeout;
-      if (timeout == std::chrono::microseconds(0))
+      if (!timeout)
         m_events_condition.wait(lock);
       else
-        result = m_events_condition.wait_for(lock, timeout);
+        result = m_events_condition.wait_for(lock, *timeout);
 
       if (result == std::cv_status::timeout) {
         log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS);
         if (log)
-          log->Printf("%p Listener::WaitForEventsInternal() timed out for %s",
+          log->Printf("%p Listener::GetEventInternal() timed out for %s",
                       static_cast<void *>(this), m_name.c_str());
         return false;
       } else if (result != std::cv_status::no_timeout) {
         log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS);
         if (log)
-          log->Printf(
-              "%p Listener::WaitForEventsInternal() unknown error for %s",
-              static_cast<void *>(this), m_name.c_str());
+          log->Printf("%p Listener::GetEventInternal() unknown error for %s",
+                      static_cast<void *>(this), m_name.c_str());
         return false;
       }
     }
@@ -419,22 +391,21 @@
   return false;
 }
 
-bool Listener::WaitForEventForBroadcasterWithType(
-    const std::chrono::microseconds &timeout, Broadcaster *broadcaster,
-    uint32_t event_type_mask, EventSP &event_sp) {
-  return WaitForEventsInternal(timeout, broadcaster, nullptr, 0,
-                               event_type_mask, event_sp);
+bool Listener::GetEventForBroadcasterWithType(
+    Broadcaster *broadcaster, uint32_t event_type_mask, EventSP &event_sp,
+    const Timeout<std::micro> &timeout) {
+  return GetEventInternal(timeout, broadcaster, nullptr, 0, event_type_mask,
+                          event_sp);
 }
 
-bool Listener::WaitForEventForBroadcaster(
-    const std::chrono::microseconds &timeout, Broadcaster *broadcaster,
-    EventSP &event_sp) {
-  return WaitForEventsInternal(timeout, broadcaster, nullptr, 0, 0, event_sp);
+bool Listener::GetEventForBroadcaster(Broadcaster *broadcaster,
+                                      EventSP &event_sp,
+                                      const Timeout<std::micro> &timeout) {
+  return GetEventInternal(timeout, broadcaster, nullptr, 0, 0, event_sp);
 }
 
-bool Listener::WaitForEvent(const std::chrono::microseconds &timeout,
-                            EventSP &event_sp) {
-  return WaitForEventsInternal(timeout, nullptr, nullptr, 0, 0, event_sp);
+bool Listener::GetEvent(EventSP &event_sp, const Timeout<std::micro> &timeout) {
+  return GetEventInternal(timeout, nullptr, nullptr, 0, 0, event_sp);
 }
 
 size_t Listener::HandleBroadcastEvent(EventSP &event_sp) {