Skip to content

Add kqueue support for macOS/BSD#735

Open
deepdiver-997 wants to merge 1 commit intochenshuo:masterfrom
deepdiver-997:kqueue-macos
Open

Add kqueue support for macOS/BSD#735
deepdiver-997 wants to merge 1 commit intochenshuo:masterfrom
deepdiver-997:kqueue-macos

Conversation

@deepdiver-997
Copy link
Copy Markdown

Summary

Add kqueue support for macOS/BSD platforms, enabling muduo network library to work on Apple macOS and other BSD-derived systems.

Motivation

muduo currently only supports epoll on Linux. macOS users cannot build or use muduo due to missing:

  • eventfd (used for EventLoop wakeup)
  • timerfd (used for TimerQueue)
  • Various Linux-specific system calls

Changes

New Files

  • muduo/net/poller/KQueuePoller.cc - KQueue implementation
  • muduo/net/poller/KQueuePoller.h - KQueue header

Platform Abstraction

  • muduo/net/EventLoop.cc - Uses socketpair instead of eventfd on macOS
  • muduo/net/TimerQueue.cc - Uses polling instead of timerfd on macOS
  • muduo/net/SocketsOps.cc - Fixed bindOrDie address length bug

Compatibility Fixes

  • muduo/net/Endian.h - Added macOS byte order macros
  • muduo/base/Mutex.h - macOS-compatible assert_perror_fail
  • Various threading and logging compatibility fixes

Testing

  • ✅ timerqueue_unittest - Timer functionality verified
  • ✅ eventloopthread_unittest - EventLoop threading verified
  • ✅ eventloopthreadpool_unittest - Multi-thread EventLoop pool verified
  • ✅ TcpServer binding - Server successfully binds to port on macOS

Technical Details

KQueuePoller uses macOS/BSD kqueue syscall:

  • kqueue() creates kernel event queue
  • kevent() registers/changes/removes events and waits for events
  • EVFILT_READ/EVFILT_WRITE map to POLLIN/POLLOUT
  • Edge-triggered semantics compatible with muduo's design

Breaking Changes

None - fully backward compatible, Linux epoll path unchanged.

Implemented KQueuePoller to replace EPollPoller on macOS/BSD platforms.

Key changes:
- New KQueuePoller class using kqueue() syscall for IO multiplexing
- EventLoop now uses socketpair instead of eventfd on macOS
- TimerQueue uses polling approach instead of timerfd on macOS
- Fixed bindOrDie to use correct address length for IPv4
- Various compatibility fixes for macOS (byte order, threading, etc.)

KQueuePoller features:
- Uses kqueue()/kevent() for event registration and waiting
- EVFILT_READ/EVFILT_WRITE for read/write events
- EVFILT_EXCEPT for exception handling
- Edge-triggered semantics compatible with muduo's level-triggered design

Tested with:
- timerqueue_unittest
- eventloopthread_unittest
- eventloopthreadpool_unittest
- TcpServer binding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant