Wednesday, January 01, 2025

native kqueue for *BSD in wayland

Posting here, but will post elsewhere if this is not the right place.

Is there any desire for using kqueue natively in the wayland libraries
instead of epoll-shim? I'm sure epoll-shim is great, but I wanted to
learn to use kqueue and figured this was a good project (since I was
already working on a wlroots compositor for OpenBSD).

In any case, here's the diff. I've been running with this on my personal
latop for a couple of days now (unless I haven't linked properly), and I
haven't noticed any issues with it (I still have crashes, but those were
there before, and the core dumps don't indicate any issues

Thoughts? Should I try to get the wayland folks to merge this upstream?

diff --git a/meson.build b/meson.build
index 6b05bb5..e7f917c 100644
--- a/meson.build
+++ b/meson.build
@@ -18,6 +18,8 @@ config_h.set_quoted('PACKAGE_VERSION', meson.project_version())
cc_args = []
if host_machine.system() not in ['freebsd', 'openbsd']
cc_args += ['-D_POSIX_C_SOURCE=200809L']
+else
+ cc_args += ['-D__USE_KQUEUE']
endif
add_project_arguments(cc_args, language: 'c')

@@ -72,18 +74,22 @@ if get_option('libraries')
if host_machine.system() in ['freebsd', 'openbsd']
# When building for FreeBSD, epoll(7) is provided by a userspace
# wrapper around kqueue(2).
- epoll_dep = dependency('epoll-shim')
+ epoll_dep = []
+ decls = [
+ { 'header': 'time.h', 'symbol': 'CLOCK_MONOTONIC' },
+ ]
+
else
# Otherwise, assume that epoll(7) is supported natively.
epoll_dep = []
- endif
- ffi_dep = dependency('libffi')
-
- decls = [
+ decls = [
{ 'header': 'sys/signalfd.h', 'symbol': 'SFD_CLOEXEC' },
{ 'header': 'sys/timerfd.h', 'symbol': 'TFD_CLOEXEC' },
{ 'header': 'time.h', 'symbol': 'CLOCK_MONOTONIC' },
- ]
+ ]
+
+ endif
+ ffi_dep = dependency('libffi')

foreach d: decls
if not cc.has_header_symbol(d['header'], d['symbol'], dependencies: epoll_dep, args: cc_args)
diff --git a/src/event-loop.c b/src/event-loop.c
index 51c9b9d..86319ac 100644
--- a/src/event-loop.c
+++ b/src/event-loop.c
@@ -34,9 +34,15 @@
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/un.h>
+#ifdef __USE_KQUEUE
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/time.h>
+#else
#include <sys/epoll.h>
#include <sys/signalfd.h>
#include <sys/timerfd.h>
+

No comments:

Post a Comment