[Lldb-commits] [lldb] [lldb] Assorted improvements to the Pipe class (PR #128719)

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Wed Feb 26 04:21:06 PST 2025


================
@@ -268,29 +267,24 @@ PipeWindows::GetReadNativeHandle() { return m_read; }
 HANDLE
 PipeWindows::GetWriteNativeHandle() { return m_write; }
 
-Status PipeWindows::ReadWithTimeout(void *buf, size_t size,
-                                    const std::chrono::microseconds &duration,
-                                    size_t &bytes_read) {
+llvm::Expected<size_t> PipeWindows::Read(void *buf, size_t size,
+                                         const Timeout<std::micro> &timeout) {
   if (!CanRead())
-    return Status(ERROR_INVALID_HANDLE, eErrorTypeWin32);
+    return Status(ERROR_INVALID_HANDLE, eErrorTypeWin32).takeError();
 
-  bytes_read = 0;
-  DWORD sys_bytes_read = 0;
-  BOOL result =
-      ::ReadFile(m_read, buf, size, &sys_bytes_read, &m_read_overlapped);
-  if (result) {
-    bytes_read = sys_bytes_read;
-    return Status();
-  }
+  DWORD bytes_read = 0;
+  BOOL result = ::ReadFile(m_read, buf, size, &bytes_read, &m_read_overlapped);
+  if (result)
+    return bytes_read;
 
   DWORD failure_error = ::GetLastError();
   if (failure_error != ERROR_IO_PENDING)
-    return Status(failure_error, eErrorTypeWin32);
+    return Status(failure_error, eErrorTypeWin32).takeError();
 
-  DWORD timeout = (duration == std::chrono::microseconds::zero())
-                      ? INFINITE
-                      : duration.count() / 1000;
-  DWORD wait_result = ::WaitForSingleObject(m_read_overlapped.hEvent, timeout);
+  DWORD timeout_msec =
+      timeout ? ceil<std::chrono::milliseconds>(*timeout).count() : INFINITE;
+  DWORD wait_result =
+      ::WaitForSingleObject(m_read_overlapped.hEvent, timeout_msec);
----------------
labath wrote:

Some form of multiplexing is possible, but not in the same way as on posix systems. Windows doesn't have a general `select` capability like the posix systems to (sockets are an exception, which I think was specially added for BSD compatibility). Windows multiplexing works more like async i/o on (some?) posix systems. You can't ask whether there is a data in pipe. You have to actually *try* to read the data (and then maybe give up after a while).

My long term plan for this is to integrate this functionality into the MainLoop class. However, that's tricky because we'd need to change the way the data is read. MainLoop can't just call us back when the data is ready. It actually has to (in one way or another) read the data (and then hand it back through the callback?). It's never been much of a priority, so I don't know when/if I can get around to it, but if you find yourself needing something like this, I think I'll be able to help you with the windows bits at least.

https://github.com/llvm/llvm-project/pull/128719


More information about the lldb-commits mailing list