jabberd2  2.2.16
mio/mio_poll.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 poll() */
00022 
00023 #ifdef HAVE_POLL_H
00024 # include <poll.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->pfds[fd].fd = -1;                                        \
00034             m->fds[fd].mio_fd.fd = fd;                                  \
00035         }                                                               \
00036         m->highfd = 0;                                                  \
00037     }                                                                   \
00038                                                                         \
00039     static mio_fd_t _mio_alloc_fd(mio_priv_t m, int fd)                 \
00040     {                                                                   \
00041         m->pfds[fd].fd = fd;                                            \
00042         m->pfds[fd].events = 0;                                         \
00043         if(fd > m->highfd) m->highfd = fd;                              \
00044         return &m->fds[fd].mio_fd;                                      \
00045     }                                                                   \
00046                                                                         \
00047     static int _mio_poll(mio_priv_t m, int t)                           \
00048     {                                                                   \
00049         return poll(m->pfds, m->highfd + 1, t*1000);                    \
00050     }
00051 
00052 #define MIO_FD_VARS
00053 
00054 #define MIO_VARS \
00055     struct mio_priv_fd_st *fds;                                         \
00056     int highfd;                                                         \
00057     struct pollfd *pfds;
00058 
00059 #define MIO_INIT_VARS(m) \
00060     do {                                                                \
00061         if((MIO(m)->fds = malloc(sizeof(struct mio_priv_fd_st) * maxfd)) == NULL) \
00062         {                                                               \
00063             mio_debug(ZONE,"internal error creating new mio");          \
00064             free(m);                                                    \
00065             return NULL;                                                \
00066         }                                                               \
00067         memset(MIO(m)->fds, 0, sizeof(struct mio_priv_fd_st) * maxfd);  \
00068                                                                         \
00069         if((MIO(m)->pfds = malloc(sizeof(struct pollfd) * maxfd)) == NULL) \
00070         {                                                               \
00071             mio_debug(ZONE, "internal error creating new mio");         \
00072             free(MIO(m)->fds);                                          \
00073             free(m);                                                    \
00074             return NULL;                                                \
00075         }                                                               \
00076         memset(MIO(m)->pfds, 0, sizeof(struct pollfd) * maxfd);         \
00077                                                                         \
00078         _mio_fds_init(MIO(m));                                          \
00079     } while(0)
00080 
00081 #define MIO_FREE_VARS(m) \
00082     do {                                                                \
00083         free(MIO(m)->fds);                                              \
00084         free(MIO(m)->pfds);                                             \
00085     } while (0)
00086 
00087 #define MIO_ALLOC_FD(m, rfd)    _mio_alloc_fd(MIO(m), rfd)
00088 #define MIO_FREE_FD(m, mfd)
00089 
00090 #define MIO_REMOVE_FD(m, mfd)   MIO(m)->pfds[mfd->mio_fd.fd].fd = -1
00091 
00092 #define MIO_CHECK(m, t)         _mio_poll(MIO(m), t)
00093 
00094 #define MIO_SET_READ(m, mfd)    MIO(m)->pfds[mfd->mio_fd.fd].events |= POLLIN
00095 #define MIO_SET_WRITE(m, mfd)   MIO(m)->pfds[mfd->mio_fd.fd].events |= POLLOUT
00096 
00097 #define MIO_UNSET_READ(m, mfd)  MIO(m)->pfds[mfd->mio_fd.fd].events &= ~POLLIN
00098 #define MIO_UNSET_WRITE(m, mfd) MIO(m)->pfds[mfd->mio_fd.fd].events &= ~POLLOUT
00099 
00100 
00101 #define MIO_CAN_READ(m, iter) \
00102     (MIO(m)->pfds[iter].revents & (POLLIN|POLLERR|POLLHUP|POLLNVAL))
00103 #define MIO_CAN_WRITE(m, iter)  (MIO(m)->pfds[iter].revents & POLLOUT)
00104 #define MIO_CAN_FREE(m)         1
00105 
00106 
00107 #define MIO_INIT_ITERATOR(iter) \
00108     int iter
00109 
00110 #define MIO_ITERATE_RESULTS(m, retval, iter) \
00111     for(iter = 0; iter <= MIO(m)->highfd; iter++)
00112 
00113 #define MIO_ITERATOR_FD(m, iter) \
00114     (&MIO(m)->fds[iter].mio_fd)