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