… | |
… | |
30 | */ |
30 | */ |
31 | |
31 | |
32 | /* for unix systems */ |
32 | /* for unix systems */ |
33 | #ifdef WIN32 |
33 | #ifdef WIN32 |
34 | typedef unsigned int uint32_t; |
34 | typedef unsigned int uint32_t; |
|
|
35 | # define EV_SELECT_USE_FD_SET 1 |
35 | #else |
36 | #else |
36 | # include <sys/select.h> |
37 | # include <sys/select.h> |
37 | # include <inttypes.h> |
38 | # include <inttypes.h> |
38 | #endif |
39 | #endif |
39 | |
40 | |
40 | #include <string.h> |
41 | #include <string.h> |
41 | |
42 | |
42 | static void |
43 | static void |
43 | select_modify (EV_P_ int fd, int oev, int nev) |
44 | select_modify (EV_P_ int fd, int oev, int nev) |
44 | { |
45 | { |
45 | int offs = fd >> 3; |
|
|
46 | int mask = 1 << (fd & 7); |
|
|
47 | |
|
|
48 | if (oev == nev) |
46 | if (oev == nev) |
49 | return; |
47 | return; |
50 | |
48 | |
51 | if (vec_max < (fd >> 5) + 1) |
49 | #if EV_SELECT_USE_FD_SET |
52 | { |
50 | if (nev & EV_READ) |
53 | int new_max = (fd >> 5) + 1; |
51 | FD_SET (fd, (struct fd_set *)vec_ri); |
|
|
52 | else |
|
|
53 | FD_CLR (fd, (struct fd_set *)vec_ri); |
54 | |
54 | |
55 | vec_ri = (unsigned char *)ev_realloc (vec_ri, new_max * 4); |
55 | if (nev & EV_WRITE) |
56 | vec_ro = (unsigned char *)ev_realloc (vec_ro, new_max * 4); /* could free/malloc */ |
56 | FD_SET (fd, (struct fd_set *)vec_wi); |
57 | vec_wi = (unsigned char *)ev_realloc (vec_wi, new_max * 4); |
57 | else |
58 | vec_wo = (unsigned char *)ev_realloc (vec_wo, new_max * 4); /* could free/malloc */ |
58 | FD_CLR (fd, (struct fd_set *)vec_wi); |
|
|
59 | #else |
|
|
60 | { |
|
|
61 | int offs = fd >> 3; |
|
|
62 | int mask = 1 << (fd & 7); |
59 | |
63 | |
60 | for (; vec_max < new_max; ++vec_max) |
64 | if (vec_max < (fd >> 5) + 1) |
61 | ((uint32_t *)vec_ri)[vec_max] = |
65 | { |
62 | ((uint32_t *)vec_wi)[vec_max] = 0; |
66 | int new_max = (fd >> 5) + 1; |
63 | } |
|
|
64 | |
67 | |
65 | vec_ri [offs] |= mask; |
68 | vec_ri = (unsigned char *)ev_realloc (vec_ri, new_max * 4); |
66 | if (!(nev & EV_READ)) |
69 | vec_ro = (unsigned char *)ev_realloc (vec_ro, new_max * 4); /* could free/malloc */ |
67 | vec_ri [offs] &= ~mask; |
70 | vec_wi = (unsigned char *)ev_realloc (vec_wi, new_max * 4); |
|
|
71 | vec_wo = (unsigned char *)ev_realloc (vec_wo, new_max * 4); /* could free/malloc */ |
68 | |
72 | |
|
|
73 | for (; vec_max < new_max; ++vec_max) |
|
|
74 | ((uint32_t *)vec_ri)[vec_max] = |
|
|
75 | ((uint32_t *)vec_wi)[vec_max] = 0; |
|
|
76 | } |
|
|
77 | |
|
|
78 | vec_ri [offs] |= mask; |
|
|
79 | if (!(nev & EV_READ)) |
|
|
80 | vec_ri [offs] &= ~mask; |
|
|
81 | |
69 | vec_wi [offs] |= mask; |
82 | vec_wi [offs] |= mask; |
70 | if (!(nev & EV_WRITE)) |
83 | if (!(nev & EV_WRITE)) |
71 | vec_wi [offs] &= ~mask; |
84 | vec_wi [offs] &= ~mask; |
|
|
85 | } |
|
|
86 | #endif |
72 | } |
87 | } |
73 | |
88 | |
74 | static void |
89 | static void |
75 | select_poll (EV_P_ ev_tstamp timeout) |
90 | select_poll (EV_P_ ev_tstamp timeout) |
76 | { |
91 | { |
77 | int word, offs; |
92 | int word, offs; |
78 | struct timeval tv; |
93 | struct timeval tv; |
79 | int res; |
94 | int res; |
80 | |
95 | |
|
|
96 | #if EV_SELECT_USE_FD_SET |
|
|
97 | memcpy (vec_ro, vec_ri, sizeof (struct fd_set)); |
|
|
98 | memcpy (vec_wo, vec_wi, sizeof (struct fd_set)); |
|
|
99 | #else |
81 | memcpy (vec_ro, vec_ri, vec_max * 4); |
100 | memcpy (vec_ro, vec_ri, vec_max * 4); |
82 | memcpy (vec_wo, vec_wi, vec_max * 4); |
101 | memcpy (vec_wo, vec_wi, vec_max * 4); |
|
|
102 | #endif |
83 | |
103 | |
84 | tv.tv_sec = (long)timeout; |
104 | tv.tv_sec = (long)timeout; |
85 | tv.tv_usec = (long)((timeout - (ev_tstamp)tv.tv_sec) * 1e6); |
105 | tv.tv_usec = (long)((timeout - (ev_tstamp)tv.tv_sec) * 1e6); |
86 | |
106 | |
87 | res = select (vec_max * 32, (fd_set *)vec_ro, (fd_set *)vec_wo, 0, &tv); |
107 | res = select (vec_max * 32, (fd_set *)vec_ro, (fd_set *)vec_wo, 0, &tv); |
88 | |
108 | |
89 | if (res < 0) |
109 | if (res < 0) |
90 | { |
110 | { |
|
|
111 | #ifdef WIN32 |
|
|
112 | if (errno == WSAEINTR ) errno = EINTR; |
|
|
113 | if (errno == WSAENOTSOCK) errno = EBADF; |
|
|
114 | #endif |
|
|
115 | |
91 | if (errno == EBADF) |
116 | if (errno == EBADF) |
92 | fd_ebadf (EV_A); |
117 | fd_ebadf (EV_A); |
93 | else if (errno == ENOMEM && !syserr_cb) |
118 | else if (errno == ENOMEM && !syserr_cb) |
94 | fd_enomem (EV_A); |
119 | fd_enomem (EV_A); |
95 | else if (errno != EINTR) |
120 | else if (errno != EINTR) |
96 | syserr ("(libev) select"); |
121 | syserr ("(libev) select"); |
97 | |
122 | |
98 | return; |
123 | return; |
99 | } |
124 | } |
100 | |
125 | |
|
|
126 | #if EV_SELECT_USE_FD_SET |
|
|
127 | for (word = 0; word < FD_SETSIZE; ++word) |
|
|
128 | { |
|
|
129 | int events = 0; |
|
|
130 | if (FD_ISSET (word, (struct fd_set *)vec_ro)) events |= EV_READ; |
|
|
131 | if (FD_ISSET (word, (struct fd_set *)vec_wo)) events |= EV_WRITE; |
|
|
132 | |
|
|
133 | if (events) |
|
|
134 | fd_event (EV_A_ word, events); |
|
|
135 | } |
|
|
136 | #else |
101 | for (word = vec_max; word--; ) |
137 | for (word = vec_max; word--; ) |
102 | { |
138 | { |
103 | if (((uint32_t *)vec_ro) [word] | ((uint32_t *)vec_wo) [word]) |
139 | if (((uint32_t *)vec_ro) [word] | ((uint32_t *)vec_wo) [word]) |
104 | for (offs = 4; offs--; ) |
140 | for (offs = 4; offs--; ) |
105 | { |
141 | { |
… | |
… | |
118 | if (events) |
154 | if (events) |
119 | fd_event (EV_A_ idx * 8 + bit, events); |
155 | fd_event (EV_A_ idx * 8 + bit, events); |
120 | } |
156 | } |
121 | } |
157 | } |
122 | } |
158 | } |
|
|
159 | #endif |
123 | } |
160 | } |
124 | |
161 | |
125 | static int |
162 | static int |
126 | select_init (EV_P_ int flags) |
163 | select_init (EV_P_ int flags) |
127 | { |
164 | { |
128 | method_fudge = 1e-2; /* needed to compensate for select returning early, very conservative */ |
165 | method_fudge = 1e-2; /* needed to compensate for select returning early, very conservative */ |
129 | method_modify = select_modify; |
166 | method_modify = select_modify; |
130 | method_poll = select_poll; |
167 | method_poll = select_poll; |
131 | |
168 | |
|
|
169 | #if EV_SELECT_USE_FD_SET |
|
|
170 | vec_max = FD_SETSIZE / 32; |
|
|
171 | vec_ri = ev_malloc (sizeof (struct fd_set)); FD_ZERO ((struct fd_set *)vec_ri); |
|
|
172 | vec_ro = ev_malloc (sizeof (struct fd_set)); |
|
|
173 | vec_wi = ev_malloc (sizeof (struct fd_set)); FD_ZERO ((struct fd_set *)vec_wi); |
|
|
174 | vec_wo = ev_malloc (sizeof (struct fd_set)); |
|
|
175 | #else |
132 | vec_max = 0; |
176 | vec_max = 0; |
133 | vec_ri = 0; |
177 | vec_ri = 0; |
134 | vec_ri = 0; |
178 | vec_ri = 0; |
135 | vec_wo = 0; |
179 | vec_wo = 0; |
136 | vec_wo = 0; |
180 | vec_wo = 0; |
|
|
181 | #endif |
137 | |
182 | |
138 | return EVMETHOD_SELECT; |
183 | return EVMETHOD_SELECT; |
139 | } |
184 | } |
140 | |
185 | |
141 | static void |
186 | static void |