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