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.148 by root, Wed Jun 17 01:14:48 2009 UTC vs.
Revision 1.155 by root, Fri Jul 17 00:56:54 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>
117 119
118#include "libeio/eio.h" 120#include "libeio/eio.h"
119 121
120#ifndef POSIX_FADV_NORMAL 122#ifndef POSIX_FADV_NORMAL
121# define POSIX_FADV_NORMAL 0 123# define POSIX_FADV_NORMAL 0
124# define NO_FADVISE 1
122#endif 125#endif
123 126
124#ifndef POSIX_FADV_SEQUENTIAL 127#ifndef POSIX_FADV_SEQUENTIAL
125# define POSIX_FADV_SEQUENTIAL 0 128# define POSIX_FADV_SEQUENTIAL 0
126#endif 129#endif
157 160
158static SV *on_next_submit; 161static SV *on_next_submit;
159static int next_pri = EIO_PRI_DEFAULT; 162static int next_pri = EIO_PRI_DEFAULT;
160static int max_outstanding; 163static int max_outstanding;
161 164
162static int respipe_osf [2], respipe [2] = { -1, -1 }; 165static s_epipe respipe;
163 166
164static void req_destroy (aio_req req); 167static void req_destroy (aio_req req);
165static void req_cancel (aio_req req); 168static void req_cancel (aio_req req);
166 169
167static void want_poll (void) 170static void want_poll (void)
168{ 171{
169 /* write a dummy byte to the pipe so fh becomes ready */ 172 /* write a dummy byte to the pipe so fh becomes ready */
170 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); 173 s_epipe_signal (&respipe);
171} 174}
172 175
173static void done_poll (void) 176static void done_poll (void)
174{ 177{
175 /* read any signals sent by the worker threads */ 178 /* read any signals sent by the worker threads */
176 char buf [4]; 179 s_epipe_drain (&respipe);
177 while (respipe_read (respipe [0], buf, 4) == 4)
178 ;
179} 180}
180 181
181/* must be called at most once */ 182/* must be called at most once */
182static SV *req_sv (aio_req req, const char *klass) 183static SV *req_sv (aio_req req, const char *klass)
183{ 184{
438 grp->sv2 = 0; 439 grp->sv2 = 0;
439 440
440 eio_grp_cancel (grp); 441 eio_grp_cancel (grp);
441} 442}
442 443
443#ifdef USE_SOCKETS_AS_HANDLES
444# define TO_SOCKET(x) (win32_get_osfhandle (x))
445#else
446# define TO_SOCKET(x) (x)
447#endif
448
449static void 444static void
450create_respipe (void) 445create_respipe (void)
451{ 446{
452 int old_readfd = respipe [0];
453
454 if (respipe [1] >= 0)
455 respipe_close (TO_SOCKET (respipe [1]));
456
457#ifdef _WIN32
458 if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe))
459#else
460 if (pipe (respipe)) 447 if (s_epipe_renew (&respipe))
461#endif
462 croak ("unable to initialize result pipe"); 448 croak ("IO::AIO: unable to initialize result pipe");
463
464 if (old_readfd >= 0)
465 {
466 if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
467 croak ("unable to initialize result pipe(2)");
468
469 respipe_close (respipe [0]);
470 respipe [0] = old_readfd;
471 }
472
473#ifdef _WIN32
474 int arg = 1;
475 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
476 || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
477#else
478 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
479 || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
480#endif
481 croak ("unable to initialize result pipe(3)");
482
483 respipe_osf [0] = TO_SOCKET (respipe [0]);
484 respipe_osf [1] = TO_SOCKET (respipe [1]);
485} 449}
486 450
487static void poll_wait (void) 451static void poll_wait (void)
488{ 452{
489 fd_set rfd;
490
491 while (eio_nreqs ()) 453 while (eio_nreqs ())
492 { 454 {
493 int size; 455 int size;
494 456
495 X_LOCK (reslock); 457 X_LOCK (reslock);
499 if (size) 461 if (size)
500 return; 462 return;
501 463
502 etp_maybe_start_thread (); 464 etp_maybe_start_thread ();
503 465
504 FD_ZERO (&rfd); 466 s_epipe_wait (&respipe);
505 FD_SET (respipe [0], &rfd);
506
507 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
508 } 467 }
509} 468}
510 469
511static int poll_cb (void) 470static int poll_cb (void)
512{ 471{
530} 489}
531 490
532static SV * 491static SV *
533get_cb (SV *cb_sv) 492get_cb (SV *cb_sv)
534{ 493{
535 HV *st; 494 SvGETMAGIC (cb_sv);
536 GV *gvp; 495 return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0;
537 CV *cv;
538
539 if (!SvOK (cb_sv))
540 return 0;
541
542 cv = sv_2cv (cb_sv, &st, &gvp, 0);
543
544 if (!cv)
545 croak ("IO::AIO callback must be undef or a CODE reference");
546
547 return (SV *)cv;
548} 496}
549 497
550#define dREQ \ 498#define dREQ \
551 SV *cb_cv; \ 499 SV *cb_cv; \
552 aio_req req; \ 500 aio_req req; \
567 req_submit (req); \ 515 req_submit (req); \
568 SPAGAIN; \ 516 SPAGAIN; \
569 \ 517 \
570 if (GIMME_V != G_VOID) \ 518 if (GIMME_V != G_VOID) \
571 XPUSHs (req_sv (req, AIO_REQ_KLASS)); 519 XPUSHs (req_sv (req, AIO_REQ_KLASS));
572
573static int
574extract_fd (SV *fh, int wr)
575{
576 int fd = PerlIO_fileno (wr ? IoOFP (sv_2io (fh)) : IoIFP (sv_2io (fh)));
577
578 if (fd < 0)
579 croak ("illegal fh argument, either not an OS file or read/write mode mismatch");
580
581 return fd;
582}
583 520
584MODULE = IO::AIO PACKAGE = IO::AIO 521MODULE = IO::AIO PACKAGE = IO::AIO
585 522
586PROTOTYPES: ENABLE 523PROTOTYPES: ENABLE
587 524
701 ALIAS: 638 ALIAS:
702 aio_fsync = EIO_FSYNC 639 aio_fsync = EIO_FSYNC
703 aio_fdatasync = EIO_FDATASYNC 640 aio_fdatasync = EIO_FDATASYNC
704 PPCODE: 641 PPCODE:
705{ 642{
706 int fd = extract_fd (fh, 0); 643 int fd = s_fileno_croak (fh, 0);
707 dREQ; 644 dREQ;
708 645
709 req->type = ix; 646 req->type = ix;
710 req->sv1 = newSVsv (fh); 647 req->sv1 = newSVsv (fh);
711 req->int1 = fd; 648 req->int1 = fd;
716void 653void
717aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) 654aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef)
718 PROTOTYPE: $$$$;$ 655 PROTOTYPE: $$$$;$
719 PPCODE: 656 PPCODE:
720{ 657{
721 int fd = extract_fd (fh, 0); 658 int fd = s_fileno_croak (fh, 0);
722 dREQ; 659 dREQ;
723 660
724 req->type = EIO_SYNC_FILE_RANGE; 661 req->type = EIO_SYNC_FILE_RANGE;
725 req->sv1 = newSVsv (fh); 662 req->sv1 = newSVsv (fh);
726 req->int1 = fd; 663 req->int1 = fd;
735aio_close (SV *fh, SV *callback=&PL_sv_undef) 672aio_close (SV *fh, SV *callback=&PL_sv_undef)
736 PROTOTYPE: $;$ 673 PROTOTYPE: $;$
737 PPCODE: 674 PPCODE:
738{ 675{
739 static int close_pipe = -1; /* dummy fd to close fds via dup2 */ 676 static int close_pipe = -1; /* dummy fd to close fds via dup2 */
740 int fd = extract_fd (fh, 0); 677 int fd = s_fileno_croak (fh, 0);
741 dREQ; 678 dREQ;
742 679
743 if (close_pipe < 0) 680 if (close_pipe < 0)
744 { 681 {
745 int pipefd [2]; 682 int pipefd [2];
767 aio_write = EIO_WRITE 704 aio_write = EIO_WRITE
768 PROTOTYPE: $$$$$;$ 705 PROTOTYPE: $$$$$;$
769 PPCODE: 706 PPCODE:
770{ 707{
771 STRLEN svlen; 708 STRLEN svlen;
772 int fd = extract_fd (fh, ix == EIO_WRITE); 709 int fd = s_fileno_croak (fh, ix == EIO_WRITE);
773 char *svptr = SvPVbyte (data, svlen); 710 char *svptr = SvPVbyte (data, svlen);
774 UV len = SvUV (length); 711 UV len = SvUV (length);
775 712
776 if (dataoffset < 0) 713 if (dataoffset < 0)
777 dataoffset += svlen; 714 dataoffset += svlen;
832void 769void
833aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) 770aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef)
834 PROTOTYPE: $$$$;$ 771 PROTOTYPE: $$$$;$
835 PPCODE: 772 PPCODE:
836{ 773{
837 int ifd = extract_fd (in_fh , 0); 774 int ifd = s_fileno_croak (in_fh , 0);
838 int ofd = extract_fd (out_fh, 0); 775 int ofd = s_fileno_croak (out_fh, 1);
839 dREQ; 776 dREQ;
840 777
841 req->type = EIO_SENDFILE; 778 req->type = EIO_SENDFILE;
842 req->sv1 = newSVsv (out_fh); 779 req->sv1 = newSVsv (out_fh);
843 req->int1 = ofd; 780 req->int1 = ofd;
852void 789void
853aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) 790aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef)
854 PROTOTYPE: $$$;$ 791 PROTOTYPE: $$$;$
855 PPCODE: 792 PPCODE:
856{ 793{
857 int fd = extract_fd (fh, 0); 794 int fd = s_fileno_croak (fh, 0);
858 dREQ; 795 dREQ;
859 796
860 req->type = EIO_READAHEAD; 797 req->type = EIO_READAHEAD;
861 req->sv1 = newSVsv (fh); 798 req->sv1 = newSVsv (fh);
862 req->int1 = fd; 799 req->int1 = fd;
1138 1075
1139int 1076int
1140poll_fileno() 1077poll_fileno()
1141 PROTOTYPE: 1078 PROTOTYPE:
1142 CODE: 1079 CODE:
1143 RETVAL = respipe [0]; 1080 RETVAL = s_epipe_fd (&respipe);
1144 OUTPUT: 1081 OUTPUT:
1145 RETVAL 1082 RETVAL
1146 1083
1147int 1084int
1148poll_cb(...) 1085poll_cb(...)
1192 1129
1193int 1130int
1194fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) 1131fadvise (aio_rfd fh, off_t offset, off_t length, IV advice)
1195 PROTOTYPE: $$$$ 1132 PROTOTYPE: $$$$
1196 CODE: 1133 CODE:
1197{
1198 #if _XOPEN_SOURCE >= 600 1134#if _XOPEN_SOURCE >= 600 && !NO_FADVISE
1199 RETVAL = posix_fadvise (fh, offset, length, advice); 1135 RETVAL = posix_fadvise (fh, offset, length, advice);
1200 #else 1136#else
1201 RETVAL = errno = ENOSYS; 1137 RETVAL = errno = ENOSYS;
1202 #endif 1138#endif
1203}
1204 OUTPUT: 1139 OUTPUT:
1205 RETVAL 1140 RETVAL
1206 1141
1207ssize_t 1142ssize_t
1208sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) 1143sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count)
1231 if (GIMME_V != G_VOID) 1166 if (GIMME_V != G_VOID)
1232 XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); 1167 XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef);
1233 1168
1234 if (items > 1) 1169 if (items > 1)
1235 { 1170 {
1236 SV *cb_cv = get_cb (callback); 1171 SV *cb_cv =get_cb (callback);
1237 1172
1238 SvREFCNT_dec (req->callback); 1173 SvREFCNT_dec (req->callback);
1239 req->callback = SvREFCNT_inc (cb_cv); 1174 req->callback = SvREFCNT_inc (cb_cv);
1240 } 1175 }
1241} 1176}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines