
Modules can now register sockets/pipe to the Redis main thread event loop and do network operations asynchronously. Previously, modules had to maintain an event loop and another thread for asynchronous network operations. Also, if a module is calling API functions after doing some network operations, it had to synchronize its event loop thread's access with Redis main thread by locking the GIL, causing contention on the lock. After this commit, no synchronization is needed as module can operate in Redis main thread context. So, this commit may improve the performance for some use cases. Added three functions to the module API: * RedisModule_EventLoopAdd(int fd, int mask, RedisModuleEventLoopFunc func, void *user_data) * RedisModule_EventLoopDel(int fd, int mask) * RedisModule_EventLoopAddOneShot(RedisModuleEventLoopOneShotFunc func, void *user_data) - This function can be called from other threads to trigger callback on Redis main thread. Callback will be triggered only once. If Redis main thread is sleeping, this call will wake up the Redis main thread. Event loop callbacks are called by Redis main thread after locking the GIL. Inside callbacks, modules can operate as if they are holding the GIL. Added REDISMODULE_EVENT_EVENTLOOP event with two subevents: * REDISMODULE_SUBEVENT_EVENTLOOP_BEFORE_SLEEP * REDISMODULE_SUBEVENT_EVENTLOOP_AFTER_SLEEP These events are for modules that want to participate in the before and after sleep action. e.g It might be useful to implement batching : Read data from the network, write all to a file in one go on BEFORE_SLEEP event.
137 lines
5.4 KiB
C
137 lines
5.4 KiB
C
/* 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-2012, Salvatore Sanfilippo <antirez at gmail dot 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.
|
|
*/
|
|
|
|
#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;
|
|
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;
|
|
} 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
|