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.72 by root, Tue Oct 24 17:22:17 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);
245static void req_cancel (aio_req req); 264static void req_cancel (aio_req req);
246 265
247/* must be called at most once */ 266/* must be called at most once */
319 fd_set rfd; 338 fd_set rfd;
320 339
321 while (nreqs) 340 while (nreqs)
322 { 341 {
323 int size; 342 int size;
324#if !(__i386 || __x86_64) /* safe without sempahore on these archs */ 343 if (WORDREAD_UNSAFE) LOCK (reslock);
325 LOCK (reslock);
326#endif
327 size = res_queue.size; 344 size = res_queue.size;
328#if !(__i386 || __x86_64) /* safe without sempahore on these archs */ 345 if (WORDREAD_UNSAFE) UNLOCK (reslock);
329 UNLOCK (reslock);
330#endif
331 346
332 if (size) 347 if (size)
333 return; 348 return;
334 349
335 FD_ZERO(&rfd); 350 FD_ZERO(&rfd);
343{ 358{
344 dSP; 359 dSP;
345 360
346 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) 361 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
347 { 362 {
348 errno = req->errorno;
349
350 ENTER; 363 ENTER;
351 SAVETMPS; 364 SAVETMPS;
352 PUSHMARK (SP); 365 PUSHMARK (SP);
353 EXTEND (SP, 1); 366 EXTEND (SP, 1);
354 367
419 default: 432 default:
420 PUSHs (sv_2mortal (newSViv (req->result))); 433 PUSHs (sv_2mortal (newSViv (req->result)));
421 break; 434 break;
422 } 435 }
423 436
437 errno = req->errorno;
424 438
425 PUTBACK; 439 PUTBACK;
426 call_sv (req->callback, G_VOID | G_EVAL); 440 call_sv (req->callback, G_VOID | G_EVAL);
427 SPAGAIN; 441 SPAGAIN;
428 442
490 req->flags |= FLAG_CANCELLED; 504 req->flags |= FLAG_CANCELLED;
491 505
492 req_cancel_subs (req); 506 req_cancel_subs (req);
493} 507}
494 508
495static int poll_cb () 509static int poll_cb (int max)
496{ 510{
497 dSP; 511 dSP;
498 int count = 0; 512 int count = 0;
499 int do_croak = 0; 513 int do_croak = 0;
500 aio_req req; 514 aio_req req;
501 515
502 for (;;) 516 for (;;)
503 { 517 {
504 LOCK (reslock); 518 while (max <= 0 || count < max)
505 req = reqq_shift (&res_queue);
506
507 if (req)
508 { 519 {
520 LOCK (reslock);
521 req = reqq_shift (&res_queue);
522
509 if (!res_queue.size) 523 if (req)
510 { 524 {
525 --npending;
526
527 if (!res_queue.size)
528 {
511 /* read any signals sent by the worker threads */ 529 /* read any signals sent by the worker threads */
512 char buf [32]; 530 char buf [32];
513 while (read (respipe [0], buf, 32) == 32) 531 while (read (respipe [0], buf, 32) == 32)
532 ;
514 ; 533 }
515 } 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);
516 } 571 }
517 572
518 UNLOCK (reslock); 573 if (nreqs <= max_outstanding)
519
520 if (!req)
521 break; 574 break;
522 575
523 --nreqs; 576 poll_wait ();
524 577
525 if (req->type == REQ_QUIT) 578 max = 0;
526 started--;
527 else if (req->type == REQ_GROUP && req->length)
528 {
529 req->fd = 1; /* mark request as delayed */
530 continue;
531 }
532 else
533 {
534 if (req->type == REQ_READ)
535 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
536
537 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
538 SvREADONLY_off (req->data);
539
540 if (req->statdata)
541 {
542 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
543 PL_laststatval = req->result;
544 PL_statcache = *(req->statdata);
545 }
546
547 req_invoke (req);
548
549 count++;
550 }
551
552 req_free (req);
553 } 579 }
554 580
555 return count; 581 return count;
556} 582}
557 583
558static void *aio_proc(void *arg); 584static void *aio_proc(void *arg);
559 585
560static void start_thread (void) 586static void start_thread (void)
561{ 587{
588 sigset_t fullsigset, oldsigset;
589 pthread_attr_t attr;
590
562 worker *wrk = calloc (1, sizeof (worker)); 591 worker *wrk = calloc (1, sizeof (worker));
563 592
564 if (!wrk) 593 if (!wrk)
565 croak ("unable to allocate worker thread data"); 594 croak ("unable to allocate worker thread data");
566
567 sigset_t fullsigset, oldsigset;
568 pthread_attr_t attr;
569 595
570 pthread_attr_init (&attr); 596 pthread_attr_init (&attr);
571 pthread_attr_setstacksize (&attr, STACKSIZE); 597 pthread_attr_setstacksize (&attr, STACKSIZE);
572 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 598 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
573 599
580 { 606 {
581 wrk->prev = &wrk_first; 607 wrk->prev = &wrk_first;
582 wrk->next = wrk_first.next; 608 wrk->next = wrk_first.next;
583 wrk_first.next->prev = wrk; 609 wrk_first.next->prev = wrk;
584 wrk_first.next = wrk; 610 wrk_first.next = wrk;
585 started++; 611 ++started;
586 } 612 }
587 else 613 else
588 free (wrk); 614 free (wrk);
589 615
590 sigprocmask (SIG_SETMASK, &oldsigset, 0); 616 sigprocmask (SIG_SETMASK, &oldsigset, 0);
597 start_thread (); 623 start_thread ();
598 624
599 ++nreqs; 625 ++nreqs;
600 626
601 LOCK (reqlock); 627 LOCK (reqlock);
628 ++nready;
602 reqq_push (&req_queue, req); 629 reqq_push (&req_queue, req);
603 pthread_cond_signal (&reqwait); 630 pthread_cond_signal (&reqwait);
604 UNLOCK (reqlock); 631 UNLOCK (reqlock);
605
606 if (nreqs > max_outstanding)
607 for (;;)
608 {
609 poll_cb ();
610
611 if (nreqs <= max_outstanding)
612 break;
613
614 poll_wait ();
615 }
616} 632}
617 633
618static void end_thread (void) 634static void end_thread (void)
619{ 635{
620 aio_req req; 636 aio_req req;
647 } 663 }
648 664
649 while (started > wanted) 665 while (started > wanted)
650 { 666 {
651 poll_wait (); 667 poll_wait ();
652 poll_cb (); 668 poll_cb (0);
653 } 669 }
654} 670}
655 671
656static void create_pipe () 672static void create_pipe ()
657{ 673{
713#if !HAVE_FDATASYNC 729#if !HAVE_FDATASYNC
714# define fdatasync fsync 730# define fdatasync fsync
715#endif 731#endif
716 732
717#if !HAVE_READAHEAD 733#if !HAVE_READAHEAD
718# define readahead aio_readahead 734# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self)
719 735
720static 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)
721{ 737{
722 dBUF; 738 dBUF;
723 739
724 while (count > 0) 740 while (count > 0)
725 { 741 {
730 count -= len; 746 count -= len;
731 } 747 }
732 748
733 errno = 0; 749 errno = 0;
734} 750}
751
735#endif 752#endif
736 753
737#if !HAVE_READDIR_R 754#if !HAVE_READDIR_R
738# define readdir_r aio_readdir_r 755# define readdir_r aio_readdir_r
739 756
784 { 801 {
785 off_t sbytes; 802 off_t sbytes;
786 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 803 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
787 804
788 if (res < 0 && sbytes) 805 if (res < 0 && sbytes)
789 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ 806 /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */
790 res = sbytes; 807 res = sbytes;
791 } 808 }
792 809
793# elif __hpux 810# elif __hpux
794 res = sendfile (ofd, ifd, offset, count, 0, 0); 811 res = sendfile (ofd, ifd, offset, count, 0, 0);
874 int errorno; 891 int errorno;
875 892
876 LOCK (wrklock); 893 LOCK (wrklock);
877 self->dirp = dirp = opendir (req->dataptr); 894 self->dirp = dirp = opendir (req->dataptr);
878 self->dbuf = u = malloc (sizeof (*u)); 895 self->dbuf = u = malloc (sizeof (*u));
896 req->data2ptr = names = malloc (memlen);
879 UNLOCK (wrklock); 897 UNLOCK (wrklock);
880
881 req->data2ptr = names = malloc (memlen);
882 898
883 if (dirp && u && names) 899 if (dirp && u && names)
884 for (;;) 900 for (;;)
885 { 901 {
886 errno = 0; 902 errno = 0;
938 if (req) 954 if (req)
939 break; 955 break;
940 956
941 pthread_cond_wait (&reqwait, &reqlock); 957 pthread_cond_wait (&reqwait, &reqlock);
942 } 958 }
959
960 --nready;
943 961
944 UNLOCK (reqlock); 962 UNLOCK (reqlock);
945 963
946 errno = 0; /* strictly unnecessary */ 964 errno = 0; /* strictly unnecessary */
947 type = req->type; /* remember type for QUIT check */ 965 type = req->type; /* remember type for QUIT check */
992 } 1010 }
993 1011
994 req->errorno = errno; 1012 req->errorno = errno;
995 1013
996 LOCK (reslock); 1014 LOCK (reslock);
1015
1016 ++npending;
997 1017
998 if (!reqq_push (&res_queue, req)) 1018 if (!reqq_push (&res_queue, req))
999 /* write a dummy byte to the pipe so fh becomes ready */ 1019 /* write a dummy byte to the pipe so fh becomes ready */
1000 write (respipe [1], &respipe, 1); 1020 write (respipe [1], &respipe, 1);
1001 1021
1107 create_pipe (); 1127 create_pipe ();
1108 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 1128 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
1109} 1129}
1110 1130
1111void 1131void
1112min_parallel (nthreads) 1132min_parallel (int nthreads)
1113 int nthreads
1114 PROTOTYPE: $ 1133 PROTOTYPE: $
1115 1134
1116void 1135void
1117max_parallel (nthreads) 1136max_parallel (int nthreads)
1118 int nthreads
1119 PROTOTYPE: $ 1137 PROTOTYPE: $
1120 1138
1121int 1139int
1122max_outstanding (nreqs) 1140max_outstanding (int maxreqs)
1123 int nreqs 1141 PROTOTYPE: $
1124 PROTOTYPE: $
1125 CODE: 1142 CODE:
1126 RETVAL = max_outstanding; 1143 RETVAL = max_outstanding;
1127 max_outstanding = nreqs; 1144 max_outstanding = maxreqs;
1145 OUTPUT:
1146 RETVAL
1128 1147
1129void 1148void
1130aio_open (pathname,flags,mode,callback=&PL_sv_undef) 1149aio_open (pathname,flags,mode,callback=&PL_sv_undef)
1131 SV * pathname 1150 SV * pathname
1132 int flags 1151 int flags
1387 req->type = REQ_NOP; 1406 req->type = REQ_NOP;
1388 1407
1389 REQ_SEND; 1408 REQ_SEND;
1390} 1409}
1391 1410
1392void 1411int
1393aioreq_pri (int pri = DEFAULT_PRI) 1412aioreq_pri (int pri = 0)
1394 CODE: 1413 PROTOTYPE: ;$
1414 CODE:
1415 RETVAL = next_pri - PRI_BIAS;
1416 if (items > 0)
1417 {
1395 if (pri < PRI_MIN) pri = PRI_MIN; 1418 if (pri < PRI_MIN) pri = PRI_MIN;
1396 if (pri > PRI_MAX) pri = PRI_MAX; 1419 if (pri > PRI_MAX) pri = PRI_MAX;
1397 next_pri = pri + PRI_BIAS; 1420 next_pri = pri + PRI_BIAS;
1421 }
1422 OUTPUT:
1423 RETVAL
1398 1424
1399void 1425void
1400aioreq_nice (int nice = 0) 1426aioreq_nice (int nice = 0)
1401 CODE: 1427 CODE:
1402 nice = next_pri - nice; 1428 nice = next_pri - nice;
1403 if (nice < PRI_MIN) nice = PRI_MIN; 1429 if (nice < PRI_MIN) nice = PRI_MIN;
1404 if (nice > PRI_MAX) nice = PRI_MAX; 1430 if (nice > PRI_MAX) nice = PRI_MAX;
1405 next_pri = nice + PRI_BIAS; 1431 next_pri = nice + PRI_BIAS;
1406 1432
1407void 1433void
1408flush () 1434flush ()
1409 PROTOTYPE: 1435 PROTOTYPE:
1410 CODE: 1436 CODE:
1411 while (nreqs) 1437 while (nreqs)
1412 { 1438 {
1413 poll_wait (); 1439 poll_wait ();
1414 poll_cb (); 1440 poll_cb (0);
1415 } 1441 }
1416 1442
1417void 1443void
1418poll() 1444poll()
1419 PROTOTYPE: 1445 PROTOTYPE:
1420 CODE: 1446 CODE:
1421 if (nreqs) 1447 if (nreqs)
1422 { 1448 {
1423 poll_wait (); 1449 poll_wait ();
1424 poll_cb (); 1450 poll_cb (0);
1425 } 1451 }
1426 1452
1427int 1453int
1428poll_fileno() 1454poll_fileno()
1429 PROTOTYPE: 1455 PROTOTYPE:
1434 1460
1435int 1461int
1436poll_cb(...) 1462poll_cb(...)
1437 PROTOTYPE: 1463 PROTOTYPE:
1438 CODE: 1464 CODE:
1439 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);
1440 OUTPUT: 1474 OUTPUT:
1441 RETVAL 1475 RETVAL
1442 1476
1443void 1477void
1444poll_wait() 1478poll_wait()
1453 CODE: 1487 CODE:
1454 RETVAL = nreqs; 1488 RETVAL = nreqs;
1455 OUTPUT: 1489 OUTPUT:
1456 RETVAL 1490 RETVAL
1457 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
1458PROTOTYPES: DISABLE 1512PROTOTYPES: DISABLE
1459 1513
1460MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1514MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1461 1515
1462void 1516void
1513void 1567void
1514result (aio_req grp, ...) 1568result (aio_req grp, ...)
1515 CODE: 1569 CODE:
1516{ 1570{
1517 int i; 1571 int i;
1572 AV *av;
1573
1574 grp->errorno = errno;
1575
1518 AV *av = newAV (); 1576 av = newAV ();
1519 1577
1520 for (i = 1; i < items; ++i ) 1578 for (i = 1; i < items; ++i )
1521 av_push (av, newSVsv (ST (i))); 1579 av_push (av, newSVsv (ST (i)));
1522 1580
1523 SvREFCNT_dec (grp->data); 1581 SvREFCNT_dec (grp->data);
1524 grp->data = (SV *)av; 1582 grp->data = (SV *)av;
1525} 1583}
1584
1585void
1586errno (aio_req grp, int errorno = errno)
1587 CODE:
1588 grp->errorno = errorno;
1526 1589
1527void 1590void
1528limit (aio_req grp, int limit) 1591limit (aio_req grp, int limit)
1529 CODE: 1592 CODE:
1530 grp->fd2 = limit; 1593 grp->fd2 = limit;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines