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.56 by pcg, Thu Apr 1 02:41:05 2004 UTC vs.
Revision 1.68 by root, Mon Dec 12 20:28:30 2005 UTC

1#define PERL_NO_GET_CONTEXT 1#define PERL_NO_GET_CONTEXT
2
3#include "libcoro/coro.c"
2 4
3#include "EXTERN.h" 5#include "EXTERN.h"
4#include "perl.h" 6#include "perl.h"
5#include "XSUB.h" 7#include "XSUB.h"
6 8
7#include "patchlevel.h" 9#include "patchlevel.h"
8 10
9#if PATCHLEVEL < 6 11#if PERL_VERSION < 6
10# ifndef PL_ppaddr 12# ifndef PL_ppaddr
11# define PL_ppaddr ppaddr 13# define PL_ppaddr ppaddr
12# endif 14# endif
13# ifndef call_sv 15# ifndef call_sv
14# define call_sv perl_call_sv 16# define call_sv perl_call_sv
25# ifndef IS_PADCONST 27# ifndef IS_PADCONST
26# define IS_PADCONST(v) 0 28# define IS_PADCONST(v) 0
27# endif 29# endif
28#endif 30#endif
29 31
30#include "libcoro/coro.c"
31
32#include <signal.h> 32#include <signal.h>
33 33
34#ifdef HAVE_MMAP 34#ifdef HAVE_MMAP
35# include <unistd.h> 35# include <unistd.h>
36# include <sys/mman.h> 36# include <sys/mman.h>
69static struct CoroAPI coroapi; 69static struct CoroAPI coroapi;
70static AV *main_mainstack; /* used to differentiate between $main and others */ 70static AV *main_mainstack; /* used to differentiate between $main and others */
71static HV *coro_state_stash; 71static HV *coro_state_stash;
72static SV *ucoro_state_sv; 72static SV *ucoro_state_sv;
73static U32 ucoro_state_hash; 73static U32 ucoro_state_hash;
74static __thread SV *coro_mortal; /* will be freed after next transfer */ 74static SV *coro_mortal; /* will be freed after next transfer */
75 75
76/* this is actually not only the c stack but also c registers etc... */ 76/* this is actually not only the c stack but also c registers etc... */
77typedef struct { 77typedef struct {
78 int refcnt; /* pointer reference counter */ 78 int refcnt; /* pointer reference counter */
79 int usecnt; /* shared by how many coroutines */ 79 int usecnt; /* shared by how many coroutines */
109 AV *mainstack; 109 AV *mainstack;
110 SV **stack_sp; 110 SV **stack_sp;
111 OP *op; 111 OP *op;
112 SV **curpad; 112 SV **curpad;
113 AV *comppad; 113 AV *comppad;
114 CV *compcv;
114 SV **stack_base; 115 SV **stack_base;
115 SV **stack_max; 116 SV **stack_max;
116 SV **tmps_stack; 117 SV **tmps_stack;
117 I32 tmps_floor; 118 I32 tmps_floor;
118 I32 tmps_ix; 119 I32 tmps_ix;
127 I32 savestack_ix; 128 I32 savestack_ix;
128 I32 savestack_max; 129 I32 savestack_max;
129 OP **retstack; 130 OP **retstack;
130 I32 retstack_ix; 131 I32 retstack_ix;
131 I32 retstack_max; 132 I32 retstack_max;
133 PMOP *curpm;
132 COP *curcop; 134 COP *curcop;
133 JMPENV *top_env; 135 JMPENV *top_env;
134 136
135 /* data associated with this coroutine (initial args) */ 137 /* data associated with this coroutine (initial args) */
136 AV *args; 138 AV *args;
270 AV *av = (AV *)mg->mg_obj; 272 AV *av = (AV *)mg->mg_obj;
271 273
272 /* casting is fun. */ 274 /* casting is fun. */
273 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 275 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
274 free_padlist (aTHX_ padlist); 276 free_padlist (aTHX_ padlist);
277
278 SvREFCNT_dec (av);
275} 279}
276 280
277#define PERL_MAGIC_coro PERL_MAGIC_ext 281#define PERL_MAGIC_coro PERL_MAGIC_ext
278 282
279static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 283static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
325 PL_mainstack = c->mainstack; 329 PL_mainstack = c->mainstack;
326 PL_stack_sp = c->stack_sp; 330 PL_stack_sp = c->stack_sp;
327 PL_op = c->op; 331 PL_op = c->op;
328 PL_curpad = c->curpad; 332 PL_curpad = c->curpad;
329 PL_comppad = c->comppad; 333 PL_comppad = c->comppad;
334 PL_compcv = c->compcv;
330 PL_stack_base = c->stack_base; 335 PL_stack_base = c->stack_base;
331 PL_stack_max = c->stack_max; 336 PL_stack_max = c->stack_max;
332 PL_tmps_stack = c->tmps_stack; 337 PL_tmps_stack = c->tmps_stack;
333 PL_tmps_floor = c->tmps_floor; 338 PL_tmps_floor = c->tmps_floor;
334 PL_tmps_ix = c->tmps_ix; 339 PL_tmps_ix = c->tmps_ix;
343 PL_savestack_ix = c->savestack_ix; 348 PL_savestack_ix = c->savestack_ix;
344 PL_savestack_max = c->savestack_max; 349 PL_savestack_max = c->savestack_max;
345 PL_retstack = c->retstack; 350 PL_retstack = c->retstack;
346 PL_retstack_ix = c->retstack_ix; 351 PL_retstack_ix = c->retstack_ix;
347 PL_retstack_max = c->retstack_max; 352 PL_retstack_max = c->retstack_max;
353 PL_curpm = c->curpm;
348 PL_curcop = c->curcop; 354 PL_curcop = c->curcop;
349 PL_top_env = c->top_env; 355 PL_top_env = c->top_env;
350 356
351 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 357 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
352 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 358 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
449 c->mainstack = PL_mainstack; 455 c->mainstack = PL_mainstack;
450 c->stack_sp = PL_stack_sp; 456 c->stack_sp = PL_stack_sp;
451 c->op = PL_op; 457 c->op = PL_op;
452 c->curpad = PL_curpad; 458 c->curpad = PL_curpad;
453 c->comppad = PL_comppad; 459 c->comppad = PL_comppad;
460 c->compcv = PL_compcv;
454 c->stack_base = PL_stack_base; 461 c->stack_base = PL_stack_base;
455 c->stack_max = PL_stack_max; 462 c->stack_max = PL_stack_max;
456 c->tmps_stack = PL_tmps_stack; 463 c->tmps_stack = PL_tmps_stack;
457 c->tmps_floor = PL_tmps_floor; 464 c->tmps_floor = PL_tmps_floor;
458 c->tmps_ix = PL_tmps_ix; 465 c->tmps_ix = PL_tmps_ix;
467 c->savestack_ix = PL_savestack_ix; 474 c->savestack_ix = PL_savestack_ix;
468 c->savestack_max = PL_savestack_max; 475 c->savestack_max = PL_savestack_max;
469 c->retstack = PL_retstack; 476 c->retstack = PL_retstack;
470 c->retstack_ix = PL_retstack_ix; 477 c->retstack_ix = PL_retstack_ix;
471 c->retstack_max = PL_retstack_max; 478 c->retstack_max = PL_retstack_max;
479 c->curpm = PL_curpm;
472 c->curcop = PL_curcop; 480 c->curcop = PL_curcop;
473 c->top_env = PL_top_env; 481 c->top_env = PL_top_env;
474} 482}
475 483
476/* 484/*
532 /* is this ugly, I ask? */ 540 /* is this ugly, I ask? */
533 LEAVE_SCOPE (0); 541 LEAVE_SCOPE (0);
534 542
535 /* sure it is, but more important: is it correct?? :/ */ 543 /* sure it is, but more important: is it correct?? :/ */
536 FREETMPS; 544 FREETMPS;
545
546 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/
537 } 547 }
538 548
539 while (PL_curstackinfo->si_next) 549 while (PL_curstackinfo->si_next)
540 PL_curstackinfo = PL_curstackinfo->si_next; 550 PL_curstackinfo = PL_curstackinfo->si_next;
541 551
542 while (PL_curstackinfo) 552 while (PL_curstackinfo)
543 { 553 {
544 PERL_SI *p = PL_curstackinfo->si_prev; 554 PERL_SI *p = PL_curstackinfo->si_prev;
545 555
546 { 556 { /*D*//*remove*/
547 dSP; 557 dSP;
548 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 558 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
549 PUTBACK; /* possibly superfluous */ 559 PUTBACK; /* possibly superfluous */
550 } 560 }
551 561
552 if (!IN_DESTRUCT) 562 if (!IN_DESTRUCT)
553 { 563 {
554 dounwind(-1); 564 dounwind (-1);/*D*//*remove*/
555 SvREFCNT_dec(PL_curstackinfo->si_stack); 565 SvREFCNT_dec (PL_curstackinfo->si_stack);
556 } 566 }
557 567
558 Safefree(PL_curstackinfo->si_cxstack); 568 Safefree (PL_curstackinfo->si_cxstack);
559 Safefree(PL_curstackinfo); 569 Safefree (PL_curstackinfo);
560 PL_curstackinfo = p; 570 PL_curstackinfo = p;
561 } 571 }
562 572
563 Safefree(PL_tmps_stack); 573 Safefree (PL_tmps_stack);
564 Safefree(PL_markstack); 574 Safefree (PL_markstack);
565 Safefree(PL_scopestack); 575 Safefree (PL_scopestack);
566 Safefree(PL_savestack); 576 Safefree (PL_savestack);
567 Safefree(PL_retstack); 577 Safefree (PL_retstack);
568} 578}
569 579
570static void 580static void
571allocate_stack (Coro__State ctx, int alloc) 581allocate_stack (Coro__State ctx, int alloc)
572{ 582{
579 stack->gencnt = ctx->gencnt = 0; 589 stack->gencnt = ctx->gencnt = 0;
580 590
581 if (alloc) 591 if (alloc)
582 { 592 {
583#if HAVE_MMAP 593#if HAVE_MMAP
584 stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ 594 stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */
585 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 595 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
586 if (stack->sptr == (void *)-1) 596 if (stack->sptr == (void *)-1)
587#endif 597#endif
588 { 598 {
589 /*FIXME*//*D*//* reasonable stack size! */ 599 /*FIXME*//*D*//* reasonable stack size! */
590 stack->ssize = - (8192 * sizeof (long)); 600 stack->ssize = - (STACKSIZE * sizeof (long));
591 New (0, stack->sptr, 8192, long); 601 New (0, stack->sptr, STACKSIZE, long);
592 } 602 }
593 } 603 }
594 else 604 else
595 stack->sptr = 0; 605 stack->sptr = 0;
596 606
977 croak ("Coro::State::_newprocess expects an arrayref"); 987 croak ("Coro::State::_newprocess expects an arrayref");
978 988
979 Newz (0, coro, 1, struct coro); 989 Newz (0, coro, 1, struct coro);
980 990
981 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 991 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
982 coro->mainstack = 0; /* actual work is done inside transfer */ 992 /*coro->mainstack = 0; *//*actual work is done inside transfer */
983 coro->stack = 0; 993 /*coro->stack = 0;*/
984 994
985 /* same as JMPENV_BOOTSTRAP */ 995 /* same as JMPENV_BOOTSTRAP */
986 /* we might be able to recycle start_env, but safe is safe */ 996 /* we might be able to recycle start_env, but safe is safe */
987 //Zero(&coro->start_env, 1, JMPENV); 997 /*Zero(&coro->start_env, 1, JMPENV);*/
988 coro->start_env.je_ret = -1; 998 coro->start_env.je_ret = -1;
989 coro->start_env.je_mustcatch = TRUE; 999 coro->start_env.je_mustcatch = TRUE;
990 1000
991 RETVAL = coro; 1001 RETVAL = coro;
992 OUTPUT: 1002 OUTPUT:
1013 if (coro->mainstack && coro->mainstack != main_mainstack) 1023 if (coro->mainstack && coro->mainstack != main_mainstack)
1014 { 1024 {
1015 struct coro temp; 1025 struct coro temp;
1016 1026
1017 PUTBACK; 1027 PUTBACK;
1018 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 1028 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
1019 LOAD(aTHX_ coro); 1029 LOAD (aTHX_ coro);
1020 SPAGAIN; 1030 SPAGAIN;
1021 1031
1022 destroy_stacks (aTHX); 1032 destroy_stacks (aTHX);
1023 1033
1024 LOAD((&temp)); /* this will get rid of defsv etc.. */ 1034 LOAD ((&temp)); /* this will get rid of defsv etc.. */
1025 SPAGAIN; 1035 SPAGAIN;
1026 1036
1027 coro->mainstack = 0; 1037 coro->mainstack = 0;
1028 } 1038 }
1029 1039
1044 1054
1045void 1055void
1046yield(...) 1056yield(...)
1047 PROTOTYPE: @ 1057 PROTOTYPE: @
1048 CODE: 1058 CODE:
1049 static SV *returnstk; 1059 SV *yieldstack;
1050 SV *sv; 1060 SV *sv;
1051 AV *defav = GvAV (PL_defgv); 1061 AV *defav = GvAV (PL_defgv);
1052 struct coro *prev, *next; 1062 struct coro *prev, *next;
1053 1063
1054 if (!returnstk) 1064 yieldstack = *hv_fetch (
1055 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); 1065 (HV *)SvRV (GvSV (coro_current)),
1066 "yieldstack", sizeof ("yieldstack") - 1,
1067 0
1068 );
1056 1069
1057 /* set up @_ -- ugly */ 1070 /* set up @_ -- ugly */
1058 av_clear (defav); 1071 av_clear (defav);
1059 av_fill (defav, items - 1); 1072 av_fill (defav, items - 1);
1060 while (items--) 1073 while (items--)
1061 av_store (defav, items, SvREFCNT_inc (ST(items))); 1074 av_store (defav, items, SvREFCNT_inc (ST(items)));
1062 1075
1063 mg_get (returnstk); /* isn't documentation wrong for mg_get? */
1064 sv = av_pop ((AV *)SvRV (returnstk)); 1076 sv = av_pop ((AV *)SvRV (yieldstack));
1065 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 1077 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)));
1066 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1078 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
1067 SvREFCNT_dec (sv); 1079 SvREFCNT_dec (sv);
1068 1080
1069 transfer(aTHX_ prev, next, 0); 1081 transfer (aTHX_ prev, next, 0);
1070 1082
1071MODULE = Coro::State PACKAGE = Coro 1083MODULE = Coro::State PACKAGE = Coro
1072 1084
1073# this is slightly dirty (should expose a c-level api) 1085# this is slightly dirty (should expose a c-level api)
1074 1086

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines