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.87 by root, Fri Nov 24 13:40:36 2006 UTC vs.
Revision 1.98 by root, Sun Nov 26 21:25:53 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"
27# ifndef IS_PADCONST 25# ifndef IS_PADCONST
28# define IS_PADCONST(v) 0 26# define IS_PADCONST(v) 0
29# endif 27# endif
30#endif 28#endif
31 29
30#include <stdio.h>
32#include <errno.h> 31#include <errno.h>
33 32
34#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 33#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
35# undef STACKGUARD 34# undef STACKGUARD
36#endif 35#endif
57# else 56# else
58# define BOOT_PAGESIZE 57# define BOOT_PAGESIZE
59# endif 58# endif
60#endif 59#endif
61 60
62#define SUB_INIT "Coro::State::initialize"
63#define UCORO_STATE "_coro_state"
64
65/* The next macro should declare a variable stacklevel that contains and approximation 61/* The next macro should declare a variable stacklevel that contains and approximation
66 * to the current C stack pointer. Its property is that it changes with each call 62 * to the current C stack pointer. Its property is that it changes with each call
67 * and should be unique. */ 63 * and should be unique. */
64#define dSTACKLEVEL int stacklevel
68#define dSTACKLEVEL void *stacklevel = &stacklevel 65#define STACKLEVEL ((void *)&stacklevel)
69 66
70#define IN_DESTRUCT (PL_main_cv == Nullcv) 67#define IN_DESTRUCT (PL_main_cv == Nullcv)
71 68
72#define labs(l) ((l) >= 0 ? (l) : -(l))
73
74#include "CoroAPI.h" 69#include "CoroAPI.h"
70
71#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
75 72
76#ifdef USE_ITHREADS 73#ifdef USE_ITHREADS
77static perl_mutex coro_mutex; 74static perl_mutex coro_mutex;
78# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 75# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
79# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 76# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
82# define UNLOCK (void)0 79# define UNLOCK (void)0
83#endif 80#endif
84 81
85static struct CoroAPI coroapi; 82static struct CoroAPI coroapi;
86static AV *main_mainstack; /* used to differentiate between $main and others */ 83static AV *main_mainstack; /* used to differentiate between $main and others */
87static HV *coro_state_stash; 84static HV *coro_state_stash, *coro_stash;
88static SV *ucoro_state_sv;
89static U32 ucoro_state_hash;
90static SV *coro_mortal; /* will be freed after next transfer */ 85static SV *coro_mortal; /* will be freed after next transfer */
91 86
92/* this is actually not only the c stack but also c registers etc... */ 87/* this is a structure representing a c-level coroutine */
93typedef struct { 88typedef struct coro_stack {
94 int refcnt; /* pointer reference counter */ 89 struct coro_stack *next;
95 int usecnt; /* shared by how many coroutines */
96 int gencnt; /* generation counter */
97 90
98 coro_context cctx; 91 /* the stack */
99
100 void *sptr; 92 void *sptr;
101 long ssize; /* positive == mmap, otherwise malloc */ 93 long ssize; /* positive == mmap, otherwise malloc */
94
95 /* cpu state */
96 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
97 JMPENV *top_env;
98 coro_context cctx;
102} coro_stack; 99} coro_stack;
103 100
101/* this is a structure representing a perl-level coroutine */
104struct coro { 102struct coro {
105 /* the optional C context */ 103 /* the c coroutine allocated to this perl coroutine, if any */
106 coro_stack *stack; 104 coro_stack *stack;
107 void *cursp; 105
106 /* data associated with this coroutine (initial args) */
107 AV *args;
108 int gencnt; 108 int refcnt;
109 109
110 /* optionally saved, might be zero */ 110 /* optionally saved, might be zero */
111 AV *defav; 111 AV *defav;
112 SV *defsv; 112 SV *defsv;
113 SV *errsv; 113 SV *errsv;
143 OP **retstack; 143 OP **retstack;
144 I32 retstack_ix; 144 I32 retstack_ix;
145 I32 retstack_max; 145 I32 retstack_max;
146 PMOP *curpm; 146 PMOP *curpm;
147 COP *curcop; 147 COP *curcop;
148 JMPENV *top_env;
149 148
150 /* coro process data */ 149 /* coro process data */
151 int prio; 150 int prio;
152
153 /* data associated with this coroutine (initial args) */
154 AV *args;
155 int refcnt;
156}; 151};
157 152
158typedef struct coro *Coro__State; 153typedef struct coro *Coro__State;
159typedef struct coro *Coro__State_or_hashref; 154typedef struct coro *Coro__State_or_hashref;
160 155
161static AV * 156static AV *
162coro_clone_padlist (pTHX_ CV *cv) 157coro_clone_padlist (CV *cv)
163{ 158{
164 AV *padlist = CvPADLIST (cv); 159 AV *padlist = CvPADLIST (cv);
165 AV *newpadlist, *newpad; 160 AV *newpadlist, *newpad;
166 161
167 newpadlist = newAV (); 162 newpadlist = newAV ();
179 174
180 return newpadlist; 175 return newpadlist;
181} 176}
182 177
183static void 178static void
184free_padlist (pTHX_ AV *padlist) 179free_padlist (AV *padlist)
185{ 180{
186 /* may be during global destruction */ 181 /* may be during global destruction */
187 if (SvREFCNT (padlist)) 182 if (SvREFCNT (padlist))
188 { 183 {
189 I32 i = AvFILLp (padlist); 184 I32 i = AvFILLp (padlist);
210 AV *padlist; 205 AV *padlist;
211 AV *av = (AV *)mg->mg_obj; 206 AV *av = (AV *)mg->mg_obj;
212 207
213 /* casting is fun. */ 208 /* casting is fun. */
214 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 209 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
215 free_padlist (aTHX_ padlist); 210 free_padlist (padlist);
216 211
217 SvREFCNT_dec (av); 212 SvREFCNT_dec (av);
218 213
219 return 0; 214 return 0;
220} 215}
223 218
224static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 219static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
225 220
226/* the next two functions merely cache the padlists */ 221/* the next two functions merely cache the padlists */
227static void 222static void
228get_padlist (pTHX_ CV *cv) 223get_padlist (CV *cv)
229{ 224{
230 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 225 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
231 226
232 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 227 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
233 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 228 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
234 else 229 else
235 { 230 {
236#if 0 231#if 0
237 /* this should work - but it doesn't :( */ 232 /* this is probably cleaner, but also slower? */
238 CV *cp = Perl_cv_clone (aTHX_ cv); 233 CV *cp = Perl_cv_clone (cv);
239 CvPADLIST (cv) = CvPADLIST (cp); 234 CvPADLIST (cv) = CvPADLIST (cp);
240 CvPADLIST (cp) = 0; 235 CvPADLIST (cp) = 0;
241 SvREFCNT_dec (cp); 236 SvREFCNT_dec (cp);
242#else 237#else
243 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 238 CvPADLIST (cv) = coro_clone_padlist (cv);
244#endif 239#endif
245 } 240 }
246} 241}
247 242
248static void 243static void
249put_padlist (pTHX_ CV *cv) 244put_padlist (CV *cv)
250{ 245{
251 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 246 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
252 247
253 if (!mg) 248 if (!mg)
254 { 249 {
262} 257}
263 258
264#define SB do { 259#define SB do {
265#define SE } while (0) 260#define SE } while (0)
266 261
267#define LOAD(state) load_state(aTHX_ (state)); 262#define LOAD(state) load_state((state));
268#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); 263#define SAVE(state,flags) save_state((state),(flags));
269 264
270#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 265#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
271 266
272static void 267static void
273load_state(pTHX_ Coro__State c) 268load_state(Coro__State c)
274{ 269{
275 PL_dowarn = c->dowarn; 270 PL_dowarn = c->dowarn;
276 PL_in_eval = c->in_eval; 271 PL_in_eval = c->in_eval;
277 272
278 PL_curstackinfo = c->curstackinfo; 273 PL_curstackinfo = c->curstackinfo;
303 PL_retstack_ix = c->retstack_ix; 298 PL_retstack_ix = c->retstack_ix;
304 PL_retstack_max = c->retstack_max; 299 PL_retstack_max = c->retstack_max;
305#endif 300#endif
306 PL_curpm = c->curpm; 301 PL_curpm = c->curpm;
307 PL_curcop = c->curcop; 302 PL_curcop = c->curcop;
308 PL_top_env = c->top_env;
309 303
310 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 304 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
311 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 305 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
312 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 306 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
313 307
320 { 314 {
321 AV *padlist = (AV *)POPs; 315 AV *padlist = (AV *)POPs;
322 316
323 if (padlist) 317 if (padlist)
324 { 318 {
325 put_padlist (aTHX_ cv); /* mark this padlist as available */ 319 put_padlist (cv); /* mark this padlist as available */
326 CvPADLIST(cv) = padlist; 320 CvPADLIST(cv) = padlist;
327 } 321 }
328 322
329 ++CvDEPTH(cv); 323 ++CvDEPTH(cv);
330 } 324 }
332 PUTBACK; 326 PUTBACK;
333 } 327 }
334} 328}
335 329
336static void 330static void
337save_state(pTHX_ Coro__State c, int flags) 331save_state(Coro__State c, int flags)
338{ 332{
339 { 333 {
340 dSP; 334 dSP;
341 I32 cxix = cxstack_ix; 335 I32 cxix = cxstack_ix;
342 PERL_CONTEXT *ccstk = cxstack; 336 PERL_CONTEXT *ccstk = cxstack;
370 } 364 }
371 365
372 PUSHs ((SV *)CvPADLIST(cv)); 366 PUSHs ((SV *)CvPADLIST(cv));
373 PUSHs ((SV *)cv); 367 PUSHs ((SV *)cv);
374 368
375 get_padlist (aTHX_ cv); 369 get_padlist (cv);
376 } 370 }
377 } 371 }
378#ifdef CXt_FORMAT 372#ifdef CXt_FORMAT
379 else if (CxTYPE(cx) == CXt_FORMAT) 373 else if (CxTYPE(cx) == CXt_FORMAT)
380 { 374 {
431 c->retstack_ix = PL_retstack_ix; 425 c->retstack_ix = PL_retstack_ix;
432 c->retstack_max = PL_retstack_max; 426 c->retstack_max = PL_retstack_max;
433#endif 427#endif
434 c->curpm = PL_curpm; 428 c->curpm = PL_curpm;
435 c->curcop = PL_curcop; 429 c->curcop = PL_curcop;
436 c->top_env = PL_top_env;
437} 430}
438 431
439/* 432/*
440 * allocate various perl stacks. This is an exact copy 433 * allocate various perl stacks. This is an exact copy
441 * of perl.c:init_stacks, except that it uses less memory 434 * of perl.c:init_stacks, except that it uses less memory
442 * on the (sometimes correct) assumption that coroutines do 435 * on the (sometimes correct) assumption that coroutines do
443 * not usually need a lot of stackspace. 436 * not usually need a lot of stackspace.
444 */ 437 */
445static void 438static void
446coro_init_stacks (pTHX) 439coro_init_stacks ()
447{ 440{
448 LOCK; 441 LOCK;
449 442
450 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 443 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
451 PL_curstackinfo->si_type = PERLSI_MAIN; 444 PL_curstackinfo->si_type = PERLSI_MAIN;
488 481
489/* 482/*
490 * destroy the stacks, the callchain etc... 483 * destroy the stacks, the callchain etc...
491 */ 484 */
492static void 485static void
493destroy_stacks(pTHX) 486destroy_stacks()
494{ 487{
495 if (!IN_DESTRUCT) 488 if (!IN_DESTRUCT)
496 { 489 {
497 /* is this ugly, I ask? */ 490 /* is this ugly, I ask? */
498 LEAVE_SCOPE (0); 491 LEAVE_SCOPE (0);
535 Safefree (PL_retstack); 528 Safefree (PL_retstack);
536#endif 529#endif
537} 530}
538 531
539static void 532static void
540allocate_stack (Coro__State ctx, int alloc) 533setup_coro (struct coro *coro)
541{
542 coro_stack *stack;
543
544 New (0, stack, 1, coro_stack);
545
546 stack->refcnt = 1;
547 stack->usecnt = 1;
548 stack->gencnt = ctx->gencnt = 0;
549
550 if (alloc)
551 {
552#if HAVE_MMAP
553 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */
554 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
555 if (stack->sptr != (void *)-1)
556 {
557# if STACKGUARD
558 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
559# endif
560 }
561 else
562#endif
563 {
564 stack->ssize = - (STACKSIZE * (long)sizeof (long));
565 New (0, stack->sptr, STACKSIZE, long);
566 }
567 }
568 else
569 stack->sptr = 0;
570
571 ctx->stack = stack;
572}
573
574static void
575deallocate_stack (Coro__State ctx)
576{
577 coro_stack *stack = ctx->stack;
578
579 ctx->stack = 0;
580
581 if (stack)
582 {
583 if (!--stack->refcnt)
584 {
585#ifdef HAVE_MMAP
586 if (stack->ssize > 0 && stack->sptr)
587 munmap (stack->sptr, stack->ssize);
588 else
589#endif
590 Safefree (stack->sptr);
591
592 Safefree (stack);
593 }
594 else if (ctx->gencnt == stack->gencnt)
595 --stack->usecnt;
596 }
597}
598
599static void
600setup_coro (void *arg)
601{ 534{
602 /* 535 /*
603 * emulate part of the perl startup here. 536 * emulate part of the perl startup here.
604 */ 537 */
605 dTHX; 538 dTHX;
606 dSP; 539 dSP;
607 Coro__State ctx = (Coro__State)arg; 540 UNOP myop;
608 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); 541 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
609 542
610 coro_init_stacks (aTHX); 543 coro_init_stacks ();
611 /*PL_curcop = 0;*/ 544 /*PL_curcop = 0;*/
612 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 545 /*PL_in_eval = PL_in_eval;*/ /* inherit */
613 SvREFCNT_dec (GvAV (PL_defgv)); 546 SvREFCNT_dec (GvAV (PL_defgv));
614 GvAV (PL_defgv) = ctx->args; ctx->args = 0; 547 GvAV (PL_defgv) = coro->args; coro->args = 0;
615 548
616 SPAGAIN; 549 SPAGAIN;
617 550
618 if (ctx->stack)
619 {
620 ctx->cursp = 0;
621
622 PUSHMARK(SP);
623 PUTBACK;
624 (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL);
625
626 if (SvTRUE (ERRSV))
627 croak (NULL);
628 else
629 croak ("FATAL: CCTXT coroutine returned!");
630 }
631 else
632 {
633 UNOP myop;
634
635 Zero(&myop, 1, UNOP); 551 Zero (&myop, 1, UNOP);
636 myop.op_next = Nullop; 552 myop.op_next = Nullop;
637 myop.op_flags = OPf_WANT_VOID; 553 myop.op_flags = OPf_WANT_VOID;
638 554
639 PL_op = (OP *)&myop; 555 PL_op = (OP *)&myop;
640 556
641 PUSHMARK(SP); 557 PUSHMARK(SP);
642 XPUSHs (sub_init); 558 XPUSHs (sub_init);
643 /*
644 * the next line is slightly wrong, as PL_op->op_next
645 * is actually being executed so we skip the first op.
646 * that doesn't matter, though, since it is only
647 * pp_nextstate and we never return...
648 * ah yes, and I don't care anyways ;)
649 */
650 PUTBACK; 559 PUTBACK;
651 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 560 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
652 SPAGAIN; 561 SPAGAIN;
653 562
654 ENTER; /* necessary e.g. for dounwind */ 563 ENTER; /* necessary e.g. for dounwind */
655 }
656} 564}
657 565
658static void 566static void
659continue_coro (void *arg) 567free_coro_mortal ()
660{ 568{
661 /*
662 * this is a _very_ stripped down perl interpreter ;)
663 */
664 dTHX;
665 Coro__State ctx = (Coro__State)arg;
666
667 PL_top_env = &PL_start_env;
668
669 ctx->cursp = 0;
670 PL_op = PL_op->op_next;
671 CALLRUNOPS(aTHX);
672
673 abort ();
674}
675
676/* never call directly, always through the coro_state_transfer global variable */
677static void
678transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags)
679{
680 dSTACKLEVEL;
681
682 if (prev != next)
683 {
684 /* has this coro been created yet? */
685 if (next->mainstack)
686 {
687 LOCK;
688 SAVE (prev, flags);
689 LOAD (next);
690 UNLOCK;
691
692 /* mark this state as in-use */
693 next->mainstack = 0;
694 next->tmps_ix = -2;
695
696 /* stacklevel changed? if yes, grab the stack for us! */
697 if (flags & TRANSFER_SAVE_CCTXT)
698 {
699 if (!prev->stack)
700 allocate_stack (prev, 0);
701 else if (prev->cursp != stacklevel
702 && prev->stack->usecnt > 1)
703 {
704 prev->gencnt = ++prev->stack->gencnt;
705 prev->stack->usecnt = 1;
706 }
707
708 /* has our stack been invalidated? */
709 if (next->stack && next->stack->gencnt != next->gencnt)
710 {
711 deallocate_stack (next);
712 allocate_stack (next, 1);
713 coro_create (&(next->stack->cctx),
714 continue_coro, (void *)next,
715 next->stack->sptr, labs (next->stack->ssize));
716 }
717
718 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
719 prev->cursp = stacklevel;
720 /* don't add any code here */
721 }
722 else
723 next->cursp = stacklevel;
724 }
725 else if (next->tmps_ix == -2)
726 croak ("tried to transfer to running coroutine");
727 else
728 {
729 LOCK;
730 SAVE (prev, -1); /* first get rid of the old state */
731 UNLOCK;
732
733 /* create the coroutine for the first time */
734 if (flags & TRANSFER_SAVE_CCTXT)
735 {
736 if (!prev->stack)
737 allocate_stack (prev, 0);
738
739 /* the new coroutine starts with start_env again */
740 PL_top_env = &PL_start_env;
741
742 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
743 {
744 setup_coro (next);
745 next->cursp = stacklevel;
746
747 prev->stack->refcnt++;
748 prev->stack->usecnt++;
749 next->stack = prev->stack;
750 next->gencnt = prev->gencnt;
751 }
752 else
753 {
754 assert (!next->stack);
755 allocate_stack (next, 1);
756 coro_create (&(next->stack->cctx),
757 setup_coro, (void *)next,
758 next->stack->sptr, labs (next->stack->ssize));
759 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
760 prev->cursp = stacklevel;
761 /* don't add any code here */
762 }
763 }
764 else
765 {
766 setup_coro (next);
767 next->cursp = stacklevel;
768 }
769 }
770 }
771
772 LOCK;
773 if (coro_mortal) 569 if (coro_mortal)
774 { 570 {
775 SvREFCNT_dec (coro_mortal); 571 SvREFCNT_dec (coro_mortal);
776 coro_mortal = 0; 572 coro_mortal = 0;
777 } 573 }
574}
575
576static void
577coro_run (void *arg)
578{
579 /*
580 * this is a _very_ stripped down perl interpreter ;)
581 */
582 dTHX;
583 int ret;
584
778 UNLOCK; 585 UNLOCK;
586
587 PL_top_env = &PL_start_env;
588
589 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg));
590 sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op));
591
592 /* continue at cctx_init, without entersub */
593 PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE));
594
595 /* somebody will hit me for both perl_run and PL_restartop */
596 ret = perl_run (PERL_GET_CONTEXT);
597 printf ("ret %d\n", ret);//D
598
599 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr);
600 abort ();
601}
602
603static coro_stack *
604stack_new ()
605{
606 coro_stack *stack;
607
608 New (0, stack, 1, coro_stack);
609
610#if HAVE_MMAP
611
612 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
613 /* mmap suppsedly does allocate-on-write for us */
614 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
615
616 if (stack->sptr == (void *)-1)
617 {
618 perror ("FATAL: unable to mmap stack for coroutine");
619 _exit (EXIT_FAILURE);
620 }
621
622# if STACKGUARD
623 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
624# endif
625
626#else
627
628 stack->ssize = STACKSIZE * (long)sizeof (long);
629 New (0, stack->sptr, STACKSIZE, long);
630
631 if (!stack->sptr)
632 {
633 perror (stderr, "FATAL: unable to malloc stack for coroutine");
634 _exit (EXIT_FAILURE);
635 }
636
637#endif
638
639 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize);
640
641 return stack;
642}
643
644static void
645stack_free (coro_stack *stack)
646{
647 if (!stack)
648 return;
649
650#if HAVE_MMAP
651 munmap (stack->sptr, stack->ssize);
652#else
653 Safefree (stack->sptr);
654#endif
655
656 Safefree (stack);
657}
658
659static coro_stack *stack_first;
660
661static coro_stack *
662stack_get ()
663{
664 coro_stack *stack;
665
666 if (stack_first)
667 {
668 stack = stack_first;
669 stack_first = stack->next;
670 }
671 else
672 {
673 stack = stack_new ();
674 PL_op = PL_op->op_next;
675 }
676
677 return stack;
678}
679
680static void
681stack_put (coro_stack *stack)
682{
683 stack->next = stack_first;
684 stack_first = stack;
685}
686
687/* never call directly, always through the coro_state_transfer global variable */
688static void
689transfer_impl (struct coro *prev, struct coro *next, int flags)
690{
691 dSTACKLEVEL;
692
693 /* sometimes transfer is only called to set idle_sp */
694 if (flags == TRANSFER_SET_STACKLEVEL)
695 ((coro_stack *)prev)->idle_sp = STACKLEVEL;
696 else if (prev != next)
697 {
698 coro_stack *prev__stack;
699
700 LOCK;
701
702 if (next->mainstack)
703 {
704 /* coroutine already started */
705 SAVE (prev, flags);
706 LOAD (next);
707 }
708 else
709 {
710 /* need to start coroutine */
711 /* first get rid of the old state */
712 SAVE (prev, -1);
713 /* setup coroutine call */
714 setup_coro (next);
715 /* need a stack */
716 next->stack = 0;
717 }
718
719 if (!prev->stack)
720 /* create a new empty context */
721 Newz (0, prev->stack, 1, coro_stack);
722
723 prev__stack = prev->stack;
724
725 /* possibly "free" the stack */
726 if (prev__stack->idle_sp == STACKLEVEL)
727 {
728 stack_put (prev__stack);
729 prev->stack = 0;
730 }
731
732 if (!next->stack)
733 next->stack = stack_get ();
734
735 if (prev__stack != next->stack)
736 {
737 prev__stack->top_env = PL_top_env;
738 PL_top_env = next->stack->top_env;
739 coro_transfer (&prev__stack->cctx, &next->stack->cctx);
740 }
741
742 free_coro_mortal ();
743
744 UNLOCK;
745 }
779} 746}
780 747
781/* use this function pointer to call the above function */ 748/* use this function pointer to call the above function */
782/* this is done to increase chances of the compiler not inlining the call */ 749/* this is done to increase chances of the compiler not inlining the call */
750/* not static to make it even harder for the compiler (and theoretically impossible in most cases */
783void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; 751void (*coro_state_transfer)(struct coro *prev, struct coro *next, int flags) = transfer_impl;
784 752
785#define SV_CORO(sv,func) \ 753struct transfer_args
786 do { \ 754{
787 if (SvROK (sv)) \ 755 struct coro *prev, *next;
788 sv = SvRV (sv); \ 756 int flags;
789 \ 757};
790 if (SvTYPE (sv) == SVt_PVHV && SvSTASH (sv) != coro_state_stash) \ 758
791 { \ 759#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags)
792 HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
793 \
794 if (!he) \
795 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
796 \
797 (sv) = SvRV (HeVAL(he)); \
798 } \
799 \
800 /* must also be changed inside Coro::Cont::yield */ \
801 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
802 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
803 \
804 } while(0)
805 760
806static void 761static void
807coro_state_destroy (struct coro *coro) 762coro_state_destroy (struct coro *coro)
808{ 763{
809 if (coro->refcnt--) 764 if (coro->refcnt--)
811 766
812 if (coro->mainstack && coro->mainstack != main_mainstack) 767 if (coro->mainstack && coro->mainstack != main_mainstack)
813 { 768 {
814 struct coro temp; 769 struct coro temp;
815 770
816 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 771 SAVE ((&temp), TRANSFER_SAVE_ALL);
817 LOAD (aTHX_ coro); 772 LOAD (coro);
818 773
819 destroy_stacks (aTHX); 774 destroy_stacks ();
820 775
821 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 776 LOAD ((&temp)); /* this will get rid of defsv etc.. */
822 777
823 coro->mainstack = 0; 778 coro->mainstack = 0;
824 } 779 }
825 780
826 deallocate_stack (coro); 781 stack_free (coro->stack);
827 SvREFCNT_dec (coro->args); 782 SvREFCNT_dec (coro->args);
828 Safefree (coro); 783 Safefree (coro);
829} 784}
830 785
831static int 786static int
832coro_state_clear (SV *sv, MAGIC *mg) 787coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
833{ 788{
834 struct coro *coro = (struct coro *)mg->mg_ptr; 789 struct coro *coro = (struct coro *)mg->mg_ptr;
835 mg->mg_ptr = 0; 790 mg->mg_ptr = 0;
836 791
837 coro_state_destroy (coro); 792 coro_state_destroy (coro);
838 793
839 return 0; 794 return 0;
840} 795}
841 796
842static int 797static int
843coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 798coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
844{ 799{
845 struct coro *coro = (struct coro *)mg->mg_ptr; 800 struct coro *coro = (struct coro *)mg->mg_ptr;
846 801
847 ++coro->refcnt; 802 ++coro->refcnt;
848 803
852static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 807static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 };
853 808
854static struct coro * 809static struct coro *
855SvSTATE (SV *coro) 810SvSTATE (SV *coro)
856{ 811{
857 MAGIC *mg = SvMAGIC (SvROK (coro) ? SvRV (coro) : coro); 812 HV *stash;
813 MAGIC *mg;
814
815 if (SvROK (coro))
816 coro = SvRV (coro);
817
818 stash = SvSTASH (coro);
819 if (stash != coro_stash && stash != coro_state_stash)
820 {
821 /* very slow, but rare, check */
822 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
823 croak ("Coro::State object required");
824 }
825
826 mg = SvMAGIC (coro);
858 assert (mg->mg_type == PERL_MAGIC_ext); 827 assert (mg->mg_type == PERL_MAGIC_ext);
859 return (struct coro *)mg->mg_ptr; 828 return (struct coro *)mg->mg_ptr;
860} 829}
861 830
862static void 831static void
832prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
833{
834 ta->prev = SvSTATE (prev);
835 ta->next = SvSTATE (next);
836 ta->flags = flags;
837}
838
839static void
863api_transfer (pTHX_ SV *prev, SV *next, int flags) 840api_transfer (SV *prev, SV *next, int flags)
864{ 841{
865 SV_CORO (prev, "Coro::transfer"); 842 dTHX;
866 SV_CORO (next, "Coro::transfer"); 843 struct transfer_args ta;
867 844
868 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 845 prepare_transfer (&ta, prev, next, flags);
846 TRANSFER (ta);
869} 847}
870 848
871/** Coro ********************************************************************/ 849/** Coro ********************************************************************/
872 850
873#define PRIO_MAX 3 851#define PRIO_MAX 3
881static GV *coro_current, *coro_idle; 859static GV *coro_current, *coro_idle;
882static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 860static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
883static int coro_nready; 861static int coro_nready;
884 862
885static void 863static void
886coro_enq (pTHX_ SV *sv) 864coro_enq (SV *sv)
887{ 865{
888 int prio; 866 int prio;
889 867
890 if (SvTYPE (sv) != SVt_PVHV) 868 if (SvTYPE (sv) != SVt_PVHV)
891 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 869 croak ("Coro::ready tried to enqueue something that is not a coroutine");
892 870
893 {
894 SV *coro = sv;
895 SV_CORO (coro, "omg");
896 prio = SvSTATE (coro)->prio; 871 prio = SvSTATE (sv)->prio;
897 }
898 872
899 av_push (coro_ready [prio - PRIO_MIN], sv); 873 av_push (coro_ready [prio - PRIO_MIN], sv);
900 coro_nready++; 874 coro_nready++;
901} 875}
902 876
903static SV * 877static SV *
904coro_deq (pTHX_ int min_prio) 878coro_deq (int min_prio)
905{ 879{
906 int prio = PRIO_MAX - PRIO_MIN; 880 int prio = PRIO_MAX - PRIO_MIN;
907 881
908 min_prio -= PRIO_MIN; 882 min_prio -= PRIO_MIN;
909 if (min_prio < 0) 883 if (min_prio < 0)
926 900
927 if (SvROK (coro)) 901 if (SvROK (coro))
928 coro = SvRV (coro); 902 coro = SvRV (coro);
929 903
930 LOCK; 904 LOCK;
931 coro_enq (aTHX_ SvREFCNT_inc (coro)); 905 coro_enq (SvREFCNT_inc (coro));
932 UNLOCK; 906 UNLOCK;
933} 907}
934 908
935static void 909static void
910prepare_schedule (struct transfer_args *ta)
911{
912 SV *current, *prev, *next;
913
914 LOCK;
915
916 current = GvSV (coro_current);
917
918 for (;;)
919 {
920 LOCK;
921
922 next = coro_deq (PRIO_MIN);
923
924 if (next)
925 break;
926
927 UNLOCK;
928
929 {
930 dSP;
931
932 ENTER;
933 SAVETMPS;
934
935 PUSHMARK (SP);
936 PUTBACK;
937 call_sv (GvSV (coro_idle), G_DISCARD);
938
939 FREETMPS;
940 LEAVE;
941 }
942 }
943
944 prev = SvRV (current);
945 SvRV (current) = next;
946
947 /* free this only after the transfer */
948 free_coro_mortal ();
949 coro_mortal = prev;
950
951 ta->prev = SvSTATE (prev);
952 ta->next = SvSTATE (next);
953 ta->flags = TRANSFER_SAVE_ALL;
954
955 UNLOCK;
956}
957
958static void
959prepare_cede (struct transfer_args *ta)
960{
961 LOCK;
962 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
963 UNLOCK;
964
965 prepare_schedule (ta);
966}
967
968static void
936api_schedule (void) 969api_schedule (void)
937{ 970{
938 dTHX; 971 dTHX;
972 struct transfer_args ta;
939 973
940 SV *prev, *next; 974 prepare_schedule (&ta);
941 975 TRANSFER (ta);
942 LOCK;
943
944 prev = SvRV (GvSV (coro_current));
945 next = coro_deq (aTHX_ PRIO_MIN);
946
947 if (!next)
948 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
949
950 /* free this only after the transfer */
951 coro_mortal = prev;
952 SV_CORO (prev, "Coro::schedule");
953
954 SvRV (GvSV (coro_current)) = next;
955
956 SV_CORO (next, "Coro::schedule");
957
958 UNLOCK;
959
960 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
961 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
962} 976}
963 977
964static void 978static void
965api_cede (void) 979api_cede (void)
966{ 980{
967 dTHX; 981 dTHX;
982 struct transfer_args ta;
968 983
969 LOCK; 984 prepare_cede (&ta);
970 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); 985 TRANSFER (ta);
971 UNLOCK;
972
973 api_schedule ();
974} 986}
975 987
976MODULE = Coro::State PACKAGE = Coro::State 988MODULE = Coro::State PACKAGE = Coro::State
977 989
978PROTOTYPES: DISABLE 990PROTOTYPES: DISABLE
979 991
980BOOT: 992BOOT:
981{ /* {} necessary for stoopid perl-5.6.x */ 993{
982#ifdef USE_ITHREADS 994#ifdef USE_ITHREADS
983 MUTEX_INIT (&coro_mutex); 995 MUTEX_INIT (&coro_mutex);
984#endif 996#endif
985 BOOT_PAGESIZE; 997 BOOT_PAGESIZE;
986 998
987 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
988 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
989 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 999 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
990 1000
991 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1001 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
992 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1002 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
993 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 1003 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
994 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
995 1004
996 main_mainstack = PL_mainstack; 1005 main_mainstack = PL_mainstack;
997 1006
998 coroapi.ver = CORO_API_VERSION; 1007 coroapi.ver = CORO_API_VERSION;
999 coroapi.transfer = api_transfer; 1008 coroapi.transfer = api_transfer;
1010 int i; 1019 int i;
1011 1020
1012 Newz (0, coro, 1, struct coro); 1021 Newz (0, coro, 1, struct coro);
1013 coro->args = newAV (); 1022 coro->args = newAV ();
1014 1023
1015 for (i = 1; i < items; i++)
1016 av_push (coro->args, newSVsv (ST (i)));
1017
1018 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1019 /*coro->stack = 0;*/
1020
1021 hv = newHV (); 1024 hv = newHV ();
1022 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1025 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1023 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1026 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1027
1028 for (i = 1; i < items; i++)
1029 av_push (coro->args, newSVsv (ST (i)));
1030
1031 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1032 /*coro->stack = 0;*/
1024} 1033}
1025 OUTPUT: 1034 OUTPUT:
1026 RETVAL 1035 RETVAL
1027 1036
1028void 1037void
1029transfer(prev, next, flags) 1038_set_stacklevel (...)
1030 SV *prev 1039 ALIAS:
1031 SV *next 1040 Coro::State::transfer = 1
1032 int flags 1041 Coro::schedule = 2
1042 Coro::cede = 3
1043 Coro::Cont::yield = 4
1033 CODE: 1044 CODE:
1034 PUTBACK;
1035 SV_CORO (next, "Coro::transfer");
1036 SV_CORO (prev, "Coro::transfer");
1037 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
1038 SPAGAIN;
1039
1040int
1041prio (Coro::State coro, int newprio = 0)
1042 ALIAS:
1043 nice = 1
1044 CODE:
1045{ 1045{
1046 RETVAL = coro->prio; 1046 struct transfer_args ta;
1047 1047
1048 if (items > 1) 1048 switch (ix)
1049 { 1049 {
1050 case 0:
1051 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0)));
1052 ta.next = 0;
1053 ta.flags = TRANSFER_SET_STACKLEVEL;
1054 break;
1055
1056 case 1:
1050 if (ix) 1057 if (items != 3)
1051 newprio += coro->prio; 1058 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items);
1052 1059
1053 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1060 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1054 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1061 break;
1055 1062
1056 coro->prio = newprio; 1063 case 2:
1064 prepare_schedule (&ta);
1065 break;
1066
1067 case 3:
1068 prepare_cede (&ta);
1069 break;
1070
1071 case 4:
1072 {
1073 SV *yieldstack;
1074 SV *sv;
1075 AV *defav = GvAV (PL_defgv);
1076
1077 yieldstack = *hv_fetch (
1078 (HV *)SvRV (GvSV (coro_current)),
1079 "yieldstack", sizeof ("yieldstack") - 1,
1080 0
1081 );
1082
1083 /* set up @_ -- ugly */
1084 av_clear (defav);
1085 av_fill (defav, items - 1);
1086 while (items--)
1087 av_store (defav, items, SvREFCNT_inc (ST(items)));
1088
1089 sv = av_pop ((AV *)SvRV (yieldstack));
1090 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1091 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1092 ta.flags = 0;
1093 SvREFCNT_dec (sv);
1094 }
1095 break;
1096
1057 } 1097 }
1098
1099 TRANSFER (ta);
1058} 1100}
1059 1101
1060void 1102void
1061_clone_state_from (SV *dst, SV *src) 1103_clone_state_from (SV *dst, SV *src)
1062 CODE: 1104 CODE:
1066 sv_unmagic (SvRV (dst), PERL_MAGIC_ext); 1108 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1067 1109
1068 ++coro_src->refcnt; 1110 ++coro_src->refcnt;
1069 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1111 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1070} 1112}
1113
1114void
1115_nonlocal_goto (IV nextop)
1116 CODE:
1117 /* uuh, somebody will kill me again for this */
1118 PL_op->op_next = INT2PTR (OP *, nextop);
1071 1119
1072void 1120void
1073_exit (code) 1121_exit (code)
1074 int code 1122 int code
1075 PROTOTYPE: $ 1123 PROTOTYPE: $
1076 CODE: 1124 CODE:
1077 _exit (code); 1125 _exit (code);
1078 1126
1079MODULE = Coro::State PACKAGE = Coro::Cont
1080
1081void
1082yield (...)
1083 PROTOTYPE: @
1084 CODE:
1085 SV *yieldstack;
1086 SV *sv;
1087 AV *defav = GvAV (PL_defgv);
1088 struct coro *prev, *next;
1089
1090 yieldstack = *hv_fetch (
1091 (HV *)SvRV (GvSV (coro_current)),
1092 "yieldstack", sizeof ("yieldstack") - 1,
1093 0
1094 );
1095
1096 /* set up @_ -- ugly */
1097 av_clear (defav);
1098 av_fill (defav, items - 1);
1099 while (items--)
1100 av_store (defav, items, SvREFCNT_inc (ST(items)));
1101
1102 sv = av_pop ((AV *)SvRV (yieldstack));
1103 prev = SvSTATE ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)));
1104 next = SvSTATE ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
1105 SvREFCNT_dec (sv);
1106
1107 coro_state_transfer (aTHX_ prev, next, 0);
1108
1109MODULE = Coro::State PACKAGE = Coro 1127MODULE = Coro::State PACKAGE = Coro
1110 1128
1111BOOT: 1129BOOT:
1112{ 1130{
1113 int i; 1131 int i;
1132
1114 HV *stash = gv_stashpv ("Coro", TRUE); 1133 coro_stash = gv_stashpv ("Coro", TRUE);
1115 1134
1116 newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1135 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1117 newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1136 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1118 newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1137 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1119 newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1138 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1120 newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1139 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1121 newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1140 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1122 1141
1123 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1142 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
1124 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1143 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1125 1144
1126 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1145 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1139 sv_setiv (sv, (IV)&coroapi); 1158 sv_setiv (sv, (IV)&coroapi);
1140 SvREADONLY_on (sv); 1159 SvREADONLY_on (sv);
1141 } 1160 }
1142} 1161}
1143 1162
1144#if !PERL_MICRO 1163int
1164prio (Coro::State coro, int newprio = 0)
1165 ALIAS:
1166 nice = 1
1167 CODE:
1168{
1169 RETVAL = coro->prio;
1170
1171 if (items > 1)
1172 {
1173 if (ix)
1174 newprio += coro->prio;
1175
1176 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1177 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1178
1179 coro->prio = newprio;
1180 }
1181}
1145 1182
1146void 1183void
1147ready (self) 1184ready (SV *self)
1148 SV * self
1149 PROTOTYPE: $ 1185 PROTOTYPE: $
1150 CODE: 1186 CODE:
1151 api_ready (self); 1187 api_ready (self);
1152
1153#endif
1154 1188
1155int 1189int
1156nready (...) 1190nready (...)
1157 PROTOTYPE: 1191 PROTOTYPE:
1158 CODE: 1192 CODE:
1159 RETVAL = coro_nready; 1193 RETVAL = coro_nready;
1160 OUTPUT: 1194 OUTPUT:
1161 RETVAL 1195 RETVAL
1162 1196
1163void 1197MODULE = Coro::State PACKAGE = Coro::AIO
1164schedule (...)
1165 PROTOTYPE:
1166 CODE:
1167 api_schedule ();
1168 1198
1169void
1170cede (...)
1171 PROTOTYPE:
1172 CODE:
1173 api_cede ();
1174
1175# and these are hacks
1176SV * 1199SV *
1177_aio_get_state () 1200_get_state ()
1178 CODE: 1201 CODE:
1179{ 1202{
1180 struct { 1203 struct {
1181 int errorno; 1204 int errorno;
1182 int laststype; 1205 int laststype;
1193} 1216}
1194 OUTPUT: 1217 OUTPUT:
1195 RETVAL 1218 RETVAL
1196 1219
1197void 1220void
1198_aio_set_state (char *data_) 1221_set_state (char *data_)
1199 PROTOTYPE: $ 1222 PROTOTYPE: $
1200 CODE: 1223 CODE:
1201{ 1224{
1202 struct { 1225 struct {
1203 int errorno; 1226 int errorno;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines