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