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.153 by root, Wed Jul 15 01:36:04 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 int respipe_osf [2];
166static s_epipe respipe;
163 167
164static void req_destroy (aio_req req); 168static void req_destroy (aio_req req);
165static void req_cancel (aio_req req); 169static void req_cancel (aio_req req);
166 170
167static void want_poll (void) 171static void want_poll (void)
168{ 172{
169 /* write a dummy byte to the pipe so fh becomes ready */ 173 /* write a dummy byte to the pipe so fh becomes ready */
170 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); 174 s_epipe_signal (&respipe);
171} 175}
172 176
173static void done_poll (void) 177static void done_poll (void)
174{ 178{
175 /* read any signals sent by the worker threads */ 179 /* read any signals sent by the worker threads */
176 char buf [4]; 180 s_epipe_drain (&respipe);
177 while (respipe_read (respipe [0], buf, 4) == 4)
178 ;
179} 181}
180 182
181/* must be called at most once */ 183/* must be called at most once */
182static SV *req_sv (aio_req req, const char *klass) 184static SV *req_sv (aio_req req, const char *klass)
183{ 185{
438 grp->sv2 = 0; 440 grp->sv2 = 0;
439 441
440 eio_grp_cancel (grp); 442 eio_grp_cancel (grp);
441} 443}
442 444
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 445static void
450create_respipe (void) 446create_respipe (void)
451{ 447{
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)) 448 if (s_epipe_renew (&respipe))
461#endif
462 croak ("unable to initialize result pipe"); 449 croak ("unable to initialize result pipe");
463 450
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]); 451 respipe_osf [0] = S_TO_SOCKET (respipe.fd [0]);
484 respipe_osf [1] = TO_SOCKET (respipe [1]); 452 respipe_osf [1] = S_TO_SOCKET (respipe.fd [1]);
485} 453}
486 454
487static void poll_wait (void) 455static void poll_wait (void)
488{ 456{
489 fd_set rfd;
490
491 while (eio_nreqs ()) 457 while (eio_nreqs ())
492 { 458 {
493 int size; 459 int size;
494 460
495 X_LOCK (reslock); 461 X_LOCK (reslock);
499 if (size) 465 if (size)
500 return; 466 return;
501 467
502 etp_maybe_start_thread (); 468 etp_maybe_start_thread ();
503 469
504 FD_ZERO (&rfd); 470 s_epipe_wait (&respipe);
505 FD_SET (respipe [0], &rfd);
506
507 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
508 } 471 }
509} 472}
510 473
511static int poll_cb (void) 474static int poll_cb (void)
512{ 475{
530} 493}
531 494
532static SV * 495static SV *
533get_cb (SV *cb_sv) 496get_cb (SV *cb_sv)
534{ 497{
535 HV *st; 498 SvGETMAGIC (cb_sv);
536 GV *gvp; 499 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} 500}
549 501
550#define dREQ \ 502#define dREQ \
551 SV *cb_cv; \ 503 SV *cb_cv; \
552 aio_req req; \ 504 aio_req req; \
567 req_submit (req); \ 519 req_submit (req); \
568 SPAGAIN; \ 520 SPAGAIN; \
569 \ 521 \
570 if (GIMME_V != G_VOID) \ 522 if (GIMME_V != G_VOID) \
571 XPUSHs (req_sv (req, AIO_REQ_KLASS)); 523 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 524
584MODULE = IO::AIO PACKAGE = IO::AIO 525MODULE = IO::AIO PACKAGE = IO::AIO
585 526
586PROTOTYPES: ENABLE 527PROTOTYPES: ENABLE
587 528
701 ALIAS: 642 ALIAS:
702 aio_fsync = EIO_FSYNC 643 aio_fsync = EIO_FSYNC
703 aio_fdatasync = EIO_FDATASYNC 644 aio_fdatasync = EIO_FDATASYNC
704 PPCODE: 645 PPCODE:
705{ 646{
706 int fd = extract_fd (fh, 0); 647 int fd = s_fileno_croak (fh, 0);
707 dREQ; 648 dREQ;
708 649
709 req->type = ix; 650 req->type = ix;
710 req->sv1 = newSVsv (fh); 651 req->sv1 = newSVsv (fh);
711 req->int1 = fd; 652 req->int1 = fd;
716void 657void
717aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) 658aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef)
718 PROTOTYPE: $$$$;$ 659 PROTOTYPE: $$$$;$
719 PPCODE: 660 PPCODE:
720{ 661{
721 int fd = extract_fd (fh, 0); 662 int fd = s_fileno_croak (fh, 0);
722 dREQ; 663 dREQ;
723 664
724 req->type = EIO_SYNC_FILE_RANGE; 665 req->type = EIO_SYNC_FILE_RANGE;
725 req->sv1 = newSVsv (fh); 666 req->sv1 = newSVsv (fh);
726 req->int1 = fd; 667 req->int1 = fd;
735aio_close (SV *fh, SV *callback=&PL_sv_undef) 676aio_close (SV *fh, SV *callback=&PL_sv_undef)
736 PROTOTYPE: $;$ 677 PROTOTYPE: $;$
737 PPCODE: 678 PPCODE:
738{ 679{
739 static int close_pipe = -1; /* dummy fd to close fds via dup2 */ 680 static int close_pipe = -1; /* dummy fd to close fds via dup2 */
740 int fd = extract_fd (fh, 0); 681 int fd = s_fileno_croak (fh, 0);
741 dREQ; 682 dREQ;
742 683
743 if (close_pipe < 0) 684 if (close_pipe < 0)
744 { 685 {
745 int pipefd [2]; 686 int pipefd [2];
767 aio_write = EIO_WRITE 708 aio_write = EIO_WRITE
768 PROTOTYPE: $$$$$;$ 709 PROTOTYPE: $$$$$;$
769 PPCODE: 710 PPCODE:
770{ 711{
771 STRLEN svlen; 712 STRLEN svlen;
772 int fd = extract_fd (fh, ix == EIO_WRITE); 713 int fd = s_fileno_croak (fh, ix == EIO_WRITE);
773 char *svptr = SvPVbyte (data, svlen); 714 char *svptr = SvPVbyte (data, svlen);
774 UV len = SvUV (length); 715 UV len = SvUV (length);
775 716
776 if (dataoffset < 0) 717 if (dataoffset < 0)
777 dataoffset += svlen; 718 dataoffset += svlen;
832void 773void
833aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) 774aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef)
834 PROTOTYPE: $$$$;$ 775 PROTOTYPE: $$$$;$
835 PPCODE: 776 PPCODE:
836{ 777{
837 int ifd = extract_fd (in_fh , 0); 778 int ifd = s_fileno_croak (in_fh , 0);
838 int ofd = extract_fd (out_fh, 0); 779 int ofd = s_fileno_croak (out_fh, 1);
839 dREQ; 780 dREQ;
840 781
841 req->type = EIO_SENDFILE; 782 req->type = EIO_SENDFILE;
842 req->sv1 = newSVsv (out_fh); 783 req->sv1 = newSVsv (out_fh);
843 req->int1 = ofd; 784 req->int1 = ofd;
852void 793void
853aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) 794aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef)
854 PROTOTYPE: $$$;$ 795 PROTOTYPE: $$$;$
855 PPCODE: 796 PPCODE:
856{ 797{
857 int fd = extract_fd (fh, 0); 798 int fd = s_fileno_croak (fh, 0);
858 dREQ; 799 dREQ;
859 800
860 req->type = EIO_READAHEAD; 801 req->type = EIO_READAHEAD;
861 req->sv1 = newSVsv (fh); 802 req->sv1 = newSVsv (fh);
862 req->int1 = fd; 803 req->int1 = fd;
1138 1079
1139int 1080int
1140poll_fileno() 1081poll_fileno()
1141 PROTOTYPE: 1082 PROTOTYPE:
1142 CODE: 1083 CODE:
1143 RETVAL = respipe [0]; 1084 RETVAL = s_epipe_fd (&respipe);
1144 OUTPUT: 1085 OUTPUT:
1145 RETVAL 1086 RETVAL
1146 1087
1147int 1088int
1148poll_cb(...) 1089poll_cb(...)
1192 1133
1193int 1134int
1194fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) 1135fadvise (aio_rfd fh, off_t offset, off_t length, IV advice)
1195 PROTOTYPE: $$$$ 1136 PROTOTYPE: $$$$
1196 CODE: 1137 CODE:
1197{
1198 #if _XOPEN_SOURCE >= 600 1138#if _XOPEN_SOURCE >= 600 && !NO_FADVISE
1199 RETVAL = posix_fadvise (fh, offset, length, advice); 1139 RETVAL = posix_fadvise (fh, offset, length, advice);
1200 #else 1140#else
1201 RETVAL = errno = ENOSYS; 1141 RETVAL = errno = ENOSYS;
1202 #endif 1142#endif
1203}
1204 OUTPUT: 1143 OUTPUT:
1205 RETVAL 1144 RETVAL
1206 1145
1207ssize_t 1146ssize_t
1208sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) 1147sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count)
1231 if (GIMME_V != G_VOID) 1170 if (GIMME_V != G_VOID)
1232 XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); 1171 XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef);
1233 1172
1234 if (items > 1) 1173 if (items > 1)
1235 { 1174 {
1236 SV *cb_cv = get_cb (callback); 1175 SV *cb_cv =get_cb (callback);
1237 1176
1238 SvREFCNT_dec (req->callback); 1177 SvREFCNT_dec (req->callback);
1239 req->callback = SvREFCNT_inc (cb_cv); 1178 req->callback = SvREFCNT_inc (cb_cv);
1240 } 1179 }
1241} 1180}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines