]>
git.saurik.com Git - redis.git/blob - src/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-2010, 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>
43 /* Include the best multiplexing layer supported by this system.
44 * The following should be ordered by performances, descending. */
49 #include "ae_kqueue.c"
51 #include "ae_select.c"
55 aeEventLoop
*aeCreateEventLoop(int setsize
) {
56 aeEventLoop
*eventLoop
;
59 if ((eventLoop
= zmalloc(sizeof(*eventLoop
))) == NULL
) return NULL
;
60 eventLoop
->events
= NULL
;
61 eventLoop
->fired
= NULL
;
62 eventLoop
->events
= zmalloc(sizeof(aeFileEvent
)*setsize
);
63 eventLoop
->fired
= zmalloc(sizeof(aeFiredEvent
)*setsize
);
64 if (eventLoop
->events
== NULL
|| eventLoop
->fired
== NULL
) {
65 zfree(eventLoop
->events
);
66 zfree(eventLoop
->fired
);
70 eventLoop
->setsize
= setsize
;
71 eventLoop
->timeEventHead
= NULL
;
72 eventLoop
->timeEventNextId
= 0;
74 eventLoop
->maxfd
= -1;
75 eventLoop
->beforesleep
= NULL
;
76 if (aeApiCreate(eventLoop
) == -1) {
80 /* Events with mask == AE_NONE are not set. So let's initialize the
82 for (i
= 0; i
< setsize
; i
++)
83 eventLoop
->events
[i
].mask
= AE_NONE
;
87 void aeDeleteEventLoop(aeEventLoop
*eventLoop
) {
89 zfree(eventLoop
->events
);
90 zfree(eventLoop
->fired
);
94 void aeStop(aeEventLoop
*eventLoop
) {
98 int aeCreateFileEvent(aeEventLoop
*eventLoop
, int fd
, int mask
,
99 aeFileProc
*proc
, void *clientData
)
101 if (fd
>= eventLoop
->setsize
) return AE_ERR
;
102 aeFileEvent
*fe
= &eventLoop
->events
[fd
];
104 if (aeApiAddEvent(eventLoop
, fd
, mask
) == -1)
107 if (mask
& AE_READABLE
) fe
->rfileProc
= proc
;
108 if (mask
& AE_WRITABLE
) fe
->wfileProc
= proc
;
109 fe
->clientData
= clientData
;
110 if (fd
> eventLoop
->maxfd
)
111 eventLoop
->maxfd
= fd
;
115 void aeDeleteFileEvent(aeEventLoop
*eventLoop
, int fd
, int mask
)
117 if (fd
>= eventLoop
->setsize
) return;
118 aeFileEvent
*fe
= &eventLoop
->events
[fd
];
120 if (fe
->mask
== AE_NONE
) return;
121 fe
->mask
= fe
->mask
& (~mask
);
122 if (fd
== eventLoop
->maxfd
&& fe
->mask
== AE_NONE
) {
123 /* Update the max fd */
126 for (j
= eventLoop
->maxfd
-1; j
>= 0; j
--)
127 if (eventLoop
->events
[j
].mask
!= AE_NONE
) break;
128 eventLoop
->maxfd
= j
;
130 aeApiDelEvent(eventLoop
, fd
, mask
);
133 int aeGetFileEvents(aeEventLoop
*eventLoop
, int fd
) {
134 if (fd
>= eventLoop
->setsize
) return 0;
135 aeFileEvent
*fe
= &eventLoop
->events
[fd
];
140 static void aeGetTime(long *seconds
, long *milliseconds
)
144 gettimeofday(&tv
, NULL
);
145 *seconds
= tv
.tv_sec
;
146 *milliseconds
= tv
.tv_usec
/1000;
149 static void aeAddMillisecondsToNow(long long milliseconds
, long *sec
, long *ms
) {
150 long cur_sec
, cur_ms
, when_sec
, when_ms
;
152 aeGetTime(&cur_sec
, &cur_ms
);
153 when_sec
= cur_sec
+ milliseconds
/1000;
154 when_ms
= cur_ms
+ milliseconds%1000
;
155 if (when_ms
>= 1000) {
163 long long aeCreateTimeEvent(aeEventLoop
*eventLoop
, long long milliseconds
,
164 aeTimeProc
*proc
, void *clientData
,
165 aeEventFinalizerProc
*finalizerProc
)
167 long long id
= eventLoop
->timeEventNextId
++;
170 te
= zmalloc(sizeof(*te
));
171 if (te
== NULL
) return AE_ERR
;
173 aeAddMillisecondsToNow(milliseconds
,&te
->when_sec
,&te
->when_ms
);
175 te
->finalizerProc
= finalizerProc
;
176 te
->clientData
= clientData
;
177 te
->next
= eventLoop
->timeEventHead
;
178 eventLoop
->timeEventHead
= te
;
182 int aeDeleteTimeEvent(aeEventLoop
*eventLoop
, long long id
)
184 aeTimeEvent
*te
, *prev
= NULL
;
186 te
= eventLoop
->timeEventHead
;
190 eventLoop
->timeEventHead
= te
->next
;
192 prev
->next
= te
->next
;
193 if (te
->finalizerProc
)
194 te
->finalizerProc(eventLoop
, te
->clientData
);
201 return AE_ERR
; /* NO event with the specified ID found */
204 /* Search the first timer to fire.
205 * This operation is useful to know how many time the select can be
206 * put in sleep without to delay any event.
207 * If there are no timers NULL is returned.
209 * Note that's O(N) since time events are unsorted.
210 * Possible optimizations (not needed by Redis so far, but...):
211 * 1) Insert the event in order, so that the nearest is just the head.
212 * Much better but still insertion or deletion of timers is O(N).
213 * 2) Use a skiplist to have this operation as O(1) and insertion as O(log(N)).
215 static aeTimeEvent
*aeSearchNearestTimer(aeEventLoop
*eventLoop
)
217 aeTimeEvent
*te
= eventLoop
->timeEventHead
;
218 aeTimeEvent
*nearest
= NULL
;
221 if (!nearest
|| te
->when_sec
< nearest
->when_sec
||
222 (te
->when_sec
== nearest
->when_sec
&&
223 te
->when_ms
< nearest
->when_ms
))
230 /* Process time events */
231 static int processTimeEvents(aeEventLoop
*eventLoop
) {
236 te
= eventLoop
->timeEventHead
;
237 maxId
= eventLoop
->timeEventNextId
-1;
239 long now_sec
, now_ms
;
242 if (te
->id
> maxId
) {
246 aeGetTime(&now_sec
, &now_ms
);
247 if (now_sec
> te
->when_sec
||
248 (now_sec
== te
->when_sec
&& now_ms
>= te
->when_ms
))
253 retval
= te
->timeProc(eventLoop
, id
, te
->clientData
);
255 /* After an event is processed our time event list may
256 * no longer be the same, so we restart from head.
257 * Still we make sure to don't process events registered
258 * by event handlers itself in order to don't loop forever.
259 * To do so we saved the max ID we want to handle.
261 * FUTURE OPTIMIZATIONS:
262 * Note that this is NOT great algorithmically. Redis uses
263 * a single time event so it's not a problem but the right
264 * way to do this is to add the new elements on head, and
265 * to flag deleted elements in a special way for later
266 * deletion (putting references to the nodes to delete into
267 * another linked list). */
268 if (retval
!= AE_NOMORE
) {
269 aeAddMillisecondsToNow(retval
,&te
->when_sec
,&te
->when_ms
);
271 aeDeleteTimeEvent(eventLoop
, id
);
273 te
= eventLoop
->timeEventHead
;
281 /* Process every pending time event, then every pending file event
282 * (that may be registered by time event callbacks just processed).
283 * Without special flags the function sleeps until some file event
284 * fires, or when the next time event occurrs (if any).
286 * If flags is 0, the function does nothing and returns.
287 * if flags has AE_ALL_EVENTS set, all the kind of events are processed.
288 * if flags has AE_FILE_EVENTS set, file events are processed.
289 * if flags has AE_TIME_EVENTS set, time events are processed.
290 * if flags has AE_DONT_WAIT set the function returns ASAP until all
291 * the events that's possible to process without to wait are processed.
293 * The function returns the number of events processed. */
294 int aeProcessEvents(aeEventLoop
*eventLoop
, int flags
)
296 int processed
= 0, numevents
;
298 /* Nothing to do? return ASAP */
299 if (!(flags
& AE_TIME_EVENTS
) && !(flags
& AE_FILE_EVENTS
)) return 0;
301 /* Note that we want call select() even if there are no
302 * file events to process as long as we want to process time
303 * events, in order to sleep until the next time event is ready
305 if (eventLoop
->maxfd
!= -1 ||
306 ((flags
& AE_TIME_EVENTS
) && !(flags
& AE_DONT_WAIT
))) {
308 aeTimeEvent
*shortest
= NULL
;
309 struct timeval tv
, *tvp
;
311 if (flags
& AE_TIME_EVENTS
&& !(flags
& AE_DONT_WAIT
))
312 shortest
= aeSearchNearestTimer(eventLoop
);
314 long now_sec
, now_ms
;
316 /* Calculate the time missing for the nearest
318 aeGetTime(&now_sec
, &now_ms
);
320 tvp
->tv_sec
= shortest
->when_sec
- now_sec
;
321 if (shortest
->when_ms
< now_ms
) {
322 tvp
->tv_usec
= ((shortest
->when_ms
+1000) - now_ms
)*1000;
325 tvp
->tv_usec
= (shortest
->when_ms
- now_ms
)*1000;
327 if (tvp
->tv_sec
< 0) tvp
->tv_sec
= 0;
328 if (tvp
->tv_usec
< 0) tvp
->tv_usec
= 0;
330 /* If we have to check for events but need to return
331 * ASAP because of AE_DONT_WAIT we need to se the timeout
333 if (flags
& AE_DONT_WAIT
) {
334 tv
.tv_sec
= tv
.tv_usec
= 0;
337 /* Otherwise we can block */
338 tvp
= NULL
; /* wait forever */
342 numevents
= aeApiPoll(eventLoop
, tvp
);
343 for (j
= 0; j
< numevents
; j
++) {
344 aeFileEvent
*fe
= &eventLoop
->events
[eventLoop
->fired
[j
].fd
];
345 int mask
= eventLoop
->fired
[j
].mask
;
346 int fd
= eventLoop
->fired
[j
].fd
;
349 /* note the fe->mask & mask & ... code: maybe an already processed
350 * event removed an element that fired and we still didn't
351 * processed, so we check if the event is still valid. */
352 if (fe
->mask
& mask
& AE_READABLE
) {
354 fe
->rfileProc(eventLoop
,fd
,fe
->clientData
,mask
);
356 if (fe
->mask
& mask
& AE_WRITABLE
) {
357 if (!rfired
|| fe
->wfileProc
!= fe
->rfileProc
)
358 fe
->wfileProc(eventLoop
,fd
,fe
->clientData
,mask
);
363 /* Check time events */
364 if (flags
& AE_TIME_EVENTS
)
365 processed
+= processTimeEvents(eventLoop
);
367 return processed
; /* return the number of processed file/time events */
370 /* Wait for millseconds until the given file descriptor becomes
371 * writable/readable/exception */
372 int aeWait(int fd
, int mask
, long long milliseconds
) {
374 fd_set rfds
, wfds
, efds
;
375 int retmask
= 0, retval
;
377 tv
.tv_sec
= milliseconds
/1000;
378 tv
.tv_usec
= (milliseconds%1000
)*1000;
383 if (mask
& AE_READABLE
) FD_SET(fd
,&rfds
);
384 if (mask
& AE_WRITABLE
) FD_SET(fd
,&wfds
);
385 if ((retval
= select(fd
+1, &rfds
, &wfds
, &efds
, &tv
)) > 0) {
386 if (FD_ISSET(fd
,&rfds
)) retmask
|= AE_READABLE
;
387 if (FD_ISSET(fd
,&wfds
)) retmask
|= AE_WRITABLE
;
394 void aeMain(aeEventLoop
*eventLoop
) {
396 while (!eventLoop
->stop
) {
397 if (eventLoop
->beforesleep
!= NULL
)
398 eventLoop
->beforesleep(eventLoop
);
399 aeProcessEvents(eventLoop
, AE_ALL_EVENTS
);
403 char *aeGetApiName(void) {
407 void aeSetBeforeSleepProc(aeEventLoop
*eventLoop
, aeBeforeSleepProc
*beforesleep
) {
408 eventLoop
->beforesleep
= beforesleep
;