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.97 by root, Sun Nov 26 21:21:14 2006 UTC vs.
Revision 1.105 by root, Mon Nov 27 02:01:33 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#if USE_VALGRIND
10# include <valgrind/valgrind.h>
11#endif
12
13#define PERL_VERSION_ATLEAST(a,b,c) \
14 (PERL_REVISION > (a) \
15 || (PERL_REVISION == (a) \
16 && (PERL_VERSION > (b) \
17 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
18
19#if !PERL_VERSION_ATLEAST (5,6,0)
12# ifndef PL_ppaddr 20# ifndef PL_ppaddr
13# define PL_ppaddr ppaddr 21# define PL_ppaddr ppaddr
14# endif 22# endif
15# ifndef call_sv 23# ifndef call_sv
16# define call_sv perl_call_sv 24# define call_sv perl_call_sv
66#define dSTACKLEVEL int stacklevel 74#define dSTACKLEVEL int stacklevel
67#define STACKLEVEL ((void *)&stacklevel) 75#define STACKLEVEL ((void *)&stacklevel)
68 76
69#define IN_DESTRUCT (PL_main_cv == Nullcv) 77#define IN_DESTRUCT (PL_main_cv == Nullcv)
70 78
79#if __GNUC__ >= 3
80# define attribute(x) __attribute__(x)
81#else
82# define attribute(x)
83#endif
84
85#define NOINLINE attribute ((noinline))
86
71#include "CoroAPI.h" 87#include "CoroAPI.h"
72 88
73#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ 89#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
74 90
75#ifdef USE_ITHREADS 91#ifdef USE_ITHREADS
96 112
97 /* cpu state */ 113 /* cpu state */
98 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 114 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
99 JMPENV *top_env; 115 JMPENV *top_env;
100 coro_context cctx; 116 coro_context cctx;
117
118#if USE_VALGRIND
119 int valgrind_id;
120#endif
101} coro_stack; 121} coro_stack;
102 122
103/* this is a structure representing a perl-level coroutine */ 123/* this is a structure representing a perl-level coroutine */
104struct coro { 124struct coro {
105 /* the c coroutine allocated to this perl coroutine, if any */ 125 /* the c coroutine allocated to this perl coroutine, if any */
154 174
155typedef struct coro *Coro__State; 175typedef struct coro *Coro__State;
156typedef struct coro *Coro__State_or_hashref; 176typedef struct coro *Coro__State_or_hashref;
157 177
158static AV * 178static AV *
159coro_clone_padlist (pTHX_ CV *cv) 179coro_clone_padlist (CV *cv)
160{ 180{
161 AV *padlist = CvPADLIST (cv); 181 AV *padlist = CvPADLIST (cv);
162 AV *newpadlist, *newpad; 182 AV *newpadlist, *newpad;
163 183
164 newpadlist = newAV (); 184 newpadlist = newAV ();
165 AvREAL_off (newpadlist); 185 AvREAL_off (newpadlist);
166#if PERL_VERSION < 9 186#if PERL_VERSION_ATLEAST (5,9,0)
187 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
188#else
167 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 189 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
168#else
169 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
170#endif 190#endif
171 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 191 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
172 --AvFILLp (padlist); 192 --AvFILLp (padlist);
173 193
174 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 194 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
176 196
177 return newpadlist; 197 return newpadlist;
178} 198}
179 199
180static void 200static void
181free_padlist (pTHX_ AV *padlist) 201free_padlist (AV *padlist)
182{ 202{
183 /* may be during global destruction */ 203 /* may be during global destruction */
184 if (SvREFCNT (padlist)) 204 if (SvREFCNT (padlist))
185 { 205 {
186 I32 i = AvFILLp (padlist); 206 I32 i = AvFILLp (padlist);
207 AV *padlist; 227 AV *padlist;
208 AV *av = (AV *)mg->mg_obj; 228 AV *av = (AV *)mg->mg_obj;
209 229
210 /* casting is fun. */ 230 /* casting is fun. */
211 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 231 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
212 free_padlist (aTHX_ padlist); 232 free_padlist (padlist);
213 233
214 SvREFCNT_dec (av); 234 SvREFCNT_dec (av);
215 235
216 return 0; 236 return 0;
217} 237}
220 240
221static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 241static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
222 242
223/* the next two functions merely cache the padlists */ 243/* the next two functions merely cache the padlists */
224static void 244static void
225get_padlist (pTHX_ CV *cv) 245get_padlist (CV *cv)
226{ 246{
227 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 247 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
228 248
229 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 249 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
230 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 250 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
231 else 251 else
232 { 252 {
233#if 0 253#if 0
234 /* this is probably cleaner, but also slower? */ 254 /* this is probably cleaner, but also slower? */
235 CV *cp = Perl_cv_clone (aTHX_ cv); 255 CV *cp = Perl_cv_clone (cv);
236 CvPADLIST (cv) = CvPADLIST (cp); 256 CvPADLIST (cv) = CvPADLIST (cp);
237 CvPADLIST (cp) = 0; 257 CvPADLIST (cp) = 0;
238 SvREFCNT_dec (cp); 258 SvREFCNT_dec (cp);
239#else 259#else
240 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 260 CvPADLIST (cv) = coro_clone_padlist (cv);
241#endif 261#endif
242 } 262 }
243} 263}
244 264
245static void 265static void
246put_padlist (pTHX_ CV *cv) 266put_padlist (CV *cv)
247{ 267{
248 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 268 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
249 269
250 if (!mg) 270 if (!mg)
251 { 271 {
259} 279}
260 280
261#define SB do { 281#define SB do {
262#define SE } while (0) 282#define SE } while (0)
263 283
264#define LOAD(state) load_state(aTHX_ (state)); 284#define LOAD(state) load_state((state));
265#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); 285#define SAVE(state,flags) save_state((state),(flags));
266 286
267#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 287#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
268 288
269static void 289static void
270load_state(pTHX_ Coro__State c) 290load_state(Coro__State c)
271{ 291{
272 PL_dowarn = c->dowarn; 292 PL_dowarn = c->dowarn;
273 PL_in_eval = c->in_eval; 293 PL_in_eval = c->in_eval;
274 294
275 PL_curstackinfo = c->curstackinfo; 295 PL_curstackinfo = c->curstackinfo;
293 PL_scopestack_ix = c->scopestack_ix; 313 PL_scopestack_ix = c->scopestack_ix;
294 PL_scopestack_max = c->scopestack_max; 314 PL_scopestack_max = c->scopestack_max;
295 PL_savestack = c->savestack; 315 PL_savestack = c->savestack;
296 PL_savestack_ix = c->savestack_ix; 316 PL_savestack_ix = c->savestack_ix;
297 PL_savestack_max = c->savestack_max; 317 PL_savestack_max = c->savestack_max;
298#if PERL_VERSION < 9 318#if !PERL_VERSION_ATLEAST (5,9,0)
299 PL_retstack = c->retstack; 319 PL_retstack = c->retstack;
300 PL_retstack_ix = c->retstack_ix; 320 PL_retstack_ix = c->retstack_ix;
301 PL_retstack_max = c->retstack_max; 321 PL_retstack_max = c->retstack_max;
302#endif 322#endif
303 PL_curpm = c->curpm; 323 PL_curpm = c->curpm;
316 { 336 {
317 AV *padlist = (AV *)POPs; 337 AV *padlist = (AV *)POPs;
318 338
319 if (padlist) 339 if (padlist)
320 { 340 {
321 put_padlist (aTHX_ cv); /* mark this padlist as available */ 341 put_padlist (cv); /* mark this padlist as available */
322 CvPADLIST(cv) = padlist; 342 CvPADLIST(cv) = padlist;
323 } 343 }
324 344
325 ++CvDEPTH(cv); 345 ++CvDEPTH(cv);
326 } 346 }
328 PUTBACK; 348 PUTBACK;
329 } 349 }
330} 350}
331 351
332static void 352static void
333save_state(pTHX_ Coro__State c, int flags) 353save_state(Coro__State c, int flags)
334{ 354{
335 { 355 {
336 dSP; 356 dSP;
337 I32 cxix = cxstack_ix; 357 I32 cxix = cxstack_ix;
338 PERL_CONTEXT *ccstk = cxstack; 358 PERL_CONTEXT *ccstk = cxstack;
366 } 386 }
367 387
368 PUSHs ((SV *)CvPADLIST(cv)); 388 PUSHs ((SV *)CvPADLIST(cv));
369 PUSHs ((SV *)cv); 389 PUSHs ((SV *)cv);
370 390
371 get_padlist (aTHX_ cv); 391 get_padlist (cv);
372 } 392 }
373 } 393 }
374#ifdef CXt_FORMAT 394#ifdef CXt_FORMAT
375 else if (CxTYPE(cx) == CXt_FORMAT) 395 else if (CxTYPE(cx) == CXt_FORMAT)
376 { 396 {
420 c->scopestack_ix = PL_scopestack_ix; 440 c->scopestack_ix = PL_scopestack_ix;
421 c->scopestack_max = PL_scopestack_max; 441 c->scopestack_max = PL_scopestack_max;
422 c->savestack = PL_savestack; 442 c->savestack = PL_savestack;
423 c->savestack_ix = PL_savestack_ix; 443 c->savestack_ix = PL_savestack_ix;
424 c->savestack_max = PL_savestack_max; 444 c->savestack_max = PL_savestack_max;
425#if PERL_VERSION < 9 445#if !PERL_VERSION_ATLEAST (5,9,0)
426 c->retstack = PL_retstack; 446 c->retstack = PL_retstack;
427 c->retstack_ix = PL_retstack_ix; 447 c->retstack_ix = PL_retstack_ix;
428 c->retstack_max = PL_retstack_max; 448 c->retstack_max = PL_retstack_max;
429#endif 449#endif
430 c->curpm = PL_curpm; 450 c->curpm = PL_curpm;
436 * of perl.c:init_stacks, except that it uses less memory 456 * of perl.c:init_stacks, except that it uses less memory
437 * on the (sometimes correct) assumption that coroutines do 457 * on the (sometimes correct) assumption that coroutines do
438 * not usually need a lot of stackspace. 458 * not usually need a lot of stackspace.
439 */ 459 */
440static void 460static void
441coro_init_stacks (pTHX) 461coro_init_stacks ()
442{ 462{
443 LOCK;
444
445 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 463 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
446 PL_curstackinfo->si_type = PERLSI_MAIN; 464 PL_curstackinfo->si_type = PERLSI_MAIN;
447 PL_curstack = PL_curstackinfo->si_stack; 465 PL_curstack = PL_curstackinfo->si_stack;
448 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 466 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
449 467
470 488
471 New(54,PL_savestack,96,ANY); 489 New(54,PL_savestack,96,ANY);
472 PL_savestack_ix = 0; 490 PL_savestack_ix = 0;
473 PL_savestack_max = 96; 491 PL_savestack_max = 96;
474 492
475#if PERL_VERSION < 9 493#if !PERL_VERSION_ATLEAST (5,9,0)
476 New(54,PL_retstack,8,OP*); 494 New(54,PL_retstack,8,OP*);
477 PL_retstack_ix = 0; 495 PL_retstack_ix = 0;
478 PL_retstack_max = 8; 496 PL_retstack_max = 8;
479#endif 497#endif
480
481 UNLOCK;
482} 498}
483 499
484/* 500/*
485 * destroy the stacks, the callchain etc... 501 * destroy the stacks, the callchain etc...
486 */ 502 */
487static void 503static void
488destroy_stacks(pTHX) 504coro_destroy_stacks()
489{ 505{
490 if (!IN_DESTRUCT) 506 if (!IN_DESTRUCT)
491 { 507 {
492 /* is this ugly, I ask? */ 508 /* is this ugly, I ask? */
493 LEAVE_SCOPE (0); 509 LEAVE_SCOPE (0);
524 540
525 Safefree (PL_tmps_stack); 541 Safefree (PL_tmps_stack);
526 Safefree (PL_markstack); 542 Safefree (PL_markstack);
527 Safefree (PL_scopestack); 543 Safefree (PL_scopestack);
528 Safefree (PL_savestack); 544 Safefree (PL_savestack);
529#if PERL_VERSION < 9 545#if !PERL_VERSION_ATLEAST (5,9,0)
530 Safefree (PL_retstack); 546 Safefree (PL_retstack);
531#endif 547#endif
532} 548}
533 549
534static void 550static void
535setup_coro (struct coro *coro) 551setup_coro (struct coro *coro)
536{ 552{
537 /* 553 /*
538 * emulate part of the perl startup here. 554 * emulate part of the perl startup here.
539 */ 555 */
540 dTHX;
541 dSP;
542 UNOP myop;
543 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
544 556
545 coro_init_stacks (aTHX); 557 coro_init_stacks ();
558
546 /*PL_curcop = 0;*/ 559 PL_curcop = 0;
547 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 560 PL_in_eval = 0;
561 PL_curpm = 0;
562
563 {
564 dSP;
565 LOGOP myop;
566
567 /* I have no idea why this is needed, but it is */
568 PUSHMARK (SP);
569
548 SvREFCNT_dec (GvAV (PL_defgv)); 570 SvREFCNT_dec (GvAV (PL_defgv));
549 GvAV (PL_defgv) = coro->args; coro->args = 0; 571 GvAV (PL_defgv) = coro->args; coro->args = 0;
550 572
551 SPAGAIN;
552
553 Zero (&myop, 1, UNOP); 573 Zero (&myop, 1, LOGOP);
554 myop.op_next = Nullop; 574 myop.op_next = Nullop;
555 myop.op_flags = OPf_WANT_VOID; 575 myop.op_flags = OPf_WANT_VOID;
556 576
557 PL_op = (OP *)&myop; 577 PL_op = (OP *)&myop;
558 578
559 PUSHMARK(SP); 579 PUSHMARK (SP);
560 XPUSHs (sub_init); 580 XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE));
561 PUTBACK; 581 PUTBACK;
562 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 582 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
563 SPAGAIN; 583 SPAGAIN;
564 584
565 ENTER; /* necessary e.g. for dounwind */ 585 ENTER; /* necessary e.g. for dounwind */
586 }
566} 587}
567 588
568static void 589static void
569free_coro_mortal () 590free_coro_mortal ()
570{ 591{
571 if (coro_mortal) 592 if (coro_mortal)
572 { 593 {
573 dTHX;
574
575 SvREFCNT_dec (coro_mortal); 594 SvREFCNT_dec (coro_mortal);
576 coro_mortal = 0; 595 coro_mortal = 0;
577 } 596 }
578} 597}
579 598
599static void NOINLINE
600prepare_cctx (coro_stack *cctx)
601{
602 dSP;
603 LOGOP myop;
604
605 Zero (&myop, 1, LOGOP);
606 myop.op_next = PL_op;
607 myop.op_flags = OPf_WANT_VOID;
608
609 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx));
610
611 PUSHMARK (SP);
612 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE));
613 PUTBACK;
614 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
615 SPAGAIN;
616}
617
580static void 618static void
581coro_run (void *arg) 619coro_run (void *arg)
582{ 620{
621 /* coro_run is the alternative epilogue of transfer() */
622 UNLOCK;
623
583 /* 624 /*
584 * this is a _very_ stripped down perl interpreter ;) 625 * this is a _very_ stripped down perl interpreter ;)
585 */ 626 */
586 dTHX;
587 int ret;
588
589 UNLOCK;
590
591 PL_top_env = &PL_start_env; 627 PL_top_env = &PL_start_env;
592 628 /* inject call to cctx_init */
593 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); 629 prepare_cctx ((coro_stack *)arg);
594 sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op));
595
596 /* continue at cctx_init, without entersub */
597 PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE));
598 630
599 /* somebody will hit me for both perl_run and PL_restartop */ 631 /* somebody will hit me for both perl_run and PL_restartop */
600 ret = perl_run (aTHX_ PERL_GET_CONTEXT); 632 perl_run (PL_curinterp);
601 printf ("ret %d\n", ret);//D
602 633
603 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); 634 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
604 abort (); 635 abort ();
605} 636}
606 637
607static coro_stack * 638static coro_stack *
608stack_new () 639stack_new ()
638 _exit (EXIT_FAILURE); 669 _exit (EXIT_FAILURE);
639 } 670 }
640 671
641#endif 672#endif
642 673
674#if USE_VALGRIND
675 stack->valgrind_id = VALGRIND_STACK_REGISTER (
676 STACKGUARD * PAGESIZE + (char *)stack->sptr,
677 stack->ssize + (char *)stack->sptr
678 );
679#endif
680
643 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); 681 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize);
644 682
645 return stack; 683 return stack;
646} 684}
647 685
648static void 686static void
649stack_free (coro_stack *stack) 687stack_free (coro_stack *stack)
650{ 688{
651 if (!stack) 689 if (!stack)
652 return; 690 return;
691
692#if USE_VALGRIND
693 VALGRIND_STACK_DEREGISTER (stack->valgrind_id);
694#endif
653 695
654#if HAVE_MMAP 696#if HAVE_MMAP
655 munmap (stack->sptr, stack->ssize); 697 munmap (stack->sptr, stack->ssize);
656#else 698#else
657 Safefree (stack->sptr); 699 Safefree (stack->sptr);
659 701
660 Safefree (stack); 702 Safefree (stack);
661} 703}
662 704
663static coro_stack *stack_first; 705static coro_stack *stack_first;
706static int cctx_count, cctx_idle;
664 707
665static coro_stack * 708static coro_stack *
666stack_get () 709stack_get ()
667{ 710{
668 coro_stack *stack; 711 coro_stack *stack;
669 712
670 if (stack_first) 713 if (stack_first)
671 { 714 {
715 --cctx_idle;
672 stack = stack_first; 716 stack = stack_first;
673 stack_first = stack->next; 717 stack_first = stack->next;
674 } 718 }
675 else 719 else
676 { 720 {
721 ++cctx_count;
677 stack = stack_new (); 722 stack = stack_new ();
678 PL_op = PL_op->op_next; 723 PL_op = PL_op->op_next;
679 } 724 }
680 725
681 return stack; 726 return stack;
682} 727}
683 728
684static void 729static void
685stack_put (coro_stack *stack) 730stack_put (coro_stack *stack)
686{ 731{
732 ++cctx_idle;
687 stack->next = stack_first; 733 stack->next = stack_first;
688 stack_first = stack; 734 stack_first = stack;
689} 735}
690 736
691/* never call directly, always through the coro_state_transfer global variable */ 737/* never call directly, always through the coro_state_transfer global variable */
692static void 738static void NOINLINE
693transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 739transfer (struct coro *prev, struct coro *next, int flags)
694{ 740{
695 dSTACKLEVEL; 741 dSTACKLEVEL;
696 742
697 /* sometimes transfer is only called to set idle_sp */ 743 /* sometimes transfer is only called to set idle_sp */
698 if (flags == TRANSFER_SET_STACKLEVEL) 744 if (flags == TRANSFER_SET_STACKLEVEL)
747 793
748 UNLOCK; 794 UNLOCK;
749 } 795 }
750} 796}
751 797
752/* use this function pointer to call the above function */
753/* this is done to increase chances of the compiler not inlining the call */
754/* not static to make it even harder for the compiler (and theoretically impossible in most cases */
755void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl;
756
757struct transfer_args 798struct transfer_args
758{ 799{
759 struct coro *prev, *next; 800 struct coro *prev, *next;
760 int flags; 801 int flags;
761}; 802};
762 803
763#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) 804#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
764 805
765static void 806static void
766coro_state_destroy (struct coro *coro) 807coro_state_destroy (struct coro *coro)
767{ 808{
768 if (coro->refcnt--) 809 if (coro->refcnt--)
770 811
771 if (coro->mainstack && coro->mainstack != main_mainstack) 812 if (coro->mainstack && coro->mainstack != main_mainstack)
772 { 813 {
773 struct coro temp; 814 struct coro temp;
774 815
775 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 816 SAVE ((&temp), TRANSFER_SAVE_ALL);
776 LOAD (aTHX_ coro); 817 LOAD (coro);
777 818
778 destroy_stacks (aTHX); 819 coro_destroy_stacks ();
779 820
780 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 821 LOAD ((&temp)); /* this will get rid of defsv etc.. */
781 822
782 coro->mainstack = 0; 823 coro->mainstack = 0;
783 } 824 }
786 SvREFCNT_dec (coro->args); 827 SvREFCNT_dec (coro->args);
787 Safefree (coro); 828 Safefree (coro);
788} 829}
789 830
790static int 831static int
791coro_state_clear (SV *sv, MAGIC *mg) 832coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
792{ 833{
793 struct coro *coro = (struct coro *)mg->mg_ptr; 834 struct coro *coro = (struct coro *)mg->mg_ptr;
794 mg->mg_ptr = 0; 835 mg->mg_ptr = 0;
795 836
796 coro_state_destroy (coro); 837 coro_state_destroy (coro);
797 838
798 return 0; 839 return 0;
799} 840}
800 841
801static int 842static int
802coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 843coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
803{ 844{
804 struct coro *coro = (struct coro *)mg->mg_ptr; 845 struct coro *coro = (struct coro *)mg->mg_ptr;
805 846
806 ++coro->refcnt; 847 ++coro->refcnt;
807 848
808 return 0; 849 return 0;
809} 850}
810 851
811static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 852static MGVTBL coro_state_vtbl = {
853 0, 0, 0, 0,
854 coro_state_clear,
855 0,
856#ifdef MGf_DUP
857 coro_state_dup,
858#else
859# define MGf_DUP 0
860#endif
861};
812 862
813static struct coro * 863static struct coro *
814SvSTATE (SV *coro) 864SvSTATE (SV *coro)
815{ 865{
816 HV *stash; 866 HV *stash;
831 assert (mg->mg_type == PERL_MAGIC_ext); 881 assert (mg->mg_type == PERL_MAGIC_ext);
832 return (struct coro *)mg->mg_ptr; 882 return (struct coro *)mg->mg_ptr;
833} 883}
834 884
835static void 885static void
836prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) 886prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
837{ 887{
838 ta->prev = SvSTATE (prev); 888 ta->prev = SvSTATE (prev);
839 ta->next = SvSTATE (next); 889 ta->next = SvSTATE (next);
840 ta->flags = flags; 890 ta->flags = flags;
841} 891}
844api_transfer (SV *prev, SV *next, int flags) 894api_transfer (SV *prev, SV *next, int flags)
845{ 895{
846 dTHX; 896 dTHX;
847 struct transfer_args ta; 897 struct transfer_args ta;
848 898
849 prepare_transfer (aTHX_ &ta, prev, next, flags); 899 prepare_transfer (&ta, prev, next, flags);
850 TRANSFER (ta); 900 TRANSFER (ta);
851} 901}
852 902
853/** Coro ********************************************************************/ 903/** Coro ********************************************************************/
854 904
863static GV *coro_current, *coro_idle; 913static GV *coro_current, *coro_idle;
864static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 914static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
865static int coro_nready; 915static int coro_nready;
866 916
867static void 917static void
868coro_enq (pTHX_ SV *sv) 918coro_enq (SV *sv)
869{ 919{
870 int prio; 920 int prio;
871 921
872 if (SvTYPE (sv) != SVt_PVHV) 922 if (SvTYPE (sv) != SVt_PVHV)
873 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 923 croak ("Coro::ready tried to enqueue something that is not a coroutine");
877 av_push (coro_ready [prio - PRIO_MIN], sv); 927 av_push (coro_ready [prio - PRIO_MIN], sv);
878 coro_nready++; 928 coro_nready++;
879} 929}
880 930
881static SV * 931static SV *
882coro_deq (pTHX_ int min_prio) 932coro_deq (int min_prio)
883{ 933{
884 int prio = PRIO_MAX - PRIO_MIN; 934 int prio = PRIO_MAX - PRIO_MIN;
885 935
886 min_prio -= PRIO_MIN; 936 min_prio -= PRIO_MIN;
887 if (min_prio < 0) 937 if (min_prio < 0)
904 954
905 if (SvROK (coro)) 955 if (SvROK (coro))
906 coro = SvRV (coro); 956 coro = SvRV (coro);
907 957
908 LOCK; 958 LOCK;
909 coro_enq (aTHX_ SvREFCNT_inc (coro)); 959 coro_enq (SvREFCNT_inc (coro));
910 UNLOCK; 960 UNLOCK;
911} 961}
912 962
913static void 963static void
914prepare_schedule (aTHX_ struct transfer_args *ta) 964prepare_schedule (struct transfer_args *ta)
915{ 965{
916 SV *current, *prev, *next; 966 SV *current, *prev, *next;
917
918 LOCK;
919 967
920 current = GvSV (coro_current); 968 current = GvSV (coro_current);
921 969
922 for (;;) 970 for (;;)
923 { 971 {
924 LOCK; 972 LOCK;
925
926 next = coro_deq (aTHX_ PRIO_MIN); 973 next = coro_deq (PRIO_MIN);
974 UNLOCK;
927 975
928 if (next) 976 if (next)
929 break; 977 break;
930
931 UNLOCK;
932 978
933 { 979 {
934 dSP; 980 dSP;
935 981
936 ENTER; 982 ENTER;
947 993
948 prev = SvRV (current); 994 prev = SvRV (current);
949 SvRV (current) = next; 995 SvRV (current) = next;
950 996
951 /* free this only after the transfer */ 997 /* free this only after the transfer */
998 LOCK;
952 free_coro_mortal (); 999 free_coro_mortal ();
1000 UNLOCK;
953 coro_mortal = prev; 1001 coro_mortal = prev;
954 1002
955 ta->prev = SvSTATE (prev); 1003 ta->prev = SvSTATE (prev);
956 ta->next = SvSTATE (next); 1004 ta->next = SvSTATE (next);
957 ta->flags = TRANSFER_SAVE_ALL; 1005 ta->flags = TRANSFER_SAVE_ALL;
958
959 UNLOCK;
960} 1006}
961 1007
962static void 1008static void
963prepare_cede (aTHX_ struct transfer_args *ta) 1009prepare_cede (struct transfer_args *ta)
964{ 1010{
965 LOCK; 1011 LOCK;
966 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); 1012 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
967 UNLOCK; 1013 UNLOCK;
968 1014
969 prepare_schedule (ta); 1015 prepare_schedule (ta);
970} 1016}
971 1017
1114 ++coro_src->refcnt; 1160 ++coro_src->refcnt;
1115 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1161 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1116} 1162}
1117 1163
1118void 1164void
1119_nonlocal_goto (IV nextop)
1120 CODE:
1121 /* uuh, somebody will kill me again for this */
1122 PL_op->op_next = INT2PTR (OP *, nextop);
1123
1124void
1125_exit (code) 1165_exit (code)
1126 int code 1166 int code
1127 PROTOTYPE: $ 1167 PROTOTYPE: $
1128 CODE: 1168 CODE:
1129 _exit (code); 1169 _exit (code);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines