ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
Revision: 1.142
Committed: Sat Jun 6 18:19:35 2009 UTC (14 years, 11 months ago) by root
Branch: MAIN
Changes since 1.141: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.115 #include "libeio/xthread.h"
2 root 1.63
3 root 1.19 #include <errno.h>
4    
5 root 1.15 #include "EXTERN.h"
6 root 1.1 #include "perl.h"
7     #include "XSUB.h"
8    
9 root 1.37 #include <stddef.h>
10 root 1.94 #include <stdlib.h>
11 root 1.41 #include <errno.h>
12 root 1.1 #include <sys/types.h>
13     #include <sys/stat.h>
14 root 1.37 #include <limits.h>
15 root 1.1 #include <fcntl.h>
16     #include <sched.h>
17 root 1.103
18 root 1.118 /* perl namespace pollution */
19     #undef VERSION
20    
21 root 1.103 #ifdef _WIN32
22    
23 root 1.115 # define EIO_STRUCT_DIRENT Direntry_t
24     # undef malloc
25     # undef free
26 root 1.103
27     // perl overrides all those nice win32 functions
28     # undef open
29     # undef read
30     # undef write
31 root 1.104 # undef send
32     # undef recv
33 root 1.103 # undef stat
34     # undef fstat
35     # define lstat stat
36     # undef truncate
37     # undef ftruncate
38     # undef open
39     # undef close
40     # undef unlink
41     # undef rmdir
42     # undef rename
43     # undef lseek
44    
45     # define chown(a,b,c) (errno = ENOSYS, -1)
46     # define fchown(a,b,c) (errno = ENOSYS, -1)
47     # define fchmod(a,b) (errno = ENOSYS, -1)
48     # define symlink(a,b) (errno = ENOSYS, -1)
49     # define readlink(a,b,c) (errno = ENOSYS, -1)
50     # define mknod(a,b,c) (errno = ENOSYS, -1)
51     # define truncate(a,b) (errno = ENOSYS, -1)
52     # define ftruncate(fd,o) chsize ((fd), (o))
53     # define fsync(fd) _commit (fd)
54     # define opendir(fd) (errno = ENOSYS, 0)
55     # define readdir(fd) (errno = ENOSYS, -1)
56     # define closedir(fd) (errno = ENOSYS, -1)
57     # define mkdir(a,b) mkdir (a)
58    
59     #else
60    
61     # include <sys/time.h>
62     # include <sys/select.h>
63     # include <unistd.h>
64     # include <utime.h>
65     # include <signal.h>
66 root 1.115 # define EIO_STRUCT_DIRENT struct dirent
67 root 1.103
68     #endif
69 root 1.1
70 root 1.125 /* perl stupidly overrides readdir and maybe others */
71     /* with thread-unsafe versions, imagine that :( */
72     #undef readdir
73     #undef opendir
74     #undef closedir
75    
76 root 1.115 #define EIO_STRUCT_STAT Stat_t
77 root 1.65
78 root 1.101 /* use NV for 32 bit perls as it allows larger offsets */
79     #if IVSIZE >= 8
80     # define SvVAL64 SvIV
81     #else
82     # define SvVAL64 SvNV
83     #endif
84    
85 root 1.127 /*****************************************************************************/
86    
87     #if __GNUC__ >= 3
88     # define expect(expr,value) __builtin_expect ((expr),(value))
89     #else
90     # define expect(expr,value) (expr)
91     #endif
92    
93     #define expect_false(expr) expect ((expr) != 0, 0)
94     #define expect_true(expr) expect ((expr) != 0, 1)
95    
96     /*****************************************************************************/
97    
98 root 1.107 static HV *stash;
99 root 1.90 typedef SV SV8; /* byte-sv, used for argument-checking */
100    
101 root 1.44 #define AIO_REQ_KLASS "IO::AIO::REQ"
102     #define AIO_GRP_KLASS "IO::AIO::GRP"
103 root 1.43
104 root 1.121 #define EIO_REQ_MEMBERS \
105 root 1.115 SV *callback; \
106     SV *sv1, *sv2; \
107     STRLEN stroffset; \
108 root 1.121 SV *self;
109 root 1.115
110 root 1.122 #define EIO_NO_WRAPPERS 1
111    
112 root 1.115 #include "libeio/eio.h"
113    
114     static int req_invoke (eio_req *req);
115     #define EIO_FINISH(req) req_invoke (req)
116     static void req_destroy (eio_req *grp);
117     #define EIO_DESTROY(req) req_destroy (req)
118 root 1.1
119 root 1.58 enum {
120 root 1.115 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */
121 root 1.58 };
122    
123 root 1.115 #include "libeio/eio.c"
124 root 1.1
125 root 1.115 typedef eio_req *aio_req;
126     typedef eio_req *aio_req_ornot;
127 root 1.60
128 root 1.117 static SV *on_next_submit;
129 root 1.121 static int next_pri = EIO_PRI_DEFAULT;
130 root 1.117 static int max_outstanding;
131 root 1.94
132 root 1.106 static int respipe_osf [2], respipe [2] = { -1, -1 };
133 root 1.80
134 root 1.115 static void req_destroy (aio_req req);
135     static void req_cancel (aio_req req);
136 root 1.80
137 root 1.115 static void want_poll (void)
138 root 1.80 {
139 root 1.115 /* write a dummy byte to the pipe so fh becomes ready */
140     respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
141 root 1.67 }
142    
143 root 1.115 static void done_poll (void)
144 root 1.67 {
145 root 1.115 /* read any signals sent by the worker threads */
146     char buf [4];
147     while (respipe_read (respipe [0], buf, 4) == 4)
148     ;
149 root 1.67 }
150 root 1.1
151 root 1.43 /* must be called at most once */
152 root 1.44 static SV *req_sv (aio_req req, const char *klass)
153 root 1.43 {
154 root 1.49 if (!req->self)
155     {
156     req->self = (SV *)newHV ();
157     sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
158     }
159 root 1.43
160 root 1.45 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1)));
161 root 1.43 }
162    
163 root 1.45 static aio_req SvAIO_REQ (SV *sv)
164 root 1.26 {
165 root 1.53 MAGIC *mg;
166    
167 root 1.45 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv))
168     croak ("object of class " AIO_REQ_KLASS " expected");
169 root 1.43
170 root 1.53 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
171 root 1.43
172     return mg ? (aio_req)mg->mg_ptr : 0;
173     }
174    
175 root 1.49 static void aio_grp_feed (aio_req grp)
176     {
177 root 1.115 if (grp->sv2 && SvOK (grp->sv2))
178 root 1.49 {
179 root 1.115 dSP;
180 root 1.49
181 root 1.115 ENTER;
182     SAVETMPS;
183     PUSHMARK (SP);
184     XPUSHs (req_sv (grp, AIO_GRP_KLASS));
185     PUTBACK;
186     call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR);
187     SPAGAIN;
188     FREETMPS;
189     LEAVE;
190 root 1.50 }
191     }
192    
193 root 1.117 static void req_submit (eio_req *req)
194     {
195     eio_submit (req);
196    
197 root 1.127 if (expect_false (on_next_submit))
198 root 1.117 {
199     dSP;
200     SV *cb = sv_2mortal (on_next_submit);
201    
202     on_next_submit = 0;
203    
204     PUSHMARK (SP);
205     PUTBACK;
206     call_sv (cb, G_DISCARD | G_EVAL);
207     }
208     }
209    
210 root 1.115 static int req_invoke (eio_req *req)
211 root 1.45 {
212     dSP;
213    
214 root 1.100 if (req->flags & FLAG_SV2_RO_OFF)
215     SvREADONLY_off (req->sv2);
216 root 1.86
217 root 1.128 if (!EIO_CANCELLED (req) && req->callback)
218 root 1.67 {
219     ENTER;
220     SAVETMPS;
221     PUSHMARK (SP);
222     EXTEND (SP, 1);
223 root 1.45
224 root 1.67 switch (req->type)
225 root 1.45 {
226 root 1.115 case EIO_READDIR:
227 root 1.45 {
228 root 1.67 SV *rv = &PL_sv_undef;
229 root 1.45
230 root 1.67 if (req->result >= 0)
231 root 1.45 {
232 root 1.71 int i;
233 root 1.86 char *buf = req->ptr2;
234 root 1.67 AV *av = newAV ();
235    
236 root 1.71 av_extend (av, req->result - 1);
237    
238     for (i = 0; i < req->result; ++i)
239 root 1.67 {
240 root 1.141 if (req->int1 & EIO_READDIR_DENTS)
241     {
242     eio_dirent *ent = (eio_dirent *)buf;
243     SV *namesv = newSVpvn (ent->name, ent->namelen);
244    
245     if (req->int1 & EIO_READDIR_CUSTOM2)
246     {
247     static SV *sv_type [EIO_DT_MAX + 1]; /* type sv cache */
248     AV *avent = newAV ();
249    
250     av_extend (avent, 2);
251    
252     if (!sv_type [ent->type])
253     {
254     sv_type [ent->type] = newSViv (ent->type);
255     SvREADONLY_on (sv_type [ent->type]);
256     }
257    
258     av_store (avent, 0, namesv);
259     av_store (avent, 1, IVSIZE >= 8 ? newSVuv (ent->inode) : newSVnv (ent->inode));
260     av_store (avent, 2, SvREFCNT_inc (sv_type [ent->type]));
261    
262     av_store (av, i, newRV_noinc ((SV *)avent));
263     }
264     else
265     av_store (av, i, namesv);
266    
267     buf += sizeof (eio_dirent);
268     }
269     else
270     {
271     SV *name = newSVpv (buf, 0);
272     av_store (av, i, name);
273     buf += SvCUR (name) + 1;
274     }
275 root 1.67 }
276 root 1.45
277 root 1.67 rv = sv_2mortal (newRV_noinc ((SV *)av));
278 root 1.45 }
279    
280 root 1.67 PUSHs (rv);
281 root 1.141
282     if (req->int1 & EIO_READDIR_CUSTOM1)
283     XPUSHs (sv_2mortal (newSViv (req->int1 & ~(EIO_READDIR_CUSTOM1 | EIO_READDIR_CUSTOM2))));
284 root 1.45 }
285 root 1.67 break;
286 root 1.45
287 root 1.115 case EIO_OPEN:
288 root 1.67 {
289     /* convert fd to fh */
290 root 1.107 SV *fh = &PL_sv_undef;
291    
292     if (req->result >= 0)
293     {
294     GV *gv = (GV *)sv_newmortal ();
295     int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR);
296     char sym [64];
297     int symlen;
298    
299     symlen = snprintf (sym, sizeof (sym), "fd#%d", req->result);
300     gv_init (gv, stash, sym, symlen, 0);
301    
302     symlen = snprintf (
303     sym,
304     sizeof (sym),
305     "%s&=%d",
306     flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<",
307     req->result
308     );
309 root 1.45
310 root 1.107 if (do_open (gv, sym, symlen, 0, 0, 0, 0))
311 root 1.108 fh = (SV *)gv;
312 root 1.107 }
313 root 1.45
314 root 1.109 PUSHs (fh);
315 root 1.67 }
316     break;
317 root 1.45
318 root 1.115 case EIO_GROUP:
319 root 1.86 req->int1 = 2; /* mark group as finished */
320 root 1.45
321 root 1.86 if (req->sv1)
322 root 1.67 {
323     int i;
324 root 1.86 AV *av = (AV *)req->sv1;
325 root 1.49
326 root 1.67 EXTEND (SP, AvFILL (av) + 1);
327     for (i = 0; i <= AvFILL (av); ++i)
328     PUSHs (*av_fetch (av, i, 0));
329     }
330     break;
331 root 1.48
332 root 1.115 case EIO_NOP:
333     case EIO_BUSY:
334 root 1.67 break;
335 root 1.48
336 root 1.115 case EIO_READLINK:
337 root 1.86 if (req->result > 0)
338 root 1.119 PUSHs (sv_2mortal (newSVpvn (req->ptr2, req->result)));
339 root 1.86 break;
340    
341 root 1.115 case EIO_STAT:
342     case EIO_LSTAT:
343     case EIO_FSTAT:
344     PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT;
345 root 1.87 PL_laststatval = req->result;
346 root 1.115 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2);
347 root 1.87 PUSHs (sv_2mortal (newSViv (req->result)));
348     break;
349    
350 root 1.115 case EIO_READ:
351 root 1.138 {
352     SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0));
353     *SvEND (req->sv2) = 0;
354     SvPOK_only (req->sv2);
355     SvSETMAGIC (req->sv2);
356     PUSHs (sv_2mortal (newSViv (req->result)));
357     }
358 root 1.87 break;
359    
360 root 1.115 case EIO_DUP2:
361     if (req->result > 0)
362     req->result = 0;
363     /* FALLTHROUGH */
364    
365 root 1.67 default:
366     PUSHs (sv_2mortal (newSViv (req->result)));
367     break;
368     }
369 root 1.45
370 root 1.79 errno = req->errorno;
371 root 1.51
372 root 1.67 PUTBACK;
373 root 1.109 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD);
374 root 1.67 SPAGAIN;
375 root 1.51
376 root 1.67 FREETMPS;
377     LEAVE;
378 root 1.109
379     PUTBACK;
380 root 1.45 }
381    
382 root 1.115 return !!SvTRUE (ERRSV);
383 root 1.67 }
384    
385 root 1.101 static void req_destroy (aio_req req)
386 root 1.67 {
387 root 1.43 if (req->self)
388     {
389     sv_unmagic (req->self, PERL_MAGIC_ext);
390     SvREFCNT_dec (req->self);
391     }
392    
393 root 1.86 SvREFCNT_dec (req->sv1);
394     SvREFCNT_dec (req->sv2);
395 root 1.49 SvREFCNT_dec (req->callback);
396 root 1.26
397     Safefree (req);
398     }
399    
400 root 1.72 static void req_cancel_subs (aio_req grp)
401     {
402     aio_req sub;
403    
404 root 1.115 if (grp->type != EIO_GROUP)
405 root 1.72 return;
406    
407 root 1.86 SvREFCNT_dec (grp->sv2);
408     grp->sv2 = 0;
409 root 1.72
410 root 1.115 eio_grp_cancel (grp);
411 root 1.1 }
412    
413 root 1.104 #ifdef USE_SOCKETS_AS_HANDLES
414     # define TO_SOCKET(x) (win32_get_osfhandle (x))
415     #else
416     # define TO_SOCKET(x) (x)
417     #endif
418    
419     static void
420 root 1.111 create_respipe (void)
421 root 1.104 {
422 root 1.106 int old_readfd = respipe [0];
423    
424     if (respipe [1] >= 0)
425     respipe_close (TO_SOCKET (respipe [1]));
426    
427     #ifdef _WIN32
428     if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe))
429     #else
430     if (pipe (respipe))
431     #endif
432     croak ("unable to initialize result pipe");
433    
434     if (old_readfd >= 0)
435     {
436     if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
437     croak ("unable to initialize result pipe(2)");
438    
439     respipe_close (respipe [0]);
440     respipe [0] = old_readfd;
441     }
442    
443 root 1.104 #ifdef _WIN32
444     int arg = 1;
445 root 1.106 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
446     || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
447 root 1.104 #else
448 root 1.106 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
449     || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
450 root 1.104 #endif
451 root 1.106 croak ("unable to initialize result pipe(3)");
452 root 1.104
453     respipe_osf [0] = TO_SOCKET (respipe [0]);
454     respipe_osf [1] = TO_SOCKET (respipe [1]);
455     }
456    
457 root 1.111 static void poll_wait (void)
458 root 1.80 {
459     fd_set rfd;
460    
461 root 1.115 while (eio_nreqs ())
462 root 1.30 {
463 root 1.80 int size;
464 root 1.115
465     X_LOCK (reslock);
466 root 1.80 size = res_queue.size;
467 root 1.115 X_UNLOCK (reslock);
468 root 1.80
469     if (size)
470     return;
471    
472 root 1.123 etp_maybe_start_thread ();
473 root 1.80
474 root 1.104 FD_ZERO (&rfd);
475     FD_SET (respipe [0], &rfd);
476 root 1.80
477 root 1.104 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
478 root 1.22 }
479 root 1.80 }
480    
481 root 1.111 static int poll_cb (void)
482 root 1.80 {
483 root 1.124 for (;;)
484 root 1.116 {
485 root 1.124 int res = eio_poll ();
486 root 1.116
487     if (res > 0)
488     croak (0);
489 root 1.124
490     if (!max_outstanding || max_outstanding > eio_nreqs ())
491     return res;
492    
493     poll_wait ();
494 root 1.116 }
495 root 1.17 }
496 root 1.37
497 root 1.116 static void atfork_child (void)
498     {
499     create_respipe ();
500     }
501    
502 root 1.128 static SV *
503     get_cb (SV *cb_sv)
504     {
505     HV *st;
506     GV *gvp;
507     CV *cv;
508    
509     if (!SvOK (cb_sv))
510     return 0;
511    
512     cv = sv_2cv (cb_sv, &st, &gvp, 0);
513    
514     if (!cv)
515 root 1.129 croak ("IO::AIO callback must be undef or a CODE reference");
516 root 1.128
517     return (SV *)cv;
518     }
519    
520 root 1.22 #define dREQ \
521 root 1.128 SV *cb_cv; \
522 root 1.22 aio_req req; \
523 root 1.60 int req_pri = next_pri; \
524 root 1.121 next_pri = EIO_PRI_DEFAULT; \
525 root 1.22 \
526 root 1.128 cb_cv = get_cb (callback); \
527 root 1.22 \
528 root 1.115 Newz (0, req, 1, eio_req); \
529 root 1.22 if (!req) \
530 root 1.115 croak ("out of memory during eio_req allocation"); \
531 root 1.22 \
532 root 1.128 req->callback = SvREFCNT_inc (cb_cv); \
533 root 1.60 req->pri = req_pri
534 root 1.43
535     #define REQ_SEND \
536 root 1.126 PUTBACK; \
537 root 1.117 req_submit (req); \
538 root 1.126 SPAGAIN; \
539 root 1.43 \
540     if (GIMME_V != G_VOID) \
541 root 1.44 XPUSHs (req_sv (req, AIO_REQ_KLASS));
542 root 1.136
543     static int
544     extract_fd (SV *fh, int wr)
545     {
546     int fd = PerlIO_fileno (wr ? IoOFP (sv_2io (fh)) : IoIFP (sv_2io (fh)));
547    
548     if (fd < 0)
549     croak ("illegal fh argument, either not an OS file or read/write mode mismatch");
550    
551     return fd;
552     }
553    
554 root 1.1 MODULE = IO::AIO PACKAGE = IO::AIO
555    
556 root 1.8 PROTOTYPES: ENABLE
557    
558 root 1.1 BOOT:
559     {
560 root 1.141 stash = gv_stashpv ("IO::AIO", 1);
561 root 1.82
562 root 1.141 static const struct {
563     const char *name;
564     IV iv;
565     } *civ, const_iv[] = {
566     # define const_iv(name, value) { # name, (IV) value },
567     # define const_eio(name) { # name, (IV) EIO_ ## name },
568     const_iv (EXDEV , EXDEV)
569     const_iv (ENOSYS , ENOSYS)
570     const_iv (O_RDONLY, O_RDONLY)
571     const_iv (O_WRONLY, O_WRONLY)
572     const_iv (O_CREAT , O_CREAT)
573     const_iv (O_TRUNC , O_TRUNC)
574 root 1.122 #ifndef _WIN32
575 root 1.141 const_iv (S_IFIFO , S_IFIFO)
576 root 1.103 #endif
577 root 1.141 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE)
578     const_eio (SYNC_FILE_RANGE_WRITE)
579     const_eio (SYNC_FILE_RANGE_WAIT_AFTER)
580    
581     const_eio (READDIR_DENTS)
582     const_eio (READDIR_DIRS_FIRST)
583     const_eio (READDIR_STAT_ORDER)
584     const_eio (READDIR_FOUND_UNKNOWN)
585    
586     const_eio (DT_UNKNOWN)
587     const_eio (DT_FIFO)
588     const_eio (DT_CHR)
589     const_eio (DT_DIR)
590     const_eio (DT_BLK)
591     const_eio (DT_REG)
592     const_eio (DT_LNK)
593     const_eio (DT_SOCK)
594     const_eio (DT_WHT)
595     };
596 root 1.103
597 root 1.141 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
598     newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
599    
600     create_respipe ();
601 root 1.41
602 root 1.141 if (eio_init (want_poll, done_poll) < 0)
603     croak ("IO::AIO: unable to initialise eio library");
604 root 1.116
605 root 1.141 /* atfork child called in fifo order, so before eio's handler */
606     X_THREAD_ATFORK (0, 0, atfork_child);
607 root 1.1 }
608    
609     void
610 root 1.85 max_poll_reqs (int nreqs)
611     PROTOTYPE: $
612     CODE:
613 root 1.115 eio_set_max_poll_reqs (nreqs);
614 root 1.85
615     void
616     max_poll_time (double nseconds)
617     PROTOTYPE: $
618     CODE:
619 root 1.115 eio_set_max_poll_time (nseconds);
620 root 1.85
621     void
622 root 1.78 min_parallel (int nthreads)
623 root 1.1 PROTOTYPE: $
624 root 1.115 CODE:
625     eio_set_min_parallel (nthreads);
626 root 1.1
627     void
628 root 1.78 max_parallel (int nthreads)
629     PROTOTYPE: $
630 root 1.115 CODE:
631     eio_set_max_parallel (nthreads);
632 root 1.78
633 root 1.85 void
634     max_idle (int nthreads)
635     PROTOTYPE: $
636     CODE:
637 root 1.115 eio_set_max_idle (nthreads);
638 root 1.85
639 root 1.115 void
640 root 1.78 max_outstanding (int maxreqs)
641 root 1.1 PROTOTYPE: $
642 root 1.78 CODE:
643     max_outstanding = maxreqs;
644 root 1.1
645     void
646 root 1.99 aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef)
647 root 1.8 PROTOTYPE: $$$;$
648 root 1.43 PPCODE:
649 root 1.1 {
650 root 1.22 dREQ;
651 root 1.1
652 root 1.115 req->type = EIO_OPEN;
653 root 1.86 req->sv1 = newSVsv (pathname);
654 root 1.89 req->ptr1 = SvPVbyte_nolen (req->sv1);
655 root 1.86 req->int1 = flags;
656 root 1.115 req->int2 = mode;
657 root 1.1
658 root 1.43 REQ_SEND;
659 root 1.1 }
660    
661     void
662 root 1.107 aio_fsync (SV *fh, SV *callback=&PL_sv_undef)
663 root 1.8 PROTOTYPE: $;$
664 root 1.1 ALIAS:
665 root 1.115 aio_fsync = EIO_FSYNC
666     aio_fdatasync = EIO_FDATASYNC
667 root 1.43 PPCODE:
668 root 1.1 {
669 root 1.136 int fd = extract_fd (fh, 0);
670 root 1.22 dREQ;
671 root 1.1
672     req->type = ix;
673 root 1.99 req->sv1 = newSVsv (fh);
674 root 1.136 req->int1 = fd;
675 root 1.1
676 root 1.43 REQ_SEND (req);
677 root 1.1 }
678    
679     void
680 root 1.133 aio_sync_file_range (SV *fh, SV *offset, SV *nbytes, IV flags, SV *callback=&PL_sv_undef)
681     PROTOTYPE: $$$$;$
682     PPCODE:
683     {
684 root 1.136 int fd = extract_fd (fh, 0);
685 root 1.133 dREQ;
686    
687     req->type = EIO_SYNC_FILE_RANGE;
688     req->sv1 = newSVsv (fh);
689 root 1.136 req->int1 = fd;
690 root 1.133 req->offs = SvVAL64 (offset);
691     req->size = SvVAL64 (nbytes);
692     req->int2 = flags;
693    
694     REQ_SEND (req);
695     }
696    
697     void
698 root 1.113 aio_close (SV *fh, SV *callback=&PL_sv_undef)
699 root 1.107 PROTOTYPE: $;$
700     PPCODE:
701     {
702 root 1.115 static int close_pipe = -1; /* dummy fd to close fds via dup2 */
703 root 1.136 int fd = extract_fd (fh, 0);
704 root 1.109 dREQ;
705 root 1.107
706 root 1.115 if (close_pipe < 0)
707     {
708     int pipefd [2];
709    
710     if (pipe (pipefd) < 0
711     || close (pipefd [1]) < 0
712     || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0)
713     abort (); /*D*/
714    
715     close_pipe = pipefd [0];
716     }
717    
718     req->type = EIO_DUP2;
719     req->int1 = close_pipe;
720     req->sv2 = newSVsv (fh);
721 root 1.136 req->int2 = fd;
722 root 1.107
723 root 1.109 REQ_SEND (req);
724 root 1.107 }
725    
726     void
727 root 1.102 aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef)
728 root 1.13 ALIAS:
729 root 1.115 aio_read = EIO_READ
730     aio_write = EIO_WRITE
731 root 1.8 PROTOTYPE: $$$$$;$
732 root 1.43 PPCODE:
733 root 1.13 {
734     STRLEN svlen;
735 root 1.137 int fd = extract_fd (fh, ix == EIO_WRITE);
736 root 1.21 char *svptr = SvPVbyte (data, svlen);
737 root 1.102 UV len = SvUV (length);
738 root 1.134
739 root 1.13 if (dataoffset < 0)
740     dataoffset += svlen;
741    
742     if (dataoffset < 0 || dataoffset > svlen)
743 root 1.102 croak ("dataoffset outside of data scalar");
744 root 1.13
745 root 1.115 if (ix == EIO_WRITE)
746 root 1.13 {
747     /* write: check length and adjust. */
748 root 1.102 if (!SvOK (length) || len + dataoffset > svlen)
749     len = svlen - dataoffset;
750 root 1.13 }
751     else
752     {
753 root 1.138 /* read: check type and grow scalar as necessary */
754     SvUPGRADE (data, SVt_PV);
755 root 1.102 svptr = SvGROW (data, len + dataoffset + 1);
756 root 1.13 }
757    
758 root 1.22 {
759     dREQ;
760 root 1.13
761 root 1.22 req->type = ix;
762 root 1.99 req->sv1 = newSVsv (fh);
763 root 1.135 req->int1 = fd;
764 root 1.101 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
765 root 1.102 req->size = len;
766 root 1.132 req->sv2 = SvREFCNT_inc (data);
767 root 1.115 req->ptr2 = (char *)svptr + dataoffset;
768 root 1.86 req->stroffset = dataoffset;
769 root 1.13
770 root 1.28 if (!SvREADONLY (data))
771     {
772     SvREADONLY_on (data);
773 root 1.100 req->flags |= FLAG_SV2_RO_OFF;
774 root 1.28 }
775    
776 root 1.43 REQ_SEND;
777 root 1.22 }
778 root 1.13 }
779 root 1.1
780     void
781 root 1.99 aio_readlink (SV8 *path, SV *callback=&PL_sv_undef)
782 root 1.86 PROTOTYPE: $$;$
783     PPCODE:
784     {
785     SV *data;
786     dREQ;
787    
788 root 1.115 req->type = EIO_READLINK;
789 root 1.99 req->sv1 = newSVsv (path);
790 root 1.115 req->ptr1 = SvPVbyte_nolen (req->sv1);
791 root 1.86
792     REQ_SEND;
793     }
794    
795     void
796 root 1.101 aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef)
797 root 1.32 PROTOTYPE: $$$$;$
798 root 1.43 PPCODE:
799 root 1.32 {
800 root 1.136 int ifd = extract_fd (in_fh , 0);
801     int ofd = extract_fd (out_fh, 0);
802 root 1.32 dREQ;
803    
804 root 1.115 req->type = EIO_SENDFILE;
805 root 1.99 req->sv1 = newSVsv (out_fh);
806 root 1.136 req->int1 = ofd;
807 root 1.86 req->sv2 = newSVsv (in_fh);
808 root 1.136 req->int2 = ifd;
809 root 1.101 req->offs = SvVAL64 (in_offset);
810 root 1.86 req->size = length;
811 root 1.32
812 root 1.43 REQ_SEND;
813 root 1.32 }
814    
815     void
816 root 1.101 aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef)
817 root 1.8 PROTOTYPE: $$$;$
818 root 1.43 PPCODE:
819 root 1.1 {
820 root 1.136 int fd = extract_fd (fh, 0);
821 root 1.22 dREQ;
822 root 1.1
823 root 1.115 req->type = EIO_READAHEAD;
824 root 1.99 req->sv1 = newSVsv (fh);
825 root 1.136 req->int1 = fd;
826 root 1.101 req->offs = SvVAL64 (offset);
827 root 1.86 req->size = length;
828 root 1.1
829 root 1.43 REQ_SEND;
830 root 1.1 }
831    
832     void
833 root 1.99 aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef)
834 root 1.1 ALIAS:
835 root 1.115 aio_stat = EIO_STAT
836     aio_lstat = EIO_LSTAT
837 root 1.43 PPCODE:
838 root 1.1 {
839 root 1.22 dREQ;
840 root 1.1
841 root 1.99 req->sv1 = newSVsv (fh_or_path);
842 root 1.87
843 root 1.119 if (SvPOK (req->sv1))
844 root 1.1 {
845 root 1.8 req->type = ix;
846 root 1.89 req->ptr1 = SvPVbyte_nolen (req->sv1);
847 root 1.1 }
848     else
849     {
850 root 1.115 req->type = EIO_FSTAT;
851 root 1.86 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
852 root 1.1 }
853    
854 root 1.43 REQ_SEND;
855 root 1.1 }
856    
857     void
858 root 1.99 aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef)
859     PPCODE:
860     {
861     dREQ;
862    
863     req->nv1 = SvOK (atime) ? SvNV (atime) : -1.;
864     req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.;
865     req->sv1 = newSVsv (fh_or_path);
866    
867 root 1.119 if (SvPOK (req->sv1))
868 root 1.99 {
869 root 1.115 req->type = EIO_UTIME;
870 root 1.99 req->ptr1 = SvPVbyte_nolen (req->sv1);
871     }
872     else
873     {
874 root 1.115 req->type = EIO_FUTIME;
875 root 1.99 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
876     }
877    
878     REQ_SEND;
879     }
880    
881     void
882 root 1.103 aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef)
883     PPCODE:
884     {
885     dREQ;
886    
887     req->sv1 = newSVsv (fh_or_path);
888     req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
889    
890 root 1.119 if (SvPOK (req->sv1))
891 root 1.103 {
892 root 1.115 req->type = EIO_TRUNCATE;
893 root 1.103 req->ptr1 = SvPVbyte_nolen (req->sv1);
894     }
895     else
896     {
897 root 1.115 req->type = EIO_FTRUNCATE;
898 root 1.103 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
899     }
900    
901     REQ_SEND;
902     }
903    
904     void
905 root 1.99 aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef)
906 root 1.115 ALIAS:
907     aio_chmod = EIO_CHMOD
908     aio_mkdir = EIO_MKDIR
909 root 1.99 PPCODE:
910     {
911     dREQ;
912    
913 root 1.115 req->int2 = mode;
914 root 1.99 req->sv1 = newSVsv (fh_or_path);
915    
916 root 1.119 if (SvPOK (req->sv1))
917     {
918 root 1.120 req->type = ix;
919     req->ptr1 = SvPVbyte_nolen (req->sv1);
920 root 1.119 }
921 root 1.99 else
922 root 1.119 {
923 root 1.120 req->type = EIO_FCHMOD;
924     req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
925 root 1.119 }
926 root 1.99
927     REQ_SEND;
928     }
929    
930     void
931     aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef)
932     PPCODE:
933     {
934     dREQ;
935    
936     req->int2 = SvOK (uid) ? SvIV (uid) : -1;
937     req->int3 = SvOK (gid) ? SvIV (gid) : -1;
938     req->sv1 = newSVsv (fh_or_path);
939    
940 root 1.119 if (SvPOK (req->sv1))
941 root 1.99 {
942 root 1.115 req->type = EIO_CHOWN;
943 root 1.99 req->ptr1 = SvPVbyte_nolen (req->sv1);
944     }
945     else
946     {
947 root 1.115 req->type = EIO_FCHOWN;
948 root 1.99 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
949     }
950    
951     REQ_SEND;
952     }
953    
954     void
955 root 1.141 aio_readdirx (SV8 *pathname, IV flags, SV *callback=&PL_sv_undef)
956     PPCODE:
957     {
958     dREQ;
959    
960     req->type = EIO_READDIR;
961     req->sv1 = newSVsv (pathname);
962     req->ptr1 = SvPVbyte_nolen (req->sv1);
963     req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1;
964    
965     if (flags & EIO_READDIR_DENTS)
966 root 1.142 req->int1 |= EIO_READDIR_CUSTOM2;
967 root 1.141
968     REQ_SEND;
969     }
970    
971     void
972 root 1.99 aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef)
973 root 1.22 ALIAS:
974 root 1.115 aio_unlink = EIO_UNLINK
975     aio_rmdir = EIO_RMDIR
976     aio_readdir = EIO_READDIR
977 root 1.43 PPCODE:
978 root 1.1 {
979 root 1.22 dREQ;
980 root 1.1
981 root 1.22 req->type = ix;
982 root 1.86 req->sv1 = newSVsv (pathname);
983 root 1.89 req->ptr1 = SvPVbyte_nolen (req->sv1);
984 root 1.87
985 root 1.43 REQ_SEND;
986 root 1.22 }
987    
988     void
989 root 1.99 aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef)
990 root 1.40 ALIAS:
991 root 1.115 aio_link = EIO_LINK
992     aio_symlink = EIO_SYMLINK
993     aio_rename = EIO_RENAME
994 root 1.43 PPCODE:
995 root 1.22 {
996     dREQ;
997 root 1.1
998 root 1.40 req->type = ix;
999 root 1.115 req->sv1 = newSVsv (oldpath);
1000     req->ptr1 = SvPVbyte_nolen (req->sv1);
1001     req->sv2 = newSVsv (newpath);
1002 root 1.99 req->ptr2 = SvPVbyte_nolen (req->sv2);
1003 root 1.1
1004 root 1.43 REQ_SEND;
1005 root 1.1 }
1006    
1007 root 1.42 void
1008 root 1.99 aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef)
1009 root 1.81 PPCODE:
1010     {
1011     dREQ;
1012    
1013 root 1.115 req->type = EIO_MKNOD;
1014 root 1.86 req->sv1 = newSVsv (pathname);
1015 root 1.89 req->ptr1 = SvPVbyte_nolen (req->sv1);
1016 root 1.115 req->int2 = (mode_t)mode;
1017 root 1.86 req->offs = dev;
1018 root 1.81
1019     REQ_SEND;
1020     }
1021    
1022     void
1023 root 1.99 aio_busy (double delay, SV *callback=&PL_sv_undef)
1024 root 1.45 PPCODE:
1025     {
1026     dREQ;
1027    
1028 root 1.115 req->type = EIO_BUSY;
1029 root 1.99 req->nv1 = delay < 0. ? 0. : delay;
1030 root 1.45
1031     REQ_SEND;
1032     }
1033    
1034     void
1035 root 1.99 aio_group (SV *callback=&PL_sv_undef)
1036 root 1.46 PROTOTYPE: ;$
1037 root 1.44 PPCODE:
1038 root 1.42 {
1039 root 1.44 dREQ;
1040 root 1.60
1041 root 1.115 req->type = EIO_GROUP;
1042 root 1.86
1043 root 1.127 req_submit (req);
1044 root 1.44 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1045 root 1.42 }
1046    
1047 root 1.6 void
1048 root 1.99 aio_nop (SV *callback=&PL_sv_undef)
1049 root 1.110 ALIAS:
1050 root 1.115 aio_nop = EIO_NOP
1051     aio_sync = EIO_SYNC
1052 root 1.54 PPCODE:
1053     {
1054     dREQ;
1055    
1056 root 1.110 req->type = ix;
1057 root 1.54
1058     REQ_SEND;
1059     }
1060    
1061 root 1.79 int
1062     aioreq_pri (int pri = 0)
1063     PROTOTYPE: ;$
1064     CODE:
1065 root 1.121 RETVAL = next_pri;
1066 root 1.79 if (items > 0)
1067     {
1068 root 1.115 if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN;
1069     if (pri > EIO_PRI_MAX) pri = EIO_PRI_MAX;
1070 root 1.121 next_pri = pri;
1071 root 1.79 }
1072     OUTPUT:
1073     RETVAL
1074 root 1.68
1075     void
1076     aioreq_nice (int nice = 0)
1077 root 1.79 CODE:
1078     nice = next_pri - nice;
1079 root 1.115 if (nice < EIO_PRI_MIN) nice = EIO_PRI_MIN;
1080     if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX;
1081 root 1.121 next_pri = nice;
1082 root 1.60
1083 root 1.54 void
1084 root 1.40 flush ()
1085 root 1.6 PROTOTYPE:
1086     CODE:
1087 root 1.116 while (eio_nreqs ())
1088 root 1.6 {
1089     poll_wait ();
1090 root 1.91 poll_cb ();
1091 root 1.6 }
1092    
1093 root 1.91 int
1094 root 1.7 poll()
1095     PROTOTYPE:
1096     CODE:
1097 root 1.92 poll_wait ();
1098     RETVAL = poll_cb ();
1099 root 1.91 OUTPUT:
1100     RETVAL
1101 root 1.7
1102 root 1.1 int
1103     poll_fileno()
1104     PROTOTYPE:
1105     CODE:
1106 root 1.3 RETVAL = respipe [0];
1107 root 1.1 OUTPUT:
1108     RETVAL
1109    
1110     int
1111     poll_cb(...)
1112     PROTOTYPE:
1113     CODE:
1114 root 1.85 RETVAL = poll_cb ();
1115 root 1.1 OUTPUT:
1116     RETVAL
1117    
1118     void
1119     poll_wait()
1120     PROTOTYPE:
1121     CODE:
1122 root 1.92 poll_wait ();
1123 root 1.1
1124     int
1125     nreqs()
1126     PROTOTYPE:
1127     CODE:
1128 root 1.115 RETVAL = eio_nreqs ();
1129 root 1.1 OUTPUT:
1130     RETVAL
1131    
1132 root 1.79 int
1133     nready()
1134     PROTOTYPE:
1135     CODE:
1136 root 1.115 RETVAL = eio_nready ();
1137 root 1.79 OUTPUT:
1138     RETVAL
1139    
1140     int
1141     npending()
1142     PROTOTYPE:
1143     CODE:
1144 root 1.115 RETVAL = eio_npending ();
1145 root 1.79 OUTPUT:
1146     RETVAL
1147    
1148 root 1.85 int
1149     nthreads()
1150     PROTOTYPE:
1151     CODE:
1152 root 1.122 RETVAL = eio_nthreads ();
1153 root 1.85 OUTPUT:
1154     RETVAL
1155    
1156 root 1.117 void _on_next_submit (SV *cb)
1157     CODE:
1158     SvREFCNT_dec (on_next_submit);
1159     on_next_submit = SvOK (cb) ? newSVsv (cb) : 0;
1160    
1161 root 1.48 PROTOTYPES: DISABLE
1162    
1163 root 1.44 MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1164 root 1.43
1165     void
1166     cancel (aio_req_ornot req)
1167     CODE:
1168 root 1.115 eio_cancel (req);
1169 root 1.45
1170 root 1.56 void
1171 root 1.59 cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1172 root 1.128 PPCODE:
1173     {
1174     if (GIMME_V != G_VOID)
1175     XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef);
1176    
1177     if (items > 1)
1178     {
1179     SV *cb_cv = get_cb (callback);
1180    
1181     SvREFCNT_dec (req->callback);
1182     req->callback = SvREFCNT_inc (cb_cv);
1183     }
1184     }
1185 root 1.56
1186 root 1.45 MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1187    
1188     void
1189     add (aio_req grp, ...)
1190     PPCODE:
1191     {
1192     int i;
1193 root 1.94
1194 root 1.86 if (grp->int1 == 2)
1195 root 1.49 croak ("cannot add requests to IO::AIO::GRP after the group finished");
1196    
1197 root 1.45 for (i = 1; i < items; ++i )
1198     {
1199 root 1.115 aio_req req;
1200    
1201 root 1.46 if (GIMME_V != G_VOID)
1202     XPUSHs (sv_2mortal (newSVsv (ST (i))));
1203    
1204 root 1.53 req = SvAIO_REQ (ST (i));
1205 root 1.45
1206 root 1.46 if (req)
1207 root 1.115 eio_grp_add (grp, req);
1208 root 1.45 }
1209     }
1210 root 1.43
1211 root 1.48 void
1212 root 1.72 cancel_subs (aio_req_ornot req)
1213     CODE:
1214     req_cancel_subs (req);
1215    
1216     void
1217 root 1.51 result (aio_req grp, ...)
1218     CODE:
1219     {
1220     int i;
1221 root 1.79 AV *av;
1222    
1223     grp->errorno = errno;
1224    
1225     av = newAV ();
1226 root 1.141 av_extend (av, items - 1);
1227 root 1.51
1228     for (i = 1; i < items; ++i )
1229     av_push (av, newSVsv (ST (i)));
1230    
1231 root 1.86 SvREFCNT_dec (grp->sv1);
1232     grp->sv1 = (SV *)av;
1233 root 1.51 }
1234    
1235     void
1236 root 1.79 errno (aio_req grp, int errorno = errno)
1237     CODE:
1238     grp->errorno = errorno;
1239    
1240     void
1241 root 1.67 limit (aio_req grp, int limit)
1242 root 1.49 CODE:
1243 root 1.115 eio_grp_limit (grp, limit);
1244 root 1.49
1245     void
1246 root 1.56 feed (aio_req grp, SV *callback=&PL_sv_undef)
1247 root 1.49 CODE:
1248     {
1249 root 1.86 SvREFCNT_dec (grp->sv2);
1250 root 1.131 grp->sv2 = newSVsv (callback);
1251     grp->feed = aio_grp_feed;
1252 root 1.49
1253 root 1.86 if (grp->int2 <= 0)
1254     grp->int2 = 2;
1255 root 1.49
1256 root 1.115 eio_grp_limit (grp, grp->int2);
1257 root 1.49 }
1258