move ae to C++

Former-commit-id: eb9070c8333ebe7d6e0d622f90e904c1b17e9710
This commit is contained in:
John Sully 2019-02-10 20:24:11 -05:00
parent f10bd5954b
commit e40a203a1d
6 changed files with 79 additions and 28 deletions

View File

@ -174,11 +174,11 @@ endif
REDIS_SERVER_NAME=keydb-server REDIS_SERVER_NAME=keydb-server
REDIS_SENTINEL_NAME=keydb-sentinel REDIS_SENTINEL_NAME=keydb-sentinel
REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o lolwut.o lolwut5.o acl.o storage.o rdb-s3.o REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o lolwut.o lolwut5.o acl.o storage.o rdb-s3.o fastlock.o
REDIS_CLI_NAME=keydb-cli REDIS_CLI_NAME=keydb-cli
REDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o zmalloc.o release.o anet.o ae.o crc64.o siphash.o crc16.o storage-lite.o REDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o zmalloc.o release.o anet.o ae.o crc64.o siphash.o crc16.o storage-lite.o fastlock.o
REDIS_BENCHMARK_NAME=keydb-benchmark REDIS_BENCHMARK_NAME=keydb-benchmark
REDIS_BENCHMARK_OBJ=ae.o anet.o redis-benchmark.o adlist.o zmalloc.o redis-benchmark.o storage-lite.o REDIS_BENCHMARK_OBJ=ae.o anet.o redis-benchmark.o adlist.o zmalloc.o redis-benchmark.o storage-lite.o fastlock.o
REDIS_CHECK_RDB_NAME=keydb-check-rdb REDIS_CHECK_RDB_NAME=keydb-check-rdb
REDIS_CHECK_AOF_NAME=keydb-check-aof REDIS_CHECK_AOF_NAME=keydb-check-aof

View File

@ -41,8 +41,10 @@
#include <errno.h> #include <errno.h>
#include "ae.h" #include "ae.h"
extern "C" {
#include "zmalloc.h" #include "zmalloc.h"
#include "config.h" #include "config.h"
}
/* Include the best multiplexing layer supported by this system. /* Include the best multiplexing layer supported by this system.
* The following should be ordered by performances, descending. */ * The following should be ordered by performances, descending. */
@ -50,7 +52,7 @@
#include "ae_evport.c" #include "ae_evport.c"
#else #else
#ifdef HAVE_EPOLL #ifdef HAVE_EPOLL
#include "ae_epoll.c" #include "ae_epoll.cpp"
#else #else
#ifdef HAVE_KQUEUE #ifdef HAVE_KQUEUE
#include "ae_kqueue.c" #include "ae_kqueue.c"
@ -64,9 +66,9 @@ aeEventLoop *aeCreateEventLoop(int setsize) {
aeEventLoop *eventLoop; aeEventLoop *eventLoop;
int i; int i;
if ((eventLoop = zmalloc(sizeof(*eventLoop), MALLOC_LOCAL)) == NULL) goto err; if ((eventLoop = (aeEventLoop*)zmalloc(sizeof(*eventLoop), MALLOC_LOCAL)) == NULL) goto err;
eventLoop->events = zmalloc(sizeof(aeFileEvent)*setsize, MALLOC_LOCAL); eventLoop->events = (aeFileEvent*)zmalloc(sizeof(aeFileEvent)*setsize, MALLOC_LOCAL);
eventLoop->fired = zmalloc(sizeof(aeFiredEvent)*setsize, MALLOC_LOCAL); eventLoop->fired = (aeFiredEvent*)zmalloc(sizeof(aeFiredEvent)*setsize, MALLOC_LOCAL);
if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err; if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err;
eventLoop->setsize = setsize; eventLoop->setsize = setsize;
eventLoop->lastTime = time(NULL); eventLoop->lastTime = time(NULL);
@ -111,8 +113,8 @@ int aeResizeSetSize(aeEventLoop *eventLoop, int setsize) {
if (eventLoop->maxfd >= setsize) return AE_ERR; if (eventLoop->maxfd >= setsize) return AE_ERR;
if (aeApiResize(eventLoop,setsize) == -1) return AE_ERR; if (aeApiResize(eventLoop,setsize) == -1) return AE_ERR;
eventLoop->events = zrealloc(eventLoop->events,sizeof(aeFileEvent)*setsize, MALLOC_LOCAL); eventLoop->events = (aeFileEvent*)zrealloc(eventLoop->events,sizeof(aeFileEvent)*setsize, MALLOC_LOCAL);
eventLoop->fired = zrealloc(eventLoop->fired,sizeof(aeFiredEvent)*setsize, MALLOC_LOCAL); eventLoop->fired = (aeFiredEvent*)zrealloc(eventLoop->fired,sizeof(aeFiredEvent)*setsize, MALLOC_LOCAL);
eventLoop->setsize = setsize; eventLoop->setsize = setsize;
/* Make sure that if we created new slots, they are initialized with /* Make sure that if we created new slots, they are initialized with
@ -122,18 +124,18 @@ int aeResizeSetSize(aeEventLoop *eventLoop, int setsize) {
return AE_OK; return AE_OK;
} }
void aeDeleteEventLoop(aeEventLoop *eventLoop) { extern "C" void aeDeleteEventLoop(aeEventLoop *eventLoop) {
aeApiFree(eventLoop); aeApiFree(eventLoop);
zfree(eventLoop->events); zfree(eventLoop->events);
zfree(eventLoop->fired); zfree(eventLoop->fired);
zfree(eventLoop); zfree(eventLoop);
} }
void aeStop(aeEventLoop *eventLoop) { extern "C" void aeStop(aeEventLoop *eventLoop) {
eventLoop->stop = 1; eventLoop->stop = 1;
} }
int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask, extern "C" int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
aeFileProc *proc, void *clientData) aeFileProc *proc, void *clientData)
{ {
if (fd >= eventLoop->setsize) { if (fd >= eventLoop->setsize) {
@ -153,7 +155,7 @@ int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
return AE_OK; return AE_OK;
} }
void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask) extern "C" void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask)
{ {
if (fd >= eventLoop->setsize) return; if (fd >= eventLoop->setsize) return;
aeFileEvent *fe = &eventLoop->events[fd]; aeFileEvent *fe = &eventLoop->events[fd];
@ -175,7 +177,7 @@ void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask)
} }
} }
int aeGetFileEvents(aeEventLoop *eventLoop, int fd) { extern "C" int aeGetFileEvents(aeEventLoop *eventLoop, int fd) {
if (fd >= eventLoop->setsize) return 0; if (fd >= eventLoop->setsize) return 0;
aeFileEvent *fe = &eventLoop->events[fd]; aeFileEvent *fe = &eventLoop->events[fd];
@ -205,14 +207,14 @@ static void aeAddMillisecondsToNow(long long milliseconds, long *sec, long *ms)
*ms = when_ms; *ms = when_ms;
} }
long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds, extern "C" long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,
aeTimeProc *proc, void *clientData, aeTimeProc *proc, void *clientData,
aeEventFinalizerProc *finalizerProc) aeEventFinalizerProc *finalizerProc)
{ {
long long id = eventLoop->timeEventNextId++; long long id = eventLoop->timeEventNextId++;
aeTimeEvent *te; aeTimeEvent *te;
te = zmalloc(sizeof(*te), MALLOC_LOCAL); te = (aeTimeEvent*)zmalloc(sizeof(*te), MALLOC_LOCAL);
if (te == NULL) return AE_ERR; if (te == NULL) return AE_ERR;
te->id = id; te->id = id;
aeAddMillisecondsToNow(milliseconds,&te->when_sec,&te->when_ms); aeAddMillisecondsToNow(milliseconds,&te->when_sec,&te->when_ms);
@ -227,7 +229,7 @@ long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,
return id; return id;
} }
int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id) extern "C" int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id)
{ {
aeTimeEvent *te = eventLoop->timeEventHead; aeTimeEvent *te = eventLoop->timeEventHead;
while(te) { while(te) {
@ -502,7 +504,7 @@ void aeMain(aeEventLoop *eventLoop) {
} }
} }
char *aeGetApiName(void) { const char *aeGetApiName(void) {
return aeApiName(); return aeApiName();
} }

View File

@ -35,6 +35,10 @@
#include <time.h> #include <time.h>
#ifdef __cplusplus
extern "C" {
#endif
#define AE_OK 0 #define AE_OK 0
#define AE_ERR -1 #define AE_ERR -1
@ -46,6 +50,7 @@
loop iteration. Useful when you want to persist loop iteration. Useful when you want to persist
things to disk before sending replies, and want things to disk before sending replies, and want
to do that in a group fashion. */ to do that in a group fashion. */
#define AE_THREADSAFE 8 /* Ok to run concurrently */
#define AE_FILE_EVENTS 1 #define AE_FILE_EVENTS 1
#define AE_TIME_EVENTS 2 #define AE_TIME_EVENTS 2
@ -123,10 +128,14 @@ int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id);
int aeProcessEvents(aeEventLoop *eventLoop, int flags); int aeProcessEvents(aeEventLoop *eventLoop, int flags);
int aeWait(int fd, int mask, long long milliseconds); int aeWait(int fd, int mask, long long milliseconds);
void aeMain(aeEventLoop *eventLoop); void aeMain(aeEventLoop *eventLoop);
char *aeGetApiName(void); const char *aeGetApiName(void);
void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep); void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep);
void aeSetAfterSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *aftersleep); void aeSetAfterSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *aftersleep);
int aeGetSetSize(aeEventLoop *eventLoop); int aeGetSetSize(aeEventLoop *eventLoop);
int aeResizeSetSize(aeEventLoop *eventLoop, int setsize); int aeResizeSetSize(aeEventLoop *eventLoop, int setsize);
#ifdef __cplusplus
}
#endif
#endif #endif

