]> git.saurik.com Git - redis.git/blame - src/ae.h
Redis test: More reliable BRPOPLPUSH replication test.
[redis.git] / src / ae.h
CommitLineData
ed9b544e 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 *
12d090d2 5 * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
ed9b544e 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
266373b2 36#define AE_OK 0
37#define AE_ERR -1
38
39#define AE_NONE 0
40#define AE_READABLE 1
41#define AE_WRITABLE 2
266373b2 42
43#define AE_FILE_EVENTS 1
44#define AE_TIME_EVENTS 2
45#define AE_ALL_EVENTS (AE_FILE_EVENTS|AE_TIME_EVENTS)
46#define AE_DONT_WAIT 4
47
48#define AE_NOMORE -1
49
50/* Macros */
51#define AE_NOTUSED(V) ((void) V)
52
ed9b544e 53struct aeEventLoop;
54
55/* Types and data structures */
56typedef void aeFileProc(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);
57typedef int aeTimeProc(struct aeEventLoop *eventLoop, long long id, void *clientData);
58typedef void aeEventFinalizerProc(struct aeEventLoop *eventLoop, void *clientData);
d5d55fc3 59typedef void aeBeforeSleepProc(struct aeEventLoop *eventLoop);
ed9b544e 60
61/* File event structure */
62typedef struct aeFileEvent {
621d5c19 63 int mask; /* one of AE_(READABLE|WRITABLE) */
266373b2 64 aeFileProc *rfileProc;
65 aeFileProc *wfileProc;
ed9b544e 66 void *clientData;
ed9b544e 67} aeFileEvent;
68
69/* Time event structure */
70typedef struct aeTimeEvent {
71 long long id; /* time event identifier. */
72 long when_sec; /* seconds */
73 long when_ms; /* milliseconds */
74 aeTimeProc *timeProc;
75 aeEventFinalizerProc *finalizerProc;
76 void *clientData;
77 struct aeTimeEvent *next;
78} aeTimeEvent;
79
266373b2 80/* A fired event */
81typedef struct aeFiredEvent {
82 int fd;
83 int mask;
84} aeFiredEvent;
85
ed9b544e 86/* State of an event based program */
87typedef struct aeEventLoop {
e074416b 88 int maxfd; /* highest file descriptor currently registered */
89 int setsize; /* max number of file descriptors tracked */
ed9b544e 90 long long timeEventNextId;
e074416b 91 aeFileEvent *events; /* Registered events */
92 aeFiredEvent *fired; /* Fired events */
ed9b544e 93 aeTimeEvent *timeEventHead;
94 int stop;
266373b2 95 void *apidata; /* This is used for polling API specific data */
d5d55fc3 96 aeBeforeSleepProc *beforesleep;
ed9b544e 97} aeEventLoop;
98
ed9b544e 99/* Prototypes */
e074416b 100aeEventLoop *aeCreateEventLoop(int setsize);
ed9b544e 101void aeDeleteEventLoop(aeEventLoop *eventLoop);
102void aeStop(aeEventLoop *eventLoop);
103int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
266373b2 104 aeFileProc *proc, void *clientData);
ed9b544e 105void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask);
f14479c7 106int aeGetFileEvents(aeEventLoop *eventLoop, int fd);
ed9b544e 107long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,
108 aeTimeProc *proc, void *clientData,
109 aeEventFinalizerProc *finalizerProc);
110int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id);
111int aeProcessEvents(aeEventLoop *eventLoop, int flags);
112int aeWait(int fd, int mask, long long milliseconds);
113void aeMain(aeEventLoop *eventLoop);
7a932b74 114char *aeGetApiName(void);
d5d55fc3 115void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep);
ed9b544e 116
117#endif