X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/f3053eb0eb704dea7ff39f389f344a37e3985aab..e74825c24f134cf75e2285ecaf8d379bc8e7d28e:/ae.c?ds=sidebyside diff --git a/ae.c b/ae.c index 048fa51c..c7918ee1 100644 --- a/ae.c +++ b/ae.c @@ -2,7 +2,7 @@ * for the Jim's event-loop (Jim is a Tcl interpreter) but later translated * it in form of a library for easy reuse. * - * Copyright (c) 2006-2009, Salvatore Sanfilippo + * Copyright (c) 2006-2010, Salvatore Sanfilippo * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -62,6 +62,7 @@ aeEventLoop *aeCreateEventLoop(void) { eventLoop->timeEventNextId = 0; eventLoop->stop = 0; eventLoop->maxfd = -1; + eventLoop->beforesleep = NULL; if (aeApiCreate(eventLoop) == -1) { zfree(eventLoop); return NULL; @@ -93,7 +94,6 @@ int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask, fe->mask |= mask; if (mask & AE_READABLE) fe->rfileProc = proc; if (mask & AE_WRITABLE) fe->wfileProc = proc; - if (mask & AE_EXCEPTION) fe->efileProc = proc; fe->clientData = clientData; if (fd > eventLoop->maxfd) eventLoop->maxfd = fd; @@ -325,18 +325,19 @@ int aeProcessEvents(aeEventLoop *eventLoop, int flags) aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd]; int mask = eventLoop->fired[j].mask; int fd = eventLoop->fired[j].fd; + int rfired = 0; /* note the fe->mask & mask & ... code: maybe an already processed * event removed an element that fired and we still didn't * processed, so we check if the event is still valid. */ - if (fe->mask & mask & AE_READABLE) + if (fe->mask & mask & AE_READABLE) { + rfired = 1; fe->rfileProc(eventLoop,fd,fe->clientData,mask); - if (fe->mask & mask & AE_WRITABLE && fe->wfileProc != fe->rfileProc) - fe->wfileProc(eventLoop,fd,fe->clientData,mask); - if (fe->mask & mask & AE_EXCEPTION && - fe->efileProc != fe->wfileProc && - fe->efileProc != fe->rfileProc) - fe->efileProc(eventLoop,fd,fe->clientData,mask); + } + if (fe->mask & mask & AE_WRITABLE) { + if (!rfired || fe->wfileProc != fe->rfileProc) + fe->wfileProc(eventLoop,fd,fe->clientData,mask); + } processed++; } } @@ -362,20 +363,28 @@ int aeWait(int fd, int mask, long long milliseconds) { if (mask & AE_READABLE) FD_SET(fd,&rfds); if (mask & AE_WRITABLE) FD_SET(fd,&wfds); - if (mask & AE_EXCEPTION) FD_SET(fd,&efds); if ((retval = select(fd+1, &rfds, &wfds, &efds, &tv)) > 0) { if (FD_ISSET(fd,&rfds)) retmask |= AE_READABLE; if (FD_ISSET(fd,&wfds)) retmask |= AE_WRITABLE; - if (FD_ISSET(fd,&efds)) retmask |= AE_EXCEPTION; return retmask; } else { return retval; } } -void aeMain(aeEventLoop *eventLoop) -{ +void aeMain(aeEventLoop *eventLoop) { eventLoop->stop = 0; - while (!eventLoop->stop) + while (!eventLoop->stop) { + if (eventLoop->beforesleep != NULL) + eventLoop->beforesleep(eventLoop); aeProcessEvents(eventLoop, AE_ALL_EVENTS); + } +} + +char *aeGetApiName(void) { + return aeApiName(); +} + +void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep) { + eventLoop->beforesleep = beforesleep; }