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.74 by root, Wed Oct 25 17:57:30 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;
151static volatile unsigned int max_outstanding = 0xffffffff;
133static int respipe [2]; 152static int respipe [2];
134 153
135#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 154#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
136# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 155# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
137#else 156#else
139#endif 158#endif
140 159
141#define LOCK(mutex) pthread_mutex_lock (&(mutex)) 160#define LOCK(mutex) pthread_mutex_lock (&(mutex))
142#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 161#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
143 162
144/* worker threasd management */ 163/* worker threads management */
145static pthread_mutex_t wrklock = AIO_MUTEX_INIT; 164static pthread_mutex_t wrklock = AIO_MUTEX_INIT;
146 165
147typedef struct worker { 166typedef struct worker {
148 /* locked by wrklock */ 167 /* locked by wrklock */
149 struct worker *prev, *next; 168 struct worker *prev, *next;
237 } 256 }
238 257
239 abort (); 258 abort ();
240} 259}
241 260
242static int poll_cb (); 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
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} 632}
606 633
636 } 663 }
637 664
638 while (started > wanted) 665 while (started > wanted)
639 { 666 {
640 poll_wait (); 667 poll_wait ();
641 poll_cb (); 668 poll_cb (0);
642 } 669 }
643} 670}
644 671
645static void create_pipe () 672static void create_pipe ()
646{ 673{
702#if !HAVE_FDATASYNC 729#if !HAVE_FDATASYNC
703# define fdatasync fsync 730# define fdatasync fsync
704#endif 731#endif
705 732
706#if !HAVE_READAHEAD 733#if !HAVE_READAHEAD
707# define readahead aio_readahead 734# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self)
708 735
709static 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)
710{ 737{
711 dBUF; 738 dBUF;
712 739
713 while (count > 0) 740 while (count > 0)
714 { 741 {
719 count -= len; 746 count -= len;
720 } 747 }
721 748
722 errno = 0; 749 errno = 0;
723} 750}
751
724#endif 752#endif
725 753
726#if !HAVE_READDIR_R 754#if !HAVE_READDIR_R
727# define readdir_r aio_readdir_r 755# define readdir_r aio_readdir_r
728 756
773 { 801 {
774 off_t sbytes; 802 off_t sbytes;
775 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 803 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
776 804
777 if (res < 0 && sbytes) 805 if (res < 0 && sbytes)
778 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ 806 /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */
779 res = sbytes; 807 res = sbytes;
780 } 808 }
781 809
782# elif __hpux 810# elif __hpux
783 res = sendfile (ofd, ifd, offset, count, 0, 0); 811 res = sendfile (ofd, ifd, offset, count, 0, 0);
863 int errorno; 891 int errorno;
864 892
865 LOCK (wrklock); 893 LOCK (wrklock);
866 self->dirp = dirp = opendir (req->dataptr); 894 self->dirp = dirp = opendir (req->dataptr);
867 self->dbuf = u = malloc (sizeof (*u)); 895 self->dbuf = u = malloc (sizeof (*u));
896 req->data2ptr = names = malloc (memlen);
868 UNLOCK (wrklock); 897 UNLOCK (wrklock);
869
870 req->data2ptr = names = malloc (memlen);
871 898
872 if (dirp && u && names) 899 if (dirp && u && names)
873 for (;;) 900 for (;;)
874 { 901 {
875 errno = 0; 902 errno = 0;
927 if (req) 954 if (req)
928 break; 955 break;
929 956
930 pthread_cond_wait (&reqwait, &reqlock); 957 pthread_cond_wait (&reqwait, &reqlock);
931 } 958 }
959
960 --nready;
932 961
933 UNLOCK (reqlock); 962 UNLOCK (reqlock);
934 963
935 errno = 0; /* strictly unnecessary */ 964 errno = 0; /* strictly unnecessary */
936 type = req->type; /* remember type for QUIT check */ 965 type = req->type; /* remember type for QUIT check */
981 } 1010 }
982 1011
983 req->errorno = errno; 1012 req->errorno = errno;
984 1013
985 LOCK (reslock); 1014 LOCK (reslock);
1015
1016 ++npending;
986 1017
987 if (!reqq_push (&res_queue, req)) 1018 if (!reqq_push (&res_queue, req))
988 /* write a dummy byte to the pipe so fh becomes ready */ 1019 /* write a dummy byte to the pipe so fh becomes ready */
989 write (respipe [1], &respipe, 1); 1020 write (respipe [1], &respipe, 1);
990 1021
1096 create_pipe (); 1127 create_pipe ();
1097 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 1128 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
1098} 1129}
1099 1130
1100void 1131void
1101min_parallel (nthreads) 1132min_parallel (int nthreads)
1102 int nthreads
1103 PROTOTYPE: $ 1133 PROTOTYPE: $
1104 1134
1105void 1135void
1106max_parallel (nthreads) 1136max_parallel (int nthreads)
1107 int nthreads
1108 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
1109 1147
1110void 1148void
1111aio_open (pathname,flags,mode,callback=&PL_sv_undef) 1149aio_open (pathname,flags,mode,callback=&PL_sv_undef)
1112 SV * pathname 1150 SV * pathname
1113 int flags 1151 int flags
1368 req->type = REQ_NOP; 1406 req->type = REQ_NOP;
1369 1407
1370 REQ_SEND; 1408 REQ_SEND;
1371} 1409}
1372 1410
1373void 1411int
1374aioreq_pri (int pri = DEFAULT_PRI) 1412aioreq_pri (int pri = 0)
1375 CODE: 1413 PROTOTYPE: ;$
1414 CODE:
1415 RETVAL = next_pri - PRI_BIAS;
1416 if (items > 0)
1417 {
1376 if (pri < PRI_MIN) pri = PRI_MIN; 1418 if (pri < PRI_MIN) pri = PRI_MIN;
1377 if (pri > PRI_MAX) pri = PRI_MAX; 1419 if (pri > PRI_MAX) pri = PRI_MAX;
1378 next_pri = pri + PRI_BIAS; 1420 next_pri = pri + PRI_BIAS;
1421 }
1422 OUTPUT:
1423 RETVAL
1379 1424
1380void 1425void
1381aioreq_nice (int nice = 0) 1426aioreq_nice (int nice = 0)
1382 CODE: 1427 CODE:
1383 nice = next_pri - nice; 1428 nice = next_pri - nice;
1384 if (nice < PRI_MIN) nice = PRI_MIN; 1429 if (nice < PRI_MIN) nice = PRI_MIN;
1385 if (nice > PRI_MAX) nice = PRI_MAX; 1430 if (nice > PRI_MAX) nice = PRI_MAX;
1386 next_pri = nice + PRI_BIAS; 1431 next_pri = nice + PRI_BIAS;
1387 1432
1388void 1433void
1389flush () 1434flush ()
1390 PROTOTYPE: 1435 PROTOTYPE:
1391 CODE: 1436 CODE:
1392 while (nreqs) 1437 while (nreqs)
1393 { 1438 {
1394 poll_wait (); 1439 poll_wait ();
1395 poll_cb (); 1440 poll_cb (0);
1396 } 1441 }
1397 1442
1398void 1443void
1399poll() 1444poll()
1400 PROTOTYPE: 1445 PROTOTYPE:
1401 CODE: 1446 CODE:
1402 if (nreqs) 1447 if (nreqs)
1403 { 1448 {
1404 poll_wait (); 1449 poll_wait ();
1405 poll_cb (); 1450 poll_cb (0);
1406 } 1451 }
1407 1452
1408int 1453int
1409poll_fileno() 1454poll_fileno()
1410 PROTOTYPE: 1455 PROTOTYPE:
1415 1460
1416int 1461int
1417poll_cb(...) 1462poll_cb(...)
1418 PROTOTYPE: 1463 PROTOTYPE:
1419 CODE: 1464 CODE:
1420 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);
1421 OUTPUT: 1474 OUTPUT:
1422 RETVAL 1475 RETVAL
1423 1476
1424void 1477void
1425poll_wait() 1478poll_wait()
1434 CODE: 1487 CODE:
1435 RETVAL = nreqs; 1488 RETVAL = nreqs;
1436 OUTPUT: 1489 OUTPUT:
1437 RETVAL 1490 RETVAL
1438 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
1439PROTOTYPES: DISABLE 1512PROTOTYPES: DISABLE
1440 1513
1441MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1514MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1442 1515
1443void 1516void
1494void 1567void
1495result (aio_req grp, ...) 1568result (aio_req grp, ...)
1496 CODE: 1569 CODE:
1497{ 1570{
1498 int i; 1571 int i;
1572 AV *av;
1573
1574 grp->errorno = errno;
1575
1499 AV *av = newAV (); 1576 av = newAV ();
1500 1577
1501 for (i = 1; i < items; ++i ) 1578 for (i = 1; i < items; ++i )
1502 av_push (av, newSVsv (ST (i))); 1579 av_push (av, newSVsv (ST (i)));
1503 1580
1504 SvREFCNT_dec (grp->data); 1581 SvREFCNT_dec (grp->data);
1505 grp->data = (SV *)av; 1582 grp->data = (SV *)av;
1506} 1583}
1584
1585void
1586errno (aio_req grp, int errorno = errno)
1587 CODE:
1588 grp->errorno = errorno;
1507 1589
1508void 1590void
1509limit (aio_req grp, int limit) 1591limit (aio_req grp, int limit)
1510 CODE: 1592 CODE:
1511 grp->fd2 = limit; 1593 grp->fd2 = limit;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines