[Lldb-commits] [PATCH] D131159: [WIP][lldb] Use WSAEventSelect for MainLoop polling

Pavel Labath via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Aug 4 05:35:04 PDT 2022


labath created this revision.
labath added a reviewer: mgorny.
Herald added a project: All.
labath requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131159

Files:
  lldb/source/Host/common/MainLoop.cpp


Index: lldb/source/Host/common/MainLoop.cpp
===================================================================
--- lldb/source/Host/common/MainLoop.cpp
+++ lldb/source/Host/common/MainLoop.cpp
@@ -69,6 +69,67 @@
 }
 #endif
 
+#ifdef _WIN32
+class MainLoop::RunImpl {
+public:
+  RunImpl(MainLoop &loop);
+  ~RunImpl();
+
+  Status Poll();
+
+  void ProcessEvents();
+
+private:
+  MainLoop &loop;
+  llvm::Optional<size_t> signaled_event;
+};
+
+MainLoop::RunImpl::RunImpl(MainLoop &loop) : loop(loop) {
+}
+
+MainLoop::RunImpl::~RunImpl() {
+}
+
+Status MainLoop::RunImpl::Poll() {
+  std::vector<WSAEVENT> read_events;
+  read_events.reserve(loop.m_read_fds.size());
+  for(auto &fd : loop.m_read_fds) {
+    WSAEVENT event = WSACreateEvent();
+    assert(event != WSA_INVALID_EVENT);
+
+    int result = WSAEventSelect(fd.first, event, FD_READ | FD_ACCEPT | FD_CLOSE);
+    assert(result == 0);
+
+    read_events.push_back(event);
+  }
+
+  DWORD result = WSAWaitForMultipleEvents(read_events.size(),
+      read_events.data(), FALSE, WSA_INFINITE, FALSE);
+
+  for(auto &fd : loop.m_read_fds) {
+    int result = WSAEventSelect(fd.first, WSA_INVALID_EVENT, 0);
+    assert(result == 0);
+  }
+
+  for (auto &event: read_events) {
+    BOOL result = WSACloseEvent(event);
+    assert(result == TRUE);
+  }
+
+  if (result >= WSA_WAIT_EVENT_0 && result < WSA_WAIT_EVENT_0 + read_events.size()) {
+    signaled_event = result - WSA_WAIT_EVENT_0;
+    return Status();
+  }
+  return Status("WSAWaitForMultipleEvents failed");
+}
+
+void MainLoop::RunImpl::ProcessEvents() {
+  assert(signaled_event);
+  auto &fd_info = *std::next(loop.m_read_fds.begin(), *signaled_event);
+
+  loop.ProcessReadObject(fd_info.first);
+}
+#else
 class MainLoop::RunImpl {
 public:
   RunImpl(MainLoop &loop);
@@ -254,6 +315,7 @@
   }
 }
 #endif
+#endif
 
 MainLoop::MainLoop() : m_terminate_request(false) {
 #if HAVE_SYS_EVENT_H


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131159.449939.patch
Type: text/x-patch
Size: 1916 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20220804/714eaaae/attachment.bin>


More information about the lldb-commits mailing list