7 #include <sys/select.h>
12 static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo;
16 select_modify (int fd, int oev, int nev)
19 int mask = 1 << (fd & 7);
21 if (vec_max < (fd >> 5) + 1)
23 vec_max = (fd >> 5) + 1;
25 vec_ri = (unsigned char *)realloc (vec_ri, vec_max * 4);
26 vec_ro = (unsigned char *)realloc (vec_ro, vec_max * 4); /* could free/malloc */
27 vec_wi = (unsigned char *)realloc (vec_wi, vec_max * 4);
28 vec_wo = (unsigned char *)realloc (vec_wo, vec_max * 4); /* could free/malloc */
31 vec_ri [offs] |= mask;
33 vec_ri [offs] &= ~mask;
35 vec_wi [offs] |= mask;
36 if (!(nev & EV_WRITE))
37 vec_wi [offs] &= ~mask;
40 static void select_poll (ev_tstamp timeout)
45 memcpy (vec_ro, vec_ri, vec_max * 4);
46 memcpy (vec_wo, vec_wi, vec_max * 4);
48 tv.tv_sec = (long)timeout;
49 tv.tv_usec = (long)((timeout - (ev_tstamp)tv.tv_sec) * 1e6);
51 res = select (vec_max * 32, (fd_set *)vec_ro, (fd_set *)vec_wo, 0, &tv);
57 for (word = vec_max; word--; )
59 if (((uint32_t *)vec_ro) [word] | ((uint32_t *)vec_wo) [word])
60 for (offs = 4; offs--; )
62 int idx = word * 4 + offs;
63 unsigned char byte_r = vec_ro [idx];
64 unsigned char byte_w = vec_wo [idx];
68 for (bit = 8; bit--; )
71 events |= byte_r & (1 << bit) ? EV_READ : 0;
72 events |= byte_w & (1 << bit) ? EV_WRITE : 0;
75 fd_event (idx * 8 + bit, events);
82 void select_init (int flags)
84 ev_method = EVMETHOD_SELECT;
85 method_fudge = 1e-2; /* needed to compensate for select returning early, very conservative */
86 method_modify = select_modify;
87 method_poll = select_poll;