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.122 by root, Mon Dec 4 13:47:56 2006 UTC vs.
Revision 1.126 by root, Tue Dec 12 04:19:56 2006 UTC

38# ifndef IS_PADCONST 38# ifndef IS_PADCONST
39# define IS_PADCONST(v) 0 39# define IS_PADCONST(v) 0
40# endif 40# endif
41#endif 41#endif
42 42
43/* 5.8.7 */
44#ifndef SvRV_set
45# define SvRV_set(s,v) SvRV(s) = (v)
46#endif
47
43#include <stdio.h> 48#include <stdio.h>
44#include <errno.h> 49#include <errno.h>
50#include <assert.h>
45 51
46#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 52#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
47# undef STACKGUARD 53# undef STACKGUARD
48#endif 54#endif
49 55
157 /* optionally saved, might be zero */ 163 /* optionally saved, might be zero */
158 AV *defav; /* @_ */ 164 AV *defav; /* @_ */
159 SV *defsv; /* $_ */ 165 SV *defsv; /* $_ */
160 SV *errsv; /* $@ */ 166 SV *errsv; /* $@ */
161 SV *irssv; /* $/ */ 167 SV *irssv; /* $/ */
168 SV *irssv_sv; /* real $/ cache */
162 169
163#define VAR(name,type) type name; 170#define VAR(name,type) type name;
164# include "state.h" 171# include "state.h"
165#undef VAR 172#undef VAR
166 173
301#undef VAR 308#undef VAR
302 309
303 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 310 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
304 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 311 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
305 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 312 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
306 if (c->irssv) REPLACE_SV (PL_rs , c->irssv); 313 if (c->irssv)
314 {
315 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
316 SvREFCNT_dec (c->irssv);
317 else
318 {
319 REPLACE_SV (PL_rs, c->irssv);
320 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
321 sv_setsv (c->irssv_sv, PL_rs);
322 }
323 }
307 324
308 { 325 {
309 dSP; 326 dSP;
310 CV *cv; 327 CV *cv;
311 328
333 /* 350 /*
334 * the worst thing you can imagine happens first - we have to save 351 * the worst thing you can imagine happens first - we have to save
335 * (and reinitialize) all cv's in the whole callchain :( 352 * (and reinitialize) all cv's in the whole callchain :(
336 */ 353 */
337 354
355 EXTEND (SP, 3 + 1);
338 PUSHs (Nullsv); 356 PUSHs (Nullsv);
339 /* this loop was inspired by pp_caller */ 357 /* this loop was inspired by pp_caller */
340 for (;;) 358 for (;;)
341 { 359 {
342 while (cxix >= 0) 360 while (cxix >= 0)
348 CV *cv = cx->blk_sub.cv; 366 CV *cv = cx->blk_sub.cv;
349 367
350 if (CvDEPTH (cv)) 368 if (CvDEPTH (cv))
351 { 369 {
352 EXTEND (SP, 3); 370 EXTEND (SP, 3);
353
354 PUSHs ((SV *)CvPADLIST (cv)); 371 PUSHs ((SV *)CvPADLIST (cv));
355 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 372 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
356 PUSHs ((SV *)cv); 373 PUSHs ((SV *)cv);
357 374
358 CvDEPTH (cv) = 0; 375 CvDEPTH (cv) = 0;
359 get_padlist (cv); 376 get_padlist (cv);
360 } 377 }
361 } 378 }
362#ifdef CXt_FORMAT
363 else if (CxTYPE (cx) == CXt_FORMAT)
364 {
365 /* I never used formats, so how should I know how these are implemented? */
366 /* my bold guess is as a simple, plain sub... */
367 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
368 }
369#endif
370 } 379 }
371 380
372 if (top_si->si_type == PERLSI_MAIN) 381 if (top_si->si_type == PERLSI_MAIN)
373 break; 382 break;
374 383
394 * allocate various perl stacks. This is an exact copy 403 * allocate various perl stacks. This is an exact copy
395 * of perl.c:init_stacks, except that it uses less memory 404 * of perl.c:init_stacks, except that it uses less memory
396 * on the (sometimes correct) assumption that coroutines do 405 * on the (sometimes correct) assumption that coroutines do
397 * not usually need a lot of stackspace. 406 * not usually need a lot of stackspace.
398 */ 407 */
408#if USE_PERL_INIT_STACKS
409# define coro_init_stacks init_stacks
410#else
411
399static void 412static void
400coro_init_stacks () 413coro_init_stacks ()
401{ 414{
402 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 415 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
403 PL_curstackinfo->si_type = PERLSI_MAIN; 416 PL_curstackinfo->si_type = PERLSI_MAIN;
442static void 455static void
443coro_destroy_stacks () 456coro_destroy_stacks ()
444{ 457{
445 if (!IN_DESTRUCT) 458 if (!IN_DESTRUCT)
446 { 459 {
447 /* is this ugly, I ask? */ 460 /* restore all saved variables and stuff */
448 LEAVE_SCOPE (0); 461 LEAVE_SCOPE (0);
462 assert (PL_tmps_floor == -1);
449 463
450 /* sure it is, but more important: is it correct?? :/ */ 464 /* free all temporaries */
451 FREETMPS; 465 FREETMPS;
466 assert (PL_tmps_ix == -1);
452 467
453 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ 468 POPSTACK_TO (PL_mainstack);
454 } 469 }
455 470
456 while (PL_curstackinfo->si_next) 471 while (PL_curstackinfo->si_next)
457 PL_curstackinfo = PL_curstackinfo->si_next; 472 PL_curstackinfo = PL_curstackinfo->si_next;
458 473
459 while (PL_curstackinfo) 474 while (PL_curstackinfo)
460 { 475 {
461 PERL_SI *p = PL_curstackinfo->si_prev; 476 PERL_SI *p = PL_curstackinfo->si_prev;
462 477
463 { /*D*//*remove*/
464 dSP;
465 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
466 PUTBACK; /* possibly superfluous */
467 }
468
469 if (!IN_DESTRUCT) 478 if (!IN_DESTRUCT)
470 {
471 dounwind (-1);/*D*//*remove*/
472 SvREFCNT_dec (PL_curstackinfo->si_stack); 479 SvREFCNT_dec (PL_curstackinfo->si_stack);
473 }
474 480
475 Safefree (PL_curstackinfo->si_cxstack); 481 Safefree (PL_curstackinfo->si_cxstack);
476 Safefree (PL_curstackinfo); 482 Safefree (PL_curstackinfo);
477 PL_curstackinfo = p; 483 PL_curstackinfo = p;
478 } 484 }
483 Safefree (PL_savestack); 489 Safefree (PL_savestack);
484#if !PERL_VERSION_ATLEAST (5,9,0) 490#if !PERL_VERSION_ATLEAST (5,9,0)
485 Safefree (PL_retstack); 491 Safefree (PL_retstack);
486#endif 492#endif
487} 493}
494#endif
488 495
489static void 496static void
490setup_coro (struct coro *coro) 497setup_coro (struct coro *coro)
491{ 498{
492 /* 499 /*
582{ 589{
583 coro_cctx *cctx; 590 coro_cctx *cctx;
584 591
585 ++cctx_count; 592 ++cctx_count;
586 593
587 New (0, cctx, 1, coro_cctx); 594 Newz (0, cctx, 1, coro_cctx);
588 595
589#if HAVE_MMAP 596#if HAVE_MMAP
590 597
591 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 598 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
592 /* mmap supposedly does allocate-on-write for us */ 599 /* mmap supposedly does allocate-on-write for us */
693{ 700{
694 dSTACKLEVEL; 701 dSTACKLEVEL;
695 702
696 /* sometimes transfer is only called to set idle_sp */ 703 /* sometimes transfer is only called to set idle_sp */
697 if (!next) 704 if (!next)
705 {
698 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 706 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
707 assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */
708 }
699 else if (prev != next) 709 else if (prev != next)
700 { 710 {
701 coro_cctx *prev__cctx; 711 coro_cctx *prev__cctx;
702 712
703 if (prev->flags & CF_NEW) 713 if (prev->flags & CF_NEW)
728 /* first get rid of the old state */ 738 /* first get rid of the old state */
729 save_perl (prev); 739 save_perl (prev);
730 /* setup coroutine call */ 740 /* setup coroutine call */
731 setup_coro (next); 741 setup_coro (next);
732 /* need a new stack */ 742 /* need a new stack */
733 assert (!next->stack); 743 assert (!next->cctx);
734 } 744 }
735 else 745 else
736 { 746 {
737 /* coroutine already started */ 747 /* coroutine already started */
738 save_perl (prev); 748 save_perl (prev);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines