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.69 by root, Tue Oct 24 11:57:30 2006 UTC vs.
Revision 1.81 by root, Fri Oct 27 20:10:06 2006 UTC

1#if __linux 1/* solaris */
2#define _POSIX_PTHREAD_SEMANTICS 1
3
4#if __linux && !defined(_GNU_SOURCE)
2# define _GNU_SOURCE 5# define _GNU_SOURCE
3#endif 6#endif
4 7
8/* just in case */
5#define _REENTRANT 1 9#define _REENTRANT 1
6 10
7#include <errno.h> 11#include <errno.h>
8 12
9#include "EXTERN.h" 13#include "EXTERN.h"
44/* used for struct dirent, AIX doesn't provide it */ 48/* used for struct dirent, AIX doesn't provide it */
45#ifndef NAME_MAX 49#ifndef NAME_MAX
46# define NAME_MAX 4096 50# define NAME_MAX 4096
47#endif 51#endif
48 52
53#ifndef PTHREAD_STACK_MIN
54/* care for broken platforms, e.g. windows */
55# define PTHREAD_STACK_MIN 16384
56#endif
57
49#if __ia64 58#if __ia64
50# define STACKSIZE 65536 59# define STACKSIZE 65536
51#elif __i386 || __x86_64 /* 16k is unreasonably high :( */ 60#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
52# define STACKSIZE PTHREAD_STACK_MIN 61# define STACKSIZE PTHREAD_STACK_MIN
53#else 62#else
54# define STACKSIZE 16384 63# define STACKSIZE 16384
55#endif 64#endif
56 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
57/* buffer size for various temporary buffers */ 78/* buffer size for various temporary buffers */
58#define AIO_BUFSIZE 65536 79#define AIO_BUFSIZE 65536
59 80
60#define dBUF \ 81#define dBUF \
82 char *aio_buf; \
83 LOCK (wrklock); \
61 char *aio_buf = malloc (AIO_BUFSIZE); \ 84 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \
85 UNLOCK (wrklock); \
62 if (!aio_buf) \ 86 if (!aio_buf) \
63 return -1; 87 return -1;
64
65#define fBUF free (aio_buf)
66 88
67enum { 89enum {
68 REQ_QUIT, 90 REQ_QUIT,
69 REQ_OPEN, REQ_CLOSE, 91 REQ_OPEN, REQ_CLOSE,
70 REQ_READ, REQ_WRITE, REQ_READAHEAD, 92 REQ_READ, REQ_WRITE, REQ_READAHEAD,
71 REQ_SENDFILE, 93 REQ_SENDFILE,
72 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 94 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
73 REQ_FSYNC, REQ_FDATASYNC, 95 REQ_FSYNC, REQ_FDATASYNC,
74 REQ_UNLINK, REQ_RMDIR, REQ_RENAME, 96 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
75 REQ_READDIR, 97 REQ_MKNOD, REQ_READDIR,
76 REQ_LINK, REQ_SYMLINK, 98 REQ_LINK, REQ_SYMLINK,
77 REQ_GROUP, REQ_NOP, 99 REQ_GROUP, REQ_NOP,
78 REQ_BUSY, 100 REQ_BUSY,
79}; 101};
80 102
122 NUM_PRI = PRI_MAX + PRI_BIAS + 1, 144 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
123}; 145};
124 146
125static int next_pri = DEFAULT_PRI + PRI_BIAS; 147static int next_pri = DEFAULT_PRI + PRI_BIAS;
126 148
127static int started, wanted; 149static unsigned int started, wanted;
128static volatile int nreqs;
129static int max_outstanding = 1<<30;
130static int respipe [2];
131 150
132#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 151#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
133# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 152# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
134#else 153#else
135# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER 154# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
136#endif 155#endif
137 156
157#define LOCK(mutex) pthread_mutex_lock (&(mutex))
158#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
159
160/* worker threads management */
161static pthread_mutex_t wrklock = AIO_MUTEX_INIT;
162
163typedef struct worker {
164 /* locked by wrklock */
165 struct worker *prev, *next;
166
167 pthread_t tid;
168
169 /* locked by reslock, reqlock or wrklock */
170 aio_req req; /* currently processed request */
171 void *dbuf;
172 DIR *dirp;
173} worker;
174
175static worker wrk_first = { &wrk_first, &wrk_first, 0 };
176
177static void worker_clear (worker *wrk)
178{
179 if (wrk->dirp)
180 {
181 closedir (wrk->dirp);
182 wrk->dirp = 0;
183 }
184
185 if (wrk->dbuf)
186 {
187 free (wrk->dbuf);
188 wrk->dbuf = 0;
189 }
190}
191
192static void worker_free (worker *wrk)
193{
194 wrk->next->prev = wrk->prev;
195 wrk->prev->next = wrk->next;
196
197 free (wrk);
198}
199
200static volatile unsigned int nreqs, nready, npending;
201static volatile unsigned int max_outstanding = 0xffffffff;
202static int respipe [2];
203
138static pthread_mutex_t reslock = AIO_MUTEX_INIT; 204static pthread_mutex_t reslock = AIO_MUTEX_INIT;
139static pthread_mutex_t reqlock = AIO_MUTEX_INIT; 205static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
140static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 206static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
207
208#if WORDREAD_UNSAFE
209
210static unsigned int get_nready ()
211{
212 unsigned int retval;
213
214 LOCK (reqlock);
215 retval = nready;
216 UNLOCK (reqlock);
217
218 return retval;
219}
220
221static unsigned int get_npending ()
222{
223 unsigned int retval;
224
225 LOCK (reslock);
226 retval = npending;
227 UNLOCK (reslock);
228
229 return retval;
230}
231
232#else
233
234# define get_nready() nready
235# define get_npending() npending
236
237#endif
141 238
142/* 239/*
143 * a somewhat faster data structure might be nice, but 240 * a somewhat faster data structure might be nice, but
144 * with 8 priorities this actually needs <20 insns 241 * with 8 priorities this actually needs <20 insns
145 * per shift, the most expensive operation. 242 * per shift, the most expensive operation.
191 } 288 }
192 289
193 abort (); 290 abort ();
194} 291}
195 292
293static int poll_cb (int max);
196static void req_invoke (aio_req req); 294static void req_invoke (aio_req req);
197static void req_free (aio_req req); 295static void req_free (aio_req req);
296static void req_cancel (aio_req req);
198 297
199/* must be called at most once */ 298/* must be called at most once */
200static SV *req_sv (aio_req req, const char *klass) 299static SV *req_sv (aio_req req, const char *klass)
201{ 300{
202 if (!req->self) 301 if (!req->self)
264 req_invoke (grp); 363 req_invoke (grp);
265 req_free (grp); 364 req_free (grp);
266 } 365 }
267} 366}
268 367
269static void poll_wait ()
270{
271 fd_set rfd;
272
273 while (nreqs)
274 {
275 int size;
276#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
277 pthread_mutex_lock (&reslock);
278#endif
279 size = res_queue.size;
280#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
281 pthread_mutex_unlock (&reslock);
282#endif
283
284 if (size)
285 return;
286
287 FD_ZERO(&rfd);
288 FD_SET(respipe [0], &rfd);
289
290 select (respipe [0] + 1, &rfd, 0, 0, 0);
291 }
292}
293
294static void req_invoke (aio_req req) 368static void req_invoke (aio_req req)
295{ 369{
296 dSP; 370 dSP;
297 371
298 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) 372 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
299 { 373 {
300 errno = req->errorno;
301
302 ENTER; 374 ENTER;
303 SAVETMPS; 375 SAVETMPS;
304 PUSHMARK (SP); 376 PUSHMARK (SP);
305 EXTEND (SP, 1); 377 EXTEND (SP, 1);
306 378
310 { 382 {
311 SV *rv = &PL_sv_undef; 383 SV *rv = &PL_sv_undef;
312 384
313 if (req->result >= 0) 385 if (req->result >= 0)
314 { 386 {
387 int i;
315 char *buf = req->data2ptr; 388 char *buf = req->data2ptr;
316 AV *av = newAV (); 389 AV *av = newAV ();
317 390
318 while (req->result) 391 av_extend (av, req->result - 1);
392
393 for (i = 0; i < req->result; ++i)
319 { 394 {
320 SV *sv = newSVpv (buf, 0); 395 SV *sv = newSVpv (buf, 0);
321 396
322 av_push (av, sv); 397 av_store (av, i, sv);
323 buf += SvCUR (sv) + 1; 398 buf += SvCUR (sv) + 1;
324 req->result--;
325 } 399 }
326 400
327 rv = sv_2mortal (newRV_noinc ((SV *)av)); 401 rv = sv_2mortal (newRV_noinc ((SV *)av));
328 } 402 }
329 403
369 default: 443 default:
370 PUSHs (sv_2mortal (newSViv (req->result))); 444 PUSHs (sv_2mortal (newSViv (req->result)));
371 break; 445 break;
372 } 446 }
373 447
448 errno = req->errorno;
374 449
375 PUTBACK; 450 PUTBACK;
376 call_sv (req->callback, G_VOID | G_EVAL); 451 call_sv (req->callback, G_VOID | G_EVAL);
377 SPAGAIN; 452 SPAGAIN;
378 453
413 SvREFCNT_dec (req->fh); 488 SvREFCNT_dec (req->fh);
414 SvREFCNT_dec (req->fh2); 489 SvREFCNT_dec (req->fh2);
415 SvREFCNT_dec (req->callback); 490 SvREFCNT_dec (req->callback);
416 Safefree (req->statdata); 491 Safefree (req->statdata);
417 492
418 if (req->type == REQ_READDIR && req->result >= 0) 493 if (req->type == REQ_READDIR)
419 free (req->data2ptr); 494 free (req->data2ptr);
420 495
421 Safefree (req); 496 Safefree (req);
422} 497}
423 498
499static void req_cancel_subs (aio_req grp)
500{
501 aio_req sub;
502
503 if (grp->type != REQ_GROUP)
504 return;
505
506 SvREFCNT_dec (grp->fh2);
507 grp->fh2 = 0;
508
509 for (sub = grp->grp_first; sub; sub = sub->grp_next)
510 req_cancel (sub);
511}
512
424static void req_cancel (aio_req req) 513static void req_cancel (aio_req req)
425{ 514{
426 req->flags |= FLAG_CANCELLED; 515 req->flags |= FLAG_CANCELLED;
427 516
428 if (req->type == REQ_GROUP) 517 req_cancel_subs (req);
429 { 518}
430 aio_req sub;
431 519
432 for (sub = req->grp_first; sub; sub = sub->grp_next) 520static void *aio_proc(void *arg);
433 req_cancel (sub); 521
522static void start_thread (void)
523{
524 sigset_t fullsigset, oldsigset;
525 pthread_attr_t attr;
526
527 worker *wrk = calloc (1, sizeof (worker));
528
529 if (!wrk)
530 croak ("unable to allocate worker thread data");
531
532 pthread_attr_init (&attr);
533 pthread_attr_setstacksize (&attr, STACKSIZE);
534 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
535#ifdef PTHREAD_SCOPE_PROCESS
536 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
537#endif
538
539 sigfillset (&fullsigset);
540
541 LOCK (wrklock);
542 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset);
543
544 if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0)
434 } 545 {
435} 546 wrk->prev = &wrk_first;
547 wrk->next = wrk_first.next;
548 wrk_first.next->prev = wrk;
549 wrk_first.next = wrk;
550 ++started;
551 }
552 else
553 free (wrk);
436 554
555 sigprocmask (SIG_SETMASK, &oldsigset, 0);
556 UNLOCK (wrklock);
557}
558
559static void maybe_start_thread ()
560{
561#if 0
562 static struct timeval last;
563 struct timeval diff, now;
564#endif
565
566 if (started >= wanted)
567 return;
568
569 if (nready <= nreqs - get_nready () - get_npending ())
570 return;
571
572#if 0
573 gettimeofday (&now, 0);
574
575 diff.tv_sec = now.tv_sec - last.tv_sec;
576 diff.tv_usec = now.tv_usec - last.tv_usec;
577
578 if (diff.tv_usec < 0)
579 {
580 --diff.tv_sec;
581 diff.tv_usec += 1000000;
582 }
583
584 if (!diff.tv_sec && diff.tv_usec < 10000)
585 return;
586
587 last = now;
588#endif
589
590 start_thread ();
591}
592
593static void req_send (aio_req req)
594{
595 ++nreqs;
596
597 LOCK (reqlock);
598 ++nready;
599 reqq_push (&req_queue, req);
600 pthread_cond_signal (&reqwait);
601 UNLOCK (reqlock);
602
603 maybe_start_thread ();
604}
605
606static void end_thread (void)
607{
608 aio_req req;
609
610 Newz (0, req, 1, aio_cb);
611
612 req->type = REQ_QUIT;
613 req->pri = PRI_MAX + PRI_BIAS;
614
615 req_send (req);
616
617 LOCK (wrklock);
618 --started;
619 UNLOCK (wrklock);
620}
621
622static void min_parallel (int nthreads)
623{
624 if (wanted < nthreads)
625 wanted = nthreads;
626}
627
628static void max_parallel (int nthreads)
629{
630 if (wanted > nthreads)
631 wanted = nthreads;
632
633 while (started > wanted)
634 end_thread ();
635}
636
637static void poll_wait ()
638{
639 fd_set rfd;
640
641 while (nreqs)
642 {
643 int size;
644 if (WORDREAD_UNSAFE) LOCK (reslock);
645 size = res_queue.size;
646 if (WORDREAD_UNSAFE) UNLOCK (reslock);
647
648 if (size)
649 return;
650
651 maybe_start_thread ();
652
653 FD_ZERO(&rfd);
654 FD_SET(respipe [0], &rfd);
655
656 select (respipe [0] + 1, &rfd, 0, 0, 0);
657 }
658}
659
437static int poll_cb () 660static int poll_cb (int max)
438{ 661{
439 dSP; 662 dSP;
440 int count = 0; 663 int count = 0;
441 int do_croak = 0; 664 int do_croak = 0;
442 aio_req req; 665 aio_req req;
443 666
444 for (;;) 667 for (;;)
445 { 668 {
446 pthread_mutex_lock (&reslock); 669 while (max <= 0 || count < max)
447 req = reqq_shift (&res_queue);
448
449 if (req)
450 { 670 {
671 maybe_start_thread ();
672
673 LOCK (reslock);
674 req = reqq_shift (&res_queue);
675
451 if (!res_queue.size) 676 if (req)
452 { 677 {
678 --npending;
679
680 if (!res_queue.size)
681 {
453 /* read any signals sent by the worker threads */ 682 /* read any signals sent by the worker threads */
454 char buf [32]; 683 char buf [32];
455 while (read (respipe [0], buf, 32) == 32) 684 while (read (respipe [0], buf, 32) == 32)
685 ;
456 ; 686 }
457 } 687 }
688
689 UNLOCK (reslock);
690
691 if (!req)
692 break;
693
694 --nreqs;
695
696 if (req->type == REQ_GROUP && req->length)
697 {
698 req->fd = 1; /* mark request as delayed */
699 continue;
700 }
701 else
702 {
703 if (req->type == REQ_READ)
704 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
705
706 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
707 SvREADONLY_off (req->data);
708
709 if (req->statdata)
710 {
711 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
712 PL_laststatval = req->result;
713 PL_statcache = *(req->statdata);
714 }
715
716 req_invoke (req);
717
718 count++;
719 }
720
721 req_free (req);
458 } 722 }
459 723
460 pthread_mutex_unlock (&reslock); 724 if (nreqs <= max_outstanding)
461
462 if (!req)
463 break; 725 break;
464 726
465 --nreqs; 727 poll_wait ();
466 728
467 if (req->type == REQ_QUIT) 729 max = 0;
468 started--;
469 else if (req->type == REQ_GROUP && req->length)
470 {
471 req->fd = 1; /* mark request as delayed */
472 continue;
473 }
474 else
475 {
476 if (req->type == REQ_READ)
477 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
478
479 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
480 SvREADONLY_off (req->data);
481
482 if (req->statdata)
483 {
484 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
485 PL_laststatval = req->result;
486 PL_statcache = *(req->statdata);
487 }
488
489 req_invoke (req);
490
491 count++;
492 }
493
494 req_free (req);
495 } 730 }
496 731
497 return count; 732 return count;
498}
499
500static void *aio_proc(void *arg);
501
502static void start_thread (void)
503{
504 sigset_t fullsigset, oldsigset;
505 pthread_t tid;
506 pthread_attr_t attr;
507
508 pthread_attr_init (&attr);
509 pthread_attr_setstacksize (&attr, STACKSIZE);
510 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
511
512 sigfillset (&fullsigset);
513 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset);
514
515 if (pthread_create (&tid, &attr, aio_proc, 0) == 0)
516 started++;
517
518 sigprocmask (SIG_SETMASK, &oldsigset, 0);
519}
520
521static void req_send (aio_req req)
522{
523 while (started < wanted && nreqs >= started)
524 start_thread ();
525
526 ++nreqs;
527
528 pthread_mutex_lock (&reqlock);
529 reqq_push (&req_queue, req);
530 pthread_cond_signal (&reqwait);
531 pthread_mutex_unlock (&reqlock);
532
533 if (nreqs > max_outstanding)
534 for (;;)
535 {
536 poll_cb ();
537
538 if (nreqs <= max_outstanding)
539 break;
540
541 poll_wait ();
542 }
543}
544
545static void end_thread (void)
546{
547 aio_req req;
548
549 Newz (0, req, 1, aio_cb);
550
551 req->type = REQ_QUIT;
552 req->pri = PRI_MAX + PRI_BIAS;
553
554 req_send (req);
555}
556
557static void min_parallel (int nthreads)
558{
559 if (wanted < nthreads)
560 wanted = nthreads;
561}
562
563static void max_parallel (int nthreads)
564{
565 int cur = started;
566
567 if (wanted > nthreads)
568 wanted = nthreads;
569
570 while (cur > wanted)
571 {
572 end_thread ();
573 cur--;
574 }
575
576 while (started > wanted)
577 {
578 poll_wait ();
579 poll_cb ();
580 }
581} 733}
582 734
583static void create_pipe () 735static void create_pipe ()
584{ 736{
585 if (pipe (respipe)) 737 if (pipe (respipe))
609static ssize_t pread (int fd, void *buf, size_t count, off_t offset) 761static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
610{ 762{
611 ssize_t res; 763 ssize_t res;
612 off_t ooffset; 764 off_t ooffset;
613 765
614 pthread_mutex_lock (&preadwritelock); 766 LOCK (preadwritelock);
615 ooffset = lseek (fd, 0, SEEK_CUR); 767 ooffset = lseek (fd, 0, SEEK_CUR);
616 lseek (fd, offset, SEEK_SET); 768 lseek (fd, offset, SEEK_SET);
617 res = read (fd, buf, count); 769 res = read (fd, buf, count);
618 lseek (fd, ooffset, SEEK_SET); 770 lseek (fd, ooffset, SEEK_SET);
619 pthread_mutex_unlock (&preadwritelock); 771 UNLOCK (preadwritelock);
620 772
621 return res; 773 return res;
622} 774}
623 775
624static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) 776static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
625{ 777{
626 ssize_t res; 778 ssize_t res;
627 off_t ooffset; 779 off_t ooffset;
628 780
629 pthread_mutex_lock (&preadwritelock); 781 LOCK (preadwritelock);
630 ooffset = lseek (fd, 0, SEEK_CUR); 782 ooffset = lseek (fd, 0, SEEK_CUR);
631 lseek (fd, offset, SEEK_SET); 783 lseek (fd, offset, SEEK_SET);
632 res = write (fd, buf, count); 784 res = write (fd, buf, count);
633 lseek (fd, offset, SEEK_SET); 785 lseek (fd, offset, SEEK_SET);
634 pthread_mutex_unlock (&preadwritelock); 786 UNLOCK (preadwritelock);
635 787
636 return res; 788 return res;
637} 789}
638#endif 790#endif
639 791
640#if !HAVE_FDATASYNC 792#if !HAVE_FDATASYNC
641# define fdatasync fsync 793# define fdatasync fsync
642#endif 794#endif
643 795
644#if !HAVE_READAHEAD 796#if !HAVE_READAHEAD
645# define readahead aio_readahead 797# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self)
646 798
647static ssize_t readahead (int fd, off_t offset, size_t count) 799static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self)
648{ 800{
649 dBUF; 801 dBUF;
650 802
651 while (count > 0) 803 while (count > 0)
652 { 804 {
655 pread (fd, aio_buf, len, offset); 807 pread (fd, aio_buf, len, offset);
656 offset += len; 808 offset += len;
657 count -= len; 809 count -= len;
658 } 810 }
659 811
660 fBUF;
661
662 errno = 0; 812 errno = 0;
663} 813}
814
664#endif 815#endif
665 816
666#if !HAVE_READDIR_R 817#if !HAVE_READDIR_R
667# define readdir_r aio_readdir_r 818# define readdir_r aio_readdir_r
668 819
671static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) 822static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
672{ 823{
673 struct dirent *e; 824 struct dirent *e;
674 int errorno; 825 int errorno;
675 826
676 pthread_mutex_lock (&readdirlock); 827 LOCK (readdirlock);
677 828
678 e = readdir (dirp); 829 e = readdir (dirp);
679 errorno = errno; 830 errorno = errno;
680 831
681 if (e) 832 if (e)
684 strcpy (ent->d_name, e->d_name); 835 strcpy (ent->d_name, e->d_name);
685 } 836 }
686 else 837 else
687 *res = 0; 838 *res = 0;
688 839
689 pthread_mutex_unlock (&readdirlock); 840 UNLOCK (readdirlock);
690 841
691 errno = errorno; 842 errno = errorno;
692 return e ? 0 : -1; 843 return e ? 0 : -1;
693} 844}
694#endif 845#endif
695 846
696/* sendfile always needs emulation */ 847/* sendfile always needs emulation */
697static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) 848static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count, worker *self)
698{ 849{
699 ssize_t res; 850 ssize_t res;
700 851
701 if (!count) 852 if (!count)
702 return 0; 853 return 0;
713 { 864 {
714 off_t sbytes; 865 off_t sbytes;
715 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 866 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
716 867
717 if (res < 0 && sbytes) 868 if (res < 0 && sbytes)
718 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ 869 /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */
719 res = sbytes; 870 res = sbytes;
720 } 871 }
721 872
722# elif __hpux 873# elif __hpux
723 res = sendfile (ofd, ifd, offset, count, 0, 0); 874 res = sendfile (ofd, ifd, offset, count, 0, 0);
779 930
780 offset += cnt; 931 offset += cnt;
781 res += cnt; 932 res += cnt;
782 count -= cnt; 933 count -= cnt;
783 } 934 }
784
785 fBUF;
786 } 935 }
787 936
788 return res; 937 return res;
789} 938}
790 939
791/* read a full directory */ 940/* read a full directory */
792static int scandir_ (const char *path, void **namesp) 941static void scandir_ (aio_req req, worker *self)
793{ 942{
794 DIR *dirp; 943 DIR *dirp;
795 union 944 union
796 { 945 {
797 struct dirent d; 946 struct dirent d;
802 int memlen = 4096; 951 int memlen = 4096;
803 int memofs = 0; 952 int memofs = 0;
804 int res = 0; 953 int res = 0;
805 int errorno; 954 int errorno;
806 955
807 dirp = opendir (path); 956 LOCK (wrklock);
808 if (!dirp) 957 self->dirp = dirp = opendir (req->dataptr);
809 return -1;
810
811 u = malloc (sizeof (*u)); 958 self->dbuf = u = malloc (sizeof (*u));
812 names = malloc (memlen); 959 req->data2ptr = names = malloc (memlen);
960 UNLOCK (wrklock);
813 961
814 if (u && names) 962 if (dirp && u && names)
815 for (;;) 963 for (;;)
816 { 964 {
817 errno = 0; 965 errno = 0;
818 readdir_r (dirp, &u->d, &entp); 966 readdir_r (dirp, &u->d, &entp);
819 967
829 res++; 977 res++;
830 978
831 while (memofs + len > memlen) 979 while (memofs + len > memlen)
832 { 980 {
833 memlen *= 2; 981 memlen *= 2;
982 LOCK (wrklock);
834 names = realloc (names, memlen); 983 req->data2ptr = names = realloc (names, memlen);
984 UNLOCK (wrklock);
985
835 if (!names) 986 if (!names)
836 break; 987 break;
837 } 988 }
838 989
839 memcpy (names + memofs, name, len); 990 memcpy (names + memofs, name, len);
840 memofs += len; 991 memofs += len;
841 } 992 }
842 } 993 }
843 994
844 errorno = errno;
845 free (u);
846 closedir (dirp);
847
848 if (errorno) 995 if (errno)
849 {
850 free (names);
851 errno = errorno;
852 res = -1; 996 res = -1;
853 } 997
854 998 req->result = res;
855 *namesp = (void *)names;
856 return res;
857} 999}
858 1000
859/*****************************************************************************/ 1001/*****************************************************************************/
860 1002
861static void *aio_proc (void *thr_arg) 1003static void *aio_proc (void *thr_arg)
862{ 1004{
863 aio_req req; 1005 aio_req req;
864 int type; 1006 worker *self = (worker *)thr_arg;
865 1007
866 do 1008 for (;;)
867 { 1009 {
868 pthread_mutex_lock (&reqlock); 1010 LOCK (reqlock);
869 1011
870 for (;;) 1012 for (;;)
871 { 1013 {
872 req = reqq_shift (&req_queue); 1014 self->req = req = reqq_shift (&req_queue);
873 1015
874 if (req) 1016 if (req)
875 break; 1017 break;
876 1018
877 pthread_cond_wait (&reqwait, &reqlock); 1019 pthread_cond_wait (&reqwait, &reqlock);
878 } 1020 }
879 1021
880 pthread_mutex_unlock (&reqlock); 1022 --nready;
1023
1024 UNLOCK (reqlock);
881 1025
882 errno = 0; /* strictly unnecessary */ 1026 errno = 0; /* strictly unnecessary */
883 type = req->type; /* remember type for QUIT check */
884 1027
885 if (!(req->flags & FLAG_CANCELLED)) 1028 if (!(req->flags & FLAG_CANCELLED))
886 switch (type) 1029 switch (req->type)
887 { 1030 {
888 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 1031 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
889 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 1032 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
890 1033
891 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 1034 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
892 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; 1035 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length, self); break;
893 1036
894 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 1037 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
895 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 1038 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
896 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 1039 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
897 1040
900 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 1043 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
901 case REQ_RMDIR: req->result = rmdir (req->dataptr); break; 1044 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
902 case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break; 1045 case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break;
903 case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; 1046 case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break;
904 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; 1047 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
1048 case REQ_MKNOD: req->result = mknod (req->data2ptr, req->mode, (dev_t)req->offset); break;
905 1049
906 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 1050 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
907 case REQ_FSYNC: req->result = fsync (req->fd); break; 1051 case REQ_FSYNC: req->result = fsync (req->fd); break;
908 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; 1052 case REQ_READDIR: scandir_ (req, self); break;
909 1053
910 case REQ_BUSY: 1054 case REQ_BUSY:
911 { 1055 {
912 struct timeval tv; 1056 struct timeval tv;
913 1057
917 req->result = select (0, 0, 0, 0, &tv); 1061 req->result = select (0, 0, 0, 0, &tv);
918 } 1062 }
919 1063
920 case REQ_GROUP: 1064 case REQ_GROUP:
921 case REQ_NOP: 1065 case REQ_NOP:
1066 break;
1067
922 case REQ_QUIT: 1068 case REQ_QUIT:
1069 LOCK (wrklock);
1070 worker_free (self);
1071 --started;
1072 UNLOCK (wrklock);
923 break; 1073 return 0;
924 1074
925 default: 1075 default:
926 req->result = ENOSYS; 1076 req->result = ENOSYS;
927 break; 1077 break;
928 } 1078 }
929 1079
930 req->errorno = errno; 1080 req->errorno = errno;
931 1081
932 pthread_mutex_lock (&reslock); 1082 LOCK (reslock);
1083
1084 ++npending;
933 1085
934 if (!reqq_push (&res_queue, req)) 1086 if (!reqq_push (&res_queue, req))
935 /* write a dummy byte to the pipe so fh becomes ready */ 1087 /* write a dummy byte to the pipe so fh becomes ready */
936 write (respipe [1], &respipe, 1); 1088 write (respipe [1], &respipe, 1);
937 1089
938 pthread_mutex_unlock (&reslock); 1090 self->req = 0;
939 } 1091 worker_clear (self);
940 while (type != REQ_QUIT);
941 1092
942 return 0; 1093 UNLOCK (reslock);
1094 }
943} 1095}
944 1096
945/*****************************************************************************/ 1097/*****************************************************************************/
946 1098
947static void atfork_prepare (void) 1099static void atfork_prepare (void)
948{ 1100{
949 pthread_mutex_lock (&reqlock); 1101 LOCK (wrklock);
950 pthread_mutex_lock (&reslock); 1102 LOCK (reqlock);
1103 LOCK (reslock);
951#if !HAVE_PREADWRITE 1104#if !HAVE_PREADWRITE
952 pthread_mutex_lock (&preadwritelock); 1105 LOCK (preadwritelock);
953#endif 1106#endif
954#if !HAVE_READDIR_R 1107#if !HAVE_READDIR_R
955 pthread_mutex_lock (&readdirlock); 1108 LOCK (readdirlock);
956#endif 1109#endif
957} 1110}
958 1111
959static void atfork_parent (void) 1112static void atfork_parent (void)
960{ 1113{
961#if !HAVE_READDIR_R 1114#if !HAVE_READDIR_R
962 pthread_mutex_unlock (&readdirlock); 1115 UNLOCK (readdirlock);
963#endif 1116#endif
964#if !HAVE_PREADWRITE 1117#if !HAVE_PREADWRITE
965 pthread_mutex_unlock (&preadwritelock); 1118 UNLOCK (preadwritelock);
966#endif 1119#endif
967 pthread_mutex_unlock (&reslock); 1120 UNLOCK (reslock);
968 pthread_mutex_unlock (&reqlock); 1121 UNLOCK (reqlock);
1122 UNLOCK (wrklock);
969} 1123}
970 1124
971static void atfork_child (void) 1125static void atfork_child (void)
972{ 1126{
973 aio_req prv; 1127 aio_req prv;
974
975 started = 0;
976 1128
977 while (prv = reqq_shift (&req_queue)) 1129 while (prv = reqq_shift (&req_queue))
978 req_free (prv); 1130 req_free (prv);
979 1131
980 while (prv = reqq_shift (&res_queue)) 1132 while (prv = reqq_shift (&res_queue))
981 req_free (prv); 1133 req_free (prv);
982 1134
1135 while (wrk_first.next != &wrk_first)
1136 {
1137 worker *wrk = wrk_first.next;
1138
1139 if (wrk->req)
1140 req_free (wrk->req);
1141
1142 worker_clear (wrk);
1143 worker_free (wrk);
1144 }
1145
1146 started = 0;
1147 nreqs = 0;
1148
983 close (respipe [0]); 1149 close (respipe [0]);
984 close (respipe [1]); 1150 close (respipe [1]);
985 create_pipe (); 1151 create_pipe ();
986 1152
987 atfork_parent (); 1153 atfork_parent ();
1016{ 1182{
1017 HV *stash = gv_stashpv ("IO::AIO", 1); 1183 HV *stash = gv_stashpv ("IO::AIO", 1);
1018 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); 1184 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
1019 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); 1185 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
1020 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); 1186 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
1187 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
1188 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
1021 1189
1022 create_pipe (); 1190 create_pipe ();
1023 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 1191 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
1024}
1025 1192
1193 start_thread ();
1194}
1195
1026void 1196void
1027min_parallel (nthreads) 1197min_parallel (int nthreads)
1028 int nthreads
1029 PROTOTYPE: $ 1198 PROTOTYPE: $
1030 1199
1031void 1200void
1032max_parallel (nthreads) 1201max_parallel (int nthreads)
1033 int nthreads
1034 PROTOTYPE: $ 1202 PROTOTYPE: $
1035 1203
1036int 1204int
1037max_outstanding (nreqs) 1205max_outstanding (int maxreqs)
1038 int nreqs 1206 PROTOTYPE: $
1039 PROTOTYPE: $
1040 CODE: 1207 CODE:
1041 RETVAL = max_outstanding; 1208 RETVAL = max_outstanding;
1042 max_outstanding = nreqs; 1209 max_outstanding = maxreqs;
1210 OUTPUT:
1211 RETVAL
1043 1212
1044void 1213void
1045aio_open (pathname,flags,mode,callback=&PL_sv_undef) 1214aio_open (pathname,flags,mode,callback=&PL_sv_undef)
1046 SV * pathname 1215 SV * pathname
1047 int flags 1216 int flags
1262 1431
1263 REQ_SEND; 1432 REQ_SEND;
1264} 1433}
1265 1434
1266void 1435void
1436aio_mknod (pathname,mode,dev,callback=&PL_sv_undef)
1437 SV * pathname
1438 SV * callback
1439 UV mode
1440 UV dev
1441 PPCODE:
1442{
1443 dREQ;
1444
1445 req->type = REQ_MKNOD;
1446 req->data = newSVsv (pathname);
1447 req->dataptr = SvPVbyte_nolen (req->data);
1448 req->mode = (mode_t)mode;
1449 req->offset = dev;
1450
1451 REQ_SEND;
1452}
1453
1454void
1267aio_busy (delay,callback=&PL_sv_undef) 1455aio_busy (delay,callback=&PL_sv_undef)
1268 double delay 1456 double delay
1269 SV * callback 1457 SV * callback
1270 PPCODE: 1458 PPCODE:
1271{ 1459{
1302 req->type = REQ_NOP; 1490 req->type = REQ_NOP;
1303 1491
1304 REQ_SEND; 1492 REQ_SEND;
1305} 1493}
1306 1494
1307void 1495int
1308aioreq_pri (int pri = DEFAULT_PRI) 1496aioreq_pri (int pri = 0)
1309 CODE: 1497 PROTOTYPE: ;$
1498 CODE:
1499 RETVAL = next_pri - PRI_BIAS;
1500 if (items > 0)
1501 {
1310 if (pri < PRI_MIN) pri = PRI_MIN; 1502 if (pri < PRI_MIN) pri = PRI_MIN;
1311 if (pri > PRI_MAX) pri = PRI_MAX; 1503 if (pri > PRI_MAX) pri = PRI_MAX;
1312 next_pri = pri + PRI_BIAS; 1504 next_pri = pri + PRI_BIAS;
1505 }
1506 OUTPUT:
1507 RETVAL
1313 1508
1314void 1509void
1315aioreq_nice (int nice = 0) 1510aioreq_nice (int nice = 0)
1316 CODE: 1511 CODE:
1317 nice = next_pri - nice; 1512 nice = next_pri - nice;
1318 if (nice < PRI_MIN) nice = PRI_MIN; 1513 if (nice < PRI_MIN) nice = PRI_MIN;
1319 if (nice > PRI_MAX) nice = PRI_MAX; 1514 if (nice > PRI_MAX) nice = PRI_MAX;
1320 next_pri = nice + PRI_BIAS; 1515 next_pri = nice + PRI_BIAS;
1321 1516
1322void 1517void
1323flush () 1518flush ()
1324 PROTOTYPE: 1519 PROTOTYPE:
1325 CODE: 1520 CODE:
1326 while (nreqs) 1521 while (nreqs)
1327 { 1522 {
1328 poll_wait (); 1523 poll_wait ();
1329 poll_cb (); 1524 poll_cb (0);
1330 } 1525 }
1331 1526
1332void 1527void
1333poll() 1528poll()
1334 PROTOTYPE: 1529 PROTOTYPE:
1335 CODE: 1530 CODE:
1336 if (nreqs) 1531 if (nreqs)
1337 { 1532 {
1338 poll_wait (); 1533 poll_wait ();
1339 poll_cb (); 1534 poll_cb (0);
1340 } 1535 }
1341 1536
1342int 1537int
1343poll_fileno() 1538poll_fileno()
1344 PROTOTYPE: 1539 PROTOTYPE:
1349 1544
1350int 1545int
1351poll_cb(...) 1546poll_cb(...)
1352 PROTOTYPE: 1547 PROTOTYPE:
1353 CODE: 1548 CODE:
1354 RETVAL = poll_cb (); 1549 RETVAL = poll_cb (0);
1550 OUTPUT:
1551 RETVAL
1552
1553int
1554poll_some(int max = 0)
1555 PROTOTYPE: $
1556 CODE:
1557 RETVAL = poll_cb (max);
1355 OUTPUT: 1558 OUTPUT:
1356 RETVAL 1559 RETVAL
1357 1560
1358void 1561void
1359poll_wait() 1562poll_wait()
1368 CODE: 1571 CODE:
1369 RETVAL = nreqs; 1572 RETVAL = nreqs;
1370 OUTPUT: 1573 OUTPUT:
1371 RETVAL 1574 RETVAL
1372 1575
1576int
1577nready()
1578 PROTOTYPE:
1579 CODE:
1580 RETVAL = get_nready ();
1581 OUTPUT:
1582 RETVAL
1583
1584int
1585npending()
1586 PROTOTYPE:
1587 CODE:
1588 RETVAL = get_npending ();
1589 OUTPUT:
1590 RETVAL
1591
1373PROTOTYPES: DISABLE 1592PROTOTYPES: DISABLE
1374 1593
1375MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1594MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1376 1595
1377void 1596void
1419 } 1638 }
1420 } 1639 }
1421} 1640}
1422 1641
1423void 1642void
1643cancel_subs (aio_req_ornot req)
1644 CODE:
1645 req_cancel_subs (req);
1646
1647void
1424result (aio_req grp, ...) 1648result (aio_req grp, ...)
1425 CODE: 1649 CODE:
1426{ 1650{
1427 int i; 1651 int i;
1652 AV *av;
1653
1654 grp->errorno = errno;
1655
1428 AV *av = newAV (); 1656 av = newAV ();
1429 1657
1430 for (i = 1; i < items; ++i ) 1658 for (i = 1; i < items; ++i )
1431 av_push (av, newSVsv (ST (i))); 1659 av_push (av, newSVsv (ST (i)));
1432 1660
1433 SvREFCNT_dec (grp->data); 1661 SvREFCNT_dec (grp->data);
1434 grp->data = (SV *)av; 1662 grp->data = (SV *)av;
1435} 1663}
1664
1665void
1666errno (aio_req grp, int errorno = errno)
1667 CODE:
1668 grp->errorno = errorno;
1436 1669
1437void 1670void
1438limit (aio_req grp, int limit) 1671limit (aio_req grp, int limit)
1439 CODE: 1672 CODE:
1440 grp->fd2 = limit; 1673 grp->fd2 = limit;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines