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.94 by root, Sun Nov 26 17:14:15 2006 UTC vs.
Revision 1.103 by root, Mon Nov 27 01:33:30 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"
8 6
9#include "patchlevel.h" 7#include "patchlevel.h"
10 8
11#if PERL_VERSION < 6 9#define PERL_VERSION_ATLEAST(a,b,c) \
10 (PERL_REVISION > (a) \
11 || (PERL_REVISION == (a) \
12 && (PERL_VERSION > (b) \
13 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
14
15#if !PERL_VERSION_ATLEAST (5,6,0)
12# ifndef PL_ppaddr 16# ifndef PL_ppaddr
13# define PL_ppaddr ppaddr 17# define PL_ppaddr ppaddr
14# endif 18# endif
15# ifndef call_sv 19# ifndef call_sv
16# define call_sv perl_call_sv 20# define call_sv perl_call_sv
66#define dSTACKLEVEL int stacklevel 70#define dSTACKLEVEL int stacklevel
67#define STACKLEVEL ((void *)&stacklevel) 71#define STACKLEVEL ((void *)&stacklevel)
68 72
69#define IN_DESTRUCT (PL_main_cv == Nullcv) 73#define IN_DESTRUCT (PL_main_cv == Nullcv)
70 74
75#if __GNUC__ >= 3
76# define attribute(x) __attribute__(x)
77#else
78# define attribute(x)
79#endif
80
81#define NOINLINE attribute ((noinline))
82
71#include "CoroAPI.h" 83#include "CoroAPI.h"
72 84
73#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ 85#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
74 86
75#ifdef USE_ITHREADS 87#ifdef USE_ITHREADS
93 /* the stack */ 105 /* the stack */
94 void *sptr; 106 void *sptr;
95 long ssize; /* positive == mmap, otherwise malloc */ 107 long ssize; /* positive == mmap, otherwise malloc */
96 108
97 /* cpu state */ 109 /* cpu state */
98 void *idle_sp; /* original stacklevel when coroutine was created */ 110 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
99 JMPENV *top_env; 111 JMPENV *top_env;
100 coro_context cctx; 112 coro_context cctx;
101} coro_stack; 113} coro_stack;
102
103/* the (fake) coro_stack representing the main program */
104static coro_stack *main_stack;
105 114
106/* this is a structure representing a perl-level coroutine */ 115/* this is a structure representing a perl-level coroutine */
107struct coro { 116struct coro {
108 /* the c coroutine allocated to this perl coroutine, if any */ 117 /* the c coroutine allocated to this perl coroutine, if any */
109 coro_stack *stack; 118 coro_stack *stack;
157 166
158typedef struct coro *Coro__State; 167typedef struct coro *Coro__State;
159typedef struct coro *Coro__State_or_hashref; 168typedef struct coro *Coro__State_or_hashref;
160 169
161static AV * 170static AV *
162coro_clone_padlist (pTHX_ CV *cv) 171coro_clone_padlist (CV *cv)
163{ 172{
164 AV *padlist = CvPADLIST (cv); 173 AV *padlist = CvPADLIST (cv);
165 AV *newpadlist, *newpad; 174 AV *newpadlist, *newpad;
166 175
167 newpadlist = newAV (); 176 newpadlist = newAV ();
168 AvREAL_off (newpadlist); 177 AvREAL_off (newpadlist);
169#if PERL_VERSION < 9 178#if PERL_VERSION_ATLEAST (5,9,0)
179 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
180#else
170 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 181 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
171#else
172 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
173#endif 182#endif
174 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 183 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
175 --AvFILLp (padlist); 184 --AvFILLp (padlist);
176 185
177 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 186 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
179 188
180 return newpadlist; 189 return newpadlist;
181} 190}
182 191
183static void 192static void
184free_padlist (pTHX_ AV *padlist) 193free_padlist (AV *padlist)
185{ 194{
186 /* may be during global destruction */ 195 /* may be during global destruction */
187 if (SvREFCNT (padlist)) 196 if (SvREFCNT (padlist))
188 { 197 {
189 I32 i = AvFILLp (padlist); 198 I32 i = AvFILLp (padlist);
210 AV *padlist; 219 AV *padlist;
211 AV *av = (AV *)mg->mg_obj; 220 AV *av = (AV *)mg->mg_obj;
212 221
213 /* casting is fun. */ 222 /* casting is fun. */
214 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 223 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
215 free_padlist (aTHX_ padlist); 224 free_padlist (padlist);
216 225
217 SvREFCNT_dec (av); 226 SvREFCNT_dec (av);
218 227
219 return 0; 228 return 0;
220} 229}
223 232
224static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 233static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
225 234
226/* the next two functions merely cache the padlists */ 235/* the next two functions merely cache the padlists */
227static void 236static void
228get_padlist (pTHX_ CV *cv) 237get_padlist (CV *cv)
229{ 238{
230 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 239 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
231 240
232 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 241 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
233 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 242 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
234 else 243 else
235 { 244 {
236#if 0 245#if 0
237 /* this should work - but it doesn't :( */ 246 /* this is probably cleaner, but also slower? */
238 CV *cp = Perl_cv_clone (aTHX_ cv); 247 CV *cp = Perl_cv_clone (cv);
239 CvPADLIST (cv) = CvPADLIST (cp); 248 CvPADLIST (cv) = CvPADLIST (cp);
240 CvPADLIST (cp) = 0; 249 CvPADLIST (cp) = 0;
241 SvREFCNT_dec (cp); 250 SvREFCNT_dec (cp);
242#else 251#else
243 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 252 CvPADLIST (cv) = coro_clone_padlist (cv);
244#endif 253#endif
245 } 254 }
246} 255}
247 256
248static void 257static void
249put_padlist (pTHX_ CV *cv) 258put_padlist (CV *cv)
250{ 259{
251 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 260 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
252 261
253 if (!mg) 262 if (!mg)
254 { 263 {
262} 271}
263 272
264#define SB do { 273#define SB do {
265#define SE } while (0) 274#define SE } while (0)
266 275
267#define LOAD(state) load_state(aTHX_ (state)); 276#define LOAD(state) load_state((state));
268#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); 277#define SAVE(state,flags) save_state((state),(flags));
269 278
270#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 279#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
271 280
272static void 281static void
273load_state(pTHX_ Coro__State c) 282load_state(Coro__State c)
274{ 283{
275 PL_dowarn = c->dowarn; 284 PL_dowarn = c->dowarn;
276 PL_in_eval = c->in_eval; 285 PL_in_eval = c->in_eval;
277 286
278 PL_curstackinfo = c->curstackinfo; 287 PL_curstackinfo = c->curstackinfo;
296 PL_scopestack_ix = c->scopestack_ix; 305 PL_scopestack_ix = c->scopestack_ix;
297 PL_scopestack_max = c->scopestack_max; 306 PL_scopestack_max = c->scopestack_max;
298 PL_savestack = c->savestack; 307 PL_savestack = c->savestack;
299 PL_savestack_ix = c->savestack_ix; 308 PL_savestack_ix = c->savestack_ix;
300 PL_savestack_max = c->savestack_max; 309 PL_savestack_max = c->savestack_max;
301#if PERL_VERSION < 9 310#if !PERL_VERSION_ATLEAST (5,9,0)
302 PL_retstack = c->retstack; 311 PL_retstack = c->retstack;
303 PL_retstack_ix = c->retstack_ix; 312 PL_retstack_ix = c->retstack_ix;
304 PL_retstack_max = c->retstack_max; 313 PL_retstack_max = c->retstack_max;
305#endif 314#endif
306 PL_curpm = c->curpm; 315 PL_curpm = c->curpm;
319 { 328 {
320 AV *padlist = (AV *)POPs; 329 AV *padlist = (AV *)POPs;
321 330
322 if (padlist) 331 if (padlist)
323 { 332 {
324 put_padlist (aTHX_ cv); /* mark this padlist as available */ 333 put_padlist (cv); /* mark this padlist as available */
325 CvPADLIST(cv) = padlist; 334 CvPADLIST(cv) = padlist;
326 } 335 }
327 336
328 ++CvDEPTH(cv); 337 ++CvDEPTH(cv);
329 } 338 }
331 PUTBACK; 340 PUTBACK;
332 } 341 }
333} 342}
334 343
335static void 344static void
336save_state(pTHX_ Coro__State c, int flags) 345save_state(Coro__State c, int flags)
337{ 346{
338 { 347 {
339 dSP; 348 dSP;
340 I32 cxix = cxstack_ix; 349 I32 cxix = cxstack_ix;
341 PERL_CONTEXT *ccstk = cxstack; 350 PERL_CONTEXT *ccstk = cxstack;
369 } 378 }
370 379
371 PUSHs ((SV *)CvPADLIST(cv)); 380 PUSHs ((SV *)CvPADLIST(cv));
372 PUSHs ((SV *)cv); 381 PUSHs ((SV *)cv);
373 382
374 get_padlist (aTHX_ cv); 383 get_padlist (cv);
375 } 384 }
376 } 385 }
377#ifdef CXt_FORMAT 386#ifdef CXt_FORMAT
378 else if (CxTYPE(cx) == CXt_FORMAT) 387 else if (CxTYPE(cx) == CXt_FORMAT)
379 { 388 {
423 c->scopestack_ix = PL_scopestack_ix; 432 c->scopestack_ix = PL_scopestack_ix;
424 c->scopestack_max = PL_scopestack_max; 433 c->scopestack_max = PL_scopestack_max;
425 c->savestack = PL_savestack; 434 c->savestack = PL_savestack;
426 c->savestack_ix = PL_savestack_ix; 435 c->savestack_ix = PL_savestack_ix;
427 c->savestack_max = PL_savestack_max; 436 c->savestack_max = PL_savestack_max;
428#if PERL_VERSION < 9 437#if !PERL_VERSION_ATLEAST (5,9,0)
429 c->retstack = PL_retstack; 438 c->retstack = PL_retstack;
430 c->retstack_ix = PL_retstack_ix; 439 c->retstack_ix = PL_retstack_ix;
431 c->retstack_max = PL_retstack_max; 440 c->retstack_max = PL_retstack_max;
432#endif 441#endif
433 c->curpm = PL_curpm; 442 c->curpm = PL_curpm;
439 * of perl.c:init_stacks, except that it uses less memory 448 * of perl.c:init_stacks, except that it uses less memory
440 * on the (sometimes correct) assumption that coroutines do 449 * on the (sometimes correct) assumption that coroutines do
441 * not usually need a lot of stackspace. 450 * not usually need a lot of stackspace.
442 */ 451 */
443static void 452static void
444coro_init_stacks (pTHX) 453coro_init_stacks ()
445{ 454{
446 LOCK;
447
448 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 455 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
449 PL_curstackinfo->si_type = PERLSI_MAIN; 456 PL_curstackinfo->si_type = PERLSI_MAIN;
450 PL_curstack = PL_curstackinfo->si_stack; 457 PL_curstack = PL_curstackinfo->si_stack;
451 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 458 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
452 459
473 480
474 New(54,PL_savestack,96,ANY); 481 New(54,PL_savestack,96,ANY);
475 PL_savestack_ix = 0; 482 PL_savestack_ix = 0;
476 PL_savestack_max = 96; 483 PL_savestack_max = 96;
477 484
478#if PERL_VERSION < 9 485#if !PERL_VERSION_ATLEAST (5,9,0)
479 New(54,PL_retstack,8,OP*); 486 New(54,PL_retstack,8,OP*);
480 PL_retstack_ix = 0; 487 PL_retstack_ix = 0;
481 PL_retstack_max = 8; 488 PL_retstack_max = 8;
482#endif 489#endif
483
484 UNLOCK;
485} 490}
486 491
487/* 492/*
488 * destroy the stacks, the callchain etc... 493 * destroy the stacks, the callchain etc...
489 */ 494 */
490static void 495static void
491destroy_stacks(pTHX) 496coro_destroy_stacks()
492{ 497{
493 if (!IN_DESTRUCT) 498 if (!IN_DESTRUCT)
494 { 499 {
495 /* is this ugly, I ask? */ 500 /* is this ugly, I ask? */
496 LEAVE_SCOPE (0); 501 LEAVE_SCOPE (0);
527 532
528 Safefree (PL_tmps_stack); 533 Safefree (PL_tmps_stack);
529 Safefree (PL_markstack); 534 Safefree (PL_markstack);
530 Safefree (PL_scopestack); 535 Safefree (PL_scopestack);
531 Safefree (PL_savestack); 536 Safefree (PL_savestack);
532#if PERL_VERSION < 9 537#if !PERL_VERSION_ATLEAST (5,9,0)
533 Safefree (PL_retstack); 538 Safefree (PL_retstack);
534#endif 539#endif
535} 540}
536 541
537static void 542static void
538setup_coro (struct coro *coro) 543setup_coro (struct coro *coro)
539{ 544{
540 /* 545 /*
541 * emulate part of the perl startup here. 546 * emulate part of the perl startup here.
542 */ 547 */
543 dTHX;
544 dSP;
545 UNOP myop;
546 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
547 548
548 coro_init_stacks (aTHX); 549 coro_init_stacks ();
550
549 /*PL_curcop = 0;*/ 551 PL_curcop = 0;
550 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 552 PL_in_eval = 0;
553 PL_curpm = 0;
554
555 {
556 dSP;
557 LOGOP myop;
558
559 /* I have no idea why this is needed, but it is */
560 PUSHMARK (SP);
561
551 SvREFCNT_dec (GvAV (PL_defgv)); 562 SvREFCNT_dec (GvAV (PL_defgv));
552 GvAV (PL_defgv) = coro->args; coro->args = 0; 563 GvAV (PL_defgv) = coro->args; coro->args = 0;
553 564
554 SPAGAIN;
555
556 Zero (&myop, 1, UNOP); 565 Zero (&myop, 1, LOGOP);
557 myop.op_next = Nullop; 566 myop.op_next = Nullop;
558 myop.op_flags = OPf_WANT_VOID; 567 myop.op_flags = OPf_WANT_VOID;
559 568
560 PL_op = (OP *)&myop; 569 PL_op = (OP *)&myop;
561 570
562 PUSHMARK(SP); 571 PUSHMARK (SP);
563 XPUSHs (sub_init); 572 XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE));
564 PUTBACK; 573 PUTBACK;
565 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 574 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
566 SPAGAIN; 575 SPAGAIN;
567 576
568 ENTER; /* necessary e.g. for dounwind */ 577 ENTER; /* necessary e.g. for dounwind */
578 }
569} 579}
570 580
571static void 581static void
572free_coro_mortal () 582free_coro_mortal ()
573{ 583{
576 SvREFCNT_dec (coro_mortal); 586 SvREFCNT_dec (coro_mortal);
577 coro_mortal = 0; 587 coro_mortal = 0;
578 } 588 }
579} 589}
580 590
591static void NOINLINE
592prepare_cctx (coro_stack *cctx)
593{
594 dSP;
595 LOGOP myop;
596
597 Zero (&myop, 1, LOGOP);
598 myop.op_next = PL_op;
599 myop.op_flags = OPf_WANT_VOID;
600
601 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx));
602
603 PUSHMARK (SP);
604 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE));
605 PUTBACK;
606 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
607 SPAGAIN;
608}
609
581static void 610static void
582coro_run (void *arg) 611coro_run (void *arg)
583{ 612{
613 /* coro_run is the alternative epilogue of transfer() */
614 UNLOCK;
615
584 /* 616 /*
585 * this is a _very_ stripped down perl interpreter ;) 617 * this is a _very_ stripped down perl interpreter ;)
586 */ 618 */
587 dTHX;
588 int ret;
589
590 UNLOCK;
591
592 PL_top_env = &PL_start_env; 619 PL_top_env = &PL_start_env;
593 620 /* inject call to cctx_init */
594 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); 621 prepare_cctx ((coro_stack *)arg);
595 sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op));
596
597 /* continue at cctx_init, without entersub */
598 PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE));
599 622
600 /* somebody will hit me for both perl_run and PL_restartop */ 623 /* somebody will hit me for both perl_run and PL_restartop */
601 ret = perl_run (aTHX_ PERL_GET_CONTEXT); 624 perl_run (PL_curinterp);
602 printf ("ret %d\n", ret);//D
603 625
604 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); 626 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr);
605 abort (); 627 abort ();
606} 628}
607 629
647} 669}
648 670
649static void 671static void
650stack_free (coro_stack *stack) 672stack_free (coro_stack *stack)
651{ 673{
652 if (!stack || stack == main_stack) 674 if (!stack)
653 return; 675 return;
654 676
655#if HAVE_MMAP 677#if HAVE_MMAP
656 munmap (stack->sptr, stack->ssize); 678 munmap (stack->sptr, stack->ssize);
657#else 679#else
660 682
661 Safefree (stack); 683 Safefree (stack);
662} 684}
663 685
664static coro_stack *stack_first; 686static coro_stack *stack_first;
687static int cctx_count, cctx_idle;
665 688
666static coro_stack * 689static coro_stack *
667stack_get () 690stack_get ()
668{ 691{
669 coro_stack *stack; 692 coro_stack *stack;
670 693
671 if (stack_first) 694 if (stack_first)
672 { 695 {
696 --cctx_idle;
673 stack = stack_first; 697 stack = stack_first;
674 stack_first = stack->next; 698 stack_first = stack->next;
675 } 699 }
676 else 700 else
677 { 701 {
702 ++cctx_count;
678 stack = stack_new (); 703 stack = stack_new ();
679 PL_op = PL_op->op_next; 704 PL_op = PL_op->op_next;
680 } 705 }
681 706
682 return stack; 707 return stack;
683} 708}
684 709
685static void 710static void
686stack_put (coro_stack *stack) 711stack_put (coro_stack *stack)
687{ 712{
713 ++cctx_idle;
688 stack->next = stack_first; 714 stack->next = stack_first;
689 stack_first = stack; 715 stack_first = stack;
690} 716}
691 717
692/* never call directly, always through the coro_state_transfer global variable */ 718/* never call directly, always through the coro_state_transfer global variable */
693static void 719static void NOINLINE
694transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 720transfer (struct coro *prev, struct coro *next, int flags)
695{ 721{
696 dSTACKLEVEL; 722 dSTACKLEVEL;
697 723
698 /* sometimes transfer is only called to set idle_sp */ 724 /* sometimes transfer is only called to set idle_sp */
699 if (flags == TRANSFER_SET_STACKLEVEL) 725 if (flags == TRANSFER_SET_STACKLEVEL)
719 setup_coro (next); 745 setup_coro (next);
720 /* need a stack */ 746 /* need a stack */
721 next->stack = 0; 747 next->stack = 0;
722 } 748 }
723 749
750 if (!prev->stack)
751 /* create a new empty context */
752 Newz (0, prev->stack, 1, coro_stack);
753
724 prev__stack = prev->stack; 754 prev__stack = prev->stack;
725 755
726 /* possibly "free" the stack */ 756 /* possibly "free" the stack */
727 if (prev__stack->idle_sp == STACKLEVEL) 757 if (prev__stack->idle_sp == STACKLEVEL)
728 { 758 {
735 765
736 if (prev__stack != next->stack) 766 if (prev__stack != next->stack)
737 { 767 {
738 prev__stack->top_env = PL_top_env; 768 prev__stack->top_env = PL_top_env;
739 PL_top_env = next->stack->top_env; 769 PL_top_env = next->stack->top_env;
740 printf ("stacksw %p %p\n", prev__stack->idle_sp, next->stack->idle_sp);//D
741 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 770 coro_transfer (&prev__stack->cctx, &next->stack->cctx);
742 } 771 }
743 772
744 free_coro_mortal (); 773 free_coro_mortal ();
745 774
746 UNLOCK; 775 UNLOCK;
747 } 776 }
748} 777}
749
750/* use this function pointer to call the above function */
751/* this is done to increase chances of the compiler not inlining the call */
752/* not static to make it even harder for the compiler (and theoretically impossible in most cases */
753void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl;
754 778
755struct transfer_args 779struct transfer_args
756{ 780{
757 struct coro *prev, *next; 781 struct coro *prev, *next;
758 int flags; 782 int flags;
759}; 783};
760 784
761#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) 785#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
762 786
763static void 787static void
764coro_state_destroy (struct coro *coro) 788coro_state_destroy (struct coro *coro)
765{ 789{
766 if (coro->refcnt--) 790 if (coro->refcnt--)
768 792
769 if (coro->mainstack && coro->mainstack != main_mainstack) 793 if (coro->mainstack && coro->mainstack != main_mainstack)
770 { 794 {
771 struct coro temp; 795 struct coro temp;
772 796
773 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 797 SAVE ((&temp), TRANSFER_SAVE_ALL);
774 LOAD (aTHX_ coro); 798 LOAD (coro);
775 799
776 destroy_stacks (aTHX); 800 coro_destroy_stacks ();
777 801
778 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 802 LOAD ((&temp)); /* this will get rid of defsv etc.. */
779 803
780 coro->mainstack = 0; 804 coro->mainstack = 0;
781 } 805 }
784 SvREFCNT_dec (coro->args); 808 SvREFCNT_dec (coro->args);
785 Safefree (coro); 809 Safefree (coro);
786} 810}
787 811
788static int 812static int
789coro_state_clear (SV *sv, MAGIC *mg) 813coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
790{ 814{
791 struct coro *coro = (struct coro *)mg->mg_ptr; 815 struct coro *coro = (struct coro *)mg->mg_ptr;
792 mg->mg_ptr = 0; 816 mg->mg_ptr = 0;
793 817
794 coro_state_destroy (coro); 818 coro_state_destroy (coro);
795 819
796 return 0; 820 return 0;
797} 821}
798 822
799static int 823static int
800coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 824coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
801{ 825{
802 struct coro *coro = (struct coro *)mg->mg_ptr; 826 struct coro *coro = (struct coro *)mg->mg_ptr;
803 827
804 ++coro->refcnt; 828 ++coro->refcnt;
805 829
806 return 0; 830 return 0;
807} 831}
808 832
809static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 833static MGVTBL coro_state_vtbl = {
834 0, 0, 0, 0,
835 coro_state_clear,
836 0,
837#ifdef MGf_DUP
838 coro_state_dup,
839#else
840# define MGf_DUP 0
841#endif
842};
810 843
811static struct coro * 844static struct coro *
812SvSTATE (SV *coro) 845SvSTATE (SV *coro)
813{ 846{
814 HV *stash; 847 HV *stash;
829 assert (mg->mg_type == PERL_MAGIC_ext); 862 assert (mg->mg_type == PERL_MAGIC_ext);
830 return (struct coro *)mg->mg_ptr; 863 return (struct coro *)mg->mg_ptr;
831} 864}
832 865
833static void 866static void
834prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) 867prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
835{ 868{
836 ta->prev = SvSTATE (prev); 869 ta->prev = SvSTATE (prev);
837 ta->next = SvSTATE (next); 870 ta->next = SvSTATE (next);
838 ta->flags = flags; 871 ta->flags = flags;
839} 872}
842api_transfer (SV *prev, SV *next, int flags) 875api_transfer (SV *prev, SV *next, int flags)
843{ 876{
844 dTHX; 877 dTHX;
845 struct transfer_args ta; 878 struct transfer_args ta;
846 879
847 prepare_transfer (aTHX_ &ta, prev, next, flags); 880 prepare_transfer (&ta, prev, next, flags);
848 TRANSFER (ta); 881 TRANSFER (ta);
849} 882}
850 883
851/** Coro ********************************************************************/ 884/** Coro ********************************************************************/
852 885
861static GV *coro_current, *coro_idle; 894static GV *coro_current, *coro_idle;
862static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 895static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
863static int coro_nready; 896static int coro_nready;
864 897
865static void 898static void
866coro_enq (pTHX_ SV *sv) 899coro_enq (SV *sv)
867{ 900{
868 int prio; 901 int prio;
869 902
870 if (SvTYPE (sv) != SVt_PVHV) 903 if (SvTYPE (sv) != SVt_PVHV)
871 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 904 croak ("Coro::ready tried to enqueue something that is not a coroutine");
875 av_push (coro_ready [prio - PRIO_MIN], sv); 908 av_push (coro_ready [prio - PRIO_MIN], sv);
876 coro_nready++; 909 coro_nready++;
877} 910}
878 911
879static SV * 912static SV *
880coro_deq (pTHX_ int min_prio) 913coro_deq (int min_prio)
881{ 914{
882 int prio = PRIO_MAX - PRIO_MIN; 915 int prio = PRIO_MAX - PRIO_MIN;
883 916
884 min_prio -= PRIO_MIN; 917 min_prio -= PRIO_MIN;
885 if (min_prio < 0) 918 if (min_prio < 0)
902 935
903 if (SvROK (coro)) 936 if (SvROK (coro))
904 coro = SvRV (coro); 937 coro = SvRV (coro);
905 938
906 LOCK; 939 LOCK;
907 coro_enq (aTHX_ SvREFCNT_inc (coro)); 940 coro_enq (SvREFCNT_inc (coro));
908 UNLOCK; 941 UNLOCK;
909} 942}
910 943
911static void 944static void
912prepare_schedule (aTHX_ struct transfer_args *ta) 945prepare_schedule (struct transfer_args *ta)
913{ 946{
914 SV *current, *prev, *next; 947 SV *current, *prev, *next;
915
916 LOCK;
917 948
918 current = GvSV (coro_current); 949 current = GvSV (coro_current);
919 950
920 for (;;) 951 for (;;)
921 { 952 {
922 LOCK; 953 LOCK;
923
924 next = coro_deq (aTHX_ PRIO_MIN); 954 next = coro_deq (PRIO_MIN);
955 UNLOCK;
925 956
926 if (next) 957 if (next)
927 break; 958 break;
928
929 UNLOCK;
930 959
931 { 960 {
932 dSP; 961 dSP;
933 962
934 ENTER; 963 ENTER;
945 974
946 prev = SvRV (current); 975 prev = SvRV (current);
947 SvRV (current) = next; 976 SvRV (current) = next;
948 977
949 /* free this only after the transfer */ 978 /* free this only after the transfer */
979 LOCK;
950 free_coro_mortal (); 980 free_coro_mortal ();
981 UNLOCK;
951 coro_mortal = prev; 982 coro_mortal = prev;
952 983
953 ta->prev = SvSTATE (prev); 984 ta->prev = SvSTATE (prev);
954 ta->next = SvSTATE (next); 985 ta->next = SvSTATE (next);
955 ta->flags = TRANSFER_SAVE_ALL; 986 ta->flags = TRANSFER_SAVE_ALL;
956
957 UNLOCK;
958} 987}
959 988
960static void 989static void
961prepare_cede (aTHX_ struct transfer_args *ta) 990prepare_cede (struct transfer_args *ta)
962{ 991{
963 LOCK; 992 LOCK;
964 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); 993 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
965 UNLOCK; 994 UNLOCK;
966 995
967 prepare_schedule (ta); 996 prepare_schedule (ta);
968} 997}
969 998
1007 main_mainstack = PL_mainstack; 1036 main_mainstack = PL_mainstack;
1008 1037
1009 coroapi.ver = CORO_API_VERSION; 1038 coroapi.ver = CORO_API_VERSION;
1010 coroapi.transfer = api_transfer; 1039 coroapi.transfer = api_transfer;
1011 1040
1012 Newz (0, main_stack, 1, coro_stack);
1013 main_stack->idle_sp = (void *)-1;
1014
1015 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1041 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1016} 1042}
1017 1043
1018SV * 1044SV *
1019new (char *klass, ...) 1045new (char *klass, ...)
1031 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1057 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1032 1058
1033 for (i = 1; i < items; i++) 1059 for (i = 1; i < items; i++)
1034 av_push (coro->args, newSVsv (ST (i))); 1060 av_push (coro->args, newSVsv (ST (i)));
1035 1061
1036 coro->stack = main_stack;
1037 /*coro->mainstack = 0; *//*actual work is done inside transfer */ 1062 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1038 /*coro->stack = 0;*/ 1063 /*coro->stack = 0;*/
1039} 1064}
1040 OUTPUT: 1065 OUTPUT:
1041 RETVAL 1066 RETVAL
1116 ++coro_src->refcnt; 1141 ++coro_src->refcnt;
1117 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1142 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1118} 1143}
1119 1144
1120void 1145void
1121_nonlocal_goto (IV nextop)
1122 CODE:
1123 /* uuh, somebody will kill me again for this */
1124 PL_op->op_next = INT2PTR (OP *, nextop);
1125
1126void
1127_exit (code) 1146_exit (code)
1128 int code 1147 int code
1129 PROTOTYPE: $ 1148 PROTOTYPE: $
1130 CODE: 1149 CODE:
1131 _exit (code); 1150 _exit (code);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines