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.76 by root, Thu Oct 26 12:38:04 2006 UTC vs.
Revision 1.79 by root, Thu Oct 26 16:28:33 2006 UTC

61# define STACKSIZE PTHREAD_STACK_MIN 61# define STACKSIZE PTHREAD_STACK_MIN
62#else 62#else
63# define STACKSIZE 16384 63# define STACKSIZE 16384
64#endif 64#endif
65 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
76#endif
77
66/* buffer size for various temporary buffers */ 78/* buffer size for various temporary buffers */
67#define AIO_BUFSIZE 65536 79#define AIO_BUFSIZE 65536
68 80
69#define dBUF \ 81#define dBUF \
70 char *aio_buf; \ 82 char *aio_buf; \
132 NUM_PRI = PRI_MAX + PRI_BIAS + 1, 144 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
133}; 145};
134 146
135static int next_pri = DEFAULT_PRI + PRI_BIAS; 147static int next_pri = DEFAULT_PRI + PRI_BIAS;
136 148
137static int started, wanted; 149static unsigned int started, wanted;
138static volatile int nreqs; 150static volatile unsigned int nreqs, nready, npending;
151static volatile unsigned int max_outstanding = 0xffffffff;
139static int respipe [2]; 152static int respipe [2];
140 153
141#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 154#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
142# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 155# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
143#else 156#else
325 fd_set rfd; 338 fd_set rfd;
326 339
327 while (nreqs) 340 while (nreqs)
328 { 341 {
329 int size; 342 int size;
330#if !(__i386 || __x86_64) /* safe without sempahore on these archs */ 343 if (WORDREAD_UNSAFE) LOCK (reslock);
331 LOCK (reslock);
332#endif
333 size = res_queue.size; 344 size = res_queue.size;
334#if !(__i386 || __x86_64) /* safe without sempahore on these archs */ 345 if (WORDREAD_UNSAFE) UNLOCK (reslock);
335 UNLOCK (reslock);
336#endif
337 346
338 if (size) 347 if (size)
339 return; 348 return;
340 349
341 FD_ZERO(&rfd); 350 FD_ZERO(&rfd);
349{ 358{
350 dSP; 359 dSP;
351 360
352 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) 361 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
353 { 362 {
354 errno = req->errorno;
355
356 ENTER; 363 ENTER;
357 SAVETMPS; 364 SAVETMPS;
358 PUSHMARK (SP); 365 PUSHMARK (SP);
359 EXTEND (SP, 1); 366 EXTEND (SP, 1);
360 367
425 default: 432 default:
426 PUSHs (sv_2mortal (newSViv (req->result))); 433 PUSHs (sv_2mortal (newSViv (req->result)));
427 break; 434 break;
428 } 435 }
429 436
437 errno = req->errorno;
430 438
431 PUTBACK; 439 PUTBACK;
432 call_sv (req->callback, G_VOID | G_EVAL); 440 call_sv (req->callback, G_VOID | G_EVAL);
433 SPAGAIN; 441 SPAGAIN;
434 442
503 dSP; 511 dSP;
504 int count = 0; 512 int count = 0;
505 int do_croak = 0; 513 int do_croak = 0;
506 aio_req req; 514 aio_req req;
507 515
516 for (;;)
517 {
508 while (max <= 0 || count < max) 518 while (max <= 0 || count < max)
509 {
510 LOCK (reslock);
511 req = reqq_shift (&res_queue);
512
513 if (req)
514 { 519 {
520 LOCK (reslock);
521 req = reqq_shift (&res_queue);
522
515 if (!res_queue.size) 523 if (req)
516 { 524 {
525 --npending;
526
527 if (!res_queue.size)
528 {
517 /* read any signals sent by the worker threads */ 529 /* read any signals sent by the worker threads */
518 char buf [32]; 530 char buf [32];
519 while (read (respipe [0], buf, 32) == 32) 531 while (read (respipe [0], buf, 32) == 32)
532 ;
520 ; 533 }
521 } 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);
522 } 571 }
523 572
524 UNLOCK (reslock); 573 if (nreqs <= max_outstanding)
525
526 if (!req)
527 break; 574 break;
528 575
529 --nreqs; 576 poll_wait ();
530 577
531 if (req->type == REQ_QUIT) 578 max = 0;
532 started--;
533 else if (req->type == REQ_GROUP && req->length)
534 {
535 req->fd = 1; /* mark request as delayed */
536 continue;
537 }
538 else
539 {
540 if (req->type == REQ_READ)
541 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
542
543 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
544 SvREADONLY_off (req->data);
545
546 if (req->statdata)
547 {
548 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
549 PL_laststatval = req->result;
550 PL_statcache = *(req->statdata);
551 }
552
553 req_invoke (req);
554
555 count++;
556 }
557
558 req_free (req);
559 } 579 }
560 580
561 return count; 581 return count;
562} 582}
563 583
586 { 606 {
587 wrk->prev = &wrk_first; 607 wrk->prev = &wrk_first;
588 wrk->next = wrk_first.next; 608 wrk->next = wrk_first.next;
589 wrk_first.next->prev = wrk; 609 wrk_first.next->prev = wrk;
590 wrk_first.next = wrk; 610 wrk_first.next = wrk;
591 started++; 611 ++started;
592 } 612 }
593 else 613 else
594 free (wrk); 614 free (wrk);
595 615
596 sigprocmask (SIG_SETMASK, &oldsigset, 0); 616 sigprocmask (SIG_SETMASK, &oldsigset, 0);
603 start_thread (); 623 start_thread ();
604 624
605 ++nreqs; 625 ++nreqs;
606 626
607 LOCK (reqlock); 627 LOCK (reqlock);
628 ++nready;
608 reqq_push (&req_queue, req); 629 reqq_push (&req_queue, req);
609 pthread_cond_signal (&reqwait); 630 pthread_cond_signal (&reqwait);
610 UNLOCK (reqlock); 631 UNLOCK (reqlock);
611} 632}
612 633
780 { 801 {
781 off_t sbytes; 802 off_t sbytes;
782 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 803 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
783 804
784 if (res < 0 && sbytes) 805 if (res < 0 && sbytes)
785 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ 806 /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */
786 res = sbytes; 807 res = sbytes;
787 } 808 }
788 809
789# elif __hpux 810# elif __hpux
790 res = sendfile (ofd, ifd, offset, count, 0, 0); 811 res = sendfile (ofd, ifd, offset, count, 0, 0);
934 break; 955 break;
935 956
936 pthread_cond_wait (&reqwait, &reqlock); 957 pthread_cond_wait (&reqwait, &reqlock);
937 } 958 }
938 959
960 --nready;
961
939 UNLOCK (reqlock); 962 UNLOCK (reqlock);
940 963
941 errno = 0; /* strictly unnecessary */ 964 errno = 0; /* strictly unnecessary */
942 type = req->type; /* remember type for QUIT check */ 965 type = req->type; /* remember type for QUIT check */
943 966
987 } 1010 }
988 1011
989 req->errorno = errno; 1012 req->errorno = errno;
990 1013
991 LOCK (reslock); 1014 LOCK (reslock);
1015
1016 ++npending;
992 1017
993 if (!reqq_push (&res_queue, req)) 1018 if (!reqq_push (&res_queue, req))
994 /* write a dummy byte to the pipe so fh becomes ready */ 1019 /* write a dummy byte to the pipe so fh becomes ready */
995 write (respipe [1], &respipe, 1); 1020 write (respipe [1], &respipe, 1);
996 1021
1102 create_pipe (); 1127 create_pipe ();
1103 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 1128 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
1104} 1129}
1105 1130
1106void 1131void
1107min_parallel (nthreads) 1132min_parallel (int nthreads)
1108 int nthreads
1109 PROTOTYPE: $ 1133 PROTOTYPE: $
1110 1134
1111void 1135void
1112max_parallel (nthreads) 1136max_parallel (int nthreads)
1113 int nthreads
1114 PROTOTYPE: $ 1137 PROTOTYPE: $
1138
1139int
1140max_outstanding (int maxreqs)
1141 PROTOTYPE: $
1142 CODE:
1143 RETVAL = max_outstanding;
1144 max_outstanding = maxreqs;
1145 OUTPUT:
1146 RETVAL
1115 1147
1116void 1148void
1117aio_open (pathname,flags,mode,callback=&PL_sv_undef) 1149aio_open (pathname,flags,mode,callback=&PL_sv_undef)
1118 SV * pathname 1150 SV * pathname
1119 int flags 1151 int flags
1374 req->type = REQ_NOP; 1406 req->type = REQ_NOP;
1375 1407
1376 REQ_SEND; 1408 REQ_SEND;
1377} 1409}
1378 1410
1379void 1411int
1380aioreq_pri (int pri = DEFAULT_PRI) 1412aioreq_pri (int pri = 0)
1381 CODE: 1413 PROTOTYPE: ;$
1414 CODE:
1415 RETVAL = next_pri - PRI_BIAS;
1416 if (items > 0)
1417 {
1382 if (pri < PRI_MIN) pri = PRI_MIN; 1418 if (pri < PRI_MIN) pri = PRI_MIN;
1383 if (pri > PRI_MAX) pri = PRI_MAX; 1419 if (pri > PRI_MAX) pri = PRI_MAX;
1384 next_pri = pri + PRI_BIAS; 1420 next_pri = pri + PRI_BIAS;
1421 }
1422 OUTPUT:
1423 RETVAL
1385 1424
1386void 1425void
1387aioreq_nice (int nice = 0) 1426aioreq_nice (int nice = 0)
1388 CODE: 1427 CODE:
1389 nice = next_pri - nice; 1428 nice = next_pri - nice;
1390 if (nice < PRI_MIN) nice = PRI_MIN; 1429 if (nice < PRI_MIN) nice = PRI_MIN;
1391 if (nice > PRI_MAX) nice = PRI_MAX; 1430 if (nice > PRI_MAX) nice = PRI_MAX;
1392 next_pri = nice + PRI_BIAS; 1431 next_pri = nice + PRI_BIAS;
1393 1432
1394void 1433void
1395flush () 1434flush ()
1396 PROTOTYPE: 1435 PROTOTYPE:
1397 CODE: 1436 CODE:
1448 CODE: 1487 CODE:
1449 RETVAL = nreqs; 1488 RETVAL = nreqs;
1450 OUTPUT: 1489 OUTPUT:
1451 RETVAL 1490 RETVAL
1452 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
1453PROTOTYPES: DISABLE 1512PROTOTYPES: DISABLE
1454 1513
1455MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1514MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1456 1515
1457void 1516void
1508void 1567void
1509result (aio_req grp, ...) 1568result (aio_req grp, ...)
1510 CODE: 1569 CODE:
1511{ 1570{
1512 int i; 1571 int i;
1572 AV *av;
1573
1574 grp->errorno = errno;
1575
1513 AV *av = newAV (); 1576 av = newAV ();
1514 1577
1515 for (i = 1; i < items; ++i ) 1578 for (i = 1; i < items; ++i )
1516 av_push (av, newSVsv (ST (i))); 1579 av_push (av, newSVsv (ST (i)));
1517 1580
1518 SvREFCNT_dec (grp->data); 1581 SvREFCNT_dec (grp->data);
1519 grp->data = (SV *)av; 1582 grp->data = (SV *)av;
1520} 1583}
1584
1585void
1586errno (aio_req grp, int errorno = errno)
1587 CODE:
1588 grp->errorno = errorno;
1521 1589
1522void 1590void
1523limit (aio_req grp, int limit) 1591limit (aio_req grp, int limit)
1524 CODE: 1592 CODE:
1525 grp->fd2 = limit; 1593 grp->fd2 = limit;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines