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.15 by root, Wed Jul 25 04:14:38 2001 UTC vs.
Revision 1.20 by root, Fri Aug 3 12:51:56 2001 UTC

2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include "libcoro/coro.c" 5#include "libcoro/coro.c"
6 6
7#include <signal.h>
8
7#ifdef HAVE_MMAP 9#ifdef HAVE_MMAP
8# include <unistd.h> 10# include <unistd.h>
9# include <sys/mman.h> 11# include <sys/mman.h>
12# ifndef MAP_ANON
13# ifdef MAP_ANONYMOUS
14# define MAP_ANON MAP_ANONYMOUS
15# else
16# undef HAVE_MMAP
17# endif
18# endif
10#endif 19#endif
11 20
12#define MAY_FLUSH /* increases codesize */ 21#define MAY_FLUSH /* increases codesize */
13 22
14/* perl-related */ 23/* perl-related */
46 55
47 void *sptr; 56 void *sptr;
48 long ssize; /* positive == mmap, otherwise malloc */ 57 long ssize; /* positive == mmap, otherwise malloc */
49} coro_stack; 58} coro_stack;
50 59
51static coro_stack main_stack = { 1, 0, 0 };
52
53struct coro { 60struct coro {
54 /* the optional C context */ 61 /* the optional C context */
55 coro_stack *stack; 62 coro_stack *stack;
56 void *cursp; 63 void *cursp;
57 int gencnt; 64 int gencnt;
61 SV *defsv; 68 SV *defsv;
62 SV *errsv; 69 SV *errsv;
63 70
64 /* saved global state not related to stacks */ 71 /* saved global state not related to stacks */
65 U8 dowarn; 72 U8 dowarn;
73 I32 in_eval;
66 74
67 /* the stacks and related info (callchain etc..) */ 75 /* the stacks and related info (callchain etc..) */
68 PERL_SI *curstackinfo; 76 PERL_SI *curstackinfo;
69 AV *curstack; 77 AV *curstack;
70 AV *mainstack; 78 AV *mainstack;
88 I32 savestack_max; 96 I32 savestack_max;
89 OP **retstack; 97 OP **retstack;
90 I32 retstack_ix; 98 I32 retstack_ix;
91 I32 retstack_max; 99 I32 retstack_max;
92 COP *curcop; 100 COP *curcop;
93 JMPENV start_env;
94 JMPENV *top_env; 101 JMPENV *top_env;
95 102
96 /* data associated with this coroutine (initial args) */ 103 /* data associated with this coroutine (initial args) */
97 AV *args; 104 AV *args;
98}; 105};
283 290
284static void 291static void
285load_state(pTHX_ Coro__State c) 292load_state(pTHX_ Coro__State c)
286{ 293{
287 PL_dowarn = c->dowarn; 294 PL_dowarn = c->dowarn;
295 PL_in_eval = c->in_eval;
288 296
289 PL_curstackinfo = c->curstackinfo; 297 PL_curstackinfo = c->curstackinfo;
290 PL_curstack = c->curstack; 298 PL_curstack = c->curstack;
291 PL_mainstack = c->mainstack; 299 PL_mainstack = c->mainstack;
292 PL_stack_sp = c->stack_sp; 300 PL_stack_sp = c->stack_sp;
309 PL_savestack_max = c->savestack_max; 317 PL_savestack_max = c->savestack_max;
310 PL_retstack = c->retstack; 318 PL_retstack = c->retstack;
311 PL_retstack_ix = c->retstack_ix; 319 PL_retstack_ix = c->retstack_ix;
312 PL_retstack_max = c->retstack_max; 320 PL_retstack_max = c->retstack_max;
313 PL_curcop = c->curcop; 321 PL_curcop = c->curcop;
314 PL_start_env = c->start_env;
315 PL_top_env = c->top_env; 322 PL_top_env = c->top_env;
316 323
317 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 324 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
318 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 325 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
319 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 326 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
418 /* but if it's missing the defav contents magically get replaced sometimes */ 425 /* but if it's missing the defav contents magically get replaced sometimes */
419 if (c->defav) 426 if (c->defav)
420 av_reify (c->defav); 427 av_reify (c->defav);
421 428
422 c->dowarn = PL_dowarn; 429 c->dowarn = PL_dowarn;
430 c->in_eval = PL_in_eval;
423 431
424 c->curstackinfo = PL_curstackinfo; 432 c->curstackinfo = PL_curstackinfo;
425 c->curstack = PL_curstack; 433 c->curstack = PL_curstack;
426 c->mainstack = PL_mainstack; 434 c->mainstack = PL_mainstack;
427 c->stack_sp = PL_stack_sp; 435 c->stack_sp = PL_stack_sp;
444 c->savestack_max = PL_savestack_max; 452 c->savestack_max = PL_savestack_max;
445 c->retstack = PL_retstack; 453 c->retstack = PL_retstack;
446 c->retstack_ix = PL_retstack_ix; 454 c->retstack_ix = PL_retstack_ix;
447 c->retstack_max = PL_retstack_max; 455 c->retstack_max = PL_retstack_max;
448 c->curcop = PL_curcop; 456 c->curcop = PL_curcop;
449 c->start_env = PL_start_env;
450 c->top_env = PL_top_env; 457 c->top_env = PL_top_env;
451} 458}
452 459
453/* 460/*
454 * allocate various perl stacks. This is an exact copy 461 * allocate various perl stacks. This is an exact copy
545 stack->refcnt = 1; 552 stack->refcnt = 1;
546 stack->usecnt = 1; 553 stack->usecnt = 1;
547 stack->gencnt = ctx->gencnt = 0; 554 stack->gencnt = ctx->gencnt = 0;
548 if (alloc) 555 if (alloc)
549 { 556 {
550#ifdef HAVE_MMAP 557#ifdef HAVE_MMAP
551 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-use */ 558 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */
552 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 559 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
553 if (stack->sptr == (void *)-1) 560 if (stack->sptr == (void *)-1)
554#endif 561#endif
555 { 562 {
556 /*FIXME*//*D*//* reasonable stack size! */ 563 /*FIXME*//*D*//* reasonable stack size! */
598 dSP; 605 dSP;
599 Coro__State ctx = (Coro__State)arg; 606 Coro__State ctx = (Coro__State)arg;
600 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 607 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE);
601 608
602 coro_init_stacks (aTHX); 609 coro_init_stacks (aTHX);
603 JMPENV_BOOTSTRAP;
604 SPAGAIN;
605
606 /*PL_curcop = 0;*/ 610 /*PL_curcop = 0;*/
611 /*PL_in_eval = PL_in_eval;*/ /* inherit */
607 SvREFCNT_dec (GvAV (PL_defgv)); 612 SvREFCNT_dec (GvAV (PL_defgv));
608 GvAV (PL_defgv) = ctx->args; 613 GvAV (PL_defgv) = ctx->args;
609 614
615 SPAGAIN;
616
610 if (ctx->stack) 617 if (ctx->stack)
611 { 618 {
612 ctx->cursp = 0; 619 ctx->cursp = 0;
613 620
614 PUSHMARK(SP); 621 PUSHMARK(SP);
615 PUTBACK; 622 PUTBACK;
616 (void) call_sv (sub_init, G_VOID|G_NOARGS); 623 (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL);
624
625 if (SvTRUE (ERRSV))
626 croak (NULL);
627 else
617 croak ("FATAL: CCTXT coroutine returned!"); 628 croak ("FATAL: CCTXT coroutine returned!");
618 } 629 }
619 else 630 else
620 { 631 {
621 UNOP myop; 632 UNOP myop;
622 633
649 /* 660 /*
650 * this is a _very_ stripped down perl interpreter ;) 661 * this is a _very_ stripped down perl interpreter ;)
651 */ 662 */
652 Coro__State ctx = (Coro__State)arg; 663 Coro__State ctx = (Coro__State)arg;
653 664
665 /*FIXME*//* must set up top_env here */
654 ctx->cursp = 0; 666 ctx->cursp = 0;
655 PL_op = PL_op->op_next; 667 PL_op = PL_op->op_next;
656 CALLRUNOPS(aTHX); 668 CALLRUNOPS(aTHX);
657 /*NORETURN*/ 669
658 abort (); 670 abort ();
659} 671}
660 672
661STATIC void 673STATIC void
662transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 674transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
784 Coro::State_or_hashref prev 796 Coro::State_or_hashref prev
785 Coro::State_or_hashref next 797 Coro::State_or_hashref next
786 int flags 798 int flags
787 PROTOTYPE: @ 799 PROTOTYPE: @
788 CODE: 800 CODE:
801 PUTBACK;
789 transfer (aTHX_ prev, next, flags); 802 transfer (aTHX_ prev, next, flags);
803 SPAGAIN;
790 804
791void 805void
792DESTROY(coro) 806DESTROY(coro)
793 Coro::State coro 807 Coro::State coro
794 CODE: 808 CODE:
814void 828void
815flush() 829flush()
816 CODE: 830 CODE:
817#ifdef MAY_FLUSH 831#ifdef MAY_FLUSH
818 flush_padlist_cache (); 832 flush_padlist_cache ();
833#endif
834
835void
836_exit(code)
837 int code
838 PROTOTYPE: $
839 CODE:
840#if defined(__GLIBC__) || _POSIX_C_SOURCE
841 _exit (code);
842#else
843 signal (SIGTERM, SIG_DFL);
844 raise (SIGTERM);
845 exit (code);
819#endif 846#endif
820 847
821MODULE = Coro::State PACKAGE = Coro::Cont 848MODULE = Coro::State PACKAGE = Coro::Cont
822 849
823# this is slightly dirty 850# this is slightly dirty

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines