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