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.162 by root, Sun Jan 10 23:05:12 2010 UTC vs.
Revision 1.171 by root, Wed Aug 4 17:16:19 2010 UTC

18#include <fcntl.h> 18#include <fcntl.h>
19#include <sched.h> 19#include <sched.h>
20 20
21#if _POSIX_MEMLOCK || _POSIX_MAPPED_FILES 21#if _POSIX_MEMLOCK || _POSIX_MAPPED_FILES
22# include <sys/mman.h> 22# include <sys/mman.h>
23#endif
24
25#if !_POSIX_MAPPED_FILES
26# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1)
27# define munmap(addr,length) (errno = ENOSYS, -1)
28#endif 23#endif
29 24
30/* perl namespace pollution */ 25/* perl namespace pollution */
31#undef VERSION 26#undef VERSION
32 27
145#endif 140#endif
146#ifndef POSIX_FADV_DONTNEED 141#ifndef POSIX_FADV_DONTNEED
147# define POSIX_FADV_DONTNEED 0 142# define POSIX_FADV_DONTNEED 0
148#endif 143#endif
149 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
170#ifndef PROT_NONE
171# define PROT_NONE 0
172#endif
173#ifndef PROT_READ
174# define PROT_READ 0
175#endif
176#ifndef PROT_WRITE
177# define PROT_READ 0
178#endif
179#ifndef PROT_EXEC
180# define PROT_EXEC 0
181#endif
182
150#ifndef ST_NODEV 183#ifndef ST_NODEV
151# define ST_NODEV 0 184# define ST_NODEV 0
152#endif 185#endif
153#ifndef ST_NOEXEC 186#ifndef ST_NOEXEC
154# define ST_NOEXEC 0 187# define ST_NOEXEC 0
309 } 342 }
310} 343}
311 344
312static int req_invoke (eio_req *req) 345static int req_invoke (eio_req *req)
313{ 346{
314 dSP;
315
316 if (req->flags & FLAG_SV2_RO_OFF) 347 if (req->flags & FLAG_SV2_RO_OFF)
317 SvREADONLY_off (req->sv2); 348 SvREADONLY_off (req->sv2);
318 349
319 if (!EIO_CANCELLED (req) && req->callback) 350 if (!EIO_CANCELLED (req) && req->callback)
320 { 351 {
352 dSP;
353 static SV *sv_result_cache; /* caches the result integer SV */
354 SV *sv_result;
355
321 ENTER; 356 ENTER;
322 SAVETMPS; 357 SAVETMPS;
323 PUSHMARK (SP); 358 PUSHMARK (SP);
324 EXTEND (SP, 1); 359 EXTEND (SP, 1);
360
361 /* do not recreate the result IV from scratch each time */
362 if (expect_true (sv_result_cache))
363 {
364 sv_result = sv_result_cache; sv_result_cache = 0;
365 SvIV_set (sv_result, req->result);
366 }
367 else
368 {
369 sv_result = newSViv (req->result);
370 SvREADONLY_on (sv_result);
371 }
325 372
326 switch (req->type) 373 switch (req->type)
327 { 374 {
328 case EIO_READDIR: 375 case EIO_READDIR:
329 { 376 {
474 case EIO_LSTAT: 521 case EIO_LSTAT:
475 case EIO_FSTAT: 522 case EIO_FSTAT:
476 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; 523 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT;
477 PL_laststatval = req->result; 524 PL_laststatval = req->result;
478 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); 525 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2);
479 PUSHs (sv_2mortal (newSViv (req->result))); 526 PUSHs (sv_result);
480 break; 527 break;
481 528
482 case EIO_READ: 529 case EIO_READ:
483 { 530 {
484 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); 531 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0));
485 *SvEND (req->sv2) = 0; 532 *SvEND (req->sv2) = 0;
486 SvPOK_only (req->sv2); 533 SvPOK_only (req->sv2);
487 SvSETMAGIC (req->sv2); 534 SvSETMAGIC (req->sv2);
488 PUSHs (sv_2mortal (newSViv (req->result))); 535 PUSHs (sv_result);
489 } 536 }
490 break; 537 break;
491 538
492 case EIO_DUP2: 539 case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), su fudge result value */
493 if (req->result > 0) 540 if (req->result > 0)
494 req->result = 0; 541 SvIV_set (sv_result, 0);
495 /* FALLTHROUGH */ 542 /* FALLTHROUGH */
496 543
497 default: 544 default:
498 PUSHs (sv_2mortal (newSViv (req->result))); 545 PUSHs (sv_result);
499 break; 546 break;
500 } 547 }
501 548
502 errno = req->errorno; 549 errno = req->errorno;
503 550
504 PUTBACK; 551 PUTBACK;
505 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); 552 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD);
506 SPAGAIN; 553 SPAGAIN;
554
555 if (expect_false (SvREFCNT (sv_result) != 1 || sv_result_cache))
556 SvREFCNT_dec (sv_result);
557 else
558 sv_result_cache = sv_result;
507 559
508 FREETMPS; 560 FREETMPS;
509 LEAVE; 561 LEAVE;
510 562
511 PUTBACK; 563 PUTBACK;
589 create_respipe (); 641 create_respipe ();
590} 642}
591 643
592/*****************************************************************************/ 644/*****************************************************************************/
593 645
646#if !_POSIX_MAPPED_FILES
647# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1)
648# define munmap(addr,length) (errno = ENOSYS, -1)
649# define mprotect(addr,len,prot) (errno = ENOSYS, -1)
650#endif
651
594#define MMAP_MAGIC PERL_MAGIC_ext 652#define MMAP_MAGIC PERL_MAGIC_ext
595 653
596static int 654static int
597mmap_free (pTHX_ SV *sv, MAGIC *mg) 655mmap_free (pTHX_ SV *sv, MAGIC *mg)
598{ 656{
601 errno = old_errno; 659 errno = old_errno;
602 660
603 mg->mg_obj = 0; /* just in case */ 661 mg->mg_obj = 0; /* just in case */
604 662
605 SvREADONLY_off (sv); 663 SvREADONLY_off (sv);
664
665 if (SvPVX (sv) != mg->mg_ptr)
666 croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected");
667
606 SvCUR_set (sv, 0); 668 SvCUR_set (sv, 0);
607 SvLEN_set (sv, 0);
608 SvPVX (sv) = 0; 669 SvPVX (sv) = 0;
609 SvOK_off (sv); 670 SvOK_off (sv);
610 671
611 return 0; 672 return 0;
612} 673}
662# define const_eio(name) { # name, (IV) EIO_ ## name }, 723# define const_eio(name) { # name, (IV) EIO_ ## name },
663 const_iv (EXDEV) 724 const_iv (EXDEV)
664 const_iv (ENOSYS) 725 const_iv (ENOSYS)
665 const_iv (O_RDONLY) 726 const_iv (O_RDONLY)
666 const_iv (O_WRONLY) 727 const_iv (O_WRONLY)
728 const_iv (O_RDWR)
667 const_iv (O_CREAT) 729 const_iv (O_CREAT)
668 const_iv (O_TRUNC) 730 const_iv (O_TRUNC)
731 const_iv (O_EXCL)
732 const_iv (O_APPEND)
669#ifndef _WIN32 733#ifndef _WIN32
670 const_iv (S_IFIFO) 734 const_iv (S_IFIFO)
671#endif 735#endif
672 const_niv (FADV_NORMAL , POSIX_FADV_NORMAL) 736 const_niv (FADV_NORMAL , POSIX_FADV_NORMAL)
673 const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) 737 const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL)
674 const_niv (FADV_RANDOM , POSIX_FADV_RANDOM) 738 const_niv (FADV_RANDOM , POSIX_FADV_RANDOM)
675 const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE) 739 const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE)
676 const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED) 740 const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED)
677 const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED) 741 const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED)
742
743 const_niv (MADV_NORMAL , POSIX_MADV_NORMAL)
744 const_niv (MADV_SEQUENTIAL, POSIX_MADV_SEQUENTIAL)
745 const_niv (MADV_RANDOM , POSIX_MADV_RANDOM)
746 const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED)
747 const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED)
678 748
679 const_iv (ST_RDONLY) 749 const_iv (ST_RDONLY)
680 const_iv (ST_NOSUID) 750 const_iv (ST_NOSUID)
681 const_iv (ST_NODEV) 751 const_iv (ST_NODEV)
682 const_iv (ST_NOEXEC) 752 const_iv (ST_NOEXEC)
687 const_iv (ST_IMMUTABLE) 757 const_iv (ST_IMMUTABLE)
688 const_iv (ST_NOATIME) 758 const_iv (ST_NOATIME)
689 const_iv (ST_NODIRATIME) 759 const_iv (ST_NODIRATIME)
690 const_iv (ST_RELATIME) 760 const_iv (ST_RELATIME)
691 761
762 const_iv (PROT_NONE)
692 const_iv (PROT_EXEC) 763 const_iv (PROT_EXEC)
693 const_iv (PROT_NONE)
694 const_iv (PROT_READ) 764 const_iv (PROT_READ)
695 const_iv (PROT_WRITE) 765 const_iv (PROT_WRITE)
696 766
697 /*const_iv (MAP_FIXED)*/ 767 /*const_iv (MAP_FIXED)*/
698 const_iv (MAP_PRIVATE) 768 const_iv (MAP_PRIVATE)
1337 1407
1338int 1408int
1339fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) 1409fadvise (aio_rfd fh, off_t offset, off_t length, IV advice)
1340 PROTOTYPE: $$$$ 1410 PROTOTYPE: $$$$
1341 CODE: 1411 CODE:
1342#if _XOPEN_SOURCE >= 600 && !NO_FADVISE
1343 RETVAL = posix_fadvise (fh, offset, length, advice); 1412 RETVAL = posix_fadvise (fh, offset, length, advice);
1344#else
1345 RETVAL = errno = ENOSYS; /* yes, this is actually correct */
1346#endif
1347 OUTPUT: 1413 OUTPUT:
1348 RETVAL 1414 RETVAL
1349 1415
1350ssize_t 1416ssize_t
1351sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) 1417sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count)
1364 int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; 1430 int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1;
1365 void *addr = (void *)mmap (0, length, prot, flags, fd, offset); 1431 void *addr = (void *)mmap (0, length, prot, flags, fd, offset);
1366 if (addr == (void *)-1) 1432 if (addr == (void *)-1)
1367 XSRETURN_NO; 1433 XSRETURN_NO;
1368 1434
1435 sv_force_normal (scalar);
1436
1369 /* we store the length in mg_obj, as namlen is I32 :/ */ 1437 /* we store the length in mg_obj, as namlen is I32 :/ */
1370 sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0) 1438 sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0)
1371 ->mg_obj = (SV *)length; 1439 ->mg_obj = (SV *)length;
1372 1440
1373 SvUPGRADE (scalar, SVt_PV); /* nop... */ 1441 SvUPGRADE (scalar, SVt_PV); /* nop... */
1442
1374 if (!(prot & PROT_WRITE)) 1443 if (!(prot & PROT_WRITE))
1375 SvREADONLY_on (scalar); 1444 SvREADONLY_on (scalar);
1445
1446 if (SvLEN (scalar))
1447 Safefree (SvPVX (scalar));
1376 1448
1377 SvPVX (scalar) = (char *)addr; 1449 SvPVX (scalar) = (char *)addr;
1378 SvCUR_set (scalar, length); 1450 SvCUR_set (scalar, length);
1379 SvLEN_set (scalar, 0); 1451 SvLEN_set (scalar, 0);
1380 SvPOK_only (scalar); 1452 SvPOK_only (scalar);
1385void 1457void
1386munmap (SV *scalar) 1458munmap (SV *scalar)
1387 PROTOTYPE: $ 1459 PROTOTYPE: $
1388 CODE: 1460 CODE:
1389 sv_unmagic (scalar, MMAP_MAGIC); 1461 sv_unmagic (scalar, MMAP_MAGIC);
1462
1463int
1464madvise (SV *scalar, off_t offset, off_t length, IV advice_or_prot)
1465 ALIAS:
1466 mprotect = 1
1467 PROTOTYPE: $$$$
1468 CODE:
1469{
1470 STRLEN cur;
1471 char *addr = SvPVbyte (scalar, cur);
1472
1473 if (offset > cur)
1474 RETVAL = errno = EFAULT;
1475 else
1476 {
1477 if (!SvOK (ST (2)))
1478 length = cur - offset;
1479
1480 if (offset + length > cur)
1481 RETVAL = errno = EFAULT;
1482 else
1483 switch (ix)
1484 {
1485 case 0: RETVAL = posix_madvise (addr + offset, length, advice_or_prot); break;
1486 case 1: RETVAL = mprotect (addr + offset, length, advice_or_prot); break;
1487 }
1488 }
1489}
1490 OUTPUT:
1491 RETVAL
1390 1492
1391int 1493int
1392mlockall (int flags) 1494mlockall (int flags)
1393 PROTOTYPE: $ 1495 PROTOTYPE: $
1394 CODE: 1496 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines