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.41 by root, Tue Nov 27 02:51:03 2001 UTC vs.
Revision 1.49 by pcg, Sun Nov 23 03:34:13 2003 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4
5#include "patchlevel.h"
6
7#if PATCHLEVEL < 6
8# ifndef PL_ppaddr
9# define PL_ppaddr ppaddr
10# endif
11# ifndef call_sv
12# define call_sv perl_call_sv
13# endif
14# ifndef get_sv
15# define get_sv perl_get_sv
16# endif
17# ifndef get_cv
18# define get_cv perl_get_cv
19# endif
20# ifndef IS_PADGV
21# define IS_PADGV(v) 0
22# endif
23# ifndef IS_PADCONST
24# define IS_PADCONST(v) 0
25# endif
26#endif
4 27
5#include "libcoro/coro.c" 28#include "libcoro/coro.c"
6 29
7#include <signal.h> 30#include <signal.h>
8 31
47 void *sptr; 70 void *sptr;
48 long ssize; /* positive == mmap, otherwise malloc */ 71 long ssize; /* positive == mmap, otherwise malloc */
49} coro_stack; 72} coro_stack;
50 73
51struct coro { 74struct coro {
75 /* the top-level JMPENV for each coroutine, needed to catch dies. */
76 JMPENV start_env;
77
52 /* the optional C context */ 78 /* the optional C context */
53 coro_stack *stack; 79 coro_stack *stack;
54 void *cursp; 80 void *cursp;
55 int gencnt; 81 int gencnt;
56 82
68 AV *curstack; 94 AV *curstack;
69 AV *mainstack; 95 AV *mainstack;
70 SV **stack_sp; 96 SV **stack_sp;
71 OP *op; 97 OP *op;
72 SV **curpad; 98 SV **curpad;
99 AV *comppad;
73 SV **stack_base; 100 SV **stack_base;
74 SV **stack_max; 101 SV **stack_max;
75 SV **tmps_stack; 102 SV **tmps_stack;
76 I32 tmps_floor; 103 I32 tmps_floor;
77 I32 tmps_ix; 104 I32 tmps_ix;
157 sv = (SV *) newAV (); 184 sv = (SV *) newAV ();
158 else if (*name == '%') 185 else if (*name == '%')
159 sv = (SV *) newHV (); 186 sv = (SV *) newHV ();
160 else 187 else
161 sv = NEWSV (0, 0); 188 sv = NEWSV (0, 0);
189#ifdef SvPADBUSY
162 if (!SvPADBUSY (sv)) 190 if (!SvPADBUSY (sv))
191#endif
163 SvPADMY_on (sv); 192 SvPADMY_on (sv);
164 npad[ix] = sv; 193 npad[ix] = sv;
165 } 194 }
166 } 195 }
167 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) 196 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix]))
276#define SE } while (0) 305#define SE } while (0)
277 306
278#define LOAD(state) load_state(aTHX_ (state)); 307#define LOAD(state) load_state(aTHX_ (state));
279#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); 308#define SAVE(state,flags) save_state(aTHX_ (state),(flags));
280 309
281#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE 310#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
282 311
283static void 312static void
284load_state(pTHX_ Coro__State c) 313load_state(pTHX_ Coro__State c)
285{ 314{
286 PL_dowarn = c->dowarn; 315 PL_dowarn = c->dowarn;
290 PL_curstack = c->curstack; 319 PL_curstack = c->curstack;
291 PL_mainstack = c->mainstack; 320 PL_mainstack = c->mainstack;
292 PL_stack_sp = c->stack_sp; 321 PL_stack_sp = c->stack_sp;
293 PL_op = c->op; 322 PL_op = c->op;
294 PL_curpad = c->curpad; 323 PL_curpad = c->curpad;
324 PL_comppad = c->comppad;
295 PL_stack_base = c->stack_base; 325 PL_stack_base = c->stack_base;
296 PL_stack_max = c->stack_max; 326 PL_stack_max = c->stack_max;
297 PL_tmps_stack = c->tmps_stack; 327 PL_tmps_stack = c->tmps_stack;
298 PL_tmps_floor = c->tmps_floor; 328 PL_tmps_floor = c->tmps_floor;
299 PL_tmps_ix = c->tmps_ix; 329 PL_tmps_ix = c->tmps_ix;
387 PUSHs ((SV *)cv); 417 PUSHs ((SV *)cv);
388 418
389 get_padlist (cv); /* this is a monster */ 419 get_padlist (cv); /* this is a monster */
390 } 420 }
391 } 421 }
422#ifdef CXt_FORMAT
392 else if (CxTYPE(cx) == CXt_FORMAT) 423 else if (CxTYPE(cx) == CXt_FORMAT)
393 { 424 {
394 /* I never used formats, so how should I know how these are implemented? */ 425 /* I never used formats, so how should I know how these are implemented? */
395 /* my bold guess is as a simple, plain sub... */ 426 /* my bold guess is as a simple, plain sub... */
396 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 427 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
397 } 428 }
429#endif
398 } 430 }
399 431
400 if (top_si->si_type == PERLSI_MAIN) 432 if (top_si->si_type == PERLSI_MAIN)
401 break; 433 break;
402 434
419 c->curstack = PL_curstack; 451 c->curstack = PL_curstack;
420 c->mainstack = PL_mainstack; 452 c->mainstack = PL_mainstack;
421 c->stack_sp = PL_stack_sp; 453 c->stack_sp = PL_stack_sp;
422 c->op = PL_op; 454 c->op = PL_op;
423 c->curpad = PL_curpad; 455 c->curpad = PL_curpad;
456 c->comppad = PL_comppad;
424 c->stack_base = PL_stack_base; 457 c->stack_base = PL_stack_base;
425 c->stack_max = PL_stack_max; 458 c->stack_max = PL_stack_max;
426 c->tmps_stack = PL_tmps_stack; 459 c->tmps_stack = PL_tmps_stack;
427 c->tmps_floor = PL_tmps_floor; 460 c->tmps_floor = PL_tmps_floor;
428 c->tmps_ix = PL_tmps_ix; 461 c->tmps_ix = PL_tmps_ix;
468 501
469 New(54,PL_markstack,16,I32); 502 New(54,PL_markstack,16,I32);
470 PL_markstack_ptr = PL_markstack; 503 PL_markstack_ptr = PL_markstack;
471 PL_markstack_max = PL_markstack + 16; 504 PL_markstack_max = PL_markstack + 16;
472 505
506#ifdef SET_MARK_OFFSET
473 SET_MARK_OFFSET; 507 SET_MARK_OFFSET;
508#endif
474 509
475 New(54,PL_scopestack,16,I32); 510 New(54,PL_scopestack,16,I32);
476 PL_scopestack_ix = 0; 511 PL_scopestack_ix = 0;
477 PL_scopestack_max = 16; 512 PL_scopestack_max = 16;
478 513
485 PL_retstack_max = 8; 520 PL_retstack_max = 8;
486} 521}
487 522
488/* 523/*
489 * destroy the stacks, the callchain etc... 524 * destroy the stacks, the callchain etc...
490 * still there is a memleak of 128 bytes...
491 */ 525 */
492STATIC void 526STATIC void
493destroy_stacks(pTHX) 527destroy_stacks(pTHX)
494{ 528{
495 if (!IN_DESTRUCT) 529 if (!IN_DESTRUCT)
576 { 610 {
577#ifdef HAVE_MMAP 611#ifdef HAVE_MMAP
578 if (stack->ssize > 0 && stack->sptr) 612 if (stack->ssize > 0 && stack->sptr)
579 munmap (stack->sptr, stack->ssize); 613 munmap (stack->sptr, stack->ssize);
580 else 614 else
581#else 615#endif
582 Safefree (stack->sptr); 616 Safefree (stack->sptr);
583#endif 617
584 Safefree (stack); 618 Safefree (stack);
585 } 619 }
586 else if (ctx->gencnt == stack->gencnt) 620 else if (ctx->gencnt == stack->gencnt)
587 --stack->usecnt; 621 --stack->usecnt;
588 } 622 }
600 634
601 coro_init_stacks (aTHX); 635 coro_init_stacks (aTHX);
602 /*PL_curcop = 0;*/ 636 /*PL_curcop = 0;*/
603 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 637 /*PL_in_eval = PL_in_eval;*/ /* inherit */
604 SvREFCNT_dec (GvAV (PL_defgv)); 638 SvREFCNT_dec (GvAV (PL_defgv));
605 GvAV (PL_defgv) = ctx->args; 639 GvAV (PL_defgv) = ctx->args; ctx->args = 0;
606 640
607 SPAGAIN; 641 SPAGAIN;
608 642
609 if (ctx->stack) 643 if (ctx->stack)
610 { 644 {
637 * that doesn't matter, though, since it is only 671 * that doesn't matter, though, since it is only
638 * pp_nextstate and we never return... 672 * pp_nextstate and we never return...
639 * ah yes, and I don't care anyways ;) 673 * ah yes, and I don't care anyways ;)
640 */ 674 */
641 PUTBACK; 675 PUTBACK;
642 PL_op = pp_entersub(); 676 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
643 SPAGAIN; 677 SPAGAIN;
644 678
645 ENTER; /* necessary e.g. for dounwind */ 679 ENTER; /* necessary e.g. for dounwind */
646 } 680 }
647} 681}
653 * this is a _very_ stripped down perl interpreter ;) 687 * this is a _very_ stripped down perl interpreter ;)
654 */ 688 */
655 Coro__State ctx = (Coro__State)arg; 689 Coro__State ctx = (Coro__State)arg;
656 JMPENV coro_start_env; 690 JMPENV coro_start_env;
657 691
658 /* same as JMPENV_BOOTSTRAP */
659 Zero(&coro_start_env, 1, JMPENV);
660 coro_start_env.je_ret = -1;
661 coro_start_env.je_mustcatch = TRUE;
662 PL_top_env = &coro_start_env; 692 PL_top_env = &ctx->start_env;
663 693
664 ctx->cursp = 0; 694 ctx->cursp = 0;
665 PL_op = PL_op->op_next; 695 PL_op = PL_op->op_next;
666 CALLRUNOPS(aTHX); 696 CALLRUNOPS(aTHX);
667 697
725 if (!prev->stack) 755 if (!prev->stack)
726 allocate_stack (prev, 0); 756 allocate_stack (prev, 0);
727 757
728 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 758 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
729 { 759 {
760 PL_top_env = &next->start_env;
761
730 setup_coro (next); 762 setup_coro (next);
731 763
732 prev->stack->refcnt++; 764 prev->stack->refcnt++;
733 prev->stack->usecnt++; 765 prev->stack->usecnt++;
734 next->stack = prev->stack; 766 next->stack = prev->stack;
735 next->gencnt = prev->gencnt; 767 next->gencnt = prev->gencnt;
736 } 768 }
737 else 769 else
738 { 770 {
771 assert (!next->stack);
739 allocate_stack (next, 1); 772 allocate_stack (next, 1);
740 coro_create (&(next->stack->cctx), 773 coro_create (&(next->stack->cctx),
741 setup_coro, (void *)next, 774 setup_coro, (void *)next,
742 next->stack->sptr, labs (next->stack->ssize)); 775 next->stack->sptr, labs (next->stack->ssize));
743 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 776 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
921 Coro__State coro; 954 Coro__State coro;
922 955
923 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) 956 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV)
924 croak ("Coro::State::_newprocess expects an arrayref"); 957 croak ("Coro::State::_newprocess expects an arrayref");
925 958
926 New (0, coro, 1, struct coro); 959 Newz (0, coro, 1, struct coro);
927 960
928 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 961 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
929 coro->mainstack = 0; /* actual work is done inside transfer */ 962 coro->mainstack = 0; /* actual work is done inside transfer */
930 coro->stack = 0; 963 coro->stack = 0;
964
965 /* same as JMPENV_BOOTSTRAP */
966 /* we might be able to recycle start_env, but safe is safe */
967 //Zero(&coro->start_env, 1, JMPENV);
968 coro->start_env.je_ret = -1;
969 coro->start_env.je_mustcatch = TRUE;
931 970
932 RETVAL = coro; 971 RETVAL = coro;
933 OUTPUT: 972 OUTPUT:
934 RETVAL 973 RETVAL
935 974
967 1006
968 coro->mainstack = 0; 1007 coro->mainstack = 0;
969 } 1008 }
970 1009
971 deallocate_stack (coro); 1010 deallocate_stack (coro);
972 1011 SvREFCNT_dec (coro->args);
973 Safefree (coro); 1012 Safefree (coro);
974 1013
975void 1014void
976flush() 1015flush()
977 CODE: 1016 CODE:
1004 SV *sv; 1043 SV *sv;
1005 AV *defav = GvAV (PL_defgv); 1044 AV *defav = GvAV (PL_defgv);
1006 struct coro *prev, *next; 1045 struct coro *prev, *next;
1007 1046
1008 if (!returnstk) 1047 if (!returnstk)
1009 returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); 1048 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE));
1010 1049
1011 /* set up @_ -- ugly */ 1050 /* set up @_ -- ugly */
1012 av_clear (defav); 1051 av_clear (defav);
1013 av_fill (defav, items - 1); 1052 av_fill (defav, items - 1);
1014 while (items--) 1053 while (items--)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines