1 /* A simple event-driven programming library. Originally I wrote this code 2 * for the Jim's event-loop (Jim is a Tcl interpreter) but later translated 3 * it in form of a library for easy reuse. 4 * 5 * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * * Redistributions of source code must retain the above copyright notice, 12 * this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * * Neither the name of Redis nor the names of its contributors may be used 17 * to endorse or promote products derived from this software without 18 * specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #ifndef __AE_H__ 34 #define __AE_H__ 35 36 #include <time.h> 37 38 #define AE_OK 0 39 #define AE_ERR -1 40 41 #define AE_NONE 0 /* No events registered. */ 42 #define AE_READABLE 1 /* Fire when descriptor is readable. */ 43 #define AE_WRITABLE 2 /* Fire when descriptor is writable. */ 44 #define AE_BARRIER 4 /* With WRITABLE, never fire the event if the 45 READABLE event already fired in the same event 46 loop iteration. Useful when you want to persist 47 things to disk before sending replies, and want 48 to do that in a group fashion. */ 49 50 #define AE_FILE_EVENTS 1 51 #define AE_TIME_EVENTS 2 52 #define AE_ALL_EVENTS (AE_FILE_EVENTS|AE_TIME_EVENTS) 53 #define AE_DONT_WAIT 4 54 #define AE_CALL_AFTER_SLEEP 8 55 56 #define AE_NOMORE -1 57 #define AE_DELETED_EVENT_ID -1 58 59 /* Macros */ 60 #define AE_NOTUSED(V) ((void) V) 61 62 struct aeEventLoop; 63 64 /* Types and data structures */ 65 typedef void aeFileProc(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask); 66 typedef int aeTimeProc(struct aeEventLoop *eventLoop, long long id, void *clientData); 67 typedef void aeEventFinalizerProc(struct aeEventLoop *eventLoop, void *clientData); 68 typedef void aeBeforeSleepProc(struct aeEventLoop *eventLoop); 69 70 /* File event structure */ 71 typedef struct aeFileEvent { 72 int mask; /* one of AE_(READABLE|WRITABLE|BARRIER) */ 73 aeFileProc *rfileProc; 74 aeFileProc *wfileProc; 75 void *clientData; 76 } aeFileEvent; 77 78 /* Time event structure */ 79 typedef struct aeTimeEvent { 80 long long id; /* time event identifier. */ 81 long when_sec; /* seconds */ 82 long when_ms; /* milliseconds */ 83 aeTimeProc *timeProc; 84 aeEventFinalizerProc *finalizerProc; 85 void *clientData; 86 struct aeTimeEvent *prev; 87 struct aeTimeEvent *next; 88 } aeTimeEvent; 89 90 /* A fired event */ 91 typedef struct aeFiredEvent { 92 int fd; 93 int mask; 94 } aeFiredEvent; 95 96 /* State of an event based program */ 97 typedef struct aeEventLoop { 98 int maxfd; /* highest file descriptor currently registered */ 99 int setsize; /* max number of file descriptors tracked */ 100 long long timeEventNextId; 101 time_t lastTime; /* Used to detect system clock skew */ 102 aeFileEvent *events; /* Registered events */ 103 aeFiredEvent *fired; /* Fired events */ 104 aeTimeEvent *timeEventHead; 105 int stop; 106 void *apidata; /* This is used for polling API specific data */ 107 aeBeforeSleepProc *beforesleep; 108 aeBeforeSleepProc *aftersleep; 109 } aeEventLoop; 110 111 /* Prototypes */ 112 aeEventLoop *aeCreateEventLoop(int setsize); 113 void aeDeleteEventLoop(aeEventLoop *eventLoop); 114 void aeStop(aeEventLoop *eventLoop); 115 int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask, 116 aeFileProc *proc, void *clientData); 117 void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask); 118 int aeGetFileEvents(aeEventLoop *eventLoop, int fd); 119 long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds, 120 aeTimeProc *proc, void *clientData, 121 aeEventFinalizerProc *finalizerProc); 122 int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id); 123 int aeProcessEvents(aeEventLoop *eventLoop, int flags); 124 int aeWait(int fd, int mask, long long milliseconds); 125 void aeMain(aeEventLoop *eventLoop); 126 char *aeGetApiName(void); 127 void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep); 128 void aeSetAfterSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *aftersleep); 129 int aeGetSetSize(aeEventLoop *eventLoop); 130 int aeResizeSetSize(aeEventLoop *eventLoop, int setsize); 131 132 #endif 133