… | |
… | |
209 | /* taken almost verbatim from libev's ev_win32.c */ |
209 | /* taken almost verbatim from libev's ev_win32.c */ |
210 | /* oh, the humanity! */ |
210 | /* oh, the humanity! */ |
211 | static int |
211 | static int |
212 | s_pipe (int filedes [2]) |
212 | s_pipe (int filedes [2]) |
213 | { |
213 | { |
|
|
214 | dTHX; |
|
|
215 | |
214 | struct sockaddr_in addr = { 0 }; |
216 | struct sockaddr_in addr = { 0 }; |
215 | int addr_size = sizeof (addr); |
217 | int addr_size = sizeof (addr); |
216 | struct sockaddr_in adr2; |
218 | struct sockaddr_in adr2; |
217 | int adr2_size = sizeof (adr2); |
219 | int adr2_size = sizeof (adr2); |
218 | SOCKET listener; |
220 | SOCKET listener; |
… | |
… | |
337 | #endif |
339 | #endif |
338 | |
340 | |
339 | typedef struct { |
341 | typedef struct { |
340 | int fd[2]; /* read, write fd, might be equal */ |
342 | int fd[2]; /* read, write fd, might be equal */ |
341 | int len; /* write length (1 pipe/socket, 8 eventfd) */ |
343 | int len; /* write length (1 pipe/socket, 8 eventfd) */ |
342 | volatile sig_atomic_t sent; |
|
|
343 | } s_epipe; |
344 | } s_epipe; |
344 | |
345 | |
345 | static int |
346 | static int |
346 | s_epipe_new (s_epipe *epp) |
347 | s_epipe_new (s_epipe *epp) |
347 | { |
348 | { |
… | |
… | |
360 | return -1; |
361 | return -1; |
361 | |
362 | |
362 | if (s_fd_prepare (ep.fd [0]) |
363 | if (s_fd_prepare (ep.fd [0]) |
363 | || s_fd_prepare (ep.fd [1])) |
364 | || s_fd_prepare (ep.fd [1])) |
364 | { |
365 | { |
|
|
366 | dTHX; |
|
|
367 | |
365 | close (ep.fd [0]); |
368 | close (ep.fd [0]); |
366 | close (ep.fd [1]); |
369 | close (ep.fd [1]); |
367 | return -1; |
370 | return -1; |
368 | } |
371 | } |
369 | |
372 | |
370 | ep.len = 1; |
373 | ep.len = 1; |
371 | } |
374 | } |
372 | |
375 | |
373 | ep.sent = 0; |
|
|
374 | *epp = ep; |
376 | *epp = ep; |
375 | return 0; |
377 | return 0; |
376 | } |
378 | } |
377 | |
379 | |
378 | static void |
380 | static void |
379 | s_epipe_destroy (s_epipe *epp) |
381 | s_epipe_destroy (s_epipe *epp) |
380 | { |
382 | { |
|
|
383 | dTHX; |
|
|
384 | |
381 | close (epp->fd [0]); |
385 | close (epp->fd [0]); |
382 | |
386 | |
383 | if (epp->fd [1] != epp->fd [0]) |
387 | if (epp->fd [1] != epp->fd [0]) |
384 | close (epp->fd [1]); |
388 | close (epp->fd [1]); |
385 | |
389 | |
… | |
… | |
387 | } |
391 | } |
388 | |
392 | |
389 | static void |
393 | static void |
390 | s_epipe_signal (s_epipe *epp) |
394 | s_epipe_signal (s_epipe *epp) |
391 | { |
395 | { |
392 | if (epp->sent) |
|
|
393 | return; |
|
|
394 | |
|
|
395 | epp->sent = 1; |
|
|
396 | { |
|
|
397 | #ifdef _WIN32 |
396 | #ifdef _WIN32 |
398 | /* perl overrides send with a function that crashes in other threads. |
397 | /* perl overrides send with a function that crashes in other threads. |
399 | * unfortunately, it overrides it with an argument-less macro, so |
398 | * unfortunately, it overrides it with an argument-less macro, so |
400 | * there is no way to force usage of the real send function. |
399 | * there is no way to force usage of the real send function. |
401 | * incompetent windows programmers - is this redundant? |
400 | * incompetent windows programmers - is this redundant? |
402 | */ |
401 | */ |
403 | DWORD dummy; |
402 | DWORD dummy; |
404 | WriteFile (S_TO_HANDLE (epp->fd [1]), (LPCVOID)&dummy, 1, &dummy, 0); |
403 | WriteFile (S_TO_HANDLE (epp->fd [1]), (LPCVOID)&dummy, 1, &dummy, 0); |
405 | #else |
404 | #else |
406 | static uint64_t counter = 1; |
405 | static uint64_t counter = 1; |
407 | /* some modules accept fd's from outside, support eventfd here */ |
406 | /* some modules accept fd's from outside, support eventfd here */ |
408 | if (write (epp->fd [1], &counter, epp->len) < 0 |
407 | if (write (epp->fd [1], &counter, epp->len) < 0 |
409 | && errno == EINVAL |
408 | && errno == EINVAL |
410 | && epp->len != 8) |
409 | && epp->len != 8) |
411 | write (epp->fd [1], &counter, (epp->len = 8)); |
410 | write (epp->fd [1], &counter, (epp->len = 8)); |
412 | #endif |
411 | #endif |
413 | } |
|
|
414 | } |
412 | } |
415 | |
413 | |
416 | static void |
414 | static void |
417 | s_epipe_drain (s_epipe *epp) |
415 | s_epipe_drain (s_epipe *epp) |
418 | { |
416 | { |
|
|
417 | dTHX; |
419 | char buf [9]; |
418 | char buf [9]; |
420 | |
419 | |
421 | #ifdef _WIN32 |
420 | #ifdef _WIN32 |
422 | recv (epp->fd [0], buf, sizeof (buf), 0); |
421 | recv (epp->fd [0], buf, sizeof (buf), 0); |
423 | #else |
422 | #else |
424 | read (epp->fd [0], buf, sizeof (buf)); |
423 | read (epp->fd [0], buf, sizeof (buf)); |
425 | #endif |
424 | #endif |
426 | |
|
|
427 | epp->sent = 0; |
|
|
428 | } |
425 | } |
429 | |
426 | |
430 | /* like new, but dups over old */ |
427 | /* like new, but dups over old */ |
431 | static int |
428 | static int |
432 | s_epipe_renew (s_epipe *epp) |
429 | s_epipe_renew (s_epipe *epp) |
433 | { |
430 | { |
|
|
431 | dTHX; |
434 | s_epipe epn; |
432 | s_epipe epn; |
435 | |
433 | |
436 | if (epp->fd [1] != epp->fd [0]) |
434 | if (epp->fd [1] != epp->fd [0]) |
437 | close (epp->fd [1]); |
435 | close (epp->fd [1]); |
438 | |
436 | |
… | |
… | |
458 | #define s_epipe_fd(epp) ((epp)->fd [0]) |
456 | #define s_epipe_fd(epp) ((epp)->fd [0]) |
459 | |
457 | |
460 | static int |
458 | static int |
461 | s_epipe_wait (s_epipe *epp) |
459 | s_epipe_wait (s_epipe *epp) |
462 | { |
460 | { |
|
|
461 | dTHX; |
463 | #ifdef _WIN32 |
462 | #ifdef _WIN32 |
464 | fd_set rfd; |
463 | fd_set rfd; |
465 | int fd = s_epipe_fd (epp); |
464 | int fd = s_epipe_fd (epp); |
466 | |
465 | |
467 | FD_ZERO (&rfd); |
466 | FD_ZERO (&rfd); |