… | |
… | |
90 | |
90 | |
91 | #define NOINLINE attribute ((noinline)) |
91 | #define NOINLINE attribute ((noinline)) |
92 | |
92 | |
93 | #include "CoroAPI.h" |
93 | #include "CoroAPI.h" |
94 | |
94 | |
95 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
|
|
96 | |
|
|
97 | #ifdef USE_ITHREADS |
95 | #ifdef USE_ITHREADS |
98 | static perl_mutex coro_mutex; |
96 | static perl_mutex coro_mutex; |
99 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
97 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
100 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
98 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
101 | #else |
99 | #else |
102 | # define LOCK (void)0 |
100 | # define LOCK (void)0 |
103 | # define UNLOCK (void)0 |
101 | # define UNLOCK (void)0 |
104 | #endif |
102 | #endif |
105 | |
103 | |
|
|
104 | struct io_state |
|
|
105 | { |
|
|
106 | int errorno; |
|
|
107 | I32 laststype; |
|
|
108 | int laststatval; |
|
|
109 | Stat_t statcache; |
|
|
110 | }; |
|
|
111 | |
106 | static struct CoroAPI coroapi; |
112 | static struct CoroAPI coroapi; |
107 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
113 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
108 | static HV *coro_state_stash, *coro_stash; |
114 | static HV *coro_state_stash, *coro_stash; |
109 | static SV *coro_mortal; /* will be freed after next transfer */ |
115 | static SV *coro_mortal; /* will be freed after next transfer */ |
110 | |
116 | |
… | |
… | |
130 | int valgrind_id; |
136 | int valgrind_id; |
131 | #endif |
137 | #endif |
132 | } coro_cctx; |
138 | } coro_cctx; |
133 | |
139 | |
134 | enum { |
140 | enum { |
135 | CF_RUNNING, /* coroutine is running */ |
141 | CF_RUNNING = 0x0001, /* coroutine is running */ |
136 | CF_READY, /* coroutine is ready */ |
142 | CF_READY = 0x0002, /* coroutine is ready */ |
|
|
143 | CF_NEW = 0x0004, /* ahs never been switched to */ |
137 | }; |
144 | }; |
138 | |
145 | |
139 | /* this is a structure representing a perl-level coroutine */ |
146 | /* this is a structure representing a perl-level coroutine */ |
140 | struct coro { |
147 | struct coro { |
141 | /* the c coroutine allocated to this perl coroutine, if any */ |
148 | /* the c coroutine allocated to this perl coroutine, if any */ |
142 | coro_cctx *cctx; |
149 | coro_cctx *cctx; |
143 | |
150 | |
144 | /* data associated with this coroutine (initial args) */ |
151 | /* data associated with this coroutine (initial args) */ |
145 | AV *args; |
152 | AV *args; |
146 | int refcnt; |
153 | int refcnt; |
147 | int flags; |
154 | int save; /* CORO_SAVE flags */ |
|
|
155 | int flags; /* CF_ flags */ |
148 | |
156 | |
149 | /* optionally saved, might be zero */ |
157 | /* optionally saved, might be zero */ |
150 | AV *defav; |
158 | AV *defav; /* @_ */ |
151 | SV *defsv; |
159 | SV *defsv; /* $_ */ |
152 | SV *errsv; |
160 | SV *errsv; /* $@ */ |
|
|
161 | SV *irssv; /* $/ */ |
153 | |
162 | |
154 | #define VAR(name,type) type name; |
163 | #define VAR(name,type) type name; |
155 | # include "state.h" |
164 | # include "state.h" |
156 | #undef VAR |
165 | #undef VAR |
157 | |
166 | |
… | |
… | |
280 | } |
289 | } |
281 | |
290 | |
282 | #define SB do { |
291 | #define SB do { |
283 | #define SE } while (0) |
292 | #define SE } while (0) |
284 | |
293 | |
285 | #define LOAD(state) load_state((state)); |
|
|
286 | #define SAVE(state,flags) save_state((state),(flags)); |
|
|
287 | |
|
|
288 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
294 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
289 | |
295 | |
290 | static void |
296 | static void |
291 | load_state(Coro__State c) |
297 | load_perl (Coro__State c) |
292 | { |
298 | { |
293 | #define VAR(name,type) PL_ ## name = c->name; |
299 | #define VAR(name,type) PL_ ## name = c->name; |
294 | # include "state.h" |
300 | # include "state.h" |
295 | #undef VAR |
301 | #undef VAR |
296 | |
302 | |
297 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
303 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
298 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
304 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
299 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
305 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
|
|
306 | if (c->irssv) REPLACE_SV (PL_rs , c->irssv); |
300 | |
307 | |
301 | { |
308 | { |
302 | dSP; |
309 | dSP; |
303 | CV *cv; |
310 | CV *cv; |
304 | |
311 | |
… | |
… | |
313 | PUTBACK; |
320 | PUTBACK; |
314 | } |
321 | } |
315 | } |
322 | } |
316 | |
323 | |
317 | static void |
324 | static void |
318 | save_state(Coro__State c, int flags) |
325 | save_perl (Coro__State c) |
319 | { |
326 | { |
320 | { |
327 | { |
321 | dSP; |
328 | dSP; |
322 | I32 cxix = cxstack_ix; |
329 | I32 cxix = cxstack_ix; |
323 | PERL_CONTEXT *ccstk = cxstack; |
330 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
334 | { |
341 | { |
335 | while (cxix >= 0) |
342 | while (cxix >= 0) |
336 | { |
343 | { |
337 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
344 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
338 | |
345 | |
339 | if (CxTYPE(cx) == CXt_SUB) |
346 | if (CxTYPE (cx) == CXt_SUB) |
340 | { |
347 | { |
341 | CV *cv = cx->blk_sub.cv; |
348 | CV *cv = cx->blk_sub.cv; |
342 | |
349 | |
343 | if (CvDEPTH (cv)) |
350 | if (CvDEPTH (cv)) |
344 | { |
351 | { |
345 | EXTEND (SP, 3); |
352 | EXTEND (SP, 3); |
346 | |
353 | |
347 | PUSHs ((SV *)CvPADLIST(cv)); |
354 | PUSHs ((SV *)CvPADLIST (cv)); |
348 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
355 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
349 | PUSHs ((SV *)cv); |
356 | PUSHs ((SV *)cv); |
350 | |
357 | |
351 | CvDEPTH (cv) = 0; |
358 | CvDEPTH (cv) = 0; |
352 | get_padlist (cv); |
359 | get_padlist (cv); |
353 | } |
360 | } |
354 | } |
361 | } |
355 | #ifdef CXt_FORMAT |
362 | #ifdef CXt_FORMAT |
356 | else if (CxTYPE(cx) == CXt_FORMAT) |
363 | else if (CxTYPE (cx) == CXt_FORMAT) |
357 | { |
364 | { |
358 | /* I never used formats, so how should I know how these are implemented? */ |
365 | /* I never used formats, so how should I know how these are implemented? */ |
359 | /* my bold guess is as a simple, plain sub... */ |
366 | /* my bold guess is as a simple, plain sub... */ |
360 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
367 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
361 | } |
368 | } |
… | |
… | |
371 | } |
378 | } |
372 | |
379 | |
373 | PUTBACK; |
380 | PUTBACK; |
374 | } |
381 | } |
375 | |
382 | |
376 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
383 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
377 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
384 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
378 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
385 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
386 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
379 | |
387 | |
380 | #define VAR(name,type)c->name = PL_ ## name; |
388 | #define VAR(name,type)c->name = PL_ ## name; |
381 | # include "state.h" |
389 | # include "state.h" |
382 | #undef VAR |
390 | #undef VAR |
383 | } |
391 | } |
… | |
… | |
485 | * emulate part of the perl startup here. |
493 | * emulate part of the perl startup here. |
486 | */ |
494 | */ |
487 | |
495 | |
488 | coro_init_stacks (); |
496 | coro_init_stacks (); |
489 | |
497 | |
490 | PL_curcop = 0; |
498 | PL_curcop = &PL_compiling; |
491 | PL_in_eval = 0; |
499 | PL_in_eval = EVAL_NULL; |
492 | PL_curpm = 0; |
500 | PL_curpm = 0; |
|
|
501 | PL_localizing = 0; |
|
|
502 | PL_dirty = 0; |
|
|
503 | PL_restartop = 0; |
493 | |
504 | |
494 | { |
505 | { |
495 | dSP; |
506 | dSP; |
496 | LOGOP myop; |
507 | LOGOP myop; |
497 | |
508 | |
498 | /* I have no idea why this is needed, but it is */ |
|
|
499 | PUSHMARK (SP); |
|
|
500 | |
|
|
501 | SvREFCNT_dec (GvAV (PL_defgv)); |
509 | SvREFCNT_dec (GvAV (PL_defgv)); |
502 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
510 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
503 | |
511 | |
504 | Zero (&myop, 1, LOGOP); |
512 | Zero (&myop, 1, LOGOP); |
505 | myop.op_next = Nullop; |
513 | myop.op_next = Nullop; |
506 | myop.op_flags = OPf_WANT_VOID; |
514 | myop.op_flags = OPf_WANT_VOID; |
507 | |
515 | |
508 | PL_op = (OP *)&myop; |
|
|
509 | |
|
|
510 | PUSHMARK (SP); |
516 | PUSHMARK (SP); |
511 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
517 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
512 | PUTBACK; |
518 | PUTBACK; |
|
|
519 | PL_op = (OP *)&myop; |
513 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
520 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
514 | SPAGAIN; |
521 | SPAGAIN; |
515 | |
|
|
516 | ENTER; /* necessary e.g. for dounwind */ |
|
|
517 | } |
522 | } |
|
|
523 | |
|
|
524 | ENTER; /* necessary e.g. for dounwind */ |
518 | } |
525 | } |
519 | |
526 | |
520 | static void |
527 | static void |
521 | free_coro_mortal () |
528 | free_coro_mortal () |
522 | { |
529 | { |
… | |
… | |
525 | SvREFCNT_dec (coro_mortal); |
532 | SvREFCNT_dec (coro_mortal); |
526 | coro_mortal = 0; |
533 | coro_mortal = 0; |
527 | } |
534 | } |
528 | } |
535 | } |
529 | |
536 | |
|
|
537 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
530 | static void NOINLINE |
538 | static void NOINLINE |
531 | prepare_cctx (coro_cctx *cctx) |
539 | prepare_cctx (coro_cctx *cctx) |
532 | { |
540 | { |
533 | dSP; |
541 | dSP; |
534 | LOGOP myop; |
542 | LOGOP myop; |
535 | |
543 | |
536 | Zero (&myop, 1, LOGOP); |
544 | Zero (&myop, 1, LOGOP); |
537 | myop.op_next = PL_op; |
545 | myop.op_next = PL_op; |
538 | myop.op_flags = OPf_WANT_VOID; |
546 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
539 | |
|
|
540 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
|
|
541 | |
547 | |
542 | PUSHMARK (SP); |
548 | PUSHMARK (SP); |
|
|
549 | EXTEND (SP, 2); |
|
|
550 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
543 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
551 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
544 | PUTBACK; |
552 | PUTBACK; |
|
|
553 | PL_op = (OP *)&myop; |
545 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
554 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
546 | SPAGAIN; |
555 | SPAGAIN; |
547 | } |
556 | } |
548 | |
557 | |
549 | static void |
558 | static void |
550 | coro_run (void *arg) |
559 | coro_run (void *arg) |
551 | { |
560 | { |
552 | /* coro_run is the alternative epilogue of transfer() */ |
561 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
553 | UNLOCK; |
562 | UNLOCK; |
554 | |
563 | |
555 | /* |
564 | /* |
556 | * this is a _very_ stripped down perl interpreter ;) |
565 | * this is a _very_ stripped down perl interpreter ;) |
557 | */ |
566 | */ |
… | |
… | |
559 | |
568 | |
560 | /* inject call to cctx_init */ |
569 | /* inject call to cctx_init */ |
561 | prepare_cctx ((coro_cctx *)arg); |
570 | prepare_cctx ((coro_cctx *)arg); |
562 | |
571 | |
563 | /* somebody will hit me for both perl_run and PL_restartop */ |
572 | /* somebody will hit me for both perl_run and PL_restartop */ |
|
|
573 | PL_restartop = PL_op; |
564 | perl_run (PL_curinterp); |
574 | perl_run (PL_curinterp); |
565 | |
575 | |
566 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
576 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
567 | abort (); |
577 | abort (); |
568 | } |
578 | } |
… | |
… | |
577 | New (0, cctx, 1, coro_cctx); |
587 | New (0, cctx, 1, coro_cctx); |
578 | |
588 | |
579 | #if HAVE_MMAP |
589 | #if HAVE_MMAP |
580 | |
590 | |
581 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
591 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
582 | /* mmap suppsedly does allocate-on-write for us */ |
592 | /* mmap supposedly does allocate-on-write for us */ |
583 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
593 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
584 | |
594 | |
585 | if (cctx->sptr == (void *)-1) |
595 | if (cctx->sptr == (void *)-1) |
586 | { |
596 | { |
587 | perror ("FATAL: unable to mmap stack for coroutine"); |
597 | perror ("FATAL: unable to mmap stack for coroutine"); |
… | |
… | |
677 | cctx_first = cctx; |
687 | cctx_first = cctx; |
678 | } |
688 | } |
679 | |
689 | |
680 | /* never call directly, always through the coro_state_transfer global variable */ |
690 | /* never call directly, always through the coro_state_transfer global variable */ |
681 | static void NOINLINE |
691 | static void NOINLINE |
682 | transfer (struct coro *prev, struct coro *next, int flags) |
692 | transfer (struct coro *prev, struct coro *next) |
683 | { |
693 | { |
684 | dSTACKLEVEL; |
694 | dSTACKLEVEL; |
685 | |
695 | |
686 | /* sometimes transfer is only called to set idle_sp */ |
696 | /* sometimes transfer is only called to set idle_sp */ |
687 | if (flags == TRANSFER_SET_STACKLEVEL) |
697 | if (!next) |
688 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
698 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
689 | else if (prev != next) |
699 | else if (prev != next) |
690 | { |
700 | { |
691 | coro_cctx *prev__cctx; |
701 | coro_cctx *prev__cctx; |
692 | |
702 | |
693 | if (!prev->cctx) |
703 | if (prev->flags & CF_NEW) |
694 | { |
704 | { |
695 | /* create a new empty context */ |
705 | /* create a new empty context */ |
696 | Newz (0, prev->cctx, 1, coro_cctx); |
706 | Newz (0, prev->cctx, 1, coro_cctx); |
697 | prev->cctx->inuse = 1; |
707 | prev->cctx->inuse = 1; |
|
|
708 | prev->flags &= ~CF_NEW; |
698 | prev->flags |= CF_RUNNING; |
709 | prev->flags |= CF_RUNNING; |
699 | } |
710 | } |
700 | |
711 | |
|
|
712 | /*TODO: must not croak here */ |
701 | if (!prev->flags & CF_RUNNING) |
713 | if (!prev->flags & CF_RUNNING) |
702 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
714 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
703 | |
715 | |
704 | if (next->flags & CF_RUNNING) |
716 | if (next->flags & CF_RUNNING) |
705 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
717 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
… | |
… | |
707 | prev->flags &= ~CF_RUNNING; |
719 | prev->flags &= ~CF_RUNNING; |
708 | next->flags |= CF_RUNNING; |
720 | next->flags |= CF_RUNNING; |
709 | |
721 | |
710 | LOCK; |
722 | LOCK; |
711 | |
723 | |
712 | if (next->mainstack) |
724 | if (next->flags & CF_NEW) |
713 | { |
|
|
714 | /* coroutine already started */ |
|
|
715 | SAVE (prev, flags); |
|
|
716 | LOAD (next); |
|
|
717 | } |
|
|
718 | else |
|
|
719 | { |
725 | { |
720 | /* need to start coroutine */ |
726 | /* need to start coroutine */ |
|
|
727 | next->flags &= ~CF_NEW; |
721 | /* first get rid of the old state */ |
728 | /* first get rid of the old state */ |
722 | SAVE (prev, -1); |
729 | save_perl (prev); |
723 | /* setup coroutine call */ |
730 | /* setup coroutine call */ |
724 | setup_coro (next); |
731 | setup_coro (next); |
725 | /* need a new stack */ |
732 | /* need a new stack */ |
726 | assert (!next->stack); |
733 | assert (!next->stack); |
727 | } |
734 | } |
|
|
735 | else |
|
|
736 | { |
|
|
737 | /* coroutine already started */ |
|
|
738 | save_perl (prev); |
|
|
739 | load_perl (next); |
|
|
740 | } |
728 | |
741 | |
729 | prev__cctx = prev->cctx; |
742 | prev__cctx = prev->cctx; |
730 | |
743 | |
731 | /* possibly "free" the cctx */ |
744 | /* possibly "free" the cctx */ |
732 | if (prev__cctx->idle_sp == STACKLEVEL) |
745 | if (prev__cctx->idle_sp == STACKLEVEL) |
733 | { |
746 | { |
734 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
747 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
735 | assert (PL_top_env == prev__cctx->top_env); |
748 | assert (PL_top_env == prev__cctx->top_env); |
736 | |
749 | |
|
|
750 | prev->cctx = 0; |
|
|
751 | |
737 | cctx_put (prev__cctx); |
752 | cctx_put (prev__cctx); |
738 | prev->cctx = 0; |
753 | prev__cctx->inuse = 0; |
739 | } |
754 | } |
740 | |
755 | |
741 | if (!next->cctx) |
756 | if (!next->cctx) |
|
|
757 | { |
742 | next->cctx = cctx_get (); |
758 | next->cctx = cctx_get (); |
|
|
759 | assert (!next->cctx->inuse); |
|
|
760 | next->cctx->inuse = 1; |
|
|
761 | } |
743 | |
762 | |
744 | if (prev__cctx != next->cctx) |
763 | if (prev__cctx != next->cctx) |
745 | { |
764 | { |
746 | assert ( prev__cctx->inuse); |
|
|
747 | assert (!next->cctx->inuse); |
|
|
748 | |
|
|
749 | prev__cctx->inuse = 0; |
|
|
750 | next->cctx->inuse = 1; |
|
|
751 | |
|
|
752 | prev__cctx->top_env = PL_top_env; |
765 | prev__cctx->top_env = PL_top_env; |
753 | PL_top_env = next->cctx->top_env; |
766 | PL_top_env = next->cctx->top_env; |
754 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
767 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
755 | } |
768 | } |
756 | |
769 | |
… | |
… | |
761 | } |
774 | } |
762 | |
775 | |
763 | struct transfer_args |
776 | struct transfer_args |
764 | { |
777 | { |
765 | struct coro *prev, *next; |
778 | struct coro *prev, *next; |
766 | int flags; |
|
|
767 | }; |
779 | }; |
768 | |
780 | |
769 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) |
781 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
770 | |
782 | |
771 | static void |
783 | static void |
772 | coro_state_destroy (struct coro *coro) |
784 | coro_state_destroy (struct coro *coro) |
773 | { |
785 | { |
774 | if (coro->refcnt--) |
786 | if (coro->refcnt--) |
775 | return; |
787 | return; |
776 | |
|
|
777 | if (coro->flags & CF_RUNNING) |
|
|
778 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
779 | |
788 | |
780 | if (coro->mainstack && coro->mainstack != main_mainstack) |
789 | if (coro->mainstack && coro->mainstack != main_mainstack) |
781 | { |
790 | { |
782 | struct coro temp; |
791 | struct coro temp; |
|
|
792 | Zero (&temp, 1, struct coro); |
|
|
793 | temp.save = CORO_SAVE_ALL; |
783 | |
794 | |
784 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
795 | if (coro->flags & CF_RUNNING) |
785 | LOAD (coro); |
796 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
797 | |
|
|
798 | save_perl (&temp); |
|
|
799 | load_perl (coro); |
786 | |
800 | |
787 | coro_destroy_stacks (); |
801 | coro_destroy_stacks (); |
788 | |
802 | |
789 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
803 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
790 | |
804 | |
791 | coro->mainstack = 0; |
805 | coro->mainstack = 0; |
792 | } |
806 | } |
793 | |
807 | |
794 | cctx_destroy (coro->cctx); |
808 | cctx_destroy (coro->cctx); |
… | |
… | |
849 | assert (mg->mg_type == PERL_MAGIC_ext); |
863 | assert (mg->mg_type == PERL_MAGIC_ext); |
850 | return (struct coro *)mg->mg_ptr; |
864 | return (struct coro *)mg->mg_ptr; |
851 | } |
865 | } |
852 | |
866 | |
853 | static void |
867 | static void |
854 | prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) |
868 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
855 | { |
869 | { |
856 | ta->prev = SvSTATE (prev); |
870 | ta->prev = SvSTATE (prev_sv); |
857 | ta->next = SvSTATE (next); |
871 | ta->next = SvSTATE (next_sv); |
858 | ta->flags = flags; |
|
|
859 | } |
872 | } |
860 | |
873 | |
861 | static void |
874 | static void |
862 | api_transfer (SV *prev, SV *next, int flags) |
875 | api_transfer (SV *prev_sv, SV *next_sv) |
863 | { |
876 | { |
864 | dTHX; |
|
|
865 | struct transfer_args ta; |
877 | struct transfer_args ta; |
866 | |
878 | |
867 | prepare_transfer (&ta, prev, next, flags); |
879 | prepare_transfer (&ta, prev_sv, next_sv); |
868 | TRANSFER (ta); |
880 | TRANSFER (ta); |
|
|
881 | } |
|
|
882 | |
|
|
883 | static int |
|
|
884 | api_save (SV *coro_sv, int new_save) |
|
|
885 | { |
|
|
886 | struct coro *coro = SvSTATE (coro_sv); |
|
|
887 | int old_save = coro->save; |
|
|
888 | |
|
|
889 | if (new_save >= 0) |
|
|
890 | coro->save = new_save; |
|
|
891 | |
|
|
892 | return old_save; |
869 | } |
893 | } |
870 | |
894 | |
871 | /** Coro ********************************************************************/ |
895 | /** Coro ********************************************************************/ |
872 | |
896 | |
873 | #define PRIO_MAX 3 |
897 | #define PRIO_MAX 3 |
… | |
… | |
876 | #define PRIO_LOW -1 |
900 | #define PRIO_LOW -1 |
877 | #define PRIO_IDLE -3 |
901 | #define PRIO_IDLE -3 |
878 | #define PRIO_MIN -4 |
902 | #define PRIO_MIN -4 |
879 | |
903 | |
880 | /* for Coro.pm */ |
904 | /* for Coro.pm */ |
881 | static GV *coro_current, *coro_idle; |
905 | static SV *coro_current; |
882 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
906 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
883 | static int coro_nready; |
907 | static int coro_nready; |
884 | |
908 | |
885 | static void |
909 | static void |
886 | coro_enq (SV *coro_sv) |
910 | coro_enq (SV *coro_sv) |
… | |
… | |
919 | coro = SvSTATE (coro_sv); |
943 | coro = SvSTATE (coro_sv); |
920 | |
944 | |
921 | if (coro->flags & CF_READY) |
945 | if (coro->flags & CF_READY) |
922 | return 0; |
946 | return 0; |
923 | |
947 | |
|
|
948 | #if 0 /* this is actually harmless */ |
924 | if (coro->flags & CF_RUNNING) |
949 | if (coro->flags & CF_RUNNING) |
925 | croak ("Coro::ready called on currently running coroutine"); |
950 | croak ("Coro::ready called on currently running coroutine"); |
|
|
951 | #endif |
926 | |
952 | |
927 | coro->flags |= CF_READY; |
953 | coro->flags |= CF_READY; |
928 | |
954 | |
929 | LOCK; |
955 | LOCK; |
930 | coro_enq (SvREFCNT_inc (coro_sv)); |
956 | coro_enq (SvREFCNT_inc (coro_sv)); |
… | |
… | |
940 | } |
966 | } |
941 | |
967 | |
942 | static void |
968 | static void |
943 | prepare_schedule (struct transfer_args *ta) |
969 | prepare_schedule (struct transfer_args *ta) |
944 | { |
970 | { |
945 | SV *current, *prev, *next; |
971 | SV *prev, *next; |
946 | |
|
|
947 | current = GvSV (coro_current); |
|
|
948 | |
972 | |
949 | for (;;) |
973 | for (;;) |
950 | { |
974 | { |
951 | LOCK; |
975 | LOCK; |
952 | next = coro_deq (PRIO_MIN); |
976 | next = coro_deq (PRIO_MIN); |
… | |
… | |
961 | ENTER; |
985 | ENTER; |
962 | SAVETMPS; |
986 | SAVETMPS; |
963 | |
987 | |
964 | PUSHMARK (SP); |
988 | PUSHMARK (SP); |
965 | PUTBACK; |
989 | PUTBACK; |
966 | call_sv (GvSV (coro_idle), G_DISCARD); |
990 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
967 | |
991 | |
968 | FREETMPS; |
992 | FREETMPS; |
969 | LEAVE; |
993 | LEAVE; |
970 | } |
994 | } |
971 | } |
995 | } |
972 | |
996 | |
973 | prev = SvRV (current); |
997 | prev = SvRV (coro_current); |
974 | SvRV (current) = next; |
998 | SvRV_set (coro_current, next); |
975 | |
999 | |
976 | /* free this only after the transfer */ |
1000 | /* free this only after the transfer */ |
977 | LOCK; |
1001 | LOCK; |
978 | free_coro_mortal (); |
1002 | free_coro_mortal (); |
979 | UNLOCK; |
1003 | UNLOCK; |
980 | coro_mortal = prev; |
1004 | coro_mortal = prev; |
981 | |
1005 | |
|
|
1006 | assert (!SvROK(prev));//D |
|
|
1007 | assert (!SvROK(next));//D |
|
|
1008 | |
982 | ta->prev = SvSTATE (prev); |
1009 | ta->prev = SvSTATE (prev); |
983 | ta->next = SvSTATE (next); |
1010 | ta->next = SvSTATE (next); |
984 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
985 | |
1011 | |
|
|
1012 | assert (ta->next->flags & CF_READY); |
986 | ta->next->flags &= ~CF_READY; |
1013 | ta->next->flags &= ~CF_READY; |
987 | } |
1014 | } |
988 | |
1015 | |
989 | static void |
1016 | static void |
990 | prepare_cede (struct transfer_args *ta) |
1017 | prepare_cede (struct transfer_args *ta) |
991 | { |
1018 | { |
992 | api_ready (GvSV (coro_current)); |
1019 | api_ready (coro_current); |
993 | |
1020 | |
994 | prepare_schedule (ta); |
1021 | prepare_schedule (ta); |
995 | } |
1022 | } |
996 | |
1023 | |
997 | static void |
1024 | static void |
998 | api_schedule (void) |
1025 | api_schedule (void) |
999 | { |
1026 | { |
1000 | dTHX; |
|
|
1001 | struct transfer_args ta; |
1027 | struct transfer_args ta; |
1002 | |
1028 | |
1003 | prepare_schedule (&ta); |
1029 | prepare_schedule (&ta); |
1004 | TRANSFER (ta); |
1030 | TRANSFER (ta); |
1005 | } |
1031 | } |
1006 | |
1032 | |
1007 | static void |
1033 | static void |
1008 | api_cede (void) |
1034 | api_cede (void) |
1009 | { |
1035 | { |
1010 | dTHX; |
|
|
1011 | struct transfer_args ta; |
1036 | struct transfer_args ta; |
1012 | |
1037 | |
1013 | prepare_cede (&ta); |
1038 | prepare_cede (&ta); |
1014 | TRANSFER (ta); |
1039 | TRANSFER (ta); |
1015 | } |
1040 | } |
… | |
… | |
1025 | #endif |
1050 | #endif |
1026 | BOOT_PAGESIZE; |
1051 | BOOT_PAGESIZE; |
1027 | |
1052 | |
1028 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1053 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1029 | |
1054 | |
1030 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
1055 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1031 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
1056 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1032 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
1057 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
|
|
1058 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1059 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1033 | |
1060 | |
1034 | main_mainstack = PL_mainstack; |
1061 | main_mainstack = PL_mainstack; |
1035 | |
1062 | |
1036 | coroapi.ver = CORO_API_VERSION; |
1063 | coroapi.ver = CORO_API_VERSION; |
1037 | coroapi.transfer = api_transfer; |
1064 | coroapi.transfer = api_transfer; |
… | |
… | |
1047 | HV *hv; |
1074 | HV *hv; |
1048 | int i; |
1075 | int i; |
1049 | |
1076 | |
1050 | Newz (0, coro, 1, struct coro); |
1077 | Newz (0, coro, 1, struct coro); |
1051 | coro->args = newAV (); |
1078 | coro->args = newAV (); |
|
|
1079 | coro->save = CORO_SAVE_ALL; |
|
|
1080 | coro->flags = CF_NEW; |
1052 | |
1081 | |
1053 | hv = newHV (); |
1082 | hv = newHV (); |
1054 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1083 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1055 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1084 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1056 | |
1085 | |
1057 | for (i = 1; i < items; i++) |
1086 | for (i = 1; i < items; i++) |
1058 | av_push (coro->args, newSVsv (ST (i))); |
1087 | av_push (coro->args, newSVsv (ST (i))); |
1059 | } |
1088 | } |
1060 | OUTPUT: |
1089 | OUTPUT: |
|
|
1090 | RETVAL |
|
|
1091 | |
|
|
1092 | int |
|
|
1093 | save (SV *coro, int new_save = -1) |
|
|
1094 | CODE: |
|
|
1095 | RETVAL = api_save (coro, new_save); |
|
|
1096 | OUTPUT: |
1061 | RETVAL |
1097 | RETVAL |
1062 | |
1098 | |
1063 | void |
1099 | void |
1064 | _set_stacklevel (...) |
1100 | _set_stacklevel (...) |
1065 | ALIAS: |
1101 | ALIAS: |
1066 | Coro::State::transfer = 1 |
1102 | Coro::State::transfer = 1 |
1067 | Coro::schedule = 2 |
1103 | Coro::schedule = 2 |
1068 | Coro::cede = 3 |
1104 | Coro::cede = 3 |
1069 | Coro::Cont::yield = 4 |
|
|
1070 | CODE: |
1105 | CODE: |
1071 | { |
1106 | { |
1072 | struct transfer_args ta; |
1107 | struct transfer_args ta; |
1073 | |
1108 | |
1074 | switch (ix) |
1109 | switch (ix) |
1075 | { |
1110 | { |
1076 | case 0: |
1111 | case 0: |
1077 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1112 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1078 | ta.next = 0; |
1113 | ta.next = 0; |
1079 | ta.flags = TRANSFER_SET_STACKLEVEL; |
|
|
1080 | break; |
1114 | break; |
1081 | |
1115 | |
1082 | case 1: |
1116 | case 1: |
1083 | if (items != 3) |
1117 | if (items != 2) |
1084 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1118 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1085 | |
1119 | |
1086 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1120 | prepare_transfer (&ta, ST (0), ST (1)); |
1087 | break; |
1121 | break; |
1088 | |
1122 | |
1089 | case 2: |
1123 | case 2: |
1090 | prepare_schedule (&ta); |
1124 | prepare_schedule (&ta); |
1091 | break; |
1125 | break; |
1092 | |
1126 | |
1093 | case 3: |
1127 | case 3: |
1094 | prepare_cede (&ta); |
1128 | prepare_cede (&ta); |
1095 | break; |
1129 | break; |
1096 | |
|
|
1097 | case 4: |
|
|
1098 | { |
|
|
1099 | SV *yieldstack; |
|
|
1100 | SV *sv; |
|
|
1101 | AV *defav = GvAV (PL_defgv); |
|
|
1102 | |
|
|
1103 | yieldstack = *hv_fetch ( |
|
|
1104 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1105 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1106 | 0 |
|
|
1107 | ); |
|
|
1108 | |
|
|
1109 | /* set up @_ -- ugly */ |
|
|
1110 | av_clear (defav); |
|
|
1111 | av_fill (defav, items - 1); |
|
|
1112 | while (items--) |
|
|
1113 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1114 | |
|
|
1115 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1116 | ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1117 | ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1118 | ta.flags = 0; |
|
|
1119 | SvREFCNT_dec (sv); |
|
|
1120 | } |
|
|
1121 | break; |
|
|
1122 | |
|
|
1123 | } |
1130 | } |
1124 | |
1131 | |
1125 | TRANSFER (ta); |
1132 | TRANSFER (ta); |
1126 | } |
1133 | } |
1127 | |
1134 | |
… | |
… | |
1171 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1178 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1172 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1179 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1173 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1180 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1174 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1181 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1175 | |
1182 | |
1176 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1183 | coro_current = get_sv ("Coro::current", FALSE); |
1177 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1184 | SvREADONLY_on (coro_current); |
1178 | |
1185 | |
1179 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1186 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1180 | coro_ready[i] = newAV (); |
1187 | coro_ready[i] = newAV (); |
1181 | |
1188 | |
1182 | { |
1189 | { |
1183 | SV *sv = perl_get_sv("Coro::API", 1); |
1190 | SV *sv = perl_get_sv("Coro::API", 1); |
1184 | |
1191 | |
1185 | coroapi.schedule = api_schedule; |
1192 | coroapi.schedule = api_schedule; |
|
|
1193 | coroapi.save = api_save; |
1186 | coroapi.cede = api_cede; |
1194 | coroapi.cede = api_cede; |
1187 | coroapi.ready = api_ready; |
1195 | coroapi.ready = api_ready; |
1188 | coroapi.is_ready = api_is_ready; |
1196 | coroapi.is_ready = api_is_ready; |
1189 | coroapi.nready = &coro_nready; |
1197 | coroapi.nready = &coro_nready; |
1190 | coroapi.current = coro_current; |
1198 | coroapi.current = coro_current; |
… | |
… | |
1192 | GCoroAPI = &coroapi; |
1200 | GCoroAPI = &coroapi; |
1193 | sv_setiv (sv, (IV)&coroapi); |
1201 | sv_setiv (sv, (IV)&coroapi); |
1194 | SvREADONLY_on (sv); |
1202 | SvREADONLY_on (sv); |
1195 | } |
1203 | } |
1196 | } |
1204 | } |
|
|
1205 | |
|
|
1206 | void |
|
|
1207 | _set_current (SV *current) |
|
|
1208 | PROTOTYPE: $ |
|
|
1209 | CODE: |
|
|
1210 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1211 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
1197 | |
1212 | |
1198 | int |
1213 | int |
1199 | prio (Coro::State coro, int newprio = 0) |
1214 | prio (Coro::State coro, int newprio = 0) |
1200 | ALIAS: |
1215 | ALIAS: |
1201 | nice = 1 |
1216 | nice = 1 |
… | |
… | |
1243 | |
1258 | |
1244 | SV * |
1259 | SV * |
1245 | _get_state () |
1260 | _get_state () |
1246 | CODE: |
1261 | CODE: |
1247 | { |
1262 | { |
1248 | struct { |
1263 | struct io_state *data; |
1249 | int errorno; |
|
|
1250 | int laststype; |
|
|
1251 | int laststatval; |
|
|
1252 | Stat_t statcache; |
|
|
1253 | } data; |
|
|
1254 | |
1264 | |
|
|
1265 | RETVAL = newSV (sizeof (struct io_state)); |
|
|
1266 | data = (struct io_state *)SvPVX (RETVAL); |
|
|
1267 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
|
|
1268 | SvPOK_only (RETVAL); |
|
|
1269 | |
1255 | data.errorno = errno; |
1270 | data->errorno = errno; |
1256 | data.laststype = PL_laststype; |
1271 | data->laststype = PL_laststype; |
1257 | data.laststatval = PL_laststatval; |
1272 | data->laststatval = PL_laststatval; |
1258 | data.statcache = PL_statcache; |
1273 | data->statcache = PL_statcache; |
1259 | |
|
|
1260 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1261 | } |
1274 | } |
1262 | OUTPUT: |
1275 | OUTPUT: |
1263 | RETVAL |
1276 | RETVAL |
1264 | |
1277 | |
1265 | void |
1278 | void |
1266 | _set_state (char *data_) |
1279 | _set_state (char *data_) |
1267 | PROTOTYPE: $ |
1280 | PROTOTYPE: $ |
1268 | CODE: |
1281 | CODE: |
1269 | { |
1282 | { |
1270 | struct { |
1283 | struct io_state *data = (void *)data_; |
1271 | int errorno; |
|
|
1272 | int laststype; |
|
|
1273 | int laststatval; |
|
|
1274 | Stat_t statcache; |
|
|
1275 | } *data = (void *)data_; |
|
|
1276 | |
1284 | |
1277 | errno = data->errorno; |
1285 | errno = data->errorno; |
1278 | PL_laststype = data->laststype; |
1286 | PL_laststype = data->laststype; |
1279 | PL_laststatval = data->laststatval; |
1287 | PL_laststatval = data->laststatval; |
1280 | PL_statcache = data->statcache; |
1288 | PL_statcache = data->statcache; |
1281 | } |
1289 | } |
|
|
1290 | |