Skip to content

Commit 0f07158

Browse files
committed
Removing disk file support and reverting unrelated changes.
1 parent a935e75 commit 0f07158

File tree

5 files changed

+111
-112
lines changed

5 files changed

+111
-112
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,19 @@ 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() {}
34+
class IOEvent {
35+
public:
36+
IOEvent(IOObject::WaitableHandle event) : m_event(event) {}
37+
virtual ~IOEvent() {}
3838
virtual void WillPoll() {}
3939
virtual void DidPoll() {}
4040
virtual void Disarm() {}
41-
intptr_t event;
42-
Callback callback;
41+
IOObject::WaitableHandle GetHandle() { return m_event; }
42+
43+
protected:
44+
IOObject::WaitableHandle m_event;
4345
};
44-
using FdInfoUP = std::unique_ptr<FdInfo>;
46+
using IOEventUP = std::unique_ptr<IOEvent>;
4547

4648
protected:
4749
void UnregisterReadObject(IOObject::WaitableHandle handle) override;
@@ -51,7 +53,11 @@ class MainLoopWindows : public MainLoopBase {
5153
private:
5254
llvm::Expected<size_t> Poll();
5355

54-
llvm::DenseMap<IOObject::WaitableHandle, FdInfoUP> m_read_fds;
56+
struct FdInfo {
57+
IOEventUP event;
58+
Callback callback;
59+
};
60+
llvm::DenseMap<IOObject::WaitableHandle, FdInfo> m_read_fds;
5561
void *m_interrupt_event;
5662
};
5763

lldb/source/Host/common/Socket.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@
3131
#include <netdb.h>
3232
#include <netinet/in.h>
3333
#include <netinet/tcp.h>
34-
#include <sys/ioctl.h>
3534
#include <sys/socket.h>
36-
#include <sys/stat.h>
3735
#include <sys/un.h>
38-
#include <termios.h>
3936
#include <unistd.h>
4037
#endif
4138

@@ -172,8 +169,7 @@ bool Socket::FindProtocolByScheme(const char *scheme,
172169

173170
Socket::Socket(SocketProtocol protocol, bool should_close)
174171
: IOObject(eFDTypeSocket), m_protocol(protocol),
175-
m_socket(kInvalidSocketValue),
176-
m_should_close_fd(should_close) {}
172+
m_socket(kInvalidSocketValue), m_should_close_fd(should_close) {}
177173

178174
Socket::~Socket() { Close(); }
179175

@@ -383,6 +379,7 @@ Status Socket::Close() {
383379
Log *log = GetLog(LLDBLog::Connection);
384380
LLDB_LOGF(log, "%p Socket::Close (fd = %" PRIu64 ")",
385381
static_cast<void *>(this), static_cast<uint64_t>(m_socket));
382+
386383
bool success = CloseSocket(m_socket) == 0;
387384
// A reference to a FD was passed in, set it to an invalid value
388385
m_socket = kInvalidSocketValue;

lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -452,16 +452,14 @@ ConnectionFileDescriptor::BytesAvailable(const Timeout<std::micro> &timeout,
452452
select_helper.SetTimeout(*timeout);
453453

454454
// FIXME: Migrate to MainLoop.
455+
select_helper.FDSetRead((lldb::socket_t)handle);
455456
#if defined(_WIN32)
456-
if (const auto *sock = static_cast<Socket *>(m_io_sp.get()))
457-
select_helper.FDSetRead((socket_t)sock->GetNativeSocket());
458457
// select() won't accept pipes on Windows. The entire Windows codepath
459458
// needs to be converted over to using WaitForMultipleObjects and event
460459
// HANDLEs, but for now at least this will allow ::select() to not return
461460
// an error.
462461
const bool have_pipe_fd = false;
463462
#else
464-
select_helper.FDSetRead(handle);
465463
const bool have_pipe_fd = pipe_fd >= 0;
466464
#endif
467465
if (have_pipe_fd)
@@ -496,12 +494,7 @@ ConnectionFileDescriptor::BytesAvailable(const Timeout<std::micro> &timeout,
496494
break; // Lets keep reading to until we timeout
497495
}
498496
} else {
499-
#if defined(_WIN32)
500-
if (const auto *sock = static_cast<Socket *>(m_io_sp.get());
501-
select_helper.FDIsSetRead(sock->GetNativeSocket()))
502-
#else
503-
if (select_helper.FDIsSetRead(handle))
504-
#endif
497+
if (select_helper.FDIsSetRead((lldb::socket_t)handle))
505498
return eConnectionStatusSuccess;
506499

507500
if (select_helper.FDIsSetRead(pipe_fd)) {

lldb/source/Host/windows/MainLoopWindows.cpp

Lines changed: 58 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
using namespace lldb;
2525
using namespace lldb_private;
2626

27-
namespace {
28-
29-
DWORD ToTimeout(std::optional<MainLoopWindows::TimePoint> point) {
27+
static DWORD ToTimeout(std::optional<MainLoopWindows::TimePoint> point) {
3028
using namespace std::chrono;
3129

3230
if (!point)
@@ -36,40 +34,39 @@ DWORD ToTimeout(std::optional<MainLoopWindows::TimePoint> point) {
3634
return ceil<milliseconds>(dur).count();
3735
}
3836

39-
class PipeFdInfo : public MainLoopWindows::FdInfo {
37+
namespace {
38+
39+
class PipeEvent : public MainLoopWindows::IOEvent {
4040
public:
41-
explicit PipeFdInfo(HANDLE handle, MainLoopBase::Callback callback)
42-
: FdInfo((intptr_t)CreateEventW(NULL, /*bManualReset=*/FALSE,
43-
/*bInitialState=*/FALSE, NULL),
44-
callback),
45-
handle(handle), ready(CreateEventW(NULL, /*bManualReset=*/FALSE,
46-
/*bInitialState=*/FALSE, NULL)) {
41+
explicit PipeEvent(HANDLE handle)
42+
: IOEvent((IOObject::WaitableHandle)CreateEventW(NULL, /*bManualReset=*/FALSE,
43+
/*bInitialState=*/FALSE, NULL)),
44+
m_handle(handle), m_ready(CreateEventW(NULL, /*bManualReset=*/FALSE,
45+
/*bInitialState=*/FALSE, NULL)) {
4746
assert(event && ready);
4847
}
4948

50-
~PipeFdInfo() override {
51-
if (monitor_thread.joinable()) {
52-
stopped = true;
53-
SetEvent(ready);
49+
~PipeEvent() override {
50+
if (m_monitor_thread.joinable()) {
51+
m_stopped = true;
52+
SetEvent(m_ready);
5453
// Keep trying to cancel ReadFile() until the thread exits.
5554
do {
56-
CancelIoEx((HANDLE)handle, /*lpOverlapped=*/NULL);
57-
} while (WaitForSingleObject(monitor_thread.native_handle(), 1) ==
55+
CancelIoEx((HANDLE)m_handle, /*lpOverlapped=*/NULL);
56+
} while (WaitForSingleObject(m_monitor_thread.native_handle(), 1) ==
5857
WAIT_TIMEOUT);
59-
monitor_thread.join();
58+
m_monitor_thread.join();
6059
}
61-
CloseHandle((HANDLE)event);
62-
CloseHandle(ready);
60+
CloseHandle((HANDLE)m_event);
61+
CloseHandle(m_ready);
6362
}
6463

65-
void WillPoll() override {
66-
if (!monitor_thread.joinable())
67-
monitor_thread = std::thread(&PipeFdInfo::Monitor, this);
64+
void WillPoll() override {
65+
if (!m_monitor_thread.joinable())
66+
m_monitor_thread = std::thread(&PipeEvent::Monitor, this);
6867
}
6968

70-
void Disarm() override {
71-
SetEvent(ready);
72-
}
69+
void Disarm() override { SetEvent(m_ready); }
7370

7471
/// Monitors the handle performing a zero byte read to determine when data is
7572
/// avaiable.
@@ -82,16 +79,17 @@ class PipeFdInfo : public MainLoopWindows::FdInfo {
8279
// available in the pipe. The pipe must be PIPE_WAIT or this thread
8380
// will spin.
8481
BOOL success =
85-
ReadFile(handle, buf, /*nNumberOfBytesToRead=*/0, &bytes_read, &ov);
82+
ReadFile(m_handle, buf, /*nNumberOfBytesToRead=*/0, &bytes_read, &ov);
8683
DWORD bytes_available = 0;
8784
DWORD err = GetLastError();
8885
if (!success && err == ERROR_IO_PENDING) {
89-
success = GetOverlappedResult(handle, &ov, &bytes_read,
86+
success = GetOverlappedResult(m_handle, &ov, &bytes_read,
9087
/*bWait=*/TRUE);
9188
err = GetLastError();
9289
}
9390
if (success) {
94-
success = PeekNamedPipe(handle, NULL, 0, NULL, &bytes_available, NULL);
91+
success =
92+
PeekNamedPipe(m_handle, NULL, 0, NULL, &bytes_available, NULL);
9593
err = GetLastError();
9694
}
9795
if (success) {
@@ -108,51 +106,45 @@ class PipeFdInfo : public MainLoopWindows::FdInfo {
108106
continue;
109107
}
110108

111-
SetEvent((HANDLE)event);
109+
SetEvent((HANDLE)m_event);
112110

113111
// Wait until the current read is consumed before doing the next read.
114-
WaitForSingleObject(ready, INFINITE);
115-
} while (!stopped);
112+
WaitForSingleObject(m_ready, INFINITE);
113+
} while (!m_stopped);
116114
}
117115

118-
HANDLE handle;
119-
HANDLE ready;
120-
std::thread monitor_thread;
121-
std::atomic<bool> stopped = false;
116+
private:
117+
HANDLE m_handle;
118+
HANDLE m_ready;
119+
std::thread m_monitor_thread;
120+
std::atomic<bool> m_stopped = false;
122121
};
123122

124-
class SocketFdInfo : public MainLoopWindows::FdInfo {
123+
class SocketEvent : public MainLoopWindows::IOEvent {
125124
public:
126-
explicit SocketFdInfo(SOCKET socket, MainLoopBase::Callback callback)
127-
: FdInfo((intptr_t)WSACreateEvent(), callback), socket(socket) {
125+
explicit SocketEvent(SOCKET socket)
126+
: IOEvent((IOObject::WaitableHandle)WSACreateEvent()), m_socket(socket) {
128127
assert(event != WSA_INVALID_EVENT);
129128
}
130129

131-
~SocketFdInfo() override { WSACloseEvent((HANDLE)event); }
130+
~SocketEvent() override { WSACloseEvent((HANDLE)m_event); }
132131

133132
void WillPoll() {
134-
int result = WSAEventSelect(socket, (HANDLE)event, FD_READ | FD_ACCEPT | FD_CLOSE);
133+
int result =
134+
WSAEventSelect(m_socket, (HANDLE)m_event, FD_READ | FD_ACCEPT | FD_CLOSE);
135135
assert(result == 0);
136136
UNUSED_IF_ASSERT_DISABLED(result);
137137
}
138138

139139
void DidPoll() {
140-
int result = WSAEventSelect(socket, WSA_INVALID_EVENT, 0);
140+
int result = WSAEventSelect(m_socket, WSA_INVALID_EVENT, 0);
141141
assert(result == 0);
142142
UNUSED_IF_ASSERT_DISABLED(result);
143143
}
144144

145-
void Disarm() override {
146-
WSAResetEvent((HANDLE)event);
147-
}
148-
149-
SOCKET socket;
150-
};
145+
void Disarm() override { WSAResetEvent((HANDLE)m_event); }
151146

152-
class FileFdInfo : public MainLoopWindows::FdInfo {
153-
public:
154-
explicit FileFdInfo(HANDLE handle, MainLoopBase::Callback callback)
155-
: FdInfo((intptr_t)handle, callback) {}
147+
SOCKET m_socket;
156148
};
157149

158150
} // namespace
@@ -173,8 +165,8 @@ llvm::Expected<size_t> MainLoopWindows::Poll() {
173165
std::vector<HANDLE> events;
174166
events.reserve(m_read_fds.size() + 1);
175167
for (auto &[_, fd_info] : m_read_fds) {
176-
fd_info->WillPoll();
177-
events.push_back((HANDLE)fd_info->event);
168+
fd_info.event->WillPoll();
169+
events.push_back((HANDLE)fd_info.event->GetHandle());
178170
}
179171
events.push_back(m_interrupt_event);
180172

@@ -183,7 +175,7 @@ llvm::Expected<size_t> MainLoopWindows::Poll() {
183175
ToTimeout(GetNextWakeupTime()), FALSE);
184176

185177
for (auto &[_, fd_info] : m_read_fds) {
186-
fd_info->DidPoll();
178+
fd_info.event->DidPoll();
187179
}
188180

189181
if (result >= WSA_WAIT_EVENT_0 && result < WSA_WAIT_EVENT_0 + events.size())
@@ -215,23 +207,16 @@ MainLoopWindows::RegisterReadObject(const IOObjectSP &object_sp,
215207
}
216208

217209
if (object_sp->GetFdType() == IOObject::eFDTypeSocket)
218-
m_read_fds[waitable_handle] =
219-
std::make_unique<SocketFdInfo>((SOCKET)waitable_handle, callback);
220-
else
221-
switch (GetFileType(waitable_handle)) {
222-
case FILE_TYPE_PIPE:
223-
m_read_fds[waitable_handle] =
224-
std::make_unique<PipeFdInfo>((HANDLE)waitable_handle, callback);
225-
break;
226-
case FILE_TYPE_DISK:
227-
m_read_fds[waitable_handle] =
228-
std::make_unique<FileFdInfo>((HANDLE)waitable_handle, callback);
229-
break;
230-
default:
231-
error = Status::FromErrorStringWithFormat("Unsupported file type %d",
232-
GetFileType(waitable_handle));
233-
return nullptr;
234-
}
210+
m_read_fds[waitable_handle] = {
211+
std::make_unique<SocketEvent>((SOCKET)waitable_handle), callback};
212+
else if (GetFileType(waitable_handle) == FILE_TYPE_PIPE)
213+
m_read_fds[waitable_handle] = {
214+
std::make_unique<PipeEvent>((HANDLE)waitable_handle), callback};
215+
else {
216+
error = Status::FromErrorStringWithFormat("Unsupported file type %d",
217+
GetFileType(waitable_handle));
218+
return nullptr;
219+
}
235220

236221
return CreateReadHandle(object_sp);
237222
}
@@ -254,8 +239,8 @@ Status MainLoopWindows::Run() {
254239

255240
if (*signaled_event < m_read_fds.size()) {
256241
auto &KV = *std::next(m_read_fds.begin(), *signaled_event);
257-
KV.second->Disarm();
258-
KV.second->callback(*this); // Do the work.
242+
KV.second.event->Disarm();
243+
KV.second.callback(*this); // Do the work.
259244
} else {
260245
assert(*signaled_event == m_read_fds.size());
261246
WSAResetEvent(m_interrupt_event);

0 commit comments

Comments
 (0)