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