1 | #include "libcoro/coro.c" |
1 | #include "libcoro/coro.c" |
2 | |
2 | |
3 | #define PERL_NO_GET_CONTEXT |
3 | #define PERL_NO_GET_CONTEXT |
|
|
4 | #define PERL_EXT |
4 | |
5 | |
5 | #include "EXTERN.h" |
6 | #include "EXTERN.h" |
6 | #include "perl.h" |
7 | #include "perl.h" |
7 | #include "XSUB.h" |
8 | #include "XSUB.h" |
8 | |
9 | |
… | |
… | |
192 | CF_READY = 0x0002, /* coroutine is ready */ |
193 | CF_READY = 0x0002, /* coroutine is ready */ |
193 | CF_NEW = 0x0004, /* has never been switched to */ |
194 | CF_NEW = 0x0004, /* has never been switched to */ |
194 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
195 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
195 | }; |
196 | }; |
196 | |
197 | |
|
|
198 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
|
|
199 | typedef struct { |
|
|
200 | SV *defsv; |
|
|
201 | AV *defav; |
|
|
202 | SV *errsv; |
|
|
203 | SV *irsgv; |
|
|
204 | #define VAR(name,type) type name; |
|
|
205 | # include "state.h" |
|
|
206 | #undef VAR |
|
|
207 | } perl_slots; |
|
|
208 | |
|
|
209 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
|
|
210 | |
197 | /* this is a structure representing a perl-level coroutine */ |
211 | /* this is a structure representing a perl-level coroutine */ |
198 | struct coro { |
212 | struct coro { |
199 | /* the c coroutine allocated to this perl coroutine, if any */ |
213 | /* the c coroutine allocated to this perl coroutine, if any */ |
200 | coro_cctx *cctx; |
214 | coro_cctx *cctx; |
201 | |
215 | |
|
|
216 | /* process data */ |
|
|
217 | AV *mainstack; |
|
|
218 | perl_slots *slot; /* basically the saved sp */ |
|
|
219 | |
202 | /* data associated with this coroutine (initial args) */ |
220 | /* data associated with this coroutine (initial args) */ |
203 | AV *args; |
221 | AV *args; |
204 | int refcnt; |
222 | int refcnt; |
205 | int flags; /* CF_ flags */ |
223 | int flags; /* CF_ flags */ |
206 | |
224 | |
207 | /* optionally saved, might be zero */ |
|
|
208 | AV *defav; /* @_ */ |
|
|
209 | SV *defsv; /* $_ */ |
|
|
210 | SV *errsv; /* $@ */ |
|
|
211 | SV *deffh; /* default filehandle */ |
|
|
212 | SV *irssv; /* $/ */ |
|
|
213 | SV *irssv_sv; /* real $/ cache */ |
|
|
214 | |
|
|
215 | #define VAR(name,type) type name; |
|
|
216 | # include "state.h" |
|
|
217 | #undef VAR |
|
|
218 | |
|
|
219 | /* statistics */ |
225 | /* statistics */ |
220 | int usecount; /* number of transfers to this coro */ |
226 | int usecount; /* number of transfers to this coro */ |
221 | |
227 | |
222 | /* coro process data */ |
228 | /* coro process data */ |
223 | int prio; |
229 | int prio; |
224 | //SV *throw; |
230 | SV *throw; |
225 | |
231 | |
226 | /* async_pool */ |
232 | /* async_pool */ |
227 | SV *saved_deffh; |
233 | SV *saved_deffh; |
228 | |
234 | |
229 | /* linked list */ |
235 | /* linked list */ |
… | |
… | |
397 | /** load & save, init *******************************************************/ |
403 | /** load & save, init *******************************************************/ |
398 | |
404 | |
399 | static void |
405 | static void |
400 | load_perl (pTHX_ Coro__State c) |
406 | load_perl (pTHX_ Coro__State c) |
401 | { |
407 | { |
|
|
408 | perl_slots *slot = c->slot; |
|
|
409 | c->slot = 0; |
|
|
410 | |
|
|
411 | PL_mainstack = c->mainstack; |
|
|
412 | |
|
|
413 | GvSV (PL_defgv) = slot->defsv; |
|
|
414 | GvAV (PL_defgv) = slot->defav; |
|
|
415 | GvSV (PL_errgv) = slot->errsv; |
|
|
416 | GvSV (irsgv) = slot->irsgv; |
|
|
417 | |
402 | #define VAR(name,type) PL_ ## name = c->name; |
418 | #define VAR(name,type) PL_ ## name = slot->name; |
403 | # include "state.h" |
419 | # include "state.h" |
404 | #undef VAR |
420 | #undef VAR |
405 | |
|
|
406 | GvSV (PL_defgv) = c->defsv; |
|
|
407 | GvAV (PL_defgv) = c->defav; |
|
|
408 | GvSV (PL_errgv) = c->errsv; |
|
|
409 | GvSV (irsgv) = c->irssv_sv; |
|
|
410 | |
421 | |
411 | { |
422 | { |
412 | dSP; |
423 | dSP; |
|
|
424 | |
413 | CV *cv; |
425 | CV *cv; |
414 | |
426 | |
415 | /* now do the ugly restore mess */ |
427 | /* now do the ugly restore mess */ |
416 | while (expect_true (cv = (CV *)POPs)) |
428 | while (expect_true (cv = (CV *)POPs)) |
417 | { |
429 | { |
… | |
… | |
472 | } |
484 | } |
473 | |
485 | |
474 | PUTBACK; |
486 | PUTBACK; |
475 | } |
487 | } |
476 | |
488 | |
|
|
489 | /* allocate some space on the context stack for our purposes */ |
|
|
490 | { |
|
|
491 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
492 | int i; |
|
|
493 | if (SLOT_COUNT >= 1) CXINC; |
|
|
494 | if (SLOT_COUNT >= 2) CXINC; |
|
|
495 | if (SLOT_COUNT >= 3) CXINC; |
|
|
496 | for (i = 3; i < SLOT_COUNT; ++i) |
|
|
497 | CXINC; |
|
|
498 | |
|
|
499 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
|
|
500 | } |
|
|
501 | |
|
|
502 | c->mainstack = PL_mainstack; |
|
|
503 | |
|
|
504 | { |
|
|
505 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
|
|
506 | |
477 | c->defav = GvAV (PL_defgv); |
507 | slot->defav = GvAV (PL_defgv); |
478 | c->defsv = DEFSV; |
508 | slot->defsv = DEFSV; |
479 | c->errsv = ERRSV; |
509 | slot->errsv = ERRSV; |
480 | c->irssv_sv = GvSV (irsgv); |
510 | slot->irsgv = GvSV (irsgv); |
481 | |
511 | |
482 | #define VAR(name,type)c->name = PL_ ## name; |
512 | #define VAR(name,type) slot->name = PL_ ## name; |
483 | # include "state.h" |
513 | # include "state.h" |
484 | #undef VAR |
514 | #undef VAR |
|
|
515 | } |
485 | } |
516 | } |
486 | |
517 | |
487 | /* |
518 | /* |
488 | * allocate various perl stacks. This is an exact copy |
519 | * allocate various perl stacks. This is an exact copy |
489 | * of perl.c:init_stacks, except that it uses less memory |
520 | * of perl.c:init_stacks, except that it uses less memory |
… | |
… | |
494 | # define coro_init_stacks init_stacks |
525 | # define coro_init_stacks init_stacks |
495 | #else |
526 | #else |
496 | static void |
527 | static void |
497 | coro_init_stacks (pTHX) |
528 | coro_init_stacks (pTHX) |
498 | { |
529 | { |
499 | PL_curstackinfo = new_stackinfo(64, 6); |
530 | PL_curstackinfo = new_stackinfo(32, 8); |
500 | PL_curstackinfo->si_type = PERLSI_MAIN; |
531 | PL_curstackinfo->si_type = PERLSI_MAIN; |
501 | PL_curstack = PL_curstackinfo->si_stack; |
532 | PL_curstack = PL_curstackinfo->si_stack; |
502 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
533 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
503 | |
534 | |
504 | PL_stack_base = AvARRAY(PL_curstack); |
535 | PL_stack_base = AvARRAY(PL_curstack); |
505 | PL_stack_sp = PL_stack_base; |
536 | PL_stack_sp = PL_stack_base; |
506 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
537 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
507 | |
538 | |
508 | New(50,PL_tmps_stack,64,SV*); |
539 | New(50,PL_tmps_stack,32,SV*); |
509 | PL_tmps_floor = -1; |
540 | PL_tmps_floor = -1; |
510 | PL_tmps_ix = -1; |
541 | PL_tmps_ix = -1; |
511 | PL_tmps_max = 64; |
542 | PL_tmps_max = 32; |
512 | |
543 | |
513 | New(54,PL_markstack,16,I32); |
544 | New(54,PL_markstack,16,I32); |
514 | PL_markstack_ptr = PL_markstack; |
545 | PL_markstack_ptr = PL_markstack; |
515 | PL_markstack_max = PL_markstack + 16; |
546 | PL_markstack_max = PL_markstack + 16; |
516 | |
547 | |
517 | #ifdef SET_MARK_OFFSET |
548 | #ifdef SET_MARK_OFFSET |
518 | SET_MARK_OFFSET; |
549 | SET_MARK_OFFSET; |
519 | #endif |
550 | #endif |
520 | |
551 | |
521 | New(54,PL_scopestack,16,I32); |
552 | New(54,PL_scopestack,8,I32); |
522 | PL_scopestack_ix = 0; |
553 | PL_scopestack_ix = 0; |
523 | PL_scopestack_max = 16; |
554 | PL_scopestack_max = 8; |
524 | |
555 | |
525 | New(54,PL_savestack,64,ANY); |
556 | New(54,PL_savestack,24,ANY); |
526 | PL_savestack_ix = 0; |
557 | PL_savestack_ix = 0; |
527 | PL_savestack_max = 64; |
558 | PL_savestack_max = 24; |
528 | |
559 | |
529 | #if !PERL_VERSION_ATLEAST (5,9,0) |
560 | #if !PERL_VERSION_ATLEAST (5,9,0) |
530 | New(54,PL_retstack,4,OP*); |
561 | New(54,PL_retstack,4,OP*); |
531 | PL_retstack_ix = 0; |
562 | PL_retstack_ix = 0; |
532 | PL_retstack_max = 4; |
563 | PL_retstack_max = 4; |
… | |
… | |
569 | { |
600 | { |
570 | size_t rss = sizeof (*coro); |
601 | size_t rss = sizeof (*coro); |
571 | |
602 | |
572 | if (coro->mainstack) |
603 | if (coro->mainstack) |
573 | { |
604 | { |
|
|
605 | perl_slots tmp_slot; |
|
|
606 | perl_slots *slot; |
|
|
607 | |
574 | if (coro->flags & CF_RUNNING) |
608 | if (coro->flags & CF_RUNNING) |
575 | { |
609 | { |
|
|
610 | slot = &tmp_slot; |
|
|
611 | |
576 | #define VAR(name,type)coro->name = PL_ ## name; |
612 | #define VAR(name,type) slot->name = PL_ ## name; |
577 | # include "state.h" |
613 | # include "state.h" |
578 | #undef VAR |
614 | #undef VAR |
579 | } |
615 | } |
|
|
616 | else |
|
|
617 | slot = coro->slot; |
580 | |
618 | |
581 | rss += sizeof (coro->curstackinfo); |
619 | rss += sizeof (slot->curstackinfo); |
582 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
|
|
583 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
620 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
584 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
621 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
585 | rss += coro->tmps_max * sizeof (SV *); |
622 | rss += slot->tmps_max * sizeof (SV *); |
586 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
623 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
587 | rss += coro->scopestack_max * sizeof (I32); |
624 | rss += slot->scopestack_max * sizeof (I32); |
588 | rss += coro->savestack_max * sizeof (ANY); |
625 | rss += slot->savestack_max * sizeof (ANY); |
589 | |
626 | |
590 | #if !PERL_VERSION_ATLEAST (5,9,0) |
627 | #if !PERL_VERSION_ATLEAST (5,9,0) |
591 | rss += coro->retstack_max * sizeof (OP *); |
628 | rss += slot->retstack_max * sizeof (OP *); |
592 | #endif |
629 | #endif |
593 | } |
630 | } |
594 | |
631 | |
595 | return rss; |
632 | return rss; |
596 | } |
633 | } |
… | |
… | |
608 | PL_runops = RUNOPS_DEFAULT; |
645 | PL_runops = RUNOPS_DEFAULT; |
609 | PL_curcop = &PL_compiling; |
646 | PL_curcop = &PL_compiling; |
610 | PL_in_eval = EVAL_NULL; |
647 | PL_in_eval = EVAL_NULL; |
611 | PL_comppad = 0; |
648 | PL_comppad = 0; |
612 | PL_curpm = 0; |
649 | PL_curpm = 0; |
613 | PL_curpad = 1; |
650 | PL_curpad = 0; |
614 | PL_localizing = 0; |
651 | PL_localizing = 0; |
615 | PL_dirty = 0; |
652 | PL_dirty = 0; |
616 | PL_restartop = 0; |
653 | PL_restartop = 0; |
|
|
654 | PL_diehook = 0; |
|
|
655 | PL_warnhook = 0; |
617 | |
656 | |
618 | GvSV (PL_defgv) = newSV (0); |
657 | GvSV (PL_defgv) = newSV (0); |
619 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
658 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
620 | GvSV (PL_errgv) = newSV (0); |
659 | GvSV (PL_errgv) = newSV (0); |
621 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
660 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
622 | PL_rs = newSVsv (GvSV (irsgv)); |
661 | PL_rs = newSVsv (GvSV (irsgv)); |
623 | PL_defoutgv = SvREFCNT_inc (stdoutgv); |
662 | PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); |
624 | |
663 | |
625 | { |
664 | { |
626 | dSP; |
665 | dSP; |
627 | LOGOP myop; |
666 | LOGOP myop; |
628 | |
667 | |
… | |
… | |
666 | SvREFCNT_dec (GvSV (PL_errgv)); |
705 | SvREFCNT_dec (GvSV (PL_errgv)); |
667 | SvREFCNT_dec (PL_defoutgv); |
706 | SvREFCNT_dec (PL_defoutgv); |
668 | SvREFCNT_dec (PL_rs); |
707 | SvREFCNT_dec (PL_rs); |
669 | SvREFCNT_dec (GvSV (irsgv)); |
708 | SvREFCNT_dec (GvSV (irsgv)); |
670 | |
709 | |
|
|
710 | SvREFCNT_dec (PL_diehook); |
|
|
711 | SvREFCNT_dec (PL_warnhook); |
|
|
712 | |
671 | SvREFCNT_dec (coro->saved_deffh); |
713 | SvREFCNT_dec (coro->saved_deffh); |
672 | //SvREFCNT_dec (coro->throw); |
714 | SvREFCNT_dec (coro->throw); |
673 | |
715 | |
674 | coro_destroy_stacks (aTHX); |
716 | coro_destroy_stacks (aTHX); |
675 | } |
717 | } |
676 | |
718 | |
677 | static void |
719 | static void |
… | |
… | |
991 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
1033 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
992 | |
1034 | |
993 | if (expect_false (next->flags & CF_DESTROYED)) |
1035 | if (expect_false (next->flags & CF_DESTROYED)) |
994 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
1036 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
995 | |
1037 | |
|
|
1038 | if ( |
|
|
1039 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
1040 | expect_false (PL_parser) |
|
|
1041 | #else |
996 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1042 | expect_false (PL_lex_state != LEX_NOTPARSING) |
|
|
1043 | #endif |
|
|
1044 | ) |
997 | croak ("Coro::State::transfer called while parsing, but this is not supported"); |
1045 | croak ("Coro::State::transfer called while parsing, but this is not supported"); |
998 | } |
1046 | } |
999 | } |
1047 | } |
1000 | |
1048 | |
1001 | /* always use the TRANSFER macro */ |
1049 | /* always use the TRANSFER macro */ |
… | |
… | |
1073 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1121 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1074 | } |
1122 | } |
1075 | |
1123 | |
1076 | free_coro_mortal (aTHX); |
1124 | free_coro_mortal (aTHX); |
1077 | UNLOCK; |
1125 | UNLOCK; |
|
|
1126 | |
|
|
1127 | if (expect_false (prev->throw || next->throw)) |
|
|
1128 | { |
|
|
1129 | struct coro *coro = SvSTATE (coro_current); |
|
|
1130 | |
|
|
1131 | if (coro->throw) |
|
|
1132 | { |
|
|
1133 | SV *exception = coro->throw; |
|
|
1134 | coro->throw = 0; |
|
|
1135 | sv_setsv (ERRSV, exception); |
|
|
1136 | croak (0); |
|
|
1137 | } |
|
|
1138 | } |
1078 | } |
1139 | } |
1079 | } |
1140 | } |
1080 | |
1141 | |
1081 | struct transfer_args |
1142 | struct transfer_args |
1082 | { |
1143 | { |
… | |
… | |
1117 | save_perl (aTHX_ &temp); |
1178 | save_perl (aTHX_ &temp); |
1118 | load_perl (aTHX_ coro); |
1179 | load_perl (aTHX_ coro); |
1119 | |
1180 | |
1120 | coro_destroy (aTHX_ coro); |
1181 | coro_destroy (aTHX_ coro); |
1121 | |
1182 | |
1122 | load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ |
1183 | load_perl (aTHX_ &temp); |
1123 | |
1184 | |
1124 | coro->mainstack = 0; |
1185 | coro->slot = 0; |
1125 | } |
1186 | } |
1126 | |
1187 | |
1127 | cctx_destroy (coro->cctx); |
1188 | cctx_destroy (coro->cctx); |
1128 | SvREFCNT_dec (coro->args); |
1189 | SvREFCNT_dec (coro->args); |
1129 | |
1190 | |
… | |
… | |
1386 | coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); |
1447 | coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); |
1387 | |
1448 | |
1388 | if (coro->flags & CF_RUNNING) |
1449 | if (coro->flags & CF_RUNNING) |
1389 | PL_runops = RUNOPS_DEFAULT; |
1450 | PL_runops = RUNOPS_DEFAULT; |
1390 | else |
1451 | else |
1391 | coro->runops = RUNOPS_DEFAULT; |
1452 | coro->slot->runops = RUNOPS_DEFAULT; |
1392 | } |
1453 | } |
1393 | } |
1454 | } |
1394 | |
1455 | |
1395 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1456 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1396 | |
1457 | |
… | |
… | |
1556 | CODE: |
1617 | CODE: |
1557 | { |
1618 | { |
1558 | if (coro->mainstack) |
1619 | if (coro->mainstack) |
1559 | { |
1620 | { |
1560 | struct coro temp; |
1621 | struct coro temp; |
1561 | Zero (&temp, 1, struct coro); |
|
|
1562 | |
1622 | |
1563 | if (!(coro->flags & CF_RUNNING)) |
1623 | if (!(coro->flags & CF_RUNNING)) |
1564 | { |
1624 | { |
1565 | save_perl (aTHX_ &temp); |
1625 | save_perl (aTHX_ &temp); |
1566 | load_perl (aTHX_ coro); |
1626 | load_perl (aTHX_ coro); |
… | |
… | |
1570 | dSP; |
1630 | dSP; |
1571 | ENTER; |
1631 | ENTER; |
1572 | SAVETMPS; |
1632 | SAVETMPS; |
1573 | PUSHMARK (SP); |
1633 | PUSHMARK (SP); |
1574 | PUTBACK; |
1634 | PUTBACK; |
|
|
1635 | |
1575 | if (ix) |
1636 | if (ix) |
1576 | eval_sv (coderef, 0); |
1637 | eval_sv (coderef, 0); |
1577 | else |
1638 | else |
1578 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1639 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
1640 | |
1579 | SPAGAIN; |
1641 | SPAGAIN; |
1580 | FREETMPS; |
1642 | FREETMPS; |
1581 | LEAVE; |
1643 | LEAVE; |
1582 | PUTBACK; |
1644 | PUTBACK; |
1583 | } |
1645 | } |
… | |
… | |
1642 | BOOT: |
1704 | BOOT: |
1643 | { |
1705 | { |
1644 | int i; |
1706 | int i; |
1645 | |
1707 | |
1646 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
1708 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
|
|
1709 | get_sv ("Coro::POOL_RSS" , TRUE); /* silence stupid 5.10 warning */ |
1647 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
1710 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
|
|
1711 | get_sv ("Coro::POOL_SIZE" , TRUE); /* silence stupid 5.10 warning */ |
1648 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
1712 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
|
|
1713 | get_av ("Coro::async_pool", TRUE); /* silence stupid 5.10 warning */ |
1649 | |
1714 | |
1650 | coro_current = get_sv ("Coro::current", FALSE); |
1715 | coro_current = get_sv ("Coro::current", FALSE); |
1651 | SvREADONLY_on (coro_current); |
1716 | SvREADONLY_on (coro_current); |
1652 | |
1717 | |
1653 | coro_stash = gv_stashpv ("Coro", TRUE); |
1718 | coro_stash = gv_stashpv ("Coro", TRUE); |
1654 | |
1719 | |
1655 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1720 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1656 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1721 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1657 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1722 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1658 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1723 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
… | |
… | |
1661 | |
1726 | |
1662 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1727 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1663 | coro_ready[i] = newAV (); |
1728 | coro_ready[i] = newAV (); |
1664 | |
1729 | |
1665 | { |
1730 | { |
1666 | SV *sv = perl_get_sv("Coro::API", 1); |
1731 | SV *sv = perl_get_sv ("Coro::API", TRUE); |
|
|
1732 | perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */ |
1667 | |
1733 | |
1668 | coroapi.schedule = api_schedule; |
1734 | coroapi.schedule = api_schedule; |
1669 | coroapi.cede = api_cede; |
1735 | coroapi.cede = api_cede; |
1670 | coroapi.cede_notself = api_cede_notself; |
1736 | coroapi.cede_notself = api_cede_notself; |
1671 | coroapi.ready = api_ready; |
1737 | coroapi.ready = api_ready; |
… | |
… | |
1722 | CODE: |
1788 | CODE: |
1723 | RETVAL = coro_nready; |
1789 | RETVAL = coro_nready; |
1724 | OUTPUT: |
1790 | OUTPUT: |
1725 | RETVAL |
1791 | RETVAL |
1726 | |
1792 | |
|
|
1793 | void |
|
|
1794 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
1795 | PROTOTYPE: $;$ |
|
|
1796 | CODE: |
|
|
1797 | SvREFCNT_dec (self->throw); |
|
|
1798 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
1799 | |
1727 | # for async_pool speedup |
1800 | # for async_pool speedup |
1728 | void |
1801 | void |
1729 | _pool_1 (SV *cb) |
1802 | _pool_1 (SV *cb) |
1730 | CODE: |
1803 | CODE: |
1731 | { |
1804 | { |
… | |
… | |
1735 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1808 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1736 | AV *invoke_av; |
1809 | AV *invoke_av; |
1737 | int i, len; |
1810 | int i, len; |
1738 | |
1811 | |
1739 | if (!invoke) |
1812 | if (!invoke) |
1740 | croak ("\3terminate\2\n"); |
1813 | croak ("\3async_pool terminate\2\n"); |
1741 | |
1814 | |
1742 | SvREFCNT_dec (coro->saved_deffh); |
1815 | SvREFCNT_dec (coro->saved_deffh); |
1743 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1816 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1744 | |
1817 | |
1745 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1818 | hv_store (hv, "desc", sizeof ("desc") - 1, |
… | |
… | |
1771 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
1844 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
1772 | coro->saved_deffh = 0; |
1845 | coro->saved_deffh = 0; |
1773 | |
1846 | |
1774 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1847 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1775 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1848 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1776 | croak ("\3terminate\2\n"); |
1849 | croak ("\3async_pool terminate\2\n"); |
1777 | |
1850 | |
1778 | av_clear (GvAV (PL_defgv)); |
1851 | av_clear (GvAV (PL_defgv)); |
1779 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1852 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1780 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1853 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1781 | |
1854 | |