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