MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
select.c
1 /* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
2 
3 /*
4  * Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_TIME_H
35 #include <sys/time.h>
36 #else
37 #include <sys/_time.h>
38 #endif
39 #ifdef HAVE_SYS_SELECT_H
40 #include <sys/select.h>
41 #endif
42 #include <sys/queue.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <errno.h>
49 #ifdef CHECK_INVARIANTS
50 #include <assert.h>
51 #endif
52 
53 #include "event.h"
54 #include "event-internal.h"
55 #include "evsignal.h"
56 #include "log.h"
57 
58 #ifndef howmany
59 #define howmany(x, y) (((x)+((y)-1))/(y))
60 #endif
61 
62 struct selectop {
63  int event_fds; /* Highest fd in fd set */
64  int event_fdsz;
65  fd_set *event_readset_in;
66  fd_set *event_writeset_in;
67  fd_set *event_readset_out;
68  fd_set *event_writeset_out;
69  struct event **event_r_by_fd;
70  struct event **event_w_by_fd;
71 };
72 
73 static void *select_init (struct event_base *);
74 static int select_add (void *, struct event *);
75 static int select_del (void *, struct event *);
76 static int select_dispatch (struct event_base *, void *, struct timeval *);
77 static void select_dealloc (struct event_base *, void *);
78 
79 const struct eventop selectops = {
80  "select",
81  select_init,
82  select_add,
83  select_del,
84  select_dispatch,
85  select_dealloc,
86  0
87 };
88 
89 static int select_resize(struct selectop *sop, int fdsz);
90 
91 static void *
92 select_init(struct event_base *base)
93 {
94  struct selectop *sop;
95 
96  /* Disable select when this environment variable is set */
97  if (getenv("EVENT_NOSELECT"))
98  return (NULL);
99 
100  if (!(sop = calloc(1, sizeof(struct selectop))))
101  return (NULL);
102 
103  select_resize(sop, howmany(32 + 1, NFDBITS)*sizeof(fd_mask));
104 
105  evsignal_init(base);
106 
107  return (sop);
108 }
109 
110 #ifdef CHECK_INVARIANTS
111 static void
112 check_selectop(struct selectop *sop)
113 {
114  int i;
115  for (i = 0; i <= sop->event_fds; ++i) {
116  if (FD_ISSET(i, sop->event_readset_in)) {
117  assert(sop->event_r_by_fd[i]);
118  assert(sop->event_r_by_fd[i]->ev_events & EV_READ);
119  assert(sop->event_r_by_fd[i]->ev_fd == i);
120  } else {
121  assert(! sop->event_r_by_fd[i]);
122  }
123  if (FD_ISSET(i, sop->event_writeset_in)) {
124  assert(sop->event_w_by_fd[i]);
125  assert(sop->event_w_by_fd[i]->ev_events & EV_WRITE);
126  assert(sop->event_w_by_fd[i]->ev_fd == i);
127  } else {
128  assert(! sop->event_w_by_fd[i]);
129  }
130  }
131 
132 }
133 #else
134 #define check_selectop(sop) do { (void) sop; } while (0)
135 #endif
136 
137 static int
138 select_dispatch(struct event_base *base, void *arg, struct timeval *tv)
139 {
140  int res, i, j;
141  struct selectop *sop = arg;
142 
143  check_selectop(sop);
144 
145  memcpy(sop->event_readset_out, sop->event_readset_in,
146  sop->event_fdsz);
147  memcpy(sop->event_writeset_out, sop->event_writeset_in,
148  sop->event_fdsz);
149 
150  res = select(sop->event_fds + 1, sop->event_readset_out,
151  sop->event_writeset_out, NULL, tv);
152 
153  check_selectop(sop);
154 
155  if (res == -1) {
156  if (errno != EINTR) {
157  event_warn("select");
158  return (-1);
159  }
160 
161  evsignal_process(base);
162  return (0);
163  } else if (base->sig.evsignal_caught) {
164  evsignal_process(base);
165  }
166 
167  event_debug(("%s: select reports %d", __func__, res));
168 
169  check_selectop(sop);
170  i = random() % (sop->event_fds+1);
171  for (j = 0; j <= sop->event_fds; ++j) {
172  struct event *r_ev = NULL, *w_ev = NULL;
173  if (++i >= sop->event_fds+1)
174  i = 0;
175 
176  res = 0;
177  if (FD_ISSET(i, sop->event_readset_out)) {
178  r_ev = sop->event_r_by_fd[i];
179  res |= EV_READ;
180  }
181  if (FD_ISSET(i, sop->event_writeset_out)) {
182  w_ev = sop->event_w_by_fd[i];
183  res |= EV_WRITE;
184  }
185  if (r_ev && (res & r_ev->ev_events)) {
186  event_active(r_ev, res & r_ev->ev_events, 1);
187  }
188  if (w_ev && w_ev != r_ev && (res & w_ev->ev_events)) {
189  event_active(w_ev, res & w_ev->ev_events, 1);
190  }
191  }
192  check_selectop(sop);
193 
194  return (0);
195 }
196 
197 
198 static int
199 select_resize(struct selectop *sop, int fdsz)
200 {
201  int n_events, n_events_old;
202 
203  fd_set *readset_in = NULL;
204  fd_set *writeset_in = NULL;
205  fd_set *readset_out = NULL;
206  fd_set *writeset_out = NULL;
207  struct event **r_by_fd = NULL;
208  struct event **w_by_fd = NULL;
209 
210  n_events = (fdsz/sizeof(fd_mask)) * NFDBITS;
211  n_events_old = (sop->event_fdsz/sizeof(fd_mask)) * NFDBITS;
212 
213  if (sop->event_readset_in)
214  check_selectop(sop);
215 
216  if ((readset_in = realloc(sop->event_readset_in, fdsz)) == NULL)
217  goto error;
218  sop->event_readset_in = readset_in;
219  if ((readset_out = realloc(sop->event_readset_out, fdsz)) == NULL)
220  goto error;
221  sop->event_readset_out = readset_out;
222  if ((writeset_in = realloc(sop->event_writeset_in, fdsz)) == NULL)
223  goto error;
224  sop->event_writeset_in = writeset_in;
225  if ((writeset_out = realloc(sop->event_writeset_out, fdsz)) == NULL)
226  goto error;
227  sop->event_writeset_out = writeset_out;
228  if ((r_by_fd = realloc(sop->event_r_by_fd,
229  n_events*sizeof(struct event*))) == NULL)
230  goto error;
231  sop->event_r_by_fd = r_by_fd;
232  if ((w_by_fd = realloc(sop->event_w_by_fd,
233  n_events * sizeof(struct event*))) == NULL)
234  goto error;
235  sop->event_w_by_fd = w_by_fd;
236 
237  memset((char *)sop->event_readset_in + sop->event_fdsz, 0,
238  fdsz - sop->event_fdsz);
239  memset((char *)sop->event_writeset_in + sop->event_fdsz, 0,
240  fdsz - sop->event_fdsz);
241  memset(sop->event_r_by_fd + n_events_old, 0,
242  (n_events-n_events_old) * sizeof(struct event*));
243  memset(sop->event_w_by_fd + n_events_old, 0,
244  (n_events-n_events_old) * sizeof(struct event*));
245 
246  sop->event_fdsz = fdsz;
247  check_selectop(sop);
248 
249  return (0);
250 
251  error:
252  event_warn("malloc");
253  return (-1);
254 }
255 
256 
257 static int
258 select_add(void *arg, struct event *ev)
259 {
260  struct selectop *sop = arg;
261 
262  if (ev->ev_events & EV_SIGNAL)
263  return (evsignal_add(ev));
264 
265  check_selectop(sop);
266  /*
267  * Keep track of the highest fd, so that we can calculate the size
268  * of the fd_sets for select(2)
269  */
270  if (sop->event_fds < ev->ev_fd) {
271  int fdsz = sop->event_fdsz;
272 
273  if (fdsz < sizeof(fd_mask))
274  fdsz = sizeof(fd_mask);
275 
276  while (fdsz <
277  (howmany(ev->ev_fd + 1, NFDBITS) * sizeof(fd_mask)))
278  fdsz *= 2;
279 
280  if (fdsz != sop->event_fdsz) {
281  if (select_resize(sop, fdsz)) {
282  check_selectop(sop);
283  return (-1);
284  }
285  }
286 
287  sop->event_fds = ev->ev_fd;
288  }
289 
290  if (ev->ev_events & EV_READ) {
291  FD_SET(ev->ev_fd, sop->event_readset_in);
292  sop->event_r_by_fd[ev->ev_fd] = ev;
293  }
294  if (ev->ev_events & EV_WRITE) {
295  FD_SET(ev->ev_fd, sop->event_writeset_in);
296  sop->event_w_by_fd[ev->ev_fd] = ev;
297  }
298  check_selectop(sop);
299 
300  return (0);
301 }
302 
303 /*
304  * Nothing to be done here.
305  */
306 
307 static int
308 select_del(void *arg, struct event *ev)
309 {
310  struct selectop *sop = arg;
311 
312  check_selectop(sop);
313  if (ev->ev_events & EV_SIGNAL)
314  return (evsignal_del(ev));
315 
316  if (sop->event_fds < ev->ev_fd) {
317  check_selectop(sop);
318  return (0);
319  }
320 
321  if (ev->ev_events & EV_READ) {
322  FD_CLR(ev->ev_fd, sop->event_readset_in);
323  sop->event_r_by_fd[ev->ev_fd] = NULL;
324  }
325 
326  if (ev->ev_events & EV_WRITE) {
327  FD_CLR(ev->ev_fd, sop->event_writeset_in);
328  sop->event_w_by_fd[ev->ev_fd] = NULL;
329  }
330 
331  check_selectop(sop);
332  return (0);
333 }
334 
335 static void
336 select_dealloc(struct event_base *base, void *arg)
337 {
338  struct selectop *sop = arg;
339 
340  evsignal_dealloc(base);
341  if (sop->event_readset_in)
342  free(sop->event_readset_in);
343  if (sop->event_writeset_in)
344  free(sop->event_writeset_in);
345  if (sop->event_readset_out)
346  free(sop->event_readset_out);
347  if (sop->event_writeset_out)
348  free(sop->event_writeset_out);
349  if (sop->event_r_by_fd)
350  free(sop->event_r_by_fd);
351  if (sop->event_w_by_fd)
352  free(sop->event_w_by_fd);
353 
354  memset(sop, 0, sizeof(struct selectop));
355  free(sop);
356 }