… | |
… | |
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) |
… | |
… | |
598 | |
541 | |
599 | if (!gotsig) |
542 | if (!gotsig) |
600 | { |
543 | { |
601 | int old_errno = errno; |
544 | int old_errno = errno; |
602 | gotsig = 1; |
545 | gotsig = 1; |
|
|
546 | #ifdef WIN32 |
|
|
547 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
548 | #else |
603 | write (sigpipe [1], &signum, 1); |
549 | write (sigpipe [1], &signum, 1); |
|
|
550 | #endif |
604 | errno = old_errno; |
551 | errno = old_errno; |
605 | } |
552 | } |
606 | } |
553 | } |
607 | |
554 | |
608 | static void |
555 | static void |
609 | sigcb (EV_P_ struct ev_io *iow, int revents) |
556 | sigcb (EV_P_ struct ev_io *iow, int revents) |
610 | { |
557 | { |
611 | WL w; |
558 | WL w; |
612 | int signum; |
559 | int signum; |
613 | |
560 | |
|
|
561 | #ifdef WIN32 |
|
|
562 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
563 | #else |
614 | read (sigpipe [0], &revents, 1); |
564 | read (sigpipe [0], &revents, 1); |
|
|
565 | #endif |
615 | gotsig = 0; |
566 | gotsig = 0; |
616 | |
567 | |
617 | for (signum = signalmax; signum--; ) |
568 | for (signum = signalmax; signum--; ) |
618 | if (signals [signum].gotsig) |
569 | if (signals [signum].gotsig) |
619 | { |
570 | { |
… | |
… | |
828 | ev_ref (EV_A); |
779 | ev_ref (EV_A); |
829 | ev_io_stop (EV_A_ &sigev); |
780 | ev_io_stop (EV_A_ &sigev); |
830 | close (sigpipe [0]); |
781 | close (sigpipe [0]); |
831 | close (sigpipe [1]); |
782 | close (sigpipe [1]); |
832 | |
783 | |
833 | while (ev_pipe (sigpipe)) |
784 | while (pipe (sigpipe)) |
834 | syserr ("(libev) error creating pipe"); |
785 | syserr ("(libev) error creating pipe"); |
835 | |
786 | |
836 | siginit (EV_A); |
787 | siginit (EV_A); |
837 | } |
788 | } |
838 | |
789 | |
… | |
… | |
881 | int |
832 | int |
882 | #endif |
833 | #endif |
883 | ev_default_loop (int methods) |
834 | ev_default_loop (int methods) |
884 | { |
835 | { |
885 | if (sigpipe [0] == sigpipe [1]) |
836 | if (sigpipe [0] == sigpipe [1]) |
886 | if (ev_pipe (sigpipe)) |
837 | if (pipe (sigpipe)) |
887 | return 0; |
838 | return 0; |
888 | |
839 | |
889 | if (!default_loop) |
840 | if (!default_loop) |
890 | { |
841 | { |
891 | #if EV_MULTIPLICITY |
842 | #if EV_MULTIPLICITY |
… | |
… | |
1266 | return; |
1217 | return; |
1267 | |
1218 | |
1268 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1219 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1269 | |
1220 | |
1270 | ev_start (EV_A_ (W)w, 1); |
1221 | ev_start (EV_A_ (W)w, 1); |
1271 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1222 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1272 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1223 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1273 | |
1224 | |
1274 | fd_change (EV_A_ fd); |
1225 | fd_change (EV_A_ fd); |
1275 | } |
1226 | } |
1276 | |
1227 | |
… | |
… | |
1296 | ((WT)w)->at += mn_now; |
1247 | ((WT)w)->at += mn_now; |
1297 | |
1248 | |
1298 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1249 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1299 | |
1250 | |
1300 | ev_start (EV_A_ (W)w, ++timercnt); |
1251 | ev_start (EV_A_ (W)w, ++timercnt); |
1301 | array_needsize (timers, timermax, timercnt, (void)); |
1252 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1302 | timers [timercnt - 1] = w; |
1253 | timers [timercnt - 1] = w; |
1303 | upheap ((WT *)timers, timercnt - 1); |
1254 | upheap ((WT *)timers, timercnt - 1); |
1304 | |
1255 | |
1305 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1256 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1306 | } |
1257 | } |
… | |
… | |
1353 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1304 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1354 | if (w->interval) |
1305 | if (w->interval) |
1355 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1306 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1356 | |
1307 | |
1357 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1308 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1358 | array_needsize (periodics, periodicmax, periodiccnt, (void)); |
1309 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1359 | periodics [periodiccnt - 1] = w; |
1310 | periodics [periodiccnt - 1] = w; |
1360 | upheap ((WT *)periodics, periodiccnt - 1); |
1311 | upheap ((WT *)periodics, periodiccnt - 1); |
1361 | |
1312 | |
1362 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1313 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1363 | } |
1314 | } |
… | |
… | |
1385 | { |
1336 | { |
1386 | if (ev_is_active (w)) |
1337 | if (ev_is_active (w)) |
1387 | return; |
1338 | return; |
1388 | |
1339 | |
1389 | ev_start (EV_A_ (W)w, ++idlecnt); |
1340 | ev_start (EV_A_ (W)w, ++idlecnt); |
1390 | array_needsize (idles, idlemax, idlecnt, (void)); |
1341 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1391 | idles [idlecnt - 1] = w; |
1342 | idles [idlecnt - 1] = w; |
1392 | } |
1343 | } |
1393 | |
1344 | |
1394 | void |
1345 | void |
1395 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1346 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1407 | { |
1358 | { |
1408 | if (ev_is_active (w)) |
1359 | if (ev_is_active (w)) |
1409 | return; |
1360 | return; |
1410 | |
1361 | |
1411 | ev_start (EV_A_ (W)w, ++preparecnt); |
1362 | ev_start (EV_A_ (W)w, ++preparecnt); |
1412 | array_needsize (prepares, preparemax, preparecnt, (void)); |
1363 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1413 | prepares [preparecnt - 1] = w; |
1364 | prepares [preparecnt - 1] = w; |
1414 | } |
1365 | } |
1415 | |
1366 | |
1416 | void |
1367 | void |
1417 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1368 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1429 | { |
1380 | { |
1430 | if (ev_is_active (w)) |
1381 | if (ev_is_active (w)) |
1431 | return; |
1382 | return; |
1432 | |
1383 | |
1433 | ev_start (EV_A_ (W)w, ++checkcnt); |
1384 | ev_start (EV_A_ (W)w, ++checkcnt); |
1434 | array_needsize (checks, checkmax, checkcnt, (void)); |
1385 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1435 | checks [checkcnt - 1] = w; |
1386 | checks [checkcnt - 1] = w; |
1436 | } |
1387 | } |
1437 | |
1388 | |
1438 | void |
1389 | void |
1439 | ev_check_stop (EV_P_ struct ev_check *w) |
1390 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1460 | return; |
1411 | return; |
1461 | |
1412 | |
1462 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1413 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1463 | |
1414 | |
1464 | ev_start (EV_A_ (W)w, 1); |
1415 | ev_start (EV_A_ (W)w, 1); |
1465 | array_needsize (signals, signalmax, w->signum, signals_init); |
1416 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1466 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1417 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1467 | |
1418 | |
1468 | if (!((WL)w)->next) |
1419 | if (!((WL)w)->next) |
1469 | { |
1420 | { |
1470 | #if WIN32 |
1421 | #if WIN32 |
… | |
… | |
1553 | } |
1504 | } |
1554 | |
1505 | |
1555 | void |
1506 | void |
1556 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1507 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1557 | { |
1508 | { |
1558 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1509 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1559 | |
1510 | |
1560 | if (!once) |
1511 | if (!once) |
1561 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1512 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1562 | else |
1513 | else |
1563 | { |
1514 | { |