Stash fd in epoll_event instead of pointer in server
This commit is contained in:
@@ -120,7 +120,7 @@ void Server::receiveConnectionBack(Connection *connection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
connection->tsan_release();
|
connection->tsan_release();
|
||||||
event.data.ptr = connection;
|
event.data.fd = connection->getFd();
|
||||||
|
|
||||||
// Distribute connections round-robin across epoll instances
|
// Distribute connections round-robin across epoll instances
|
||||||
size_t epoll_index =
|
size_t epoll_index =
|
||||||
@@ -295,7 +295,7 @@ void Server::create_epoll_instances() {
|
|||||||
// Add shutdown pipe to each epoll instance
|
// Add shutdown pipe to each epoll instance
|
||||||
struct epoll_event shutdown_event;
|
struct epoll_event shutdown_event;
|
||||||
shutdown_event.events = EPOLLIN;
|
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],
|
if (epoll_ctl(epoll_fds_[i], EPOLL_CTL_ADD, shutdown_pipe_[0],
|
||||||
&shutdown_event) == -1) {
|
&shutdown_event) == -1) {
|
||||||
@@ -307,7 +307,7 @@ void Server::create_epoll_instances() {
|
|||||||
// thundering herd
|
// thundering herd
|
||||||
struct epoll_event listen_event;
|
struct epoll_event listen_event;
|
||||||
listen_event.events = EPOLLIN | EPOLLEXCLUSIVE;
|
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_,
|
if (epoll_ctl(epoll_fds_[i], EPOLL_CTL_ADD, listen_sockfd_,
|
||||||
&listen_event) == -1) {
|
&listen_event) == -1) {
|
||||||
perror("epoll_ctl listen socket");
|
perror("epoll_ctl listen socket");
|
||||||
@@ -351,22 +351,20 @@ void Server::start_io_threads(std::vector<std::thread> &threads) {
|
|||||||
int batch_count = 0;
|
int batch_count = 0;
|
||||||
for (int i = 0; i < event_count; ++i) {
|
for (int i = 0; i < event_count; ++i) {
|
||||||
// Check for shutdown event
|
// Check for shutdown event
|
||||||
if (events[i].data.ptr == &shutdown_pipe_tag) {
|
if (events[i].data.fd == shutdown_pipe_[0]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Check for new connections
|
// Check for new connections
|
||||||
if (events[i].data.ptr == &listen_socket_tag) {
|
if (events[i].data.fd == listen_sockfd_) {
|
||||||
listenReady = true;
|
listenReady = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle existing connection events
|
// Handle existing connection events
|
||||||
std::unique_ptr<Connection> conn;
|
int fd = events[i].data.fd;
|
||||||
{
|
std::unique_ptr<Connection> conn = connection_registry_.remove(fd);
|
||||||
// borrowed
|
if (conn) {
|
||||||
Connection *conn_ = static_cast<Connection *>(events[i].data.ptr);
|
conn->tsan_acquire();
|
||||||
conn_->tsan_acquire();
|
|
||||||
conn = connection_registry_.remove(conn_->getFd());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (events[i].events & (EPOLLERR | EPOLLHUP)) {
|
if (events[i].events & (EPOLLERR | EPOLLHUP)) {
|
||||||
@@ -529,7 +527,7 @@ void Server::process_connection_batch(
|
|||||||
}
|
}
|
||||||
|
|
||||||
conn_ptr->tsan_release();
|
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.
|
// Put connection back in registry since handler didn't take ownership.
|
||||||
// Must happen before epoll_ctl
|
// Must happen before epoll_ctl
|
||||||
connection_registry_.store(fd, std::move(conn_ptr));
|
connection_registry_.store(fd, std::move(conn_ptr));
|
||||||
|
|||||||
@@ -118,10 +118,6 @@ private:
|
|||||||
std::vector<int> epoll_fds_;
|
std::vector<int> epoll_fds_;
|
||||||
int listen_sockfd_ = -1;
|
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
|
// Private helper methods
|
||||||
void setup_shutdown_pipe();
|
void setup_shutdown_pipe();
|
||||||
void setup_signal_handling();
|
void setup_signal_handling();
|
||||||
|
|||||||
Reference in New Issue
Block a user