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.120 by root, Sun May 11 19:17:09 2008 UTC vs.
Revision 1.128 by root, Tue Sep 30 14:07:59 2008 UTC

65# include <signal.h> 65# include <signal.h>
66# define EIO_STRUCT_DIRENT struct dirent 66# define EIO_STRUCT_DIRENT struct dirent
67 67
68#endif 68#endif
69 69
70/* perl stupidly overrides readdir and maybe others */
71/* with thread-unsafe versions, imagine that :( */
72#undef readdir
73#undef opendir
74#undef closedir
75
70#define EIO_STRUCT_STAT Stat_t 76#define EIO_STRUCT_STAT Stat_t
71 77
72/* use NV for 32 bit perls as it allows larger offsets */ 78/* use NV for 32 bit perls as it allows larger offsets */
73#if IVSIZE >= 8 79#if IVSIZE >= 8
74# define SvVAL64 SvIV 80# define SvVAL64 SvIV
75#else 81#else
76# define SvVAL64 SvNV 82# define SvVAL64 SvNV
77#endif 83#endif
78 84
85/*****************************************************************************/
86
87#if __GNUC__ >= 3
88# define expect(expr,value) __builtin_expect ((expr),(value))
89#else
90# define expect(expr,value) (expr)
91#endif
92
93#define expect_false(expr) expect ((expr) != 0, 0)
94#define expect_true(expr) expect ((expr) != 0, 1)
95
96/*****************************************************************************/
97
79static HV *stash; 98static HV *stash;
80typedef SV SV8; /* byte-sv, used for argument-checking */ 99typedef SV SV8; /* byte-sv, used for argument-checking */
81 100
82#define AIO_REQ_KLASS "IO::AIO::REQ" 101#define AIO_REQ_KLASS "IO::AIO::REQ"
83#define AIO_GRP_KLASS "IO::AIO::GRP" 102#define AIO_GRP_KLASS "IO::AIO::GRP"
84 103
85#define EIO_COMMON \ 104#define EIO_REQ_MEMBERS \
86 SV *callback; \ 105 SV *callback; \
87 SV *sv1, *sv2; \ 106 SV *sv1, *sv2; \
88 STRLEN stroffset; \ 107 STRLEN stroffset; \
89 SV *self 108 SV *self;
109
110#define EIO_NO_WRAPPERS 1
90 111
91#include "libeio/eio.h" 112#include "libeio/eio.h"
92 113
93static int req_invoke (eio_req *req); 114static int req_invoke (eio_req *req);
94#define EIO_FINISH(req) req_invoke (req) 115#define EIO_FINISH(req) req_invoke (req)
105 126
106typedef eio_req *aio_req; 127typedef eio_req *aio_req;
107typedef eio_req *aio_req_ornot; 128typedef eio_req *aio_req_ornot;
108 129
109static SV *on_next_submit; 130static SV *on_next_submit;
110static int next_pri = EIO_DEFAULT_PRI + EIO_PRI_BIAS; 131static int next_pri = EIO_PRI_DEFAULT;
111static int max_outstanding; 132static int max_outstanding;
112 133
113static int respipe_osf [2], respipe [2] = { -1, -1 }; 134static int respipe_osf [2], respipe [2] = { -1, -1 };
114 135
115static void req_destroy (aio_req req); 136static void req_destroy (aio_req req);
173 194
174static void req_submit (eio_req *req) 195static void req_submit (eio_req *req)
175{ 196{
176 eio_submit (req); 197 eio_submit (req);
177 198
178 if (on_next_submit) 199 if (expect_false (on_next_submit))
179 { 200 {
180 dSP; 201 dSP;
181 SV *cb = sv_2mortal (on_next_submit); 202 SV *cb = sv_2mortal (on_next_submit);
182 203
183 on_next_submit = 0; 204 on_next_submit = 0;
193 dSP; 214 dSP;
194 215
195 if (req->flags & FLAG_SV2_RO_OFF) 216 if (req->flags & FLAG_SV2_RO_OFF)
196 SvREADONLY_off (req->sv2); 217 SvREADONLY_off (req->sv2);
197 218
198 if (!EIO_CANCELLED (req) && SvOK (req->callback)) 219 if (!EIO_CANCELLED (req) && req->callback)
199 { 220 {
200 ENTER; 221 ENTER;
201 SAVETMPS; 222 SAVETMPS;
202 PUSHMARK (SP); 223 PUSHMARK (SP);
203 EXTEND (SP, 1); 224 EXTEND (SP, 1);
410 X_UNLOCK (reslock); 431 X_UNLOCK (reslock);
411 432
412 if (size) 433 if (size)
413 return; 434 return;
414 435
415 maybe_start_thread (); 436 etp_maybe_start_thread ();
416 437
417 FD_ZERO (&rfd); 438 FD_ZERO (&rfd);
418 FD_SET (respipe [0], &rfd); 439 FD_SET (respipe [0], &rfd);
419 440
420 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); 441 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
421 } 442 }
422} 443}
423 444
424static int poll_cb (void) 445static int poll_cb (void)
425{ 446{
426 int res; 447 for (;;)
427
428 do
429 { 448 {
430 res = eio_poll (); 449 int res = eio_poll ();
431 450
432 if (res > 0) 451 if (res > 0)
433 croak (0); 452 croak (0);
453
454 if (!max_outstanding || max_outstanding > eio_nreqs ())
455 return res;
456
457 poll_wait ();
434 } 458 }
435 while (max_outstanding && max_outstanding <= eio_nreqs ());
436
437 return res;
438} 459}
439 460
440static void atfork_child (void) 461static void atfork_child (void)
441{ 462{
442 create_respipe (); 463 create_respipe ();
443} 464}
444 465
466static SV *
467get_cb (SV *cb_sv)
468{
469 HV *st;
470 GV *gvp;
471 CV *cv;
472
473 if (!SvOK (cb_sv))
474 return 0;
475
476 cv = sv_2cv (cb_sv, &st, &gvp, 0);
477
478 if (!cv)
479 croak ("IO::AIO watcher callback must be undef or a CODE reference");
480
481 return (SV *)cv;
482}
483
445#define dREQ \ 484#define dREQ \
485 SV *cb_cv; \
446 aio_req req; \ 486 aio_req req; \
447 int req_pri = next_pri; \ 487 int req_pri = next_pri; \
448 next_pri = EIO_DEFAULT_PRI + EIO_PRI_BIAS; \ 488 next_pri = EIO_PRI_DEFAULT; \
449 \ 489 \
450 if (SvOK (callback) && !SvROK (callback)) \ 490 cb_cv = get_cb (callback); \
451 croak ("callback must be undef or of reference type"); \
452 \ 491 \
453 Newz (0, req, 1, eio_req); \ 492 Newz (0, req, 1, eio_req); \
454 if (!req) \ 493 if (!req) \
455 croak ("out of memory during eio_req allocation"); \ 494 croak ("out of memory during eio_req allocation"); \
456 \ 495 \
457 req->callback = newSVsv (callback); \ 496 req->callback = SvREFCNT_inc (cb_cv); \
458 req->pri = req_pri 497 req->pri = req_pri
459 498
460#define REQ_SEND \ 499#define REQ_SEND \
500 PUTBACK; \
461 req_submit (req); \ 501 req_submit (req); \
502 SPAGAIN; \
462 \ 503 \
463 if (GIMME_V != G_VOID) \ 504 if (GIMME_V != G_VOID) \
464 XPUSHs (req_sv (req, AIO_REQ_KLASS)); 505 XPUSHs (req_sv (req, AIO_REQ_KLASS));
465 506
466MODULE = IO::AIO PACKAGE = IO::AIO 507MODULE = IO::AIO PACKAGE = IO::AIO
474 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); 515 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
475 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); 516 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
476 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); 517 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
477 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); 518 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
478 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); 519 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
479#ifdef _WIN32 520#ifndef _WIN32
480 X_MUTEX_CHECK (wrklock);
481 X_MUTEX_CHECK (reslock);
482 X_MUTEX_CHECK (reqlock);
483 X_MUTEX_CHECK (reqwait);
484 X_MUTEX_CHECK (preadwritelock);
485 X_MUTEX_CHECK (readdirlock);
486
487 X_COND_CHECK (reqwait);
488#else
489 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); 521 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
490#endif 522#endif
491 523
492 create_respipe (); 524 create_respipe ();
493 525
895{ 927{
896 dREQ; 928 dREQ;
897 929
898 req->type = EIO_GROUP; 930 req->type = EIO_GROUP;
899 931
900 eio_submit (req); 932 req_submit (req);
901 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 933 XPUSHs (req_sv (req, AIO_GRP_KLASS));
902} 934}
903 935
904void 936void
905aio_nop (SV *callback=&PL_sv_undef) 937aio_nop (SV *callback=&PL_sv_undef)
917 949
918int 950int
919aioreq_pri (int pri = 0) 951aioreq_pri (int pri = 0)
920 PROTOTYPE: ;$ 952 PROTOTYPE: ;$
921 CODE: 953 CODE:
922 RETVAL = next_pri - EIO_PRI_BIAS; 954 RETVAL = next_pri;
923 if (items > 0) 955 if (items > 0)
924 { 956 {
925 if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN; 957 if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN;
926 if (pri > EIO_PRI_MAX) pri = EIO_PRI_MAX; 958 if (pri > EIO_PRI_MAX) pri = EIO_PRI_MAX;
927 next_pri = pri + EIO_PRI_BIAS; 959 next_pri = pri;
928 } 960 }
929 OUTPUT: 961 OUTPUT:
930 RETVAL 962 RETVAL
931 963
932void 964void
933aioreq_nice (int nice = 0) 965aioreq_nice (int nice = 0)
934 CODE: 966 CODE:
935 nice = next_pri - nice; 967 nice = next_pri - nice;
936 if (nice < EIO_PRI_MIN) nice = EIO_PRI_MIN; 968 if (nice < EIO_PRI_MIN) nice = EIO_PRI_MIN;
937 if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX; 969 if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX;
938 next_pri = nice + EIO_PRI_BIAS; 970 next_pri = nice;
939 971
940void 972void
941flush () 973flush ()
942 PROTOTYPE: 974 PROTOTYPE:
943 CODE: 975 CODE:
1004 1036
1005int 1037int
1006nthreads() 1038nthreads()
1007 PROTOTYPE: 1039 PROTOTYPE:
1008 CODE: 1040 CODE:
1009 if (WORDACCESS_UNSAFE) X_LOCK (wrklock); 1041 RETVAL = eio_nthreads ();
1010 RETVAL = started;
1011 if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock);
1012 OUTPUT: 1042 OUTPUT:
1013 RETVAL 1043 RETVAL
1014 1044
1015void _on_next_submit (SV *cb) 1045void _on_next_submit (SV *cb)
1016 CODE: 1046 CODE:
1026 CODE: 1056 CODE:
1027 eio_cancel (req); 1057 eio_cancel (req);
1028 1058
1029void 1059void
1030cb (aio_req_ornot req, SV *callback=&PL_sv_undef) 1060cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1031 CODE: 1061 PPCODE:
1062{
1063 if (GIMME_V != G_VOID)
1064 XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef);
1065
1066 if (items > 1)
1067 {
1068 SV *cb_cv = get_cb (callback);
1069
1032 SvREFCNT_dec (req->callback); 1070 SvREFCNT_dec (req->callback);
1033 req->callback = newSVsv (callback); 1071 req->callback = SvREFCNT_inc (cb_cv);
1072 }
1073}
1034 1074
1035MODULE = IO::AIO PACKAGE = IO::AIO::GRP 1075MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1036 1076
1037void 1077void
1038add (aio_req grp, ...) 1078add (aio_req grp, ...)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines