ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
Revision: 1.93
Committed: Wed Nov 8 02:01:02 2006 UTC (17 years, 6 months ago) by root
Branch: MAIN
Changes since 1.92: +2 -3 lines
Log Message:
*** empty log message ***

File Contents

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