ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
Revision: 1.111
Committed: Sun Mar 30 06:31:49 2008 UTC (16 years, 1 month ago) by root
Branch: MAIN
CVS Tags: rel-2_6
Changes since 1.110: +10 -11 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 root 1.103 {//D
1188 root 1.1 aio_req req;
1189 root 1.84 struct timespec ts;
1190 root 1.71 worker *self = (worker *)thr_arg;
1191 root 1.1
1192 root 1.103 /* try to distribute timeouts somewhat randomly */
1193     ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1194 root 1.84
1195 root 1.80 for (;;)
1196 root 1.1 {
1197 root 1.84 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1198    
1199 root 1.103 X_LOCK (reqlock);
1200 root 1.3
1201     for (;;)
1202     {
1203 root 1.71 self->req = req = reqq_shift (&req_queue);
1204 root 1.3
1205     if (req)
1206     break;
1207    
1208 root 1.84 ++idle;
1209    
1210 root 1.103 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts)
1211 root 1.84 == ETIMEDOUT)
1212     {
1213     if (idle > max_idle)
1214     {
1215     --idle;
1216 root 1.103 X_UNLOCK (reqlock);
1217     X_LOCK (wrklock);
1218 root 1.84 --started;
1219 root 1.103 X_UNLOCK (wrklock);
1220 root 1.84 goto quit;
1221     }
1222    
1223     /* we are allowed to idle, so do so without any timeout */
1224 root 1.103 X_COND_WAIT (reqwait, reqlock);
1225 root 1.84 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1226     }
1227    
1228     --idle;
1229 root 1.3 }
1230    
1231 root 1.79 --nready;
1232    
1233 root 1.103 X_UNLOCK (reqlock);
1234 root 1.3
1235 root 1.1 errno = 0; /* strictly unnecessary */
1236    
1237 root 1.58 if (!(req->flags & FLAG_CANCELLED))
1238 root 1.80 switch (req->type)
1239 root 1.43 {
1240 root 1.101 case REQ_READ: req->result = req->offs >= 0
1241     ? pread (req->int1, req->ptr1, req->size, req->offs)
1242     : read (req->int1, req->ptr1, req->size); break;
1243     case REQ_WRITE: req->result = req->offs >= 0
1244     ? pwrite (req->int1, req->ptr1, req->size, req->offs)
1245     : write (req->int1, req->ptr1, req->size); break;
1246 root 1.3
1247 root 1.86 case REQ_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
1248     case REQ_SENDFILE: req->result = sendfile_ (req->int1, req->int2, req->offs, req->size, self); break;
1249 root 1.16
1250 root 1.87 case REQ_STAT: req->result = stat (req->ptr1, (Stat_t *)req->ptr2); break;
1251     case REQ_LSTAT: req->result = lstat (req->ptr1, (Stat_t *)req->ptr2); break;
1252     case REQ_FSTAT: req->result = fstat (req->int1, (Stat_t *)req->ptr2); break;
1253 root 1.86
1254 root 1.99 case REQ_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break;
1255     case REQ_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break;
1256     case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break;
1257     case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break;
1258 root 1.103 case REQ_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break;
1259     case REQ_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
1260 root 1.99
1261 root 1.86 case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break;
1262 root 1.109 case REQ_CLOSE: req->result = close (req->int1); break;
1263 root 1.86 case REQ_UNLINK: req->result = unlink (req->ptr1); break;
1264     case REQ_RMDIR: req->result = rmdir (req->ptr1); break;
1265 root 1.97 case REQ_MKDIR: req->result = mkdir (req->ptr1, req->mode); break;
1266 root 1.86 case REQ_RENAME: req->result = rename (req->ptr2, req->ptr1); break;
1267     case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break;
1268     case REQ_SYMLINK: req->result = symlink (req->ptr2, req->ptr1); break;
1269     case REQ_MKNOD: req->result = mknod (req->ptr2, req->mode, (dev_t)req->offs); break;
1270     case REQ_READLINK: req->result = readlink (req->ptr2, req->ptr1, NAME_MAX); break;
1271 root 1.43
1272 root 1.110 case REQ_SYNC: req->result = 0; sync (); break;
1273     case REQ_FSYNC: req->result = fsync (req->int1); break;
1274 root 1.86 case REQ_FDATASYNC: req->result = fdatasync (req->int1); break;
1275 root 1.110
1276 root 1.71 case REQ_READDIR: scandir_ (req, self); break;
1277 root 1.1
1278 root 1.69 case REQ_BUSY:
1279 root 1.103 #ifdef _WIN32
1280     Sleep (req->nv1 * 1000.);
1281     #else
1282 root 1.45 {
1283     struct timeval tv;
1284    
1285 root 1.99 tv.tv_sec = req->nv1;
1286 root 1.103 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.;
1287 root 1.45
1288     req->result = select (0, 0, 0, 0, &tv);
1289     }
1290 root 1.103 #endif
1291     break;
1292 root 1.45
1293 root 1.99 case REQ_UTIME:
1294     case REQ_FUTIME:
1295     {
1296     struct timeval tv[2];
1297     struct timeval *times;
1298    
1299     if (req->nv1 != -1. || req->nv2 != -1.)
1300     {
1301     tv[0].tv_sec = req->nv1;
1302     tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.;
1303     tv[1].tv_sec = req->nv2;
1304     tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.;
1305    
1306     times = tv;
1307     }
1308     else
1309     times = 0;
1310    
1311    
1312     req->result = req->type == REQ_FUTIME
1313     ? futimes (req->int1, times)
1314     : utimes (req->ptr1, times);
1315     }
1316    
1317 root 1.55 case REQ_GROUP:
1318     case REQ_NOP:
1319 root 1.80 break;
1320    
1321 root 1.43 case REQ_QUIT:
1322 root 1.84 goto quit;
1323 root 1.1
1324 root 1.43 default:
1325 root 1.99 req->result = -1;
1326 root 1.43 break;
1327     }
1328 root 1.1
1329     req->errorno = errno;
1330 root 1.3
1331 root 1.103 X_LOCK (reslock);
1332 root 1.3
1333 root 1.79 ++npending;
1334    
1335 root 1.67 if (!reqq_push (&res_queue, req))
1336 root 1.94 {
1337     /* write a dummy byte to the pipe so fh becomes ready */
1338 root 1.105 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
1339 root 1.94
1340     /* optionally signal the main thread asynchronously */
1341     if (main_sig)
1342     pthread_kill (main_tid, main_sig);
1343     }
1344 root 1.3
1345 root 1.71 self->req = 0;
1346     worker_clear (self);
1347    
1348 root 1.103 X_UNLOCK (reslock);
1349 root 1.1 }
1350 root 1.84
1351     quit:
1352 root 1.103 X_LOCK (wrklock);
1353 root 1.84 worker_free (self);
1354 root 1.103 X_UNLOCK (wrklock);
1355 root 1.84
1356     return 0;
1357 root 1.103 }//D
1358 root 1.1 }
1359    
1360 root 1.37 /*****************************************************************************/
1361    
1362     static void atfork_prepare (void)
1363     {
1364 root 1.103 X_LOCK (wrklock);
1365     X_LOCK (reqlock);
1366     X_LOCK (reslock);
1367 root 1.37 #if !HAVE_PREADWRITE
1368 root 1.103 X_LOCK (preadwritelock);
1369 root 1.37 #endif
1370     #if !HAVE_READDIR_R
1371 root 1.103 X_LOCK (readdirlock);
1372 root 1.37 #endif
1373     }
1374    
1375     static void atfork_parent (void)
1376     {
1377     #if !HAVE_READDIR_R
1378 root 1.103 X_UNLOCK (readdirlock);
1379 root 1.37 #endif
1380     #if !HAVE_PREADWRITE
1381 root 1.103 X_UNLOCK (preadwritelock);
1382 root 1.37 #endif
1383 root 1.103 X_UNLOCK (reslock);
1384     X_UNLOCK (reqlock);
1385     X_UNLOCK (wrklock);
1386 root 1.37 }
1387    
1388     static void atfork_child (void)
1389     {
1390     aio_req prv;
1391    
1392 root 1.67 while (prv = reqq_shift (&req_queue))
1393 root 1.101 req_destroy (prv);
1394 root 1.37
1395 root 1.67 while (prv = reqq_shift (&res_queue))
1396 root 1.101 req_destroy (prv);
1397 root 1.71
1398     while (wrk_first.next != &wrk_first)
1399     {
1400     worker *wrk = wrk_first.next;
1401    
1402     if (wrk->req)
1403 root 1.101 req_destroy (wrk->req);
1404 root 1.71
1405     worker_clear (wrk);
1406     worker_free (wrk);
1407     }
1408    
1409 root 1.84 started = 0;
1410     idle = 0;
1411     nreqs = 0;
1412     nready = 0;
1413     npending = 0;
1414 root 1.71
1415 root 1.106 create_respipe ();
1416 root 1.37
1417     atfork_parent ();
1418     }
1419    
1420 root 1.22 #define dREQ \
1421     aio_req req; \
1422 root 1.60 int req_pri = next_pri; \
1423     next_pri = DEFAULT_PRI + PRI_BIAS; \
1424 root 1.22 \
1425     if (SvOK (callback) && !SvROK (callback)) \
1426 root 1.43 croak ("callback must be undef or of reference type"); \
1427 root 1.22 \
1428     Newz (0, req, 1, aio_cb); \
1429     if (!req) \
1430     croak ("out of memory during aio_req allocation"); \
1431     \
1432 root 1.60 req->callback = newSVsv (callback); \
1433     req->pri = req_pri
1434 root 1.43
1435     #define REQ_SEND \
1436     req_send (req); \
1437     \
1438     if (GIMME_V != G_VOID) \
1439 root 1.44 XPUSHs (req_sv (req, AIO_REQ_KLASS));
1440 root 1.22
1441 root 1.1 MODULE = IO::AIO PACKAGE = IO::AIO
1442    
1443 root 1.8 PROTOTYPES: ENABLE
1444    
1445 root 1.1 BOOT:
1446     {
1447 root 1.107 stash = gv_stashpv ("IO::AIO", 1);
1448 root 1.82
1449 root 1.41 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
1450     newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
1451     newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
1452 root 1.81 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
1453     newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
1454 root 1.103 #ifdef _WIN32
1455     X_MUTEX_CHECK (wrklock);
1456     X_MUTEX_CHECK (reslock);
1457     X_MUTEX_CHECK (reqlock);
1458     X_MUTEX_CHECK (reqwait);
1459     X_MUTEX_CHECK (preadwritelock);
1460     X_MUTEX_CHECK (readdirlock);
1461 root 1.104
1462     X_COND_CHECK (reqwait);
1463 root 1.103 #else
1464 root 1.82 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
1465 root 1.94 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO));
1466 root 1.103 #endif
1467    
1468 root 1.106 create_respipe ();
1469 root 1.41
1470 root 1.103 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
1471 root 1.1 }
1472    
1473     void
1474 root 1.85 max_poll_reqs (int nreqs)
1475     PROTOTYPE: $
1476     CODE:
1477     max_poll_reqs = nreqs;
1478    
1479     void
1480     max_poll_time (double nseconds)
1481     PROTOTYPE: $
1482     CODE:
1483     max_poll_time = nseconds * AIO_TICKS;
1484    
1485     void
1486 root 1.78 min_parallel (int nthreads)
1487 root 1.1 PROTOTYPE: $
1488    
1489     void
1490 root 1.78 max_parallel (int nthreads)
1491     PROTOTYPE: $
1492    
1493 root 1.85 void
1494     max_idle (int nthreads)
1495     PROTOTYPE: $
1496     CODE:
1497     set_max_idle (nthreads);
1498    
1499 root 1.78 int
1500     max_outstanding (int maxreqs)
1501 root 1.1 PROTOTYPE: $
1502 root 1.78 CODE:
1503     RETVAL = max_outstanding;
1504     max_outstanding = maxreqs;
1505     OUTPUT:
1506     RETVAL
1507 root 1.1
1508     void
1509 root 1.99 aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef)
1510 root 1.8 PROTOTYPE: $$$;$
1511 root 1.43 PPCODE:
1512 root 1.1 {
1513 root 1.22 dREQ;
1514 root 1.1
1515     req->type = REQ_OPEN;
1516 root 1.86 req->sv1 = newSVsv (pathname);
1517 root 1.89 req->ptr1 = SvPVbyte_nolen (req->sv1);
1518 root 1.86 req->int1 = flags;
1519 root 1.1 req->mode = mode;
1520    
1521 root 1.43 REQ_SEND;
1522 root 1.1 }
1523    
1524     void
1525 root 1.107 aio_fsync (SV *fh, SV *callback=&PL_sv_undef)
1526 root 1.8 PROTOTYPE: $;$
1527 root 1.1 ALIAS:
1528     aio_fsync = REQ_FSYNC
1529     aio_fdatasync = REQ_FDATASYNC
1530 root 1.43 PPCODE:
1531 root 1.1 {
1532 root 1.22 dREQ;
1533 root 1.1
1534     req->type = ix;
1535 root 1.99 req->sv1 = newSVsv (fh);
1536 root 1.86 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1537 root 1.1
1538 root 1.43 REQ_SEND (req);
1539 root 1.1 }
1540    
1541 root 1.109 int
1542     _dup (int fd)
1543     PROTOTYPE: $
1544     CODE:
1545     RETVAL = dup (fd);
1546     OUTPUT:
1547     RETVAL
1548    
1549 root 1.1 void
1550 root 1.109 _aio_close (int fd, SV *callback=&PL_sv_undef)
1551 root 1.107 PROTOTYPE: $;$
1552     PPCODE:
1553     {
1554 root 1.109 dREQ;
1555 root 1.107
1556 root 1.109 req->type = REQ_CLOSE;
1557     req->int1 = fd;
1558 root 1.107
1559 root 1.109 REQ_SEND (req);
1560 root 1.107 }
1561    
1562     void
1563 root 1.102 aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef)
1564 root 1.13 ALIAS:
1565     aio_read = REQ_READ
1566     aio_write = REQ_WRITE
1567 root 1.8 PROTOTYPE: $$$$$;$
1568 root 1.43 PPCODE:
1569 root 1.13 {
1570     STRLEN svlen;
1571 root 1.21 char *svptr = SvPVbyte (data, svlen);
1572 root 1.102 UV len = SvUV (length);
1573 root 1.13
1574     SvUPGRADE (data, SVt_PV);
1575     SvPOK_on (data);
1576 root 1.1
1577 root 1.13 if (dataoffset < 0)
1578     dataoffset += svlen;
1579    
1580     if (dataoffset < 0 || dataoffset > svlen)
1581 root 1.102 croak ("dataoffset outside of data scalar");
1582 root 1.13
1583     if (ix == REQ_WRITE)
1584     {
1585     /* write: check length and adjust. */
1586 root 1.102 if (!SvOK (length) || len + dataoffset > svlen)
1587     len = svlen - dataoffset;
1588 root 1.13 }
1589     else
1590     {
1591     /* read: grow scalar as necessary */
1592 root 1.102 svptr = SvGROW (data, len + dataoffset + 1);
1593 root 1.13 }
1594    
1595 root 1.102 if (len < 0)
1596 root 1.13 croak ("length must not be negative");
1597    
1598 root 1.22 {
1599     dREQ;
1600 root 1.13
1601 root 1.22 req->type = ix;
1602 root 1.99 req->sv1 = newSVsv (fh);
1603 root 1.86 req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
1604     : IoOFP (sv_2io (fh)));
1605 root 1.101 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1606 root 1.102 req->size = len;
1607 root 1.99 req->sv2 = SvREFCNT_inc (data);
1608 root 1.86 req->ptr1 = (char *)svptr + dataoffset;
1609     req->stroffset = dataoffset;
1610 root 1.13
1611 root 1.28 if (!SvREADONLY (data))
1612     {
1613     SvREADONLY_on (data);
1614 root 1.100 req->flags |= FLAG_SV2_RO_OFF;
1615 root 1.28 }
1616    
1617 root 1.43 REQ_SEND;
1618 root 1.22 }
1619 root 1.13 }
1620 root 1.1
1621     void
1622 root 1.99 aio_readlink (SV8 *path, SV *callback=&PL_sv_undef)
1623 root 1.86 PROTOTYPE: $$;$
1624     PPCODE:
1625     {
1626     SV *data;
1627     dREQ;
1628    
1629     data = newSV (NAME_MAX);
1630     SvPOK_on (data);
1631    
1632     req->type = REQ_READLINK;
1633 root 1.99 req->sv1 = newSVsv (path);
1634     req->ptr2 = SvPVbyte_nolen (req->sv1);
1635     req->sv2 = data;
1636 root 1.86 req->ptr1 = SvPVbyte_nolen (data);
1637    
1638     REQ_SEND;
1639     }
1640    
1641     void
1642 root 1.101 aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef)
1643 root 1.32 PROTOTYPE: $$$$;$
1644 root 1.43 PPCODE:
1645 root 1.32 {
1646     dREQ;
1647    
1648     req->type = REQ_SENDFILE;
1649 root 1.99 req->sv1 = newSVsv (out_fh);
1650 root 1.86 req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
1651     req->sv2 = newSVsv (in_fh);
1652     req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
1653 root 1.101 req->offs = SvVAL64 (in_offset);
1654 root 1.86 req->size = length;
1655 root 1.32
1656 root 1.43 REQ_SEND;
1657 root 1.32 }
1658    
1659     void
1660 root 1.101 aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef)
1661 root 1.8 PROTOTYPE: $$$;$
1662 root 1.43 PPCODE:
1663 root 1.1 {
1664 root 1.22 dREQ;
1665 root 1.1
1666     req->type = REQ_READAHEAD;
1667 root 1.99 req->sv1 = newSVsv (fh);
1668 root 1.86 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1669 root 1.101 req->offs = SvVAL64 (offset);
1670 root 1.86 req->size = length;
1671 root 1.1
1672 root 1.43 REQ_SEND;
1673 root 1.1 }
1674    
1675     void
1676 root 1.99 aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef)
1677 root 1.1 ALIAS:
1678 root 1.8 aio_stat = REQ_STAT
1679     aio_lstat = REQ_LSTAT
1680 root 1.43 PPCODE:
1681 root 1.1 {
1682 root 1.22 dREQ;
1683 root 1.1
1684 root 1.87 req->ptr2 = malloc (sizeof (Stat_t));
1685     if (!req->ptr2)
1686 root 1.27 {
1687 root 1.101 req_destroy (req);
1688 root 1.88 croak ("out of memory during aio_stat statdata allocation");
1689 root 1.27 }
1690 root 1.1
1691 root 1.87 req->flags |= FLAG_PTR2_FREE;
1692 root 1.99 req->sv1 = newSVsv (fh_or_path);
1693 root 1.87
1694 root 1.1 if (SvPOK (fh_or_path))
1695     {
1696 root 1.8 req->type = ix;
1697 root 1.89 req->ptr1 = SvPVbyte_nolen (req->sv1);
1698 root 1.1 }
1699     else
1700     {
1701     req->type = REQ_FSTAT;
1702 root 1.86 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1703 root 1.1 }
1704    
1705 root 1.43 REQ_SEND;
1706 root 1.1 }
1707    
1708     void
1709 root 1.99 aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef)
1710     PPCODE:
1711     {
1712     dREQ;
1713    
1714     req->nv1 = SvOK (atime) ? SvNV (atime) : -1.;
1715     req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.;
1716     req->sv1 = newSVsv (fh_or_path);
1717    
1718     if (SvPOK (fh_or_path))
1719     {
1720     req->type = REQ_UTIME;
1721     req->ptr1 = SvPVbyte_nolen (req->sv1);
1722     }
1723     else
1724     {
1725     req->type = REQ_FUTIME;
1726     req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1727     }
1728    
1729     REQ_SEND;
1730     }
1731    
1732     void
1733 root 1.103 aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef)
1734     PPCODE:
1735     {
1736     dREQ;
1737    
1738     req->sv1 = newSVsv (fh_or_path);
1739     req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1740    
1741     if (SvPOK (fh_or_path))
1742     {
1743     req->type = REQ_TRUNCATE;
1744     req->ptr1 = SvPVbyte_nolen (req->sv1);
1745     }
1746     else
1747     {
1748     req->type = REQ_FTRUNCATE;
1749     req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1750     }
1751    
1752     REQ_SEND;
1753     }
1754    
1755     void
1756 root 1.99 aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef)
1757     PPCODE:
1758     {
1759     dREQ;
1760    
1761     req->mode = mode;
1762     req->sv1 = newSVsv (fh_or_path);
1763    
1764     if (SvPOK (fh_or_path))
1765     {
1766     req->type = REQ_CHMOD;
1767     req->ptr1 = SvPVbyte_nolen (req->sv1);
1768     }
1769     else
1770     {
1771     req->type = REQ_FCHMOD;
1772     req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1773     }
1774    
1775     REQ_SEND;
1776     }
1777    
1778     void
1779     aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef)
1780     PPCODE:
1781     {
1782     dREQ;
1783    
1784     req->int2 = SvOK (uid) ? SvIV (uid) : -1;
1785     req->int3 = SvOK (gid) ? SvIV (gid) : -1;
1786     req->sv1 = newSVsv (fh_or_path);
1787    
1788     if (SvPOK (fh_or_path))
1789     {
1790     req->type = REQ_CHOWN;
1791     req->ptr1 = SvPVbyte_nolen (req->sv1);
1792     }
1793     else
1794     {
1795     req->type = REQ_FCHOWN;
1796     req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1797     }
1798    
1799     REQ_SEND;
1800     }
1801    
1802     void
1803     aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef)
1804 root 1.22 ALIAS:
1805 root 1.40 aio_unlink = REQ_UNLINK
1806     aio_rmdir = REQ_RMDIR
1807     aio_readdir = REQ_READDIR
1808 root 1.43 PPCODE:
1809 root 1.1 {
1810 root 1.22 dREQ;
1811 root 1.1
1812 root 1.22 req->type = ix;
1813 root 1.86 req->sv1 = newSVsv (pathname);
1814 root 1.89 req->ptr1 = SvPVbyte_nolen (req->sv1);
1815 root 1.87
1816 root 1.43 REQ_SEND;
1817 root 1.22 }
1818    
1819     void
1820 root 1.99 aio_mkdir (SV8 *pathname, int mode, SV *callback=&PL_sv_undef)
1821 root 1.97 PPCODE:
1822     {
1823     dREQ;
1824    
1825     req->type = REQ_MKDIR;
1826     req->sv1 = newSVsv (pathname);
1827     req->ptr1 = SvPVbyte_nolen (req->sv1);
1828     req->mode = mode;
1829    
1830     REQ_SEND;
1831     }
1832    
1833     void
1834 root 1.99 aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef)
1835 root 1.40 ALIAS:
1836     aio_link = REQ_LINK
1837     aio_symlink = REQ_SYMLINK
1838     aio_rename = REQ_RENAME
1839 root 1.43 PPCODE:
1840 root 1.22 {
1841     dREQ;
1842 root 1.1
1843 root 1.40 req->type = ix;
1844 root 1.99 req->sv2 = newSVsv (oldpath);
1845     req->ptr2 = SvPVbyte_nolen (req->sv2);
1846 root 1.86 req->sv1 = newSVsv (newpath);
1847 root 1.89 req->ptr1 = SvPVbyte_nolen (req->sv1);
1848 root 1.1
1849 root 1.43 REQ_SEND;
1850 root 1.1 }
1851    
1852 root 1.42 void
1853 root 1.99 aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef)
1854 root 1.81 PPCODE:
1855     {
1856     dREQ;
1857    
1858     req->type = REQ_MKNOD;
1859 root 1.86 req->sv1 = newSVsv (pathname);
1860 root 1.89 req->ptr1 = SvPVbyte_nolen (req->sv1);
1861 root 1.81 req->mode = (mode_t)mode;
1862 root 1.86 req->offs = dev;
1863 root 1.81
1864     REQ_SEND;
1865     }
1866    
1867     void
1868 root 1.99 aio_busy (double delay, SV *callback=&PL_sv_undef)
1869 root 1.45 PPCODE:
1870     {
1871     dREQ;
1872    
1873 root 1.69 req->type = REQ_BUSY;
1874 root 1.99 req->nv1 = delay < 0. ? 0. : delay;
1875 root 1.45
1876     REQ_SEND;
1877     }
1878    
1879     void
1880 root 1.99 aio_group (SV *callback=&PL_sv_undef)
1881 root 1.46 PROTOTYPE: ;$
1882 root 1.44 PPCODE:
1883 root 1.42 {
1884 root 1.44 dREQ;
1885 root 1.60
1886 root 1.44 req->type = REQ_GROUP;
1887 root 1.86
1888 root 1.45 req_send (req);
1889 root 1.44 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1890 root 1.42 }
1891    
1892 root 1.6 void
1893 root 1.99 aio_nop (SV *callback=&PL_sv_undef)
1894 root 1.110 ALIAS:
1895     aio_nop = REQ_NOP
1896     aio_sync = REQ_SYNC
1897 root 1.54 PPCODE:
1898     {
1899     dREQ;
1900    
1901 root 1.110 req->type = ix;
1902 root 1.54
1903     REQ_SEND;
1904     }
1905    
1906 root 1.79 int
1907     aioreq_pri (int pri = 0)
1908     PROTOTYPE: ;$
1909     CODE:
1910     RETVAL = next_pri - PRI_BIAS;
1911     if (items > 0)
1912     {
1913     if (pri < PRI_MIN) pri = PRI_MIN;
1914     if (pri > PRI_MAX) pri = PRI_MAX;
1915     next_pri = pri + PRI_BIAS;
1916     }
1917     OUTPUT:
1918     RETVAL
1919 root 1.68
1920     void
1921     aioreq_nice (int nice = 0)
1922 root 1.79 CODE:
1923     nice = next_pri - nice;
1924     if (nice < PRI_MIN) nice = PRI_MIN;
1925     if (nice > PRI_MAX) nice = PRI_MAX;
1926     next_pri = nice + PRI_BIAS;
1927 root 1.60
1928 root 1.54 void
1929 root 1.40 flush ()
1930 root 1.6 PROTOTYPE:
1931     CODE:
1932     while (nreqs)
1933     {
1934     poll_wait ();
1935 root 1.91 poll_cb ();
1936 root 1.6 }
1937    
1938 root 1.91 int
1939 root 1.7 poll()
1940     PROTOTYPE:
1941     CODE:
1942 root 1.92 poll_wait ();
1943     RETVAL = poll_cb ();
1944 root 1.91 OUTPUT:
1945     RETVAL
1946 root 1.7
1947 root 1.1 int
1948     poll_fileno()
1949     PROTOTYPE:
1950     CODE:
1951 root 1.3 RETVAL = respipe [0];
1952 root 1.1 OUTPUT:
1953     RETVAL
1954    
1955     int
1956     poll_cb(...)
1957     PROTOTYPE:
1958     CODE:
1959 root 1.85 RETVAL = poll_cb ();
1960 root 1.1 OUTPUT:
1961     RETVAL
1962    
1963     void
1964     poll_wait()
1965     PROTOTYPE:
1966     CODE:
1967 root 1.92 poll_wait ();
1968 root 1.1
1969 root 1.94 void
1970     setsig (int signum = SIGIO)
1971     PROTOTYPE: ;$
1972     CODE:
1973     {
1974     if (block_sig_level)
1975     croak ("cannot call IO::AIO::setsig from within aio_block/callback");
1976    
1977 root 1.103 X_LOCK (reslock);
1978 root 1.94 main_tid = pthread_self ();
1979     main_sig = signum;
1980 root 1.103 X_UNLOCK (reslock);
1981 root 1.94
1982     if (main_sig && npending)
1983     pthread_kill (main_tid, main_sig);
1984     }
1985    
1986     void
1987     aio_block (SV *cb)
1988     PROTOTYPE: &
1989     PPCODE:
1990     {
1991     int count;
1992    
1993     block_sig ();
1994     PUSHMARK (SP);
1995     PUTBACK;
1996     count = call_sv (cb, GIMME_V | G_NOARGS | G_EVAL);
1997     unblock_sig ();
1998    
1999     if (SvTRUE (ERRSV))
2000     croak (0);
2001    
2002     XSRETURN (count);
2003     }
2004    
2005 root 1.1 int
2006     nreqs()
2007     PROTOTYPE:
2008     CODE:
2009     RETVAL = nreqs;
2010     OUTPUT:
2011     RETVAL
2012    
2013 root 1.79 int
2014     nready()
2015     PROTOTYPE:
2016     CODE:
2017 root 1.80 RETVAL = get_nready ();
2018 root 1.79 OUTPUT:
2019     RETVAL
2020    
2021     int
2022     npending()
2023     PROTOTYPE:
2024     CODE:
2025 root 1.80 RETVAL = get_npending ();
2026 root 1.79 OUTPUT:
2027     RETVAL
2028    
2029 root 1.85 int
2030     nthreads()
2031     PROTOTYPE:
2032     CODE:
2033 root 1.103 if (WORDACCESS_UNSAFE) X_LOCK (wrklock);
2034 root 1.85 RETVAL = started;
2035 root 1.103 if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock);
2036 root 1.85 OUTPUT:
2037     RETVAL
2038    
2039 root 1.48 PROTOTYPES: DISABLE
2040    
2041 root 1.44 MODULE = IO::AIO PACKAGE = IO::AIO::REQ
2042 root 1.43
2043     void
2044     cancel (aio_req_ornot req)
2045     CODE:
2046 root 1.45 req_cancel (req);
2047    
2048 root 1.56 void
2049 root 1.59 cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
2050 root 1.56 CODE:
2051     SvREFCNT_dec (req->callback);
2052     req->callback = newSVsv (callback);
2053    
2054 root 1.45 MODULE = IO::AIO PACKAGE = IO::AIO::GRP
2055    
2056     void
2057     add (aio_req grp, ...)
2058     PPCODE:
2059     {
2060     int i;
2061 root 1.53 aio_req req;
2062 root 1.45
2063 root 1.94 if (main_sig && !block_sig_level)
2064     croak ("aio_group->add called outside aio_block/callback context while IO::AIO::setsig is in use");
2065    
2066 root 1.86 if (grp->int1 == 2)
2067 root 1.49 croak ("cannot add requests to IO::AIO::GRP after the group finished");
2068    
2069 root 1.45 for (i = 1; i < items; ++i )
2070     {
2071 root 1.46 if (GIMME_V != G_VOID)
2072     XPUSHs (sv_2mortal (newSVsv (ST (i))));
2073    
2074 root 1.53 req = SvAIO_REQ (ST (i));
2075 root 1.45
2076 root 1.46 if (req)
2077     {
2078 root 1.86 ++grp->size;
2079 root 1.49 req->grp = grp;
2080    
2081     req->grp_prev = 0;
2082     req->grp_next = grp->grp_first;
2083 root 1.45
2084 root 1.49 if (grp->grp_first)
2085     grp->grp_first->grp_prev = req;
2086    
2087     grp->grp_first = req;
2088 root 1.46 }
2089 root 1.45 }
2090     }
2091 root 1.43
2092 root 1.48 void
2093 root 1.72 cancel_subs (aio_req_ornot req)
2094     CODE:
2095     req_cancel_subs (req);
2096    
2097     void
2098 root 1.51 result (aio_req grp, ...)
2099     CODE:
2100     {
2101     int i;
2102 root 1.79 AV *av;
2103    
2104     grp->errorno = errno;
2105    
2106     av = newAV ();
2107 root 1.51
2108     for (i = 1; i < items; ++i )
2109     av_push (av, newSVsv (ST (i)));
2110    
2111 root 1.86 SvREFCNT_dec (grp->sv1);
2112     grp->sv1 = (SV *)av;
2113 root 1.51 }
2114    
2115     void
2116 root 1.79 errno (aio_req grp, int errorno = errno)
2117     CODE:
2118     grp->errorno = errorno;
2119    
2120     void
2121 root 1.67 limit (aio_req grp, int limit)
2122 root 1.49 CODE:
2123 root 1.86 grp->int2 = limit;
2124 root 1.49 aio_grp_feed (grp);
2125    
2126     void
2127 root 1.56 feed (aio_req grp, SV *callback=&PL_sv_undef)
2128 root 1.49 CODE:
2129     {
2130 root 1.86 SvREFCNT_dec (grp->sv2);
2131     grp->sv2 = newSVsv (callback);
2132 root 1.49
2133 root 1.86 if (grp->int2 <= 0)
2134     grp->int2 = 2;
2135 root 1.49
2136     aio_grp_feed (grp);
2137     }
2138