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.88 by root, Fri Nov 24 15:34:33 2006 UTC vs.
Revision 1.98 by root, Sun Nov 26 21:25:53 2006 UTC

1#define PERL_NO_GET_CONTEXT
2
3#include "libcoro/coro.c" 1#include "libcoro/coro.c"
4 2
5#include "EXTERN.h" 3#include "EXTERN.h"
6#include "perl.h" 4#include "perl.h"
7#include "XSUB.h" 5#include "XSUB.h"
27# ifndef IS_PADCONST 25# ifndef IS_PADCONST
28# define IS_PADCONST(v) 0 26# define IS_PADCONST(v) 0
29# endif 27# endif
30#endif 28#endif
31 29
30#include <stdio.h>
32#include <errno.h> 31#include <errno.h>
33 32
34#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 33#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
35# undef STACKGUARD 34# undef STACKGUARD
36#endif 35#endif
57# else 56# else
58# define BOOT_PAGESIZE 57# define BOOT_PAGESIZE
59# endif 58# endif
60#endif 59#endif
61 60
62#define SUB_INIT "Coro::State::initialize"
63
64/* The next macro should declare a variable stacklevel that contains and approximation 61/* The next macro should declare a variable stacklevel that contains and approximation
65 * to the current C stack pointer. Its property is that it changes with each call 62 * to the current C stack pointer. Its property is that it changes with each call
66 * and should be unique. */ 63 * and should be unique. */
64#define dSTACKLEVEL int stacklevel
67#define dSTACKLEVEL void *stacklevel = &stacklevel 65#define STACKLEVEL ((void *)&stacklevel)
68 66
69#define IN_DESTRUCT (PL_main_cv == Nullcv) 67#define IN_DESTRUCT (PL_main_cv == Nullcv)
70 68
71#define labs(l) ((l) >= 0 ? (l) : -(l))
72
73#include "CoroAPI.h" 69#include "CoroAPI.h"
70
71#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
74 72
75#ifdef USE_ITHREADS 73#ifdef USE_ITHREADS
76static perl_mutex coro_mutex; 74static perl_mutex coro_mutex;
77# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 75# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
78# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 76# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
84static struct CoroAPI coroapi; 82static struct CoroAPI coroapi;
85static AV *main_mainstack; /* used to differentiate between $main and others */ 83static AV *main_mainstack; /* used to differentiate between $main and others */
86static HV *coro_state_stash, *coro_stash; 84static HV *coro_state_stash, *coro_stash;
87static SV *coro_mortal; /* will be freed after next transfer */ 85static SV *coro_mortal; /* will be freed after next transfer */
88 86
89/* this is actually not only the c stack but also c registers etc... */ 87/* this is a structure representing a c-level coroutine */
90typedef struct { 88typedef struct coro_stack {
91 int refcnt; /* pointer reference counter */ 89 struct coro_stack *next;
92 int usecnt; /* shared by how many coroutines */
93 int gencnt; /* generation counter */
94 90
95 coro_context cctx; 91 /* the stack */
96
97 void *sptr; 92 void *sptr;
98 long ssize; /* positive == mmap, otherwise malloc */ 93 long ssize; /* positive == mmap, otherwise malloc */
94
95 /* cpu state */
96 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
97 JMPENV *top_env;
98 coro_context cctx;
99} coro_stack; 99} coro_stack;
100 100
101/* this is a structure representing a perl-level coroutine */
101struct coro { 102struct coro {
102 /* the optional C context */ 103 /* the c coroutine allocated to this perl coroutine, if any */
103 coro_stack *stack; 104 coro_stack *stack;
104 void *cursp; 105
106 /* data associated with this coroutine (initial args) */
107 AV *args;
105 int gencnt; 108 int refcnt;
106 109
107 /* optionally saved, might be zero */ 110 /* optionally saved, might be zero */
108 AV *defav; 111 AV *defav;
109 SV *defsv; 112 SV *defsv;
110 SV *errsv; 113 SV *errsv;
140 OP **retstack; 143 OP **retstack;
141 I32 retstack_ix; 144 I32 retstack_ix;
142 I32 retstack_max; 145 I32 retstack_max;
143 PMOP *curpm; 146 PMOP *curpm;
144 COP *curcop; 147 COP *curcop;
145 JMPENV *top_env;
146 148
147 /* coro process data */ 149 /* coro process data */
148 int prio; 150 int prio;
149
150 /* data associated with this coroutine (initial args) */
151 AV *args;
152 int refcnt;
153}; 151};
154 152
155typedef struct coro *Coro__State; 153typedef struct coro *Coro__State;
156typedef struct coro *Coro__State_or_hashref; 154typedef struct coro *Coro__State_or_hashref;
157 155
158static AV * 156static AV *
159coro_clone_padlist (pTHX_ CV *cv) 157coro_clone_padlist (CV *cv)
160{ 158{
161 AV *padlist = CvPADLIST (cv); 159 AV *padlist = CvPADLIST (cv);
162 AV *newpadlist, *newpad; 160 AV *newpadlist, *newpad;
163 161
164 newpadlist = newAV (); 162 newpadlist = newAV ();
176 174
177 return newpadlist; 175 return newpadlist;
178} 176}
179 177
180static void 178static void
181free_padlist (pTHX_ AV *padlist) 179free_padlist (AV *padlist)
182{ 180{
183 /* may be during global destruction */ 181 /* may be during global destruction */
184 if (SvREFCNT (padlist)) 182 if (SvREFCNT (padlist))
185 { 183 {
186 I32 i = AvFILLp (padlist); 184 I32 i = AvFILLp (padlist);
207 AV *padlist; 205 AV *padlist;
208 AV *av = (AV *)mg->mg_obj; 206 AV *av = (AV *)mg->mg_obj;
209 207
210 /* casting is fun. */ 208 /* casting is fun. */
211 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 209 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
212 free_padlist (aTHX_ padlist); 210 free_padlist (padlist);
213 211
214 SvREFCNT_dec (av); 212 SvREFCNT_dec (av);
215 213
216 return 0; 214 return 0;
217} 215}
220 218
221static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 219static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
222 220
223/* the next two functions merely cache the padlists */ 221/* the next two functions merely cache the padlists */
224static void 222static void
225get_padlist (pTHX_ CV *cv) 223get_padlist (CV *cv)
226{ 224{
227 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 225 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
228 226
229 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 227 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
230 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 228 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
231 else 229 else
232 { 230 {
233#if 0 231#if 0
234 /* this should work - but it doesn't :( */ 232 /* this is probably cleaner, but also slower? */
235 CV *cp = Perl_cv_clone (aTHX_ cv); 233 CV *cp = Perl_cv_clone (cv);
236 CvPADLIST (cv) = CvPADLIST (cp); 234 CvPADLIST (cv) = CvPADLIST (cp);
237 CvPADLIST (cp) = 0; 235 CvPADLIST (cp) = 0;
238 SvREFCNT_dec (cp); 236 SvREFCNT_dec (cp);
239#else 237#else
240 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 238 CvPADLIST (cv) = coro_clone_padlist (cv);
241#endif 239#endif
242 } 240 }
243} 241}
244 242
245static void 243static void
246put_padlist (pTHX_ CV *cv) 244put_padlist (CV *cv)
247{ 245{
248 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 246 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
249 247
250 if (!mg) 248 if (!mg)
251 { 249 {
259} 257}
260 258
261#define SB do { 259#define SB do {
262#define SE } while (0) 260#define SE } while (0)
263 261
264#define LOAD(state) load_state(aTHX_ (state)); 262#define LOAD(state) load_state((state));
265#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); 263#define SAVE(state,flags) save_state((state),(flags));
266 264
267#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 265#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
268 266
269static void 267static void
270load_state(pTHX_ Coro__State c) 268load_state(Coro__State c)
271{ 269{
272 PL_dowarn = c->dowarn; 270 PL_dowarn = c->dowarn;
273 PL_in_eval = c->in_eval; 271 PL_in_eval = c->in_eval;
274 272
275 PL_curstackinfo = c->curstackinfo; 273 PL_curstackinfo = c->curstackinfo;
300 PL_retstack_ix = c->retstack_ix; 298 PL_retstack_ix = c->retstack_ix;
301 PL_retstack_max = c->retstack_max; 299 PL_retstack_max = c->retstack_max;
302#endif 300#endif
303 PL_curpm = c->curpm; 301 PL_curpm = c->curpm;
304 PL_curcop = c->curcop; 302 PL_curcop = c->curcop;
305 PL_top_env = c->top_env;
306 303
307 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 304 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
308 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 305 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
309 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 306 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
310 307
317 { 314 {
318 AV *padlist = (AV *)POPs; 315 AV *padlist = (AV *)POPs;
319 316
320 if (padlist) 317 if (padlist)
321 { 318 {
322 put_padlist (aTHX_ cv); /* mark this padlist as available */ 319 put_padlist (cv); /* mark this padlist as available */
323 CvPADLIST(cv) = padlist; 320 CvPADLIST(cv) = padlist;
324 } 321 }
325 322
326 ++CvDEPTH(cv); 323 ++CvDEPTH(cv);
327 } 324 }
329 PUTBACK; 326 PUTBACK;
330 } 327 }
331} 328}
332 329
333static void 330static void
334save_state(pTHX_ Coro__State c, int flags) 331save_state(Coro__State c, int flags)
335{ 332{
336 { 333 {
337 dSP; 334 dSP;
338 I32 cxix = cxstack_ix; 335 I32 cxix = cxstack_ix;
339 PERL_CONTEXT *ccstk = cxstack; 336 PERL_CONTEXT *ccstk = cxstack;
367 } 364 }
368 365
369 PUSHs ((SV *)CvPADLIST(cv)); 366 PUSHs ((SV *)CvPADLIST(cv));
370 PUSHs ((SV *)cv); 367 PUSHs ((SV *)cv);
371 368
372 get_padlist (aTHX_ cv); 369 get_padlist (cv);
373 } 370 }
374 } 371 }
375#ifdef CXt_FORMAT 372#ifdef CXt_FORMAT
376 else if (CxTYPE(cx) == CXt_FORMAT) 373 else if (CxTYPE(cx) == CXt_FORMAT)
377 { 374 {
428 c->retstack_ix = PL_retstack_ix; 425 c->retstack_ix = PL_retstack_ix;
429 c->retstack_max = PL_retstack_max; 426 c->retstack_max = PL_retstack_max;
430#endif 427#endif
431 c->curpm = PL_curpm; 428 c->curpm = PL_curpm;
432 c->curcop = PL_curcop; 429 c->curcop = PL_curcop;
433 c->top_env = PL_top_env;
434} 430}
435 431
436/* 432/*
437 * allocate various perl stacks. This is an exact copy 433 * allocate various perl stacks. This is an exact copy
438 * of perl.c:init_stacks, except that it uses less memory 434 * of perl.c:init_stacks, except that it uses less memory
439 * on the (sometimes correct) assumption that coroutines do 435 * on the (sometimes correct) assumption that coroutines do
440 * not usually need a lot of stackspace. 436 * not usually need a lot of stackspace.
441 */ 437 */
442static void 438static void
443coro_init_stacks (pTHX) 439coro_init_stacks ()
444{ 440{
445 LOCK; 441 LOCK;
446 442
447 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 443 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
448 PL_curstackinfo->si_type = PERLSI_MAIN; 444 PL_curstackinfo->si_type = PERLSI_MAIN;
485 481
486/* 482/*
487 * destroy the stacks, the callchain etc... 483 * destroy the stacks, the callchain etc...
488 */ 484 */
489static void 485static void
490destroy_stacks(pTHX) 486destroy_stacks()
491{ 487{
492 if (!IN_DESTRUCT) 488 if (!IN_DESTRUCT)
493 { 489 {
494 /* is this ugly, I ask? */ 490 /* is this ugly, I ask? */
495 LEAVE_SCOPE (0); 491 LEAVE_SCOPE (0);
532 Safefree (PL_retstack); 528 Safefree (PL_retstack);
533#endif 529#endif
534} 530}
535 531
536static void 532static void
537allocate_stack (Coro__State ctx, int alloc) 533setup_coro (struct coro *coro)
538{
539 coro_stack *stack;
540
541 New (0, stack, 1, coro_stack);
542
543 stack->refcnt = 1;
544 stack->usecnt = 1;
545 stack->gencnt = ctx->gencnt = 0;
546
547 if (alloc)
548 {
549#if HAVE_MMAP
550 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */
551 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
552 if (stack->sptr != (void *)-1)
553 {
554# if STACKGUARD
555 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
556# endif
557 }
558 else
559#endif
560 {
561 stack->ssize = - (STACKSIZE * (long)sizeof (long));
562 New (0, stack->sptr, STACKSIZE, long);
563 }
564 }
565 else
566 stack->sptr = 0;
567
568 ctx->stack = stack;
569}
570
571static void
572deallocate_stack (Coro__State ctx)
573{
574 coro_stack *stack = ctx->stack;
575
576 ctx->stack = 0;
577
578 if (stack)
579 {
580 if (!--stack->refcnt)
581 {
582#ifdef HAVE_MMAP
583 if (stack->ssize > 0 && stack->sptr)
584 munmap (stack->sptr, stack->ssize);
585 else
586#endif
587 Safefree (stack->sptr);
588
589 Safefree (stack);
590 }
591 else if (ctx->gencnt == stack->gencnt)
592 --stack->usecnt;
593 }
594}
595
596static void
597setup_coro (void *arg)
598{ 534{
599 /* 535 /*
600 * emulate part of the perl startup here. 536 * emulate part of the perl startup here.
601 */ 537 */
602 dTHX; 538 dTHX;
603 dSP; 539 dSP;
604 Coro__State ctx = (Coro__State)arg; 540 UNOP myop;
605 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); 541 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
606 542
607 coro_init_stacks (aTHX); 543 coro_init_stacks ();
608 /*PL_curcop = 0;*/ 544 /*PL_curcop = 0;*/
609 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 545 /*PL_in_eval = PL_in_eval;*/ /* inherit */
610 SvREFCNT_dec (GvAV (PL_defgv)); 546 SvREFCNT_dec (GvAV (PL_defgv));
611 GvAV (PL_defgv) = ctx->args; ctx->args = 0; 547 GvAV (PL_defgv) = coro->args; coro->args = 0;
612 548
613 SPAGAIN; 549 SPAGAIN;
614 550
615 if (ctx->stack)
616 {
617 ctx->cursp = 0;
618
619 PUSHMARK(SP);
620 PUTBACK;
621 (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL);
622
623 if (SvTRUE (ERRSV))
624 croak (NULL);
625 else
626 croak ("FATAL: CCTXT coroutine returned!");
627 }
628 else
629 {
630 UNOP myop;
631
632 Zero(&myop, 1, UNOP); 551 Zero (&myop, 1, UNOP);
633 myop.op_next = Nullop; 552 myop.op_next = Nullop;
634 myop.op_flags = OPf_WANT_VOID; 553 myop.op_flags = OPf_WANT_VOID;
635 554
636 PL_op = (OP *)&myop; 555 PL_op = (OP *)&myop;
637 556
638 PUSHMARK(SP); 557 PUSHMARK(SP);
639 XPUSHs (sub_init); 558 XPUSHs (sub_init);
640 /*
641 * the next line is slightly wrong, as PL_op->op_next
642 * is actually being executed so we skip the first op.
643 * that doesn't matter, though, since it is only
644 * pp_nextstate and we never return...
645 * ah yes, and I don't care anyways ;)
646 */
647 PUTBACK; 559 PUTBACK;
648 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 560 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
649 SPAGAIN; 561 SPAGAIN;
650 562
651 ENTER; /* necessary e.g. for dounwind */ 563 ENTER; /* necessary e.g. for dounwind */
652 }
653} 564}
654 565
655static void 566static void
656continue_coro (void *arg) 567free_coro_mortal ()
657{ 568{
658 /*
659 * this is a _very_ stripped down perl interpreter ;)
660 */
661 dTHX;
662 Coro__State ctx = (Coro__State)arg;
663
664 PL_top_env = &PL_start_env;
665
666 ctx->cursp = 0;
667 PL_op = PL_op->op_next;
668 CALLRUNOPS(aTHX);
669
670 abort ();
671}
672
673/* never call directly, always through the coro_state_transfer global variable */
674static void
675transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags)
676{
677 dSTACKLEVEL;
678
679 if (prev != next)
680 {
681 /* has this coro been created yet? */
682 if (next->mainstack)
683 {
684 LOCK;
685 SAVE (prev, flags);
686 LOAD (next);
687 UNLOCK;
688
689 /* mark this state as in-use */
690 next->mainstack = 0;
691 next->tmps_ix = -2;
692
693 /* stacklevel changed? if yes, grab the stack for us! */
694 if (flags & TRANSFER_SAVE_CCTXT)
695 {
696 if (!prev->stack)
697 allocate_stack (prev, 0);
698 else if (prev->cursp != stacklevel
699 && prev->stack->usecnt > 1)
700 {
701 prev->gencnt = ++prev->stack->gencnt;
702 prev->stack->usecnt = 1;
703 }
704
705 /* has our stack been invalidated? */
706 if (next->stack && next->stack->gencnt != next->gencnt)
707 {
708 deallocate_stack (next);
709 allocate_stack (next, 1);
710 coro_create (&(next->stack->cctx),
711 continue_coro, (void *)next,
712 next->stack->sptr, labs (next->stack->ssize));
713 }
714
715 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
716 prev->cursp = stacklevel;
717 /* don't add any code here */
718 }
719 else
720 next->cursp = stacklevel;
721 }
722 else if (next->tmps_ix == -2)
723 croak ("tried to transfer to running coroutine");
724 else
725 {
726 LOCK;
727 SAVE (prev, -1); /* first get rid of the old state */
728 UNLOCK;
729
730 /* create the coroutine for the first time */
731 if (flags & TRANSFER_SAVE_CCTXT)
732 {
733 if (!prev->stack)
734 allocate_stack (prev, 0);
735
736 /* the new coroutine starts with start_env again */
737 PL_top_env = &PL_start_env;
738
739 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
740 {
741 setup_coro (next);
742 next->cursp = stacklevel;
743
744 prev->stack->refcnt++;
745 prev->stack->usecnt++;
746 next->stack = prev->stack;
747 next->gencnt = prev->gencnt;
748 }
749 else
750 {
751 assert (!next->stack);
752 allocate_stack (next, 1);
753 coro_create (&(next->stack->cctx),
754 setup_coro, (void *)next,
755 next->stack->sptr, labs (next->stack->ssize));
756 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
757 prev->cursp = stacklevel;
758 /* don't add any code here */
759 }
760 }
761 else
762 {
763 setup_coro (next);
764 next->cursp = stacklevel;
765 }
766 }
767 }
768
769 LOCK;
770 if (coro_mortal) 569 if (coro_mortal)
771 { 570 {
772 SvREFCNT_dec (coro_mortal); 571 SvREFCNT_dec (coro_mortal);
773 coro_mortal = 0; 572 coro_mortal = 0;
774 } 573 }
574}
575
576static void
577coro_run (void *arg)
578{
579 /*
580 * this is a _very_ stripped down perl interpreter ;)
581 */
582 dTHX;
583 int ret;
584
775 UNLOCK; 585 UNLOCK;
586
587 PL_top_env = &PL_start_env;
588
589 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg));
590 sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op));
591
592 /* continue at cctx_init, without entersub */
593 PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE));
594
595 /* somebody will hit me for both perl_run and PL_restartop */
596 ret = perl_run (PERL_GET_CONTEXT);
597 printf ("ret %d\n", ret);//D
598
599 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr);
600 abort ();
601}
602
603static coro_stack *
604stack_new ()
605{
606 coro_stack *stack;
607
608 New (0, stack, 1, coro_stack);
609
610#if HAVE_MMAP
611
612 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
613 /* mmap suppsedly does allocate-on-write for us */
614 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
615
616 if (stack->sptr == (void *)-1)
617 {
618 perror ("FATAL: unable to mmap stack for coroutine");
619 _exit (EXIT_FAILURE);
620 }
621
622# if STACKGUARD
623 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
624# endif
625
626#else
627
628 stack->ssize = STACKSIZE * (long)sizeof (long);
629 New (0, stack->sptr, STACKSIZE, long);
630
631 if (!stack->sptr)
632 {
633 perror (stderr, "FATAL: unable to malloc stack for coroutine");
634 _exit (EXIT_FAILURE);
635 }
636
637#endif
638
639 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize);
640
641 return stack;
642}
643
644static void
645stack_free (coro_stack *stack)
646{
647 if (!stack)
648 return;
649
650#if HAVE_MMAP
651 munmap (stack->sptr, stack->ssize);
652#else
653 Safefree (stack->sptr);
654#endif
655
656 Safefree (stack);
657}
658
659static coro_stack *stack_first;
660
661static coro_stack *
662stack_get ()
663{
664 coro_stack *stack;
665
666 if (stack_first)
667 {
668 stack = stack_first;
669 stack_first = stack->next;
670 }
671 else
672 {
673 stack = stack_new ();
674 PL_op = PL_op->op_next;
675 }
676
677 return stack;
678}
679
680static void
681stack_put (coro_stack *stack)
682{
683 stack->next = stack_first;
684 stack_first = stack;
685}
686
687/* never call directly, always through the coro_state_transfer global variable */
688static void
689transfer_impl (struct coro *prev, struct coro *next, int flags)
690{
691 dSTACKLEVEL;
692
693 /* sometimes transfer is only called to set idle_sp */
694 if (flags == TRANSFER_SET_STACKLEVEL)
695 ((coro_stack *)prev)->idle_sp = STACKLEVEL;
696 else if (prev != next)
697 {
698 coro_stack *prev__stack;
699
700 LOCK;
701
702 if (next->mainstack)
703 {
704 /* coroutine already started */
705 SAVE (prev, flags);
706 LOAD (next);
707 }
708 else
709 {
710 /* need to start coroutine */
711 /* first get rid of the old state */
712 SAVE (prev, -1);
713 /* setup coroutine call */
714 setup_coro (next);
715 /* need a stack */
716 next->stack = 0;
717 }
718
719 if (!prev->stack)
720 /* create a new empty context */
721 Newz (0, prev->stack, 1, coro_stack);
722
723 prev__stack = prev->stack;
724
725 /* possibly "free" the stack */
726 if (prev__stack->idle_sp == STACKLEVEL)
727 {
728 stack_put (prev__stack);
729 prev->stack = 0;
730 }
731
732 if (!next->stack)
733 next->stack = stack_get ();
734
735 if (prev__stack != next->stack)
736 {
737 prev__stack->top_env = PL_top_env;
738 PL_top_env = next->stack->top_env;
739 coro_transfer (&prev__stack->cctx, &next->stack->cctx);
740 }
741
742 free_coro_mortal ();
743
744 UNLOCK;
745 }
776} 746}
777 747
778/* use this function pointer to call the above function */ 748/* use this function pointer to call the above function */
779/* this is done to increase chances of the compiler not inlining the call */ 749/* this is done to increase chances of the compiler not inlining the call */
750/* not static to make it even harder for the compiler (and theoretically impossible in most cases */
780void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; 751void (*coro_state_transfer)(struct coro *prev, struct coro *next, int flags) = transfer_impl;
752
753struct transfer_args
754{
755 struct coro *prev, *next;
756 int flags;
757};
758
759#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags)
781 760
782static void 761static void
783coro_state_destroy (struct coro *coro) 762coro_state_destroy (struct coro *coro)
784{ 763{
785 if (coro->refcnt--) 764 if (coro->refcnt--)
787 766
788 if (coro->mainstack && coro->mainstack != main_mainstack) 767 if (coro->mainstack && coro->mainstack != main_mainstack)
789 { 768 {
790 struct coro temp; 769 struct coro temp;
791 770
792 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 771 SAVE ((&temp), TRANSFER_SAVE_ALL);
793 LOAD (aTHX_ coro); 772 LOAD (coro);
794 773
795 destroy_stacks (aTHX); 774 destroy_stacks ();
796 775
797 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 776 LOAD ((&temp)); /* this will get rid of defsv etc.. */
798 777
799 coro->mainstack = 0; 778 coro->mainstack = 0;
800 } 779 }
801 780
802 deallocate_stack (coro); 781 stack_free (coro->stack);
803 SvREFCNT_dec (coro->args); 782 SvREFCNT_dec (coro->args);
804 Safefree (coro); 783 Safefree (coro);
805} 784}
806 785
807static int 786static int
808coro_state_clear (SV *sv, MAGIC *mg) 787coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
809{ 788{
810 struct coro *coro = (struct coro *)mg->mg_ptr; 789 struct coro *coro = (struct coro *)mg->mg_ptr;
811 mg->mg_ptr = 0; 790 mg->mg_ptr = 0;
812 791
813 coro_state_destroy (coro); 792 coro_state_destroy (coro);
814 793
815 return 0; 794 return 0;
816} 795}
817 796
818static int 797static int
819coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 798coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
820{ 799{
821 struct coro *coro = (struct coro *)mg->mg_ptr; 800 struct coro *coro = (struct coro *)mg->mg_ptr;
822 801
823 ++coro->refcnt; 802 ++coro->refcnt;
824 803
848 assert (mg->mg_type == PERL_MAGIC_ext); 827 assert (mg->mg_type == PERL_MAGIC_ext);
849 return (struct coro *)mg->mg_ptr; 828 return (struct coro *)mg->mg_ptr;
850} 829}
851 830
852static void 831static void
832prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
833{
834 ta->prev = SvSTATE (prev);
835 ta->next = SvSTATE (next);
836 ta->flags = flags;
837}
838
839static void
853api_transfer (pTHX_ SV *prev, SV *next, int flags) 840api_transfer (SV *prev, SV *next, int flags)
854{ 841{
855 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 842 dTHX;
843 struct transfer_args ta;
844
845 prepare_transfer (&ta, prev, next, flags);
846 TRANSFER (ta);
856} 847}
857 848
858/** Coro ********************************************************************/ 849/** Coro ********************************************************************/
859 850
860#define PRIO_MAX 3 851#define PRIO_MAX 3
868static GV *coro_current, *coro_idle; 859static GV *coro_current, *coro_idle;
869static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 860static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
870static int coro_nready; 861static int coro_nready;
871 862
872static void 863static void
873coro_enq (pTHX_ SV *sv) 864coro_enq (SV *sv)
874{ 865{
875 int prio; 866 int prio;
876 867
877 if (SvTYPE (sv) != SVt_PVHV) 868 if (SvTYPE (sv) != SVt_PVHV)
878 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 869 croak ("Coro::ready tried to enqueue something that is not a coroutine");
882 av_push (coro_ready [prio - PRIO_MIN], sv); 873 av_push (coro_ready [prio - PRIO_MIN], sv);
883 coro_nready++; 874 coro_nready++;
884} 875}
885 876
886static SV * 877static SV *
887coro_deq (pTHX_ int min_prio) 878coro_deq (int min_prio)
888{ 879{
889 int prio = PRIO_MAX - PRIO_MIN; 880 int prio = PRIO_MAX - PRIO_MIN;
890 881
891 min_prio -= PRIO_MIN; 882 min_prio -= PRIO_MIN;
892 if (min_prio < 0) 883 if (min_prio < 0)
909 900
910 if (SvROK (coro)) 901 if (SvROK (coro))
911 coro = SvRV (coro); 902 coro = SvRV (coro);
912 903
913 LOCK; 904 LOCK;
914 coro_enq (aTHX_ SvREFCNT_inc (coro)); 905 coro_enq (SvREFCNT_inc (coro));
915 UNLOCK; 906 UNLOCK;
916} 907}
917 908
918static void 909static void
919api_schedule (void) 910prepare_schedule (struct transfer_args *ta)
920{ 911{
921 dTHX;
922
923 SV *prev, *next; 912 SV *current, *prev, *next;
913
914 LOCK;
915
924 SV *current = GvSV (coro_current); 916 current = GvSV (coro_current);
925 917
926 for (;;) 918 for (;;)
927 { 919 {
928 LOCK; 920 LOCK;
929 921
930 next = coro_deq (aTHX_ PRIO_MIN); 922 next = coro_deq (PRIO_MIN);
931 923
932 if (next) 924 if (next)
933 break; 925 break;
934 926
935 UNLOCK; 927 UNLOCK;
951 943
952 prev = SvRV (current); 944 prev = SvRV (current);
953 SvRV (current) = next; 945 SvRV (current) = next;
954 946
955 /* free this only after the transfer */ 947 /* free this only after the transfer */
948 free_coro_mortal ();
956 coro_mortal = prev; 949 coro_mortal = prev;
957 950
951 ta->prev = SvSTATE (prev);
952 ta->next = SvSTATE (next);
953 ta->flags = TRANSFER_SAVE_ALL;
954
958 UNLOCK; 955 UNLOCK;
956}
959 957
960 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), 958static void
961 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 959prepare_cede (struct transfer_args *ta)
960{
961 LOCK;
962 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
963 UNLOCK;
964
965 prepare_schedule (ta);
966}
967
968static void
969api_schedule (void)
970{
971 dTHX;
972 struct transfer_args ta;
973
974 prepare_schedule (&ta);
975 TRANSFER (ta);
962} 976}
963 977
964static void 978static void
965api_cede (void) 979api_cede (void)
966{ 980{
967 dTHX; 981 dTHX;
982 struct transfer_args ta;
968 983
969 LOCK; 984 prepare_cede (&ta);
970 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); 985 TRANSFER (ta);
971 UNLOCK;
972
973 api_schedule ();
974} 986}
975 987
976MODULE = Coro::State PACKAGE = Coro::State 988MODULE = Coro::State PACKAGE = Coro::State
977 989
978PROTOTYPES: DISABLE 990PROTOTYPES: DISABLE
987 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 999 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
988 1000
989 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1001 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
990 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1002 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
991 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 1003 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
992 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
993 1004
994 main_mainstack = PL_mainstack; 1005 main_mainstack = PL_mainstack;
995 1006
996 coroapi.ver = CORO_API_VERSION; 1007 coroapi.ver = CORO_API_VERSION;
997 coroapi.transfer = api_transfer; 1008 coroapi.transfer = api_transfer;
1008 int i; 1019 int i;
1009 1020
1010 Newz (0, coro, 1, struct coro); 1021 Newz (0, coro, 1, struct coro);
1011 coro->args = newAV (); 1022 coro->args = newAV ();
1012 1023
1013 for (i = 1; i < items; i++)
1014 av_push (coro->args, newSVsv (ST (i)));
1015
1016 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1017 /*coro->stack = 0;*/
1018
1019 hv = newHV (); 1024 hv = newHV ();
1020 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1025 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1021 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1026 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1027
1028 for (i = 1; i < items; i++)
1029 av_push (coro->args, newSVsv (ST (i)));
1030
1031 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1032 /*coro->stack = 0;*/
1022} 1033}
1023 OUTPUT: 1034 OUTPUT:
1024 RETVAL 1035 RETVAL
1025 1036
1026void 1037void
1027transfer(prev, next, flags) 1038_set_stacklevel (...)
1028 SV *prev 1039 ALIAS:
1029 SV *next 1040 Coro::State::transfer = 1
1030 int flags 1041 Coro::schedule = 2
1042 Coro::cede = 3
1043 Coro::Cont::yield = 4
1031 CODE: 1044 CODE:
1032 PUTBACK;
1033 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
1034 SPAGAIN;
1035
1036int
1037prio (Coro::State coro, int newprio = 0)
1038 ALIAS:
1039 nice = 1
1040 CODE:
1041{ 1045{
1042 RETVAL = coro->prio; 1046 struct transfer_args ta;
1043 1047
1044 if (items > 1) 1048 switch (ix)
1045 { 1049 {
1050 case 0:
1051 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0)));
1052 ta.next = 0;
1053 ta.flags = TRANSFER_SET_STACKLEVEL;
1054 break;
1055
1056 case 1:
1046 if (ix) 1057 if (items != 3)
1047 newprio += coro->prio; 1058 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items);
1048 1059
1049 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1060 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1050 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1061 break;
1051 1062
1052 coro->prio = newprio; 1063 case 2:
1064 prepare_schedule (&ta);
1065 break;
1066
1067 case 3:
1068 prepare_cede (&ta);
1069 break;
1070
1071 case 4:
1072 {
1073 SV *yieldstack;
1074 SV *sv;
1075 AV *defav = GvAV (PL_defgv);
1076
1077 yieldstack = *hv_fetch (
1078 (HV *)SvRV (GvSV (coro_current)),
1079 "yieldstack", sizeof ("yieldstack") - 1,
1080 0
1081 );
1082
1083 /* set up @_ -- ugly */
1084 av_clear (defav);
1085 av_fill (defav, items - 1);
1086 while (items--)
1087 av_store (defav, items, SvREFCNT_inc (ST(items)));
1088
1089 sv = av_pop ((AV *)SvRV (yieldstack));
1090 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1091 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1092 ta.flags = 0;
1093 SvREFCNT_dec (sv);
1094 }
1095 break;
1096
1053 } 1097 }
1098
1099 TRANSFER (ta);
1054} 1100}
1055 1101
1056void 1102void
1057_clone_state_from (SV *dst, SV *src) 1103_clone_state_from (SV *dst, SV *src)
1058 CODE: 1104 CODE:
1062 sv_unmagic (SvRV (dst), PERL_MAGIC_ext); 1108 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1063 1109
1064 ++coro_src->refcnt; 1110 ++coro_src->refcnt;
1065 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1111 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1066} 1112}
1113
1114void
1115_nonlocal_goto (IV nextop)
1116 CODE:
1117 /* uuh, somebody will kill me again for this */
1118 PL_op->op_next = INT2PTR (OP *, nextop);
1067 1119
1068void 1120void
1069_exit (code) 1121_exit (code)
1070 int code 1122 int code
1071 PROTOTYPE: $ 1123 PROTOTYPE: $
1072 CODE: 1124 CODE:
1073 _exit (code); 1125 _exit (code);
1074 1126
1075MODULE = Coro::State PACKAGE = Coro::Cont
1076
1077void
1078yield (...)
1079 PROTOTYPE: @
1080 CODE:
1081{
1082 SV *yieldstack;
1083 SV *sv;
1084 AV *defav = GvAV (PL_defgv);
1085 struct coro *prev, *next;
1086
1087 yieldstack = *hv_fetch (
1088 (HV *)SvRV (GvSV (coro_current)),
1089 "yieldstack", sizeof ("yieldstack") - 1,
1090 0
1091 );
1092
1093 /* set up @_ -- ugly */
1094 av_clear (defav);
1095 av_fill (defav, items - 1);
1096 while (items--)
1097 av_store (defav, items, SvREFCNT_inc (ST(items)));
1098
1099 sv = av_pop ((AV *)SvRV (yieldstack));
1100 prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1101 next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1102 SvREFCNT_dec (sv);
1103
1104 coro_state_transfer (aTHX_ prev, next, 0);
1105}
1106
1107MODULE = Coro::State PACKAGE = Coro 1127MODULE = Coro::State PACKAGE = Coro
1108 1128
1109BOOT: 1129BOOT:
1110{ 1130{
1111 int i; 1131 int i;
1138 sv_setiv (sv, (IV)&coroapi); 1158 sv_setiv (sv, (IV)&coroapi);
1139 SvREADONLY_on (sv); 1159 SvREADONLY_on (sv);
1140 } 1160 }
1141} 1161}
1142 1162
1143#if !PERL_MICRO 1163int
1164prio (Coro::State coro, int newprio = 0)
1165 ALIAS:
1166 nice = 1
1167 CODE:
1168{
1169 RETVAL = coro->prio;
1170
1171 if (items > 1)
1172 {
1173 if (ix)
1174 newprio += coro->prio;
1175
1176 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1177 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1178
1179 coro->prio = newprio;
1180 }
1181}
1144 1182
1145void 1183void
1146ready (self) 1184ready (SV *self)
1147 SV * self
1148 PROTOTYPE: $ 1185 PROTOTYPE: $
1149 CODE: 1186 CODE:
1150 api_ready (self); 1187 api_ready (self);
1151
1152#endif
1153 1188
1154int 1189int
1155nready (...) 1190nready (...)
1156 PROTOTYPE: 1191 PROTOTYPE:
1157 CODE: 1192 CODE:
1158 RETVAL = coro_nready; 1193 RETVAL = coro_nready;
1159 OUTPUT: 1194 OUTPUT:
1160 RETVAL 1195 RETVAL
1161 1196
1162void 1197MODULE = Coro::State PACKAGE = Coro::AIO
1163schedule (...)
1164 PROTOTYPE:
1165 CODE:
1166 api_schedule ();
1167 1198
1168void
1169cede (...)
1170 PROTOTYPE:
1171 CODE:
1172 api_cede ();
1173
1174# and these are hacks
1175SV * 1199SV *
1176_aio_get_state () 1200_get_state ()
1177 CODE: 1201 CODE:
1178{ 1202{
1179 struct { 1203 struct {
1180 int errorno; 1204 int errorno;
1181 int laststype; 1205 int laststype;
1192} 1216}
1193 OUTPUT: 1217 OUTPUT:
1194 RETVAL 1218 RETVAL
1195 1219
1196void 1220void
1197_aio_set_state (char *data_) 1221_set_state (char *data_)
1198 PROTOTYPE: $ 1222 PROTOTYPE: $
1199 CODE: 1223 CODE:
1200{ 1224{
1201 struct { 1225 struct {
1202 int errorno; 1226 int errorno;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines