… | |
… | |
148 | typedef struct ev_watcher_list *WL; |
148 | typedef struct ev_watcher_list *WL; |
149 | typedef struct ev_watcher_time *WT; |
149 | typedef struct ev_watcher_time *WT; |
150 | |
150 | |
151 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
151 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
152 | |
152 | |
153 | #if WIN32 |
153 | #include "ev_win32.c" |
154 | /* note: the comment below could not be substantiated, but what would I care */ |
|
|
155 | /* MSDN says this is required to handle SIGFPE */ |
|
|
156 | volatile double SIGFPE_REQ = 0.0f; |
|
|
157 | |
|
|
158 | static int |
|
|
159 | ev_socketpair_tcp (int filedes [2]) |
|
|
160 | { |
|
|
161 | struct sockaddr_in addr = { 0 }; |
|
|
162 | int addr_size = sizeof (addr); |
|
|
163 | SOCKET listener; |
|
|
164 | SOCKET sock [2] = { -1, -1 }; |
|
|
165 | |
|
|
166 | if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
|
|
167 | return -1; |
|
|
168 | |
|
|
169 | addr.sin_family = AF_INET; |
|
|
170 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
|
|
171 | addr.sin_port = 0; |
|
|
172 | |
|
|
173 | if (bind (listener, (struct sockaddr *)&addr, addr_size)) |
|
|
174 | goto fail; |
|
|
175 | |
|
|
176 | if (getsockname(listener, (struct sockaddr *)&addr, &addr_size)) |
|
|
177 | goto fail; |
|
|
178 | |
|
|
179 | if (listen (listener, 1)) |
|
|
180 | goto fail; |
|
|
181 | |
|
|
182 | if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
|
|
183 | goto fail; |
|
|
184 | |
|
|
185 | if (connect (sock[0], (struct sockaddr *)&addr, addr_size)) |
|
|
186 | goto fail; |
|
|
187 | |
|
|
188 | if ((sock[1] = accept (listener, 0, 0)) < 0) |
|
|
189 | goto fail; |
|
|
190 | |
|
|
191 | closesocket (listener); |
|
|
192 | |
|
|
193 | filedes [0] = sock [0]; |
|
|
194 | filedes [1] = sock [1]; |
|
|
195 | |
|
|
196 | return 0; |
|
|
197 | |
|
|
198 | fail: |
|
|
199 | closesocket (listener); |
|
|
200 | |
|
|
201 | if (sock [0] != INVALID_SOCKET) closesocket (sock [0]); |
|
|
202 | if (sock [1] != INVALID_SOCKET) closesocket (sock [1]); |
|
|
203 | |
|
|
204 | return -1; |
|
|
205 | } |
|
|
206 | |
|
|
207 | # define ev_pipe(filedes) ev_socketpair_tcp (filedes) |
|
|
208 | #else |
|
|
209 | # define ev_pipe(filedes) pipe (filedes) |
|
|
210 | #endif |
|
|
211 | |
154 | |
212 | /*****************************************************************************/ |
155 | /*****************************************************************************/ |
213 | |
156 | |
214 | static void (*syserr_cb)(const char *msg); |
157 | static void (*syserr_cb)(const char *msg); |
215 | |
158 | |
… | |
… | |
325 | ev_now (EV_P) |
268 | ev_now (EV_P) |
326 | { |
269 | { |
327 | return rt_now; |
270 | return rt_now; |
328 | } |
271 | } |
329 | |
272 | |
330 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
273 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
331 | |
274 | |
332 | #define array_needsize(base,cur,cnt,init) \ |
275 | #define array_needsize(type,base,cur,cnt,init) \ |
333 | if (expect_false ((cnt) > cur)) \ |
276 | if (expect_false ((cnt) > cur)) \ |
334 | { \ |
277 | { \ |
335 | int newcnt = cur; \ |
278 | int newcnt = cur; \ |
336 | do \ |
279 | do \ |
337 | { \ |
280 | { \ |
338 | newcnt = array_roundsize (base, newcnt << 1); \ |
281 | newcnt = array_roundsize (type, newcnt << 1); \ |
339 | } \ |
282 | } \ |
340 | while ((cnt) > newcnt); \ |
283 | while ((cnt) > newcnt); \ |
341 | \ |
284 | \ |
342 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
285 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
343 | init (base + cur, newcnt - cur); \ |
286 | init (base + cur, newcnt - cur); \ |
344 | cur = newcnt; \ |
287 | cur = newcnt; \ |
345 | } |
288 | } |
346 | |
289 | |
347 | #define array_slim(stem) \ |
290 | #define array_slim(type,stem) \ |
348 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
291 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
349 | { \ |
292 | { \ |
350 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
293 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
351 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
294 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
352 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
295 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
353 | } |
296 | } |
354 | |
297 | |
355 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
298 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
356 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
299 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
… | |
… | |
383 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
326 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
384 | return; |
327 | return; |
385 | } |
328 | } |
386 | |
329 | |
387 | w->pending = ++pendingcnt [ABSPRI (w)]; |
330 | w->pending = ++pendingcnt [ABSPRI (w)]; |
388 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
331 | array_needsize (ANPENDING, pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
389 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
332 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
390 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
333 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
391 | } |
334 | } |
392 | |
335 | |
393 | static void |
336 | static void |
… | |
… | |
448 | return; |
391 | return; |
449 | |
392 | |
450 | anfds [fd].reify = 1; |
393 | anfds [fd].reify = 1; |
451 | |
394 | |
452 | ++fdchangecnt; |
395 | ++fdchangecnt; |
453 | array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); |
396 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
454 | fdchanges [fdchangecnt - 1] = fd; |
397 | fdchanges [fdchangecnt - 1] = fd; |
455 | } |
398 | } |
456 | |
399 | |
457 | static void |
400 | static void |
458 | fd_kill (EV_P_ int fd) |
401 | fd_kill (EV_P_ int fd) |
… | |
… | |
828 | ev_ref (EV_A); |
771 | ev_ref (EV_A); |
829 | ev_io_stop (EV_A_ &sigev); |
772 | ev_io_stop (EV_A_ &sigev); |
830 | close (sigpipe [0]); |
773 | close (sigpipe [0]); |
831 | close (sigpipe [1]); |
774 | close (sigpipe [1]); |
832 | |
775 | |
833 | while (ev_pipe (sigpipe)) |
776 | while (pipe (sigpipe)) |
834 | syserr ("(libev) error creating pipe"); |
777 | syserr ("(libev) error creating pipe"); |
835 | |
778 | |
836 | siginit (EV_A); |
779 | siginit (EV_A); |
837 | } |
780 | } |
838 | |
781 | |
… | |
… | |
881 | int |
824 | int |
882 | #endif |
825 | #endif |
883 | ev_default_loop (int methods) |
826 | ev_default_loop (int methods) |
884 | { |
827 | { |
885 | if (sigpipe [0] == sigpipe [1]) |
828 | if (sigpipe [0] == sigpipe [1]) |
886 | if (ev_pipe (sigpipe)) |
829 | if (pipe (sigpipe)) |
887 | return 0; |
830 | return 0; |
888 | |
831 | |
889 | if (!default_loop) |
832 | if (!default_loop) |
890 | { |
833 | { |
891 | #if EV_MULTIPLICITY |
834 | #if EV_MULTIPLICITY |
… | |
… | |
1266 | return; |
1209 | return; |
1267 | |
1210 | |
1268 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1211 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1269 | |
1212 | |
1270 | ev_start (EV_A_ (W)w, 1); |
1213 | ev_start (EV_A_ (W)w, 1); |
1271 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1214 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1272 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1215 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1273 | |
1216 | |
1274 | fd_change (EV_A_ fd); |
1217 | fd_change (EV_A_ fd); |
1275 | } |
1218 | } |
1276 | |
1219 | |
… | |
… | |
1296 | ((WT)w)->at += mn_now; |
1239 | ((WT)w)->at += mn_now; |
1297 | |
1240 | |
1298 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1241 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1299 | |
1242 | |
1300 | ev_start (EV_A_ (W)w, ++timercnt); |
1243 | ev_start (EV_A_ (W)w, ++timercnt); |
1301 | array_needsize (timers, timermax, timercnt, (void)); |
1244 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1302 | timers [timercnt - 1] = w; |
1245 | timers [timercnt - 1] = w; |
1303 | upheap ((WT *)timers, timercnt - 1); |
1246 | upheap ((WT *)timers, timercnt - 1); |
1304 | |
1247 | |
1305 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1248 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1306 | } |
1249 | } |
… | |
… | |
1353 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1296 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1354 | if (w->interval) |
1297 | if (w->interval) |
1355 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1298 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1356 | |
1299 | |
1357 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1300 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1358 | array_needsize (periodics, periodicmax, periodiccnt, (void)); |
1301 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1359 | periodics [periodiccnt - 1] = w; |
1302 | periodics [periodiccnt - 1] = w; |
1360 | upheap ((WT *)periodics, periodiccnt - 1); |
1303 | upheap ((WT *)periodics, periodiccnt - 1); |
1361 | |
1304 | |
1362 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1305 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1363 | } |
1306 | } |
… | |
… | |
1385 | { |
1328 | { |
1386 | if (ev_is_active (w)) |
1329 | if (ev_is_active (w)) |
1387 | return; |
1330 | return; |
1388 | |
1331 | |
1389 | ev_start (EV_A_ (W)w, ++idlecnt); |
1332 | ev_start (EV_A_ (W)w, ++idlecnt); |
1390 | array_needsize (idles, idlemax, idlecnt, (void)); |
1333 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1391 | idles [idlecnt - 1] = w; |
1334 | idles [idlecnt - 1] = w; |
1392 | } |
1335 | } |
1393 | |
1336 | |
1394 | void |
1337 | void |
1395 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1338 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1407 | { |
1350 | { |
1408 | if (ev_is_active (w)) |
1351 | if (ev_is_active (w)) |
1409 | return; |
1352 | return; |
1410 | |
1353 | |
1411 | ev_start (EV_A_ (W)w, ++preparecnt); |
1354 | ev_start (EV_A_ (W)w, ++preparecnt); |
1412 | array_needsize (prepares, preparemax, preparecnt, (void)); |
1355 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1413 | prepares [preparecnt - 1] = w; |
1356 | prepares [preparecnt - 1] = w; |
1414 | } |
1357 | } |
1415 | |
1358 | |
1416 | void |
1359 | void |
1417 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1360 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1429 | { |
1372 | { |
1430 | if (ev_is_active (w)) |
1373 | if (ev_is_active (w)) |
1431 | return; |
1374 | return; |
1432 | |
1375 | |
1433 | ev_start (EV_A_ (W)w, ++checkcnt); |
1376 | ev_start (EV_A_ (W)w, ++checkcnt); |
1434 | array_needsize (checks, checkmax, checkcnt, (void)); |
1377 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1435 | checks [checkcnt - 1] = w; |
1378 | checks [checkcnt - 1] = w; |
1436 | } |
1379 | } |
1437 | |
1380 | |
1438 | void |
1381 | void |
1439 | ev_check_stop (EV_P_ struct ev_check *w) |
1382 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1460 | return; |
1403 | return; |
1461 | |
1404 | |
1462 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1405 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1463 | |
1406 | |
1464 | ev_start (EV_A_ (W)w, 1); |
1407 | ev_start (EV_A_ (W)w, 1); |
1465 | array_needsize (signals, signalmax, w->signum, signals_init); |
1408 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1466 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1409 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1467 | |
1410 | |
1468 | if (!((WL)w)->next) |
1411 | if (!((WL)w)->next) |
1469 | { |
1412 | { |
1470 | #if WIN32 |
1413 | #if WIN32 |
… | |
… | |
1553 | } |
1496 | } |
1554 | |
1497 | |
1555 | void |
1498 | void |
1556 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1499 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1557 | { |
1500 | { |
1558 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1501 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1559 | |
1502 | |
1560 | if (!once) |
1503 | if (!once) |
1561 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1504 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1562 | else |
1505 | else |
1563 | { |
1506 | { |