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.71 by root, Tue Oct 24 16:35:04 2006 UTC vs.
Revision 1.79 by root, Thu Oct 26 16:28:33 2006 UTC

1/* solaris */ 1/* solaris */
2#define _POSIX_PTHREAD_SEMANTICS 1 2#define _POSIX_PTHREAD_SEMANTICS 1
3 3
4#if __linux 4#if __linux && !defined(_GNU_SOURCE)
5# define _GNU_SOURCE 5# define _GNU_SOURCE
6#endif 6#endif
7 7
8/* just in case */
8#define _REENTRANT 1 9#define _REENTRANT 1
9 10
10#include <errno.h> 11#include <errno.h>
11 12
12#include "EXTERN.h" 13#include "EXTERN.h"
47/* used for struct dirent, AIX doesn't provide it */ 48/* used for struct dirent, AIX doesn't provide it */
48#ifndef NAME_MAX 49#ifndef NAME_MAX
49# define NAME_MAX 4096 50# define NAME_MAX 4096
50#endif 51#endif
51 52
53#ifndef PTHREAD_STACK_MIN
54/* care for broken platforms, e.g. windows */
55# define PTHREAD_STACK_MIN 16384
56#endif
57
52#if __ia64 58#if __ia64
53# define STACKSIZE 65536 59# define STACKSIZE 65536
54#elif __i386 || __x86_64 /* 16k is unreasonably high :( */ 60#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
55# define STACKSIZE PTHREAD_STACK_MIN 61# define STACKSIZE PTHREAD_STACK_MIN
56#else 62#else
57# define STACKSIZE 16384 63# define STACKSIZE 16384
64#endif
65
66/* wether word reads are potentially non-atomic.
67 * this is conservatice, likely most arches this runs
68 * on have atomic word read/writes.
69 */
70#ifndef WORDREAD_UNSAFE
71# if __i386 || __x86_64
72# define WORDREAD_UNSAFE 0
73# else
74# define WORDREAD_UNSAFE 1
75# endif
58#endif 76#endif
59 77
60/* buffer size for various temporary buffers */ 78/* buffer size for various temporary buffers */
61#define AIO_BUFSIZE 65536 79#define AIO_BUFSIZE 65536
62 80
126 NUM_PRI = PRI_MAX + PRI_BIAS + 1, 144 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
127}; 145};
128 146
129static int next_pri = DEFAULT_PRI + PRI_BIAS; 147static int next_pri = DEFAULT_PRI + PRI_BIAS;
130 148
131static int started, wanted; 149static unsigned int started, wanted;
132static volatile int nreqs; 150static volatile unsigned int nreqs, nready, npending;
133static int max_outstanding = 1<<30; 151static volatile unsigned int max_outstanding = 0xffffffff;
134static int respipe [2]; 152static int respipe [2];
135 153
136#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 154#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
137# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 155# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
138#else 156#else
140#endif 158#endif
141 159
142#define LOCK(mutex) pthread_mutex_lock (&(mutex)) 160#define LOCK(mutex) pthread_mutex_lock (&(mutex))
143#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 161#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
144 162
145/* worker threasd management */ 163/* worker threads management */
146static pthread_mutex_t wrklock = AIO_MUTEX_INIT; 164static pthread_mutex_t wrklock = AIO_MUTEX_INIT;
147 165
148typedef struct worker { 166typedef struct worker {
149 /* locked by wrklock */ 167 /* locked by wrklock */
150 struct worker *prev, *next; 168 struct worker *prev, *next;
238 } 256 }
239 257
240 abort (); 258 abort ();
241} 259}
242 260
261static int poll_cb (int max);
243static void req_invoke (aio_req req); 262static void req_invoke (aio_req req);
244static void req_free (aio_req req); 263static void req_free (aio_req req);
264static void req_cancel (aio_req req);
245 265
246/* must be called at most once */ 266/* must be called at most once */
247static SV *req_sv (aio_req req, const char *klass) 267static SV *req_sv (aio_req req, const char *klass)
248{ 268{
249 if (!req->self) 269 if (!req->self)
318 fd_set rfd; 338 fd_set rfd;
319 339
320 while (nreqs) 340 while (nreqs)
321 { 341 {
322 int size; 342 int size;
323#if !(__i386 || __x86_64) /* safe without sempahore on these archs */ 343 if (WORDREAD_UNSAFE) LOCK (reslock);
324 LOCK (reslock);
325#endif
326 size = res_queue.size; 344 size = res_queue.size;
327#if !(__i386 || __x86_64) /* safe without sempahore on these archs */ 345 if (WORDREAD_UNSAFE) UNLOCK (reslock);
328 UNLOCK (reslock);
329#endif
330 346
331 if (size) 347 if (size)
332 return; 348 return;
333 349
334 FD_ZERO(&rfd); 350 FD_ZERO(&rfd);
342{ 358{
343 dSP; 359 dSP;
344 360
345 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) 361 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
346 { 362 {
347 errno = req->errorno;
348
349 ENTER; 363 ENTER;
350 SAVETMPS; 364 SAVETMPS;
351 PUSHMARK (SP); 365 PUSHMARK (SP);
352 EXTEND (SP, 1); 366 EXTEND (SP, 1);
353 367
418 default: 432 default:
419 PUSHs (sv_2mortal (newSViv (req->result))); 433 PUSHs (sv_2mortal (newSViv (req->result)));
420 break; 434 break;
421 } 435 }
422 436
437 errno = req->errorno;
423 438
424 PUTBACK; 439 PUTBACK;
425 call_sv (req->callback, G_VOID | G_EVAL); 440 call_sv (req->callback, G_VOID | G_EVAL);
426 SPAGAIN; 441 SPAGAIN;
427 442
468 free (req->data2ptr); 483 free (req->data2ptr);
469 484
470 Safefree (req); 485 Safefree (req);
471} 486}
472 487
488static void req_cancel_subs (aio_req grp)
489{
490 aio_req sub;
491
492 if (grp->type != REQ_GROUP)
493 return;
494
495 SvREFCNT_dec (grp->fh2);
496 grp->fh2 = 0;
497
498 for (sub = grp->grp_first; sub; sub = sub->grp_next)
499 req_cancel (sub);
500}
501
473static void req_cancel (aio_req req) 502static void req_cancel (aio_req req)
474{ 503{
475 req->flags |= FLAG_CANCELLED; 504 req->flags |= FLAG_CANCELLED;
476 505
477 if (req->type == REQ_GROUP) 506 req_cancel_subs (req);
478 {
479 aio_req sub;
480
481 for (sub = req->grp_first; sub; sub = sub->grp_next)
482 req_cancel (sub);
483 }
484} 507}
485 508
486static int poll_cb () 509static int poll_cb (int max)
487{ 510{
488 dSP; 511 dSP;
489 int count = 0; 512 int count = 0;
490 int do_croak = 0; 513 int do_croak = 0;
491 aio_req req; 514 aio_req req;
492 515
493 for (;;) 516 for (;;)
494 { 517 {
495 LOCK (reslock); 518 while (max <= 0 || count < max)
496 req = reqq_shift (&res_queue);
497
498 if (req)
499 { 519 {
520 LOCK (reslock);
521 req = reqq_shift (&res_queue);
522
500 if (!res_queue.size) 523 if (req)
501 { 524 {
525 --npending;
526
527 if (!res_queue.size)
528 {
502 /* read any signals sent by the worker threads */ 529 /* read any signals sent by the worker threads */
503 char buf [32]; 530 char buf [32];
504 while (read (respipe [0], buf, 32) == 32) 531 while (read (respipe [0], buf, 32) == 32)
532 ;
505 ; 533 }
506 } 534 }
535
536 UNLOCK (reslock);
537
538 if (!req)
539 break;
540
541 --nreqs;
542
543 if (req->type == REQ_QUIT)
544 --started;
545 else if (req->type == REQ_GROUP && req->length)
546 {
547 req->fd = 1; /* mark request as delayed */
548 continue;
549 }
550 else
551 {
552 if (req->type == REQ_READ)
553 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
554
555 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
556 SvREADONLY_off (req->data);
557
558 if (req->statdata)
559 {
560 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
561 PL_laststatval = req->result;
562 PL_statcache = *(req->statdata);
563 }
564
565 req_invoke (req);
566
567 count++;
568 }
569
570 req_free (req);
507 } 571 }
508 572
509 UNLOCK (reslock); 573 if (nreqs <= max_outstanding)
510
511 if (!req)
512 break; 574 break;
513 575
514 --nreqs; 576 poll_wait ();
515 577
516 if (req->type == REQ_QUIT) 578 max = 0;
517 started--;
518 else if (req->type == REQ_GROUP && req->length)
519 {
520 req->fd = 1; /* mark request as delayed */
521 continue;
522 }
523 else
524 {
525 if (req->type == REQ_READ)
526 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
527
528 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
529 SvREADONLY_off (req->data);
530
531 if (req->statdata)
532 {
533 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
534 PL_laststatval = req->result;
535 PL_statcache = *(req->statdata);
536 }
537
538 req_invoke (req);
539
540 count++;
541 }
542
543 req_free (req);
544 } 579 }
545 580
546 return count; 581 return count;
547} 582}
548 583
549static void *aio_proc(void *arg); 584static void *aio_proc(void *arg);
550 585
551static void start_thread (void) 586static void start_thread (void)
552{ 587{
588 sigset_t fullsigset, oldsigset;
589 pthread_attr_t attr;
590
553 worker *wrk = calloc (1, sizeof (worker)); 591 worker *wrk = calloc (1, sizeof (worker));
554 592
555 if (!wrk) 593 if (!wrk)
556 croak ("unable to allocate worker thread data"); 594 croak ("unable to allocate worker thread data");
557
558 sigset_t fullsigset, oldsigset;
559 pthread_attr_t attr;
560 595
561 pthread_attr_init (&attr); 596 pthread_attr_init (&attr);
562 pthread_attr_setstacksize (&attr, STACKSIZE); 597 pthread_attr_setstacksize (&attr, STACKSIZE);
563 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 598 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
564 599
571 { 606 {
572 wrk->prev = &wrk_first; 607 wrk->prev = &wrk_first;
573 wrk->next = wrk_first.next; 608 wrk->next = wrk_first.next;
574 wrk_first.next->prev = wrk; 609 wrk_first.next->prev = wrk;
575 wrk_first.next = wrk; 610 wrk_first.next = wrk;
576 started++; 611 ++started;
577 } 612 }
578 else 613 else
579 free (wrk); 614 free (wrk);
580 615
581 sigprocmask (SIG_SETMASK, &oldsigset, 0); 616 sigprocmask (SIG_SETMASK, &oldsigset, 0);
588 start_thread (); 623 start_thread ();
589 624
590 ++nreqs; 625 ++nreqs;
591 626
592 LOCK (reqlock); 627 LOCK (reqlock);
628 ++nready;
593 reqq_push (&req_queue, req); 629 reqq_push (&req_queue, req);
594 pthread_cond_signal (&reqwait); 630 pthread_cond_signal (&reqwait);
595 UNLOCK (reqlock); 631 UNLOCK (reqlock);
596
597 if (nreqs > max_outstanding)
598 for (;;)
599 {
600 poll_cb ();
601
602 if (nreqs <= max_outstanding)
603 break;
604
605 poll_wait ();
606 }
607} 632}
608 633
609static void end_thread (void) 634static void end_thread (void)
610{ 635{
611 aio_req req; 636 aio_req req;
638 } 663 }
639 664
640 while (started > wanted) 665 while (started > wanted)
641 { 666 {
642 poll_wait (); 667 poll_wait ();
643 poll_cb (); 668 poll_cb (0);
644 } 669 }
645} 670}
646 671
647static void create_pipe () 672static void create_pipe ()
648{ 673{
704#if !HAVE_FDATASYNC 729#if !HAVE_FDATASYNC
705# define fdatasync fsync 730# define fdatasync fsync
706#endif 731#endif
707 732
708#if !HAVE_READAHEAD 733#if !HAVE_READAHEAD
709# define readahead aio_readahead 734# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self)
710 735
711static ssize_t readahead (int fd, off_t offset, size_t count) 736static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self)
712{ 737{
713 dBUF; 738 dBUF;
714 739
715 while (count > 0) 740 while (count > 0)
716 { 741 {
721 count -= len; 746 count -= len;
722 } 747 }
723 748
724 errno = 0; 749 errno = 0;
725} 750}
751
726#endif 752#endif
727 753
728#if !HAVE_READDIR_R 754#if !HAVE_READDIR_R
729# define readdir_r aio_readdir_r 755# define readdir_r aio_readdir_r
730 756
775 { 801 {
776 off_t sbytes; 802 off_t sbytes;
777 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 803 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
778 804
779 if (res < 0 && sbytes) 805 if (res < 0 && sbytes)
780 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ 806 /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */
781 res = sbytes; 807 res = sbytes;
782 } 808 }
783 809
784# elif __hpux 810# elif __hpux
785 res = sendfile (ofd, ifd, offset, count, 0, 0); 811 res = sendfile (ofd, ifd, offset, count, 0, 0);
865 int errorno; 891 int errorno;
866 892
867 LOCK (wrklock); 893 LOCK (wrklock);
868 self->dirp = dirp = opendir (req->dataptr); 894 self->dirp = dirp = opendir (req->dataptr);
869 self->dbuf = u = malloc (sizeof (*u)); 895 self->dbuf = u = malloc (sizeof (*u));
896 req->data2ptr = names = malloc (memlen);
870 UNLOCK (wrklock); 897 UNLOCK (wrklock);
871
872 req->data2ptr = names = malloc (memlen);
873 898
874 if (dirp && u && names) 899 if (dirp && u && names)
875 for (;;) 900 for (;;)
876 { 901 {
877 errno = 0; 902 errno = 0;
929 if (req) 954 if (req)
930 break; 955 break;
931 956
932 pthread_cond_wait (&reqwait, &reqlock); 957 pthread_cond_wait (&reqwait, &reqlock);
933 } 958 }
959
960 --nready;
934 961
935 UNLOCK (reqlock); 962 UNLOCK (reqlock);
936 963
937 errno = 0; /* strictly unnecessary */ 964 errno = 0; /* strictly unnecessary */
938 type = req->type; /* remember type for QUIT check */ 965 type = req->type; /* remember type for QUIT check */
983 } 1010 }
984 1011
985 req->errorno = errno; 1012 req->errorno = errno;
986 1013
987 LOCK (reslock); 1014 LOCK (reslock);
1015
1016 ++npending;
988 1017
989 if (!reqq_push (&res_queue, req)) 1018 if (!reqq_push (&res_queue, req))
990 /* write a dummy byte to the pipe so fh becomes ready */ 1019 /* write a dummy byte to the pipe so fh becomes ready */
991 write (respipe [1], &respipe, 1); 1020 write (respipe [1], &respipe, 1);
992 1021
1098 create_pipe (); 1127 create_pipe ();
1099 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 1128 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
1100} 1129}
1101 1130
1102void 1131void
1103min_parallel (nthreads) 1132min_parallel (int nthreads)
1104 int nthreads
1105 PROTOTYPE: $ 1133 PROTOTYPE: $
1106 1134
1107void 1135void
1108max_parallel (nthreads) 1136max_parallel (int nthreads)
1109 int nthreads
1110 PROTOTYPE: $ 1137 PROTOTYPE: $
1111 1138
1112int 1139int
1113max_outstanding (nreqs) 1140max_outstanding (int maxreqs)
1114 int nreqs 1141 PROTOTYPE: $
1115 PROTOTYPE: $
1116 CODE: 1142 CODE:
1117 RETVAL = max_outstanding; 1143 RETVAL = max_outstanding;
1118 max_outstanding = nreqs; 1144 max_outstanding = maxreqs;
1145 OUTPUT:
1146 RETVAL
1119 1147
1120void 1148void
1121aio_open (pathname,flags,mode,callback=&PL_sv_undef) 1149aio_open (pathname,flags,mode,callback=&PL_sv_undef)
1122 SV * pathname 1150 SV * pathname
1123 int flags 1151 int flags
1378 req->type = REQ_NOP; 1406 req->type = REQ_NOP;
1379 1407
1380 REQ_SEND; 1408 REQ_SEND;
1381} 1409}
1382 1410
1383void 1411int
1384aioreq_pri (int pri = DEFAULT_PRI) 1412aioreq_pri (int pri = 0)
1385 CODE: 1413 PROTOTYPE: ;$
1414 CODE:
1415 RETVAL = next_pri - PRI_BIAS;
1416 if (items > 0)
1417 {
1386 if (pri < PRI_MIN) pri = PRI_MIN; 1418 if (pri < PRI_MIN) pri = PRI_MIN;
1387 if (pri > PRI_MAX) pri = PRI_MAX; 1419 if (pri > PRI_MAX) pri = PRI_MAX;
1388 next_pri = pri + PRI_BIAS; 1420 next_pri = pri + PRI_BIAS;
1421 }
1422 OUTPUT:
1423 RETVAL
1389 1424
1390void 1425void
1391aioreq_nice (int nice = 0) 1426aioreq_nice (int nice = 0)
1392 CODE: 1427 CODE:
1393 nice = next_pri - nice; 1428 nice = next_pri - nice;
1394 if (nice < PRI_MIN) nice = PRI_MIN; 1429 if (nice < PRI_MIN) nice = PRI_MIN;
1395 if (nice > PRI_MAX) nice = PRI_MAX; 1430 if (nice > PRI_MAX) nice = PRI_MAX;
1396 next_pri = nice + PRI_BIAS; 1431 next_pri = nice + PRI_BIAS;
1397 1432
1398void 1433void
1399flush () 1434flush ()
1400 PROTOTYPE: 1435 PROTOTYPE:
1401 CODE: 1436 CODE:
1402 while (nreqs) 1437 while (nreqs)
1403 { 1438 {
1404 poll_wait (); 1439 poll_wait ();
1405 poll_cb (); 1440 poll_cb (0);
1406 } 1441 }
1407 1442
1408void 1443void
1409poll() 1444poll()
1410 PROTOTYPE: 1445 PROTOTYPE:
1411 CODE: 1446 CODE:
1412 if (nreqs) 1447 if (nreqs)
1413 { 1448 {
1414 poll_wait (); 1449 poll_wait ();
1415 poll_cb (); 1450 poll_cb (0);
1416 } 1451 }
1417 1452
1418int 1453int
1419poll_fileno() 1454poll_fileno()
1420 PROTOTYPE: 1455 PROTOTYPE:
1425 1460
1426int 1461int
1427poll_cb(...) 1462poll_cb(...)
1428 PROTOTYPE: 1463 PROTOTYPE:
1429 CODE: 1464 CODE:
1430 RETVAL = poll_cb (); 1465 RETVAL = poll_cb (0);
1466 OUTPUT:
1467 RETVAL
1468
1469int
1470poll_some(int max = 0)
1471 PROTOTYPE: $
1472 CODE:
1473 RETVAL = poll_cb (max);
1431 OUTPUT: 1474 OUTPUT:
1432 RETVAL 1475 RETVAL
1433 1476
1434void 1477void
1435poll_wait() 1478poll_wait()
1444 CODE: 1487 CODE:
1445 RETVAL = nreqs; 1488 RETVAL = nreqs;
1446 OUTPUT: 1489 OUTPUT:
1447 RETVAL 1490 RETVAL
1448 1491
1492int
1493nready()
1494 PROTOTYPE:
1495 CODE:
1496 if (WORDREAD_UNSAFE) LOCK (reqlock);
1497 RETVAL = nready;
1498 if (WORDREAD_UNSAFE) UNLOCK (reqlock);
1499 OUTPUT:
1500 RETVAL
1501
1502int
1503npending()
1504 PROTOTYPE:
1505 CODE:
1506 if (WORDREAD_UNSAFE) LOCK (reslock);
1507 RETVAL = npending;
1508 if (WORDREAD_UNSAFE) UNLOCK (reslock);
1509 OUTPUT:
1510 RETVAL
1511
1449PROTOTYPES: DISABLE 1512PROTOTYPES: DISABLE
1450 1513
1451MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1514MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1452 1515
1453void 1516void
1495 } 1558 }
1496 } 1559 }
1497} 1560}
1498 1561
1499void 1562void
1563cancel_subs (aio_req_ornot req)
1564 CODE:
1565 req_cancel_subs (req);
1566
1567void
1500result (aio_req grp, ...) 1568result (aio_req grp, ...)
1501 CODE: 1569 CODE:
1502{ 1570{
1503 int i; 1571 int i;
1572 AV *av;
1573
1574 grp->errorno = errno;
1575
1504 AV *av = newAV (); 1576 av = newAV ();
1505 1577
1506 for (i = 1; i < items; ++i ) 1578 for (i = 1; i < items; ++i )
1507 av_push (av, newSVsv (ST (i))); 1579 av_push (av, newSVsv (ST (i)));
1508 1580
1509 SvREFCNT_dec (grp->data); 1581 SvREFCNT_dec (grp->data);
1510 grp->data = (SV *)av; 1582 grp->data = (SV *)av;
1511} 1583}
1584
1585void
1586errno (aio_req grp, int errorno = errno)
1587 CODE:
1588 grp->errorno = errorno;
1512 1589
1513void 1590void
1514limit (aio_req grp, int limit) 1591limit (aio_req grp, int limit)
1515 CODE: 1592 CODE:
1516 grp->fd2 = limit; 1593 grp->fd2 = limit;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines