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