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.58 by root, Mon Oct 23 18:48:08 2006 UTC vs.
Revision 1.70 by root, Tue Oct 24 15:15:56 2006 UTC

1/* solaris */
2#define _POSIX_PTHREAD_SEMANTICS 1
3
4#if __linux
5# define _GNU_SOURCE
6#endif
7
1#define _REENTRANT 1 8#define _REENTRANT 1
9
2#include <errno.h> 10#include <errno.h>
3 11
4#include "EXTERN.h" 12#include "EXTERN.h"
5#include "perl.h" 13#include "perl.h"
6#include "XSUB.h" 14#include "XSUB.h"
41# define NAME_MAX 4096 49# define NAME_MAX 4096
42#endif 50#endif
43 51
44#if __ia64 52#if __ia64
45# define STACKSIZE 65536 53# define STACKSIZE 65536
54#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
55# define STACKSIZE PTHREAD_STACK_MIN
46#else 56#else
47# define STACKSIZE 8192 57# define STACKSIZE 16384
48#endif 58#endif
59
60/* buffer size for various temporary buffers */
61#define AIO_BUFSIZE 65536
62
63#define dBUF \
64 char *aio_buf = malloc (AIO_BUFSIZE); \
65 if (!aio_buf) \
66 return -1;
67
68#define fBUF free (aio_buf)
49 69
50enum { 70enum {
51 REQ_QUIT, 71 REQ_QUIT,
52 REQ_OPEN, REQ_CLOSE, 72 REQ_OPEN, REQ_CLOSE,
53 REQ_READ, REQ_WRITE, REQ_READAHEAD, 73 REQ_READ, REQ_WRITE, REQ_READAHEAD,
56 REQ_FSYNC, REQ_FDATASYNC, 76 REQ_FSYNC, REQ_FDATASYNC,
57 REQ_UNLINK, REQ_RMDIR, REQ_RENAME, 77 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
58 REQ_READDIR, 78 REQ_READDIR,
59 REQ_LINK, REQ_SYMLINK, 79 REQ_LINK, REQ_SYMLINK,
60 REQ_GROUP, REQ_NOP, 80 REQ_GROUP, REQ_NOP,
61 REQ_SLEEP, 81 REQ_BUSY,
62}; 82};
63 83
64#define AIO_REQ_KLASS "IO::AIO::REQ" 84#define AIO_REQ_KLASS "IO::AIO::REQ"
65#define AIO_GRP_KLASS "IO::AIO::GRP" 85#define AIO_GRP_KLASS "IO::AIO::GRP"
66 86
67typedef struct aio_cb 87typedef struct aio_cb
68{ 88{
69 struct aio_cb *volatile next; 89 struct aio_cb *volatile next;
70
71 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
72
73 SV *self; /* the perl counterpart of this request, if any */
74 90
75 SV *data, *callback; 91 SV *data, *callback;
76 SV *fh, *fh2; 92 SV *fh, *fh2;
77 void *dataptr, *data2ptr; 93 void *dataptr, *data2ptr;
78 Stat_t *statdata; 94 Stat_t *statdata;
79 off_t offset; 95 off_t offset;
80 size_t length; 96 size_t length;
81 ssize_t result; 97 ssize_t result;
82 98
99 STRLEN dataoffset;
83 int type; 100 int type;
84 int fd, fd2; 101 int fd, fd2;
85 int errorno; 102 int errorno;
86 STRLEN dataoffset;
87 mode_t mode; /* open */ 103 mode_t mode; /* open */
104
105 unsigned char flags;
88 unsigned char pri; 106 unsigned char pri;
89 unsigned char flags; 107
108 SV *self; /* the perl counterpart of this request, if any */
109 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
90} aio_cb; 110} aio_cb;
91 111
92enum { 112enum {
93 FLAG_CANCELLED = 0x01, 113 FLAG_CANCELLED = 0x01,
94}; 114};
95 115
96typedef aio_cb *aio_req; 116typedef aio_cb *aio_req;
97typedef aio_cb *aio_req_ornot; 117typedef aio_cb *aio_req_ornot;
118
119enum {
120 PRI_MIN = -4,
121 PRI_MAX = 4,
122
123 DEFAULT_PRI = 0,
124 PRI_BIAS = -PRI_MIN,
125 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
126};
127
128static int next_pri = DEFAULT_PRI + PRI_BIAS;
98 129
99static int started, wanted; 130static int started, wanted;
100static volatile int nreqs; 131static volatile int nreqs;
101static int max_outstanding = 1<<30; 132static int max_outstanding = 1<<30;
102static int respipe [2]; 133static int respipe [2];
103 134
135#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
136# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
137#else
138# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
139#endif
140
104static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 141static pthread_mutex_t reslock = AIO_MUTEX_INIT;
105static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 142static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
106static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 143static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
107 144
108static volatile aio_req reqs, reqe; /* queue start, queue end */ 145/*
109static volatile aio_req ress, rese; /* queue start, queue end */ 146 * a somewhat faster data structure might be nice, but
147 * with 8 priorities this actually needs <20 insns
148 * per shift, the most expensive operation.
149 */
150typedef struct {
151 aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */
152 int size;
153} reqq;
154
155static reqq req_queue;
156static reqq res_queue;
157
158int reqq_push (reqq *q, aio_req req)
159{
160 int pri = req->pri;
161 req->next = 0;
162
163 if (q->qe[pri])
164 {
165 q->qe[pri]->next = req;
166 q->qe[pri] = req;
167 }
168 else
169 q->qe[pri] = q->qs[pri] = req;
170
171 return q->size++;
172}
173
174aio_req reqq_shift (reqq *q)
175{
176 int pri;
177
178 if (!q->size)
179 return 0;
180
181 --q->size;
182
183 for (pri = NUM_PRI; pri--; )
184 {
185 aio_req req = q->qs[pri];
186
187 if (req)
188 {
189 if (!(q->qs[pri] = req->next))
190 q->qe[pri] = 0;
191
192 return req;
193 }
194 }
195
196 abort ();
197}
110 198
111static void req_invoke (aio_req req); 199static void req_invoke (aio_req req);
112static void req_free (aio_req req); 200static void req_free (aio_req req);
113 201
114/* must be called at most once */ 202/* must be called at most once */
148 ENTER; 236 ENTER;
149 SAVETMPS; 237 SAVETMPS;
150 PUSHMARK (SP); 238 PUSHMARK (SP);
151 XPUSHs (req_sv (grp, AIO_GRP_KLASS)); 239 XPUSHs (req_sv (grp, AIO_GRP_KLASS));
152 PUTBACK; 240 PUTBACK;
153 call_sv (grp->fh2, G_VOID | G_EVAL); 241 call_sv (grp->fh2, G_VOID | G_EVAL | G_KEEPERR);
154 SPAGAIN; 242 SPAGAIN;
155 FREETMPS; 243 FREETMPS;
156 LEAVE; 244 LEAVE;
157 } 245 }
158 246
181 } 269 }
182} 270}
183 271
184static void poll_wait () 272static void poll_wait ()
185{ 273{
186 if (nreqs && !ress)
187 {
188 fd_set rfd; 274 fd_set rfd;
275
276 while (nreqs)
277 {
278 int size;
279#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
280 pthread_mutex_lock (&reslock);
281#endif
282 size = res_queue.size;
283#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
284 pthread_mutex_unlock (&reslock);
285#endif
286
287 if (size)
288 return;
289
189 FD_ZERO(&rfd); 290 FD_ZERO(&rfd);
190 FD_SET(respipe [0], &rfd); 291 FD_SET(respipe [0], &rfd);
191 292
192 select (respipe [0] + 1, &rfd, 0, 0, 0); 293 select (respipe [0] + 1, &rfd, 0, 0, 0);
193 } 294 }
194} 295}
195 296
196static void req_invoke (aio_req req) 297static void req_invoke (aio_req req)
197{ 298{
198 dSP; 299 dSP;
199 int errorno = errno;
200 300
201 if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) 301 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
202 return; 302 {
203
204 errno = req->errorno; 303 errno = req->errorno;
205 304
206 ENTER; 305 ENTER;
207 SAVETMPS; 306 SAVETMPS;
208 PUSHMARK (SP); 307 PUSHMARK (SP);
209 EXTEND (SP, 1); 308 EXTEND (SP, 1);
210 309
211 switch (req->type) 310 switch (req->type)
212 {
213 case REQ_READDIR:
214 { 311 {
215 SV *rv = &PL_sv_undef; 312 case REQ_READDIR:
216
217 if (req->result >= 0)
218 { 313 {
219 char *buf = req->data2ptr; 314 SV *rv = &PL_sv_undef;
220 AV *av = newAV ();
221 315
222 while (req->result) 316 if (req->result >= 0)
223 { 317 {
318 char *buf = req->data2ptr;
319 AV *av = newAV ();
320
321 while (req->result)
322 {
224 SV *sv = newSVpv (buf, 0); 323 SV *sv = newSVpv (buf, 0);
225 324
226 av_push (av, sv); 325 av_push (av, sv);
227 buf += SvCUR (sv) + 1; 326 buf += SvCUR (sv) + 1;
228 req->result--; 327 req->result--;
328 }
329
330 rv = sv_2mortal (newRV_noinc ((SV *)av));
229 } 331 }
230 332
231 rv = sv_2mortal (newRV_noinc ((SV *)av)); 333 PUSHs (rv);
232 } 334 }
335 break;
233 336
234 PUSHs (rv); 337 case REQ_OPEN:
338 {
339 /* convert fd to fh */
340 SV *fh;
341
342 PUSHs (sv_2mortal (newSViv (req->result)));
343 PUTBACK;
344 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
345 SPAGAIN;
346
347 fh = SvREFCNT_inc (POPs);
348
349 PUSHMARK (SP);
350 XPUSHs (sv_2mortal (fh));
351 }
352 break;
353
354 case REQ_GROUP:
355 req->fd = 2; /* mark group as finished */
356
357 if (req->data)
358 {
359 int i;
360 AV *av = (AV *)req->data;
361
362 EXTEND (SP, AvFILL (av) + 1);
363 for (i = 0; i <= AvFILL (av); ++i)
364 PUSHs (*av_fetch (av, i, 0));
365 }
366 break;
367
368 case REQ_NOP:
369 case REQ_BUSY:
370 break;
371
372 default:
373 PUSHs (sv_2mortal (newSViv (req->result)));
374 break;
235 } 375 }
236 break;
237 376
238 case REQ_OPEN:
239 {
240 /* convert fd to fh */
241 SV *fh;
242 377
243 PUSHs (sv_2mortal (newSViv (req->result)));
244 PUTBACK; 378 PUTBACK;
245 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
246 SPAGAIN;
247
248 fh = SvREFCNT_inc (POPs);
249
250 PUSHMARK (SP);
251 XPUSHs (sv_2mortal (fh));
252 }
253 break;
254
255 case REQ_GROUP:
256 req->fd = 2; /* mark group as finished */
257
258 if (req->data)
259 {
260 int i;
261 AV *av = (AV *)req->data;
262
263 EXTEND (SP, AvFILL (av) + 1);
264 for (i = 0; i <= AvFILL (av); ++i)
265 PUSHs (*av_fetch (av, i, 0));
266 }
267 break;
268
269 case REQ_NOP:
270 case REQ_SLEEP:
271 break;
272
273 default:
274 PUSHs (sv_2mortal (newSViv (req->result)));
275 break;
276 }
277
278
279 PUTBACK;
280 call_sv (req->callback, G_VOID | G_EVAL); 379 call_sv (req->callback, G_VOID | G_EVAL);
281 SPAGAIN; 380 SPAGAIN;
282 381
283 FREETMPS; 382 FREETMPS;
284 LEAVE; 383 LEAVE;
285
286 errno = errorno;
287
288 if (SvTRUE (ERRSV))
289 { 384 }
290 req_free (req);
291 croak (0);
292 }
293}
294 385
295static void req_free (aio_req req)
296{
297 if (req->grp) 386 if (req->grp)
298 { 387 {
299 aio_req grp = req->grp; 388 aio_req grp = req->grp;
300 389
301 /* unlink request */ 390 /* unlink request */
306 grp->grp_first = req->grp_next; 395 grp->grp_first = req->grp_next;
307 396
308 aio_grp_dec (grp); 397 aio_grp_dec (grp);
309 } 398 }
310 399
400 if (SvTRUE (ERRSV))
401 {
402 req_free (req);
403 croak (0);
404 }
405}
406
407static void req_free (aio_req req)
408{
311 if (req->self) 409 if (req->self)
312 { 410 {
313 sv_unmagic (req->self, PERL_MAGIC_ext); 411 sv_unmagic (req->self, PERL_MAGIC_ext);
314 SvREFCNT_dec (req->self); 412 SvREFCNT_dec (req->self);
315 } 413 }
347 aio_req req; 445 aio_req req;
348 446
349 for (;;) 447 for (;;)
350 { 448 {
351 pthread_mutex_lock (&reslock); 449 pthread_mutex_lock (&reslock);
352 req = ress; 450 req = reqq_shift (&res_queue);
353 451
354 if (req) 452 if (req)
355 { 453 {
356 ress = req->next;
357
358 if (!ress) 454 if (!res_queue.size)
359 { 455 {
360 /* read any signals sent by the worker threads */ 456 /* read any signals sent by the worker threads */
361 char buf [32]; 457 char buf [32];
362 while (read (respipe [0], buf, 32) == 32) 458 while (read (respipe [0], buf, 32) == 32)
363 ; 459 ;
364
365 rese = 0;
366 } 460 }
367 } 461 }
368 462
369 pthread_mutex_unlock (&reslock); 463 pthread_mutex_unlock (&reslock);
370 464
433 start_thread (); 527 start_thread ();
434 528
435 ++nreqs; 529 ++nreqs;
436 530
437 pthread_mutex_lock (&reqlock); 531 pthread_mutex_lock (&reqlock);
438 532 reqq_push (&req_queue, req);
439 req->next = 0;
440
441 if (reqe)
442 {
443 reqe->next = req;
444 reqe = req;
445 }
446 else
447 reqe = reqs = req;
448
449 pthread_cond_signal (&reqwait); 533 pthread_cond_signal (&reqwait);
450 pthread_mutex_unlock (&reqlock); 534 pthread_mutex_unlock (&reqlock);
451 535
452 if (nreqs > max_outstanding) 536 if (nreqs > max_outstanding)
453 for (;;) 537 for (;;)
462} 546}
463 547
464static void end_thread (void) 548static void end_thread (void)
465{ 549{
466 aio_req req; 550 aio_req req;
551
467 Newz (0, req, 1, aio_cb); 552 Newz (0, req, 1, aio_cb);
553
468 req->type = REQ_QUIT; 554 req->type = REQ_QUIT;
555 req->pri = PRI_MAX + PRI_BIAS;
469 556
470 req_send (req); 557 req_send (req);
471} 558}
472 559
473static void min_parallel (int nthreads) 560static void min_parallel (int nthreads)
560#if !HAVE_READAHEAD 647#if !HAVE_READAHEAD
561# define readahead aio_readahead 648# define readahead aio_readahead
562 649
563static ssize_t readahead (int fd, off_t offset, size_t count) 650static ssize_t readahead (int fd, off_t offset, size_t count)
564{ 651{
565 char readahead_buf[4096]; 652 dBUF;
566 653
567 while (count > 0) 654 while (count > 0)
568 { 655 {
569 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 656 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
570 657
571 pread (fd, readahead_buf, len, offset); 658 pread (fd, aio_buf, len, offset);
572 offset += len; 659 offset += len;
573 count -= len; 660 count -= len;
574 } 661 }
662
663 fBUF;
575 664
576 errno = 0; 665 errno = 0;
577} 666}
578#endif 667#endif
579 668
665#endif 754#endif
666 ) 755 )
667 ) 756 )
668 { 757 {
669 /* emulate sendfile. this is a major pain in the ass */ 758 /* emulate sendfile. this is a major pain in the ass */
670 char buf[4096]; 759 dBUF;
760
671 res = 0; 761 res = 0;
672 762
673 while (count) 763 while (count)
674 { 764 {
675 ssize_t cnt; 765 ssize_t cnt;
676 766
677 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); 767 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
678 768
679 if (cnt <= 0) 769 if (cnt <= 0)
680 { 770 {
681 if (cnt && !res) res = -1; 771 if (cnt && !res) res = -1;
682 break; 772 break;
683 } 773 }
684 774
685 cnt = write (ofd, buf, cnt); 775 cnt = write (ofd, aio_buf, cnt);
686 776
687 if (cnt <= 0) 777 if (cnt <= 0)
688 { 778 {
689 if (cnt && !res) res = -1; 779 if (cnt && !res) res = -1;
690 break; 780 break;
692 782
693 offset += cnt; 783 offset += cnt;
694 res += cnt; 784 res += cnt;
695 count -= cnt; 785 count -= cnt;
696 } 786 }
787
788 fBUF;
697 } 789 }
698 790
699 return res; 791 return res;
700} 792}
701 793
702/* read a full directory */ 794/* read a full directory */
703static int scandir_ (const char *path, void **namesp) 795static int scandir_ (const char *path, void **namesp)
704{ 796{
705 DIR *dirp = opendir (path); 797 DIR *dirp;
706 union 798 union
707 { 799 {
708 struct dirent d; 800 struct dirent d;
709 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 801 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
710 } u; 802 } *u;
711 struct dirent *entp; 803 struct dirent *entp;
712 char *name, *names; 804 char *name, *names;
713 int memlen = 4096; 805 int memlen = 4096;
714 int memofs = 0; 806 int memofs = 0;
715 int res = 0; 807 int res = 0;
716 int errorno; 808 int errorno;
717 809
810 dirp = opendir (path);
718 if (!dirp) 811 if (!dirp)
719 return -1; 812 return -1;
720 813
814 u = malloc (sizeof (*u));
721 names = malloc (memlen); 815 names = malloc (memlen);
722 816
817 if (u && names)
723 for (;;) 818 for (;;)
724 { 819 {
820 errno = 0;
725 errno = 0, readdir_r (dirp, &u.d, &entp); 821 readdir_r (dirp, &u->d, &entp);
726 822
727 if (!entp) 823 if (!entp)
728 break; 824 break;
729 825
730 name = entp->d_name; 826 name = entp->d_name;
731 827
732 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 828 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
733 { 829 {
734 int len = strlen (name) + 1; 830 int len = strlen (name) + 1;
735 831
736 res++; 832 res++;
737 833
738 while (memofs + len > memlen) 834 while (memofs + len > memlen)
739 { 835 {
740 memlen *= 2; 836 memlen *= 2;
741 names = realloc (names, memlen); 837 names = realloc (names, memlen);
742 if (!names) 838 if (!names)
743 break; 839 break;
744 } 840 }
745 841
746 memcpy (names + memofs, name, len); 842 memcpy (names + memofs, name, len);
747 memofs += len; 843 memofs += len;
748 } 844 }
749 } 845 }
750 846
751 errorno = errno; 847 errorno = errno;
848 free (u);
752 closedir (dirp); 849 closedir (dirp);
753 850
754 if (errorno) 851 if (errorno)
755 { 852 {
756 free (names); 853 free (names);
773 { 870 {
774 pthread_mutex_lock (&reqlock); 871 pthread_mutex_lock (&reqlock);
775 872
776 for (;;) 873 for (;;)
777 { 874 {
778 req = reqs; 875 req = reqq_shift (&req_queue);
779
780 if (reqs)
781 {
782 reqs = reqs->next;
783 if (!reqs) reqe = 0;
784 }
785 876
786 if (req) 877 if (req)
787 break; 878 break;
788 879
789 pthread_cond_wait (&reqwait, &reqlock); 880 pthread_cond_wait (&reqwait, &reqlock);
790 } 881 }
791 882
792 pthread_mutex_unlock (&reqlock); 883 pthread_mutex_unlock (&reqlock);
793 884
794 errno = 0; /* strictly unnecessary */ 885 errno = 0; /* strictly unnecessary */
886 type = req->type; /* remember type for QUIT check */
795 887
796 if (!(req->flags & FLAG_CANCELLED)) 888 if (!(req->flags & FLAG_CANCELLED))
797 switch (type = req->type) /* remember type for QUIT check */ 889 switch (type)
798 { 890 {
799 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 891 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
800 case REQ_WRITE: req->result = pwrite (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;
801 893
802 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 894 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
816 908
817 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 909 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
818 case REQ_FSYNC: req->result = fsync (req->fd); break; 910 case REQ_FSYNC: req->result = fsync (req->fd); break;
819 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; 911 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break;
820 912
821 case REQ_SLEEP: 913 case REQ_BUSY:
822 { 914 {
823 struct timeval tv; 915 struct timeval tv;
824 916
825 tv.tv_sec = req->fd; 917 tv.tv_sec = req->fd;
826 tv.tv_usec = req->fd2; 918 tv.tv_usec = req->fd2;
840 932
841 req->errorno = errno; 933 req->errorno = errno;
842 934
843 pthread_mutex_lock (&reslock); 935 pthread_mutex_lock (&reslock);
844 936
845 req->next = 0; 937 if (!reqq_push (&res_queue, req))
846
847 if (rese)
848 {
849 rese->next = req;
850 rese = req;
851 }
852 else
853 {
854 rese = ress = req;
855
856 /* write a dummy byte to the pipe so fh becomes ready */ 938 /* write a dummy byte to the pipe so fh becomes ready */
857 write (respipe [1], &respipe, 1); 939 write (respipe [1], &respipe, 1);
858 }
859 940
860 pthread_mutex_unlock (&reslock); 941 pthread_mutex_unlock (&reslock);
861 } 942 }
862 while (type != REQ_QUIT); 943 while (type != REQ_QUIT);
863 944
894{ 975{
895 aio_req prv; 976 aio_req prv;
896 977
897 started = 0; 978 started = 0;
898 979
899 while (reqs) 980 while (prv = reqq_shift (&req_queue))
900 {
901 prv = reqs;
902 reqs = prv->next;
903 req_free (prv); 981 req_free (prv);
904 }
905 982
906 reqs = reqe = 0; 983 while (prv = reqq_shift (&res_queue))
907
908 while (ress)
909 {
910 prv = ress;
911 ress = prv->next;
912 req_free (prv); 984 req_free (prv);
913 } 985
914
915 ress = rese = 0;
916
917 close (respipe [0]); 986 close (respipe [0]);
918 close (respipe [1]); 987 close (respipe [1]);
919 create_pipe (); 988 create_pipe ();
920 989
921 atfork_parent (); 990 atfork_parent ();
922} 991}
923 992
924#define dREQ \ 993#define dREQ \
925 aio_req req; \ 994 aio_req req; \
995 int req_pri = next_pri; \
996 next_pri = DEFAULT_PRI + PRI_BIAS; \
926 \ 997 \
927 if (SvOK (callback) && !SvROK (callback)) \ 998 if (SvOK (callback) && !SvROK (callback)) \
928 croak ("callback must be undef or of reference type"); \ 999 croak ("callback must be undef or of reference type"); \
929 \ 1000 \
930 Newz (0, req, 1, aio_cb); \ 1001 Newz (0, req, 1, aio_cb); \
931 if (!req) \ 1002 if (!req) \
932 croak ("out of memory during aio_req allocation"); \ 1003 croak ("out of memory during aio_req allocation"); \
933 \ 1004 \
934 req->callback = newSVsv (callback) 1005 req->callback = newSVsv (callback); \
1006 req->pri = req_pri
935 1007
936#define REQ_SEND \ 1008#define REQ_SEND \
937 req_send (req); \ 1009 req_send (req); \
938 \ 1010 \
939 if (GIMME_V != G_VOID) \ 1011 if (GIMME_V != G_VOID) \
1193 1265
1194 REQ_SEND; 1266 REQ_SEND;
1195} 1267}
1196 1268
1197void 1269void
1198aio_sleep (delay,callback=&PL_sv_undef) 1270aio_busy (delay,callback=&PL_sv_undef)
1199 double delay 1271 double delay
1200 SV * callback 1272 SV * callback
1201 PPCODE: 1273 PPCODE:
1202{ 1274{
1203 dREQ; 1275 dREQ;
1204 1276
1205 req->type = REQ_SLEEP; 1277 req->type = REQ_BUSY;
1206 req->fd = delay < 0. ? 0 : delay; 1278 req->fd = delay < 0. ? 0 : delay;
1207 req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); 1279 req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd);
1208 1280
1209 REQ_SEND; 1281 REQ_SEND;
1210} 1282}
1214 SV * callback 1286 SV * callback
1215 PROTOTYPE: ;$ 1287 PROTOTYPE: ;$
1216 PPCODE: 1288 PPCODE:
1217{ 1289{
1218 dREQ; 1290 dREQ;
1291
1219 req->type = REQ_GROUP; 1292 req->type = REQ_GROUP;
1220 req_send (req); 1293 req_send (req);
1294
1221 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1295 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1222} 1296}
1223 1297
1224void 1298void
1225aio_nop (callback=&PL_sv_undef) 1299aio_nop (callback=&PL_sv_undef)
1230 1304
1231 req->type = REQ_NOP; 1305 req->type = REQ_NOP;
1232 1306
1233 REQ_SEND; 1307 REQ_SEND;
1234} 1308}
1309
1310void
1311aioreq_pri (int pri = DEFAULT_PRI)
1312 CODE:
1313 if (pri < PRI_MIN) pri = PRI_MIN;
1314 if (pri > PRI_MAX) pri = PRI_MAX;
1315 next_pri = pri + PRI_BIAS;
1316
1317void
1318aioreq_nice (int nice = 0)
1319 CODE:
1320 nice = next_pri - nice;
1321 if (nice < PRI_MIN) nice = PRI_MIN;
1322 if (nice > PRI_MAX) nice = PRI_MAX;
1323 next_pri = nice + PRI_BIAS;
1235 1324
1236void 1325void
1237flush () 1326flush ()
1238 PROTOTYPE: 1327 PROTOTYPE:
1239 CODE: 1328 CODE:
1288 1377
1289MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1378MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1290 1379
1291void 1380void
1292cancel (aio_req_ornot req) 1381cancel (aio_req_ornot req)
1293 PROTOTYPE:
1294 CODE: 1382 CODE:
1295 req_cancel (req); 1383 req_cancel (req);
1296 1384
1297void 1385void
1298cb (aio_req req, SV *callback=&PL_sv_undef) 1386cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1299 CODE: 1387 CODE:
1300 SvREFCNT_dec (req->callback); 1388 SvREFCNT_dec (req->callback);
1301 req->callback = newSVsv (callback); 1389 req->callback = newSVsv (callback);
1302 1390
1303MODULE = IO::AIO PACKAGE = IO::AIO::GRP 1391MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1348 SvREFCNT_dec (grp->data); 1436 SvREFCNT_dec (grp->data);
1349 grp->data = (SV *)av; 1437 grp->data = (SV *)av;
1350} 1438}
1351 1439
1352void 1440void
1353feed_limit (aio_req grp, int limit) 1441limit (aio_req grp, int limit)
1354 CODE: 1442 CODE:
1355 grp->fd2 = limit; 1443 grp->fd2 = limit;
1356 aio_grp_feed (grp); 1444 aio_grp_feed (grp);
1357 1445
1358void 1446void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines