Back to home page

Redis cross reference

 
 

    


0001 /* A simple event-driven programming library. Originally I wrote this code
0002  * for the Jim's event-loop (Jim is a Tcl interpreter) but later translated
0003  * it in form of a library for easy reuse.
0004  *
0005  * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com>
0006  * All rights reserved.
0007  *
0008  * Redistribution and use in source and binary forms, with or without
0009  * modification, are permitted provided that the following conditions are met:
0010  *
0011  *   * Redistributions of source code must retain the above copyright notice,
0012  *     this list of conditions and the following disclaimer.
0013  *   * Redistributions in binary form must reproduce the above copyright
0014  *     notice, this list of conditions and the following disclaimer in the
0015  *     documentation and/or other materials provided with the distribution.
0016  *   * Neither the name of Redis nor the names of its contributors may be used
0017  *     to endorse or promote products derived from this software without
0018  *     specific prior written permission.
0019  *
0020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0021  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0022  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0023  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0024  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0025  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0026  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0029  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0030  * POSSIBILITY OF SUCH DAMAGE.
0031  */
0032 
0033 #ifndef __AE_H__
0034 #define __AE_H__
0035 
0036 #define AE_OK 0
0037 #define AE_ERR -1
0038 
0039 #define AE_NONE 0
0040 #define AE_READABLE 1
0041 #define AE_WRITABLE 2
0042 
0043 #define AE_FILE_EVENTS 1
0044 #define AE_TIME_EVENTS 2
0045 #define AE_ALL_EVENTS (AE_FILE_EVENTS|AE_TIME_EVENTS)
0046 #define AE_DONT_WAIT 4
0047 
0048 #define AE_NOMORE -1
0049 
0050 /* Macros */
0051 #define AE_NOTUSED(V) ((void) V)
0052 
0053 struct aeEventLoop;
0054 
0055 /* Types and data structures */
0056 typedef void aeFileProc(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);
0057 typedef int aeTimeProc(struct aeEventLoop *eventLoop, long long id, void *clientData);
0058 typedef void aeEventFinalizerProc(struct aeEventLoop *eventLoop, void *clientData);
0059 typedef void aeBeforeSleepProc(struct aeEventLoop *eventLoop);
0060 
0061 /* File event structure */
0062 typedef struct aeFileEvent {
0063     int mask; /* one of AE_(READABLE|WRITABLE) */
0064     aeFileProc *rfileProc;
0065     aeFileProc *wfileProc;
0066     void *clientData;
0067 } aeFileEvent;
0068 
0069 /* Time event structure */
0070 typedef struct aeTimeEvent {
0071     long long id; /* time event identifier. */
0072     long when_sec; /* seconds */
0073     long when_ms; /* milliseconds */
0074     aeTimeProc *timeProc;
0075     aeEventFinalizerProc *finalizerProc;
0076     void *clientData;
0077     struct aeTimeEvent *next;
0078 } aeTimeEvent;
0079 
0080 /* A fired event */
0081 typedef struct aeFiredEvent {
0082     int fd;
0083     int mask;
0084 } aeFiredEvent;
0085 
0086 /* State of an event based program */
0087 typedef struct aeEventLoop {
0088     int maxfd;   /* highest file descriptor currently registered */
0089     int setsize; /* max number of file descriptors tracked */
0090     long long timeEventNextId;
0091     time_t lastTime;     /* Used to detect system clock skew */
0092     aeFileEvent *events; /* Registered events */
0093     aeFiredEvent *fired; /* Fired events */
0094     aeTimeEvent *timeEventHead;
0095     int stop;
0096     void *apidata; /* This is used for polling API specific data */
0097     aeBeforeSleepProc *beforesleep;
0098 } aeEventLoop;
0099 
0100 /* Prototypes */
0101 aeEventLoop *aeCreateEventLoop(int setsize);
0102 void aeDeleteEventLoop(aeEventLoop *eventLoop);
0103 void aeStop(aeEventLoop *eventLoop);
0104 int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
0105         aeFileProc *proc, void *clientData);
0106 void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask);
0107 int aeGetFileEvents(aeEventLoop *eventLoop, int fd);
0108 long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,
0109         aeTimeProc *proc, void *clientData,
0110         aeEventFinalizerProc *finalizerProc);
0111 int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id);
0112 int aeProcessEvents(aeEventLoop *eventLoop, int flags);
0113 int aeWait(int fd, int mask, long long milliseconds);
0114 void aeMain(aeEventLoop *eventLoop);
0115 char *aeGetApiName(void);
0116 void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep);
0117 
0118 #endif