Annotation of embedaddon/lighttpd/src/fdevent_select.c, revision 1.1.1.3

1.1.1.3 ! misho       1: #include "first.h"
        !             2: 
1.1       misho       3: #include "fdevent.h"
                      4: #include "buffer.h"
                      5: #include "log.h"
                      6: 
                      7: #include <sys/time.h>
                      8: #include <sys/types.h>
                      9: 
                     10: #include <unistd.h>
                     11: #include <stdlib.h>
                     12: #include <string.h>
                     13: #include <errno.h>
                     14: #include <signal.h>
                     15: #include <fcntl.h>
                     16: #include <assert.h>
                     17: 
                     18: #ifdef USE_SELECT
                     19: 
                     20: static int fdevent_select_reset(fdevents *ev) {
                     21:        FD_ZERO(&(ev->select_set_read));
                     22:        FD_ZERO(&(ev->select_set_write));
                     23:        FD_ZERO(&(ev->select_set_error));
                     24:        ev->select_max_fd = -1;
                     25: 
                     26:        return 0;
                     27: }
                     28: 
                     29: static int fdevent_select_event_del(fdevents *ev, int fde_ndx, int fd) {
                     30:        if (fde_ndx < 0) return -1;
                     31: 
                     32:        FD_CLR(fd, &(ev->select_set_read));
                     33:        FD_CLR(fd, &(ev->select_set_write));
                     34:        FD_CLR(fd, &(ev->select_set_error));
                     35: 
                     36:        return -1;
                     37: }
                     38: 
                     39: static int fdevent_select_event_set(fdevents *ev, int fde_ndx, int fd, int events) {
                     40:        UNUSED(fde_ndx);
                     41: 
                     42:        /* we should be protected by max-fds, but you never know */
1.1.1.2   misho      43:        force_assert(fd < ((int)FD_SETSIZE));
1.1       misho      44: 
                     45:        if (events & FDEVENT_IN) {
                     46:                FD_SET(fd, &(ev->select_set_read));
                     47:        } else {
                     48:                FD_CLR(fd, &(ev->select_set_read));
                     49:        }
                     50:        if (events & FDEVENT_OUT) {
                     51:                FD_SET(fd, &(ev->select_set_write));
                     52:        } else {
                     53:                FD_CLR(fd, &(ev->select_set_write));
                     54:        }
                     55:        FD_SET(fd, &(ev->select_set_error));
                     56: 
                     57:        if (fd > ev->select_max_fd) ev->select_max_fd = fd;
                     58: 
                     59:        return fd;
                     60: }
                     61: 
                     62: static int fdevent_select_poll(fdevents *ev, int timeout_ms) {
                     63:        struct timeval tv;
                     64: 
                     65:        tv.tv_sec =  timeout_ms / 1000;
                     66:        tv.tv_usec = (timeout_ms % 1000) * 1000;
                     67: 
                     68:        ev->select_read = ev->select_set_read;
                     69:        ev->select_write = ev->select_set_write;
                     70:        ev->select_error = ev->select_set_error;
                     71: 
                     72:        return select(ev->select_max_fd + 1, &(ev->select_read), &(ev->select_write), &(ev->select_error), &tv);
                     73: }
                     74: 
                     75: static int fdevent_select_event_get_revent(fdevents *ev, size_t ndx) {
                     76:        int revents = 0;
                     77: 
                     78:        if (FD_ISSET(ndx, &(ev->select_read))) {
                     79:                revents |= FDEVENT_IN;
                     80:        }
                     81:        if (FD_ISSET(ndx, &(ev->select_write))) {
                     82:                revents |= FDEVENT_OUT;
                     83:        }
                     84:        if (FD_ISSET(ndx, &(ev->select_error))) {
                     85:                revents |= FDEVENT_ERR;
                     86:        }
                     87: 
                     88:        return revents;
                     89: }
                     90: 
                     91: static int fdevent_select_event_get_fd(fdevents *ev, size_t ndx) {
                     92:        UNUSED(ev);
                     93: 
                     94:        return ndx;
                     95: }
                     96: 
                     97: static int fdevent_select_event_next_fdndx(fdevents *ev, int ndx) {
                     98:        int i;
                     99: 
                    100:        i = (ndx < 0) ? 0 : ndx + 1;
                    101: 
                    102:        for (; i < ev->select_max_fd + 1; i++) {
                    103:                if (FD_ISSET(i, &(ev->select_read))) return i;
                    104:                if (FD_ISSET(i, &(ev->select_write))) return i;
                    105:                if (FD_ISSET(i, &(ev->select_error))) return i;
                    106:        }
                    107: 
                    108:        return -1;
                    109: }
                    110: 
                    111: int fdevent_select_init(fdevents *ev) {
                    112:        ev->type = FDEVENT_HANDLER_SELECT;
                    113: #define SET(x) \
                    114:        ev->x = fdevent_select_##x;
                    115: 
                    116:        SET(reset);
                    117:        SET(poll);
                    118: 
                    119:        SET(event_del);
                    120:        SET(event_set);
                    121: 
                    122:        SET(event_next_fdndx);
                    123:        SET(event_get_fd);
                    124:        SET(event_get_revent);
                    125: 
                    126:        return 0;
                    127: }
                    128: 
                    129: #else
                    130: int fdevent_select_init(fdevents *ev) {
                    131:        UNUSED(ev);
                    132: 
                    133:        return -1;
                    134: }
                    135: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>