Skip to content

Commit a935e75

Browse files
committed
Adopting a different strategy for monitoring pipes and files.
For pipes, we use a monitor thread that performs a zero byte read to detect changes before signaling. For files, WaitFor{SingleObject,MultipleObjects}() is signaled if the file changes on disk.
1 parent 79c10b6 commit a935e75

File tree

9 files changed

+207
-123
lines changed

9 files changed

+207
-123
lines changed

lldb/include/lldb/Host/File.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ class File : public IOObject {
127127
/// \return
128128
/// a valid handle or IOObject::kInvalidHandleValue
129129
WaitableHandle GetWaitableHandle() override;
130-
bool HasReadableData() override;
131130

132131
/// Get the file specification for this file, if possible.
133132
///
@@ -401,7 +400,6 @@ class NativeFile : public File {
401400
Status Write(const void *buf, size_t &num_bytes) override;
402401
Status Close() override;
403402
WaitableHandle GetWaitableHandle() override;
404-
bool HasReadableData() override;
405403
Status GetFileSpec(FileSpec &file_spec) const override;
406404
int GetDescriptor() const override;
407405
FILE *GetStream() override;

lldb/include/lldb/Host/Socket.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ class Socket : public IOObject {
158158

159159
bool IsValid() const override { return m_socket != kInvalidSocketValue; }
160160
WaitableHandle GetWaitableHandle() override;
161-
bool HasReadableData() override;
162161

163162
static llvm::Expected<HostAndPort>
164163
DecodeHostAndPort(llvm::StringRef host_and_port);
@@ -186,7 +185,6 @@ class Socket : public IOObject {
186185

187186
SocketProtocol m_protocol;
188187
NativeSocket m_socket;
189-
WaitableHandle m_waitable_handle;
190188
bool m_should_close_fd;
191189
};
192190

lldb/include/lldb/Host/windows/MainLoopWindows.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ class MainLoopWindows : public MainLoopBase {
3131

3232
Status Run() override;
3333

34+
struct FdInfo {
35+
FdInfo(intptr_t event, Callback callback)
36+
: event(event), callback(callback) {}
37+
virtual ~FdInfo() {}
38+
virtual void WillPoll() {}
39+
virtual void DidPoll() {}
40+
virtual void Disarm() {}
41+
intptr_t event;
42+
Callback callback;
43+
};
44+
using FdInfoUP = std::unique_ptr<FdInfo>;
45+
3446
protected:
3547
void UnregisterReadObject(IOObject::WaitableHandle handle) override;
3648

@@ -39,11 +51,7 @@ class MainLoopWindows : public MainLoopBase {
3951
private:
4052
llvm::Expected<size_t> Poll();
4153

42-
struct FdInfo {
43-
lldb::IOObjectSP object_sp;
44-
Callback callback;
45-
};
46-
llvm::DenseMap<IOObject::WaitableHandle, FdInfo> m_read_fds;
54+
llvm::DenseMap<IOObject::WaitableHandle, FdInfoUP> m_read_fds;
4755
void *m_interrupt_event;
4856
};
4957

lldb/include/lldb/Utility/IOObject.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class IOObject {
4141
FDType GetFdType() const { return m_fd_type; }
4242

4343
virtual WaitableHandle GetWaitableHandle() = 0;
44-
virtual bool HasReadableData() = 0;
4544

4645
protected:
4746
FDType m_fd_type;

lldb/source/Host/common/File.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ IOObject::WaitableHandle File::GetWaitableHandle() {
118118
return IOObject::kInvalidHandleValue;
119119
}
120120

121-
bool File::HasReadableData() { return false; }
122-
123121
Status File::GetFileSpec(FileSpec &file_spec) const {
124122
file_spec.Clear();
125123
return std::error_code(ENOTSUP, std::system_category());
@@ -283,18 +281,6 @@ IOObject::WaitableHandle NativeFile::GetWaitableHandle() {
283281
#endif
284282
}
285283

286-
bool NativeFile::HasReadableData() {
287-
#ifdef _WIN32
288-
DWORD available_bytes = 0;
289-
return !PeekNamedPipe((HANDLE)_get_osfhandle(GetDescriptor()), NULL, 0, NULL,
290-
&available_bytes, NULL) ||
291-
available_bytes > 0;
292-
#else
293-
size_t buffer_size = 0;
294-
return ioctl(GetDescriptor(), FIONREAD, buffer_size) != -1 && buffer_size > 0;
295-
#endif
296-
}
297-
298284
FILE *NativeFile::GetStream() {
299285
ValueGuard stream_guard = StreamIsValid();
300286
if (!stream_guard) {

lldb/source/Host/common/JSONTransport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ ReadFull(IOObject &descriptor, size_t length,
4242
if (timeout && timeout_supported) {
4343
SelectHelper sh;
4444
sh.SetTimeout(*timeout);
45-
sh.FDSetRead(descriptor.GetWaitableHandle());
45+
sh.FDSetRead((lldb::socket_t)descriptor.GetWaitableHandle());
4646
Status status = sh.Select();
4747
if (status.Fail()) {
4848
// Convert timeouts into a specific error.

lldb/source/Host/common/Socket.cpp

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ bool Socket::FindProtocolByScheme(const char *scheme,
173173
Socket::Socket(SocketProtocol protocol, bool should_close)
174174
: IOObject(eFDTypeSocket), m_protocol(protocol),
175175
m_socket(kInvalidSocketValue),
176-
m_waitable_handle(IOObject::kInvalidHandleValue),
177176
m_should_close_fd(should_close) {}
178177

179178
Socket::~Socket() { Close(); }
@@ -318,39 +317,7 @@ Socket::DecodeHostAndPort(llvm::StringRef host_and_port) {
318317
}
319318

320319
IOObject::WaitableHandle Socket::GetWaitableHandle() {
321-
#ifdef _WIN32
322-
if (m_socket == kInvalidSocketValue)
323-
return kInvalidHandleValue;
324-
325-
if (m_waitable_handle == kInvalidHandleValue) {
326-
m_waitable_handle = WSACreateEvent();
327-
assert(m_waitable_handle != WSA_INVALID_EVENT);
328-
if (WSAEventSelect(m_socket, m_waitable_handle,
329-
FD_ACCEPT | FD_READ | FD_WRITE) != 0)
330-
return kInvalidHandleValue;
331-
}
332-
333-
return m_waitable_handle;
334-
#else
335-
return m_socket;
336-
#endif
337-
}
338-
339-
bool Socket::HasReadableData() {
340-
#ifdef _WIN32
341-
if (!IsValid() || m_waitable_handle == kInvalidHandleValue)
342-
return false;
343-
344-
WSANETWORKEVENTS events;
345-
if (WSAEnumNetworkEvents(m_socket, m_waitable_handle, &events) != 0)
346-
return false;
347-
348-
return events.lNetworkEvents & FD_CLOSE ||
349-
events.lNetworkEvents & FD_ACCEPT || events.lNetworkEvents & FD_READ;
350-
#else
351-
size_t buffer_size = 0;
352-
return ioctl(m_socket, FIONREAD, buffer_size) != -1 && buffer_size > 0;
353-
#endif
320+
return (IOObject::WaitableHandle)m_socket;
354321
}
355322

356323
Status Socket::Read(void *buf, size_t &num_bytes) {
@@ -416,14 +383,6 @@ Status Socket::Close() {
416383
Log *log = GetLog(LLDBLog::Connection);
417384
LLDB_LOGF(log, "%p Socket::Close (fd = %" PRIu64 ")",
418385
static_cast<void *>(this), static_cast<uint64_t>(m_socket));
419-
#ifdef _WIN32
420-
if (m_waitable_handle != kInvalidHandleValue) {
421-
if (WSACloseEvent(m_waitable_handle) == 0)
422-
m_waitable_handle = kInvalidHandleValue;
423-
else
424-
error = GetLastError();
425-
}
426-
#endif
427386
bool success = CloseSocket(m_socket) == 0;
428387
// A reference to a FD was passed in, set it to an invalid value
429388
m_socket = kInvalidSocketValue;

0 commit comments

Comments
 (0)