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