2 * libev select fd activity backend
4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 /* for unix systems */
34 # include <sys/select.h>
35 # include <inttypes.h>
38 #ifndef EV_SELECT_USE_FD_SET
40 # define EV_SELECT_USE_FD_SET 0
42 # define EV_SELECT_USE_FD_SET 1
46 #if EV_SELECT_IS_WINSOCKET
47 # undef EV_SELECT_USE_FD_SET
48 # define EV_SELECT_USE_FD_SET 1
50 # define EINTR WSAEINTR
52 # define EBADF WSAENOTSOCK
54 # define ENOMEM (errno + 1)
57 #if !EV_SELECT_USE_FD_SET
58 # define NFDBYTES (NFDBITS / 8)
64 select_modify (EV_P_ int fd, int oev, int nev)
70 #if EV_SELECT_USE_FD_SET
72 #if EV_SELECT_IS_WINSOCKET
73 SOCKET handle = anfds [fd].handle;
79 FD_SET (handle, (fd_set *)vec_ri);
81 FD_CLR (handle, (fd_set *)vec_ri);
84 FD_SET (handle, (fd_set *)vec_wi);
86 FD_CLR (handle, (fd_set *)vec_wi);
90 int word = fd / NFDBITS;
91 int mask = 1UL << (fd % NFDBITS);
93 if (vec_max < word + 1)
95 int new_max = word + 1;
97 vec_ri = ev_realloc (vec_ri, new_max * NFDBYTES);
98 vec_ro = ev_realloc (vec_ro, new_max * NFDBYTES); /* could free/malloc */
99 vec_wi = ev_realloc (vec_wi, new_max * NFDBYTES);
100 vec_wo = ev_realloc (vec_wo, new_max * NFDBYTES); /* could free/malloc */
102 for (; vec_max < new_max; ++vec_max)
103 ((fd_mask *)vec_ri)[vec_max] =
104 ((fd_mask *)vec_wi)[vec_max] = 0;
107 ((fd_mask *)vec_ri) [word] |= mask;
108 if (!(nev & EV_READ))
109 ((fd_mask *)vec_ri) [word] &= ~mask;
111 ((fd_mask *)vec_wi) [word] |= mask;
112 if (!(nev & EV_WRITE))
113 ((fd_mask *)vec_wi) [word] &= ~mask;
119 select_poll (EV_P_ ev_tstamp timeout)
124 #if EV_SELECT_USE_FD_SET
125 memcpy (vec_ro, vec_ri, sizeof (fd_set));
126 memcpy (vec_wo, vec_wi, sizeof (fd_set));
128 memcpy (vec_ro, vec_ri, vec_max * NFDBYTES);
129 memcpy (vec_wo, vec_wi, vec_max * NFDBYTES);
132 tv.tv_sec = (long)timeout;
133 tv.tv_usec = (long)((timeout - (ev_tstamp)tv.tv_sec) * 1e6);
135 res = select (vec_max * NFDBITS, (fd_set *)vec_ro, (fd_set *)vec_wo, 0, &tv);
139 #if EV_SELECT_IS_WINSOCKET
140 errno = WSAGetLastError ();
145 else if (errno == ENOMEM && !syserr_cb)
147 else if (errno != EINTR)
148 syserr ("(libev) select");
153 #if EV_SELECT_USE_FD_SET
158 for (fd = 0; fd < anfdmax; ++fd)
159 if (anfds [fd].events)
162 #if EV_SELECT_IS_WINSOCKET
163 SOCKET handle = anfds [fd].handle;
168 if (FD_ISSET (handle, (fd_set *)vec_ro)) events |= EV_READ;
169 if (FD_ISSET (handle, (fd_set *)vec_wo)) events |= EV_WRITE;
172 fd_event (EV_A_ fd, events);
180 for (word = vec_max; word--; )
182 fd_mask word_r = ((fd_mask *)vec_ro) [word];
183 fd_mask word_w = ((fd_mask *)vec_wo) [word];
185 if (word_r || word_w)
186 for (bit = NFDBITS; bit--; )
188 fd_mask mask = 1UL << bit;
191 events |= word_r & mask ? EV_READ : 0;
192 events |= word_w & mask ? EV_WRITE : 0;
195 fd_event (EV_A_ word * NFDBITS + bit, events);
204 select_init (EV_P_ int flags)
206 method_fudge = 1e-2; /* needed to compensate for select returning early, very conservative */
207 method_modify = select_modify;
208 method_poll = select_poll;
210 #if EV_SELECT_USE_FD_SET
211 vec_max = FD_SETSIZE / 32;
212 vec_ri = ev_malloc (sizeof (fd_set)); FD_ZERO ((fd_set *)vec_ri);
213 vec_ro = ev_malloc (sizeof (fd_set));
214 vec_wi = ev_malloc (sizeof (fd_set)); FD_ZERO ((fd_set *)vec_wi);
215 vec_wo = ev_malloc (sizeof (fd_set));
224 return EVBACKEND_SELECT;
228 select_destroy (EV_P)