jabberd2  2.2.16
mio/mio_select.h
Go to the documentation of this file.
00001 /*
00002  * jabberd - Jabber Open Source Server
00003  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
00004  *                    Ryan Eatmon, Robert Norris
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
00019  */
00020 
00021 /* MIO backend for select() */
00022 
00023 #ifdef HAVE_SYS_SELECT_H
00024 # include <sys/select.h>
00025 #endif
00026 
00027 #define MIO_FUNCS \
00028     static void _mio_fds_init(mio_priv_t m)                             \
00029     {                                                                   \
00030         int fd;                                                         \
00031         for(fd = 0; fd < m->maxfd; fd++)                                \
00032         {                                                               \
00033             m->fds[fd].mio_fd.fd = fd;                                  \
00034         }                                                               \
00035         m->highfd = 0;                                                  \
00036         m->lowfd = m->maxfd;                                            \
00037     }                                                                   \
00038                                                                         \
00039     static mio_fd_t _mio_alloc_fd(mio_priv_t m, int fd)                 \
00040     {                                                                   \
00041         if(fd > m->highfd) m->highfd = fd;                              \
00042         if(fd < m->lowfd) m->lowfd = fd;                                \
00043         return &m->fds[fd].mio_fd;                                      \
00044     }                                                                   \
00045                                                                         \
00046     static int _mio_select(mio_priv_t m, int t)                         \
00047     {                                                                   \
00048         struct timeval tv;                                              \
00049                                                                         \
00050         m->rfds_out = m->rfds_in;                                       \
00051         m->wfds_out = m->wfds_in;                                       \
00052                                                                         \
00053         tv.tv_sec = t;                                                  \
00054         tv.tv_usec = 0;                                                 \
00055         return select(m->highfd + 1, &m->rfds_out, &m->wfds_out, NULL, &tv); \
00056     }
00057 
00058 #define MIO_FD_VARS
00059 
00060 #define MIO_VARS \
00061     struct mio_priv_fd_st *fds;                                         \
00062     int lowfd;                                                          \
00063     int highfd;                                                         \
00064     fd_set rfds_in, wfds_in, rfds_out, wfds_out;
00065 
00066 #define MIO_INIT_VARS(m) \
00067     do {                                                                \
00068         if (maxfd > FD_SETSIZE)                                         \
00069         {                                                               \
00070             mio_debug(ZONE,"wanted MIO larger than %d file descriptors", FD_SETSIZE); \
00071             free(m);                                                    \
00072             return NULL;                                                \
00073         }                                                               \
00074                                                                         \
00075         if((MIO(m)->fds = calloc(1, sizeof(struct mio_priv_fd_st) * maxfd)) == NULL) \
00076         {                                                               \
00077             mio_debug(ZONE,"internal error creating new mio");          \
00078             free(m);                                                    \
00079             return NULL;                                                \
00080         }                                                               \
00081                                                                         \
00082         _mio_fds_init(MIO(m));                                          \
00083         FD_ZERO(&MIO(m)->rfds_in);                                      \
00084         FD_ZERO(&MIO(m)->wfds_in);                                      \
00085     } while(0)
00086 
00087 #define MIO_FREE_VARS(m)        free(MIO(m)->fds)
00088 
00089 #define MIO_ALLOC_FD(m, rfd)    _mio_alloc_fd(MIO(m), rfd)
00090 #define MIO_FREE_FD(m, mfd)
00091 
00092 #define MIO_REMOVE_FD(m, mfd) \
00093     do {                                                                \
00094         FD_CLR(mfd->mio_fd.fd, &MIO(m)->rfds_in);                       \
00095         FD_CLR(mfd->mio_fd.fd, &MIO(m)->wfds_in);                       \
00096     } while(0)
00097 
00098 #define MIO_CHECK(m, t)         _mio_select(MIO(m), t)
00099 
00100 #define MIO_SET_READ(m, mfd)    FD_SET(mfd->mio_fd.fd, &MIO(m)->rfds_in)
00101 #define MIO_SET_WRITE(m, mfd)   FD_SET(mfd->mio_fd.fd, &MIO(m)->wfds_in)
00102 
00103 #define MIO_UNSET_READ(m, mfd)  FD_CLR(mfd->mio_fd.fd, &MIO(m)->rfds_in)
00104 #define MIO_UNSET_WRITE(m, mfd) FD_CLR(mfd->mio_fd.fd, &MIO(m)->wfds_in)
00105 
00106 #define MIO_CAN_READ(m, iter)   FD_ISSET(iter, &MIO(m)->rfds_out)
00107 #define MIO_CAN_WRITE(m, iter)  FD_ISSET(iter, &MIO(m)->wfds_out)
00108 #define MIO_CAN_FREE(m)         1
00109 
00110 
00111 #define MIO_INIT_ITERATOR(iter) \
00112     int iter
00113 
00114 #define MIO_ITERATE_RESULTS(m, retval, iter) \
00115     for(iter = MIO(m)->lowfd; iter <= MIO(m)->highfd; iter++)
00116 
00117 #define MIO_ITERATOR_FD(m, iter) \
00118     (&MIO(m)->fds[iter].mio_fd)