summaryrefslogtreecommitdiff
path: root/examples/redis-unstable/src/eventnotifier.c
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:40:55 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:40:55 +0100
commit5d8dfe892a2ea89f706ee140c3bdcfd89fe03fda (patch)
tree1acdfa5220cd13b7be43a2a01368e80d306473ca /examples/redis-unstable/src/eventnotifier.c
parentc7ab12bba64d9c20ccd79b132dac475f7bc3923e (diff)
downloadcrep-5d8dfe892a2ea89f706ee140c3bdcfd89fe03fda.tar.gz
Add Redis source code for testing
Diffstat (limited to 'examples/redis-unstable/src/eventnotifier.c')
-rw-r--r--examples/redis-unstable/src/eventnotifier.c98
1 files changed, 98 insertions, 0 deletions
diff --git a/examples/redis-unstable/src/eventnotifier.c b/examples/redis-unstable/src/eventnotifier.c
new file mode 100644
index 0000000..0a6c145
--- /dev/null
+++ b/examples/redis-unstable/src/eventnotifier.c
@@ -0,0 +1,98 @@
+/* eventnotifier.c -- An event notifier based on eventfd or pipe.
+ *
+ * Copyright (c) 2024-Present, Redis Ltd.
+ * All rights reserved.
+ *
+ * Licensed under your choice of (a) the Redis Source Available License 2.0
+ * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
+ * GNU Affero General Public License v3 (AGPLv3).
+ */
+
+#include "eventnotifier.h"
+
+#include <stdint.h>
+#include <unistd.h>
+#include <fcntl.h>
+#ifdef HAVE_EVENT_FD
+#include <sys/eventfd.h>
+#endif
+
+#include "anet.h"
+#include "zmalloc.h"
+
+eventNotifier* createEventNotifier(void) {
+ eventNotifier *en = zmalloc(sizeof(eventNotifier));
+ if (!en) return NULL;
+
+#ifdef HAVE_EVENT_FD
+ if ((en->efd = eventfd(0, EFD_NONBLOCK| EFD_CLOEXEC)) != -1) {
+ return en;
+ }
+#else
+ if (anetPipe(en->pipefd, O_CLOEXEC|O_NONBLOCK, O_CLOEXEC|O_NONBLOCK) != -1) {
+ return en;
+ }
+#endif
+
+ /* Clean up if error. */
+ zfree(en);
+ return NULL;
+}
+
+int getReadEventFd(struct eventNotifier *en) {
+#ifdef HAVE_EVENT_FD
+ return en->efd;
+#else
+ return en->pipefd[0];
+#endif
+}
+
+int getWriteEventFd(struct eventNotifier *en) {
+#ifdef HAVE_EVENT_FD
+ return en->efd;
+#else
+ return en->pipefd[1];
+#endif
+}
+
+int triggerEventNotifier(struct eventNotifier *en) {
+#ifdef HAVE_EVENT_FD
+ uint64_t u = 1;
+ if (write(en->efd, &u, sizeof(uint64_t)) == -1) {
+ return EN_ERR;
+ }
+#else
+ char buf[1] = {'R'};
+ if (write(en->pipefd[1], buf, 1) == -1) {
+ return EN_ERR;
+ }
+#endif
+ return EN_OK;
+}
+
+int handleEventNotifier(struct eventNotifier *en) {
+#ifdef HAVE_EVENT_FD
+ uint64_t u;
+ if (read(en->efd, &u, sizeof(uint64_t)) == -1) {
+ return EN_ERR;
+ }
+#else
+ char buf[1];
+ if (read(en->pipefd[0], buf, 1) == -1) {
+ return EN_ERR;
+ }
+#endif
+ return EN_OK;
+}
+
+void freeEventNotifier(struct eventNotifier *en) {
+#ifdef HAVE_EVENT_FD
+ close(en->efd);
+#else
+ close(en->pipefd[0]);
+ close(en->pipefd[1]);
+#endif
+
+ /* Free memory */
+ zfree(en);
+}