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.196 by root, Sat Oct 6 01:11:01 2007 UTC vs.
Revision 1.210 by root, Wed Oct 10 03:24:40 2007 UTC

1#include "libcoro/coro.c" 1#include "libcoro/coro.c"
2 2
3#define PERL_NO_GET_CONTEXT 3#define PERL_NO_GET_CONTEXT
4#define PERL_EXT
4 5
5#include "EXTERN.h" 6#include "EXTERN.h"
6#include "perl.h" 7#include "perl.h"
7#include "XSUB.h" 8#include "XSUB.h"
8 9
9#include "patchlevel.h" 10#include "patchlevel.h"
10 11
11#include <stdio.h> 12#include <stdio.h>
12#include <errno.h> 13#include <errno.h>
13#include <assert.h> 14#include <assert.h>
15#include <inttypes.h> /* portable stdint.h */
14 16
15#ifdef HAVE_MMAP 17#ifdef HAVE_MMAP
16# include <unistd.h> 18# include <unistd.h>
17# include <sys/mman.h> 19# include <sys/mman.h>
18# ifndef MAP_ANONYMOUS 20# ifndef MAP_ANONYMOUS
129#else 131#else
130# define LOCK (void)0 132# define LOCK (void)0
131# define UNLOCK (void)0 133# define UNLOCK (void)0
132#endif 134#endif
133 135
136#define strpair(const) const, sizeof (const) - 1
137
134/* helper storage struct for Coro::AIO */ 138/* helper storage struct for Coro::AIO */
135struct io_state 139struct io_state
136{ 140{
137 int errorno; 141 int errorno;
138 I32 laststype; 142 I32 laststype;
148static SV *coro_mortal; /* will be freed after next transfer */ 152static SV *coro_mortal; /* will be freed after next transfer */
149 153
150static GV *irsgv; /* $/ */ 154static GV *irsgv; /* $/ */
151static GV *stdoutgv; /* *STDOUT */ 155static GV *stdoutgv; /* *STDOUT */
152 156
157static HV *hv_sig; /* %SIG */
158static SV *sv_diehook;
159static SV *sv_warnhook;
160
153/* async_pool helper stuff */ 161/* async_pool helper stuff */
154static SV *sv_pool_rss; 162static SV *sv_pool_rss;
155static SV *sv_pool_size; 163static SV *sv_pool_size;
156static AV *av_async_pool; 164static AV *av_async_pool;
157 165
158static struct coro_cctx *cctx_first; 166static struct coro_cctx *cctx_first[3]; /* index by GIMME_V type, void, scalar, array */
159static int cctx_count, cctx_idle; 167static int cctx_count, cctx_idle[3];
160 168
161enum { 169enum {
162 CC_MAPPED = 0x01, 170 CC_MAPPED = 0x01,
163 CC_NOREUSE = 0x02, /* throw this away after tracing */ 171 CC_NOREUSE = 0x02, /* throw this away after tracing */
164 CC_TRACE = 0x04, 172 CC_TRACE = 0x04,
192 CF_READY = 0x0002, /* coroutine is ready */ 200 CF_READY = 0x0002, /* coroutine is ready */
193 CF_NEW = 0x0004, /* has never been switched to */ 201 CF_NEW = 0x0004, /* has never been switched to */
194 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 202 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
195}; 203};
196 204
205/* the structure where most of the perl state is stored, overlaid on the cxstack */
206typedef struct {
207 SV *defsv;
208 AV *defav;
209 SV *errsv;
210 SV *irsgv;
211#define VAR(name,type) type name;
212# include "state.h"
213#undef VAR
214} perl_slots;
215
216#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
217
197/* this is a structure representing a perl-level coroutine */ 218/* this is a structure representing a perl-level coroutine */
198struct coro { 219struct coro {
199 /* the c coroutine allocated to this perl coroutine, if any */ 220 /* the c coroutine allocated to this perl coroutine, if any */
200 coro_cctx *cctx; 221 coro_cctx *cctx;
222 int gimme;
201 223
224 /* process data */
225 AV *mainstack;
226 perl_slots *slot; /* basically the saved sp */
227
202 /* data associated with this coroutine (initial args) */ 228 AV *args; /* data associated with this coroutine (initial args) */
203 AV *args; 229 int refcnt; /* coroutines are refcounted, yes */
204 int refcnt;
205 int flags; /* CF_ flags */ 230 int flags; /* CF_ flags */
206 231 HV *hv; /* the perl hash associated with this coro, if any */
207 /* optionally saved, might be zero */
208 AV *defav; /* @_ */
209 SV *defsv; /* $_ */
210 SV *errsv; /* $@ */
211 SV *deffh; /* default filehandle */
212 SV *irssv; /* $/ */
213 SV *irssv_sv; /* real $/ cache */
214
215#define VAR(name,type) type name;
216# include "state.h"
217#undef VAR
218 232
219 /* statistics */ 233 /* statistics */
220 int usecount; /* number of transfers to this coro */ 234 int usecount; /* number of transfers to this coro */
221 235
222 /* coro process data */ 236 /* coro process data */
223 int prio; 237 int prio;
224 SV *throw; 238 SV *throw; /* exception to be thrown */
225 239
226 /* async_pool */ 240 /* async_pool */
227 SV *saved_deffh; 241 SV *saved_deffh;
228 242
229 /* linked list */ 243 /* linked list */
230 struct coro *next, *prev; 244 struct coro *next, *prev;
231 HV *hv; /* the perl hash associated with this coro, if any */
232}; 245};
233 246
234typedef struct coro *Coro__State; 247typedef struct coro *Coro__State;
235typedef struct coro *Coro__State_or_hashref; 248typedef struct coro *Coro__State_or_hashref;
236 249
248static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 261static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
249static int coro_nready; 262static int coro_nready;
250static struct coro *coro_first; 263static struct coro *coro_first;
251 264
252/** lowlevel stuff **********************************************************/ 265/** lowlevel stuff **********************************************************/
266
267static SV *
268coro_get_sv (const char *name, int create)
269{
270#if PERL_VERSION_ATLEAST (5,9,0)
271 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
272 get_sv (name, create);
273#endif
274 return get_sv (name, create);
275}
276
277static AV *
278coro_get_av (const char *name, int create)
279{
280#if PERL_VERSION_ATLEAST (5,9,0)
281 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
282 get_av (name, create);
283#endif
284 return get_av (name, create);
285}
286
287static HV *
288coro_get_hv (const char *name, int create)
289{
290#if PERL_VERSION_ATLEAST (5,9,0)
291 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
292 get_hv (name, create);
293#endif
294 return get_hv (name, create);
295}
253 296
254static AV * 297static AV *
255coro_clone_padlist (pTHX_ CV *cv) 298coro_clone_padlist (pTHX_ CV *cv)
256{ 299{
257 AV *padlist = CvPADLIST (cv); 300 AV *padlist = CvPADLIST (cv);
397/** load & save, init *******************************************************/ 440/** load & save, init *******************************************************/
398 441
399static void 442static void
400load_perl (pTHX_ Coro__State c) 443load_perl (pTHX_ Coro__State c)
401{ 444{
445 perl_slots *slot = c->slot;
446 c->slot = 0;
447
448 PL_mainstack = c->mainstack;
449
450 GvSV (PL_defgv) = slot->defsv;
451 GvAV (PL_defgv) = slot->defav;
452 GvSV (PL_errgv) = slot->errsv;
453 GvSV (irsgv) = slot->irsgv;
454
402#define VAR(name,type) PL_ ## name = c->name; 455 #define VAR(name,type) PL_ ## name = slot->name;
403# include "state.h" 456 # include "state.h"
404#undef VAR 457 #undef VAR
405 458
406 GvSV (PL_defgv) = c->defsv; 459 /*hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);*/
407 GvAV (PL_defgv) = c->defav; 460 /*hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);*/
408 GvSV (PL_errgv) = c->errsv;
409 GvSV (irsgv) = c->irssv_sv;
410 461
411 { 462 {
412 dSP; 463 dSP;
464
413 CV *cv; 465 CV *cv;
414 466
415 /* now do the ugly restore mess */ 467 /* now do the ugly restore mess */
416 while (expect_true (cv = (CV *)POPs)) 468 while (expect_true (cv = (CV *)POPs))
417 { 469 {
472 } 524 }
473 525
474 PUTBACK; 526 PUTBACK;
475 } 527 }
476 528
529 /* allocate some space on the context stack for our purposes */
530 /* we manually unroll here, as usually 2 slots is enough */
531 if (SLOT_COUNT >= 1) CXINC;
532 if (SLOT_COUNT >= 2) CXINC;
533 if (SLOT_COUNT >= 3) CXINC;
534 {
535 int i;
536 for (i = 3; i < SLOT_COUNT; ++i)
537 CXINC;
538 }
539 cxstack_ix -= SLOT_COUNT; /* undo allocation */
540
541 c->mainstack = PL_mainstack;
542
543 {
544 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
545
477 c->defav = GvAV (PL_defgv); 546 slot->defav = GvAV (PL_defgv);
478 c->defsv = DEFSV; 547 slot->defsv = DEFSV;
479 c->errsv = ERRSV; 548 slot->errsv = ERRSV;
480 c->irssv_sv = GvSV (irsgv); 549 slot->irsgv = GvSV (irsgv);
481 550
482#define VAR(name,type)c->name = PL_ ## name; 551 #define VAR(name,type) slot->name = PL_ ## name;
483# include "state.h" 552 # include "state.h"
484#undef VAR 553 #undef VAR
554 }
485} 555}
486 556
487/* 557/*
488 * allocate various perl stacks. This is an exact copy 558 * allocate various perl stacks. This is an exact copy
489 * of perl.c:init_stacks, except that it uses less memory 559 * of perl.c:init_stacks, except that it uses less memory
494# define coro_init_stacks init_stacks 564# define coro_init_stacks init_stacks
495#else 565#else
496static void 566static void
497coro_init_stacks (pTHX) 567coro_init_stacks (pTHX)
498{ 568{
499 PL_curstackinfo = new_stackinfo(64, 6); 569 PL_curstackinfo = new_stackinfo(32, 8);
500 PL_curstackinfo->si_type = PERLSI_MAIN; 570 PL_curstackinfo->si_type = PERLSI_MAIN;
501 PL_curstack = PL_curstackinfo->si_stack; 571 PL_curstack = PL_curstackinfo->si_stack;
502 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 572 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
503 573
504 PL_stack_base = AvARRAY(PL_curstack); 574 PL_stack_base = AvARRAY(PL_curstack);
505 PL_stack_sp = PL_stack_base; 575 PL_stack_sp = PL_stack_base;
506 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 576 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
507 577
508 New(50,PL_tmps_stack,64,SV*); 578 New(50,PL_tmps_stack,32,SV*);
509 PL_tmps_floor = -1; 579 PL_tmps_floor = -1;
510 PL_tmps_ix = -1; 580 PL_tmps_ix = -1;
511 PL_tmps_max = 64; 581 PL_tmps_max = 32;
512 582
513 New(54,PL_markstack,16,I32); 583 New(54,PL_markstack,16,I32);
514 PL_markstack_ptr = PL_markstack; 584 PL_markstack_ptr = PL_markstack;
515 PL_markstack_max = PL_markstack + 16; 585 PL_markstack_max = PL_markstack + 16;
516 586
517#ifdef SET_MARK_OFFSET 587#ifdef SET_MARK_OFFSET
518 SET_MARK_OFFSET; 588 SET_MARK_OFFSET;
519#endif 589#endif
520 590
521 New(54,PL_scopestack,16,I32); 591 New(54,PL_scopestack,8,I32);
522 PL_scopestack_ix = 0; 592 PL_scopestack_ix = 0;
523 PL_scopestack_max = 16; 593 PL_scopestack_max = 8;
524 594
525 New(54,PL_savestack,64,ANY); 595 New(54,PL_savestack,24,ANY);
526 PL_savestack_ix = 0; 596 PL_savestack_ix = 0;
527 PL_savestack_max = 64; 597 PL_savestack_max = 24;
528 598
529#if !PERL_VERSION_ATLEAST (5,9,0) 599#if !PERL_VERSION_ATLEAST (5,9,0)
530 New(54,PL_retstack,4,OP*); 600 New(54,PL_retstack,4,OP*);
531 PL_retstack_ix = 0; 601 PL_retstack_ix = 0;
532 PL_retstack_max = 4; 602 PL_retstack_max = 4;
569{ 639{
570 size_t rss = sizeof (*coro); 640 size_t rss = sizeof (*coro);
571 641
572 if (coro->mainstack) 642 if (coro->mainstack)
573 { 643 {
644 perl_slots tmp_slot;
645 perl_slots *slot;
646
574 if (coro->flags & CF_RUNNING) 647 if (coro->flags & CF_RUNNING)
575 { 648 {
649 slot = &tmp_slot;
650
576 #define VAR(name,type)coro->name = PL_ ## name; 651 #define VAR(name,type) slot->name = PL_ ## name;
577 # include "state.h" 652 # include "state.h"
578 #undef VAR 653 #undef VAR
579 } 654 }
655 else
656 slot = coro->slot;
580 657
581 rss += sizeof (coro->curstackinfo); 658 rss += sizeof (slot->curstackinfo);
582 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
583 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 659 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
584 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); 660 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *);
585 rss += coro->tmps_max * sizeof (SV *); 661 rss += slot->tmps_max * sizeof (SV *);
586 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); 662 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
587 rss += coro->scopestack_max * sizeof (I32); 663 rss += slot->scopestack_max * sizeof (I32);
588 rss += coro->savestack_max * sizeof (ANY); 664 rss += slot->savestack_max * sizeof (ANY);
589 665
590#if !PERL_VERSION_ATLEAST (5,9,0) 666#if !PERL_VERSION_ATLEAST (5,9,0)
591 rss += coro->retstack_max * sizeof (OP *); 667 rss += slot->retstack_max * sizeof (OP *);
592#endif 668#endif
593 } 669 }
594 670
595 return rss; 671 return rss;
596} 672}
612 PL_curpm = 0; 688 PL_curpm = 0;
613 PL_curpad = 0; 689 PL_curpad = 0;
614 PL_localizing = 0; 690 PL_localizing = 0;
615 PL_dirty = 0; 691 PL_dirty = 0;
616 PL_restartop = 0; 692 PL_restartop = 0;
693 PL_diehook = 0; hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);
694 PL_warnhook = 0; hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);
617 695
618 GvSV (PL_defgv) = newSV (0); 696 GvSV (PL_defgv) = newSV (0);
619 GvAV (PL_defgv) = coro->args; coro->args = 0; 697 GvAV (PL_defgv) = coro->args; coro->args = 0;
620 GvSV (PL_errgv) = newSV (0); 698 GvSV (PL_errgv) = newSV (0);
621 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 699 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
634 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 712 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
635 PUTBACK; 713 PUTBACK;
636 PL_op = (OP *)&myop; 714 PL_op = (OP *)&myop;
637 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 715 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
638 SPAGAIN; 716 SPAGAIN;
717
718 /*
719 * now its very tricky. the "tail" of the next transfer might end up
720 * either in a new cctx, or an existing one.
721 * in case of an existing one we have to take care of whatever
722 * entersub and transfer do to the perl stack.
723 */
724 ENTER;
725 EXTEND (SP, 4);
726 PUSHs ((SV *)0); /* items */
727 PUSHs ((SV *)0); /* ix, set_stacklevel */
728 PUSHs ((SV *)(sp - PL_stack_base + 1)); /* ax */
729 PUSHs ((SV *)0); /* again */
730 PUTBACK;
639 } 731 }
640 732
641 ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ 733 /* this newly created coroutine might be run on an existing cctx which most
734 * likely was suspended in set_stacklevel, called from entersub.
735 * set_stacklevl doesn't do anything on return, but entersub does LEAVE,
736 * so we ENTER here for symmetry
737 */
738 ENTER;
642} 739}
643 740
644static void 741static void
645coro_destroy (pTHX_ struct coro *coro) 742coro_destroy (pTHX_ struct coro *coro)
646{ 743{
666 SvREFCNT_dec (GvSV (PL_errgv)); 763 SvREFCNT_dec (GvSV (PL_errgv));
667 SvREFCNT_dec (PL_defoutgv); 764 SvREFCNT_dec (PL_defoutgv);
668 SvREFCNT_dec (PL_rs); 765 SvREFCNT_dec (PL_rs);
669 SvREFCNT_dec (GvSV (irsgv)); 766 SvREFCNT_dec (GvSV (irsgv));
670 767
768 SvREFCNT_dec (PL_diehook);
769 SvREFCNT_dec (PL_warnhook);
770
671 SvREFCNT_dec (coro->saved_deffh); 771 SvREFCNT_dec (coro->saved_deffh);
672 SvREFCNT_dec (coro->throw); 772 SvREFCNT_dec (coro->throw);
673 773
674 coro_destroy_stacks (aTHX); 774 coro_destroy_stacks (aTHX);
675} 775}
727 PUSHMARK (SP); 827 PUSHMARK (SP);
728 PUSHs (&PL_sv_no); 828 PUSHs (&PL_sv_no);
729 PUSHs (fullname); 829 PUSHs (fullname);
730 PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); 830 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
731 PUTBACK; 831 PUTBACK;
732 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 832 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0);
733 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 833 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
734 SPAGAIN; 834 SPAGAIN;
735 FREETMPS; 835 FREETMPS;
736 LEAVE; 836 LEAVE;
737 PL_runops = runops_trace; 837 PL_runops = runops_trace;
766 PUSHMARK (SP); 866 PUSHMARK (SP);
767 PUSHs (&PL_sv_yes); 867 PUSHs (&PL_sv_yes);
768 PUSHs (fullname); 868 PUSHs (fullname);
769 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 869 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
770 PUTBACK; 870 PUTBACK;
771 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 871 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0);
772 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 872 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
773 SPAGAIN; 873 SPAGAIN;
774 FREETMPS; 874 FREETMPS;
775 LEAVE; 875 LEAVE;
776 PL_runops = runops_trace; 876 PL_runops = runops_trace;
790 PL_runops = RUNOPS_DEFAULT; 890 PL_runops = RUNOPS_DEFAULT;
791 PUSHMARK (SP); 891 PUSHMARK (SP);
792 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); 892 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
793 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); 893 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
794 PUTBACK; 894 PUTBACK;
795 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); 895 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_line_cb"), 0);
796 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 896 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
797 SPAGAIN; 897 SPAGAIN;
798 FREETMPS; 898 FREETMPS;
799 LEAVE; 899 LEAVE;
800 PL_runops = runops_trace; 900 PL_runops = runops_trace;
810 910
811/* inject a fake call to Coro::State::_cctx_init into the execution */ 911/* inject a fake call to Coro::State::_cctx_init into the execution */
812/* _cctx_init should be careful, as it could be called at almost any time */ 912/* _cctx_init should be careful, as it could be called at almost any time */
813/* during execution of a perl program */ 913/* during execution of a perl program */
814static void NOINLINE 914static void NOINLINE
815prepare_cctx (pTHX_ coro_cctx *cctx) 915cctx_prepare (pTHX_ coro_cctx *cctx)
816{ 916{
817 dSP; 917 dSP;
818 LOGOP myop; 918 LOGOP myop;
819 919
820 PL_top_env = &PL_start_env; 920 PL_top_env = &PL_start_env;
838 938
839/* 939/*
840 * this is a _very_ stripped down perl interpreter ;) 940 * this is a _very_ stripped down perl interpreter ;)
841 */ 941 */
842static void 942static void
843coro_run (void *arg) 943cctx_run (void *arg)
844{ 944{
845 dTHX; 945 dTHX;
846 946
847 /* coro_run is the alternative tail of transfer(), so unlock here. */ 947 /* cctx_run is the alternative tail of transfer(), so unlock here. */
848 UNLOCK; 948 UNLOCK;
849 949
850 /* we now skip the entersub that lead to transfer() */ 950 /* we now skip the entersub that lead to transfer() */
851 PL_op = PL_op->op_next; 951 PL_op = PL_op->op_next;
852 952
853 /* inject a fake subroutine call to cctx_init */ 953 /* inject a fake subroutine call to cctx_init */
854 prepare_cctx (aTHX_ (coro_cctx *)arg); 954 cctx_prepare (aTHX_ (coro_cctx *)arg);
855 955
856 /* somebody or something will hit me for both perl_run and PL_restartop */ 956 /* somebody or something will hit me for both perl_run and PL_restartop */
857 PL_restartop = PL_op; 957 PL_restartop = PL_op;
858 perl_run (PL_curinterp); 958 perl_run (PL_curinterp);
859 959
878 ++cctx_count; 978 ++cctx_count;
879 979
880 Newz (0, cctx, 1, coro_cctx); 980 Newz (0, cctx, 1, coro_cctx);
881 981
882#if HAVE_MMAP 982#if HAVE_MMAP
883
884 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 983 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
885 /* mmap supposedly does allocate-on-write for us */ 984 /* mmap supposedly does allocate-on-write for us */
886 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 985 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
887 986
888 if (cctx->sptr != (void *)-1) 987 if (cctx->sptr != (void *)-1)
909 stack_start = cctx->sptr; 1008 stack_start = cctx->sptr;
910 stack_size = cctx->ssize; 1009 stack_size = cctx->ssize;
911 } 1010 }
912 1011
913 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); 1012 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size);
914 coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); 1013 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size);
915 1014
916 return cctx; 1015 return cctx;
917} 1016}
918 1017
919static void 1018static void
940 1039
941/* wether this cctx should be destructed */ 1040/* wether this cctx should be destructed */
942#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1041#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE))
943 1042
944static coro_cctx * 1043static coro_cctx *
945cctx_get (pTHX) 1044cctx_get (pTHX_ int gimme)
946{ 1045{
947 while (expect_true (cctx_first)) 1046 while (expect_true (cctx_first[gimme]))
948 { 1047 {
949 coro_cctx *cctx = cctx_first; 1048 coro_cctx *cctx = cctx_first[gimme];
950 cctx_first = cctx->next; 1049 cctx_first[gimme] = cctx->next;
951 --cctx_idle; 1050 --cctx_idle[gimme];
952 1051
953 if (expect_true (!CCTX_EXPIRED (cctx))) 1052 if (expect_true (!CCTX_EXPIRED (cctx)))
954 return cctx; 1053 return cctx;
955 1054
956 cctx_destroy (cctx); 1055 cctx_destroy (cctx);
957 } 1056 }
958 1057
1058 assert (!gimme);
959 return cctx_new (); 1059 return cctx_new ();
960} 1060}
961 1061
962static void 1062static void
963cctx_put (coro_cctx *cctx) 1063cctx_put (coro_cctx *cctx, int gimme)
964{ 1064{
965 /* free another cctx if overlimit */ 1065 /* free another cctx if overlimit */
966 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1066 if (expect_false (cctx_idle[gimme] >= MAX_IDLE_CCTX))
967 { 1067 {
968 coro_cctx *first = cctx_first; 1068 coro_cctx *first = cctx_first[gimme];
969 cctx_first = first->next; 1069 cctx_first[gimme] = first->next;
970 --cctx_idle; 1070 --cctx_idle[gimme];
971 1071
972 cctx_destroy (first); 1072 cctx_destroy (first);
973 } 1073 }
974 1074
975 ++cctx_idle; 1075 ++cctx_idle[gimme];
976 cctx->next = cctx_first; 1076 cctx->next = cctx_first[gimme];
977 cctx_first = cctx; 1077 cctx_first[gimme] = cctx;
978} 1078}
979 1079
980/** coroutine switching *****************************************************/ 1080/** coroutine switching *****************************************************/
981 1081
982static void 1082static void
991 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 1091 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
992 1092
993 if (expect_false (next->flags & CF_DESTROYED)) 1093 if (expect_false (next->flags & CF_DESTROYED))
994 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); 1094 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
995 1095
1096 if (
1097#if PERL_VERSION_ATLEAST (5,9,0)
1098 expect_false (PL_parser)
1099#else
996 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1100 expect_false (PL_lex_state != LEX_NOTPARSING)
1101#endif
1102 )
997 croak ("Coro::State::transfer called while parsing, but this is not supported"); 1103 croak ("Coro::State::transfer called while parsing, but this is not supported");
998 } 1104 }
999} 1105}
1000 1106
1001/* always use the TRANSFER macro */ 1107/* always use the TRANSFER macro */
1054 prev->cctx = 0; 1160 prev->cctx = 0;
1055 1161
1056 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1162 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1057 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1163 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1058 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1164 if (expect_false (CCTX_EXPIRED (prev__cctx)))
1165 if (!next->cctx)
1059 next->cctx = cctx_get (aTHX); 1166 next->cctx = cctx_get (aTHX_ next->gimme);
1060 1167
1061 cctx_put (prev__cctx); 1168 cctx_put (prev__cctx, prev->gimme);
1062 } 1169 }
1063 1170
1064 ++next->usecount; 1171 ++next->usecount;
1065 1172
1066 if (expect_true (!next->cctx)) 1173 if (expect_true (!next->cctx))
1067 next->cctx = cctx_get (aTHX); 1174 next->cctx = cctx_get (aTHX_ next->gimme);
1068 1175
1069 if (expect_false (prev__cctx != next->cctx)) 1176 if (expect_false (prev__cctx != next->cctx))
1070 { 1177 {
1071 prev__cctx->top_env = PL_top_env; 1178 prev__cctx->top_env = PL_top_env;
1072 PL_top_env = next->cctx->top_env; 1179 PL_top_env = next->cctx->top_env;
1130 save_perl (aTHX_ &temp); 1237 save_perl (aTHX_ &temp);
1131 load_perl (aTHX_ coro); 1238 load_perl (aTHX_ coro);
1132 1239
1133 coro_destroy (aTHX_ coro); 1240 coro_destroy (aTHX_ coro);
1134 1241
1135 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ 1242 load_perl (aTHX_ &temp);
1136 1243
1137 coro->mainstack = 0; 1244 coro->slot = 0;
1138 } 1245 }
1139 1246
1140 cctx_destroy (coro->cctx); 1247 cctx_destroy (coro->cctx);
1141 SvREFCNT_dec (coro->args); 1248 SvREFCNT_dec (coro->args);
1142 1249
1399 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); 1506 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1400 1507
1401 if (coro->flags & CF_RUNNING) 1508 if (coro->flags & CF_RUNNING)
1402 PL_runops = RUNOPS_DEFAULT; 1509 PL_runops = RUNOPS_DEFAULT;
1403 else 1510 else
1404 coro->runops = RUNOPS_DEFAULT; 1511 coro->slot->runops = RUNOPS_DEFAULT;
1405 } 1512 }
1406} 1513}
1407 1514
1408MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1515MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1409 1516
1416#endif 1523#endif
1417 BOOT_PAGESIZE; 1524 BOOT_PAGESIZE;
1418 1525
1419 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1526 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1420 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1527 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1528
1529 hv_sig = coro_get_hv ("SIG", TRUE);
1530 sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE);
1531 sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE);
1532
1533 if (!PL_diehook ) hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);
1534 if (!PL_warnhook) hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);
1421 1535
1422 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1536 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1423 1537
1424 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1538 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1425 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1539 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1431 1545
1432 while (main_top_env->je_prev) 1546 while (main_top_env->je_prev)
1433 main_top_env = main_top_env->je_prev; 1547 main_top_env = main_top_env->je_prev;
1434 1548
1435 coroapi.ver = CORO_API_VERSION; 1549 coroapi.ver = CORO_API_VERSION;
1550 coroapi.rev = CORO_API_REVISION;
1436 coroapi.transfer = api_transfer; 1551 coroapi.transfer = api_transfer;
1437 1552
1438 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1553 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1439} 1554}
1440 1555
1507 1622
1508 break; 1623 break;
1509 } 1624 }
1510 1625
1511 BARRIER; 1626 BARRIER;
1627 PUTBACK;
1512 TRANSFER (ta); 1628 TRANSFER (ta);
1513 1629 SPAGAIN; /* might be the sp of a different coroutine now */
1514 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) 1630 /* be extra careful not to ever do anything after TRANSFER */
1515 XSRETURN_YES;
1516} 1631}
1517 1632
1518bool 1633bool
1519_destroy (SV *coro_sv) 1634_destroy (SV *coro_sv)
1520 CODE: 1635 CODE:
1546 RETVAL 1661 RETVAL
1547 1662
1548int 1663int
1549cctx_idle () 1664cctx_idle ()
1550 CODE: 1665 CODE:
1551 RETVAL = cctx_idle; 1666 RETVAL = cctx_idle[0] + cctx_idle[1] + cctx_idle[2];
1552 OUTPUT: 1667 OUTPUT:
1553 RETVAL 1668 RETVAL
1554 1669
1555void 1670void
1556list () 1671list ()
1569 CODE: 1684 CODE:
1570{ 1685{
1571 if (coro->mainstack) 1686 if (coro->mainstack)
1572 { 1687 {
1573 struct coro temp; 1688 struct coro temp;
1574 Zero (&temp, 1, struct coro);
1575 1689
1576 if (!(coro->flags & CF_RUNNING)) 1690 if (!(coro->flags & CF_RUNNING))
1577 { 1691 {
1578 save_perl (aTHX_ &temp); 1692 save_perl (aTHX_ &temp);
1579 load_perl (aTHX_ coro); 1693 load_perl (aTHX_ coro);
1581 1695
1582 { 1696 {
1583 dSP; 1697 dSP;
1584 ENTER; 1698 ENTER;
1585 SAVETMPS; 1699 SAVETMPS;
1700 PUTBACK;
1701 PUSHSTACK;
1586 PUSHMARK (SP); 1702 PUSHMARK (SP);
1587 PUTBACK; 1703
1588 if (ix) 1704 if (ix)
1589 eval_sv (coderef, 0); 1705 eval_sv (coderef, 0);
1590 else 1706 else
1591 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1707 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1708
1592 SPAGAIN; 1709 POPSTACK;
1593 FREETMPS; 1710 FREETMPS;
1594 LEAVE; 1711 LEAVE;
1595 PUTBACK; 1712 PUTBACK;
1596 } 1713 }
1597 1714
1654 1771
1655BOOT: 1772BOOT:
1656{ 1773{
1657 int i; 1774 int i;
1658 1775
1659 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); 1776 sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE);
1660 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); 1777 sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE);
1661 av_async_pool = get_av ("Coro::async_pool", TRUE); 1778 av_async_pool = coro_get_av ("Coro::async_pool", TRUE);
1662 1779
1663 coro_current = get_sv ("Coro::current", FALSE); 1780 coro_current = coro_get_sv ("Coro::current", FALSE);
1664 SvREADONLY_on (coro_current); 1781 SvREADONLY_on (coro_current);
1665 1782
1666 coro_stash = gv_stashpv ("Coro", TRUE); 1783 coro_stash = gv_stashpv ("Coro", TRUE);
1667 1784
1668 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1785 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1669 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1786 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1670 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1787 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1671 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1788 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1674 1791
1675 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1792 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1676 coro_ready[i] = newAV (); 1793 coro_ready[i] = newAV ();
1677 1794
1678 { 1795 {
1679 SV *sv = perl_get_sv("Coro::API", 1); 1796 SV *sv = perl_get_sv ("Coro::API", TRUE);
1797 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
1680 1798
1681 coroapi.schedule = api_schedule; 1799 coroapi.schedule = api_schedule;
1682 coroapi.cede = api_cede; 1800 coroapi.cede = api_cede;
1683 coroapi.cede_notself = api_cede_notself; 1801 coroapi.cede_notself = api_cede_notself;
1684 coroapi.ready = api_ready; 1802 coroapi.ready = api_ready;
1750 CODE: 1868 CODE:
1751{ 1869{
1752 struct coro *coro = SvSTATE (coro_current); 1870 struct coro *coro = SvSTATE (coro_current);
1753 HV *hv = (HV *)SvRV (coro_current); 1871 HV *hv = (HV *)SvRV (coro_current);
1754 AV *defav = GvAV (PL_defgv); 1872 AV *defav = GvAV (PL_defgv);
1755 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); 1873 SV *invoke = hv_delete (hv, strpair ("_invoke"), 0);
1756 AV *invoke_av; 1874 AV *invoke_av;
1757 int i, len; 1875 int i, len;
1758 1876
1759 if (!invoke) 1877 if (!invoke)
1760 croak ("\3terminate\2\n"); 1878 croak ("\3async_pool terminate\2\n");
1761 1879
1762 SvREFCNT_dec (coro->saved_deffh); 1880 SvREFCNT_dec (coro->saved_deffh);
1763 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 1881 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv);
1764 1882
1765 hv_store (hv, "desc", sizeof ("desc") - 1, 1883 hv_store (hv, "desc", sizeof ("desc") - 1,
1766 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 1884 newSVpvn (strpair ("[async_pool]")), 0);
1767 1885
1768 invoke_av = (AV *)SvRV (invoke); 1886 invoke_av = (AV *)SvRV (invoke);
1769 len = av_len (invoke_av); 1887 len = av_len (invoke_av);
1770 1888
1771 sv_setsv (cb, AvARRAY (invoke_av)[0]); 1889 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1791 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 1909 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1792 coro->saved_deffh = 0; 1910 coro->saved_deffh = 0;
1793 1911
1794 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 1912 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1795 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1913 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1796 croak ("\3terminate\2\n"); 1914 croak ("\3async_pool terminate\2\n");
1797 1915
1798 av_clear (GvAV (PL_defgv)); 1916 av_clear (GvAV (PL_defgv));
1799 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 1917 hv_store ((HV *)SvRV (coro_current), strpair ("desc"),
1800 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 1918 newSVpvn (strpair ("[async_pool idle]")), 0);
1801 1919
1802 coro->prio = 0; 1920 coro->prio = 0;
1803 1921
1804 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 1922 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1805 api_trace (coro_current, 0); 1923 api_trace (coro_current, 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines