]>
git.saurik.com Git - redis.git/blob - ae.c
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.
35 #include <sys/types.h>
42 aeEventLoop
*aeCreateEventLoop(void) {
43 aeEventLoop
*eventLoop
;
45 eventLoop
= zmalloc(sizeof(*eventLoop
));
46 if (!eventLoop
) return NULL
;
47 eventLoop
->fileEventHead
= NULL
;
48 eventLoop
->timeEventHead
= NULL
;
49 eventLoop
->timeEventNextId
= 0;
54 void aeDeleteEventLoop(aeEventLoop
*eventLoop
) {
58 void aeStop(aeEventLoop
*eventLoop
) {
62 int aeCreateFileEvent(aeEventLoop
*eventLoop
, int fd
, int mask
,
63 aeFileProc
*proc
, void *clientData
,
64 aeEventFinalizerProc
*finalizerProc
)
68 fe
= zmalloc(sizeof(*fe
));
69 if (fe
== NULL
) return AE_ERR
;
73 fe
->finalizerProc
= finalizerProc
;
74 fe
->clientData
= clientData
;
75 fe
->next
= eventLoop
->fileEventHead
;
76 eventLoop
->fileEventHead
= fe
;
80 void aeDeleteFileEvent(aeEventLoop
*eventLoop
, int fd
, int mask
)
82 aeFileEvent
*fe
, *prev
= NULL
;
84 fe
= eventLoop
->fileEventHead
;
86 if (fe
->fd
== fd
&& fe
->mask
== mask
) {
88 eventLoop
->fileEventHead
= fe
->next
;
90 prev
->next
= fe
->next
;
91 if (fe
->finalizerProc
)
92 fe
->finalizerProc(eventLoop
, fe
->clientData
);
101 static void aeGetTime(long *seconds
, long *milliseconds
)
105 gettimeofday(&tv
, NULL
);
106 *seconds
= tv
.tv_sec
;
107 *milliseconds
= tv
.tv_usec
/1000;
110 static void aeAddMillisecondsToNow(long long milliseconds
, long *sec
, long *ms
) {
111 long cur_sec
, cur_ms
, when_sec
, when_ms
;
113 aeGetTime(&cur_sec
, &cur_ms
);
114 when_sec
= cur_sec
+ milliseconds
/1000;
115 when_ms
= cur_ms
+ milliseconds%1000
;
116 if (when_ms
>= 1000) {
124 long long aeCreateTimeEvent(aeEventLoop
*eventLoop
, long long milliseconds
,
125 aeTimeProc
*proc
, void *clientData
,
126 aeEventFinalizerProc
*finalizerProc
)
128 long long id
= eventLoop
->timeEventNextId
++;
131 te
= zmalloc(sizeof(*te
));
132 if (te
== NULL
) return AE_ERR
;
134 aeAddMillisecondsToNow(milliseconds
,&te
->when_sec
,&te
->when_ms
);
136 te
->finalizerProc
= finalizerProc
;
137 te
->clientData
= clientData
;
138 te
->next
= eventLoop
->timeEventHead
;
139 eventLoop
->timeEventHead
= te
;
143 int aeDeleteTimeEvent(aeEventLoop
*eventLoop
, long long id
)
145 aeTimeEvent
*te
, *prev
= NULL
;
147 te
= eventLoop
->timeEventHead
;
151 eventLoop
->timeEventHead
= te
->next
;
153 prev
->next
= te
->next
;
154 if (te
->finalizerProc
)
155 te
->finalizerProc(eventLoop
, te
->clientData
);
162 return AE_ERR
; /* NO event with the specified ID found */
165 /* Search the first timer to fire.
166 * This operation is useful to know how many time the select can be
167 * put in sleep without to delay any event.
168 * If there are no timers NULL is returned.
170 * Note that's O(N) since time events are unsorted. */
171 static aeTimeEvent
*aeSearchNearestTimer(aeEventLoop
*eventLoop
)
173 aeTimeEvent
*te
= eventLoop
->timeEventHead
;
174 aeTimeEvent
*nearest
= NULL
;
177 if (!nearest
|| te
->when_sec
< nearest
->when_sec
||
178 (te
->when_sec
== nearest
->when_sec
&&
179 te
->when_ms
< nearest
->when_ms
))
186 /* Process every pending time event, then every pending file event
187 * (that may be registered by time event callbacks just processed).
188 * Without special flags the function sleeps until some file event
189 * fires, or when the next time event occurrs (if any).
191 * If flags is 0, the function does nothing and returns.
192 * if flags has AE_ALL_EVENTS set, all the kind of events are processed.
193 * if flags has AE_FILE_EVENTS set, file events are processed.
194 * if flags has AE_TIME_EVENTS set, time events are processed.
195 * if flags has AE_DONT_WAIT set the function returns ASAP until all
196 * the events that's possible to process without to wait are processed.
198 * The function returns the number of events processed. */
199 int aeProcessEvents(aeEventLoop
*eventLoop
, int flags
)
201 int maxfd
= 0, numfd
= 0, processed
= 0;
202 fd_set rfds
, wfds
, efds
;
203 aeFileEvent
*fe
= eventLoop
->fileEventHead
;
208 /* Nothing to do? return ASAP */
209 if (!(flags
& AE_TIME_EVENTS
) && !(flags
& AE_FILE_EVENTS
)) return 0;
215 /* Check file events */
216 if (flags
& AE_FILE_EVENTS
) {
218 if (fe
->mask
& AE_READABLE
) FD_SET(fe
->fd
, &rfds
);
219 if (fe
->mask
& AE_WRITABLE
) FD_SET(fe
->fd
, &wfds
);
220 if (fe
->mask
& AE_EXCEPTION
) FD_SET(fe
->fd
, &efds
);
221 if (maxfd
< fe
->fd
) maxfd
= fe
->fd
;
226 /* Note that we want call select() even if there are no
227 * file events to process as long as we want to process time
228 * events, in order to sleep until the next time event is ready
230 if (numfd
|| ((flags
& AE_TIME_EVENTS
) && !(flags
& AE_DONT_WAIT
))) {
232 aeTimeEvent
*shortest
= NULL
;
233 struct timeval tv
, *tvp
;
235 if (flags
& AE_TIME_EVENTS
&& !(flags
& AE_DONT_WAIT
))
236 shortest
= aeSearchNearestTimer(eventLoop
);
238 long now_sec
, now_ms
;
240 /* Calculate the time missing for the nearest
242 aeGetTime(&now_sec
, &now_ms
);
244 tvp
->tv_sec
= shortest
->when_sec
- now_sec
;
245 if (shortest
->when_ms
< now_ms
) {
246 tvp
->tv_usec
= ((shortest
->when_ms
+1000) - now_ms
)*1000;
249 tvp
->tv_usec
= (shortest
->when_ms
- now_ms
)*1000;
252 /* If we have to check for events but need to return
253 * ASAP because of AE_DONT_WAIT we need to se the timeout
255 if (flags
& AE_DONT_WAIT
) {
256 tv
.tv_sec
= tv
.tv_usec
= 0;
259 /* Otherwise we can block */
260 tvp
= NULL
; /* wait forever */
264 retval
= select(maxfd
+1, &rfds
, &wfds
, &efds
, tvp
);
266 fe
= eventLoop
->fileEventHead
;
268 int fd
= (int) fe
->fd
;
270 if ((fe
->mask
& AE_READABLE
&& FD_ISSET(fd
, &rfds
)) ||
271 (fe
->mask
& AE_WRITABLE
&& FD_ISSET(fd
, &wfds
)) ||
272 (fe
->mask
& AE_EXCEPTION
&& FD_ISSET(fd
, &efds
)))
276 if (fe
->mask
& AE_READABLE
&& FD_ISSET(fd
, &rfds
))
278 if (fe
->mask
& AE_WRITABLE
&& FD_ISSET(fd
, &wfds
))
280 if (fe
->mask
& AE_EXCEPTION
&& FD_ISSET(fd
, &efds
))
281 mask
|= AE_EXCEPTION
;
282 fe
->fileProc(eventLoop
, fe
->fd
, fe
->clientData
, mask
);
284 /* After an event is processed our file event list
285 * may no longer be the same, so what we do
286 * is to clear the bit for this file descriptor and
287 * restart again from the head. */
288 fe
= eventLoop
->fileEventHead
;
298 /* Check time events */
299 if (flags
& AE_TIME_EVENTS
) {
300 te
= eventLoop
->timeEventHead
;
301 maxId
= eventLoop
->timeEventNextId
-1;
303 long now_sec
, now_ms
;
306 if (te
->id
> maxId
) {
310 aeGetTime(&now_sec
, &now_ms
);
311 if (now_sec
> te
->when_sec
||
312 (now_sec
== te
->when_sec
&& now_ms
>= te
->when_ms
))
317 retval
= te
->timeProc(eventLoop
, id
, te
->clientData
);
318 /* After an event is processed our time event list may
319 * no longer be the same, so we restart from head.
320 * Still we make sure to don't process events registered
321 * by event handlers itself in order to don't loop forever.
322 * To do so we saved the max ID we want to handle. */
323 if (retval
!= AE_NOMORE
) {
324 aeAddMillisecondsToNow(retval
,&te
->when_sec
,&te
->when_ms
);
326 aeDeleteTimeEvent(eventLoop
, id
);
328 te
= eventLoop
->timeEventHead
;
334 return processed
; /* return the number of processed file/time events */
337 /* Wait for millseconds until the given file descriptor becomes
338 * writable/readable/exception */
339 int aeWait(int fd
, int mask
, long long milliseconds
) {
341 fd_set rfds
, wfds
, efds
;
342 int retmask
= 0, retval
;
344 tv
.tv_sec
= milliseconds
/1000;
345 tv
.tv_usec
= (milliseconds%1000
)*1000;
350 if (mask
& AE_READABLE
) FD_SET(fd
,&rfds
);
351 if (mask
& AE_WRITABLE
) FD_SET(fd
,&wfds
);
352 if (mask
& AE_EXCEPTION
) FD_SET(fd
,&efds
);
353 if ((retval
= select(fd
+1, &rfds
, &wfds
, &efds
, &tv
)) > 0) {
354 if (FD_ISSET(fd
,&rfds
)) retmask
|= AE_READABLE
;
355 if (FD_ISSET(fd
,&wfds
)) retmask
|= AE_WRITABLE
;
356 if (FD_ISSET(fd
,&efds
)) retmask
|= AE_EXCEPTION
;
363 void aeMain(aeEventLoop
*eventLoop
)
366 while (!eventLoop
->stop
)
367 aeProcessEvents(eventLoop
, AE_ALL_EVENTS
);