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