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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines