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.57 by pcg, Thu Apr 22 04:14:49 2004 UTC vs.
Revision 1.67 by root, Mon Dec 12 20:07:44 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// threaded perls might need much more, 65536 or more,
12// which is 0.5 to 1MB
13#define STACKSIZE 16384
14
15#if PERL_VERSION < 6
10# ifndef PL_ppaddr 16# ifndef PL_ppaddr
11# define PL_ppaddr ppaddr 17# define PL_ppaddr ppaddr
12# endif 18# endif
13# ifndef call_sv 19# ifndef call_sv
14# define call_sv perl_call_sv 20# define call_sv perl_call_sv
25# ifndef IS_PADCONST 31# ifndef IS_PADCONST
26# define IS_PADCONST(v) 0 32# define IS_PADCONST(v) 0
27# endif 33# endif
28#endif 34#endif
29 35
30#include "libcoro/coro.c"
31
32#include <signal.h> 36#include <signal.h>
33 37
34#ifdef HAVE_MMAP 38#ifdef HAVE_MMAP
35# include <unistd.h> 39# include <unistd.h>
36# include <sys/mman.h> 40# include <sys/mman.h>
69static struct CoroAPI coroapi; 73static struct CoroAPI coroapi;
70static AV *main_mainstack; /* used to differentiate between $main and others */ 74static AV *main_mainstack; /* used to differentiate between $main and others */
71static HV *coro_state_stash; 75static HV *coro_state_stash;
72static SV *ucoro_state_sv; 76static SV *ucoro_state_sv;
73static U32 ucoro_state_hash; 77static U32 ucoro_state_hash;
74static __thread SV *coro_mortal; /* will be freed after next transfer */ 78static SV *coro_mortal; /* will be freed after next transfer */
75 79
76/* this is actually not only the c stack but also c registers etc... */ 80/* this is actually not only the c stack but also c registers etc... */
77typedef struct { 81typedef struct {
78 int refcnt; /* pointer reference counter */ 82 int refcnt; /* pointer reference counter */
79 int usecnt; /* shared by how many coroutines */ 83 int usecnt; /* shared by how many coroutines */
109 AV *mainstack; 113 AV *mainstack;
110 SV **stack_sp; 114 SV **stack_sp;
111 OP *op; 115 OP *op;
112 SV **curpad; 116 SV **curpad;
113 AV *comppad; 117 AV *comppad;
118 CV *compcv;
114 SV **stack_base; 119 SV **stack_base;
115 SV **stack_max; 120 SV **stack_max;
116 SV **tmps_stack; 121 SV **tmps_stack;
117 I32 tmps_floor; 122 I32 tmps_floor;
118 I32 tmps_ix; 123 I32 tmps_ix;
127 I32 savestack_ix; 132 I32 savestack_ix;
128 I32 savestack_max; 133 I32 savestack_max;
129 OP **retstack; 134 OP **retstack;
130 I32 retstack_ix; 135 I32 retstack_ix;
131 I32 retstack_max; 136 I32 retstack_max;
137 PMOP *curpm;
132 COP *curcop; 138 COP *curcop;
133 JMPENV *top_env; 139 JMPENV *top_env;
134 140
135 /* data associated with this coroutine (initial args) */ 141 /* data associated with this coroutine (initial args) */
136 AV *args; 142 AV *args;
270 AV *av = (AV *)mg->mg_obj; 276 AV *av = (AV *)mg->mg_obj;
271 277
272 /* casting is fun. */ 278 /* casting is fun. */
273 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 279 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
274 free_padlist (aTHX_ padlist); 280 free_padlist (aTHX_ padlist);
281
282 SvREFCNT_dec (av);
275} 283}
276 284
277#define PERL_MAGIC_coro PERL_MAGIC_ext 285#define PERL_MAGIC_coro PERL_MAGIC_ext
278 286
279static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 287static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
325 PL_mainstack = c->mainstack; 333 PL_mainstack = c->mainstack;
326 PL_stack_sp = c->stack_sp; 334 PL_stack_sp = c->stack_sp;
327 PL_op = c->op; 335 PL_op = c->op;
328 PL_curpad = c->curpad; 336 PL_curpad = c->curpad;
329 PL_comppad = c->comppad; 337 PL_comppad = c->comppad;
338 PL_compcv = c->compcv;
330 PL_stack_base = c->stack_base; 339 PL_stack_base = c->stack_base;
331 PL_stack_max = c->stack_max; 340 PL_stack_max = c->stack_max;
332 PL_tmps_stack = c->tmps_stack; 341 PL_tmps_stack = c->tmps_stack;
333 PL_tmps_floor = c->tmps_floor; 342 PL_tmps_floor = c->tmps_floor;
334 PL_tmps_ix = c->tmps_ix; 343 PL_tmps_ix = c->tmps_ix;
343 PL_savestack_ix = c->savestack_ix; 352 PL_savestack_ix = c->savestack_ix;
344 PL_savestack_max = c->savestack_max; 353 PL_savestack_max = c->savestack_max;
345 PL_retstack = c->retstack; 354 PL_retstack = c->retstack;
346 PL_retstack_ix = c->retstack_ix; 355 PL_retstack_ix = c->retstack_ix;
347 PL_retstack_max = c->retstack_max; 356 PL_retstack_max = c->retstack_max;
357 PL_curpm = c->curpm;
348 PL_curcop = c->curcop; 358 PL_curcop = c->curcop;
349 PL_top_env = c->top_env; 359 PL_top_env = c->top_env;
350 360
351 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 361 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
352 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 362 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
449 c->mainstack = PL_mainstack; 459 c->mainstack = PL_mainstack;
450 c->stack_sp = PL_stack_sp; 460 c->stack_sp = PL_stack_sp;
451 c->op = PL_op; 461 c->op = PL_op;
452 c->curpad = PL_curpad; 462 c->curpad = PL_curpad;
453 c->comppad = PL_comppad; 463 c->comppad = PL_comppad;
464 c->compcv = PL_compcv;
454 c->stack_base = PL_stack_base; 465 c->stack_base = PL_stack_base;
455 c->stack_max = PL_stack_max; 466 c->stack_max = PL_stack_max;
456 c->tmps_stack = PL_tmps_stack; 467 c->tmps_stack = PL_tmps_stack;
457 c->tmps_floor = PL_tmps_floor; 468 c->tmps_floor = PL_tmps_floor;
458 c->tmps_ix = PL_tmps_ix; 469 c->tmps_ix = PL_tmps_ix;
467 c->savestack_ix = PL_savestack_ix; 478 c->savestack_ix = PL_savestack_ix;
468 c->savestack_max = PL_savestack_max; 479 c->savestack_max = PL_savestack_max;
469 c->retstack = PL_retstack; 480 c->retstack = PL_retstack;
470 c->retstack_ix = PL_retstack_ix; 481 c->retstack_ix = PL_retstack_ix;
471 c->retstack_max = PL_retstack_max; 482 c->retstack_max = PL_retstack_max;
483 c->curpm = PL_curpm;
472 c->curcop = PL_curcop; 484 c->curcop = PL_curcop;
473 c->top_env = PL_top_env; 485 c->top_env = PL_top_env;
474} 486}
475 487
476/* 488/*
581 stack->gencnt = ctx->gencnt = 0; 593 stack->gencnt = ctx->gencnt = 0;
582 594
583 if (alloc) 595 if (alloc)
584 { 596 {
585#if HAVE_MMAP 597#if HAVE_MMAP
586 stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ 598 stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */
587 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 599 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
588 if (stack->sptr == (void *)-1) 600 if (stack->sptr == (void *)-1)
589#endif 601#endif
590 { 602 {
591 /*FIXME*//*D*//* reasonable stack size! */ 603 /*FIXME*//*D*//* reasonable stack size! */
592 stack->ssize = - (8192 * sizeof (long)); 604 stack->ssize = - (STACKSIZE * sizeof (long));
593 New (0, stack->sptr, 8192, long); 605 New (0, stack->sptr, STACKSIZE, long);
594 } 606 }
595 } 607 }
596 else 608 else
597 stack->sptr = 0; 609 stack->sptr = 0;
598 610
979 croak ("Coro::State::_newprocess expects an arrayref"); 991 croak ("Coro::State::_newprocess expects an arrayref");
980 992
981 Newz (0, coro, 1, struct coro); 993 Newz (0, coro, 1, struct coro);
982 994
983 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 995 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
984 coro->mainstack = 0; /* actual work is done inside transfer */ 996 /*coro->mainstack = 0; *//*actual work is done inside transfer */
985 coro->stack = 0; 997 /*coro->stack = 0;*/
986 998
987 /* same as JMPENV_BOOTSTRAP */ 999 /* same as JMPENV_BOOTSTRAP */
988 /* we might be able to recycle start_env, but safe is safe */ 1000 /* we might be able to recycle start_env, but safe is safe */
989 //Zero(&coro->start_env, 1, JMPENV); 1001 /*Zero(&coro->start_env, 1, JMPENV);*/
990 coro->start_env.je_ret = -1; 1002 coro->start_env.je_ret = -1;
991 coro->start_env.je_mustcatch = TRUE; 1003 coro->start_env.je_mustcatch = TRUE;
992 1004
993 RETVAL = coro; 1005 RETVAL = coro;
994 OUTPUT: 1006 OUTPUT:
1015 if (coro->mainstack && coro->mainstack != main_mainstack) 1027 if (coro->mainstack && coro->mainstack != main_mainstack)
1016 { 1028 {
1017 struct coro temp; 1029 struct coro temp;
1018 1030
1019 PUTBACK; 1031 PUTBACK;
1020 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 1032 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
1021 LOAD(aTHX_ coro); 1033 LOAD (aTHX_ coro);
1022 SPAGAIN; 1034 SPAGAIN;
1023 1035
1024 destroy_stacks (aTHX); 1036 destroy_stacks (aTHX);
1025 1037
1026 LOAD((&temp)); /* this will get rid of defsv etc.. */ 1038 LOAD ((&temp)); /* this will get rid of defsv etc.. */
1027 SPAGAIN; 1039 SPAGAIN;
1028 1040
1029 coro->mainstack = 0; 1041 coro->mainstack = 0;
1030 } 1042 }
1031 1043
1046 1058
1047void 1059void
1048yield(...) 1060yield(...)
1049 PROTOTYPE: @ 1061 PROTOTYPE: @
1050 CODE: 1062 CODE:
1051 static SV *returnstk; 1063 SV *yieldstack;
1052 SV *sv; 1064 SV *sv;
1053 AV *defav = GvAV (PL_defgv); 1065 AV *defav = GvAV (PL_defgv);
1054 struct coro *prev, *next; 1066 struct coro *prev, *next;
1055 1067
1056 if (!returnstk) 1068 yieldstack = *hv_fetch (
1057 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); 1069 (HV *)SvRV (GvSV (coro_current)),
1070 "yieldstack", sizeof ("yieldstack") - 1,
1071 0
1072 );
1058 1073
1059 /* set up @_ -- ugly */ 1074 /* set up @_ -- ugly */
1060 av_clear (defav); 1075 av_clear (defav);
1061 av_fill (defav, items - 1); 1076 av_fill (defav, items - 1);
1062 while (items--) 1077 while (items--)
1063 av_store (defav, items, SvREFCNT_inc (ST(items))); 1078 av_store (defav, items, SvREFCNT_inc (ST(items)));
1064 1079
1065 mg_get (returnstk); /* isn't documentation wrong for mg_get? */
1066 sv = av_pop ((AV *)SvRV (returnstk)); 1080 sv = av_pop ((AV *)SvRV (yieldstack));
1067 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 1081 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)));
1068 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1082 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
1069 SvREFCNT_dec (sv); 1083 SvREFCNT_dec (sv);
1070 1084
1071 transfer(aTHX_ prev, next, 0); 1085 transfer (aTHX_ prev, next, 0);
1072 1086
1073MODULE = Coro::State PACKAGE = Coro 1087MODULE = Coro::State PACKAGE = Coro
1074 1088
1075# this is slightly dirty (should expose a c-level api) 1089# this is slightly dirty (should expose a c-level api)
1076 1090

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines