X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/7a932b74cc22097655d6792f64cab66bb0fe1694..a4798f733dc2cb973552e0ea84cc5f553951e162:/ae.c diff --git a/ae.c b/ae.c index 81fd9add..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,11 +363,9 @@ 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; @@ -375,10 +374,17 @@ int aeWait(int fd, int mask, long long milliseconds) { 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; +}