aboutsummaryrefslogtreecommitdiff
path: root/deps
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-05-30 04:07:37 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-14 08:18:59 +0200
commitab50d908ba2eae14929a07341b1b716cde681c1c (patch)
tree52e3f1f8cd579eb234185dc3b397ac66cda47cbf /deps
parent46f9e7c67134caf43a04ea5ee3b2325588647e28 (diff)
downloadffwd-ab50d908ba2eae14929a07341b1b716cde681c1c.tar.gz
ffwd-ab50d908ba2eae14929a07341b1b716cde681c1c.zip
server: implement HTTP API with libae integration
Diffstat (limited to 'deps')
-rw-r--r--deps/ae/ae.c513
-rw-r--r--deps/ae/ae.h118
-rw-r--r--deps/ae/ae_epoll.c144
-rw-r--r--deps/ae/ae_kqueue.c183
-rw-r--r--deps/ae/ae_select.c108
-rw-r--r--deps/ae/anet.c812
-rw-r--r--deps/ae/anet.h58
-rw-r--r--deps/ae/config.h375
-rw-r--r--deps/ae/fmacros.h56
-rw-r--r--deps/ae/monotonic.c247
-rw-r--r--deps/ae/monotonic.h61
-rw-r--r--deps/ae/redisassert.h18
-rw-r--r--deps/ae/util.h21
-rw-r--r--deps/ae/zmalloc.h15
14 files changed, 2729 insertions, 0 deletions
diff --git a/deps/ae/ae.c b/deps/ae/ae.c
new file mode 100644
index 0000000..c07b203
--- /dev/null
+++ b/deps/ae/ae.c
@@ -0,0 +1,513 @@
+/* A simple event-driven programming library. Originally I wrote this code
+ * for the Jim's event-loop (Jim is a Tcl interpreter) but later translated
+ * it in form of a library for easy reuse.
+ *
+ * Copyright (c) 2006-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 "ae.h"
+#include "anet.h"
+#include "redisassert.h"
+
+#include <stdio.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <poll.h>
+#include <string.h>
+#include <time.h>
+#include <errno.h>
+
+#include "zmalloc.h"
+#include "config.h"
+
+/* Include the best multiplexing layer supported by this system.
+ * The following should be ordered by performances, descending. */
+#ifdef HAVE_EVPORT
+#include "ae_evport.c"
+#else
+ #ifdef HAVE_EPOLL
+ #include "ae_epoll.c"
+ #else
+ #ifdef HAVE_KQUEUE
+ #include "ae_kqueue.c"
+ #else
+ #include "ae_select.c"
+ #endif
+ #endif
+#endif
+
+#define INITIAL_EVENT 1024
+aeEventLoop *aeCreateEventLoop(int setsize) {
+ aeEventLoop *eventLoop;
+ int i;
+
+ monotonicInit(); /* just in case the calling app didn't initialize */
+
+ if ((eventLoop = zmalloc(sizeof(*eventLoop))) == NULL) goto err;
+ eventLoop->nevents = setsize < INITIAL_EVENT ? setsize : INITIAL_EVENT;
+ eventLoop->events = zmalloc(sizeof(aeFileEvent)*eventLoop->nevents);
+ eventLoop->fired = zmalloc(sizeof(aeFiredEvent)*eventLoop->nevents);
+ if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err;
+ eventLoop->setsize = setsize;
+ eventLoop->timeEventHead = NULL;
+ eventLoop->timeEventNextId = 0;
+ eventLoop->stop = 0;
+ eventLoop->maxfd = -1;
+ eventLoop->beforesleep = NULL;
+ eventLoop->aftersleep = NULL;
+ eventLoop->flags = 0;
+ memset(eventLoop->privdata, 0, sizeof(eventLoop->privdata));
+ if (aeApiCreate(eventLoop) == -1) goto err;
+ /* Events with mask == AE_NONE are not set. So let's initialize the
+ * vector with it. */
+ for (i = 0; i < eventLoop->nevents; i++)
+ eventLoop->events[i].mask = AE_NONE;
+ return eventLoop;
+
+err:
+ if (eventLoop) {
+ zfree(eventLoop->events);
+ zfree(eventLoop->fired);
+ zfree(eventLoop);
+ }
+ return NULL;
+}
+
+/* Return the current set size. */
+int aeGetSetSize(aeEventLoop *eventLoop) {
+ return eventLoop->setsize;
+}
+
+/*
+ * Tell the event processing to change the wait timeout as soon as possible.
+ *
+ * Note: it just means you turn on/off the global AE_DONT_WAIT.
+ */
+void aeSetDontWait(aeEventLoop *eventLoop, int noWait) {
+ if (noWait)
+ eventLoop->flags |= AE_DONT_WAIT;
+ else
+ eventLoop->flags &= ~AE_DONT_WAIT;
+}
+
+/* Resize the maximum set size of the event loop.
+ * If the requested set size is smaller than the current set size, but
+ * there is already a file descriptor in use that is >= the requested
+ * set size minus one, AE_ERR is returned and the operation is not
+ * performed at all.
+ *
+ * Otherwise AE_OK is returned and the operation is successful. */
+int aeResizeSetSize(aeEventLoop *eventLoop, int setsize) {
+ if (setsize == eventLoop->setsize) return AE_OK;
+ if (eventLoop->maxfd >= setsize) return AE_ERR;
+ if (aeApiResize(eventLoop,setsize) == -1) return AE_ERR;
+
+ eventLoop->setsize = setsize;
+
+ /* If the current allocated space is larger than the requested size,
+ * we need to shrink it to the requested size. */
+ if (setsize < eventLoop->nevents) {
+ eventLoop->events = zrealloc(eventLoop->events,sizeof(aeFileEvent)*setsize);
+ eventLoop->fired = zrealloc(eventLoop->fired,sizeof(aeFiredEvent)*setsize);
+ eventLoop->nevents = setsize;
+ }
+ return AE_OK;
+}
+
+void aeDeleteEventLoop(aeEventLoop *eventLoop) {
+ aeApiFree(eventLoop);
+ zfree(eventLoop->events);
+ zfree(eventLoop->fired);
+
+ /* Free the time events list. */
+ aeTimeEvent *next_te, *te = eventLoop->timeEventHead;
+ while (te) {
+ next_te = te->next;
+ if (te->finalizerProc)
+ te->finalizerProc(eventLoop, te->clientData);
+ zfree(te);
+ te = next_te;
+ }
+ zfree(eventLoop);
+}
+
+void aeStop(aeEventLoop *eventLoop) {
+ eventLoop->stop = 1;
+}
+
+int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
+ aeFileProc *proc, void *clientData)
+{
+ if (fd >= eventLoop->setsize) {
+ errno = ERANGE;
+ return AE_ERR;
+ }
+
+ /* Resize the events and fired arrays if the file
+ * descriptor exceeds the current number of events. */
+ if (unlikely(fd >= eventLoop->nevents)) {
+ int newnevents = eventLoop->nevents;
+ newnevents = (newnevents * 2 > fd + 1) ? newnevents * 2 : fd + 1;
+ newnevents = (newnevents > eventLoop->setsize) ? eventLoop->setsize : newnevents;
+ eventLoop->events = zrealloc(eventLoop->events, sizeof(aeFileEvent) * newnevents);
+ eventLoop->fired = zrealloc(eventLoop->fired, sizeof(aeFiredEvent) * newnevents);
+
+ /* Initialize new slots with an AE_NONE mask */
+ for (int i = eventLoop->nevents; i < newnevents; i++)
+ eventLoop->events[i].mask = AE_NONE;
+ eventLoop->nevents = newnevents;
+ }
+
+ aeFileEvent *fe = &eventLoop->events[fd];
+
+ if (aeApiAddEvent(eventLoop, fd, mask) == -1)
+ return AE_ERR;
+ fe->mask |= mask;
+ if (mask & AE_READABLE) fe->rfileProc = proc;
+ if (mask & AE_WRITABLE) fe->wfileProc = proc;
+ fe->clientData = clientData;
+ if (fd > eventLoop->maxfd)
+ eventLoop->maxfd = fd;
+ return AE_OK;
+}
+
+void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask)
+{
+ if (fd >= eventLoop->setsize) return;
+ aeFileEvent *fe = &eventLoop->events[fd];
+ if (fe->mask == AE_NONE) return;
+
+ /* We want to always remove AE_BARRIER if set when AE_WRITABLE
+ * is removed. */
+ if (mask & AE_WRITABLE) mask |= AE_BARRIER;
+
+ aeApiDelEvent(eventLoop, fd, mask);
+ fe->mask = fe->mask & (~mask);
+ if (fd == eventLoop->maxfd && fe->mask == AE_NONE) {
+ /* Update the max fd */
+ int j;
+
+ for (j = eventLoop->maxfd-1; j >= 0; j--)
+ if (eventLoop->events[j].mask != AE_NONE) break;
+ eventLoop->maxfd = j;
+ }
+}
+
+void *aeGetFileClientData(aeEventLoop *eventLoop, int fd) {
+ if (fd >= eventLoop->setsize) return NULL;
+ aeFileEvent *fe = &eventLoop->events[fd];
+ if (fe->mask == AE_NONE) return NULL;
+
+ return fe->clientData;
+}
+
+int aeGetFileEvents(aeEventLoop *eventLoop, int fd) {
+ if (fd >= eventLoop->setsize) return 0;
+ aeFileEvent *fe = &eventLoop->events[fd];
+
+ return fe->mask;
+}
+
+long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,
+ aeTimeProc *proc, void *clientData,
+ aeEventFinalizerProc *finalizerProc)
+{
+ long long id = eventLoop->timeEventNextId++;
+ aeTimeEvent *te;
+
+ te = zmalloc(sizeof(*te));
+ if (te == NULL) return AE_ERR;
+ te->id = id;
+ te->when = getMonotonicUs() + milliseconds * 1000;
+ te->timeProc = proc;
+ te->finalizerProc = finalizerProc;
+ te->clientData = clientData;
+ te->prev = NULL;
+ te->next = eventLoop->timeEventHead;
+ te->refcount = 0;
+ if (te->next)
+ te->next->prev = te;
+ eventLoop->timeEventHead = te;
+ return id;
+}
+
+int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id)
+{
+ aeTimeEvent *te = eventLoop->timeEventHead;
+ while(te) {
+ if (te->id == id) {
+ te->id = AE_DELETED_EVENT_ID;
+ return AE_OK;
+ }
+ te = te->next;
+ }
+ return AE_ERR; /* NO event with the specified ID found */
+}
+
+/* How many microseconds until the first timer should fire.
+ * If there are no timers, -1 is returned.
+ *
+ * Note that's O(N) since time events are unsorted.
+ * Possible optimizations (not needed by Redis so far, but...):
+ * 1) Insert the event in order, so that the nearest is just the head.
+ * Much better but still insertion or deletion of timers is O(N).
+ * 2) Use a skiplist to have this operation as O(1) and insertion as O(log(N)).
+ */
+static int64_t usUntilEarliestTimer(aeEventLoop *eventLoop) {
+ aeTimeEvent *te = eventLoop->timeEventHead;
+ if (te == NULL) return -1;
+
+ aeTimeEvent *earliest = NULL;
+ while (te) {
+ if ((!earliest || te->when < earliest->when) && te->id != AE_DELETED_EVENT_ID)
+ earliest = te;
+ te = te->next;
+ }
+
+ if (earliest == NULL) return -1;
+
+ monotime now = getMonotonicUs();
+ return (now >= earliest->when) ? 0 : earliest->when - now;
+}
+
+/* Process time events */
+static int processTimeEvents(aeEventLoop *eventLoop) {
+ int processed = 0;
+ aeTimeEvent *te;
+ long long maxId;
+
+ te = eventLoop->timeEventHead;
+ maxId = eventLoop->timeEventNextId-1;
+ monotime now = getMonotonicUs();
+ while(te) {
+ long long id;
+
+ /* Remove events scheduled for deletion. */
+ if (te->id == AE_DELETED_EVENT_ID) {
+ aeTimeEvent *next = te->next;
+ /* If a reference exists for this timer event,
+ * don't free it. This is currently incremented
+ * for recursive timerProc calls */
+ if (te->refcount) {
+ te = next;
+ continue;
+ }
+ if (te->prev)
+ te->prev->next = te->next;
+ else
+ eventLoop->timeEventHead = te->next;
+ if (te->next)
+ te->next->prev = te->prev;
+ if (te->finalizerProc) {
+ te->finalizerProc(eventLoop, te->clientData);
+ now = getMonotonicUs();
+ }
+ zfree(te);
+ te = next;
+ continue;
+ }
+
+ /* Make sure we don't process time events created by time events in
+ * this iteration. Note that this check is currently useless: we always
+ * add new timers on the head, however if we change the implementation
+ * detail, this check may be useful again: we keep it here for future
+ * defense. */
+ if (te->id > maxId) {
+ te = te->next;
+ continue;
+ }
+
+ if (te->when <= now) {
+ int retval;
+
+ id = te->id;
+ te->refcount++;
+ retval = te->timeProc(eventLoop, id, te->clientData);
+ te->refcount--;
+ processed++;
+ now = getMonotonicUs();
+ if (retval != AE_NOMORE) {
+ te->when = now + (monotime)retval * 1000;
+ } else {
+ te->id = AE_DELETED_EVENT_ID;
+ }
+ }
+ te = te->next;
+ }
+ return processed;
+}
+
+/* Process every pending file event, then every pending time event
+ * (that may be registered by file event callbacks just processed).
+ * Without special flags the function sleeps until some file event
+ * fires, or when the next time event occurs (if any).
+ *
+ * If flags is 0, the function does nothing and returns.
+ * if flags has AE_ALL_EVENTS set, all the kind of events are processed.
+ * if flags has AE_FILE_EVENTS set, file events are processed.
+ * if flags has AE_TIME_EVENTS set, time events are processed.
+ * if flags has AE_DONT_WAIT set, the function returns ASAP once all
+ * the events that can be handled without a wait are processed.
+ * if flags has AE_CALL_AFTER_SLEEP set, the aftersleep callback is called.
+ * if flags has AE_CALL_BEFORE_SLEEP set, the beforesleep callback is called.
+ *
+ * The function returns the number of events processed. */
+int aeProcessEvents(aeEventLoop *eventLoop, int flags)
+{
+ int processed = 0, numevents;
+
+ /* Nothing to do? return ASAP */
+ if (!(flags & AE_TIME_EVENTS) && !(flags & AE_FILE_EVENTS)) return 0;
+
+ /* Note that we want to call aeApiPoll() even if there are no
+ * file events to process as long as we want to process time
+ * events, in order to sleep until the next time event is ready
+ * to fire. */
+ if (eventLoop->maxfd != -1 ||
+ ((flags & AE_TIME_EVENTS) && !(flags & AE_DONT_WAIT))) {
+ int j;
+ struct timeval tv, *tvp = NULL; /* NULL means infinite wait. */
+ int64_t usUntilTimer;
+
+ if (eventLoop->beforesleep != NULL && (flags & AE_CALL_BEFORE_SLEEP))
+ eventLoop->beforesleep(eventLoop);
+
+ /* The eventLoop->flags may be changed inside beforesleep.
+ * So we should check it after beforesleep be called. At the same time,
+ * the parameter flags always should have the highest priority.
+ * That is to say, once the parameter flag is set to AE_DONT_WAIT,
+ * no matter what value eventLoop->flags is set to, we should ignore it. */
+ if ((flags & AE_DONT_WAIT) || (eventLoop->flags & AE_DONT_WAIT)) {
+ tv.tv_sec = tv.tv_usec = 0;
+ tvp = &tv;
+ } else if (flags & AE_TIME_EVENTS) {
+ usUntilTimer = usUntilEarliestTimer(eventLoop);
+ if (usUntilTimer >= 0) {
+ tv.tv_sec = usUntilTimer / 1000000;
+ tv.tv_usec = usUntilTimer % 1000000;
+ tvp = &tv;
+ }
+ }
+ /* Call the multiplexing API, will return only on timeout or when
+ * some event fires. */
+ numevents = aeApiPoll(eventLoop, tvp);
+
+ /* Don't process file events if not requested. */
+ if (!(flags & AE_FILE_EVENTS)) {
+ numevents = 0;
+ }
+
+ /* After sleep callback. */
+ if (eventLoop->aftersleep != NULL && flags & AE_CALL_AFTER_SLEEP)
+ eventLoop->aftersleep(eventLoop);
+
+ for (j = 0; j < numevents; j++) {
+ int fd = eventLoop->fired[j].fd;
+ aeFileEvent *fe = &eventLoop->events[fd];
+ int mask = eventLoop->fired[j].mask;
+ int fired = 0; /* Number of events fired for current fd. */
+
+ /* Normally we execute the readable event first, and the writable
+ * event later. This is useful as sometimes we may be able
+ * to serve the reply of a query immediately after processing the
+ * query.
+ *
+ * However if AE_BARRIER is set in the mask, our application is
+ * asking us to do the reverse: never fire the writable event
+ * after the readable. In such a case, we invert the calls.
+ * This is useful when, for instance, we want to do things
+ * in the beforeSleep() hook, like fsyncing a file to disk,
+ * before replying to a client. */
+ int invert = fe->mask & AE_BARRIER;
+
+ /* Note the "fe->mask & mask & ..." code: maybe an already
+ * processed event removed an element that fired and we still
+ * didn't processed, so we check if the event is still valid.
+ *
+ * Fire the readable event if the call sequence is not
+ * inverted. */
+ if (!invert && fe->mask & mask & AE_READABLE) {
+ fe->rfileProc(eventLoop,fd,fe->clientData,mask);
+ fired++;
+ fe = &eventLoop->events[fd]; /* Refresh in case of resize. */
+ }
+
+ /* Fire the writable event. */
+ if (fe->mask & mask & AE_WRITABLE) {
+ if (!fired || fe->wfileProc != fe->rfileProc) {
+ fe->wfileProc(eventLoop,fd,fe->clientData,mask);
+ fired++;
+ }
+ }
+
+ /* If we have to invert the call, fire the readable event now
+ * after the writable one. */
+ if (invert) {
+ fe = &eventLoop->events[fd]; /* Refresh in case of resize. */
+ if ((fe->mask & mask & AE_READABLE) &&
+ (!fired || fe->wfileProc != fe->rfileProc))
+ {
+ fe->rfileProc(eventLoop,fd,fe->clientData,mask);
+ fired++;
+ }
+ }
+
+ processed++;
+ }
+ }
+ /* Check time events */
+ if (flags & AE_TIME_EVENTS)
+ processed += processTimeEvents(eventLoop);
+
+ return processed; /* return the number of processed file/time events */
+}
+
+/* Wait for milliseconds until the given file descriptor becomes
+ * writable/readable/exception */
+int aeWait(int fd, int mask, long long milliseconds) {
+ struct pollfd pfd;
+ int retmask = 0, retval;
+
+ memset(&pfd, 0, sizeof(pfd));
+ pfd.fd = fd;
+ if (mask & AE_READABLE) pfd.events |= POLLIN;
+ if (mask & AE_WRITABLE) pfd.events |= POLLOUT;
+
+ if ((retval = poll(&pfd, 1, milliseconds))== 1) {
+ if (pfd.revents & POLLIN) retmask |= AE_READABLE;
+ if (pfd.revents & POLLOUT) retmask |= AE_WRITABLE;
+ if (pfd.revents & POLLERR) retmask |= AE_WRITABLE;
+ if (pfd.revents & POLLHUP) retmask |= AE_WRITABLE;
+ return retmask;
+ } else {
+ return retval;
+ }
+}
+
+void aeMain(aeEventLoop *eventLoop) {
+ eventLoop->stop = 0;
+ while (!eventLoop->stop) {
+ aeProcessEvents(eventLoop, AE_ALL_EVENTS|
+ AE_CALL_BEFORE_SLEEP|
+ AE_CALL_AFTER_SLEEP);
+ }
+}
+
+char *aeGetApiName(void) {
+ return aeApiName();
+}
+
+void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep) {
+ eventLoop->beforesleep = beforesleep;
+}
+
+void aeSetAfterSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *aftersleep) {
+ eventLoop->aftersleep = aftersleep;
+}
diff --git a/deps/ae/ae.h b/deps/ae/ae.h
new file mode 100644
index 0000000..996d48b
--- /dev/null
+++ b/deps/ae/ae.h
@@ -0,0 +1,118 @@
+/* A simple event-driven programming library. Originally I wrote this code
+ * for the Jim's event-loop (Jim is a Tcl interpreter) but later translated
+ * it in form of a library for easy reuse.
+ *
+ * Copyright (c) 2006-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).
+ */
+
+#ifndef __AE_H__
+#define __AE_H__
+
+#include "monotonic.h"
+
+#define AE_OK 0
+#define AE_ERR -1
+
+#define AE_NONE 0 /* No events registered. */
+#define AE_READABLE 1 /* Fire when descriptor is readable. */
+#define AE_WRITABLE 2 /* Fire when descriptor is writable. */
+#define AE_BARRIER 4 /* With WRITABLE, never fire the event if the
+ READABLE event already fired in the same event
+ loop iteration. Useful when you want to persist
+ things to disk before sending replies, and want
+ to do that in a group fashion. */
+
+#define AE_FILE_EVENTS (1<<0)
+#define AE_TIME_EVENTS (1<<1)
+#define AE_ALL_EVENTS (AE_FILE_EVENTS|AE_TIME_EVENTS)
+#define AE_DONT_WAIT (1<<2)
+#define AE_CALL_BEFORE_SLEEP (1<<3)
+#define AE_CALL_AFTER_SLEEP (1<<4)
+
+#define AE_NOMORE -1
+#define AE_DELETED_EVENT_ID -1
+
+/* Macros */
+#define AE_NOTUSED(V) ((void) V)
+
+struct aeEventLoop;
+
+/* Types and data structures */
+typedef void aeFileProc(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);
+typedef int aeTimeProc(struct aeEventLoop *eventLoop, long long id, void *clientData);
+typedef void aeEventFinalizerProc(struct aeEventLoop *eventLoop, void *clientData);
+typedef void aeBeforeSleepProc(struct aeEventLoop *eventLoop);
+
+/* File event structure */
+typedef struct aeFileEvent {
+ int mask; /* one of AE_(READABLE|WRITABLE|BARRIER) */
+ aeFileProc *rfileProc;
+ aeFileProc *wfileProc;
+ void *clientData;
+} aeFileEvent;
+
+/* Time event structure */
+typedef struct aeTimeEvent {
+ long long id; /* time event identifier. */
+ monotime when;
+ aeTimeProc *timeProc;
+ aeEventFinalizerProc *finalizerProc;
+ void *clientData;
+ struct aeTimeEvent *prev;
+ struct aeTimeEvent *next;
+ int refcount; /* refcount to prevent timer events from being
+ * freed in recursive time event calls. */
+} aeTimeEvent;
+
+/* A fired event */
+typedef struct aeFiredEvent {
+ int fd;
+ int mask;
+} aeFiredEvent;
+
+/* State of an event based program */
+typedef struct aeEventLoop {
+ int maxfd; /* highest file descriptor currently registered */
+ int setsize; /* max number of file descriptors tracked */
+ long long timeEventNextId;
+ int nevents; /* Size of Registered events */
+ aeFileEvent *events; /* Registered events */
+ aeFiredEvent *fired; /* Fired events */
+ aeTimeEvent *timeEventHead;
+ int stop;
+ void *apidata; /* This is used for polling API specific data */
+ aeBeforeSleepProc *beforesleep;
+ aeBeforeSleepProc *aftersleep;
+ int flags;
+ void *privdata[2];
+} aeEventLoop;
+
+/* Prototypes */
+aeEventLoop *aeCreateEventLoop(int setsize);
+void aeDeleteEventLoop(aeEventLoop *eventLoop);
+void aeStop(aeEventLoop *eventLoop);
+int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
+ aeFileProc *proc, void *clientData);
+void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask);
+int aeGetFileEvents(aeEventLoop *eventLoop, int fd);
+void *aeGetFileClientData(aeEventLoop *eventLoop, int fd);
+long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,
+ aeTimeProc *proc, void *clientData,
+ aeEventFinalizerProc *finalizerProc);
+int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id);
+int aeProcessEvents(aeEventLoop *eventLoop, int flags);
+int aeWait(int fd, int mask, long long milliseconds);
+void aeMain(aeEventLoop *eventLoop);
+char *aeGetApiName(void);
+void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep);
+void aeSetAfterSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *aftersleep);
+int aeGetSetSize(aeEventLoop *eventLoop);
+int aeResizeSetSize(aeEventLoop *eventLoop, int setsize);
+void aeSetDontWait(aeEventLoop *eventLoop, int noWait);
+
+#endif
diff --git a/deps/ae/ae_epoll.c b/deps/ae/ae_epoll.c
new file mode 100644
index 0000000..751639b
--- /dev/null
+++ b/deps/ae/ae_epoll.c
@@ -0,0 +1,144 @@
+/* Linux epoll(2) based ae.c module
+ *
+ * Copyright (c) 2009-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 <sys/epoll.h>
+
+typedef struct aeApiState
+{
+ int epfd;
+ struct epoll_event *events;
+} aeApiState;
+
+static int aeApiCreate(aeEventLoop *eventLoop)
+{
+ aeApiState *state = zmalloc(sizeof(aeApiState));
+
+ if (!state)
+ return -1;
+ state->events = zmalloc(sizeof(struct epoll_event) * eventLoop->setsize);
+ if (!state->events)
+ {
+ zfree(state);
+ return -1;
+ }
+ state->epfd = epoll_create(1024); /* 1024 is just a hint for the kernel */
+ if (state->epfd == -1)
+ {
+ zfree(state->events);
+ zfree(state);
+ return -1;
+ }
+ anetCloexec(state->epfd);
+ eventLoop->apidata = state;
+ return 0;
+}
+
+static int aeApiResize(aeEventLoop *eventLoop, int setsize)
+{
+ aeApiState *state = eventLoop->apidata;
+
+ state->events = zrealloc(state->events, sizeof(struct epoll_event) * setsize);
+ return 0;
+}
+
+static void aeApiFree(aeEventLoop *eventLoop)
+{
+ aeApiState *state = eventLoop->apidata;
+
+ close(state->epfd);
+ zfree(state->events);
+ zfree(state);
+}
+
+static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask)
+{
+ aeApiState *state = eventLoop->apidata;
+ struct epoll_event ee = {0}; /* avoid valgrind warning */
+ /* If the fd was already monitored for some event, we need a MOD
+ * operation. Otherwise we need an ADD operation. */
+ int op = eventLoop->events[fd].mask == AE_NONE ? EPOLL_CTL_ADD : EPOLL_CTL_MOD;
+
+ ee.events = 0;
+ mask |= eventLoop->events[fd].mask; /* Merge old events */
+ if (mask & AE_READABLE)
+ ee.events |= EPOLLIN;
+ if (mask & AE_WRITABLE)
+ ee.events |= EPOLLOUT;
+ ee.data.fd = fd;
+ if (epoll_ctl(state->epfd, op, fd, &ee) == -1)
+ return -1;
+ return 0;
+}
+
+static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int delmask)
+{
+ aeApiState *state = eventLoop->apidata;
+ struct epoll_event ee = {0}; /* avoid valgrind warning */
+ int mask = eventLoop->events[fd].mask & (~delmask);
+
+ ee.events = 0;
+ if (mask & AE_READABLE)
+ ee.events |= EPOLLIN;
+ if (mask & AE_WRITABLE)
+ ee.events |= EPOLLOUT;
+ ee.data.fd = fd;
+ if (mask != AE_NONE)
+ {
+ epoll_ctl(state->epfd, EPOLL_CTL_MOD, fd, &ee);
+ }
+ else
+ {
+ /* Note, Kernel < 2.6.9 requires a non null event pointer even for
+ * EPOLL_CTL_DEL. */
+ epoll_ctl(state->epfd, EPOLL_CTL_DEL, fd, &ee);
+ }
+}
+
+static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp)
+{
+ aeApiState *state = eventLoop->apidata;
+ int retval, numevents = 0;
+
+ retval = epoll_wait(state->epfd, state->events, eventLoop->setsize,
+ tvp ? (tvp->tv_sec * 1000 + (tvp->tv_usec + 999) / 1000) : -1);
+ if (retval > 0)
+ {
+ int j;
+
+ numevents = retval;
+ for (j = 0; j < numevents; j++)
+ {
+ int mask = 0;
+ struct epoll_event *e = state->events + j;
+
+ if (e->events & EPOLLIN)
+ mask |= AE_READABLE;
+ if (e->events & EPOLLOUT)
+ mask |= AE_WRITABLE;
+ if (e->events & EPOLLERR)
+ mask |= AE_WRITABLE | AE_READABLE;
+ if (e->events & EPOLLHUP)
+ mask |= AE_WRITABLE | AE_READABLE;
+ eventLoop->fired[j].fd = e->data.fd;
+ eventLoop->fired[j].mask = mask;
+ }
+ }
+ else if (retval == -1 && errno != EINTR)
+ {
+ panic("aeApiPoll: epoll_wait, %s", strerror(errno));
+ }
+
+ return numevents;
+}
+
+static char *aeApiName(void)
+{
+ return "epoll";
+}
diff --git a/deps/ae/ae_kqueue.c b/deps/ae/ae_kqueue.c
new file mode 100644
index 0000000..ec10a5e
--- /dev/null
+++ b/deps/ae/ae_kqueue.c
@@ -0,0 +1,183 @@
+/* Kqueue(2)-based ae.c module
+ *
+ * Copyright (C) 2009 Harish Mallipeddi - harish.mallipeddi@gmail.com
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of Redis nor the names of its contributors may be used
+ * to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/time.h>
+
+typedef struct aeApiState {
+ int kqfd;
+ struct kevent *events;
+
+ /* Events mask for merge read and write event.
+ * To reduce memory consumption, we use 2 bits to store the mask
+ * of an event, so that 1 byte will store the mask of 4 events. */
+ char *eventsMask;
+} aeApiState;
+
+#define EVENT_MASK_MALLOC_SIZE(sz) (((sz) + 3) / 4)
+#define EVENT_MASK_OFFSET(fd) ((fd) % 4 * 2)
+#define EVENT_MASK_ENCODE(fd, mask) (((mask) & 0x3) << EVENT_MASK_OFFSET(fd))
+
+static inline int getEventMask(const char *eventsMask, int fd) {
+ return (eventsMask[fd/4] >> EVENT_MASK_OFFSET(fd)) & 0x3;
+}
+
+static inline void addEventMask(char *eventsMask, int fd, int mask) {
+ eventsMask[fd/4] |= EVENT_MASK_ENCODE(fd, mask);
+}
+
+static inline void resetEventMask(char *eventsMask, int fd) {
+ eventsMask[fd/4] &= ~EVENT_MASK_ENCODE(fd, 0x3);
+}
+
+static int aeApiCreate(aeEventLoop *eventLoop) {
+ aeApiState *state = zmalloc(sizeof(aeApiState));
+
+ if (!state) return -1;
+ state->events = zmalloc(sizeof(struct kevent)*eventLoop->setsize);
+ if (!state->events) {
+ zfree(state);
+ return -1;
+ }
+ state->kqfd = kqueue();
+ if (state->kqfd == -1) {
+ zfree(state->events);
+ zfree(state);
+ return -1;
+ }
+ anetCloexec(state->kqfd);
+ state->eventsMask = zmalloc(EVENT_MASK_MALLOC_SIZE(eventLoop->setsize));
+ memset(state->eventsMask, 0, EVENT_MASK_MALLOC_SIZE(eventLoop->setsize));
+ eventLoop->apidata = state;
+ return 0;
+}
+
+static int aeApiResize(aeEventLoop *eventLoop, int setsize) {
+ aeApiState *state = eventLoop->apidata;
+
+ state->events = zrealloc(state->events, sizeof(struct kevent)*setsize);
+ state->eventsMask = zrealloc(state->eventsMask, EVENT_MASK_MALLOC_SIZE(setsize));
+ memset(state->eventsMask, 0, EVENT_MASK_MALLOC_SIZE(setsize));
+ return 0;
+}
+
+static void aeApiFree(aeEventLoop *eventLoop) {
+ aeApiState *state = eventLoop->apidata;
+
+ close(state->kqfd);
+ zfree(state->events);
+ zfree(state->eventsMask);
+ zfree(state);
+}
+
+static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
+ aeApiState *state = eventLoop->apidata;
+ struct kevent evs[2];
+ int nch = 0;
+
+ if (mask & AE_READABLE) EV_SET(evs + nch++, fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
+ if (mask & AE_WRITABLE) EV_SET(evs + nch++, fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
+
+ return kevent(state->kqfd, evs, nch, NULL, 0, NULL);
+}
+
+static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {
+ aeApiState *state = eventLoop->apidata;
+ struct kevent evs[2];
+ int nch = 0;
+
+ if (mask & AE_READABLE) EV_SET(evs + nch++, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
+ if (mask & AE_WRITABLE) EV_SET(evs + nch++, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
+
+ kevent(state->kqfd, evs, nch, NULL, 0, NULL);
+}
+
+static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
+ aeApiState *state = eventLoop->apidata;
+ int retval, numevents = 0;
+
+ if (tvp != NULL) {
+ struct timespec timeout;
+ timeout.tv_sec = tvp->tv_sec;
+ timeout.tv_nsec = tvp->tv_usec * 1000;
+ retval = kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize,
+ &timeout);
+ } else {
+ retval = kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize,
+ NULL);
+ }
+
+ if (retval > 0) {
+ int j;
+
+ /* Normally we execute the read event first and then the write event.
+ * When the barrier is set, we will do it reverse.
+ *
+ * However, under kqueue, read and write events would be separate
+ * events, which would make it impossible to control the order of
+ * reads and writes. So we store the event's mask we've got and merge
+ * the same fd events later. */
+ for (j = 0; j < retval; j++) {
+ struct kevent *e = state->events+j;
+ int fd = e->ident;
+ int mask = 0;
+
+ if (e->filter == EVFILT_READ) mask = AE_READABLE;
+ else if (e->filter == EVFILT_WRITE) mask = AE_WRITABLE;
+ addEventMask(state->eventsMask, fd, mask);
+ }
+
+ /* Re-traversal to merge read and write events, and set the fd's mask to
+ * 0 so that events are not added again when the fd is encountered again. */
+ numevents = 0;
+ for (j = 0; j < retval; j++) {
+ struct kevent *e = state->events+j;
+ int fd = e->ident;
+ int mask = getEventMask(state->eventsMask, fd);
+
+ if (mask) {
+ eventLoop->fired[numevents].fd = fd;
+ eventLoop->fired[numevents].mask = mask;
+ resetEventMask(state->eventsMask, fd);
+ numevents++;
+ }
+ }
+ } else if (retval == -1 && errno != EINTR) {
+ panic("aeApiPoll: kevent, %s", strerror(errno));
+ }
+
+ return numevents;
+}
+
+static char *aeApiName(void) {
+ return "kqueue";
+}
diff --git a/deps/ae/ae_select.c b/deps/ae/ae_select.c
new file mode 100644
index 0000000..f3c7aa7
--- /dev/null
+++ b/deps/ae/ae_select.c
@@ -0,0 +1,108 @@
+/* Select()-based ae.c module.
+ *
+ * Copyright (c) 2009-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 <sys/select.h>
+#include <string.h>
+
+typedef struct aeApiState
+{
+ fd_set rfds, wfds;
+ /* We need to have a copy of the fd sets as it's not safe to reuse
+ * FD sets after select(). */
+ fd_set _rfds, _wfds;
+} aeApiState;
+
+static int aeApiCreate(aeEventLoop *eventLoop)
+{
+ aeApiState *state = zmalloc(sizeof(aeApiState));
+
+ if (!state)
+ return -1;
+ FD_ZERO(&state->rfds);
+ FD_ZERO(&state->wfds);
+ eventLoop->apidata = state;
+ return 0;
+}
+
+static int aeApiResize(aeEventLoop *eventLoop, int setsize)
+{
+ AE_NOTUSED(eventLoop);
+ /* Just ensure we have enough room in the fd_set type. */
+ if (setsize >= FD_SETSIZE)
+ return -1;
+ return 0;
+}
+
+static void aeApiFree(aeEventLoop *eventLoop)
+{
+ zfree(eventLoop->apidata);
+}
+
+static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask)
+{
+ aeApiState *state = eventLoop->apidata;
+
+ if (mask & AE_READABLE)
+ FD_SET(fd, &state->rfds);
+ if (mask & AE_WRITABLE)
+ FD_SET(fd, &state->wfds);
+ return 0;
+}
+
+static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask)
+{
+ aeApiState *state = eventLoop->apidata;
+
+ if (mask & AE_READABLE)
+ FD_CLR(fd, &state->rfds);
+ if (mask & AE_WRITABLE)
+ FD_CLR(fd, &state->wfds);
+}
+
+static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp)
+{
+ aeApiState *state = eventLoop->apidata;
+ int retval, j, numevents = 0;
+
+ memcpy(&state->_rfds, &state->rfds, sizeof(fd_set));
+ memcpy(&state->_wfds, &state->wfds, sizeof(fd_set));
+
+ retval = select(eventLoop->maxfd + 1,
+ &state->_rfds, &state->_wfds, NULL, tvp);
+ if (retval > 0)
+ {
+ for (j = 0; j <= eventLoop->maxfd; j++)
+ {
+ int mask = 0;
+ aeFileEvent *fe = &eventLoop->events[j];
+
+ if (fe->mask == AE_NONE)
+ continue;
+ if (fe->mask & AE_READABLE && FD_ISSET(j, &state->_rfds))
+ mask |= AE_READABLE;
+ if (fe->mask & AE_WRITABLE && FD_ISSET(j, &state->_wfds))
+ mask |= AE_WRITABLE;
+ eventLoop->fired[numevents].fd = j;
+ eventLoop->fired[numevents].mask = mask;
+ numevents++;
+ }
+ }
+ else if (retval == -1 && errno != EINTR)
+ {
+ panic("aeApiPoll: select, %s", strerror(errno));
+ }
+
+ return numevents;
+}
+
+static char *aeApiName(void)
+{
+ return "select";
+}
diff --git a/deps/ae/anet.c b/deps/ae/anet.c
new file mode 100644
index 0000000..8b7b91e
--- /dev/null
+++ b/deps/ae/anet.c
@@ -0,0 +1,812 @@
+/* anet.c -- Basic TCP socket stuff made a bit less boring
+ *
+ * Copyright (c) 2006-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 "fmacros.h"
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/un.h>
+#include <sys/time.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <netdb.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "anet.h"
+#include "config.h"
+#include "util.h"
+
+#define UNUSED(x) (void)(x)
+
+static void anetSetError(char *err, const char *fmt, ...)
+{
+ va_list ap;
+
+ if (!err) return;
+ va_start(ap, fmt);
+ vsnprintf(err, ANET_ERR_LEN, fmt, ap);
+ va_end(ap);
+}
+
+int anetGetError(int fd) {
+ int sockerr = 0;
+ socklen_t errlen = sizeof(sockerr);
+
+ if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &sockerr, &errlen) == -1)
+ sockerr = errno;
+ return sockerr;
+}
+
+int anetSetBlock(char *err, int fd, int non_block) {
+ int flags;
+
+ /* Set the socket blocking (if non_block is zero) or non-blocking.
+ * Note that fcntl(2) for F_GETFL and F_SETFL can't be
+ * interrupted by a signal. */
+ if ((flags = fcntl(fd, F_GETFL)) == -1) {
+ anetSetError(err, "fcntl(F_GETFL): %s", strerror(errno));
+ return ANET_ERR;
+ }
+
+ /* Check if this flag has been set or unset, if so,
+ * then there is no need to call fcntl to set/unset it again. */
+ if (!!(flags & O_NONBLOCK) == !!non_block)
+ return ANET_OK;
+
+ if (non_block)
+ flags |= O_NONBLOCK;
+ else
+ flags &= ~O_NONBLOCK;
+
+ if (fcntl(fd, F_SETFL, flags) == -1) {
+ anetSetError(err, "fcntl(F_SETFL,O_NONBLOCK): %s", strerror(errno));
+ return ANET_ERR;
+ }
+ return ANET_OK;
+}
+
+int anetNonBlock(char *err, int fd) {
+ return anetSetBlock(err,fd,1);
+}
+
+int anetBlock(char *err, int fd) {
+ return anetSetBlock(err,fd,0);
+}
+
+/* Enable the FD_CLOEXEC on the given fd to avoid fd leaks.
+ * This function should be invoked for fd's on specific places
+ * where fork + execve system calls are called. */
+int anetCloexec(int fd) {
+ int r;
+ int flags;
+
+ do {
+ r = fcntl(fd, F_GETFD);
+ } while (r == -1 && errno == EINTR);
+
+ if (r == -1 || (r & FD_CLOEXEC))
+ return r;
+
+ flags = r | FD_CLOEXEC;
+
+ do {
+ r = fcntl(fd, F_SETFD, flags);
+ } while (r == -1 && errno == EINTR);
+
+ return r;
+}
+
+/* Enable TCP keep-alive mechanism to detect dead peers,
+ * TCP_KEEPIDLE, TCP_KEEPINTVL and TCP_KEEPCNT will be set accordingly. */
+int anetKeepAlive(char *err, int fd, int interval)
+{
+ int enabled = 1;
+ if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &enabled, sizeof(enabled)))
+ {
+ anetSetError(err, "setsockopt SO_KEEPALIVE: %s", strerror(errno));
+ return ANET_ERR;
+ }
+
+ int idle;
+ int intvl;
+ int cnt;
+
+ /* There are platforms that are expected to support the full mechanism of TCP keep-alive,
+ * we want the compiler to emit warnings of unused variables if the preprocessor directives
+ * somehow fail, and other than those platforms, just omit these warnings if they happen.
+ */
+#if !(defined(_AIX) || defined(__APPLE__) || defined(__DragonFly__) || \
+ defined(__FreeBSD__) || defined(__illumos__) || defined(__linux__) || \
+ defined(__NetBSD__) || defined(__sun))
+ UNUSED(interval);
+ UNUSED(idle);
+ UNUSED(intvl);
+ UNUSED(cnt);
+#endif
+
+#ifdef __sun
+ /* The implementation of TCP keep-alive on Solaris/SmartOS is a bit unusual
+ * compared to other Unix-like systems.
+ * Thus, we need to specialize it on Solaris.
+ *
+ * There are two keep-alive mechanisms on Solaris:
+ * - By default, the first keep-alive probe is sent out after a TCP connection is idle for two hours.
+ * If the peer does not respond to the probe within eight minutes, the TCP connection is aborted.
+ * You can alter the interval for sending out the first probe using the socket option TCP_KEEPALIVE_THRESHOLD
+ * in milliseconds or TCP_KEEPIDLE in seconds.
+ * The system default is controlled by the TCP ndd parameter tcp_keepalive_interval. The minimum value is ten seconds.
+ * The maximum is ten days, while the default is two hours. If you receive no response to the probe,
+ * you can use the TCP_KEEPALIVE_ABORT_THRESHOLD socket option to change the time threshold for aborting a TCP connection.
+ * The option value is an unsigned integer in milliseconds. The value zero indicates that TCP should never time out and
+ * abort the connection when probing. The system default is controlled by the TCP ndd parameter tcp_keepalive_abort_interval.
+ * The default is eight minutes.
+ *
+ * - The second implementation is activated if socket option TCP_KEEPINTVL and/or TCP_KEEPCNT are set.
+ * The time between each consequent probes is set by TCP_KEEPINTVL in seconds.
+ * The minimum value is ten seconds. The maximum is ten days, while the default is two hours.
+ * The TCP connection will be aborted after certain amount of probes, which is set by TCP_KEEPCNT, without receiving response.
+ */
+
+ idle = interval;
+ if (idle < 10) idle = 10; // kernel expects at least 10 seconds
+ if (idle > 10*24*60*60) idle = 10*24*60*60; // kernel expects at most 10 days
+
+ /* `TCP_KEEPIDLE`, `TCP_KEEPINTVL`, and `TCP_KEEPCNT` were not available on Solaris
+ * until version 11.4, but let's take a chance here. */
+#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
+ if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle))) {
+ anetSetError(err, "setsockopt TCP_KEEPIDLE: %s\n", strerror(errno));
+ return ANET_ERR;
+ }
+
+ intvl = idle/3;
+ if (intvl < 10) intvl = 10; /* kernel expects at least 10 seconds */
+ if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &intvl, sizeof(intvl))) {
+ anetSetError(err, "setsockopt TCP_KEEPINTVL: %s\n", strerror(errno));
+ return ANET_ERR;
+ }
+
+ cnt = 3;
+ if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt))) {
+ anetSetError(err, "setsockopt TCP_KEEPCNT: %s\n", strerror(errno));
+ return ANET_ERR;
+ }
+#else
+ /* Fall back to the first implementation of tcp-alive mechanism for older Solaris,
+ * simulate the tcp-alive mechanism on other platforms via `TCP_KEEPALIVE_THRESHOLD` + `TCP_KEEPALIVE_ABORT_THRESHOLD`.
+ */
+ idle *= 1000; // kernel expects milliseconds
+ if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE_THRESHOLD, &idle, sizeof(idle))) {
+ anetSetError(err, "setsockopt TCP_KEEPINTVL: %s\n", strerror(errno));
+ return ANET_ERR;
+ }
+
+ /* Note that the consequent probes will not be sent at equal intervals on Solaris,
+ * but will be sent using the exponential backoff algorithm. */
+ int time_to_abort = idle;
+ if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE_ABORT_THRESHOLD, &time_to_abort, sizeof(time_to_abort))) {
+ anetSetError(err, "setsockopt TCP_KEEPCNT: %s\n", strerror(errno));
+ return ANET_ERR;
+ }
+#endif
+
+ return ANET_OK;
+
+#endif
+
+#ifdef TCP_KEEPIDLE
+ /* Default settings are more or less garbage, with the keepalive time
+ * set to 7200 by default on Linux and other Unix-like systems.
+ * Modify settings to make the feature actually useful. */
+
+ /* Send first probe after interval. */
+ idle = interval;
+ if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle))) {
+ anetSetError(err, "setsockopt TCP_KEEPIDLE: %s\n", strerror(errno));
+ return ANET_ERR;
+ }
+#elif defined(TCP_KEEPALIVE)
+ /* Darwin/macOS uses TCP_KEEPALIVE in place of TCP_KEEPIDLE. */
+ idle = interval;
+ if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &idle, sizeof(idle))) {
+ anetSetError(err, "setsockopt TCP_KEEPALIVE: %s\n", strerror(errno));
+ return ANET_ERR;
+ }
+#endif
+
+#ifdef TCP_KEEPINTVL
+ /* Send next probes after the specified interval. Note that we set the
+ * delay as interval / 3, as we send three probes before detecting
+ * an error (see the next setsockopt call). */
+ intvl = interval/3;
+ if (intvl == 0) intvl = 1;
+ if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &intvl, sizeof(intvl))) {
+ anetSetError(err, "setsockopt TCP_KEEPINTVL: %s\n", strerror(errno));
+ return ANET_ERR;
+ }
+#endif
+
+#ifdef TCP_KEEPCNT
+ /* Consider the socket in error state after three we send three ACK
+ * probes without getting a reply. */
+ cnt = 3;
+ if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt))) {
+ anetSetError(err, "setsockopt TCP_KEEPCNT: %s\n", strerror(errno));
+ return ANET_ERR;
+ }
+#endif
+
+ return ANET_OK;
+}
+
+static int anetSetTcpNoDelay(char *err, int fd, int val)
+{
+ if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) == -1)
+ {
+ anetSetError(err, "setsockopt TCP_NODELAY: %s", strerror(errno));
+ return ANET_ERR;
+ }
+ return ANET_OK;
+}
+
+int anetEnableTcpNoDelay(char *err, int fd)
+{
+ return anetSetTcpNoDelay(err, fd, 1);
+}
+
+int anetDisableTcpNoDelay(char *err, int fd)
+{
+ return anetSetTcpNoDelay(err, fd, 0);
+}
+
+/* Set the socket send timeout (SO_SNDTIMEO socket option) to the specified
+ * number of milliseconds, or disable it if the 'ms' argument is zero. */
+int anetSendTimeout(char *err, int fd, long long ms) {
+ struct timeval tv;
+
+ tv.tv_sec = ms/1000;
+ tv.tv_usec = (ms%1000)*1000;
+ if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
+ anetSetError(err, "setsockopt SO_SNDTIMEO: %s", strerror(errno));
+ return ANET_ERR;
+ }
+ return ANET_OK;
+}
+
+/* Set the socket receive timeout (SO_RCVTIMEO socket option) to the specified
+ * number of milliseconds, or disable it if the 'ms' argument is zero. */
+int anetRecvTimeout(char *err, int fd, long long ms) {
+ struct timeval tv;
+
+ tv.tv_sec = ms/1000;
+ tv.tv_usec = (ms%1000)*1000;
+ if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
+ anetSetError(err, "setsockopt SO_RCVTIMEO: %s", strerror(errno));
+ return ANET_ERR;
+ }
+ return ANET_OK;
+}
+
+/* Resolve the hostname "host" and set the string representation of the
+ * IP address into the buffer pointed by "ipbuf".
+ *
+ * If flags is set to ANET_IP_ONLY the function only resolves hostnames
+ * that are actually already IPv4 or IPv6 addresses. This turns the function
+ * into a validating / normalizing function.
+ *
+ * If the flag ANET_PREFER_IPV4 is set, IPv4 is preferred over IPv6.
+ * If the flag ANET_PREFER_IPV6 is set, IPv6 is preferred over IPv4.
+ * */
+int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len,
+ int flags)
+{
+ struct addrinfo hints, *info;
+ int rv;
+
+ memset(&hints,0,sizeof(hints));
+ if (flags & ANET_IP_ONLY) hints.ai_flags = AI_NUMERICHOST;
+ hints.ai_family = AF_UNSPEC;
+ if (flags & ANET_PREFER_IPV4 && !(flags & ANET_PREFER_IPV6)) {
+ hints.ai_family = AF_INET;
+ } else if (flags & ANET_PREFER_IPV6 && !(flags & ANET_PREFER_IPV4)) {
+ hints.ai_family = AF_INET6;
+ }
+ hints.ai_socktype = SOCK_STREAM; /* specify socktype to avoid dups */
+
+ rv = getaddrinfo(host, NULL, &hints, &info);
+ if (rv != 0 && hints.ai_family != AF_UNSPEC) {
+ /* Try the other IP version. */
+ hints.ai_family = (hints.ai_family == AF_INET) ? AF_INET6 : AF_INET;
+ rv = getaddrinfo(host, NULL, &hints, &info);
+ }
+ if (rv != 0) {
+ anetSetError(err, "%s", gai_strerror(rv));
+ return ANET_ERR;
+ }
+ if (info->ai_family == AF_INET) {
+ struct sockaddr_in *sa = (struct sockaddr_in *)info->ai_addr;
+ inet_ntop(AF_INET, &(sa->sin_addr), ipbuf, ipbuf_len);
+ } else {
+ struct sockaddr_in6 *sa = (struct sockaddr_in6 *)info->ai_addr;
+ inet_ntop(AF_INET6, &(sa->sin6_addr), ipbuf, ipbuf_len);
+ }
+
+ freeaddrinfo(info);
+ return ANET_OK;
+}
+
+static int anetSetReuseAddr(char *err, int fd) {
+ int yes = 1;
+ /* Make sure connection-intensive things like the redis benchmark
+ * will be able to close/open sockets a zillion of times */
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
+ anetSetError(err, "setsockopt SO_REUSEADDR: %s", strerror(errno));
+ return ANET_ERR;
+ }
+ return ANET_OK;
+}
+
+static int anetCreateSocket(char *err, int domain) {
+ int s;
+ if ((s = socket(domain, SOCK_STREAM, 0)) == -1) {
+ anetSetError(err, "creating socket: %s", strerror(errno));
+ return ANET_ERR;
+ }
+
+ /* Make sure connection-intensive things like the redis benchmark
+ * will be able to close/open sockets a zillion of times */
+ if (anetSetReuseAddr(err,s) == ANET_ERR) {
+ close(s);
+ return ANET_ERR;
+ }
+ return s;
+}
+
+#define ANET_CONNECT_NONE 0
+#define ANET_CONNECT_NONBLOCK 1
+#define ANET_CONNECT_BE_BINDING 2 /* Best effort binding. */
+static int anetTcpGenericConnect(char *err, const char *addr, int port,
+ const char *source_addr, int flags)
+{
+ int s = ANET_ERR, rv;
+ char portstr[6]; /* strlen("65535") + 1; */
+ struct addrinfo hints, *servinfo, *bservinfo, *p, *b;
+
+ snprintf(portstr,sizeof(portstr),"%d",port);
+ memset(&hints,0,sizeof(hints));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+
+ if ((rv = getaddrinfo(addr,portstr,&hints,&servinfo)) != 0) {
+ anetSetError(err, "%s", gai_strerror(rv));
+ return ANET_ERR;
+ }
+ for (p = servinfo; p != NULL; p = p->ai_next) {
+ /* Try to create the socket and to connect it.
+ * If we fail in the socket() call, or on connect(), we retry with
+ * the next entry in servinfo. */
+ if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)
+ continue;
+ if (anetSetReuseAddr(err,s) == ANET_ERR) goto error;
+ if (flags & ANET_CONNECT_NONBLOCK && anetNonBlock(err,s) != ANET_OK)
+ goto error;
+ if (source_addr) {
+ int bound = 0;
+ /* Using getaddrinfo saves us from self-determining IPv4 vs IPv6 */
+ if ((rv = getaddrinfo(source_addr, NULL, &hints, &bservinfo)) != 0)
+ {
+ anetSetError(err, "%s", gai_strerror(rv));
+ goto error;
+ }
+ for (b = bservinfo; b != NULL; b = b->ai_next) {
+ if (bind(s,b->ai_addr,b->ai_addrlen) != -1) {
+ bound = 1;
+ break;
+ }
+ }
+ freeaddrinfo(bservinfo);
+ if (!bound) {
+ anetSetError(err, "bind: %s", strerror(errno));
+ goto error;
+ }
+ }
+ if (connect(s,p->ai_addr,p->ai_addrlen) == -1) {
+ /* If the socket is non-blocking, it is ok for connect() to
+ * return an EINPROGRESS error here. */
+ if (errno == EINPROGRESS && flags & ANET_CONNECT_NONBLOCK)
+ goto end;
+ close(s);
+ s = ANET_ERR;
+ continue;
+ }
+
+ /* If we ended an iteration of the for loop without errors, we
+ * have a connected socket. Let's return to the caller. */
+ goto end;
+ }
+ if (p == NULL)
+ anetSetError(err, "creating socket: %s", strerror(errno));
+
+error:
+ if (s != ANET_ERR) {
+ close(s);
+ s = ANET_ERR;
+ }
+
+end:
+ freeaddrinfo(servinfo);
+
+ /* Handle best effort binding: if a binding address was used, but it is
+ * not possible to create a socket, try again without a binding address. */
+ if (s == ANET_ERR && source_addr && (flags & ANET_CONNECT_BE_BINDING)) {
+ return anetTcpGenericConnect(err,addr,port,NULL,flags);
+ } else {
+ return s;
+ }
+}
+
+int anetTcpNonBlockConnect(char *err, const char *addr, int port)
+{
+ return anetTcpGenericConnect(err,addr,port,NULL,ANET_CONNECT_NONBLOCK);
+}
+
+int anetTcpNonBlockBestEffortBindConnect(char *err, const char *addr, int port,
+ const char *source_addr)
+{
+ return anetTcpGenericConnect(err,addr,port,source_addr,
+ ANET_CONNECT_NONBLOCK|ANET_CONNECT_BE_BINDING);
+}
+
+int anetUnixGenericConnect(char *err, const char *path, int flags)
+{
+ int s;
+ struct sockaddr_un sa;
+
+ if ((s = anetCreateSocket(err,AF_LOCAL)) == ANET_ERR)
+ return ANET_ERR;
+
+ sa.sun_family = AF_LOCAL;
+ redis_strlcpy(sa.sun_path,path,sizeof(sa.sun_path));
+ if (flags & ANET_CONNECT_NONBLOCK) {
+ if (anetNonBlock(err,s) != ANET_OK) {
+ close(s);
+ return ANET_ERR;
+ }
+ }
+ if (connect(s,(struct sockaddr*)&sa,sizeof(sa)) == -1) {
+ if (errno == EINPROGRESS &&
+ flags & ANET_CONNECT_NONBLOCK)
+ return s;
+
+ anetSetError(err, "connect: %s", strerror(errno));
+ close(s);
+ return ANET_ERR;
+ }
+ return s;
+}
+
+static int anetListen(char *err, int s, struct sockaddr *sa, socklen_t len, int backlog, mode_t perm) {
+ if (bind(s,sa,len) == -1) {
+ anetSetError(err, "bind: %s", strerror(errno));
+ close(s);
+ return ANET_ERR;
+ }
+
+ if (sa->sa_family == AF_LOCAL && perm)
+ chmod(((struct sockaddr_un *) sa)->sun_path, perm);
+
+ if (listen(s, backlog) == -1) {
+ anetSetError(err, "listen: %s", strerror(errno));
+ close(s);
+ return ANET_ERR;
+ }
+ return ANET_OK;
+}
+
+static int anetV6Only(char *err, int s) {
+ int yes = 1;
+ if (setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,&yes,sizeof(yes)) == -1) {
+ anetSetError(err, "setsockopt: %s", strerror(errno));
+ return ANET_ERR;
+ }
+ return ANET_OK;
+}
+
+static int _anetTcpServer(char *err, int port, char *bindaddr, int af, int backlog)
+{
+ int s = -1, rv;
+ char _port[6]; /* strlen("65535") */
+ struct addrinfo hints, *servinfo, *p;
+
+ snprintf(_port,6,"%d",port);
+ memset(&hints,0,sizeof(hints));
+ hints.ai_family = af;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags = AI_PASSIVE; /* No effect if bindaddr != NULL */
+ if (bindaddr && !strcmp("*", bindaddr))
+ bindaddr = NULL;
+ if (af == AF_INET6 && bindaddr && !strcmp("::*", bindaddr))
+ bindaddr = NULL;
+
+ if ((rv = getaddrinfo(bindaddr,_port,&hints,&servinfo)) != 0) {
+ anetSetError(err, "%s", gai_strerror(rv));
+ return ANET_ERR;
+ }
+ for (p = servinfo; p != NULL; p = p->ai_next) {
+ if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)
+ continue;
+
+ if (af == AF_INET6 && anetV6Only(err,s) == ANET_ERR) goto error;
+ if (anetSetReuseAddr(err,s) == ANET_ERR) goto error;
+ if (anetListen(err,s,p->ai_addr,p->ai_addrlen,backlog,0) == ANET_ERR) s = ANET_ERR;
+ goto end;
+ }
+ if (p == NULL) {
+ anetSetError(err, "unable to bind socket, errno: %d", errno);
+ goto error;
+ }
+
+error:
+ if (s != -1) close(s);
+ s = ANET_ERR;
+end:
+ freeaddrinfo(servinfo);
+ return s;
+}
+
+int anetTcpServer(char *err, int port, char *bindaddr, int backlog)
+{
+ return _anetTcpServer(err, port, bindaddr, AF_INET, backlog);
+}
+
+int anetTcp6Server(char *err, int port, char *bindaddr, int backlog)
+{
+ return _anetTcpServer(err, port, bindaddr, AF_INET6, backlog);
+}
+
+int anetUnixServer(char *err, char *path, mode_t perm, int backlog)
+{
+ int s;
+ struct sockaddr_un sa;
+
+ if (strlen(path) > sizeof(sa.sun_path)-1) {
+ anetSetError(err,"unix socket path too long (%zu), must be under %zu", strlen(path), sizeof(sa.sun_path));
+ return ANET_ERR;
+ }
+ if ((s = anetCreateSocket(err,AF_LOCAL)) == ANET_ERR)
+ return ANET_ERR;
+
+ memset(&sa,0,sizeof(sa));
+ sa.sun_family = AF_LOCAL;
+ redis_strlcpy(sa.sun_path,path,sizeof(sa.sun_path));
+ if (anetListen(err,s,(struct sockaddr*)&sa,sizeof(sa),backlog,perm) == ANET_ERR)
+ return ANET_ERR;
+ return s;
+}
+
+/* Accept a connection and also make sure the socket is non-blocking, and CLOEXEC.
+ * returns the new socket FD, or -1 on error. */
+static int anetGenericAccept(char *err, int s, struct sockaddr *sa, socklen_t *len) {
+ int fd;
+ do {
+ /* Use the accept4() call on linux to simultaneously accept and
+ * set a socket as non-blocking. */
+#ifdef HAVE_ACCEPT4
+ fd = accept4(s, sa, len, SOCK_NONBLOCK | SOCK_CLOEXEC);
+#else
+ fd = accept(s,sa,len);
+#endif
+ } while(fd == -1 && errno == EINTR);
+ if (fd == -1) {
+ anetSetError(err, "accept: %s", strerror(errno));
+ return ANET_ERR;
+ }
+#ifndef HAVE_ACCEPT4
+ if (anetCloexec(fd) == -1) {
+ anetSetError(err, "anetCloexec: %s", strerror(errno));
+ close(fd);
+ return ANET_ERR;
+ }
+ if (anetNonBlock(err, fd) != ANET_OK) {
+ close(fd);
+ return ANET_ERR;
+ }
+#endif
+ return fd;
+}
+
+/* Accept a connection and also make sure the socket is non-blocking, and CLOEXEC.
+ * returns the new socket FD, or -1 on error. */
+int anetTcpAccept(char *err, int serversock, char *ip, size_t ip_len, int *port) {
+ int fd;
+ struct sockaddr_storage sa;
+ socklen_t salen = sizeof(sa);
+ if ((fd = anetGenericAccept(err,serversock,(struct sockaddr*)&sa,&salen)) == ANET_ERR)
+ return ANET_ERR;
+
+ if (sa.ss_family == AF_INET) {
+ struct sockaddr_in *s = (struct sockaddr_in *)&sa;
+ if (ip) inet_ntop(AF_INET,(void*)&(s->sin_addr),ip,ip_len);
+ if (port) *port = ntohs(s->sin_port);
+ } else {
+ struct sockaddr_in6 *s = (struct sockaddr_in6 *)&sa;
+ if (ip) inet_ntop(AF_INET6,(void*)&(s->sin6_addr),ip,ip_len);
+ if (port) *port = ntohs(s->sin6_port);
+ }
+ return fd;
+}
+
+/* Accept a connection and also make sure the socket is non-blocking, and CLOEXEC.
+ * returns the new socket FD, or -1 on error. */
+int anetUnixAccept(char *err, int s) {
+ int fd;
+ struct sockaddr_un sa;
+ socklen_t salen = sizeof(sa);
+ if ((fd = anetGenericAccept(err,s,(struct sockaddr*)&sa,&salen)) == ANET_ERR)
+ return ANET_ERR;
+
+ return fd;
+}
+
+int anetFdToString(int fd, char *ip, size_t ip_len, int *port, int remote) {
+ struct sockaddr_storage sa;
+ socklen_t salen = sizeof(sa);
+
+ if (remote) {
+ if (getpeername(fd, (struct sockaddr *)&sa, &salen) == -1) goto error;
+ } else {
+ if (getsockname(fd, (struct sockaddr *)&sa, &salen) == -1) goto error;
+ }
+
+ if (sa.ss_family == AF_INET) {
+ struct sockaddr_in *s = (struct sockaddr_in *)&sa;
+ if (ip) {
+ if (inet_ntop(AF_INET,(void*)&(s->sin_addr),ip,ip_len) == NULL)
+ goto error;
+ }
+ if (port) *port = ntohs(s->sin_port);
+ } else if (sa.ss_family == AF_INET6) {
+ struct sockaddr_in6 *s = (struct sockaddr_in6 *)&sa;
+ if (ip) {
+ if (inet_ntop(AF_INET6,(void*)&(s->sin6_addr),ip,ip_len) == NULL)
+ goto error;
+ }
+ if (port) *port = ntohs(s->sin6_port);
+ } else if (sa.ss_family == AF_UNIX) {
+ if (ip) {
+ int res = snprintf(ip, ip_len, "/unixsocket");
+ if (res < 0 || (unsigned int) res >= ip_len) goto error;
+ }
+ if (port) *port = 0;
+ } else {
+ goto error;
+ }
+ return 0;
+
+error:
+ if (ip) {
+ if (ip_len >= 2) {
+ ip[0] = '?';
+ ip[1] = '\0';
+ } else if (ip_len == 1) {
+ ip[0] = '\0';
+ }
+ }
+ if (port) *port = 0;
+ return -1;
+}
+
+/* Create a pipe buffer with given flags for read end and write end.
+ * Note that it supports the file flags defined by pipe2() and fcntl(F_SETFL),
+ * and one of the use cases is O_CLOEXEC|O_NONBLOCK. */
+int anetPipe(int fds[2], int read_flags, int write_flags) {
+ int pipe_flags = 0;
+#if defined(__linux__) || defined(__FreeBSD__)
+ /* When possible, try to leverage pipe2() to apply flags that are common to both ends.
+ * There is no harm to set O_CLOEXEC to prevent fd leaks. */
+ pipe_flags = O_CLOEXEC | (read_flags & write_flags);
+ if (pipe2(fds, pipe_flags)) {
+ /* Fail on real failures, and fallback to simple pipe if pipe2 is unsupported. */
+ if (errno != ENOSYS && errno != EINVAL)
+ return -1;
+ pipe_flags = 0;
+ } else {
+ /* If the flags on both ends are identical, no need to do anything else. */
+ if ((O_CLOEXEC | read_flags) == (O_CLOEXEC | write_flags))
+ return 0;
+ /* Clear the flags which have already been set using pipe2. */
+ read_flags &= ~pipe_flags;
+ write_flags &= ~pipe_flags;
+ }
+#endif
+
+ /* When we reach here with pipe_flags of 0, it means pipe2 failed (or was not attempted),
+ * so we try to use pipe. Otherwise, we skip and proceed to set specific flags below. */
+ if (pipe_flags == 0 && pipe(fds))
+ return -1;
+
+ /* File descriptor flags.
+ * Currently, only one such flag is defined: FD_CLOEXEC, the close-on-exec flag. */
+ if (read_flags & O_CLOEXEC)
+ if (fcntl(fds[0], F_SETFD, FD_CLOEXEC))
+ goto error;
+ if (write_flags & O_CLOEXEC)
+ if (fcntl(fds[1], F_SETFD, FD_CLOEXEC))
+ goto error;
+
+ /* File status flags after clearing the file descriptor flag O_CLOEXEC. */
+ read_flags &= ~O_CLOEXEC;
+ if (read_flags)
+ if (fcntl(fds[0], F_SETFL, read_flags))
+ goto error;
+ write_flags &= ~O_CLOEXEC;
+ if (write_flags)
+ if (fcntl(fds[1], F_SETFL, write_flags))
+ goto error;
+
+ return 0;
+
+error:
+ close(fds[0]);
+ close(fds[1]);
+ return -1;
+}
+
+int anetSetSockMarkId(char *err, int fd, uint32_t id) {
+#ifdef HAVE_SOCKOPTMARKID
+ if (setsockopt(fd, SOL_SOCKET, SOCKOPTMARKID, (void *)&id, sizeof(id)) == -1) {
+ anetSetError(err, "setsockopt: %s", strerror(errno));
+ return ANET_ERR;
+ }
+ return ANET_OK;
+#else
+ UNUSED(fd);
+ UNUSED(id);
+ anetSetError(err,"anetSetSockMarkid unsupported on this platform");
+ return ANET_OK;
+#endif
+}
+
+int anetIsFifo(char *filepath) {
+ struct stat sb;
+ if (stat(filepath, &sb) == -1) return 0;
+ return S_ISFIFO(sb.st_mode);
+}
+
+/* This function must be called after accept4() fails. It returns 1 if 'err'
+ * indicates accepted connection faced an error, and it's okay to continue
+ * accepting next connection by calling accept4() again. Other errors either
+ * indicate programming errors, e.g. calling accept() on a closed fd or indicate
+ * a resource limit has been reached, e.g. -EMFILE, open fd limit has been
+ * reached. In the latter case, caller might wait until resources are available.
+ * See accept4() documentation for details. */
+int anetAcceptFailureNeedsRetry(int err) {
+ if (err == ECONNABORTED)
+ return 1;
+
+#if defined(__linux__)
+ /* For details, see 'Error Handling' section on
+ * https://man7.org/linux/man-pages/man2/accept.2.html */
+ if (err == ENETDOWN || err == EPROTO || err == ENOPROTOOPT ||
+ err == EHOSTDOWN || err == ENONET || err == EHOSTUNREACH ||
+ err == EOPNOTSUPP || err == ENETUNREACH)
+ {
+ return 1;
+ }
+#endif
+ return 0;
+}
diff --git a/deps/ae/anet.h b/deps/ae/anet.h
new file mode 100644
index 0000000..1d3aec9
--- /dev/null
+++ b/deps/ae/anet.h
@@ -0,0 +1,58 @@
+/* anet.c -- Basic TCP socket stuff made a bit less boring
+ *
+ * Copyright (c) 2006-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).
+ */
+
+#ifndef ANET_H
+#define ANET_H
+
+#include <sys/types.h>
+
+#define ANET_OK 0
+#define ANET_ERR -1
+#define ANET_ERR_LEN 256
+
+/* Flags used with certain functions. */
+#define ANET_NONE 0
+#define ANET_IP_ONLY (1<<0)
+#define ANET_PREFER_IPV4 (1<<1)
+#define ANET_PREFER_IPV6 (1<<2)
+
+#if defined(__sun) || defined(_AIX)
+#define AF_LOCAL AF_UNIX
+#endif
+
+#ifdef _AIX
+#undef ip_len
+#endif
+
+int anetTcpNonBlockConnect(char *err, const char *addr, int port);
+int anetTcpNonBlockBestEffortBindConnect(char *err, const char *addr, int port, const char *source_addr);
+int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len, int flags);
+int anetTcpServer(char *err, int port, char *bindaddr, int backlog);
+int anetTcp6Server(char *err, int port, char *bindaddr, int backlog);
+int anetUnixServer(char *err, char *path, mode_t perm, int backlog);
+int anetTcpAccept(char *err, int serversock, char *ip, size_t ip_len, int *port);
+int anetUnixAccept(char *err, int serversock);
+int anetNonBlock(char *err, int fd);
+int anetBlock(char *err, int fd);
+int anetCloexec(int fd);
+int anetEnableTcpNoDelay(char *err, int fd);
+int anetDisableTcpNoDelay(char *err, int fd);
+int anetSendTimeout(char *err, int fd, long long ms);
+int anetRecvTimeout(char *err, int fd, long long ms);
+int anetFdToString(int fd, char *ip, size_t ip_len, int *port, int remote);
+int anetKeepAlive(char *err, int fd, int interval);
+int anetFormatAddr(char *fmt, size_t fmt_len, char *ip, int port);
+int anetPipe(int fds[2], int read_flags, int write_flags);
+int anetSetSockMarkId(char *err, int fd, uint32_t id);
+int anetGetError(int fd);
+int anetIsFifo(char *filepath);
+int anetAcceptFailureNeedsRetry(int err);
+
+#endif
diff --git a/deps/ae/config.h b/deps/ae/config.h
new file mode 100644
index 0000000..a2fdabf
--- /dev/null
+++ b/deps/ae/config.h
@@ -0,0 +1,375 @@
+/*
+ * Copyright (c) 2009-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).
+ */
+
+#ifndef __CONFIG_H
+#define __CONFIG_H
+
+#include <sys/param.h>
+
+#ifdef __APPLE__
+#include <fcntl.h> // for fcntl(fd, F_FULLFSYNC)
+#include <AvailabilityMacros.h>
+#endif
+
+#ifdef __linux__
+#include <features.h>
+#include <fcntl.h>
+#endif
+
+#if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
+#define MAC_OS_10_6_DETECTED
+#endif
+
+/* Define redis_fstat to fstat or fstat64() */
+#if defined(__APPLE__) && !defined(MAC_OS_10_6_DETECTED)
+#define redis_fstat fstat64
+#define redis_stat stat64
+#else
+#define redis_fstat fstat
+#define redis_stat stat
+#endif
+
+#ifndef CACHE_LINE_SIZE
+#if defined(__aarch64__) && defined(__APPLE__)
+#define CACHE_LINE_SIZE 128
+#else
+#define CACHE_LINE_SIZE 64
+#endif
+#endif
+
+/* Test for proc filesystem */
+#ifdef __linux__
+#define HAVE_PROC_STAT 1
+#define HAVE_PROC_MAPS 1
+#define HAVE_PROC_SMAPS 1
+#define HAVE_PROC_SOMAXCONN 1
+#define HAVE_PROC_OOM_SCORE_ADJ 1
+#define HAVE_EVENT_FD 1
+#endif
+
+/* Test for task_info() */
+#if defined(__APPLE__)
+#define HAVE_TASKINFO 1
+#endif
+
+/* Test for somaxconn check */
+#if defined(__APPLE__) || defined(__FreeBSD__)
+#define HAVE_SYSCTL_KIPC_SOMAXCONN 1
+#elif defined(__OpenBSD__)
+#define HAVE_SYSCTL_KERN_SOMAXCONN 1
+#endif
+
+/* Test for backtrace() */
+#if defined(__APPLE__) || (defined(__linux__) && defined(__GLIBC__)) || \
+ defined(__FreeBSD__) || ((defined(__OpenBSD__) || defined(__NetBSD__) || defined(__sun)) && defined(USE_BACKTRACE))\
+ || defined(__DragonFly__) || (defined(__UCLIBC__) && defined(__UCLIBC_HAS_BACKTRACE__))
+#define HAVE_BACKTRACE 1
+#endif
+
+/* MSG_NOSIGNAL. */
+#ifdef __linux__
+#define HAVE_MSG_NOSIGNAL 1
+#if defined(SO_MARK)
+#define HAVE_SOCKOPTMARKID 1
+#define SOCKOPTMARKID SO_MARK
+#endif
+#endif
+
+/* Test for polling API */
+#ifdef __linux__
+#define HAVE_EPOLL 1
+#endif
+
+/* Test for accept4() */
+#if defined(__linux__) || (defined(OpenBSD) && OpenBSD >= 201505) || \
+ defined(__FreeBSD__) || \
+ (defined(__NetBSD_Version__) && __NetBSD_Version__ >= 800000000) || \
+ (defined(__DragonFly__) && __DragonFly_version >= 400305)
+#define HAVE_ACCEPT4 1
+#endif
+
+#if (defined(__APPLE__) && defined(MAC_OS_10_6_DETECTED)) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined (__NetBSD__)
+#define HAVE_KQUEUE 1
+#endif
+
+#ifdef __sun
+#include <sys/feature_tests.h>
+#ifdef _DTRACE_VERSION
+#define HAVE_EVPORT 1
+#define HAVE_PSINFO 1
+#endif
+#endif
+
+/* Test for __builtin_prefetch()
+ * Supported in LLVM since 2.9: https://releases.llvm.org/2.9/docs/ReleaseNotes.html
+ * Supported in GCC since 3.1 but we use 4.8 given it's too old: https://gcc.gnu.org/gcc-3.1/changes.html. */
+#if defined(__clang__) && (__clang_major__ > 2 || (__clang_major__ == 2 && __clang_minor__ >= 9))
+#define HAS_BUILTIN_PREFETCH 1
+#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
+#define HAS_BUILTIN_PREFETCH 1
+#else
+#define HAS_BUILTIN_PREFETCH 0
+#endif
+
+#if HAS_BUILTIN_PREFETCH
+#define redis_prefetch_read(addr) __builtin_prefetch(addr, 0, 3) /* Read with high locality */
+#define redis_prefetch_write(addr) __builtin_prefetch(addr, 1, 3) /* Write with high locality */
+#else
+#define redis_prefetch_read(addr) ((void)(addr)) /* No-op if unsupported */
+#define redis_prefetch_write(addr) ((void)(addr)) /* No-op if unsupported */
+#endif
+
+/* Define redis_fsync to fdatasync() in Linux and fsync() for all the rest */
+#if defined(__linux__)
+#define redis_fsync(fd) fdatasync(fd)
+#elif defined(__APPLE__)
+#define redis_fsync(fd) fcntl(fd, F_FULLFSYNC)
+#else
+#define redis_fsync(fd) fsync(fd)
+#endif
+
+#if defined(__FreeBSD__)
+#if defined(SO_USER_COOKIE)
+#define HAVE_SOCKOPTMARKID 1
+#define SOCKOPTMARKID SO_USER_COOKIE
+#endif
+#endif
+
+#if defined(__OpenBSD__)
+#if defined(SO_RTABLE)
+#define HAVE_SOCKOPTMARKID 1
+#define SOCKOPTMARKID SO_RTABLE
+#endif
+#endif
+
+#if __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
+#define redis_unreachable __builtin_unreachable
+#else
+#define redis_unreachable abort
+#endif
+
+#if __GNUC__ >= 3
+#define likely(x) __builtin_expect(!!(x), 1)
+#define unlikely(x) __builtin_expect(!!(x), 0)
+#else
+#define likely(x) (x)
+#define unlikely(x) (x)
+#endif
+
+#if defined(__has_attribute)
+#if __has_attribute(no_sanitize)
+#define REDIS_NO_SANITIZE(sanitizer) __attribute__((no_sanitize(sanitizer)))
+#endif
+#endif
+#if !defined(REDIS_NO_SANITIZE)
+#define REDIS_NO_SANITIZE(sanitizer)
+#endif
+
+#if defined(__clang__)
+#define REDIS_NO_SANITIZE_MSAN(sanitizer) REDIS_NO_SANITIZE(sanitizer)
+#else
+#define REDIS_NO_SANITIZE_MSAN(sanitizer)
+#endif
+
+/* Define rdb_fsync_range to sync_file_range() on Linux, otherwise we use
+ * the plain fsync() call. */
+#if (defined(__linux__) && defined(SYNC_FILE_RANGE_WAIT_BEFORE))
+#define HAVE_SYNC_FILE_RANGE 1
+#define rdb_fsync_range(fd,off,size) sync_file_range(fd,off,size,SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE)
+#elif defined(__APPLE__)
+#define rdb_fsync_range(fd,off,size) fcntl(fd, F_FULLFSYNC)
+#else
+#define rdb_fsync_range(fd,off,size) fsync(fd)
+#endif
+
+/* Check if we can use setproctitle().
+ * BSD systems have support for it, we provide an implementation for
+ * Linux and osx. */
+#if (defined __NetBSD__ || defined __FreeBSD__ || defined __OpenBSD__)
+#define USE_SETPROCTITLE
+#endif
+
+#if defined(__HAIKU__)
+#define ESOCKTNOSUPPORT 0
+#endif
+
+#if (defined __linux || defined __APPLE__)
+#define USE_SETPROCTITLE
+#define INIT_SETPROCTITLE_REPLACEMENT
+void spt_init(int argc, char *argv[]);
+void setproctitle(const char *fmt, ...);
+#endif
+
+/* Byte ordering detection */
+#include <sys/types.h> /* This will likely define BYTE_ORDER */
+
+#ifndef BYTE_ORDER
+#if (BSD >= 199103)
+# include <machine/endian.h>
+#else
+#if defined(linux) || defined(__linux__)
+# include <endian.h>
+#else
+#define LITTLE_ENDIAN 1234 /* least-significant byte first (vax, pc) */
+#define BIG_ENDIAN 4321 /* most-significant byte first (IBM, net) */
+#define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp)*/
+
+#if defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || \
+ defined(vax) || defined(ns32000) || defined(sun386) || \
+ defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || \
+ defined(__alpha__) || defined(__alpha)
+#define BYTE_ORDER LITTLE_ENDIAN
+#endif
+
+#if defined(sel) || defined(pyr) || defined(mc68000) || defined(sparc) || \
+ defined(is68k) || defined(tahoe) || defined(ibm032) || defined(ibm370) || \
+ defined(MIPSEB) || defined(_MIPSEB) || defined(_IBMR2) || defined(DGUX) ||\
+ defined(apollo) || defined(__convex__) || defined(_CRAY) || \
+ defined(__hppa) || defined(__hp9000) || \
+ defined(__hp9000s300) || defined(__hp9000s700) || \
+ defined (BIT_ZERO_ON_LEFT) || defined(m68k) || defined(__sparc)
+#define BYTE_ORDER BIG_ENDIAN
+#endif
+#endif /* linux */
+#endif /* BSD */
+#endif /* BYTE_ORDER */
+
+/* Sometimes after including an OS-specific header that defines the
+ * endianness we end with __BYTE_ORDER but not with BYTE_ORDER that is what
+ * the Redis code uses. In this case let's define everything without the
+ * underscores. */
+#ifndef BYTE_ORDER
+#ifdef __BYTE_ORDER
+#if defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
+#ifndef LITTLE_ENDIAN
+#define LITTLE_ENDIAN __LITTLE_ENDIAN
+#endif
+#ifndef BIG_ENDIAN
+#define BIG_ENDIAN __BIG_ENDIAN
+#endif
+#if (__BYTE_ORDER == __LITTLE_ENDIAN)
+#define BYTE_ORDER LITTLE_ENDIAN
+#else
+#define BYTE_ORDER BIG_ENDIAN
+#endif
+#endif
+#endif
+#endif
+
+#if !defined(BYTE_ORDER) || \
+ (BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN)
+ /* you must determine what the correct bit order is for
+ * your compiler - the next line is an intentional error
+ * which will force your compiles to bomb until you fix
+ * the above macros.
+ */
+#error "Undefined or invalid BYTE_ORDER"
+#endif
+
+#if (__i386 || __amd64 || __powerpc__) && __GNUC__
+#define GNUC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
+#if defined(__clang__)
+#define HAVE_ATOMIC
+#endif
+#if (defined(__GLIBC__) && defined(__GLIBC_PREREQ))
+#if (GNUC_VERSION >= 40100 && __GLIBC_PREREQ(2, 6))
+#define HAVE_ATOMIC
+#endif
+#endif
+#endif
+
+/* Make sure we can test for ARM just checking for __arm__, since sometimes
+ * __arm is defined but __arm__ is not. */
+#if defined(__arm) && !defined(__arm__)
+#define __arm__
+#endif
+#if defined (__aarch64__) && !defined(__arm64__)
+#define __arm64__
+#endif
+
+/* Make sure we can test for SPARC just checking for __sparc__. */
+#if defined(__sparc) && !defined(__sparc__)
+#define __sparc__
+#endif
+
+#if defined(__sparc__) || defined(__arm__)
+#define USE_ALIGNED_ACCESS
+#endif
+
+/* Define for redis_set_thread_title */
+#ifdef __linux__
+#define redis_set_thread_title(name) pthread_setname_np(pthread_self(), name)
+#else
+#if (defined __FreeBSD__ || defined __OpenBSD__)
+#include <pthread_np.h>
+#define redis_set_thread_title(name) pthread_set_name_np(pthread_self(), name)
+#elif defined __NetBSD__
+#include <pthread.h>
+#define redis_set_thread_title(name) pthread_setname_np(pthread_self(), "%s", name)
+#elif defined __HAIKU__
+#include <kernel/OS.h>
+#define redis_set_thread_title(name) rename_thread(find_thread(0), name)
+#else
+#if (defined __APPLE__ && defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
+int pthread_setname_np(const char *name);
+#include <pthread.h>
+#define redis_set_thread_title(name) pthread_setname_np(name)
+#else
+#define redis_set_thread_title(name)
+#endif
+#endif
+#endif
+
+/* Check if we can use setcpuaffinity(). */
+#if (defined __linux || defined __NetBSD__ || defined __FreeBSD__ || defined __DragonFly__)
+#define USE_SETCPUAFFINITY
+void setcpuaffinity(const char *cpulist);
+#endif
+
+/* Test for posix_fadvise() */
+#if defined(__linux__) || defined(__FreeBSD__)
+#define HAVE_FADVISE
+#endif
+
+#if defined(__x86_64__) && ((defined(__GNUC__) && __GNUC__ > 5) || (defined(__clang__)))
+ #if defined(__has_attribute) && __has_attribute(target)
+ #define HAVE_POPCNT
+ #define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
+ #else
+ #define ATTRIBUTE_TARGET_POPCNT
+ #endif
+#else
+ #define ATTRIBUTE_TARGET_POPCNT
+#endif
+
+/* Check if we can compile AVX2 code */
+#if defined (__x86_64__) && ((defined(__GNUC__) && __GNUC__ >= 5) || (defined(__clang__) && __clang_major__ >= 4))
+#if defined(__has_attribute) && __has_attribute(target)
+#define HAVE_AVX2
+#define ATTRIBUTE_TARGET_AVX2 __attribute__((target("avx2")))
+#define ATTRIBUTE_TARGET_AVX2_POPCOUNT __attribute__((target("avx2,popcnt")))
+#endif
+#endif
+
+/* Check if we can compile AVX512 code */
+#if defined (__x86_64__) && ((defined(__GNUC__) && __GNUC__ >= 5) || (defined(__clang__) && __clang_major__ >= 4))
+#if defined(__has_attribute) && __has_attribute(target)
+#define HAVE_AVX512
+#define ATTRIBUTE_TARGET_AVX512_POPCOUNT __attribute__((target("avx512f,avx512vpopcntdq")))
+#endif
+#endif
+
+/* Check for AArch64 (ARM v8) specific optimizations */
+#if defined(__aarch64__) && ((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__))
+#if defined(__has_attribute) && __has_attribute(target)
+#define HAVE_AARCH64_NEON
+#endif
+#endif
+
+#endif
diff --git a/deps/ae/fmacros.h b/deps/ae/fmacros.h
new file mode 100644
index 0000000..d05230d
--- /dev/null
+++ b/deps/ae/fmacros.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2009-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).
+ */
+
+#ifndef _REDIS_FMACRO_H
+#define _REDIS_FMACRO_H
+
+#define _BSD_SOURCE
+
+#if defined(__linux__)
+#define _GNU_SOURCE
+#define _DEFAULT_SOURCE
+#endif
+
+#if defined(_AIX)
+#define _ALL_SOURCE
+#endif
+
+#if defined(__linux__) || defined(__OpenBSD__)
+#define _XOPEN_SOURCE 700
+/*
+ * On NetBSD, _XOPEN_SOURCE undefines _NETBSD_SOURCE and
+ * thus hides inet_aton etc.
+ */
+#elif !defined(__NetBSD__)
+#define _XOPEN_SOURCE
+#endif
+
+#if defined(__sun)
+#define _POSIX_C_SOURCE 199506L
+#endif
+
+#define _LARGEFILE_SOURCE
+#define _FILE_OFFSET_BITS 64
+
+/* deprecate unsafe functions
+ *
+ * NOTE: We do not use the poison pragma since it
+ * will error on stdlib definitions in files as well*/
+#if (__GNUC__ && __GNUC__ >= 4) && !defined __APPLE__
+int sprintf(char *str, const char *format, ...) __attribute__((deprecated("please avoid use of unsafe C functions. prefer use of snprintf instead")));
+char *strcpy(char *restrict dest, const char *src) __attribute__((deprecated("please avoid use of unsafe C functions. prefer use of redis_strlcpy instead")));
+char *strcat(char *restrict dest, const char *restrict src) __attribute__((deprecated("please avoid use of unsafe C functions. prefer use of redis_strlcat instead")));
+#endif
+
+#ifdef __linux__
+/* features.h uses the defines above to set feature specific defines. */
+#include <features.h>
+#endif
+
+#endif
diff --git a/deps/ae/monotonic.c b/deps/ae/monotonic.c
new file mode 100644
index 0000000..b198fe3
--- /dev/null
+++ b/deps/ae/monotonic.c
@@ -0,0 +1,247 @@
+#include "monotonic.h"
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include "redisassert.h"
+#include <string.h>
+
+/* The function pointer for clock retrieval. */
+monotime (*getMonotonicUs)(void) = NULL;
+
+static char monotonic_info_string[32];
+
+
+/* Using the processor clock (aka TSC on x86) can provide improved performance
+ * throughout Redis wherever the monotonic clock is used. The processor clock
+ * is significantly faster than calling 'clock_getting' (POSIX). While this is
+ * generally safe on modern systems, this link provides additional information
+ * about use of the x86 TSC: http://oliveryang.net/2015/09/pitfalls-of-TSC-usage
+ *
+ * On ARM aarch64 systems, the hardware clock is enabled by default because the
+ * ARM Generic Timer is architecturally guaranteed to be available and monotonic
+ * on all ARMv8-A processors (see the “The Generic Timer in AArch64 state”
+ * section of the Arm Architecture Reference Manual for Armv8-A).
+ *
+ * To use the processor clock on other architectures, either uncomment this line,
+ * or build with
+ * CFLAGS="-DUSE_PROCESSOR_CLOCK"
+#define USE_PROCESSOR_CLOCK
+ */
+
+
+#if defined(USE_PROCESSOR_CLOCK) && defined(__x86_64__) && defined(__linux__)
+#include <regex.h>
+#include <x86intrin.h>
+
+static long mono_ticksPerMicrosecond = 0;
+
+static monotime getMonotonicUs_x86(void) {
+ return __rdtsc() / mono_ticksPerMicrosecond;
+}
+
+static void monotonicInit_x86linux(void) {
+ const int bufflen = 256;
+ char buf[bufflen];
+ regex_t cpuGhzRegex, constTscRegex;
+ const size_t nmatch = 2;
+ regmatch_t pmatch[nmatch];
+ int constantTsc = 0;
+ int rc;
+
+ /* Determine the number of TSC ticks in a micro-second. This is
+ * a constant value matching the standard speed of the processor.
+ * On modern processors, this speed remains constant even though
+ * the actual clock speed varies dynamically for each core. */
+ rc = regcomp(&cpuGhzRegex, "^model name\\s+:.*@ ([0-9.]+)GHz", REG_EXTENDED);
+ assert(rc == 0);
+
+ /* Also check that the constant_tsc flag is present. (It should be
+ * unless this is a really old CPU. */
+ rc = regcomp(&constTscRegex, "^flags\\s+:.* constant_tsc", REG_EXTENDED);
+ assert(rc == 0);
+
+ FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
+ if (cpuinfo != NULL) {
+ while (fgets(buf, bufflen, cpuinfo) != NULL) {
+ if (regexec(&cpuGhzRegex, buf, nmatch, pmatch, 0) == 0) {
+ buf[pmatch[1].rm_eo] = '\0';
+ double ghz = atof(&buf[pmatch[1].rm_so]);
+ mono_ticksPerMicrosecond = (long)(ghz * 1000);
+ break;
+ }
+ }
+ while (fgets(buf, bufflen, cpuinfo) != NULL) {
+ if (regexec(&constTscRegex, buf, nmatch, pmatch, 0) == 0) {
+ constantTsc = 1;
+ break;
+ }
+ }
+
+ fclose(cpuinfo);
+ }
+ regfree(&cpuGhzRegex);
+ regfree(&constTscRegex);
+
+ if (mono_ticksPerMicrosecond == 0) {
+ fprintf(stderr, "monotonic: x86 linux, unable to determine clock rate\n");
+ return;
+ }
+ if (!constantTsc) {
+ fprintf(stderr, "monotonic: x86 linux, 'constant_tsc' flag not present\n");
+ return;
+ }
+
+ snprintf(monotonic_info_string, sizeof(monotonic_info_string),
+ "X86 TSC @ %ld ticks/us", mono_ticksPerMicrosecond);
+ getMonotonicUs = getMonotonicUs_x86;
+}
+#endif
+
+#if defined(__aarch64__)
+static long mono_ticksPerMicrosecond = 0;
+
+/* Read the clock value.
+ * CNTVCT_EL0 is a system counter register, that provides the monotonic
+ * timestamp as a 64-bit count value. */
+static inline uint64_t __cntvct(void) {
+ uint64_t virtual_timer_value;
+ __asm__ volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
+ return virtual_timer_value;
+}
+
+/* Read the Count-timer Frequency.
+ * CNTFRQ_EL0 is a system counter register that provides the frequency (in Hz)
+ * needed to convert ticks to microseconds. Together with CNTVCT_EL0, this enables
+ * high-performance monotonic time measurement without system calls. */
+static inline uint32_t cntfrq_hz(void) {
+ uint64_t virtual_freq_value;
+ __asm__ volatile("mrs %0, cntfrq_el0" : "=r"(virtual_freq_value));
+ return (uint32_t)virtual_freq_value; /* top 32 bits are reserved */
+}
+
+static monotime getMonotonicUs_aarch64(void) {
+ return __cntvct() / mono_ticksPerMicrosecond;
+}
+
+static void monotonicInit_aarch64(void) {
+ mono_ticksPerMicrosecond = (long)cntfrq_hz() / 1000L / 1000L;
+ if (mono_ticksPerMicrosecond == 0) {
+ fprintf(stderr, "monotonic: aarch64, unable to determine clock rate\n");
+ return;
+ }
+
+ snprintf(monotonic_info_string, sizeof(monotonic_info_string),
+ "ARM CNTVCT @ %ld ticks/us", mono_ticksPerMicrosecond);
+ getMonotonicUs = getMonotonicUs_aarch64;
+}
+#endif
+
+
+#if defined(USE_PROCESSOR_CLOCK) && defined(__riscv) && defined(__linux__)
+static long mono_ticksPerMicrosecond = 0;
+
+static inline uint64_t read_mtime(void) {
+ uint64_t val;
+ asm volatile("csrr %0, time" : "=r"(val));
+ return val;
+}
+
+/* Read RISC-V timebase-frequency, which may be stored as either a 64-bit
+ * or 32-bit big-endian integer in the device tree. */
+static uint64_t get_timebase_frequency(void) {
+ uint64_t freq = 0;
+ FILE *fp = fopen("/proc/device-tree/cpus/timebase-frequency", "rb");
+ if (!fp)
+ return 0;
+
+ uint8_t buf[8] = {0};
+ size_t cnt = fread(buf, 1, sizeof(buf), fp);
+ fclose(fp);
+
+ if (cnt == 8) {
+ uint64_t be64 = 0;
+ memcpy(&be64, buf, sizeof(be64));
+ /* Convert be64 from big-endian to little-endian. */
+ freq = __builtin_bswap64(be64);
+ } else if (cnt == 4) {
+ uint32_t be32 = 0;
+ memcpy(&be32, buf, sizeof(be32));
+ /* Convert be32 from big-endian to little-endian. */
+ freq = __builtin_bswap32(be32);
+ } else {
+ /* Unable to read timebase-frequency. */
+ return 0;
+ }
+
+ return freq;
+}
+
+static monotime getMonotonicUs_riscv(void) {
+ return read_mtime() / mono_ticksPerMicrosecond;
+}
+
+static void monotonicInit_riscv(void) {
+ mono_ticksPerMicrosecond = (long)get_timebase_frequency() / 1000L / 1000L;
+ if (mono_ticksPerMicrosecond == 0) {
+ fprintf(stderr, "monotonic: riscv, unable to determine clock rate\n");
+ return;
+ }
+ snprintf(monotonic_info_string, sizeof(monotonic_info_string),
+ "RISC-V mtime @ %ld ticks/us", mono_ticksPerMicrosecond);
+ getMonotonicUs = getMonotonicUs_riscv;
+}
+#endif
+
+static monotime getMonotonicUs_posix(void) {
+ /* clock_gettime() is specified in POSIX.1b (1993). Even so, some systems
+ * did not support this until much later. CLOCK_MONOTONIC is technically
+ * optional and may not be supported - but it appears to be universal.
+ * If this is not supported, provide a system-specific alternate version. */
+ struct timespec ts;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return ((uint64_t)ts.tv_sec) * 1000000 + ts.tv_nsec / 1000;
+}
+
+static void monotonicInit_posix(void) {
+ /* Ensure that CLOCK_MONOTONIC is supported. This should be supported
+ * on any reasonably current OS. If the assertion below fails, provide
+ * an appropriate alternate implementation. */
+ struct timespec ts;
+ int rc = clock_gettime(CLOCK_MONOTONIC, &ts);
+ assert(rc == 0);
+
+ snprintf(monotonic_info_string, sizeof(monotonic_info_string),
+ "POSIX clock_gettime");
+ getMonotonicUs = getMonotonicUs_posix;
+}
+
+
+
+const char * monotonicInit(void) {
+ #if defined(USE_PROCESSOR_CLOCK) && defined(__x86_64__) && defined(__linux__)
+ if (getMonotonicUs == NULL) monotonicInit_x86linux();
+ #endif
+
+ #if defined(__aarch64__)
+ if (getMonotonicUs == NULL) monotonicInit_aarch64();
+ #endif
+
+ #if defined(USE_PROCESSOR_CLOCK) && defined(__riscv) && defined(__linux__)
+ if (getMonotonicUs == NULL) monotonicInit_riscv();
+ #endif
+
+ if (getMonotonicUs == NULL) monotonicInit_posix();
+
+ return monotonic_info_string;
+}
+
+const char *monotonicInfoString(void) {
+ return monotonic_info_string;
+}
+
+monotonic_clock_type monotonicGetType(void) {
+ if (getMonotonicUs == getMonotonicUs_posix)
+ return MONOTONIC_CLOCK_POSIX;
+ return MONOTONIC_CLOCK_HW;
+}
diff --git a/deps/ae/monotonic.h b/deps/ae/monotonic.h
new file mode 100644
index 0000000..b465f90
--- /dev/null
+++ b/deps/ae/monotonic.h
@@ -0,0 +1,61 @@
+#ifndef __MONOTONIC_H
+#define __MONOTONIC_H
+/* The monotonic clock is an always increasing clock source. It is unrelated to
+ * the actual time of day and should only be used for relative timings. The
+ * monotonic clock is also not guaranteed to be chronologically precise; there
+ * may be slight skew/shift from a precise clock.
+ *
+ * Depending on system architecture, the monotonic time may be able to be
+ * retrieved much faster than a normal clock source by using an instruction
+ * counter on the CPU. On x86 architectures (for example), the RDTSC
+ * instruction is a very fast clock source for this purpose.
+ */
+
+#include "fmacros.h"
+#include <stdint.h>
+#include <unistd.h>
+
+/* A counter in micro-seconds. The 'monotime' type is provided for variables
+ * holding a monotonic time. This will help distinguish & document that the
+ * variable is associated with the monotonic clock and should not be confused
+ * with other types of time.*/
+typedef uint64_t monotime;
+
+/* Retrieve counter of micro-seconds relative to an arbitrary point in time. */
+extern monotime (*getMonotonicUs)(void);
+
+typedef enum monotonic_clock_type {
+ MONOTONIC_CLOCK_POSIX,
+ MONOTONIC_CLOCK_HW,
+} monotonic_clock_type;
+
+/* Call once at startup to initialize the monotonic clock. Though this only
+ * needs to be called once, it may be called additional times without impact.
+ * Returns a printable string indicating the type of clock initialized.
+ * (The returned string is static and doesn't need to be freed.) */
+const char *monotonicInit(void);
+
+/* Return a string indicating the type of monotonic clock being used. */
+const char *monotonicInfoString(void);
+
+/* Return the type of monotonic clock being used. */
+monotonic_clock_type monotonicGetType(void);
+
+/* Functions to measure elapsed time. Example:
+ * monotime myTimer;
+ * elapsedStart(&myTimer);
+ * while (elapsedMs(myTimer) < 10) {} // loops for 10ms
+ */
+static inline void elapsedStart(monotime *start_time) {
+ *start_time = getMonotonicUs();
+}
+
+static inline uint64_t elapsedUs(monotime start_time) {
+ return getMonotonicUs() - start_time;
+}
+
+static inline uint64_t elapsedMs(monotime start_time) {
+ return elapsedUs(start_time) / 1000;
+}
+
+#endif
diff --git a/deps/ae/redisassert.h b/deps/ae/redisassert.h
new file mode 100644
index 0000000..8abecfb
--- /dev/null
+++ b/deps/ae/redisassert.h
@@ -0,0 +1,18 @@
+/* redisassert.h - simplified assert for standalone ae library */
+#ifndef __REDIS_ASSERT_H__
+#define __REDIS_ASSERT_H__
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+/* config.h provides redis_unreachable for standalone AE builds. */
+
+/* Panic function for error handling */
+#define panic(...) do { \
+ fprintf(stderr, "PANIC: " __VA_ARGS__); \
+ fprintf(stderr, "\n"); \
+ abort(); \
+} while(0)
+
+#endif
diff --git a/deps/ae/util.h b/deps/ae/util.h
new file mode 100644
index 0000000..630c4ca
--- /dev/null
+++ b/deps/ae/util.h
@@ -0,0 +1,21 @@
+/* util.h - minimal utilities needed by anet */
+#ifndef __UTIL_H
+#define __UTIL_H
+
+#include <string.h>
+
+/* Simple error buffer size */
+#define ANET_ERR_LEN 256
+
+/* Safe string copy - simplified version */
+static inline size_t redis_strlcpy(char *dst, const char *src, size_t dsize) {
+ size_t len = strlen(src);
+ if (dsize > 0) {
+ size_t copy_len = (len >= dsize) ? dsize - 1 : len;
+ memcpy(dst, src, copy_len);
+ dst[copy_len] = '\0';
+ }
+ return len;
+}
+
+#endif
diff --git a/deps/ae/zmalloc.h b/deps/ae/zmalloc.h
new file mode 100644
index 0000000..86e9d92
--- /dev/null
+++ b/deps/ae/zmalloc.h
@@ -0,0 +1,15 @@
+/* zmalloc.h - simplified memory allocation wrapper for ae library
+ * This is a minimal version for standalone use outside Redis.
+ */
+#ifndef __ZMALLOC_H
+#define __ZMALLOC_H
+
+#include <stdlib.h>
+
+/* Simple wrappers around standard malloc/free */
+#define zmalloc malloc
+#define zfree free
+#define zrealloc realloc
+#define zcalloc calloc
+
+#endif