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.68 by root, Tue Oct 24 03:40:25 2006 UTC vs.
Revision 1.72 by root, Tue Oct 24 17:22:17 2006 UTC

1/* solaris */
2#define _POSIX_PTHREAD_SEMANTICS 1
3
1#if __linux 4#if __linux
2# define _GNU_SOURCE 5# define _GNU_SOURCE
3#endif 6#endif
4 7
5#define _REENTRANT 1 8#define _REENTRANT 1
56 59
57/* buffer size for various temporary buffers */ 60/* buffer size for various temporary buffers */
58#define AIO_BUFSIZE 65536 61#define AIO_BUFSIZE 65536
59 62
60#define dBUF \ 63#define dBUF \
64 char *aio_buf; \
65 LOCK (wrklock); \
61 char *aio_buf = malloc (AIO_BUFSIZE); \ 66 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \
67 UNLOCK (wrklock); \
62 if (!aio_buf) \ 68 if (!aio_buf) \
63 return -1; 69 return -1;
64
65#define fBUF free (aio_buf)
66 70
67enum { 71enum {
68 REQ_QUIT, 72 REQ_QUIT,
69 REQ_OPEN, REQ_CLOSE, 73 REQ_OPEN, REQ_CLOSE,
70 REQ_READ, REQ_WRITE, REQ_READAHEAD, 74 REQ_READ, REQ_WRITE, REQ_READAHEAD,
73 REQ_FSYNC, REQ_FDATASYNC, 77 REQ_FSYNC, REQ_FDATASYNC,
74 REQ_UNLINK, REQ_RMDIR, REQ_RENAME, 78 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
75 REQ_READDIR, 79 REQ_READDIR,
76 REQ_LINK, REQ_SYMLINK, 80 REQ_LINK, REQ_SYMLINK,
77 REQ_GROUP, REQ_NOP, 81 REQ_GROUP, REQ_NOP,
78 REQ_SLEEP, 82 REQ_BUSY,
79}; 83};
80 84
81#define AIO_REQ_KLASS "IO::AIO::REQ" 85#define AIO_REQ_KLASS "IO::AIO::REQ"
82#define AIO_GRP_KLASS "IO::AIO::GRP" 86#define AIO_GRP_KLASS "IO::AIO::GRP"
83 87
132#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 136#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
133# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 137# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
134#else 138#else
135# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER 139# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
136#endif 140#endif
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}
137 184
138static pthread_mutex_t reslock = AIO_MUTEX_INIT; 185static pthread_mutex_t reslock = AIO_MUTEX_INIT;
139static pthread_mutex_t reqlock = AIO_MUTEX_INIT; 186static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
140static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 187static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
141 188
193 abort (); 240 abort ();
194} 241}
195 242
196static void req_invoke (aio_req req); 243static void req_invoke (aio_req req);
197static void req_free (aio_req req); 244static void req_free (aio_req req);
245static void req_cancel (aio_req req);
198 246
199/* must be called at most once */ 247/* must be called at most once */
200static SV *req_sv (aio_req req, const char *klass) 248static SV *req_sv (aio_req req, const char *klass)
201{ 249{
202 if (!req->self) 250 if (!req->self)
271 fd_set rfd; 319 fd_set rfd;
272 320
273 while (nreqs) 321 while (nreqs)
274 { 322 {
275 int size; 323 int size;
276#if !(__i386 || __x86_64) /* safe without sempahore on this archs */ 324#if !(__i386 || __x86_64) /* safe without sempahore on these archs */
277 pthread_mutex_lock (&reslock); 325 LOCK (reslock);
278#endif 326#endif
279 size = res_queue.size; 327 size = res_queue.size;
280#if !(__i386 || __x86_64) /* safe without sempahore on this archs */ 328#if !(__i386 || __x86_64) /* safe without sempahore on these archs */
281 pthread_mutex_unlock (&reslock); 329 UNLOCK (reslock);
282#endif 330#endif
283 331
284 if (size) 332 if (size)
285 return; 333 return;
286 334
310 { 358 {
311 SV *rv = &PL_sv_undef; 359 SV *rv = &PL_sv_undef;
312 360
313 if (req->result >= 0) 361 if (req->result >= 0)
314 { 362 {
363 int i;
315 char *buf = req->data2ptr; 364 char *buf = req->data2ptr;
316 AV *av = newAV (); 365 AV *av = newAV ();
317 366
318 while (req->result) 367 av_extend (av, req->result - 1);
368
369 for (i = 0; i < req->result; ++i)
319 { 370 {
320 SV *sv = newSVpv (buf, 0); 371 SV *sv = newSVpv (buf, 0);
321 372
322 av_push (av, sv); 373 av_store (av, i, sv);
323 buf += SvCUR (sv) + 1; 374 buf += SvCUR (sv) + 1;
324 req->result--;
325 } 375 }
326 376
327 rv = sv_2mortal (newRV_noinc ((SV *)av)); 377 rv = sv_2mortal (newRV_noinc ((SV *)av));
328 } 378 }
329 379
361 PUSHs (*av_fetch (av, i, 0)); 411 PUSHs (*av_fetch (av, i, 0));
362 } 412 }
363 break; 413 break;
364 414
365 case REQ_NOP: 415 case REQ_NOP:
366 case REQ_SLEEP: 416 case REQ_BUSY:
367 break; 417 break;
368 418
369 default: 419 default:
370 PUSHs (sv_2mortal (newSViv (req->result))); 420 PUSHs (sv_2mortal (newSViv (req->result)));
371 break; 421 break;
413 SvREFCNT_dec (req->fh); 463 SvREFCNT_dec (req->fh);
414 SvREFCNT_dec (req->fh2); 464 SvREFCNT_dec (req->fh2);
415 SvREFCNT_dec (req->callback); 465 SvREFCNT_dec (req->callback);
416 Safefree (req->statdata); 466 Safefree (req->statdata);
417 467
418 if (req->type == REQ_READDIR && req->result >= 0) 468 if (req->type == REQ_READDIR)
419 free (req->data2ptr); 469 free (req->data2ptr);
420 470
421 Safefree (req); 471 Safefree (req);
422} 472}
423 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
424static void req_cancel (aio_req req) 488static void req_cancel (aio_req req)
425{ 489{
426 req->flags |= FLAG_CANCELLED; 490 req->flags |= FLAG_CANCELLED;
427 491
428 if (req->type == REQ_GROUP) 492 req_cancel_subs (req);
429 {
430 aio_req sub;
431
432 for (sub = req->grp_first; sub; sub = sub->grp_next)
433 req_cancel (sub);
434 }
435} 493}
436 494
437static int poll_cb () 495static int poll_cb ()
438{ 496{
439 dSP; 497 dSP;
441 int do_croak = 0; 499 int do_croak = 0;
442 aio_req req; 500 aio_req req;
443 501
444 for (;;) 502 for (;;)
445 { 503 {
446 pthread_mutex_lock (&reslock); 504 LOCK (reslock);
447 req = reqq_shift (&res_queue); 505 req = reqq_shift (&res_queue);
448 506
449 if (req) 507 if (req)
450 { 508 {
451 if (!res_queue.size) 509 if (!res_queue.size)
455 while (read (respipe [0], buf, 32) == 32) 513 while (read (respipe [0], buf, 32) == 32)
456 ; 514 ;
457 } 515 }
458 } 516 }
459 517
460 pthread_mutex_unlock (&reslock); 518 UNLOCK (reslock);
461 519
462 if (!req) 520 if (!req)
463 break; 521 break;
464 522
465 --nreqs; 523 --nreqs;
499 557
500static void *aio_proc(void *arg); 558static void *aio_proc(void *arg);
501 559
502static void start_thread (void) 560static void start_thread (void)
503{ 561{
562 worker *wrk = calloc (1, sizeof (worker));
563
564 if (!wrk)
565 croak ("unable to allocate worker thread data");
566
504 sigset_t fullsigset, oldsigset; 567 sigset_t fullsigset, oldsigset;
505 pthread_t tid;
506 pthread_attr_t attr; 568 pthread_attr_t attr;
507 569
508 pthread_attr_init (&attr); 570 pthread_attr_init (&attr);
509 pthread_attr_setstacksize (&attr, STACKSIZE); 571 pthread_attr_setstacksize (&attr, STACKSIZE);
510 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 572 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
511 573
512 sigfillset (&fullsigset); 574 sigfillset (&fullsigset);
575
576 LOCK (wrklock);
513 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); 577 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset);
514 578
515 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;
516 started++; 585 started++;
586 }
587 else
588 free (wrk);
517 589
518 sigprocmask (SIG_SETMASK, &oldsigset, 0); 590 sigprocmask (SIG_SETMASK, &oldsigset, 0);
591 UNLOCK (wrklock);
519} 592}
520 593
521static void req_send (aio_req req) 594static void req_send (aio_req req)
522{ 595{
523 while (started < wanted && nreqs >= started) 596 while (started < wanted && nreqs >= started)
524 start_thread (); 597 start_thread ();
525 598
526 ++nreqs; 599 ++nreqs;
527 600
528 pthread_mutex_lock (&reqlock); 601 LOCK (reqlock);
529 reqq_push (&req_queue, req); 602 reqq_push (&req_queue, req);
530 pthread_cond_signal (&reqwait); 603 pthread_cond_signal (&reqwait);
531 pthread_mutex_unlock (&reqlock); 604 UNLOCK (reqlock);
532 605
533 if (nreqs > max_outstanding) 606 if (nreqs > max_outstanding)
534 for (;;) 607 for (;;)
535 { 608 {
536 poll_cb (); 609 poll_cb ();
609static 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)
610{ 683{
611 ssize_t res; 684 ssize_t res;
612 off_t ooffset; 685 off_t ooffset;
613 686
614 pthread_mutex_lock (&preadwritelock); 687 LOCK (preadwritelock);
615 ooffset = lseek (fd, 0, SEEK_CUR); 688 ooffset = lseek (fd, 0, SEEK_CUR);
616 lseek (fd, offset, SEEK_SET); 689 lseek (fd, offset, SEEK_SET);
617 res = read (fd, buf, count); 690 res = read (fd, buf, count);
618 lseek (fd, ooffset, SEEK_SET); 691 lseek (fd, ooffset, SEEK_SET);
619 pthread_mutex_unlock (&preadwritelock); 692 UNLOCK (preadwritelock);
620 693
621 return res; 694 return res;
622} 695}
623 696
624static 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)
625{ 698{
626 ssize_t res; 699 ssize_t res;
627 off_t ooffset; 700 off_t ooffset;
628 701
629 pthread_mutex_lock (&preadwritelock); 702 LOCK (preadwritelock);
630 ooffset = lseek (fd, 0, SEEK_CUR); 703 ooffset = lseek (fd, 0, SEEK_CUR);
631 lseek (fd, offset, SEEK_SET); 704 lseek (fd, offset, SEEK_SET);
632 res = write (fd, buf, count); 705 res = write (fd, buf, count);
633 lseek (fd, offset, SEEK_SET); 706 lseek (fd, offset, SEEK_SET);
634 pthread_mutex_unlock (&preadwritelock); 707 UNLOCK (preadwritelock);
635 708
636 return res; 709 return res;
637} 710}
638#endif 711#endif
639 712
655 pread (fd, aio_buf, len, offset); 728 pread (fd, aio_buf, len, offset);
656 offset += len; 729 offset += len;
657 count -= len; 730 count -= len;
658 } 731 }
659 732
660 fBUF;
661
662 errno = 0; 733 errno = 0;
663} 734}
664#endif 735#endif
665 736
666#if !HAVE_READDIR_R 737#if !HAVE_READDIR_R
671static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) 742static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
672{ 743{
673 struct dirent *e; 744 struct dirent *e;
674 int errorno; 745 int errorno;
675 746
676 pthread_mutex_lock (&readdirlock); 747 LOCK (readdirlock);
677 748
678 e = readdir (dirp); 749 e = readdir (dirp);
679 errorno = errno; 750 errorno = errno;
680 751
681 if (e) 752 if (e)
684 strcpy (ent->d_name, e->d_name); 755 strcpy (ent->d_name, e->d_name);
685 } 756 }
686 else 757 else
687 *res = 0; 758 *res = 0;
688 759
689 pthread_mutex_unlock (&readdirlock); 760 UNLOCK (readdirlock);
690 761
691 errno = errorno; 762 errno = errorno;
692 return e ? 0 : -1; 763 return e ? 0 : -1;
693} 764}
694#endif 765#endif
695 766
696/* sendfile always needs emulation */ 767/* sendfile always needs emulation */
697static 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)
698{ 769{
699 ssize_t res; 770 ssize_t res;
700 771
701 if (!count) 772 if (!count)
702 return 0; 773 return 0;
779 850
780 offset += cnt; 851 offset += cnt;
781 res += cnt; 852 res += cnt;
782 count -= cnt; 853 count -= cnt;
783 } 854 }
784
785 fBUF;
786 } 855 }
787 856
788 return res; 857 return res;
789} 858}
790 859
791/* read a full directory */ 860/* read a full directory */
792static int scandir_ (const char *path, void **namesp) 861static void scandir_ (aio_req req, worker *self)
793{ 862{
794 DIR *dirp; 863 DIR *dirp;
795 union 864 union
796 { 865 {
797 struct dirent d; 866 struct dirent d;
802 int memlen = 4096; 871 int memlen = 4096;
803 int memofs = 0; 872 int memofs = 0;
804 int res = 0; 873 int res = 0;
805 int errorno; 874 int errorno;
806 875
807 dirp = opendir (path); 876 LOCK (wrklock);
808 if (!dirp) 877 self->dirp = dirp = opendir (req->dataptr);
809 return -1;
810
811 u = malloc (sizeof (*u)); 878 self->dbuf = u = malloc (sizeof (*u));
879 UNLOCK (wrklock);
880
812 names = malloc (memlen); 881 req->data2ptr = names = malloc (memlen);
813 882
814 if (u && names) 883 if (dirp && u && names)
815 for (;;) 884 for (;;)
816 { 885 {
817 errno = 0; 886 errno = 0;
818 readdir_r (dirp, &u->d, &entp); 887 readdir_r (dirp, &u->d, &entp);
819 888
829 res++; 898 res++;
830 899
831 while (memofs + len > memlen) 900 while (memofs + len > memlen)
832 { 901 {
833 memlen *= 2; 902 memlen *= 2;
903 LOCK (wrklock);
834 names = realloc (names, memlen); 904 req->data2ptr = names = realloc (names, memlen);
905 UNLOCK (wrklock);
906
835 if (!names) 907 if (!names)
836 break; 908 break;
837 } 909 }
838 910
839 memcpy (names + memofs, name, len); 911 memcpy (names + memofs, name, len);
840 memofs += len; 912 memofs += len;
841 } 913 }
842 } 914 }
843 915
844 errorno = errno;
845 free (u);
846 closedir (dirp);
847
848 if (errorno) 916 if (errno)
849 {
850 free (names);
851 errno = errorno;
852 res = -1; 917 res = -1;
853 } 918
854 919 req->result = res;
855 *namesp = (void *)names;
856 return res;
857} 920}
858 921
859/*****************************************************************************/ 922/*****************************************************************************/
860 923
861static void *aio_proc (void *thr_arg) 924static void *aio_proc (void *thr_arg)
862{ 925{
863 aio_req req; 926 aio_req req;
864 int type; 927 int type;
928 worker *self = (worker *)thr_arg;
865 929
866 do 930 do
867 { 931 {
868 pthread_mutex_lock (&reqlock); 932 LOCK (reqlock);
869 933
870 for (;;) 934 for (;;)
871 { 935 {
872 req = reqq_shift (&req_queue); 936 self->req = req = reqq_shift (&req_queue);
873 937
874 if (req) 938 if (req)
875 break; 939 break;
876 940
877 pthread_cond_wait (&reqwait, &reqlock); 941 pthread_cond_wait (&reqwait, &reqlock);
878 } 942 }
879 943
880 pthread_mutex_unlock (&reqlock); 944 UNLOCK (reqlock);
881 945
882 errno = 0; /* strictly unnecessary */ 946 errno = 0; /* strictly unnecessary */
883 type = req->type; /* remember type for QUIT check */ 947 type = req->type; /* remember type for QUIT check */
884 948
885 if (!(req->flags & FLAG_CANCELLED)) 949 if (!(req->flags & FLAG_CANCELLED))
887 { 951 {
888 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;
889 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;
890 954
891 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;
892 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;
893 957
894 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 958 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
895 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 959 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
896 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 960 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
897 961
903 case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; 967 case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break;
904 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; 968 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
905 969
906 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 970 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
907 case REQ_FSYNC: req->result = fsync (req->fd); break; 971 case REQ_FSYNC: req->result = fsync (req->fd); break;
908 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; 972 case REQ_READDIR: scandir_ (req, self); break;
909 973
910 case REQ_SLEEP: 974 case REQ_BUSY:
911 { 975 {
912 struct timeval tv; 976 struct timeval tv;
913 977
914 tv.tv_sec = req->fd; 978 tv.tv_sec = req->fd;
915 tv.tv_usec = req->fd2; 979 tv.tv_usec = req->fd2;
927 break; 991 break;
928 } 992 }
929 993
930 req->errorno = errno; 994 req->errorno = errno;
931 995
932 pthread_mutex_lock (&reslock); 996 LOCK (reslock);
933 997
934 if (!reqq_push (&res_queue, req)) 998 if (!reqq_push (&res_queue, req))
935 /* write a dummy byte to the pipe so fh becomes ready */ 999 /* write a dummy byte to the pipe so fh becomes ready */
936 write (respipe [1], &respipe, 1); 1000 write (respipe [1], &respipe, 1);
937 1001
938 pthread_mutex_unlock (&reslock); 1002 self->req = 0;
1003 worker_clear (self);
1004
1005 UNLOCK (reslock);
939 } 1006 }
940 while (type != REQ_QUIT); 1007 while (type != REQ_QUIT);
941 1008
1009 LOCK (wrklock);
1010 worker_free (self);
1011 UNLOCK (wrklock);
1012
942 return 0; 1013 return 0;
943} 1014}
944 1015
945/*****************************************************************************/ 1016/*****************************************************************************/
946 1017
947static void atfork_prepare (void) 1018static void atfork_prepare (void)
948{ 1019{
949 pthread_mutex_lock (&reqlock); 1020 LOCK (wrklock);
950 pthread_mutex_lock (&reslock); 1021 LOCK (reqlock);
1022 LOCK (reslock);
951#if !HAVE_PREADWRITE 1023#if !HAVE_PREADWRITE
952 pthread_mutex_lock (&preadwritelock); 1024 LOCK (preadwritelock);
953#endif 1025#endif
954#if !HAVE_READDIR_R 1026#if !HAVE_READDIR_R
955 pthread_mutex_lock (&readdirlock); 1027 LOCK (readdirlock);
956#endif 1028#endif
957} 1029}
958 1030
959static void atfork_parent (void) 1031static void atfork_parent (void)
960{ 1032{
961#if !HAVE_READDIR_R 1033#if !HAVE_READDIR_R
962 pthread_mutex_unlock (&readdirlock); 1034 UNLOCK (readdirlock);
963#endif 1035#endif
964#if !HAVE_PREADWRITE 1036#if !HAVE_PREADWRITE
965 pthread_mutex_unlock (&preadwritelock); 1037 UNLOCK (preadwritelock);
966#endif 1038#endif
967 pthread_mutex_unlock (&reslock); 1039 UNLOCK (reslock);
968 pthread_mutex_unlock (&reqlock); 1040 UNLOCK (reqlock);
1041 UNLOCK (wrklock);
969} 1042}
970 1043
971static void atfork_child (void) 1044static void atfork_child (void)
972{ 1045{
973 aio_req prv; 1046 aio_req prv;
974
975 started = 0;
976 1047
977 while (prv = reqq_shift (&req_queue)) 1048 while (prv = reqq_shift (&req_queue))
978 req_free (prv); 1049 req_free (prv);
979 1050
980 while (prv = reqq_shift (&res_queue)) 1051 while (prv = reqq_shift (&res_queue))
981 req_free (prv); 1052 req_free (prv);
982 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
983 close (respipe [0]); 1068 close (respipe [0]);
984 close (respipe [1]); 1069 close (respipe [1]);
985 create_pipe (); 1070 create_pipe ();
986 1071
987 atfork_parent (); 1072 atfork_parent ();
1262 1347
1263 REQ_SEND; 1348 REQ_SEND;
1264} 1349}
1265 1350
1266void 1351void
1267aio_sleep (delay,callback=&PL_sv_undef) 1352aio_busy (delay,callback=&PL_sv_undef)
1268 double delay 1353 double delay
1269 SV * callback 1354 SV * callback
1270 PPCODE: 1355 PPCODE:
1271{ 1356{
1272 dREQ; 1357 dREQ;
1273 1358
1274 req->type = REQ_SLEEP; 1359 req->type = REQ_BUSY;
1275 req->fd = delay < 0. ? 0 : delay; 1360 req->fd = delay < 0. ? 0 : delay;
1276 req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); 1361 req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd);
1277 1362
1278 REQ_SEND; 1363 REQ_SEND;
1279} 1364}
1419 } 1504 }
1420 } 1505 }
1421} 1506}
1422 1507
1423void 1508void
1509cancel_subs (aio_req_ornot req)
1510 CODE:
1511 req_cancel_subs (req);
1512
1513void
1424result (aio_req grp, ...) 1514result (aio_req grp, ...)
1425 CODE: 1515 CODE:
1426{ 1516{
1427 int i; 1517 int i;
1428 AV *av = newAV (); 1518 AV *av = newAV ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines