libcutils/selector.c
static void prepareForSelect(Selector* selector) {
    fd_set* exceptFds = &selector->exceptFds;
    fd_set* readFds = &selector->readFds;
    fd_set* writeFds = &selector->writeFds;
    
    FD_ZERO(exceptFds);
    FD_ZERO(readFds);
    FD_ZERO(writeFds);

    Array* selectableFds = selector->selectableFds;
    int i = 0;
    selector->maxFd = 0;
    int size = arraySize(selectableFds);
    while (i < size) {
        SelectableFd* selectableFd = arrayGet(selectableFds, i);
        if (selectableFd->remove) {
            // This descriptor should be removed.
            arrayRemove(selectableFds, i);
            size--;
            if (selectableFd->onRemove != NULL) {
                selectableFd->onRemove(selectableFd);
            }
            free(selectableFd);
        } else {
            if (selectableFd->beforeSelect != NULL) {
                selectableFd->beforeSelect(selectableFd);
            }
            
            bool inSet = false;
            if (maybeAdd(selectableFd, selectableFd->onExcept, exceptFds)) {
            	LOGD("Selecting fd %d for writing...", selectableFd->fd);
                inSet = true;
            }
            if (maybeAdd(selectableFd, selectableFd->onReadable, readFds)) {
            	LOGD("Selecting fd %d for reading...", selectableFd->fd);
                inSet = true;
            }
            if (maybeAdd(selectableFd, selectableFd->onWritable, writeFds)) {
                inSet = true;
            }

            if (inSet) {
                // If the fd is in a set, check it against max.
                int fd = selectableFd->fd;
                if (fd > selector->maxFd) {
                    selector->maxFd = fd;
                }
            }
            
            // Move to next descriptor.
            i++;
        }
    }
}
/**
 * Removes stale file descriptors and initializes file descriptor sets.
 */
Removes stale file descriptors and initializes file descriptor sets.
Removes stale file descriptors and initializes file descriptor sets .
static void prepareForSelect(Selector* selector) {
    fd_set* exceptFds = &selector->exceptFds;
    fd_set* readFds = &selector->readFds;
    fd_set* writeFds = &selector->writeFds;
    FD_ZERO(exceptFds);
    FD_ZERO(readFds);
    FD_ZERO(writeFds);
    Array* selectableFds = selector->selectableFds;
    int i = 0;
    selector->maxFd = 0;
    int size = arraySize(selectableFds);
    while (i < size) {
        SelectableFd* selectableFd = arrayGet(selectableFds, i);
        if (selectableFd->remove) {
            arrayRemove(selectableFds, i);
            size--;
            if (selectableFd->onRemove != NULL) {
                selectableFd->onRemove(selectableFd);
            }
            free(selectableFd);
        } else {
            if (selectableFd->beforeSelect != NULL) {
                selectableFd->beforeSelect(selectableFd);
            }
            bool inSet = false;
            if (maybeAdd(selectableFd, selectableFd->onExcept, exceptFds)) {
            	LOGD("Selecting fd %d for writing...", selectableFd->fd);
                inSet = true;
            }
            if (maybeAdd(selectableFd, selectableFd->onReadable, readFds)) {
            	LOGD("Selecting fd %d for reading...", selectableFd->fd);
                inSet = true;
            }
            if (maybeAdd(selectableFd, selectableFd->onWritable, writeFds)) {
                inSet = true;
            }
            if (inSet) {
                int fd = selectableFd->fd;
                if (fd > selector->maxFd) {
                    selector->maxFd = fd;
                }
            }
            i++;
        }
    }
}
static void prepareForSelect ( Selector * selector ) { fd_set * exceptFds = & selector -> exceptFds ; fd_set * readFds = & selector -> readFds ; fd_set * writeFds = & selector -> writeFds ; FD_ZERO ( exceptFds ) ; FD_ZERO ( readFds ) ; FD_ZERO ( writeFds ) ; Array * selectableFds = selector -> selectableFds ; int i = 0 ; selector -> maxFd = 0 ; int size = arraySize ( selectableFds ) ; while ( i < size ) { SelectableFd * selectableFd = arrayGet ( selectableFds , i ) ; if ( selectableFd -> remove ) { arrayRemove ( selectableFds , i ) ; size -- ; if ( selectableFd -> onRemove != NULL ) { selectableFd -> onRemove ( selectableFd ) ; } free ( selectableFd ) ; } else { if ( selectableFd -> beforeSelect != NULL ) { selectableFd -> beforeSelect ( selectableFd ) ; } bool inSet = false ; if ( maybeAdd ( selectableFd , selectableFd -> onExcept , exceptFds ) ) { LOGD ( " " , selectableFd -> fd ) ; inSet = true ; } if ( maybeAdd ( selectableFd , selectableFd -> onReadable , readFds ) ) { LOGD ( " " , selectableFd -> fd ) ; inSet = true ; } if ( maybeAdd ( selectableFd , selectableFd -> onWritable , writeFds ) ) { inSet = true ; } if ( inSet ) { int fd = selectableFd -> fd ; if ( fd > selector -> maxFd ) { selector -> maxFd = fd ; } } i ++ ; } } }
Removes stale file descriptors and initializes file descriptor sets.
Removes stale file descriptors and initializes file descriptor sets .
// This descriptor should be removed. // If the fd is in a set, check it against max. // Move to next descriptor.