]>
git.saurik.com Git - redis.git/blob - ae.h
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.
5 * Copyright (c) 2006-2009, Salvatore Sanfilippo <antirez at gmail dot com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
38 /* Types and data structures */
39 typedef void aeFileProc(struct aeEventLoop
*eventLoop
, int fd
, void *clientData
, int mask
);
40 typedef int aeTimeProc(struct aeEventLoop
*eventLoop
, long long id
, void *clientData
);
41 typedef void aeEventFinalizerProc(struct aeEventLoop
*eventLoop
, void *clientData
);
43 /* File event structure */
44 typedef struct aeFileEvent
{
46 int mask
; /* one of AE_(READABLE|WRITABLE|EXCEPTION) */
48 aeEventFinalizerProc
*finalizerProc
;
50 struct aeFileEvent
*next
;
53 /* Time event structure */
54 typedef struct aeTimeEvent
{
55 long long id
; /* time event identifier. */
56 long when_sec
; /* seconds */
57 long when_ms
; /* milliseconds */
59 aeEventFinalizerProc
*finalizerProc
;
61 struct aeTimeEvent
*next
;
64 /* State of an event based program */
65 typedef struct aeEventLoop
{
66 long long timeEventNextId
;
67 aeFileEvent
*fileEventHead
;
68 aeTimeEvent
*timeEventHead
;
78 #define AE_EXCEPTION 4
80 #define AE_FILE_EVENTS 1
81 #define AE_TIME_EVENTS 2
82 #define AE_ALL_EVENTS (AE_FILE_EVENTS|AE_TIME_EVENTS)
83 #define AE_DONT_WAIT 4
88 #define AE_NOTUSED(V) ((void) V)
91 aeEventLoop
*aeCreateEventLoop(void);
92 void aeDeleteEventLoop(aeEventLoop
*eventLoop
);
93 void aeStop(aeEventLoop
*eventLoop
);
94 int aeCreateFileEvent(aeEventLoop
*eventLoop
, int fd
, int mask
,
95 aeFileProc
*proc
, void *clientData
,
96 aeEventFinalizerProc
*finalizerProc
);
97 void aeDeleteFileEvent(aeEventLoop
*eventLoop
, int fd
, int mask
);
98 long long aeCreateTimeEvent(aeEventLoop
*eventLoop
, long long milliseconds
,
99 aeTimeProc
*proc
, void *clientData
,
100 aeEventFinalizerProc
*finalizerProc
);
101 int aeDeleteTimeEvent(aeEventLoop
*eventLoop
, long long id
);
102 int aeProcessEvents(aeEventLoop
*eventLoop
, int flags
);
103 int aeWait(int fd
, int mask
, long long milliseconds
);
104 void aeMain(aeEventLoop
*eventLoop
);