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.104 by root, Mon Nov 27 01:51:02 2006 UTC vs.
Revision 1.109 by root, Mon Nov 27 18:45:29 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines