From 6dcf401843cc57efeb4adbef4849a7373e08e44f Mon Sep 17 00:00:00 2001
From: barracuda156 <vital.had@gmail.com>
Date: Mon, 31 Jul 2023 05:34:02 +0800
Subject: [PATCH] Support EV_TRIGGER fallback; disable KJ_USE_KQUEUE on old
 macOS

Fixes: https://github.com/capnproto/capnproto/issues/1735
---
 c++/src/kj/async-unix.c++ |  4 ++++
 c++/src/kj/async-unix.h   | 10 ++++++++--
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git c++/src/kj/async-unix.c++ c++/src/kj/async-unix.c++
index a8179ea5..67489e46 100644
--- src/kj/async-unix.c++
+++ src/kj/async-unix.c++
@@ -1065,7 +1065,11 @@ void UnixEventPort::captureChildExit() {
 void UnixEventPort::wake() const {
   // Trigger our user event.
   struct kevent event;
+#if defined(NOTE_TRIGGER)
   EV_SET(&event, 0, EVFILT_USER, 0, NOTE_TRIGGER, 0, nullptr);
+#elif defined(EV_TRIGGER)
+  EV_SET(&event, 0, EVFILT_USER, EV_TRIGGER, 0, 0, nullptr);
+#endif
   KJ_SYSCALL(kevent(kqueueFd, &event, 1, nullptr, 0, nullptr));
 }
 
diff --git c++/src/kj/async-unix.h c++/src/kj/async-unix.h
index 665305ea..a647820f 100644
--- src/kj/async-unix.h
+++ src/kj/async-unix.h
@@ -31,14 +31,20 @@
 #include <kj/io.h>
 #include <signal.h>
 
+#ifdef __APPLE__
+#include <AvailabilityMacros.h>
+#endif
+
 KJ_BEGIN_HEADER
 
 #if !defined(KJ_USE_EPOLL) && !defined(KJ_USE_KQUEUE)
 #if __linux__
 // Default to epoll on Linux.
 #define KJ_USE_EPOLL 1
-#elif __APPLE__ || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __DragonFly__
-// MacOS and BSDs prefer kqueue() for event notification.
+#elif (__APPLE__ && (MAC_OS_X_VERSION_MIN_REQUIRED > 1050)) \
+  || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __DragonFly__
+// MacOS and BSDs prefer kqueue() for event notification, however MacOS prior
+// to 10.6.x does not have NOTE_TRIGGER.
 #define KJ_USE_KQUEUE 1
 #endif
 #endif
