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.89 by root, Tue Oct 31 00:11:52 2006 UTC vs.
Revision 1.95 by root, Sun Nov 26 22:47:58 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,
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);
161} 164}
162 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);
198}
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;
166 203
167#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 204#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
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)
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);
714 763
764 block_sig ();
765
715 for (;;) 766 for (;;)
716 { 767 {
717 for (;;) 768 for (;;)
718 { 769 {
719 maybe_start_thread (); 770 maybe_start_thread ();
726 --npending; 777 --npending;
727 778
728 if (!res_queue.size) 779 if (!res_queue.size)
729 { 780 {
730 /* read any signals sent by the worker threads */ 781 /* read any signals sent by the worker threads */
731 char buf [32]; 782 char buf [4];
732 while (read (respipe [0], buf, 32) == 32) 783 while (read (respipe [0], buf, 4) == 4)
733 ; 784 ;
734 } 785 }
735 } 786 }
736 787
737 UNLOCK (reslock); 788 UNLOCK (reslock);
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:
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;
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:
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
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
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{
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