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.100 by root, Mon Nov 27 00:07:49 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
65 * and should be unique. */ 69 * and should be unique. */
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)
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))
70 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
154 166
155typedef struct coro *Coro__State; 167typedef struct coro *Coro__State;
156typedef struct coro *Coro__State_or_hashref; 168typedef struct coro *Coro__State_or_hashref;
157 169
158static AV * 170static AV *
159coro_clone_padlist (pTHX_ CV *cv) 171coro_clone_padlist (CV *cv)
160{ 172{
161 AV *padlist = CvPADLIST (cv); 173 AV *padlist = CvPADLIST (cv);
162 AV *newpadlist, *newpad; 174 AV *newpadlist, *newpad;
163 175
164 newpadlist = newAV (); 176 newpadlist = newAV ();
165 AvREAL_off (newpadlist); 177 AvREAL_off (newpadlist);
166#if PERL_VERSION < 9 178#if PERL_VERSION_ATLEAST (5,9,0)
179 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
180#else
167 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 181 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
168#else
169 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
170#endif 182#endif
171 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 183 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
172 --AvFILLp (padlist); 184 --AvFILLp (padlist);
173 185
174 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 186 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
176 188
177 return newpadlist; 189 return newpadlist;
178} 190}
179 191
180static void 192static void
181free_padlist (pTHX_ AV *padlist) 193free_padlist (AV *padlist)
182{ 194{
183 /* may be during global destruction */ 195 /* may be during global destruction */
184 if (SvREFCNT (padlist)) 196 if (SvREFCNT (padlist))
185 { 197 {
186 I32 i = AvFILLp (padlist); 198 I32 i = AvFILLp (padlist);
207 AV *padlist; 219 AV *padlist;
208 AV *av = (AV *)mg->mg_obj; 220 AV *av = (AV *)mg->mg_obj;
209 221
210 /* casting is fun. */ 222 /* casting is fun. */
211 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 223 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
212 free_padlist (aTHX_ padlist); 224 free_padlist (padlist);
213 225
214 SvREFCNT_dec (av); 226 SvREFCNT_dec (av);
215 227
216 return 0; 228 return 0;
217} 229}
220 232
221static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 233static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
222 234
223/* the next two functions merely cache the padlists */ 235/* the next two functions merely cache the padlists */
224static void 236static void
225get_padlist (pTHX_ CV *cv) 237get_padlist (CV *cv)
226{ 238{
227 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 239 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
228 240
229 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 241 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
230 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 242 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
231 else 243 else
232 { 244 {
233#if 0 245#if 0
234 /* this is probably cleaner, but also slower? */ 246 /* this is probably cleaner, but also slower? */
235 CV *cp = Perl_cv_clone (aTHX_ cv); 247 CV *cp = Perl_cv_clone (cv);
236 CvPADLIST (cv) = CvPADLIST (cp); 248 CvPADLIST (cv) = CvPADLIST (cp);
237 CvPADLIST (cp) = 0; 249 CvPADLIST (cp) = 0;
238 SvREFCNT_dec (cp); 250 SvREFCNT_dec (cp);
239#else 251#else
240 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 252 CvPADLIST (cv) = coro_clone_padlist (cv);
241#endif 253#endif
242 } 254 }
243} 255}
244 256
245static void 257static void
246put_padlist (pTHX_ CV *cv) 258put_padlist (CV *cv)
247{ 259{
248 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 260 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
249 261
250 if (!mg) 262 if (!mg)
251 { 263 {
259} 271}
260 272
261#define SB do { 273#define SB do {
262#define SE } while (0) 274#define SE } while (0)
263 275
264#define LOAD(state) load_state(aTHX_ (state)); 276#define LOAD(state) load_state((state));
265#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); 277#define SAVE(state,flags) save_state((state),(flags));
266 278
267#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
268 280
269static void 281static void
270load_state(pTHX_ Coro__State c) 282load_state(Coro__State c)
271{ 283{
272 PL_dowarn = c->dowarn; 284 PL_dowarn = c->dowarn;
273 PL_in_eval = c->in_eval; 285 PL_in_eval = c->in_eval;
274 286
275 PL_curstackinfo = c->curstackinfo; 287 PL_curstackinfo = c->curstackinfo;
293 PL_scopestack_ix = c->scopestack_ix; 305 PL_scopestack_ix = c->scopestack_ix;
294 PL_scopestack_max = c->scopestack_max; 306 PL_scopestack_max = c->scopestack_max;
295 PL_savestack = c->savestack; 307 PL_savestack = c->savestack;
296 PL_savestack_ix = c->savestack_ix; 308 PL_savestack_ix = c->savestack_ix;
297 PL_savestack_max = c->savestack_max; 309 PL_savestack_max = c->savestack_max;
298#if PERL_VERSION < 9 310#if !PERL_VERSION_ATLEAST (5,9,0)
299 PL_retstack = c->retstack; 311 PL_retstack = c->retstack;
300 PL_retstack_ix = c->retstack_ix; 312 PL_retstack_ix = c->retstack_ix;
301 PL_retstack_max = c->retstack_max; 313 PL_retstack_max = c->retstack_max;
302#endif 314#endif
303 PL_curpm = c->curpm; 315 PL_curpm = c->curpm;
316 { 328 {
317 AV *padlist = (AV *)POPs; 329 AV *padlist = (AV *)POPs;
318 330
319 if (padlist) 331 if (padlist)
320 { 332 {
321 put_padlist (aTHX_ cv); /* mark this padlist as available */ 333 put_padlist (cv); /* mark this padlist as available */
322 CvPADLIST(cv) = padlist; 334 CvPADLIST(cv) = padlist;
323 } 335 }
324 336
325 ++CvDEPTH(cv); 337 ++CvDEPTH(cv);
326 } 338 }
328 PUTBACK; 340 PUTBACK;
329 } 341 }
330} 342}
331 343
332static void 344static void
333save_state(pTHX_ Coro__State c, int flags) 345save_state(Coro__State c, int flags)
334{ 346{
335 { 347 {
336 dSP; 348 dSP;
337 I32 cxix = cxstack_ix; 349 I32 cxix = cxstack_ix;
338 PERL_CONTEXT *ccstk = cxstack; 350 PERL_CONTEXT *ccstk = cxstack;
366 } 378 }
367 379
368 PUSHs ((SV *)CvPADLIST(cv)); 380 PUSHs ((SV *)CvPADLIST(cv));
369 PUSHs ((SV *)cv); 381 PUSHs ((SV *)cv);
370 382
371 get_padlist (aTHX_ cv); 383 get_padlist (cv);
372 } 384 }
373 } 385 }
374#ifdef CXt_FORMAT 386#ifdef CXt_FORMAT
375 else if (CxTYPE(cx) == CXt_FORMAT) 387 else if (CxTYPE(cx) == CXt_FORMAT)
376 { 388 {
420 c->scopestack_ix = PL_scopestack_ix; 432 c->scopestack_ix = PL_scopestack_ix;
421 c->scopestack_max = PL_scopestack_max; 433 c->scopestack_max = PL_scopestack_max;
422 c->savestack = PL_savestack; 434 c->savestack = PL_savestack;
423 c->savestack_ix = PL_savestack_ix; 435 c->savestack_ix = PL_savestack_ix;
424 c->savestack_max = PL_savestack_max; 436 c->savestack_max = PL_savestack_max;
425#if PERL_VERSION < 9 437#if !PERL_VERSION_ATLEAST (5,9,0)
426 c->retstack = PL_retstack; 438 c->retstack = PL_retstack;
427 c->retstack_ix = PL_retstack_ix; 439 c->retstack_ix = PL_retstack_ix;
428 c->retstack_max = PL_retstack_max; 440 c->retstack_max = PL_retstack_max;
429#endif 441#endif
430 c->curpm = PL_curpm; 442 c->curpm = PL_curpm;
436 * of perl.c:init_stacks, except that it uses less memory 448 * of perl.c:init_stacks, except that it uses less memory
437 * on the (sometimes correct) assumption that coroutines do 449 * on the (sometimes correct) assumption that coroutines do
438 * not usually need a lot of stackspace. 450 * not usually need a lot of stackspace.
439 */ 451 */
440static void 452static void
441coro_init_stacks (pTHX) 453coro_init_stacks ()
442{ 454{
443 LOCK;
444
445 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 455 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
446 PL_curstackinfo->si_type = PERLSI_MAIN; 456 PL_curstackinfo->si_type = PERLSI_MAIN;
447 PL_curstack = PL_curstackinfo->si_stack; 457 PL_curstack = PL_curstackinfo->si_stack;
448 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 458 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
449 459
470 480
471 New(54,PL_savestack,96,ANY); 481 New(54,PL_savestack,96,ANY);
472 PL_savestack_ix = 0; 482 PL_savestack_ix = 0;
473 PL_savestack_max = 96; 483 PL_savestack_max = 96;
474 484
475#if PERL_VERSION < 9 485#if !PERL_VERSION_ATLEAST (5,9,0)
476 New(54,PL_retstack,8,OP*); 486 New(54,PL_retstack,8,OP*);
477 PL_retstack_ix = 0; 487 PL_retstack_ix = 0;
478 PL_retstack_max = 8; 488 PL_retstack_max = 8;
479#endif 489#endif
480
481 UNLOCK;
482} 490}
483 491
484/* 492/*
485 * destroy the stacks, the callchain etc... 493 * destroy the stacks, the callchain etc...
486 */ 494 */
487static void 495static void
488destroy_stacks(pTHX) 496destroy_stacks()
489{ 497{
490 if (!IN_DESTRUCT) 498 if (!IN_DESTRUCT)
491 { 499 {
492 /* is this ugly, I ask? */ 500 /* is this ugly, I ask? */
493 LEAVE_SCOPE (0); 501 LEAVE_SCOPE (0);
524 532
525 Safefree (PL_tmps_stack); 533 Safefree (PL_tmps_stack);
526 Safefree (PL_markstack); 534 Safefree (PL_markstack);
527 Safefree (PL_scopestack); 535 Safefree (PL_scopestack);
528 Safefree (PL_savestack); 536 Safefree (PL_savestack);
529#if PERL_VERSION < 9 537#if !PERL_VERSION_ATLEAST (5,9,0)
530 Safefree (PL_retstack); 538 Safefree (PL_retstack);
531#endif 539#endif
532} 540}
533 541
534static void 542static void
540 dTHX; 548 dTHX;
541 dSP; 549 dSP;
542 UNOP myop; 550 UNOP myop;
543 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); 551 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
544 552
545 coro_init_stacks (aTHX); 553 coro_init_stacks ();
546 /*PL_curcop = 0;*/ 554 /*PL_curcop = 0;*/
547 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 555 /*PL_in_eval = PL_in_eval;*/ /* inherit */
548 SvREFCNT_dec (GvAV (PL_defgv)); 556 SvREFCNT_dec (GvAV (PL_defgv));
549 GvAV (PL_defgv) = coro->args; coro->args = 0; 557 GvAV (PL_defgv) = coro->args; coro->args = 0;
550 558
568static void 576static void
569free_coro_mortal () 577free_coro_mortal ()
570{ 578{
571 if (coro_mortal) 579 if (coro_mortal)
572 { 580 {
573 dTHX;
574
575 SvREFCNT_dec (coro_mortal); 581 SvREFCNT_dec (coro_mortal);
576 coro_mortal = 0; 582 coro_mortal = 0;
577 } 583 }
578} 584}
579 585
586static void NOINLINE
587prepare_cctx (coro_stack *cctx)
588{
589 dSP;
590 UNOP myop;
591
592 Zero (&myop, 1, UNOP);
593 myop.op_next = PL_op;
594 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
595
596 PUSHMARK(SP);
597 EXTEND (SP, 2);
598 PUSHs (newSViv (PTR2IV (cctx)));
599 PUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE));
600 PUTBACK;
601 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
602 SPAGAIN;
603}
604
580static void 605static void
581coro_run (void *arg) 606coro_run (void *arg)
582{ 607{
583 /* 608 /*
584 * this is a _very_ stripped down perl interpreter ;) 609 * this is a _very_ stripped down perl interpreter ;)
585 */ 610 */
586 dTHX;
587 int ret;
588
589 UNLOCK; 611 UNLOCK;
590 612
591 PL_top_env = &PL_start_env; 613 PL_top_env = &PL_start_env;
592 614 prepare_cctx ((coro_stack *)arg);
593 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((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 615
599 /* somebody will hit me for both perl_run and PL_restartop */ 616 /* somebody will hit me for both perl_run and PL_restartop */
600 ret = perl_run (aTHX_ PERL_GET_CONTEXT); 617 perl_run (PERL_GET_CONTEXT);
601 printf ("ret %d\n", ret);//D
602 618
603 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); 619 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr);
604 abort (); 620 abort ();
605} 621}
606 622
687 stack->next = stack_first; 703 stack->next = stack_first;
688 stack_first = stack; 704 stack_first = stack;
689} 705}
690 706
691/* never call directly, always through the coro_state_transfer global variable */ 707/* never call directly, always through the coro_state_transfer global variable */
692static void 708static void NOINLINE
693transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 709transfer (struct coro *prev, struct coro *next, int flags)
694{ 710{
695 dSTACKLEVEL; 711 dSTACKLEVEL;
696 712
697 /* sometimes transfer is only called to set idle_sp */ 713 /* sometimes transfer is only called to set idle_sp */
698 if (flags == TRANSFER_SET_STACKLEVEL) 714 if (flags == TRANSFER_SET_STACKLEVEL)
747 763
748 UNLOCK; 764 UNLOCK;
749 } 765 }
750} 766}
751 767
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 768struct transfer_args
758{ 769{
759 struct coro *prev, *next; 770 struct coro *prev, *next;
760 int flags; 771 int flags;
761}; 772};
762 773
763#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) 774#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
764 775
765static void 776static void
766coro_state_destroy (struct coro *coro) 777coro_state_destroy (struct coro *coro)
767{ 778{
768 if (coro->refcnt--) 779 if (coro->refcnt--)
770 781
771 if (coro->mainstack && coro->mainstack != main_mainstack) 782 if (coro->mainstack && coro->mainstack != main_mainstack)
772 { 783 {
773 struct coro temp; 784 struct coro temp;
774 785
775 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 786 SAVE ((&temp), TRANSFER_SAVE_ALL);
776 LOAD (aTHX_ coro); 787 LOAD (coro);
777 788
778 destroy_stacks (aTHX); 789 destroy_stacks ();
779 790
780 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 791 LOAD ((&temp)); /* this will get rid of defsv etc.. */
781 792
782 coro->mainstack = 0; 793 coro->mainstack = 0;
783 } 794 }
786 SvREFCNT_dec (coro->args); 797 SvREFCNT_dec (coro->args);
787 Safefree (coro); 798 Safefree (coro);
788} 799}
789 800
790static int 801static int
791coro_state_clear (SV *sv, MAGIC *mg) 802coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
792{ 803{
793 struct coro *coro = (struct coro *)mg->mg_ptr; 804 struct coro *coro = (struct coro *)mg->mg_ptr;
794 mg->mg_ptr = 0; 805 mg->mg_ptr = 0;
795 806
796 coro_state_destroy (coro); 807 coro_state_destroy (coro);
797 808
798 return 0; 809 return 0;
799} 810}
800 811
801static int 812static int
802coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 813coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
803{ 814{
804 struct coro *coro = (struct coro *)mg->mg_ptr; 815 struct coro *coro = (struct coro *)mg->mg_ptr;
805 816
806 ++coro->refcnt; 817 ++coro->refcnt;
807 818
808 return 0; 819 return 0;
809} 820}
810 821
811static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 822static MGVTBL coro_state_vtbl = {
823 0, 0, 0, 0,
824 coro_state_clear,
825 0,
826#ifdef MGf_DUP
827 coro_state_dup,
828#endif
829};
812 830
813static struct coro * 831static struct coro *
814SvSTATE (SV *coro) 832SvSTATE (SV *coro)
815{ 833{
816 HV *stash; 834 HV *stash;
831 assert (mg->mg_type == PERL_MAGIC_ext); 849 assert (mg->mg_type == PERL_MAGIC_ext);
832 return (struct coro *)mg->mg_ptr; 850 return (struct coro *)mg->mg_ptr;
833} 851}
834 852
835static void 853static void
836prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) 854prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
837{ 855{
838 ta->prev = SvSTATE (prev); 856 ta->prev = SvSTATE (prev);
839 ta->next = SvSTATE (next); 857 ta->next = SvSTATE (next);
840 ta->flags = flags; 858 ta->flags = flags;
841} 859}
844api_transfer (SV *prev, SV *next, int flags) 862api_transfer (SV *prev, SV *next, int flags)
845{ 863{
846 dTHX; 864 dTHX;
847 struct transfer_args ta; 865 struct transfer_args ta;
848 866
849 prepare_transfer (aTHX_ &ta, prev, next, flags); 867 prepare_transfer (&ta, prev, next, flags);
850 TRANSFER (ta); 868 TRANSFER (ta);
851} 869}
852 870
853/** Coro ********************************************************************/ 871/** Coro ********************************************************************/
854 872
863static GV *coro_current, *coro_idle; 881static GV *coro_current, *coro_idle;
864static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 882static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
865static int coro_nready; 883static int coro_nready;
866 884
867static void 885static void
868coro_enq (pTHX_ SV *sv) 886coro_enq (SV *sv)
869{ 887{
870 int prio; 888 int prio;
871 889
872 if (SvTYPE (sv) != SVt_PVHV) 890 if (SvTYPE (sv) != SVt_PVHV)
873 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 891 croak ("Coro::ready tried to enqueue something that is not a coroutine");
877 av_push (coro_ready [prio - PRIO_MIN], sv); 895 av_push (coro_ready [prio - PRIO_MIN], sv);
878 coro_nready++; 896 coro_nready++;
879} 897}
880 898
881static SV * 899static SV *
882coro_deq (pTHX_ int min_prio) 900coro_deq (int min_prio)
883{ 901{
884 int prio = PRIO_MAX - PRIO_MIN; 902 int prio = PRIO_MAX - PRIO_MIN;
885 903
886 min_prio -= PRIO_MIN; 904 min_prio -= PRIO_MIN;
887 if (min_prio < 0) 905 if (min_prio < 0)
904 922
905 if (SvROK (coro)) 923 if (SvROK (coro))
906 coro = SvRV (coro); 924 coro = SvRV (coro);
907 925
908 LOCK; 926 LOCK;
909 coro_enq (aTHX_ SvREFCNT_inc (coro)); 927 coro_enq (SvREFCNT_inc (coro));
910 UNLOCK; 928 UNLOCK;
911} 929}
912 930
913static void 931static void
914prepare_schedule (aTHX_ struct transfer_args *ta) 932prepare_schedule (struct transfer_args *ta)
915{ 933{
916 SV *current, *prev, *next; 934 SV *current, *prev, *next;
917
918 LOCK;
919 935
920 current = GvSV (coro_current); 936 current = GvSV (coro_current);
921 937
922 for (;;) 938 for (;;)
923 { 939 {
924 LOCK; 940 LOCK;
925
926 next = coro_deq (aTHX_ PRIO_MIN); 941 next = coro_deq (PRIO_MIN);
942 UNLOCK;
927 943
928 if (next) 944 if (next)
929 break; 945 break;
930
931 UNLOCK;
932 946
933 { 947 {
934 dSP; 948 dSP;
935 949
936 ENTER; 950 ENTER;
947 961
948 prev = SvRV (current); 962 prev = SvRV (current);
949 SvRV (current) = next; 963 SvRV (current) = next;
950 964
951 /* free this only after the transfer */ 965 /* free this only after the transfer */
966 LOCK;
952 free_coro_mortal (); 967 free_coro_mortal ();
968 UNLOCK;
953 coro_mortal = prev; 969 coro_mortal = prev;
954 970
955 ta->prev = SvSTATE (prev); 971 ta->prev = SvSTATE (prev);
956 ta->next = SvSTATE (next); 972 ta->next = SvSTATE (next);
957 ta->flags = TRANSFER_SAVE_ALL; 973 ta->flags = TRANSFER_SAVE_ALL;
958
959 UNLOCK;
960} 974}
961 975
962static void 976static void
963prepare_cede (aTHX_ struct transfer_args *ta) 977prepare_cede (struct transfer_args *ta)
964{ 978{
965 LOCK; 979 LOCK;
966 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); 980 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
967 UNLOCK; 981 UNLOCK;
968 982
969 prepare_schedule (ta); 983 prepare_schedule (ta);
970} 984}
971 985
1114 ++coro_src->refcnt; 1128 ++coro_src->refcnt;
1115 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1129 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1116} 1130}
1117 1131
1118void 1132void
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) 1133_exit (code)
1126 int code 1134 int code
1127 PROTOTYPE: $ 1135 PROTOTYPE: $
1128 CODE: 1136 CODE:
1129 _exit (code); 1137 _exit (code);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines