Back to home page

Redis cross reference

 
 

    


0001 /* A simple event-driven programming library. Originally I wrote this code
0002  * for the Jim's event-loop (Jim is a Tcl interpreter) but later translated
0003  * it in form of a library for easy reuse.
0004  *
0005  * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
0006  * All rights reserved.
0007  *
0008  * Redistribution and use in source and binary forms, with or without
0009  * modification, are permitted provided that the following conditions are met:
0010  *
0011  *   * Redistributions of source code must retain the above copyright notice,
0012  *     this list of conditions and the following disclaimer.
0013  *   * Redistributions in binary form must reproduce the above copyright
0014  *     notice, this list of conditions and the following disclaimer in the
0015  *     documentation and/or other materials provided with the distribution.
0016  *   * Neither the name of Redis nor the names of its contributors may be used
0017  *     to endorse or promote products derived from this software without
0018  *     specific prior written permission.
0019  *
0020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0021  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0022  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0023  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0024  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0025  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0026  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0029  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0030  * POSSIBILITY OF SUCH DAMAGE.
0031  */
0032 
0033 #include <stdio.h>
0034 #include <sys/time.h>
0035 #include <sys/types.h>
0036 #include <unistd.h>
0037 #include <stdlib.h>
0038 #include <poll.h>
0039 #include <string.h>
0040 #include <time.h>
0041 #include <errno.h>
0042 
0043 #include "ae.h"
0044 #include "zmalloc.h"
0045 #include "config.h"
0046 
0047 /* Include the best multiplexing layer supported by this system.
0048  * The following should be ordered by performances, descending. */
0049 #ifdef HAVE_EVPORT
0050 #include "ae_evport.c"
0051 #else
0052     #ifdef HAVE_EPOLL
0053     #include "ae_epoll.c"
0054     #else
0055         #ifdef HAVE_KQUEUE
0056         #include "ae_kqueue.c"
0057         #else
0058         #include "ae_select.c"
0059         #endif
0060     #endif
0061 #endif
0062 
0063 aeEventLoop *aeCreateEventLoop(int setsize) {
0064     aeEventLoop *eventLoop;
0065     int i;
0066 
0067     if ((eventLoop = zmalloc(sizeof(*eventLoop))) == NULL) goto err;
0068     eventLoop->events = zmalloc(sizeof(aeFileEvent)*setsize);
0069     eventLoop->fired = zmalloc(sizeof(aeFiredEvent)*setsize);
0070     if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err;
0071     eventLoop->setsize = setsize;
0072     eventLoop->lastTime = time(NULL);
0073     eventLoop->timeEventHead = NULL;
0074     eventLoop->timeEventNextId = 0;
0075     eventLoop->stop = 0;
0076     eventLoop->maxfd = -1;
0077     eventLoop->beforesleep = NULL;
0078     if (aeApiCreate(eventLoop) == -1) goto err;
0079     /* Events with mask == AE_NONE are not set. So let's initialize the
0080      * vector with it. */
0081     for (i = 0; i < setsize; i++)
0082         eventLoop->events[i].mask = AE_NONE;
0083     return eventLoop;
0084 
0085 err:
0086     if (eventLoop) {
0087         zfree(eventLoop->events);
0088         zfree(eventLoop->fired);
0089         zfree(eventLoop);
0090     }
0091     return NULL;
0092 }
0093 
0094 void aeDeleteEventLoop(aeEventLoop *eventLoop) {
0095     aeApiFree(eventLoop);
0096     zfree(eventLoop->events);
0097     zfree(eventLoop->fired);
0098     zfree(eventLoop);
0099 }
0100 
0101 void aeStop(aeEventLoop *eventLoop) {
0102     eventLoop->stop = 1;
0103 }
0104 
0105 int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
0106         aeFileProc *proc, void *clientData)
0107 {
0108     if (fd >= eventLoop->setsize) {
0109         errno = ERANGE;
0110         return AE_ERR;
0111     }
0112     aeFileEvent *fe = &eventLoop->events[fd];
0113 
0114     if (aeApiAddEvent(eventLoop, fd, mask) == -1)
0115         return AE_ERR;
0116     fe->mask |= mask;
0117     if (mask & AE_READABLE) fe->rfileProc = proc;
0118     if (mask & AE_WRITABLE) fe->wfileProc = proc;
0119     fe->clientData = clientData;
0120     if (fd > eventLoop->maxfd)
0121         eventLoop->maxfd = fd;
0122     return AE_OK;
0123 }
0124 
0125 void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask)
0126 {
0127     if (fd >= eventLoop->setsize) return;
0128     aeFileEvent *fe = &eventLoop->events[fd];
0129 
0130     if (fe->mask == AE_NONE) return;
0131     fe->mask = fe->mask & (~mask);
0132     if (fd == eventLoop->maxfd && fe->mask == AE_NONE) {
0133         /* Update the max fd */
0134         int j;
0135 
0136         for (j = eventLoop->maxfd-1; j >= 0; j--)
0137             if (eventLoop->events[j].mask != AE_NONE) break;
0138         eventLoop->maxfd = j;
0139     }
0140     aeApiDelEvent(eventLoop, fd, mask);
0141 }
0142 
0143 int aeGetFileEvents(aeEventLoop *eventLoop, int fd) {
0144     if (fd >= eventLoop->setsize) return 0;
0145     aeFileEvent *fe = &eventLoop->events[fd];
0146 
0147     return fe->mask;
0148 }
0149 
0150 static void aeGetTime(long *seconds, long *milliseconds)
0151 {
0152     struct timeval tv;
0153 
0154     gettimeofday(&tv, NULL);
0155     *seconds = tv.tv_sec;
0156     *milliseconds = tv.tv_usec/1000;
0157 }
0158 
0159 static void aeAddMillisecondsToNow(long long milliseconds, long *sec, long *ms) {
0160     long cur_sec, cur_ms, when_sec, when_ms;
0161 
0162     aeGetTime(&cur_sec, &cur_ms);
0163     when_sec = cur_sec + milliseconds/1000;
0164     when_ms = cur_ms + milliseconds%1000;
0165     if (when_ms >= 1000) {
0166         when_sec ++;
0167         when_ms -= 1000;
0168     }
0169     *sec = when_sec;
0170     *ms = when_ms;
0171 }
0172 
0173 long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,
0174         aeTimeProc *proc, void *clientData,
0175         aeEventFinalizerProc *finalizerProc)
0176 {
0177     long long id = eventLoop->timeEventNextId++;
0178     aeTimeEvent *te;
0179 
0180     te = zmalloc(sizeof(*te));
0181     if (te == NULL) return AE_ERR;
0182     te->id = id;
0183     aeAddMillisecondsToNow(milliseconds,&te->when_sec,&te->when_ms);
0184     te->timeProc = proc;
0185     te->finalizerProc = finalizerProc;
0186     te->clientData = clientData;
0187     te->next = eventLoop->timeEventHead;
0188     eventLoop->timeEventHead = te;
0189     return id;
0190 }
0191 
0192 int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id)
0193 {
0194     aeTimeEvent *te, *prev = NULL;
0195 
0196     te = eventLoop->timeEventHead;
0197     while(te) {
0198         if (te->id == id) {
0199             if (prev == NULL)
0200                 eventLoop->timeEventHead = te->next;
0201             else
0202                 prev->next = te->next;
0203             if (te->finalizerProc)
0204                 te->finalizerProc(eventLoop, te->clientData);
0205             zfree(te);
0206             return AE_OK;
0207         }
0208         prev = te;
0209         te = te->next;
0210     }
0211     return AE_ERR; /* NO event with the specified ID found */
0212 }
0213 
0214 /* Search the first timer to fire.
0215  * This operation is useful to know how many time the select can be
0216  * put in sleep without to delay any event.
0217  * If there are no timers NULL is returned.
0218  *
0219  * Note that's O(N) since time events are unsorted.
0220  * Possible optimizations (not needed by Redis so far, but...):
0221  * 1) Insert the event in order, so that the nearest is just the head.
0222  *    Much better but still insertion or deletion of timers is O(N).
0223  * 2) Use a skiplist to have this operation as O(1) and insertion as O(log(N)).
0224  */
0225 static aeTimeEvent *aeSearchNearestTimer(aeEventLoop *eventLoop)
0226 {
0227     aeTimeEvent *te = eventLoop->timeEventHead;
0228     aeTimeEvent *nearest = NULL;
0229 
0230     while(te) {
0231         if (!nearest || te->when_sec < nearest->when_sec ||
0232                 (te->when_sec == nearest->when_sec &&
0233                  te->when_ms < nearest->when_ms))
0234             nearest = te;
0235         te = te->next;
0236     }
0237     return nearest;
0238 }
0239 
0240 /* Process time events */
0241 static int processTimeEvents(aeEventLoop *eventLoop) {
0242     int processed = 0;
0243     aeTimeEvent *te;
0244     long long maxId;
0245     time_t now = time(NULL);
0246 
0247     /* If the system clock is moved to the future, and then set back to the
0248      * right value, time events may be delayed in a random way. Often this
0249      * means that scheduled operations will not be performed soon enough.
0250      *
0251      * Here we try to detect system clock skews, and force all the time
0252      * events to be processed ASAP when this happens: the idea is that
0253      * processing events earlier is less dangerous than delaying them
0254      * indefinitely, and practice suggests it is. */
0255     if (now < eventLoop->lastTime) {
0256         te = eventLoop->timeEventHead;
0257         while(te) {
0258             te->when_sec = 0;
0259             te = te->next;
0260         }
0261     }
0262     eventLoop->lastTime = now;
0263 
0264     te = eventLoop->timeEventHead;
0265     maxId = eventLoop->timeEventNextId-1;
0266     while(te) {
0267         long now_sec, now_ms;
0268         long long id;
0269 
0270         if (te->id > maxId) {
0271             te = te->next;
0272             continue;
0273         }
0274         aeGetTime(&now_sec, &now_ms);
0275         if (now_sec > te->when_sec ||
0276             (now_sec == te->when_sec && now_ms >= te->when_ms))
0277         {
0278             int retval;
0279 
0280             id = te->id;
0281             retval = te->timeProc(eventLoop, id, te->clientData);
0282             processed++;
0283             /* After an event is processed our time event list may
0284              * no longer be the same, so we restart from head.
0285              * Still we make sure to don't process events registered
0286              * by event handlers itself in order to don't loop forever.
0287              * To do so we saved the max ID we want to handle.
0288              *
0289              * FUTURE OPTIMIZATIONS:
0290              * Note that this is NOT great algorithmically. Redis uses
0291              * a single time event so it's not a problem but the right
0292              * way to do this is to add the new elements on head, and
0293              * to flag deleted elements in a special way for later
0294              * deletion (putting references to the nodes to delete into
0295              * another linked list). */
0296             if (retval != AE_NOMORE) {
0297                 aeAddMillisecondsToNow(retval,&te->when_sec,&te->when_ms);
0298             } else {
0299                 aeDeleteTimeEvent(eventLoop, id);
0300             }
0301             te = eventLoop->timeEventHead;
0302         } else {
0303             te = te->next;
0304         }
0305     }
0306     return processed;
0307 }
0308 
0309 /* Process every pending time event, then every pending file event
0310  * (that may be registered by time event callbacks just processed).
0311  * Without special flags the function sleeps until some file event
0312  * fires, or when the next time event occurs (if any).
0313  *
0314  * If flags is 0, the function does nothing and returns.
0315  * if flags has AE_ALL_EVENTS set, all the kind of events are processed.
0316  * if flags has AE_FILE_EVENTS set, file events are processed.
0317  * if flags has AE_TIME_EVENTS set, time events are processed.
0318  * if flags has AE_DONT_WAIT set the function returns ASAP until all
0319  * the events that's possible to process without to wait are processed.
0320  *
0321  * The function returns the number of events processed. */
0322 int aeProcessEvents(aeEventLoop *eventLoop, int flags)
0323 {
0324     int processed = 0, numevents;
0325 
0326     /* Nothing to do? return ASAP */
0327     if (!(flags & AE_TIME_EVENTS) && !(flags & AE_FILE_EVENTS)) return 0;
0328 
0329     /* Note that we want call select() even if there are no
0330      * file events to process as long as we want to process time
0331      * events, in order to sleep until the next time event is ready
0332      * to fire. */
0333     if (eventLoop->maxfd != -1 ||
0334         ((flags & AE_TIME_EVENTS) && !(flags & AE_DONT_WAIT))) {
0335         int j;
0336         aeTimeEvent *shortest = NULL;
0337         struct timeval tv, *tvp;
0338 
0339         if (flags & AE_TIME_EVENTS && !(flags & AE_DONT_WAIT))
0340             shortest = aeSearchNearestTimer(eventLoop);
0341         if (shortest) {
0342             long now_sec, now_ms;
0343 
0344             /* Calculate the time missing for the nearest
0345              * timer to fire. */
0346             aeGetTime(&now_sec, &now_ms);
0347             tvp = &tv;
0348             tvp->tv_sec = shortest->when_sec - now_sec;
0349             if (shortest->when_ms < now_ms) {
0350                 tvp->tv_usec = ((shortest->when_ms+1000) - now_ms)*1000;
0351                 tvp->tv_sec --;
0352             } else {
0353                 tvp->tv_usec = (shortest->when_ms - now_ms)*1000;
0354             }
0355             if (tvp->tv_sec < 0) tvp->tv_sec = 0;
0356             if (tvp->tv_usec < 0) tvp->tv_usec = 0;
0357         } else {
0358             /* If we have to check for events but need to return
0359              * ASAP because of AE_DONT_WAIT we need to set the timeout
0360              * to zero */
0361             if (flags & AE_DONT_WAIT) {
0362                 tv.tv_sec = tv.tv_usec = 0;
0363                 tvp = &tv;
0364             } else {
0365                 /* Otherwise we can block */
0366                 tvp = NULL; /* wait forever */
0367             }
0368         }
0369 
0370         numevents = aeApiPoll(eventLoop, tvp);
0371         for (j = 0; j < numevents; j++) {
0372             aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd];
0373             int mask = eventLoop->fired[j].mask;
0374             int fd = eventLoop->fired[j].fd;
0375             int rfired = 0;
0376 
0377         /* note the fe->mask & mask & ... code: maybe an already processed
0378              * event removed an element that fired and we still didn't
0379              * processed, so we check if the event is still valid. */
0380             if (fe->mask & mask & AE_READABLE) {
0381                 rfired = 1;
0382                 fe->rfileProc(eventLoop,fd,fe->clientData,mask);
0383             }
0384             if (fe->mask & mask & AE_WRITABLE) {
0385                 if (!rfired || fe->wfileProc != fe->rfileProc)
0386                     fe->wfileProc(eventLoop,fd,fe->clientData,mask);
0387             }
0388             processed++;
0389         }
0390     }
0391     /* Check time events */
0392     if (flags & AE_TIME_EVENTS)
0393         processed += processTimeEvents(eventLoop);
0394 
0395     return processed; /* return the number of processed file/time events */
0396 }
0397 
0398 /* Wait for milliseconds until the given file descriptor becomes
0399  * writable/readable/exception */
0400 int aeWait(int fd, int mask, long long milliseconds) {
0401     struct pollfd pfd;
0402     int retmask = 0, retval;
0403 
0404     memset(&pfd, 0, sizeof(pfd));
0405     pfd.fd = fd;
0406     if (mask & AE_READABLE) pfd.events |= POLLIN;
0407     if (mask & AE_WRITABLE) pfd.events |= POLLOUT;
0408 
0409     if ((retval = poll(&pfd, 1, milliseconds))== 1) {
0410         if (pfd.revents & POLLIN) retmask |= AE_READABLE;
0411         if (pfd.revents & POLLOUT) retmask |= AE_WRITABLE;
0412     if (pfd.revents & POLLERR) retmask |= AE_WRITABLE;
0413         if (pfd.revents & POLLHUP) retmask |= AE_WRITABLE;
0414         return retmask;
0415     } else {
0416         return retval;
0417     }
0418 }
0419 
0420 void aeMain(aeEventLoop *eventLoop) {
0421     eventLoop->stop = 0;
0422     while (!eventLoop->stop) {
0423         if (eventLoop->beforesleep != NULL)
0424             eventLoop->beforesleep(eventLoop);
0425         aeProcessEvents(eventLoop, AE_ALL_EVENTS);
0426     }
0427 }
0428 
0429 char *aeGetApiName(void) {
0430     return aeApiName();
0431 }
0432 
0433 void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep) {
0434     eventLoop->beforesleep = beforesleep;
0435 }