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.161 by root, Sun Jan 10 20:28:43 2010 UTC vs.
Revision 1.170 by root, Wed Aug 4 16:09:36 2010 UTC

16#include <sys/statvfs.h> 16#include <sys/statvfs.h>
17#include <limits.h> 17#include <limits.h>
18#include <fcntl.h> 18#include <fcntl.h>
19#include <sched.h> 19#include <sched.h>
20 20
21#if _POSIX_MEMLOCK 21#if _POSIX_MEMLOCK || _POSIX_MAPPED_FILES
22# include <sys/mman.h> 22# include <sys/mman.h>
23#endif 23#endif
24 24
25/* perl namespace pollution */ 25/* perl namespace pollution */
26#undef VERSION 26#undef VERSION
140#endif 140#endif
141#ifndef POSIX_FADV_DONTNEED 141#ifndef POSIX_FADV_DONTNEED
142# define POSIX_FADV_DONTNEED 0 142# define POSIX_FADV_DONTNEED 0
143#endif 143#endif
144 144
145#if _XOPEN_SOURCE < 600 || NO_FADVISE
146# define posix_fadvise(a,b,c,d) errno = ENOSYS /* also return ENOSYS */
147#endif
148
149#ifndef POSIX_MADV_NORMAL
150# define POSIX_MADV_NORMAL 0
151# define NO_MADVISE 1
152#endif
153#ifndef POSIX_MADV_SEQUENTIAL
154# define POSIX_MADV_SEQUENTIAL 0
155#endif
156#ifndef POSIX_MADV_RANDOM
157# define POSIX_MADV_RANDOM 0
158#endif
159#ifndef POSIX_MADV_WILLNEED
160# define POSIX_MADV_WILLNEED 0
161#endif
162#ifndef POSIX_MADV_DONTNEED
163# define POSIX_MADV_DONTNEED 0
164#endif
165
166#if _XOPEN_SOURCE < 600 || NO_MADVISE
167# define posix_madvise(a,b,c) errno = ENOSYS /* also return ENOSYS */
168#endif
169
145#ifndef ST_NODEV 170#ifndef ST_NODEV
146# define ST_NODEV 0 171# define ST_NODEV 0
147#endif 172#endif
148#ifndef ST_NOEXEC 173#ifndef ST_NOEXEC
149# define ST_NOEXEC 0 174# define ST_NOEXEC 0
176#ifndef MCL_CURRENT 201#ifndef MCL_CURRENT
177# define MCL_CURRENT 0 202# define MCL_CURRENT 0
178#endif 203#endif
179#ifndef MCL_FUTURE 204#ifndef MCL_FUTURE
180# define MCL_FUTURE 0 205# define MCL_FUTURE 0
206#endif
207
208#ifndef MAP_ANONYMOUS
209# ifdef MAP_ANON
210# define MAP_ANONYMOUS MAP_ANON
211# else
212# define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */
213# endif
214#endif
215#ifndef MAP_HUGETLB
216# define MAP_HUGETLB 0
217#endif
218#ifndef MAP_LOCKED
219# define MAP_LOCKED 0
220#endif
221#ifndef MAP_NORESERVE
222# define MAP_NORESERVE 0
223#endif
224#ifndef MAP_POPULATE
225# define MAP_POPULATE 0
226#endif
227#ifndef MAP_NONBLOCK
228# define MAP_NONBLOCK 0
229#endif
230
231#ifndef PAGESIZE
232# define PAGESIZE sysconf (_SC_PAGESIZE)
181#endif 233#endif
182 234
183static int req_invoke (eio_req *req); 235static int req_invoke (eio_req *req);
184#define EIO_FINISH(req) req_invoke (req) 236#define EIO_FINISH(req) req_invoke (req)
185static void req_destroy (eio_req *grp); 237static void req_destroy (eio_req *grp);
277 } 329 }
278} 330}
279 331
280static int req_invoke (eio_req *req) 332static int req_invoke (eio_req *req)
281{ 333{
282 dSP;
283
284 if (req->flags & FLAG_SV2_RO_OFF) 334 if (req->flags & FLAG_SV2_RO_OFF)
285 SvREADONLY_off (req->sv2); 335 SvREADONLY_off (req->sv2);
286 336
287 if (!EIO_CANCELLED (req) && req->callback) 337 if (!EIO_CANCELLED (req) && req->callback)
288 { 338 {
339 dSP;
340 static SV *sv_result_cache; /* caches the result integer SV */
341 SV *sv_result;
342
289 ENTER; 343 ENTER;
290 SAVETMPS; 344 SAVETMPS;
291 PUSHMARK (SP); 345 PUSHMARK (SP);
292 EXTEND (SP, 1); 346 EXTEND (SP, 1);
347
348 /* do not recreate the result IV from scratch each time */
349 if (expect_true (sv_result_cache))
350 {
351 sv_result = sv_result_cache; sv_result_cache = 0;
352 SvIV_set (sv_result, req->result);
353 }
354 else
355 {
356 sv_result = newSViv (req->result);
357 SvREADONLY_on (sv_result);
358 }
293 359
294 switch (req->type) 360 switch (req->type)
295 { 361 {
296 case EIO_READDIR: 362 case EIO_READDIR:
297 { 363 {
442 case EIO_LSTAT: 508 case EIO_LSTAT:
443 case EIO_FSTAT: 509 case EIO_FSTAT:
444 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; 510 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT;
445 PL_laststatval = req->result; 511 PL_laststatval = req->result;
446 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); 512 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2);
447 PUSHs (sv_2mortal (newSViv (req->result))); 513 PUSHs (sv_result);
448 break; 514 break;
449 515
450 case EIO_READ: 516 case EIO_READ:
451 { 517 {
452 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); 518 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0));
453 *SvEND (req->sv2) = 0; 519 *SvEND (req->sv2) = 0;
454 SvPOK_only (req->sv2); 520 SvPOK_only (req->sv2);
455 SvSETMAGIC (req->sv2); 521 SvSETMAGIC (req->sv2);
456 PUSHs (sv_2mortal (newSViv (req->result))); 522 PUSHs (sv_result);
457 } 523 }
458 break; 524 break;
459 525
460 case EIO_DUP2: 526 case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), su fudge result value */
461 if (req->result > 0) 527 if (req->result > 0)
462 req->result = 0; 528 SvIV_set (sv_result, 0);
463 /* FALLTHROUGH */ 529 /* FALLTHROUGH */
464 530
465 default: 531 default:
466 PUSHs (sv_2mortal (newSViv (req->result))); 532 PUSHs (sv_result);
467 break; 533 break;
468 } 534 }
469 535
470 errno = req->errorno; 536 errno = req->errorno;
471 537
472 PUTBACK; 538 PUTBACK;
473 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); 539 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD);
474 SPAGAIN; 540 SPAGAIN;
541
542 if (expect_false (SvREFCNT (sv_result) != 1 || sv_result_cache))
543 SvREFCNT_dec (sv_result);
544 else
545 sv_result_cache = sv_result;
475 546
476 FREETMPS; 547 FREETMPS;
477 LEAVE; 548 LEAVE;
478 549
479 PUTBACK; 550 PUTBACK;
554 625
555static void atfork_child (void) 626static void atfork_child (void)
556{ 627{
557 create_respipe (); 628 create_respipe ();
558} 629}
630
631/*****************************************************************************/
632
633#if !_POSIX_MAPPED_FILES
634# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1)
635# define munmap(addr,length) (errno = ENOSYS, -1)
636#endif
637
638#define MMAP_MAGIC PERL_MAGIC_ext
639
640static int
641mmap_free (pTHX_ SV *sv, MAGIC *mg)
642{
643 int old_errno = errno;
644 munmap (mg->mg_ptr, (size_t)mg->mg_obj);
645 errno = old_errno;
646
647 mg->mg_obj = 0; /* just in case */
648
649 SvREADONLY_off (sv);
650
651 if (SvPVX (sv) != mg->mg_ptr)
652 croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected");
653
654 SvCUR_set (sv, 0);
655 SvPVX (sv) = 0;
656 SvOK_off (sv);
657
658 return 0;
659}
660
661static MGVTBL mmap_vtbl = {
662 0, 0, 0, 0, mmap_free
663};
664
665/*****************************************************************************/
559 666
560static SV * 667static SV *
561get_cb (SV *cb_sv) 668get_cb (SV *cb_sv)
562{ 669{
563 SvGETMAGIC (cb_sv); 670 SvGETMAGIC (cb_sv);
595{ 702{
596 static const struct { 703 static const struct {
597 const char *name; 704 const char *name;
598 IV iv; 705 IV iv;
599 } *civ, const_iv[] = { 706 } *civ, const_iv[] = {
600# define const_iv(name, value) { # name, (IV) value }, 707# define const_niv(name, value) { # name, (IV) value },
708# define const_iv(name) { # name, (IV) name },
601# define const_eio(name) { # name, (IV) EIO_ ## name }, 709# define const_eio(name) { # name, (IV) EIO_ ## name },
602 const_iv (EXDEV , EXDEV) 710 const_iv (EXDEV)
603 const_iv (ENOSYS , ENOSYS) 711 const_iv (ENOSYS)
604 const_iv (O_RDONLY, O_RDONLY) 712 const_iv (O_RDONLY)
605 const_iv (O_WRONLY, O_WRONLY) 713 const_iv (O_WRONLY)
714 const_iv (O_RDWR)
606 const_iv (O_CREAT , O_CREAT) 715 const_iv (O_CREAT)
607 const_iv (O_TRUNC , O_TRUNC) 716 const_iv (O_TRUNC)
717 const_iv (O_EXCL)
718 const_iv (O_APPEND)
608#ifndef _WIN32 719#ifndef _WIN32
609 const_iv (S_IFIFO , S_IFIFO) 720 const_iv (S_IFIFO)
610#endif 721#endif
611 const_iv (FADV_NORMAL , POSIX_FADV_NORMAL) 722 const_niv (FADV_NORMAL , POSIX_FADV_NORMAL)
612 const_iv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) 723 const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL)
613 const_iv (FADV_RANDOM , POSIX_FADV_RANDOM) 724 const_niv (FADV_RANDOM , POSIX_FADV_RANDOM)
614 const_iv (FADV_NOREUSE , POSIX_FADV_NOREUSE) 725 const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE)
615 const_iv (FADV_WILLNEED , POSIX_FADV_WILLNEED) 726 const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED)
616 const_iv (FADV_DONTNEED , POSIX_FADV_DONTNEED) 727 const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED)
617 728
618 const_iv (ST_RDONLY , ST_RDONLY) 729 const_niv (MADV_NORMAL , POSIX_MADV_NORMAL)
619 const_iv (ST_NOSUID , ST_NOSUID) 730 const_niv (MADV_SEQUENTIAL, POSIX_MADV_SEQUENTIAL)
620 const_iv (ST_NODEV , ST_NODEV) 731 const_niv (MADV_RANDOM , POSIX_MADV_RANDOM)
621 const_iv (ST_NOEXEC , ST_NOEXEC) 732 const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED)
733 const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED)
734
735 const_iv (ST_RDONLY)
736 const_iv (ST_NOSUID)
737 const_iv (ST_NODEV)
738 const_iv (ST_NOEXEC)
622 const_iv (ST_SYNCHRONOUS , ST_SYNCHRONOUS) 739 const_iv (ST_SYNCHRONOUS)
623 const_iv (ST_MANDLOCK , ST_MANDLOCK) 740 const_iv (ST_MANDLOCK)
624 const_iv (ST_WRITE , ST_WRITE) 741 const_iv (ST_WRITE)
625 const_iv (ST_APPEND , ST_APPEND) 742 const_iv (ST_APPEND)
626 const_iv (ST_IMMUTABLE , ST_IMMUTABLE) 743 const_iv (ST_IMMUTABLE)
627 const_iv (ST_NOATIME , ST_NOATIME) 744 const_iv (ST_NOATIME)
628 const_iv (ST_NODIRATIME , ST_NODIRATIME) 745 const_iv (ST_NODIRATIME)
629 const_iv (ST_RELATIME , ST_RELATIME) 746 const_iv (ST_RELATIME)
630 747
631 const_iv (MCL_FUTURE , MCL_FUTURE) 748 const_iv (PROT_EXEC)
749 const_iv (PROT_NONE)
750 const_iv (PROT_READ)
751 const_iv (PROT_WRITE)
752
753 /*const_iv (MAP_FIXED)*/
754 const_iv (MAP_PRIVATE)
755 const_iv (MAP_SHARED)
756 const_iv (MAP_ANONYMOUS)
757
758 /* linuxish */
759 const_iv (MAP_HUGETLB)
760 const_iv (MAP_LOCKED)
761 const_iv (MAP_NORESERVE)
762 const_iv (MAP_POPULATE)
763 const_iv (MAP_NONBLOCK)
764
765 const_iv (MCL_FUTURE)
632 const_iv (MCL_CURRENT , MCL_CURRENT) 766 const_iv (MCL_CURRENT)
633 767
634 const_eio (MS_ASYNC) 768 const_eio (MS_ASYNC)
635 const_eio (MS_INVALIDATE) 769 const_eio (MS_INVALIDATE)
636 const_eio (MS_SYNC) 770 const_eio (MS_SYNC)
637 771
661 aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1); 795 aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1);
662 aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1); 796 aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1);
663 797
664 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 798 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
665 newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv)); 799 newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv));
800
801 newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE));
666 802
667 create_respipe (); 803 create_respipe ();
668 804
669 if (eio_init (want_poll, done_poll) < 0) 805 if (eio_init (want_poll, done_poll) < 0)
670 croak ("IO::AIO: unable to initialise eio library"); 806 croak ("IO::AIO: unable to initialise eio library");
1257 1393
1258int 1394int
1259fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) 1395fadvise (aio_rfd fh, off_t offset, off_t length, IV advice)
1260 PROTOTYPE: $$$$ 1396 PROTOTYPE: $$$$
1261 CODE: 1397 CODE:
1262#if _XOPEN_SOURCE >= 600 && !NO_FADVISE
1263 RETVAL = posix_fadvise (fh, offset, length, advice); 1398 RETVAL = posix_fadvise (fh, offset, length, advice);
1264#else
1265 RETVAL = errno = ENOSYS;
1266#endif
1267 OUTPUT: 1399 OUTPUT:
1268 RETVAL 1400 RETVAL
1269 1401
1270ssize_t 1402ssize_t
1271sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) 1403sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count)
1272 PROTOTYPE: $$$$ 1404 PROTOTYPE: $$$$
1273 CODE: 1405 CODE:
1274 RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); 1406 RETVAL = eio_sendfile_sync (ofh, ifh, offset, count);
1407 OUTPUT:
1408 RETVAL
1409
1410void
1411mmap (SV *scalar, size_t length, int prot, int flags, SV *fh, off_t offset = 0)
1412 PROTOTYPE: $$$$$;$
1413 PPCODE:
1414 sv_unmagic (scalar, MMAP_MAGIC);
1415{
1416 int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1;
1417 void *addr = (void *)mmap (0, length, prot, flags, fd, offset);
1418 if (addr == (void *)-1)
1419 XSRETURN_NO;
1420
1421 sv_force_normal (scalar);
1422
1423 /* we store the length in mg_obj, as namlen is I32 :/ */
1424 sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0)
1425 ->mg_obj = (SV *)length;
1426
1427 SvUPGRADE (scalar, SVt_PV); /* nop... */
1428
1429 if (!(prot & PROT_WRITE))
1430 SvREADONLY_on (scalar);
1431
1432 if (SvLEN (scalar))
1433 Safefree (SvPVX (scalar));
1434
1435 SvPVX (scalar) = (char *)addr;
1436 SvCUR_set (scalar, length);
1437 SvLEN_set (scalar, 0);
1438 SvPOK_only (scalar);
1439
1440 XSRETURN_YES;
1441}
1442
1443void
1444munmap (SV *scalar)
1445 PROTOTYPE: $
1446 CODE:
1447 sv_unmagic (scalar, MMAP_MAGIC);
1448
1449int
1450madvise (SV *scalar, off_t offset, off_t length, IV advice)
1451 PROTOTYPE: $$$$
1452 CODE:
1453{
1454 char *addr = SvPV_nolen (scalar) + offset;
1455
1456 if (!SvOK (ST (2)))
1457 length = SvCUR (scalar) - offset;
1458
1459 if (addr >= SvEND (scalar) || length <= 0)
1460 XSRETURN_EMPTY;
1461
1462 RETVAL = posix_madvise (addr, length, advice);
1463}
1275 OUTPUT: 1464 OUTPUT:
1276 RETVAL 1465 RETVAL
1277 1466
1278int 1467int
1279mlockall (int flags) 1468mlockall (int flags)
1284 extern int mallopt (int, int); 1473 extern int mallopt (int, int);
1285 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ 1474 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1286#endif 1475#endif
1287 mlockall (flags); 1476 mlockall (flags);
1288#else 1477#else
1289 RETVAL -1; 1478 RETVAL = -1;
1290 errno = ENOSYS; 1479 errno = ENOSYS;
1291#endif 1480#endif
1292 OUTPUT: 1481 OUTPUT:
1293 RETVAL 1482 RETVAL
1294 1483
1297 PROTOTYPE: 1486 PROTOTYPE:
1298 CODE: 1487 CODE:
1299#if _POSIX_MEMLOCK 1488#if _POSIX_MEMLOCK
1300 munlockall (); 1489 munlockall ();
1301#else 1490#else
1302 RETVAL -1; 1491 RETVAL = -1;
1303 errno = ENOSYS; 1492 errno = ENOSYS;
1304#endif 1493#endif
1305 OUTPUT: 1494 OUTPUT:
1306 RETVAL 1495 RETVAL
1307 1496

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines