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.45 by root, Sun Oct 22 00:19:05 2006 UTC vs.
Revision 1.66 by root, Tue Oct 24 00:34:47 2006 UTC

1#if __linux
2# define _GNU_SOURCE
3#endif
4
1#define _REENTRANT 1 5#define _REENTRANT 1
6
2#include <errno.h> 7#include <errno.h>
3 8
4#include "EXTERN.h" 9#include "EXTERN.h"
5#include "perl.h" 10#include "perl.h"
6#include "XSUB.h" 11#include "XSUB.h"
41# define NAME_MAX 4096 46# define NAME_MAX 4096
42#endif 47#endif
43 48
44#if __ia64 49#if __ia64
45# define STACKSIZE 65536 50# define STACKSIZE 65536
51#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
52# define STACKSIZE PTHREAD_STACK_MIN
46#else 53#else
47# define STACKSIZE 8192 54# define STACKSIZE 16384
48#endif 55#endif
56
57/* buffer size for various temporary buffers */
58#define AIO_BUFSIZE 65536
59
60#define dBUF \
61 char *aio_buf = malloc (AIO_BUFSIZE); \
62 if (!aio_buf) \
63 return -1;
64
65#define fBUF free (aio_buf)
49 66
50enum { 67enum {
51 REQ_QUIT, 68 REQ_QUIT,
52 REQ_OPEN, REQ_CLOSE, 69 REQ_OPEN, REQ_CLOSE,
53 REQ_READ, REQ_WRITE, REQ_READAHEAD, 70 REQ_READ, REQ_WRITE, REQ_READAHEAD,
55 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 72 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
56 REQ_FSYNC, REQ_FDATASYNC, 73 REQ_FSYNC, REQ_FDATASYNC,
57 REQ_UNLINK, REQ_RMDIR, REQ_RENAME, 74 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
58 REQ_READDIR, 75 REQ_READDIR,
59 REQ_LINK, REQ_SYMLINK, 76 REQ_LINK, REQ_SYMLINK,
77 REQ_GROUP, REQ_NOP,
60 REQ_SLEEP, 78 REQ_SLEEP,
61 REQ_GROUP,
62}; 79};
63 80
64#define AIO_REQ_KLASS "IO::AIO::REQ" 81#define AIO_REQ_KLASS "IO::AIO::REQ"
65#define AIO_GRP_KLASS "IO::AIO::GRP" 82#define AIO_GRP_KLASS "IO::AIO::GRP"
66 83
67typedef struct aio_cb 84typedef struct aio_cb
68{ 85{
69 struct aio_cb *grp, *grp_prev, *grp_next;
70
71 struct aio_cb *volatile next; 86 struct aio_cb *volatile next;
72
73 SV *self; /* the perl counterpart of this request, if any */
74 87
75 SV *data, *callback; 88 SV *data, *callback;
76 SV *fh, *fh2; 89 SV *fh, *fh2;
77 void *dataptr, *data2ptr; 90 void *dataptr, *data2ptr;
78 Stat_t *statdata; 91 Stat_t *statdata;
79 off_t offset; 92 off_t offset;
80 size_t length; 93 size_t length;
81 ssize_t result; 94 ssize_t result;
82 95
96 STRLEN dataoffset;
83 int type; 97 int type;
84 int fd, fd2; 98 int fd, fd2;
85 int errorno; 99 int errorno;
86 STRLEN dataoffset;
87 mode_t mode; /* open */ 100 mode_t mode; /* open */
101
88 unsigned char cancelled; 102 unsigned char flags;
103 unsigned char pri;
104
105 SV *self; /* the perl counterpart of this request, if any */
106 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
89} aio_cb; 107} aio_cb;
108
109enum {
110 FLAG_CANCELLED = 0x01,
111};
90 112
91typedef aio_cb *aio_req; 113typedef aio_cb *aio_req;
92typedef aio_cb *aio_req_ornot; 114typedef aio_cb *aio_req_ornot;
93typedef aio_cb *aio_group; 115
116enum {
117 PRI_MIN = -4,
118 PRI_MAX = 4,
119
120 DEFAULT_PRI = 0,
121 PRI_BIAS = -PRI_MIN,
122};
123
124static int next_pri = DEFAULT_PRI + PRI_BIAS;
94 125
95static int started, wanted; 126static int started, wanted;
96static volatile int nreqs; 127static volatile int nreqs;
97static int max_outstanding = 1<<30; 128static int max_outstanding = 1<<30;
98static int respipe [2]; 129static int respipe [2];
99 130
131#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
132# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
133#else
134# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
135#endif
136
100static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 137static pthread_mutex_t reslock = AIO_MUTEX_INIT;
101static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 138static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
102static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 139static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
103 140
104static volatile aio_req reqs, reqe; /* queue start, queue end */ 141static volatile aio_req reqs, reqe; /* queue start, queue end */
105static volatile aio_req ress, rese; /* queue start, queue end */ 142static volatile aio_req ress, rese; /* queue start, queue end */
106 143
144static void req_invoke (aio_req req);
107static void req_free (aio_req req); 145static void req_free (aio_req req);
108 146
109/* must be called at most once */ 147/* must be called at most once */
110static SV *req_sv (aio_req req, const char *klass) 148static SV *req_sv (aio_req req, const char *klass)
111{ 149{
150 if (!req->self)
151 {
112 req->self = (SV *)newHV (); 152 req->self = (SV *)newHV ();
113 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); 153 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
154 }
114 155
115 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); 156 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1)));
116} 157}
117 158
118static aio_req SvAIO_REQ (SV *sv) 159static aio_req SvAIO_REQ (SV *sv)
119{ 160{
161 MAGIC *mg;
162
120 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) 163 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv))
121 croak ("object of class " AIO_REQ_KLASS " expected"); 164 croak ("object of class " AIO_REQ_KLASS " expected");
122 165
123 MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); 166 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
124 167
125 return mg ? (aio_req)mg->mg_ptr : 0; 168 return mg ? (aio_req)mg->mg_ptr : 0;
126} 169}
127 170
171static void aio_grp_feed (aio_req grp)
172{
173 while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED))
174 {
175 int old_len = grp->length;
176
177 if (grp->fh2 && SvOK (grp->fh2))
178 {
179 dSP;
180
181 ENTER;
182 SAVETMPS;
183 PUSHMARK (SP);
184 XPUSHs (req_sv (grp, AIO_GRP_KLASS));
185 PUTBACK;
186 call_sv (grp->fh2, G_VOID | G_EVAL);
187 SPAGAIN;
188 FREETMPS;
189 LEAVE;
190 }
191
192 /* stop if no progress has been made */
193 if (old_len == grp->length)
194 {
195 SvREFCNT_dec (grp->fh2);
196 grp->fh2 = 0;
197 break;
198 }
199 }
200}
201
202static void aio_grp_dec (aio_req grp)
203{
204 --grp->length;
205
206 /* call feeder, if applicable */
207 aio_grp_feed (grp);
208
209 /* finish, if done */
210 if (!grp->length && grp->fd)
211 {
212 req_invoke (grp);
213 req_free (grp);
214 }
215}
216
128static void poll_wait () 217static void poll_wait ()
129{ 218{
130 if (nreqs && !ress)
131 {
132 fd_set rfd; 219 fd_set rfd;
220
221 while (nreqs)
222 {
223 aio_req req;
224#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
225 pthread_mutex_lock (&reslock);
226#endif
227 req = ress;
228#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
229 pthread_mutex_unlock (&reslock);
230#endif
231
232 if (req)
233 return;
234
133 FD_ZERO(&rfd); 235 FD_ZERO(&rfd);
134 FD_SET(respipe [0], &rfd); 236 FD_SET(respipe [0], &rfd);
135 237
136 select (respipe [0] + 1, &rfd, 0, 0, 0); 238 select (respipe [0] + 1, &rfd, 0, 0, 0);
137 } 239 }
140static void req_invoke (aio_req req) 242static void req_invoke (aio_req req)
141{ 243{
142 dSP; 244 dSP;
143 int errorno = errno; 245 int errorno = errno;
144 246
145 if (req->cancelled || !SvOK (req->callback)) 247 if (req->flags & FLAG_CANCELLED || !SvOK (req->callback))
146 return; 248 return;
147 249
148 errno = req->errorno; 250 errno = req->errorno;
149 251
150 ENTER; 252 ENTER;
253 SAVETMPS;
151 PUSHMARK (SP); 254 PUSHMARK (SP);
255 EXTEND (SP, 1);
152 256
153 switch (req->type) 257 switch (req->type)
154 { 258 {
155 case REQ_READDIR: 259 case REQ_READDIR:
156 { 260 {
171 } 275 }
172 276
173 rv = sv_2mortal (newRV_noinc ((SV *)av)); 277 rv = sv_2mortal (newRV_noinc ((SV *)av));
174 } 278 }
175 279
176 XPUSHs (rv); 280 PUSHs (rv);
177 } 281 }
178 break; 282 break;
179 283
180 case REQ_OPEN: 284 case REQ_OPEN:
181 { 285 {
182 /* convert fd to fh */ 286 /* convert fd to fh */
183 SV *fh; 287 SV *fh;
184 288
185 XPUSHs (sv_2mortal (newSViv (req->result))); 289 PUSHs (sv_2mortal (newSViv (req->result)));
186 PUTBACK; 290 PUTBACK;
187 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 291 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
188 SPAGAIN; 292 SPAGAIN;
189 293
190 fh = SvREFCNT_inc (POPs); 294 fh = SvREFCNT_inc (POPs);
192 PUSHMARK (SP); 296 PUSHMARK (SP);
193 XPUSHs (sv_2mortal (fh)); 297 XPUSHs (sv_2mortal (fh));
194 } 298 }
195 break; 299 break;
196 300
301 case REQ_GROUP:
302 req->fd = 2; /* mark group as finished */
303
304 if (req->data)
305 {
306 int i;
307 AV *av = (AV *)req->data;
308
309 EXTEND (SP, AvFILL (av) + 1);
310 for (i = 0; i <= AvFILL (av); ++i)
311 PUSHs (*av_fetch (av, i, 0));
312 }
313 break;
314
315 case REQ_NOP:
197 case REQ_SLEEP: 316 case REQ_SLEEP:
198 case REQ_GROUP:
199 break; 317 break;
200 318
201 default: 319 default:
202 XPUSHs (sv_2mortal (newSViv (req->result))); 320 PUSHs (sv_2mortal (newSViv (req->result)));
203 break; 321 break;
204 } 322 }
205 323
206 324
207 PUTBACK; 325 PUTBACK;
208 call_sv (req->callback, G_VOID | G_EVAL); 326 call_sv (req->callback, G_VOID | G_EVAL);
209 SPAGAIN; 327 SPAGAIN;
210 328
329 FREETMPS;
330 LEAVE;
331
332 errno = errorno;
333
211 if (SvTRUE (ERRSV)) 334 if (SvTRUE (ERRSV))
212 { 335 {
213 req_free (req); 336 req_free (req);
214 croak (0); 337 croak (0);
215 } 338 }
216
217 LEAVE;
218
219 errno = errorno;
220} 339}
221 340
222static void req_free (aio_req req) 341static void req_free (aio_req req)
223{ 342{
224 if (req->grp) 343 if (req->grp)
225 { 344 {
226 aio_req grp = req->grp; 345 aio_req grp = req->grp;
227 346
228 /* unlink request */ 347 /* unlink request */
229 req->grp_next->grp_prev = req->grp_prev; 348 if (req->grp_next) req->grp_next->grp_prev = req->grp_prev;
230 req->grp_prev->grp_next = req->grp_next; 349 if (req->grp_prev) req->grp_prev->grp_next = req->grp_next;
231 350
232 if (grp->grp_next == grp) 351 if (grp->grp_first == req)
233 { 352 grp->grp_first = req->grp_next;
234 req_invoke (grp); 353
235 req_free (grp); 354 aio_grp_dec (grp);
236 }
237 } 355 }
238 356
239 if (req->self) 357 if (req->self)
240 { 358 {
241 sv_unmagic (req->self, PERL_MAGIC_ext); 359 sv_unmagic (req->self, PERL_MAGIC_ext);
242 SvREFCNT_dec (req->self); 360 SvREFCNT_dec (req->self);
243 } 361 }
244 362
245 if (req->data)
246 SvREFCNT_dec (req->data); 363 SvREFCNT_dec (req->data);
247
248 if (req->fh)
249 SvREFCNT_dec (req->fh); 364 SvREFCNT_dec (req->fh);
250
251 if (req->fh2)
252 SvREFCNT_dec (req->fh2); 365 SvREFCNT_dec (req->fh2);
253
254 if (req->statdata)
255 Safefree (req->statdata);
256
257 if (req->callback)
258 SvREFCNT_dec (req->callback); 366 SvREFCNT_dec (req->callback);
367 Safefree (req->statdata);
259 368
260 if (req->type == REQ_READDIR && req->result >= 0) 369 if (req->type == REQ_READDIR && req->result >= 0)
261 free (req->data2ptr); 370 free (req->data2ptr);
262 371
263 Safefree (req); 372 Safefree (req);
264} 373}
265 374
266static void req_cancel (aio_req req) 375static void req_cancel (aio_req req)
267{ 376{
268 req->cancelled = 1; 377 req->flags |= FLAG_CANCELLED;
269 378
270 if (req->type == REQ_GROUP) 379 if (req->type == REQ_GROUP)
271 { 380 {
272 aio_req sub; 381 aio_req sub;
273 382
274 for (sub = req->grp_next; sub != req; sub = sub->grp_next) 383 for (sub = req->grp_first; sub; sub = sub->grp_next)
275 req_cancel (sub); 384 req_cancel (sub);
276 } 385 }
277} 386}
278 387
279static int poll_cb () 388static int poll_cb ()
306 pthread_mutex_unlock (&reslock); 415 pthread_mutex_unlock (&reslock);
307 416
308 if (!req) 417 if (!req)
309 break; 418 break;
310 419
311 nreqs--; 420 --nreqs;
312 421
313 if (req->type == REQ_QUIT) 422 if (req->type == REQ_QUIT)
314 started--; 423 started--;
315 else if (req->type == REQ_GROUP && req->grp_next != req) 424 else if (req->type == REQ_GROUP && req->length)
425 {
426 req->fd = 1; /* mark request as delayed */
316 continue; 427 continue;
428 }
317 else 429 else
318 { 430 {
319 if (req->type == REQ_READ) 431 if (req->type == REQ_READ)
320 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); 432 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
321 433
364static void req_send (aio_req req) 476static void req_send (aio_req req)
365{ 477{
366 while (started < wanted && nreqs >= started) 478 while (started < wanted && nreqs >= started)
367 start_thread (); 479 start_thread ();
368 480
369 nreqs++; 481 ++nreqs;
370 482
371 pthread_mutex_lock (&reqlock); 483 pthread_mutex_lock (&reqlock);
372 484
373 req->next = 0; 485 req->next = 0;
374 486
494#if !HAVE_READAHEAD 606#if !HAVE_READAHEAD
495# define readahead aio_readahead 607# define readahead aio_readahead
496 608
497static ssize_t readahead (int fd, off_t offset, size_t count) 609static ssize_t readahead (int fd, off_t offset, size_t count)
498{ 610{
499 char readahead_buf[4096]; 611 dBUF;
500 612
501 while (count > 0) 613 while (count > 0)
502 { 614 {
503 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 615 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
504 616
505 pread (fd, readahead_buf, len, offset); 617 pread (fd, aio_buf, len, offset);
506 offset += len; 618 offset += len;
507 count -= len; 619 count -= len;
508 } 620 }
621
622 fBUF;
509 623
510 errno = 0; 624 errno = 0;
511} 625}
512#endif 626#endif
513 627
599#endif 713#endif
600 ) 714 )
601 ) 715 )
602 { 716 {
603 /* emulate sendfile. this is a major pain in the ass */ 717 /* emulate sendfile. this is a major pain in the ass */
604 char buf[4096]; 718 dBUF;
719
605 res = 0; 720 res = 0;
606 721
607 while (count) 722 while (count)
608 { 723 {
609 ssize_t cnt; 724 ssize_t cnt;
610 725
611 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); 726 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
612 727
613 if (cnt <= 0) 728 if (cnt <= 0)
614 { 729 {
615 if (cnt && !res) res = -1; 730 if (cnt && !res) res = -1;
616 break; 731 break;
617 } 732 }
618 733
619 cnt = write (ofd, buf, cnt); 734 cnt = write (ofd, aio_buf, cnt);
620 735
621 if (cnt <= 0) 736 if (cnt <= 0)
622 { 737 {
623 if (cnt && !res) res = -1; 738 if (cnt && !res) res = -1;
624 break; 739 break;
626 741
627 offset += cnt; 742 offset += cnt;
628 res += cnt; 743 res += cnt;
629 count -= cnt; 744 count -= cnt;
630 } 745 }
746
747 fBUF;
631 } 748 }
632 749
633 return res; 750 return res;
634} 751}
635 752
636/* read a full directory */ 753/* read a full directory */
637static int scandir_ (const char *path, void **namesp) 754static int scandir_ (const char *path, void **namesp)
638{ 755{
639 DIR *dirp = opendir (path); 756 DIR *dirp;
640 union 757 union
641 { 758 {
642 struct dirent d; 759 struct dirent d;
643 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 760 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
644 } u; 761 } *u;
645 struct dirent *entp; 762 struct dirent *entp;
646 char *name, *names; 763 char *name, *names;
647 int memlen = 4096; 764 int memlen = 4096;
648 int memofs = 0; 765 int memofs = 0;
649 int res = 0; 766 int res = 0;
650 int errorno; 767 int errorno;
651 768
769 dirp = opendir (path);
652 if (!dirp) 770 if (!dirp)
653 return -1; 771 return -1;
654 772
773 u = malloc (sizeof (*u));
655 names = malloc (memlen); 774 names = malloc (memlen);
656 775
776 if (u && names)
657 for (;;) 777 for (;;)
658 { 778 {
779 errno = 0;
659 errno = 0, readdir_r (dirp, &u.d, &entp); 780 readdir_r (dirp, &u->d, &entp);
660 781
661 if (!entp) 782 if (!entp)
662 break; 783 break;
663 784
664 name = entp->d_name; 785 name = entp->d_name;
665 786
666 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 787 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
667 { 788 {
668 int len = strlen (name) + 1; 789 int len = strlen (name) + 1;
669 790
670 res++; 791 res++;
671 792
672 while (memofs + len > memlen) 793 while (memofs + len > memlen)
673 { 794 {
674 memlen *= 2; 795 memlen *= 2;
675 names = realloc (names, memlen); 796 names = realloc (names, memlen);
676 if (!names) 797 if (!names)
677 break; 798 break;
678 } 799 }
679 800
680 memcpy (names + memofs, name, len); 801 memcpy (names + memofs, name, len);
681 memofs += len; 802 memofs += len;
682 } 803 }
683 } 804 }
684 805
685 errorno = errno; 806 errorno = errno;
807 free (u);
686 closedir (dirp); 808 closedir (dirp);
687 809
688 if (errorno) 810 if (errorno)
689 { 811 {
690 free (names); 812 free (names);
724 } 846 }
725 847
726 pthread_mutex_unlock (&reqlock); 848 pthread_mutex_unlock (&reqlock);
727 849
728 errno = 0; /* strictly unnecessary */ 850 errno = 0; /* strictly unnecessary */
851 type = req->type; /* remember type for QUIT check */
729 852
730 if (!req->cancelled) 853 if (!(req->flags & FLAG_CANCELLED))
731 switch (req->type) 854 switch (type)
732 { 855 {
733 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 856 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
734 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 857 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
735 858
736 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 859 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
760 tv.tv_usec = req->fd2; 883 tv.tv_usec = req->fd2;
761 884
762 req->result = select (0, 0, 0, 0, &tv); 885 req->result = select (0, 0, 0, 0, &tv);
763 } 886 }
764 887
888 case REQ_GROUP:
889 case REQ_NOP:
765 case REQ_QUIT: 890 case REQ_QUIT:
766 break; 891 break;
767 892
768 default: 893 default:
769 req->result = ENOSYS; 894 req->result = ENOSYS;
853 atfork_parent (); 978 atfork_parent ();
854} 979}
855 980
856#define dREQ \ 981#define dREQ \
857 aio_req req; \ 982 aio_req req; \
983 int req_pri = next_pri; \
984 next_pri = DEFAULT_PRI + PRI_BIAS; \
858 \ 985 \
859 if (SvOK (callback) && !SvROK (callback)) \ 986 if (SvOK (callback) && !SvROK (callback)) \
860 croak ("callback must be undef or of reference type"); \ 987 croak ("callback must be undef or of reference type"); \
861 \ 988 \
862 Newz (0, req, 1, aio_cb); \ 989 Newz (0, req, 1, aio_cb); \
863 if (!req) \ 990 if (!req) \
864 croak ("out of memory during aio_req allocation"); \ 991 croak ("out of memory during aio_req allocation"); \
865 \ 992 \
866 req->callback = newSVsv (callback) 993 req->callback = newSVsv (callback); \
994 req->pri = req_pri
867 995
868#define REQ_SEND \ 996#define REQ_SEND \
869 req_send (req); \ 997 req_send (req); \
870 \ 998 \
871 if (GIMME_V != G_VOID) \ 999 if (GIMME_V != G_VOID) \
1142} 1270}
1143 1271
1144void 1272void
1145aio_group (callback=&PL_sv_undef) 1273aio_group (callback=&PL_sv_undef)
1146 SV * callback 1274 SV * callback
1147 PROTOTYPE: ;& 1275 PROTOTYPE: ;$
1148 PPCODE: 1276 PPCODE:
1149{ 1277{
1150 dREQ; 1278 dREQ;
1279
1151 req->type = REQ_GROUP; 1280 req->type = REQ_GROUP;
1152 req->grp_next = req;
1153 req->grp_prev = req;
1154
1155 req_send (req); 1281 req_send (req);
1282
1156 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1283 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1157} 1284}
1285
1286void
1287aio_nop (callback=&PL_sv_undef)
1288 SV * callback
1289 PPCODE:
1290{
1291 dREQ;
1292
1293 req->type = REQ_NOP;
1294
1295 REQ_SEND;
1296}
1297
1298#if 0
1299
1300void
1301aio_pri (int pri = DEFAULT_PRI)
1302 CODE:
1303 if (pri < PRI_MIN) pri = PRI_MIN;
1304 if (pri > PRI_MAX) pri = PRI_MAX;
1305 next_pri = pri + PRI_BIAS;
1306
1307#endif
1158 1308
1159void 1309void
1160flush () 1310flush ()
1161 PROTOTYPE: 1311 PROTOTYPE:
1162 CODE: 1312 CODE:
1205 CODE: 1355 CODE:
1206 RETVAL = nreqs; 1356 RETVAL = nreqs;
1207 OUTPUT: 1357 OUTPUT:
1208 RETVAL 1358 RETVAL
1209 1359
1360PROTOTYPES: DISABLE
1361
1210MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1362MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1211 1363
1212void 1364void
1213cancel (aio_req_ornot req) 1365cancel (aio_req_ornot req)
1214 PROTOTYPE:
1215 CODE: 1366 CODE:
1216 req_cancel (req); 1367 req_cancel (req);
1217 1368
1369void
1370cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1371 CODE:
1372 SvREFCNT_dec (req->callback);
1373 req->callback = newSVsv (callback);
1374
1218MODULE = IO::AIO PACKAGE = IO::AIO::GRP 1375MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1219 1376
1220void 1377void
1221add (aio_req grp, ...) 1378add (aio_req grp, ...)
1222 PROTOTYPE: $;@
1223 PPCODE: 1379 PPCODE:
1224{ 1380{
1225 int i; 1381 int i;
1382 aio_req req;
1383
1384 if (grp->fd == 2)
1385 croak ("cannot add requests to IO::AIO::GRP after the group finished");
1226 1386
1227 for (i = 1; i < items; ++i ) 1387 for (i = 1; i < items; ++i )
1228 { 1388 {
1229 aio_req req = SvAIO_REQ (ST (i));
1230
1231 req->grp_prev = grp;
1232 req->grp_next = grp->grp_next;
1233 grp->grp_next->grp_prev = req;
1234 grp->grp_next = req;
1235
1236 req->grp = grp;
1237
1238 if (GIMME_V != G_VOID) 1389 if (GIMME_V != G_VOID)
1239 XPUSHs (sv_2mortal (newSVsv (ST (i)))); 1390 XPUSHs (sv_2mortal (newSVsv (ST (i))));
1391
1392 req = SvAIO_REQ (ST (i));
1393
1394 if (req)
1395 {
1396 ++grp->length;
1397 req->grp = grp;
1398
1399 req->grp_prev = 0;
1400 req->grp_next = grp->grp_first;
1401
1402 if (grp->grp_first)
1403 grp->grp_first->grp_prev = req;
1404
1405 grp->grp_first = req;
1406 }
1240 } 1407 }
1241} 1408}
1242 1409
1410void
1411result (aio_req grp, ...)
1412 CODE:
1413{
1414 int i;
1415 AV *av = newAV ();
1416
1417 for (i = 1; i < items; ++i )
1418 av_push (av, newSVsv (ST (i)));
1419
1420 SvREFCNT_dec (grp->data);
1421 grp->data = (SV *)av;
1422}
1423
1424void
1425feed_limit (aio_req grp, int limit)
1426 CODE:
1427 grp->fd2 = limit;
1428 aio_grp_feed (grp);
1429
1430void
1431feed (aio_req grp, SV *callback=&PL_sv_undef)
1432 CODE:
1433{
1434 SvREFCNT_dec (grp->fh2);
1435 grp->fh2 = newSVsv (callback);
1436
1437 if (grp->fd2 <= 0)
1438 grp->fd2 = 2;
1439
1440 aio_grp_feed (grp);
1441}
1442

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines