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.95 by root, Sun Nov 26 17:35:42 2006 UTC vs.
Revision 1.101 by root, Mon Nov 27 00:08:18 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,8,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 114
103/* this is a structure representing a perl-level coroutine */ 115/* this is a structure representing a perl-level coroutine */
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 should work - but it doesn't :( */ 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
573 SvREFCNT_dec (coro_mortal); 581 SvREFCNT_dec (coro_mortal);
574 coro_mortal = 0; 582 coro_mortal = 0;
575 } 583 }
576} 584}
577 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
578static void 605static void
579coro_run (void *arg) 606coro_run (void *arg)
580{ 607{
581 /* 608 /*
582 * this is a _very_ stripped down perl interpreter ;) 609 * this is a _very_ stripped down perl interpreter ;)
583 */ 610 */
584 dTHX;
585 int ret;
586
587 UNLOCK; 611 UNLOCK;
588 612
589 PL_top_env = &PL_start_env; 613 PL_top_env = &PL_start_env;
590 614 prepare_cctx ((coro_stack *)arg);
591 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg));
592 sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op));
593
594 /* continue at cctx_init, without entersub */
595 PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE));
596 615
597 /* somebody will hit me for both perl_run and PL_restartop */ 616 /* somebody will hit me for both perl_run and PL_restartop */
598 ret = perl_run (aTHX_ PERL_GET_CONTEXT); 617 perl_run (PERL_GET_CONTEXT);
599 printf ("ret %d\n", ret);//D
600 618
601 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);
602 abort (); 620 abort ();
603} 621}
604 622
685 stack->next = stack_first; 703 stack->next = stack_first;
686 stack_first = stack; 704 stack_first = stack;
687} 705}
688 706
689/* never call directly, always through the coro_state_transfer global variable */ 707/* never call directly, always through the coro_state_transfer global variable */
690static void 708static void NOINLINE
691transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 709transfer (struct coro *prev, struct coro *next, int flags)
692{ 710{
693 dSTACKLEVEL; 711 dSTACKLEVEL;
694 712
695 /* sometimes transfer is only called to set idle_sp */ 713 /* sometimes transfer is only called to set idle_sp */
696 if (flags == TRANSFER_SET_STACKLEVEL) 714 if (flags == TRANSFER_SET_STACKLEVEL)
745 763
746 UNLOCK; 764 UNLOCK;
747 } 765 }
748} 766}
749 767
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
755struct transfer_args 768struct transfer_args
756{ 769{
757 struct coro *prev, *next; 770 struct coro *prev, *next;
758 int flags; 771 int flags;
759}; 772};
760 773
761#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) 774#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
762 775
763static void 776static void
764coro_state_destroy (struct coro *coro) 777coro_state_destroy (struct coro *coro)
765{ 778{
766 if (coro->refcnt--) 779 if (coro->refcnt--)
768 781
769 if (coro->mainstack && coro->mainstack != main_mainstack) 782 if (coro->mainstack && coro->mainstack != main_mainstack)
770 { 783 {
771 struct coro temp; 784 struct coro temp;
772 785
773 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 786 SAVE ((&temp), TRANSFER_SAVE_ALL);
774 LOAD (aTHX_ coro); 787 LOAD (coro);
775 788
776 destroy_stacks (aTHX); 789 destroy_stacks ();
777 790
778 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 791 LOAD ((&temp)); /* this will get rid of defsv etc.. */
779 792
780 coro->mainstack = 0; 793 coro->mainstack = 0;
781 } 794 }
784 SvREFCNT_dec (coro->args); 797 SvREFCNT_dec (coro->args);
785 Safefree (coro); 798 Safefree (coro);
786} 799}
787 800
788static int 801static int
789coro_state_clear (SV *sv, MAGIC *mg) 802coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
790{ 803{
791 struct coro *coro = (struct coro *)mg->mg_ptr; 804 struct coro *coro = (struct coro *)mg->mg_ptr;
792 mg->mg_ptr = 0; 805 mg->mg_ptr = 0;
793 806
794 coro_state_destroy (coro); 807 coro_state_destroy (coro);
795 808
796 return 0; 809 return 0;
797} 810}
798 811
799static int 812static int
800coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 813coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
801{ 814{
802 struct coro *coro = (struct coro *)mg->mg_ptr; 815 struct coro *coro = (struct coro *)mg->mg_ptr;
803 816
804 ++coro->refcnt; 817 ++coro->refcnt;
805 818
806 return 0; 819 return 0;
807} 820}
808 821
809static 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};
810 830
811static struct coro * 831static struct coro *
812SvSTATE (SV *coro) 832SvSTATE (SV *coro)
813{ 833{
814 HV *stash; 834 HV *stash;
829 assert (mg->mg_type == PERL_MAGIC_ext); 849 assert (mg->mg_type == PERL_MAGIC_ext);
830 return (struct coro *)mg->mg_ptr; 850 return (struct coro *)mg->mg_ptr;
831} 851}
832 852
833static void 853static void
834prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) 854prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
835{ 855{
836 ta->prev = SvSTATE (prev); 856 ta->prev = SvSTATE (prev);
837 ta->next = SvSTATE (next); 857 ta->next = SvSTATE (next);
838 ta->flags = flags; 858 ta->flags = flags;
839} 859}
842api_transfer (SV *prev, SV *next, int flags) 862api_transfer (SV *prev, SV *next, int flags)
843{ 863{
844 dTHX; 864 dTHX;
845 struct transfer_args ta; 865 struct transfer_args ta;
846 866
847 prepare_transfer (aTHX_ &ta, prev, next, flags); 867 prepare_transfer (&ta, prev, next, flags);
848 TRANSFER (ta); 868 TRANSFER (ta);
849} 869}
850 870
851/** Coro ********************************************************************/ 871/** Coro ********************************************************************/
852 872
861static GV *coro_current, *coro_idle; 881static GV *coro_current, *coro_idle;
862static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 882static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
863static int coro_nready; 883static int coro_nready;
864 884
865static void 885static void
866coro_enq (pTHX_ SV *sv) 886coro_enq (SV *sv)
867{ 887{
868 int prio; 888 int prio;
869 889
870 if (SvTYPE (sv) != SVt_PVHV) 890 if (SvTYPE (sv) != SVt_PVHV)
871 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");
875 av_push (coro_ready [prio - PRIO_MIN], sv); 895 av_push (coro_ready [prio - PRIO_MIN], sv);
876 coro_nready++; 896 coro_nready++;
877} 897}
878 898
879static SV * 899static SV *
880coro_deq (pTHX_ int min_prio) 900coro_deq (int min_prio)
881{ 901{
882 int prio = PRIO_MAX - PRIO_MIN; 902 int prio = PRIO_MAX - PRIO_MIN;
883 903
884 min_prio -= PRIO_MIN; 904 min_prio -= PRIO_MIN;
885 if (min_prio < 0) 905 if (min_prio < 0)
902 922
903 if (SvROK (coro)) 923 if (SvROK (coro))
904 coro = SvRV (coro); 924 coro = SvRV (coro);
905 925
906 LOCK; 926 LOCK;
907 coro_enq (aTHX_ SvREFCNT_inc (coro)); 927 coro_enq (SvREFCNT_inc (coro));
908 UNLOCK; 928 UNLOCK;
909} 929}
910 930
911static void 931static void
912prepare_schedule (aTHX_ struct transfer_args *ta) 932prepare_schedule (struct transfer_args *ta)
913{ 933{
914 SV *current, *prev, *next; 934 SV *current, *prev, *next;
915
916 LOCK;
917 935
918 current = GvSV (coro_current); 936 current = GvSV (coro_current);
919 937
920 for (;;) 938 for (;;)
921 { 939 {
922 LOCK; 940 LOCK;
923
924 next = coro_deq (aTHX_ PRIO_MIN); 941 next = coro_deq (PRIO_MIN);
942 UNLOCK;
925 943
926 if (next) 944 if (next)
927 break; 945 break;
928
929 UNLOCK;
930 946
931 { 947 {
932 dSP; 948 dSP;
933 949
934 ENTER; 950 ENTER;
945 961
946 prev = SvRV (current); 962 prev = SvRV (current);
947 SvRV (current) = next; 963 SvRV (current) = next;
948 964
949 /* free this only after the transfer */ 965 /* free this only after the transfer */
966 LOCK;
950 free_coro_mortal (); 967 free_coro_mortal ();
968 UNLOCK;
951 coro_mortal = prev; 969 coro_mortal = prev;
952 970
953 ta->prev = SvSTATE (prev); 971 ta->prev = SvSTATE (prev);
954 ta->next = SvSTATE (next); 972 ta->next = SvSTATE (next);
955 ta->flags = TRANSFER_SAVE_ALL; 973 ta->flags = TRANSFER_SAVE_ALL;
956
957 UNLOCK;
958} 974}
959 975
960static void 976static void
961prepare_cede (aTHX_ struct transfer_args *ta) 977prepare_cede (struct transfer_args *ta)
962{ 978{
963 LOCK; 979 LOCK;
964 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); 980 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
965 UNLOCK; 981 UNLOCK;
966 982
967 prepare_schedule (ta); 983 prepare_schedule (ta);
968} 984}
969 985
1112 ++coro_src->refcnt; 1128 ++coro_src->refcnt;
1113 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;
1114} 1130}
1115 1131
1116void 1132void
1117_nonlocal_goto (IV nextop)
1118 CODE:
1119 /* uuh, somebody will kill me again for this */
1120 PL_op->op_next = INT2PTR (OP *, nextop);
1121
1122void
1123_exit (code) 1133_exit (code)
1124 int code 1134 int code
1125 PROTOTYPE: $ 1135 PROTOTYPE: $
1126 CODE: 1136 CODE:
1127 _exit (code); 1137 _exit (code);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines