ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/EV/schmorp.h
(Generate patch)

Comparing EV/schmorp.h (file contents):
Revision 1.4 by root, Wed Jul 15 16:58:53 2009 UTC vs.
Revision 1.9 by root, Thu Jul 8 00:45:03 2010 UTC

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#if defined(WIN32 ) || defined(_MINIX)
12# define SCHMORP_H_PREFER_SELECT 1
13#endif
14
15#if !SCHMORP_H_PREFER_SELECT
11# include <poll.h> 16# include <poll.h>
12#endif 17#endif
13 18
14/* useful stuff, used by schmorp mostly */ 19/* useful stuff, used by schmorp mostly */
15 20
19 (PERL_REVISION > (a) \ 24 (PERL_REVISION > (a) \
20 || (PERL_REVISION == (a) \ 25 || (PERL_REVISION == (a) \
21 && (PERL_VERSION > (b) \ 26 && (PERL_VERSION > (b) \
22 || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) 27 || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c)))))
23 28
29#ifndef PERL_MAGIC_ext
30# define PERL_MAGIC_ext '~'
31#endif
32
24#if !PERL_VERSION_ATLEAST (5,6,0) 33#if !PERL_VERSION_ATLEAST (5,6,0)
25# ifndef PL_ppaddr 34# ifndef PL_ppaddr
26# define PL_ppaddr ppaddr 35# define PL_ppaddr ppaddr
27# endif 36# endif
28# ifndef call_sv 37# ifndef call_sv
208/* taken almost verbatim from libev's ev_win32.c */ 217/* taken almost verbatim from libev's ev_win32.c */
209/* oh, the humanity! */ 218/* oh, the humanity! */
210static int 219static int
211s_pipe (int filedes [2]) 220s_pipe (int filedes [2])
212{ 221{
222 dTHX;
223
213 struct sockaddr_in addr = { 0 }; 224 struct sockaddr_in addr = { 0 };
214 int addr_size = sizeof (addr); 225 int addr_size = sizeof (addr);
215 struct sockaddr_in adr2; 226 struct sockaddr_in adr2;
216 int adr2_size = sizeof (adr2); 227 int adr2_size = sizeof (adr2);
217 SOCKET listener; 228 SOCKET listener;
320} 331}
321 332
322#endif 333#endif
323 334
324#if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) 335#if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
336# define SCHMORP_H_HAVE_EVENTFD 1
325/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 337/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
326# include <stdint.h> 338# include <stdint.h>
327# ifdef __cplusplus 339# ifdef __cplusplus
328extern "C" { 340extern "C" {
329# endif 341# endif
336#endif 348#endif
337 349
338typedef struct { 350typedef struct {
339 int fd[2]; /* read, write fd, might be equal */ 351 int fd[2]; /* read, write fd, might be equal */
340 int len; /* write length (1 pipe/socket, 8 eventfd) */ 352 int len; /* write length (1 pipe/socket, 8 eventfd) */
341 volatile sig_atomic_t sent;
342} s_epipe; 353} s_epipe;
343 354
344static int 355static int
345s_epipe_new (s_epipe *epp) 356s_epipe_new (s_epipe *epp)
346{ 357{
359 return -1; 370 return -1;
360 371
361 if (s_fd_prepare (ep.fd [0]) 372 if (s_fd_prepare (ep.fd [0])
362 || s_fd_prepare (ep.fd [1])) 373 || s_fd_prepare (ep.fd [1]))
363 { 374 {
375 dTHX;
376
364 close (ep.fd [0]); 377 close (ep.fd [0]);
365 close (ep.fd [1]); 378 close (ep.fd [1]);
366 return -1; 379 return -1;
367 } 380 }
368 381
369 ep.len = 1; 382 ep.len = 1;
370 } 383 }
371 384
372 ep.sent = 0;
373 *epp = ep; 385 *epp = ep;
374 return 0; 386 return 0;
375} 387}
376 388
377static void 389static void
378s_epipe_destroy (s_epipe *epp) 390s_epipe_destroy (s_epipe *epp)
379{ 391{
392 dTHX;
393
380 close (epp->fd [0]); 394 close (epp->fd [0]);
381 395
382 if (epp->fd [1] != epp->fd [0]) 396 if (epp->fd [1] != epp->fd [0])
383 close (epp->fd [1]); 397 close (epp->fd [1]);
384 398
386} 400}
387 401
388static void 402static void
389s_epipe_signal (s_epipe *epp) 403s_epipe_signal (s_epipe *epp)
390{ 404{
391 if (epp->sent)
392 return;
393
394 epp->sent = 1;
395 {
396#ifdef _WIN32 405#ifdef _WIN32
397 /* perl overrides send with a function that crashes in other threads. 406 /* perl overrides send with a function that crashes in other threads.
398 * unfortunately, it overrides it with an argument-less macro, so 407 * unfortunately, it overrides it with an argument-less macro, so
399 * there is no way to force usage of the real send function. 408 * there is no way to force usage of the real send function.
400 * incompetent windows programmers - is this redundant? 409 * incompetent windows programmers - is this redundant?
401 */ 410 */
402 DWORD dummy; 411 DWORD dummy;
403 WriteFile (S_TO_HANDLE (epp->fd [1]), (LPCVOID)&dummy, 1, &dummy, 0); 412 WriteFile (S_TO_HANDLE (epp->fd [1]), (LPCVOID)&dummy, 1, &dummy, 0);
404#else 413#else
414# if SCHMORP_H_HAVE_EVENTFD
405 static uint64_t counter = 1; 415 static uint64_t counter = 1;
416# else
417 static char counter [8];
418# endif
419 /* some modules accept fd's from outside, support eventfd here */
420 if (write (epp->fd [1], &counter, epp->len) < 0
421 && errno == EINVAL
422 && epp->len != 8)
406 write (epp->fd [1], &counter, epp->len); 423 write (epp->fd [1], &counter, (epp->len = 8));
407#endif 424#endif
408 }
409} 425}
410 426
411static void 427static void
412s_epipe_drain (s_epipe *epp) 428s_epipe_drain (s_epipe *epp)
413{ 429{
430 dTHX;
414 char buf [9]; 431 char buf [9];
415 432
416#ifdef _WIN32 433#ifdef _WIN32
417 recv (epp->fd [0], buf, sizeof (buf), 0); 434 recv (epp->fd [0], buf, sizeof (buf), 0);
418#else 435#else
419 read (epp->fd [0], buf, sizeof (buf)); 436 read (epp->fd [0], buf, sizeof (buf));
420#endif 437#endif
421
422 epp->sent = 0;
423} 438}
424 439
425/* like new, but dups over old */ 440/* like new, but dups over old */
426static int 441static int
427s_epipe_renew (s_epipe *epp) 442s_epipe_renew (s_epipe *epp)
428{ 443{
444 dTHX;
429 s_epipe epn; 445 s_epipe epn;
430 446
431 if (epp->fd [1] != epp->fd [0]) 447 if (epp->fd [1] != epp->fd [0])
432 close (epp->fd [1]); 448 close (epp->fd [1]);
433 449
437 if (epp->len) 453 if (epp->len)
438 { 454 {
439 if (dup2 (epn.fd [0], epp->fd [0]) < 0) 455 if (dup2 (epn.fd [0], epp->fd [0]) < 0)
440 croak ("unable to dup over old event pipe"); /* should not croak */ 456 croak ("unable to dup over old event pipe"); /* should not croak */
441 457
442 if (epp->fd [1] != epp->fd [0])
443 close (epn.fd [0]); 458 close (epn.fd [0]);
459
460 if (epn.fd [0] == epn.fd [1])
461 epn.fd [1] = epp->fd [0];
444 462
445 epn.fd [0] = epp->fd [0]; 463 epn.fd [0] = epp->fd [0];
446 } 464 }
447 465
448 *epp = epn; 466 *epp = epn;
453#define s_epipe_fd(epp) ((epp)->fd [0]) 471#define s_epipe_fd(epp) ((epp)->fd [0])
454 472
455static int 473static int
456s_epipe_wait (s_epipe *epp) 474s_epipe_wait (s_epipe *epp)
457{ 475{
458#ifdef _WIN32 476 dTHX;
477#if SCHMORP_H_PREFER_SELECT
459 fd_set rfd; 478 fd_set rfd;
460 int fd = s_epipe_fd (epp); 479 int fd = s_epipe_fd (epp);
461 480
462 FD_ZERO (&rfd); 481 FD_ZERO (&rfd);
463 FD_SET (fd, &rfd); 482 FD_SET (fd, &rfd);
468 struct pollfd pfd; 487 struct pollfd pfd;
469 488
470 pfd.fd = s_epipe_fd (epp); 489 pfd.fd = s_epipe_fd (epp);
471 pfd.events = POLLIN; 490 pfd.events = POLLIN;
472 491
473 return poll (&pfd, 1, 0); 492 return poll (&pfd, 1, -1);
474#endif 493#endif
475} 494}
476 495
477#endif 496#endif
478 497

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines