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.102 by root, Mon Nov 27 01:28:03 2006 UTC vs.
Revision 1.112 by root, Thu Nov 30 20:52:52 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);
546 * emulate part of the perl startup here. 482 * emulate part of the perl startup here.
547 */ 483 */
548 484
549 coro_init_stacks (); 485 coro_init_stacks ();
550 486
487 PL_curcop = 0;
488 PL_in_eval = 0;
489 PL_curpm = 0;
490
551 { 491 {
552 dSP; 492 dSP;
553 LOGOP myop; 493 LOGOP myop;
554 494
555 /*PL_curcop = 0;*/ 495 /* I have no idea why this is needed, but it is */
556 PL_in_eval = 0; 496 PUSHMARK (SP);
497
557 SvREFCNT_dec (GvAV (PL_defgv)); 498 SvREFCNT_dec (GvAV (PL_defgv));
558 GvAV (PL_defgv) = coro->args; coro->args = 0; 499 GvAV (PL_defgv) = coro->args; coro->args = 0;
559
560 SPAGAIN;
561 500
562 Zero (&myop, 1, LOGOP); 501 Zero (&myop, 1, LOGOP);
563 myop.op_next = Nullop; 502 myop.op_next = Nullop;
564 myop.op_flags = OPf_WANT_VOID; 503 myop.op_flags = OPf_WANT_VOID;
565 504
566 PL_op = (OP *)&myop; 505 PL_op = (OP *)&myop;
567 506
568 PUSHMARK (SP); 507 PUSHMARK (SP);
569 PUSHMARK (SP);
570 XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE)); 508 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
571 PUTBACK; 509 PUTBACK;
572 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 510 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
573 SPAGAIN; 511 SPAGAIN;
574 512
575 ENTER; /* necessary e.g. for dounwind */ 513 ENTER; /* necessary e.g. for dounwind */
585 coro_mortal = 0; 523 coro_mortal = 0;
586 } 524 }
587} 525}
588 526
589static void NOINLINE 527static void NOINLINE
590prepare_cctx (coro_stack *cctx) 528prepare_cctx (coro_cctx *cctx)
591{ 529{
592 dSP; 530 dSP;
593 LOGOP myop; 531 LOGOP myop;
594 532
595 Zero (&myop, 1, LOGOP); 533 Zero (&myop, 1, LOGOP);
596 myop.op_next = PL_op; 534 myop.op_next = PL_op;
597 myop.op_flags = OPf_WANT_VOID; 535 myop.op_flags = OPf_WANT_VOID;
598 536
599 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx)); 537 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
600 538
601 PUSHMARK (SP); 539 PUSHMARK (SP);
602 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); 540 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
603 PUTBACK; 541 PUTBACK;
604 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 542 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
605 SPAGAIN; 543 SPAGAIN;
606} 544}
607 545
613 551
614 /* 552 /*
615 * this is a _very_ stripped down perl interpreter ;) 553 * this is a _very_ stripped down perl interpreter ;)
616 */ 554 */
617 PL_top_env = &PL_start_env; 555 PL_top_env = &PL_start_env;
556
618 /* inject call to cctx_init */ 557 /* inject call to cctx_init */
619 prepare_cctx ((coro_stack *)arg); 558 prepare_cctx ((coro_cctx *)arg);
620 559
621 /* somebody will hit me for both perl_run and PL_restartop */ 560 /* somebody will hit me for both perl_run and PL_restartop */
622 perl_run (PL_curinterp); 561 perl_run (PL_curinterp);
623 562
624 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);
625 abort (); 564 abort ();
626} 565}
627 566
628static coro_stack * 567static coro_cctx *
629stack_new () 568cctx_new ()
630{ 569{
631 coro_stack *stack; 570 coro_cctx *cctx;
632 571
572 ++cctx_count;
573
633 New (0, stack, 1, coro_stack); 574 New (0, cctx, 1, coro_cctx);
634 575
635#if HAVE_MMAP 576#if HAVE_MMAP
636 577
637 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 578 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
638 /* mmap suppsedly does allocate-on-write for us */ 579 /* mmap suppsedly does allocate-on-write for us */
639 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);
640 581
641 if (stack->sptr == (void *)-1) 582 if (cctx->sptr == (void *)-1)
642 { 583 {
643 perror ("FATAL: unable to mmap stack for coroutine"); 584 perror ("FATAL: unable to mmap stack for coroutine");
644 _exit (EXIT_FAILURE); 585 _exit (EXIT_FAILURE);
645 } 586 }
646 587
647# if STACKGUARD 588# if STACKGUARD
648 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 589 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
649# endif 590# endif
650 591
651#else 592#else
652 593
653 stack->ssize = STACKSIZE * (long)sizeof (long); 594 cctx->ssize = STACKSIZE * (long)sizeof (long);
654 New (0, stack->sptr, STACKSIZE, long); 595 New (0, cctx->sptr, STACKSIZE, long);
655 596
656 if (!stack->sptr) 597 if (!cctx->sptr)
657 { 598 {
658 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 599 perror ("FATAL: unable to malloc stack for coroutine");
659 _exit (EXIT_FAILURE); 600 _exit (EXIT_FAILURE);
660 } 601 }
661 602
662#endif 603#endif
663 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
664 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);
665 613
666 return stack; 614 return cctx;
667} 615}
668 616
669static void 617static void
670stack_free (coro_stack *stack) 618cctx_free (coro_cctx *cctx)
671{ 619{
672 if (!stack) 620 if (!cctx)
673 return; 621 return;
674 622
623 --cctx_count;
624
625#if USE_VALGRIND
626 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
627#endif
628
675#if HAVE_MMAP 629#if HAVE_MMAP
676 munmap (stack->sptr, stack->ssize); 630 munmap (cctx->sptr, cctx->ssize);
677#else 631#else
678 Safefree (stack->sptr); 632 Safefree (cctx->sptr);
679#endif 633#endif
680 634
681 Safefree (stack); 635 Safefree (cctx);
682} 636}
683 637
684static coro_stack *stack_first;
685static int cctx_count, cctx_idle;
686
687static coro_stack * 638static coro_cctx *
688stack_get () 639cctx_get ()
689{ 640{
690 coro_stack *stack; 641 coro_cctx *cctx;
691 642
692 if (stack_first) 643 if (cctx_first)
693 { 644 {
694 --cctx_idle; 645 --cctx_idle;
695 stack = stack_first; 646 cctx = cctx_first;
696 stack_first = stack->next; 647 cctx_first = cctx->next;
697 } 648 }
698 else 649 else
699 { 650 {
700 ++cctx_count; 651 cctx = cctx_new ();
701 stack = stack_new ();
702 PL_op = PL_op->op_next; 652 PL_op = PL_op->op_next;
703 } 653 }
704 654
705 return stack; 655 return cctx;
706} 656}
707 657
708static void 658static void
709stack_put (coro_stack *stack) 659cctx_put (coro_cctx *cctx)
710{ 660{
711 ++cctx_idle; 661 ++cctx_idle;
712 stack->next = stack_first; 662 cctx->next = cctx_first;
713 stack_first = stack; 663 cctx_first = cctx;
714} 664}
715 665
716/* never call directly, always through the coro_state_transfer global variable */ 666/* never call directly, always through the coro_state_transfer global variable */
717static void NOINLINE 667static void NOINLINE
718transfer (struct coro *prev, struct coro *next, int flags) 668transfer (struct coro *prev, struct coro *next, int flags)
719{ 669{
720 dSTACKLEVEL; 670 dSTACKLEVEL;
721 671
722 /* sometimes transfer is only called to set idle_sp */ 672 /* sometimes transfer is only called to set idle_sp */
723 if (flags == TRANSFER_SET_STACKLEVEL) 673 if (flags == TRANSFER_SET_STACKLEVEL)
724 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 674 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
725 else if (prev != next) 675 else if (prev != next)
726 { 676 {
727 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;
728 695
729 LOCK; 696 LOCK;
730 697
731 if (next->mainstack) 698 if (next->mainstack)
732 { 699 {
739 /* need to start coroutine */ 706 /* need to start coroutine */
740 /* first get rid of the old state */ 707 /* first get rid of the old state */
741 SAVE (prev, -1); 708 SAVE (prev, -1);
742 /* setup coroutine call */ 709 /* setup coroutine call */
743 setup_coro (next); 710 setup_coro (next);
744 /* need a stack */ 711 /* need a new stack */
745 next->stack = 0; 712 assert (!next->stack);
746 } 713 }
747 714
748 if (!prev->stack)
749 /* create a new empty context */
750 Newz (0, prev->stack, 1, coro_stack);
751
752 prev__stack = prev->stack; 715 prev__cctx = prev->cctx;
753 716
754 /* possibly "free" the stack */ 717 /* possibly "free" the cctx */
755 if (prev__stack->idle_sp == STACKLEVEL) 718 if (prev__cctx->idle_sp == STACKLEVEL)
756 { 719 {
720 assert (PL_top_env == prev__cctx->top_env);
721
757 stack_put (prev__stack); 722 cctx_put (prev__cctx);
758 prev->stack = 0; 723 prev->cctx = 0;
759 } 724 }
760 725
761 if (!next->stack) 726 if (!next->cctx)
762 next->stack = stack_get (); 727 next->cctx = cctx_get ();
763 728
764 if (prev__stack != next->stack) 729 if (prev__cctx != next->cctx)
765 { 730 {
731 assert ( prev__cctx->inuse);
732 assert (!next->cctx->inuse);
733
734 prev__cctx->inuse = 0;
735 next->cctx->inuse = 1;
736
766 prev__stack->top_env = PL_top_env; 737 prev__cctx->top_env = PL_top_env;
767 PL_top_env = next->stack->top_env; 738 PL_top_env = next->cctx->top_env;
768 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 739 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
769 } 740 }
770 741
771 free_coro_mortal (); 742 free_coro_mortal ();
772 743
773 UNLOCK; 744 UNLOCK;
786coro_state_destroy (struct coro *coro) 757coro_state_destroy (struct coro *coro)
787{ 758{
788 if (coro->refcnt--) 759 if (coro->refcnt--)
789 return; 760 return;
790 761
762 if (coro->flags & CF_RUNNING)
763 croak ("FATAL: tried to destroy currently running coroutine");
764
791 if (coro->mainstack && coro->mainstack != main_mainstack) 765 if (coro->mainstack && coro->mainstack != main_mainstack)
792 { 766 {
793 struct coro temp; 767 struct coro temp;
794 768
795 SAVE ((&temp), TRANSFER_SAVE_ALL); 769 SAVE ((&temp), TRANSFER_SAVE_ALL);
800 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 774 LOAD ((&temp)); /* this will get rid of defsv etc.. */
801 775
802 coro->mainstack = 0; 776 coro->mainstack = 0;
803 } 777 }
804 778
805 stack_free (coro->stack); 779 cctx_free (coro->cctx);
806 SvREFCNT_dec (coro->args); 780 SvREFCNT_dec (coro->args);
807 Safefree (coro); 781 Safefree (coro);
808} 782}
809 783
810static int 784static int
892static GV *coro_current, *coro_idle; 866static GV *coro_current, *coro_idle;
893static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 867static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
894static int coro_nready; 868static int coro_nready;
895 869
896static void 870static void
897coro_enq (SV *sv) 871coro_enq (SV *coro_sv)
898{ 872{
899 int prio;
900
901 if (SvTYPE (sv) != SVt_PVHV)
902 croak ("Coro::ready tried to enqueue something that is not a coroutine");
903
904 prio = SvSTATE (sv)->prio;
905
906 av_push (coro_ready [prio - PRIO_MIN], sv); 873 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
907 coro_nready++; 874 coro_nready++;
908} 875}
909 876
910static SV * 877static SV *
911coro_deq (int min_prio) 878coro_deq (int min_prio)
924 } 891 }
925 892
926 return 0; 893 return 0;
927} 894}
928 895
929static void 896static int
930api_ready (SV *coro) 897api_ready (SV *coro_sv)
931{ 898{
932 dTHX; 899 struct coro *coro;
933 900
934 if (SvROK (coro)) 901 if (SvROK (coro_sv))
935 coro = SvRV (coro); 902 coro_sv = SvRV (coro_sv);
903
904 coro = SvSTATE (coro_sv);
905
906 if (coro->flags & CF_READY)
907 return 0;
908
909 if (coro->flags & CF_RUNNING)
910 croak ("Coro::ready called on currently running coroutine");
911
912 coro->flags |= CF_READY;
936 913
937 LOCK; 914 LOCK;
938 coro_enq (SvREFCNT_inc (coro)); 915 coro_enq (SvREFCNT_inc (coro_sv));
939 UNLOCK; 916 UNLOCK;
917
918 return 1;
919}
920
921static int
922api_is_ready (SV *coro_sv)
923{
924 return !!SvSTATE (coro_sv)->flags & CF_READY;
940} 925}
941 926
942static void 927static void
943prepare_schedule (struct transfer_args *ta) 928prepare_schedule (struct transfer_args *ta)
944{ 929{
980 coro_mortal = prev; 965 coro_mortal = prev;
981 966
982 ta->prev = SvSTATE (prev); 967 ta->prev = SvSTATE (prev);
983 ta->next = SvSTATE (next); 968 ta->next = SvSTATE (next);
984 ta->flags = TRANSFER_SAVE_ALL; 969 ta->flags = TRANSFER_SAVE_ALL;
970
971 ta->next->flags &= ~CF_READY;
985} 972}
986 973
987static void 974static void
988prepare_cede (struct transfer_args *ta) 975prepare_cede (struct transfer_args *ta)
989{ 976{
990 LOCK; 977 api_ready (GvSV (coro_current));
991 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
992 UNLOCK;
993 978
994 prepare_schedule (ta); 979 prepare_schedule (ta);
995} 980}
996 981
997static void 982static void
1054 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1039 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1055 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1040 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1056 1041
1057 for (i = 1; i < items; i++) 1042 for (i = 1; i < items; i++)
1058 av_push (coro->args, newSVsv (ST (i))); 1043 av_push (coro->args, newSVsv (ST (i)));
1059
1060 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1061 /*coro->stack = 0;*/
1062} 1044}
1063 OUTPUT: 1045 OUTPUT:
1064 RETVAL 1046 RETVAL
1065 1047
1066void 1048void
1075 struct transfer_args ta; 1057 struct transfer_args ta;
1076 1058
1077 switch (ix) 1059 switch (ix)
1078 { 1060 {
1079 case 0: 1061 case 0:
1080 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1062 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1081 ta.next = 0; 1063 ta.next = 0;
1082 ta.flags = TRANSFER_SET_STACKLEVEL; 1064 ta.flags = TRANSFER_SET_STACKLEVEL;
1083 break; 1065 break;
1084 1066
1085 case 1: 1067 case 1:
1145 int code 1127 int code
1146 PROTOTYPE: $ 1128 PROTOTYPE: $
1147 CODE: 1129 CODE:
1148 _exit (code); 1130 _exit (code);
1149 1131
1132int
1133cctx_count ()
1134 CODE:
1135 RETVAL = cctx_count;
1136 OUTPUT:
1137 RETVAL
1138
1139int
1140cctx_idle ()
1141 CODE:
1142 RETVAL = cctx_idle;
1143 OUTPUT:
1144 RETVAL
1145
1150MODULE = Coro::State PACKAGE = Coro 1146MODULE = Coro::State PACKAGE = Coro
1151 1147
1152BOOT: 1148BOOT:
1153{ 1149{
1154 int i; 1150 int i;
1172 SV *sv = perl_get_sv("Coro::API", 1); 1168 SV *sv = perl_get_sv("Coro::API", 1);
1173 1169
1174 coroapi.schedule = api_schedule; 1170 coroapi.schedule = api_schedule;
1175 coroapi.cede = api_cede; 1171 coroapi.cede = api_cede;
1176 coroapi.ready = api_ready; 1172 coroapi.ready = api_ready;
1173 coroapi.is_ready = api_is_ready;
1177 coroapi.nready = &coro_nready; 1174 coroapi.nready = &coro_nready;
1178 coroapi.current = coro_current; 1175 coroapi.current = coro_current;
1179 1176
1180 GCoroAPI = &coroapi; 1177 GCoroAPI = &coroapi;
1181 sv_setiv (sv, (IV)&coroapi); 1178 sv_setiv (sv, (IV)&coroapi);
1201 1198
1202 coro->prio = newprio; 1199 coro->prio = newprio;
1203 } 1200 }
1204} 1201}
1205 1202
1206void 1203SV *
1207ready (SV *self) 1204ready (SV *self)
1208 PROTOTYPE: $ 1205 PROTOTYPE: $
1209 CODE: 1206 CODE:
1210 api_ready (self); 1207 RETVAL = boolSV (api_ready (self));
1208 OUTPUT:
1209 RETVAL
1210
1211SV *
1212is_ready (SV *self)
1213 PROTOTYPE: $
1214 CODE:
1215 RETVAL = boolSV (api_is_ready (self));
1216 OUTPUT:
1217 RETVAL
1211 1218
1212int 1219int
1213nready (...) 1220nready (...)
1214 PROTOTYPE: 1221 PROTOTYPE:
1215 CODE: 1222 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines