1 | /* |
1 | /* |
2 | * libev select fd activity backend |
2 | * libev select fd activity backend |
3 | * |
3 | * |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007,2008 Marc Alexander Lehmann <libev@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
… | |
… | |
52 | #endif |
52 | #endif |
53 | |
53 | |
54 | #if EV_SELECT_IS_WINSOCKET |
54 | #if EV_SELECT_IS_WINSOCKET |
55 | # undef EV_SELECT_USE_FD_SET |
55 | # undef EV_SELECT_USE_FD_SET |
56 | # define EV_SELECT_USE_FD_SET 1 |
56 | # define EV_SELECT_USE_FD_SET 1 |
57 | # undef EINTR |
|
|
58 | # define EINTR WSAEINTR |
|
|
59 | # undef EBADF |
|
|
60 | # define EBADF WSAENOTSOCK |
|
|
61 | # undef ENOMEM |
|
|
62 | # define ENOMEM (errno + 1) |
|
|
63 | #endif |
57 | #endif |
64 | |
58 | |
65 | #if !EV_SELECT_USE_FD_SET |
59 | #if !EV_SELECT_USE_FD_SET |
66 | # define NFDBYTES (NFDBITS / 8) |
60 | # define NFDBYTES (NFDBITS / 8) |
67 | #endif |
61 | #endif |
… | |
… | |
96 | #else |
90 | #else |
97 | |
91 | |
98 | int word = fd / NFDBITS; |
92 | int word = fd / NFDBITS; |
99 | fd_mask mask = 1UL << (fd % NFDBITS); |
93 | fd_mask mask = 1UL << (fd % NFDBITS); |
100 | |
94 | |
101 | if (expect_false (vec_max < word + 1)) |
95 | if (expect_false (vec_max <= word)) |
102 | { |
96 | { |
103 | int new_max = word + 1; |
97 | int new_max = word + 1; |
104 | |
98 | |
105 | vec_ri = ev_realloc (vec_ri, new_max * NFDBYTES); |
99 | vec_ri = ev_realloc (vec_ri, new_max * NFDBYTES); |
106 | vec_ro = ev_realloc (vec_ro, new_max * NFDBYTES); /* could free/malloc */ |
100 | vec_ro = ev_realloc (vec_ro, new_max * NFDBYTES); /* could free/malloc */ |
… | |
… | |
138 | #endif |
132 | #endif |
139 | |
133 | |
140 | tv.tv_sec = (long)timeout; |
134 | tv.tv_sec = (long)timeout; |
141 | tv.tv_usec = (long)((timeout - (ev_tstamp)tv.tv_sec) * 1e6); |
135 | tv.tv_usec = (long)((timeout - (ev_tstamp)tv.tv_sec) * 1e6); |
142 | |
136 | |
|
|
137 | #ifdef _WIN32 |
|
|
138 | /* pass in the write set as except set. |
|
|
139 | * the idea behind this is to work around a windows bug that causes |
|
|
140 | * errors to be reported as an exception and not by setting |
|
|
141 | * the writable bit. this is so uncontrollably lame. |
|
|
142 | */ |
|
|
143 | res = select (vec_max * NFDBITS, (fd_set *)vec_ro, (fd_set *)vec_wo, (fd_set *)vec_wo, &tv); |
|
|
144 | #else |
143 | res = select (vec_max * NFDBITS, (fd_set *)vec_ro, (fd_set *)vec_wo, 0, &tv); |
145 | res = select (vec_max * NFDBITS, (fd_set *)vec_ro, (fd_set *)vec_wo, 0, &tv); |
|
|
146 | #endif |
144 | |
147 | |
145 | if (expect_false (res < 0)) |
148 | if (expect_false (res < 0)) |
146 | { |
149 | { |
147 | #if EV_SELECT_IS_WINSOCKET |
150 | #if EV_SELECT_IS_WINSOCKET |
148 | errno = WSAGetLastError (); |
151 | errno = WSAGetLastError (); |
|
|
152 | #endif |
|
|
153 | #ifdef WSABASEERR |
|
|
154 | /* on windows, select returns incompatible error codes, fix this */ |
|
|
155 | if (errno >= WSABASEERR && errno < WSABASEERR + 1000) |
|
|
156 | if (errno == WSAENOTSOCK) |
|
|
157 | errno = EBADF; |
|
|
158 | else |
|
|
159 | errno -= WSABASEERR; |
|
|
160 | #endif |
|
|
161 | |
|
|
162 | #ifdef _WIN32 |
|
|
163 | /* select on windows errornously returns EINVAL when no fd sets have been |
|
|
164 | * provided (this is documented). what microsoft doesn't tell you that this bug |
|
|
165 | * exists even when the fd sets are provided, so we have to check for this bug |
|
|
166 | * here and emulate by sleeping manually. |
|
|
167 | * we also get EINVAL when the timeout is invalid, but we ignore this case here |
|
|
168 | * and assume that EINVAL always means: you have to wait manually. |
|
|
169 | */ |
|
|
170 | if (errno == EINVAL) |
|
|
171 | { |
|
|
172 | ev_sleep (timeout); |
|
|
173 | return; |
|
|
174 | } |
149 | #endif |
175 | #endif |
150 | |
176 | |
151 | if (errno == EBADF) |
177 | if (errno == EBADF) |
152 | fd_ebadf (EV_A); |
178 | fd_ebadf (EV_A); |
153 | else if (errno == ENOMEM && !syserr_cb) |
179 | else if (errno == ENOMEM && !syserr_cb) |