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.165 by root, Sun May 9 20:42:55 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
178#endif 178#endif
179#ifndef MCL_FUTURE 179#ifndef MCL_FUTURE
180# define MCL_FUTURE 0 180# define MCL_FUTURE 0
181#endif 181#endif
182 182
183#ifndef MAP_ANONYMOUS
184# ifdef MAP_ANON
185# define MAP_ANONYMOUS MAP_ANON
186# else
187# define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */
188# endif
189#endif
190#ifndef MAP_HUGETLB
191# define MAP_HUGETLB 0
192#endif
193#ifndef MAP_LOCKED
194# define MAP_LOCKED 0
195#endif
196#ifndef MAP_NORESERVE
197# define MAP_NORESERVE 0
198#endif
199#ifndef MAP_POPULATE
200# define MAP_POPULATE 0
201#endif
202#ifndef MAP_NONBLOCK
203# define MAP_NONBLOCK 0
204#endif
205
206#ifndef PAGESIZE
207# define PAGESIZE sysconf (_SC_PAGESIZE)
208#endif
209
183static int req_invoke (eio_req *req); 210static int req_invoke (eio_req *req);
184#define EIO_FINISH(req) req_invoke (req) 211#define EIO_FINISH(req) req_invoke (req)
185static void req_destroy (eio_req *grp); 212static void req_destroy (eio_req *grp);
186#define EIO_DESTROY(req) req_destroy (req) 213#define EIO_DESTROY(req) req_destroy (req)
187 214
284 if (req->flags & FLAG_SV2_RO_OFF) 311 if (req->flags & FLAG_SV2_RO_OFF)
285 SvREADONLY_off (req->sv2); 312 SvREADONLY_off (req->sv2);
286 313
287 if (!EIO_CANCELLED (req) && req->callback) 314 if (!EIO_CANCELLED (req) && req->callback)
288 { 315 {
316 static SV *sv_result_cache; /* caches the result integer SV */
317 SV *sv_result;
318
289 ENTER; 319 ENTER;
290 SAVETMPS; 320 SAVETMPS;
291 PUSHMARK (SP); 321 PUSHMARK (SP);
292 EXTEND (SP, 1); 322 EXTEND (SP, 1);
323
324 /* do not recreate the result IV from scratch each time */
325 if (expect_true (sv_result_cache))
326 {
327 sv_result = sv_result_cache; sv_result_cache = 0;
328 SvIV_set (sv_result, req->result);
329 }
330 else
331 {
332 sv_result = newSViv (req->result);
333 SvREADONLY_on (sv_result);
334 }
293 335
294 switch (req->type) 336 switch (req->type)
295 { 337 {
296 case EIO_READDIR: 338 case EIO_READDIR:
297 { 339 {
442 case EIO_LSTAT: 484 case EIO_LSTAT:
443 case EIO_FSTAT: 485 case EIO_FSTAT:
444 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; 486 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT;
445 PL_laststatval = req->result; 487 PL_laststatval = req->result;
446 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); 488 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2);
447 PUSHs (sv_2mortal (newSViv (req->result))); 489 PUSHs (sv_result);
448 break; 490 break;
449 491
450 case EIO_READ: 492 case EIO_READ:
451 { 493 {
452 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); 494 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0));
453 *SvEND (req->sv2) = 0; 495 *SvEND (req->sv2) = 0;
454 SvPOK_only (req->sv2); 496 SvPOK_only (req->sv2);
455 SvSETMAGIC (req->sv2); 497 SvSETMAGIC (req->sv2);
456 PUSHs (sv_2mortal (newSViv (req->result))); 498 PUSHs (sv_result);
457 } 499 }
458 break; 500 break;
459 501
460 case EIO_DUP2: 502 case EIO_DUP2:
461 if (req->result > 0) 503 if (req->result > 0)
462 req->result = 0; 504 SvIV_set (sv_result, 0);
463 /* FALLTHROUGH */ 505 /* FALLTHROUGH */
464 506
465 default: 507 default:
466 PUSHs (sv_2mortal (newSViv (req->result))); 508 PUSHs (sv_result);
467 break; 509 break;
468 } 510 }
469 511
470 errno = req->errorno; 512 errno = req->errorno;
471 513
472 PUTBACK; 514 PUTBACK;
473 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); 515 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD);
474 SPAGAIN; 516 SPAGAIN;
517
518 if (expect_false (SvREFCNT (sv_result) != 1 || sv_result_cache))
519 SvREFCNT_dec (sv_result);
520 else
521 sv_result_cache = sv_result;
475 522
476 FREETMPS; 523 FREETMPS;
477 LEAVE; 524 LEAVE;
478 525
479 PUTBACK; 526 PUTBACK;
554 601
555static void atfork_child (void) 602static void atfork_child (void)
556{ 603{
557 create_respipe (); 604 create_respipe ();
558} 605}
606
607/*****************************************************************************/
608
609#if !_POSIX_MAPPED_FILES
610# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1)
611# define munmap(addr,length) (errno = ENOSYS, -1)
612#endif
613
614#define MMAP_MAGIC PERL_MAGIC_ext
615
616static int
617mmap_free (pTHX_ SV *sv, MAGIC *mg)
618{
619 int old_errno = errno;
620 munmap (mg->mg_ptr, (size_t)mg->mg_obj);
621 errno = old_errno;
622
623 mg->mg_obj = 0; /* just in case */
624
625 SvREADONLY_off (sv);
626 SvCUR_set (sv, 0);
627 SvLEN_set (sv, 0);
628 SvPVX (sv) = 0;
629 SvOK_off (sv);
630
631 return 0;
632}
633
634static MGVTBL mmap_vtbl = {
635 0, 0, 0, 0, mmap_free
636};
637
638/*****************************************************************************/
559 639
560static SV * 640static SV *
561get_cb (SV *cb_sv) 641get_cb (SV *cb_sv)
562{ 642{
563 SvGETMAGIC (cb_sv); 643 SvGETMAGIC (cb_sv);
595{ 675{
596 static const struct { 676 static const struct {
597 const char *name; 677 const char *name;
598 IV iv; 678 IV iv;
599 } *civ, const_iv[] = { 679 } *civ, const_iv[] = {
600# define const_iv(name, value) { # name, (IV) value }, 680# define const_niv(name, value) { # name, (IV) value },
681# define const_iv(name) { # name, (IV) name },
601# define const_eio(name) { # name, (IV) EIO_ ## name }, 682# define const_eio(name) { # name, (IV) EIO_ ## name },
602 const_iv (EXDEV , EXDEV) 683 const_iv (EXDEV)
603 const_iv (ENOSYS , ENOSYS) 684 const_iv (ENOSYS)
604 const_iv (O_RDONLY, O_RDONLY) 685 const_iv (O_RDONLY)
605 const_iv (O_WRONLY, O_WRONLY) 686 const_iv (O_WRONLY)
687 const_iv (O_RDWR)
606 const_iv (O_CREAT , O_CREAT) 688 const_iv (O_CREAT)
607 const_iv (O_TRUNC , O_TRUNC) 689 const_iv (O_TRUNC)
690 const_iv (O_EXCL)
691 const_iv (O_APPEND)
608#ifndef _WIN32 692#ifndef _WIN32
609 const_iv (S_IFIFO , S_IFIFO) 693 const_iv (S_IFIFO)
610#endif 694#endif
611 const_iv (FADV_NORMAL , POSIX_FADV_NORMAL) 695 const_niv (FADV_NORMAL , POSIX_FADV_NORMAL)
612 const_iv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) 696 const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL)
613 const_iv (FADV_RANDOM , POSIX_FADV_RANDOM) 697 const_niv (FADV_RANDOM , POSIX_FADV_RANDOM)
614 const_iv (FADV_NOREUSE , POSIX_FADV_NOREUSE) 698 const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE)
615 const_iv (FADV_WILLNEED , POSIX_FADV_WILLNEED) 699 const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED)
616 const_iv (FADV_DONTNEED , POSIX_FADV_DONTNEED) 700 const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED)
617 701
618 const_iv (ST_RDONLY , ST_RDONLY) 702 const_iv (ST_RDONLY)
619 const_iv (ST_NOSUID , ST_NOSUID) 703 const_iv (ST_NOSUID)
620 const_iv (ST_NODEV , ST_NODEV) 704 const_iv (ST_NODEV)
621 const_iv (ST_NOEXEC , ST_NOEXEC) 705 const_iv (ST_NOEXEC)
622 const_iv (ST_SYNCHRONOUS , ST_SYNCHRONOUS) 706 const_iv (ST_SYNCHRONOUS)
623 const_iv (ST_MANDLOCK , ST_MANDLOCK) 707 const_iv (ST_MANDLOCK)
624 const_iv (ST_WRITE , ST_WRITE) 708 const_iv (ST_WRITE)
625 const_iv (ST_APPEND , ST_APPEND) 709 const_iv (ST_APPEND)
626 const_iv (ST_IMMUTABLE , ST_IMMUTABLE) 710 const_iv (ST_IMMUTABLE)
627 const_iv (ST_NOATIME , ST_NOATIME) 711 const_iv (ST_NOATIME)
628 const_iv (ST_NODIRATIME , ST_NODIRATIME) 712 const_iv (ST_NODIRATIME)
629 const_iv (ST_RELATIME , ST_RELATIME) 713 const_iv (ST_RELATIME)
630 714
631 const_iv (MCL_FUTURE , MCL_FUTURE) 715 const_iv (PROT_EXEC)
716 const_iv (PROT_NONE)
717 const_iv (PROT_READ)
718 const_iv (PROT_WRITE)
719
720 /*const_iv (MAP_FIXED)*/
721 const_iv (MAP_PRIVATE)
722 const_iv (MAP_SHARED)
723 const_iv (MAP_ANONYMOUS)
724
725 /* linuxish */
726 const_iv (MAP_HUGETLB)
727 const_iv (MAP_LOCKED)
728 const_iv (MAP_NORESERVE)
729 const_iv (MAP_POPULATE)
730 const_iv (MAP_NONBLOCK)
731
732 const_iv (MCL_FUTURE)
632 const_iv (MCL_CURRENT , MCL_CURRENT) 733 const_iv (MCL_CURRENT)
633 734
634 const_eio (MS_ASYNC) 735 const_eio (MS_ASYNC)
635 const_eio (MS_INVALIDATE) 736 const_eio (MS_INVALIDATE)
636 const_eio (MS_SYNC) 737 const_eio (MS_SYNC)
637 738
661 aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1); 762 aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1);
662 aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1); 763 aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1);
663 764
664 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 765 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
665 newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv)); 766 newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv));
767
768 newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE));
666 769
667 create_respipe (); 770 create_respipe ();
668 771
669 if (eio_init (want_poll, done_poll) < 0) 772 if (eio_init (want_poll, done_poll) < 0)
670 croak ("IO::AIO: unable to initialise eio library"); 773 croak ("IO::AIO: unable to initialise eio library");
1260 PROTOTYPE: $$$$ 1363 PROTOTYPE: $$$$
1261 CODE: 1364 CODE:
1262#if _XOPEN_SOURCE >= 600 && !NO_FADVISE 1365#if _XOPEN_SOURCE >= 600 && !NO_FADVISE
1263 RETVAL = posix_fadvise (fh, offset, length, advice); 1366 RETVAL = posix_fadvise (fh, offset, length, advice);
1264#else 1367#else
1265 RETVAL = errno = ENOSYS; 1368 RETVAL = errno = ENOSYS; /* yes, this is actually correct */
1266#endif 1369#endif
1267 OUTPUT: 1370 OUTPUT:
1268 RETVAL 1371 RETVAL
1269 1372
1270ssize_t 1373ssize_t
1272 PROTOTYPE: $$$$ 1375 PROTOTYPE: $$$$
1273 CODE: 1376 CODE:
1274 RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); 1377 RETVAL = eio_sendfile_sync (ofh, ifh, offset, count);
1275 OUTPUT: 1378 OUTPUT:
1276 RETVAL 1379 RETVAL
1380
1381void
1382mmap (SV *scalar, size_t length, int prot, int flags, SV *fh, off_t offset = 0)
1383 PROTOTYPE: $$$$$;$
1384 PPCODE:
1385 sv_unmagic (scalar, MMAP_MAGIC);
1386{
1387 int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1;
1388 void *addr = (void *)mmap (0, length, prot, flags, fd, offset);
1389 if (addr == (void *)-1)
1390 XSRETURN_NO;
1391
1392 /* we store the length in mg_obj, as namlen is I32 :/ */
1393 sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0)
1394 ->mg_obj = (SV *)length;
1395
1396 SvUPGRADE (scalar, SVt_PV); /* nop... */
1397 if (!(prot & PROT_WRITE))
1398 SvREADONLY_on (scalar);
1399
1400 SvPVX (scalar) = (char *)addr;
1401 SvCUR_set (scalar, length);
1402 SvLEN_set (scalar, 0);
1403 SvPOK_only (scalar);
1404
1405 XSRETURN_YES;
1406}
1407
1408void
1409munmap (SV *scalar)
1410 PROTOTYPE: $
1411 CODE:
1412 sv_unmagic (scalar, MMAP_MAGIC);
1277 1413
1278int 1414int
1279mlockall (int flags) 1415mlockall (int flags)
1280 PROTOTYPE: $ 1416 PROTOTYPE: $
1281 CODE: 1417 CODE:
1284 extern int mallopt (int, int); 1420 extern int mallopt (int, int);
1285 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ 1421 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1286#endif 1422#endif
1287 mlockall (flags); 1423 mlockall (flags);
1288#else 1424#else
1289 RETVAL -1; 1425 RETVAL = -1;
1290 errno = ENOSYS; 1426 errno = ENOSYS;
1291#endif 1427#endif
1292 OUTPUT: 1428 OUTPUT:
1293 RETVAL 1429 RETVAL
1294 1430
1297 PROTOTYPE: 1433 PROTOTYPE:
1298 CODE: 1434 CODE:
1299#if _POSIX_MEMLOCK 1435#if _POSIX_MEMLOCK
1300 munlockall (); 1436 munlockall ();
1301#else 1437#else
1302 RETVAL -1; 1438 RETVAL = -1;
1303 errno = ENOSYS; 1439 errno = ENOSYS;
1304#endif 1440#endif
1305 OUTPUT: 1441 OUTPUT:
1306 RETVAL 1442 RETVAL
1307 1443

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines