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.88 by root, Mon Oct 30 23:43:17 2006 UTC vs.
Revision 1.94 by root, Sun Nov 26 18:28:37 2006 UTC

17#include "autoconf/config.h" 17#include "autoconf/config.h"
18 18
19#include <pthread.h> 19#include <pthread.h>
20 20
21#include <stddef.h> 21#include <stddef.h>
22#include <stdlib.h>
22#include <errno.h> 23#include <errno.h>
23#include <sys/time.h> 24#include <sys/time.h>
24#include <sys/select.h> 25#include <sys/select.h>
25#include <sys/types.h> 26#include <sys/types.h>
26#include <sys/stat.h> 27#include <sys/stat.h>
87 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ 88 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \
88 UNLOCK (wrklock); \ 89 UNLOCK (wrklock); \
89 if (!aio_buf) \ 90 if (!aio_buf) \
90 return -1; 91 return -1;
91 92
93typedef SV SV8; /* byte-sv, used for argument-checking */
94
92enum { 95enum {
93 REQ_QUIT, 96 REQ_QUIT,
94 REQ_OPEN, REQ_CLOSE, 97 REQ_OPEN, REQ_CLOSE,
95 REQ_READ, REQ_WRITE, REQ_READAHEAD, 98 REQ_READ, REQ_WRITE, REQ_READAHEAD,
96 REQ_SENDFILE, 99 REQ_SENDFILE,
129 SV *self; /* the perl counterpart of this request, if any */ 132 SV *self; /* the perl counterpart of this request, if any */
130 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; 133 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
131} aio_cb; 134} aio_cb;
132 135
133enum { 136enum {
134 FLAG_CANCELLED = 0x01, 137 FLAG_CANCELLED = 0x01, /* request was cancelled */
135 FLAG_SV1_RO_OFF = 0x40, /* data was set readonly */ 138 FLAG_SV1_RO_OFF = 0x40, /* data was set readonly */
136 FLAG_PTR2_FREE = 0x80, /* need free(ptr2) */ 139 FLAG_PTR2_FREE = 0x80, /* need to free(ptr2) */
137}; 140};
138 141
139typedef aio_cb *aio_req; 142typedef aio_cb *aio_req;
140typedef aio_cb *aio_req_ornot; 143typedef aio_cb *aio_req_ornot;
141 144
156/* calculcate time difference in ~1/AIO_TICKS of a second */ 159/* calculcate time difference in ~1/AIO_TICKS of a second */
157static int tvdiff (struct timeval *tv1, struct timeval *tv2) 160static int tvdiff (struct timeval *tv1, struct timeval *tv2)
158{ 161{
159 return (tv2->tv_sec - tv1->tv_sec ) * AIO_TICKS 162 return (tv2->tv_sec - tv1->tv_sec ) * AIO_TICKS
160 + ((tv2->tv_usec - tv1->tv_usec) >> 10); 163 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
164}
165
166static pthread_t main_tid;
167static int main_sig;
168static int block_sig_level;
169
170void block_sig ()
171{
172 sigset_t ss;
173
174 if (block_sig_level++)
175 return;
176
177 if (!main_sig)
178 return;
179
180 sigemptyset (&ss);
181 sigaddset (&ss, main_sig);
182 pthread_sigmask (SIG_BLOCK, &ss, 0);
183}
184
185void unblock_sig ()
186{
187 sigset_t ss;
188
189 if (--block_sig_level)
190 return;
191
192 if (!main_sig)
193 return;
194
195 sigemptyset (&ss);
196 sigaddset (&ss, main_sig);
197 pthread_sigmask (SIG_UNBLOCK, &ss, 0);
161} 198}
162 199
163static int next_pri = DEFAULT_PRI + PRI_BIAS; 200static int next_pri = DEFAULT_PRI + PRI_BIAS;
164 201
165static unsigned int started, idle, wanted; 202static unsigned int started, idle, wanted;
318 355
319 abort (); 356 abort ();
320} 357}
321 358
322static int poll_cb (); 359static int poll_cb ();
323static void req_invoke (aio_req req); 360static int req_invoke (aio_req req);
324static void req_free (aio_req req); 361static void req_free (aio_req req);
325static void req_cancel (aio_req req); 362static void req_cancel (aio_req req);
326 363
327/* must be called at most once */ 364/* must be called at most once */
328static SV *req_sv (aio_req req, const char *klass) 365static SV *req_sv (aio_req req, const char *klass)
348 return mg ? (aio_req)mg->mg_ptr : 0; 385 return mg ? (aio_req)mg->mg_ptr : 0;
349} 386}
350 387
351static void aio_grp_feed (aio_req grp) 388static void aio_grp_feed (aio_req grp)
352{ 389{
390 block_sig ();
391
353 while (grp->size < grp->int2 && !(grp->flags & FLAG_CANCELLED)) 392 while (grp->size < grp->int2 && !(grp->flags & FLAG_CANCELLED))
354 { 393 {
355 int old_len = grp->size; 394 int old_len = grp->size;
356 395
357 if (grp->sv2 && SvOK (grp->sv2)) 396 if (grp->sv2 && SvOK (grp->sv2))
375 SvREFCNT_dec (grp->sv2); 414 SvREFCNT_dec (grp->sv2);
376 grp->sv2 = 0; 415 grp->sv2 = 0;
377 break; 416 break;
378 } 417 }
379 } 418 }
419
420 unblock_sig ();
380} 421}
381 422
382static void aio_grp_dec (aio_req grp) 423static void aio_grp_dec (aio_req grp)
383{ 424{
384 --grp->size; 425 --grp->size;
387 aio_grp_feed (grp); 428 aio_grp_feed (grp);
388 429
389 /* finish, if done */ 430 /* finish, if done */
390 if (!grp->size && grp->int1) 431 if (!grp->size && grp->int1)
391 { 432 {
433 block_sig ();
434
392 req_invoke (grp); 435 if (!req_invoke (grp))
436 {
437 req_free (grp);
438 unblock_sig ();
439 croak (0);
440 }
441
393 req_free (grp); 442 req_free (grp);
443 unblock_sig ();
394 } 444 }
395} 445}
396 446
397static void req_invoke (aio_req req) 447static int req_invoke (aio_req req)
398{ 448{
399 dSP; 449 dSP;
400 450
401 if (req->flags & FLAG_SV1_RO_OFF) 451 if (req->flags & FLAG_SV1_RO_OFF)
402 SvREADONLY_off (req->sv1); 452 SvREADONLY_off (req->sv1);
445 PUSHs (sv_2mortal (newSViv (req->result))); 495 PUSHs (sv_2mortal (newSViv (req->result)));
446 PUTBACK; 496 PUTBACK;
447 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 497 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
448 SPAGAIN; 498 SPAGAIN;
449 499
450 fh = SvREFCNT_inc (POPs); 500 fh = POPs;
451
452 PUSHMARK (SP); 501 PUSHMARK (SP);
453 XPUSHs (sv_2mortal (fh)); 502 XPUSHs (fh);
454 } 503 }
455 break; 504 break;
456 505
457 case REQ_GROUP: 506 case REQ_GROUP:
458 req->int1 = 2; /* mark group as finished */ 507 req->int1 = 2; /* mark group as finished */
523 grp->grp_first = req->grp_next; 572 grp->grp_first = req->grp_next;
524 573
525 aio_grp_dec (grp); 574 aio_grp_dec (grp);
526 } 575 }
527 576
528 if (SvTRUE (ERRSV)) 577 return !SvTRUE (ERRSV);
529 {
530 req_free (req);
531 croak (0);
532 }
533} 578}
534 579
535static void req_free (aio_req req) 580static void req_free (aio_req req)
536{ 581{
537 if (req->self) 582 if (req->self)
592#endif 637#endif
593 638
594 sigfillset (&fullsigset); 639 sigfillset (&fullsigset);
595 640
596 LOCK (wrklock); 641 LOCK (wrklock);
597 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); 642 pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset);
598 643
599 if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0) 644 if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0)
600 { 645 {
601 wrk->prev = &wrk_first; 646 wrk->prev = &wrk_first;
602 wrk->next = wrk_first.next; 647 wrk->next = wrk_first.next;
605 ++started; 650 ++started;
606 } 651 }
607 else 652 else
608 free (wrk); 653 free (wrk);
609 654
610 sigprocmask (SIG_SETMASK, &oldsigset, 0); 655 pthread_sigmask (SIG_SETMASK, &oldsigset, 0);
611 UNLOCK (wrklock); 656 UNLOCK (wrklock);
612} 657}
613 658
614static void maybe_start_thread () 659static void maybe_start_thread ()
615{ 660{
623 start_thread (); 668 start_thread ();
624} 669}
625 670
626static void req_send (aio_req req) 671static void req_send (aio_req req)
627{ 672{
673 block_sig ();
674
628 ++nreqs; 675 ++nreqs;
629 676
630 LOCK (reqlock); 677 LOCK (reqlock);
631 ++nready; 678 ++nready;
632 reqq_push (&req_queue, req); 679 reqq_push (&req_queue, req);
633 pthread_cond_signal (&reqwait); 680 pthread_cond_signal (&reqwait);
634 UNLOCK (reqlock); 681 UNLOCK (reqlock);
682
683 unblock_sig ();
635 684
636 maybe_start_thread (); 685 maybe_start_thread ();
637} 686}
638 687
639static void end_thread (void) 688static void end_thread (void)
709 struct timeval tv_start, tv_now; 758 struct timeval tv_start, tv_now;
710 aio_req req; 759 aio_req req;
711 760
712 if (max_poll_time) 761 if (max_poll_time)
713 gettimeofday (&tv_start, 0); 762 gettimeofday (&tv_start, 0);
763
764 block_sig ();
714 765
715 for (;;) 766 for (;;)
716 { 767 {
717 for (;;) 768 for (;;)
718 { 769 {
746 req->int1 = 1; /* mark request as delayed */ 797 req->int1 = 1; /* mark request as delayed */
747 continue; 798 continue;
748 } 799 }
749 else 800 else
750 { 801 {
751 req_invoke (req); 802 if (!req_invoke (req))
803 {
804 req_free (req);
805 unblock_sig ();
806 croak (0);
807 }
752 808
753 count++; 809 count++;
754 } 810 }
755 811
756 req_free (req); 812 req_free (req);
773 poll_wait (); 829 poll_wait ();
774 830
775 ++maxreqs; 831 ++maxreqs;
776 } 832 }
777 833
834 unblock_sig ();
778 return count; 835 return count;
779} 836}
780 837
781static void create_pipe () 838static void create_pipe ()
782{ 839{
1153 LOCK (reslock); 1210 LOCK (reslock);
1154 1211
1155 ++npending; 1212 ++npending;
1156 1213
1157 if (!reqq_push (&res_queue, req)) 1214 if (!reqq_push (&res_queue, req))
1215 {
1158 /* write a dummy byte to the pipe so fh becomes ready */ 1216 /* write a dummy byte to the pipe so fh becomes ready */
1159 write (respipe [1], &respipe, 1); 1217 write (respipe [1], &respipe, 1);
1218
1219 /* optionally signal the main thread asynchronously */
1220 if (main_sig)
1221 pthread_kill (main_tid, main_sig);
1222 }
1160 1223
1161 self->req = 0; 1224 self->req = 0;
1162 worker_clear (self); 1225 worker_clear (self);
1163 1226
1164 UNLOCK (reslock); 1227 UNLOCK (reslock);
1267 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); 1330 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
1268 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); 1331 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
1269 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); 1332 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
1270 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); 1333 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
1271 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); 1334 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
1335 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO));
1272 1336
1273 create_pipe (); 1337 create_pipe ();
1274 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 1338 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
1275} 1339}
1276 1340
1309 OUTPUT: 1373 OUTPUT:
1310 RETVAL 1374 RETVAL
1311 1375
1312void 1376void
1313aio_open (pathname,flags,mode,callback=&PL_sv_undef) 1377aio_open (pathname,flags,mode,callback=&PL_sv_undef)
1314 SV * pathname 1378 SV8 * pathname
1315 int flags 1379 int flags
1316 int mode 1380 int mode
1317 SV * callback 1381 SV * callback
1318 PROTOTYPE: $$$;$ 1382 PROTOTYPE: $$$;$
1319 PPCODE: 1383 PPCODE:
1320{ 1384{
1321 dREQ; 1385 dREQ;
1322 1386
1323 req->type = REQ_OPEN; 1387 req->type = REQ_OPEN;
1324 req->sv1 = newSVsv (pathname); 1388 req->sv1 = newSVsv (pathname);
1325 req->ptr1 = SvPVbyte_nolen (pathname); 1389 req->ptr1 = SvPVbyte_nolen (req->sv1);
1326 req->int1 = flags; 1390 req->int1 = flags;
1327 req->mode = mode; 1391 req->mode = mode;
1328 1392
1329 REQ_SEND; 1393 REQ_SEND;
1330} 1394}
1352void 1416void
1353aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 1417aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
1354 SV * fh 1418 SV * fh
1355 UV offset 1419 UV offset
1356 UV length 1420 UV length
1357 SV * data 1421 SV8 * data
1358 UV dataoffset 1422 UV dataoffset
1359 SV * callback 1423 SV * callback
1360 ALIAS: 1424 ALIAS:
1361 aio_read = REQ_READ 1425 aio_read = REQ_READ
1362 aio_write = REQ_WRITE 1426 aio_write = REQ_WRITE
1413 } 1477 }
1414} 1478}
1415 1479
1416void 1480void
1417aio_readlink (path,callback=&PL_sv_undef) 1481aio_readlink (path,callback=&PL_sv_undef)
1418 SV * path 1482 SV8 * path
1419 SV * callback 1483 SV * callback
1420 PROTOTYPE: $$;$ 1484 PROTOTYPE: $$;$
1421 PPCODE: 1485 PPCODE:
1422{ 1486{
1423 SV *data; 1487 SV *data;
1426 data = newSV (NAME_MAX); 1490 data = newSV (NAME_MAX);
1427 SvPOK_on (data); 1491 SvPOK_on (data);
1428 1492
1429 req->type = REQ_READLINK; 1493 req->type = REQ_READLINK;
1430 req->fh = newSVsv (path); 1494 req->fh = newSVsv (path);
1431 req->ptr2 = SvPVbyte_nolen (path); 1495 req->ptr2 = SvPVbyte_nolen (req->fh);
1432 req->sv1 = data; 1496 req->sv1 = data;
1433 req->ptr1 = SvPVbyte_nolen (data); 1497 req->ptr1 = SvPVbyte_nolen (data);
1434 1498
1435 REQ_SEND; 1499 REQ_SEND;
1436} 1500}
1478 REQ_SEND; 1542 REQ_SEND;
1479} 1543}
1480 1544
1481void 1545void
1482aio_stat (fh_or_path,callback=&PL_sv_undef) 1546aio_stat (fh_or_path,callback=&PL_sv_undef)
1483 SV * fh_or_path 1547 SV8 * fh_or_path
1484 SV * callback 1548 SV * callback
1485 ALIAS: 1549 ALIAS:
1486 aio_stat = REQ_STAT 1550 aio_stat = REQ_STAT
1487 aio_lstat = REQ_LSTAT 1551 aio_lstat = REQ_LSTAT
1488 PPCODE: 1552 PPCODE:
1500 1564
1501 if (SvPOK (fh_or_path)) 1565 if (SvPOK (fh_or_path))
1502 { 1566 {
1503 req->type = ix; 1567 req->type = ix;
1504 req->sv1 = newSVsv (fh_or_path); 1568 req->sv1 = newSVsv (fh_or_path);
1505 req->ptr1 = SvPVbyte_nolen (fh_or_path); 1569 req->ptr1 = SvPVbyte_nolen (req->sv1);
1506 } 1570 }
1507 else 1571 else
1508 { 1572 {
1509 req->type = REQ_FSTAT; 1573 req->type = REQ_FSTAT;
1510 req->fh = newSVsv (fh_or_path); 1574 req->fh = newSVsv (fh_or_path);
1514 REQ_SEND; 1578 REQ_SEND;
1515} 1579}
1516 1580
1517void 1581void
1518aio_unlink (pathname,callback=&PL_sv_undef) 1582aio_unlink (pathname,callback=&PL_sv_undef)
1519 SV * pathname 1583 SV8 * pathname
1520 SV * callback 1584 SV * callback
1521 ALIAS: 1585 ALIAS:
1522 aio_unlink = REQ_UNLINK 1586 aio_unlink = REQ_UNLINK
1523 aio_rmdir = REQ_RMDIR 1587 aio_rmdir = REQ_RMDIR
1524 aio_readdir = REQ_READDIR 1588 aio_readdir = REQ_READDIR
1526{ 1590{
1527 dREQ; 1591 dREQ;
1528 1592
1529 req->type = ix; 1593 req->type = ix;
1530 req->sv1 = newSVsv (pathname); 1594 req->sv1 = newSVsv (pathname);
1531 req->ptr1 = SvPVbyte_nolen (pathname); 1595 req->ptr1 = SvPVbyte_nolen (req->sv1);
1532 1596
1533 REQ_SEND; 1597 REQ_SEND;
1534} 1598}
1535 1599
1536void 1600void
1537aio_link (oldpath,newpath,callback=&PL_sv_undef) 1601aio_link (oldpath,newpath,callback=&PL_sv_undef)
1538 SV * oldpath 1602 SV8 * oldpath
1539 SV * newpath 1603 SV8 * newpath
1540 SV * callback 1604 SV * callback
1541 ALIAS: 1605 ALIAS:
1542 aio_link = REQ_LINK 1606 aio_link = REQ_LINK
1543 aio_symlink = REQ_SYMLINK 1607 aio_symlink = REQ_SYMLINK
1544 aio_rename = REQ_RENAME 1608 aio_rename = REQ_RENAME
1548 1612
1549 req->type = ix; 1613 req->type = ix;
1550 req->fh = newSVsv (oldpath); 1614 req->fh = newSVsv (oldpath);
1551 req->ptr2 = SvPVbyte_nolen (req->fh); 1615 req->ptr2 = SvPVbyte_nolen (req->fh);
1552 req->sv1 = newSVsv (newpath); 1616 req->sv1 = newSVsv (newpath);
1553 req->ptr1 = SvPVbyte_nolen (newpath); 1617 req->ptr1 = SvPVbyte_nolen (req->sv1);
1554 1618
1555 REQ_SEND; 1619 REQ_SEND;
1556} 1620}
1557 1621
1558void 1622void
1559aio_mknod (pathname,mode,dev,callback=&PL_sv_undef) 1623aio_mknod (pathname,mode,dev,callback=&PL_sv_undef)
1560 SV * pathname 1624 SV8 * pathname
1561 SV * callback 1625 SV * callback
1562 UV mode 1626 UV mode
1563 UV dev 1627 UV dev
1564 PPCODE: 1628 PPCODE:
1565{ 1629{
1566 dREQ; 1630 dREQ;
1567 1631
1568 req->type = REQ_MKNOD; 1632 req->type = REQ_MKNOD;
1569 req->sv1 = newSVsv (pathname); 1633 req->sv1 = newSVsv (pathname);
1570 req->ptr1 = SvPVbyte_nolen (pathname); 1634 req->ptr1 = SvPVbyte_nolen (req->sv1);
1571 req->mode = (mode_t)mode; 1635 req->mode = (mode_t)mode;
1572 req->offs = dev; 1636 req->offs = dev;
1573 1637
1574 REQ_SEND; 1638 REQ_SEND;
1575} 1639}
1642 PROTOTYPE: 1706 PROTOTYPE:
1643 CODE: 1707 CODE:
1644 while (nreqs) 1708 while (nreqs)
1645 { 1709 {
1646 poll_wait (); 1710 poll_wait ();
1647 poll_cb (0); 1711 poll_cb ();
1648 } 1712 }
1649 1713
1650void 1714int
1651poll() 1715poll()
1652 PROTOTYPE: 1716 PROTOTYPE:
1653 CODE: 1717 CODE:
1654 if (nreqs)
1655 {
1656 poll_wait (); 1718 poll_wait ();
1657 poll_cb (0); 1719 RETVAL = poll_cb ();
1658 } 1720 OUTPUT:
1721 RETVAL
1659 1722
1660int 1723int
1661poll_fileno() 1724poll_fileno()
1662 PROTOTYPE: 1725 PROTOTYPE:
1663 CODE: 1726 CODE:
1675 1738
1676void 1739void
1677poll_wait() 1740poll_wait()
1678 PROTOTYPE: 1741 PROTOTYPE:
1679 CODE: 1742 CODE:
1680 if (nreqs)
1681 poll_wait (); 1743 poll_wait ();
1744
1745void
1746setsig (int signum = SIGIO)
1747 PROTOTYPE: ;$
1748 CODE:
1749{
1750 if (block_sig_level)
1751 croak ("cannot call IO::AIO::setsig from within aio_block/callback");
1752
1753 LOCK (reslock);
1754 main_tid = pthread_self ();
1755 main_sig = signum;
1756 UNLOCK (reslock);
1757
1758 if (main_sig && npending)
1759 pthread_kill (main_tid, main_sig);
1760}
1761
1762void
1763aio_block (SV *cb)
1764 PROTOTYPE: &
1765 PPCODE:
1766{
1767 int count;
1768
1769 block_sig ();
1770 PUSHMARK (SP);
1771 PUTBACK;
1772 count = call_sv (cb, GIMME_V | G_NOARGS | G_EVAL);
1773 SPAGAIN;
1774 unblock_sig ();
1775
1776 if (SvTRUE (ERRSV))
1777 croak (0);
1778
1779 XSRETURN (count);
1780}
1682 1781
1683int 1782int
1684nreqs() 1783nreqs()
1685 PROTOTYPE: 1784 PROTOTYPE:
1686 CODE: 1785 CODE:
1736 PPCODE: 1835 PPCODE:
1737{ 1836{
1738 int i; 1837 int i;
1739 aio_req req; 1838 aio_req req;
1740 1839
1840 if (main_sig && !block_sig_level)
1841 croak ("aio_group->add called outside aio_block/callback context while IO::AIO::setsig is in use");
1842
1741 if (grp->int1 == 2) 1843 if (grp->int1 == 2)
1742 croak ("cannot add requests to IO::AIO::GRP after the group finished"); 1844 croak ("cannot add requests to IO::AIO::GRP after the group finished");
1743 1845
1744 for (i = 1; i < items; ++i ) 1846 for (i = 1; i < items; ++i )
1745 { 1847 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines