ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
Revision: 1.49
Committed: Sun Oct 22 13:33:28 2006 UTC (17 years, 6 months ago) by root
Branch: MAIN
Changes since 1.48: +93 -34 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.41 #include <errno.h>
14 root 1.45 #include <sys/time.h>
15     #include <sys/select.h>
16 root 1.1 #include <sys/types.h>
17     #include <sys/stat.h>
18 root 1.37 #include <limits.h>
19 root 1.1 #include <unistd.h>
20     #include <fcntl.h>
21     #include <signal.h>
22     #include <sched.h>
23    
24 root 1.32 #if HAVE_SENDFILE
25     # if __linux
26     # include <sys/sendfile.h>
27     # elif __freebsd
28     # include <sys/socket.h>
29     # include <sys/uio.h>
30     # elif __hpux
31     # include <sys/socket.h>
32 root 1.35 # elif __solaris /* not yet */
33     # include <sys/sendfile.h>
34 root 1.34 # else
35     # error sendfile support requested but not available
36 root 1.32 # endif
37     #endif
38 root 1.1
39 root 1.39 /* used for struct dirent, AIX doesn't provide it */
40     #ifndef NAME_MAX
41     # define NAME_MAX 4096
42     #endif
43    
44 root 1.4 #if __ia64
45     # define STACKSIZE 65536
46 root 1.1 #else
47 root 1.37 # define STACKSIZE 8192
48 root 1.1 #endif
49    
50     enum {
51     REQ_QUIT,
52     REQ_OPEN, REQ_CLOSE,
53     REQ_READ, REQ_WRITE, REQ_READAHEAD,
54 root 1.32 REQ_SENDFILE,
55 root 1.22 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
56 root 1.1 REQ_FSYNC, REQ_FDATASYNC,
57 root 1.40 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
58 root 1.37 REQ_READDIR,
59 root 1.40 REQ_LINK, REQ_SYMLINK,
60 root 1.45 REQ_SLEEP,
61 root 1.44 REQ_GROUP,
62 root 1.1 };
63    
64 root 1.44 #define AIO_REQ_KLASS "IO::AIO::REQ"
65     #define AIO_GRP_KLASS "IO::AIO::GRP"
66 root 1.43
67     typedef struct aio_cb
68     {
69 root 1.49 struct aio_cb *volatile next;
70 root 1.43
71 root 1.49 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
72 root 1.1
73 root 1.43 SV *self; /* the perl counterpart of this request, if any */
74 root 1.1
75 root 1.43 SV *data, *callback;
76     SV *fh, *fh2;
77     void *dataptr, *data2ptr;
78     Stat_t *statdata;
79 root 1.1 off_t offset;
80     size_t length;
81     ssize_t result;
82 root 1.43
83     int type;
84     int fd, fd2;
85 root 1.1 int errorno;
86     STRLEN dataoffset;
87 root 1.43 mode_t mode; /* open */
88     unsigned char cancelled;
89 root 1.1 } aio_cb;
90    
91     typedef aio_cb *aio_req;
92 root 1.43 typedef aio_cb *aio_req_ornot;
93 root 1.1
94 root 1.30 static int started, wanted;
95 root 1.11 static volatile int nreqs;
96 root 1.4 static int max_outstanding = 1<<30;
97 root 1.3 static int respipe [2];
98 root 1.1
99 root 1.3 static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
100     static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
101     static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
102    
103     static volatile aio_req reqs, reqe; /* queue start, queue end */
104     static volatile aio_req ress, rese; /* queue start, queue end */
105 root 1.1
106 root 1.45 static void req_free (aio_req req);
107    
108 root 1.43 /* must be called at most once */
109 root 1.44 static SV *req_sv (aio_req req, const char *klass)
110 root 1.43 {
111 root 1.49 if (!req->self)
112     {
113     req->self = (SV *)newHV ();
114     sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
115     }
116 root 1.43
117 root 1.45 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1)));
118 root 1.43 }
119    
120 root 1.45 static aio_req SvAIO_REQ (SV *sv)
121 root 1.26 {
122 root 1.45 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv))
123     croak ("object of class " AIO_REQ_KLASS " expected");
124 root 1.43
125     MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
126    
127     return mg ? (aio_req)mg->mg_ptr : 0;
128     }
129    
130 root 1.49 static void aio_grp_feed (aio_req grp)
131     {
132     while (grp->length < grp->fd2)
133     {
134     int old_len = grp->length;
135    
136     if (grp->fh2 && SvOK (grp->fh2))
137     {
138     dSP;
139    
140     ENTER;
141     SAVETMPS;
142     PUSHMARK (SP);
143     XPUSHs (req_sv (grp, AIO_GRP_KLASS));
144     PUTBACK;
145     call_sv (grp->fh2, G_VOID | G_EVAL);
146     SPAGAIN;
147     FREETMPS;
148     LEAVE;
149     }
150    
151     /* stop if no progress has been made */
152     if (old_len == grp->length)
153     {
154     SvREFCNT_dec (grp->fh2);
155     grp->fh2 = 0;
156     break;
157     }
158     }
159     }
160    
161 root 1.45 static void poll_wait ()
162     {
163     if (nreqs && !ress)
164     {
165     fd_set rfd;
166     FD_ZERO(&rfd);
167     FD_SET(respipe [0], &rfd);
168    
169     select (respipe [0] + 1, &rfd, 0, 0, 0);
170     }
171     }
172    
173     static void req_invoke (aio_req req)
174     {
175     dSP;
176     int errorno = errno;
177    
178     if (req->cancelled || !SvOK (req->callback))
179     return;
180    
181     errno = req->errorno;
182    
183     ENTER;
184 root 1.49 SAVETMPS;
185 root 1.45 PUSHMARK (SP);
186 root 1.48 EXTEND (SP, 1);
187 root 1.45
188     switch (req->type)
189     {
190     case REQ_READDIR:
191     {
192     SV *rv = &PL_sv_undef;
193    
194     if (req->result >= 0)
195     {
196     char *buf = req->data2ptr;
197     AV *av = newAV ();
198    
199     while (req->result)
200     {
201     SV *sv = newSVpv (buf, 0);
202    
203     av_push (av, sv);
204     buf += SvCUR (sv) + 1;
205     req->result--;
206     }
207    
208     rv = sv_2mortal (newRV_noinc ((SV *)av));
209     }
210    
211 root 1.48 PUSHs (rv);
212 root 1.45 }
213     break;
214    
215     case REQ_OPEN:
216     {
217     /* convert fd to fh */
218     SV *fh;
219    
220 root 1.48 PUSHs (sv_2mortal (newSViv (req->result)));
221 root 1.45 PUTBACK;
222     call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
223     SPAGAIN;
224    
225     fh = SvREFCNT_inc (POPs);
226    
227     PUSHMARK (SP);
228     XPUSHs (sv_2mortal (fh));
229     }
230     break;
231    
232 root 1.48 case REQ_GROUP:
233 root 1.49 req->fd = 2; /* mark group as finished */
234    
235 root 1.48 if (req->data)
236     {
237     int i;
238     AV *av = (AV *)req->data;
239    
240     EXTEND (SP, AvFILL (av) + 1);
241     for (i = 0; i <= AvFILL (av); ++i)
242     PUSHs (*av_fetch (av, i, 0));
243     }
244     break;
245    
246 root 1.45 case REQ_SLEEP:
247     break;
248    
249     default:
250 root 1.48 PUSHs (sv_2mortal (newSViv (req->result)));
251 root 1.45 break;
252     }
253    
254    
255     PUTBACK;
256     call_sv (req->callback, G_VOID | G_EVAL);
257     SPAGAIN;
258    
259     if (SvTRUE (ERRSV))
260     {
261     req_free (req);
262     croak (0);
263     }
264    
265 root 1.49 FREETMPS;
266 root 1.45 LEAVE;
267    
268     errno = errorno;
269     }
270    
271 root 1.43 static void req_free (aio_req req)
272     {
273 root 1.45 if (req->grp)
274     {
275     aio_req grp = req->grp;
276    
277     /* unlink request */
278 root 1.49 if (req->grp_next) req->grp_next->grp_prev = req->grp_prev;
279     if (req->grp_prev) req->grp_prev->grp_next = req->grp_next;
280    
281     if (grp->grp_first == req)
282     grp->grp_first = req->grp_next;
283    
284     --grp->length;
285 root 1.45
286 root 1.49 /* call feeder, if applicable */
287     aio_grp_feed (grp);
288    
289     /* finish, if done */
290     if (!grp->length && grp->fd)
291 root 1.45 {
292     req_invoke (grp);
293     req_free (grp);
294     }
295     }
296    
297 root 1.43 if (req->self)
298     {
299     sv_unmagic (req->self, PERL_MAGIC_ext);
300     SvREFCNT_dec (req->self);
301     }
302    
303 root 1.49 SvREFCNT_dec (req->data);
304     SvREFCNT_dec (req->fh);
305     SvREFCNT_dec (req->fh2);
306     SvREFCNT_dec (req->callback);
307     Safefree (req->statdata);
308 root 1.26
309 root 1.37 if (req->type == REQ_READDIR && req->result >= 0)
310     free (req->data2ptr);
311    
312 root 1.26 Safefree (req);
313     }
314    
315 root 1.45 static void req_cancel (aio_req req)
316 root 1.1 {
317 root 1.45 req->cancelled = 1;
318    
319     if (req->type == REQ_GROUP)
320 root 1.11 {
321 root 1.45 aio_req sub;
322 root 1.3
323 root 1.49 for (sub = req->grp_first; sub; sub = sub->grp_next)
324 root 1.45 req_cancel (sub);
325 root 1.11 }
326 root 1.1 }
327    
328 root 1.45 static int poll_cb ()
329 root 1.1 {
330     dSP;
331     int count = 0;
332 root 1.24 int do_croak = 0;
333 root 1.27 aio_req req;
334    
335     for (;;)
336     {
337     pthread_mutex_lock (&reslock);
338     req = ress;
339    
340     if (req)
341     {
342     ress = req->next;
343 root 1.11
344 root 1.27 if (!ress)
345     {
346     /* read any signals sent by the worker threads */
347     char buf [32];
348     while (read (respipe [0], buf, 32) == 32)
349     ;
350 root 1.30
351     rese = 0;
352 root 1.27 }
353     }
354 root 1.1
355 root 1.27 pthread_mutex_unlock (&reslock);
356 root 1.3
357 root 1.27 if (!req)
358     break;
359 root 1.3
360 root 1.1 nreqs--;
361    
362     if (req->type == REQ_QUIT)
363     started--;
364 root 1.49 else if (req->type == REQ_GROUP && req->length)
365 root 1.46 {
366     req->fd = 1; /* mark request as delayed */
367     continue;
368     }
369 root 1.1 else
370     {
371     if (req->type == REQ_READ)
372 root 1.27 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
373 root 1.1
374 root 1.28 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
375     SvREADONLY_off (req->data);
376    
377 root 1.27 if (req->statdata)
378 root 1.1 {
379     PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
380     PL_laststatval = req->result;
381     PL_statcache = *(req->statdata);
382     }
383    
384 root 1.45 req_invoke (req);
385 root 1.26
386 root 1.1 count++;
387     }
388    
389 root 1.43 req_free (req);
390 root 1.1 }
391    
392     return count;
393     }
394    
395 root 1.4 static void *aio_proc(void *arg);
396    
397 root 1.45 static void start_thread (void)
398 root 1.4 {
399     sigset_t fullsigset, oldsigset;
400     pthread_t tid;
401     pthread_attr_t attr;
402    
403     pthread_attr_init (&attr);
404     pthread_attr_setstacksize (&attr, STACKSIZE);
405 root 1.31 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
406 root 1.4
407     sigfillset (&fullsigset);
408     sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset);
409    
410     if (pthread_create (&tid, &attr, aio_proc, 0) == 0)
411     started++;
412    
413     sigprocmask (SIG_SETMASK, &oldsigset, 0);
414     }
415    
416 root 1.45 static void req_send (aio_req req)
417 root 1.4 {
418 root 1.30 while (started < wanted && nreqs >= started)
419     start_thread ();
420    
421 root 1.4 nreqs++;
422    
423     pthread_mutex_lock (&reqlock);
424    
425     req->next = 0;
426    
427     if (reqe)
428     {
429     reqe->next = req;
430     reqe = req;
431     }
432     else
433     reqe = reqs = req;
434    
435     pthread_cond_signal (&reqwait);
436     pthread_mutex_unlock (&reqlock);
437    
438 root 1.27 if (nreqs > max_outstanding)
439     for (;;)
440     {
441     poll_cb ();
442    
443     if (nreqs <= max_outstanding)
444     break;
445    
446     poll_wait ();
447     }
448 root 1.4 }
449    
450 root 1.45 static void end_thread (void)
451 root 1.4 {
452     aio_req req;
453 root 1.26 Newz (0, req, 1, aio_cb);
454 root 1.4 req->type = REQ_QUIT;
455    
456 root 1.43 req_send (req);
457 root 1.4 }
458    
459 root 1.22 static void min_parallel (int nthreads)
460     {
461 root 1.30 if (wanted < nthreads)
462     wanted = nthreads;
463 root 1.22 }
464    
465     static void max_parallel (int nthreads)
466     {
467     int cur = started;
468 root 1.27
469 root 1.30 if (wanted > nthreads)
470     wanted = nthreads;
471    
472     while (cur > wanted)
473     {
474 root 1.22 end_thread ();
475     cur--;
476     }
477    
478 root 1.30 while (started > wanted)
479 root 1.22 {
480     poll_wait ();
481     poll_cb ();
482     }
483     }
484    
485 root 1.26 static void create_pipe ()
486 root 1.22 {
487 root 1.26 if (pipe (respipe))
488     croak ("unable to initialize result pipe");
489 root 1.22
490 root 1.26 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
491     croak ("cannot set result pipe to nonblocking mode");
492 root 1.22
493 root 1.26 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
494     croak ("cannot set result pipe to nonblocking mode");
495     }
496 root 1.22
497     /*****************************************************************************/
498 root 1.17 /* work around various missing functions */
499    
500     #if !HAVE_PREADWRITE
501     # define pread aio_pread
502     # define pwrite aio_pwrite
503    
504     /*
505     * make our pread/pwrite safe against themselves, but not against
506     * normal read/write by using a mutex. slows down execution a lot,
507     * but that's your problem, not mine.
508     */
509 root 1.37 static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER;
510 root 1.17
511 root 1.45 static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
512 root 1.17 {
513     ssize_t res;
514     off_t ooffset;
515    
516 root 1.37 pthread_mutex_lock (&preadwritelock);
517 root 1.17 ooffset = lseek (fd, 0, SEEK_CUR);
518     lseek (fd, offset, SEEK_SET);
519     res = read (fd, buf, count);
520     lseek (fd, ooffset, SEEK_SET);
521 root 1.37 pthread_mutex_unlock (&preadwritelock);
522 root 1.17
523     return res;
524     }
525    
526 root 1.45 static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
527 root 1.17 {
528     ssize_t res;
529     off_t ooffset;
530    
531 root 1.37 pthread_mutex_lock (&preadwritelock);
532 root 1.17 ooffset = lseek (fd, 0, SEEK_CUR);
533     lseek (fd, offset, SEEK_SET);
534     res = write (fd, buf, count);
535     lseek (fd, offset, SEEK_SET);
536 root 1.37 pthread_mutex_unlock (&preadwritelock);
537 root 1.17
538     return res;
539     }
540     #endif
541    
542     #if !HAVE_FDATASYNC
543     # define fdatasync fsync
544     #endif
545    
546     #if !HAVE_READAHEAD
547     # define readahead aio_readahead
548    
549 root 1.45 static ssize_t readahead (int fd, off_t offset, size_t count)
550 root 1.17 {
551 root 1.37 char readahead_buf[4096];
552    
553 root 1.17 while (count > 0)
554     {
555     size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
556    
557     pread (fd, readahead_buf, len, offset);
558     offset += len;
559     count -= len;
560     }
561    
562     errno = 0;
563     }
564     #endif
565    
566 root 1.37 #if !HAVE_READDIR_R
567     # define readdir_r aio_readdir_r
568    
569     static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER;
570    
571 root 1.45 static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
572 root 1.37 {
573     struct dirent *e;
574     int errorno;
575    
576     pthread_mutex_lock (&readdirlock);
577    
578     e = readdir (dirp);
579     errorno = errno;
580    
581     if (e)
582     {
583     *res = ent;
584     strcpy (ent->d_name, e->d_name);
585     }
586     else
587     *res = 0;
588    
589     pthread_mutex_unlock (&readdirlock);
590    
591     errno = errorno;
592     return e ? 0 : -1;
593     }
594     #endif
595    
596 root 1.32 /* sendfile always needs emulation */
597 root 1.45 static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count)
598 root 1.32 {
599 root 1.37 ssize_t res;
600 root 1.32
601 root 1.37 if (!count)
602     return 0;
603 root 1.32
604 root 1.35 #if HAVE_SENDFILE
605     # if __linux
606 root 1.37 res = sendfile (ofd, ifd, &offset, count);
607 root 1.32
608 root 1.35 # elif __freebsd
609 root 1.37 /*
610     * Of course, the freebsd sendfile is a dire hack with no thoughts
611     * wasted on making it similar to other I/O functions.
612     */
613     {
614     off_t sbytes;
615     res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
616    
617     if (res < 0 && sbytes)
618     /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */
619     res = sbytes;
620     }
621 root 1.32
622 root 1.35 # elif __hpux
623 root 1.37 res = sendfile (ofd, ifd, offset, count, 0, 0);
624 root 1.32
625 root 1.35 # elif __solaris
626 root 1.37 {
627     struct sendfilevec vec;
628     size_t sbytes;
629    
630     vec.sfv_fd = ifd;
631     vec.sfv_flag = 0;
632     vec.sfv_off = offset;
633     vec.sfv_len = count;
634    
635     res = sendfilev (ofd, &vec, 1, &sbytes);
636    
637     if (res < 0 && sbytes)
638     res = sbytes;
639     }
640 root 1.35
641 root 1.38 # endif
642     #else
643 root 1.37 res = -1;
644     errno = ENOSYS;
645 root 1.32 #endif
646    
647 root 1.37 if (res < 0
648     && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
649 root 1.35 #if __solaris
650 root 1.37 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
651 root 1.35 #endif
652 root 1.37 )
653     )
654     {
655     /* emulate sendfile. this is a major pain in the ass */
656     char buf[4096];
657     res = 0;
658    
659 root 1.38 while (count)
660 root 1.37 {
661     ssize_t cnt;
662    
663 root 1.38 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset);
664 root 1.32
665 root 1.37 if (cnt <= 0)
666     {
667     if (cnt && !res) res = -1;
668     break;
669     }
670    
671     cnt = write (ofd, buf, cnt);
672    
673     if (cnt <= 0)
674     {
675     if (cnt && !res) res = -1;
676     break;
677     }
678    
679     offset += cnt;
680     res += cnt;
681 root 1.38 count -= cnt;
682 root 1.37 }
683     }
684    
685     return res;
686     }
687    
688     /* read a full directory */
689 root 1.45 static int scandir_ (const char *path, void **namesp)
690 root 1.37 {
691     DIR *dirp = opendir (path);
692     union
693     {
694     struct dirent d;
695     char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
696     } u;
697     struct dirent *entp;
698     char *name, *names;
699     int memlen = 4096;
700     int memofs = 0;
701     int res = 0;
702     int errorno;
703    
704     if (!dirp)
705     return -1;
706    
707     names = malloc (memlen);
708    
709     for (;;)
710     {
711     errno = 0, readdir_r (dirp, &u.d, &entp);
712    
713     if (!entp)
714     break;
715    
716     name = entp->d_name;
717    
718     if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
719     {
720     int len = strlen (name) + 1;
721    
722     res++;
723    
724     while (memofs + len > memlen)
725     {
726     memlen *= 2;
727     names = realloc (names, memlen);
728     if (!names)
729     break;
730     }
731    
732     memcpy (names + memofs, name, len);
733     memofs += len;
734     }
735     }
736    
737     errorno = errno;
738     closedir (dirp);
739    
740     if (errorno)
741     {
742     free (names);
743     errno = errorno;
744     res = -1;
745     }
746    
747     *namesp = (void *)names;
748     return res;
749 root 1.32 }
750    
751 root 1.22 /*****************************************************************************/
752    
753 root 1.45 static void *aio_proc (void *thr_arg)
754 root 1.1 {
755     aio_req req;
756 root 1.3 int type;
757 root 1.1
758 root 1.3 do
759 root 1.1 {
760 root 1.3 pthread_mutex_lock (&reqlock);
761    
762     for (;;)
763     {
764     req = reqs;
765    
766     if (reqs)
767     {
768     reqs = reqs->next;
769     if (!reqs) reqe = 0;
770     }
771    
772     if (req)
773     break;
774    
775     pthread_cond_wait (&reqwait, &reqlock);
776     }
777    
778     pthread_mutex_unlock (&reqlock);
779    
780 root 1.1 errno = 0; /* strictly unnecessary */
781    
782 root 1.43 if (!req->cancelled)
783     switch (req->type)
784     {
785     case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
786     case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
787 root 1.3
788 root 1.43 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
789     case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break;
790 root 1.16
791 root 1.43 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
792     case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
793     case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
794    
795     case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
796     case REQ_CLOSE: req->result = close (req->fd); break;
797     case REQ_UNLINK: req->result = unlink (req->dataptr); break;
798     case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
799     case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break;
800     case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break;
801     case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
802    
803     case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
804     case REQ_FSYNC: req->result = fsync (req->fd); break;
805     case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break;
806 root 1.1
807 root 1.45 case REQ_SLEEP:
808     {
809     struct timeval tv;
810    
811     tv.tv_sec = req->fd;
812     tv.tv_usec = req->fd2;
813    
814     req->result = select (0, 0, 0, 0, &tv);
815     }
816    
817 root 1.43 case REQ_QUIT:
818     break;
819 root 1.1
820 root 1.43 default:
821     req->result = ENOSYS;
822     break;
823     }
824 root 1.1
825     req->errorno = errno;
826 root 1.3
827     pthread_mutex_lock (&reslock);
828    
829     req->next = 0;
830    
831     if (rese)
832     {
833     rese->next = req;
834     rese = req;
835     }
836     else
837     {
838     rese = ress = req;
839    
840     /* write a dummy byte to the pipe so fh becomes ready */
841     write (respipe [1], &respipe, 1);
842     }
843    
844     pthread_mutex_unlock (&reslock);
845 root 1.1 }
846 root 1.3 while (type != REQ_QUIT);
847 root 1.1
848     return 0;
849     }
850    
851 root 1.37 /*****************************************************************************/
852    
853     static void atfork_prepare (void)
854     {
855     pthread_mutex_lock (&reqlock);
856     pthread_mutex_lock (&reslock);
857     #if !HAVE_PREADWRITE
858     pthread_mutex_lock (&preadwritelock);
859     #endif
860     #if !HAVE_READDIR_R
861     pthread_mutex_lock (&readdirlock);
862     #endif
863     }
864    
865     static void atfork_parent (void)
866     {
867     #if !HAVE_READDIR_R
868     pthread_mutex_unlock (&readdirlock);
869     #endif
870     #if !HAVE_PREADWRITE
871     pthread_mutex_unlock (&preadwritelock);
872     #endif
873     pthread_mutex_unlock (&reslock);
874     pthread_mutex_unlock (&reqlock);
875     }
876    
877     static void atfork_child (void)
878     {
879     aio_req prv;
880    
881     started = 0;
882    
883     while (reqs)
884     {
885     prv = reqs;
886     reqs = prv->next;
887 root 1.43 req_free (prv);
888 root 1.37 }
889    
890     reqs = reqe = 0;
891    
892     while (ress)
893     {
894     prv = ress;
895     ress = prv->next;
896 root 1.43 req_free (prv);
897 root 1.37 }
898    
899     ress = rese = 0;
900    
901     close (respipe [0]);
902     close (respipe [1]);
903     create_pipe ();
904    
905     atfork_parent ();
906     }
907    
908 root 1.22 #define dREQ \
909     aio_req req; \
910     \
911     if (SvOK (callback) && !SvROK (callback)) \
912 root 1.43 croak ("callback must be undef or of reference type"); \
913 root 1.22 \
914     Newz (0, req, 1, aio_cb); \
915     if (!req) \
916     croak ("out of memory during aio_req allocation"); \
917     \
918 root 1.43 req->callback = newSVsv (callback)
919    
920     #define REQ_SEND \
921     req_send (req); \
922     \
923     if (GIMME_V != G_VOID) \
924 root 1.44 XPUSHs (req_sv (req, AIO_REQ_KLASS));
925 root 1.22
926 root 1.1 MODULE = IO::AIO PACKAGE = IO::AIO
927    
928 root 1.8 PROTOTYPES: ENABLE
929    
930 root 1.1 BOOT:
931     {
932 root 1.41 HV *stash = gv_stashpv ("IO::AIO", 1);
933     newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
934     newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
935     newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
936    
937 root 1.26 create_pipe ();
938 root 1.22 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
939 root 1.1 }
940    
941     void
942 root 1.40 min_parallel (nthreads)
943 root 1.1 int nthreads
944     PROTOTYPE: $
945    
946     void
947 root 1.40 max_parallel (nthreads)
948 root 1.1 int nthreads
949     PROTOTYPE: $
950    
951 root 1.4 int
952 root 1.40 max_outstanding (nreqs)
953 root 1.4 int nreqs
954     PROTOTYPE: $
955     CODE:
956     RETVAL = max_outstanding;
957     max_outstanding = nreqs;
958    
959 root 1.1 void
960 root 1.40 aio_open (pathname,flags,mode,callback=&PL_sv_undef)
961 root 1.1 SV * pathname
962     int flags
963     int mode
964     SV * callback
965 root 1.8 PROTOTYPE: $$$;$
966 root 1.43 PPCODE:
967 root 1.1 {
968 root 1.22 dREQ;
969 root 1.1
970     req->type = REQ_OPEN;
971     req->data = newSVsv (pathname);
972 root 1.22 req->dataptr = SvPVbyte_nolen (req->data);
973 root 1.1 req->fd = flags;
974     req->mode = mode;
975    
976 root 1.43 REQ_SEND;
977 root 1.1 }
978    
979     void
980 root 1.40 aio_close (fh,callback=&PL_sv_undef)
981 root 1.13 SV * fh
982     SV * callback
983 root 1.8 PROTOTYPE: $;$
984 root 1.1 ALIAS:
985     aio_close = REQ_CLOSE
986     aio_fsync = REQ_FSYNC
987     aio_fdatasync = REQ_FDATASYNC
988 root 1.43 PPCODE:
989 root 1.1 {
990 root 1.22 dREQ;
991 root 1.1
992     req->type = ix;
993 root 1.13 req->fh = newSVsv (fh);
994     req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
995 root 1.1
996 root 1.43 REQ_SEND (req);
997 root 1.1 }
998    
999     void
1000 root 1.40 aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
1001 root 1.13 SV * fh
1002     UV offset
1003 root 1.32 UV length
1004 root 1.13 SV * data
1005 root 1.32 UV dataoffset
1006 root 1.13 SV * callback
1007     ALIAS:
1008     aio_read = REQ_READ
1009     aio_write = REQ_WRITE
1010 root 1.8 PROTOTYPE: $$$$$;$
1011 root 1.43 PPCODE:
1012 root 1.13 {
1013     aio_req req;
1014     STRLEN svlen;
1015 root 1.21 char *svptr = SvPVbyte (data, svlen);
1016 root 1.13
1017     SvUPGRADE (data, SVt_PV);
1018     SvPOK_on (data);
1019 root 1.1
1020 root 1.13 if (dataoffset < 0)
1021     dataoffset += svlen;
1022    
1023     if (dataoffset < 0 || dataoffset > svlen)
1024     croak ("data offset outside of string");
1025    
1026     if (ix == REQ_WRITE)
1027     {
1028     /* write: check length and adjust. */
1029     if (length < 0 || length + dataoffset > svlen)
1030     length = svlen - dataoffset;
1031     }
1032     else
1033     {
1034     /* read: grow scalar as necessary */
1035     svptr = SvGROW (data, length + dataoffset);
1036     }
1037    
1038     if (length < 0)
1039     croak ("length must not be negative");
1040    
1041 root 1.22 {
1042     dREQ;
1043 root 1.13
1044 root 1.22 req->type = ix;
1045     req->fh = newSVsv (fh);
1046     req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
1047     : IoOFP (sv_2io (fh)));
1048     req->offset = offset;
1049     req->length = length;
1050     req->data = SvREFCNT_inc (data);
1051     req->dataptr = (char *)svptr + dataoffset;
1052 root 1.13
1053 root 1.28 if (!SvREADONLY (data))
1054     {
1055     SvREADONLY_on (data);
1056     req->data2ptr = (void *)data;
1057     }
1058    
1059 root 1.43 REQ_SEND;
1060 root 1.22 }
1061 root 1.13 }
1062 root 1.1
1063     void
1064 root 1.40 aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef)
1065 root 1.32 SV * out_fh
1066     SV * in_fh
1067     UV in_offset
1068     UV length
1069     SV * callback
1070     PROTOTYPE: $$$$;$
1071 root 1.43 PPCODE:
1072 root 1.32 {
1073     dREQ;
1074    
1075     req->type = REQ_SENDFILE;
1076     req->fh = newSVsv (out_fh);
1077     req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
1078     req->fh2 = newSVsv (in_fh);
1079     req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
1080     req->offset = in_offset;
1081     req->length = length;
1082    
1083 root 1.43 REQ_SEND;
1084 root 1.32 }
1085    
1086     void
1087 root 1.40 aio_readahead (fh,offset,length,callback=&PL_sv_undef)
1088 root 1.13 SV * fh
1089     UV offset
1090     IV length
1091     SV * callback
1092 root 1.8 PROTOTYPE: $$$;$
1093 root 1.43 PPCODE:
1094 root 1.1 {
1095 root 1.22 dREQ;
1096 root 1.1
1097     req->type = REQ_READAHEAD;
1098 root 1.13 req->fh = newSVsv (fh);
1099     req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
1100 root 1.1 req->offset = offset;
1101     req->length = length;
1102    
1103 root 1.43 REQ_SEND;
1104 root 1.1 }
1105    
1106     void
1107 root 1.40 aio_stat (fh_or_path,callback=&PL_sv_undef)
1108 root 1.1 SV * fh_or_path
1109     SV * callback
1110     ALIAS:
1111 root 1.8 aio_stat = REQ_STAT
1112     aio_lstat = REQ_LSTAT
1113 root 1.43 PPCODE:
1114 root 1.1 {
1115 root 1.22 dREQ;
1116 root 1.1
1117     New (0, req->statdata, 1, Stat_t);
1118     if (!req->statdata)
1119 root 1.27 {
1120 root 1.43 req_free (req);
1121 root 1.27 croak ("out of memory during aio_req->statdata allocation");
1122     }
1123 root 1.1
1124     if (SvPOK (fh_or_path))
1125     {
1126 root 1.8 req->type = ix;
1127 root 1.1 req->data = newSVsv (fh_or_path);
1128 root 1.22 req->dataptr = SvPVbyte_nolen (req->data);
1129 root 1.1 }
1130     else
1131     {
1132     req->type = REQ_FSTAT;
1133 root 1.13 req->fh = newSVsv (fh_or_path);
1134 root 1.1 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1135     }
1136    
1137 root 1.43 REQ_SEND;
1138 root 1.1 }
1139    
1140     void
1141 root 1.40 aio_unlink (pathname,callback=&PL_sv_undef)
1142 root 1.1 SV * pathname
1143     SV * callback
1144 root 1.22 ALIAS:
1145 root 1.40 aio_unlink = REQ_UNLINK
1146     aio_rmdir = REQ_RMDIR
1147     aio_readdir = REQ_READDIR
1148 root 1.43 PPCODE:
1149 root 1.1 {
1150 root 1.22 dREQ;
1151 root 1.1
1152 root 1.22 req->type = ix;
1153     req->data = newSVsv (pathname);
1154     req->dataptr = SvPVbyte_nolen (req->data);
1155 root 1.1
1156 root 1.43 REQ_SEND;
1157 root 1.22 }
1158    
1159     void
1160 root 1.40 aio_link (oldpath,newpath,callback=&PL_sv_undef)
1161 root 1.22 SV * oldpath
1162     SV * newpath
1163     SV * callback
1164 root 1.40 ALIAS:
1165     aio_link = REQ_LINK
1166     aio_symlink = REQ_SYMLINK
1167     aio_rename = REQ_RENAME
1168 root 1.43 PPCODE:
1169 root 1.22 {
1170     dREQ;
1171 root 1.1
1172 root 1.40 req->type = ix;
1173 root 1.22 req->fh = newSVsv (oldpath);
1174     req->data2ptr = SvPVbyte_nolen (req->fh);
1175     req->data = newSVsv (newpath);
1176     req->dataptr = SvPVbyte_nolen (req->data);
1177 root 1.1
1178 root 1.43 REQ_SEND;
1179 root 1.1 }
1180    
1181 root 1.42 void
1182 root 1.45 aio_sleep (delay,callback=&PL_sv_undef)
1183     double delay
1184     SV * callback
1185     PPCODE:
1186     {
1187     dREQ;
1188    
1189     req->type = REQ_SLEEP;
1190     req->fd = delay < 0. ? 0 : delay;
1191     req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd);
1192    
1193     REQ_SEND;
1194     }
1195    
1196     void
1197 root 1.44 aio_group (callback=&PL_sv_undef)
1198     SV * callback
1199 root 1.46 PROTOTYPE: ;$
1200 root 1.44 PPCODE:
1201 root 1.42 {
1202 root 1.44 dREQ;
1203     req->type = REQ_GROUP;
1204 root 1.45 req_send (req);
1205 root 1.44 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1206 root 1.42 }
1207    
1208 root 1.6 void
1209 root 1.40 flush ()
1210 root 1.6 PROTOTYPE:
1211     CODE:
1212     while (nreqs)
1213     {
1214     poll_wait ();
1215     poll_cb ();
1216     }
1217    
1218 root 1.7 void
1219     poll()
1220     PROTOTYPE:
1221     CODE:
1222     if (nreqs)
1223     {
1224     poll_wait ();
1225     poll_cb ();
1226     }
1227    
1228 root 1.1 int
1229     poll_fileno()
1230     PROTOTYPE:
1231     CODE:
1232 root 1.3 RETVAL = respipe [0];
1233 root 1.1 OUTPUT:
1234     RETVAL
1235    
1236     int
1237     poll_cb(...)
1238     PROTOTYPE:
1239     CODE:
1240 root 1.5 RETVAL = poll_cb ();
1241 root 1.1 OUTPUT:
1242     RETVAL
1243    
1244     void
1245     poll_wait()
1246     PROTOTYPE:
1247     CODE:
1248 root 1.3 if (nreqs)
1249     poll_wait ();
1250 root 1.1
1251     int
1252     nreqs()
1253     PROTOTYPE:
1254     CODE:
1255     RETVAL = nreqs;
1256     OUTPUT:
1257     RETVAL
1258    
1259 root 1.48 PROTOTYPES: DISABLE
1260    
1261 root 1.44 MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1262 root 1.43
1263     void
1264     cancel (aio_req_ornot req)
1265     PROTOTYPE:
1266     CODE:
1267 root 1.45 req_cancel (req);
1268    
1269     MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1270    
1271     void
1272     add (aio_req grp, ...)
1273     PPCODE:
1274     {
1275     int i;
1276    
1277 root 1.49 if (grp->fd == 2)
1278     croak ("cannot add requests to IO::AIO::GRP after the group finished");
1279    
1280 root 1.45 for (i = 1; i < items; ++i )
1281     {
1282 root 1.46 if (GIMME_V != G_VOID)
1283     XPUSHs (sv_2mortal (newSVsv (ST (i))));
1284    
1285 root 1.45 aio_req req = SvAIO_REQ (ST (i));
1286    
1287 root 1.46 if (req)
1288     {
1289 root 1.49 ++grp->length;
1290     req->grp = grp;
1291    
1292     req->grp_prev = 0;
1293     req->grp_next = grp->grp_first;
1294 root 1.45
1295 root 1.49 if (grp->grp_first)
1296     grp->grp_first->grp_prev = req;
1297    
1298     grp->grp_first = req;
1299 root 1.46 }
1300 root 1.45 }
1301     }
1302 root 1.43
1303 root 1.48 void
1304     result (aio_req grp, ...)
1305     CODE:
1306     {
1307     int i;
1308     AV *av = newAV ();
1309    
1310     for (i = 1; i < items; ++i )
1311     av_push (av, newSVsv (ST (i)));
1312    
1313 root 1.49 SvREFCNT_dec (grp->data);
1314 root 1.48 grp->data = (SV *)av;
1315     }
1316    
1317 root 1.49 void
1318     feeder_limit (aio_req grp, int limit)
1319     CODE:
1320     grp->fd2 = limit;
1321     aio_grp_feed (grp);
1322    
1323     void
1324     set_feeder (aio_req grp, SV *callback=&PL_sv_undef)
1325     CODE:
1326     {
1327     SvREFCNT_dec (grp->fh2);
1328     grp->fh2 = newSVsv (callback);
1329    
1330     if (grp->fd2 <= 0)
1331     grp->fd2 = 2;
1332    
1333     aio_grp_feed (grp);
1334     }
1335