ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.238 by root, Sat May 31 12:10:55 2008 UTC vs.
Revision 1.253 by root, Fri Nov 7 20:12:26 2008 UTC

4#define PERL_EXT 4#define PERL_EXT
5 5
6#include "EXTERN.h" 6#include "EXTERN.h"
7#include "perl.h" 7#include "perl.h"
8#include "XSUB.h" 8#include "XSUB.h"
9#include "perliol.h"
9 10
10#include "patchlevel.h" 11#include "patchlevel.h"
11 12
12#include <stdio.h> 13#include <stdio.h>
13#include <errno.h> 14#include <errno.h>
80# ifndef IS_PADCONST 81# ifndef IS_PADCONST
81# define IS_PADCONST(v) 0 82# define IS_PADCONST(v) 0
82# endif 83# endif
83#endif 84#endif
84 85
86/* 5.11 */
87#ifndef CxHASARGS
88# define CxHASARGS(cx) (cx)->blk_sub.hasargs
89#endif
90
91/* 5.10.0 */
92#ifndef SvREFCNT_inc_NN
93# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
94#endif
95
85/* 5.8.8 */ 96/* 5.8.8 */
86#ifndef GV_NOTQUAL 97#ifndef GV_NOTQUAL
87# define GV_NOTQUAL 0 98# define GV_NOTQUAL 0
88#endif 99#endif
89#ifndef newSV 100#ifndef newSV
90# define newSV(l) NEWSV(0,l) 101# define newSV(l) NEWSV(0,l)
91#endif
92
93/* 5.11 */
94#ifndef CxHASARGS
95# define CxHASARGS(cx) (cx)->blk_sub.hasargs
96#endif 102#endif
97 103
98/* 5.8.7 */ 104/* 5.8.7 */
99#ifndef SvRV_set 105#ifndef SvRV_set
100# define SvRV_set(s,v) SvRV(s) = (v) 106# define SvRV_set(s,v) SvRV(s) = (v)
136#define NOINLINE attribute ((noinline)) 142#define NOINLINE attribute ((noinline))
137 143
138#include "CoroAPI.h" 144#include "CoroAPI.h"
139 145
140#ifdef USE_ITHREADS 146#ifdef USE_ITHREADS
141static perl_mutex coro_mutex; 147static perl_mutex coro_lock;
142# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 148# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
143# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 149# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
144#else 150#else
145# define LOCK (void)0 151# define LOCK (void)0
146# define UNLOCK (void)0 152# define UNLOCK (void)0
147#endif 153#endif
148 154
149/* helper storage struct for Coro::AIO */ 155/* helper storage struct for Coro::AIO */
150struct io_state 156struct io_state
151{ 157{
158 AV *res;
152 int errorno; 159 int errorno;
153 I32 laststype; 160 I32 laststype;
154 int laststatval; 161 int laststatval;
155 Stat_t statcache; 162 Stat_t statcache;
156}; 163};
157 164
165static double (*nvtime)(); /* so why doesn't it take void? */
166
158static size_t coro_stacksize = CORO_STACKSIZE; 167static size_t coro_stacksize = CORO_STACKSIZE;
159static struct CoroAPI coroapi; 168static struct CoroAPI coroapi;
160static AV *main_mainstack; /* used to differentiate between $main and others */ 169static AV *main_mainstack; /* used to differentiate between $main and others */
161static JMPENV *main_top_env; 170static JMPENV *main_top_env;
162static HV *coro_state_stash, *coro_stash; 171static HV *coro_state_stash, *coro_stash;
163static volatile SV *coro_mortal; /* will be freed after next transfer */ 172static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
164 173
165static GV *irsgv; /* $/ */ 174static GV *irsgv; /* $/ */
166static GV *stdoutgv; /* *STDOUT */ 175static GV *stdoutgv; /* *STDOUT */
167static SV *rv_diehook; 176static SV *rv_diehook;
168static SV *rv_warnhook; 177static SV *rv_warnhook;
321 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 330 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
322#endif 331#endif
323 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 332 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
324 --AvFILLp (padlist); 333 --AvFILLp (padlist);
325 334
326 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 335 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
327 av_store (newpadlist, 1, (SV *)newpad); 336 av_store (newpadlist, 1, (SV *)newpad);
328 337
329 return newpadlist; 338 return newpadlist;
330} 339}
331 340
361 370
362 /* casting is fun. */ 371 /* casting is fun. */
363 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 372 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
364 free_padlist (aTHX_ padlist); 373 free_padlist (aTHX_ padlist);
365 374
375 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
376
366 return 0; 377 return 0;
367} 378}
368 379
369#define CORO_MAGIC_type_cv PERL_MAGIC_ext 380#define CORO_MAGIC_type_cv PERL_MAGIC_ext
370#define CORO_MAGIC_type_state PERL_MAGIC_ext 381#define CORO_MAGIC_type_state PERL_MAGIC_ext
372static MGVTBL coro_cv_vtbl = { 383static MGVTBL coro_cv_vtbl = {
373 0, 0, 0, 0, 384 0, 0, 0, 0,
374 coro_cv_free 385 coro_cv_free
375}; 386};
376 387
377#define CORO_MAGIC(sv,type) \ 388#define CORO_MAGIC(sv, type) \
378 SvMAGIC (sv) \ 389 SvMAGIC (sv) \
379 ? SvMAGIC (sv)->mg_type == type \ 390 ? SvMAGIC (sv)->mg_type == type \
380 ? SvMAGIC (sv) \ 391 ? SvMAGIC (sv) \
381 : mg_find (sv, type) \ 392 : mg_find (sv, type) \
382 : 0 393 : 0
383 394
384#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 395#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
385#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) 396#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
386 397
387static struct coro * 398static struct coro *
420 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 431 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
421 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 432 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
422 else 433 else
423 { 434 {
424#if CORO_PREFER_PERL_FUNCTIONS 435#if CORO_PREFER_PERL_FUNCTIONS
425 /* this is probably cleaner, but also slower? */ 436 /* this is probably cleaner? but also slower! */
437 /* in practise, it seems to be less stable */
426 CV *cp = Perl_cv_clone (cv); 438 CV *cp = Perl_cv_clone (cv);
427 CvPADLIST (cv) = CvPADLIST (cp); 439 CvPADLIST (cv) = CvPADLIST (cp);
428 CvPADLIST (cp) = 0; 440 CvPADLIST (cp) = 0;
429 SvREFCNT_dec (cp); 441 SvREFCNT_dec (cp);
430#else 442#else
616 628
617/* 629/*
618 * destroy the stacks, the callchain etc... 630 * destroy the stacks, the callchain etc...
619 */ 631 */
620static void 632static void
621coro_destroy_stacks (pTHX) 633coro_destruct_stacks (pTHX)
622{ 634{
623 while (PL_curstackinfo->si_next) 635 while (PL_curstackinfo->si_next)
624 PL_curstackinfo = PL_curstackinfo->si_next; 636 PL_curstackinfo = PL_curstackinfo->si_next;
625 637
626 while (PL_curstackinfo) 638 while (PL_curstackinfo)
663 #undef VAR 675 #undef VAR
664 } 676 }
665 else 677 else
666 slot = coro->slot; 678 slot = coro->slot;
667 679
680 if (slot)
681 {
668 rss += sizeof (slot->curstackinfo); 682 rss += sizeof (slot->curstackinfo);
669 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 683 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
670 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 684 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *);
671 rss += slot->tmps_max * sizeof (SV *); 685 rss += slot->tmps_max * sizeof (SV *);
672 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); 686 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
673 rss += slot->scopestack_max * sizeof (I32); 687 rss += slot->scopestack_max * sizeof (I32);
674 rss += slot->savestack_max * sizeof (ANY); 688 rss += slot->savestack_max * sizeof (ANY);
675 689
676#if !PERL_VERSION_ATLEAST (5,10,0) 690#if !PERL_VERSION_ATLEAST (5,10,0)
677 rss += slot->retstack_max * sizeof (OP *); 691 rss += slot->retstack_max * sizeof (OP *);
678#endif 692#endif
693 }
679 } 694 }
680 695
681 return rss; 696 return rss;
682} 697}
683 698
684/** coroutine stack handling ************************************************/ 699/** coroutine stack handling ************************************************/
685 700
686static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 701static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
687static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); 702static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
703static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
688 704
689/* apparently < 5.8.8 */ 705/* apparently < 5.8.8 */
690#undef MgPV_nolen_const
691#ifndef MgPV_nolen_const 706#ifndef MgPV_nolen_const
692#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ 707#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
693 SvPV_nolen_const((SV*)((mg)->mg_ptr)) : \ 708 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
694 (const char*)(mg)->mg_ptr) 709 (const char*)(mg)->mg_ptr)
695#endif 710#endif
696 711
697/* 712/*
698 * This overrides the default magic get method of %SIG elements. 713 * This overrides the default magic get method of %SIG elements.
708{ 723{
709 const char *s = MgPV_nolen_const (mg); 724 const char *s = MgPV_nolen_const (mg);
710 725
711 if (*s == '_') 726 if (*s == '_')
712 { 727 {
713 if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0; 728 SV **svp = 0;
714 if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0; 729
730 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
731 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
732
733 if (svp)
734 {
735 sv_setsv (sv, *svp ? *svp : &PL_sv_undef);
736 return 0;
737 }
715 } 738 }
716 739
717 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; 740 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
741}
742
743static int
744coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
745{
746 const char *s = MgPV_nolen_const (mg);
747
748 if (*s == '_')
749 {
750 SV **svp = 0;
751
752 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
753 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
754
755 if (svp)
756 {
757 SV *old = *svp;
758 *svp = 0;
759 SvREFCNT_dec (old);
760 return 0;
761 }
762 }
763
764 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0;
718} 765}
719 766
720static int 767static int
721coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) 768coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
722{ 769{
769 GvSV (PL_defgv) = newSV (0); 816 GvSV (PL_defgv) = newSV (0);
770 GvAV (PL_defgv) = coro->args; coro->args = 0; 817 GvAV (PL_defgv) = coro->args; coro->args = 0;
771 GvSV (PL_errgv) = newSV (0); 818 GvSV (PL_errgv) = newSV (0);
772 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 819 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
773 PL_rs = newSVsv (GvSV (irsgv)); 820 PL_rs = newSVsv (GvSV (irsgv));
774 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 821 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
775 822
776 { 823 {
777 dSP; 824 dSP;
778 LOGOP myop; 825 LOGOP myop;
779 826
796 */ 843 */
797 ENTER; 844 ENTER;
798} 845}
799 846
800static void 847static void
801coro_destroy (pTHX_ struct coro *coro) 848coro_destruct (pTHX_ struct coro *coro)
802{ 849{
803 if (!IN_DESTRUCT) 850 if (!IN_DESTRUCT)
804 { 851 {
805 /* restore all saved variables and stuff */ 852 /* restore all saved variables and stuff */
806 LEAVE_SCOPE (0); 853 LEAVE_SCOPE (0);
828 SvREFCNT_dec (PL_warnhook); 875 SvREFCNT_dec (PL_warnhook);
829 876
830 SvREFCNT_dec (coro->saved_deffh); 877 SvREFCNT_dec (coro->saved_deffh);
831 SvREFCNT_dec (coro->throw); 878 SvREFCNT_dec (coro->throw);
832 879
833 coro_destroy_stacks (aTHX); 880 coro_destruct_stacks (aTHX);
834} 881}
835 882
836static void 883static void
837free_coro_mortal (pTHX) 884free_coro_mortal (pTHX)
838{ 885{
875 : cx->blk_gimme == G_SCALAR ? bot + 1 922 : cx->blk_gimme == G_SCALAR ? bot + 1
876 : bot; 923 : bot;
877 924
878 av_extend (av, top - bot); 925 av_extend (av, top - bot);
879 while (bot < top) 926 while (bot < top)
880 av_push (av, SvREFCNT_inc (*bot++)); 927 av_push (av, SvREFCNT_inc_NN (*bot++));
881 928
882 PL_runops = RUNOPS_DEFAULT; 929 PL_runops = RUNOPS_DEFAULT;
883 ENTER; 930 ENTER;
884 SAVETMPS; 931 SAVETMPS;
885 EXTEND (SP, 3); 932 EXTEND (SP, 3);
1033 coro_cctx *cctx; 1080 coro_cctx *cctx;
1034 void *stack_start; 1081 void *stack_start;
1035 size_t stack_size; 1082 size_t stack_size;
1036 1083
1037 ++cctx_count; 1084 ++cctx_count;
1038
1039 Newz (0, cctx, 1, coro_cctx); 1085 Newz (0, cctx, 1, coro_cctx);
1040 1086
1041#if HAVE_MMAP 1087#if HAVE_MMAP
1042 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1088 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1043 /* mmap supposedly does allocate-on-write for us */ 1089 /* mmap supposedly does allocate-on-write for us */
1079{ 1125{
1080 if (!cctx) 1126 if (!cctx)
1081 return; 1127 return;
1082 1128
1083 --cctx_count; 1129 --cctx_count;
1130 coro_destroy (&cctx->cctx);
1084 1131
1132 /* coro_transfer creates new, empty cctx's */
1133 if (cctx->sptr)
1134 {
1085#if CORO_USE_VALGRIND 1135#if CORO_USE_VALGRIND
1086 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1136 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1087#endif 1137#endif
1088 1138
1089#if HAVE_MMAP 1139#if HAVE_MMAP
1090 if (cctx->flags & CC_MAPPED) 1140 if (cctx->flags & CC_MAPPED)
1091 munmap (cctx->sptr, cctx->ssize); 1141 munmap (cctx->sptr, cctx->ssize);
1092 else 1142 else
1093#endif 1143#endif
1094 Safefree (cctx->sptr); 1144 Safefree (cctx->sptr);
1145 }
1095 1146
1096 Safefree (cctx); 1147 Safefree (cctx);
1097} 1148}
1098 1149
1099/* wether this cctx should be destructed */ 1150/* wether this cctx should be destructed */
1118} 1169}
1119 1170
1120static void 1171static void
1121cctx_put (coro_cctx *cctx) 1172cctx_put (coro_cctx *cctx)
1122{ 1173{
1174 assert (("cctx_put called on non-initialised cctx", cctx->sptr));
1175
1123 /* free another cctx if overlimit */ 1176 /* free another cctx if overlimit */
1124 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1177 if (expect_false (cctx_idle >= MAX_IDLE_CCTX))
1125 { 1178 {
1126 coro_cctx *first = cctx_first; 1179 coro_cctx *first = cctx_first;
1127 cctx_first = first->next; 1180 cctx_first = first->next;
1161/* always use the TRANSFER macro */ 1214/* always use the TRANSFER macro */
1162static void NOINLINE 1215static void NOINLINE
1163transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1216transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1164{ 1217{
1165 dSTACKLEVEL; 1218 dSTACKLEVEL;
1166 static volatile int has_throw;
1167 1219
1168 /* sometimes transfer is only called to set idle_sp */ 1220 /* sometimes transfer is only called to set idle_sp */
1169 if (expect_false (!next)) 1221 if (expect_false (!next))
1170 { 1222 {
1171 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1223 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1172 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1224 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1173 } 1225 }
1174 else if (expect_true (prev != next)) 1226 else if (expect_true (prev != next))
1175 { 1227 {
1228 static volatile int has_throw;
1176 coro_cctx *prev__cctx; 1229 coro_cctx *prev__cctx;
1177 1230
1178 if (expect_false (prev->flags & CF_NEW)) 1231 if (expect_false (prev->flags & CF_NEW))
1179 { 1232 {
1180 /* create a new empty context */ 1233 /* create a new empty/source context */
1234 ++cctx_count;
1181 Newz (0, prev->cctx, 1, coro_cctx); 1235 New (0, prev->cctx, 1, coro_cctx);
1236 prev->cctx->sptr = 0;
1237 coro_create (&prev->cctx->cctx, 0, 0, 0, 0);
1238
1182 prev->flags &= ~CF_NEW; 1239 prev->flags &= ~CF_NEW;
1183 prev->flags |= CF_RUNNING; 1240 prev->flags |= CF_RUNNING;
1184 } 1241 }
1185 1242
1186 prev->flags &= ~CF_RUNNING; 1243 prev->flags &= ~CF_RUNNING;
1293 croak ("FATAL: tried to destroy currently running coroutine"); 1350 croak ("FATAL: tried to destroy currently running coroutine");
1294 1351
1295 save_perl (aTHX_ &temp); 1352 save_perl (aTHX_ &temp);
1296 load_perl (aTHX_ coro); 1353 load_perl (aTHX_ coro);
1297 1354
1298 coro_destroy (aTHX_ coro); 1355 coro_destruct (aTHX_ coro);
1299 1356
1300 load_perl (aTHX_ &temp); 1357 load_perl (aTHX_ &temp);
1301 1358
1302 coro->slot = 0; 1359 coro->slot = 0;
1303 } 1360 }
1409 LOCK; 1466 LOCK;
1410 1467
1411 sv_hook = coro_nready ? 0 : coro_readyhook; 1468 sv_hook = coro_nready ? 0 : coro_readyhook;
1412 xs_hook = coro_nready ? 0 : coroapi.readyhook; 1469 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1413 1470
1414 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1471 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1415 ++coro_nready; 1472 ++coro_nready;
1416 1473
1417 UNLOCK; 1474 UNLOCK;
1418 1475
1419 if (sv_hook) 1476 if (sv_hook)
1591 else 1648 else
1592 coro->slot->runops = RUNOPS_DEFAULT; 1649 coro->slot->runops = RUNOPS_DEFAULT;
1593 } 1650 }
1594} 1651}
1595 1652
1653static int
1654coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1655{
1656 AV *padlist;
1657 AV *av = (AV *)mg->mg_obj;
1658
1659 abort ();
1660
1661 return 0;
1662}
1663
1664static MGVTBL coro_gensub_vtbl = {
1665 0, 0, 0, 0,
1666 coro_gensub_free
1667};
1668
1669/*****************************************************************************/
1670/* PerlIO::cede */
1671
1672typedef struct
1673{
1674 PerlIOBuf base;
1675 NV next, every;
1676} PerlIOCede;
1677
1678static IV
1679PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1680{
1681 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1682
1683 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1684 self->next = nvtime () + self->every;
1685
1686 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1687}
1688
1689static SV *
1690PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
1691{
1692 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1693
1694 return newSVnv (self->every);
1695}
1696
1697static IV
1698PerlIOCede_flush (pTHX_ PerlIO *f)
1699{
1700 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1701 double now = nvtime ();
1702
1703 if (now >= self->next)
1704 {
1705 api_cede ();
1706 self->next = now + self->every;
1707 }
1708
1709 return PerlIOBuf_flush (aTHX_ f);
1710}
1711
1712static PerlIO_funcs PerlIO_cede =
1713{
1714 sizeof(PerlIO_funcs),
1715 "cede",
1716 sizeof(PerlIOCede),
1717 PERLIO_K_DESTRUCT | PERLIO_K_RAW,
1718 PerlIOCede_pushed,
1719 PerlIOBuf_popped,
1720 PerlIOBuf_open,
1721 PerlIOBase_binmode,
1722 PerlIOCede_getarg,
1723 PerlIOBase_fileno,
1724 PerlIOBuf_dup,
1725 PerlIOBuf_read,
1726 PerlIOBuf_unread,
1727 PerlIOBuf_write,
1728 PerlIOBuf_seek,
1729 PerlIOBuf_tell,
1730 PerlIOBuf_close,
1731 PerlIOCede_flush,
1732 PerlIOBuf_fill,
1733 PerlIOBase_eof,
1734 PerlIOBase_error,
1735 PerlIOBase_clearerr,
1736 PerlIOBase_setlinebuf,
1737 PerlIOBuf_get_base,
1738 PerlIOBuf_bufsiz,
1739 PerlIOBuf_get_ptr,
1740 PerlIOBuf_get_cnt,
1741 PerlIOBuf_set_ptrcnt,
1742};
1743
1744
1596MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1745MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1597 1746
1598PROTOTYPES: DISABLE 1747PROTOTYPES: DISABLE
1599 1748
1600BOOT: 1749BOOT:
1601{ 1750{
1602#ifdef USE_ITHREADS 1751#ifdef USE_ITHREADS
1603 MUTEX_INIT (&coro_mutex); 1752 MUTEX_INIT (&coro_lock);
1604#endif 1753#endif
1605 BOOT_PAGESIZE; 1754 BOOT_PAGESIZE;
1606 1755
1607 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1756 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1608 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1757 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1609 1758
1610 orig_sigelem_get = PL_vtbl_sigelem.svt_get; 1759 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1611 PL_vtbl_sigelem.svt_get = coro_sigelem_get; 1760 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1612 orig_sigelem_set = PL_vtbl_sigelem.svt_set; 1761 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1613 PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1614 1762
1615 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); 1763 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1616 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); 1764 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1617 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); 1765 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1618 1766
1630 main_top_env = main_top_env->je_prev; 1778 main_top_env = main_top_env->je_prev;
1631 1779
1632 coroapi.ver = CORO_API_VERSION; 1780 coroapi.ver = CORO_API_VERSION;
1633 coroapi.rev = CORO_API_REVISION; 1781 coroapi.rev = CORO_API_REVISION;
1634 coroapi.transfer = api_transfer; 1782 coroapi.transfer = api_transfer;
1783
1784 {
1785 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1786
1787 if (!svp) croak ("Time::HiRes is required");
1788 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1789
1790 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1791 }
1635 1792
1636 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1793 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1637} 1794}
1638 1795
1639SV * 1796SV *
1687 ta.next = 0; 1844 ta.next = 0;
1688 break; 1845 break;
1689 1846
1690 case 1: 1847 case 1:
1691 if (items != 2) 1848 if (items != 2)
1692 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1849 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1693 1850
1694 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1851 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1695 break; 1852 break;
1696 1853
1697 case 2: 1854 case 2:
1767call (Coro::State coro, SV *coderef) 1924call (Coro::State coro, SV *coderef)
1768 ALIAS: 1925 ALIAS:
1769 eval = 1 1926 eval = 1
1770 CODE: 1927 CODE:
1771{ 1928{
1772 if (coro->mainstack) 1929 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1773 { 1930 {
1774 struct coro temp; 1931 struct coro temp;
1775 1932
1776 if (!(coro->flags & CF_RUNNING)) 1933 if (!(coro->flags & CF_RUNNING))
1777 { 1934 {
1859force_cctx () 2016force_cctx ()
1860 CODE: 2017 CODE:
1861 struct coro *coro = SvSTATE (coro_current); 2018 struct coro *coro = SvSTATE (coro_current);
1862 coro->cctx->idle_sp = 0; 2019 coro->cctx->idle_sp = 0;
1863 2020
1864MODULE = Coro::State PACKAGE = Coro
1865
1866BOOT:
1867{
1868 int i;
1869
1870 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
1871 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
1872 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1873
1874 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1875 SvREADONLY_on (coro_current);
1876
1877 coro_stash = gv_stashpv ("Coro", TRUE);
1878
1879 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1880 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1881 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1882 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1883 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1884 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1885
1886 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1887 coro_ready[i] = newAV ();
1888
1889 {
1890 SV *sv = perl_get_sv ("Coro::API", TRUE);
1891 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
1892
1893 coroapi.schedule = api_schedule;
1894 coroapi.cede = api_cede;
1895 coroapi.cede_notself = api_cede_notself;
1896 coroapi.ready = api_ready;
1897 coroapi.is_ready = api_is_ready;
1898 coroapi.nready = &coro_nready;
1899 coroapi.current = coro_current;
1900
1901 GCoroAPI = &coroapi;
1902 sv_setiv (sv, (IV)&coroapi);
1903 SvREADONLY_on (sv);
1904 }
1905}
1906
1907void
1908_set_current (SV *current)
1909 PROTOTYPE: $
1910 CODE:
1911 SvREFCNT_dec (SvRV (coro_current));
1912 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1913
1914void
1915_set_readyhook (SV *hook)
1916 PROTOTYPE: $
1917 CODE:
1918 LOCK;
1919 SvREFCNT_dec (coro_readyhook);
1920 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
1921 UNLOCK;
1922
1923int
1924prio (Coro::State coro, int newprio = 0)
1925 ALIAS:
1926 nice = 1
1927 CODE:
1928{
1929 RETVAL = coro->prio;
1930
1931 if (items > 1)
1932 {
1933 if (ix)
1934 newprio = coro->prio - newprio;
1935
1936 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1937 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1938
1939 coro->prio = newprio;
1940 }
1941}
1942 OUTPUT:
1943 RETVAL
1944
1945SV *
1946ready (SV *self)
1947 PROTOTYPE: $
1948 CODE:
1949 RETVAL = boolSV (api_ready (self));
1950 OUTPUT:
1951 RETVAL
1952
1953int
1954nready (...)
1955 PROTOTYPE:
1956 CODE:
1957 RETVAL = coro_nready;
1958 OUTPUT:
1959 RETVAL
1960
1961void
1962throw (Coro::State self, SV *throw = &PL_sv_undef)
1963 PROTOTYPE: $;$
1964 CODE:
1965 SvREFCNT_dec (self->throw);
1966 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
1967
1968void 2021void
1969swap_defsv (Coro::State self) 2022swap_defsv (Coro::State self)
1970 PROTOTYPE: $ 2023 PROTOTYPE: $
1971 ALIAS: 2024 ALIAS:
1972 swap_defav = 1 2025 swap_defav = 1
1979 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 2032 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
1980 2033
1981 SV *tmp = *src; *src = *dst; *dst = tmp; 2034 SV *tmp = *src; *src = *dst; *dst = tmp;
1982 } 2035 }
1983 2036
2037MODULE = Coro::State PACKAGE = Coro
2038
2039BOOT:
2040{
2041 int i;
2042
2043 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2044 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2045 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2046
2047 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
2048 SvREADONLY_on (coro_current);
2049
2050 coro_stash = gv_stashpv ("Coro", TRUE);
2051
2052 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2053 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2054 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
2055 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
2056 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
2057 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
2058
2059 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2060 coro_ready[i] = newAV ();
2061
2062 {
2063 SV *sv = perl_get_sv ("Coro::API", TRUE);
2064 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2065
2066 coroapi.schedule = api_schedule;
2067 coroapi.cede = api_cede;
2068 coroapi.cede_notself = api_cede_notself;
2069 coroapi.ready = api_ready;
2070 coroapi.is_ready = api_is_ready;
2071 coroapi.nready = &coro_nready;
2072 coroapi.current = coro_current;
2073
2074 GCoroAPI = &coroapi;
2075 sv_setiv (sv, (IV)&coroapi);
2076 SvREADONLY_on (sv);
2077 }
2078}
2079
2080void
2081_set_current (SV *current)
2082 PROTOTYPE: $
2083 CODE:
2084 SvREFCNT_dec (SvRV (coro_current));
2085 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2086
2087void
2088_set_readyhook (SV *hook)
2089 PROTOTYPE: $
2090 CODE:
2091 LOCK;
2092 SvREFCNT_dec (coro_readyhook);
2093 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2094 UNLOCK;
2095
2096int
2097prio (Coro::State coro, int newprio = 0)
2098 ALIAS:
2099 nice = 1
2100 CODE:
2101{
2102 RETVAL = coro->prio;
2103
2104 if (items > 1)
2105 {
2106 if (ix)
2107 newprio = coro->prio - newprio;
2108
2109 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
2110 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
2111
2112 coro->prio = newprio;
2113 }
2114}
2115 OUTPUT:
2116 RETVAL
2117
2118SV *
2119ready (SV *self)
2120 PROTOTYPE: $
2121 CODE:
2122 RETVAL = boolSV (api_ready (self));
2123 OUTPUT:
2124 RETVAL
2125
2126int
2127nready (...)
2128 PROTOTYPE:
2129 CODE:
2130 RETVAL = coro_nready;
2131 OUTPUT:
2132 RETVAL
2133
2134void
2135throw (Coro::State self, SV *throw = &PL_sv_undef)
2136 PROTOTYPE: $;$
2137 CODE:
2138 SvREFCNT_dec (self->throw);
2139 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2140
1984# for async_pool speedup 2141# for async_pool speedup
1985void 2142void
1986_pool_1 (SV *cb) 2143_pool_1 (SV *cb)
1987 CODE: 2144 CODE:
1988{ 2145{
1993 AV *invoke_av; 2150 AV *invoke_av;
1994 int i, len; 2151 int i, len;
1995 2152
1996 if (!invoke) 2153 if (!invoke)
1997 { 2154 {
1998 SvREFCNT_dec (PL_diehook); PL_diehook = 0; 2155 SV *old = PL_diehook;
2156 PL_diehook = 0;
2157 SvREFCNT_dec (old);
1999 croak ("\3async_pool terminate\2\n"); 2158 croak ("\3async_pool terminate\2\n");
2000 } 2159 }
2001 2160
2002 SvREFCNT_dec (coro->saved_deffh); 2161 SvREFCNT_dec (coro->saved_deffh);
2003 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2162 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2004 2163
2005 hv_store (hv, "desc", sizeof ("desc") - 1, 2164 hv_store (hv, "desc", sizeof ("desc") - 1,
2006 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2165 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2007 2166
2008 invoke_av = (AV *)SvRV (invoke); 2167 invoke_av = (AV *)SvRV (invoke);
2012 2171
2013 if (len > 0) 2172 if (len > 0)
2014 { 2173 {
2015 av_fill (defav, len - 1); 2174 av_fill (defav, len - 1);
2016 for (i = 0; i < len; ++i) 2175 for (i = 0; i < len; ++i)
2017 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2176 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2018 } 2177 }
2019 2178
2020 SvREFCNT_dec (invoke); 2179 SvREFCNT_dec (invoke);
2021} 2180}
2022 2181
2032 coro->saved_deffh = 0; 2191 coro->saved_deffh = 0;
2033 2192
2034 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2193 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
2035 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2194 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2036 { 2195 {
2037 SvREFCNT_dec (PL_diehook); PL_diehook = 0; 2196 SV *old = PL_diehook;
2197 PL_diehook = 0;
2198 SvREFCNT_dec (old);
2038 croak ("\3async_pool terminate\2\n"); 2199 croak ("\3async_pool terminate\2\n");
2039 } 2200 }
2040 2201
2041 av_clear (GvAV (PL_defgv)); 2202 av_clear (GvAV (PL_defgv));
2042 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 2203 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
2048 api_trace (coro_current, 0); 2209 api_trace (coro_current, 0);
2049 2210
2050 av_push (av_async_pool, newSVsv (coro_current)); 2211 av_push (av_async_pool, newSVsv (coro_current));
2051} 2212}
2052 2213
2214#if 0
2215
2216void
2217_generator_call (...)
2218 PROTOTYPE: @
2219 PPCODE:
2220 fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr);
2221 xxxx
2222 abort ();
2223
2224SV *
2225gensub (SV *sub, ...)
2226 PROTOTYPE: &;@
2227 CODE:
2228{
2229 struct coro *coro;
2230 MAGIC *mg;
2231 CV *xcv;
2232 CV *ncv = (CV *)newSV_type (SVt_PVCV);
2233 int i;
2234
2235 CvGV (ncv) = CvGV (cv);
2236 CvFILE (ncv) = CvFILE (cv);
2237
2238 Newz (0, coro, 1, struct coro);
2239 coro->args = newAV ();
2240 coro->flags = CF_NEW;
2241
2242 av_extend (coro->args, items - 1);
2243 for (i = 1; i < items; i++)
2244 av_push (coro->args, newSVsv (ST (i)));
2245
2246 CvISXSUB_on (ncv);
2247 CvXSUBANY (ncv).any_ptr = (void *)coro;
2248
2249 xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV));
2250
2251 CvXSUB (ncv) = CvXSUB (xcv);
2252 CvANON_on (ncv);
2253
2254 mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0);
2255 RETVAL = newRV_noinc ((SV *)ncv);
2256}
2257 OUTPUT:
2258 RETVAL
2259
2260#endif
2261
2053 2262
2054MODULE = Coro::State PACKAGE = Coro::AIO 2263MODULE = Coro::State PACKAGE = Coro::AIO
2055 2264
2056SV * 2265void
2057_get_state () 2266_get_state (SV *self)
2058 CODE: 2267 PPCODE:
2059{ 2268{
2060 struct io_state *data; 2269 AV *defav = GvAV (PL_defgv);
2061 2270 AV *av = newAV ();
2271 int i;
2062 RETVAL = newSV (sizeof (struct io_state)); 2272 SV *data_sv = newSV (sizeof (struct io_state));
2063 data = (struct io_state *)SvPVX (RETVAL); 2273 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2064 SvCUR_set (RETVAL, sizeof (struct io_state)); 2274 SvCUR_set (data_sv, sizeof (struct io_state));
2065 SvPOK_only (RETVAL); 2275 SvPOK_only (data_sv);
2066 2276
2067 data->errorno = errno; 2277 data->errorno = errno;
2068 data->laststype = PL_laststype; 2278 data->laststype = PL_laststype;
2069 data->laststatval = PL_laststatval; 2279 data->laststatval = PL_laststatval;
2070 data->statcache = PL_statcache; 2280 data->statcache = PL_statcache;
2281
2282 av_extend (av, AvFILLp (defav) + 1 + 1);
2283
2284 for (i = 0; i <= AvFILLp (defav); ++i)
2285 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2286
2287 av_push (av, data_sv);
2288
2289 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2290
2291 api_ready (self);
2071} 2292}
2072 OUTPUT:
2073 RETVAL
2074 2293
2075void 2294void
2076_set_state (char *data_) 2295_set_state (SV *state)
2077 PROTOTYPE: $ 2296 PROTOTYPE: $
2078 CODE: 2297 PPCODE:
2079{ 2298{
2080 struct io_state *data = (void *)data_; 2299 AV *av = (AV *)SvRV (state);
2300 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2301 int i;
2081 2302
2082 errno = data->errorno; 2303 errno = data->errorno;
2083 PL_laststype = data->laststype; 2304 PL_laststype = data->laststype;
2084 PL_laststatval = data->laststatval; 2305 PL_laststatval = data->laststatval;
2085 PL_statcache = data->statcache; 2306 PL_statcache = data->statcache;
2307
2308 EXTEND (SP, AvFILLp (av));
2309 for (i = 0; i < AvFILLp (av); ++i)
2310 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2086} 2311}
2087 2312
2088 2313
2089MODULE = Coro::State PACKAGE = Coro::AnyEvent 2314MODULE = Coro::State PACKAGE = Coro::AnyEvent
2090 2315
2114 } 2339 }
2115 2340
2116 --incede; 2341 --incede;
2117} 2342}
2118 2343
2344
2345MODULE = Coro::State PACKAGE = PerlIO::cede
2346
2347BOOT:
2348 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines