Loading src/event/kqueue.cpp +57 −13 Changes for src/event/kqueue.cpp: 57 added lines, 13 removed lines. Original line number Diff line number Diff line Loading @@ -100,8 +100,48 @@ namespace netplus { #define KQUEUE_EV_ERROR 4 #define KQUEUE_EV_EOF 8 // Level-triggered kqueue no longer requires re-arming // (EV_ONESHOT eliminated for better performance) // RAII Rearm (kqueue EV_ONESHOT) - more efficient than level-triggered // Since we batch send operations, re-arming syscalls are amortized class KqueueArmGuard { public: KqueueArmGuard(int fd, std::shared_ptr<con> c, pollapi* pptr) : _fd(fd), _con(std::move(c)), _pptr(pptr) {} ~KqueueArmGuard() { if (!_armed) return; if (!_con || !_con->csock) return; int next = 0; // during handshake => need READ, and WRITE only if pending writes if (!_con->csock->getHandshakeDone()) { next |= KQUEUE_EV_READ; if (_con->csock->hasPendingWrite()) next |= KQUEUE_EV_WRITE; } else { next |= KQUEUE_EV_READ; if (!_con->SendData.empty() || _con->csock->hasPendingWrite()) next |= KQUEUE_EV_WRITE; // If TLS has buffered data, trigger EVFILT_WRITE to force a wakeup if (_con->csock->hasBufferedData()) next |= KQUEUE_EV_WRITE; } try { _pptr->setpollEventsFd(_fd, next); } catch (...) { // ignore } } void disarm() { _armed = false; } private: bool _armed = true; int _fd; std::shared_ptr<con> _con; pollapi* _pptr; }; // ------------------------------------------------------------ // poll implementation (kqueue) Loading Loading @@ -172,10 +212,10 @@ namespace netplus { return; } // Set up read event (level-triggered) // Set up read event (EV_ONESHOT) if (events & KQUEUE_EV_READ) { EV_SET(&changes[nchanges], fd, EVFILT_READ, EV_ADD, 0, 0, nullptr); EV_ADD | EV_ONESHOT, 0, 0, nullptr); nchanges++; } else { struct kevent delev; Loading @@ -183,10 +223,10 @@ namespace netplus { kevent(_kqfd, &delev, 1, nullptr, 0, nullptr); } // Set up write event (level-triggered) // Set up write event (EV_ONESHOT) if (events & KQUEUE_EV_WRITE) { EV_SET(&changes[nchanges], fd, EVFILT_WRITE, EV_ADD, 0, 0, nullptr); EV_ADD | EV_ONESHOT, 0, 0, nullptr); nchanges++; } else { struct kevent delev; Loading Loading @@ -282,10 +322,11 @@ namespace netplus { CONNECTIONS.emplace(fd, ccon); } // Add to kqueue (level-triggered) // Add to kqueue (EV_ONESHOT) // Batching recv/send operations in user space amortizes re-arm cost struct kevent setevent; EV_SET(&setevent, fd, EVFILT_READ, EV_ADD, 0, 0, nullptr); EV_ADD | EV_ONESHOT, 0, 0, nullptr); if (kevent(_kqfd, &setevent, 1, nullptr, 0, nullptr) < 0) { std::lock_guard<std::mutex> lk(POLL_HANDLER_MUTEX); Loading Loading @@ -319,6 +360,7 @@ namespace netplus { return; } KqueueArmGuard rearm(fd, c, this); bool needClose = false; { Loading Loading @@ -360,7 +402,7 @@ namespace netplus { c->csock->flush_out(); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { // Level-triggered: socket will re-fire when ready rearm.disarm(); setpollEventsFd(fd, KQUEUE_EV_WRITE); return; } Loading @@ -378,11 +420,11 @@ namespace netplus { } } // No need to re-arm with level-triggered kqueue // Socket will automatically re-fire when ready // Re-arm if handshake still in progress (EV_ONESHOT requires re-arm) if (!c->csock->getHandshakeDone()) { int ev = KQUEUE_EV_READ; if (c->csock->hasPendingWrite()) ev |= KQUEUE_EV_WRITE; rearm.disarm(); setpollEventsFd(fd, ev); return; } Loading Loading @@ -441,7 +483,7 @@ namespace netplus { c->csock->flush_out(); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { // Level-triggered: socket will re-fire when ready rearm.disarm(); setpollEventsFd(fd, KQUEUE_EV_WRITE); return; } Loading @@ -460,7 +502,9 @@ namespace netplus { c->csock->flush_out(); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { // Level-triggered: socket will re-fire when ready rearm.disarm(); setpollEventsFd(fd, KQUEUE_EV_WRITE); return; } throw; } Loading Loading
src/event/kqueue.cpp +57 −13 Changes for src/event/kqueue.cpp: 57 added lines, 13 removed lines. Original line number Diff line number Diff line Loading @@ -100,8 +100,48 @@ namespace netplus { #define KQUEUE_EV_ERROR 4 #define KQUEUE_EV_EOF 8 // Level-triggered kqueue no longer requires re-arming // (EV_ONESHOT eliminated for better performance) // RAII Rearm (kqueue EV_ONESHOT) - more efficient than level-triggered // Since we batch send operations, re-arming syscalls are amortized class KqueueArmGuard { public: KqueueArmGuard(int fd, std::shared_ptr<con> c, pollapi* pptr) : _fd(fd), _con(std::move(c)), _pptr(pptr) {} ~KqueueArmGuard() { if (!_armed) return; if (!_con || !_con->csock) return; int next = 0; // during handshake => need READ, and WRITE only if pending writes if (!_con->csock->getHandshakeDone()) { next |= KQUEUE_EV_READ; if (_con->csock->hasPendingWrite()) next |= KQUEUE_EV_WRITE; } else { next |= KQUEUE_EV_READ; if (!_con->SendData.empty() || _con->csock->hasPendingWrite()) next |= KQUEUE_EV_WRITE; // If TLS has buffered data, trigger EVFILT_WRITE to force a wakeup if (_con->csock->hasBufferedData()) next |= KQUEUE_EV_WRITE; } try { _pptr->setpollEventsFd(_fd, next); } catch (...) { // ignore } } void disarm() { _armed = false; } private: bool _armed = true; int _fd; std::shared_ptr<con> _con; pollapi* _pptr; }; // ------------------------------------------------------------ // poll implementation (kqueue) Loading Loading @@ -172,10 +212,10 @@ namespace netplus { return; } // Set up read event (level-triggered) // Set up read event (EV_ONESHOT) if (events & KQUEUE_EV_READ) { EV_SET(&changes[nchanges], fd, EVFILT_READ, EV_ADD, 0, 0, nullptr); EV_ADD | EV_ONESHOT, 0, 0, nullptr); nchanges++; } else { struct kevent delev; Loading @@ -183,10 +223,10 @@ namespace netplus { kevent(_kqfd, &delev, 1, nullptr, 0, nullptr); } // Set up write event (level-triggered) // Set up write event (EV_ONESHOT) if (events & KQUEUE_EV_WRITE) { EV_SET(&changes[nchanges], fd, EVFILT_WRITE, EV_ADD, 0, 0, nullptr); EV_ADD | EV_ONESHOT, 0, 0, nullptr); nchanges++; } else { struct kevent delev; Loading Loading @@ -282,10 +322,11 @@ namespace netplus { CONNECTIONS.emplace(fd, ccon); } // Add to kqueue (level-triggered) // Add to kqueue (EV_ONESHOT) // Batching recv/send operations in user space amortizes re-arm cost struct kevent setevent; EV_SET(&setevent, fd, EVFILT_READ, EV_ADD, 0, 0, nullptr); EV_ADD | EV_ONESHOT, 0, 0, nullptr); if (kevent(_kqfd, &setevent, 1, nullptr, 0, nullptr) < 0) { std::lock_guard<std::mutex> lk(POLL_HANDLER_MUTEX); Loading Loading @@ -319,6 +360,7 @@ namespace netplus { return; } KqueueArmGuard rearm(fd, c, this); bool needClose = false; { Loading Loading @@ -360,7 +402,7 @@ namespace netplus { c->csock->flush_out(); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { // Level-triggered: socket will re-fire when ready rearm.disarm(); setpollEventsFd(fd, KQUEUE_EV_WRITE); return; } Loading @@ -378,11 +420,11 @@ namespace netplus { } } // No need to re-arm with level-triggered kqueue // Socket will automatically re-fire when ready // Re-arm if handshake still in progress (EV_ONESHOT requires re-arm) if (!c->csock->getHandshakeDone()) { int ev = KQUEUE_EV_READ; if (c->csock->hasPendingWrite()) ev |= KQUEUE_EV_WRITE; rearm.disarm(); setpollEventsFd(fd, ev); return; } Loading Loading @@ -441,7 +483,7 @@ namespace netplus { c->csock->flush_out(); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { // Level-triggered: socket will re-fire when ready rearm.disarm(); setpollEventsFd(fd, KQUEUE_EV_WRITE); return; } Loading @@ -460,7 +502,9 @@ namespace netplus { c->csock->flush_out(); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { // Level-triggered: socket will re-fire when ready rearm.disarm(); setpollEventsFd(fd, KQUEUE_EV_WRITE); return; } throw; } Loading