ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
Revision: 1.40
Committed: Sat Jun 24 16:27:02 2006 UTC (17 years, 10 months ago) by root
Branch: MAIN
Changes since 1.39: +24 -32 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.20 #define _REENTRANT 1
2 root 1.19 #include <errno.h>
3    
4 root 1.15 #include "EXTERN.h"
5 root 1.1 #include "perl.h"
6     #include "XSUB.h"
7    
8 root 1.16 #include "autoconf/config.h"
9    
10 root 1.32 #include <pthread.h>
11    
12 root 1.37 #include <stddef.h>
13 root 1.1 #include <sys/types.h>
14     #include <sys/stat.h>
15 root 1.37 #include <limits.h>
16 root 1.1 #include <unistd.h>
17     #include <fcntl.h>
18     #include <signal.h>
19     #include <sched.h>
20    
21 root 1.32 #if HAVE_SENDFILE
22     # if __linux
23     # include <sys/sendfile.h>
24     # elif __freebsd
25     # include <sys/socket.h>
26     # include <sys/uio.h>
27     # elif __hpux
28     # include <sys/socket.h>
29 root 1.35 # elif __solaris /* not yet */
30     # include <sys/sendfile.h>
31 root 1.34 # else
32     # error sendfile support requested but not available
33 root 1.32 # endif
34     #endif
35 root 1.1
36 root 1.39 /* used for struct dirent, AIX doesn't provide it */
37     #ifndef NAME_MAX
38     # define NAME_MAX 4096
39     #endif
40    
41 root 1.4 #if __ia64
42     # define STACKSIZE 65536
43 root 1.1 #else
44 root 1.37 # define STACKSIZE 8192
45 root 1.1 #endif
46    
47     enum {
48     REQ_QUIT,
49     REQ_OPEN, REQ_CLOSE,
50     REQ_READ, REQ_WRITE, REQ_READAHEAD,
51 root 1.32 REQ_SENDFILE,
52 root 1.22 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
53 root 1.1 REQ_FSYNC, REQ_FDATASYNC,
54 root 1.40 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
55 root 1.37 REQ_READDIR,
56 root 1.40 REQ_LINK, REQ_SYMLINK,
57 root 1.1 };
58    
59     typedef struct aio_cb {
60 root 1.3 struct aio_cb *volatile next;
61 root 1.1
62     int type;
63    
64 root 1.37 /* should receive a cleanup, with unions */
65 root 1.32 int fd, fd2;
66 root 1.1 off_t offset;
67     size_t length;
68     ssize_t result;
69     mode_t mode; /* open */
70     int errorno;
71 root 1.32 SV *data, *callback;
72     SV *fh, *fh2;
73 root 1.22 void *dataptr, *data2ptr;
74 root 1.1 STRLEN dataoffset;
75    
76     Stat_t *statdata;
77     } aio_cb;
78    
79     typedef aio_cb *aio_req;
80    
81 root 1.30 static int started, wanted;
82 root 1.11 static volatile int nreqs;
83 root 1.4 static int max_outstanding = 1<<30;
84 root 1.3 static int respipe [2];
85 root 1.1
86 root 1.3 static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
87     static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
88     static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
89    
90     static volatile aio_req reqs, reqe; /* queue start, queue end */
91     static volatile aio_req ress, rese; /* queue start, queue end */
92 root 1.1
93 root 1.26 static void free_req (aio_req req)
94     {
95     if (req->data)
96     SvREFCNT_dec (req->data);
97    
98     if (req->fh)
99     SvREFCNT_dec (req->fh);
100    
101 root 1.32 if (req->fh2)
102     SvREFCNT_dec (req->fh2);
103    
104 root 1.27 if (req->statdata)
105 root 1.26 Safefree (req->statdata);
106    
107     if (req->callback)
108     SvREFCNT_dec (req->callback);
109    
110 root 1.37 if (req->type == REQ_READDIR && req->result >= 0)
111     free (req->data2ptr);
112    
113 root 1.26 Safefree (req);
114     }
115    
116 root 1.1 static void
117     poll_wait ()
118     {
119 root 1.11 if (nreqs && !ress)
120     {
121     fd_set rfd;
122     FD_ZERO(&rfd);
123     FD_SET(respipe [0], &rfd);
124 root 1.3
125 root 1.11 select (respipe [0] + 1, &rfd, 0, 0, 0);
126     }
127 root 1.1 }
128    
129     static int
130 root 1.5 poll_cb ()
131 root 1.1 {
132     dSP;
133     int count = 0;
134 root 1.24 int do_croak = 0;
135 root 1.27 aio_req req;
136    
137     for (;;)
138     {
139     pthread_mutex_lock (&reslock);
140     req = ress;
141    
142     if (req)
143     {
144     ress = req->next;
145 root 1.11
146 root 1.27 if (!ress)
147     {
148     /* read any signals sent by the worker threads */
149     char buf [32];
150     while (read (respipe [0], buf, 32) == 32)
151     ;
152 root 1.30
153     rese = 0;
154 root 1.27 }
155     }
156 root 1.1
157 root 1.27 pthread_mutex_unlock (&reslock);
158 root 1.3
159 root 1.27 if (!req)
160     break;
161 root 1.3
162 root 1.1 nreqs--;
163    
164     if (req->type == REQ_QUIT)
165     started--;
166     else
167     {
168     int errorno = errno;
169     errno = req->errorno;
170    
171     if (req->type == REQ_READ)
172 root 1.27 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
173 root 1.1
174 root 1.28 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
175     SvREADONLY_off (req->data);
176    
177 root 1.27 if (req->statdata)
178 root 1.1 {
179     PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
180     PL_laststatval = req->result;
181     PL_statcache = *(req->statdata);
182     }
183    
184 root 1.13 ENTER;
185 root 1.1 PUSHMARK (SP);
186 root 1.2
187 root 1.37 if (req->type == REQ_READDIR)
188     {
189     SV *rv = &PL_sv_undef;
190    
191     if (req->result >= 0)
192     {
193     char *buf = req->data2ptr;
194     AV *av = newAV ();
195    
196     while (req->result)
197     {
198     SV *sv = newSVpv (buf, 0);
199    
200     av_push (av, sv);
201     buf += SvCUR (sv) + 1;
202     req->result--;
203     }
204    
205     rv = sv_2mortal (newRV_noinc ((SV *)av));
206     }
207    
208     XPUSHs (rv);
209     }
210     else
211 root 1.2 {
212 root 1.37 XPUSHs (sv_2mortal (newSViv (req->result)));
213    
214     if (req->type == REQ_OPEN)
215     {
216     /* convert fd to fh */
217     SV *fh;
218 root 1.2
219 root 1.37 PUTBACK;
220     call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
221     SPAGAIN;
222 root 1.2
223 root 1.37 fh = SvREFCNT_inc (POPs);
224 root 1.2
225 root 1.37 PUSHMARK (SP);
226     XPUSHs (sv_2mortal (fh));
227     }
228 root 1.2 }
229    
230 root 1.8 if (SvOK (req->callback))
231     {
232     PUTBACK;
233     call_sv (req->callback, G_VOID | G_EVAL);
234     SPAGAIN;
235 root 1.27
236     if (SvTRUE (ERRSV))
237     {
238     free_req (req);
239     croak (0);
240     }
241 root 1.8 }
242 root 1.13
243 root 1.26 LEAVE;
244    
245 root 1.1 errno = errorno;
246     count++;
247     }
248    
249 root 1.27 free_req (req);
250 root 1.1 }
251    
252     return count;
253     }
254    
255 root 1.4 static void *aio_proc(void *arg);
256    
257     static void
258     start_thread (void)
259     {
260     sigset_t fullsigset, oldsigset;
261     pthread_t tid;
262     pthread_attr_t attr;
263    
264     pthread_attr_init (&attr);
265     pthread_attr_setstacksize (&attr, STACKSIZE);
266 root 1.31 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
267 root 1.4
268     sigfillset (&fullsigset);
269     sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset);
270    
271     if (pthread_create (&tid, &attr, aio_proc, 0) == 0)
272     started++;
273    
274     sigprocmask (SIG_SETMASK, &oldsigset, 0);
275     }
276    
277     static void
278     send_req (aio_req req)
279     {
280 root 1.30 while (started < wanted && nreqs >= started)
281     start_thread ();
282    
283 root 1.4 nreqs++;
284    
285     pthread_mutex_lock (&reqlock);
286    
287     req->next = 0;
288    
289     if (reqe)
290     {
291     reqe->next = req;
292     reqe = req;
293     }
294     else
295     reqe = reqs = req;
296    
297     pthread_cond_signal (&reqwait);
298     pthread_mutex_unlock (&reqlock);
299    
300 root 1.27 if (nreqs > max_outstanding)
301     for (;;)
302     {
303     poll_cb ();
304    
305     if (nreqs <= max_outstanding)
306     break;
307    
308     poll_wait ();
309     }
310 root 1.4 }
311    
312     static void
313     end_thread (void)
314     {
315     aio_req req;
316 root 1.26 Newz (0, req, 1, aio_cb);
317 root 1.4 req->type = REQ_QUIT;
318    
319     send_req (req);
320     }
321    
322 root 1.22 static void min_parallel (int nthreads)
323     {
324 root 1.30 if (wanted < nthreads)
325     wanted = nthreads;
326 root 1.22 }
327    
328     static void max_parallel (int nthreads)
329     {
330     int cur = started;
331 root 1.27
332 root 1.30 if (wanted > nthreads)
333     wanted = nthreads;
334    
335     while (cur > wanted)
336     {
337 root 1.22 end_thread ();
338     cur--;
339     }
340    
341 root 1.30 while (started > wanted)
342 root 1.22 {
343     poll_wait ();
344     poll_cb ();
345     }
346     }
347    
348 root 1.26 static void create_pipe ()
349 root 1.22 {
350 root 1.26 if (pipe (respipe))
351     croak ("unable to initialize result pipe");
352 root 1.22
353 root 1.26 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
354     croak ("cannot set result pipe to nonblocking mode");
355 root 1.22
356 root 1.26 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
357     croak ("cannot set result pipe to nonblocking mode");
358     }
359 root 1.22
360     /*****************************************************************************/
361 root 1.17 /* work around various missing functions */
362    
363     #if !HAVE_PREADWRITE
364     # define pread aio_pread
365     # define pwrite aio_pwrite
366    
367     /*
368     * make our pread/pwrite safe against themselves, but not against
369     * normal read/write by using a mutex. slows down execution a lot,
370     * but that's your problem, not mine.
371     */
372 root 1.37 static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER;
373 root 1.17
374     static ssize_t
375     pread (int fd, void *buf, size_t count, off_t offset)
376     {
377     ssize_t res;
378     off_t ooffset;
379    
380 root 1.37 pthread_mutex_lock (&preadwritelock);
381 root 1.17 ooffset = lseek (fd, 0, SEEK_CUR);
382     lseek (fd, offset, SEEK_SET);
383     res = read (fd, buf, count);
384     lseek (fd, ooffset, SEEK_SET);
385 root 1.37 pthread_mutex_unlock (&preadwritelock);
386 root 1.17
387     return res;
388     }
389    
390     static ssize_t
391     pwrite (int fd, void *buf, size_t count, off_t offset)
392     {
393     ssize_t res;
394     off_t ooffset;
395    
396 root 1.37 pthread_mutex_lock (&preadwritelock);
397 root 1.17 ooffset = lseek (fd, 0, SEEK_CUR);
398     lseek (fd, offset, SEEK_SET);
399     res = write (fd, buf, count);
400     lseek (fd, offset, SEEK_SET);
401 root 1.37 pthread_mutex_unlock (&preadwritelock);
402 root 1.17
403     return res;
404     }
405     #endif
406    
407     #if !HAVE_FDATASYNC
408     # define fdatasync fsync
409     #endif
410    
411     #if !HAVE_READAHEAD
412     # define readahead aio_readahead
413    
414     static ssize_t
415     readahead (int fd, off_t offset, size_t count)
416     {
417 root 1.37 char readahead_buf[4096];
418    
419 root 1.17 while (count > 0)
420     {
421     size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
422    
423     pread (fd, readahead_buf, len, offset);
424     offset += len;
425     count -= len;
426     }
427    
428     errno = 0;
429     }
430     #endif
431    
432 root 1.37 #if !HAVE_READDIR_R
433     # define readdir_r aio_readdir_r
434    
435     static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER;
436    
437     static int
438     readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
439     {
440     struct dirent *e;
441     int errorno;
442    
443     pthread_mutex_lock (&readdirlock);
444    
445     e = readdir (dirp);
446     errorno = errno;
447    
448     if (e)
449     {
450     *res = ent;
451     strcpy (ent->d_name, e->d_name);
452     }
453     else
454     *res = 0;
455    
456     pthread_mutex_unlock (&readdirlock);
457    
458     errno = errorno;
459     return e ? 0 : -1;
460     }
461     #endif
462    
463 root 1.32 /* sendfile always needs emulation */
464     static ssize_t
465     sendfile_ (int ofd, int ifd, off_t offset, size_t count)
466     {
467 root 1.37 ssize_t res;
468 root 1.32
469 root 1.37 if (!count)
470     return 0;
471 root 1.32
472 root 1.35 #if HAVE_SENDFILE
473     # if __linux
474 root 1.37 res = sendfile (ofd, ifd, &offset, count);
475 root 1.32
476 root 1.35 # elif __freebsd
477 root 1.37 /*
478     * Of course, the freebsd sendfile is a dire hack with no thoughts
479     * wasted on making it similar to other I/O functions.
480     */
481     {
482     off_t sbytes;
483     res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
484    
485     if (res < 0 && sbytes)
486     /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */
487     res = sbytes;
488     }
489 root 1.32
490 root 1.35 # elif __hpux
491 root 1.37 res = sendfile (ofd, ifd, offset, count, 0, 0);
492 root 1.32
493 root 1.35 # elif __solaris
494 root 1.37 {
495     struct sendfilevec vec;
496     size_t sbytes;
497    
498     vec.sfv_fd = ifd;
499     vec.sfv_flag = 0;
500     vec.sfv_off = offset;
501     vec.sfv_len = count;
502    
503     res = sendfilev (ofd, &vec, 1, &sbytes);
504    
505     if (res < 0 && sbytes)
506     res = sbytes;
507     }
508 root 1.35
509 root 1.38 # endif
510     #else
511 root 1.37 res = -1;
512     errno = ENOSYS;
513 root 1.32 #endif
514    
515 root 1.37 if (res < 0
516     && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
517 root 1.35 #if __solaris
518 root 1.37 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
519 root 1.35 #endif
520 root 1.37 )
521     )
522     {
523     /* emulate sendfile. this is a major pain in the ass */
524     char buf[4096];
525     res = 0;
526    
527 root 1.38 while (count)
528 root 1.37 {
529     ssize_t cnt;
530    
531 root 1.38 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset);
532 root 1.32
533 root 1.37 if (cnt <= 0)
534     {
535     if (cnt && !res) res = -1;
536     break;
537     }
538    
539     cnt = write (ofd, buf, cnt);
540    
541     if (cnt <= 0)
542     {
543     if (cnt && !res) res = -1;
544     break;
545     }
546    
547     offset += cnt;
548     res += cnt;
549 root 1.38 count -= cnt;
550 root 1.37 }
551     }
552    
553     return res;
554     }
555    
556     /* read a full directory */
557     static int
558     scandir_ (const char *path, void **namesp)
559     {
560     DIR *dirp = opendir (path);
561     union
562     {
563     struct dirent d;
564     char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
565     } u;
566     struct dirent *entp;
567     char *name, *names;
568     int memlen = 4096;
569     int memofs = 0;
570     int res = 0;
571     int errorno;
572    
573     if (!dirp)
574     return -1;
575    
576     names = malloc (memlen);
577    
578     for (;;)
579     {
580     errno = 0, readdir_r (dirp, &u.d, &entp);
581    
582     if (!entp)
583     break;
584    
585     name = entp->d_name;
586    
587     if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
588     {
589     int len = strlen (name) + 1;
590    
591     res++;
592    
593     while (memofs + len > memlen)
594     {
595     memlen *= 2;
596     names = realloc (names, memlen);
597     if (!names)
598     break;
599     }
600    
601     memcpy (names + memofs, name, len);
602     memofs += len;
603     }
604     }
605    
606     errorno = errno;
607     closedir (dirp);
608    
609     if (errorno)
610     {
611     free (names);
612     errno = errorno;
613     res = -1;
614     }
615    
616     *namesp = (void *)names;
617     return res;
618 root 1.32 }
619    
620 root 1.22 /*****************************************************************************/
621    
622 root 1.1 static void *
623     aio_proc (void *thr_arg)
624     {
625     aio_req req;
626 root 1.3 int type;
627 root 1.1
628 root 1.3 do
629 root 1.1 {
630 root 1.3 pthread_mutex_lock (&reqlock);
631    
632     for (;;)
633     {
634     req = reqs;
635    
636     if (reqs)
637     {
638     reqs = reqs->next;
639     if (!reqs) reqe = 0;
640     }
641    
642     if (req)
643     break;
644    
645     pthread_cond_wait (&reqwait, &reqlock);
646     }
647    
648     pthread_mutex_unlock (&reqlock);
649    
650 root 1.1 errno = 0; /* strictly unnecessary */
651    
652 root 1.3 type = req->type;
653    
654     switch (type)
655 root 1.1 {
656 root 1.10 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
657     case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
658 root 1.16
659 root 1.1 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
660 root 1.32 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break;
661 root 1.1
662     case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
663     case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
664     case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
665    
666     case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
667     case REQ_CLOSE: req->result = close (req->fd); break;
668     case REQ_UNLINK: req->result = unlink (req->dataptr); break;
669 root 1.22 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
670 root 1.40 case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break;
671     case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break;
672 root 1.22 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
673 root 1.1
674 root 1.17 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
675 root 1.1 case REQ_FSYNC: req->result = fsync (req->fd); break;
676 root 1.37 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break;
677 root 1.1
678     case REQ_QUIT:
679 root 1.3 break;
680 root 1.1
681     default:
682     req->result = ENOSYS;
683     break;
684     }
685    
686     req->errorno = errno;
687 root 1.3
688     pthread_mutex_lock (&reslock);
689    
690     req->next = 0;
691    
692     if (rese)
693     {
694     rese->next = req;
695     rese = req;
696     }
697     else
698     {
699     rese = ress = req;
700    
701     /* write a dummy byte to the pipe so fh becomes ready */
702     write (respipe [1], &respipe, 1);
703     }
704    
705     pthread_mutex_unlock (&reslock);
706 root 1.1 }
707 root 1.3 while (type != REQ_QUIT);
708 root 1.1
709     return 0;
710     }
711    
712 root 1.37 /*****************************************************************************/
713    
714     static void atfork_prepare (void)
715     {
716     pthread_mutex_lock (&reqlock);
717     pthread_mutex_lock (&reslock);
718     #if !HAVE_PREADWRITE
719     pthread_mutex_lock (&preadwritelock);
720     #endif
721     #if !HAVE_READDIR_R
722     pthread_mutex_lock (&readdirlock);
723     #endif
724     }
725    
726     static void atfork_parent (void)
727     {
728     #if !HAVE_READDIR_R
729     pthread_mutex_unlock (&readdirlock);
730     #endif
731     #if !HAVE_PREADWRITE
732     pthread_mutex_unlock (&preadwritelock);
733     #endif
734     pthread_mutex_unlock (&reslock);
735     pthread_mutex_unlock (&reqlock);
736     }
737    
738     static void atfork_child (void)
739     {
740     aio_req prv;
741    
742     started = 0;
743    
744     while (reqs)
745     {
746     prv = reqs;
747     reqs = prv->next;
748     free_req (prv);
749     }
750    
751     reqs = reqe = 0;
752    
753     while (ress)
754     {
755     prv = ress;
756     ress = prv->next;
757     free_req (prv);
758     }
759    
760     ress = rese = 0;
761    
762     close (respipe [0]);
763     close (respipe [1]);
764     create_pipe ();
765    
766     atfork_parent ();
767     }
768    
769 root 1.22 #define dREQ \
770     aio_req req; \
771     \
772     if (SvOK (callback) && !SvROK (callback)) \
773     croak ("clalback must be undef or of reference type"); \
774     \
775     Newz (0, req, 1, aio_cb); \
776     if (!req) \
777     croak ("out of memory during aio_req allocation"); \
778     \
779 root 1.27 req->callback = newSVsv (callback);
780 root 1.22
781 root 1.1 MODULE = IO::AIO PACKAGE = IO::AIO
782    
783 root 1.8 PROTOTYPES: ENABLE
784    
785 root 1.1 BOOT:
786     {
787 root 1.26 create_pipe ();
788 root 1.22 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
789 root 1.1 }
790    
791     void
792 root 1.40 min_parallel (nthreads)
793 root 1.1 int nthreads
794     PROTOTYPE: $
795    
796     void
797 root 1.40 max_parallel (nthreads)
798 root 1.1 int nthreads
799     PROTOTYPE: $
800    
801 root 1.4 int
802 root 1.40 max_outstanding (nreqs)
803 root 1.4 int nreqs
804     PROTOTYPE: $
805     CODE:
806     RETVAL = max_outstanding;
807     max_outstanding = nreqs;
808    
809 root 1.1 void
810 root 1.40 aio_open (pathname,flags,mode,callback=&PL_sv_undef)
811 root 1.1 SV * pathname
812     int flags
813     int mode
814     SV * callback
815 root 1.8 PROTOTYPE: $$$;$
816 root 1.1 CODE:
817     {
818 root 1.22 dREQ;
819 root 1.1
820     req->type = REQ_OPEN;
821     req->data = newSVsv (pathname);
822 root 1.22 req->dataptr = SvPVbyte_nolen (req->data);
823 root 1.1 req->fd = flags;
824     req->mode = mode;
825    
826     send_req (req);
827     }
828    
829     void
830 root 1.40 aio_close (fh,callback=&PL_sv_undef)
831 root 1.13 SV * fh
832     SV * callback
833 root 1.8 PROTOTYPE: $;$
834 root 1.1 ALIAS:
835     aio_close = REQ_CLOSE
836     aio_fsync = REQ_FSYNC
837     aio_fdatasync = REQ_FDATASYNC
838     CODE:
839     {
840 root 1.22 dREQ;
841 root 1.1
842     req->type = ix;
843 root 1.13 req->fh = newSVsv (fh);
844     req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
845 root 1.1
846     send_req (req);
847     }
848    
849     void
850 root 1.40 aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
851 root 1.13 SV * fh
852     UV offset
853 root 1.32 UV length
854 root 1.13 SV * data
855 root 1.32 UV dataoffset
856 root 1.13 SV * callback
857     ALIAS:
858     aio_read = REQ_READ
859     aio_write = REQ_WRITE
860 root 1.8 PROTOTYPE: $$$$$;$
861 root 1.1 CODE:
862 root 1.13 {
863     aio_req req;
864     STRLEN svlen;
865 root 1.21 char *svptr = SvPVbyte (data, svlen);
866 root 1.13
867     SvUPGRADE (data, SVt_PV);
868     SvPOK_on (data);
869 root 1.1
870 root 1.13 if (dataoffset < 0)
871     dataoffset += svlen;
872    
873     if (dataoffset < 0 || dataoffset > svlen)
874     croak ("data offset outside of string");
875    
876     if (ix == REQ_WRITE)
877     {
878     /* write: check length and adjust. */
879     if (length < 0 || length + dataoffset > svlen)
880     length = svlen - dataoffset;
881     }
882     else
883     {
884     /* read: grow scalar as necessary */
885     svptr = SvGROW (data, length + dataoffset);
886     }
887    
888     if (length < 0)
889     croak ("length must not be negative");
890    
891 root 1.22 {
892     dREQ;
893 root 1.13
894 root 1.22 req->type = ix;
895     req->fh = newSVsv (fh);
896     req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
897     : IoOFP (sv_2io (fh)));
898     req->offset = offset;
899     req->length = length;
900     req->data = SvREFCNT_inc (data);
901     req->dataptr = (char *)svptr + dataoffset;
902 root 1.13
903 root 1.28 if (!SvREADONLY (data))
904     {
905     SvREADONLY_on (data);
906     req->data2ptr = (void *)data;
907     }
908    
909 root 1.22 send_req (req);
910     }
911 root 1.13 }
912 root 1.1
913     void
914 root 1.40 aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef)
915 root 1.32 SV * out_fh
916     SV * in_fh
917     UV in_offset
918     UV length
919     SV * callback
920     PROTOTYPE: $$$$;$
921     CODE:
922     {
923     dREQ;
924    
925     req->type = REQ_SENDFILE;
926     req->fh = newSVsv (out_fh);
927     req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
928     req->fh2 = newSVsv (in_fh);
929     req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
930     req->offset = in_offset;
931     req->length = length;
932    
933     send_req (req);
934     }
935    
936     void
937 root 1.40 aio_readahead (fh,offset,length,callback=&PL_sv_undef)
938 root 1.13 SV * fh
939     UV offset
940     IV length
941     SV * callback
942 root 1.8 PROTOTYPE: $$$;$
943 root 1.1 CODE:
944     {
945 root 1.22 dREQ;
946 root 1.1
947     req->type = REQ_READAHEAD;
948 root 1.13 req->fh = newSVsv (fh);
949     req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
950 root 1.1 req->offset = offset;
951     req->length = length;
952    
953     send_req (req);
954     }
955    
956     void
957 root 1.40 aio_stat (fh_or_path,callback=&PL_sv_undef)
958 root 1.1 SV * fh_or_path
959     SV * callback
960     ALIAS:
961 root 1.8 aio_stat = REQ_STAT
962     aio_lstat = REQ_LSTAT
963 root 1.1 CODE:
964     {
965 root 1.22 dREQ;
966 root 1.1
967     New (0, req->statdata, 1, Stat_t);
968     if (!req->statdata)
969 root 1.27 {
970     free_req (req);
971     croak ("out of memory during aio_req->statdata allocation");
972     }
973 root 1.1
974     if (SvPOK (fh_or_path))
975     {
976 root 1.8 req->type = ix;
977 root 1.1 req->data = newSVsv (fh_or_path);
978 root 1.22 req->dataptr = SvPVbyte_nolen (req->data);
979 root 1.1 }
980     else
981     {
982     req->type = REQ_FSTAT;
983 root 1.13 req->fh = newSVsv (fh_or_path);
984 root 1.1 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
985     }
986    
987     send_req (req);
988     }
989    
990     void
991 root 1.40 aio_unlink (pathname,callback=&PL_sv_undef)
992 root 1.1 SV * pathname
993     SV * callback
994 root 1.22 ALIAS:
995 root 1.40 aio_unlink = REQ_UNLINK
996     aio_rmdir = REQ_RMDIR
997     aio_readdir = REQ_READDIR
998 root 1.1 CODE:
999     {
1000 root 1.22 dREQ;
1001 root 1.1
1002 root 1.22 req->type = ix;
1003     req->data = newSVsv (pathname);
1004     req->dataptr = SvPVbyte_nolen (req->data);
1005 root 1.1
1006 root 1.22 send_req (req);
1007     }
1008    
1009     void
1010 root 1.40 aio_link (oldpath,newpath,callback=&PL_sv_undef)
1011 root 1.22 SV * oldpath
1012     SV * newpath
1013     SV * callback
1014 root 1.40 ALIAS:
1015     aio_link = REQ_LINK
1016     aio_symlink = REQ_SYMLINK
1017     aio_rename = REQ_RENAME
1018 root 1.22 CODE:
1019     {
1020     dREQ;
1021 root 1.1
1022 root 1.40 req->type = ix;
1023 root 1.22 req->fh = newSVsv (oldpath);
1024     req->data2ptr = SvPVbyte_nolen (req->fh);
1025     req->data = newSVsv (newpath);
1026     req->dataptr = SvPVbyte_nolen (req->data);
1027 root 1.1
1028     send_req (req);
1029     }
1030    
1031 root 1.6 void
1032 root 1.40 flush ()
1033 root 1.6 PROTOTYPE:
1034     CODE:
1035     while (nreqs)
1036     {
1037     poll_wait ();
1038     poll_cb ();
1039     }
1040    
1041 root 1.7 void
1042     poll()
1043     PROTOTYPE:
1044     CODE:
1045     if (nreqs)
1046     {
1047     poll_wait ();
1048     poll_cb ();
1049     }
1050    
1051 root 1.1 int
1052     poll_fileno()
1053     PROTOTYPE:
1054     CODE:
1055 root 1.3 RETVAL = respipe [0];
1056 root 1.1 OUTPUT:
1057     RETVAL
1058    
1059     int
1060     poll_cb(...)
1061     PROTOTYPE:
1062     CODE:
1063 root 1.5 RETVAL = poll_cb ();
1064 root 1.1 OUTPUT:
1065     RETVAL
1066    
1067     void
1068     poll_wait()
1069     PROTOTYPE:
1070     CODE:
1071 root 1.3 if (nreqs)
1072     poll_wait ();
1073 root 1.1
1074     int
1075     nreqs()
1076     PROTOTYPE:
1077     CODE:
1078     RETVAL = nreqs;
1079     OUTPUT:
1080     RETVAL
1081