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.153 by root, Wed Jul 15 01:36:04 2009 UTC vs.
Revision 1.169 by root, Wed Aug 4 16:06:54 2010 UTC

11#include <stddef.h> 11#include <stddef.h>
12#include <stdlib.h> 12#include <stdlib.h>
13#include <errno.h> 13#include <errno.h>
14#include <sys/types.h> 14#include <sys/types.h>
15#include <sys/stat.h> 15#include <sys/stat.h>
16#include <sys/statvfs.h>
16#include <limits.h> 17#include <limits.h>
17#include <fcntl.h> 18#include <fcntl.h>
18#include <sched.h> 19#include <sched.h>
20
21#if _POSIX_MEMLOCK || _POSIX_MAPPED_FILES
22# include <sys/mman.h>
23#endif
19 24
20/* perl namespace pollution */ 25/* perl namespace pollution */
21#undef VERSION 26#undef VERSION
22 27
23#ifdef _WIN32 28#ifdef _WIN32
99#define expect_false(expr) expect ((expr) != 0, 0) 104#define expect_false(expr) expect ((expr) != 0, 0)
100#define expect_true(expr) expect ((expr) != 0, 1) 105#define expect_true(expr) expect ((expr) != 0, 1)
101 106
102/*****************************************************************************/ 107/*****************************************************************************/
103 108
104static HV *stash;
105typedef SV SV8; /* byte-sv, used for argument-checking */ 109typedef SV SV8; /* byte-sv, used for argument-checking */
106typedef int aio_rfd; /* read file desriptor */ 110typedef int aio_rfd; /* read file desriptor */
107typedef int aio_wfd; /* write file descriptor */ 111typedef int aio_wfd; /* write file descriptor */
108 112
109#define AIO_REQ_KLASS "IO::AIO::REQ" 113static HV *aio_stash, *aio_req_stash, *aio_grp_stash;
110#define AIO_GRP_KLASS "IO::AIO::GRP"
111 114
112#define EIO_REQ_MEMBERS \ 115#define EIO_REQ_MEMBERS \
113 SV *callback; \ 116 SV *callback; \
114 SV *sv1, *sv2; \ 117 SV *sv1, *sv2; \
115 STRLEN stroffset; \ 118 STRLEN stroffset; \
121 124
122#ifndef POSIX_FADV_NORMAL 125#ifndef POSIX_FADV_NORMAL
123# define POSIX_FADV_NORMAL 0 126# define POSIX_FADV_NORMAL 0
124# define NO_FADVISE 1 127# define NO_FADVISE 1
125#endif 128#endif
126
127#ifndef POSIX_FADV_SEQUENTIAL 129#ifndef POSIX_FADV_SEQUENTIAL
128# define POSIX_FADV_SEQUENTIAL 0 130# define POSIX_FADV_SEQUENTIAL 0
129#endif 131#endif
130
131#ifndef POSIX_FADV_RANDOM 132#ifndef POSIX_FADV_RANDOM
132# define POSIX_FADV_RANDOM 0 133# define POSIX_FADV_RANDOM 0
133#endif 134#endif
134
135#ifndef POSIX_FADV_NOREUSE 135#ifndef POSIX_FADV_NOREUSE
136# define POSIX_FADV_NOREUSE 0 136# define POSIX_FADV_NOREUSE 0
137#endif 137#endif
138
139#ifndef POSIX_FADV_WILLNEED 138#ifndef POSIX_FADV_WILLNEED
140# define POSIX_FADV_WILLNEED 0 139# define POSIX_FADV_WILLNEED 0
141#endif 140#endif
142
143#ifndef POSIX_FADV_DONTNEED 141#ifndef POSIX_FADV_DONTNEED
144# define POSIX_FADV_DONTNEED 0 142# define POSIX_FADV_DONTNEED 0
143#endif
144
145#ifndef POSIX_MADV_NORMAL
146# define POSIX_MADV_NORMAL 0
147# define NO_MADVISE 1
148#endif
149#ifndef POSIX_MADV_SEQUENTIAL
150# define POSIX_MADV_SEQUENTIAL 0
151#endif
152#ifndef POSIX_MADV_RANDOM
153# define POSIX_MADV_RANDOM 0
154#endif
155#ifndef POSIX_MADV_WILLNEED
156# define POSIX_MADV_WILLNEED 0
157#endif
158#ifndef POSIX_MADV_DONTNEED
159# define POSIX_MADV_DONTNEED 0
160#endif
161
162#ifndef ST_NODEV
163# define ST_NODEV 0
164#endif
165#ifndef ST_NOEXEC
166# define ST_NOEXEC 0
167#endif
168#ifndef ST_SYNCHRONOUS
169# define ST_SYNCHRONOUS 0
170#endif
171#ifndef ST_MANDLOCK
172# define ST_MANDLOCK 0
173#endif
174#ifndef ST_WRITE
175# define ST_WRITE 0
176#endif
177#ifndef ST_APPEND
178# define ST_APPEND 0
179#endif
180#ifndef ST_IMMUTABLE
181# define ST_IMMUTABLE 0
182#endif
183#ifndef ST_NOATIME
184# define ST_NOATIME 0
185#endif
186#ifndef ST_NODIRATIME
187# define ST_NODIRATIME 0
188#endif
189#ifndef ST_RELATIME
190# define ST_RELATIME 0
191#endif
192
193#ifndef MCL_CURRENT
194# define MCL_CURRENT 0
195#endif
196#ifndef MCL_FUTURE
197# define MCL_FUTURE 0
198#endif
199
200#ifndef MAP_ANONYMOUS
201# ifdef MAP_ANON
202# define MAP_ANONYMOUS MAP_ANON
203# else
204# define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */
205# endif
206#endif
207#ifndef MAP_HUGETLB
208# define MAP_HUGETLB 0
209#endif
210#ifndef MAP_LOCKED
211# define MAP_LOCKED 0
212#endif
213#ifndef MAP_NORESERVE
214# define MAP_NORESERVE 0
215#endif
216#ifndef MAP_POPULATE
217# define MAP_POPULATE 0
218#endif
219#ifndef MAP_NONBLOCK
220# define MAP_NONBLOCK 0
221#endif
222
223#ifndef PAGESIZE
224# define PAGESIZE sysconf (_SC_PAGESIZE)
145#endif 225#endif
146 226
147static int req_invoke (eio_req *req); 227static int req_invoke (eio_req *req);
148#define EIO_FINISH(req) req_invoke (req) 228#define EIO_FINISH(req) req_invoke (req)
149static void req_destroy (eio_req *grp); 229static void req_destroy (eio_req *grp);
160 240
161static SV *on_next_submit; 241static SV *on_next_submit;
162static int next_pri = EIO_PRI_DEFAULT; 242static int next_pri = EIO_PRI_DEFAULT;
163static int max_outstanding; 243static int max_outstanding;
164 244
165static int respipe_osf [2];
166static s_epipe respipe; 245static s_epipe respipe;
167 246
168static void req_destroy (aio_req req); 247static void req_destroy (aio_req req);
169static void req_cancel (aio_req req); 248static void req_cancel (aio_req req);
170 249
179 /* read any signals sent by the worker threads */ 258 /* read any signals sent by the worker threads */
180 s_epipe_drain (&respipe); 259 s_epipe_drain (&respipe);
181} 260}
182 261
183/* must be called at most once */ 262/* must be called at most once */
184static SV *req_sv (aio_req req, const char *klass) 263static SV *req_sv (aio_req req, HV *stash)
185{ 264{
186 if (!req->self) 265 if (!req->self)
187 { 266 {
188 req->self = (SV *)newHV (); 267 req->self = (SV *)newHV ();
189 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); 268 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
190 } 269 }
191 270
192 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); 271 return sv_2mortal (sv_bless (newRV_inc (req->self), stash));
193} 272}
194 273
195static aio_req SvAIO_REQ (SV *sv) 274static aio_req SvAIO_REQ (SV *sv)
196{ 275{
197 MAGIC *mg; 276 MAGIC *mg;
198 277
199 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) 278 if (!SvROK (sv)
279 || (SvSTASH (SvRV (sv)) != aio_grp_stash
280 && SvSTASH (SvRV (sv)) != aio_req_stash
281 && !sv_derived_from (sv, "IO::AIO::REQ")))
200 croak ("object of class " AIO_REQ_KLASS " expected"); 282 croak ("object of class IO::AIO::REQ expected");
201 283
202 mg = mg_find (SvRV (sv), PERL_MAGIC_ext); 284 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
203 285
204 return mg ? (aio_req)mg->mg_ptr : 0; 286 return mg ? (aio_req)mg->mg_ptr : 0;
205} 287}
211 dSP; 293 dSP;
212 294
213 ENTER; 295 ENTER;
214 SAVETMPS; 296 SAVETMPS;
215 PUSHMARK (SP); 297 PUSHMARK (SP);
216 XPUSHs (req_sv (grp, AIO_GRP_KLASS)); 298 XPUSHs (req_sv (grp, aio_grp_stash));
217 PUTBACK; 299 PUTBACK;
218 call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); 300 call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR);
219 SPAGAIN; 301 SPAGAIN;
220 FREETMPS; 302 FREETMPS;
221 LEAVE; 303 LEAVE;
239 } 321 }
240} 322}
241 323
242static int req_invoke (eio_req *req) 324static int req_invoke (eio_req *req)
243{ 325{
244 dSP;
245
246 if (req->flags & FLAG_SV2_RO_OFF) 326 if (req->flags & FLAG_SV2_RO_OFF)
247 SvREADONLY_off (req->sv2); 327 SvREADONLY_off (req->sv2);
248 328
249 if (!EIO_CANCELLED (req) && req->callback) 329 if (!EIO_CANCELLED (req) && req->callback)
250 { 330 {
331 dSP;
332 static SV *sv_result_cache; /* caches the result integer SV */
333 SV *sv_result;
334
251 ENTER; 335 ENTER;
252 SAVETMPS; 336 SAVETMPS;
253 PUSHMARK (SP); 337 PUSHMARK (SP);
254 EXTEND (SP, 1); 338 EXTEND (SP, 1);
339
340 /* do not recreate the result IV from scratch each time */
341 if (expect_true (sv_result_cache))
342 {
343 sv_result = sv_result_cache; sv_result_cache = 0;
344 SvIV_set (sv_result, req->result);
345 }
346 else
347 {
348 sv_result = newSViv (req->result);
349 SvREADONLY_on (sv_result);
350 }
255 351
256 switch (req->type) 352 switch (req->type)
257 { 353 {
258 case EIO_READDIR: 354 case EIO_READDIR:
259 { 355 {
327 int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); 423 int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR);
328 char sym [64]; 424 char sym [64];
329 int symlen; 425 int symlen;
330 426
331 symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); 427 symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result);
332 gv_init (gv, stash, sym, symlen, 0); 428 gv_init (gv, aio_stash, sym, symlen, 0);
333 429
334 symlen = snprintf ( 430 symlen = snprintf (
335 sym, 431 sym,
336 sizeof (sym), 432 sizeof (sym),
337 "%s&=%d", 433 "%s&=%d",
345 441
346 PUSHs (fh); 442 PUSHs (fh);
347 } 443 }
348 break; 444 break;
349 445
446 case EIO_STATVFS:
447 case EIO_FSTATVFS:
448 {
449 SV *rv = &PL_sv_undef;
450
451 if (req->result >= 0)
452 {
453 EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req);
454 HV *hv = newHV ();
455
456 rv = sv_2mortal (newRV_noinc ((SV *)hv));
457
458 hv_store (hv, "bsize" , sizeof ("bsize" ) - 1, newSVval64 (f->f_bsize ), 0);
459 hv_store (hv, "frsize" , sizeof ("frsize" ) - 1, newSVval64 (f->f_frsize ), 0);
460 hv_store (hv, "blocks" , sizeof ("blocks" ) - 1, newSVval64 (f->f_blocks ), 0);
461 hv_store (hv, "bfree" , sizeof ("bfree" ) - 1, newSVval64 (f->f_bfree ), 0);
462 hv_store (hv, "bavail" , sizeof ("bavail" ) - 1, newSVval64 (f->f_bavail ), 0);
463 hv_store (hv, "files" , sizeof ("files" ) - 1, newSVval64 (f->f_files ), 0);
464 hv_store (hv, "ffree" , sizeof ("ffree" ) - 1, newSVval64 (f->f_ffree ), 0);
465 hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0);
466 hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (f->f_fsid ), 0);
467 hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0);
468 hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0);
469 }
470
471 PUSHs (rv);
472 }
473
474 break;
475
350 case EIO_GROUP: 476 case EIO_GROUP:
351 req->int1 = 2; /* mark group as finished */ 477 req->int1 = 2; /* mark group as finished */
352 478
353 if (req->sv1) 479 if (req->sv1)
354 { 480 {
374 case EIO_LSTAT: 500 case EIO_LSTAT:
375 case EIO_FSTAT: 501 case EIO_FSTAT:
376 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; 502 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT;
377 PL_laststatval = req->result; 503 PL_laststatval = req->result;
378 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); 504 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2);
379 PUSHs (sv_2mortal (newSViv (req->result))); 505 PUSHs (sv_result);
380 break; 506 break;
381 507
382 case EIO_READ: 508 case EIO_READ:
383 { 509 {
384 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); 510 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0));
385 *SvEND (req->sv2) = 0; 511 *SvEND (req->sv2) = 0;
386 SvPOK_only (req->sv2); 512 SvPOK_only (req->sv2);
387 SvSETMAGIC (req->sv2); 513 SvSETMAGIC (req->sv2);
388 PUSHs (sv_2mortal (newSViv (req->result))); 514 PUSHs (sv_result);
389 } 515 }
390 break; 516 break;
391 517
392 case EIO_DUP2: 518 case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), su fudge result value */
393 if (req->result > 0) 519 if (req->result > 0)
394 req->result = 0; 520 SvIV_set (sv_result, 0);
395 /* FALLTHROUGH */ 521 /* FALLTHROUGH */
396 522
397 default: 523 default:
398 PUSHs (sv_2mortal (newSViv (req->result))); 524 PUSHs (sv_result);
399 break; 525 break;
400 } 526 }
401 527
402 errno = req->errorno; 528 errno = req->errorno;
403 529
404 PUTBACK; 530 PUTBACK;
405 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); 531 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD);
406 SPAGAIN; 532 SPAGAIN;
533
534 if (expect_false (SvREFCNT (sv_result) != 1 || sv_result_cache))
535 SvREFCNT_dec (sv_result);
536 else
537 sv_result_cache = sv_result;
407 538
408 FREETMPS; 539 FREETMPS;
409 LEAVE; 540 LEAVE;
410 541
411 PUTBACK; 542 PUTBACK;
444 575
445static void 576static void
446create_respipe (void) 577create_respipe (void)
447{ 578{
448 if (s_epipe_renew (&respipe)) 579 if (s_epipe_renew (&respipe))
449 croak ("unable to initialize result pipe"); 580 croak ("IO::AIO: unable to initialize result pipe");
450
451 respipe_osf [0] = S_TO_SOCKET (respipe.fd [0]);
452 respipe_osf [1] = S_TO_SOCKET (respipe.fd [1]);
453} 581}
454 582
455static void poll_wait (void) 583static void poll_wait (void)
456{ 584{
457 while (eio_nreqs ()) 585 while (eio_nreqs ())
489 617
490static void atfork_child (void) 618static void atfork_child (void)
491{ 619{
492 create_respipe (); 620 create_respipe ();
493} 621}
622
623/*****************************************************************************/
624
625#if !_POSIX_MAPPED_FILES
626# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1)
627# define munmap(addr,length) (errno = ENOSYS, -1)
628#endif
629
630#define MMAP_MAGIC PERL_MAGIC_ext
631
632static int
633mmap_free (pTHX_ SV *sv, MAGIC *mg)
634{
635 int old_errno = errno;
636 munmap (mg->mg_ptr, (size_t)mg->mg_obj);
637 errno = old_errno;
638
639 mg->mg_obj = 0; /* just in case */
640
641 SvREADONLY_off (sv);
642
643 if (SvPVX (sv) != mg->mg_ptr)
644 croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected");
645
646 SvCUR_set (sv, 0);
647 SvPVX (sv) = 0;
648 SvOK_off (sv);
649
650 return 0;
651}
652
653static MGVTBL mmap_vtbl = {
654 0, 0, 0, 0, mmap_free
655};
656
657/*****************************************************************************/
494 658
495static SV * 659static SV *
496get_cb (SV *cb_sv) 660get_cb (SV *cb_sv)
497{ 661{
498 SvGETMAGIC (cb_sv); 662 SvGETMAGIC (cb_sv);
518 PUTBACK; \ 682 PUTBACK; \
519 req_submit (req); \ 683 req_submit (req); \
520 SPAGAIN; \ 684 SPAGAIN; \
521 \ 685 \
522 if (GIMME_V != G_VOID) \ 686 if (GIMME_V != G_VOID) \
523 XPUSHs (req_sv (req, AIO_REQ_KLASS)); 687 XPUSHs (req_sv (req, aio_req_stash));
524 688
525MODULE = IO::AIO PACKAGE = IO::AIO 689MODULE = IO::AIO PACKAGE = IO::AIO
526 690
527PROTOTYPES: ENABLE 691PROTOTYPES: ENABLE
528 692
530{ 694{
531 static const struct { 695 static const struct {
532 const char *name; 696 const char *name;
533 IV iv; 697 IV iv;
534 } *civ, const_iv[] = { 698 } *civ, const_iv[] = {
535# define const_iv(name, value) { # name, (IV) value }, 699# define const_niv(name, value) { # name, (IV) value },
700# define const_iv(name) { # name, (IV) name },
536# define const_eio(name) { # name, (IV) EIO_ ## name }, 701# define const_eio(name) { # name, (IV) EIO_ ## name },
537 const_iv (EXDEV , EXDEV) 702 const_iv (EXDEV)
538 const_iv (ENOSYS , ENOSYS) 703 const_iv (ENOSYS)
539 const_iv (O_RDONLY, O_RDONLY) 704 const_iv (O_RDONLY)
540 const_iv (O_WRONLY, O_WRONLY) 705 const_iv (O_WRONLY)
706 const_iv (O_RDWR)
541 const_iv (O_CREAT , O_CREAT) 707 const_iv (O_CREAT)
542 const_iv (O_TRUNC , O_TRUNC) 708 const_iv (O_TRUNC)
709 const_iv (O_EXCL)
710 const_iv (O_APPEND)
543#ifndef _WIN32 711#ifndef _WIN32
544 const_iv (S_IFIFO , S_IFIFO) 712 const_iv (S_IFIFO)
545#endif 713#endif
546 const_iv (FADV_NORMAL , POSIX_FADV_NORMAL) 714 const_niv (FADV_NORMAL , POSIX_FADV_NORMAL)
547 const_iv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) 715 const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL)
548 const_iv (FADV_RANDOM , POSIX_FADV_RANDOM) 716 const_niv (FADV_RANDOM , POSIX_FADV_RANDOM)
549 const_iv (FADV_NOREUSE , POSIX_FADV_NOREUSE) 717 const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE)
550 const_iv (FADV_WILLNEED , POSIX_FADV_WILLNEED) 718 const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED)
551 const_iv (FADV_DONTNEED , POSIX_FADV_DONTNEED) 719 const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED)
720
721 const_niv (MADV_NORMAL , POSIX_MADV_NORMAL)
722 const_niv (MADV_SEQUENTIAL, POSIX_MADV_SEQUENTIAL)
723 const_niv (MADV_RANDOM , POSIX_MADV_RANDOM)
724 const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED)
725 const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED)
726
727 const_iv (ST_RDONLY)
728 const_iv (ST_NOSUID)
729 const_iv (ST_NODEV)
730 const_iv (ST_NOEXEC)
731 const_iv (ST_SYNCHRONOUS)
732 const_iv (ST_MANDLOCK)
733 const_iv (ST_WRITE)
734 const_iv (ST_APPEND)
735 const_iv (ST_IMMUTABLE)
736 const_iv (ST_NOATIME)
737 const_iv (ST_NODIRATIME)
738 const_iv (ST_RELATIME)
739
740 const_iv (PROT_EXEC)
741 const_iv (PROT_NONE)
742 const_iv (PROT_READ)
743 const_iv (PROT_WRITE)
744
745 /*const_iv (MAP_FIXED)*/
746 const_iv (MAP_PRIVATE)
747 const_iv (MAP_SHARED)
748 const_iv (MAP_ANONYMOUS)
749
750 /* linuxish */
751 const_iv (MAP_HUGETLB)
752 const_iv (MAP_LOCKED)
753 const_iv (MAP_NORESERVE)
754 const_iv (MAP_POPULATE)
755 const_iv (MAP_NONBLOCK)
756
757 const_iv (MCL_FUTURE)
758 const_iv (MCL_CURRENT)
759
760 const_eio (MS_ASYNC)
761 const_eio (MS_INVALIDATE)
762 const_eio (MS_SYNC)
763
764 const_eio (MT_MODIFY)
552 765
553 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) 766 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE)
554 const_eio (SYNC_FILE_RANGE_WRITE) 767 const_eio (SYNC_FILE_RANGE_WRITE)
555 const_eio (SYNC_FILE_RANGE_WAIT_AFTER) 768 const_eio (SYNC_FILE_RANGE_WAIT_AFTER)
556 769
568 const_eio (DT_LNK) 781 const_eio (DT_LNK)
569 const_eio (DT_SOCK) 782 const_eio (DT_SOCK)
570 const_eio (DT_WHT) 783 const_eio (DT_WHT)
571 }; 784 };
572 785
573 stash = gv_stashpv ("IO::AIO", 1); 786 aio_stash = gv_stashpv ("IO::AIO" , 1);
787 aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1);
788 aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1);
574 789
575 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 790 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
576 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 791 newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv));
792
793 newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE));
577 794
578 create_respipe (); 795 create_respipe ();
579 796
580 if (eio_init (want_poll, done_poll) < 0) 797 if (eio_init (want_poll, done_poll) < 0)
581 croak ("IO::AIO: unable to initialise eio library"); 798 croak ("IO::AIO: unable to initialise eio library");
808} 1025}
809 1026
810void 1027void
811aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) 1028aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef)
812 ALIAS: 1029 ALIAS:
813 aio_stat = EIO_STAT 1030 aio_stat = EIO_STAT
814 aio_lstat = EIO_LSTAT 1031 aio_lstat = EIO_LSTAT
1032 aio_statvfs = EIO_STATVFS
815 PPCODE: 1033 PPCODE:
816{ 1034{
817 dREQ; 1035 dREQ;
818 1036
819 req->sv1 = newSVsv (fh_or_path); 1037 req->sv1 = newSVsv (fh_or_path);
823 req->type = ix; 1041 req->type = ix;
824 req->ptr1 = SvPVbyte_nolen (req->sv1); 1042 req->ptr1 = SvPVbyte_nolen (req->sv1);
825 } 1043 }
826 else 1044 else
827 { 1045 {
828 req->type = EIO_FSTAT; 1046 req->type = ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT;
829 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 1047 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
830 } 1048 }
831 1049
832 REQ_SEND; 1050 REQ_SEND;
833} 1051}
996 1214
997 REQ_SEND; 1215 REQ_SEND;
998} 1216}
999 1217
1000void 1218void
1219aio_mtouch (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, int flags = 0, SV *callback=&PL_sv_undef)
1220 ALIAS:
1221 aio_mtouch = EIO_MTOUCH
1222 aio_msync = EIO_MSYNC
1223 PROTOTYPE: $$$$;$
1224 PPCODE:
1225{
1226 STRLEN svlen;
1227 UV len = SvUV (length);
1228 char *svptr = SvPVbyte (data, svlen);
1229
1230 if (offset < 0)
1231 offset += svlen;
1232
1233 if (offset < 0 || offset > svlen)
1234 croak ("offset outside of scalar");
1235
1236 if (!SvOK (length) || len + offset > svlen)
1237 len = svlen - offset;
1238
1239 {
1240 dREQ;
1241
1242 req->type = ix;
1243 req->size = len;
1244 req->sv2 = SvREFCNT_inc (data);
1245 req->ptr2 = (char *)svptr + offset;
1246 req->int1 = flags;
1247
1248 REQ_SEND;
1249 }
1250}
1251
1252void
1001aio_busy (double delay, SV *callback=&PL_sv_undef) 1253aio_busy (double delay, SV *callback=&PL_sv_undef)
1002 PPCODE: 1254 PPCODE:
1003{ 1255{
1004 dREQ; 1256 dREQ;
1005 1257
1017 dREQ; 1269 dREQ;
1018 1270
1019 req->type = EIO_GROUP; 1271 req->type = EIO_GROUP;
1020 1272
1021 req_submit (req); 1273 req_submit (req);
1022 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1274 XPUSHs (req_sv (req, aio_grp_stash));
1023} 1275}
1024 1276
1025void 1277void
1026aio_nop (SV *callback=&PL_sv_undef) 1278aio_nop (SV *callback=&PL_sv_undef)
1027 ALIAS: 1279 ALIAS:
1136 PROTOTYPE: $$$$ 1388 PROTOTYPE: $$$$
1137 CODE: 1389 CODE:
1138#if _XOPEN_SOURCE >= 600 && !NO_FADVISE 1390#if _XOPEN_SOURCE >= 600 && !NO_FADVISE
1139 RETVAL = posix_fadvise (fh, offset, length, advice); 1391 RETVAL = posix_fadvise (fh, offset, length, advice);
1140#else 1392#else
1141 RETVAL = errno = ENOSYS; 1393 RETVAL = errno = ENOSYS; /* yes, this is actually correct */
1142#endif 1394#endif
1143 OUTPUT: 1395 OUTPUT:
1144 RETVAL 1396 RETVAL
1145 1397
1146ssize_t 1398ssize_t
1147sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) 1399sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count)
1148 PROTOTYPE: $$$$ 1400 PROTOTYPE: $$$$
1149 CODE: 1401 CODE:
1150 eio_sendfile_sync (ofh, ifh, offset, count); 1402 RETVAL = eio_sendfile_sync (ofh, ifh, offset, count);
1403 OUTPUT:
1404 RETVAL
1405
1406void
1407mmap (SV *scalar, size_t length, int prot, int flags, SV *fh, off_t offset = 0)
1408 PROTOTYPE: $$$$$;$
1409 PPCODE:
1410 sv_unmagic (scalar, MMAP_MAGIC);
1411{
1412 int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1;
1413 void *addr = (void *)mmap (0, length, prot, flags, fd, offset);
1414 if (addr == (void *)-1)
1415 XSRETURN_NO;
1416
1417 sv_force_normal (scalar);
1418
1419 /* we store the length in mg_obj, as namlen is I32 :/ */
1420 sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0)
1421 ->mg_obj = (SV *)length;
1422
1423 SvUPGRADE (scalar, SVt_PV); /* nop... */
1424
1425 if (!(prot & PROT_WRITE))
1426 SvREADONLY_on (scalar);
1427
1428 if (SvLEN (scalar))
1429 Safefree (SvPVX (scalar));
1430
1431 SvPVX (scalar) = (char *)addr;
1432 SvCUR_set (scalar, length);
1433 SvLEN_set (scalar, 0);
1434 SvPOK_only (scalar);
1435
1436 XSRETURN_YES;
1437}
1438
1439void
1440munmap (SV *scalar)
1441 PROTOTYPE: $
1442 CODE:
1443 sv_unmagic (scalar, MMAP_MAGIC);
1444
1445int
1446madvise (SV *scalar, off_t offset, off_t length, IV advice)
1447 PROTOTYPE: $$$$
1448 CODE:
1449{
1450 char *addr = SvPV_nolen (scalar) + offset;
1451
1452 if (!SvOK (ST (2)))
1453 length = SvCUR (scalar) - offset;
1454
1455 if (addr >= SvEND (scalar) || length <= 0)
1456 XSRETURN_EMPTY;
1457
1458 #if _XOPEN_SOURCE >= 600 && !NO_MADVISE
1459 RETVAL = posix_madvise (addr, length, advice);
1460 #else
1461 RETVAL = errno = ENOSYS; /* yes, this is actually correct */
1462 #endif
1463}
1464 OUTPUT:
1465 RETVAL
1466
1467int
1468mlockall (int flags)
1469 PROTOTYPE: $
1470 CODE:
1471#if _POSIX_MEMLOCK
1472#if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1473 extern int mallopt (int, int);
1474 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1475#endif
1476 mlockall (flags);
1477#else
1478 RETVAL = -1;
1479 errno = ENOSYS;
1480#endif
1481 OUTPUT:
1482 RETVAL
1483
1484int
1485munlockall ()
1486 PROTOTYPE:
1487 CODE:
1488#if _POSIX_MEMLOCK
1489 munlockall ();
1490#else
1491 RETVAL = -1;
1492 errno = ENOSYS;
1493#endif
1494 OUTPUT:
1495 RETVAL
1151 1496
1152void _on_next_submit (SV *cb) 1497void _on_next_submit (SV *cb)
1153 CODE: 1498 CODE:
1154 SvREFCNT_dec (on_next_submit); 1499 SvREFCNT_dec (on_next_submit);
1155 on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; 1500 on_next_submit = SvOK (cb) ? newSVsv (cb) : 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines