ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
Revision: 1.112
Committed: Tue Apr 1 19:40:47 2008 UTC (16 years, 1 month ago) by root
Branch: MAIN
Changes since 1.111: +0 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.98 #include "xthread.h"
2 root 1.63
3 root 1.19 #include <errno.h>
4    
5 root 1.15 #include "EXTERN.h"
6 root 1.1 #include "perl.h"
7     #include "XSUB.h"
8    
9 root 1.37 #include <stddef.h>
10 root 1.94 #include <stdlib.h>
11 root 1.41 #include <errno.h>
12 root 1.1 #include <sys/types.h>
13     #include <sys/stat.h>
14 root 1.37 #include <limits.h>
15 root 1.1 #include <fcntl.h>
16     #include <sched.h>
17 root 1.103
18     #ifdef _WIN32
19    
20     # define SIGIO 0
21     typedef Direntry_t X_DIRENT;
22     #undef malloc
23     #undef free
24    
25     // perl overrides all those nice win32 functions
26     # undef open
27     # undef read
28     # undef write
29 root 1.104 # undef send
30     # undef recv
31 root 1.103 # undef stat
32     # undef fstat
33     # define lstat stat
34     # undef truncate
35     # undef ftruncate
36     # undef open
37     # undef close
38     # undef unlink
39     # undef rmdir
40     # undef rename
41     # undef lseek
42    
43     # define chown(a,b,c) (errno = ENOSYS, -1)
44     # define fchown(a,b,c) (errno = ENOSYS, -1)
45     # define fchmod(a,b) (errno = ENOSYS, -1)
46     # define symlink(a,b) (errno = ENOSYS, -1)
47     # define readlink(a,b,c) (errno = ENOSYS, -1)
48     # define mknod(a,b,c) (errno = ENOSYS, -1)
49     # define truncate(a,b) (errno = ENOSYS, -1)
50     # define ftruncate(fd,o) chsize ((fd), (o))
51     # define fsync(fd) _commit (fd)
52     # define opendir(fd) (errno = ENOSYS, 0)
53     # define readdir(fd) (errno = ENOSYS, -1)
54     # define closedir(fd) (errno = ENOSYS, -1)
55     # define mkdir(a,b) mkdir (a)
56    
57     #else
58    
59     # include "autoconf/config.h"
60     # include <sys/time.h>
61     # include <sys/select.h>
62     # include <unistd.h>
63     # include <utime.h>
64     # include <signal.h>
65     typedef struct dirent X_DIRENT;
66    
67     #endif
68 root 1.1
69 root 1.32 #if HAVE_SENDFILE
70     # if __linux
71     # include <sys/sendfile.h>
72     # elif __freebsd
73     # include <sys/socket.h>
74     # include <sys/uio.h>
75     # elif __hpux
76     # include <sys/socket.h>
77 root 1.35 # elif __solaris /* not yet */
78     # include <sys/sendfile.h>
79 root 1.34 # else
80     # error sendfile support requested but not available
81 root 1.32 # endif
82     #endif
83 root 1.1
84 root 1.84 /* number of seconds after which idle threads exit */
85     #define IDLE_TIMEOUT 10
86    
87 root 1.39 /* used for struct dirent, AIX doesn't provide it */
88     #ifndef NAME_MAX
89     # define NAME_MAX 4096
90     #endif
91    
92 root 1.65 /* buffer size for various temporary buffers */
93     #define AIO_BUFSIZE 65536
94    
95 root 1.101 /* use NV for 32 bit perls as it allows larger offsets */
96     #if IVSIZE >= 8
97     # define SvVAL64 SvIV
98     #else
99     # define SvVAL64 SvNV
100     #endif
101    
102 root 1.107 static HV *stash;
103    
104 root 1.65 #define dBUF \
105 root 1.71 char *aio_buf; \
106 root 1.103 X_LOCK (wrklock); \
107 root 1.71 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \
108 root 1.103 X_UNLOCK (wrklock); \
109 root 1.65 if (!aio_buf) \
110     return -1;
111    
112 root 1.90 typedef SV SV8; /* byte-sv, used for argument-checking */
113    
114 root 1.1 enum {
115     REQ_QUIT,
116     REQ_OPEN, REQ_CLOSE,
117 root 1.101 REQ_READ, REQ_WRITE,
118     REQ_READAHEAD, REQ_SENDFILE,
119 root 1.22 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
120 root 1.103 REQ_TRUNCATE, REQ_FTRUNCATE,
121 root 1.101 REQ_UTIME, REQ_FUTIME,
122     REQ_CHMOD, REQ_FCHMOD,
123     REQ_CHOWN, REQ_FCHOWN,
124 root 1.110 REQ_SYNC, REQ_FSYNC, REQ_FDATASYNC,
125 root 1.97 REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME,
126 root 1.81 REQ_MKNOD, REQ_READDIR,
127 root 1.86 REQ_LINK, REQ_SYMLINK, REQ_READLINK,
128 root 1.54 REQ_GROUP, REQ_NOP,
129 root 1.69 REQ_BUSY,
130 root 1.1 };
131    
132 root 1.44 #define AIO_REQ_KLASS "IO::AIO::REQ"
133     #define AIO_GRP_KLASS "IO::AIO::GRP"
134 root 1.43
135     typedef struct aio_cb
136     {
137 root 1.49 struct aio_cb *volatile next;
138 root 1.43
139 root 1.99 SV *callback;
140 root 1.86 SV *sv1, *sv2;
141     void *ptr1, *ptr2;
142     off_t offs;
143     size_t size;
144 root 1.1 ssize_t result;
145 root 1.99 double nv1, nv2;
146 root 1.43
147 root 1.86 STRLEN stroffset;
148 root 1.43 int type;
149 root 1.99 int int1, int2, int3;
150 root 1.1 int errorno;
151 root 1.43 mode_t mode; /* open */
152 root 1.60
153     unsigned char flags;
154 root 1.58 unsigned char pri;
155 root 1.60
156     SV *self; /* the perl counterpart of this request, if any */
157     struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
158 root 1.1 } aio_cb;
159    
160 root 1.58 enum {
161 root 1.89 FLAG_CANCELLED = 0x01, /* request was cancelled */
162 root 1.100 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */
163 root 1.89 FLAG_PTR2_FREE = 0x80, /* need to free(ptr2) */
164 root 1.58 };
165    
166 root 1.1 typedef aio_cb *aio_req;
167 root 1.43 typedef aio_cb *aio_req_ornot;
168 root 1.1
169 root 1.60 enum {
170 root 1.61 PRI_MIN = -4,
171     PRI_MAX = 4,
172 root 1.60
173     DEFAULT_PRI = 0,
174 root 1.61 PRI_BIAS = -PRI_MIN,
175 root 1.67 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
176 root 1.60 };
177    
178 root 1.85 #define AIO_TICKS ((1000000 + 1023) >> 10)
179    
180     static unsigned int max_poll_time = 0;
181     static unsigned int max_poll_reqs = 0;
182    
183     /* calculcate time difference in ~1/AIO_TICKS of a second */
184     static int tvdiff (struct timeval *tv1, struct timeval *tv2)
185     {
186     return (tv2->tv_sec - tv1->tv_sec ) * AIO_TICKS
187     + ((tv2->tv_usec - tv1->tv_usec) >> 10);
188     }
189    
190 root 1.98 static thread_t main_tid;
191 root 1.94 static int main_sig;
192     static int block_sig_level;
193    
194 root 1.111 void block_sig (void)
195 root 1.94 {
196     sigset_t ss;
197    
198     if (block_sig_level++)
199     return;
200    
201     if (!main_sig)
202     return;
203    
204     sigemptyset (&ss);
205     sigaddset (&ss, main_sig);
206     pthread_sigmask (SIG_BLOCK, &ss, 0);
207     }
208    
209 root 1.111 void unblock_sig (void)
210 root 1.94 {
211     sigset_t ss;
212    
213     if (--block_sig_level)
214     return;
215    
216     if (!main_sig)
217     return;
218    
219     sigemptyset (&ss);
220     sigaddset (&ss, main_sig);
221     pthread_sigmask (SIG_UNBLOCK, &ss, 0);
222     }
223    
224 root 1.60 static int next_pri = DEFAULT_PRI + PRI_BIAS;
225    
226 root 1.84 static unsigned int started, idle, wanted;
227 root 1.1
228 root 1.76 /* worker threads management */
229 root 1.103 static mutex_t wrklock = X_MUTEX_INIT;
230 root 1.71
231     typedef struct worker {
232     /* locked by wrklock */
233     struct worker *prev, *next;
234    
235 root 1.98 thread_t tid;
236 root 1.71
237     /* locked by reslock, reqlock or wrklock */
238     aio_req req; /* currently processed request */
239     void *dbuf;
240     DIR *dirp;
241     } worker;
242    
243     static worker wrk_first = { &wrk_first, &wrk_first, 0 };
244    
245     static void worker_clear (worker *wrk)
246     {
247     if (wrk->dirp)
248     {
249     closedir (wrk->dirp);
250     wrk->dirp = 0;
251     }
252    
253     if (wrk->dbuf)
254     {
255     free (wrk->dbuf);
256     wrk->dbuf = 0;
257     }
258     }
259    
260     static void worker_free (worker *wrk)
261     {
262     wrk->next->prev = wrk->prev;
263     wrk->prev->next = wrk->next;
264    
265     free (wrk);
266     }
267    
268 root 1.80 static volatile unsigned int nreqs, nready, npending;
269 root 1.84 static volatile unsigned int max_idle = 4;
270 root 1.80 static volatile unsigned int max_outstanding = 0xffffffff;
271 root 1.106 static int respipe_osf [2], respipe [2] = { -1, -1 };
272 root 1.80
273 root 1.103 static mutex_t reslock = X_MUTEX_INIT;
274     static mutex_t reqlock = X_MUTEX_INIT;
275     static cond_t reqwait = X_COND_INIT;
276 root 1.3
277 root 1.85 #if WORDACCESS_UNSAFE
278 root 1.80
279 root 1.111 static unsigned int get_nready (void)
280 root 1.80 {
281     unsigned int retval;
282    
283 root 1.103 X_LOCK (reqlock);
284 root 1.80 retval = nready;
285 root 1.103 X_UNLOCK (reqlock);
286 root 1.80
287     return retval;
288     }
289    
290 root 1.111 static unsigned int get_npending (void)
291 root 1.80 {
292     unsigned int retval;
293    
294 root 1.103 X_LOCK (reslock);
295 root 1.80 retval = npending;
296 root 1.103 X_UNLOCK (reslock);
297 root 1.80
298     return retval;
299     }
300    
301 root 1.111 static unsigned int get_nthreads (void)
302 root 1.85 {
303     unsigned int retval;
304    
305 root 1.103 X_LOCK (wrklock);
306 root 1.85 retval = started;
307 root 1.103 X_UNLOCK (wrklock);
308 root 1.85
309     return retval;
310     }
311    
312 root 1.80 #else
313    
314     # define get_nready() nready
315     # define get_npending() npending
316 root 1.85 # define get_nthreads() started
317 root 1.80
318     #endif
319    
320 root 1.67 /*
321     * a somewhat faster data structure might be nice, but
322     * with 8 priorities this actually needs <20 insns
323     * per shift, the most expensive operation.
324     */
325     typedef struct {
326     aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */
327     int size;
328     } reqq;
329    
330     static reqq req_queue;
331     static reqq res_queue;
332    
333     int reqq_push (reqq *q, aio_req req)
334     {
335     int pri = req->pri;
336     req->next = 0;
337    
338     if (q->qe[pri])
339     {
340     q->qe[pri]->next = req;
341     q->qe[pri] = req;
342     }
343     else
344     q->qe[pri] = q->qs[pri] = req;
345    
346     return q->size++;
347     }
348    
349     aio_req reqq_shift (reqq *q)
350     {
351     int pri;
352    
353     if (!q->size)
354     return 0;
355    
356     --q->size;
357    
358     for (pri = NUM_PRI; pri--; )
359     {
360     aio_req req = q->qs[pri];
361    
362     if (req)
363     {
364     if (!(q->qs[pri] = req->next))
365     q->qe[pri] = 0;
366    
367     return req;
368     }
369     }
370    
371     abort ();
372     }
373 root 1.1
374 root 1.111 static int poll_cb (void);
375 root 1.94 static int req_invoke (aio_req req);
376 root 1.101 static void req_destroy (aio_req req);
377 root 1.72 static void req_cancel (aio_req req);
378 root 1.45
379 root 1.43 /* must be called at most once */
380 root 1.44 static SV *req_sv (aio_req req, const char *klass)
381 root 1.43 {
382 root 1.49 if (!req->self)
383     {
384     req->self = (SV *)newHV ();
385     sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
386     }
387 root 1.43
388 root 1.45 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1)));
389 root 1.43 }
390    
391 root 1.45 static aio_req SvAIO_REQ (SV *sv)
392 root 1.26 {
393 root 1.53 MAGIC *mg;
394    
395 root 1.45 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv))
396     croak ("object of class " AIO_REQ_KLASS " expected");
397 root 1.43
398 root 1.53 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
399 root 1.43
400     return mg ? (aio_req)mg->mg_ptr : 0;
401     }
402    
403 root 1.49 static void aio_grp_feed (aio_req grp)
404     {
405 root 1.94 block_sig ();
406    
407 root 1.86 while (grp->size < grp->int2 && !(grp->flags & FLAG_CANCELLED))
408 root 1.49 {
409 root 1.86 int old_len = grp->size;
410 root 1.49
411 root 1.86 if (grp->sv2 && SvOK (grp->sv2))
412 root 1.49 {
413     dSP;
414    
415     ENTER;
416     SAVETMPS;
417     PUSHMARK (SP);
418     XPUSHs (req_sv (grp, AIO_GRP_KLASS));
419     PUTBACK;
420 root 1.86 call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR);
421 root 1.49 SPAGAIN;
422     FREETMPS;
423     LEAVE;
424     }
425    
426     /* stop if no progress has been made */
427 root 1.86 if (old_len == grp->size)
428 root 1.49 {
429 root 1.86 SvREFCNT_dec (grp->sv2);
430     grp->sv2 = 0;
431 root 1.49 break;
432     }
433     }
434 root 1.94
435     unblock_sig ();
436 root 1.49 }
437    
438 root 1.50 static void aio_grp_dec (aio_req grp)
439     {
440 root 1.86 --grp->size;
441 root 1.50
442     /* call feeder, if applicable */
443     aio_grp_feed (grp);
444    
445     /* finish, if done */
446 root 1.86 if (!grp->size && grp->int1)
447 root 1.50 {
448 root 1.94 block_sig ();
449    
450     if (!req_invoke (grp))
451     {
452 root 1.101 req_destroy (grp);
453 root 1.94 unblock_sig ();
454     croak (0);
455     }
456    
457 root 1.101 req_destroy (grp);
458 root 1.94 unblock_sig ();
459 root 1.50 }
460     }
461    
462 root 1.94 static int req_invoke (aio_req req)
463 root 1.45 {
464     dSP;
465    
466 root 1.100 if (req->flags & FLAG_SV2_RO_OFF)
467     SvREADONLY_off (req->sv2);
468 root 1.86
469 root 1.67 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
470     {
471     ENTER;
472     SAVETMPS;
473     PUSHMARK (SP);
474     EXTEND (SP, 1);
475 root 1.45
476 root 1.67 switch (req->type)
477 root 1.45 {
478 root 1.67 case REQ_READDIR:
479 root 1.45 {
480 root 1.67 SV *rv = &PL_sv_undef;
481 root 1.45
482 root 1.67 if (req->result >= 0)
483 root 1.45 {
484 root 1.71 int i;
485 root 1.86 char *buf = req->ptr2;
486 root 1.67 AV *av = newAV ();
487    
488 root 1.71 av_extend (av, req->result - 1);
489    
490     for (i = 0; i < req->result; ++i)
491 root 1.67 {
492     SV *sv = newSVpv (buf, 0);
493    
494 root 1.71 av_store (av, i, sv);
495 root 1.67 buf += SvCUR (sv) + 1;
496     }
497 root 1.45
498 root 1.67 rv = sv_2mortal (newRV_noinc ((SV *)av));
499 root 1.45 }
500    
501 root 1.67 PUSHs (rv);
502 root 1.45 }
503 root 1.67 break;
504 root 1.45
505 root 1.67 case REQ_OPEN:
506     {
507     /* convert fd to fh */
508 root 1.107 SV *fh = &PL_sv_undef;
509    
510     if (req->result >= 0)
511     {
512     GV *gv = (GV *)sv_newmortal ();
513     int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR);
514     char sym [64];
515     int symlen;
516    
517     symlen = snprintf (sym, sizeof (sym), "fd#%d", req->result);
518     gv_init (gv, stash, sym, symlen, 0);
519    
520     symlen = snprintf (
521     sym,
522     sizeof (sym),
523     "%s&=%d",
524     flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<",
525     req->result
526     );
527 root 1.45
528 root 1.107 if (do_open (gv, sym, symlen, 0, 0, 0, 0))
529 root 1.108 fh = (SV *)gv;
530 root 1.107 }
531 root 1.45
532 root 1.109 PUSHs (fh);
533 root 1.67 }
534     break;
535 root 1.45
536 root 1.67 case REQ_GROUP:
537 root 1.86 req->int1 = 2; /* mark group as finished */
538 root 1.45
539 root 1.86 if (req->sv1)
540 root 1.67 {
541     int i;
542 root 1.86 AV *av = (AV *)req->sv1;
543 root 1.49
544 root 1.67 EXTEND (SP, AvFILL (av) + 1);
545     for (i = 0; i <= AvFILL (av); ++i)
546     PUSHs (*av_fetch (av, i, 0));
547     }
548     break;
549 root 1.48
550 root 1.67 case REQ_NOP:
551 root 1.69 case REQ_BUSY:
552 root 1.67 break;
553 root 1.48
554 root 1.86 case REQ_READLINK:
555     if (req->result > 0)
556     {
557 root 1.99 SvCUR_set (req->sv2, req->result);
558     *SvEND (req->sv2) = 0;
559     PUSHs (req->sv2);
560 root 1.86 }
561     break;
562    
563 root 1.87 case REQ_STAT:
564     case REQ_LSTAT:
565     case REQ_FSTAT:
566     PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
567     PL_laststatval = req->result;
568     PL_statcache = *(Stat_t *)(req->ptr2);
569     PUSHs (sv_2mortal (newSViv (req->result)));
570     break;
571    
572 root 1.86 case REQ_READ:
573 root 1.99 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0));
574     *SvEND (req->sv2) = 0;
575 root 1.87 PUSHs (sv_2mortal (newSViv (req->result)));
576     break;
577    
578 root 1.67 default:
579     PUSHs (sv_2mortal (newSViv (req->result)));
580     break;
581     }
582 root 1.45
583 root 1.79 errno = req->errorno;
584 root 1.51
585 root 1.67 PUTBACK;
586 root 1.109 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD);
587 root 1.67 SPAGAIN;
588 root 1.51
589 root 1.67 FREETMPS;
590     LEAVE;
591 root 1.109
592     PUTBACK;
593 root 1.45 }
594    
595     if (req->grp)
596     {
597     aio_req grp = req->grp;
598    
599     /* unlink request */
600 root 1.49 if (req->grp_next) req->grp_next->grp_prev = req->grp_prev;
601     if (req->grp_prev) req->grp_prev->grp_next = req->grp_next;
602    
603     if (grp->grp_first == req)
604     grp->grp_first = req->grp_next;
605    
606 root 1.50 aio_grp_dec (grp);
607 root 1.45 }
608    
609 root 1.94 return !SvTRUE (ERRSV);
610 root 1.67 }
611    
612 root 1.101 static void req_destroy (aio_req req)
613 root 1.67 {
614 root 1.43 if (req->self)
615     {
616     sv_unmagic (req->self, PERL_MAGIC_ext);
617     SvREFCNT_dec (req->self);
618     }
619    
620 root 1.86 SvREFCNT_dec (req->sv1);
621     SvREFCNT_dec (req->sv2);
622 root 1.49 SvREFCNT_dec (req->callback);
623 root 1.26
624 root 1.87 if (req->flags & FLAG_PTR2_FREE)
625 root 1.86 free (req->ptr2);
626 root 1.37
627 root 1.26 Safefree (req);
628     }
629    
630 root 1.72 static void req_cancel_subs (aio_req grp)
631     {
632     aio_req sub;
633    
634     if (grp->type != REQ_GROUP)
635     return;
636    
637 root 1.86 SvREFCNT_dec (grp->sv2);
638     grp->sv2 = 0;
639 root 1.72
640     for (sub = grp->grp_first; sub; sub = sub->grp_next)
641     req_cancel (sub);
642     }
643    
644 root 1.45 static void req_cancel (aio_req req)
645 root 1.1 {
646 root 1.58 req->flags |= FLAG_CANCELLED;
647 root 1.45
648 root 1.72 req_cancel_subs (req);
649 root 1.1 }
650    
651 root 1.104 #ifdef USE_SOCKETS_AS_HANDLES
652     # define TO_SOCKET(x) (win32_get_osfhandle (x))
653     #else
654     # define TO_SOCKET(x) (x)
655     #endif
656    
657     static void
658 root 1.111 create_respipe (void)
659 root 1.104 {
660 root 1.106 int old_readfd = respipe [0];
661    
662     if (respipe [1] >= 0)
663     respipe_close (TO_SOCKET (respipe [1]));
664    
665     #ifdef _WIN32
666     if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe))
667     #else
668     if (pipe (respipe))
669     #endif
670     croak ("unable to initialize result pipe");
671    
672     if (old_readfd >= 0)
673     {
674     if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
675     croak ("unable to initialize result pipe(2)");
676    
677     respipe_close (respipe [0]);
678     respipe [0] = old_readfd;
679     }
680    
681 root 1.104 #ifdef _WIN32
682     int arg = 1;
683 root 1.106 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
684     || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
685 root 1.104 #else
686 root 1.106 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
687     || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
688 root 1.104 #endif
689 root 1.106 croak ("unable to initialize result pipe(3)");
690 root 1.104
691     respipe_osf [0] = TO_SOCKET (respipe [0]);
692     respipe_osf [1] = TO_SOCKET (respipe [1]);
693     }
694    
695 root 1.103 X_THREAD_PROC (aio_proc);
696 root 1.4
697 root 1.45 static void start_thread (void)
698 root 1.4 {
699 root 1.71 worker *wrk = calloc (1, sizeof (worker));
700    
701     if (!wrk)
702     croak ("unable to allocate worker thread data");
703    
704 root 1.103 X_LOCK (wrklock);
705 root 1.4
706 root 1.98 if (thread_create (&wrk->tid, aio_proc, (void *)wrk))
707 root 1.71 {
708     wrk->prev = &wrk_first;
709     wrk->next = wrk_first.next;
710     wrk_first.next->prev = wrk;
711     wrk_first.next = wrk;
712 root 1.78 ++started;
713 root 1.71 }
714     else
715     free (wrk);
716 root 1.4
717 root 1.103 X_UNLOCK (wrklock);
718 root 1.4 }
719    
720 root 1.111 static void maybe_start_thread (void)
721 root 1.80 {
722 root 1.85 if (get_nthreads () >= wanted)
723 root 1.80 return;
724    
725 root 1.84 /* todo: maybe use idle here, but might be less exact */
726 root 1.85 if (0 <= (int)get_nthreads () + (int)get_npending () - (int)nreqs)
727 root 1.80 return;
728    
729     start_thread ();
730     }
731    
732 root 1.45 static void req_send (aio_req req)
733 root 1.4 {
734 root 1.94 block_sig ();
735    
736 root 1.50 ++nreqs;
737 root 1.4
738 root 1.103 X_LOCK (reqlock);
739 root 1.79 ++nready;
740 root 1.67 reqq_push (&req_queue, req);
741 root 1.103 X_COND_SIGNAL (reqwait);
742     X_UNLOCK (reqlock);
743 root 1.80
744 root 1.94 unblock_sig ();
745    
746 root 1.80 maybe_start_thread ();
747 root 1.4 }
748    
749 root 1.45 static void end_thread (void)
750 root 1.4 {
751     aio_req req;
752 root 1.67
753 root 1.26 Newz (0, req, 1, aio_cb);
754 root 1.67
755 root 1.4 req->type = REQ_QUIT;
756 root 1.67 req->pri = PRI_MAX + PRI_BIAS;
757 root 1.4
758 root 1.103 X_LOCK (reqlock);
759 root 1.83 reqq_push (&req_queue, req);
760 root 1.103 X_COND_SIGNAL (reqwait);
761     X_UNLOCK (reqlock);
762 root 1.80
763 root 1.103 X_LOCK (wrklock);
764 root 1.80 --started;
765 root 1.103 X_UNLOCK (wrklock);
766 root 1.4 }
767    
768 root 1.85 static void set_max_idle (int nthreads)
769     {
770 root 1.103 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
771 root 1.85 max_idle = nthreads <= 0 ? 1 : nthreads;
772 root 1.103 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
773 root 1.85 }
774    
775 root 1.22 static void min_parallel (int nthreads)
776     {
777 root 1.30 if (wanted < nthreads)
778     wanted = nthreads;
779 root 1.22 }
780    
781     static void max_parallel (int nthreads)
782     {
783 root 1.30 if (wanted > nthreads)
784     wanted = nthreads;
785    
786 root 1.80 while (started > wanted)
787     end_thread ();
788     }
789    
790 root 1.111 static void poll_wait (void)
791 root 1.80 {
792     fd_set rfd;
793    
794     while (nreqs)
795 root 1.30 {
796 root 1.80 int size;
797 root 1.103 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
798 root 1.80 size = res_queue.size;
799 root 1.103 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
800 root 1.80
801     if (size)
802     return;
803    
804     maybe_start_thread ();
805    
806 root 1.104 FD_ZERO (&rfd);
807     FD_SET (respipe [0], &rfd);
808 root 1.80
809 root 1.104 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
810 root 1.22 }
811 root 1.80 }
812    
813 root 1.111 static int poll_cb (void)
814 root 1.80 {
815     dSP;
816     int count = 0;
817 root 1.85 int maxreqs = max_poll_reqs;
818 root 1.80 int do_croak = 0;
819 root 1.85 struct timeval tv_start, tv_now;
820 root 1.80 aio_req req;
821 root 1.22
822 root 1.85 if (max_poll_time)
823     gettimeofday (&tv_start, 0);
824    
825 root 1.94 block_sig ();
826    
827 root 1.80 for (;;)
828 root 1.22 {
829 root 1.85 for (;;)
830 root 1.80 {
831     maybe_start_thread ();
832    
833 root 1.103 X_LOCK (reslock);
834 root 1.80 req = reqq_shift (&res_queue);
835    
836     if (req)
837     {
838     --npending;
839    
840     if (!res_queue.size)
841     {
842     /* read any signals sent by the worker threads */
843 root 1.95 char buf [4];
844 root 1.105 while (respipe_read (respipe [0], buf, 4) == 4)
845 root 1.80 ;
846     }
847     }
848    
849 root 1.103 X_UNLOCK (reslock);
850 root 1.80
851     if (!req)
852     break;
853    
854     --nreqs;
855    
856 root 1.86 if (req->type == REQ_GROUP && req->size)
857 root 1.80 {
858 root 1.86 req->int1 = 1; /* mark request as delayed */
859 root 1.80 continue;
860     }
861     else
862     {
863 root 1.94 if (!req_invoke (req))
864     {
865 root 1.101 req_destroy (req);
866 root 1.94 unblock_sig ();
867     croak (0);
868     }
869 root 1.80
870     count++;
871     }
872    
873 root 1.101 req_destroy (req);
874 root 1.85
875     if (maxreqs && !--maxreqs)
876     break;
877    
878     if (max_poll_time)
879     {
880     gettimeofday (&tv_now, 0);
881    
882     if (tvdiff (&tv_start, &tv_now) >= max_poll_time)
883     break;
884     }
885 root 1.80 }
886    
887     if (nreqs <= max_outstanding)
888     break;
889    
890 root 1.22 poll_wait ();
891 root 1.80
892 root 1.85 ++maxreqs;
893 root 1.22 }
894 root 1.80
895 root 1.94 unblock_sig ();
896 root 1.80 return count;
897 root 1.22 }
898    
899     /*****************************************************************************/
900 root 1.17 /* work around various missing functions */
901    
902     #if !HAVE_PREADWRITE
903     # define pread aio_pread
904     # define pwrite aio_pwrite
905    
906     /*
907     * make our pread/pwrite safe against themselves, but not against
908     * normal read/write by using a mutex. slows down execution a lot,
909     * but that's your problem, not mine.
910     */
911 root 1.103 static mutex_t preadwritelock = X_MUTEX_INIT;
912 root 1.17
913 root 1.45 static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
914 root 1.17 {
915     ssize_t res;
916     off_t ooffset;
917    
918 root 1.103 X_LOCK (preadwritelock);
919 root 1.17 ooffset = lseek (fd, 0, SEEK_CUR);
920     lseek (fd, offset, SEEK_SET);
921     res = read (fd, buf, count);
922     lseek (fd, ooffset, SEEK_SET);
923 root 1.103 X_UNLOCK (preadwritelock);
924 root 1.17
925     return res;
926     }
927    
928 root 1.45 static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
929 root 1.17 {
930     ssize_t res;
931     off_t ooffset;
932    
933 root 1.103 X_LOCK (preadwritelock);
934 root 1.17 ooffset = lseek (fd, 0, SEEK_CUR);
935     lseek (fd, offset, SEEK_SET);
936     res = write (fd, buf, count);
937     lseek (fd, offset, SEEK_SET);
938 root 1.103 X_UNLOCK (preadwritelock);
939 root 1.17
940     return res;
941     }
942     #endif
943    
944 root 1.99 #ifndef HAVE_FUTIMES
945    
946     # define utimes(path,times) aio_utimes (path, times)
947     # define futimes(fd,times) aio_futimes (fd, times)
948    
949     int aio_utimes (const char *filename, const struct timeval times[2])
950     {
951     if (times)
952     {
953     struct utimbuf buf;
954    
955     buf.actime = times[0].tv_sec;
956     buf.modtime = times[1].tv_sec;
957    
958     return utime (filename, &buf);
959     }
960     else
961     return utime (filename, 0);
962     }
963    
964     int aio_futimes (int fd, const struct timeval tv[2])
965     {
966     errno = ENOSYS;
967     return -1;
968     }
969    
970     #endif
971    
972 root 1.17 #if !HAVE_FDATASYNC
973     # define fdatasync fsync
974     #endif
975    
976     #if !HAVE_READAHEAD
977 root 1.75 # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self)
978 root 1.17
979 root 1.75 static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self)
980 root 1.17 {
981 root 1.103 size_t todo = count;
982 root 1.65 dBUF;
983 root 1.37
984 root 1.103 while (todo > 0)
985 root 1.17 {
986 root 1.103 size_t len = todo < AIO_BUFSIZE ? todo : AIO_BUFSIZE;
987 root 1.17
988 root 1.65 pread (fd, aio_buf, len, offset);
989 root 1.17 offset += len;
990 root 1.103 todo -= len;
991 root 1.17 }
992    
993     errno = 0;
994 root 1.103 return count;
995 root 1.17 }
996 root 1.75
997 root 1.17 #endif
998    
999 root 1.37 #if !HAVE_READDIR_R
1000     # define readdir_r aio_readdir_r
1001    
1002 root 1.103 static mutex_t readdirlock = X_MUTEX_INIT;
1003 root 1.37
1004 root 1.103 static int readdir_r (DIR *dirp, X_DIRENT *ent, X_DIRENT **res)
1005 root 1.37 {
1006 root 1.103 X_DIRENT *e;
1007 root 1.37 int errorno;
1008    
1009 root 1.103 X_LOCK (readdirlock);
1010 root 1.37
1011     e = readdir (dirp);
1012     errorno = errno;
1013    
1014     if (e)
1015     {
1016     *res = ent;
1017     strcpy (ent->d_name, e->d_name);
1018     }
1019     else
1020     *res = 0;
1021    
1022 root 1.103 X_UNLOCK (readdirlock);
1023 root 1.37
1024     errno = errorno;
1025     return e ? 0 : -1;
1026     }
1027     #endif
1028    
1029 root 1.32 /* sendfile always needs emulation */
1030 root 1.71 static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count, worker *self)
1031 root 1.32 {
1032 root 1.37 ssize_t res;
1033 root 1.32
1034 root 1.37 if (!count)
1035     return 0;
1036 root 1.32
1037 root 1.35 #if HAVE_SENDFILE
1038     # if __linux
1039 root 1.37 res = sendfile (ofd, ifd, &offset, count);
1040 root 1.32
1041 root 1.35 # elif __freebsd
1042 root 1.37 /*
1043     * Of course, the freebsd sendfile is a dire hack with no thoughts
1044     * wasted on making it similar to other I/O functions.
1045     */
1046     {
1047     off_t sbytes;
1048     res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
1049    
1050     if (res < 0 && sbytes)
1051 root 1.77 /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */
1052 root 1.37 res = sbytes;
1053     }
1054 root 1.32
1055 root 1.35 # elif __hpux
1056 root 1.37 res = sendfile (ofd, ifd, offset, count, 0, 0);
1057 root 1.32
1058 root 1.35 # elif __solaris
1059 root 1.37 {
1060     struct sendfilevec vec;
1061     size_t sbytes;
1062    
1063     vec.sfv_fd = ifd;
1064     vec.sfv_flag = 0;
1065     vec.sfv_off = offset;
1066     vec.sfv_len = count;
1067    
1068     res = sendfilev (ofd, &vec, 1, &sbytes);
1069    
1070     if (res < 0 && sbytes)
1071     res = sbytes;
1072     }
1073 root 1.35
1074 root 1.38 # endif
1075     #else
1076 root 1.37 res = -1;
1077     errno = ENOSYS;
1078 root 1.32 #endif
1079    
1080 root 1.37 if (res < 0
1081     && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
1082 root 1.35 #if __solaris
1083 root 1.37 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
1084 root 1.35 #endif
1085 root 1.37 )
1086     )
1087     {
1088     /* emulate sendfile. this is a major pain in the ass */
1089 root 1.65 dBUF;
1090    
1091 root 1.37 res = 0;
1092    
1093 root 1.38 while (count)
1094 root 1.37 {
1095     ssize_t cnt;
1096    
1097 root 1.65 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
1098 root 1.32
1099 root 1.37 if (cnt <= 0)
1100     {
1101     if (cnt && !res) res = -1;
1102     break;
1103     }
1104    
1105 root 1.65 cnt = write (ofd, aio_buf, cnt);
1106 root 1.37
1107     if (cnt <= 0)
1108     {
1109     if (cnt && !res) res = -1;
1110     break;
1111     }
1112    
1113     offset += cnt;
1114     res += cnt;
1115 root 1.38 count -= cnt;
1116 root 1.37 }
1117     }
1118    
1119     return res;
1120     }
1121    
1122     /* read a full directory */
1123 root 1.71 static void scandir_ (aio_req req, worker *self)
1124 root 1.37 {
1125 root 1.65 DIR *dirp;
1126 root 1.37 union
1127     {
1128 root 1.103 X_DIRENT d;
1129     char b [offsetof (X_DIRENT, d_name) + NAME_MAX + 1];
1130 root 1.65 } *u;
1131 root 1.103 X_DIRENT *entp;
1132 root 1.37 char *name, *names;
1133     int memlen = 4096;
1134     int memofs = 0;
1135     int res = 0;
1136    
1137 root 1.103 X_LOCK (wrklock);
1138 root 1.86 self->dirp = dirp = opendir (req->ptr1);
1139 root 1.71 self->dbuf = u = malloc (sizeof (*u));
1140 root 1.87 req->flags |= FLAG_PTR2_FREE;
1141 root 1.86 req->ptr2 = names = malloc (memlen);
1142 root 1.103 X_UNLOCK (wrklock);
1143 root 1.37
1144 root 1.71 if (dirp && u && names)
1145 root 1.65 for (;;)
1146     {
1147     errno = 0;
1148     readdir_r (dirp, &u->d, &entp);
1149 root 1.37
1150 root 1.65 if (!entp)
1151     break;
1152 root 1.37
1153 root 1.65 name = entp->d_name;
1154 root 1.37
1155 root 1.65 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1156     {
1157     int len = strlen (name) + 1;
1158 root 1.37
1159 root 1.65 res++;
1160 root 1.37
1161 root 1.65 while (memofs + len > memlen)
1162     {
1163     memlen *= 2;
1164 root 1.103 X_LOCK (wrklock);
1165 root 1.86 req->ptr2 = names = realloc (names, memlen);
1166 root 1.103 X_UNLOCK (wrklock);
1167 root 1.71
1168 root 1.65 if (!names)
1169     break;
1170     }
1171 root 1.37
1172 root 1.65 memcpy (names + memofs, name, len);
1173     memofs += len;
1174     }
1175     }
1176 root 1.37
1177 root 1.71 if (errno)
1178     res = -1;
1179    
1180     req->result = res;
1181 root 1.32 }
1182    
1183 root 1.22 /*****************************************************************************/
1184    
1185 root 1.103 X_THREAD_PROC (aio_proc)
1186 root 1.1 {
1187     aio_req req;
1188 root 1.84 struct timespec ts;
1189 root 1.71 worker *self = (worker *)thr_arg;
1190 root 1.1
1191 root 1.103 /* try to distribute timeouts somewhat randomly */
1192     ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1193 root 1.84
1194 root 1.80 for (;;)
1195 root 1.1 {
1196 root 1.84 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1197    
1198 root 1.103 X_LOCK (reqlock);
1199 root 1.3
1200     for (;;)
1201     {
1202 root 1.71 self->req = req = reqq_shift (&req_queue);
1203 root 1.3
1204     if (req)
1205     break;
1206    
1207 root 1.84 ++idle;
1208    
1209 root 1.103 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts)
1210 root 1.84 == ETIMEDOUT)
1211     {
1212     if (idle > max_idle)
1213     {
1214     --idle;
1215 root 1.103 X_UNLOCK (reqlock);
1216     X_LOCK (wrklock);
1217 root 1.84 --started;
1218 root 1.103 X_UNLOCK (wrklock);
1219 root 1.84 goto quit;
1220     }
1221    
1222     /* we are allowed to idle, so do so without any timeout */
1223 root 1.103 X_COND_WAIT (reqwait, reqlock);
1224 root 1.84 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1225     }
1226    
1227     --idle;
1228 root 1.3 }
1229    
1230 root 1.79 --nready;
1231    
1232 root 1.103 X_UNLOCK (reqlock);
1233 root 1.3
1234 root 1.1 errno = 0; /* strictly unnecessary */
1235    
1236 root 1.58 if (!(req->flags & FLAG_CANCELLED))
1237 root 1.80 switch (req->type)
1238 root 1.43 {
1239 root 1.101 case REQ_READ: req->result = req->offs >= 0
1240     ? pread (req->int1, req->ptr1, req->size, req->offs)
1241     : read (req->int1, req->ptr1, req->size); break;
1242     case REQ_WRITE: req->result = req->offs >= 0
1243     ? pwrite (req->int1, req->ptr1, req->size, req->offs)
1244     : write (req->int1, req->ptr1, req->size); break;
1245 root 1.3
1246 root 1.86 case REQ_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
1247     case REQ_SENDFILE: req->result = sendfile_ (req->int1, req->int2, req->offs, req->size, self); break;
1248 root 1.16
1249 root 1.87 case REQ_STAT: req->result = stat (req->ptr1, (Stat_t *)req->ptr2); break;
1250     case REQ_LSTAT: req->result = lstat (req->ptr1, (Stat_t *)req->ptr2); break;
1251     case REQ_FSTAT: req->result = fstat (req->int1, (Stat_t *)req->ptr2); break;
1252 root 1.86
1253 root 1.99 case REQ_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break;
1254     case REQ_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break;
1255     case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break;
1256     case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break;
1257 root 1.103 case REQ_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break;
1258     case REQ_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
1259 root 1.99
1260 root 1.86 case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break;
1261 root 1.109 case REQ_CLOSE: req->result = close (req->int1); break;
1262 root 1.86 case REQ_UNLINK: req->result = unlink (req->ptr1); break;
1263     case REQ_RMDIR: req->result = rmdir (req->ptr1); break;
1264 root 1.97 case REQ_MKDIR: req->result = mkdir (req->ptr1, req->mode); break;
1265 root 1.86 case REQ_RENAME: req->result = rename (req->ptr2, req->ptr1); break;
1266     case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break;
1267     case REQ_SYMLINK: req->result = symlink (req->ptr2, req->ptr1); break;
1268     case REQ_MKNOD: req->result = mknod (req->ptr2, req->mode, (dev_t)req->offs); break;
1269     case REQ_READLINK: req->result = readlink (req->ptr2, req->ptr1, NAME_MAX); break;
1270 root 1.43
1271 root 1.110 case REQ_SYNC: req->result = 0; sync (); break;
1272     case REQ_FSYNC: req->result = fsync (req->int1); break;
1273 root 1.86 case REQ_FDATASYNC: req->result = fdatasync (req->int1); break;
1274 root 1.110
1275 root 1.71 case REQ_READDIR: scandir_ (req, self); break;
1276 root 1.1
1277 root 1.69 case REQ_BUSY:
1278 root 1.103 #ifdef _WIN32
1279     Sleep (req->nv1 * 1000.);
1280     #else
1281 root 1.45 {
1282     struct timeval tv;
1283    
1284 root 1.99 tv.tv_sec = req->nv1;
1285 root 1.103 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.;
1286 root 1.45
1287     req->result = select (0, 0, 0, 0, &tv);
1288     }
1289 root 1.103 #endif
1290     break;
1291 root 1.45
1292 root 1.99 case REQ_UTIME:
1293     case REQ_FUTIME:
1294     {
1295     struct timeval tv[2];
1296     struct timeval *times;
1297    
1298     if (req->nv1 != -1. || req->nv2 != -1.)
1299     {
1300     tv[0].tv_sec = req->nv1;
1301     tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.;
1302     tv[1].tv_sec = req->nv2;
1303     tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.;
1304    
1305     times = tv;
1306     }
1307     else
1308     times = 0;
1309    
1310    
1311     req->result = req->type == REQ_FUTIME
1312     ? futimes (req->int1, times)
1313     : utimes (req->ptr1, times);
1314     }
1315    
1316 root 1.55 case REQ_GROUP:
1317     case REQ_NOP:
1318 root 1.80 break;
1319    
1320 root 1.43 case REQ_QUIT:
1321 root 1.84 goto quit;
1322 root 1.1
1323 root 1.43 default:
1324 root 1.99 req->result = -1;
1325 root 1.43 break;
1326     }
1327 root 1.1
1328     req->errorno = errno;
1329 root 1.3
1330 root 1.103 X_LOCK (reslock);
1331 root 1.3
1332 root 1.79 ++npending;
1333    
1334 root 1.67 if (!reqq_push (&res_queue, req))
1335 root 1.94 {
1336     /* write a dummy byte to the pipe so fh becomes ready */
1337 root 1.105 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
1338 root 1.94
1339     /* optionally signal the main thread asynchronously */
1340     if (main_sig)
1341     pthread_kill (main_tid, main_sig);
1342     }
1343 root 1.3
1344 root 1.71 self->req = 0;
1345     worker_clear (self);
1346    
1347 root 1.103 X_UNLOCK (reslock);
1348 root 1.1 }
1349 root 1.84
1350     quit:
1351 root 1.103 X_LOCK (wrklock);
1352 root 1.84 worker_free (self);
1353 root 1.103 X_UNLOCK (wrklock);
1354 root 1.84
1355     return 0;
1356 root 1.1 }
1357    
1358 root 1.37 /*****************************************************************************/
1359    
1360     static void atfork_prepare (void)
1361     {
1362 root 1.103 X_LOCK (wrklock);
1363     X_LOCK (reqlock);
1364     X_LOCK (reslock);
1365 root 1.37 #if !HAVE_PREADWRITE
1366 root 1.103 X_LOCK (preadwritelock);
1367 root 1.37 #endif
1368     #if !HAVE_READDIR_R
1369 root 1.103 X_LOCK (readdirlock);
1370 root 1.37 #endif
1371     }
1372    
1373     static void atfork_parent (void)
1374     {
1375     #if !HAVE_READDIR_R
1376 root 1.103 X_UNLOCK (readdirlock);
1377 root 1.37 #endif
1378     #if !HAVE_PREADWRITE
1379 root 1.103 X_UNLOCK (preadwritelock);
1380 root 1.37 #endif
1381 root 1.103 X_UNLOCK (reslock);
1382     X_UNLOCK (reqlock);
1383     X_UNLOCK (wrklock);
1384 root 1.37 }
1385    
1386     static void atfork_child (void)
1387     {
1388     aio_req prv;
1389    
1390 root 1.67 while (prv = reqq_shift (&req_queue))
1391 root 1.101 req_destroy (prv);
1392 root 1.37
1393 root 1.67 while (prv = reqq_shift (&res_queue))
1394 root 1.101 req_destroy (prv);
1395 root 1.71
1396     while (wrk_first.next != &wrk_first)
1397     {
1398     worker *wrk = wrk_first.next;
1399    
1400     if (wrk->req)
1401 root 1.101 req_destroy (wrk->req);
1402 root 1.71
1403     worker_clear (wrk);
1404     worker_free (wrk);
1405     }
1406    
1407 root 1.84 started = 0;
1408     idle = 0;
1409     nreqs = 0;
1410     nready = 0;
1411     npending = 0;
1412 root 1.71
1413 root 1.106 create_respipe ();
1414 root 1.37
1415     atfork_parent ();
1416     }
1417    
1418 root 1.22 #define dREQ \
1419     aio_req req; \
1420 root 1.60 int req_pri = next_pri; \
1421     next_pri = DEFAULT_PRI + PRI_BIAS; \
1422 root 1.22 \
1423     if (SvOK (callback) && !SvROK (callback)) \
1424 root 1.43 croak ("callback must be undef or of reference type"); \
1425 root 1.22 \
1426     Newz (0, req, 1, aio_cb); \
1427     if (!req) \
1428     croak ("out of memory during aio_req allocation"); \
1429     \
1430 root 1.60 req->callback = newSVsv (callback); \
1431     req->pri = req_pri
1432 root 1.43
1433     #define REQ_SEND \
1434     req_send (req); \
1435     \
1436     if (GIMME_V != G_VOID) \
1437 root 1.44 XPUSHs (req_sv (req, AIO_REQ_KLASS));
1438 root 1.22
1439 root 1.1 MODULE = IO::AIO PACKAGE = IO::AIO
1440    
1441 root 1.8 PROTOTYPES: ENABLE
1442    
1443 root 1.1 BOOT:
1444     {
1445 root 1.107 stash = gv_stashpv ("IO::AIO", 1);
1446 root 1.82
1447 root 1.41 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
1448     newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
1449     newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
1450 root 1.81 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
1451     newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
1452 root 1.103 #ifdef _WIN32
1453     X_MUTEX_CHECK (wrklock);
1454     X_MUTEX_CHECK (reslock);
1455     X_MUTEX_CHECK (reqlock);
1456     X_MUTEX_CHECK (reqwait);
1457     X_MUTEX_CHECK (preadwritelock);
1458     X_MUTEX_CHECK (readdirlock);
1459 root 1.104
1460     X_COND_CHECK (reqwait);
1461 root 1.103 #else
1462 root 1.82 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
1463 root 1.94 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO));
1464 root 1.103 #endif
1465    
1466 root 1.106 create_respipe ();
1467 root 1.41
1468 root 1.103 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
1469 root 1.1 }
1470    
1471     void
1472 root 1.85 max_poll_reqs (int nreqs)
1473     PROTOTYPE: $
1474     CODE:
1475     max_poll_reqs = nreqs;
1476    
1477     void
1478     max_poll_time (double nseconds)
1479     PROTOTYPE: $
1480     CODE:
1481     max_poll_time = nseconds * AIO_TICKS;
1482    
1483     void
1484 root 1.78 min_parallel (int nthreads)
1485 root 1.1 PROTOTYPE: $
1486    
1487     void
1488 root 1.78 max_parallel (int nthreads)
1489     PROTOTYPE: $
1490    
1491 root 1.85 void
1492     max_idle (int nthreads)
1493     PROTOTYPE: $
1494     CODE:
1495     set_max_idle (nthreads);
1496    
1497 root 1.78 int
1498     max_outstanding (int maxreqs)
1499 root 1.1 PROTOTYPE: $
1500 root 1.78 CODE:
1501     RETVAL = max_outstanding;
1502     max_outstanding = maxreqs;
1503     OUTPUT:
1504     RETVAL
1505 root 1.1
1506     void
1507 root 1.99 aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef)
1508 root 1.8 PROTOTYPE: $$$;$
1509 root 1.43 PPCODE:
1510 root 1.1 {
1511 root 1.22 dREQ;
1512 root 1.1
1513     req->type = REQ_OPEN;
1514 root 1.86 req->sv1 = newSVsv (pathname);
1515 root 1.89 req->ptr1 = SvPVbyte_nolen (req->sv1);
1516 root 1.86 req->int1 = flags;
1517 root 1.1 req->mode = mode;
1518    
1519 root 1.43 REQ_SEND;
1520 root 1.1 }
1521    
1522     void
1523 root 1.107 aio_fsync (SV *fh, SV *callback=&PL_sv_undef)
1524 root 1.8 PROTOTYPE: $;$
1525 root 1.1 ALIAS:
1526     aio_fsync = REQ_FSYNC
1527     aio_fdatasync = REQ_FDATASYNC
1528 root 1.43 PPCODE:
1529 root 1.1 {
1530 root 1.22 dREQ;
1531 root 1.1
1532     req->type = ix;
1533 root 1.99 req->sv1 = newSVsv (fh);
1534 root 1.86 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1535 root 1.1
1536 root 1.43 REQ_SEND (req);
1537 root 1.1 }
1538    
1539 root 1.109 int
1540     _dup (int fd)
1541     PROTOTYPE: $
1542     CODE:
1543     RETVAL = dup (fd);
1544     OUTPUT:
1545     RETVAL
1546    
1547 root 1.1 void
1548 root 1.109 _aio_close (int fd, SV *callback=&PL_sv_undef)
1549 root 1.107 PROTOTYPE: $;$
1550     PPCODE:
1551     {
1552 root 1.109 dREQ;
1553 root 1.107
1554 root 1.109 req->type = REQ_CLOSE;
1555     req->int1 = fd;
1556 root 1.107
1557 root 1.109 REQ_SEND (req);
1558 root 1.107 }
1559    
1560     void
1561 root 1.102 aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef)
1562 root 1.13 ALIAS:
1563     aio_read = REQ_READ
1564     aio_write = REQ_WRITE
1565 root 1.8 PROTOTYPE: $$$$$;$
1566 root 1.43 PPCODE:
1567 root 1.13 {
1568     STRLEN svlen;
1569 root 1.21 char *svptr = SvPVbyte (data, svlen);
1570 root 1.102 UV len = SvUV (length);
1571 root 1.13
1572     SvUPGRADE (data, SVt_PV);
1573     SvPOK_on (data);
1574 root 1.1
1575 root 1.13 if (dataoffset < 0)
1576     dataoffset += svlen;
1577    
1578     if (dataoffset < 0 || dataoffset > svlen)
1579 root 1.102 croak ("dataoffset outside of data scalar");
1580 root 1.13
1581     if (ix == REQ_WRITE)
1582     {
1583     /* write: check length and adjust. */
1584 root 1.102 if (!SvOK (length) || len + dataoffset > svlen)
1585     len = svlen - dataoffset;
1586 root 1.13 }
1587     else
1588     {
1589     /* read: grow scalar as necessary */
1590 root 1.102 svptr = SvGROW (data, len + dataoffset + 1);
1591 root 1.13 }
1592    
1593 root 1.102 if (len < 0)
1594 root 1.13 croak ("length must not be negative");
1595    
1596 root 1.22 {
1597     dREQ;
1598 root 1.13
1599 root 1.22 req->type = ix;
1600 root 1.99 req->sv1 = newSVsv (fh);
1601 root 1.86 req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
1602     : IoOFP (sv_2io (fh)));
1603 root 1.101 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1604 root 1.102 req->size = len;
1605 root 1.99 req->sv2 = SvREFCNT_inc (data);
1606 root 1.86 req->ptr1 = (char *)svptr + dataoffset;
1607     req->stroffset = dataoffset;
1608 root 1.13
1609 root 1.28 if (!SvREADONLY (data))
1610     {
1611     SvREADONLY_on (data);
1612 root 1.100 req->flags |= FLAG_SV2_RO_OFF;
1613 root 1.28 }
1614    
1615 root 1.43 REQ_SEND;
1616 root 1.22 }
1617 root 1.13 }
1618 root 1.1
1619     void
1620 root 1.99 aio_readlink (SV8 *path, SV *callback=&PL_sv_undef)
1621 root 1.86 PROTOTYPE: $$;$
1622     PPCODE:
1623     {
1624     SV *data;
1625     dREQ;
1626    
1627     data = newSV (NAME_MAX);
1628     SvPOK_on (data);
1629    
1630     req->type = REQ_READLINK;
1631 root 1.99 req->sv1 = newSVsv (path);
1632     req->ptr2 = SvPVbyte_nolen (req->sv1);
1633     req->sv2 = data;
1634 root 1.86 req->ptr1 = SvPVbyte_nolen (data);
1635    
1636     REQ_SEND;
1637     }
1638    
1639     void
1640 root 1.101 aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef)
1641 root 1.32 PROTOTYPE: $$$$;$
1642 root 1.43 PPCODE:
1643 root 1.32 {
1644     dREQ;
1645    
1646     req->type = REQ_SENDFILE;
1647 root 1.99 req->sv1 = newSVsv (out_fh);
1648 root 1.86 req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
1649     req->sv2 = newSVsv (in_fh);
1650     req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
1651 root 1.101 req->offs = SvVAL64 (in_offset);
1652 root 1.86 req->size = length;
1653 root 1.32
1654 root 1.43 REQ_SEND;
1655 root 1.32 }
1656    
1657     void
1658 root 1.101 aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef)
1659 root 1.8 PROTOTYPE: $$$;$
1660 root 1.43 PPCODE:
1661 root 1.1 {
1662 root 1.22 dREQ;
1663 root 1.1
1664     req->type = REQ_READAHEAD;
1665 root 1.99 req->sv1 = newSVsv (fh);
1666 root 1.86 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1667 root 1.101 req->offs = SvVAL64 (offset);
1668 root 1.86 req->size = length;
1669 root 1.1
1670 root 1.43 REQ_SEND;
1671 root 1.1 }
1672    
1673     void
1674 root 1.99 aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef)
1675 root 1.1 ALIAS:
1676 root 1.8 aio_stat = REQ_STAT
1677     aio_lstat = REQ_LSTAT
1678 root 1.43 PPCODE:
1679 root 1.1 {
1680 root 1.22 dREQ;
1681 root 1.1
1682 root 1.87 req->ptr2 = malloc (sizeof (Stat_t));
1683     if (!req->ptr2)
1684 root 1.27 {
1685 root 1.101 req_destroy (req);
1686 root 1.88 croak ("out of memory during aio_stat statdata allocation");
1687 root 1.27 }
1688 root 1.1
1689 root 1.87 req->flags |= FLAG_PTR2_FREE;
1690 root 1.99 req->sv1 = newSVsv (fh_or_path);
1691 root 1.87
1692 root 1.1 if (SvPOK (fh_or_path))
1693     {
1694 root 1.8 req->type = ix;
1695 root 1.89 req->ptr1 = SvPVbyte_nolen (req->sv1);
1696 root 1.1 }
1697     else
1698     {
1699     req->type = REQ_FSTAT;
1700 root 1.86 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1701 root 1.1 }
1702    
1703 root 1.43 REQ_SEND;
1704 root 1.1 }
1705    
1706     void
1707 root 1.99 aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef)
1708     PPCODE:
1709     {
1710     dREQ;
1711    
1712     req->nv1 = SvOK (atime) ? SvNV (atime) : -1.;
1713     req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.;
1714     req->sv1 = newSVsv (fh_or_path);
1715    
1716     if (SvPOK (fh_or_path))
1717     {
1718     req->type = REQ_UTIME;
1719     req->ptr1 = SvPVbyte_nolen (req->sv1);
1720     }
1721     else
1722     {
1723     req->type = REQ_FUTIME;
1724     req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1725     }
1726    
1727     REQ_SEND;
1728     }
1729    
1730     void
1731 root 1.103 aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef)
1732     PPCODE:
1733     {
1734     dREQ;
1735    
1736     req->sv1 = newSVsv (fh_or_path);
1737     req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1738    
1739     if (SvPOK (fh_or_path))
1740     {
1741     req->type = REQ_TRUNCATE;
1742     req->ptr1 = SvPVbyte_nolen (req->sv1);
1743     }
1744     else
1745     {
1746     req->type = REQ_FTRUNCATE;
1747     req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1748     }
1749    
1750     REQ_SEND;
1751     }
1752    
1753     void
1754 root 1.99 aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef)
1755     PPCODE:
1756     {
1757     dREQ;
1758    
1759     req->mode = mode;
1760     req->sv1 = newSVsv (fh_or_path);
1761    
1762     if (SvPOK (fh_or_path))
1763     {
1764     req->type = REQ_CHMOD;
1765     req->ptr1 = SvPVbyte_nolen (req->sv1);
1766     }
1767     else
1768     {
1769     req->type = REQ_FCHMOD;
1770     req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1771     }
1772    
1773     REQ_SEND;
1774     }
1775    
1776     void
1777     aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef)
1778     PPCODE:
1779     {
1780     dREQ;
1781    
1782     req->int2 = SvOK (uid) ? SvIV (uid) : -1;
1783     req->int3 = SvOK (gid) ? SvIV (gid) : -1;
1784     req->sv1 = newSVsv (fh_or_path);
1785    
1786     if (SvPOK (fh_or_path))
1787     {
1788     req->type = REQ_CHOWN;
1789     req->ptr1 = SvPVbyte_nolen (req->sv1);
1790     }
1791     else
1792     {
1793     req->type = REQ_FCHOWN;
1794     req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1795     }
1796    
1797     REQ_SEND;
1798     }
1799    
1800     void
1801     aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef)
1802 root 1.22 ALIAS:
1803 root 1.40 aio_unlink = REQ_UNLINK
1804     aio_rmdir = REQ_RMDIR
1805     aio_readdir = REQ_READDIR
1806 root 1.43 PPCODE:
1807 root 1.1 {
1808 root 1.22 dREQ;
1809 root 1.1
1810 root 1.22 req->type = ix;
1811 root 1.86 req->sv1 = newSVsv (pathname);
1812 root 1.89 req->ptr1 = SvPVbyte_nolen (req->sv1);
1813 root 1.87
1814 root 1.43 REQ_SEND;
1815 root 1.22 }
1816    
1817     void
1818 root 1.99 aio_mkdir (SV8 *pathname, int mode, SV *callback=&PL_sv_undef)
1819 root 1.97 PPCODE:
1820     {
1821     dREQ;
1822    
1823     req->type = REQ_MKDIR;
1824     req->sv1 = newSVsv (pathname);
1825     req->ptr1 = SvPVbyte_nolen (req->sv1);
1826     req->mode = mode;
1827    
1828     REQ_SEND;
1829     }
1830    
1831     void
1832 root 1.99 aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef)
1833 root 1.40 ALIAS:
1834     aio_link = REQ_LINK
1835     aio_symlink = REQ_SYMLINK
1836     aio_rename = REQ_RENAME
1837 root 1.43 PPCODE:
1838 root 1.22 {
1839     dREQ;
1840 root 1.1
1841 root 1.40 req->type = ix;
1842 root 1.99 req->sv2 = newSVsv (oldpath);
1843     req->ptr2 = SvPVbyte_nolen (req->sv2);
1844 root 1.86 req->sv1 = newSVsv (newpath);
1845 root 1.89 req->ptr1 = SvPVbyte_nolen (req->sv1);
1846 root 1.1
1847 root 1.43 REQ_SEND;
1848 root 1.1 }
1849    
1850 root 1.42 void
1851 root 1.99 aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef)
1852 root 1.81 PPCODE:
1853     {
1854     dREQ;
1855    
1856     req->type = REQ_MKNOD;
1857 root 1.86 req->sv1 = newSVsv (pathname);
1858 root 1.89 req->ptr1 = SvPVbyte_nolen (req->sv1);
1859 root 1.81 req->mode = (mode_t)mode;
1860 root 1.86 req->offs = dev;
1861 root 1.81
1862     REQ_SEND;
1863     }
1864    
1865     void
1866 root 1.99 aio_busy (double delay, SV *callback=&PL_sv_undef)
1867 root 1.45 PPCODE:
1868     {
1869     dREQ;
1870    
1871 root 1.69 req->type = REQ_BUSY;
1872 root 1.99 req->nv1 = delay < 0. ? 0. : delay;
1873 root 1.45
1874     REQ_SEND;
1875     }
1876    
1877     void
1878 root 1.99 aio_group (SV *callback=&PL_sv_undef)
1879 root 1.46 PROTOTYPE: ;$
1880 root 1.44 PPCODE:
1881 root 1.42 {
1882 root 1.44 dREQ;
1883 root 1.60
1884 root 1.44 req->type = REQ_GROUP;
1885 root 1.86
1886 root 1.45 req_send (req);
1887 root 1.44 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1888 root 1.42 }
1889    
1890 root 1.6 void
1891 root 1.99 aio_nop (SV *callback=&PL_sv_undef)
1892 root 1.110 ALIAS:
1893     aio_nop = REQ_NOP
1894     aio_sync = REQ_SYNC
1895 root 1.54 PPCODE:
1896     {
1897     dREQ;
1898    
1899 root 1.110 req->type = ix;
1900 root 1.54
1901     REQ_SEND;
1902     }
1903    
1904 root 1.79 int
1905     aioreq_pri (int pri = 0)
1906     PROTOTYPE: ;$
1907     CODE:
1908     RETVAL = next_pri - PRI_BIAS;
1909     if (items > 0)
1910     {
1911     if (pri < PRI_MIN) pri = PRI_MIN;
1912     if (pri > PRI_MAX) pri = PRI_MAX;
1913     next_pri = pri + PRI_BIAS;
1914     }
1915     OUTPUT:
1916     RETVAL
1917 root 1.68
1918     void
1919     aioreq_nice (int nice = 0)
1920 root 1.79 CODE:
1921     nice = next_pri - nice;
1922     if (nice < PRI_MIN) nice = PRI_MIN;
1923     if (nice > PRI_MAX) nice = PRI_MAX;
1924     next_pri = nice + PRI_BIAS;
1925 root 1.60
1926 root 1.54 void
1927 root 1.40 flush ()
1928 root 1.6 PROTOTYPE:
1929     CODE:
1930     while (nreqs)
1931     {
1932     poll_wait ();
1933 root 1.91 poll_cb ();
1934 root 1.6 }
1935    
1936 root 1.91 int
1937 root 1.7 poll()
1938     PROTOTYPE:
1939     CODE:
1940 root 1.92 poll_wait ();
1941     RETVAL = poll_cb ();
1942 root 1.91 OUTPUT:
1943     RETVAL
1944 root 1.7
1945 root 1.1 int
1946     poll_fileno()
1947     PROTOTYPE:
1948     CODE:
1949 root 1.3 RETVAL = respipe [0];
1950 root 1.1 OUTPUT:
1951     RETVAL
1952    
1953     int
1954     poll_cb(...)
1955     PROTOTYPE:
1956     CODE:
1957 root 1.85 RETVAL = poll_cb ();
1958 root 1.1 OUTPUT:
1959     RETVAL
1960    
1961     void
1962     poll_wait()
1963     PROTOTYPE:
1964     CODE:
1965 root 1.92 poll_wait ();
1966 root 1.1
1967 root 1.94 void
1968     setsig (int signum = SIGIO)
1969     PROTOTYPE: ;$
1970     CODE:
1971     {
1972     if (block_sig_level)
1973     croak ("cannot call IO::AIO::setsig from within aio_block/callback");
1974    
1975 root 1.103 X_LOCK (reslock);
1976 root 1.94 main_tid = pthread_self ();
1977     main_sig = signum;
1978 root 1.103 X_UNLOCK (reslock);
1979 root 1.94
1980     if (main_sig && npending)
1981     pthread_kill (main_tid, main_sig);
1982     }
1983    
1984     void
1985     aio_block (SV *cb)
1986     PROTOTYPE: &
1987     PPCODE:
1988     {
1989     int count;
1990    
1991     block_sig ();
1992     PUSHMARK (SP);
1993     PUTBACK;
1994     count = call_sv (cb, GIMME_V | G_NOARGS | G_EVAL);
1995     unblock_sig ();
1996    
1997     if (SvTRUE (ERRSV))
1998     croak (0);
1999    
2000     XSRETURN (count);
2001     }
2002    
2003 root 1.1 int
2004     nreqs()
2005     PROTOTYPE:
2006     CODE:
2007     RETVAL = nreqs;
2008     OUTPUT:
2009     RETVAL
2010    
2011 root 1.79 int
2012     nready()
2013     PROTOTYPE:
2014     CODE:
2015 root 1.80 RETVAL = get_nready ();
2016 root 1.79 OUTPUT:
2017     RETVAL
2018    
2019     int
2020     npending()
2021     PROTOTYPE:
2022     CODE:
2023 root 1.80 RETVAL = get_npending ();
2024 root 1.79 OUTPUT:
2025     RETVAL
2026    
2027 root 1.85 int
2028     nthreads()
2029     PROTOTYPE:
2030     CODE:
2031 root 1.103 if (WORDACCESS_UNSAFE) X_LOCK (wrklock);
2032 root 1.85 RETVAL = started;
2033 root 1.103 if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock);
2034 root 1.85 OUTPUT:
2035     RETVAL
2036    
2037 root 1.48 PROTOTYPES: DISABLE
2038    
2039 root 1.44 MODULE = IO::AIO PACKAGE = IO::AIO::REQ
2040 root 1.43
2041     void
2042     cancel (aio_req_ornot req)
2043     CODE:
2044 root 1.45 req_cancel (req);
2045    
2046 root 1.56 void
2047 root 1.59 cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
2048 root 1.56 CODE:
2049     SvREFCNT_dec (req->callback);
2050     req->callback = newSVsv (callback);
2051    
2052 root 1.45 MODULE = IO::AIO PACKAGE = IO::AIO::GRP
2053    
2054     void
2055     add (aio_req grp, ...)
2056     PPCODE:
2057     {
2058     int i;
2059 root 1.53 aio_req req;
2060 root 1.45
2061 root 1.94 if (main_sig && !block_sig_level)
2062     croak ("aio_group->add called outside aio_block/callback context while IO::AIO::setsig is in use");
2063    
2064 root 1.86 if (grp->int1 == 2)
2065 root 1.49 croak ("cannot add requests to IO::AIO::GRP after the group finished");
2066    
2067 root 1.45 for (i = 1; i < items; ++i )
2068     {
2069 root 1.46 if (GIMME_V != G_VOID)
2070     XPUSHs (sv_2mortal (newSVsv (ST (i))));
2071    
2072 root 1.53 req = SvAIO_REQ (ST (i));
2073 root 1.45
2074 root 1.46 if (req)
2075     {
2076 root 1.86 ++grp->size;
2077 root 1.49 req->grp = grp;
2078    
2079     req->grp_prev = 0;
2080     req->grp_next = grp->grp_first;
2081 root 1.45
2082 root 1.49 if (grp->grp_first)
2083     grp->grp_first->grp_prev = req;
2084    
2085     grp->grp_first = req;
2086 root 1.46 }
2087 root 1.45 }
2088     }
2089 root 1.43
2090 root 1.48 void
2091 root 1.72 cancel_subs (aio_req_ornot req)
2092     CODE:
2093     req_cancel_subs (req);
2094    
2095     void
2096 root 1.51 result (aio_req grp, ...)
2097     CODE:
2098     {
2099     int i;
2100 root 1.79 AV *av;
2101    
2102     grp->errorno = errno;
2103    
2104     av = newAV ();
2105 root 1.51
2106     for (i = 1; i < items; ++i )
2107     av_push (av, newSVsv (ST (i)));
2108    
2109 root 1.86 SvREFCNT_dec (grp->sv1);
2110     grp->sv1 = (SV *)av;
2111 root 1.51 }
2112    
2113     void
2114 root 1.79 errno (aio_req grp, int errorno = errno)
2115     CODE:
2116     grp->errorno = errorno;
2117    
2118     void
2119 root 1.67 limit (aio_req grp, int limit)
2120 root 1.49 CODE:
2121 root 1.86 grp->int2 = limit;
2122 root 1.49 aio_grp_feed (grp);
2123    
2124     void
2125 root 1.56 feed (aio_req grp, SV *callback=&PL_sv_undef)
2126 root 1.49 CODE:
2127     {
2128 root 1.86 SvREFCNT_dec (grp->sv2);
2129     grp->sv2 = newSVsv (callback);
2130 root 1.49
2131 root 1.86 if (grp->int2 <= 0)
2132     grp->int2 = 2;
2133 root 1.49
2134     aio_grp_feed (grp);
2135     }
2136