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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines