ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
(Generate patch)

Comparing IO-AIO/AIO.xs (file contents):
Revision 1.150 by root, Wed Jul 1 08:11:24 2009 UTC vs.
Revision 1.226 by root, Sun Jan 6 11:48:14 2013 UTC

3#include <errno.h> 3#include <errno.h>
4 4
5#include "EXTERN.h" 5#include "EXTERN.h"
6#include "perl.h" 6#include "perl.h"
7#include "XSUB.h" 7#include "XSUB.h"
8
9#include "schmorp.h"
8 10
9#include <stddef.h> 11#include <stddef.h>
10#include <stdlib.h> 12#include <stdlib.h>
11#include <errno.h> 13#include <errno.h>
12#include <sys/types.h> 14#include <sys/types.h>
13#include <sys/stat.h> 15#include <sys/stat.h>
14#include <limits.h> 16#include <limits.h>
15#include <fcntl.h> 17#include <fcntl.h>
16#include <sched.h> 18#include <sched.h>
17 19
20#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
21# include <sys/mman.h>
22#endif
23
24#if __linux__
25# include <linux/fs.h>
26# ifdef FS_IOC_FIEMAP
27# include <linux/types.h>
28# include <linux/fiemap.h>
29# define HAVE_FIEMAP 1
30# endif
31#endif
32
18/* perl namespace pollution */ 33/* perl namespace pollution */
19#undef VERSION 34#undef VERSION
20
21#ifdef _WIN32
22
23# define EIO_STRUCT_DIRENT Direntry_t
24# undef malloc
25# undef free
26
27// perl overrides all those nice win32 functions
28# undef open
29# undef read
30# undef write
31# undef send
32# undef recv
33# undef stat
34# undef fstat
35# define lstat stat
36# undef truncate
37# undef ftruncate
38# undef open
39# undef close
40# undef unlink
41# undef rmdir
42# undef rename
43# undef lseek
44
45# define chown(a,b,c) (errno = ENOSYS, -1)
46# define fchown(a,b,c) (errno = ENOSYS, -1)
47# define fchmod(a,b) (errno = ENOSYS, -1)
48# define symlink(a,b) (errno = ENOSYS, -1)
49# define readlink(a,b,c) (errno = ENOSYS, -1)
50# define mknod(a,b,c) (errno = ENOSYS, -1)
51# define truncate(a,b) (errno = ENOSYS, -1)
52# define ftruncate(fd,o) chsize ((fd), (o))
53# define fsync(fd) _commit (fd)
54# define opendir(fd) (errno = ENOSYS, 0)
55# define readdir(fd) (errno = ENOSYS, -1)
56# define closedir(fd) (errno = ENOSYS, -1)
57# define mkdir(a,b) mkdir (a)
58
59#else
60
61# include <sys/time.h>
62# include <sys/select.h>
63# include <unistd.h>
64# include <utime.h>
65# include <signal.h>
66# define EIO_STRUCT_DIRENT struct dirent
67
68#endif
69 35
70/* perl stupidly overrides readdir and maybe others */ 36/* perl stupidly overrides readdir and maybe others */
71/* with thread-unsafe versions, imagine that :( */ 37/* with thread-unsafe versions, imagine that :( */
72#undef readdir 38#undef readdir
73#undef opendir 39#undef opendir
74#undef closedir 40#undef closedir
75 41
42#ifdef _WIN32
43
44 // perl overrides all those nice libc functions
45
46 #undef malloc
47 #undef free
48 #undef open
49 #undef read
50 #undef write
51 #undef send
52 #undef recv
53 #undef stat
54 #undef lstat
55 #undef fstat
56 #undef truncate
57 #undef ftruncate
58 #undef open
59 #undef link
60 #undef close
61 #undef unlink
62 #undef mkdir
63 #undef rmdir
64 #undef rename
65 #undef lseek
66 #undef opendir
67 #undef readdir
68 #undef closedir
69 #undef chmod
70 #undef fchmod
71 #undef dup
72 #undef dup2
73 #undef abort
74 #undef pipe
75
76 #define EIO_STRUCT_STAT struct _stati64
76#define EIO_STRUCT_STAT Stat_t 77 #define EIO_STRUCT_STATI64
77 78
78/* use NV for 32 bit perls as it allows larger offsets */
79#if IVSIZE >= 8
80# define VAL64 IV
81# define SvVAL64 SvIV
82# define newSVval64 newSViv
83#else 79#else
84# define VAL64 NV 80
85# define SvVAL64 SvNV 81 #include <sys/time.h>
86# define newSVval64 newSVnv 82 #include <sys/select.h>
83 #include <unistd.h>
84 #include <utime.h>
85 #include <signal.h>
86
87 #define EIO_STRUCT_STAT Stat_t
88
87#endif 89#endif
88 90
89/*****************************************************************************/ 91/*****************************************************************************/
90 92
91#if __GNUC__ >= 3 93#if __GNUC__ >= 3
97#define expect_false(expr) expect ((expr) != 0, 0) 99#define expect_false(expr) expect ((expr) != 0, 0)
98#define expect_true(expr) expect ((expr) != 0, 1) 100#define expect_true(expr) expect ((expr) != 0, 1)
99 101
100/*****************************************************************************/ 102/*****************************************************************************/
101 103
102static HV *stash;
103typedef SV SV8; /* byte-sv, used for argument-checking */ 104typedef SV SV8; /* byte-sv, used for argument-checking */
104typedef int aio_rfd; /* read file desriptor */ 105typedef int aio_rfd; /* read file desriptor */
105typedef int aio_wfd; /* write file descriptor */ 106typedef int aio_wfd; /* write file descriptor */
106 107
107#define AIO_REQ_KLASS "IO::AIO::REQ" 108static HV *aio_stash, *aio_req_stash, *aio_grp_stash, *aio_wd_stash;
108#define AIO_GRP_KLASS "IO::AIO::GRP"
109 109
110#define EIO_REQ_MEMBERS \ 110#define EIO_REQ_MEMBERS \
111 SV *callback; \ 111 SV *callback; \
112 SV *sv1, *sv2; \ 112 SV *sv1, *sv2; \
113 SV *sv3, *sv4; \
113 STRLEN stroffset; \ 114 STRLEN stroffset; \
114 SV *self; 115 SV *self;
115 116
116#define EIO_NO_WRAPPERS 1 117#define EIO_NO_WRAPPERS 1
117 118
119#include "libeio/config.h"
118#include "libeio/eio.h" 120#include "libeio/eio.h"
119
120#ifndef POSIX_FADV_NORMAL
121# define POSIX_FADV_NORMAL 0
122# define NO_FADVISE 1
123#endif
124
125#ifndef POSIX_FADV_SEQUENTIAL
126# define POSIX_FADV_SEQUENTIAL 0
127#endif
128
129#ifndef POSIX_FADV_RANDOM
130# define POSIX_FADV_RANDOM 0
131#endif
132
133#ifndef POSIX_FADV_NOREUSE
134# define POSIX_FADV_NOREUSE 0
135#endif
136
137#ifndef POSIX_FADV_WILLNEED
138# define POSIX_FADV_WILLNEED 0
139#endif
140
141#ifndef POSIX_FADV_DONTNEED
142# define POSIX_FADV_DONTNEED 0
143#endif
144 121
145static int req_invoke (eio_req *req); 122static int req_invoke (eio_req *req);
146#define EIO_FINISH(req) req_invoke (req) 123#define EIO_FINISH(req) req_invoke (req)
147static void req_destroy (eio_req *grp); 124static void req_destroy (eio_req *grp);
148#define EIO_DESTROY(req) req_destroy (req) 125#define EIO_DESTROY(req) req_destroy (req)
149 126
127#include "libeio/eio.c"
128
129#if !HAVE_POSIX_FADVISE
130# define posix_fadvise(a,b,c,d) errno = ENOSYS /* also return ENOSYS */
131#endif
132
133#if !HAVE_POSIX_MADVISE
134# define posix_madvise(a,b,c) errno = ENOSYS /* also return ENOSYS */
135#endif
136
137#ifndef MAP_ANONYMOUS
138# ifdef MAP_ANON
139# define MAP_ANONYMOUS MAP_ANON
140# else
141# define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */
142# endif
143#endif
144
145/* defines all sorts of constants to 0 unless they are already defined */
146#include "def0.h"
147
148#ifndef makedev
149# define makedev(maj,min) (((maj) << 8) | (min))
150#endif
151#ifndef major
152# define major(dev) ((dev) >> 8)
153#endif
154#ifndef minor
155# define minor(dev) ((dev) & 0xff)
156#endif
157
158#if PAGESIZE <= 0
159# define PAGESIZE sysconf (_SC_PAGESIZE)
160#endif
161
162/*****************************************************************************/
163
164static void
165fiemap (eio_req *req)
166{
167 req->result = -1;
168
169#if HAVE_FIEMAP
170 /* assume some c99 */
171 struct fiemap *fiemap = 0;
172 size_t end_offset;
173 int count = req->int3;
174
175 req->flags |= EIO_FLAG_PTR1_FREE;
176
177 /* heuristic: start with 512 bytes (8 extents), and if that isn't enough, */
178 /* increase in 3.5kb steps */
179 if (count < 0)
180 count = 8;
181
182 fiemap = malloc (sizeof (*fiemap) + sizeof (struct fiemap_extent) * count);
183 errno = ENOMEM;
184 if (!fiemap)
185 return;
186
187 req->ptr1 = fiemap;
188
189 fiemap->fm_start = req->offs;
190 fiemap->fm_length = req->size;
191 fiemap->fm_flags = req->int2;
192 fiemap->fm_extent_count = count;
193
194 if (ioctl (req->int1, FS_IOC_FIEMAP, fiemap) < 0)
195 return;
196
197 if (req->int3 >= 0 /* not autosizing */
198 || !fiemap->fm_mapped_extents /* no more extents */
199 || fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_flags & FIEMAP_EXTENT_LAST /* hit eof */)
200 goto done;
201
202 /* else we have to loop -
203 * it would be tempting (actually I tried that first) to just query the
204 * number of extents needed, but linux often feels like not returning all
205 * extents, without telling us it left any out. this complicates
206 * this quite a bit.
207 */
208
209 end_offset = fiemap->fm_length + (fiemap->fm_length == FIEMAP_MAX_OFFSET ? 0 : fiemap->fm_start);
210
211 for (;;)
212 {
213 /* we go in 54 extent steps - 3kb, in the hope that this fits nicely on the eio stack (normally 16+ kb) */
214 char scratch[3072];
215 struct fiemap *incmap = (struct fiemap *)scratch;
216
217 incmap->fm_start = fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_logical
218 + fiemap->fm_extents [fiemap->fm_mapped_extents - 1].fe_length;
219 incmap->fm_length = fiemap->fm_length - (incmap->fm_start - fiemap->fm_start);
220 incmap->fm_flags = fiemap->fm_flags;
221 incmap->fm_extent_count = (sizeof (scratch) - sizeof (struct fiemap)) / sizeof (struct fiemap_extent);
222
223 if (ioctl (req->int1, FS_IOC_FIEMAP, incmap) < 0)
224 return;
225
226 if (!incmap->fm_mapped_extents)
227 goto done;
228
229 count = fiemap->fm_mapped_extents + incmap->fm_mapped_extents;
230 fiemap = realloc (fiemap, sizeof (*fiemap) + sizeof (struct fiemap_extent) * count);
231 errno = ENOMEM;
232 if (!fiemap)
233 return;
234
235 req->ptr1 = fiemap;
236
237 for (count = 0; count < incmap->fm_mapped_extents; ++count)
238 {
239 struct fiemap_extent *e = incmap->fm_extents + count;
240
241 if (e->fe_logical + e->fe_length >= end_offset)
242 goto done;
243
244 fiemap->fm_extents [fiemap->fm_mapped_extents++] = *e;
245
246 if (e->fe_flags & FIEMAP_EXTENT_LAST)
247 goto done;
248
249 }
250 }
251
252done:
253 req->result = 0;
254
255#else
256 errno = ENOSYS;
257#endif
258}
259
260/*****************************************************************************/
261
150enum { 262enum {
151 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ 263 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */
152}; 264};
153 265
154#include "libeio/eio.c"
155
156typedef eio_req *aio_req; 266typedef eio_req *aio_req;
157typedef eio_req *aio_req_ornot; 267typedef eio_req *aio_req_ornot;
268typedef eio_wd aio_wd;
158 269
159static SV *on_next_submit; 270static SV *on_next_submit;
160static int next_pri = EIO_PRI_DEFAULT; 271static int next_pri = EIO_PRI_DEFAULT;
161static int max_outstanding; 272static int max_outstanding;
162 273
163static int respipe_osf [2], respipe [2] = { -1, -1 }; 274static s_epipe respipe;
164 275
165static void req_destroy (aio_req req); 276static void req_destroy (aio_req req);
166static void req_cancel (aio_req req); 277static void req_cancel (aio_req req);
167 278
168static void want_poll (void) 279static void
280want_poll (void)
169{ 281{
170 /* write a dummy byte to the pipe so fh becomes ready */ 282 /* write a dummy byte to the pipe so fh becomes ready */
171 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); 283 s_epipe_signal (&respipe);
172} 284}
173 285
174static void done_poll (void) 286static void
287done_poll (void)
175{ 288{
176 /* read any signals sent by the worker threads */ 289 /* read any signals sent by the worker threads */
177 char buf [4]; 290 s_epipe_drain (&respipe);
178 while (respipe_read (respipe [0], buf, 4) == 4)
179 ;
180} 291}
181 292
182/* must be called at most once */ 293/* must be called at most once */
183static SV *req_sv (aio_req req, const char *klass) 294static SV *
295req_sv (aio_req req, HV *stash)
184{ 296{
185 if (!req->self) 297 if (!req->self)
186 { 298 {
187 req->self = (SV *)newHV (); 299 req->self = (SV *)newHV ();
188 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); 300 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
189 } 301 }
190 302
191 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); 303 return sv_2mortal (sv_bless (newRV_inc (req->self), stash));
192} 304}
193 305
194static aio_req SvAIO_REQ (SV *sv) 306static SV *
307newSVaio_wd (aio_wd wd)
308{
309 return sv_bless (newRV_noinc (newSViv ((IV)wd)), aio_wd_stash);
310}
311
312static aio_req
313SvAIO_REQ (SV *sv)
195{ 314{
196 MAGIC *mg; 315 MAGIC *mg;
197 316
198 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) 317 if (!SvROK (sv)
318 /* for speed reasons, we do not verify that SvROK actually has a stash ptr */
319 || (SvSTASH (SvRV (sv)) != aio_grp_stash
320 && SvSTASH (SvRV (sv)) != aio_req_stash
321 && !sv_derived_from (sv, "IO::AIO::REQ")))
199 croak ("object of class " AIO_REQ_KLASS " expected"); 322 croak ("object of class IO::AIO::REQ expected");
200 323
201 mg = mg_find (SvRV (sv), PERL_MAGIC_ext); 324 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
202 325
203 return mg ? (aio_req)mg->mg_ptr : 0; 326 return mg ? (aio_req)mg->mg_ptr : 0;
204} 327}
205 328
329static aio_wd
330SvAIO_WD (SV *sv)
331{
332 if (!SvROK (sv)
333 || SvTYPE (SvRV (sv)) != SVt_PVMG
334 || SvSTASH (SvRV (sv)) != aio_wd_stash)
335 croak ("IO::AIO: expected a working directory object as returned by aio_wd");
336
337 return (aio_wd)(long)SvIVX (SvRV (sv));
338}
339
340static void
206static void aio_grp_feed (aio_req grp) 341aio_grp_feed (aio_req grp)
207{ 342{
208 if (grp->sv2 && SvOK (grp->sv2)) 343 if (grp->sv2 && SvOK (grp->sv2))
209 { 344 {
210 dSP; 345 dSP;
211 346
212 ENTER; 347 ENTER;
213 SAVETMPS; 348 SAVETMPS;
214 PUSHMARK (SP); 349 PUSHMARK (SP);
215 XPUSHs (req_sv (grp, AIO_GRP_KLASS)); 350 XPUSHs (req_sv (grp, aio_grp_stash));
216 PUTBACK; 351 PUTBACK;
217 call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); 352 call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR);
218 SPAGAIN; 353 SPAGAIN;
219 FREETMPS; 354 FREETMPS;
220 LEAVE; 355 LEAVE;
221 } 356 }
222} 357}
223 358
359static void
224static void req_submit (eio_req *req) 360req_submit (eio_req *req)
225{ 361{
226 eio_submit (req); 362 eio_submit (req);
227 363
228 if (expect_false (on_next_submit)) 364 if (expect_false (on_next_submit))
229 { 365 {
236 PUTBACK; 372 PUTBACK;
237 call_sv (cb, G_DISCARD | G_EVAL); 373 call_sv (cb, G_DISCARD | G_EVAL);
238 } 374 }
239} 375}
240 376
377static int
241static int req_invoke (eio_req *req) 378req_invoke (eio_req *req)
242{ 379{
243 dSP;
244
245 if (req->flags & FLAG_SV2_RO_OFF) 380 if (req->flags & FLAG_SV2_RO_OFF)
246 SvREADONLY_off (req->sv2); 381 SvREADONLY_off (req->sv2);
247 382
248 if (!EIO_CANCELLED (req) && req->callback) 383 if (!EIO_CANCELLED (req) && req->callback)
249 { 384 {
385 dSP;
386 static SV *sv_result_cache; /* caches the result integer SV */
387 SV *sv_result;
388
250 ENTER; 389 ENTER;
251 SAVETMPS; 390 SAVETMPS;
252 PUSHMARK (SP); 391 PUSHMARK (SP);
253 EXTEND (SP, 1); 392 EXTEND (SP, 1);
254 393
394 /* do not recreate the result IV from scratch each time */
395 if (expect_true (sv_result_cache))
396 {
397 sv_result = sv_result_cache; sv_result_cache = 0;
398 SvIV_set (sv_result, req->result);
399 SvIOK_only (sv_result);
400 }
401 else
402 {
403 sv_result = newSViv (req->result);
404 SvREADONLY_on (sv_result);
405 }
406
255 switch (req->type) 407 switch (req->type)
256 { 408 {
409 case EIO_WD_OPEN:
410 PUSHs (req->result ? &PL_sv_undef : sv_2mortal (newSVaio_wd (req->wd)));
411 break;
412
257 case EIO_READDIR: 413 case EIO_READDIR:
258 { 414 {
259 SV *rv = &PL_sv_undef; 415 SV *rv = &PL_sv_undef;
260 416
261 if (req->result >= 0) 417 if (req->result >= 0)
326 int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); 482 int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR);
327 char sym [64]; 483 char sym [64];
328 int symlen; 484 int symlen;
329 485
330 symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); 486 symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result);
331 gv_init (gv, stash, sym, symlen, 0); 487 gv_init (gv, aio_stash, sym, symlen, 0);
332 488
333 symlen = snprintf ( 489 symlen = snprintf (
334 sym, 490 sym,
335 sizeof (sym), 491 sizeof (sym),
336 "%s&=%d", 492 "%s&=%d",
344 500
345 PUSHs (fh); 501 PUSHs (fh);
346 } 502 }
347 break; 503 break;
348 504
505 case EIO_STATVFS:
506 case EIO_FSTATVFS:
507 {
508 SV *rv = &PL_sv_undef;
509
510#ifndef _WIN32
511 if (req->result >= 0)
512 {
513 EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req);
514 HV *hv = newHV ();
515 /* POSIX requires fsid to be unsigned long, but AIX in its infinite wisdom
516 * chooses to make it a struct.
517 */
518 unsigned long fsid = 0;
519 memcpy (&fsid, &f->f_fsid, sizeof (unsigned long) < sizeof (f->f_fsid) ? sizeof (unsigned long) : sizeof (f->f_fsid));
520
521 rv = sv_2mortal (newRV_noinc ((SV *)hv));
522
523 hv_store (hv, "bsize" , sizeof ("bsize" ) - 1, newSVval64 (f->f_bsize ), 0);
524 hv_store (hv, "frsize" , sizeof ("frsize" ) - 1, newSVval64 (f->f_frsize ), 0);
525 hv_store (hv, "blocks" , sizeof ("blocks" ) - 1, newSVval64 (f->f_blocks ), 0);
526 hv_store (hv, "bfree" , sizeof ("bfree" ) - 1, newSVval64 (f->f_bfree ), 0);
527 hv_store (hv, "bavail" , sizeof ("bavail" ) - 1, newSVval64 (f->f_bavail ), 0);
528 hv_store (hv, "files" , sizeof ("files" ) - 1, newSVval64 (f->f_files ), 0);
529 hv_store (hv, "ffree" , sizeof ("ffree" ) - 1, newSVval64 (f->f_ffree ), 0);
530 hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0);
531 hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (fsid ), 0);
532 hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0);
533 hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0);
534 }
535#endif
536
537 PUSHs (rv);
538 }
539
540 break;
541
349 case EIO_GROUP: 542 case EIO_GROUP:
350 req->int1 = 2; /* mark group as finished */ 543 req->int1 = 2; /* mark group as finished */
351 544
352 if (req->sv1) 545 if (req->sv1)
353 { 546 {
363 case EIO_NOP: 556 case EIO_NOP:
364 case EIO_BUSY: 557 case EIO_BUSY:
365 break; 558 break;
366 559
367 case EIO_READLINK: 560 case EIO_READLINK:
561 case EIO_REALPATH:
368 if (req->result > 0) 562 if (req->result > 0)
369 PUSHs (sv_2mortal (newSVpvn (req->ptr2, req->result))); 563 PUSHs (sv_2mortal (newSVpvn (req->ptr2, req->result)));
370 break; 564 break;
371 565
372 case EIO_STAT: 566 case EIO_STAT:
373 case EIO_LSTAT: 567 case EIO_LSTAT:
374 case EIO_FSTAT: 568 case EIO_FSTAT:
375 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; 569 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT;
570
376 PL_laststatval = req->result; 571 if (!(PL_laststatval = req->result))
572 /* if compilation fails here then perl's Stat_t is not struct _stati64 */
377 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); 573 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2);
378 PUSHs (sv_2mortal (newSViv (req->result))); 574
575 PUSHs (sv_result);
576 break;
577
578 case EIO_SEEK:
579 PUSHs (req->result ? sv_result : sv_2mortal (newSVval64 (req->offs)));
379 break; 580 break;
380 581
381 case EIO_READ: 582 case EIO_READ:
382 { 583 {
383 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); 584 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0));
384 *SvEND (req->sv2) = 0; 585 *SvEND (req->sv2) = 0;
385 SvPOK_only (req->sv2); 586 SvPOK_only (req->sv2);
386 SvSETMAGIC (req->sv2); 587 SvSETMAGIC (req->sv2);
387 PUSHs (sv_2mortal (newSViv (req->result))); 588 PUSHs (sv_result);
388 } 589 }
389 break; 590 break;
390 591
391 case EIO_DUP2: 592 case EIO_CUSTOM:
593 if (req->feed == fiemap)
594 {
595#if HAVE_FIEMAP
596 if (!req->result)
597 {
598 struct fiemap *fiemap = (struct fiemap *)req->ptr1;
599
600 if (fiemap->fm_extent_count)
601 {
602 AV *av = newAV ();
603 int i;
604
605 while (fiemap->fm_mapped_extents)
606 {
607 struct fiemap_extent *extent = &fiemap->fm_extents [--fiemap->fm_mapped_extents];
608 AV *ext_av = newAV ();
609
610 av_store (ext_av, 3, newSVuv (extent->fe_flags));
611 av_store (ext_av, 2, newSVval64 (extent->fe_length));
612 av_store (ext_av, 1, newSVval64 (extent->fe_physical));
613 av_store (ext_av, 0, newSVval64 (extent->fe_logical));
614
615 av_store (av, fiemap->fm_mapped_extents, newRV_noinc ((SV *)ext_av));
616 }
617
618 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
619 }
620 else
621 {
622 SvIV_set (sv_result, fiemap->fm_mapped_extents);
623 PUSHs (sv_result);
624 }
625 }
626#endif
627 }
628 else
629 PUSHs (sv_result);
630 break;
631
632 case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), so fudge result value */
392 if (req->result > 0) 633 if (req->result > 0)
393 req->result = 0; 634 SvIV_set (sv_result, 0);
394 /* FALLTHROUGH */ 635 /* FALLTHROUGH */
395 636
396 default: 637 default:
397 PUSHs (sv_2mortal (newSViv (req->result))); 638 PUSHs (sv_result);
398 break; 639 break;
399 } 640 }
400 641
401 errno = req->errorno; 642 errno = req->errorno;
402 643
403 PUTBACK; 644 PUTBACK;
404 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); 645 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD);
405 SPAGAIN; 646 SPAGAIN;
406 647
648 if (expect_false (SvREFCNT (sv_result) != 1 || sv_result_cache))
649 SvREFCNT_dec (sv_result);
650 else
651 sv_result_cache = sv_result;
652
407 FREETMPS; 653 FREETMPS;
408 LEAVE; 654 LEAVE;
409 655
410 PUTBACK; 656 PUTBACK;
411 } 657 }
412 658
413 return !!SvTRUE (ERRSV); 659 return !!SvTRUE (ERRSV);
414} 660}
415 661
662static void
416static void req_destroy (aio_req req) 663req_destroy (aio_req req)
417{ 664{
418 if (req->self) 665 if (req->self)
419 { 666 {
420 sv_unmagic (req->self, PERL_MAGIC_ext); 667 sv_unmagic (req->self, PERL_MAGIC_ext);
421 SvREFCNT_dec (req->self); 668 SvREFCNT_dec (req->self);
422 } 669 }
423 670
424 SvREFCNT_dec (req->sv1); 671 SvREFCNT_dec (req->sv1);
425 SvREFCNT_dec (req->sv2); 672 SvREFCNT_dec (req->sv2);
673 SvREFCNT_dec (req->sv3);
674 SvREFCNT_dec (req->sv4);
426 SvREFCNT_dec (req->callback); 675 SvREFCNT_dec (req->callback);
427 676
428 Safefree (req); 677 free (req);
429} 678}
430 679
680static void
431static void req_cancel_subs (aio_req grp) 681req_cancel_subs (aio_req grp)
432{ 682{
433 aio_req sub;
434
435 if (grp->type != EIO_GROUP) 683 if (grp->type != EIO_GROUP)
436 return; 684 return;
437 685
438 SvREFCNT_dec (grp->sv2); 686 SvREFCNT_dec (grp->sv2);
439 grp->sv2 = 0; 687 grp->sv2 = 0;
440 688
441 eio_grp_cancel (grp); 689 eio_grp_cancel (grp);
442} 690}
443 691
444#ifdef USE_SOCKETS_AS_HANDLES 692static void ecb_cold
445# define TO_SOCKET(x) (win32_get_osfhandle (x)) 693create_respipe (void)
446#else 694{
447# define TO_SOCKET(x) (x) 695 if (s_epipe_renew (&respipe))
448#endif 696 croak ("IO::AIO: unable to initialize result pipe");
697}
449 698
450static void 699static void
451create_respipe (void) 700poll_wait (void)
452{ 701{
453 int old_readfd = respipe [0];
454
455 if (respipe [1] >= 0)
456 respipe_close (TO_SOCKET (respipe [1]));
457
458#ifdef _WIN32
459 if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe))
460#else
461 if (pipe (respipe))
462#endif
463 croak ("unable to initialize result pipe");
464
465 if (old_readfd >= 0)
466 {
467 if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
468 croak ("unable to initialize result pipe(2)");
469
470 respipe_close (respipe [0]);
471 respipe [0] = old_readfd;
472 }
473
474#ifdef _WIN32
475 int arg = 1;
476 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
477 || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
478#else
479 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
480 || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
481#endif
482 croak ("unable to initialize result pipe(3)");
483
484 respipe_osf [0] = TO_SOCKET (respipe [0]);
485 respipe_osf [1] = TO_SOCKET (respipe [1]);
486}
487
488static void poll_wait (void)
489{
490 fd_set rfd;
491
492 while (eio_nreqs ()) 702 while (eio_nreqs ())
493 { 703 {
494 int size; 704 int size;
495 705
496 X_LOCK (reslock); 706 X_LOCK (reslock);
500 if (size) 710 if (size)
501 return; 711 return;
502 712
503 etp_maybe_start_thread (); 713 etp_maybe_start_thread ();
504 714
505 FD_ZERO (&rfd); 715 s_epipe_wait (&respipe);
506 FD_SET (respipe [0], &rfd);
507
508 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
509 } 716 }
510} 717}
511 718
512static int poll_cb (void) 719static int
720poll_cb (void)
513{ 721{
514 for (;;) 722 for (;;)
515 { 723 {
516 int res = eio_poll (); 724 int res = eio_poll ();
517 725
523 731
524 poll_wait (); 732 poll_wait ();
525 } 733 }
526} 734}
527 735
528static void atfork_child (void) 736static void ecb_cold
737reinit (void)
529{ 738{
530 create_respipe (); 739 create_respipe ();
740
741 if (eio_init (want_poll, done_poll) < 0)
742 croak ("IO::AIO: unable to initialise eio library");
531} 743}
744
745/*****************************************************************************/
746
747#if !_POSIX_MAPPED_FILES
748# define mmap(addr,length,prot,flags,fd,offs) EIO_ENOSYS ()
749# define munmap(addr,length) EIO_ENOSYS ()
750#endif
751
752#if !_POSIX_MEMORY_PROTECTION
753# define mprotect(addr,len,prot) EIO_ENOSYS ()
754# define PROT_NONE 0
755# define PROT_WRITE 0
756# define MAP_PRIVATE 0
757# define MAP_SHARED 0
758# define MAP_FIXED 0
759#endif
760
761#define MMAP_MAGIC PERL_MAGIC_ext
762
763static int ecb_cold
764mmap_free (pTHX_ SV *sv, MAGIC *mg)
765{
766 int old_errno = errno;
767 munmap (mg->mg_ptr, (size_t)mg->mg_obj);
768 errno = old_errno;
769
770 mg->mg_obj = 0; /* just in case */
771
772 SvREADONLY_off (sv);
773
774 if (SvPVX (sv) != mg->mg_ptr)
775 croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected");
776
777 SvCUR_set (sv, 0);
778 SvPVX (sv) = 0;
779 SvOK_off (sv);
780
781 return 0;
782}
783
784static MGVTBL mmap_vtbl = {
785 0, 0, 0, 0, mmap_free
786};
787
788/*****************************************************************************/
532 789
533static SV * 790static SV *
534get_cb (SV *cb_sv) 791get_cb (SV *cb_sv)
535{ 792{
536 HV *st; 793 SvGETMAGIC (cb_sv);
537 GV *gvp; 794 return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0;
795}
796
797static aio_req ecb_noinline
798dreq (SV *callback)
799{
538 CV *cv; 800 SV *cb_cv;
801 aio_req req;
802 int req_pri = next_pri;
803 next_pri = EIO_PRI_DEFAULT;
539 804
540 if (!SvOK (cb_sv)) 805 cb_cv = get_cb (callback);
806
807 req = calloc (sizeof (*req), 1);
808 if (!req)
809 croak ("out of memory during eio_req allocation");
810
811 req->callback = SvREFCNT_inc (cb_cv);
812 req->pri = req_pri;
813
541 return 0; 814 return req;
542
543 cv = sv_2cv (cb_sv, &st, &gvp, 0);
544
545 if (!cv)
546 croak ("IO::AIO callback must be undef or a CODE reference");
547
548 return (SV *)cv;
549} 815}
550 816
551#define dREQ \ 817#define dREQ \
552 SV *cb_cv; \ 818 aio_req req = dreq (callback); \
553 aio_req req; \
554 int req_pri = next_pri; \
555 next_pri = EIO_PRI_DEFAULT; \
556 \
557 cb_cv = get_cb (callback); \
558 \
559 Newz (0, req, 1, eio_req); \
560 if (!req) \
561 croak ("out of memory during eio_req allocation"); \
562 \
563 req->callback = SvREFCNT_inc (cb_cv); \
564 req->pri = req_pri
565 819
566#define REQ_SEND \ 820#define REQ_SEND \
567 PUTBACK; \ 821 PUTBACK; \
568 req_submit (req); \ 822 req_submit (req); \
569 SPAGAIN; \ 823 SPAGAIN; \
570 \ 824 \
571 if (GIMME_V != G_VOID) \ 825 if (GIMME_V != G_VOID) \
572 XPUSHs (req_sv (req, AIO_REQ_KLASS)); 826 XPUSHs (req_sv (req, aio_req_stash));
573 827
574static int 828ecb_inline void
575extract_fd (SV *fh, int wr) 829req_set_path (aio_req req, SV *path, SV **wdsv, SV **pathsv, eio_wd *wd, void **ptr)
576{ 830{
577 int fd = PerlIO_fileno (wr ? IoOFP (sv_2io (fh)) : IoIFP (sv_2io (fh))); 831 if (expect_false (SvROK (path)))
832 {
833 SV *rv = SvRV (path);
834 SV *wdob;
578 835
579 if (fd < 0) 836 if (SvTYPE (rv) == SVt_PVAV && AvFILLp (rv) == 1)
580 croak ("illegal fh argument, either not an OS file or read/write mode mismatch"); 837 {
838 path = AvARRAY (rv)[1];
839 wdob = AvARRAY (rv)[0];
581 840
582 return fd; 841 if (SvOK (wdob))
842 {
843 *wd = SvAIO_WD (wdob);
844 *wdsv = SvREFCNT_inc_NN (SvRV (wdob));
845 }
846 else
847 *wd = EIO_INVALID_WD;
848 }
849 else if (SvTYPE (rv) == SVt_PVMG && SvSTASH (rv) == aio_wd_stash)
850 {
851 *wd = (aio_wd)(long)SvIVX (rv);
852 *wdsv = SvREFCNT_inc_NN (rv);
853 *ptr = ".";
854 return; /* path set to "." */
855 }
856 else
857 croak ("IO::AIO: pathname arguments must be specified as a string, an IO::AIO::WD object or a [IO::AIO::WD, path] pair");
858 }
859
860 *pathsv = newSVsv (path);
861 *ptr = SvPVbyte_nolen (*pathsv);
583} 862}
863
864static void ecb_noinline
865req_set_path1 (aio_req req, SV *path)
866{
867 req_set_path (req, path, &req->sv1, &req->sv3, &req->wd, &req->ptr1);
868}
869
870static void ecb_noinline
871req_set_fh_or_path (aio_req req, int type_path, int type_fh, SV *fh_or_path)
872{
873 SV *rv = SvROK (fh_or_path) ? SvRV (fh_or_path) : fh_or_path;
874
875 switch (SvTYPE (rv))
876 {
877 case SVt_PVIO:
878 case SVt_PVLV:
879 case SVt_PVGV:
880 req->type = type_fh;
881 req->sv1 = newSVsv (fh_or_path);
882 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
883 break;
884
885 default:
886 req->type = type_path;
887 req_set_path1 (req, fh_or_path);
888 break;
889 }
890}
891
892XS(boot_IO__AIO) ecb_cold;
584 893
585MODULE = IO::AIO PACKAGE = IO::AIO 894MODULE = IO::AIO PACKAGE = IO::AIO
586 895
587PROTOTYPES: ENABLE 896PROTOTYPES: ENABLE
588 897
590{ 899{
591 static const struct { 900 static const struct {
592 const char *name; 901 const char *name;
593 IV iv; 902 IV iv;
594 } *civ, const_iv[] = { 903 } *civ, const_iv[] = {
595# define const_iv(name, value) { # name, (IV) value }, 904# define const_niv(name, value) { # name, (IV) value },
905# define const_iv(name) { # name, (IV) name },
596# define const_eio(name) { # name, (IV) EIO_ ## name }, 906# define const_eio(name) { # name, (IV) EIO_ ## name },
597 const_iv (EXDEV , EXDEV) 907
908 /* you have to re-run ./gendef0 after adding/Removing any constants here */
909
598 const_iv (ENOSYS , ENOSYS) 910 const_iv (ENOSYS)
911 const_iv (EXDEV)
912 const_iv (EBADR)
913
599 const_iv (O_RDONLY, O_RDONLY) 914 const_iv (O_RDONLY)
600 const_iv (O_WRONLY, O_WRONLY) 915 const_iv (O_WRONLY)
916 const_iv (O_RDWR)
601 const_iv (O_CREAT , O_CREAT) 917 const_iv (O_CREAT)
602 const_iv (O_TRUNC , O_TRUNC) 918 const_iv (O_TRUNC)
603#ifndef _WIN32 919 const_iv (O_EXCL)
920 const_iv (O_APPEND)
921
922 const_iv (O_ASYNC)
923 const_iv (O_DIRECT)
924 const_iv (O_NOATIME)
925
926 const_iv (O_CLOEXEC)
927 const_iv (O_NOCTTY)
928 const_iv (O_NOFOLLOW)
929 const_iv (O_NONBLOCK)
930 const_iv (O_EXEC)
931 const_iv (O_SEARCH)
932 const_iv (O_DIRECTORY)
933 const_iv (O_DSYNC)
934 const_iv (O_RSYNC)
935 const_iv (O_SYNC)
936 const_iv (O_TTY_INIT)
937
604 const_iv (S_IFIFO , S_IFIFO) 938 const_iv (S_IFIFO)
605#endif 939 const_iv (S_IFCHR)
940 const_iv (S_IFBLK)
941 const_iv (S_IFLNK)
942 const_iv (S_IFREG)
943 const_iv (S_IFDIR)
944 const_iv (S_IFWHT)
945 const_iv (S_IFSOCK)
946 const_iv (S_IFMT)
947
606 const_iv (FADV_NORMAL , POSIX_FADV_NORMAL) 948 const_niv (FADV_NORMAL , POSIX_FADV_NORMAL)
607 const_iv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) 949 const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL)
608 const_iv (FADV_RANDOM , POSIX_FADV_RANDOM) 950 const_niv (FADV_RANDOM , POSIX_FADV_RANDOM)
609 const_iv (FADV_NOREUSE , POSIX_FADV_NOREUSE) 951 const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE)
610 const_iv (FADV_WILLNEED , POSIX_FADV_WILLNEED) 952 const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED)
611 const_iv (FADV_DONTNEED , POSIX_FADV_DONTNEED) 953 const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED)
954
955 const_niv (MADV_NORMAL , POSIX_MADV_NORMAL)
956 const_niv (MADV_SEQUENTIAL, POSIX_MADV_SEQUENTIAL)
957 const_niv (MADV_RANDOM , POSIX_MADV_RANDOM)
958 const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED)
959 const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED)
960
961 const_iv (ST_RDONLY)
962 const_iv (ST_NOSUID)
963 const_iv (ST_NODEV)
964 const_iv (ST_NOEXEC)
965 const_iv (ST_SYNCHRONOUS)
966 const_iv (ST_MANDLOCK)
967 const_iv (ST_WRITE)
968 const_iv (ST_APPEND)
969 const_iv (ST_IMMUTABLE)
970 const_iv (ST_NOATIME)
971 const_iv (ST_NODIRATIME)
972 const_iv (ST_RELATIME)
973
974 const_iv (PROT_NONE)
975 const_iv (PROT_EXEC)
976 const_iv (PROT_READ)
977 const_iv (PROT_WRITE)
978
979 /*const_iv (MAP_FIXED)*/
980 const_iv (MAP_PRIVATE)
981 const_iv (MAP_SHARED)
982 const_iv (MAP_ANONYMOUS)
983
984 /* linuxish */
985 const_iv (MAP_HUGETLB)
986 const_iv (MAP_LOCKED)
987 const_iv (MAP_NORESERVE)
988 const_iv (MAP_POPULATE)
989 const_iv (MAP_NONBLOCK)
990
991 const_iv (FIEMAP_FLAG_SYNC)
992 const_iv (FIEMAP_FLAG_XATTR)
993 const_iv (FIEMAP_FLAGS_COMPAT)
994 const_iv (FIEMAP_EXTENT_LAST)
995 const_iv (FIEMAP_EXTENT_UNKNOWN)
996 const_iv (FIEMAP_EXTENT_DELALLOC)
997 const_iv (FIEMAP_EXTENT_ENCODED)
998 const_iv (FIEMAP_EXTENT_DATA_ENCRYPTED)
999 const_iv (FIEMAP_EXTENT_NOT_ALIGNED)
1000 const_iv (FIEMAP_EXTENT_DATA_INLINE)
1001 const_iv (FIEMAP_EXTENT_DATA_TAIL)
1002 const_iv (FIEMAP_EXTENT_UNWRITTEN)
1003 const_iv (FIEMAP_EXTENT_MERGED)
1004 const_iv (FIEMAP_EXTENT_SHARED)
1005
1006 const_iv (SPLICE_F_MOVE)
1007 const_iv (SPLICE_F_NONBLOCK)
1008 const_iv (SPLICE_F_MORE)
1009 const_iv (SPLICE_F_GIFT)
1010
1011 const_iv (SEEK_DATA)
1012 const_iv (SEEK_HOLE)
1013
1014 /* libeio constants */
1015 const_eio (SEEK_SET)
1016 const_eio (SEEK_CUR)
1017 const_eio (SEEK_END)
1018
1019 const_eio (MCL_FUTURE)
1020 const_eio (MCL_CURRENT)
1021
1022 const_eio (MS_ASYNC)
1023 const_eio (MS_INVALIDATE)
1024 const_eio (MS_SYNC)
1025
1026 const_eio (MT_MODIFY)
612 1027
613 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) 1028 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE)
614 const_eio (SYNC_FILE_RANGE_WRITE) 1029 const_eio (SYNC_FILE_RANGE_WRITE)
615 const_eio (SYNC_FILE_RANGE_WAIT_AFTER) 1030 const_eio (SYNC_FILE_RANGE_WAIT_AFTER)
1031
1032 const_eio (FALLOC_FL_KEEP_SIZE)
1033 const_eio (FALLOC_FL_PUNCH_HOLE)
616 1034
617 const_eio (READDIR_DENTS) 1035 const_eio (READDIR_DENTS)
618 const_eio (READDIR_DIRS_FIRST) 1036 const_eio (READDIR_DIRS_FIRST)
619 const_eio (READDIR_STAT_ORDER) 1037 const_eio (READDIR_STAT_ORDER)
620 const_eio (READDIR_FOUND_UNKNOWN) 1038 const_eio (READDIR_FOUND_UNKNOWN)
628 const_eio (DT_LNK) 1046 const_eio (DT_LNK)
629 const_eio (DT_SOCK) 1047 const_eio (DT_SOCK)
630 const_eio (DT_WHT) 1048 const_eio (DT_WHT)
631 }; 1049 };
632 1050
633 stash = gv_stashpv ("IO::AIO", 1); 1051 aio_stash = gv_stashpv ("IO::AIO" , 1);
1052 aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1);
1053 aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1);
1054 aio_wd_stash = gv_stashpv ("IO::AIO::WD" , 1);
634 1055
635 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 1056 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
636 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 1057 newCONSTSUB (aio_stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
637 1058
638 create_respipe (); 1059 newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE));
639 1060
640 if (eio_init (want_poll, done_poll) < 0) 1061 reinit ();
641 croak ("IO::AIO: unable to initialise eio library");
642
643 /* atfork child called in fifo order, so before eio's handler */
644 X_THREAD_ATFORK (0, 0, atfork_child);
645} 1062}
646 1063
647void 1064void
1065reinit ()
1066 PROTOTYPE:
1067
1068void
648max_poll_reqs (int nreqs) 1069max_poll_reqs (unsigned int nreqs)
649 PROTOTYPE: $ 1070 PROTOTYPE: $
650 CODE: 1071 CODE:
651 eio_set_max_poll_reqs (nreqs); 1072 eio_set_max_poll_reqs (nreqs);
652 1073
653void 1074void
655 PROTOTYPE: $ 1076 PROTOTYPE: $
656 CODE: 1077 CODE:
657 eio_set_max_poll_time (nseconds); 1078 eio_set_max_poll_time (nseconds);
658 1079
659void 1080void
660min_parallel (int nthreads) 1081min_parallel (unsigned int nthreads)
661 PROTOTYPE: $ 1082 PROTOTYPE: $
662 CODE: 1083 CODE:
663 eio_set_min_parallel (nthreads); 1084 eio_set_min_parallel (nthreads);
664 1085
665void 1086void
666max_parallel (int nthreads) 1087max_parallel (unsigned int nthreads)
667 PROTOTYPE: $ 1088 PROTOTYPE: $
668 CODE: 1089 CODE:
669 eio_set_max_parallel (nthreads); 1090 eio_set_max_parallel (nthreads);
670 1091
671void 1092void
672max_idle (int nthreads) 1093max_idle (unsigned int nthreads)
673 PROTOTYPE: $ 1094 PROTOTYPE: $
674 CODE: 1095 CODE:
675 eio_set_max_idle (nthreads); 1096 eio_set_max_idle (nthreads);
676 1097
677void 1098void
1099idle_timeout (unsigned int seconds)
1100 PROTOTYPE: $
1101 CODE:
1102 eio_set_idle_timeout (seconds);
1103
1104void
678max_outstanding (int maxreqs) 1105max_outstanding (unsigned int maxreqs)
679 PROTOTYPE: $ 1106 PROTOTYPE: $
680 CODE: 1107 CODE:
681 max_outstanding = maxreqs; 1108 max_outstanding = maxreqs;
682 1109
683void 1110void
1111aio_wd (SV8 *pathname, SV *callback=&PL_sv_undef)
1112 PPCODE:
1113{
1114 dREQ;
1115
1116 req->type = EIO_WD_OPEN;
1117 req_set_path1 (req, pathname);
1118
1119 REQ_SEND;
1120}
1121
1122void
684aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef) 1123aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef)
685 PROTOTYPE: $$$;$
686 PPCODE: 1124 PPCODE:
687{ 1125{
688 dREQ; 1126 dREQ;
689 1127
690 req->type = EIO_OPEN; 1128 req->type = EIO_OPEN;
691 req->sv1 = newSVsv (pathname); 1129 req_set_path1 (req, pathname);
692 req->ptr1 = SvPVbyte_nolen (req->sv1);
693 req->int1 = flags; 1130 req->int1 = flags;
694 req->int2 = mode; 1131 req->int2 = mode;
695 1132
696 REQ_SEND; 1133 REQ_SEND;
697} 1134}
698 1135
699void 1136void
700aio_fsync (SV *fh, SV *callback=&PL_sv_undef) 1137aio_fsync (SV *fh, SV *callback=&PL_sv_undef)
701 PROTOTYPE: $;$
702 ALIAS: 1138 ALIAS:
703 aio_fsync = EIO_FSYNC 1139 aio_fsync = EIO_FSYNC
704 aio_fdatasync = EIO_FDATASYNC 1140 aio_fdatasync = EIO_FDATASYNC
1141 aio_syncfs = EIO_SYNCFS
705 PPCODE: 1142 PPCODE:
706{ 1143{
707 int fd = extract_fd (fh, 0); 1144 int fd = s_fileno_croak (fh, 0);
708 dREQ; 1145 dREQ;
709 1146
710 req->type = ix; 1147 req->type = ix;
711 req->sv1 = newSVsv (fh); 1148 req->sv1 = newSVsv (fh);
712 req->int1 = fd; 1149 req->int1 = fd;
713 1150
714 REQ_SEND (req); 1151 REQ_SEND;
715} 1152}
716 1153
717void 1154void
718aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) 1155aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef)
719 PROTOTYPE: $$$$;$
720 PPCODE: 1156 PPCODE:
721{ 1157{
722 int fd = extract_fd (fh, 0); 1158 int fd = s_fileno_croak (fh, 0);
723 dREQ; 1159 dREQ;
724 1160
725 req->type = EIO_SYNC_FILE_RANGE; 1161 req->type = EIO_SYNC_FILE_RANGE;
726 req->sv1 = newSVsv (fh); 1162 req->sv1 = newSVsv (fh);
727 req->int1 = fd; 1163 req->int1 = fd;
728 req->offs = offset; 1164 req->offs = offset;
729 req->size = nbytes; 1165 req->size = nbytes;
730 req->int2 = flags; 1166 req->int2 = flags;
731 1167
732 REQ_SEND (req); 1168 REQ_SEND;
1169}
1170
1171void
1172aio_allocate (SV *fh, int mode, off_t offset, size_t len, SV *callback=&PL_sv_undef)
1173 PPCODE:
1174{
1175 int fd = s_fileno_croak (fh, 0);
1176 dREQ;
1177
1178 req->type = EIO_FALLOCATE;
1179 req->sv1 = newSVsv (fh);
1180 req->int1 = fd;
1181 req->int2 = mode;
1182 req->offs = offset;
1183 req->size = len;
1184
1185 REQ_SEND;
733} 1186}
734 1187
735void 1188void
736aio_close (SV *fh, SV *callback=&PL_sv_undef) 1189aio_close (SV *fh, SV *callback=&PL_sv_undef)
737 PROTOTYPE: $;$
738 PPCODE: 1190 PPCODE:
739{ 1191{
740 static int close_pipe = -1; /* dummy fd to close fds via dup2 */ 1192 static int close_fd = -1; /* dummy fd to close fds via dup2 */
741 int fd = extract_fd (fh, 0); 1193 int fd = s_fileno_croak (fh, 0);
742 dREQ; 1194 dREQ;
743 1195
744 if (close_pipe < 0) 1196 if (expect_false (close_fd < 0))
745 { 1197 {
746 int pipefd [2]; 1198 int pipefd [2];
747 1199
1200 if (
1201#ifdef _WIN32
1202 _pipe (pipefd, 1, _O_BINARY) < 0
1203#else
748 if (pipe (pipefd) < 0 1204 pipe (pipefd) < 0
749 || close (pipefd [1]) < 0
750 || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0) 1205 || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0
1206#endif
1207 || close (pipefd [1]) < 0
1208 )
751 abort (); /*D*/ 1209 abort (); /*D*/
752 1210
753 close_pipe = pipefd [0]; 1211 close_fd = pipefd [0];
754 } 1212 }
755 1213
756 req->type = EIO_DUP2; 1214 req->type = EIO_DUP2;
757 req->int1 = close_pipe; 1215 req->int1 = close_fd;
758 req->sv2 = newSVsv (fh); 1216 req->sv2 = newSVsv (fh);
759 req->int2 = fd; 1217 req->int2 = fd;
760 1218
761 REQ_SEND (req); 1219 REQ_SEND;
1220}
1221
1222void
1223aio_seek (SV *fh, SV *offset, int whence, SV *callback=&PL_sv_undef)
1224 PPCODE:
1225{
1226 int fd = s_fileno_croak (fh, 0);
1227 dREQ;
1228
1229 req->type = EIO_SEEK;
1230 req->sv1 = newSVsv (fh);
1231 req->int1 = fd;
1232 req->offs = SvVAL64 (offset);
1233 req->int2 = whence;
1234
1235 REQ_SEND;
762} 1236}
763 1237
764void 1238void
765aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef) 1239aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef)
766 ALIAS: 1240 ALIAS:
767 aio_read = EIO_READ 1241 aio_read = EIO_READ
768 aio_write = EIO_WRITE 1242 aio_write = EIO_WRITE
769 PROTOTYPE: $$$$$;$
770 PPCODE: 1243 PPCODE:
771{ 1244{
772 STRLEN svlen; 1245 STRLEN svlen;
773 int fd = extract_fd (fh, ix == EIO_WRITE); 1246 int fd = s_fileno_croak (fh, ix == EIO_WRITE);
774 char *svptr = SvPVbyte (data, svlen); 1247 char *svptr = SvPVbyte (data, svlen);
775 UV len = SvUV (length); 1248 UV len = SvUV (length);
776 1249
777 if (dataoffset < 0) 1250 if (dataoffset < 0)
778 dataoffset += svlen; 1251 dataoffset += svlen;
788 } 1261 }
789 else 1262 else
790 { 1263 {
791 /* read: check type and grow scalar as necessary */ 1264 /* read: check type and grow scalar as necessary */
792 SvUPGRADE (data, SVt_PV); 1265 SvUPGRADE (data, SVt_PV);
1266 if (SvLEN (data) >= SvCUR (data))
793 svptr = SvGROW (data, len + dataoffset + 1); 1267 svptr = SvGROW (data, len + dataoffset + 1);
1268 else if (SvCUR (data) < len + dataoffset)
1269 croak ("length + dataoffset outside of scalar, and cannot grow");
794 } 1270 }
795 1271
796 { 1272 {
797 dREQ; 1273 dREQ;
798 1274
814 REQ_SEND; 1290 REQ_SEND;
815 } 1291 }
816} 1292}
817 1293
818void 1294void
819aio_readlink (SV8 *path, SV *callback=&PL_sv_undef) 1295aio_readlink (SV8 *pathname, SV *callback=&PL_sv_undef)
820 PROTOTYPE: $$;$ 1296 ALIAS:
1297 aio_readlink = EIO_READLINK
1298 aio_realpath = EIO_REALPATH
821 PPCODE: 1299 PPCODE:
822{ 1300{
823 SV *data;
824 dREQ; 1301 dREQ;
825 1302
826 req->type = EIO_READLINK; 1303 req->type = ix;
827 req->sv1 = newSVsv (path); 1304 req_set_path1 (req, pathname);
828 req->ptr1 = SvPVbyte_nolen (req->sv1);
829 1305
830 REQ_SEND; 1306 REQ_SEND;
831} 1307}
832 1308
833void 1309void
834aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) 1310aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef)
835 PROTOTYPE: $$$$;$
836 PPCODE: 1311 PPCODE:
837{ 1312{
838 int ifd = extract_fd (in_fh , 0); 1313 int ifd = s_fileno_croak (in_fh , 0);
839 int ofd = extract_fd (out_fh, 0); 1314 int ofd = s_fileno_croak (out_fh, 1);
840 dREQ; 1315 dREQ;
841 1316
842 req->type = EIO_SENDFILE; 1317 req->type = EIO_SENDFILE;
843 req->sv1 = newSVsv (out_fh); 1318 req->sv1 = newSVsv (out_fh);
844 req->int1 = ofd; 1319 req->int1 = ofd;
850 REQ_SEND; 1325 REQ_SEND;
851} 1326}
852 1327
853void 1328void
854aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) 1329aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef)
855 PROTOTYPE: $$$;$
856 PPCODE: 1330 PPCODE:
857{ 1331{
858 int fd = extract_fd (fh, 0); 1332 int fd = s_fileno_croak (fh, 0);
859 dREQ; 1333 dREQ;
860 1334
861 req->type = EIO_READAHEAD; 1335 req->type = EIO_READAHEAD;
862 req->sv1 = newSVsv (fh); 1336 req->sv1 = newSVsv (fh);
863 req->int1 = fd; 1337 req->int1 = fd;
868} 1342}
869 1343
870void 1344void
871aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) 1345aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef)
872 ALIAS: 1346 ALIAS:
873 aio_stat = EIO_STAT 1347 aio_stat = EIO_STAT
874 aio_lstat = EIO_LSTAT 1348 aio_lstat = EIO_LSTAT
1349 aio_statvfs = EIO_STATVFS
875 PPCODE: 1350 PPCODE:
876{ 1351{
877 dREQ; 1352 dREQ;
878 1353
879 req->sv1 = newSVsv (fh_or_path); 1354 req_set_fh_or_path (req, ix, ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT, fh_or_path);
880
881 if (SvPOK (req->sv1))
882 {
883 req->type = ix;
884 req->ptr1 = SvPVbyte_nolen (req->sv1);
885 }
886 else
887 {
888 req->type = EIO_FSTAT;
889 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
890 }
891 1355
892 REQ_SEND; 1356 REQ_SEND;
893} 1357}
1358
1359UV
1360major (UV dev)
1361 ALIAS:
1362 minor = 1
1363 CODE:
1364 RETVAL = ix ? major (dev) : minor (dev);
1365 OUTPUT:
1366 RETVAL
1367
1368UV
1369makedev (UV maj, UV min)
1370 CODE:
1371 RETVAL = makedev (maj, min);
1372 OUTPUT:
1373 RETVAL
894 1374
895void 1375void
896aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef) 1376aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef)
897 PPCODE: 1377 PPCODE:
898{ 1378{
899 dREQ; 1379 dREQ;
900 1380
901 req->nv1 = SvOK (atime) ? SvNV (atime) : -1.; 1381 req->nv1 = SvOK (atime) ? SvNV (atime) : -1.;
902 req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.; 1382 req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.;
903 req->sv1 = newSVsv (fh_or_path); 1383 req_set_fh_or_path (req, EIO_UTIME, EIO_FUTIME, fh_or_path);
904
905 if (SvPOK (req->sv1))
906 {
907 req->type = EIO_UTIME;
908 req->ptr1 = SvPVbyte_nolen (req->sv1);
909 }
910 else
911 {
912 req->type = EIO_FUTIME;
913 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
914 }
915 1384
916 REQ_SEND; 1385 REQ_SEND;
917} 1386}
918 1387
919void 1388void
920aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef) 1389aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef)
921 PPCODE: 1390 PPCODE:
922{ 1391{
923 dREQ; 1392 dREQ;
924 1393
925 req->sv1 = newSVsv (fh_or_path);
926 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; 1394 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
927 1395 req_set_fh_or_path (req, EIO_TRUNCATE, EIO_FTRUNCATE, fh_or_path);
928 if (SvPOK (req->sv1))
929 {
930 req->type = EIO_TRUNCATE;
931 req->ptr1 = SvPVbyte_nolen (req->sv1);
932 }
933 else
934 {
935 req->type = EIO_FTRUNCATE;
936 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
937 }
938 1396
939 REQ_SEND; 1397 REQ_SEND;
940} 1398}
941 1399
942void 1400void
943aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef) 1401aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef)
944 ALIAS:
945 aio_chmod = EIO_CHMOD
946 aio_mkdir = EIO_MKDIR
947 PPCODE: 1402 PPCODE:
948{ 1403{
949 dREQ; 1404 dREQ;
950 1405
951 req->int2 = mode; 1406 req->int2 = mode;
952 req->sv1 = newSVsv (fh_or_path); 1407 req_set_fh_or_path (req, EIO_CHMOD, EIO_FCHMOD, fh_or_path);
953
954 if (SvPOK (req->sv1))
955 {
956 req->type = ix;
957 req->ptr1 = SvPVbyte_nolen (req->sv1);
958 }
959 else
960 {
961 req->type = EIO_FCHMOD;
962 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
963 }
964 1408
965 REQ_SEND; 1409 REQ_SEND;
966} 1410}
967 1411
968void 1412void
971{ 1415{
972 dREQ; 1416 dREQ;
973 1417
974 req->int2 = SvOK (uid) ? SvIV (uid) : -1; 1418 req->int2 = SvOK (uid) ? SvIV (uid) : -1;
975 req->int3 = SvOK (gid) ? SvIV (gid) : -1; 1419 req->int3 = SvOK (gid) ? SvIV (gid) : -1;
976 req->sv1 = newSVsv (fh_or_path); 1420 req_set_fh_or_path (req, EIO_CHOWN, EIO_FCHOWN, fh_or_path);
977
978 if (SvPOK (req->sv1))
979 {
980 req->type = EIO_CHOWN;
981 req->ptr1 = SvPVbyte_nolen (req->sv1);
982 }
983 else
984 {
985 req->type = EIO_FCHOWN;
986 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
987 }
988 1421
989 REQ_SEND; 1422 REQ_SEND;
990} 1423}
991 1424
992void 1425void
994 PPCODE: 1427 PPCODE:
995{ 1428{
996 dREQ; 1429 dREQ;
997 1430
998 req->type = EIO_READDIR; 1431 req->type = EIO_READDIR;
999 req->sv1 = newSVsv (pathname);
1000 req->ptr1 = SvPVbyte_nolen (req->sv1);
1001 req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1; 1432 req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1;
1002 1433
1003 if (flags & EIO_READDIR_DENTS) 1434 if (flags & EIO_READDIR_DENTS)
1004 req->int1 |= EIO_READDIR_CUSTOM2; 1435 req->int1 |= EIO_READDIR_CUSTOM2;
1005 1436
1437 req_set_path1 (req, pathname);
1438
1006 REQ_SEND; 1439 REQ_SEND;
1440}
1441
1442void
1443aio_mkdir (SV8 *pathname, int mode, SV *callback=&PL_sv_undef)
1444 PPCODE:
1445{
1446 dREQ;
1447
1448 req->type = EIO_MKDIR;
1449 req->int2 = mode;
1450 req_set_path1 (req, pathname);
1451
1452 REQ_SEND;
1007} 1453}
1008 1454
1009void 1455void
1010aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef) 1456aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef)
1011 ALIAS: 1457 ALIAS:
1015 PPCODE: 1461 PPCODE:
1016{ 1462{
1017 dREQ; 1463 dREQ;
1018 1464
1019 req->type = ix; 1465 req->type = ix;
1020 req->sv1 = newSVsv (pathname); 1466 req_set_path1 (req, pathname);
1021 req->ptr1 = SvPVbyte_nolen (req->sv1);
1022 1467
1023 REQ_SEND; 1468 REQ_SEND;
1024} 1469}
1025 1470
1026void 1471void
1029 aio_link = EIO_LINK 1474 aio_link = EIO_LINK
1030 aio_symlink = EIO_SYMLINK 1475 aio_symlink = EIO_SYMLINK
1031 aio_rename = EIO_RENAME 1476 aio_rename = EIO_RENAME
1032 PPCODE: 1477 PPCODE:
1033{ 1478{
1479 eio_wd wd2 = 0;
1034 dREQ; 1480 dREQ;
1035 1481
1036 req->type = ix; 1482 req->type = ix;
1037 req->sv1 = newSVsv (oldpath); 1483 req_set_path1 (req, oldpath);
1038 req->ptr1 = SvPVbyte_nolen (req->sv1); 1484 req_set_path (req, newpath, &req->sv2, &req->sv4, &wd2, &req->ptr2);
1039 req->sv2 = newSVsv (newpath); 1485 req->int3 = (long)wd2;
1040 req->ptr2 = SvPVbyte_nolen (req->sv2);
1041 1486
1042 REQ_SEND; 1487 REQ_SEND;
1043} 1488}
1044 1489
1045void 1490void
1047 PPCODE: 1492 PPCODE:
1048{ 1493{
1049 dREQ; 1494 dREQ;
1050 1495
1051 req->type = EIO_MKNOD; 1496 req->type = EIO_MKNOD;
1052 req->sv1 = newSVsv (pathname);
1053 req->ptr1 = SvPVbyte_nolen (req->sv1);
1054 req->int2 = (mode_t)mode; 1497 req->int2 = (mode_t)mode;
1055 req->offs = dev; 1498 req->offs = dev;
1499 req_set_path1 (req, pathname);
1056 1500
1057 REQ_SEND; 1501 REQ_SEND;
1058} 1502}
1059 1503
1060void 1504void
1505aio_mtouch (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, int flags = 0, SV *callback=&PL_sv_undef)
1506 ALIAS:
1507 aio_mtouch = EIO_MTOUCH
1508 aio_msync = EIO_MSYNC
1509 PPCODE:
1510{
1511 STRLEN svlen;
1512 char *svptr = SvPVbyte (data, svlen);
1513 UV len = SvUV (length);
1514
1515 if (offset < 0)
1516 offset += svlen;
1517
1518 if (offset < 0 || offset > svlen)
1519 croak ("offset outside of scalar");
1520
1521 if (!SvOK (length) || len + offset > svlen)
1522 len = svlen - offset;
1523
1524 {
1525 dREQ;
1526
1527 req->type = ix;
1528 req->sv2 = SvREFCNT_inc (data);
1529 req->ptr2 = (char *)svptr + offset;
1530 req->size = len;
1531 req->int1 = flags;
1532
1533 REQ_SEND;
1534 }
1535}
1536
1537void
1538aio_mlock (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, SV *callback=&PL_sv_undef)
1539 PPCODE:
1540{
1541 STRLEN svlen;
1542 char *svptr = SvPVbyte (data, svlen);
1543 UV len = SvUV (length);
1544
1545 if (offset < 0)
1546 offset += svlen;
1547
1548 if (offset < 0 || offset > svlen)
1549 croak ("offset outside of scalar");
1550
1551 if (!SvOK (length) || len + offset > svlen)
1552 len = svlen - offset;
1553
1554 {
1555 dREQ;
1556
1557 req->type = EIO_MLOCK;
1558 req->sv2 = SvREFCNT_inc (data);
1559 req->ptr2 = (char *)svptr + offset;
1560 req->size = len;
1561
1562 REQ_SEND;
1563 }
1564}
1565
1566void
1567aio_mlockall (IV flags, SV *callback=&PL_sv_undef)
1568 PPCODE:
1569{
1570 dREQ;
1571
1572 req->type = EIO_MLOCKALL;
1573 req->int1 = flags;
1574
1575 REQ_SEND;
1576}
1577
1578void
1579aio_fiemap (SV *fh, off_t start, SV *length, U32 flags, SV *count, SV *callback=&PL_sv_undef)
1580 PPCODE:
1581{
1582 int fd = s_fileno_croak (fh, 0);
1583 dREQ;
1584
1585 req->type = EIO_CUSTOM;
1586 req->sv1 = newSVsv (fh);
1587 req->int1 = fd;
1588
1589 req->feed = fiemap;
1590#if HAVE_FIEMAP
1591 /* keep our fingers crossed that the next two types are 64 bit */
1592 req->offs = start;
1593 req->size = SvOK (length) ? SvVAL64 (length) : ~0ULL;
1594 req->int2 = flags;
1595 req->int3 = SvOK (count) ? SvIV (count) : -1;
1596#endif
1597
1598 REQ_SEND;
1599}
1600
1601void
1061aio_busy (double delay, SV *callback=&PL_sv_undef) 1602aio_busy (double delay, SV *callback=&PL_sv_undef)
1062 PPCODE: 1603 PPCODE:
1063{ 1604{
1064 dREQ; 1605 dREQ;
1065 1606
1069 REQ_SEND; 1610 REQ_SEND;
1070} 1611}
1071 1612
1072void 1613void
1073aio_group (SV *callback=&PL_sv_undef) 1614aio_group (SV *callback=&PL_sv_undef)
1074 PROTOTYPE: ;$
1075 PPCODE: 1615 PPCODE:
1076{ 1616{
1077 dREQ; 1617 dREQ;
1078 1618
1079 req->type = EIO_GROUP; 1619 req->type = EIO_GROUP;
1080 1620
1081 req_submit (req); 1621 req_submit (req);
1082 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1622 XPUSHs (req_sv (req, aio_grp_stash));
1083} 1623}
1084 1624
1085void 1625void
1086aio_nop (SV *callback=&PL_sv_undef) 1626aio_nop (SV *callback=&PL_sv_undef)
1087 ALIAS: 1627 ALIAS:
1096 REQ_SEND; 1636 REQ_SEND;
1097} 1637}
1098 1638
1099int 1639int
1100aioreq_pri (int pri = 0) 1640aioreq_pri (int pri = 0)
1101 PROTOTYPE: ;$
1102 CODE: 1641 CODE:
1103 RETVAL = next_pri; 1642 RETVAL = next_pri;
1104 if (items > 0) 1643 if (items > 0)
1105 { 1644 {
1106 if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN; 1645 if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN;
1118 if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX; 1657 if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX;
1119 next_pri = nice; 1658 next_pri = nice;
1120 1659
1121void 1660void
1122flush () 1661flush ()
1123 PROTOTYPE:
1124 CODE: 1662 CODE:
1125 while (eio_nreqs ()) 1663 while (eio_nreqs ())
1126 { 1664 {
1127 poll_wait (); 1665 poll_wait ();
1128 poll_cb (); 1666 poll_cb ();
1129 } 1667 }
1130 1668
1131int 1669int
1132poll() 1670poll ()
1133 PROTOTYPE:
1134 CODE: 1671 CODE:
1135 poll_wait (); 1672 poll_wait ();
1136 RETVAL = poll_cb (); 1673 RETVAL = poll_cb ();
1137 OUTPUT: 1674 OUTPUT:
1138 RETVAL 1675 RETVAL
1139 1676
1140int 1677int
1141poll_fileno() 1678poll_fileno ()
1142 PROTOTYPE:
1143 CODE: 1679 CODE:
1144 RETVAL = respipe [0]; 1680 RETVAL = s_epipe_fd (&respipe);
1145 OUTPUT: 1681 OUTPUT:
1146 RETVAL 1682 RETVAL
1147 1683
1148int 1684int
1149poll_cb(...) 1685poll_cb (...)
1150 PROTOTYPE: 1686 PROTOTYPE:
1151 CODE: 1687 CODE:
1152 RETVAL = poll_cb (); 1688 RETVAL = poll_cb ();
1153 OUTPUT: 1689 OUTPUT:
1154 RETVAL 1690 RETVAL
1155 1691
1156void 1692void
1157poll_wait() 1693poll_wait ()
1158 PROTOTYPE:
1159 CODE: 1694 CODE:
1160 poll_wait (); 1695 poll_wait ();
1161 1696
1162int 1697int
1163nreqs() 1698nreqs ()
1164 PROTOTYPE:
1165 CODE: 1699 CODE:
1166 RETVAL = eio_nreqs (); 1700 RETVAL = eio_nreqs ();
1167 OUTPUT: 1701 OUTPUT:
1168 RETVAL 1702 RETVAL
1169 1703
1170int 1704int
1171nready() 1705nready ()
1172 PROTOTYPE:
1173 CODE: 1706 CODE:
1174 RETVAL = eio_nready (); 1707 RETVAL = eio_nready ();
1175 OUTPUT: 1708 OUTPUT:
1176 RETVAL 1709 RETVAL
1177 1710
1178int 1711int
1179npending() 1712npending ()
1180 PROTOTYPE:
1181 CODE: 1713 CODE:
1182 RETVAL = eio_npending (); 1714 RETVAL = eio_npending ();
1183 OUTPUT: 1715 OUTPUT:
1184 RETVAL 1716 RETVAL
1185 1717
1186int 1718int
1187nthreads() 1719nthreads ()
1188 PROTOTYPE:
1189 CODE: 1720 CODE:
1190 RETVAL = eio_nthreads (); 1721 RETVAL = eio_nthreads ();
1191 OUTPUT: 1722 OUTPUT:
1192 RETVAL 1723 RETVAL
1193 1724
1194int 1725int
1195fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) 1726fadvise (aio_rfd fh, off_t offset, off_t length, IV advice)
1196 PROTOTYPE: $$$$
1197 CODE: 1727 CODE:
1198#if _XOPEN_SOURCE >= 600 && !NO_FADVISE
1199 RETVAL = posix_fadvise (fh, offset, length, advice); 1728 RETVAL = posix_fadvise (fh, offset, length, advice);
1200#else
1201 RETVAL = errno = ENOSYS;
1202#endif
1203 OUTPUT: 1729 OUTPUT:
1204 RETVAL 1730 RETVAL
1205 1731
1206ssize_t 1732IV
1207sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) 1733sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count)
1208 PROTOTYPE: $$$$
1209 CODE: 1734 CODE:
1210 eio_sendfile_sync (ofh, ifh, offset, count); 1735 RETVAL = eio_sendfile_sync (ofh, ifh, offset, count);
1736 OUTPUT:
1737 RETVAL
1738
1739void
1740mmap (SV *scalar, size_t length, int prot, int flags, SV *fh = &PL_sv_undef, off_t offset = 0)
1741 PPCODE:
1742 sv_unmagic (scalar, MMAP_MAGIC);
1743{
1744 int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1;
1745 void *addr = (void *)mmap (0, length, prot, flags, fd, offset);
1746 if (addr == (void *)-1)
1747 XSRETURN_NO;
1748
1749 sv_force_normal (scalar);
1750
1751 /* we store the length in mg_obj, as namlen is I32 :/ */
1752 sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0)
1753 ->mg_obj = (SV *)length;
1754
1755 SvUPGRADE (scalar, SVt_PV); /* nop... */
1756
1757 if (!(prot & PROT_WRITE))
1758 SvREADONLY_on (scalar);
1759
1760 if (SvLEN (scalar))
1761 Safefree (SvPVX (scalar));
1762
1763 SvPVX (scalar) = (char *)addr;
1764 SvCUR_set (scalar, length);
1765 SvLEN_set (scalar, 0);
1766 SvPOK_only (scalar);
1767
1768 XSRETURN_YES;
1769}
1770
1771void
1772munmap (SV *scalar)
1773 CODE:
1774 sv_unmagic (scalar, MMAP_MAGIC);
1775
1776int
1777madvise (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot)
1778 ALIAS:
1779 mprotect = 1
1780 CODE:
1781{
1782 STRLEN svlen;
1783 void *addr = SvPVbyte (scalar, svlen);
1784 size_t len = SvUV (length);
1785
1786 if (offset < 0)
1787 offset += svlen;
1788
1789 if (offset < 0 || offset > svlen)
1790 croak ("offset outside of scalar");
1791
1792 if (!SvOK (length) || len + offset > svlen)
1793 len = svlen - offset;
1794
1795 addr = (void *)(((intptr_t)addr) + offset);
1796 eio_page_align (&addr, &len);
1797
1798 switch (ix)
1799 {
1800 case 0: RETVAL = posix_madvise (addr, len, advice_or_prot); break;
1801 case 1: RETVAL = mprotect (addr, len, advice_or_prot); break;
1802 }
1803}
1804 OUTPUT:
1805 RETVAL
1806
1807int
1808munlock (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef)
1809 CODE:
1810{
1811 STRLEN svlen;
1812 void *addr = SvPVbyte (scalar, svlen);
1813 size_t len = SvUV (length);
1814
1815 if (offset < 0)
1816 offset += svlen;
1817
1818 if (offset < 0 || offset > svlen)
1819 croak ("offset outside of scalar");
1820
1821 if (!SvOK (length) || len + offset > svlen)
1822 len = svlen - offset;
1823
1824 addr = (void *)(((intptr_t)addr) + offset);
1825 eio_page_align (&addr, &len);
1826#if _POSIX_MEMLOCK_RANGE
1827 RETVAL = munlock (addr, len);
1828#else
1829 RETVAL = EIO_ENOSYS ();
1830#endif
1831}
1832 OUTPUT:
1833 RETVAL
1834
1835int
1836munlockall ()
1837 CODE:
1838#if _POSIX_MEMLOCK
1839 munlockall ();
1840#else
1841 RETVAL = EIO_ENOSYS ();
1842#endif
1843 OUTPUT:
1844 RETVAL
1845
1846int
1847splice (aio_rfd rfh, SV *off_in, aio_wfd wfh, SV *off_out, size_t length, unsigned int flags)
1848 CODE:
1849{
1850#if HAVE_LINUX_SPLICE
1851 loff_t off_in_, off_out_;
1852 RETVAL = splice (
1853 rfh, SvOK (off_in ) ? (off_in_ = SvVAL64 (off_in )), &off_in_ : 0,
1854 wfh, SvOK (off_out) ? (off_out_ = SvVAL64 (off_out)), &off_out_ : 0,
1855 length, flags
1856 );
1857#else
1858 RETVAL = EIO_ENOSYS ();
1859#endif
1860}
1861 OUTPUT:
1862 RETVAL
1863
1864int
1865tee (aio_rfd rfh, aio_wfd wfh, size_t length, unsigned int flags)
1866 CODE:
1867#if HAVE_LINUX_SPLICE
1868 RETVAL = tee (rfh, wfh, length, flags);
1869#else
1870 RETVAL = EIO_ENOSYS ();
1871#endif
1872 OUTPUT:
1873 RETVAL
1211 1874
1212void _on_next_submit (SV *cb) 1875void _on_next_submit (SV *cb)
1213 CODE: 1876 CODE:
1214 SvREFCNT_dec (on_next_submit); 1877 SvREFCNT_dec (on_next_submit);
1215 on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; 1878 on_next_submit = SvOK (cb) ? newSVsv (cb) : 0;
1216 1879
1217PROTOTYPES: DISABLE 1880PROTOTYPES: DISABLE
1218 1881
1882MODULE = IO::AIO PACKAGE = IO::AIO::WD
1883
1884BOOT:
1885{
1886 newCONSTSUB (aio_stash, "CWD" , newSVaio_wd (EIO_CWD ));
1887 newCONSTSUB (aio_stash, "INVALID_WD", newSVaio_wd (EIO_INVALID_WD));
1888}
1889
1890void
1891DESTROY (SV *self)
1892 CODE:
1893{
1894 aio_wd wd = SvAIO_WD (self);
1895#if HAVE_AT
1896 {
1897 SV *callback = &PL_sv_undef;
1898 dREQ; /* clobbers next_pri :/ */
1899 next_pri = req->pri; /* restore next_pri */
1900 req->pri = EIO_PRI_MAX; /* better use max. priority to conserve fds */
1901 req->type = EIO_WD_CLOSE;
1902 req->wd = wd;
1903 REQ_SEND;
1904 }
1905#else
1906 eio_wd_close_sync (wd);
1907#endif
1908}
1909
1219MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1910MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1220 1911
1221void 1912void
1222cancel (aio_req_ornot req) 1913cancel (aio_req_ornot req)
1223 CODE: 1914 CODE:
1230 if (GIMME_V != G_VOID) 1921 if (GIMME_V != G_VOID)
1231 XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); 1922 XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef);
1232 1923
1233 if (items > 1) 1924 if (items > 1)
1234 { 1925 {
1235 SV *cb_cv = get_cb (callback); 1926 SV *cb_cv =get_cb (callback);
1236 1927
1237 SvREFCNT_dec (req->callback); 1928 SvREFCNT_dec (req->callback);
1238 req->callback = SvREFCNT_inc (cb_cv); 1929 req->callback = SvREFCNT_inc (cb_cv);
1239 } 1930 }
1240} 1931}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines