… | |
… | |
97 | # define SvVAL64 SvIV |
97 | # define SvVAL64 SvIV |
98 | #else |
98 | #else |
99 | # define SvVAL64 SvNV |
99 | # define SvVAL64 SvNV |
100 | #endif |
100 | #endif |
101 | |
101 | |
|
|
102 | static HV *stash; |
|
|
103 | |
102 | #define dBUF \ |
104 | #define dBUF \ |
103 | char *aio_buf; \ |
105 | char *aio_buf; \ |
104 | X_LOCK (wrklock); \ |
106 | X_LOCK (wrklock); \ |
105 | self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ |
107 | self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ |
106 | X_UNLOCK (wrklock); \ |
108 | X_UNLOCK (wrklock); \ |
… | |
… | |
264 | } |
266 | } |
265 | |
267 | |
266 | static volatile unsigned int nreqs, nready, npending; |
268 | static volatile unsigned int nreqs, nready, npending; |
267 | static volatile unsigned int max_idle = 4; |
269 | static volatile unsigned int max_idle = 4; |
268 | static volatile unsigned int max_outstanding = 0xffffffff; |
270 | static volatile unsigned int max_outstanding = 0xffffffff; |
269 | static int respipe [2], respipe_osf [2]; |
271 | static int respipe_osf [2], respipe [2] = { -1, -1 }; |
270 | |
272 | |
271 | static mutex_t reslock = X_MUTEX_INIT; |
273 | static mutex_t reslock = X_MUTEX_INIT; |
272 | static mutex_t reqlock = X_MUTEX_INIT; |
274 | static mutex_t reqlock = X_MUTEX_INIT; |
273 | static cond_t reqwait = X_COND_INIT; |
275 | static cond_t reqwait = X_COND_INIT; |
274 | |
276 | |
… | |
… | |
501 | break; |
503 | break; |
502 | |
504 | |
503 | case REQ_OPEN: |
505 | case REQ_OPEN: |
504 | { |
506 | { |
505 | /* convert fd to fh */ |
507 | /* convert fd to fh */ |
506 | SV *fh; |
508 | SV *fh = &PL_sv_undef; |
507 | |
509 | |
508 | PUSHs (sv_2mortal (newSViv (req->result))); |
510 | if (req->result >= 0) |
509 | PUTBACK; |
511 | { |
510 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
512 | GV *gv = (GV *)sv_newmortal (); |
511 | SPAGAIN; |
513 | int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); |
|
|
514 | char sym [64]; |
|
|
515 | int symlen; |
|
|
516 | |
|
|
517 | symlen = snprintf (sym, sizeof (sym), "fd#%d", req->result); |
|
|
518 | gv_init (gv, stash, sym, symlen, 0); |
512 | |
519 | |
513 | fh = POPs; |
520 | symlen = snprintf ( |
514 | PUSHMARK (SP); |
521 | sym, |
|
|
522 | sizeof (sym), |
|
|
523 | "%s&=%d", |
|
|
524 | flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<", |
|
|
525 | req->result |
|
|
526 | ); |
|
|
527 | |
|
|
528 | if (do_open (gv, sym, symlen, 0, 0, 0, 0)) |
|
|
529 | fh = (SV *)gv; |
|
|
530 | } |
|
|
531 | |
515 | XPUSHs (fh); |
532 | PUSHs (fh); |
516 | } |
533 | } |
517 | break; |
534 | break; |
518 | |
535 | |
519 | case REQ_GROUP: |
536 | case REQ_GROUP: |
520 | req->int1 = 2; /* mark group as finished */ |
537 | req->int1 = 2; /* mark group as finished */ |
… | |
… | |
564 | } |
581 | } |
565 | |
582 | |
566 | errno = req->errorno; |
583 | errno = req->errorno; |
567 | |
584 | |
568 | PUTBACK; |
585 | PUTBACK; |
569 | call_sv (req->callback, G_VOID | G_EVAL); |
586 | call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); |
570 | SPAGAIN; |
587 | SPAGAIN; |
571 | |
588 | |
572 | FREETMPS; |
589 | FREETMPS; |
573 | LEAVE; |
590 | LEAVE; |
|
|
591 | |
|
|
592 | PUTBACK; |
574 | } |
593 | } |
575 | |
594 | |
576 | if (req->grp) |
595 | if (req->grp) |
577 | { |
596 | { |
578 | aio_req grp = req->grp; |
597 | aio_req grp = req->grp; |
… | |
… | |
634 | #else |
653 | #else |
635 | # define TO_SOCKET(x) (x) |
654 | # define TO_SOCKET(x) (x) |
636 | #endif |
655 | #endif |
637 | |
656 | |
638 | static void |
657 | static void |
639 | create_pipe (int fd[2]) |
658 | create_respipe () |
640 | { |
659 | { |
|
|
660 | int old_readfd = respipe [0]; |
|
|
661 | |
|
|
662 | if (respipe [1] >= 0) |
|
|
663 | respipe_close (TO_SOCKET (respipe [1])); |
|
|
664 | |
|
|
665 | #ifdef _WIN32 |
|
|
666 | if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe)) |
|
|
667 | #else |
|
|
668 | if (pipe (respipe)) |
|
|
669 | #endif |
|
|
670 | croak ("unable to initialize result pipe"); |
|
|
671 | |
|
|
672 | if (old_readfd >= 0) |
|
|
673 | { |
|
|
674 | if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0) |
|
|
675 | croak ("unable to initialize result pipe(2)"); |
|
|
676 | |
|
|
677 | respipe_close (respipe [0]); |
|
|
678 | respipe [0] = old_readfd; |
|
|
679 | } |
|
|
680 | |
641 | #ifdef _WIN32 |
681 | #ifdef _WIN32 |
642 | int arg = 1; |
682 | int arg = 1; |
643 | if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, fd) |
|
|
644 | || ioctlsocket (TO_SOCKET (fd [0]), FIONBIO, &arg) |
683 | if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg) |
645 | || ioctlsocket (TO_SOCKET (fd [1]), FIONBIO, &arg)) |
684 | || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg)) |
646 | #else |
685 | #else |
647 | if (pipe (fd) |
|
|
648 | || fcntl (fd [0], F_SETFL, O_NONBLOCK) |
686 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK) |
649 | || fcntl (fd [1], F_SETFL, O_NONBLOCK)) |
687 | || fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
650 | #endif |
688 | #endif |
651 | croak ("unable to initialize result pipe"); |
689 | croak ("unable to initialize result pipe(3)"); |
652 | |
690 | |
653 | respipe_osf [0] = TO_SOCKET (respipe [0]); |
691 | respipe_osf [0] = TO_SOCKET (respipe [0]); |
654 | respipe_osf [1] = TO_SOCKET (respipe [1]); |
692 | respipe_osf [1] = TO_SOCKET (respipe [1]); |
655 | } |
693 | } |
656 | |
694 | |
… | |
… | |
801 | |
839 | |
802 | if (!res_queue.size) |
840 | if (!res_queue.size) |
803 | { |
841 | { |
804 | /* read any signals sent by the worker threads */ |
842 | /* read any signals sent by the worker threads */ |
805 | char buf [4]; |
843 | char buf [4]; |
806 | while (PerlSock_recv (respipe [0], buf, 4, 0) == 4) |
844 | while (respipe_read (respipe [0], buf, 4) == 4) |
807 | ; |
845 | ; |
808 | } |
846 | } |
809 | } |
847 | } |
810 | |
848 | |
811 | X_UNLOCK (reslock); |
849 | X_UNLOCK (reslock); |
… | |
… | |
1293 | ++npending; |
1331 | ++npending; |
1294 | |
1332 | |
1295 | if (!reqq_push (&res_queue, req)) |
1333 | if (!reqq_push (&res_queue, req)) |
1296 | { |
1334 | { |
1297 | /* write a dummy byte to the pipe so fh becomes ready */ |
1335 | /* write a dummy byte to the pipe so fh becomes ready */ |
1298 | send (respipe_osf [1], (const void *)&respipe_osf, 1, 0); |
1336 | respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); |
1299 | |
1337 | |
1300 | /* optionally signal the main thread asynchronously */ |
1338 | /* optionally signal the main thread asynchronously */ |
1301 | if (main_sig) |
1339 | if (main_sig) |
1302 | pthread_kill (main_tid, main_sig); |
1340 | pthread_kill (main_tid, main_sig); |
1303 | } |
1341 | } |
… | |
… | |
1370 | idle = 0; |
1408 | idle = 0; |
1371 | nreqs = 0; |
1409 | nreqs = 0; |
1372 | nready = 0; |
1410 | nready = 0; |
1373 | npending = 0; |
1411 | npending = 0; |
1374 | |
1412 | |
1375 | PerlSock_closesocket (respipe [0]); |
|
|
1376 | PerlSock_closesocket (respipe [1]); |
|
|
1377 | |
|
|
1378 | create_pipe (respipe); |
1413 | create_respipe (); |
1379 | |
1414 | |
1380 | atfork_parent (); |
1415 | atfork_parent (); |
1381 | } |
1416 | } |
1382 | |
1417 | |
1383 | #define dREQ \ |
1418 | #define dREQ \ |
… | |
… | |
1405 | |
1440 | |
1406 | PROTOTYPES: ENABLE |
1441 | PROTOTYPES: ENABLE |
1407 | |
1442 | |
1408 | BOOT: |
1443 | BOOT: |
1409 | { |
1444 | { |
1410 | HV *stash = gv_stashpv ("IO::AIO", 1); |
1445 | stash = gv_stashpv ("IO::AIO", 1); |
1411 | |
1446 | |
1412 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1447 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1413 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1448 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1414 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1449 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1415 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
1450 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
… | |
… | |
1426 | #else |
1461 | #else |
1427 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
1462 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
1428 | newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); |
1463 | newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); |
1429 | #endif |
1464 | #endif |
1430 | |
1465 | |
1431 | create_pipe (respipe); |
1466 | create_respipe (); |
1432 | |
1467 | |
1433 | X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1468 | X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); |
1434 | } |
1469 | } |
1435 | |
1470 | |
1436 | void |
1471 | void |
… | |
… | |
1483 | |
1518 | |
1484 | REQ_SEND; |
1519 | REQ_SEND; |
1485 | } |
1520 | } |
1486 | |
1521 | |
1487 | void |
1522 | void |
1488 | aio_close (SV *fh, SV *callback=&PL_sv_undef) |
1523 | aio_fsync (SV *fh, SV *callback=&PL_sv_undef) |
1489 | PROTOTYPE: $;$ |
1524 | PROTOTYPE: $;$ |
1490 | ALIAS: |
1525 | ALIAS: |
1491 | aio_close = REQ_CLOSE |
|
|
1492 | aio_fsync = REQ_FSYNC |
1526 | aio_fsync = REQ_FSYNC |
1493 | aio_fdatasync = REQ_FDATASYNC |
1527 | aio_fdatasync = REQ_FDATASYNC |
1494 | PPCODE: |
1528 | PPCODE: |
1495 | { |
1529 | { |
1496 | dREQ; |
1530 | dREQ; |
1497 | |
1531 | |
1498 | req->type = ix; |
1532 | req->type = ix; |
1499 | req->sv1 = newSVsv (fh); |
1533 | req->sv1 = newSVsv (fh); |
1500 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1534 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
|
|
1535 | |
|
|
1536 | REQ_SEND (req); |
|
|
1537 | } |
|
|
1538 | |
|
|
1539 | int |
|
|
1540 | _dup (int fd) |
|
|
1541 | PROTOTYPE: $ |
|
|
1542 | CODE: |
|
|
1543 | RETVAL = dup (fd); |
|
|
1544 | OUTPUT: |
|
|
1545 | RETVAL |
|
|
1546 | |
|
|
1547 | void |
|
|
1548 | _aio_close (int fd, SV *callback=&PL_sv_undef) |
|
|
1549 | PROTOTYPE: $;$ |
|
|
1550 | PPCODE: |
|
|
1551 | { |
|
|
1552 | dREQ; |
|
|
1553 | |
|
|
1554 | req->type = REQ_CLOSE; |
|
|
1555 | req->int1 = fd; |
1501 | |
1556 | |
1502 | REQ_SEND (req); |
1557 | REQ_SEND (req); |
1503 | } |
1558 | } |
1504 | |
1559 | |
1505 | void |
1560 | void |