13#define _DARWIN_UNLIMITED_SELECT
22#include "llvm/ADT/DenseMap.h"
35#include <sys/select.h>
40 : m_fd_map(), m_end_time()
45 using namespace std::chrono;
46 m_end_time = steady_clock::time_point(steady_clock::now() + timeout);
64 return pos->second.read_is_set;
72 return pos->second.write_is_set;
80 return pos->second.error_is_set;
90 vold = std::max(*vold, vnew);
103 std::optional<lldb::socket_t> max_read_fd;
104 std::optional<lldb::socket_t> max_write_fd;
105 std::optional<lldb::socket_t> max_error_fd;
106 std::optional<lldb::socket_t> max_fd;
108 pair.second.PrepareForSelect();
110#if !defined(__APPLE__) && !defined(_WIN32)
111 lldbassert(fd <
static_cast<int>(FD_SETSIZE));
112 if (fd >=
static_cast<int>(FD_SETSIZE)) {
113 error.SetErrorStringWithFormat(
"%i is too large for select()", fd);
117 if (pair.second.read_set)
119 if (pair.second.write_set)
121 if (pair.second.error_set)
127 error.SetErrorString(
"no valid file descriptors");
131 const unsigned nfds =
static_cast<unsigned>(*max_fd) + 1;
132 fd_set *read_fdset_ptr =
nullptr;
133 fd_set *write_fdset_ptr =
nullptr;
134 fd_set *error_fdset_ptr =
nullptr;
136#if defined(__APPLE__)
137 llvm::SmallVector<fd_set, 1> read_fdset;
138 llvm::SmallVector<fd_set, 1> write_fdset;
139 llvm::SmallVector<fd_set, 1> error_fdset;
141 if (max_read_fd.has_value()) {
142 read_fdset.resize((nfds / FD_SETSIZE) + 1);
143 read_fdset_ptr = read_fdset.data();
145 if (max_write_fd.has_value()) {
146 write_fdset.resize((nfds / FD_SETSIZE) + 1);
147 write_fdset_ptr = write_fdset.data();
149 if (max_error_fd.has_value()) {
150 error_fdset.resize((nfds / FD_SETSIZE) + 1);
151 error_fdset_ptr = error_fdset.data();
153 for (
auto &fd_set : read_fdset)
155 for (
auto &fd_set : write_fdset)
157 for (
auto &fd_set : error_fdset)
165 FD_ZERO(&read_fdset);
166 read_fdset_ptr = &read_fdset;
169 FD_ZERO(&write_fdset);
170 write_fdset_ptr = &write_fdset;
173 FD_ZERO(&error_fdset);
174 error_fdset_ptr = &error_fdset;
181 if (pair.second.read_set)
182 FD_SET(fd, read_fdset_ptr);
184 if (pair.second.write_set)
185 FD_SET(fd, write_fdset_ptr);
187 if (pair.second.error_set)
188 FD_SET(fd, error_fdset_ptr);
192 struct timeval *tv_ptr =
nullptr;
193 struct timeval tv = {0, 0};
196 using namespace std::chrono;
200 const auto remaining_dur =
201 duration_cast<microseconds>(*
m_end_time - steady_clock::now());
202 if (remaining_dur.count() > 0) {
204 const auto dur_secs = duration_cast<seconds>(remaining_dur);
205 const auto dur_usecs = remaining_dur % seconds(1);
206 tv.tv_sec = dur_secs.count();
207 tv.tv_usec = dur_usecs.count();
214 const int num_set_fds = ::select(nfds, read_fdset_ptr, write_fdset_ptr,
215 error_fdset_ptr, tv_ptr);
216 if (num_set_fds < 0) {
218 error.SetErrorToErrno();
219 if (
error.GetError() == EINTR) {
224 }
else if (num_set_fds == 0) {
227 error.SetErrorString(
"timed out");
235 const int fd = pair.first;
237 if (pair.second.read_set) {
238 if (FD_ISSET(fd, read_fdset_ptr))
239 pair.second.read_is_set =
true;
241 if (pair.second.write_set) {
242 if (FD_ISSET(fd, write_fdset_ptr))
243 pair.second.write_is_set =
true;
245 if (pair.second.error_set) {
246 if (FD_ISSET(fd, error_fdset_ptr))
247 pair.second.error_is_set =
true;
static llvm::raw_ostream & error(Stream &strm)
static void updateMaxFd(std::optional< lldb::socket_t > &vold, lldb::socket_t vnew)
bool FDIsSetWrite(lldb::socket_t fd) const
lldb_private::Status Select()
void FDSetRead(lldb::socket_t fd)
bool FDIsSetError(lldb::socket_t fd) const
llvm::DenseMap< lldb::socket_t, FDInfo > m_fd_map
void SetTimeout(const std::chrono::microseconds &timeout)
std::optional< std::chrono::steady_clock::time_point > m_end_time
void FDSetError(lldb::socket_t fd)
void FDSetWrite(lldb::socket_t fd)
bool FDIsSetRead(lldb::socket_t fd) const
@ eErrorTypePOSIX
POSIX error codes.