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.28 by root, Sat Aug 25 21:32:05 2001 UTC vs.
Revision 1.37 by root, Tue Oct 9 00:39:11 2001 UTC

7#include <signal.h> 7#include <signal.h>
8 8
9#ifdef HAVE_MMAP 9#ifdef HAVE_MMAP
10# include <unistd.h> 10# include <unistd.h>
11# include <sys/mman.h> 11# include <sys/mman.h>
12# ifndef MAP_ANON
13# ifdef MAP_ANONYMOUS 12# ifndef MAP_ANONYMOUS
14# define MAP_ANON MAP_ANONYMOUS 13# ifdef MAP_ANON
14# define MAP_ANONYMOUS MAP_ANON
15# else 15# else
16# undef HAVE_MMAP 16# undef HAVE_MMAP
17# endif 17# endif
18# endif 18# endif
19#endif 19#endif
25 25
26/* The next macro should declare a variable stacklevel that contains and approximation 26/* The next macro should declare a variable stacklevel that contains and approximation
27 * to the current C stack pointer. Its property is that it changes with each call 27 * to the current C stack pointer. Its property is that it changes with each call
28 * and should be unique. */ 28 * and should be unique. */
29#define dSTACKLEVEL void *stacklevel = &stacklevel 29#define dSTACKLEVEL void *stacklevel = &stacklevel
30
31#define IN_DESTRUCT (PL_main_cv == Nullcv)
30 32
31#define labs(l) ((l) >= 0 ? (l) : -(l)) 33#define labs(l) ((l) >= 0 ? (l) : -(l))
32 34
33#include "CoroAPI.h" 35#include "CoroAPI.h"
34 36
355 357
356 PUSHs (Nullsv); 358 PUSHs (Nullsv);
357 /* this loop was inspired by pp_caller */ 359 /* this loop was inspired by pp_caller */
358 for (;;) 360 for (;;)
359 { 361 {
360 do 362 while (cxix >= 0)
361 { 363 {
362 PERL_CONTEXT *cx = &ccstk[cxix--]; 364 PERL_CONTEXT *cx = &ccstk[cxix--];
363 365
364 if (CxTYPE(cx) == CXt_SUB) 366 if (CxTYPE(cx) == CXt_SUB)
365 { 367 {
391 /* I never used formats, so how should I know how these are implemented? */ 393 /* I never used formats, so how should I know how these are implemented? */
392 /* my bold guess is as a simple, plain sub... */ 394 /* my bold guess is as a simple, plain sub... */
393 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 395 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
394 } 396 }
395 } 397 }
396 while (cxix >= 0);
397 398
398 if (top_si->si_type == PERLSI_MAIN) 399 if (top_si->si_type == PERLSI_MAIN)
399 break; 400 break;
400 401
401 top_si = top_si->si_prev; 402 top_si = top_si->si_prev;
488 * still there is a memleak of 128 bytes... 489 * still there is a memleak of 128 bytes...
489 */ 490 */
490STATIC void 491STATIC void
491destroy_stacks(pTHX) 492destroy_stacks(pTHX)
492{ 493{
494 if (!IN_DESTRUCT)
495 {
493 /* is this ugly, I ask? */ 496 /* is this ugly, I ask? */
494 while (PL_scopestack_ix) 497 while (PL_scopestack_ix)
495 LEAVE; 498 LEAVE;
496 499
497 /* sure it is, but more important: is it correct?? :/ */ 500 /* sure it is, but more important: is it correct?? :/ */
498 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ 501 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
499 FREETMPS; 502 FREETMPS;
503 }
500 504
501 while (PL_curstackinfo->si_next) 505 while (PL_curstackinfo->si_next)
502 PL_curstackinfo = PL_curstackinfo->si_next; 506 PL_curstackinfo = PL_curstackinfo->si_next;
503 507
504 while (PL_curstackinfo) 508 while (PL_curstackinfo)
509 dSP; 513 dSP;
510 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 514 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
511 PUTBACK; /* possibly superfluous */ 515 PUTBACK; /* possibly superfluous */
512 } 516 }
513 517
514 if (PL_main_cv != Nullcv) /* don't during destruction. hack? */ 518 if (!IN_DESTRUCT)
519 {
515 dounwind(-1); 520 dounwind(-1);
516
517 SvREFCNT_dec(PL_curstackinfo->si_stack); 521 SvREFCNT_dec(PL_curstackinfo->si_stack);
522 }
523
518 Safefree(PL_curstackinfo->si_cxstack); 524 Safefree(PL_curstackinfo->si_cxstack);
519 Safefree(PL_curstackinfo); 525 Safefree(PL_curstackinfo);
520 PL_curstackinfo = p; 526 PL_curstackinfo = p;
521 } 527 }
522 528
537 stack->refcnt = 1; 543 stack->refcnt = 1;
538 stack->usecnt = 1; 544 stack->usecnt = 1;
539 stack->gencnt = ctx->gencnt = 0; 545 stack->gencnt = ctx->gencnt = 0;
540 if (alloc) 546 if (alloc)
541 { 547 {
542#ifdef HAVE_MMAP 548#if HAVE_MMAP
543 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ 549 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */
544 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 550 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
545 if (stack->sptr == (void *)-1) 551 if (stack->sptr == (void *)-1)
546#endif 552#endif
547 { 553 {
548 /*FIXME*//*D*//* reasonable stack size! */ 554 /*FIXME*//*D*//* reasonable stack size! */
549 stack->ssize = -4096 * sizeof (long); 555 stack->ssize = -4096 * sizeof (long);
563 569
564 ctx->stack = 0; 570 ctx->stack = 0;
565 571
566 if (stack) 572 if (stack)
567 { 573 {
568 printf ("deallocating stack %p %d\n", stack, stack->refcnt);/*D*/
569 if (!--stack->refcnt) 574 if (!--stack->refcnt)
570 { 575 {
571#ifdef HAVE_MMAP 576#ifdef HAVE_MMAP
572 if (stack->ssize > 0 && stack->sptr) 577 if (stack->ssize > 0 && stack->sptr)
573 munmap (stack->sptr, stack->ssize); 578 munmap (stack->sptr, stack->ssize);
645{ 650{
646 /* 651 /*
647 * this is a _very_ stripped down perl interpreter ;) 652 * this is a _very_ stripped down perl interpreter ;)
648 */ 653 */
649 Coro__State ctx = (Coro__State)arg; 654 Coro__State ctx = (Coro__State)arg;
655 JMPENV coro_start_env;
650 656
651 /*FIXME*//* must set up top_env here */ 657 /* same as JMPENV_BOOTSTRAP */
658 Zero(&coro_start_env, 1, JMPENV);
659 coro_start_env.je_ret = -1;
660 coro_start_env.je_mustcatch = TRUE;
661 PL_top_env = &coro_start_env;
662
652 ctx->cursp = 0; 663 ctx->cursp = 0;
653 PL_op = PL_op->op_next; 664 PL_op = PL_op->op_next;
654 CALLRUNOPS(aTHX); 665 CALLRUNOPS(aTHX);
655 666
656 abort (); 667 abort ();
660transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 671transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
661{ 672{
662 dSTACKLEVEL; 673 dSTACKLEVEL;
663 static struct coro *xnext; 674 static struct coro *xnext;
664 675
665 printf ("%p => %p\n", prev, next);/*D*/
666 if (prev != next) 676 if (prev != next)
667 { 677 {
668 xnext = next; 678 xnext = next;
669 679
670 if (next->mainstack) 680 if (next->mainstack)
734 } 744 }
735 } 745 }
736 else 746 else
737 setup_coro (next); 747 setup_coro (next);
738 } 748 }
739 }
740 749
750 /*
751 * xnext is now either prev or next, depending on wether
752 * we switched the c stack or not. that's why i use a global
753 * variable, that should become thread-specific at one point.
754 */
741 xnext->cursp = stacklevel; 755 xnext->cursp = stacklevel;
756 }
742} 757}
743 758
744static struct coro * 759static struct coro *
745sv_to_coro (SV *arg, const char *funcname, const char *varname) 760sv_to_coro (SV *arg, const char *funcname, const char *varname)
746{ 761{
753 768
754 arg = HeVAL(he); 769 arg = HeVAL(he);
755 } 770 }
756 771
757 /* must also be changed inside Coro::Cont::yield */ 772 /* must also be changed inside Coro::Cont::yield */
773 if (SvROK(arg) && SvOBJECT(SvRV(arg))
758 if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) 774 && SvSTASH(SvRV(arg)) == coro_state_stash)
759 return (struct coro *) SvIV((SV*)SvRV(arg)); 775 return (struct coro *) SvIV((SV*)SvRV(arg));
760 776
761 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); 777 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname);
762 /*NORETURN*/ 778 /*NORETURN*/
763} 779}
1027} 1043}
1028 1044
1029void 1045void
1030ready(self) 1046ready(self)
1031 SV * self 1047 SV * self
1048 PROTOTYPE: $
1032 CODE: 1049 CODE:
1033 api_ready (self); 1050 api_ready (self);
1034 1051
1035int 1052int
1036nready(...) 1053nready(...)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines