… | |
… | |
88 | /* prefer perl internal functions over our own? */ |
88 | /* prefer perl internal functions over our own? */ |
89 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
89 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
90 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
90 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
91 | #endif |
91 | #endif |
92 | |
92 | |
93 | /* The next macro should declare a variable stacklevel that contains and approximation |
93 | /* The next macros try to return the current stack pointer, in an as |
94 | * to the current C stack pointer. Its property is that it changes with each call |
94 | * portable way as possible. */ |
95 | * and should be unique. */ |
|
|
96 | #define dSTACKLEVEL int stacklevel |
95 | #define dSTACKLEVEL volatile char stacklevel |
97 | #define STACKLEVEL ((void *)&stacklevel) |
96 | #define STACKLEVEL ((void *)&stacklevel) |
98 | |
97 | |
99 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
98 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
100 | |
99 | |
101 | #if __GNUC__ >= 3 |
100 | #if __GNUC__ >= 3 |
… | |
… | |
129 | }; |
128 | }; |
130 | |
129 | |
131 | static size_t coro_stacksize = CORO_STACKSIZE; |
130 | static size_t coro_stacksize = CORO_STACKSIZE; |
132 | static struct CoroAPI coroapi; |
131 | static struct CoroAPI coroapi; |
133 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
132 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
133 | static JMPENV *main_top_env; |
134 | static HV *coro_state_stash, *coro_stash; |
134 | static HV *coro_state_stash, *coro_stash; |
135 | static SV *coro_mortal; /* will be freed after next transfer */ |
135 | static SV *coro_mortal; /* will be freed after next transfer */ |
|
|
136 | |
|
|
137 | /* async_pool helper stuff */ |
|
|
138 | static SV *sv_pool_rss; |
|
|
139 | static SV *sv_pool_size; |
|
|
140 | static AV *av_async_pool; |
136 | |
141 | |
137 | static struct coro_cctx *cctx_first; |
142 | static struct coro_cctx *cctx_first; |
138 | static int cctx_count, cctx_idle; |
143 | static int cctx_count, cctx_idle; |
139 | |
144 | |
140 | /* this is a structure representing a c-level coroutine */ |
145 | /* this is a structure representing a c-level coroutine */ |
… | |
… | |
187 | # include "state.h" |
192 | # include "state.h" |
188 | #undef VAR |
193 | #undef VAR |
189 | |
194 | |
190 | /* coro process data */ |
195 | /* coro process data */ |
191 | int prio; |
196 | int prio; |
|
|
197 | |
|
|
198 | /* linked list */ |
|
|
199 | struct coro *next, *prev; |
|
|
200 | HV *hv; /* the perl hash associated with this coro, if any */ |
192 | }; |
201 | }; |
193 | |
202 | |
194 | typedef struct coro *Coro__State; |
203 | typedef struct coro *Coro__State; |
195 | typedef struct coro *Coro__State_or_hashref; |
204 | typedef struct coro *Coro__State_or_hashref; |
196 | |
205 | |
… | |
… | |
205 | |
214 | |
206 | /* for Coro.pm */ |
215 | /* for Coro.pm */ |
207 | static SV *coro_current; |
216 | static SV *coro_current; |
208 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
217 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
209 | static int coro_nready; |
218 | static int coro_nready; |
|
|
219 | static struct coro *coro_first; |
210 | |
220 | |
211 | /** lowlevel stuff **********************************************************/ |
221 | /** lowlevel stuff **********************************************************/ |
212 | |
222 | |
213 | static AV * |
223 | static AV * |
214 | coro_clone_padlist (pTHX_ CV *cv) |
224 | coro_clone_padlist (pTHX_ CV *cv) |
… | |
… | |
373 | CvPADLIST (cv) = (AV *)POPs; |
383 | CvPADLIST (cv) = (AV *)POPs; |
374 | } |
384 | } |
375 | |
385 | |
376 | PUTBACK; |
386 | PUTBACK; |
377 | } |
387 | } |
378 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
|
|
379 | } |
388 | } |
380 | |
389 | |
381 | static void |
390 | static void |
382 | save_perl (pTHX_ Coro__State c) |
391 | save_perl (pTHX_ Coro__State c) |
383 | { |
392 | { |
384 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
|
|
385 | { |
393 | { |
386 | dSP; |
394 | dSP; |
387 | I32 cxix = cxstack_ix; |
395 | I32 cxix = cxstack_ix; |
388 | PERL_CONTEXT *ccstk = cxstack; |
396 | PERL_CONTEXT *ccstk = cxstack; |
389 | PERL_SI *top_si = PL_curstackinfo; |
397 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
391 | /* |
399 | /* |
392 | * the worst thing you can imagine happens first - we have to save |
400 | * the worst thing you can imagine happens first - we have to save |
393 | * (and reinitialize) all cv's in the whole callchain :( |
401 | * (and reinitialize) all cv's in the whole callchain :( |
394 | */ |
402 | */ |
395 | |
403 | |
396 | EXTEND (SP, 3 + 1); |
|
|
397 | PUSHs (Nullsv); |
404 | XPUSHs (Nullsv); |
398 | /* this loop was inspired by pp_caller */ |
405 | /* this loop was inspired by pp_caller */ |
399 | for (;;) |
406 | for (;;) |
400 | { |
407 | { |
401 | while (cxix >= 0) |
408 | while (cxix >= 0) |
402 | { |
409 | { |
403 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
410 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
404 | |
411 | |
405 | if (CxTYPE (cx) == CXt_SUB) |
412 | if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) |
406 | { |
413 | { |
407 | CV *cv = cx->blk_sub.cv; |
414 | CV *cv = cx->blk_sub.cv; |
408 | |
415 | |
409 | if (CvDEPTH (cv)) |
416 | if (CvDEPTH (cv)) |
410 | { |
417 | { |
… | |
… | |
421 | |
428 | |
422 | if (top_si->si_type == PERLSI_MAIN) |
429 | if (top_si->si_type == PERLSI_MAIN) |
423 | break; |
430 | break; |
424 | |
431 | |
425 | top_si = top_si->si_prev; |
432 | top_si = top_si->si_prev; |
426 | ccstk = top_si->si_cxstack; |
433 | ccstk = top_si->si_cxstack; |
427 | cxix = top_si->si_cxix; |
434 | cxix = top_si->si_cxix; |
428 | } |
435 | } |
429 | |
436 | |
430 | PUTBACK; |
437 | PUTBACK; |
431 | } |
438 | } |
432 | |
439 | |
… | |
… | |
451 | # define coro_init_stacks init_stacks |
458 | # define coro_init_stacks init_stacks |
452 | #else |
459 | #else |
453 | static void |
460 | static void |
454 | coro_init_stacks (pTHX) |
461 | coro_init_stacks (pTHX) |
455 | { |
462 | { |
456 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
463 | PL_curstackinfo = new_stackinfo(64, 6); |
457 | PL_curstackinfo->si_type = PERLSI_MAIN; |
464 | PL_curstackinfo->si_type = PERLSI_MAIN; |
458 | PL_curstack = PL_curstackinfo->si_stack; |
465 | PL_curstack = PL_curstackinfo->si_stack; |
459 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
466 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
460 | |
467 | |
461 | PL_stack_base = AvARRAY(PL_curstack); |
468 | PL_stack_base = AvARRAY(PL_curstack); |
462 | PL_stack_sp = PL_stack_base; |
469 | PL_stack_sp = PL_stack_base; |
463 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
470 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
464 | |
471 | |
465 | New(50,PL_tmps_stack,128,SV*); |
472 | New(50,PL_tmps_stack,64,SV*); |
466 | PL_tmps_floor = -1; |
473 | PL_tmps_floor = -1; |
467 | PL_tmps_ix = -1; |
474 | PL_tmps_ix = -1; |
468 | PL_tmps_max = 128; |
475 | PL_tmps_max = 64; |
469 | |
476 | |
470 | New(54,PL_markstack,32,I32); |
477 | New(54,PL_markstack,16,I32); |
471 | PL_markstack_ptr = PL_markstack; |
478 | PL_markstack_ptr = PL_markstack; |
472 | PL_markstack_max = PL_markstack + 32; |
479 | PL_markstack_max = PL_markstack + 16; |
473 | |
480 | |
474 | #ifdef SET_MARK_OFFSET |
481 | #ifdef SET_MARK_OFFSET |
475 | SET_MARK_OFFSET; |
482 | SET_MARK_OFFSET; |
476 | #endif |
483 | #endif |
477 | |
484 | |
478 | New(54,PL_scopestack,32,I32); |
485 | New(54,PL_scopestack,16,I32); |
479 | PL_scopestack_ix = 0; |
486 | PL_scopestack_ix = 0; |
480 | PL_scopestack_max = 32; |
487 | PL_scopestack_max = 16; |
481 | |
488 | |
482 | New(54,PL_savestack,64,ANY); |
489 | New(54,PL_savestack,64,ANY); |
483 | PL_savestack_ix = 0; |
490 | PL_savestack_ix = 0; |
484 | PL_savestack_max = 64; |
491 | PL_savestack_max = 64; |
485 | |
492 | |
486 | #if !PERL_VERSION_ATLEAST (5,9,0) |
493 | #if !PERL_VERSION_ATLEAST (5,9,0) |
487 | New(54,PL_retstack,16,OP*); |
494 | New(54,PL_retstack,4,OP*); |
488 | PL_retstack_ix = 0; |
495 | PL_retstack_ix = 0; |
489 | PL_retstack_max = 16; |
496 | PL_retstack_max = 4; |
490 | #endif |
497 | #endif |
491 | } |
498 | } |
492 | #endif |
499 | #endif |
493 | |
500 | |
494 | /* |
501 | /* |
… | |
… | |
534 | Safefree (PL_scopestack); |
541 | Safefree (PL_scopestack); |
535 | Safefree (PL_savestack); |
542 | Safefree (PL_savestack); |
536 | #if !PERL_VERSION_ATLEAST (5,9,0) |
543 | #if !PERL_VERSION_ATLEAST (5,9,0) |
537 | Safefree (PL_retstack); |
544 | Safefree (PL_retstack); |
538 | #endif |
545 | #endif |
|
|
546 | } |
|
|
547 | |
|
|
548 | static size_t |
|
|
549 | coro_rss (struct coro *coro) |
|
|
550 | { |
|
|
551 | size_t rss = sizeof (coro); |
|
|
552 | |
|
|
553 | if (coro->mainstack) |
|
|
554 | { |
|
|
555 | if (coro->flags & CF_RUNNING) |
|
|
556 | { |
|
|
557 | #define VAR(name,type)coro->name = PL_ ## name; |
|
|
558 | # include "state.h" |
|
|
559 | #undef VAR |
|
|
560 | } |
|
|
561 | |
|
|
562 | rss += sizeof (coro->curstackinfo); |
|
|
563 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
|
|
564 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
|
|
565 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
|
|
566 | rss += coro->tmps_max * sizeof (SV *); |
|
|
567 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
|
|
568 | rss += coro->scopestack_max * sizeof (I32); |
|
|
569 | rss += coro->savestack_max * sizeof (ANY); |
|
|
570 | |
|
|
571 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
572 | rss += coro->retstack_max * sizeof (OP *); |
|
|
573 | #endif |
|
|
574 | } |
|
|
575 | |
|
|
576 | return rss; |
539 | } |
577 | } |
540 | |
578 | |
541 | /** coroutine stack handling ************************************************/ |
579 | /** coroutine stack handling ************************************************/ |
542 | |
580 | |
543 | static void |
581 | static void |
… | |
… | |
566 | Zero (&myop, 1, LOGOP); |
604 | Zero (&myop, 1, LOGOP); |
567 | myop.op_next = Nullop; |
605 | myop.op_next = Nullop; |
568 | myop.op_flags = OPf_WANT_VOID; |
606 | myop.op_flags = OPf_WANT_VOID; |
569 | |
607 | |
570 | PUSHMARK (SP); |
608 | PUSHMARK (SP); |
571 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
609 | XPUSHs (av_shift (GvAV (PL_defgv))); |
572 | PUTBACK; |
610 | PUTBACK; |
573 | PL_op = (OP *)&myop; |
611 | PL_op = (OP *)&myop; |
574 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
612 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
575 | SPAGAIN; |
613 | SPAGAIN; |
576 | } |
614 | } |
… | |
… | |
587 | coro_mortal = 0; |
625 | coro_mortal = 0; |
588 | } |
626 | } |
589 | } |
627 | } |
590 | |
628 | |
591 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
629 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
|
|
630 | /* _cctx_init should be careful, as it could be called at almost any time */ |
|
|
631 | /* during execution of a perl program */ |
592 | static void NOINLINE |
632 | static void NOINLINE |
593 | prepare_cctx (pTHX_ coro_cctx *cctx) |
633 | prepare_cctx (pTHX_ coro_cctx *cctx) |
594 | { |
634 | { |
595 | dSP; |
635 | dSP; |
596 | LOGOP myop; |
636 | LOGOP myop; |
… | |
… | |
607 | PL_op = (OP *)&myop; |
647 | PL_op = (OP *)&myop; |
608 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
648 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
609 | SPAGAIN; |
649 | SPAGAIN; |
610 | } |
650 | } |
611 | |
651 | |
|
|
652 | /* |
|
|
653 | * this is a _very_ stripped down perl interpreter ;) |
|
|
654 | */ |
612 | static void |
655 | static void |
613 | coro_run (void *arg) |
656 | coro_run (void *arg) |
614 | { |
657 | { |
615 | dTHX; |
658 | dTHX; |
616 | |
659 | |
617 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
660 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
618 | UNLOCK; |
661 | UNLOCK; |
619 | |
662 | |
620 | /* |
|
|
621 | * this is a _very_ stripped down perl interpreter ;) |
|
|
622 | */ |
|
|
623 | PL_top_env = &PL_start_env; |
663 | PL_top_env = &PL_start_env; |
624 | |
664 | |
625 | /* inject call to cctx_init */ |
665 | /* inject a fake subroutine call to cctx_init */ |
626 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
666 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
627 | |
667 | |
628 | /* somebody will hit me for both perl_run and PL_restartop */ |
668 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
629 | PL_restartop = PL_op; |
669 | PL_restartop = PL_op; |
630 | perl_run (PL_curinterp); |
670 | perl_run (PL_curinterp); |
631 | |
671 | |
632 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
672 | /* |
633 | abort (); |
673 | * If perl-run returns we assume exit() was being called or the coro |
|
|
674 | * fell off the end, which seems to be the only valid (non-bug) |
|
|
675 | * reason for perl_run to return. We try to exit by jumping to the |
|
|
676 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
|
|
677 | * coroutine as Coro has no such concept |
|
|
678 | */ |
|
|
679 | PL_top_env = main_top_env; |
|
|
680 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
634 | } |
681 | } |
635 | |
682 | |
636 | static coro_cctx * |
683 | static coro_cctx * |
637 | cctx_new () |
684 | cctx_new () |
638 | { |
685 | { |
… | |
… | |
867 | struct coro temp; |
914 | struct coro temp; |
868 | |
915 | |
869 | assert (!(coro->flags & CF_RUNNING)); |
916 | assert (!(coro->flags & CF_RUNNING)); |
870 | |
917 | |
871 | Zero (&temp, 1, struct coro); |
918 | Zero (&temp, 1, struct coro); |
872 | temp.save = CORO_SAVE_DEF; |
919 | temp.save = CORO_SAVE_ALL; |
873 | |
920 | |
874 | if (coro->flags & CF_RUNNING) |
921 | if (coro->flags & CF_RUNNING) |
875 | croak ("FATAL: tried to destroy currently running coroutine"); |
922 | croak ("FATAL: tried to destroy currently running coroutine"); |
876 | |
923 | |
877 | save_perl (aTHX_ &temp); |
924 | save_perl (aTHX_ &temp); |
… | |
… | |
885 | } |
932 | } |
886 | |
933 | |
887 | cctx_destroy (coro->cctx); |
934 | cctx_destroy (coro->cctx); |
888 | SvREFCNT_dec (coro->args); |
935 | SvREFCNT_dec (coro->args); |
889 | |
936 | |
|
|
937 | if (coro->next) coro->next->prev = coro->prev; |
|
|
938 | if (coro->prev) coro->prev->next = coro->next; |
|
|
939 | if (coro == coro_first) coro_first = coro->next; |
|
|
940 | |
890 | return 1; |
941 | return 1; |
891 | } |
942 | } |
892 | |
943 | |
893 | static int |
944 | static int |
894 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
945 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
895 | { |
946 | { |
896 | struct coro *coro = (struct coro *)mg->mg_ptr; |
947 | struct coro *coro = (struct coro *)mg->mg_ptr; |
897 | mg->mg_ptr = 0; |
948 | mg->mg_ptr = 0; |
|
|
949 | |
|
|
950 | coro->hv = 0; |
898 | |
951 | |
899 | if (--coro->refcnt < 0) |
952 | if (--coro->refcnt < 0) |
900 | { |
953 | { |
901 | coro_state_destroy (aTHX_ coro); |
954 | coro_state_destroy (aTHX_ coro); |
902 | Safefree (coro); |
955 | Safefree (coro); |
… | |
… | |
1175 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
1228 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
1176 | newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); |
1229 | newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); |
1177 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1230 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1178 | |
1231 | |
1179 | main_mainstack = PL_mainstack; |
1232 | main_mainstack = PL_mainstack; |
|
|
1233 | main_top_env = PL_top_env; |
|
|
1234 | |
|
|
1235 | while (main_top_env->je_prev) |
|
|
1236 | main_top_env = main_top_env->je_prev; |
1180 | |
1237 | |
1181 | coroapi.ver = CORO_API_VERSION; |
1238 | coroapi.ver = CORO_API_VERSION; |
1182 | coroapi.transfer = api_transfer; |
1239 | coroapi.transfer = api_transfer; |
1183 | |
1240 | |
1184 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1241 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
… | |
… | |
1195 | Newz (0, coro, 1, struct coro); |
1252 | Newz (0, coro, 1, struct coro); |
1196 | coro->args = newAV (); |
1253 | coro->args = newAV (); |
1197 | coro->save = CORO_SAVE_DEF; |
1254 | coro->save = CORO_SAVE_DEF; |
1198 | coro->flags = CF_NEW; |
1255 | coro->flags = CF_NEW; |
1199 | |
1256 | |
|
|
1257 | if (coro_first) coro_first->prev = coro; |
|
|
1258 | coro->next = coro_first; |
|
|
1259 | coro_first = coro; |
|
|
1260 | |
1200 | hv = newHV (); |
1261 | coro->hv = hv = newHV (); |
1201 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1262 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1202 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1263 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1203 | |
1264 | |
1204 | for (i = 1; i < items; i++) |
1265 | for (i = 1; i < items; i++) |
1205 | av_push (coro->args, newSVsv (ST (i))); |
1266 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1307 | CODE: |
1368 | CODE: |
1308 | RETVAL = cctx_idle; |
1369 | RETVAL = cctx_idle; |
1309 | OUTPUT: |
1370 | OUTPUT: |
1310 | RETVAL |
1371 | RETVAL |
1311 | |
1372 | |
|
|
1373 | void |
|
|
1374 | list () |
|
|
1375 | PPCODE: |
|
|
1376 | { |
|
|
1377 | struct coro *coro; |
|
|
1378 | for (coro = coro_first; coro; coro = coro->next) |
|
|
1379 | if (coro->hv) |
|
|
1380 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
|
|
1381 | } |
|
|
1382 | |
|
|
1383 | void |
|
|
1384 | _eval (Coro::State coro, SV *coderef) |
|
|
1385 | CODE: |
|
|
1386 | { |
|
|
1387 | if (coro->mainstack) |
|
|
1388 | { |
|
|
1389 | struct coro temp; |
|
|
1390 | Zero (&temp, 1, struct coro); |
|
|
1391 | temp.save = CORO_SAVE_ALL; |
|
|
1392 | |
|
|
1393 | if (!(coro->flags & CF_RUNNING)) |
|
|
1394 | { |
|
|
1395 | save_perl (aTHX_ &temp); |
|
|
1396 | load_perl (aTHX_ coro); |
|
|
1397 | } |
|
|
1398 | |
|
|
1399 | { |
|
|
1400 | dSP; |
|
|
1401 | ENTER; |
|
|
1402 | SAVETMPS; |
|
|
1403 | PUSHMARK (SP); |
|
|
1404 | PUTBACK; |
|
|
1405 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
1406 | SPAGAIN; |
|
|
1407 | FREETMPS; |
|
|
1408 | LEAVE; |
|
|
1409 | PUTBACK; |
|
|
1410 | } |
|
|
1411 | |
|
|
1412 | if (!(coro->flags & CF_RUNNING)) |
|
|
1413 | { |
|
|
1414 | save_perl (aTHX_ coro); |
|
|
1415 | load_perl (aTHX_ &temp); |
|
|
1416 | } |
|
|
1417 | } |
|
|
1418 | } |
|
|
1419 | |
|
|
1420 | SV * |
|
|
1421 | is_ready (Coro::State coro) |
|
|
1422 | PROTOTYPE: $ |
|
|
1423 | ALIAS: |
|
|
1424 | is_ready = CF_READY |
|
|
1425 | is_running = CF_RUNNING |
|
|
1426 | is_new = CF_NEW |
|
|
1427 | is_destroyed = CF_DESTROYED |
|
|
1428 | CODE: |
|
|
1429 | RETVAL = boolSV (coro->flags & ix); |
|
|
1430 | OUTPUT: |
|
|
1431 | RETVAL |
|
|
1432 | |
|
|
1433 | SV * |
|
|
1434 | has_stack (Coro::State coro) |
|
|
1435 | PROTOTYPE: $ |
|
|
1436 | CODE: |
|
|
1437 | RETVAL = boolSV (!!coro->cctx); |
|
|
1438 | OUTPUT: |
|
|
1439 | RETVAL |
|
|
1440 | |
|
|
1441 | IV |
|
|
1442 | rss (Coro::State coro) |
|
|
1443 | PROTOTYPE: $ |
|
|
1444 | CODE: |
|
|
1445 | RETVAL = coro_rss (coro); |
|
|
1446 | OUTPUT: |
|
|
1447 | RETVAL |
|
|
1448 | |
|
|
1449 | |
1312 | MODULE = Coro::State PACKAGE = Coro |
1450 | MODULE = Coro::State PACKAGE = Coro |
1313 | |
1451 | |
1314 | BOOT: |
1452 | BOOT: |
1315 | { |
1453 | { |
1316 | int i; |
1454 | int i; |
|
|
1455 | |
|
|
1456 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
|
|
1457 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
|
|
1458 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
|
|
1459 | |
|
|
1460 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1461 | SvREADONLY_on (coro_current); |
1317 | |
1462 | |
1318 | coro_stash = gv_stashpv ("Coro", TRUE); |
1463 | coro_stash = gv_stashpv ("Coro", TRUE); |
1319 | |
1464 | |
1320 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1465 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1321 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1466 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1322 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1467 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1323 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1468 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1324 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1469 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1325 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1470 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1326 | |
|
|
1327 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1328 | SvREADONLY_on (coro_current); |
|
|
1329 | |
1471 | |
1330 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1472 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1331 | coro_ready[i] = newAV (); |
1473 | coro_ready[i] = newAV (); |
1332 | |
1474 | |
1333 | { |
1475 | { |
… | |
… | |
1383 | CODE: |
1525 | CODE: |
1384 | RETVAL = boolSV (api_ready (self)); |
1526 | RETVAL = boolSV (api_ready (self)); |
1385 | OUTPUT: |
1527 | OUTPUT: |
1386 | RETVAL |
1528 | RETVAL |
1387 | |
1529 | |
1388 | SV * |
|
|
1389 | is_ready (SV *self) |
|
|
1390 | PROTOTYPE: $ |
|
|
1391 | CODE: |
|
|
1392 | RETVAL = boolSV (api_is_ready (self)); |
|
|
1393 | OUTPUT: |
|
|
1394 | RETVAL |
|
|
1395 | |
|
|
1396 | int |
1530 | int |
1397 | nready (...) |
1531 | nready (...) |
1398 | PROTOTYPE: |
1532 | PROTOTYPE: |
1399 | CODE: |
1533 | CODE: |
1400 | RETVAL = coro_nready; |
1534 | RETVAL = coro_nready; |
1401 | OUTPUT: |
1535 | OUTPUT: |
1402 | RETVAL |
1536 | RETVAL |
1403 | |
1537 | |
|
|
1538 | # for async_pool speedup |
|
|
1539 | void |
|
|
1540 | _pool_1 (SV *cb) |
|
|
1541 | CODE: |
|
|
1542 | { |
|
|
1543 | int i, len; |
|
|
1544 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1545 | AV *defav = GvAV (PL_defgv); |
|
|
1546 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
1547 | AV *invoke_av; |
|
|
1548 | |
|
|
1549 | if (!invoke) |
|
|
1550 | croak ("\3terminate\2\n"); |
|
|
1551 | |
|
|
1552 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1553 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1554 | |
|
|
1555 | invoke_av = (AV *)SvRV (invoke); |
|
|
1556 | len = av_len (invoke_av); |
|
|
1557 | |
|
|
1558 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
1559 | |
|
|
1560 | if (len > 0) |
|
|
1561 | { |
|
|
1562 | av_fill (defav, len - 1); |
|
|
1563 | for (i = 0; i < len; ++i) |
|
|
1564 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
|
|
1565 | } |
|
|
1566 | |
|
|
1567 | SvREFCNT_dec (invoke); |
|
|
1568 | } |
|
|
1569 | |
|
|
1570 | void |
|
|
1571 | _pool_2 (SV *cb) |
|
|
1572 | CODE: |
|
|
1573 | { |
|
|
1574 | struct coro *coro = SvSTATE (coro_current); |
|
|
1575 | |
|
|
1576 | sv_setsv (cb, &PL_sv_undef); |
|
|
1577 | |
|
|
1578 | if (coro_rss (coro) > SvIV (sv_pool_rss) |
|
|
1579 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1580 | croak ("\3terminate\2\n"); |
|
|
1581 | |
|
|
1582 | av_clear (GvAV (PL_defgv)); |
|
|
1583 | hv_store (SvRV (coro_current), "desc", sizeof ("desc") - 1, |
|
|
1584 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
1585 | coro->save = CORO_SAVE_DEF; |
|
|
1586 | coro->prio = 0; |
|
|
1587 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
1588 | } |
|
|
1589 | |
|
|
1590 | |
1404 | MODULE = Coro::State PACKAGE = Coro::AIO |
1591 | MODULE = Coro::State PACKAGE = Coro::AIO |
1405 | |
1592 | |
1406 | SV * |
1593 | SV * |
1407 | _get_state () |
1594 | _get_state () |
1408 | CODE: |
1595 | CODE: |