ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
Revision: 1.124
Committed: Thu May 29 03:20:40 2008 UTC (15 years, 11 months ago) by root
Branch: MAIN
CVS Tags: rel-3_03, rel-3_04
Changes since 1.123: +7 -7 lines
Log Message:
*** empty log message ***

File Contents

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