… | |
… | |
4 | /* WARNING |
4 | /* WARNING |
5 | * This header file is a shared resource between many modules. |
5 | * This header file is a shared resource between many modules. |
6 | */ |
6 | */ |
7 | |
7 | |
8 | #include <signal.h> |
8 | #include <signal.h> |
|
|
9 | #include <errno.h> |
9 | |
10 | |
10 | #ifndef _WIN32 |
11 | #ifndef _WIN32 |
11 | # include <poll.h> |
12 | # include <poll.h> |
12 | #endif |
13 | #endif |
13 | |
14 | |
… | |
… | |
193 | S_GENSUB_ARG = arg; |
194 | S_GENSUB_ARG = arg; |
194 | |
195 | |
195 | return newRV_noinc ((SV *)cv); |
196 | return newRV_noinc ((SV *)cv); |
196 | } |
197 | } |
197 | |
198 | |
|
|
199 | /*****************************************************************************/ |
198 | /** portable pipe/socketpair */ |
200 | /* portable pipe/socketpair */ |
199 | |
201 | |
200 | #ifdef USE_SOCKETS_AS_HANDLES |
202 | #ifdef USE_SOCKETS_AS_HANDLES |
201 | # define S_TO_SOCKET(x) (win32_get_osfhandle (x)) |
203 | # define S_TO_HANDLE(x) ((HANDLE)win32_get_osfhandle (x)) |
202 | #else |
204 | #else |
203 | # define S_TO_SOCKET(x) (x) |
205 | # define S_TO_HANDLE(x) ((HANDLE)x) |
204 | #endif |
206 | #endif |
205 | |
207 | |
206 | #ifdef _WIN32 |
208 | #ifdef _WIN32 |
207 | /* taken almost verbatim from libev's ev_win32.c */ |
209 | /* taken almost verbatim from libev's ev_win32.c */ |
208 | /* oh, the humanity! */ |
210 | /* oh, the humanity! */ |
… | |
… | |
291 | #define s_socketpair(domain,type,protocol,filedes) s_pipe (filedes) |
293 | #define s_socketpair(domain,type,protocol,filedes) s_pipe (filedes) |
292 | |
294 | |
293 | static int |
295 | static int |
294 | s_fd_blocking (int fd, int blocking) |
296 | s_fd_blocking (int fd, int blocking) |
295 | { |
297 | { |
296 | blocking = !blocking; |
298 | u_long nonblocking = !blocking; |
297 | |
299 | |
298 | return ioctlsocket (S_TO_SOCKET (fd), FIONBIO, &blocking); |
300 | return ioctlsocket ((SOCKET)S_TO_HANDLE (fd), FIONBIO, &nonblocking); |
299 | } |
301 | } |
300 | |
302 | |
301 | #define s_fd_prepare(fd) s_fd_blocking (fd, 0) |
303 | #define s_fd_prepare(fd) s_fd_blocking (fd, 0) |
302 | |
304 | |
303 | #else |
305 | #else |
… | |
… | |
389 | { |
391 | { |
390 | if (epp->sent) |
392 | if (epp->sent) |
391 | return; |
393 | return; |
392 | |
394 | |
393 | epp->sent = 1; |
395 | epp->sent = 1; |
|
|
396 | { |
394 | #ifdef _WIN32 |
397 | #ifdef _WIN32 |
395 | send (epp->fd [1], epp, 1); |
398 | /* perl overrides send with a function that crashes in other threads. |
|
|
399 | * unfortunately, it overrides it with an argument-less macro, so |
|
|
400 | * there is no way to force usage of the real send function. |
|
|
401 | * incompetent windows programmers - is this redundant? |
|
|
402 | */ |
|
|
403 | DWORD dummy; |
|
|
404 | WriteFile (S_TO_HANDLE (epp->fd [1]), (LPCVOID)&dummy, 1, &dummy, 0); |
396 | #else |
405 | #else |
397 | static uint64_t counter = 1; |
406 | static uint64_t counter = 1; |
|
|
407 | /* some modules accept fd's from outside, support eventfd here */ |
|
|
408 | if (write (epp->fd [1], &counter, epp->len) < 0 |
|
|
409 | && errno == EINVAL |
|
|
410 | && epp->len != 8) |
398 | write (epp->fd [1], &counter, epp->len); |
411 | write (epp->fd [1], &counter, (epp->len = 8)); |
399 | #endif |
412 | #endif |
|
|
413 | } |
400 | } |
414 | } |
401 | |
415 | |
402 | static void |
416 | static void |
403 | s_epipe_drain (s_epipe *epp) |
417 | s_epipe_drain (s_epipe *epp) |
404 | { |
418 | { |
405 | char buf [9]; |
419 | char buf [9]; |
406 | |
420 | |
407 | #ifdef _WIN32 |
421 | #ifdef _WIN32 |
408 | PerlSock_recv (epp->fd [0], buf, sizeof (buf), 0); |
422 | recv (epp->fd [0], buf, sizeof (buf), 0); |
409 | #else |
423 | #else |
410 | read (epp->fd [0], buf, sizeof (buf)); |
424 | read (epp->fd [0], buf, sizeof (buf)); |
411 | #endif |
425 | #endif |
412 | |
426 | |
413 | epp->sent = 0; |
427 | epp->sent = 0; |
… | |
… | |
425 | if (s_epipe_new (&epn)) |
439 | if (s_epipe_new (&epn)) |
426 | return -1; |
440 | return -1; |
427 | |
441 | |
428 | if (epp->len) |
442 | if (epp->len) |
429 | { |
443 | { |
430 | if (dup2 (S_TO_SOCKET (epn.fd [0]), S_TO_SOCKET (epp->fd [0])) < 0) |
444 | if (dup2 (epn.fd [0], epp->fd [0]) < 0) |
431 | croak ("unable to dup over old event pipe"); /* should not croak */ |
445 | croak ("unable to dup over old event pipe"); /* should not croak */ |
432 | |
446 | |
433 | if (epp->fd [1] != epp->fd [0]) |
447 | if (epp->fd [1] != epp->fd [0]) |
434 | close (epn.fd [0]); |
448 | close (epn.fd [0]); |
435 | |
449 | |
… | |
… | |
446 | static int |
460 | static int |
447 | s_epipe_wait (s_epipe *epp) |
461 | s_epipe_wait (s_epipe *epp) |
448 | { |
462 | { |
449 | #ifdef _WIN32 |
463 | #ifdef _WIN32 |
450 | fd_set rfd; |
464 | fd_set rfd; |
|
|
465 | int fd = s_epipe_fd (epp); |
451 | |
466 | |
452 | FD_ZERO (&rfd); |
467 | FD_ZERO (&rfd); |
453 | FD_SET (s_epipe_fd (epp), &rfd); |
468 | FD_SET (fd, &rfd); |
454 | |
469 | |
455 | return PerlSock_select (s_epipe_fd (epp) + 1, &rfd, 0, 0, 0); |
470 | return PerlSock_select (fd + 1, &rfd, 0, 0, 0); |
456 | #else |
471 | #else |
457 | /* poll is preferable on posix systems */ |
472 | /* poll is preferable on posix systems */ |
458 | struct pollfd pfd; |
473 | struct pollfd pfd; |
459 | |
474 | |
460 | pfd.fd = s_epipe_fd (epp); |
475 | pfd.fd = s_epipe_fd (epp); |