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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines