--- Linux-AIO/AIO.xs 2001/12/25 02:33:48 1.12 +++ Linux-AIO/AIO.xs 2002/04/01 20:30:08 1.13 @@ -3,16 +3,19 @@ #include "XSUB.h" #include +#include #include #include #include #include +typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ +typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ #define STACKSIZE 1024 /* yeah */ -enum { REQ_QUIT, REQ_READ, REQ_WRITE, REQ_OPEN, REQ_CLOSE }; +enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; typedef struct { char stack[STACKSIZE]; @@ -22,7 +25,8 @@ int type; aio_thread *thread; -/* read/write */ + SV *savesv; + int fd; off_t offset; size_t length; @@ -32,6 +36,8 @@ SV *data, *callback; void *dataptr; STRLEN dataoffset; + + struct stat64 *statdata; } aio_cb; typedef aio_cb *aio_req; @@ -82,6 +88,9 @@ STRLEN svlen; char *svptr = SvPV (data, svlen); + SvUPGRADE (data, SVt_PV); + SvPOK_on (data); + if (dataoffset < 0) dataoffset += svlen; @@ -138,10 +147,34 @@ int errorno = errno; errno = req->errorno; + if (req->savesv) + SvREFCNT_dec (req->savesv); + if (req->type == REQ_READ) SvCUR_set (req->data, req->dataoffset + req->result > 0 ? req->result : 0); + if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) + { + PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; + PL_laststatval = req->result; + PL_statcache.st_dev = req->statdata->st_dev; + PL_statcache.st_ino = req->statdata->st_ino; + PL_statcache.st_mode = req->statdata->st_mode; + PL_statcache.st_nlink = req->statdata->st_nlink; + PL_statcache.st_uid = req->statdata->st_uid; + PL_statcache.st_gid = req->statdata->st_gid; + PL_statcache.st_rdev = req->statdata->st_rdev; + PL_statcache.st_size = req->statdata->st_size; + PL_statcache.st_atime = req->statdata->st_atime; + PL_statcache.st_mtime = req->statdata->st_mtime; + PL_statcache.st_ctime = req->statdata->st_ctime; + PL_statcache.st_blksize = req->statdata->st_blksize; + PL_statcache.st_blocks = req->statdata->st_blocks; + + Safefree (req->statdata); + } + PUSHMARK (SP); XPUSHs (sv_2mortal (newSViv (req->result))); PUTBACK; @@ -175,12 +208,19 @@ int errno; /* we rely on gcc's ability to create closures. */ - _syscall3(int,lseek,int,fd,off_t,offset,int,whence) - _syscall3(int,read,int,fd,char *,buf,off_t,count) - _syscall3(int,write,int,fd,char *,buf,off_t,count) + _syscall3(int,read,int,fd,char *,buf,size_t,count) + _syscall3(int,write,int,fd,char *,buf,size_t,count) + _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) _syscall1(int,close,int,fd) + _syscall4(int,pread,int,fd,char *,buf,size_t,count,off_t,offset) + _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,off_t,offset) + + _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) + _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) + _syscall2(int,fstat64, int, fd, struct stat64 *, buf) + sigprocmask (SIG_SETMASK, &fullsigset, 0); /* then loop */ @@ -189,24 +229,20 @@ req->thread = thr; errno = 0; - if (req->type == REQ_READ || req->type == REQ_WRITE) - { - if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) - { - if (req->type == REQ_READ) - req->result = read (req->fd, req->dataptr, req->length); - else - req->result = write(req->fd, req->dataptr, req->length); - } - } + if (req->type == REQ_READ) + req->result = pread (req->fd, req->dataptr, req->length, req->offset); + else if (req->type == REQ_WRITE) + req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); else if (req->type == REQ_OPEN) - { - req->result = open (req->dataptr, req->fd, req->mode); - } + req->result = open (req->dataptr, req->fd, req->mode); else if (req->type == REQ_CLOSE) - { - req->result = close (req->fd); - } + req->result = close (req->fd); + else if (req->type == REQ_STAT) + req->result = stat64 (req->dataptr, req->statdata); + else if (req->type == REQ_LSTAT) + req->result = lstat64 (req->dataptr, req->statdata); + else if (req->type == REQ_FSTAT) + req->result = fstat64 (req->fd, req->statdata); else { write (respipe[1], (void *)&req, sizeof (req)); @@ -267,24 +303,8 @@ } void -aio_read(fh,offset,length,data,dataoffset,callback) - InOutStream fh - UV offset - IV length - SV * data - IV dataoffset - SV * callback - PROTOTYPE: $$$$$$ - ALIAS: - aio_write = 1 - CODE: - SvUPGRADE (data, SVt_PV); - SvPOK_on (data); - read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); - -void aio_open(pathname,flags,mode,callback) - char * pathname + SV * pathname int flags int mode SV * callback @@ -298,7 +318,8 @@ croak ("out of memory during aio_req allocation"); req->type = REQ_OPEN; - req->dataptr = pathname; + req->savesv = newSVsv (pathname); + req->dataptr = SvPV_nolen (req->savesv); req->fd = flags; req->mode = mode; req->callback = SvREFCNT_inc (callback); @@ -307,7 +328,7 @@ void aio_close(fh,callback) - InOutStream fh + InputStream fh SV * callback PROTOTYPE: $ CODE: @@ -323,6 +344,66 @@ req->callback = SvREFCNT_inc (callback); send_req (req); + +void +aio_read(fh,offset,length,data,dataoffset,callback) + InputStream fh + UV offset + IV length + SV * data + IV dataoffset + SV * callback + PROTOTYPE: $$$$$$ + CODE: + read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); + +void +aio_write(fh,offset,length,data,dataoffset,callback) + OutputStream fh + UV offset + IV length + SV * data + IV dataoffset + SV * callback + PROTOTYPE: $$$$$$ + CODE: + read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); + +void +aio_stat(fh_or_path,callback) + SV * fh_or_path + SV * callback + PROTOTYPE: $$ + ALIAS: + aio_lstat = 1 + CODE: + aio_req req; + + New (0, req, 1, aio_cb); + + if (!req) + croak ("out of memory during aio_req allocation"); + + New (0, req->statdata, 1, struct stat64); + + if (!req->statdata) + croak ("out of memory during aio_req->statdata allocation"); + + if (SvPOK (fh_or_path)) + { + req->type = ix ? REQ_LSTAT : REQ_STAT; + req->savesv = newSVsv (fh_or_path); + req->dataptr = SvPV_nolen (req->savesv); + } + else + { + req->type = REQ_FSTAT; + req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); + } + + req->callback = SvREFCNT_inc (callback); + + send_req (req); int poll_fileno()