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.78 by root, Thu Oct 26 14:35:34 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
126 NUM_PRI = PRI_MAX + PRI_BIAS + 1, 132 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
127}; 133};
128 134
129static int next_pri = DEFAULT_PRI + PRI_BIAS; 135static int next_pri = DEFAULT_PRI + PRI_BIAS;
130 136
131static int started, wanted; 137static unsigned int started, wanted;
132static volatile int nreqs; 138static volatile unsigned int nreqs;
133static int max_outstanding = 1<<30; 139static volatile unsigned int max_outstanding = 0xffffffff;
134static int respipe [2]; 140static int respipe [2];
135 141
136#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 142#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
137# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 143# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
138#else 144#else
140#endif 146#endif
141 147
142#define LOCK(mutex) pthread_mutex_lock (&(mutex)) 148#define LOCK(mutex) pthread_mutex_lock (&(mutex))
143#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 149#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
144 150
145/* worker threasd management */ 151/* worker threads management */
146static pthread_mutex_t wrklock = AIO_MUTEX_INIT; 152static pthread_mutex_t wrklock = AIO_MUTEX_INIT;
147 153
148typedef struct worker { 154typedef struct worker {
149 /* locked by wrklock */ 155 /* locked by wrklock */
150 struct worker *prev, *next; 156 struct worker *prev, *next;
238 } 244 }
239 245
240 abort (); 246 abort ();
241} 247}
242 248
249static int poll_cb (int max);
243static void req_invoke (aio_req req); 250static void req_invoke (aio_req req);
244static void req_free (aio_req req); 251static void req_free (aio_req req);
252static void req_cancel (aio_req req);
245 253
246/* must be called at most once */ 254/* must be called at most once */
247static SV *req_sv (aio_req req, const char *klass) 255static SV *req_sv (aio_req req, const char *klass)
248{ 256{
249 if (!req->self) 257 if (!req->self)
468 free (req->data2ptr); 476 free (req->data2ptr);
469 477
470 Safefree (req); 478 Safefree (req);
471} 479}
472 480
481static void req_cancel_subs (aio_req grp)
482{
483 aio_req sub;
484
485 if (grp->type != REQ_GROUP)
486 return;
487
488 SvREFCNT_dec (grp->fh2);
489 grp->fh2 = 0;
490
491 for (sub = grp->grp_first; sub; sub = sub->grp_next)
492 req_cancel (sub);
493}
494
473static void req_cancel (aio_req req) 495static void req_cancel (aio_req req)
474{ 496{
475 req->flags |= FLAG_CANCELLED; 497 req->flags |= FLAG_CANCELLED;
476 498
477 if (req->type == REQ_GROUP) 499 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} 500}
485 501
486static int poll_cb () 502static int poll_cb (int max)
487{ 503{
488 dSP; 504 dSP;
489 int count = 0; 505 int count = 0;
490 int do_croak = 0; 506 int do_croak = 0;
491 aio_req req; 507 aio_req req;
492 508
493 for (;;) 509 for (;;)
494 { 510 {
495 LOCK (reslock); 511 while (max <= 0 || count < max)
496 req = reqq_shift (&res_queue);
497
498 if (req)
499 { 512 {
513 LOCK (reslock);
514 req = reqq_shift (&res_queue);
515
500 if (!res_queue.size) 516 if (req)
501 { 517 {
518 if (!res_queue.size)
519 {
502 /* read any signals sent by the worker threads */ 520 /* read any signals sent by the worker threads */
503 char buf [32]; 521 char buf [32];
504 while (read (respipe [0], buf, 32) == 32) 522 while (read (respipe [0], buf, 32) == 32)
523 ;
505 ; 524 }
506 } 525 }
526
527 UNLOCK (reslock);
528
529 if (!req)
530 break;
531
532 --nreqs;
533
534 if (req->type == REQ_QUIT)
535 --started;
536 else if (req->type == REQ_GROUP && req->length)
537 {
538 req->fd = 1; /* mark request as delayed */
539 continue;
540 }
541 else
542 {
543 if (req->type == REQ_READ)
544 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
545
546 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
547 SvREADONLY_off (req->data);
548
549 if (req->statdata)
550 {
551 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
552 PL_laststatval = req->result;
553 PL_statcache = *(req->statdata);
554 }
555
556 req_invoke (req);
557
558 count++;
559 }
560
561 req_free (req);
507 } 562 }
508 563
509 UNLOCK (reslock); 564 if (nreqs <= max_outstanding)
510
511 if (!req)
512 break; 565 break;
513 566
514 --nreqs; 567 poll_wait ();
515 568
516 if (req->type == REQ_QUIT) 569 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 } 570 }
545 571
546 return count; 572 return count;
547} 573}
548 574
549static void *aio_proc(void *arg); 575static void *aio_proc(void *arg);
550 576
551static void start_thread (void) 577static void start_thread (void)
552{ 578{
579 sigset_t fullsigset, oldsigset;
580 pthread_attr_t attr;
581
553 worker *wrk = calloc (1, sizeof (worker)); 582 worker *wrk = calloc (1, sizeof (worker));
554 583
555 if (!wrk) 584 if (!wrk)
556 croak ("unable to allocate worker thread data"); 585 croak ("unable to allocate worker thread data");
557
558 sigset_t fullsigset, oldsigset;
559 pthread_attr_t attr;
560 586
561 pthread_attr_init (&attr); 587 pthread_attr_init (&attr);
562 pthread_attr_setstacksize (&attr, STACKSIZE); 588 pthread_attr_setstacksize (&attr, STACKSIZE);
563 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 589 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
564 590
571 { 597 {
572 wrk->prev = &wrk_first; 598 wrk->prev = &wrk_first;
573 wrk->next = wrk_first.next; 599 wrk->next = wrk_first.next;
574 wrk_first.next->prev = wrk; 600 wrk_first.next->prev = wrk;
575 wrk_first.next = wrk; 601 wrk_first.next = wrk;
576 started++; 602 ++started;
577 } 603 }
578 else 604 else
579 free (wrk); 605 free (wrk);
580 606
581 sigprocmask (SIG_SETMASK, &oldsigset, 0); 607 sigprocmask (SIG_SETMASK, &oldsigset, 0);
591 617
592 LOCK (reqlock); 618 LOCK (reqlock);
593 reqq_push (&req_queue, req); 619 reqq_push (&req_queue, req);
594 pthread_cond_signal (&reqwait); 620 pthread_cond_signal (&reqwait);
595 UNLOCK (reqlock); 621 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} 622}
608 623
609static void end_thread (void) 624static void end_thread (void)
610{ 625{
611 aio_req req; 626 aio_req req;
638 } 653 }
639 654
640 while (started > wanted) 655 while (started > wanted)
641 { 656 {
642 poll_wait (); 657 poll_wait ();
643 poll_cb (); 658 poll_cb (0);
644 } 659 }
645} 660}
646 661
647static void create_pipe () 662static void create_pipe ()
648{ 663{
704#if !HAVE_FDATASYNC 719#if !HAVE_FDATASYNC
705# define fdatasync fsync 720# define fdatasync fsync
706#endif 721#endif
707 722
708#if !HAVE_READAHEAD 723#if !HAVE_READAHEAD
709# define readahead aio_readahead 724# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self)
710 725
711static ssize_t readahead (int fd, off_t offset, size_t count) 726static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self)
712{ 727{
713 dBUF; 728 dBUF;
714 729
715 while (count > 0) 730 while (count > 0)
716 { 731 {
721 count -= len; 736 count -= len;
722 } 737 }
723 738
724 errno = 0; 739 errno = 0;
725} 740}
741
726#endif 742#endif
727 743
728#if !HAVE_READDIR_R 744#if !HAVE_READDIR_R
729# define readdir_r aio_readdir_r 745# define readdir_r aio_readdir_r
730 746
775 { 791 {
776 off_t sbytes; 792 off_t sbytes;
777 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 793 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
778 794
779 if (res < 0 && sbytes) 795 if (res < 0 && sbytes)
780 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ 796 /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */
781 res = sbytes; 797 res = sbytes;
782 } 798 }
783 799
784# elif __hpux 800# elif __hpux
785 res = sendfile (ofd, ifd, offset, count, 0, 0); 801 res = sendfile (ofd, ifd, offset, count, 0, 0);
865 int errorno; 881 int errorno;
866 882
867 LOCK (wrklock); 883 LOCK (wrklock);
868 self->dirp = dirp = opendir (req->dataptr); 884 self->dirp = dirp = opendir (req->dataptr);
869 self->dbuf = u = malloc (sizeof (*u)); 885 self->dbuf = u = malloc (sizeof (*u));
886 req->data2ptr = names = malloc (memlen);
870 UNLOCK (wrklock); 887 UNLOCK (wrklock);
871
872 req->data2ptr = names = malloc (memlen);
873 888
874 if (dirp && u && names) 889 if (dirp && u && names)
875 for (;;) 890 for (;;)
876 { 891 {
877 errno = 0; 892 errno = 0;
1098 create_pipe (); 1113 create_pipe ();
1099 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 1114 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
1100} 1115}
1101 1116
1102void 1117void
1103min_parallel (nthreads) 1118min_parallel (int nthreads)
1104 int nthreads
1105 PROTOTYPE: $ 1119 PROTOTYPE: $
1106 1120
1107void 1121void
1108max_parallel (nthreads) 1122max_parallel (int nthreads)
1109 int nthreads
1110 PROTOTYPE: $ 1123 PROTOTYPE: $
1111 1124
1112int 1125int
1113max_outstanding (nreqs) 1126max_outstanding (int maxreqs)
1114 int nreqs 1127 PROTOTYPE: $
1115 PROTOTYPE: $
1116 CODE: 1128 CODE:
1117 RETVAL = max_outstanding; 1129 RETVAL = max_outstanding;
1118 max_outstanding = nreqs; 1130 max_outstanding = maxreqs;
1131 OUTPUT:
1132 RETVAL
1119 1133
1120void 1134void
1121aio_open (pathname,flags,mode,callback=&PL_sv_undef) 1135aio_open (pathname,flags,mode,callback=&PL_sv_undef)
1122 SV * pathname 1136 SV * pathname
1123 int flags 1137 int flags
1400 PROTOTYPE: 1414 PROTOTYPE:
1401 CODE: 1415 CODE:
1402 while (nreqs) 1416 while (nreqs)
1403 { 1417 {
1404 poll_wait (); 1418 poll_wait ();
1405 poll_cb (); 1419 poll_cb (0);
1406 } 1420 }
1407 1421
1408void 1422void
1409poll() 1423poll()
1410 PROTOTYPE: 1424 PROTOTYPE:
1411 CODE: 1425 CODE:
1412 if (nreqs) 1426 if (nreqs)
1413 { 1427 {
1414 poll_wait (); 1428 poll_wait ();
1415 poll_cb (); 1429 poll_cb (0);
1416 } 1430 }
1417 1431
1418int 1432int
1419poll_fileno() 1433poll_fileno()
1420 PROTOTYPE: 1434 PROTOTYPE:
1425 1439
1426int 1440int
1427poll_cb(...) 1441poll_cb(...)
1428 PROTOTYPE: 1442 PROTOTYPE:
1429 CODE: 1443 CODE:
1430 RETVAL = poll_cb (); 1444 RETVAL = poll_cb (0);
1445 OUTPUT:
1446 RETVAL
1447
1448int
1449poll_some(int max = 0)
1450 PROTOTYPE: $
1451 CODE:
1452 RETVAL = poll_cb (max);
1431 OUTPUT: 1453 OUTPUT:
1432 RETVAL 1454 RETVAL
1433 1455
1434void 1456void
1435poll_wait() 1457poll_wait()
1495 } 1517 }
1496 } 1518 }
1497} 1519}
1498 1520
1499void 1521void
1522cancel_subs (aio_req_ornot req)
1523 CODE:
1524 req_cancel_subs (req);
1525
1526void
1500result (aio_req grp, ...) 1527result (aio_req grp, ...)
1501 CODE: 1528 CODE:
1502{ 1529{
1503 int i; 1530 int i;
1504 AV *av = newAV (); 1531 AV *av = newAV ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines