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.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"
131 NUM_PRI = PRI_MAX + PRI_BIAS + 1, 132 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
132}; 133};
133 134
134static int next_pri = DEFAULT_PRI + PRI_BIAS; 135static int next_pri = DEFAULT_PRI + PRI_BIAS;
135 136
136static int started, wanted; 137static unsigned int started, wanted;
137static volatile int nreqs; 138static volatile unsigned int nreqs;
139static volatile unsigned int max_outstanding = 0xffffffff;
138static int respipe [2]; 140static int respipe [2];
139 141
140#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 142#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
141# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 143# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
142#else 144#else
144#endif 146#endif
145 147
146#define LOCK(mutex) pthread_mutex_lock (&(mutex)) 148#define LOCK(mutex) pthread_mutex_lock (&(mutex))
147#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) 149#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
148 150
149/* worker threasd management */ 151/* worker threads management */
150static pthread_mutex_t wrklock = AIO_MUTEX_INIT; 152static pthread_mutex_t wrklock = AIO_MUTEX_INIT;
151 153
152typedef struct worker { 154typedef struct worker {
153 /* locked by wrklock */ 155 /* locked by wrklock */
154 struct worker *prev, *next; 156 struct worker *prev, *next;
242 } 244 }
243 245
244 abort (); 246 abort ();
245} 247}
246 248
247static int poll_cb (); 249static int poll_cb (int max);
248static void req_invoke (aio_req req); 250static void req_invoke (aio_req req);
249static void req_free (aio_req req); 251static void req_free (aio_req req);
250static void req_cancel (aio_req req); 252static void req_cancel (aio_req req);
251 253
252/* must be called at most once */ 254/* must be called at most once */
495 req->flags |= FLAG_CANCELLED; 497 req->flags |= FLAG_CANCELLED;
496 498
497 req_cancel_subs (req); 499 req_cancel_subs (req);
498} 500}
499 501
500static int poll_cb () 502static int poll_cb (int max)
501{ 503{
502 dSP; 504 dSP;
503 int count = 0; 505 int count = 0;
504 int do_croak = 0; 506 int do_croak = 0;
505 aio_req req; 507 aio_req req;
506 508
507 for (;;) 509 for (;;)
508 { 510 {
509 LOCK (reslock); 511 while (max <= 0 || count < max)
510 req = reqq_shift (&res_queue);
511
512 if (req)
513 { 512 {
513 LOCK (reslock);
514 req = reqq_shift (&res_queue);
515
514 if (!res_queue.size) 516 if (req)
515 { 517 {
518 if (!res_queue.size)
519 {
516 /* read any signals sent by the worker threads */ 520 /* read any signals sent by the worker threads */
517 char buf [32]; 521 char buf [32];
518 while (read (respipe [0], buf, 32) == 32) 522 while (read (respipe [0], buf, 32) == 32)
523 ;
519 ; 524 }
520 } 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);
521 } 562 }
522 563
523 UNLOCK (reslock); 564 if (nreqs <= max_outstanding)
524
525 if (!req)
526 break; 565 break;
527 566
528 --nreqs; 567 poll_wait ();
529 568
530 if (req->type == REQ_QUIT) 569 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 } 570 }
559 571
560 return count; 572 return count;
561} 573}
562 574
585 { 597 {
586 wrk->prev = &wrk_first; 598 wrk->prev = &wrk_first;
587 wrk->next = wrk_first.next; 599 wrk->next = wrk_first.next;
588 wrk_first.next->prev = wrk; 600 wrk_first.next->prev = wrk;
589 wrk_first.next = wrk; 601 wrk_first.next = wrk;
590 started++; 602 ++started;
591 } 603 }
592 else 604 else
593 free (wrk); 605 free (wrk);
594 606
595 sigprocmask (SIG_SETMASK, &oldsigset, 0); 607 sigprocmask (SIG_SETMASK, &oldsigset, 0);
641 } 653 }
642 654
643 while (started > wanted) 655 while (started > wanted)
644 { 656 {
645 poll_wait (); 657 poll_wait ();
646 poll_cb (); 658 poll_cb (0);
647 } 659 }
648} 660}
649 661
650static void create_pipe () 662static void create_pipe ()
651{ 663{
779 { 791 {
780 off_t sbytes; 792 off_t sbytes;
781 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 793 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
782 794
783 if (res < 0 && sbytes) 795 if (res < 0 && sbytes)
784 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ 796 /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */
785 res = sbytes; 797 res = sbytes;
786 } 798 }
787 799
788# elif __hpux 800# elif __hpux
789 res = sendfile (ofd, ifd, offset, count, 0, 0); 801 res = sendfile (ofd, ifd, offset, count, 0, 0);
869 int errorno; 881 int errorno;
870 882
871 LOCK (wrklock); 883 LOCK (wrklock);
872 self->dirp = dirp = opendir (req->dataptr); 884 self->dirp = dirp = opendir (req->dataptr);
873 self->dbuf = u = malloc (sizeof (*u)); 885 self->dbuf = u = malloc (sizeof (*u));
886 req->data2ptr = names = malloc (memlen);
874 UNLOCK (wrklock); 887 UNLOCK (wrklock);
875
876 req->data2ptr = names = malloc (memlen);
877 888
878 if (dirp && u && names) 889 if (dirp && u && names)
879 for (;;) 890 for (;;)
880 { 891 {
881 errno = 0; 892 errno = 0;
1102 create_pipe (); 1113 create_pipe ();
1103 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 1114 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
1104} 1115}
1105 1116
1106void 1117void
1107min_parallel (nthreads) 1118min_parallel (int nthreads)
1108 int nthreads
1109 PROTOTYPE: $ 1119 PROTOTYPE: $
1110 1120
1111void 1121void
1112max_parallel (nthreads) 1122max_parallel (int nthreads)
1113 int nthreads
1114 PROTOTYPE: $ 1123 PROTOTYPE: $
1124
1125int
1126max_outstanding (int maxreqs)
1127 PROTOTYPE: $
1128 CODE:
1129 RETVAL = max_outstanding;
1130 max_outstanding = maxreqs;
1131 OUTPUT:
1132 RETVAL
1115 1133
1116void 1134void
1117aio_open (pathname,flags,mode,callback=&PL_sv_undef) 1135aio_open (pathname,flags,mode,callback=&PL_sv_undef)
1118 SV * pathname 1136 SV * pathname
1119 int flags 1137 int flags
1396 PROTOTYPE: 1414 PROTOTYPE:
1397 CODE: 1415 CODE:
1398 while (nreqs) 1416 while (nreqs)
1399 { 1417 {
1400 poll_wait (); 1418 poll_wait ();
1401 poll_cb (); 1419 poll_cb (0);
1402 } 1420 }
1403 1421
1404void 1422void
1405poll() 1423poll()
1406 PROTOTYPE: 1424 PROTOTYPE:
1407 CODE: 1425 CODE:
1408 if (nreqs) 1426 if (nreqs)
1409 { 1427 {
1410 poll_wait (); 1428 poll_wait ();
1411 poll_cb (); 1429 poll_cb (0);
1412 } 1430 }
1413 1431
1414int 1432int
1415poll_fileno() 1433poll_fileno()
1416 PROTOTYPE: 1434 PROTOTYPE:
1421 1439
1422int 1440int
1423poll_cb(...) 1441poll_cb(...)
1424 PROTOTYPE: 1442 PROTOTYPE:
1425 CODE: 1443 CODE:
1426 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);
1427 OUTPUT: 1453 OUTPUT:
1428 RETVAL 1454 RETVAL
1429 1455
1430void 1456void
1431poll_wait() 1457poll_wait()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines