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