Stash fd in epoll_event instead of pointer in server

This commit is contained in:
2025-08-21 22:12:13 -04:00
parent 5e8fe590c1
commit 1d86f48d5e
2 changed files with 10 additions and 16 deletions

View File

@@ -120,7 +120,7 @@ void Server::receiveConnectionBack(Connection *connection) {
}
connection->tsan_release();
event.data.ptr = connection;
event.data.fd = connection->getFd();
// Distribute connections round-robin across epoll instances
size_t epoll_index =
@@ -295,7 +295,7 @@ void Server::create_epoll_instances() {
// Add shutdown pipe to each epoll instance
struct epoll_event shutdown_event;
shutdown_event.events = EPOLLIN;
shutdown_event.data.ptr = const_cast<char *>(&shutdown_pipe_tag);
shutdown_event.data.fd = shutdown_pipe_[0];
if (epoll_ctl(epoll_fds_[i], EPOLL_CTL_ADD, shutdown_pipe_[0],
&shutdown_event) == -1) {
@@ -307,7 +307,7 @@ void Server::create_epoll_instances() {
// thundering herd
struct epoll_event listen_event;
listen_event.events = EPOLLIN | EPOLLEXCLUSIVE;
listen_event.data.ptr = const_cast<char *>(&listen_socket_tag);
listen_event.data.fd = listen_sockfd_;
if (epoll_ctl(epoll_fds_[i], EPOLL_CTL_ADD, listen_sockfd_,
&listen_event) == -1) {
perror("epoll_ctl listen socket");
@@ -351,22 +351,20 @@ void Server::start_io_threads(std::vector<std::thread> &threads) {
int batch_count = 0;
for (int i = 0; i < event_count; ++i) {
// Check for shutdown event
if (events[i].data.ptr == &shutdown_pipe_tag) {
if (events[i].data.fd == shutdown_pipe_[0]) {
return;
}
// Check for new connections
if (events[i].data.ptr == &listen_socket_tag) {
if (events[i].data.fd == listen_sockfd_) {
listenReady = true;
continue;
}
// Handle existing connection events
std::unique_ptr<Connection> conn;
{
// borrowed
Connection *conn_ = static_cast<Connection *>(events[i].data.ptr);
conn_->tsan_acquire();
conn = connection_registry_.remove(conn_->getFd());
int fd = events[i].data.fd;
std::unique_ptr<Connection> conn = connection_registry_.remove(fd);
if (conn) {
conn->tsan_acquire();
}
if (events[i].events & (EPOLLERR | EPOLLHUP)) {
@@ -529,7 +527,7 @@ void Server::process_connection_batch(
}
conn_ptr->tsan_release();
event.data.ptr = conn_ptr.get(); // Use raw pointer for epoll
event.data.fd = fd; // Use file descriptor for epoll
// Put connection back in registry since handler didn't take ownership.
// Must happen before epoll_ctl
connection_registry_.store(fd, std::move(conn_ptr));

View File

@@ -118,10 +118,6 @@ private:
std::vector<int> epoll_fds_;
int listen_sockfd_ = -1;
// Unique tags for special events to avoid type confusion in epoll data union
static inline const char listen_socket_tag = 0;
static inline const char shutdown_pipe_tag = 0;
// Private helper methods
void setup_shutdown_pipe();
void setup_signal_handling();