jabberd2  2.3.1
mio_poll.h
Go to the documentation of this file.
1 /*
2  * jabberd - Jabber Open Source Server
3  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
4  * Ryan Eatmon, Robert Norris
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
19  */
20 
21 /* MIO backend for poll() */
22 
23 #ifdef HAVE_POLL_H
24 # include <poll.h>
25 #endif
26 
27 #define MIO_FUNCS \
28  static void _mio_fds_init(mio_priv_t m) \
29  { \
30  int fd; \
31  for(fd = 0; fd < m->maxfd; fd++) \
32  { \
33  m->pfds[fd].fd = -1; \
34  m->fds[fd].mio_fd.fd = fd; \
35  } \
36  m->highfd = 0; \
37  } \
38  \
39  static mio_fd_t _mio_alloc_fd(mio_priv_t m, int fd) \
40  { \
41  m->pfds[fd].fd = fd; \
42  m->pfds[fd].events = 0; \
43  if(fd > m->highfd) m->highfd = fd; \
44  return &m->fds[fd].mio_fd; \
45  } \
46  \
47  static int _mio_poll(mio_priv_t m, int t) \
48  { \
49  return poll(m->pfds, m->highfd + 1, t*1000); \
50  }
51 
52 #define MIO_FD_VARS
53 
54 #define MIO_VARS \
55  struct mio_priv_fd_st *fds; \
56  int highfd; \
57  struct pollfd *pfds;
58 
59 #define MIO_INIT_VARS(m) \
60  do { \
61  if((MIO(m)->fds = malloc(sizeof(struct mio_priv_fd_st) * maxfd)) == NULL) \
62  { \
63  mio_debug(ZONE,"internal error creating new mio"); \
64  free(m); \
65  return NULL; \
66  } \
67  memset(MIO(m)->fds, 0, sizeof(struct mio_priv_fd_st) * maxfd); \
68  \
69  if((MIO(m)->pfds = malloc(sizeof(struct pollfd) * maxfd)) == NULL) \
70  { \
71  mio_debug(ZONE, "internal error creating new mio"); \
72  free(MIO(m)->fds); \
73  free(m); \
74  return NULL; \
75  } \
76  memset(MIO(m)->pfds, 0, sizeof(struct pollfd) * maxfd); \
77  \
78  _mio_fds_init(MIO(m)); \
79  } while(0)
80 
81 #define MIO_FREE_VARS(m) \
82  do { \
83  free(MIO(m)->fds); \
84  free(MIO(m)->pfds); \
85  } while (0)
86 
87 #define MIO_ALLOC_FD(m, rfd) _mio_alloc_fd(MIO(m), rfd)
88 #define MIO_FREE_FD(m, mfd)
89 
90 #define MIO_REMOVE_FD(m, mfd) MIO(m)->pfds[mfd->mio_fd.fd].fd = -1
91 
92 #define MIO_CHECK(m, t) _mio_poll(MIO(m), t)
93 
94 #define MIO_SET_READ(m, mfd) MIO(m)->pfds[mfd->mio_fd.fd].events |= POLLIN
95 #define MIO_SET_WRITE(m, mfd) MIO(m)->pfds[mfd->mio_fd.fd].events |= POLLOUT
96 
97 #define MIO_UNSET_READ(m, mfd) MIO(m)->pfds[mfd->mio_fd.fd].events &= ~POLLIN
98 #define MIO_UNSET_WRITE(m, mfd) MIO(m)->pfds[mfd->mio_fd.fd].events &= ~POLLOUT
99 
100 
101 #define MIO_CAN_READ(m, iter) \
102  (MIO(m)->pfds[iter].revents & (POLLIN|POLLERR|POLLHUP|POLLNVAL))
103 #define MIO_CAN_WRITE(m, iter) (MIO(m)->pfds[iter].revents & POLLOUT)
104 #define MIO_CAN_FREE(m) 1
105 
106 
107 #define MIO_INIT_ITERATOR(iter) \
108  int iter
109 
110 #define MIO_ITERATE_RESULTS(m, retval, iter) \
111  for(iter = 0; iter <= MIO(m)->highfd; iter++)
112 
113 #define MIO_ITERATOR_FD(m, iter) \
114  (&MIO(m)->fds[iter].mio_fd)