]>
Commit | Line | Data |
---|---|---|
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 | #include <stdio.h> | |
34 | #include <sys/time.h> | |
35 | #include <sys/types.h> | |
36 | #include <unistd.h> | |
37 | #include <stdlib.h> | |
3e1e1ac2 | 38 | #include <poll.h> |
ac834d23 | 39 | #include <string.h> |
ed9b544e | 40 | |
41 | #include "ae.h" | |
42 | #include "zmalloc.h" | |
266373b2 | 43 | #include "config.h" |
44 | ||
45 | /* Include the best multiplexing layer supported by this system. | |
46 | * The following should be ordered by performances, descending. */ | |
05da63da DP |
47 | #ifdef HAVE_EVPORT |
48 | #include "ae_evport.c" | |
266373b2 | 49 | #else |
05da63da DP |
50 | #ifdef HAVE_EPOLL |
51 | #include "ae_epoll.c" | |
f3053eb0 | 52 | #else |
05da63da DP |
53 | #ifdef HAVE_KQUEUE |
54 | #include "ae_kqueue.c" | |
55 | #else | |
56 | #include "ae_select.c" | |
57 | #endif | |
f3053eb0 | 58 | #endif |
266373b2 | 59 | #endif |
ed9b544e | 60 | |
e074416b | 61 | aeEventLoop *aeCreateEventLoop(int setsize) { |
ed9b544e | 62 | aeEventLoop *eventLoop; |
266373b2 | 63 | int i; |
ed9b544e | 64 | |
ecc57021 | 65 | if ((eventLoop = zmalloc(sizeof(*eventLoop))) == NULL) goto err; |
e074416b | 66 | eventLoop->events = zmalloc(sizeof(aeFileEvent)*setsize); |
67 | eventLoop->fired = zmalloc(sizeof(aeFiredEvent)*setsize); | |
ecc57021 | 68 | if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err; |
e074416b | 69 | eventLoop->setsize = setsize; |
b7b2a1cc | 70 | eventLoop->lastTime = time(NULL); |
ed9b544e | 71 | eventLoop->timeEventHead = NULL; |
72 | eventLoop->timeEventNextId = 0; | |
73 | eventLoop->stop = 0; | |
266373b2 | 74 | eventLoop->maxfd = -1; |
d5d55fc3 | 75 | eventLoop->beforesleep = NULL; |
ecc57021 | 76 | if (aeApiCreate(eventLoop) == -1) goto err; |
266373b2 | 77 | /* Events with mask == AE_NONE are not set. So let's initialize the |
78 | * vector with it. */ | |
e074416b | 79 | for (i = 0; i < setsize; i++) |
266373b2 | 80 | eventLoop->events[i].mask = AE_NONE; |
ed9b544e | 81 | return eventLoop; |
ecc57021 | 82 | |
83 | err: | |
84 | if (eventLoop) { | |
85 | zfree(eventLoop->events); | |
86 | zfree(eventLoop->fired); | |
87 | zfree(eventLoop); | |
88 | } | |
89 | return NULL; | |
ed9b544e | 90 | } |
91 | ||
92 | void aeDeleteEventLoop(aeEventLoop *eventLoop) { | |
266373b2 | 93 | aeApiFree(eventLoop); |
18d0ef4b | 94 | zfree(eventLoop->events); |
95 | zfree(eventLoop->fired); | |
ed9b544e | 96 | zfree(eventLoop); |
97 | } | |
98 | ||
99 | void aeStop(aeEventLoop *eventLoop) { | |
100 | eventLoop->stop = 1; | |
101 | } | |
102 | ||
103 | int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask, | |
266373b2 | 104 | aeFileProc *proc, void *clientData) |
ed9b544e | 105 | { |
e074416b | 106 | if (fd >= eventLoop->setsize) return AE_ERR; |
266373b2 | 107 | aeFileEvent *fe = &eventLoop->events[fd]; |
108 | ||
109 | if (aeApiAddEvent(eventLoop, fd, mask) == -1) | |
110 | return AE_ERR; | |
111 | fe->mask |= mask; | |
112 | if (mask & AE_READABLE) fe->rfileProc = proc; | |
113 | if (mask & AE_WRITABLE) fe->wfileProc = proc; | |
ed9b544e | 114 | fe->clientData = clientData; |
266373b2 | 115 | if (fd > eventLoop->maxfd) |
116 | eventLoop->maxfd = fd; | |
ed9b544e | 117 | return AE_OK; |
118 | } | |
119 | ||
120 | void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask) | |
121 | { | |
e074416b | 122 | if (fd >= eventLoop->setsize) return; |
266373b2 | 123 | aeFileEvent *fe = &eventLoop->events[fd]; |
124 | ||
125 | if (fe->mask == AE_NONE) return; | |
126 | fe->mask = fe->mask & (~mask); | |
127 | if (fd == eventLoop->maxfd && fe->mask == AE_NONE) { | |
128 | /* Update the max fd */ | |
129 | int j; | |
130 | ||
131 | for (j = eventLoop->maxfd-1; j >= 0; j--) | |
132 | if (eventLoop->events[j].mask != AE_NONE) break; | |
133 | eventLoop->maxfd = j; | |
ed9b544e | 134 | } |
266373b2 | 135 | aeApiDelEvent(eventLoop, fd, mask); |
ed9b544e | 136 | } |
137 | ||
f14479c7 | 138 | int aeGetFileEvents(aeEventLoop *eventLoop, int fd) { |
e074416b | 139 | if (fd >= eventLoop->setsize) return 0; |
f14479c7 | 140 | aeFileEvent *fe = &eventLoop->events[fd]; |
141 | ||
142 | return fe->mask; | |
143 | } | |
144 | ||
ed9b544e | 145 | static void aeGetTime(long *seconds, long *milliseconds) |
146 | { | |
147 | struct timeval tv; | |
148 | ||
149 | gettimeofday(&tv, NULL); | |
150 | *seconds = tv.tv_sec; | |
151 | *milliseconds = tv.tv_usec/1000; | |
152 | } | |
153 | ||
154 | static void aeAddMillisecondsToNow(long long milliseconds, long *sec, long *ms) { | |
155 | long cur_sec, cur_ms, when_sec, when_ms; | |
156 | ||
157 | aeGetTime(&cur_sec, &cur_ms); | |
158 | when_sec = cur_sec + milliseconds/1000; | |
159 | when_ms = cur_ms + milliseconds%1000; | |
160 | if (when_ms >= 1000) { | |
161 | when_sec ++; | |
162 | when_ms -= 1000; | |
163 | } | |
164 | *sec = when_sec; | |
165 | *ms = when_ms; | |
166 | } | |
167 | ||
168 | long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds, | |
169 | aeTimeProc *proc, void *clientData, | |
170 | aeEventFinalizerProc *finalizerProc) | |
171 | { | |
172 | long long id = eventLoop->timeEventNextId++; | |
173 | aeTimeEvent *te; | |
174 | ||
175 | te = zmalloc(sizeof(*te)); | |
176 | if (te == NULL) return AE_ERR; | |
177 | te->id = id; | |
178 | aeAddMillisecondsToNow(milliseconds,&te->when_sec,&te->when_ms); | |
179 | te->timeProc = proc; | |
180 | te->finalizerProc = finalizerProc; | |
181 | te->clientData = clientData; | |
182 | te->next = eventLoop->timeEventHead; | |
183 | eventLoop->timeEventHead = te; | |
184 | return id; | |
185 | } | |
186 | ||
187 | int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id) | |
188 | { | |
189 | aeTimeEvent *te, *prev = NULL; | |
190 | ||
191 | te = eventLoop->timeEventHead; | |
192 | while(te) { | |
193 | if (te->id == id) { | |
194 | if (prev == NULL) | |
195 | eventLoop->timeEventHead = te->next; | |
196 | else | |
197 | prev->next = te->next; | |
198 | if (te->finalizerProc) | |
199 | te->finalizerProc(eventLoop, te->clientData); | |
200 | zfree(te); | |
201 | return AE_OK; | |
202 | } | |
203 | prev = te; | |
204 | te = te->next; | |
205 | } | |
206 | return AE_ERR; /* NO event with the specified ID found */ | |
207 | } | |
208 | ||
209 | /* Search the first timer to fire. | |
210 | * This operation is useful to know how many time the select can be | |
211 | * put in sleep without to delay any event. | |
212 | * If there are no timers NULL is returned. | |
213 | * | |
5b2a1c29 | 214 | * Note that's O(N) since time events are unsorted. |
215 | * Possible optimizations (not needed by Redis so far, but...): | |
216 | * 1) Insert the event in order, so that the nearest is just the head. | |
217 | * Much better but still insertion or deletion of timers is O(N). | |
218 | * 2) Use a skiplist to have this operation as O(1) and insertion as O(log(N)). | |
219 | */ | |
ed9b544e | 220 | static aeTimeEvent *aeSearchNearestTimer(aeEventLoop *eventLoop) |
221 | { | |
222 | aeTimeEvent *te = eventLoop->timeEventHead; | |
223 | aeTimeEvent *nearest = NULL; | |
224 | ||
225 | while(te) { | |
226 | if (!nearest || te->when_sec < nearest->when_sec || | |
227 | (te->when_sec == nearest->when_sec && | |
228 | te->when_ms < nearest->when_ms)) | |
229 | nearest = te; | |
230 | te = te->next; | |
231 | } | |
232 | return nearest; | |
233 | } | |
234 | ||
5b2a1c29 | 235 | /* Process time events */ |
236 | static int processTimeEvents(aeEventLoop *eventLoop) { | |
237 | int processed = 0; | |
238 | aeTimeEvent *te; | |
239 | long long maxId; | |
b7b2a1cc J |
240 | time_t now = time(NULL); |
241 | ||
242 | /* If the system clock is moved to the future, and then set back to the | |
243 | * right value, time events may be delayed in a random way. Often this | |
244 | * means that scheduled operations will not be performed soon enough. | |
245 | * | |
246 | * Here we try to detect system clock skews, and force all the time | |
247 | * events to be processed ASAP when this happens: the idea is that | |
248 | * processing events earlier is less dangerous than delaying them | |
249 | * indefinitely, and practice suggests it is. */ | |
250 | if (now < eventLoop->lastTime) { | |
251 | te = eventLoop->timeEventHead; | |
252 | while(te) { | |
253 | te->when_sec = 0; | |
254 | te = te->next; | |
255 | } | |
256 | } | |
257 | eventLoop->lastTime = now; | |
5b2a1c29 | 258 | |
259 | te = eventLoop->timeEventHead; | |
260 | maxId = eventLoop->timeEventNextId-1; | |
261 | while(te) { | |
262 | long now_sec, now_ms; | |
263 | long long id; | |
264 | ||
265 | if (te->id > maxId) { | |
266 | te = te->next; | |
267 | continue; | |
268 | } | |
269 | aeGetTime(&now_sec, &now_ms); | |
270 | if (now_sec > te->when_sec || | |
271 | (now_sec == te->when_sec && now_ms >= te->when_ms)) | |
272 | { | |
273 | int retval; | |
274 | ||
275 | id = te->id; | |
276 | retval = te->timeProc(eventLoop, id, te->clientData); | |
277 | processed++; | |
278 | /* After an event is processed our time event list may | |
279 | * no longer be the same, so we restart from head. | |
280 | * Still we make sure to don't process events registered | |
281 | * by event handlers itself in order to don't loop forever. | |
282 | * To do so we saved the max ID we want to handle. | |
283 | * | |
284 | * FUTURE OPTIMIZATIONS: | |
285 | * Note that this is NOT great algorithmically. Redis uses | |
286 | * a single time event so it's not a problem but the right | |
287 | * way to do this is to add the new elements on head, and | |
288 | * to flag deleted elements in a special way for later | |
289 | * deletion (putting references to the nodes to delete into | |
290 | * another linked list). */ | |
291 | if (retval != AE_NOMORE) { | |
292 | aeAddMillisecondsToNow(retval,&te->when_sec,&te->when_ms); | |
293 | } else { | |
294 | aeDeleteTimeEvent(eventLoop, id); | |
295 | } | |
296 | te = eventLoop->timeEventHead; | |
297 | } else { | |
298 | te = te->next; | |
299 | } | |
300 | } | |
301 | return processed; | |
302 | } | |
303 | ||
ed9b544e | 304 | /* Process every pending time event, then every pending file event |
305 | * (that may be registered by time event callbacks just processed). | |
306 | * Without special flags the function sleeps until some file event | |
307 | * fires, or when the next time event occurrs (if any). | |
308 | * | |
309 | * If flags is 0, the function does nothing and returns. | |
310 | * if flags has AE_ALL_EVENTS set, all the kind of events are processed. | |
311 | * if flags has AE_FILE_EVENTS set, file events are processed. | |
312 | * if flags has AE_TIME_EVENTS set, time events are processed. | |
313 | * if flags has AE_DONT_WAIT set the function returns ASAP until all | |
314 | * the events that's possible to process without to wait are processed. | |
315 | * | |
316 | * The function returns the number of events processed. */ | |
317 | int aeProcessEvents(aeEventLoop *eventLoop, int flags) | |
318 | { | |
266373b2 | 319 | int processed = 0, numevents; |
ed9b544e | 320 | |
321 | /* Nothing to do? return ASAP */ | |
322 | if (!(flags & AE_TIME_EVENTS) && !(flags & AE_FILE_EVENTS)) return 0; | |
323 | ||
ed9b544e | 324 | /* Note that we want call select() even if there are no |
325 | * file events to process as long as we want to process time | |
326 | * events, in order to sleep until the next time event is ready | |
327 | * to fire. */ | |
266373b2 | 328 | if (eventLoop->maxfd != -1 || |
329 | ((flags & AE_TIME_EVENTS) && !(flags & AE_DONT_WAIT))) { | |
330 | int j; | |
ed9b544e | 331 | aeTimeEvent *shortest = NULL; |
332 | struct timeval tv, *tvp; | |
333 | ||
334 | if (flags & AE_TIME_EVENTS && !(flags & AE_DONT_WAIT)) | |
335 | shortest = aeSearchNearestTimer(eventLoop); | |
336 | if (shortest) { | |
337 | long now_sec, now_ms; | |
338 | ||
339 | /* Calculate the time missing for the nearest | |
340 | * timer to fire. */ | |
341 | aeGetTime(&now_sec, &now_ms); | |
342 | tvp = &tv; | |
343 | tvp->tv_sec = shortest->when_sec - now_sec; | |
344 | if (shortest->when_ms < now_ms) { | |
345 | tvp->tv_usec = ((shortest->when_ms+1000) - now_ms)*1000; | |
346 | tvp->tv_sec --; | |
347 | } else { | |
348 | tvp->tv_usec = (shortest->when_ms - now_ms)*1000; | |
349 | } | |
266373b2 | 350 | if (tvp->tv_sec < 0) tvp->tv_sec = 0; |
351 | if (tvp->tv_usec < 0) tvp->tv_usec = 0; | |
ed9b544e | 352 | } else { |
353 | /* If we have to check for events but need to return | |
354 | * ASAP because of AE_DONT_WAIT we need to se the timeout | |
355 | * to zero */ | |
356 | if (flags & AE_DONT_WAIT) { | |
357 | tv.tv_sec = tv.tv_usec = 0; | |
358 | tvp = &tv; | |
359 | } else { | |
360 | /* Otherwise we can block */ | |
361 | tvp = NULL; /* wait forever */ | |
362 | } | |
363 | } | |
364 | ||
266373b2 | 365 | numevents = aeApiPoll(eventLoop, tvp); |
366 | for (j = 0; j < numevents; j++) { | |
367 | aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd]; | |
368 | int mask = eventLoop->fired[j].mask; | |
369 | int fd = eventLoop->fired[j].fd; | |
621d5c19 | 370 | int rfired = 0; |
266373b2 | 371 | |
372 | /* note the fe->mask & mask & ... code: maybe an already processed | |
373 | * event removed an element that fired and we still didn't | |
374 | * processed, so we check if the event is still valid. */ | |
621d5c19 | 375 | if (fe->mask & mask & AE_READABLE) { |
376 | rfired = 1; | |
266373b2 | 377 | fe->rfileProc(eventLoop,fd,fe->clientData,mask); |
621d5c19 | 378 | } |
379 | if (fe->mask & mask & AE_WRITABLE) { | |
380 | if (!rfired || fe->wfileProc != fe->rfileProc) | |
381 | fe->wfileProc(eventLoop,fd,fe->clientData,mask); | |
382 | } | |
266373b2 | 383 | processed++; |
ed9b544e | 384 | } |
385 | } | |
386 | /* Check time events */ | |
5b2a1c29 | 387 | if (flags & AE_TIME_EVENTS) |
388 | processed += processTimeEvents(eventLoop); | |
ed9b544e | 389 | |
ed9b544e | 390 | return processed; /* return the number of processed file/time events */ |
391 | } | |
392 | ||
393 | /* Wait for millseconds until the given file descriptor becomes | |
394 | * writable/readable/exception */ | |
395 | int aeWait(int fd, int mask, long long milliseconds) { | |
3e1e1ac2 | 396 | struct pollfd pfd; |
ed9b544e | 397 | int retmask = 0, retval; |
398 | ||
3e1e1ac2 | 399 | memset(&pfd, 0, sizeof(pfd)); |
400 | pfd.fd = fd; | |
401 | if (mask & AE_READABLE) pfd.events |= POLLIN; | |
402 | if (mask & AE_WRITABLE) pfd.events |= POLLOUT; | |
403 | ||
404 | if ((retval = poll(&pfd, 1, milliseconds))== 1) { | |
405 | if (pfd.revents & POLLIN) retmask |= AE_READABLE; | |
406 | if (pfd.revents & POLLOUT) retmask |= AE_WRITABLE; | |
e150ce3c | 407 | if (pfd.revents & POLLERR) retmask |= AE_WRITABLE; |
408 | if (pfd.revents & POLLHUP) retmask |= AE_WRITABLE; | |
ed9b544e | 409 | return retmask; |
410 | } else { | |
411 | return retval; | |
412 | } | |
413 | } | |
414 | ||
7a932b74 | 415 | void aeMain(aeEventLoop *eventLoop) { |
ed9b544e | 416 | eventLoop->stop = 0; |
d5d55fc3 | 417 | while (!eventLoop->stop) { |
418 | if (eventLoop->beforesleep != NULL) | |
419 | eventLoop->beforesleep(eventLoop); | |
ed9b544e | 420 | aeProcessEvents(eventLoop, AE_ALL_EVENTS); |
d5d55fc3 | 421 | } |
ed9b544e | 422 | } |
7a932b74 | 423 | |
424 | char *aeGetApiName(void) { | |
425 | return aeApiName(); | |
426 | } | |
d5d55fc3 | 427 | |
428 | void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep) { | |
429 | eventLoop->beforesleep = beforesleep; | |
430 | } |