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.111 by root, Thu Nov 30 18:21:14 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 int inuse;
125
126#if USE_VALGRIND
127 int valgrind_id;
128#endif
129} coro_cctx;
130
131enum {
132 CF_RUNNING, /* coroutine is running */
133 CF_READY, /* coroutine is ready */
134};
114 135
115/* this is a structure representing a perl-level coroutine */ 136/* this is a structure representing a perl-level coroutine */
116struct coro { 137struct coro {
117 /* the c coroutine allocated to this perl coroutine, if any */ 138 /* the c coroutine allocated to this perl coroutine, if any */
118 coro_stack *stack; 139 coro_cctx *cctx;
119 140
120 /* data associated with this coroutine (initial args) */ 141 /* data associated with this coroutine (initial args) */
121 AV *args; 142 AV *args;
122 int refcnt; 143 int refcnt;
144 int flags;
123 145
124 /* optionally saved, might be zero */ 146 /* optionally saved, might be zero */
125 AV *defav; 147 AV *defav;
126 SV *defsv; 148 SV *defsv;
127 SV *errsv; 149 SV *errsv;
128 150
129 /* saved global state not related to stacks */ 151#define VAR(name,type) type name;
130 U8 dowarn; 152# include "state.h"
131 I32 in_eval; 153#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 154
163 /* coro process data */ 155 /* coro process data */
164 int prio; 156 int prio;
165}; 157};
166 158
230 222
231#define PERL_MAGIC_coro PERL_MAGIC_ext 223#define PERL_MAGIC_coro PERL_MAGIC_ext
232 224
233static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 225static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
234 226
227#define CORO_MAGIC(cv) \
228 SvMAGIC (cv) \
229 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
230 ? SvMAGIC (cv) \
231 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
232 : 0
233
235/* the next two functions merely cache the padlists */ 234/* the next two functions merely cache the padlists */
236static void 235static void
237get_padlist (CV *cv) 236get_padlist (CV *cv)
238{ 237{
239 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 238 MAGIC *mg = CORO_MAGIC (cv);
239 AV *av;
240 240
241 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 241 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
242 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 242 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
243 else 243 else
244 { 244 {
245#if 0 245#if 0
246 /* this is probably cleaner, but also slower? */ 246 /* this is probably cleaner, but also slower? */
247 CV *cp = Perl_cv_clone (cv); 247 CV *cp = Perl_cv_clone (cv);
255} 255}
256 256
257static void 257static void
258put_padlist (CV *cv) 258put_padlist (CV *cv)
259{ 259{
260 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 260 MAGIC *mg = CORO_MAGIC (cv);
261 AV *av;
261 262
262 if (!mg) 263 if (!mg)
263 { 264 {
264 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 265 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
265 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 266 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
266 mg->mg_virtual = &vtbl_coro; 267 mg->mg_virtual = &vtbl_coro;
267 mg->mg_obj = (SV *)newAV (); 268 mg->mg_obj = (SV *)newAV ();
268 } 269 }
269 270
270 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); 271 av = (AV *)mg->mg_obj;
272
273 if (AvFILLp (av) >= AvMAX (av))
274 av_extend (av, AvMAX (av) + 1);
275
276 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
271} 277}
272 278
273#define SB do { 279#define SB do {
274#define SE } while (0) 280#define SE } while (0)
275 281
279#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 285#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
280 286
281static void 287static void
282load_state(Coro__State c) 288load_state(Coro__State c)
283{ 289{
284 PL_dowarn = c->dowarn; 290#define VAR(name,type) PL_ ## name = c->name;
285 PL_in_eval = c->in_eval; 291# include "state.h"
286 292#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 293
318 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 294 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
319 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 295 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
320 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 296 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
321 297
324 CV *cv; 300 CV *cv;
325 301
326 /* now do the ugly restore mess */ 302 /* now do the ugly restore mess */
327 while ((cv = (CV *)POPs)) 303 while ((cv = (CV *)POPs))
328 { 304 {
329 AV *padlist = (AV *)POPs;
330
331 if (padlist)
332 {
333 put_padlist (cv); /* mark this padlist as available */ 305 put_padlist (cv); /* mark this padlist as available */
334 CvPADLIST(cv) = padlist; 306 CvDEPTH (cv) = PTR2IV (POPs);
335 } 307 CvPADLIST (cv) = (AV *)POPs;
336
337 ++CvDEPTH(cv);
338 } 308 }
339 309
340 PUTBACK; 310 PUTBACK;
341 } 311 }
342} 312}
364 PERL_CONTEXT *cx = &ccstk[cxix--]; 334 PERL_CONTEXT *cx = &ccstk[cxix--];
365 335
366 if (CxTYPE(cx) == CXt_SUB) 336 if (CxTYPE(cx) == CXt_SUB)
367 { 337 {
368 CV *cv = cx->blk_sub.cv; 338 CV *cv = cx->blk_sub.cv;
339
369 if (CvDEPTH(cv)) 340 if (CvDEPTH (cv))
370 { 341 {
371 EXTEND (SP, CvDEPTH(cv)*2); 342 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 343
380 PUSHs ((SV *)CvPADLIST(cv)); 344 PUSHs ((SV *)CvPADLIST(cv));
345 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
381 PUSHs ((SV *)cv); 346 PUSHs ((SV *)cv);
382 347
348 CvDEPTH (cv) = 0;
383 get_padlist (cv); 349 get_padlist (cv);
384 } 350 }
385 } 351 }
386#ifdef CXt_FORMAT 352#ifdef CXt_FORMAT
387 else if (CxTYPE(cx) == CXt_FORMAT) 353 else if (CxTYPE(cx) == CXt_FORMAT)
406 372
407 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 373 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
408 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 374 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
409 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 375 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
410 376
411 c->dowarn = PL_dowarn; 377#define VAR(name,type)c->name = PL_ ## name;
412 c->in_eval = PL_in_eval; 378# include "state.h"
413 379#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} 380}
445 381
446/* 382/*
447 * allocate various perl stacks. This is an exact copy 383 * allocate various perl stacks. This is an exact copy
448 * of perl.c:init_stacks, except that it uses less memory 384 * of perl.c:init_stacks, except that it uses less memory
491 427
492/* 428/*
493 * destroy the stacks, the callchain etc... 429 * destroy the stacks, the callchain etc...
494 */ 430 */
495static void 431static void
496coro_destroy_stacks() 432coro_destroy_stacks ()
497{ 433{
498 if (!IN_DESTRUCT) 434 if (!IN_DESTRUCT)
499 { 435 {
500 /* is this ugly, I ask? */ 436 /* is this ugly, I ask? */
501 LEAVE_SCOPE (0); 437 LEAVE_SCOPE (0);
567 myop.op_flags = OPf_WANT_VOID; 503 myop.op_flags = OPf_WANT_VOID;
568 504
569 PL_op = (OP *)&myop; 505 PL_op = (OP *)&myop;
570 506
571 PUSHMARK (SP); 507 PUSHMARK (SP);
572 XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE)); 508 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
573 PUTBACK; 509 PUTBACK;
574 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 510 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
575 SPAGAIN; 511 SPAGAIN;
576 512
577 ENTER; /* necessary e.g. for dounwind */ 513 ENTER; /* necessary e.g. for dounwind */
587 coro_mortal = 0; 523 coro_mortal = 0;
588 } 524 }
589} 525}
590 526
591static void NOINLINE 527static void NOINLINE
592prepare_cctx (coro_stack *cctx) 528prepare_cctx (coro_cctx *cctx)
593{ 529{
594 dSP; 530 dSP;
595 LOGOP myop; 531 LOGOP myop;
596 532
597 Zero (&myop, 1, LOGOP); 533 Zero (&myop, 1, LOGOP);
598 myop.op_next = PL_op; 534 myop.op_next = PL_op;
599 myop.op_flags = OPf_WANT_VOID; 535 myop.op_flags = OPf_WANT_VOID;
600 536
601 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx)); 537 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
602 538
603 PUSHMARK (SP); 539 PUSHMARK (SP);
604 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); 540 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
605 PUTBACK; 541 PUTBACK;
606 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 542 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
607 SPAGAIN; 543 SPAGAIN;
608} 544}
609 545
615 551
616 /* 552 /*
617 * this is a _very_ stripped down perl interpreter ;) 553 * this is a _very_ stripped down perl interpreter ;)
618 */ 554 */
619 PL_top_env = &PL_start_env; 555 PL_top_env = &PL_start_env;
556
620 /* inject call to cctx_init */ 557 /* inject call to cctx_init */
621 prepare_cctx ((coro_stack *)arg); 558 prepare_cctx ((coro_cctx *)arg);
622 559
623 /* somebody will hit me for both perl_run and PL_restartop */ 560 /* somebody will hit me for both perl_run and PL_restartop */
624 perl_run (PL_curinterp); 561 perl_run (PL_curinterp);
625 562
626 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); 563 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
627 abort (); 564 abort ();
628} 565}
629 566
630static coro_stack * 567static coro_cctx *
631stack_new () 568cctx_new ()
632{ 569{
633 coro_stack *stack; 570 coro_cctx *cctx;
634 571
572 ++cctx_count;
573
635 New (0, stack, 1, coro_stack); 574 New (0, cctx, 1, coro_cctx);
636 575
637#if HAVE_MMAP 576#if HAVE_MMAP
638 577
639 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 578 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
640 /* mmap suppsedly does allocate-on-write for us */ 579 /* 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); 580 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
642 581
643 if (stack->sptr == (void *)-1) 582 if (cctx->sptr == (void *)-1)
644 { 583 {
645 perror ("FATAL: unable to mmap stack for coroutine"); 584 perror ("FATAL: unable to mmap stack for coroutine");
646 _exit (EXIT_FAILURE); 585 _exit (EXIT_FAILURE);
647 } 586 }
648 587
649# if STACKGUARD 588# if STACKGUARD
650 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 589 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
651# endif 590# endif
652 591
653#else 592#else
654 593
655 stack->ssize = STACKSIZE * (long)sizeof (long); 594 cctx->ssize = STACKSIZE * (long)sizeof (long);
656 New (0, stack->sptr, STACKSIZE, long); 595 New (0, cctx->sptr, STACKSIZE, long);
657 596
658 if (!stack->sptr) 597 if (!cctx->sptr)
659 { 598 {
660 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 599 perror ("FATAL: unable to malloc stack for coroutine");
661 _exit (EXIT_FAILURE); 600 _exit (EXIT_FAILURE);
662 } 601 }
663 602
664#endif 603#endif
665 604
605#if USE_VALGRIND
606 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
607 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
608 cctx->ssize + (char *)cctx->sptr
609 );
610#endif
611
666 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); 612 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
667 613
668 return stack; 614 return cctx;
669} 615}
670 616
671static void 617static void
672stack_free (coro_stack *stack) 618cctx_free (coro_cctx *cctx)
673{ 619{
674 if (!stack) 620 if (!cctx)
675 return; 621 return;
676 622
623 --cctx_count;
624
625#if USE_VALGRIND
626 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
627#endif
628
677#if HAVE_MMAP 629#if HAVE_MMAP
678 munmap (stack->sptr, stack->ssize); 630 munmap (cctx->sptr, cctx->ssize);
679#else 631#else
680 Safefree (stack->sptr); 632 Safefree (cctx->sptr);
681#endif 633#endif
682 634
683 Safefree (stack); 635 Safefree (cctx);
684} 636}
685 637
686static coro_stack *stack_first;
687static int cctx_count, cctx_idle;
688
689static coro_stack * 638static coro_cctx *
690stack_get () 639cctx_get ()
691{ 640{
692 coro_stack *stack; 641 coro_cctx *cctx;
693 642
694 if (stack_first) 643 if (cctx_first)
695 { 644 {
696 --cctx_idle; 645 --cctx_idle;
697 stack = stack_first; 646 cctx = cctx_first;
698 stack_first = stack->next; 647 cctx_first = cctx->next;
699 } 648 }
700 else 649 else
701 { 650 {
702 ++cctx_count; 651 cctx = cctx_new ();
703 stack = stack_new ();
704 PL_op = PL_op->op_next; 652 PL_op = PL_op->op_next;
705 } 653 }
706 654
707 return stack; 655 return cctx;
708} 656}
709 657
710static void 658static void
711stack_put (coro_stack *stack) 659cctx_put (coro_cctx *cctx)
712{ 660{
713 ++cctx_idle; 661 ++cctx_idle;
714 stack->next = stack_first; 662 cctx->next = cctx_first;
715 stack_first = stack; 663 cctx_first = cctx;
716} 664}
717 665
718/* never call directly, always through the coro_state_transfer global variable */ 666/* never call directly, always through the coro_state_transfer global variable */
719static void NOINLINE 667static void NOINLINE
720transfer (struct coro *prev, struct coro *next, int flags) 668transfer (struct coro *prev, struct coro *next, int flags)
721{ 669{
722 dSTACKLEVEL; 670 dSTACKLEVEL;
723 671
724 /* sometimes transfer is only called to set idle_sp */ 672 /* sometimes transfer is only called to set idle_sp */
725 if (flags == TRANSFER_SET_STACKLEVEL) 673 if (flags == TRANSFER_SET_STACKLEVEL)
726 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 674 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
727 else if (prev != next) 675 else if (prev != next)
728 { 676 {
729 coro_stack *prev__stack; 677 coro_cctx *prev__cctx;
678
679 if (!prev->cctx)
680 {
681 /* create a new empty context */
682 Newz (0, prev->cctx, 1, coro_cctx);
683 prev->cctx->inuse = 1;
684 prev->flags |= CF_RUNNING;
685 }
686
687 assert ( prev->flags & CF_RUNNING);
688 assert (!next->flags & CF_RUNNING);
689
690 prev->flags &= ~CF_RUNNING;
691 next->flags |= CF_RUNNING;
730 692
731 LOCK; 693 LOCK;
732 694
733 if (next->mainstack) 695 if (next->mainstack)
734 { 696 {
742 /* first get rid of the old state */ 704 /* first get rid of the old state */
743 SAVE (prev, -1); 705 SAVE (prev, -1);
744 /* setup coroutine call */ 706 /* setup coroutine call */
745 setup_coro (next); 707 setup_coro (next);
746 /* need a stack */ 708 /* need a stack */
709 assert (!next->stack);
747 next->stack = 0; 710 next->cctx = 0;
748 } 711 }
749 712
750 if (!prev->stack)
751 /* create a new empty context */
752 Newz (0, prev->stack, 1, coro_stack);
753
754 prev__stack = prev->stack; 713 prev__cctx = prev->cctx;
755 714
756 /* possibly "free" the stack */ 715 /* possibly "free" the cctx */
757 if (prev__stack->idle_sp == STACKLEVEL) 716 if (prev__cctx->idle_sp == STACKLEVEL)
758 { 717 {
718 assert (PL_top_env == prev__cctx->top_env);//D
759 stack_put (prev__stack); 719 cctx_put (prev__cctx);
760 prev->stack = 0; 720 prev->cctx = 0;
761 } 721 }
762 722
763 if (!next->stack) 723 if (!next->cctx)
764 next->stack = stack_get (); 724 next->cctx = cctx_get ();
765 725
766 if (prev__stack != next->stack) 726 if (prev__cctx != next->cctx)
767 { 727 {
728 assert ( prev__cctx->inuse);
729 assert (!next->cctx->inuse);
730
731 prev__cctx->inuse = 0;
732 next->cctx->inuse = 1;
733
768 prev__stack->top_env = PL_top_env; 734 prev__cctx->top_env = PL_top_env;
769 PL_top_env = next->stack->top_env; 735 PL_top_env = next->cctx->top_env;
770 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 736 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
771 } 737 }
772 738
773 free_coro_mortal (); 739 free_coro_mortal ();
774 740
775 UNLOCK; 741 UNLOCK;
802 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 768 LOAD ((&temp)); /* this will get rid of defsv etc.. */
803 769
804 coro->mainstack = 0; 770 coro->mainstack = 0;
805 } 771 }
806 772
807 stack_free (coro->stack); 773 cctx_free (coro->cctx);
808 SvREFCNT_dec (coro->args); 774 SvREFCNT_dec (coro->args);
809 Safefree (coro); 775 Safefree (coro);
810} 776}
811 777
812static int 778static int
926 } 892 }
927 893
928 return 0; 894 return 0;
929} 895}
930 896
931static void 897static int
932api_ready (SV *coro) 898api_ready (SV *coro_sv)
933{ 899{
934 dTHX; 900 struct coro *coro;
935 901
936 if (SvROK (coro)) 902 if (SvROK (coro_sv))
937 coro = SvRV (coro); 903 coro_sv = SvRV (coro_sv);
904
905 coro = SvSTATE (coro_sv);
906 if (coro->flags & CF_READY)
907 return 0;
908
909 coro->flags |= CF_READY;
938 910
939 LOCK; 911 LOCK;
940 coro_enq (SvREFCNT_inc (coro)); 912 coro_enq (SvREFCNT_inc (coro_sv));
941 UNLOCK; 913 UNLOCK;
914
915 return 1;
916}
917
918static int
919api_is_ready (SV *coro_sv)
920{
921 struct coro *coro;
922
923 return !!(SvSTATE (coro_sv)->flags & CF_READY);
942} 924}
943 925
944static void 926static void
945prepare_schedule (struct transfer_args *ta) 927prepare_schedule (struct transfer_args *ta)
946{ 928{
982 coro_mortal = prev; 964 coro_mortal = prev;
983 965
984 ta->prev = SvSTATE (prev); 966 ta->prev = SvSTATE (prev);
985 ta->next = SvSTATE (next); 967 ta->next = SvSTATE (next);
986 ta->flags = TRANSFER_SAVE_ALL; 968 ta->flags = TRANSFER_SAVE_ALL;
969
970 ta->next->flags &= ~CF_READY;
987} 971}
988 972
989static void 973static void
990prepare_cede (struct transfer_args *ta) 974prepare_cede (struct transfer_args *ta)
991{ 975{
992 LOCK; 976 api_ready (GvSV (coro_current));
993 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
994 UNLOCK;
995 977
996 prepare_schedule (ta); 978 prepare_schedule (ta);
997} 979}
998 980
999static void 981static void
1056 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1038 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)); 1039 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1058 1040
1059 for (i = 1; i < items; i++) 1041 for (i = 1; i < items; i++)
1060 av_push (coro->args, newSVsv (ST (i))); 1042 av_push (coro->args, newSVsv (ST (i)));
1061
1062 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1063 /*coro->stack = 0;*/
1064} 1043}
1065 OUTPUT: 1044 OUTPUT:
1066 RETVAL 1045 RETVAL
1067 1046
1068void 1047void
1077 struct transfer_args ta; 1056 struct transfer_args ta;
1078 1057
1079 switch (ix) 1058 switch (ix)
1080 { 1059 {
1081 case 0: 1060 case 0:
1082 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1061 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1083 ta.next = 0; 1062 ta.next = 0;
1084 ta.flags = TRANSFER_SET_STACKLEVEL; 1063 ta.flags = TRANSFER_SET_STACKLEVEL;
1085 break; 1064 break;
1086 1065
1087 case 1: 1066 case 1:
1147 int code 1126 int code
1148 PROTOTYPE: $ 1127 PROTOTYPE: $
1149 CODE: 1128 CODE:
1150 _exit (code); 1129 _exit (code);
1151 1130
1131int
1132cctx_count ()
1133 CODE:
1134 RETVAL = cctx_count;
1135 OUTPUT:
1136 RETVAL
1137
1138int
1139cctx_idle ()
1140 CODE:
1141 RETVAL = cctx_idle;
1142 OUTPUT:
1143 RETVAL
1144
1152MODULE = Coro::State PACKAGE = Coro 1145MODULE = Coro::State PACKAGE = Coro
1153 1146
1154BOOT: 1147BOOT:
1155{ 1148{
1156 int i; 1149 int i;
1174 SV *sv = perl_get_sv("Coro::API", 1); 1167 SV *sv = perl_get_sv("Coro::API", 1);
1175 1168
1176 coroapi.schedule = api_schedule; 1169 coroapi.schedule = api_schedule;
1177 coroapi.cede = api_cede; 1170 coroapi.cede = api_cede;
1178 coroapi.ready = api_ready; 1171 coroapi.ready = api_ready;
1172 coroapi.is_ready = api_is_ready;
1179 coroapi.nready = &coro_nready; 1173 coroapi.nready = &coro_nready;
1180 coroapi.current = coro_current; 1174 coroapi.current = coro_current;
1181 1175
1182 GCoroAPI = &coroapi; 1176 GCoroAPI = &coroapi;
1183 sv_setiv (sv, (IV)&coroapi); 1177 sv_setiv (sv, (IV)&coroapi);
1203 1197
1204 coro->prio = newprio; 1198 coro->prio = newprio;
1205 } 1199 }
1206} 1200}
1207 1201
1208void 1202SV *
1209ready (SV *self) 1203ready (SV *self)
1210 PROTOTYPE: $ 1204 PROTOTYPE: $
1211 CODE: 1205 CODE:
1212 api_ready (self); 1206 RETVAL = boolSV (api_ready (self));
1207 OUTPUT:
1208 RETVAL
1209
1210SV *
1211is_ready (SV *self)
1212 PROTOTYPE: $
1213 CODE:
1214 RETVAL = boolSV (api_is_ready (self));
1215 OUTPUT:
1216 RETVAL
1213 1217
1214int 1218int
1215nready (...) 1219nready (...)
1216 PROTOTYPE: 1220 PROTOTYPE:
1217 CODE: 1221 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines