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.103 by root, Mon Nov 27 01:33:30 2006 UTC vs.
Revision 1.110 by root, Tue Nov 28 23:08:07 2006 UTC

3#include "EXTERN.h" 3#include "EXTERN.h"
4#include "perl.h" 4#include "perl.h"
5#include "XSUB.h" 5#include "XSUB.h"
6 6
7#include "patchlevel.h" 7#include "patchlevel.h"
8
9#if USE_VALGRIND
10# include <valgrind/valgrind.h>
11#endif
8 12
9#define PERL_VERSION_ATLEAST(a,b,c) \ 13#define PERL_VERSION_ATLEAST(a,b,c) \
10 (PERL_REVISION > (a) \ 14 (PERL_REVISION > (a) \
11 || (PERL_REVISION == (a) \ 15 || (PERL_REVISION == (a) \
12 && (PERL_VERSION > (b) \ 16 && (PERL_VERSION > (b) \
58# ifndef PAGESIZE 62# ifndef PAGESIZE
59# define PAGESIZE pagesize 63# define PAGESIZE pagesize
60# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) 64# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
61static long pagesize; 65static long pagesize;
62# else 66# else
63# define BOOT_PAGESIZE 67# define BOOT_PAGESIZE (void)0
64# endif 68# endif
69#else
70# define PAGESIZE 0
71# define BOOT_PAGESIZE (void)0
65#endif 72#endif
66 73
67/* The next macro should declare a variable stacklevel that contains and approximation 74/* The next macro should declare a variable stacklevel that contains and approximation
68 * to the current C stack pointer. Its property is that it changes with each call 75 * to the current C stack pointer. Its property is that it changes with each call
69 * and should be unique. */ 76 * and should be unique. */
96static struct CoroAPI coroapi; 103static struct CoroAPI coroapi;
97static AV *main_mainstack; /* used to differentiate between $main and others */ 104static AV *main_mainstack; /* used to differentiate between $main and others */
98static HV *coro_state_stash, *coro_stash; 105static HV *coro_state_stash, *coro_stash;
99static SV *coro_mortal; /* will be freed after next transfer */ 106static SV *coro_mortal; /* will be freed after next transfer */
100 107
108static struct coro_cctx *cctx_first;
109static int cctx_count, cctx_idle;
110
101/* this is a structure representing a c-level coroutine */ 111/* this is a structure representing a c-level coroutine */
102typedef struct coro_stack { 112typedef struct coro_cctx {
103 struct coro_stack *next; 113 struct coro_cctx *next;
104 114
105 /* the stack */ 115 /* the stack */
106 void *sptr; 116 void *sptr;
107 long ssize; /* positive == mmap, otherwise malloc */ 117 long ssize; /* positive == mmap, otherwise malloc */
108 118
109 /* cpu state */ 119 /* cpu state */
110 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 120 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
111 JMPENV *top_env; 121 JMPENV *top_env;
112 coro_context cctx; 122 coro_context cctx;
113} coro_stack; 123
124#if USE_VALGRIND
125 int valgrind_id;
126#endif
127} coro_cctx;
114 128
115/* this is a structure representing a perl-level coroutine */ 129/* this is a structure representing a perl-level coroutine */
116struct coro { 130struct coro {
117 /* the c coroutine allocated to this perl coroutine, if any */ 131 /* the c coroutine allocated to this perl coroutine, if any */
118 coro_stack *stack; 132 coro_cctx *cctx;
119 133
120 /* data associated with this coroutine (initial args) */ 134 /* data associated with this coroutine (initial args) */
121 AV *args; 135 AV *args;
122 int refcnt; 136 int refcnt;
123 137
124 /* optionally saved, might be zero */ 138 /* optionally saved, might be zero */
125 AV *defav; 139 AV *defav;
126 SV *defsv; 140 SV *defsv;
127 SV *errsv; 141 SV *errsv;
128 142
129 /* saved global state not related to stacks */ 143#define VAR(name,type) type name;
130 U8 dowarn; 144# include "state.h"
131 I32 in_eval; 145#undef VAR
132
133 /* the stacks and related info (callchain etc..) */
134 PERL_SI *curstackinfo;
135 AV *curstack;
136 AV *mainstack;
137 SV **stack_sp;
138 OP *op;
139 SV **curpad;
140 AV *comppad;
141 CV *compcv;
142 SV **stack_base;
143 SV **stack_max;
144 SV **tmps_stack;
145 I32 tmps_floor;
146 I32 tmps_ix;
147 I32 tmps_max;
148 I32 *markstack;
149 I32 *markstack_ptr;
150 I32 *markstack_max;
151 I32 *scopestack;
152 I32 scopestack_ix;
153 I32 scopestack_max;
154 ANY *savestack;
155 I32 savestack_ix;
156 I32 savestack_max;
157 OP **retstack;
158 I32 retstack_ix;
159 I32 retstack_max;
160 PMOP *curpm;
161 COP *curcop;
162 146
163 /* coro process data */ 147 /* coro process data */
164 int prio; 148 int prio;
165}; 149};
166 150
230 214
231#define PERL_MAGIC_coro PERL_MAGIC_ext 215#define PERL_MAGIC_coro PERL_MAGIC_ext
232 216
233static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 217static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
234 218
219#define CORO_MAGIC(cv) \
220 SvMAGIC (cv) \
221 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
222 ? SvMAGIC (cv) \
223 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
224 : 0
225
235/* the next two functions merely cache the padlists */ 226/* the next two functions merely cache the padlists */
236static void 227static void
237get_padlist (CV *cv) 228get_padlist (CV *cv)
238{ 229{
239 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 230 MAGIC *mg = CORO_MAGIC (cv);
231 AV *av;
240 232
241 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 233 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
242 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 234 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
243 else 235 else
244 { 236 {
245#if 0 237#if 0
246 /* this is probably cleaner, but also slower? */ 238 /* this is probably cleaner, but also slower? */
247 CV *cp = Perl_cv_clone (cv); 239 CV *cp = Perl_cv_clone (cv);
255} 247}
256 248
257static void 249static void
258put_padlist (CV *cv) 250put_padlist (CV *cv)
259{ 251{
260 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 252 MAGIC *mg = CORO_MAGIC (cv);
253 AV *av;
261 254
262 if (!mg) 255 if (!mg)
263 { 256 {
264 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 257 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
265 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 258 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
266 mg->mg_virtual = &vtbl_coro; 259 mg->mg_virtual = &vtbl_coro;
267 mg->mg_obj = (SV *)newAV (); 260 mg->mg_obj = (SV *)newAV ();
268 } 261 }
269 262
270 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); 263 av = (AV *)mg->mg_obj;
264
265 if (AvFILLp (av) >= AvMAX (av))
266 av_extend (av, AvMAX (av) + 1);
267
268 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
271} 269}
272 270
273#define SB do { 271#define SB do {
274#define SE } while (0) 272#define SE } while (0)
275 273
279#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 277#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
280 278
281static void 279static void
282load_state(Coro__State c) 280load_state(Coro__State c)
283{ 281{
284 PL_dowarn = c->dowarn; 282#define VAR(name,type) PL_ ## name = c->name;
285 PL_in_eval = c->in_eval; 283# include "state.h"
286 284#undef VAR
287 PL_curstackinfo = c->curstackinfo;
288 PL_curstack = c->curstack;
289 PL_mainstack = c->mainstack;
290 PL_stack_sp = c->stack_sp;
291 PL_op = c->op;
292 PL_curpad = c->curpad;
293 PL_comppad = c->comppad;
294 PL_compcv = c->compcv;
295 PL_stack_base = c->stack_base;
296 PL_stack_max = c->stack_max;
297 PL_tmps_stack = c->tmps_stack;
298 PL_tmps_floor = c->tmps_floor;
299 PL_tmps_ix = c->tmps_ix;
300 PL_tmps_max = c->tmps_max;
301 PL_markstack = c->markstack;
302 PL_markstack_ptr = c->markstack_ptr;
303 PL_markstack_max = c->markstack_max;
304 PL_scopestack = c->scopestack;
305 PL_scopestack_ix = c->scopestack_ix;
306 PL_scopestack_max = c->scopestack_max;
307 PL_savestack = c->savestack;
308 PL_savestack_ix = c->savestack_ix;
309 PL_savestack_max = c->savestack_max;
310#if !PERL_VERSION_ATLEAST (5,9,0)
311 PL_retstack = c->retstack;
312 PL_retstack_ix = c->retstack_ix;
313 PL_retstack_max = c->retstack_max;
314#endif
315 PL_curpm = c->curpm;
316 PL_curcop = c->curcop;
317 285
318 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 286 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
319 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 287 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
320 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 288 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
321 289
324 CV *cv; 292 CV *cv;
325 293
326 /* now do the ugly restore mess */ 294 /* now do the ugly restore mess */
327 while ((cv = (CV *)POPs)) 295 while ((cv = (CV *)POPs))
328 { 296 {
329 AV *padlist = (AV *)POPs;
330
331 if (padlist)
332 {
333 put_padlist (cv); /* mark this padlist as available */ 297 put_padlist (cv); /* mark this padlist as available */
334 CvPADLIST(cv) = padlist; 298 CvDEPTH (cv) = PTR2IV (POPs);
335 } 299 CvPADLIST (cv) = (AV *)POPs;
336
337 ++CvDEPTH(cv);
338 } 300 }
339 301
340 PUTBACK; 302 PUTBACK;
341 } 303 }
342} 304}
364 PERL_CONTEXT *cx = &ccstk[cxix--]; 326 PERL_CONTEXT *cx = &ccstk[cxix--];
365 327
366 if (CxTYPE(cx) == CXt_SUB) 328 if (CxTYPE(cx) == CXt_SUB)
367 { 329 {
368 CV *cv = cx->blk_sub.cv; 330 CV *cv = cx->blk_sub.cv;
331
369 if (CvDEPTH(cv)) 332 if (CvDEPTH (cv))
370 { 333 {
371 EXTEND (SP, CvDEPTH(cv)*2); 334 EXTEND (SP, 3);
372
373 while (--CvDEPTH(cv))
374 {
375 /* this tells the restore code to increment CvDEPTH */
376 PUSHs (Nullsv);
377 PUSHs ((SV *)cv);
378 }
379 335
380 PUSHs ((SV *)CvPADLIST(cv)); 336 PUSHs ((SV *)CvPADLIST(cv));
337 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
381 PUSHs ((SV *)cv); 338 PUSHs ((SV *)cv);
382 339
340 CvDEPTH (cv) = 0;
383 get_padlist (cv); 341 get_padlist (cv);
384 } 342 }
385 } 343 }
386#ifdef CXt_FORMAT 344#ifdef CXt_FORMAT
387 else if (CxTYPE(cx) == CXt_FORMAT) 345 else if (CxTYPE(cx) == CXt_FORMAT)
406 364
407 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 365 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
408 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 366 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
409 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 367 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
410 368
411 c->dowarn = PL_dowarn; 369#define VAR(name,type)c->name = PL_ ## name;
412 c->in_eval = PL_in_eval; 370# include "state.h"
413 371#undef VAR
414 c->curstackinfo = PL_curstackinfo;
415 c->curstack = PL_curstack;
416 c->mainstack = PL_mainstack;
417 c->stack_sp = PL_stack_sp;
418 c->op = PL_op;
419 c->curpad = PL_curpad;
420 c->comppad = PL_comppad;
421 c->compcv = PL_compcv;
422 c->stack_base = PL_stack_base;
423 c->stack_max = PL_stack_max;
424 c->tmps_stack = PL_tmps_stack;
425 c->tmps_floor = PL_tmps_floor;
426 c->tmps_ix = PL_tmps_ix;
427 c->tmps_max = PL_tmps_max;
428 c->markstack = PL_markstack;
429 c->markstack_ptr = PL_markstack_ptr;
430 c->markstack_max = PL_markstack_max;
431 c->scopestack = PL_scopestack;
432 c->scopestack_ix = PL_scopestack_ix;
433 c->scopestack_max = PL_scopestack_max;
434 c->savestack = PL_savestack;
435 c->savestack_ix = PL_savestack_ix;
436 c->savestack_max = PL_savestack_max;
437#if !PERL_VERSION_ATLEAST (5,9,0)
438 c->retstack = PL_retstack;
439 c->retstack_ix = PL_retstack_ix;
440 c->retstack_max = PL_retstack_max;
441#endif
442 c->curpm = PL_curpm;
443 c->curcop = PL_curcop;
444} 372}
445 373
446/* 374/*
447 * allocate various perl stacks. This is an exact copy 375 * allocate various perl stacks. This is an exact copy
448 * of perl.c:init_stacks, except that it uses less memory 376 * of perl.c:init_stacks, except that it uses less memory
491 419
492/* 420/*
493 * destroy the stacks, the callchain etc... 421 * destroy the stacks, the callchain etc...
494 */ 422 */
495static void 423static void
496coro_destroy_stacks() 424coro_destroy_stacks ()
497{ 425{
498 if (!IN_DESTRUCT) 426 if (!IN_DESTRUCT)
499 { 427 {
500 /* is this ugly, I ask? */ 428 /* is this ugly, I ask? */
501 LEAVE_SCOPE (0); 429 LEAVE_SCOPE (0);
567 myop.op_flags = OPf_WANT_VOID; 495 myop.op_flags = OPf_WANT_VOID;
568 496
569 PL_op = (OP *)&myop; 497 PL_op = (OP *)&myop;
570 498
571 PUSHMARK (SP); 499 PUSHMARK (SP);
572 XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE)); 500 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
573 PUTBACK; 501 PUTBACK;
574 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 502 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
575 SPAGAIN; 503 SPAGAIN;
576 504
577 ENTER; /* necessary e.g. for dounwind */ 505 ENTER; /* necessary e.g. for dounwind */
587 coro_mortal = 0; 515 coro_mortal = 0;
588 } 516 }
589} 517}
590 518
591static void NOINLINE 519static void NOINLINE
592prepare_cctx (coro_stack *cctx) 520prepare_cctx (coro_cctx *cctx)
593{ 521{
594 dSP; 522 dSP;
595 LOGOP myop; 523 LOGOP myop;
596 524
597 Zero (&myop, 1, LOGOP); 525 Zero (&myop, 1, LOGOP);
598 myop.op_next = PL_op; 526 myop.op_next = PL_op;
599 myop.op_flags = OPf_WANT_VOID; 527 myop.op_flags = OPf_WANT_VOID;
600 528
601 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx)); 529 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
602 530
603 PUSHMARK (SP); 531 PUSHMARK (SP);
604 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); 532 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
605 PUTBACK; 533 PUTBACK;
606 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 534 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
607 SPAGAIN; 535 SPAGAIN;
608} 536}
609 537
615 543
616 /* 544 /*
617 * this is a _very_ stripped down perl interpreter ;) 545 * this is a _very_ stripped down perl interpreter ;)
618 */ 546 */
619 PL_top_env = &PL_start_env; 547 PL_top_env = &PL_start_env;
548
620 /* inject call to cctx_init */ 549 /* inject call to cctx_init */
621 prepare_cctx ((coro_stack *)arg); 550 prepare_cctx ((coro_cctx *)arg);
622 551
623 /* somebody will hit me for both perl_run and PL_restartop */ 552 /* somebody will hit me for both perl_run and PL_restartop */
624 perl_run (PL_curinterp); 553 perl_run (PL_curinterp);
625 554
626 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); 555 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
627 abort (); 556 abort ();
628} 557}
629 558
630static coro_stack * 559static coro_cctx *
631stack_new () 560cctx_new ()
632{ 561{
633 coro_stack *stack; 562 coro_cctx *cctx;
634 563
564 ++cctx_count;
565
635 New (0, stack, 1, coro_stack); 566 New (0, cctx, 1, coro_cctx);
636 567
637#if HAVE_MMAP 568#if HAVE_MMAP
638 569
639 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 570 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
640 /* mmap suppsedly does allocate-on-write for us */ 571 /* mmap suppsedly does allocate-on-write for us */
641 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 572 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
642 573
643 if (stack->sptr == (void *)-1) 574 if (cctx->sptr == (void *)-1)
644 { 575 {
645 perror ("FATAL: unable to mmap stack for coroutine"); 576 perror ("FATAL: unable to mmap stack for coroutine");
646 _exit (EXIT_FAILURE); 577 _exit (EXIT_FAILURE);
647 } 578 }
648 579
649# if STACKGUARD 580# if STACKGUARD
650 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 581 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
651# endif 582# endif
652 583
653#else 584#else
654 585
655 stack->ssize = STACKSIZE * (long)sizeof (long); 586 cctx->ssize = STACKSIZE * (long)sizeof (long);
656 New (0, stack->sptr, STACKSIZE, long); 587 New (0, cctx->sptr, STACKSIZE, long);
657 588
658 if (!stack->sptr) 589 if (!cctx->sptr)
659 { 590 {
660 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 591 perror ("FATAL: unable to malloc stack for coroutine");
661 _exit (EXIT_FAILURE); 592 _exit (EXIT_FAILURE);
662 } 593 }
663 594
664#endif 595#endif
665 596
597#if USE_VALGRIND
598 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
599 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
600 cctx->ssize + (char *)cctx->sptr
601 );
602#endif
603
666 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); 604 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
667 605
668 return stack; 606 return cctx;
669} 607}
670 608
671static void 609static void
672stack_free (coro_stack *stack) 610cctx_free (coro_cctx *cctx)
673{ 611{
674 if (!stack) 612 if (!cctx)
675 return; 613 return;
676 614
615 --cctx_count;
616
617#if USE_VALGRIND
618 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
619#endif
620
677#if HAVE_MMAP 621#if HAVE_MMAP
678 munmap (stack->sptr, stack->ssize); 622 munmap (cctx->sptr, cctx->ssize);
679#else 623#else
680 Safefree (stack->sptr); 624 Safefree (cctx->sptr);
681#endif 625#endif
682 626
683 Safefree (stack); 627 Safefree (cctx);
684} 628}
685 629
686static coro_stack *stack_first;
687static int cctx_count, cctx_idle;
688
689static coro_stack * 630static coro_cctx *
690stack_get () 631cctx_get ()
691{ 632{
692 coro_stack *stack; 633 coro_cctx *cctx;
693 634
694 if (stack_first) 635 if (cctx_first)
695 { 636 {
696 --cctx_idle; 637 --cctx_idle;
697 stack = stack_first; 638 cctx = cctx_first;
698 stack_first = stack->next; 639 cctx_first = cctx->next;
699 } 640 }
700 else 641 else
701 { 642 {
702 ++cctx_count; 643 cctx = cctx_new ();
703 stack = stack_new ();
704 PL_op = PL_op->op_next; 644 PL_op = PL_op->op_next;
705 } 645 }
706 646
707 return stack; 647 return cctx;
708} 648}
709 649
710static void 650static void
711stack_put (coro_stack *stack) 651cctx_put (coro_cctx *cctx)
712{ 652{
713 ++cctx_idle; 653 ++cctx_idle;
714 stack->next = stack_first; 654 cctx->next = cctx_first;
715 stack_first = stack; 655 cctx_first = cctx;
716} 656}
717 657
718/* never call directly, always through the coro_state_transfer global variable */ 658/* never call directly, always through the coro_state_transfer global variable */
719static void NOINLINE 659static void NOINLINE
720transfer (struct coro *prev, struct coro *next, int flags) 660transfer (struct coro *prev, struct coro *next, int flags)
721{ 661{
722 dSTACKLEVEL; 662 dSTACKLEVEL;
723 663
724 /* sometimes transfer is only called to set idle_sp */ 664 /* sometimes transfer is only called to set idle_sp */
725 if (flags == TRANSFER_SET_STACKLEVEL) 665 if (flags == TRANSFER_SET_STACKLEVEL)
726 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 666 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
727 else if (prev != next) 667 else if (prev != next)
728 { 668 {
729 coro_stack *prev__stack; 669 coro_cctx *prev__cctx;
730 670
731 LOCK; 671 LOCK;
732 672
733 if (next->mainstack) 673 if (next->mainstack)
734 { 674 {
742 /* first get rid of the old state */ 682 /* first get rid of the old state */
743 SAVE (prev, -1); 683 SAVE (prev, -1);
744 /* setup coroutine call */ 684 /* setup coroutine call */
745 setup_coro (next); 685 setup_coro (next);
746 /* need a stack */ 686 /* need a stack */
747 next->stack = 0; 687 next->cctx = 0;
748 } 688 }
749 689
750 if (!prev->stack) 690 if (!prev->cctx)
751 /* create a new empty context */ 691 /* create a new empty context */
752 Newz (0, prev->stack, 1, coro_stack); 692 Newz (0, prev->cctx, 1, coro_cctx);
753 693
754 prev__stack = prev->stack; 694 prev__cctx = prev->cctx;
755 695
756 /* possibly "free" the stack */ 696 /* possibly "free" the cctx */
757 if (prev__stack->idle_sp == STACKLEVEL) 697 if (prev__cctx->idle_sp == STACKLEVEL)
758 { 698 {
699 assert (PL_top_env == prev__cctx->top_env);//D
759 stack_put (prev__stack); 700 cctx_put (prev__cctx);
760 prev->stack = 0; 701 prev->cctx = 0;
761 } 702 }
762 703
763 if (!next->stack) 704 if (!next->cctx)
764 next->stack = stack_get (); 705 next->cctx = cctx_get ();
765 706
766 if (prev__stack != next->stack) 707 if (prev__cctx != next->cctx)
767 { 708 {
768 prev__stack->top_env = PL_top_env; 709 prev__cctx->top_env = PL_top_env;
769 PL_top_env = next->stack->top_env; 710 PL_top_env = next->cctx->top_env;
770 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 711 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
771 } 712 }
772 713
773 free_coro_mortal (); 714 free_coro_mortal ();
774 715
775 UNLOCK; 716 UNLOCK;
802 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 743 LOAD ((&temp)); /* this will get rid of defsv etc.. */
803 744
804 coro->mainstack = 0; 745 coro->mainstack = 0;
805 } 746 }
806 747
807 stack_free (coro->stack); 748 cctx_free (coro->cctx);
808 SvREFCNT_dec (coro->args); 749 SvREFCNT_dec (coro->args);
809 Safefree (coro); 750 Safefree (coro);
810} 751}
811 752
812static int 753static int
1056 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 997 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1057 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 998 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1058 999
1059 for (i = 1; i < items; i++) 1000 for (i = 1; i < items; i++)
1060 av_push (coro->args, newSVsv (ST (i))); 1001 av_push (coro->args, newSVsv (ST (i)));
1061
1062 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1063 /*coro->stack = 0;*/
1064} 1002}
1065 OUTPUT: 1003 OUTPUT:
1066 RETVAL 1004 RETVAL
1067 1005
1068void 1006void
1077 struct transfer_args ta; 1015 struct transfer_args ta;
1078 1016
1079 switch (ix) 1017 switch (ix)
1080 { 1018 {
1081 case 0: 1019 case 0:
1082 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1020 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1083 ta.next = 0; 1021 ta.next = 0;
1084 ta.flags = TRANSFER_SET_STACKLEVEL; 1022 ta.flags = TRANSFER_SET_STACKLEVEL;
1085 break; 1023 break;
1086 1024
1087 case 1: 1025 case 1:
1147 int code 1085 int code
1148 PROTOTYPE: $ 1086 PROTOTYPE: $
1149 CODE: 1087 CODE:
1150 _exit (code); 1088 _exit (code);
1151 1089
1090int
1091cctx_count ()
1092 CODE:
1093 RETVAL = cctx_count;
1094 OUTPUT:
1095 RETVAL
1096
1097int
1098cctx_idle ()
1099 CODE:
1100 RETVAL = cctx_idle;
1101 OUTPUT:
1102 RETVAL
1103
1152MODULE = Coro::State PACKAGE = Coro 1104MODULE = Coro::State PACKAGE = Coro
1153 1105
1154BOOT: 1106BOOT:
1155{ 1107{
1156 int i; 1108 int i;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines