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.70 by root, Tue Oct 24 15:15:56 2006 UTC vs.
Revision 1.73 by root, Tue Oct 24 21:26:30 2006 UTC

59 59
60/* buffer size for various temporary buffers */ 60/* buffer size for various temporary buffers */
61#define AIO_BUFSIZE 65536 61#define AIO_BUFSIZE 65536
62 62
63#define dBUF \ 63#define dBUF \
64 char *aio_buf; \
65 LOCK (wrklock); \
64 char *aio_buf = malloc (AIO_BUFSIZE); \ 66 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \
67 UNLOCK (wrklock); \
65 if (!aio_buf) \ 68 if (!aio_buf) \
66 return -1; 69 return -1;
67
68#define fBUF free (aio_buf)
69 70
70enum { 71enum {
71 REQ_QUIT, 72 REQ_QUIT,
72 REQ_OPEN, REQ_CLOSE, 73 REQ_OPEN, REQ_CLOSE,
73 REQ_READ, REQ_WRITE, REQ_READAHEAD, 74 REQ_READ, REQ_WRITE, REQ_READAHEAD,
136# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 137# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
137#else 138#else
138# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER 139# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
139#endif 140#endif
140 141
142#define LOCK(mutex) pthread_mutex_lock (&(mutex))
143#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
144
145/* worker threasd management */
146static pthread_mutex_t wrklock = AIO_MUTEX_INIT;
147
148typedef struct worker {
149 /* locked by wrklock */
150 struct worker *prev, *next;
151
152 pthread_t tid;
153
154 /* locked by reslock, reqlock or wrklock */
155 aio_req req; /* currently processed request */
156 void *dbuf;
157 DIR *dirp;
158} worker;
159
160static worker wrk_first = { &wrk_first, &wrk_first, 0 };
161
162static void worker_clear (worker *wrk)
163{
164 if (wrk->dirp)
165 {
166 closedir (wrk->dirp);
167 wrk->dirp = 0;
168 }
169
170 if (wrk->dbuf)
171 {
172 free (wrk->dbuf);
173 wrk->dbuf = 0;
174 }
175}
176
177static void worker_free (worker *wrk)
178{
179 wrk->next->prev = wrk->prev;
180 wrk->prev->next = wrk->next;
181
182 free (wrk);
183}
184
141static pthread_mutex_t reslock = AIO_MUTEX_INIT; 185static pthread_mutex_t reslock = AIO_MUTEX_INIT;
142static pthread_mutex_t reqlock = AIO_MUTEX_INIT; 186static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
143static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 187static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
144 188
145/* 189/*
196 abort (); 240 abort ();
197} 241}
198 242
199static void req_invoke (aio_req req); 243static void req_invoke (aio_req req);
200static void req_free (aio_req req); 244static void req_free (aio_req req);
245static void req_cancel (aio_req req);
201 246
202/* must be called at most once */ 247/* must be called at most once */
203static SV *req_sv (aio_req req, const char *klass) 248static SV *req_sv (aio_req req, const char *klass)
204{ 249{
205 if (!req->self) 250 if (!req->self)
274 fd_set rfd; 319 fd_set rfd;
275 320
276 while (nreqs) 321 while (nreqs)
277 { 322 {
278 int size; 323 int size;
279#if !(__i386 || __x86_64) /* safe without sempahore on this archs */ 324#if !(__i386 || __x86_64) /* safe without sempahore on these archs */
280 pthread_mutex_lock (&reslock); 325 LOCK (reslock);
281#endif 326#endif
282 size = res_queue.size; 327 size = res_queue.size;
283#if !(__i386 || __x86_64) /* safe without sempahore on this archs */ 328#if !(__i386 || __x86_64) /* safe without sempahore on these archs */
284 pthread_mutex_unlock (&reslock); 329 UNLOCK (reslock);
285#endif 330#endif
286 331
287 if (size) 332 if (size)
288 return; 333 return;
289 334
313 { 358 {
314 SV *rv = &PL_sv_undef; 359 SV *rv = &PL_sv_undef;
315 360
316 if (req->result >= 0) 361 if (req->result >= 0)
317 { 362 {
363 int i;
318 char *buf = req->data2ptr; 364 char *buf = req->data2ptr;
319 AV *av = newAV (); 365 AV *av = newAV ();
320 366
321 while (req->result) 367 av_extend (av, req->result - 1);
368
369 for (i = 0; i < req->result; ++i)
322 { 370 {
323 SV *sv = newSVpv (buf, 0); 371 SV *sv = newSVpv (buf, 0);
324 372
325 av_push (av, sv); 373 av_store (av, i, sv);
326 buf += SvCUR (sv) + 1; 374 buf += SvCUR (sv) + 1;
327 req->result--;
328 } 375 }
329 376
330 rv = sv_2mortal (newRV_noinc ((SV *)av)); 377 rv = sv_2mortal (newRV_noinc ((SV *)av));
331 } 378 }
332 379
416 SvREFCNT_dec (req->fh); 463 SvREFCNT_dec (req->fh);
417 SvREFCNT_dec (req->fh2); 464 SvREFCNT_dec (req->fh2);
418 SvREFCNT_dec (req->callback); 465 SvREFCNT_dec (req->callback);
419 Safefree (req->statdata); 466 Safefree (req->statdata);
420 467
421 if (req->type == REQ_READDIR && req->result >= 0) 468 if (req->type == REQ_READDIR)
422 free (req->data2ptr); 469 free (req->data2ptr);
423 470
424 Safefree (req); 471 Safefree (req);
425} 472}
426 473
474static void req_cancel_subs (aio_req grp)
475{
476 aio_req sub;
477
478 if (grp->type != REQ_GROUP)
479 return;
480
481 SvREFCNT_dec (grp->fh2);
482 grp->fh2 = 0;
483
484 for (sub = grp->grp_first; sub; sub = sub->grp_next)
485 req_cancel (sub);
486}
487
427static void req_cancel (aio_req req) 488static void req_cancel (aio_req req)
428{ 489{
429 req->flags |= FLAG_CANCELLED; 490 req->flags |= FLAG_CANCELLED;
430 491
431 if (req->type == REQ_GROUP) 492 req_cancel_subs (req);
432 {
433 aio_req sub;
434
435 for (sub = req->grp_first; sub; sub = sub->grp_next)
436 req_cancel (sub);
437 }
438} 493}
439 494
440static int poll_cb () 495static int poll_cb ()
441{ 496{
442 dSP; 497 dSP;
444 int do_croak = 0; 499 int do_croak = 0;
445 aio_req req; 500 aio_req req;
446 501
447 for (;;) 502 for (;;)
448 { 503 {
449 pthread_mutex_lock (&reslock); 504 LOCK (reslock);
450 req = reqq_shift (&res_queue); 505 req = reqq_shift (&res_queue);
451 506
452 if (req) 507 if (req)
453 { 508 {
454 if (!res_queue.size) 509 if (!res_queue.size)
458 while (read (respipe [0], buf, 32) == 32) 513 while (read (respipe [0], buf, 32) == 32)
459 ; 514 ;
460 } 515 }
461 } 516 }
462 517
463 pthread_mutex_unlock (&reslock); 518 UNLOCK (reslock);
464 519
465 if (!req) 520 if (!req)
466 break; 521 break;
467 522
468 --nreqs; 523 --nreqs;
503static void *aio_proc(void *arg); 558static void *aio_proc(void *arg);
504 559
505static void start_thread (void) 560static void start_thread (void)
506{ 561{
507 sigset_t fullsigset, oldsigset; 562 sigset_t fullsigset, oldsigset;
508 pthread_t tid;
509 pthread_attr_t attr; 563 pthread_attr_t attr;
564
565 worker *wrk = calloc (1, sizeof (worker));
566
567 if (!wrk)
568 croak ("unable to allocate worker thread data");
510 569
511 pthread_attr_init (&attr); 570 pthread_attr_init (&attr);
512 pthread_attr_setstacksize (&attr, STACKSIZE); 571 pthread_attr_setstacksize (&attr, STACKSIZE);
513 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 572 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
514 573
515 sigfillset (&fullsigset); 574 sigfillset (&fullsigset);
575
576 LOCK (wrklock);
516 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); 577 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset);
517 578
518 if (pthread_create (&tid, &attr, aio_proc, 0) == 0) 579 if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0)
580 {
581 wrk->prev = &wrk_first;
582 wrk->next = wrk_first.next;
583 wrk_first.next->prev = wrk;
584 wrk_first.next = wrk;
519 started++; 585 started++;
586 }
587 else
588 free (wrk);
520 589
521 sigprocmask (SIG_SETMASK, &oldsigset, 0); 590 sigprocmask (SIG_SETMASK, &oldsigset, 0);
591 UNLOCK (wrklock);
522} 592}
523 593
524static void req_send (aio_req req) 594static void req_send (aio_req req)
525{ 595{
526 while (started < wanted && nreqs >= started) 596 while (started < wanted && nreqs >= started)
527 start_thread (); 597 start_thread ();
528 598
529 ++nreqs; 599 ++nreqs;
530 600
531 pthread_mutex_lock (&reqlock); 601 LOCK (reqlock);
532 reqq_push (&req_queue, req); 602 reqq_push (&req_queue, req);
533 pthread_cond_signal (&reqwait); 603 pthread_cond_signal (&reqwait);
534 pthread_mutex_unlock (&reqlock); 604 UNLOCK (reqlock);
535 605
536 if (nreqs > max_outstanding) 606 if (nreqs > max_outstanding)
537 for (;;) 607 for (;;)
538 { 608 {
539 poll_cb (); 609 poll_cb ();
612static ssize_t pread (int fd, void *buf, size_t count, off_t offset) 682static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
613{ 683{
614 ssize_t res; 684 ssize_t res;
615 off_t ooffset; 685 off_t ooffset;
616 686
617 pthread_mutex_lock (&preadwritelock); 687 LOCK (preadwritelock);
618 ooffset = lseek (fd, 0, SEEK_CUR); 688 ooffset = lseek (fd, 0, SEEK_CUR);
619 lseek (fd, offset, SEEK_SET); 689 lseek (fd, offset, SEEK_SET);
620 res = read (fd, buf, count); 690 res = read (fd, buf, count);
621 lseek (fd, ooffset, SEEK_SET); 691 lseek (fd, ooffset, SEEK_SET);
622 pthread_mutex_unlock (&preadwritelock); 692 UNLOCK (preadwritelock);
623 693
624 return res; 694 return res;
625} 695}
626 696
627static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) 697static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
628{ 698{
629 ssize_t res; 699 ssize_t res;
630 off_t ooffset; 700 off_t ooffset;
631 701
632 pthread_mutex_lock (&preadwritelock); 702 LOCK (preadwritelock);
633 ooffset = lseek (fd, 0, SEEK_CUR); 703 ooffset = lseek (fd, 0, SEEK_CUR);
634 lseek (fd, offset, SEEK_SET); 704 lseek (fd, offset, SEEK_SET);
635 res = write (fd, buf, count); 705 res = write (fd, buf, count);
636 lseek (fd, offset, SEEK_SET); 706 lseek (fd, offset, SEEK_SET);
637 pthread_mutex_unlock (&preadwritelock); 707 UNLOCK (preadwritelock);
638 708
639 return res; 709 return res;
640} 710}
641#endif 711#endif
642 712
658 pread (fd, aio_buf, len, offset); 728 pread (fd, aio_buf, len, offset);
659 offset += len; 729 offset += len;
660 count -= len; 730 count -= len;
661 } 731 }
662 732
663 fBUF;
664
665 errno = 0; 733 errno = 0;
666} 734}
667#endif 735#endif
668 736
669#if !HAVE_READDIR_R 737#if !HAVE_READDIR_R
674static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) 742static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
675{ 743{
676 struct dirent *e; 744 struct dirent *e;
677 int errorno; 745 int errorno;
678 746
679 pthread_mutex_lock (&readdirlock); 747 LOCK (readdirlock);
680 748
681 e = readdir (dirp); 749 e = readdir (dirp);
682 errorno = errno; 750 errorno = errno;
683 751
684 if (e) 752 if (e)
687 strcpy (ent->d_name, e->d_name); 755 strcpy (ent->d_name, e->d_name);
688 } 756 }
689 else 757 else
690 *res = 0; 758 *res = 0;
691 759
692 pthread_mutex_unlock (&readdirlock); 760 UNLOCK (readdirlock);
693 761
694 errno = errorno; 762 errno = errorno;
695 return e ? 0 : -1; 763 return e ? 0 : -1;
696} 764}
697#endif 765#endif
698 766
699/* sendfile always needs emulation */ 767/* sendfile always needs emulation */
700static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) 768static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count, worker *self)
701{ 769{
702 ssize_t res; 770 ssize_t res;
703 771
704 if (!count) 772 if (!count)
705 return 0; 773 return 0;
782 850
783 offset += cnt; 851 offset += cnt;
784 res += cnt; 852 res += cnt;
785 count -= cnt; 853 count -= cnt;
786 } 854 }
787
788 fBUF;
789 } 855 }
790 856
791 return res; 857 return res;
792} 858}
793 859
794/* read a full directory */ 860/* read a full directory */
795static int scandir_ (const char *path, void **namesp) 861static void scandir_ (aio_req req, worker *self)
796{ 862{
797 DIR *dirp; 863 DIR *dirp;
798 union 864 union
799 { 865 {
800 struct dirent d; 866 struct dirent d;
805 int memlen = 4096; 871 int memlen = 4096;
806 int memofs = 0; 872 int memofs = 0;
807 int res = 0; 873 int res = 0;
808 int errorno; 874 int errorno;
809 875
810 dirp = opendir (path); 876 LOCK (wrklock);
811 if (!dirp) 877 self->dirp = dirp = opendir (req->dataptr);
812 return -1;
813
814 u = malloc (sizeof (*u)); 878 self->dbuf = u = malloc (sizeof (*u));
879 UNLOCK (wrklock);
880
815 names = malloc (memlen); 881 req->data2ptr = names = malloc (memlen);
816 882
817 if (u && names) 883 if (dirp && u && names)
818 for (;;) 884 for (;;)
819 { 885 {
820 errno = 0; 886 errno = 0;
821 readdir_r (dirp, &u->d, &entp); 887 readdir_r (dirp, &u->d, &entp);
822 888
832 res++; 898 res++;
833 899
834 while (memofs + len > memlen) 900 while (memofs + len > memlen)
835 { 901 {
836 memlen *= 2; 902 memlen *= 2;
903 LOCK (wrklock);
837 names = realloc (names, memlen); 904 req->data2ptr = names = realloc (names, memlen);
905 UNLOCK (wrklock);
906
838 if (!names) 907 if (!names)
839 break; 908 break;
840 } 909 }
841 910
842 memcpy (names + memofs, name, len); 911 memcpy (names + memofs, name, len);
843 memofs += len; 912 memofs += len;
844 } 913 }
845 } 914 }
846 915
847 errorno = errno;
848 free (u);
849 closedir (dirp);
850
851 if (errorno) 916 if (errno)
852 {
853 free (names);
854 errno = errorno;
855 res = -1; 917 res = -1;
856 } 918
857 919 req->result = res;
858 *namesp = (void *)names;
859 return res;
860} 920}
861 921
862/*****************************************************************************/ 922/*****************************************************************************/
863 923
864static void *aio_proc (void *thr_arg) 924static void *aio_proc (void *thr_arg)
865{ 925{
866 aio_req req; 926 aio_req req;
867 int type; 927 int type;
928 worker *self = (worker *)thr_arg;
868 929
869 do 930 do
870 { 931 {
871 pthread_mutex_lock (&reqlock); 932 LOCK (reqlock);
872 933
873 for (;;) 934 for (;;)
874 { 935 {
875 req = reqq_shift (&req_queue); 936 self->req = req = reqq_shift (&req_queue);
876 937
877 if (req) 938 if (req)
878 break; 939 break;
879 940
880 pthread_cond_wait (&reqwait, &reqlock); 941 pthread_cond_wait (&reqwait, &reqlock);
881 } 942 }
882 943
883 pthread_mutex_unlock (&reqlock); 944 UNLOCK (reqlock);
884 945
885 errno = 0; /* strictly unnecessary */ 946 errno = 0; /* strictly unnecessary */
886 type = req->type; /* remember type for QUIT check */ 947 type = req->type; /* remember type for QUIT check */
887 948
888 if (!(req->flags & FLAG_CANCELLED)) 949 if (!(req->flags & FLAG_CANCELLED))
890 { 951 {
891 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 952 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
892 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 953 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
893 954
894 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 955 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
895 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; 956 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length, self); break;
896 957
897 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 958 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
898 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 959 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
899 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 960 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
900 961
906 case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; 967 case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break;
907 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; 968 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
908 969
909 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 970 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
910 case REQ_FSYNC: req->result = fsync (req->fd); break; 971 case REQ_FSYNC: req->result = fsync (req->fd); break;
911 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; 972 case REQ_READDIR: scandir_ (req, self); break;
912 973
913 case REQ_BUSY: 974 case REQ_BUSY:
914 { 975 {
915 struct timeval tv; 976 struct timeval tv;
916 977
930 break; 991 break;
931 } 992 }
932 993
933 req->errorno = errno; 994 req->errorno = errno;
934 995
935 pthread_mutex_lock (&reslock); 996 LOCK (reslock);
936 997
937 if (!reqq_push (&res_queue, req)) 998 if (!reqq_push (&res_queue, req))
938 /* write a dummy byte to the pipe so fh becomes ready */ 999 /* write a dummy byte to the pipe so fh becomes ready */
939 write (respipe [1], &respipe, 1); 1000 write (respipe [1], &respipe, 1);
940 1001
941 pthread_mutex_unlock (&reslock); 1002 self->req = 0;
1003 worker_clear (self);
1004
1005 UNLOCK (reslock);
942 } 1006 }
943 while (type != REQ_QUIT); 1007 while (type != REQ_QUIT);
944 1008
1009 LOCK (wrklock);
1010 worker_free (self);
1011 UNLOCK (wrklock);
1012
945 return 0; 1013 return 0;
946} 1014}
947 1015
948/*****************************************************************************/ 1016/*****************************************************************************/
949 1017
950static void atfork_prepare (void) 1018static void atfork_prepare (void)
951{ 1019{
952 pthread_mutex_lock (&reqlock); 1020 LOCK (wrklock);
953 pthread_mutex_lock (&reslock); 1021 LOCK (reqlock);
1022 LOCK (reslock);
954#if !HAVE_PREADWRITE 1023#if !HAVE_PREADWRITE
955 pthread_mutex_lock (&preadwritelock); 1024 LOCK (preadwritelock);
956#endif 1025#endif
957#if !HAVE_READDIR_R 1026#if !HAVE_READDIR_R
958 pthread_mutex_lock (&readdirlock); 1027 LOCK (readdirlock);
959#endif 1028#endif
960} 1029}
961 1030
962static void atfork_parent (void) 1031static void atfork_parent (void)
963{ 1032{
964#if !HAVE_READDIR_R 1033#if !HAVE_READDIR_R
965 pthread_mutex_unlock (&readdirlock); 1034 UNLOCK (readdirlock);
966#endif 1035#endif
967#if !HAVE_PREADWRITE 1036#if !HAVE_PREADWRITE
968 pthread_mutex_unlock (&preadwritelock); 1037 UNLOCK (preadwritelock);
969#endif 1038#endif
970 pthread_mutex_unlock (&reslock); 1039 UNLOCK (reslock);
971 pthread_mutex_unlock (&reqlock); 1040 UNLOCK (reqlock);
1041 UNLOCK (wrklock);
972} 1042}
973 1043
974static void atfork_child (void) 1044static void atfork_child (void)
975{ 1045{
976 aio_req prv; 1046 aio_req prv;
977
978 started = 0;
979 1047
980 while (prv = reqq_shift (&req_queue)) 1048 while (prv = reqq_shift (&req_queue))
981 req_free (prv); 1049 req_free (prv);
982 1050
983 while (prv = reqq_shift (&res_queue)) 1051 while (prv = reqq_shift (&res_queue))
984 req_free (prv); 1052 req_free (prv);
985 1053
1054 while (wrk_first.next != &wrk_first)
1055 {
1056 worker *wrk = wrk_first.next;
1057
1058 if (wrk->req)
1059 req_free (wrk->req);
1060
1061 worker_clear (wrk);
1062 worker_free (wrk);
1063 }
1064
1065 started = 0;
1066 nreqs = 0;
1067
986 close (respipe [0]); 1068 close (respipe [0]);
987 close (respipe [1]); 1069 close (respipe [1]);
988 create_pipe (); 1070 create_pipe ();
989 1071
990 atfork_parent (); 1072 atfork_parent ();
1422 } 1504 }
1423 } 1505 }
1424} 1506}
1425 1507
1426void 1508void
1509cancel_subs (aio_req_ornot req)
1510 CODE:
1511 req_cancel_subs (req);
1512
1513void
1427result (aio_req grp, ...) 1514result (aio_req grp, ...)
1428 CODE: 1515 CODE:
1429{ 1516{
1430 int i; 1517 int i;
1431 AV *av = newAV (); 1518 AV *av = newAV ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines