ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
Revision: 1.130
Committed: Thu Oct 2 11:35:03 2008 UTC (15 years, 7 months ago) by root
Branch: MAIN
CVS Tags: rel-3_1
Changes since 1.129: +1 -1 lines
Log Message:
3.1

File Contents

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