int aioPoll(int microSeconds) { int fd; fd_set rd, wr, ex; unsigned long long us; FPRINTF((stderr, "aioPoll(%d)\n", microSeconds)); DO_TICK(SHOULD_TICK()); /* get out early if there is no pending i/o and no need to relinquish cpu */ if ((maxFd == 0) && (microSeconds == 0)) return 0; rd= rdMask; wr= wrMask; ex= exMask; us= ioUTCMicroseconds(); for (;;) { struct timeval tv; int n; unsigned long long now; tv.tv_sec= microSeconds / 1000000; tv.tv_usec= microSeconds % 1000000; n= select(maxFd, &rd, &wr, &ex, &tv); if (n > 0) break; if (n == 0) return 0; if (errno && (EINTR != errno)) { fprintf(stderr, "errno %d\n", errno); perror("select"); return 0; } now= ioUTCMicroseconds(); microSeconds -= max(now - us,1); if (microSeconds <= 0) return 0; us= now; } for (fd= 0; fd < maxFd; ++fd) { # define _DO(FLAG, TYPE) \ { \ if (FD_ISSET(fd, &TYPE)) \ { \ aioHandler handler= TYPE##Handler[fd]; \ FD_CLR(fd, &TYPE##Mask); \ TYPE##Handler[fd]= undefinedHandler; \ handler(fd, clientData[fd], FLAG); \ } \ } _DO_FLAG_TYPE(); # undef _DO } return 1; }
void aioHandle(int fd, aioHandler handlerFn, int mask) { FPRINTF((stderr, "aioHandle(%d, %s, %d)\n", fd, handlerName(handlerFn), mask)); if (fd < 0) { FPRINTF((stderr, "aioHandle(%d): IGNORED\n", fd)); return; } # define _DO(FLAG, TYPE) \ if (mask & FLAG) { \ FD_SET(fd, &TYPE##Mask); \ TYPE##Handler[fd]= handlerFn; \ } _DO_FLAG_TYPE(); # undef _DO }
void aioSuspend(int fd, int mask) { if (fd < 0) { FPRINTF((stderr, "aioSuspend(%d): IGNORED\n", fd)); return; } FPRINTF((stderr, "aioSuspend(%d)\n", fd)); # define _DO(FLAG, TYPE) \ { \ if (mask & FLAG) \ { \ FD_CLR(fd, &TYPE##Mask); \ TYPE##Handler[fd]= undefinedHandler; \ } \ } _DO_FLAG_TYPE(); # undef _DO }
int aioPoll(int microSeconds) { int fd, ms; fd_set rd, wr, ex; FPRINTF((stderr, "aioPoll(%d)\n", microSeconds)); DO_TICK(); /* get out early if there is no pending i/o and no need to relinquish cpu */ if ((maxFd == 0) && (microSeconds == 0)) return 0; rd= rdMask; wr= wrMask; ex= exMask; #ifdef BUILD_FOR_OSX #else if (maxFd == 0) return 0; #endif ms= (int) ioMSecs(); for (;;) { struct timeval tv; int n, now,why; tv.tv_sec= microSeconds / 1000000; tv.tv_usec= microSeconds % 1000000; n= select(maxFd, &rd, &wr, &ex, &tv); why = errno; if (n > 0) break; if (n == 0) return 0; if (errno && (EINTR != errno)) { fprintf(stderr, "errno %d\n", errno); perror("select"); return 0; } now= (int) ioMSecs(); microSeconds -= (now - ms) * 1000; if (microSeconds <= 0) return 0; ms= now; } for (fd= 0; fd < maxFd; ++fd) { # define _DO(FLAG, TYPE) \ { \ if (FD_ISSET(fd, &TYPE)) \ { \ aioHandler handler= TYPE##Handler[fd]; \ FD_CLR(fd, &TYPE##Mask); \ TYPE##Handler[fd]= undefinedHandler; \ handler(fd, clientData[fd], FLAG); \ } \ } _DO_FLAG_TYPE(); # undef _DO } return 1; }