View File

@ -37,10 +37,10 @@ typedef struct aeApiState {
} aeApiState; } aeApiState;
static int aeApiCreate(aeEventLoop *eventLoop) { static int aeApiCreate(aeEventLoop *eventLoop) {
aeApiState *state = zmalloc(sizeof(aeApiState), MALLOC_LOCAL); aeApiState *state = (aeApiState*)zmalloc(sizeof(aeApiState), MALLOC_LOCAL);
if (!state) return -1; if (!state) return -1;
state->events = zmalloc(sizeof(struct epoll_event)*eventLoop->setsize, MALLOC_LOCAL); state->events = (epoll_event*)zmalloc(sizeof(struct epoll_event)*eventLoop->setsize, MALLOC_LOCAL);
if (!state->events) { if (!state->events) {
zfree(state); zfree(state);
return -1; return -1;
@ -56,14 +56,14 @@ static int aeApiCreate(aeEventLoop *eventLoop) {
} }
static int aeApiResize(aeEventLoop *eventLoop, int setsize) { static int aeApiResize(aeEventLoop *eventLoop, int setsize) {
aeApiState *state = eventLoop->apidata; aeApiState *state = (aeApiState*)eventLoop->apidata;
state->events = zrealloc(state->events, sizeof(struct epoll_event)*setsize, MALLOC_LOCAL); state->events = (epoll_event*)zrealloc(state->events, sizeof(struct epoll_event)*setsize, MALLOC_LOCAL);
return 0; return 0;
} }
static void aeApiFree(aeEventLoop *eventLoop) { static void aeApiFree(aeEventLoop *eventLoop) {
aeApiState *state = eventLoop->apidata; aeApiState *state = (aeApiState*)eventLoop->apidata;
close(state->epfd); close(state->epfd);
zfree(state->events); zfree(state->events);
@ -71,7 +71,7 @@ static void aeApiFree(aeEventLoop *eventLoop) {
} }
static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) { static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
aeApiState *state = eventLoop->apidata; aeApiState *state = (aeApiState*)eventLoop->apidata;
struct epoll_event ee = {0}; /* avoid valgrind warning */ struct epoll_event ee = {0}; /* avoid valgrind warning */
/* If the fd was already monitored for some event, we need a MOD /* If the fd was already monitored for some event, we need a MOD
* operation. Otherwise we need an ADD operation. */ * operation. Otherwise we need an ADD operation. */
@ -88,7 +88,7 @@ static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
} }
static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int delmask) { static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int delmask) {
aeApiState *state = eventLoop->apidata; aeApiState *state = (aeApiState*)eventLoop->apidata;
struct epoll_event ee = {0}; /* avoid valgrind warning */ struct epoll_event ee = {0}; /* avoid valgrind warning */
int mask = eventLoop->events[fd].mask & (~delmask); int mask = eventLoop->events[fd].mask & (~delmask);
@ -106,7 +106,7 @@ static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int delmask) {
} }
static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) { static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
aeApiState *state = eventLoop->apidata; aeApiState *state = (aeApiState*)eventLoop->apidata;
int retval, numevents = 0; int retval, numevents = 0;
retval = epoll_wait(state->epfd,state->events,eventLoop->setsize, retval = epoll_wait(state->epfd,state->events,eventLoop->setsize,
@ -130,6 +130,6 @@ static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
return numevents; return numevents;
} }
static char *aeApiName(void) { static const char *aeApiName(void) {
return "epoll"; return "epoll";
} }

21
src/fastlock.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "fastlock.h"
extern "C" void fastlock_init(struct fastlock *lock)
{
lock->lock = 0;
}
extern "C" void fastlock_lock(struct fastlock *lock)
{
while (!__sync_bool_compare_and_swap(&lock->lock, 0, 1));
}
extern "C" void fastlock_unlock(struct fastlock *lock)
{
lock->lock = 0;
}
extern "C" void fastlock_free(struct fastlock *lock)
{
// NOP
(void)lock;
}

19
src/fastlock.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
struct fastlock
{
int lock;
};
void fastlock_init(struct fastlock *lock);
void fastlock_lock(struct fastlock *lock);
void fastlock_unlock(struct fastlock *lock);
void fastlock_free(struct fastlock *lock);
#ifdef __cplusplus
}
#endif