--- IO-AIO/AIO.xs 2008/05/11 00:01:03 1.118 +++ IO-AIO/AIO.xs 2009/06/17 01:14:48 1.148 @@ -67,33 +67,82 @@ #endif +/* perl stupidly overrides readdir and maybe others */ +/* with thread-unsafe versions, imagine that :( */ +#undef readdir +#undef opendir +#undef closedir + #define EIO_STRUCT_STAT Stat_t /* use NV for 32 bit perls as it allows larger offsets */ #if IVSIZE >= 8 +# define VAL64 IV # define SvVAL64 SvIV +# define newSVval64 newSViv #else +# define VAL64 NV # define SvVAL64 SvNV +# define newSVval64 newSVnv #endif +/*****************************************************************************/ + +#if __GNUC__ >= 3 +# define expect(expr,value) __builtin_expect ((expr),(value)) +#else +# define expect(expr,value) (expr) +#endif + +#define expect_false(expr) expect ((expr) != 0, 0) +#define expect_true(expr) expect ((expr) != 0, 1) + +/*****************************************************************************/ + static HV *stash; typedef SV SV8; /* byte-sv, used for argument-checking */ +typedef int aio_rfd; /* read file desriptor */ +typedef int aio_wfd; /* write file descriptor */ #define AIO_REQ_KLASS "IO::AIO::REQ" #define AIO_GRP_KLASS "IO::AIO::GRP" -#define EIO_COMMON \ +#define EIO_REQ_MEMBERS \ SV *callback; \ SV *sv1, *sv2; \ STRLEN stroffset; \ - SV *self + SV *self; + +#define EIO_NO_WRAPPERS 1 #include "libeio/eio.h" +#ifndef POSIX_FADV_NORMAL +# define POSIX_FADV_NORMAL 0 +#endif + +#ifndef POSIX_FADV_SEQUENTIAL +# define POSIX_FADV_SEQUENTIAL 0 +#endif + +#ifndef POSIX_FADV_RANDOM +# define POSIX_FADV_RANDOM 0 +#endif + +#ifndef POSIX_FADV_NOREUSE +# define POSIX_FADV_NOREUSE 0 +#endif + +#ifndef POSIX_FADV_WILLNEED +# define POSIX_FADV_WILLNEED 0 +#endif + +#ifndef POSIX_FADV_DONTNEED +# define POSIX_FADV_DONTNEED 0 +#endif + static int req_invoke (eio_req *req); #define EIO_FINISH(req) req_invoke (req) -static void aio_grp_feed (eio_req *grp); -#define EIO_FEED(req) aio_grp_feed (req) static void req_destroy (eio_req *grp); #define EIO_DESTROY(req) req_destroy (req) @@ -107,7 +156,7 @@ typedef eio_req *aio_req_ornot; static SV *on_next_submit; -static int next_pri = EIO_DEFAULT_PRI + EIO_PRI_BIAS; +static int next_pri = EIO_PRI_DEFAULT; static int max_outstanding; static int respipe_osf [2], respipe [2] = { -1, -1 }; @@ -175,7 +224,7 @@ { eio_submit (req); - if (on_next_submit) + if (expect_false (on_next_submit)) { dSP; SV *cb = sv_2mortal (on_next_submit); @@ -195,7 +244,7 @@ if (req->flags & FLAG_SV2_RO_OFF) SvREADONLY_off (req->sv2); - if (!EIO_CANCELLED (req) && SvOK (req->callback)) + if (!EIO_CANCELLED (req) && req->callback) { ENTER; SAVETMPS; @@ -211,23 +260,57 @@ if (req->result >= 0) { int i; - char *buf = req->ptr2; + char *names = (char *)req->ptr2; + eio_dirent *ent = (eio_dirent *)req->ptr1; /* might be 0 */ AV *av = newAV (); av_extend (av, req->result - 1); for (i = 0; i < req->result; ++i) { - SV *sv = newSVpv (buf, 0); - - av_store (av, i, sv); - buf += SvCUR (sv) + 1; + if (req->int1 & EIO_READDIR_DENTS) + { + SV *namesv = newSVpvn (names + ent->nameofs, ent->namelen); + + if (req->int1 & EIO_READDIR_CUSTOM2) + { + static SV *sv_type [EIO_DT_MAX + 1]; /* type sv cache */ + AV *avent = newAV (); + + av_extend (avent, 2); + + if (!sv_type [ent->type]) + { + sv_type [ent->type] = newSViv (ent->type); + SvREADONLY_on (sv_type [ent->type]); + } + + av_store (avent, 0, namesv); + av_store (avent, 1, SvREFCNT_inc (sv_type [ent->type])); + av_store (avent, 2, IVSIZE >= 8 ? newSVuv (ent->inode) : newSVnv (ent->inode)); + + av_store (av, i, newRV_noinc ((SV *)avent)); + } + else + av_store (av, i, namesv); + + ++ent; + } + else + { + SV *name = newSVpv (names, 0); + av_store (av, i, name); + names += SvCUR (name) + 1; + } } rv = sv_2mortal (newRV_noinc ((SV *)av)); } PUSHs (rv); + + if (req->int1 & EIO_READDIR_CUSTOM1) + XPUSHs (sv_2mortal (newSViv (req->int1 & ~(EIO_READDIR_CUSTOM1 | EIO_READDIR_CUSTOM2)))); } break; @@ -243,7 +326,7 @@ char sym [64]; int symlen; - symlen = snprintf (sym, sizeof (sym), "fd#%d", req->result); + symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); gv_init (gv, stash, sym, symlen, 0); symlen = snprintf ( @@ -251,7 +334,7 @@ sizeof (sym), "%s&=%d", flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<", - req->result + (int)req->result ); if (do_open (gv, sym, symlen, 0, 0, 0, 0)) @@ -282,11 +365,7 @@ case EIO_READLINK: if (req->result > 0) - { - SvCUR_set (req->sv2, req->result); - *SvEND (req->sv2) = 0; - PUSHs (req->sv2); - } + PUSHs (sv_2mortal (newSVpvn (req->ptr2, req->result))); break; case EIO_STAT: @@ -299,9 +378,13 @@ break; case EIO_READ: - SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); - *SvEND (req->sv2) = 0; - PUSHs (sv_2mortal (newSViv (req->result))); + { + SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); + *SvEND (req->sv2) = 0; + SvPOK_only (req->sv2); + SvSETMAGIC (req->sv2); + PUSHs (sv_2mortal (newSViv (req->result))); + } break; case EIO_DUP2: @@ -416,7 +499,7 @@ if (size) return; - maybe_start_thread (); + etp_maybe_start_thread (); FD_ZERO (&rfd); FD_SET (respipe [0], &rfd); @@ -427,18 +510,18 @@ static int poll_cb (void) { - int res; - - do + for (;;) { - res = eio_poll (); + int res = eio_poll (); if (res > 0) croak (0); - } - while (max_outstanding && max_outstanding <= eio_nreqs ()); - return res; + if (!max_outstanding || max_outstanding > eio_nreqs ()) + return res; + + poll_wait (); + } } static void atfork_child (void) @@ -446,60 +529,118 @@ create_respipe (); } +static SV * +get_cb (SV *cb_sv) +{ + HV *st; + GV *gvp; + CV *cv; + + if (!SvOK (cb_sv)) + return 0; + + cv = sv_2cv (cb_sv, &st, &gvp, 0); + + if (!cv) + croak ("IO::AIO callback must be undef or a CODE reference"); + + return (SV *)cv; +} + #define dREQ \ + SV *cb_cv; \ aio_req req; \ int req_pri = next_pri; \ - next_pri = EIO_DEFAULT_PRI + EIO_PRI_BIAS; \ + next_pri = EIO_PRI_DEFAULT; \ \ - if (SvOK (callback) && !SvROK (callback)) \ - croak ("callback must be undef or of reference type"); \ + cb_cv = get_cb (callback); \ \ Newz (0, req, 1, eio_req); \ if (!req) \ croak ("out of memory during eio_req allocation"); \ \ - req->callback = newSVsv (callback); \ + req->callback = SvREFCNT_inc (cb_cv); \ req->pri = req_pri #define REQ_SEND \ + PUTBACK; \ req_submit (req); \ + SPAGAIN; \ \ if (GIMME_V != G_VOID) \ XPUSHs (req_sv (req, AIO_REQ_KLASS)); - + +static int +extract_fd (SV *fh, int wr) +{ + int fd = PerlIO_fileno (wr ? IoOFP (sv_2io (fh)) : IoIFP (sv_2io (fh))); + + if (fd < 0) + croak ("illegal fh argument, either not an OS file or read/write mode mismatch"); + + return fd; +} + MODULE = IO::AIO PACKAGE = IO::AIO PROTOTYPES: ENABLE BOOT: { - stash = gv_stashpv ("IO::AIO", 1); + static const struct { + const char *name; + IV iv; + } *civ, const_iv[] = { +# define const_iv(name, value) { # name, (IV) value }, +# define const_eio(name) { # name, (IV) EIO_ ## name }, + const_iv (EXDEV , EXDEV) + const_iv (ENOSYS , ENOSYS) + const_iv (O_RDONLY, O_RDONLY) + const_iv (O_WRONLY, O_WRONLY) + const_iv (O_CREAT , O_CREAT) + const_iv (O_TRUNC , O_TRUNC) +#ifndef _WIN32 + const_iv (S_IFIFO , S_IFIFO) +#endif + const_iv (FADV_NORMAL , POSIX_FADV_NORMAL) + const_iv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) + const_iv (FADV_RANDOM , POSIX_FADV_RANDOM) + const_iv (FADV_NOREUSE , POSIX_FADV_NOREUSE) + const_iv (FADV_WILLNEED , POSIX_FADV_WILLNEED) + const_iv (FADV_DONTNEED , POSIX_FADV_DONTNEED) + + const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) + const_eio (SYNC_FILE_RANGE_WRITE) + const_eio (SYNC_FILE_RANGE_WAIT_AFTER) + + const_eio (READDIR_DENTS) + const_eio (READDIR_DIRS_FIRST) + const_eio (READDIR_STAT_ORDER) + const_eio (READDIR_FOUND_UNKNOWN) + + const_eio (DT_UNKNOWN) + const_eio (DT_FIFO) + const_eio (DT_CHR) + const_eio (DT_DIR) + const_eio (DT_BLK) + const_eio (DT_REG) + const_eio (DT_LNK) + const_eio (DT_SOCK) + const_eio (DT_WHT) + }; - newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); - newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); - newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); - newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); - newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); -#ifdef _WIN32 - X_MUTEX_CHECK (wrklock); - X_MUTEX_CHECK (reslock); - X_MUTEX_CHECK (reqlock); - X_MUTEX_CHECK (reqwait); - X_MUTEX_CHECK (preadwritelock); - X_MUTEX_CHECK (readdirlock); + stash = gv_stashpv ("IO::AIO", 1); - X_COND_CHECK (reqwait); -#else - newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); -#endif + for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) + newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); - create_respipe (); + create_respipe (); - if (eio_init (want_poll, done_poll) < 0) - croak ("IO::AIO: unable to initialise eio library"); + if (eio_init (want_poll, done_poll) < 0) + croak ("IO::AIO: unable to initialise eio library"); - /* atfork child called in fifo order, so before eio's handler */ - X_THREAD_ATFORK (0, 0, atfork_child); + /* atfork child called in fifo order, so before eio's handler */ + X_THREAD_ATFORK (0, 0, atfork_child); } void @@ -562,11 +703,30 @@ aio_fdatasync = EIO_FDATASYNC PPCODE: { + int fd = extract_fd (fh, 0); dREQ; req->type = ix; req->sv1 = newSVsv (fh); - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); + req->int1 = fd; + + REQ_SEND (req); +} + +void +aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) + PROTOTYPE: $$$$;$ + PPCODE: +{ + int fd = extract_fd (fh, 0); + dREQ; + + req->type = EIO_SYNC_FILE_RANGE; + req->sv1 = newSVsv (fh); + req->int1 = fd; + req->offs = offset; + req->size = nbytes; + req->int2 = flags; REQ_SEND (req); } @@ -577,6 +737,7 @@ PPCODE: { static int close_pipe = -1; /* dummy fd to close fds via dup2 */ + int fd = extract_fd (fh, 0); dREQ; if (close_pipe < 0) @@ -594,7 +755,7 @@ req->type = EIO_DUP2; req->int1 = close_pipe; req->sv2 = newSVsv (fh); - req->int2 = PerlIO_fileno (IoIFP (sv_2io (fh))); + req->int2 = fd; REQ_SEND (req); } @@ -608,12 +769,10 @@ PPCODE: { STRLEN svlen; + int fd = extract_fd (fh, ix == EIO_WRITE); char *svptr = SvPVbyte (data, svlen); UV len = SvUV (length); - SvUPGRADE (data, SVt_PV); - SvPOK_on (data); - if (dataoffset < 0) dataoffset += svlen; @@ -628,20 +787,17 @@ } else { - /* read: grow scalar as necessary */ + /* read: check type and grow scalar as necessary */ + SvUPGRADE (data, SVt_PV); svptr = SvGROW (data, len + dataoffset + 1); } - if (len < 0) - croak ("length must not be negative"); - { dREQ; req->type = ix; req->sv1 = newSVsv (fh); - req->int1 = PerlIO_fileno (ix == EIO_READ ? IoIFP (sv_2io (fh)) - : IoOFP (sv_2io (fh))); + req->int1 = fd; req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; req->size = len; req->sv2 = SvREFCNT_inc (data); @@ -666,47 +822,45 @@ SV *data; dREQ; - data = newSV (NAME_MAX); - SvPOK_on (data); - req->type = EIO_READLINK; req->sv1 = newSVsv (path); req->ptr1 = SvPVbyte_nolen (req->sv1); - req->sv2 = data; - req->ptr2 = SvPVbyte_nolen (data); REQ_SEND; } void -aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef) +aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) PROTOTYPE: $$$$;$ PPCODE: { + int ifd = extract_fd (in_fh , 0); + int ofd = extract_fd (out_fh, 0); dREQ; req->type = EIO_SENDFILE; req->sv1 = newSVsv (out_fh); - req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); + req->int1 = ofd; req->sv2 = newSVsv (in_fh); - req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); - req->offs = SvVAL64 (in_offset); + req->int2 = ifd; + req->offs = in_offset; req->size = length; REQ_SEND; } void -aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef) +aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) PROTOTYPE: $$$;$ PPCODE: { + int fd = extract_fd (fh, 0); dREQ; req->type = EIO_READAHEAD; req->sv1 = newSVsv (fh); - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); - req->offs = SvVAL64 (offset); + req->int1 = fd; + req->offs = offset; req->size = length; REQ_SEND; @@ -721,17 +875,9 @@ { dREQ; - req->ptr2 = malloc (sizeof (EIO_STRUCT_STAT)); - if (!req->ptr2) - { - req_destroy (req); - croak ("out of memory during aio_stat statdata allocation"); - } - - req->flags |= EIO_FLAG_PTR2_FREE; req->sv1 = newSVsv (fh_or_path); - if (SvPOK (fh_or_path)) + if (SvPOK (req->sv1)) { req->type = ix; req->ptr1 = SvPVbyte_nolen (req->sv1); @@ -755,7 +901,7 @@ req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.; req->sv1 = newSVsv (fh_or_path); - if (SvPOK (fh_or_path)) + if (SvPOK (req->sv1)) { req->type = EIO_UTIME; req->ptr1 = SvPVbyte_nolen (req->sv1); @@ -778,7 +924,7 @@ req->sv1 = newSVsv (fh_or_path); req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; - if (SvPOK (fh_or_path)) + if (SvPOK (req->sv1)) { req->type = EIO_TRUNCATE; req->ptr1 = SvPVbyte_nolen (req->sv1); @@ -796,20 +942,24 @@ aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef) ALIAS: aio_chmod = EIO_CHMOD - aio_fchmod = EIO_FCHMOD aio_mkdir = EIO_MKDIR PPCODE: { dREQ; - req->type = ix; req->int2 = mode; req->sv1 = newSVsv (fh_or_path); - if (ix == EIO_FCHMOD) - req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); + if (SvPOK (req->sv1)) + { + req->type = ix; + req->ptr1 = SvPVbyte_nolen (req->sv1); + } else - req->ptr1 = SvPVbyte_nolen (req->sv1); + { + req->type = EIO_FCHMOD; + req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); + } REQ_SEND; } @@ -824,7 +974,7 @@ req->int3 = SvOK (gid) ? SvIV (gid) : -1; req->sv1 = newSVsv (fh_or_path); - if (SvPOK (fh_or_path)) + if (SvPOK (req->sv1)) { req->type = EIO_CHOWN; req->ptr1 = SvPVbyte_nolen (req->sv1); @@ -839,6 +989,23 @@ } void +aio_readdirx (SV8 *pathname, IV flags, SV *callback=&PL_sv_undef) + PPCODE: +{ + dREQ; + + req->type = EIO_READDIR; + req->sv1 = newSVsv (pathname); + req->ptr1 = SvPVbyte_nolen (req->sv1); + req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1; + + if (flags & EIO_READDIR_DENTS) + req->int1 |= EIO_READDIR_CUSTOM2; + + REQ_SEND; +} + +void aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef) ALIAS: aio_unlink = EIO_UNLINK @@ -910,7 +1077,7 @@ req->type = EIO_GROUP; - eio_submit (req); + req_submit (req); XPUSHs (req_sv (req, AIO_GRP_KLASS)); } @@ -932,12 +1099,12 @@ aioreq_pri (int pri = 0) PROTOTYPE: ;$ CODE: - RETVAL = next_pri - EIO_PRI_BIAS; + RETVAL = next_pri; if (items > 0) { if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN; if (pri > EIO_PRI_MAX) pri = EIO_PRI_MAX; - next_pri = pri + EIO_PRI_BIAS; + next_pri = pri; } OUTPUT: RETVAL @@ -948,7 +1115,7 @@ nice = next_pri - nice; if (nice < EIO_PRI_MIN) nice = EIO_PRI_MIN; if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX; - next_pri = nice + EIO_PRI_BIAS; + next_pri = nice; void flush () @@ -1019,12 +1186,30 @@ nthreads() PROTOTYPE: CODE: - if (WORDACCESS_UNSAFE) X_LOCK (wrklock); - RETVAL = started; - if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock); + RETVAL = eio_nthreads (); OUTPUT: RETVAL +int +fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) + PROTOTYPE: $$$$ + CODE: +{ + #if _XOPEN_SOURCE >= 600 + RETVAL = posix_fadvise (fh, offset, length, advice); + #else + RETVAL = errno = ENOSYS; + #endif +} + OUTPUT: + RETVAL + +ssize_t +sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) + PROTOTYPE: $$$$ + CODE: + eio_sendfile_sync (ofh, ifh, offset, count); + void _on_next_submit (SV *cb) CODE: SvREFCNT_dec (on_next_submit); @@ -1041,9 +1226,19 @@ void cb (aio_req_ornot req, SV *callback=&PL_sv_undef) - CODE: - SvREFCNT_dec (req->callback); - req->callback = newSVsv (callback); + PPCODE: +{ + if (GIMME_V != G_VOID) + XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); + + if (items > 1) + { + SV *cb_cv = get_cb (callback); + + SvREFCNT_dec (req->callback); + req->callback = SvREFCNT_inc (cb_cv); + } +} MODULE = IO::AIO PACKAGE = IO::AIO::GRP @@ -1085,6 +1280,7 @@ grp->errorno = errno; av = newAV (); + av_extend (av, items - 1); for (i = 1; i < items; ++i ) av_push (av, newSVsv (ST (i))); @@ -1108,7 +1304,8 @@ CODE: { SvREFCNT_dec (grp->sv2); - grp->sv2 = newSVsv (callback); + grp->sv2 = newSVsv (callback); + grp->feed = aio_grp_feed; if (grp->int2 <= 0) grp->int2 = 2;