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 "monotonic.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<<0)
51#define AE_TIME_EVENTS (1<<1)
52#define AE_ALL_EVENTS (AE_FILE_EVENTS|AE_TIME_EVENTS)
53#define AE_DONT_WAIT (1<<2)
54#define AE_CALL_BEFORE_SLEEP (1<<3)
55#define AE_CALL_AFTER_SLEEP (1<<4)
56
57#define AE_NOMORE -1
58#define AE_DELETED_EVENT_ID -1
59
60/* Macros */
61#define AE_NOTUSED(V) ((void) V)
62
63struct aeEventLoop;
64
65/* Types and data structures */
66typedef void aeFileProc(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);
67typedef int aeTimeProc(struct aeEventLoop *eventLoop, long long id, void *clientData);
68typedef void aeEventFinalizerProc(struct aeEventLoop *eventLoop, void *clientData);
69typedef void aeBeforeSleepProc(struct aeEventLoop *eventLoop);
70
71/* File event structure */
72typedef struct aeFileEvent {
73 int mask; /* one of AE_(READABLE|WRITABLE|BARRIER) */
74 aeFileProc *rfileProc;
75 aeFileProc *wfileProc;
76 void *clientData;
77} aeFileEvent;
78
79/* Time event structure */
80typedef struct aeTimeEvent {
81 long long id; /* time event identifier. */
82 monotime when;
83 aeTimeProc *timeProc;
84 aeEventFinalizerProc *finalizerProc;
85 void *clientData;
86 struct aeTimeEvent *prev;
87 struct aeTimeEvent *next;
88 int refcount; /* refcount to prevent timer events from being
89 * freed in recursive time event calls. */
90} aeTimeEvent;
91
92/* A fired event */
93typedef struct aeFiredEvent {
94 int fd;
95 int mask;
96} aeFiredEvent;
97
98/* State of an event based program */
99typedef struct aeEventLoop {
100 int maxfd; /* highest file descriptor currently registered */
101 int setsize; /* max number of file descriptors tracked */
102 long long timeEventNextId;
103 aeFileEvent *events; /* Registered events */
104 aeFiredEvent *fired; /* Fired events */
105 aeTimeEvent *timeEventHead;
106 int stop;
107 void *apidata; /* This is used for polling API specific data */
108 aeBeforeSleepProc *beforesleep;
109 aeBeforeSleepProc *aftersleep;
110 int flags;
111} aeEventLoop;
112
113/* Prototypes */
114aeEventLoop *aeCreateEventLoop(int setsize);
115void aeDeleteEventLoop(aeEventLoop *eventLoop);
116void aeStop(aeEventLoop *eventLoop);
117int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
118 aeFileProc *proc, void *clientData);
119void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask);
120int aeGetFileEvents(aeEventLoop *eventLoop, int fd);
121void *aeGetFileClientData(aeEventLoop *eventLoop, int fd);
122long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,
123 aeTimeProc *proc, void *clientData,
124 aeEventFinalizerProc *finalizerProc);
125int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id);
126int aeProcessEvents(aeEventLoop *eventLoop, int flags);
127int aeWait(int fd, int mask, long long milliseconds);
128void aeMain(aeEventLoop *eventLoop);
129char *aeGetApiName(void);
130void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep);
131void aeSetAfterSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *aftersleep);
132int aeGetSetSize(aeEventLoop *eventLoop);
133int aeResizeSetSize(aeEventLoop *eventLoop, int setsize);
134void aeSetDontWait(aeEventLoop *eventLoop, int noWait);
135
136#endif
137