ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
(Generate patch)

Comparing IO-AIO/AIO.xs (file contents):
Revision 1.103 by root, Sun Jul 8 09:09:34 2007 UTC vs.
Revision 1.110 by root, Sun Dec 2 20:54:33 2007 UTC

24 24
25// perl overrides all those nice win32 functions 25// perl overrides all those nice win32 functions
26# undef open 26# undef open
27# undef read 27# undef read
28# undef write 28# undef write
29# undef send
30# undef recv
29# undef stat 31# undef stat
30# undef fstat 32# undef fstat
31# define lstat stat 33# define lstat stat
32# undef truncate 34# undef truncate
33# undef ftruncate 35# undef ftruncate
95# define SvVAL64 SvIV 97# define SvVAL64 SvIV
96#else 98#else
97# define SvVAL64 SvNV 99# define SvVAL64 SvNV
98#endif 100#endif
99 101
102static HV *stash;
103
100#define dBUF \ 104#define dBUF \
101 char *aio_buf; \ 105 char *aio_buf; \
102 X_LOCK (wrklock); \ 106 X_LOCK (wrklock); \
103 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ 107 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \
104 X_UNLOCK (wrklock); \ 108 X_UNLOCK (wrklock); \
115 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 119 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
116 REQ_TRUNCATE, REQ_FTRUNCATE, 120 REQ_TRUNCATE, REQ_FTRUNCATE,
117 REQ_UTIME, REQ_FUTIME, 121 REQ_UTIME, REQ_FUTIME,
118 REQ_CHMOD, REQ_FCHMOD, 122 REQ_CHMOD, REQ_FCHMOD,
119 REQ_CHOWN, REQ_FCHOWN, 123 REQ_CHOWN, REQ_FCHOWN,
120 REQ_FSYNC, REQ_FDATASYNC, 124 REQ_SYNC, REQ_FSYNC, REQ_FDATASYNC,
121 REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME, 125 REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME,
122 REQ_MKNOD, REQ_READDIR, 126 REQ_MKNOD, REQ_READDIR,
123 REQ_LINK, REQ_SYMLINK, REQ_READLINK, 127 REQ_LINK, REQ_SYMLINK, REQ_READLINK,
124 REQ_GROUP, REQ_NOP, 128 REQ_GROUP, REQ_NOP,
125 REQ_BUSY, 129 REQ_BUSY,
262} 266}
263 267
264static volatile unsigned int nreqs, nready, npending; 268static volatile unsigned int nreqs, nready, npending;
265static volatile unsigned int max_idle = 4; 269static volatile unsigned int max_idle = 4;
266static volatile unsigned int max_outstanding = 0xffffffff; 270static volatile unsigned int max_outstanding = 0xffffffff;
267static int respipe [2]; 271static int respipe_osf [2], respipe [2] = { -1, -1 };
268 272
269static mutex_t reslock = X_MUTEX_INIT; 273static mutex_t reslock = X_MUTEX_INIT;
270static mutex_t reqlock = X_MUTEX_INIT; 274static mutex_t reqlock = X_MUTEX_INIT;
271static cond_t reqwait = X_COND_INIT; 275static cond_t reqwait = X_COND_INIT;
272 276
499 break; 503 break;
500 504
501 case REQ_OPEN: 505 case REQ_OPEN:
502 { 506 {
503 /* convert fd to fh */ 507 /* convert fd to fh */
504 SV *fh; 508 SV *fh = &PL_sv_undef;
505 509
506 PUSHs (sv_2mortal (newSViv (req->result))); 510 if (req->result >= 0)
507 PUTBACK; 511 {
508 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 512 GV *gv = (GV *)sv_newmortal ();
509 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);
510 519
511 fh = POPs; 520 symlen = snprintf (
512 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
513 XPUSHs (fh); 532 PUSHs (fh);
514 } 533 }
515 break; 534 break;
516 535
517 case REQ_GROUP: 536 case REQ_GROUP:
518 req->int1 = 2; /* mark group as finished */ 537 req->int1 = 2; /* mark group as finished */
562 } 581 }
563 582
564 errno = req->errorno; 583 errno = req->errorno;
565 584
566 PUTBACK; 585 PUTBACK;
567 call_sv (req->callback, G_VOID | G_EVAL); 586 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD);
568 SPAGAIN; 587 SPAGAIN;
569 588
570 FREETMPS; 589 FREETMPS;
571 LEAVE; 590 LEAVE;
591
592 PUTBACK;
572 } 593 }
573 594
574 if (req->grp) 595 if (req->grp)
575 { 596 {
576 aio_req grp = req->grp; 597 aio_req grp = req->grp;
623static void req_cancel (aio_req req) 644static void req_cancel (aio_req req)
624{ 645{
625 req->flags |= FLAG_CANCELLED; 646 req->flags |= FLAG_CANCELLED;
626 647
627 req_cancel_subs (req); 648 req_cancel_subs (req);
649}
650
651#ifdef USE_SOCKETS_AS_HANDLES
652# define TO_SOCKET(x) (win32_get_osfhandle (x))
653#else
654# define TO_SOCKET(x) (x)
655#endif
656
657static void
658create_respipe ()
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
681#ifdef _WIN32
682 int arg = 1;
683 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
684 || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
685#else
686 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
687 || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
688#endif
689 croak ("unable to initialize result pipe(3)");
690
691 respipe_osf [0] = TO_SOCKET (respipe [0]);
692 respipe_osf [1] = TO_SOCKET (respipe [1]);
628} 693}
629 694
630X_THREAD_PROC (aio_proc); 695X_THREAD_PROC (aio_proc);
631 696
632static void start_thread (void) 697static void start_thread (void)
736 if (size) 801 if (size)
737 return; 802 return;
738 803
739 maybe_start_thread (); 804 maybe_start_thread ();
740 805
741 FD_ZERO(&rfd); 806 FD_ZERO (&rfd);
742 FD_SET(respipe [0], &rfd); 807 FD_SET (respipe [0], &rfd);
743 808
744 select (respipe [0] + 1, &rfd, 0, 0, 0); 809 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
745 } 810 }
746} 811}
747 812
748static int poll_cb () 813static int poll_cb ()
749{ 814{
774 839
775 if (!res_queue.size) 840 if (!res_queue.size)
776 { 841 {
777 /* read any signals sent by the worker threads */ 842 /* read any signals sent by the worker threads */
778 char buf [4]; 843 char buf [4];
779 while (read (respipe [0], buf, 4) == 4) 844 while (respipe_read (respipe [0], buf, 4) == 4)
780 ; 845 ;
781 } 846 }
782 } 847 }
783 848
784 X_UNLOCK (reslock); 849 X_UNLOCK (reslock);
1202 case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break; 1267 case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break;
1203 case REQ_SYMLINK: req->result = symlink (req->ptr2, req->ptr1); break; 1268 case REQ_SYMLINK: req->result = symlink (req->ptr2, req->ptr1); break;
1204 case REQ_MKNOD: req->result = mknod (req->ptr2, req->mode, (dev_t)req->offs); break; 1269 case REQ_MKNOD: req->result = mknod (req->ptr2, req->mode, (dev_t)req->offs); break;
1205 case REQ_READLINK: req->result = readlink (req->ptr2, req->ptr1, NAME_MAX); break; 1270 case REQ_READLINK: req->result = readlink (req->ptr2, req->ptr1, NAME_MAX); break;
1206 1271
1272 case REQ_SYNC: req->result = 0; sync (); break;
1273 case REQ_FSYNC: req->result = fsync (req->int1); break;
1207 case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; 1274 case REQ_FDATASYNC: req->result = fdatasync (req->int1); break;
1208 case REQ_FSYNC: req->result = fsync (req->int1); break; 1275
1209 case REQ_READDIR: scandir_ (req, self); break; 1276 case REQ_READDIR: scandir_ (req, self); break;
1210 1277
1211 case REQ_BUSY: 1278 case REQ_BUSY:
1212#ifdef _WIN32 1279#ifdef _WIN32
1213 Sleep (req->nv1 * 1000.); 1280 Sleep (req->nv1 * 1000.);
1266 ++npending; 1333 ++npending;
1267 1334
1268 if (!reqq_push (&res_queue, req)) 1335 if (!reqq_push (&res_queue, req))
1269 { 1336 {
1270 /* write a dummy byte to the pipe so fh becomes ready */ 1337 /* write a dummy byte to the pipe so fh becomes ready */
1271 write (respipe [1], &respipe, 1); 1338 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
1272 1339
1273 /* optionally signal the main thread asynchronously */ 1340 /* optionally signal the main thread asynchronously */
1274 if (main_sig) 1341 if (main_sig)
1275 pthread_kill (main_tid, main_sig); 1342 pthread_kill (main_tid, main_sig);
1276 } 1343 }
1343 idle = 0; 1410 idle = 0;
1344 nreqs = 0; 1411 nreqs = 0;
1345 nready = 0; 1412 nready = 0;
1346 npending = 0; 1413 npending = 0;
1347 1414
1348 close (respipe [0]); 1415 create_respipe ();
1349 close (respipe [1]);
1350
1351 if (!create_pipe (respipe))
1352 croak ("cannot set result pipe to nonblocking mode");
1353 1416
1354 atfork_parent (); 1417 atfork_parent ();
1355} 1418}
1356 1419
1357#define dREQ \ 1420#define dREQ \
1379 1442
1380PROTOTYPES: ENABLE 1443PROTOTYPES: ENABLE
1381 1444
1382BOOT: 1445BOOT:
1383{ 1446{
1384 HV *stash = gv_stashpv ("IO::AIO", 1); 1447 stash = gv_stashpv ("IO::AIO", 1);
1385 1448
1386 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); 1449 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
1387 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); 1450 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
1388 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); 1451 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
1389 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); 1452 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
1393 X_MUTEX_CHECK (reslock); 1456 X_MUTEX_CHECK (reslock);
1394 X_MUTEX_CHECK (reqlock); 1457 X_MUTEX_CHECK (reqlock);
1395 X_MUTEX_CHECK (reqwait); 1458 X_MUTEX_CHECK (reqwait);
1396 X_MUTEX_CHECK (preadwritelock); 1459 X_MUTEX_CHECK (preadwritelock);
1397 X_MUTEX_CHECK (readdirlock); 1460 X_MUTEX_CHECK (readdirlock);
1461
1462 X_COND_CHECK (reqwait);
1398#else 1463#else
1399 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); 1464 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
1400 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); 1465 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO));
1401#endif 1466#endif
1402 1467
1403 if (!create_pipe (respipe)) 1468 create_respipe ();
1404 croak ("cannot set result pipe to nonblocking mode");
1405 1469
1406 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child); 1470 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
1407} 1471}
1408 1472
1409void 1473void
1456 1520
1457 REQ_SEND; 1521 REQ_SEND;
1458} 1522}
1459 1523
1460void 1524void
1461aio_close (SV *fh, SV *callback=&PL_sv_undef) 1525aio_fsync (SV *fh, SV *callback=&PL_sv_undef)
1462 PROTOTYPE: $;$ 1526 PROTOTYPE: $;$
1463 ALIAS: 1527 ALIAS:
1464 aio_close = REQ_CLOSE
1465 aio_fsync = REQ_FSYNC 1528 aio_fsync = REQ_FSYNC
1466 aio_fdatasync = REQ_FDATASYNC 1529 aio_fdatasync = REQ_FDATASYNC
1467 PPCODE: 1530 PPCODE:
1468{ 1531{
1469 dREQ; 1532 dREQ;
1470 1533
1471 req->type = ix; 1534 req->type = ix;
1472 req->sv1 = newSVsv (fh); 1535 req->sv1 = newSVsv (fh);
1473 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); 1536 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1537
1538 REQ_SEND (req);
1539}
1540
1541int
1542_dup (int fd)
1543 PROTOTYPE: $
1544 CODE:
1545 RETVAL = dup (fd);
1546 OUTPUT:
1547 RETVAL
1548
1549void
1550_aio_close (int fd, SV *callback=&PL_sv_undef)
1551 PROTOTYPE: $;$
1552 PPCODE:
1553{
1554 dREQ;
1555
1556 req->type = REQ_CLOSE;
1557 req->int1 = fd;
1474 1558
1475 REQ_SEND (req); 1559 REQ_SEND (req);
1476} 1560}
1477 1561
1478void 1562void
1805 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1889 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1806} 1890}
1807 1891
1808void 1892void
1809aio_nop (SV *callback=&PL_sv_undef) 1893aio_nop (SV *callback=&PL_sv_undef)
1894 ALIAS:
1895 aio_nop = REQ_NOP
1896 aio_sync = REQ_SYNC
1810 PPCODE: 1897 PPCODE:
1811{ 1898{
1812 dREQ; 1899 dREQ;
1813 1900
1814 req->type = REQ_NOP; 1901 req->type = ix;
1815 1902
1816 REQ_SEND; 1903 REQ_SEND;
1817} 1904}
1818 1905
1819int 1906int

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines