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.90 by root, Sat Nov 25 00:56:35 2006 UTC vs.
Revision 1.99 by root, Sun Nov 26 23:53:20 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 just the c stack but also c registers etc... */ 87/* this is a structure representing a c-level coroutine */
90typedef struct coro_stack { 88typedef struct coro_stack {
91 struct coro_stack *next; 89 struct coro_stack *next;
92 90
93 void *idle_sp; 91 /* the stack */
94 void *sptr; 92 void *sptr;
95 long ssize; /* positive == mmap, otherwise malloc */ 93 long ssize; /* positive == mmap, otherwise malloc */
96 94
97 /* cpu state */ 95 /* cpu state */
96 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
97 JMPENV *top_env;
98 coro_context cctx; 98 coro_context cctx;
99} coro_stack; 99} coro_stack;
100 100
101static coro_stack *main_stack; 101/* this is a structure representing a perl-level coroutine */
102
103struct coro { 102struct coro {
103 /* the c coroutine allocated to this perl coroutine, if any */
104 coro_stack *stack;
105
106 /* data associated with this coroutine (initial args) */
107 AV *args;
108 int refcnt;
109
104 /* optionally saved, might be zero */ 110 /* optionally saved, might be zero */
105 AV *defav; 111 AV *defav;
106 SV *defsv; 112 SV *defsv;
107 SV *errsv; 113 SV *errsv;
108 114
109 /* saved global state not related to stacks */ 115 /* saved global state not related to stacks */
110 U8 dowarn; 116 U8 dowarn;
111 I32 in_eval; 117 I32 in_eval;
112
113 /* the c stack, if any */
114 coro_stack *stack;
115 118
116 /* the stacks and related info (callchain etc..) */ 119 /* the stacks and related info (callchain etc..) */
117 PERL_SI *curstackinfo; 120 PERL_SI *curstackinfo;
118 AV *curstack; 121 AV *curstack;
119 AV *mainstack; 122 AV *mainstack;
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;
446
447 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 441 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
448 PL_curstackinfo->si_type = PERLSI_MAIN; 442 PL_curstackinfo->si_type = PERLSI_MAIN;
449 PL_curstack = PL_curstackinfo->si_stack; 443 PL_curstack = PL_curstackinfo->si_stack;
450 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 444 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
451 445
477#if PERL_VERSION < 9 471#if PERL_VERSION < 9
478 New(54,PL_retstack,8,OP*); 472 New(54,PL_retstack,8,OP*);
479 PL_retstack_ix = 0; 473 PL_retstack_ix = 0;
480 PL_retstack_max = 8; 474 PL_retstack_max = 8;
481#endif 475#endif
482
483 UNLOCK;
484} 476}
485 477
486/* 478/*
487 * destroy the stacks, the callchain etc... 479 * destroy the stacks, the callchain etc...
488 */ 480 */
489static void 481static void
490destroy_stacks(pTHX) 482destroy_stacks()
491{ 483{
492 if (!IN_DESTRUCT) 484 if (!IN_DESTRUCT)
493 { 485 {
494 /* is this ugly, I ask? */ 486 /* is this ugly, I ask? */
495 LEAVE_SCOPE (0); 487 LEAVE_SCOPE (0);
540 * emulate part of the perl startup here. 532 * emulate part of the perl startup here.
541 */ 533 */
542 dTHX; 534 dTHX;
543 dSP; 535 dSP;
544 UNOP myop; 536 UNOP myop;
545 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); 537 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
546 538
547 coro_init_stacks (aTHX); 539 coro_init_stacks ();
548 /*PL_curcop = 0;*/ 540 /*PL_curcop = 0;*/
549 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 541 /*PL_in_eval = PL_in_eval;*/ /* inherit */
550 SvREFCNT_dec (GvAV (PL_defgv)); 542 SvREFCNT_dec (GvAV (PL_defgv));
551 GvAV (PL_defgv) = coro->args; coro->args = 0; 543 GvAV (PL_defgv) = coro->args; coro->args = 0;
552 544
566 558
567 ENTER; /* necessary e.g. for dounwind */ 559 ENTER; /* necessary e.g. for dounwind */
568} 560}
569 561
570static void 562static void
571transfer_tail () 563free_coro_mortal ()
572{ 564{
573 if (coro_mortal) 565 if (coro_mortal)
574 { 566 {
575 SvREFCNT_dec (coro_mortal); 567 SvREFCNT_dec (coro_mortal);
576 coro_mortal = 0; 568 coro_mortal = 0;
577 } 569 }
570}
578 571
579 UNLOCK; 572static void
573prepare_cctx (coro_stack *cctx)
574{
575 dSP;
576 UNOP myop;
577
578 Zero (&myop, 1, UNOP);
579 myop.op_next = PL_op;
580 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
581
582 PUSHMARK(SP);
583 EXTEND (SP, 2);
584 PUSHs (newSViv (PTR2IV (cctx)));
585 PUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE));
586 PUTBACK;
587 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
588 SPAGAIN;
580} 589}
581 590
582static void 591static void
583coro_run (void *arg) 592coro_run (void *arg)
584{ 593{
585 /* 594 /*
586 * this is a _very_ stripped down perl interpreter ;) 595 * this is a _very_ stripped down perl interpreter ;)
587 */ 596 */
588 dTHX; 597 UNLOCK;
589
590 transfer_tail ();
591 598
592 PL_top_env = &PL_start_env; 599 PL_top_env = &PL_start_env;
593 PL_restartop = PL_op->op_next; 600 prepare_cctx ((coro_stack *)arg);
601
594 /* somebody will hit me for both perl_run and PL_restart_top */ 602 /* somebody will hit me for both perl_run and PL_restartop */
595 perl_run (aTHX_ PERL_GET_CONTEXT); 603 perl_run (PERL_GET_CONTEXT);
596 604
605 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr);
597 abort (); 606 abort ();
598} 607}
599 608
600static coro_stack * 609static coro_stack *
601stack_new () 610stack_new ()
604 613
605 New (0, stack, 1, coro_stack); 614 New (0, stack, 1, coro_stack);
606 615
607#if HAVE_MMAP 616#if HAVE_MMAP
608 617
609 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ 618 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
619 /* mmap suppsedly does allocate-on-write for us */
610 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 620 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
611 621
612 if (stack->sptr == (void *)-1) 622 if (stack->sptr == (void *)-1)
613 { 623 {
614 fprintf (stderr, "FATAL: unable to mmap stack for coroutine\n"); 624 perror ("FATAL: unable to mmap stack for coroutine");
615 _exit (EXIT_FAILURE); 625 _exit (EXIT_FAILURE);
616 } 626 }
617 627
628# if STACKGUARD
618 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 629 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
630# endif
619 631
620#else 632#else
621 633
622 stack->ssize = STACKSIZE * (long)sizeof (long); 634 stack->ssize = STACKSIZE * (long)sizeof (long);
623 New (0, stack->sptr, STACKSIZE, long); 635 New (0, stack->sptr, STACKSIZE, long);
624 636
625 if (!stack->sptr) 637 if (!stack->sptr)
626 { 638 {
627 fprintf (stderr, "FATAL: unable to malloc stack for coroutine\n"); 639 perror (stderr, "FATAL: unable to malloc stack for coroutine");
628 _exit (EXIT_FAILURE); 640 _exit (EXIT_FAILURE);
629 } 641 }
630 642
631#endif 643#endif
632 644
633 coro_create (&stack->cctx, coro_run, 0, stack->sptr, stack->ssize); 645 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize);
634 646
635 return stack; 647 return stack;
636} 648}
637 649
638static void 650static void
639stack_free (coro_stack *stack) 651stack_free (coro_stack *stack)
640{ 652{
641 if (!stack || stack == main_stack) 653 if (!stack)
642 return; 654 return;
643 655
644#if HAVE_MMAP 656#if HAVE_MMAP
645 munmap (stack->sptr, stack->ssize); 657 munmap (stack->sptr, stack->ssize);
646#else 658#else
650 Safefree (stack); 662 Safefree (stack);
651} 663}
652 664
653static coro_stack *stack_first; 665static coro_stack *stack_first;
654 666
655static void 667static coro_stack *
656stack_get (struct coro *coro) 668stack_get ()
657{ 669{
670 coro_stack *stack;
671
658 if (stack_first) 672 if (stack_first)
659 { 673 {
660 coro->stack = stack_first; 674 stack = stack_first;
661 stack_first = stack_first->next; 675 stack_first = stack->next;
662 } 676 }
663 else 677 else
678 {
664 coro->stack = stack_new (); 679 stack = stack_new ();
680 PL_op = PL_op->op_next;
681 }
682
683 return stack;
665} 684}
666 685
667static void 686static void
668stack_put (coro_stack *stack) 687stack_put (coro_stack *stack)
669{ 688{
671 stack_first = stack; 690 stack_first = stack;
672} 691}
673 692
674/* never call directly, always through the coro_state_transfer global variable */ 693/* never call directly, always through the coro_state_transfer global variable */
675static void 694static void
676transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 695transfer_impl (struct coro *prev, struct coro *next, int flags)
677{ 696{
678 dSTACKLEVEL; 697 dSTACKLEVEL;
679 698
680 /* sometimes transfer is only called to set idle_sp */ 699 /* sometimes transfer is only called to set idle_sp */
681 if (!prev->stack->idle_sp) 700 if (flags == TRANSFER_SET_STACKLEVEL)
682 prev->stack->idle_sp = stacklevel; 701 ((coro_stack *)prev)->idle_sp = STACKLEVEL;
683
684 LOCK;
685
686 if (prev != next) 702 else if (prev != next)
687 { 703 {
688 coro_stack *prev_stack = prev->stack; 704 coro_stack *prev__stack;
689 705
690 /* possibly "free" the stack */ 706 LOCK;
691 if (prev_stack->idle_sp == stacklevel)
692 {
693 stack_put (prev_stack);
694 prev->stack = 0;
695 }
696 707
697 if (next->mainstack) 708 if (next->mainstack)
698 { 709 {
699 /* coroutine already started */ 710 /* coroutine already started */
700 SAVE (prev, flags); 711 SAVE (prev, flags);
705 /* need to start coroutine */ 716 /* need to start coroutine */
706 /* first get rid of the old state */ 717 /* first get rid of the old state */
707 SAVE (prev, -1); 718 SAVE (prev, -1);
708 /* setup coroutine call */ 719 /* setup coroutine call */
709 setup_coro (next); 720 setup_coro (next);
710 /* need to change stack from main_stack to real one */ 721 /* need a stack */
711 next->stack = 0; 722 next->stack = 0;
712 } 723 }
713 724
725 if (!prev->stack)
726 /* create a new empty context */
727 Newz (0, prev->stack, 1, coro_stack);
728
729 prev__stack = prev->stack;
730
731 /* possibly "free" the stack */
732 if (prev__stack->idle_sp == STACKLEVEL)
733 {
734 stack_put (prev__stack);
735 prev->stack = 0;
736 }
737
714 if (!next->stack) 738 if (!next->stack)
715 stack_get (next); 739 next->stack = stack_get ();
716 740
717 if (prev_stack != next->stack) 741 if (prev__stack != next->stack)
742 {
743 prev__stack->top_env = PL_top_env;
744 PL_top_env = next->stack->top_env;
718 coro_transfer (&prev_stack->cctx, &next->stack->cctx); 745 coro_transfer (&prev__stack->cctx, &next->stack->cctx);
746 }
747
748 free_coro_mortal ();
749
750 UNLOCK;
719 } 751 }
720
721 transfer_tail ();
722} 752}
723 753
724/* use this function pointer to call the above function */ 754/* use this function pointer to call the above function */
725/* this is done to increase chances of the compiler not inlining the call */ 755/* this is done to increase chances of the compiler not inlining the call */
756/* not static to make it even harder for the compiler (and theoretically impossible in most cases */
726void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; 757void (*coro_state_transfer)(struct coro *prev, struct coro *next, int flags) = transfer_impl;
758
759struct transfer_args
760{
761 struct coro *prev, *next;
762 int flags;
763};
764
765#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags)
727 766
728static void 767static void
729coro_state_destroy (struct coro *coro) 768coro_state_destroy (struct coro *coro)
730{ 769{
731 if (coro->refcnt--) 770 if (coro->refcnt--)
733 772
734 if (coro->mainstack && coro->mainstack != main_mainstack) 773 if (coro->mainstack && coro->mainstack != main_mainstack)
735 { 774 {
736 struct coro temp; 775 struct coro temp;
737 776
738 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 777 SAVE ((&temp), TRANSFER_SAVE_ALL);
739 LOAD (aTHX_ coro); 778 LOAD (coro);
740 779
741 destroy_stacks (aTHX); 780 destroy_stacks ();
742 781
743 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 782 LOAD ((&temp)); /* this will get rid of defsv etc.. */
744 783
745 coro->mainstack = 0; 784 coro->mainstack = 0;
746 } 785 }
749 SvREFCNT_dec (coro->args); 788 SvREFCNT_dec (coro->args);
750 Safefree (coro); 789 Safefree (coro);
751} 790}
752 791
753static int 792static int
754coro_state_clear (SV *sv, MAGIC *mg) 793coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
755{ 794{
756 struct coro *coro = (struct coro *)mg->mg_ptr; 795 struct coro *coro = (struct coro *)mg->mg_ptr;
757 mg->mg_ptr = 0; 796 mg->mg_ptr = 0;
758 797
759 coro_state_destroy (coro); 798 coro_state_destroy (coro);
760 799
761 return 0; 800 return 0;
762} 801}
763 802
764static int 803static int
765coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 804coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
766{ 805{
767 struct coro *coro = (struct coro *)mg->mg_ptr; 806 struct coro *coro = (struct coro *)mg->mg_ptr;
768 807
769 ++coro->refcnt; 808 ++coro->refcnt;
770 809
794 assert (mg->mg_type == PERL_MAGIC_ext); 833 assert (mg->mg_type == PERL_MAGIC_ext);
795 return (struct coro *)mg->mg_ptr; 834 return (struct coro *)mg->mg_ptr;
796} 835}
797 836
798static void 837static void
838prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
839{
840 ta->prev = SvSTATE (prev);
841 ta->next = SvSTATE (next);
842 ta->flags = flags;
843}
844
845static void
799api_transfer (pTHX_ SV *prev, SV *next, int flags) 846api_transfer (SV *prev, SV *next, int flags)
800{ 847{
801 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 848 dTHX;
849 struct transfer_args ta;
850
851 prepare_transfer (&ta, prev, next, flags);
852 TRANSFER (ta);
802} 853}
803 854
804/** Coro ********************************************************************/ 855/** Coro ********************************************************************/
805 856
806#define PRIO_MAX 3 857#define PRIO_MAX 3
814static GV *coro_current, *coro_idle; 865static GV *coro_current, *coro_idle;
815static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 866static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
816static int coro_nready; 867static int coro_nready;
817 868
818static void 869static void
819coro_enq (pTHX_ SV *sv) 870coro_enq (SV *sv)
820{ 871{
821 int prio; 872 int prio;
822 873
823 if (SvTYPE (sv) != SVt_PVHV) 874 if (SvTYPE (sv) != SVt_PVHV)
824 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 875 croak ("Coro::ready tried to enqueue something that is not a coroutine");
828 av_push (coro_ready [prio - PRIO_MIN], sv); 879 av_push (coro_ready [prio - PRIO_MIN], sv);
829 coro_nready++; 880 coro_nready++;
830} 881}
831 882
832static SV * 883static SV *
833coro_deq (pTHX_ int min_prio) 884coro_deq (int min_prio)
834{ 885{
835 int prio = PRIO_MAX - PRIO_MIN; 886 int prio = PRIO_MAX - PRIO_MIN;
836 887
837 min_prio -= PRIO_MIN; 888 min_prio -= PRIO_MIN;
838 if (min_prio < 0) 889 if (min_prio < 0)
855 906
856 if (SvROK (coro)) 907 if (SvROK (coro))
857 coro = SvRV (coro); 908 coro = SvRV (coro);
858 909
859 LOCK; 910 LOCK;
860 coro_enq (aTHX_ SvREFCNT_inc (coro)); 911 coro_enq (SvREFCNT_inc (coro));
861 UNLOCK; 912 UNLOCK;
862} 913}
863 914
864static void 915static void
865api_schedule (void) 916prepare_schedule (struct transfer_args *ta)
866{ 917{
867 dTHX;
868
869 SV *prev, *next; 918 SV *current, *prev, *next;
919
870 SV *current = GvSV (coro_current); 920 current = GvSV (coro_current);
871 921
872 for (;;) 922 for (;;)
873 { 923 {
874 LOCK; 924 LOCK;
875
876 next = coro_deq (aTHX_ PRIO_MIN); 925 next = coro_deq (PRIO_MIN);
926 UNLOCK;
877 927
878 if (next) 928 if (next)
879 break; 929 break;
880
881 UNLOCK;
882 930
883 { 931 {
884 dSP; 932 dSP;
885 933
886 ENTER; 934 ENTER;
897 945
898 prev = SvRV (current); 946 prev = SvRV (current);
899 SvRV (current) = next; 947 SvRV (current) = next;
900 948
901 /* free this only after the transfer */ 949 /* free this only after the transfer */
950 LOCK;
951 free_coro_mortal ();
952 UNLOCK;
902 coro_mortal = prev; 953 coro_mortal = prev;
903 954
955 ta->prev = SvSTATE (prev);
956 ta->next = SvSTATE (next);
957 ta->flags = TRANSFER_SAVE_ALL;
958}
959
960static void
961prepare_cede (struct transfer_args *ta)
962{
963 LOCK;
964 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
904 UNLOCK; 965 UNLOCK;
905 966
906 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), TRANSFER_SAVE_ALL); 967 prepare_schedule (ta);
907} 968}
908 969
909static int coro_cede_self; 970static void
971api_schedule (void)
972{
973 dTHX;
974 struct transfer_args ta;
975
976 prepare_schedule (&ta);
977 TRANSFER (ta);
978}
910 979
911static void 980static void
912api_cede (void) 981api_cede (void)
913{ 982{
914 dTHX; 983 dTHX;
915 SV *current = SvREFCNT_inc (SvRV (GvSV (coro_current))); 984 struct transfer_args ta;
916 985
917 LOCK; 986 prepare_cede (&ta);
918 987 TRANSFER (ta);
919 if (coro_cede_self)
920 {
921 AV *runqueue = coro_ready [PRIO_MAX - PRIO_MIN];
922 av_unshift (runqueue, 1);
923 av_store (runqueue, 0, current);
924 coro_nready++;
925 coro_cede_self = 0;
926 }
927 else
928 coro_enq (aTHX_ current);
929
930 UNLOCK;
931
932 api_schedule ();
933} 988}
934 989
935MODULE = Coro::State PACKAGE = Coro::State 990MODULE = Coro::State PACKAGE = Coro::State
936 991
937PROTOTYPES: DISABLE 992PROTOTYPES: DISABLE
952 main_mainstack = PL_mainstack; 1007 main_mainstack = PL_mainstack;
953 1008
954 coroapi.ver = CORO_API_VERSION; 1009 coroapi.ver = CORO_API_VERSION;
955 coroapi.transfer = api_transfer; 1010 coroapi.transfer = api_transfer;
956 1011
957 Newz (0, main_stack, 1, coro_stack);
958 main_stack->idle_sp = (void *)-1;
959
960 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1012 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
961} 1013}
962 1014
963SV * 1015SV *
964new (char *klass, ...) 1016new (char *klass, ...)
973 1025
974 hv = newHV (); 1026 hv = newHV ();
975 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1027 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
976 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1028 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
977 1029
978 av_push (coro->args, newSVsv (RETVAL));
979 for (i = 1; i < items; i++) 1030 for (i = 1; i < items; i++)
980 av_push (coro->args, newSVsv (ST (i))); 1031 av_push (coro->args, newSVsv (ST (i)));
981 1032
982 coro->stack = main_stack;
983 /*coro->mainstack = 0; *//*actual work is done inside transfer */ 1033 /*coro->mainstack = 0; *//*actual work is done inside transfer */
984 /*coro->stack = 0;*/ 1034 /*coro->stack = 0;*/
985} 1035}
986 OUTPUT: 1036 OUTPUT:
987 RETVAL 1037 RETVAL
988 1038
989void 1039void
990transfer (prev, next, flags) 1040_set_stacklevel (...)
991 SV *prev 1041 ALIAS:
992 SV *next 1042 Coro::State::transfer = 1
993 int flags 1043 Coro::schedule = 2
1044 Coro::cede = 3
1045 Coro::Cont::yield = 4
994 CODE: 1046 CODE:
995 PUTBACK;
996 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
997 SPAGAIN;
998
999int
1000prio (Coro::State coro, int newprio = 0)
1001 ALIAS:
1002 nice = 1
1003 CODE:
1004{ 1047{
1005 RETVAL = coro->prio; 1048 struct transfer_args ta;
1006 1049
1007 if (items > 1) 1050 switch (ix)
1008 { 1051 {
1052 case 0:
1053 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0)));
1054 ta.next = 0;
1055 ta.flags = TRANSFER_SET_STACKLEVEL;
1056 break;
1057
1058 case 1:
1009 if (ix) 1059 if (items != 3)
1010 newprio += coro->prio; 1060 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items);
1011 1061
1012 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1062 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1013 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1063 break;
1014 1064
1015 coro->prio = newprio; 1065 case 2:
1066 prepare_schedule (&ta);
1067 break;
1068
1069 case 3:
1070 prepare_cede (&ta);
1071 break;
1072
1073 case 4:
1074 {
1075 SV *yieldstack;
1076 SV *sv;
1077 AV *defav = GvAV (PL_defgv);
1078
1079 yieldstack = *hv_fetch (
1080 (HV *)SvRV (GvSV (coro_current)),
1081 "yieldstack", sizeof ("yieldstack") - 1,
1082 0
1083 );
1084
1085 /* set up @_ -- ugly */
1086 av_clear (defav);
1087 av_fill (defav, items - 1);
1088 while (items--)
1089 av_store (defav, items, SvREFCNT_inc (ST(items)));
1090
1091 sv = av_pop ((AV *)SvRV (yieldstack));
1092 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1093 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1094 ta.flags = 0;
1095 SvREFCNT_dec (sv);
1096 }
1097 break;
1098
1016 } 1099 }
1017}
1018 1100
1019void 1101 TRANSFER (ta);
1020_clear_idle_sp (Coro::State self) 1102}
1021 CODE:
1022 self->stack->idle_sp = 0;
1023 1103
1024void 1104void
1025_clone_state_from (SV *dst, SV *src) 1105_clone_state_from (SV *dst, SV *src)
1026 CODE: 1106 CODE:
1027{ 1107{
1030 sv_unmagic (SvRV (dst), PERL_MAGIC_ext); 1110 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1031 1111
1032 ++coro_src->refcnt; 1112 ++coro_src->refcnt;
1033 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1113 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1034} 1114}
1115
1116void
1117_nonlocal_goto (IV nextop)
1118 CODE:
1119 /* uuh, somebody will kill me again for this */
1120 PL_op->op_next = INT2PTR (OP *, nextop);
1035 1121
1036void 1122void
1037_exit (code) 1123_exit (code)
1038 int code 1124 int code
1039 PROTOTYPE: $ 1125 PROTOTYPE: $
1040 CODE: 1126 CODE:
1041 _exit (code); 1127 _exit (code);
1042 1128
1043MODULE = Coro::State PACKAGE = Coro::Cont
1044
1045void
1046yield (...)
1047 PROTOTYPE: @
1048 CODE:
1049{
1050 SV *yieldstack;
1051 SV *sv;
1052 AV *defav = GvAV (PL_defgv);
1053 struct coro *prev, *next;
1054
1055 yieldstack = *hv_fetch (
1056 (HV *)SvRV (GvSV (coro_current)),
1057 "yieldstack", sizeof ("yieldstack") - 1,
1058 0
1059 );
1060
1061 /* set up @_ -- ugly */
1062 av_clear (defav);
1063 av_fill (defav, items - 1);
1064 while (items--)
1065 av_store (defav, items, SvREFCNT_inc (ST(items)));
1066
1067 sv = av_pop ((AV *)SvRV (yieldstack));
1068 prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1069 next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1070 SvREFCNT_dec (sv);
1071
1072 coro_state_transfer (aTHX_ prev, next, 0);
1073}
1074
1075MODULE = Coro::State PACKAGE = Coro 1129MODULE = Coro::State PACKAGE = Coro
1076 1130
1077BOOT: 1131BOOT:
1078{ 1132{
1079 int i; 1133 int i;
1104 1158
1105 GCoroAPI = &coroapi; 1159 GCoroAPI = &coroapi;
1106 sv_setiv (sv, (IV)&coroapi); 1160 sv_setiv (sv, (IV)&coroapi);
1107 SvREADONLY_on (sv); 1161 SvREADONLY_on (sv);
1108 } 1162 }
1163}
1164
1165int
1166prio (Coro::State coro, int newprio = 0)
1167 ALIAS:
1168 nice = 1
1169 CODE:
1170{
1171 RETVAL = coro->prio;
1172
1173 if (items > 1)
1174 {
1175 if (ix)
1176 newprio += coro->prio;
1177
1178 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1179 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1180
1181 coro->prio = newprio;
1182 }
1109} 1183}
1110 1184
1111void 1185void
1112ready (SV *self) 1186ready (SV *self)
1113 PROTOTYPE: $ 1187 PROTOTYPE: $
1120 CODE: 1194 CODE:
1121 RETVAL = coro_nready; 1195 RETVAL = coro_nready;
1122 OUTPUT: 1196 OUTPUT:
1123 RETVAL 1197 RETVAL
1124 1198
1125void
1126schedule (...)
1127 PROTOTYPE:
1128 CODE:
1129 api_schedule ();
1130
1131void
1132_set_cede_self ()
1133 CODE:
1134 coro_cede_self = 1;
1135
1136void
1137cede (...)
1138 PROTOTYPE:
1139 CODE:
1140 api_cede ();
1141
1142MODULE = Coro::State PACKAGE = Coro::AIO 1199MODULE = Coro::State PACKAGE = Coro::AIO
1143 1200
1144SV * 1201SV *
1145_get_state () 1202_get_state ()
1146 CODE: 1203 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines