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.113 by root, Thu Nov 30 20:54:24 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
113 /* cpu state */ 119 /* cpu state */
114 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 120 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
115 JMPENV *top_env; 121 JMPENV *top_env;
116 coro_context cctx; 122 coro_context cctx;
117 123
124 int inuse;
125
118#if USE_VALGRIND 126#if USE_VALGRIND
119 int valgrind_id; 127 int valgrind_id;
120#endif 128#endif
121} coro_stack; 129} coro_cctx;
130
131enum {
132 CF_RUNNING, /* coroutine is running */
133 CF_READY, /* coroutine is ready */
134};
122 135
123/* this is a structure representing a perl-level coroutine */ 136/* this is a structure representing a perl-level coroutine */
124struct coro { 137struct coro {
125 /* the c coroutine allocated to this perl coroutine, if any */ 138 /* the c coroutine allocated to this perl coroutine, if any */
126 coro_stack *stack; 139 coro_cctx *cctx;
127 140
128 /* data associated with this coroutine (initial args) */ 141 /* data associated with this coroutine (initial args) */
129 AV *args; 142 AV *args;
130 int refcnt; 143 int refcnt;
144 int flags;
131 145
132 /* optionally saved, might be zero */ 146 /* optionally saved, might be zero */
133 AV *defav; 147 AV *defav;
134 SV *defsv; 148 SV *defsv;
135 SV *errsv; 149 SV *errsv;
136 150
137 /* saved global state not related to stacks */ 151#define VAR(name,type) type name;
138 U8 dowarn; 152# include "state.h"
139 I32 in_eval; 153#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 154
171 /* coro process data */ 155 /* coro process data */
172 int prio; 156 int prio;
173}; 157};
174 158
238 222
239#define PERL_MAGIC_coro PERL_MAGIC_ext 223#define PERL_MAGIC_coro PERL_MAGIC_ext
240 224
241static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 225static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
242 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
243/* the next two functions merely cache the padlists */ 234/* the next two functions merely cache the padlists */
244static void 235static void
245get_padlist (CV *cv) 236get_padlist (CV *cv)
246{ 237{
247 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 238 MAGIC *mg = CORO_MAGIC (cv);
239 AV *av;
248 240
249 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 241 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
250 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 242 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
251 else 243 else
252 { 244 {
253#if 0 245#if 0
254 /* this is probably cleaner, but also slower? */ 246 /* this is probably cleaner, but also slower? */
255 CV *cp = Perl_cv_clone (cv); 247 CV *cp = Perl_cv_clone (cv);
263} 255}
264 256
265static void 257static void
266put_padlist (CV *cv) 258put_padlist (CV *cv)
267{ 259{
268 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 260 MAGIC *mg = CORO_MAGIC (cv);
261 AV *av;
269 262
270 if (!mg) 263 if (!mg)
271 { 264 {
272 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 265 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
273 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 266 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
274 mg->mg_virtual = &vtbl_coro; 267 mg->mg_virtual = &vtbl_coro;
275 mg->mg_obj = (SV *)newAV (); 268 mg->mg_obj = (SV *)newAV ();
276 } 269 }
277 270
278 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);
279} 277}
280 278
281#define SB do { 279#define SB do {
282#define SE } while (0) 280#define SE } while (0)
283 281
287#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
288 286
289static void 287static void
290load_state(Coro__State c) 288load_state(Coro__State c)
291{ 289{
292 PL_dowarn = c->dowarn; 290#define VAR(name,type) PL_ ## name = c->name;
293 PL_in_eval = c->in_eval; 291# include "state.h"
294 292#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 293
326 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 294 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
327 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 295 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
328 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 296 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
329 297
332 CV *cv; 300 CV *cv;
333 301
334 /* now do the ugly restore mess */ 302 /* now do the ugly restore mess */
335 while ((cv = (CV *)POPs)) 303 while ((cv = (CV *)POPs))
336 { 304 {
337 AV *padlist = (AV *)POPs;
338
339 if (padlist)
340 {
341 put_padlist (cv); /* mark this padlist as available */ 305 put_padlist (cv); /* mark this padlist as available */
342 CvPADLIST(cv) = padlist; 306 CvDEPTH (cv) = PTR2IV (POPs);
343 } 307 CvPADLIST (cv) = (AV *)POPs;
344
345 ++CvDEPTH(cv);
346 } 308 }
347 309
348 PUTBACK; 310 PUTBACK;
349 } 311 }
350} 312}
372 PERL_CONTEXT *cx = &ccstk[cxix--]; 334 PERL_CONTEXT *cx = &ccstk[cxix--];
373 335
374 if (CxTYPE(cx) == CXt_SUB) 336 if (CxTYPE(cx) == CXt_SUB)
375 { 337 {
376 CV *cv = cx->blk_sub.cv; 338 CV *cv = cx->blk_sub.cv;
339
377 if (CvDEPTH(cv)) 340 if (CvDEPTH (cv))
378 { 341 {
379 EXTEND (SP, CvDEPTH(cv)*2); 342 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 343
388 PUSHs ((SV *)CvPADLIST(cv)); 344 PUSHs ((SV *)CvPADLIST(cv));
345 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
389 PUSHs ((SV *)cv); 346 PUSHs ((SV *)cv);
390 347
348 CvDEPTH (cv) = 0;
391 get_padlist (cv); 349 get_padlist (cv);
392 } 350 }
393 } 351 }
394#ifdef CXt_FORMAT 352#ifdef CXt_FORMAT
395 else if (CxTYPE(cx) == CXt_FORMAT) 353 else if (CxTYPE(cx) == CXt_FORMAT)
414 372
415 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;
416 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 374 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
417 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 375 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
418 376
419 c->dowarn = PL_dowarn; 377#define VAR(name,type)c->name = PL_ ## name;
420 c->in_eval = PL_in_eval; 378# include "state.h"
421 379#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} 380}
453 381
454/* 382/*
455 * allocate various perl stacks. This is an exact copy 383 * allocate various perl stacks. This is an exact copy
456 * of perl.c:init_stacks, except that it uses less memory 384 * of perl.c:init_stacks, except that it uses less memory
499 427
500/* 428/*
501 * destroy the stacks, the callchain etc... 429 * destroy the stacks, the callchain etc...
502 */ 430 */
503static void 431static void
504coro_destroy_stacks() 432coro_destroy_stacks ()
505{ 433{
506 if (!IN_DESTRUCT) 434 if (!IN_DESTRUCT)
507 { 435 {
508 /* is this ugly, I ask? */ 436 /* is this ugly, I ask? */
509 LEAVE_SCOPE (0); 437 LEAVE_SCOPE (0);
575 myop.op_flags = OPf_WANT_VOID; 503 myop.op_flags = OPf_WANT_VOID;
576 504
577 PL_op = (OP *)&myop; 505 PL_op = (OP *)&myop;
578 506
579 PUSHMARK (SP); 507 PUSHMARK (SP);
580 XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE)); 508 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
581 PUTBACK; 509 PUTBACK;
582 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 510 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
583 SPAGAIN; 511 SPAGAIN;
584 512
585 ENTER; /* necessary e.g. for dounwind */ 513 ENTER; /* necessary e.g. for dounwind */
595 coro_mortal = 0; 523 coro_mortal = 0;
596 } 524 }
597} 525}
598 526
599static void NOINLINE 527static void NOINLINE
600prepare_cctx (coro_stack *cctx) 528prepare_cctx (coro_cctx *cctx)
601{ 529{
602 dSP; 530 dSP;
603 LOGOP myop; 531 LOGOP myop;
604 532
605 Zero (&myop, 1, LOGOP); 533 Zero (&myop, 1, LOGOP);
606 myop.op_next = PL_op; 534 myop.op_next = PL_op;
607 myop.op_flags = OPf_WANT_VOID; 535 myop.op_flags = OPf_WANT_VOID;
608 536
609 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx)); 537 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
610 538
611 PUSHMARK (SP); 539 PUSHMARK (SP);
612 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); 540 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
613 PUTBACK; 541 PUTBACK;
614 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 542 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
615 SPAGAIN; 543 SPAGAIN;
616} 544}
617 545
623 551
624 /* 552 /*
625 * this is a _very_ stripped down perl interpreter ;) 553 * this is a _very_ stripped down perl interpreter ;)
626 */ 554 */
627 PL_top_env = &PL_start_env; 555 PL_top_env = &PL_start_env;
556
628 /* inject call to cctx_init */ 557 /* inject call to cctx_init */
629 prepare_cctx ((coro_stack *)arg); 558 prepare_cctx ((coro_cctx *)arg);
630 559
631 /* somebody will hit me for both perl_run and PL_restartop */ 560 /* somebody will hit me for both perl_run and PL_restartop */
632 perl_run (PL_curinterp); 561 perl_run (PL_curinterp);
633 562
634 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);
635 abort (); 564 abort ();
636} 565}
637 566
638static coro_stack * 567static coro_cctx *
639stack_new () 568cctx_new ()
640{ 569{
641 coro_stack *stack; 570 coro_cctx *cctx;
642 571
572 ++cctx_count;
573
643 New (0, stack, 1, coro_stack); 574 New (0, cctx, 1, coro_cctx);
644 575
645#if HAVE_MMAP 576#if HAVE_MMAP
646 577
647 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 578 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
648 /* mmap suppsedly does allocate-on-write for us */ 579 /* 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); 580 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
650 581
651 if (stack->sptr == (void *)-1) 582 if (cctx->sptr == (void *)-1)
652 { 583 {
653 perror ("FATAL: unable to mmap stack for coroutine"); 584 perror ("FATAL: unable to mmap stack for coroutine");
654 _exit (EXIT_FAILURE); 585 _exit (EXIT_FAILURE);
655 } 586 }
656 587
657# if STACKGUARD 588# if STACKGUARD
658 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 589 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
659# endif 590# endif
660 591
661#else 592#else
662 593
663 stack->ssize = STACKSIZE * (long)sizeof (long); 594 cctx->ssize = STACKSIZE * (long)sizeof (long);
664 New (0, stack->sptr, STACKSIZE, long); 595 New (0, cctx->sptr, STACKSIZE, long);
665 596
666 if (!stack->sptr) 597 if (!cctx->sptr)
667 { 598 {
668 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 599 perror ("FATAL: unable to malloc stack for coroutine");
669 _exit (EXIT_FAILURE); 600 _exit (EXIT_FAILURE);
670 } 601 }
671 602
672#endif 603#endif
673 604
674#if USE_VALGRIND 605#if USE_VALGRIND
675 stack->valgrind_id = VALGRIND_STACK_REGISTER ( 606 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
676 STACKGUARD * PAGESIZE + (char *)stack->sptr, 607 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
677 stack->ssize + (char *)stack->sptr 608 cctx->ssize + (char *)cctx->sptr
678 ); 609 );
679#endif 610#endif
680 611
681 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);
682 613
683 return stack; 614 return cctx;
684} 615}
685 616
686static void 617static void
687stack_free (coro_stack *stack) 618cctx_free (coro_cctx *cctx)
688{ 619{
689 if (!stack) 620 if (!cctx)
690 return; 621 return;
691 622
623 --cctx_count;
624
692#if USE_VALGRIND 625#if USE_VALGRIND
693 VALGRIND_STACK_DEREGISTER (stack->valgrind_id); 626 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
694#endif 627#endif
695 628
696#if HAVE_MMAP 629#if HAVE_MMAP
697 munmap (stack->sptr, stack->ssize); 630 munmap (cctx->sptr, cctx->ssize);
698#else 631#else
699 Safefree (stack->sptr); 632 Safefree (cctx->sptr);
700#endif 633#endif
701 634
702 Safefree (stack); 635 Safefree (cctx);
703} 636}
704 637
705static coro_stack *stack_first;
706static int cctx_count, cctx_idle;
707
708static coro_stack * 638static coro_cctx *
709stack_get () 639cctx_get ()
710{ 640{
711 coro_stack *stack; 641 coro_cctx *cctx;
712 642
713 if (stack_first) 643 if (cctx_first)
714 { 644 {
715 --cctx_idle; 645 --cctx_idle;
716 stack = stack_first; 646 cctx = cctx_first;
717 stack_first = stack->next; 647 cctx_first = cctx->next;
718 } 648 }
719 else 649 else
720 { 650 {
721 ++cctx_count; 651 cctx = cctx_new ();
722 stack = stack_new ();
723 PL_op = PL_op->op_next; 652 PL_op = PL_op->op_next;
724 } 653 }
725 654
726 return stack; 655 return cctx;
727} 656}
728 657
729static void 658static void
730stack_put (coro_stack *stack) 659cctx_put (coro_cctx *cctx)
731{ 660{
732 ++cctx_idle; 661 ++cctx_idle;
733 stack->next = stack_first; 662 cctx->next = cctx_first;
734 stack_first = stack; 663 cctx_first = cctx;
735} 664}
736 665
737/* never call directly, always through the coro_state_transfer global variable */ 666/* never call directly, always through the coro_state_transfer global variable */
738static void NOINLINE 667static void NOINLINE
739transfer (struct coro *prev, struct coro *next, int flags) 668transfer (struct coro *prev, struct coro *next, int flags)
740{ 669{
741 dSTACKLEVEL; 670 dSTACKLEVEL;
742 671
743 /* sometimes transfer is only called to set idle_sp */ 672 /* sometimes transfer is only called to set idle_sp */
744 if (flags == TRANSFER_SET_STACKLEVEL) 673 if (flags == TRANSFER_SET_STACKLEVEL)
745 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 674 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
746 else if (prev != next) 675 else if (prev != next)
747 { 676 {
748 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 if (!prev->flags & CF_RUNNING)
688 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
689
690 if (next->flags & CF_RUNNING)
691 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
692
693 prev->flags &= ~CF_RUNNING;
694 next->flags |= CF_RUNNING;
749 695
750 LOCK; 696 LOCK;
751 697
752 if (next->mainstack) 698 if (next->mainstack)
753 { 699 {
760 /* need to start coroutine */ 706 /* need to start coroutine */
761 /* first get rid of the old state */ 707 /* first get rid of the old state */
762 SAVE (prev, -1); 708 SAVE (prev, -1);
763 /* setup coroutine call */ 709 /* setup coroutine call */
764 setup_coro (next); 710 setup_coro (next);
765 /* need a stack */ 711 /* need a new stack */
766 next->stack = 0; 712 assert (!next->stack);
767 } 713 }
768 714
769 if (!prev->stack)
770 /* create a new empty context */
771 Newz (0, prev->stack, 1, coro_stack);
772
773 prev__stack = prev->stack; 715 prev__cctx = prev->cctx;
774 716
775 /* possibly "free" the stack */ 717 /* possibly "free" the cctx */
776 if (prev__stack->idle_sp == STACKLEVEL) 718 if (prev__cctx->idle_sp == STACKLEVEL)
777 { 719 {
720 assert (PL_top_env == prev__cctx->top_env);
721
778 stack_put (prev__stack); 722 cctx_put (prev__cctx);
779 prev->stack = 0; 723 prev->cctx = 0;
780 } 724 }
725 else if (prev__cctx->idle_sp) //D
726 fprintf (stderr, "coro %p has own stack %p-%p\n", prev__cctx, prev__cctx->idle_sp, STACKLEVEL);//D
781 727
782 if (!next->stack) 728 if (!next->cctx)
783 next->stack = stack_get (); 729 next->cctx = cctx_get ();
784 730
785 if (prev__stack != next->stack) 731 if (prev__cctx != next->cctx)
786 { 732 {
733 assert ( prev__cctx->inuse);
734 assert (!next->cctx->inuse);
735
736 prev__cctx->inuse = 0;
737 next->cctx->inuse = 1;
738
787 prev__stack->top_env = PL_top_env; 739 prev__cctx->top_env = PL_top_env;
788 PL_top_env = next->stack->top_env; 740 PL_top_env = next->cctx->top_env;
789 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 741 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
790 } 742 }
791 743
792 free_coro_mortal (); 744 free_coro_mortal ();
793 745
794 UNLOCK; 746 UNLOCK;
807coro_state_destroy (struct coro *coro) 759coro_state_destroy (struct coro *coro)
808{ 760{
809 if (coro->refcnt--) 761 if (coro->refcnt--)
810 return; 762 return;
811 763
764 if (coro->flags & CF_RUNNING)
765 croak ("FATAL: tried to destroy currently running coroutine");
766
812 if (coro->mainstack && coro->mainstack != main_mainstack) 767 if (coro->mainstack && coro->mainstack != main_mainstack)
813 { 768 {
814 struct coro temp; 769 struct coro temp;
815 770
816 SAVE ((&temp), TRANSFER_SAVE_ALL); 771 SAVE ((&temp), TRANSFER_SAVE_ALL);
821 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 776 LOAD ((&temp)); /* this will get rid of defsv etc.. */
822 777
823 coro->mainstack = 0; 778 coro->mainstack = 0;
824 } 779 }
825 780
826 stack_free (coro->stack); 781 cctx_free (coro->cctx);
827 SvREFCNT_dec (coro->args); 782 SvREFCNT_dec (coro->args);
828 Safefree (coro); 783 Safefree (coro);
829} 784}
830 785
831static int 786static int
913static GV *coro_current, *coro_idle; 868static GV *coro_current, *coro_idle;
914static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 869static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
915static int coro_nready; 870static int coro_nready;
916 871
917static void 872static void
918coro_enq (SV *sv) 873coro_enq (SV *coro_sv)
919{ 874{
920 int prio;
921
922 if (SvTYPE (sv) != SVt_PVHV)
923 croak ("Coro::ready tried to enqueue something that is not a coroutine");
924
925 prio = SvSTATE (sv)->prio;
926
927 av_push (coro_ready [prio - PRIO_MIN], sv); 875 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
928 coro_nready++; 876 coro_nready++;
929} 877}
930 878
931static SV * 879static SV *
932coro_deq (int min_prio) 880coro_deq (int min_prio)
945 } 893 }
946 894
947 return 0; 895 return 0;
948} 896}
949 897
950static void 898static int
951api_ready (SV *coro) 899api_ready (SV *coro_sv)
952{ 900{
953 dTHX; 901 struct coro *coro;
954 902
955 if (SvROK (coro)) 903 if (SvROK (coro_sv))
956 coro = SvRV (coro); 904 coro_sv = SvRV (coro_sv);
905
906 coro = SvSTATE (coro_sv);
907
908 if (coro->flags & CF_READY)
909 return 0;
910
911 if (coro->flags & CF_RUNNING)
912 croak ("Coro::ready called on currently running coroutine");
913
914 coro->flags |= CF_READY;
957 915
958 LOCK; 916 LOCK;
959 coro_enq (SvREFCNT_inc (coro)); 917 coro_enq (SvREFCNT_inc (coro_sv));
960 UNLOCK; 918 UNLOCK;
919
920 return 1;
921}
922
923static int
924api_is_ready (SV *coro_sv)
925{
926 return !!SvSTATE (coro_sv)->flags & CF_READY;
961} 927}
962 928
963static void 929static void
964prepare_schedule (struct transfer_args *ta) 930prepare_schedule (struct transfer_args *ta)
965{ 931{
1001 coro_mortal = prev; 967 coro_mortal = prev;
1002 968
1003 ta->prev = SvSTATE (prev); 969 ta->prev = SvSTATE (prev);
1004 ta->next = SvSTATE (next); 970 ta->next = SvSTATE (next);
1005 ta->flags = TRANSFER_SAVE_ALL; 971 ta->flags = TRANSFER_SAVE_ALL;
972
973 ta->next->flags &= ~CF_READY;
1006} 974}
1007 975
1008static void 976static void
1009prepare_cede (struct transfer_args *ta) 977prepare_cede (struct transfer_args *ta)
1010{ 978{
1011 LOCK; 979 api_ready (GvSV (coro_current));
1012 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
1013 UNLOCK;
1014 980
1015 prepare_schedule (ta); 981 prepare_schedule (ta);
1016} 982}
1017 983
1018static void 984static void
1075 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1041 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)); 1042 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1077 1043
1078 for (i = 1; i < items; i++) 1044 for (i = 1; i < items; i++)
1079 av_push (coro->args, newSVsv (ST (i))); 1045 av_push (coro->args, newSVsv (ST (i)));
1080
1081 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1082 /*coro->stack = 0;*/
1083} 1046}
1084 OUTPUT: 1047 OUTPUT:
1085 RETVAL 1048 RETVAL
1086 1049
1087void 1050void
1096 struct transfer_args ta; 1059 struct transfer_args ta;
1097 1060
1098 switch (ix) 1061 switch (ix)
1099 { 1062 {
1100 case 0: 1063 case 0:
1101 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1064 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1102 ta.next = 0; 1065 ta.next = 0;
1103 ta.flags = TRANSFER_SET_STACKLEVEL; 1066 ta.flags = TRANSFER_SET_STACKLEVEL;
1104 break; 1067 break;
1105 1068
1106 case 1: 1069 case 1:
1166 int code 1129 int code
1167 PROTOTYPE: $ 1130 PROTOTYPE: $
1168 CODE: 1131 CODE:
1169 _exit (code); 1132 _exit (code);
1170 1133
1134int
1135cctx_count ()
1136 CODE:
1137 RETVAL = cctx_count;
1138 OUTPUT:
1139 RETVAL
1140
1141int
1142cctx_idle ()
1143 CODE:
1144 RETVAL = cctx_idle;
1145 OUTPUT:
1146 RETVAL
1147
1171MODULE = Coro::State PACKAGE = Coro 1148MODULE = Coro::State PACKAGE = Coro
1172 1149
1173BOOT: 1150BOOT:
1174{ 1151{
1175 int i; 1152 int i;
1193 SV *sv = perl_get_sv("Coro::API", 1); 1170 SV *sv = perl_get_sv("Coro::API", 1);
1194 1171
1195 coroapi.schedule = api_schedule; 1172 coroapi.schedule = api_schedule;
1196 coroapi.cede = api_cede; 1173 coroapi.cede = api_cede;
1197 coroapi.ready = api_ready; 1174 coroapi.ready = api_ready;
1175 coroapi.is_ready = api_is_ready;
1198 coroapi.nready = &coro_nready; 1176 coroapi.nready = &coro_nready;
1199 coroapi.current = coro_current; 1177 coroapi.current = coro_current;
1200 1178
1201 GCoroAPI = &coroapi; 1179 GCoroAPI = &coroapi;
1202 sv_setiv (sv, (IV)&coroapi); 1180 sv_setiv (sv, (IV)&coroapi);
1222 1200
1223 coro->prio = newprio; 1201 coro->prio = newprio;
1224 } 1202 }
1225} 1203}
1226 1204
1227void 1205SV *
1228ready (SV *self) 1206ready (SV *self)
1229 PROTOTYPE: $ 1207 PROTOTYPE: $
1230 CODE: 1208 CODE:
1231 api_ready (self); 1209 RETVAL = boolSV (api_ready (self));
1210 OUTPUT:
1211 RETVAL
1212
1213SV *
1214is_ready (SV *self)
1215 PROTOTYPE: $
1216 CODE:
1217 RETVAL = boolSV (api_is_ready (self));
1218 OUTPUT:
1219 RETVAL
1232 1220
1233int 1221int
1234nready (...) 1222nready (...)
1235 PROTOTYPE: 1223 PROTOTYPE:
1236 CODE: 1224 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines