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.75 by root, Thu Oct 26 06:44:48 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"
60# define STACKSIZE PTHREAD_STACK_MIN 61# define STACKSIZE PTHREAD_STACK_MIN
61#else 62#else
62# define STACKSIZE 16384 63# define STACKSIZE 16384
63#endif 64#endif
64 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
65/* buffer size for various temporary buffers */ 78/* buffer size for various temporary buffers */
66#define AIO_BUFSIZE 65536 79#define AIO_BUFSIZE 65536
67 80
68#define dBUF \ 81#define dBUF \
69 char *aio_buf; \ 82 char *aio_buf; \
131 NUM_PRI = PRI_MAX + PRI_BIAS + 1, 144 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
132}; 145};
133 146
134static int next_pri = DEFAULT_PRI + PRI_BIAS; 147static int next_pri = DEFAULT_PRI + PRI_BIAS;
135 148
136static int started, wanted; 149static unsigned int started, wanted;
137static volatile int nreqs; 150static volatile unsigned int nreqs, nready, npending;
151static volatile unsigned int max_outstanding = 0xffffffff;
138static int respipe [2]; 152static int respipe [2];
139 153
140#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 154#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
141# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 155# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
142#else 156#else
144#endif 158#endif
145 159
146#define LOCK(mutex) pthread_mutex_lock (&(mutex)) 160#define LOCK(mutex) pthread_mutex_lock (&(mutex))
147#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 161#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
148 162
149/* worker threasd management */ 163/* worker threads management */
150static pthread_mutex_t wrklock = AIO_MUTEX_INIT; 164static pthread_mutex_t wrklock = AIO_MUTEX_INIT;
151 165
152typedef struct worker { 166typedef struct worker {
153 /* locked by wrklock */ 167 /* locked by wrklock */
154 struct worker *prev, *next; 168 struct worker *prev, *next;
242 } 256 }
243 257
244 abort (); 258 abort ();
245} 259}
246 260
247static int poll_cb (); 261static int poll_cb (int max);
248static void req_invoke (aio_req req); 262static void req_invoke (aio_req req);
249static void req_free (aio_req req); 263static void req_free (aio_req req);
250static void req_cancel (aio_req req); 264static void req_cancel (aio_req req);
251 265
252/* must be called at most once */ 266/* must be called at most once */
324 fd_set rfd; 338 fd_set rfd;
325 339
326 while (nreqs) 340 while (nreqs)
327 { 341 {
328 int size; 342 int size;
329#if !(__i386 || __x86_64) /* safe without sempahore on these archs */ 343 if (WORDREAD_UNSAFE) LOCK (reslock);
330 LOCK (reslock);
331#endif
332 size = res_queue.size; 344 size = res_queue.size;
333#if !(__i386 || __x86_64) /* safe without sempahore on these archs */ 345 if (WORDREAD_UNSAFE) UNLOCK (reslock);
334 UNLOCK (reslock);
335#endif
336 346
337 if (size) 347 if (size)
338 return; 348 return;
339 349
340 FD_ZERO(&rfd); 350 FD_ZERO(&rfd);
348{ 358{
349 dSP; 359 dSP;
350 360
351 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) 361 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
352 { 362 {
353 errno = req->errorno;
354
355 ENTER; 363 ENTER;
356 SAVETMPS; 364 SAVETMPS;
357 PUSHMARK (SP); 365 PUSHMARK (SP);
358 EXTEND (SP, 1); 366 EXTEND (SP, 1);
359 367
424 default: 432 default:
425 PUSHs (sv_2mortal (newSViv (req->result))); 433 PUSHs (sv_2mortal (newSViv (req->result)));
426 break; 434 break;
427 } 435 }
428 436
437 errno = req->errorno;
429 438
430 PUTBACK; 439 PUTBACK;
431 call_sv (req->callback, G_VOID | G_EVAL); 440 call_sv (req->callback, G_VOID | G_EVAL);
432 SPAGAIN; 441 SPAGAIN;
433 442
495 req->flags |= FLAG_CANCELLED; 504 req->flags |= FLAG_CANCELLED;
496 505
497 req_cancel_subs (req); 506 req_cancel_subs (req);
498} 507}
499 508
500static int poll_cb () 509static int poll_cb (int max)
501{ 510{
502 dSP; 511 dSP;
503 int count = 0; 512 int count = 0;
504 int do_croak = 0; 513 int do_croak = 0;
505 aio_req req; 514 aio_req req;
506 515
507 for (;;) 516 for (;;)
508 { 517 {
509 LOCK (reslock); 518 while (max <= 0 || count < max)
510 req = reqq_shift (&res_queue);
511
512 if (req)
513 { 519 {
520 LOCK (reslock);
521 req = reqq_shift (&res_queue);
522
514 if (!res_queue.size) 523 if (req)
515 { 524 {
525 --npending;
526
527 if (!res_queue.size)
528 {
516 /* read any signals sent by the worker threads */ 529 /* read any signals sent by the worker threads */
517 char buf [32]; 530 char buf [32];
518 while (read (respipe [0], buf, 32) == 32) 531 while (read (respipe [0], buf, 32) == 32)
532 ;
519 ; 533 }
520 } 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);
521 } 571 }
522 572
523 UNLOCK (reslock); 573 if (nreqs <= max_outstanding)
524
525 if (!req)
526 break; 574 break;
527 575
528 --nreqs; 576 poll_wait ();
529 577
530 if (req->type == REQ_QUIT) 578 max = 0;
531 started--;
532 else if (req->type == REQ_GROUP && req->length)
533 {
534 req->fd = 1; /* mark request as delayed */
535 continue;
536 }
537 else
538 {
539 if (req->type == REQ_READ)
540 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
541
542 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
543 SvREADONLY_off (req->data);
544
545 if (req->statdata)
546 {
547 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
548 PL_laststatval = req->result;
549 PL_statcache = *(req->statdata);
550 }
551
552 req_invoke (req);
553
554 count++;
555 }
556
557 req_free (req);
558 } 579 }
559 580
560 return count; 581 return count;
561} 582}
562 583
585 { 606 {
586 wrk->prev = &wrk_first; 607 wrk->prev = &wrk_first;
587 wrk->next = wrk_first.next; 608 wrk->next = wrk_first.next;
588 wrk_first.next->prev = wrk; 609 wrk_first.next->prev = wrk;
589 wrk_first.next = wrk; 610 wrk_first.next = wrk;
590 started++; 611 ++started;
591 } 612 }
592 else 613 else
593 free (wrk); 614 free (wrk);
594 615
595 sigprocmask (SIG_SETMASK, &oldsigset, 0); 616 sigprocmask (SIG_SETMASK, &oldsigset, 0);
602 start_thread (); 623 start_thread ();
603 624
604 ++nreqs; 625 ++nreqs;
605 626
606 LOCK (reqlock); 627 LOCK (reqlock);
628 ++nready;
607 reqq_push (&req_queue, req); 629 reqq_push (&req_queue, req);
608 pthread_cond_signal (&reqwait); 630 pthread_cond_signal (&reqwait);
609 UNLOCK (reqlock); 631 UNLOCK (reqlock);
610} 632}
611 633
641 } 663 }
642 664
643 while (started > wanted) 665 while (started > wanted)
644 { 666 {
645 poll_wait (); 667 poll_wait ();
646 poll_cb (); 668 poll_cb (0);
647 } 669 }
648} 670}
649 671
650static void create_pipe () 672static void create_pipe ()
651{ 673{
779 { 801 {
780 off_t sbytes; 802 off_t sbytes;
781 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 803 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
782 804
783 if (res < 0 && sbytes) 805 if (res < 0 && sbytes)
784 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ 806 /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */
785 res = sbytes; 807 res = sbytes;
786 } 808 }
787 809
788# elif __hpux 810# elif __hpux
789 res = sendfile (ofd, ifd, offset, count, 0, 0); 811 res = sendfile (ofd, ifd, offset, count, 0, 0);
869 int errorno; 891 int errorno;
870 892
871 LOCK (wrklock); 893 LOCK (wrklock);
872 self->dirp = dirp = opendir (req->dataptr); 894 self->dirp = dirp = opendir (req->dataptr);
873 self->dbuf = u = malloc (sizeof (*u)); 895 self->dbuf = u = malloc (sizeof (*u));
896 req->data2ptr = names = malloc (memlen);
874 UNLOCK (wrklock); 897 UNLOCK (wrklock);
875
876 req->data2ptr = names = malloc (memlen);
877 898
878 if (dirp && u && names) 899 if (dirp && u && names)
879 for (;;) 900 for (;;)
880 { 901 {
881 errno = 0; 902 errno = 0;
933 if (req) 954 if (req)
934 break; 955 break;
935 956
936 pthread_cond_wait (&reqwait, &reqlock); 957 pthread_cond_wait (&reqwait, &reqlock);
937 } 958 }
959
960 --nready;
938 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 */
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:
1398 while (nreqs) 1437 while (nreqs)
1399 { 1438 {
1400 poll_wait (); 1439 poll_wait ();
1401 poll_cb (); 1440 poll_cb (0);
1402 } 1441 }
1403 1442
1404void 1443void
1405poll() 1444poll()
1406 PROTOTYPE: 1445 PROTOTYPE:
1407 CODE: 1446 CODE:
1408 if (nreqs) 1447 if (nreqs)
1409 { 1448 {
1410 poll_wait (); 1449 poll_wait ();
1411 poll_cb (); 1450 poll_cb (0);
1412 } 1451 }
1413 1452
1414int 1453int
1415poll_fileno() 1454poll_fileno()
1416 PROTOTYPE: 1455 PROTOTYPE:
1421 1460
1422int 1461int
1423poll_cb(...) 1462poll_cb(...)
1424 PROTOTYPE: 1463 PROTOTYPE:
1425 CODE: 1464 CODE:
1426 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);
1427 OUTPUT: 1474 OUTPUT:
1428 RETVAL 1475 RETVAL
1429 1476
1430void 1477void
1431poll_wait() 1478poll_wait()
1440 CODE: 1487 CODE:
1441 RETVAL = nreqs; 1488 RETVAL = nreqs;
1442 OUTPUT: 1489 OUTPUT:
1443 RETVAL 1490 RETVAL
1444 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
1445PROTOTYPES: DISABLE 1512PROTOTYPES: DISABLE
1446 1513
1447MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1514MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1448 1515
1449void 1516void
1500void 1567void
1501result (aio_req grp, ...) 1568result (aio_req grp, ...)
1502 CODE: 1569 CODE:
1503{ 1570{
1504 int i; 1571 int i;
1572 AV *av;
1573
1574 grp->errorno = errno;
1575
1505 AV *av = newAV (); 1576 av = newAV ();
1506 1577
1507 for (i = 1; i < items; ++i ) 1578 for (i = 1; i < items; ++i )
1508 av_push (av, newSVsv (ST (i))); 1579 av_push (av, newSVsv (ST (i)));
1509 1580
1510 SvREFCNT_dec (grp->data); 1581 SvREFCNT_dec (grp->data);
1511 grp->data = (SV *)av; 1582 grp->data = (SV *)av;
1512} 1583}
1584
1585void
1586errno (aio_req grp, int errorno = errno)
1587 CODE:
1588 grp->errorno = errorno;
1513 1589
1514void 1590void
1515limit (aio_req grp, int limit) 1591limit (aio_req grp, int limit)
1516 CODE: 1592 CODE:
1517 grp->fd2 = limit; 1593 grp->fd2 = limit;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines