Loading src/event/kqueue.cpp +21 −13 Changes for src/event/kqueue.cpp: 21 added lines, 13 removed lines. Original line number Diff line number Diff line Loading @@ -32,6 +32,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include <vector> #include <atomic> #include <mutex> #include <shared_mutex> #include <unordered_map> #include <map> #include <unistd.h> Loading @@ -53,9 +55,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace netplus { static std::vector<socket*> SSOCKETS; static std::map<int, std::shared_ptr<con>> CONNECTIONS; static std::unordered_map<int, std::shared_ptr<con>> CONNECTIONS; static std::mutex POLL_HANDLER_MUTEX; static std::shared_mutex POLL_HANDLER_MUTEX; // ------------------------------------------------------------ // Utility: safe close (no deadlock, no double close) Loading Loading @@ -120,7 +122,13 @@ namespace netplus { void flush() { if (_count > 0) { kevent(_kqfd, _changes, _count, nullptr, 0, nullptr); // Provide an output buffer so kevent() reports per-change // errors via EV_ERROR instead of aborting with -1. // Without this, one stale change (e.g. EV_DELETE on a // closed fd) silently drops every subsequent change in // the batch. struct kevent errors[MAX_CHANGES]; kevent(_kqfd, _changes, _count, errors, _count, nullptr); _count = 0; } } Loading Loading @@ -179,7 +187,7 @@ namespace netplus { _batch(kqfd) { { std::lock_guard<std::mutex> lk(POLL_HANDLER_MUTEX); std::unique_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX); SSOCKETS.emplace_back(_ServerSocket); } Loading Loading @@ -216,7 +224,7 @@ namespace netplus { } std::shared_ptr<con> getConByFd(int fd) { std::lock_guard<std::mutex> lk(POLL_HANDLER_MUTEX); std::shared_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX); auto it = CONNECTIONS.find(fd); if (it == CONNECTIONS.end()) return nullptr; return it->second; Loading Loading @@ -327,7 +335,7 @@ namespace netplus { // insert into connection map { std::lock_guard<std::mutex> lk(POLL_HANDLER_MUTEX); std::unique_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX); CONNECTIONS.emplace(fd, ccon); } Loading Loading @@ -578,11 +586,11 @@ namespace netplus { // take out of map first { std::lock_guard<std::mutex> lock(POLL_HANDLER_MUTEX); std::unique_lock<std::shared_mutex> lock(POLL_HANDLER_MUTEX); auto it = CONNECTIONS.find(fd); if (it == CONNECTIONS.end()) return; ccon = it->second; ccon = std::move(it->second); CONNECTIONS.erase(it); } Loading @@ -592,11 +600,11 @@ namespace netplus { { std::lock_guard<std::mutex> lk(ccon->event_mutex); // remove from kqueue (using batch for efficiency) _batch.add_change(fd, EVFILT_READ, EV_DELETE); _batch.add_change(fd, EVFILT_WRITE, EV_DELETE); // close socket // close() automatically removes all kqueue filters for // this fd. Do NOT add EV_DELETE to the batch — those // changes would execute after close() invalidates the // fd, producing ERR#9 and potentially dropping later // batch entries for other connections. safeCloseSocket(ccon); } Loading Loading
src/event/kqueue.cpp +21 −13 Changes for src/event/kqueue.cpp: 21 added lines, 13 removed lines. Original line number Diff line number Diff line Loading @@ -32,6 +32,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include <vector> #include <atomic> #include <mutex> #include <shared_mutex> #include <unordered_map> #include <map> #include <unistd.h> Loading @@ -53,9 +55,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace netplus { static std::vector<socket*> SSOCKETS; static std::map<int, std::shared_ptr<con>> CONNECTIONS; static std::unordered_map<int, std::shared_ptr<con>> CONNECTIONS; static std::mutex POLL_HANDLER_MUTEX; static std::shared_mutex POLL_HANDLER_MUTEX; // ------------------------------------------------------------ // Utility: safe close (no deadlock, no double close) Loading Loading @@ -120,7 +122,13 @@ namespace netplus { void flush() { if (_count > 0) { kevent(_kqfd, _changes, _count, nullptr, 0, nullptr); // Provide an output buffer so kevent() reports per-change // errors via EV_ERROR instead of aborting with -1. // Without this, one stale change (e.g. EV_DELETE on a // closed fd) silently drops every subsequent change in // the batch. struct kevent errors[MAX_CHANGES]; kevent(_kqfd, _changes, _count, errors, _count, nullptr); _count = 0; } } Loading Loading @@ -179,7 +187,7 @@ namespace netplus { _batch(kqfd) { { std::lock_guard<std::mutex> lk(POLL_HANDLER_MUTEX); std::unique_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX); SSOCKETS.emplace_back(_ServerSocket); } Loading Loading @@ -216,7 +224,7 @@ namespace netplus { } std::shared_ptr<con> getConByFd(int fd) { std::lock_guard<std::mutex> lk(POLL_HANDLER_MUTEX); std::shared_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX); auto it = CONNECTIONS.find(fd); if (it == CONNECTIONS.end()) return nullptr; return it->second; Loading Loading @@ -327,7 +335,7 @@ namespace netplus { // insert into connection map { std::lock_guard<std::mutex> lk(POLL_HANDLER_MUTEX); std::unique_lock<std::shared_mutex> lk(POLL_HANDLER_MUTEX); CONNECTIONS.emplace(fd, ccon); } Loading Loading @@ -578,11 +586,11 @@ namespace netplus { // take out of map first { std::lock_guard<std::mutex> lock(POLL_HANDLER_MUTEX); std::unique_lock<std::shared_mutex> lock(POLL_HANDLER_MUTEX); auto it = CONNECTIONS.find(fd); if (it == CONNECTIONS.end()) return; ccon = it->second; ccon = std::move(it->second); CONNECTIONS.erase(it); } Loading @@ -592,11 +600,11 @@ namespace netplus { { std::lock_guard<std::mutex> lk(ccon->event_mutex); // remove from kqueue (using batch for efficiency) _batch.add_change(fd, EVFILT_READ, EV_DELETE); _batch.add_change(fd, EVFILT_WRITE, EV_DELETE); // close socket // close() automatically removes all kqueue filters for // this fd. Do NOT add EV_DELETE to the batch — those // changes would execute after close() invalidates the // fd, producing ERR#9 and potentially dropping later // batch entries for other connections. safeCloseSocket(ccon); } Loading