jabberd2  2.3.3
mio_select.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 select() */
22 
23 #ifdef HAVE_SYS_SELECT_H
24 # include <sys/select.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->fds[fd].mio_fd.fd = fd; \
34  } \
35  m->highfd = 0; \
36  m->lowfd = m->maxfd; \
37  } \
38  \
39  static mio_fd_t _mio_alloc_fd(mio_priv_t m, int fd) \
40  { \
41  if(fd > m->highfd) m->highfd = fd; \
42  if(fd < m->lowfd) m->lowfd = fd; \
43  return &m->fds[fd].mio_fd; \
44  } \
45  \
46  static int _mio_select(mio_priv_t m, int t) \
47  { \
48  struct timeval tv; \
49  \
50  m->rfds_out = m->rfds_in; \
51  m->wfds_out = m->wfds_in; \
52  \
53  tv.tv_sec = t; \
54  tv.tv_usec = 0; \
55  return select(m->highfd + 1, &m->rfds_out, &m->wfds_out, NULL, &tv); \
56  }
57 
58 #define MIO_FD_VARS
59 
60 #define MIO_VARS \
61  struct mio_priv_fd_st *fds; \
62  int lowfd; \
63  int highfd; \
64  fd_set rfds_in, wfds_in, rfds_out, wfds_out;
65 
66 #define MIO_INIT_VARS(m) \
67  do { \
68  if (maxfd > FD_SETSIZE) \
69  { \
70  mio_debug(ZONE,"wanted MIO larger than %d file descriptors", FD_SETSIZE); \
71  free(m); \
72  return NULL; \
73  } \
74  \
75  if((MIO(m)->fds = calloc(1, sizeof(struct mio_priv_fd_st) * maxfd)) == NULL) \
76  { \
77  mio_debug(ZONE,"internal error creating new mio"); \
78  free(m); \
79  return NULL; \
80  } \
81  \
82  _mio_fds_init(MIO(m)); \
83  FD_ZERO(&MIO(m)->rfds_in); \
84  FD_ZERO(&MIO(m)->wfds_in); \
85  } while(0)
86 
87 #define MIO_FREE_VARS(m) free(MIO(m)->fds)
88 
89 #define MIO_ALLOC_FD(m, rfd) _mio_alloc_fd(MIO(m), rfd)
90 #define MIO_FREE_FD(m, mfd)
91 
92 #define MIO_REMOVE_FD(m, mfd) \
93  do { \
94  FD_CLR(mfd->mio_fd.fd, &MIO(m)->rfds_in); \
95  FD_CLR(mfd->mio_fd.fd, &MIO(m)->wfds_in); \
96  } while(0)
97 
98 #define MIO_CHECK(m, t) _mio_select(MIO(m), t)
99 
100 #define MIO_SET_READ(m, mfd) FD_SET(mfd->mio_fd.fd, &MIO(m)->rfds_in)
101 #define MIO_SET_WRITE(m, mfd) FD_SET(mfd->mio_fd.fd, &MIO(m)->wfds_in)
102 
103 #define MIO_UNSET_READ(m, mfd) FD_CLR(mfd->mio_fd.fd, &MIO(m)->rfds_in)
104 #define MIO_UNSET_WRITE(m, mfd) FD_CLR(mfd->mio_fd.fd, &MIO(m)->wfds_in)
105 
106 #define MIO_CAN_READ(m, iter) FD_ISSET(iter, &MIO(m)->rfds_out)
107 #define MIO_CAN_WRITE(m, iter) FD_ISSET(iter, &MIO(m)->wfds_out)
108 #define MIO_CAN_FREE(m) 1
109 
110 
111 #define MIO_INIT_ITERATOR(iter) \
112  int iter
113 
114 #define MIO_ITERATE_RESULTS(m, retval, iter) \
115  for(iter = MIO(m)->lowfd; iter <= MIO(m)->highfd; iter++)
116 
117 #define MIO_ITERATOR_FD(m, iter) \
118  (&MIO(m)->fds[iter].mio_fd)