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.141 by root, Sat Jun 6 17:25:13 2009 UTC vs.
Revision 1.151 by root, Tue Jul 14 00:13:36 2009 UTC

3#include <errno.h> 3#include <errno.h>
4 4
5#include "EXTERN.h" 5#include "EXTERN.h"
6#include "perl.h" 6#include "perl.h"
7#include "XSUB.h" 7#include "XSUB.h"
8
9#include "schmorp.h"
8 10
9#include <stddef.h> 11#include <stddef.h>
10#include <stdlib.h> 12#include <stdlib.h>
11#include <errno.h> 13#include <errno.h>
12#include <sys/types.h> 14#include <sys/types.h>
75 77
76#define EIO_STRUCT_STAT Stat_t 78#define EIO_STRUCT_STAT Stat_t
77 79
78/* use NV for 32 bit perls as it allows larger offsets */ 80/* use NV for 32 bit perls as it allows larger offsets */
79#if IVSIZE >= 8 81#if IVSIZE >= 8
82# define VAL64 IV
80# define SvVAL64 SvIV 83# define SvVAL64 SvIV
84# define newSVval64 newSViv
81#else 85#else
86# define VAL64 NV
82# define SvVAL64 SvNV 87# define SvVAL64 SvNV
88# define newSVval64 newSVnv
83#endif 89#endif
84 90
85/*****************************************************************************/ 91/*****************************************************************************/
86 92
87#if __GNUC__ >= 3 93#if __GNUC__ >= 3
95 101
96/*****************************************************************************/ 102/*****************************************************************************/
97 103
98static HV *stash; 104static HV *stash;
99typedef SV SV8; /* byte-sv, used for argument-checking */ 105typedef SV SV8; /* byte-sv, used for argument-checking */
106typedef int aio_rfd; /* read file desriptor */
107typedef int aio_wfd; /* write file descriptor */
100 108
101#define AIO_REQ_KLASS "IO::AIO::REQ" 109#define AIO_REQ_KLASS "IO::AIO::REQ"
102#define AIO_GRP_KLASS "IO::AIO::GRP" 110#define AIO_GRP_KLASS "IO::AIO::GRP"
103 111
104#define EIO_REQ_MEMBERS \ 112#define EIO_REQ_MEMBERS \
108 SV *self; 116 SV *self;
109 117
110#define EIO_NO_WRAPPERS 1 118#define EIO_NO_WRAPPERS 1
111 119
112#include "libeio/eio.h" 120#include "libeio/eio.h"
121
122#ifndef POSIX_FADV_NORMAL
123# define POSIX_FADV_NORMAL 0
124# define NO_FADVISE 1
125#endif
126
127#ifndef POSIX_FADV_SEQUENTIAL
128# define POSIX_FADV_SEQUENTIAL 0
129#endif
130
131#ifndef POSIX_FADV_RANDOM
132# define POSIX_FADV_RANDOM 0
133#endif
134
135#ifndef POSIX_FADV_NOREUSE
136# define POSIX_FADV_NOREUSE 0
137#endif
138
139#ifndef POSIX_FADV_WILLNEED
140# define POSIX_FADV_WILLNEED 0
141#endif
142
143#ifndef POSIX_FADV_DONTNEED
144# define POSIX_FADV_DONTNEED 0
145#endif
113 146
114static int req_invoke (eio_req *req); 147static int req_invoke (eio_req *req);
115#define EIO_FINISH(req) req_invoke (req) 148#define EIO_FINISH(req) req_invoke (req)
116static void req_destroy (eio_req *grp); 149static void req_destroy (eio_req *grp);
117#define EIO_DESTROY(req) req_destroy (req) 150#define EIO_DESTROY(req) req_destroy (req)
228 SV *rv = &PL_sv_undef; 261 SV *rv = &PL_sv_undef;
229 262
230 if (req->result >= 0) 263 if (req->result >= 0)
231 { 264 {
232 int i; 265 int i;
233 char *buf = req->ptr2; 266 char *names = (char *)req->ptr2;
267 eio_dirent *ent = (eio_dirent *)req->ptr1; /* might be 0 */
234 AV *av = newAV (); 268 AV *av = newAV ();
235 269
236 av_extend (av, req->result - 1); 270 av_extend (av, req->result - 1);
237 271
238 for (i = 0; i < req->result; ++i) 272 for (i = 0; i < req->result; ++i)
239 { 273 {
240 if (req->int1 & EIO_READDIR_DENTS) 274 if (req->int1 & EIO_READDIR_DENTS)
241 { 275 {
242 eio_dirent *ent = (eio_dirent *)buf;
243 SV *namesv = newSVpvn (ent->name, ent->namelen); 276 SV *namesv = newSVpvn (names + ent->nameofs, ent->namelen);
244 277
245 if (req->int1 & EIO_READDIR_CUSTOM2) 278 if (req->int1 & EIO_READDIR_CUSTOM2)
246 { 279 {
247 static SV *sv_type [EIO_DT_MAX + 1]; /* type sv cache */ 280 static SV *sv_type [EIO_DT_MAX + 1]; /* type sv cache */
248 AV *avent = newAV (); 281 AV *avent = newAV ();
254 sv_type [ent->type] = newSViv (ent->type); 287 sv_type [ent->type] = newSViv (ent->type);
255 SvREADONLY_on (sv_type [ent->type]); 288 SvREADONLY_on (sv_type [ent->type]);
256 } 289 }
257 290
258 av_store (avent, 0, namesv); 291 av_store (avent, 0, namesv);
292 av_store (avent, 1, SvREFCNT_inc (sv_type [ent->type]));
259 av_store (avent, 1, IVSIZE >= 8 ? newSVuv (ent->inode) : newSVnv (ent->inode)); 293 av_store (avent, 2, IVSIZE >= 8 ? newSVuv (ent->inode) : newSVnv (ent->inode));
260 av_store (avent, 2, SvREFCNT_inc (sv_type [ent->type]));
261 294
262 av_store (av, i, newRV_noinc ((SV *)avent)); 295 av_store (av, i, newRV_noinc ((SV *)avent));
263 } 296 }
264 else 297 else
265 av_store (av, i, namesv); 298 av_store (av, i, namesv);
266 299
267 buf += sizeof (eio_dirent); 300 ++ent;
268 } 301 }
269 else 302 else
270 { 303 {
271 SV *name = newSVpv (buf, 0); 304 SV *name = newSVpv (names, 0);
272 av_store (av, i, name); 305 av_store (av, i, name);
273 buf += SvCUR (name) + 1; 306 names += SvCUR (name) + 1;
274 } 307 }
275 } 308 }
276 309
277 rv = sv_2mortal (newRV_noinc ((SV *)av)); 310 rv = sv_2mortal (newRV_noinc ((SV *)av));
278 } 311 }
294 GV *gv = (GV *)sv_newmortal (); 327 GV *gv = (GV *)sv_newmortal ();
295 int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); 328 int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR);
296 char sym [64]; 329 char sym [64];
297 int symlen; 330 int symlen;
298 331
299 symlen = snprintf (sym, sizeof (sym), "fd#%d", req->result); 332 symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result);
300 gv_init (gv, stash, sym, symlen, 0); 333 gv_init (gv, stash, sym, symlen, 0);
301 334
302 symlen = snprintf ( 335 symlen = snprintf (
303 sym, 336 sym,
304 sizeof (sym), 337 sizeof (sym),
305 "%s&=%d", 338 "%s&=%d",
306 flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<", 339 flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<",
307 req->result 340 (int)req->result
308 ); 341 );
309 342
310 if (do_open (gv, sym, symlen, 0, 0, 0, 0)) 343 if (do_open (gv, sym, symlen, 0, 0, 0, 0))
311 fh = (SV *)gv; 344 fh = (SV *)gv;
312 } 345 }
538 SPAGAIN; \ 571 SPAGAIN; \
539 \ 572 \
540 if (GIMME_V != G_VOID) \ 573 if (GIMME_V != G_VOID) \
541 XPUSHs (req_sv (req, AIO_REQ_KLASS)); 574 XPUSHs (req_sv (req, AIO_REQ_KLASS));
542 575
543static int
544extract_fd (SV *fh, int wr)
545{
546 int fd = PerlIO_fileno (wr ? IoOFP (sv_2io (fh)) : IoIFP (sv_2io (fh)));
547
548 if (fd < 0)
549 croak ("illegal fh argument, either not an OS file or read/write mode mismatch");
550
551 return fd;
552}
553
554MODULE = IO::AIO PACKAGE = IO::AIO 576MODULE = IO::AIO PACKAGE = IO::AIO
555 577
556PROTOTYPES: ENABLE 578PROTOTYPES: ENABLE
557 579
558BOOT: 580BOOT:
559{ 581{
560 stash = gv_stashpv ("IO::AIO", 1);
561
562 static const struct { 582 static const struct {
563 const char *name; 583 const char *name;
564 IV iv; 584 IV iv;
565 } *civ, const_iv[] = { 585 } *civ, const_iv[] = {
566# define const_iv(name, value) { # name, (IV) value }, 586# define const_iv(name, value) { # name, (IV) value },
572 const_iv (O_CREAT , O_CREAT) 592 const_iv (O_CREAT , O_CREAT)
573 const_iv (O_TRUNC , O_TRUNC) 593 const_iv (O_TRUNC , O_TRUNC)
574#ifndef _WIN32 594#ifndef _WIN32
575 const_iv (S_IFIFO , S_IFIFO) 595 const_iv (S_IFIFO , S_IFIFO)
576#endif 596#endif
597 const_iv (FADV_NORMAL , POSIX_FADV_NORMAL)
598 const_iv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL)
599 const_iv (FADV_RANDOM , POSIX_FADV_RANDOM)
600 const_iv (FADV_NOREUSE , POSIX_FADV_NOREUSE)
601 const_iv (FADV_WILLNEED , POSIX_FADV_WILLNEED)
602 const_iv (FADV_DONTNEED , POSIX_FADV_DONTNEED)
603
577 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) 604 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE)
578 const_eio (SYNC_FILE_RANGE_WRITE) 605 const_eio (SYNC_FILE_RANGE_WRITE)
579 const_eio (SYNC_FILE_RANGE_WAIT_AFTER) 606 const_eio (SYNC_FILE_RANGE_WAIT_AFTER)
580 607
581 const_eio (READDIR_DENTS) 608 const_eio (READDIR_DENTS)
592 const_eio (DT_LNK) 619 const_eio (DT_LNK)
593 const_eio (DT_SOCK) 620 const_eio (DT_SOCK)
594 const_eio (DT_WHT) 621 const_eio (DT_WHT)
595 }; 622 };
596 623
624 stash = gv_stashpv ("IO::AIO", 1);
625
597 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 626 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
598 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 627 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
599 628
600 create_respipe (); 629 create_respipe ();
601 630
664 ALIAS: 693 ALIAS:
665 aio_fsync = EIO_FSYNC 694 aio_fsync = EIO_FSYNC
666 aio_fdatasync = EIO_FDATASYNC 695 aio_fdatasync = EIO_FDATASYNC
667 PPCODE: 696 PPCODE:
668{ 697{
669 int fd = extract_fd (fh, 0); 698 int fd = s_fileno_croak (fh, 0);
670 dREQ; 699 dREQ;
671 700
672 req->type = ix; 701 req->type = ix;
673 req->sv1 = newSVsv (fh); 702 req->sv1 = newSVsv (fh);
674 req->int1 = fd; 703 req->int1 = fd;
675 704
676 REQ_SEND (req); 705 REQ_SEND (req);
677} 706}
678 707
679void 708void
680aio_sync_file_range (SV *fh, SV *offset, SV *nbytes, IV flags, SV *callback=&PL_sv_undef) 709aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef)
681 PROTOTYPE: $$$$;$ 710 PROTOTYPE: $$$$;$
682 PPCODE: 711 PPCODE:
683{ 712{
684 int fd = extract_fd (fh, 0); 713 int fd = s_fileno_croak (fh, 0);
685 dREQ; 714 dREQ;
686 715
687 req->type = EIO_SYNC_FILE_RANGE; 716 req->type = EIO_SYNC_FILE_RANGE;
688 req->sv1 = newSVsv (fh); 717 req->sv1 = newSVsv (fh);
689 req->int1 = fd; 718 req->int1 = fd;
690 req->offs = SvVAL64 (offset); 719 req->offs = offset;
691 req->size = SvVAL64 (nbytes); 720 req->size = nbytes;
692 req->int2 = flags; 721 req->int2 = flags;
693 722
694 REQ_SEND (req); 723 REQ_SEND (req);
695} 724}
696 725
698aio_close (SV *fh, SV *callback=&PL_sv_undef) 727aio_close (SV *fh, SV *callback=&PL_sv_undef)
699 PROTOTYPE: $;$ 728 PROTOTYPE: $;$
700 PPCODE: 729 PPCODE:
701{ 730{
702 static int close_pipe = -1; /* dummy fd to close fds via dup2 */ 731 static int close_pipe = -1; /* dummy fd to close fds via dup2 */
703 int fd = extract_fd (fh, 0); 732 int fd = s_fileno_croak (fh, 0);
704 dREQ; 733 dREQ;
705 734
706 if (close_pipe < 0) 735 if (close_pipe < 0)
707 { 736 {
708 int pipefd [2]; 737 int pipefd [2];
730 aio_write = EIO_WRITE 759 aio_write = EIO_WRITE
731 PROTOTYPE: $$$$$;$ 760 PROTOTYPE: $$$$$;$
732 PPCODE: 761 PPCODE:
733{ 762{
734 STRLEN svlen; 763 STRLEN svlen;
735 int fd = extract_fd (fh, ix == EIO_WRITE); 764 int fd = s_fileno_croak (fh, ix == EIO_WRITE);
736 char *svptr = SvPVbyte (data, svlen); 765 char *svptr = SvPVbyte (data, svlen);
737 UV len = SvUV (length); 766 UV len = SvUV (length);
738 767
739 if (dataoffset < 0) 768 if (dataoffset < 0)
740 dataoffset += svlen; 769 dataoffset += svlen;
791 820
792 REQ_SEND; 821 REQ_SEND;
793} 822}
794 823
795void 824void
796aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef) 825aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef)
797 PROTOTYPE: $$$$;$ 826 PROTOTYPE: $$$$;$
798 PPCODE: 827 PPCODE:
799{ 828{
800 int ifd = extract_fd (in_fh , 0); 829 int ifd = s_fileno_croak (in_fh , 0);
801 int ofd = extract_fd (out_fh, 0); 830 int ofd = s_fileno_croak (out_fh, 1);
802 dREQ; 831 dREQ;
803 832
804 req->type = EIO_SENDFILE; 833 req->type = EIO_SENDFILE;
805 req->sv1 = newSVsv (out_fh); 834 req->sv1 = newSVsv (out_fh);
806 req->int1 = ofd; 835 req->int1 = ofd;
807 req->sv2 = newSVsv (in_fh); 836 req->sv2 = newSVsv (in_fh);
808 req->int2 = ifd; 837 req->int2 = ifd;
809 req->offs = SvVAL64 (in_offset); 838 req->offs = in_offset;
810 req->size = length; 839 req->size = length;
811 840
812 REQ_SEND; 841 REQ_SEND;
813} 842}
814 843
815void 844void
816aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef) 845aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef)
817 PROTOTYPE: $$$;$ 846 PROTOTYPE: $$$;$
818 PPCODE: 847 PPCODE:
819{ 848{
820 int fd = extract_fd (fh, 0); 849 int fd = s_fileno_croak (fh, 0);
821 dREQ; 850 dREQ;
822 851
823 req->type = EIO_READAHEAD; 852 req->type = EIO_READAHEAD;
824 req->sv1 = newSVsv (fh); 853 req->sv1 = newSVsv (fh);
825 req->int1 = fd; 854 req->int1 = fd;
826 req->offs = SvVAL64 (offset); 855 req->offs = offset;
827 req->size = length; 856 req->size = length;
828 857
829 REQ_SEND; 858 REQ_SEND;
830} 859}
831 860
961 req->sv1 = newSVsv (pathname); 990 req->sv1 = newSVsv (pathname);
962 req->ptr1 = SvPVbyte_nolen (req->sv1); 991 req->ptr1 = SvPVbyte_nolen (req->sv1);
963 req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1; 992 req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1;
964 993
965 if (flags & EIO_READDIR_DENTS) 994 if (flags & EIO_READDIR_DENTS)
966 flags |= EIO_READDIR_CUSTOM2; 995 req->int1 |= EIO_READDIR_CUSTOM2;
967 996
968 REQ_SEND; 997 REQ_SEND;
969} 998}
970 999
971void 1000void
1151 CODE: 1180 CODE:
1152 RETVAL = eio_nthreads (); 1181 RETVAL = eio_nthreads ();
1153 OUTPUT: 1182 OUTPUT:
1154 RETVAL 1183 RETVAL
1155 1184
1185int
1186fadvise (aio_rfd fh, off_t offset, off_t length, IV advice)
1187 PROTOTYPE: $$$$
1188 CODE:
1189#if _XOPEN_SOURCE >= 600 && !NO_FADVISE
1190 RETVAL = posix_fadvise (fh, offset, length, advice);
1191#else
1192 RETVAL = errno = ENOSYS;
1193#endif
1194 OUTPUT:
1195 RETVAL
1196
1197ssize_t
1198sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count)
1199 PROTOTYPE: $$$$
1200 CODE:
1201 eio_sendfile_sync (ofh, ifh, offset, count);
1202
1156void _on_next_submit (SV *cb) 1203void _on_next_submit (SV *cb)
1157 CODE: 1204 CODE:
1158 SvREFCNT_dec (on_next_submit); 1205 SvREFCNT_dec (on_next_submit);
1159 on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; 1206 on_next_submit = SvOK (cb) ? newSVsv (cb) : 0;
1160 1207

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines