ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
Revision: 1.126
Committed: Thu Sep 25 07:56:07 2008 UTC (15 years, 7 months ago) by root
Branch: MAIN
Changes since 1.125: +2 -0 lines
Log Message:
*** empty log message ***

File Contents

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