|
|
1 | /* for broken bsd's */ |
|
|
2 | #include <sys/time.h> |
|
|
3 | #include <sys/types.h> |
|
|
4 | #include <unistd.h> |
|
|
5 | |
|
|
6 | /* for unix systems */ |
|
|
7 | #include <sys/select.h> |
|
|
8 | |
|
|
9 | #include <string.h> |
|
|
10 | #include <inttypes.h> |
|
|
11 | |
|
|
12 | static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo; |
|
|
13 | static int vec_max; |
|
|
14 | |
|
|
15 | static void |
|
|
16 | select_modify (int fd, int oev, int nev) |
|
|
17 | { |
|
|
18 | int offs = fd >> 3; |
|
|
19 | int mask = 1 << (fd & 7); |
|
|
20 | |
|
|
21 | if (vec_max < (fd >> 5) + 1) |
|
|
22 | { |
|
|
23 | vec_max = (fd >> 5) + 1; |
|
|
24 | |
|
|
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 */ |
|
|
29 | } |
|
|
30 | |
|
|
31 | vec_ri [offs] |= mask; |
|
|
32 | if (!(nev & EV_READ)) |
|
|
33 | vec_ri [offs] &= ~mask; |
|
|
34 | |
|
|
35 | vec_wi [offs] |= mask; |
|
|
36 | if (!(nev & EV_WRITE)) |
|
|
37 | vec_wi [offs] &= ~mask; |
|
|
38 | } |
|
|
39 | |
|
|
40 | static void select_poll (ev_tstamp timeout) |
|
|
41 | { |
|
|
42 | struct timeval tv; |
|
|
43 | int res; |
|
|
44 | |
|
|
45 | memcpy (vec_ro, vec_ri, vec_max * 4); |
|
|
46 | memcpy (vec_wo, vec_wi, vec_max * 4); |
|
|
47 | |
|
|
48 | tv.tv_sec = (long)timeout; |
|
|
49 | tv.tv_usec = (long)((timeout - (ev_tstamp)tv.tv_sec) * 1e6); |
|
|
50 | |
|
|
51 | res = select (vec_max * 32, (fd_set *)vec_ro, (fd_set *)vec_wo, 0, &tv); |
|
|
52 | |
|
|
53 | if (res > 0) |
|
|
54 | { |
|
|
55 | int word, offs; |
|
|
56 | |
|
|
57 | for (word = vec_max; word--; ) |
|
|
58 | { |
|
|
59 | if (((uint32_t *)vec_ro) [word] | ((uint32_t *)vec_wo) [word]) |
|
|
60 | for (offs = 4; offs--; ) |
|
|
61 | { |
|
|
62 | int idx = word * 4 + offs; |
|
|
63 | unsigned char byte_r = vec_ro [idx]; |
|
|
64 | unsigned char byte_w = vec_wo [idx]; |
|
|
65 | int bit; |
|
|
66 | |
|
|
67 | if (byte_r | byte_w) |
|
|
68 | for (bit = 8; bit--; ) |
|
|
69 | { |
|
|
70 | int events = 0; |
|
|
71 | events |= byte_r & (1 << bit) ? EV_READ : 0; |
|
|
72 | events |= byte_w & (1 << bit) ? EV_WRITE : 0; |
|
|
73 | |
|
|
74 | if (events) |
|
|
75 | fd_event (idx * 8 + bit, events); |
|
|
76 | } |
|
|
77 | } |
|
|
78 | } |
|
|
79 | } |
|
|
80 | } |
|
|
81 | |
|
|
82 | int select_init (int flags) |
|
|
83 | { |
|
|
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; |
|
|
88 | |
|
|
89 | return 1; |
|
|
90 | } |
|
|
91 | |
|
|
92 | |