1 | #ifndef SCHMORP_PERL_H_ |
1 | #ifndef SCHMORP_PERL_H_ |
2 | #define SCHMORP_PERL_H_ |
2 | #define SCHMORP_PERL_H_ |
3 | |
3 | |
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 | * perl header files MUST already be included. |
6 | */ |
7 | */ |
7 | |
8 | |
8 | #include <signal.h> |
9 | #include <signal.h> |
9 | #include <errno.h> |
10 | #include <errno.h> |
10 | |
11 | |
11 | #ifndef _WIN32 |
12 | #if defined(WIN32 ) || defined(_MINIX) |
|
|
13 | # define SCHMORP_H_PREFER_SELECT 1 |
|
|
14 | #endif |
|
|
15 | |
|
|
16 | #if !SCHMORP_H_PREFER_SELECT |
12 | # include <poll.h> |
17 | # include <poll.h> |
13 | #endif |
18 | #endif |
14 | |
19 | |
15 | /* useful stuff, used by schmorp mostly */ |
20 | /* useful stuff, used by schmorp mostly */ |
16 | |
21 | |
… | |
… | |
20 | (PERL_REVISION > (a) \ |
25 | (PERL_REVISION > (a) \ |
21 | || (PERL_REVISION == (a) \ |
26 | || (PERL_REVISION == (a) \ |
22 | && (PERL_VERSION > (b) \ |
27 | && (PERL_VERSION > (b) \ |
23 | || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
28 | || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
24 | |
29 | |
|
|
30 | #ifndef PERL_MAGIC_ext |
|
|
31 | # define PERL_MAGIC_ext '~' |
|
|
32 | #endif |
|
|
33 | |
25 | #if !PERL_VERSION_ATLEAST (5,6,0) |
34 | #if !PERL_VERSION_ATLEAST (5,6,0) |
26 | # ifndef PL_ppaddr |
35 | # ifndef PL_ppaddr |
27 | # define PL_ppaddr ppaddr |
36 | # define PL_ppaddr ppaddr |
28 | # endif |
37 | # endif |
29 | # ifndef call_sv |
38 | # ifndef call_sv |
… | |
… | |
40 | # endif |
49 | # endif |
41 | # ifndef IS_PADCONST |
50 | # ifndef IS_PADCONST |
42 | # define IS_PADCONST(v) 0 |
51 | # define IS_PADCONST(v) 0 |
43 | # endif |
52 | # endif |
44 | #endif |
53 | #endif |
|
|
54 | |
|
|
55 | /* use NV for 32 bit perls as it allows larger offsets */ |
|
|
56 | #if IVSIZE >= 8 |
|
|
57 | typedef IV VAL64; |
|
|
58 | # define SvVAL64(sv) SvIV (sv) |
|
|
59 | # define newSVval64(i64) newSViv (i64) |
|
|
60 | #else |
|
|
61 | typedef NV VAL64; |
|
|
62 | # define SvVAL64(sv) SvNV (sv) |
|
|
63 | # define newSVval64(i64) newSVnv (i64) |
|
|
64 | #endif |
|
|
65 | |
|
|
66 | /* typemap for the above */ |
|
|
67 | /* |
|
|
68 | VAL64 T_VAL64 |
|
|
69 | |
|
|
70 | INPUT |
|
|
71 | |
|
|
72 | T_VAL64 |
|
|
73 | $var = ($type)SvVAL64 ($arg); |
|
|
74 | |
|
|
75 | OUTPUT |
|
|
76 | |
|
|
77 | T_VAL64 |
|
|
78 | $arg = newSVval64 ($var); |
|
|
79 | */ |
45 | |
80 | |
46 | /* 5.11 */ |
81 | /* 5.11 */ |
47 | #ifndef CxHASARGS |
82 | #ifndef CxHASARGS |
48 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
83 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
49 | #endif |
84 | #endif |
… | |
… | |
209 | /* taken almost verbatim from libev's ev_win32.c */ |
244 | /* taken almost verbatim from libev's ev_win32.c */ |
210 | /* oh, the humanity! */ |
245 | /* oh, the humanity! */ |
211 | static int |
246 | static int |
212 | s_pipe (int filedes [2]) |
247 | s_pipe (int filedes [2]) |
213 | { |
248 | { |
|
|
249 | dTHX; |
|
|
250 | |
214 | struct sockaddr_in addr = { 0 }; |
251 | struct sockaddr_in addr = { 0 }; |
215 | int addr_size = sizeof (addr); |
252 | int addr_size = sizeof (addr); |
216 | struct sockaddr_in adr2; |
253 | struct sockaddr_in adr2; |
217 | int adr2_size = sizeof (adr2); |
254 | int adr2_size = sizeof (adr2); |
218 | SOCKET listener; |
255 | SOCKET listener; |
… | |
… | |
321 | } |
358 | } |
322 | |
359 | |
323 | #endif |
360 | #endif |
324 | |
361 | |
325 | #if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) |
362 | #if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) |
|
|
363 | # define SCHMORP_H_HAVE_EVENTFD 1 |
326 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
364 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
327 | # include <stdint.h> |
365 | # include <stdint.h> |
328 | # ifdef __cplusplus |
366 | # ifdef __cplusplus |
329 | extern "C" { |
367 | extern "C" { |
330 | # endif |
368 | # endif |
… | |
… | |
337 | #endif |
375 | #endif |
338 | |
376 | |
339 | typedef struct { |
377 | typedef struct { |
340 | int fd[2]; /* read, write fd, might be equal */ |
378 | int fd[2]; /* read, write fd, might be equal */ |
341 | int len; /* write length (1 pipe/socket, 8 eventfd) */ |
379 | int len; /* write length (1 pipe/socket, 8 eventfd) */ |
342 | volatile sig_atomic_t sent; |
|
|
343 | } s_epipe; |
380 | } s_epipe; |
344 | |
381 | |
345 | static int |
382 | static int |
346 | s_epipe_new (s_epipe *epp) |
383 | s_epipe_new (s_epipe *epp) |
347 | { |
384 | { |
… | |
… | |
360 | return -1; |
397 | return -1; |
361 | |
398 | |
362 | if (s_fd_prepare (ep.fd [0]) |
399 | if (s_fd_prepare (ep.fd [0]) |
363 | || s_fd_prepare (ep.fd [1])) |
400 | || s_fd_prepare (ep.fd [1])) |
364 | { |
401 | { |
|
|
402 | dTHX; |
|
|
403 | |
365 | close (ep.fd [0]); |
404 | close (ep.fd [0]); |
366 | close (ep.fd [1]); |
405 | close (ep.fd [1]); |
367 | return -1; |
406 | return -1; |
368 | } |
407 | } |
369 | |
408 | |
370 | ep.len = 1; |
409 | ep.len = 1; |
371 | } |
410 | } |
372 | |
411 | |
373 | ep.sent = 0; |
|
|
374 | *epp = ep; |
412 | *epp = ep; |
375 | return 0; |
413 | return 0; |
376 | } |
414 | } |
377 | |
415 | |
378 | static void |
416 | static void |
379 | s_epipe_destroy (s_epipe *epp) |
417 | s_epipe_destroy (s_epipe *epp) |
380 | { |
418 | { |
|
|
419 | dTHX; |
|
|
420 | |
381 | close (epp->fd [0]); |
421 | close (epp->fd [0]); |
382 | |
422 | |
383 | if (epp->fd [1] != epp->fd [0]) |
423 | if (epp->fd [1] != epp->fd [0]) |
384 | close (epp->fd [1]); |
424 | close (epp->fd [1]); |
385 | |
425 | |
… | |
… | |
387 | } |
427 | } |
388 | |
428 | |
389 | static void |
429 | static void |
390 | s_epipe_signal (s_epipe *epp) |
430 | s_epipe_signal (s_epipe *epp) |
391 | { |
431 | { |
392 | if (epp->sent) |
|
|
393 | return; |
|
|
394 | |
|
|
395 | epp->sent = 1; |
|
|
396 | { |
|
|
397 | #ifdef _WIN32 |
432 | #ifdef _WIN32 |
398 | /* perl overrides send with a function that crashes in other threads. |
433 | /* perl overrides send with a function that crashes in other threads. |
399 | * unfortunately, it overrides it with an argument-less macro, so |
434 | * unfortunately, it overrides it with an argument-less macro, so |
400 | * there is no way to force usage of the real send function. |
435 | * there is no way to force usage of the real send function. |
401 | * incompetent windows programmers - is this redundant? |
436 | * incompetent windows programmers - is this redundant? |
402 | */ |
437 | */ |
403 | DWORD dummy; |
438 | DWORD dummy; |
404 | WriteFile (S_TO_HANDLE (epp->fd [1]), (LPCVOID)&dummy, 1, &dummy, 0); |
439 | WriteFile (S_TO_HANDLE (epp->fd [1]), (LPCVOID)&dummy, 1, &dummy, 0); |
405 | #else |
440 | #else |
|
|
441 | # if SCHMORP_H_HAVE_EVENTFD |
406 | static uint64_t counter = 1; |
442 | static uint64_t counter = 1; |
|
|
443 | # else |
|
|
444 | static char counter [8]; |
|
|
445 | # endif |
407 | /* some modules accept fd's from outside, support eventfd here */ |
446 | /* some modules accept fd's from outside, support eventfd here */ |
408 | if (write (epp->fd [1], &counter, epp->len) < 0 |
447 | if (write (epp->fd [1], &counter, epp->len) < 0 |
409 | && errno == EINVAL |
448 | && errno == EINVAL |
410 | && epp->len != 8) |
449 | && epp->len != 8) |
411 | write (epp->fd [1], &counter, (epp->len = 8)); |
450 | write (epp->fd [1], &counter, (epp->len = 8)); |
412 | #endif |
451 | #endif |
413 | } |
|
|
414 | } |
452 | } |
415 | |
453 | |
416 | static void |
454 | static void |
417 | s_epipe_drain (s_epipe *epp) |
455 | s_epipe_drain (s_epipe *epp) |
418 | { |
456 | { |
|
|
457 | dTHX; |
419 | char buf [9]; |
458 | char buf [9]; |
420 | |
459 | |
421 | #ifdef _WIN32 |
460 | #ifdef _WIN32 |
422 | recv (epp->fd [0], buf, sizeof (buf), 0); |
461 | recv (epp->fd [0], buf, sizeof (buf), 0); |
423 | #else |
462 | #else |
424 | read (epp->fd [0], buf, sizeof (buf)); |
463 | read (epp->fd [0], buf, sizeof (buf)); |
425 | #endif |
464 | #endif |
426 | |
|
|
427 | epp->sent = 0; |
|
|
428 | } |
465 | } |
429 | |
466 | |
430 | /* like new, but dups over old */ |
467 | /* like new, but dups over old */ |
431 | static int |
468 | static int |
432 | s_epipe_renew (s_epipe *epp) |
469 | s_epipe_renew (s_epipe *epp) |
433 | { |
470 | { |
|
|
471 | dTHX; |
434 | s_epipe epn; |
472 | s_epipe epn; |
435 | |
473 | |
436 | if (epp->fd [1] != epp->fd [0]) |
474 | if (epp->fd [1] != epp->fd [0]) |
437 | close (epp->fd [1]); |
475 | close (epp->fd [1]); |
438 | |
476 | |
… | |
… | |
442 | if (epp->len) |
480 | if (epp->len) |
443 | { |
481 | { |
444 | if (dup2 (epn.fd [0], epp->fd [0]) < 0) |
482 | if (dup2 (epn.fd [0], epp->fd [0]) < 0) |
445 | croak ("unable to dup over old event pipe"); /* should not croak */ |
483 | croak ("unable to dup over old event pipe"); /* should not croak */ |
446 | |
484 | |
447 | if (epp->fd [1] != epp->fd [0]) |
|
|
448 | close (epn.fd [0]); |
485 | close (epn.fd [0]); |
|
|
486 | |
|
|
487 | if (epn.fd [0] == epn.fd [1]) |
|
|
488 | epn.fd [1] = epp->fd [0]; |
449 | |
489 | |
450 | epn.fd [0] = epp->fd [0]; |
490 | epn.fd [0] = epp->fd [0]; |
451 | } |
491 | } |
452 | |
492 | |
453 | *epp = epn; |
493 | *epp = epn; |
… | |
… | |
458 | #define s_epipe_fd(epp) ((epp)->fd [0]) |
498 | #define s_epipe_fd(epp) ((epp)->fd [0]) |
459 | |
499 | |
460 | static int |
500 | static int |
461 | s_epipe_wait (s_epipe *epp) |
501 | s_epipe_wait (s_epipe *epp) |
462 | { |
502 | { |
463 | #ifdef _WIN32 |
503 | dTHX; |
|
|
504 | #if SCHMORP_H_PREFER_SELECT |
464 | fd_set rfd; |
505 | fd_set rfd; |
465 | int fd = s_epipe_fd (epp); |
506 | int fd = s_epipe_fd (epp); |
466 | |
507 | |
467 | FD_ZERO (&rfd); |
508 | FD_ZERO (&rfd); |
468 | FD_SET (fd, &rfd); |
509 | FD_SET (fd, &rfd); |
… | |
… | |
473 | struct pollfd pfd; |
514 | struct pollfd pfd; |
474 | |
515 | |
475 | pfd.fd = s_epipe_fd (epp); |
516 | pfd.fd = s_epipe_fd (epp); |
476 | pfd.events = POLLIN; |
517 | pfd.events = POLLIN; |
477 | |
518 | |
478 | return poll (&pfd, 1, 0); |
519 | return poll (&pfd, 1, -1); |
479 | #endif |
520 | #endif |
480 | } |
521 | } |
481 | |
522 | |
482 | #endif |
523 | #endif |
483 | |
524 | |