ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_select.c
(Generate patch)

Comparing libev/ev_select.c (file contents):
Revision 1.15 by root, Tue Nov 6 00:52:33 2007 UTC vs.
Revision 1.18 by root, Wed Nov 7 13:27:56 2007 UTC

27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32/* for broken bsd's */
33#include <sys/time.h>
34#include <sys/types.h>
35#include <unistd.h>
36
37/* for unix systems */ 32/* for unix systems */
38#ifndef WIN32 33#if WIN32
34typedef unsigned int uint32_t;
35# ifndef EV_SELECT_USE_FD_SET
36# define EV_SELECT_USE_FD_SET 1
37# endif
38#else
39# include <sys/select.h> 39# include <sys/select.h>
40# include <inttypes.h>
41#endif
42
43#if EV_SELECT_USE_WIN32_HANDLES
44# undef EV_SELECT_USE_FD_SET
45# define EV_SELECT_USE_FD_SET 1
46#else
40#endif 47#endif
41 48
42#include <string.h> 49#include <string.h>
43#include <inttypes.h>
44 50
45static void 51static void
46select_modify (EV_P_ int fd, int oev, int nev) 52select_modify (EV_P_ int fd, int oev, int nev)
47{ 53{
48 int offs = fd >> 3;
49 int mask = 1 << (fd & 7);
50
51 if (oev == nev) 54 if (oev == nev)
52 return; 55 return;
53 56
57#if EV_SELECT_USE_FD_SET
58# if EV_SELECT_USE_WIN32_HANDLES
59 fd = _get_osfhandle (fd);
60 if (fd < 0)
61 return;
62# endif
63
64 if (nev & EV_READ)
65 FD_SET (fd, (struct fd_set *)vec_ri);
66 else
67 FD_CLR (fd, (struct fd_set *)vec_ri);
68
69 if (nev & EV_WRITE)
70 FD_SET (fd, (struct fd_set *)vec_wi);
71 else
72 FD_CLR (fd, (struct fd_set *)vec_wi);
73#else
74 {
75 int offs = fd >> 3;
76 int mask = 1 << (fd & 7);
77
54 if (vec_max < (fd >> 5) + 1) 78 if (vec_max < (fd >> 5) + 1)
55 { 79 {
56 int new_max = (fd >> 5) + 1; 80 int new_max = (fd >> 5) + 1;
57 81
58 vec_ri = (unsigned char *)ev_realloc (vec_ri, new_max * 4); 82 vec_ri = (unsigned char *)ev_realloc (vec_ri, new_max * 4);
59 vec_ro = (unsigned char *)ev_realloc (vec_ro, new_max * 4); /* could free/malloc */ 83 vec_ro = (unsigned char *)ev_realloc (vec_ro, new_max * 4); /* could free/malloc */
60 vec_wi = (unsigned char *)ev_realloc (vec_wi, new_max * 4); 84 vec_wi = (unsigned char *)ev_realloc (vec_wi, new_max * 4);
61 vec_wo = (unsigned char *)ev_realloc (vec_wo, new_max * 4); /* could free/malloc */ 85 vec_wo = (unsigned char *)ev_realloc (vec_wo, new_max * 4); /* could free/malloc */
62 86
63 for (; vec_max < new_max; ++vec_max) 87 for (; vec_max < new_max; ++vec_max)
64 ((uint32_t *)vec_ri)[vec_max] = 88 ((uint32_t *)vec_ri)[vec_max] =
65 ((uint32_t *)vec_wi)[vec_max] = 0; 89 ((uint32_t *)vec_wi)[vec_max] = 0;
66 } 90 }
67 91
68 vec_ri [offs] |= mask; 92 vec_ri [offs] |= mask;
69 if (!(nev & EV_READ)) 93 if (!(nev & EV_READ))
70 vec_ri [offs] &= ~mask; 94 vec_ri [offs] &= ~mask;
71 95
72 vec_wi [offs] |= mask; 96 vec_wi [offs] |= mask;
73 if (!(nev & EV_WRITE)) 97 if (!(nev & EV_WRITE))
74 vec_wi [offs] &= ~mask; 98 vec_wi [offs] &= ~mask;
99 }
100#endif
75} 101}
76 102
77static void 103static void
78select_poll (EV_P_ ev_tstamp timeout) 104select_poll (EV_P_ ev_tstamp timeout)
79{ 105{
80 int word, offs; 106 int word, offs;
81 struct timeval tv; 107 struct timeval tv;
82 int res; 108 int res;
83 109
110#if EV_SELECT_USE_FD_SET
111 memcpy (vec_ro, vec_ri, sizeof (struct fd_set));
112 memcpy (vec_wo, vec_wi, sizeof (struct fd_set));
113#else
84 memcpy (vec_ro, vec_ri, vec_max * 4); 114 memcpy (vec_ro, vec_ri, vec_max * 4);
85 memcpy (vec_wo, vec_wi, vec_max * 4); 115 memcpy (vec_wo, vec_wi, vec_max * 4);
116#endif
86 117
87 tv.tv_sec = (long)timeout; 118 tv.tv_sec = (long)timeout;
88 tv.tv_usec = (long)((timeout - (ev_tstamp)tv.tv_sec) * 1e6); 119 tv.tv_usec = (long)((timeout - (ev_tstamp)tv.tv_sec) * 1e6);
89 120
90 res = select (vec_max * 32, (fd_set *)vec_ro, (fd_set *)vec_wo, 0, &tv); 121 res = select (vec_max * 32, (fd_set *)vec_ro, (fd_set *)vec_wo, 0, &tv);
91 122
92 if (res < 0) 123 if (res < 0)
93 { 124 {
125#ifdef WIN32
126 if (errno == WSAEINTR ) errno = EINTR;
127 if (errno == WSAENOTSOCK) errno = EBADF;
128#endif
129
94 if (errno == EBADF) 130 if (errno == EBADF)
95 fd_ebadf (EV_A); 131 fd_ebadf (EV_A);
96 else if (errno == ENOMEM && !syserr_cb) 132 else if (errno == ENOMEM && !syserr_cb)
97 fd_enomem (EV_A); 133 fd_enomem (EV_A);
98 else if (errno != EINTR) 134 else if (errno != EINTR)
99 syserr ("(libev) select"); 135 syserr ("(libev) select");
100 136
101 return; 137 return;
102 } 138 }
103 139
140#if EV_SELECT_USE_FD_SET
141# if EV_SELECT_USE_WIN32_HANDLES
142 for (word = 0; word < anfdmax; ++word)
143 {
144 if (!anfd [word].events)
145 {
146 int fd = _get_osfhandle (word);
147
148 if (fd >= 0)
149 {
150 int events = 0;
151
152 if (FD_ISSET (fd, (struct fd_set *)vec_ro)) events |= EV_READ;
153 if (FD_ISSET (fd, (struct fd_set *)vec_wo)) events |= EV_WRITE;
154
155 if (events)
156 fd_event (EV_A_ word, events);
157 }
158 }
159 }
160# else
161 for (word = 0; word < FD_SETSIZE; ++word)
162 {
163 int events = 0;
164 if (FD_ISSET (word, (struct fd_set *)vec_ro)) events |= EV_READ;
165 if (FD_ISSET (word, (struct fd_set *)vec_wo)) events |= EV_WRITE;
166
167 if (events)
168 fd_event (EV_A_ word, events);
169 }
170# endif
171#else
104 for (word = vec_max; word--; ) 172 for (word = vec_max; word--; )
105 { 173 {
106 if (((uint32_t *)vec_ro) [word] | ((uint32_t *)vec_wo) [word]) 174 if (((uint32_t *)vec_ro) [word] | ((uint32_t *)vec_wo) [word])
107 for (offs = 4; offs--; ) 175 for (offs = 4; offs--; )
108 { 176 {
121 if (events) 189 if (events)
122 fd_event (EV_A_ idx * 8 + bit, events); 190 fd_event (EV_A_ idx * 8 + bit, events);
123 } 191 }
124 } 192 }
125 } 193 }
194#endif
126} 195}
127 196
128static int 197static int
129select_init (EV_P_ int flags) 198select_init (EV_P_ int flags)
130{ 199{
131 method_fudge = 1e-2; /* needed to compensate for select returning early, very conservative */ 200 method_fudge = 1e-2; /* needed to compensate for select returning early, very conservative */
132 method_modify = select_modify; 201 method_modify = select_modify;
133 method_poll = select_poll; 202 method_poll = select_poll;
134 203
204#if EV_SELECT_USE_FD_SET
205 vec_max = FD_SETSIZE / 32;
206 vec_ri = ev_malloc (sizeof (struct fd_set)); FD_ZERO ((struct fd_set *)vec_ri);
207 vec_ro = ev_malloc (sizeof (struct fd_set));
208 vec_wi = ev_malloc (sizeof (struct fd_set)); FD_ZERO ((struct fd_set *)vec_wi);
209 vec_wo = ev_malloc (sizeof (struct fd_set));
210#else
135 vec_max = 0; 211 vec_max = 0;
136 vec_ri = 0; 212 vec_ri = 0;
137 vec_ri = 0; 213 vec_ri = 0;
138 vec_wo = 0; 214 vec_wo = 0;
139 vec_wo = 0; 215 vec_wo = 0;
216#endif
140 217
141 return EVMETHOD_SELECT; 218 return EVMETHOD_SELECT;
142} 219}
143 220
144static void 221static void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines