From 9d6d02e25b415e971f968365c46b16f7bc689113 Mon Sep 17 00:00:00 2001
From: Sergey Fedorov <barracuda@macos-powerpc.org>
Date: Tue, 26 Nov 2024 23:10:54 +0800
Subject: [PATCH 1/3] Use EV_TRIGGER as a fallback for NOTE_TRIGGER

---
 src/compat_ppoll.c |  7 ++++++-
 src/epollfd_ctx.c  | 13 +++++++++++--
 src/kqueue_event.c | 13 +++++++++++--
 3 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/src/compat_ppoll.c b/src/compat_ppoll.c
index 44a579d..19b0c88 100644
--- a/src/compat_ppoll.c
+++ b/src/compat_ppoll.c
@@ -93,7 +93,7 @@ compat_ppoll_impl(struct pollfd fds[], nfds_t nfds,
 			goto out;
 		}
 
-#ifdef EVFILT_USER
+#if defined(EVFILT_USER) && (defined(NOTE_TRIGGER) || defined(EV_TRIGGER))
 		sigset_t pending;
 		if (sigpending(&pending) < 0) {
 			ec = errno;
@@ -103,8 +103,13 @@ compat_ppoll_impl(struct pollfd fds[], nfds_t nfds,
 			if (sigismember(&pending, (int)kevs[i].ident)) {
 				EV_SET(&kevs[0], 0, EVFILT_USER, /**/
 				    EV_ADD | EV_ONESHOT, 0, 0, 0);
+#if defined(NOTE_TRIGGER)
 				EV_SET(&kevs[1], 0, EVFILT_USER, /**/
 				    0, NOTE_TRIGGER, 0, 0);
+#elif defined(EV_TRIGGER)
+				EV_SET(&kevs[1], 0, EVFILT_USER, /**/
+				    EV_TRIGGER, 0, 0, 0);
+#endif
 				if (kevent(kq, kevs, 2, NULL, 0, NULL) < 0) {
 					ec = errno;
 					goto out;
diff --git a/src/epollfd_ctx.c b/src/epollfd_ctx.c
index 0db5b01..08836a4 100644
--- a/src/epollfd_ctx.c
+++ b/src/epollfd_ctx.c
@@ -268,10 +268,15 @@ registered_fds_node_add_self_trigger(RegisteredFDsNode *fd2_node, int kq)
 static void
 registered_fds_node_trigger_self(RegisteredFDsNode *fd2_node, int kq)
 {
-#ifdef EVFILT_USER
+#if defined(EVFILT_USER) && (defined(NOTE_TRIGGER) || defined(EV_TRIGGER))
 	struct kevent kevs[1];
+#if defined(NOTE_TRIGGER)
 	EV_SET(&kevs[0], (uintptr_t)fd2_node, EVFILT_USER, /**/
 	    0, NOTE_TRIGGER, 0, fd2_node);
+#elif defined(EV_TRIGGER)
+	EV_SET(&kevs[0], (uintptr_t)fd2_node, EVFILT_USER, /**/
+	    EV_TRIGGER, 0, 0, fd2_node);
+#endif
 	(void)kevent(kq, kevs, 1, NULL, 0, NULL);
 #else
 	(void)kq;
@@ -792,10 +797,14 @@ epollfd_ctx__add_self_trigger(EpollFDCtx *epollfd, int kq)
 static void
 epollfd_ctx__trigger_self(EpollFDCtx *epollfd, int kq)
 {
-#ifdef EVFILT_USER
+#if defined(EVFILT_USER) && (defined(NOTE_TRIGGER) || defined(EV_TRIGGER))
 	(void)epollfd;
 	struct kevent kevs[1];
+#if defined(NOTE_TRIGGER)
 	EV_SET(&kevs[0], 0, EVFILT_USER, 0, NOTE_TRIGGER, 0, 0);
+#elif defined(EV_TRIGGER)
+	EV_SET(&kevs[0], 0, EVFILT_USER, EV_TRIGGER, 0, 0, 0);
+#endif
 	(void)kevent(kq, kevs, 1, NULL, 0, NULL);
 #else
 	assert(epollfd->self_pipe[0] >= 0);
diff --git a/src/kqueue_event.c b/src/kqueue_event.c
index e26816f..d06233e 100644
--- a/src/kqueue_event.c
+++ b/src/kqueue_event.c
@@ -16,13 +16,18 @@ kqueue_event_init(KQueueEvent *kqueue_event, struct kevent *kevs,
 {
 	*kqueue_event = (KQueueEvent) { .self_pipe_ = { -1, -1 } };
 
-#ifdef EVFILT_USER
+#if defined(EVFILT_USER) && (defined(NOTE_TRIGGER) || defined(EV_TRIGGER))
 	EV_SET(&kevs[(*kevs_length)++], 0, /**/
 	    EVFILT_USER, EV_ADD | EV_CLEAR, 0, 0, 0);
 
 	if (should_trigger) {
+#if defined(NOTE_TRIGGER)
 		EV_SET(&kevs[(*kevs_length)++], 0, /**/
 		    EVFILT_USER, 0, NOTE_TRIGGER, 0, 0);
+#elif defined(EV_TRIGGER)
+		EV_SET(&kevs[(*kevs_length)++], 0, /**/
+		    EVFILT_USER, EV_TRIGGER, 0, 0, 0);
+#endif
 		kqueue_event->is_triggered_ = true;
 	}
 
@@ -82,9 +87,13 @@ kqueue_event_trigger(KQueueEvent *kqueue_event, int kq)
 		return 0;
 	}
 
-#ifdef EVFILT_USER
+#if defined(EVFILT_USER) && (defined(NOTE_TRIGGER) || defined(EV_TRIGGER))
 	struct kevent kevs[1];
+#if defined(NOTE_TRIGGER)
 	EV_SET(&kevs[0], 0, EVFILT_USER, 0, NOTE_TRIGGER, 0, 0);
+#elif defined(EV_TRIGGER)
+	EV_SET(&kevs[0], 0, EVFILT_USER, EV_TRIGGER, 0, 0, 0);
+#endif
 
 	if (kevent(kq, kevs, 1, NULL, 0, NULL) < 0) {
 		return errno;
-- 
2.47.1

