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