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