… | |
… | |
132 | 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; |
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 | |
136 | |
|
|
137 | static GV *irsgv; /* $/ */ |
|
|
138 | |
137 | /* async_pool helper stuff */ |
139 | /* async_pool helper stuff */ |
138 | static SV *sv_pool_rss; |
140 | static SV *sv_pool_rss; |
139 | static SV *sv_pool_size; |
141 | static SV *sv_pool_size; |
140 | static AV *av_async_pool; |
142 | static AV *av_async_pool; |
141 | |
143 | |
… | |
… | |
184 | coro_cctx *cctx; |
186 | coro_cctx *cctx; |
185 | |
187 | |
186 | /* data associated with this coroutine (initial args) */ |
188 | /* data associated with this coroutine (initial args) */ |
187 | AV *args; |
189 | AV *args; |
188 | int refcnt; |
190 | int refcnt; |
189 | int save; /* CORO_SAVE flags */ |
|
|
190 | int flags; /* CF_ flags */ |
191 | int flags; /* CF_ flags */ |
191 | |
192 | |
192 | /* optionally saved, might be zero */ |
193 | /* optionally saved, might be zero */ |
193 | AV *defav; /* @_ */ |
194 | AV *defav; /* @_ */ |
194 | SV *defsv; /* $_ */ |
195 | SV *defsv; /* $_ */ |
195 | SV *errsv; /* $@ */ |
196 | SV *errsv; /* $@ */ |
196 | GV *deffh; /* default filehandle */ |
197 | IO *deffh; /* default filehandle */ |
197 | SV *irssv; /* $/ */ |
198 | SV *irssv; /* $/ */ |
198 | SV *irssv_sv; /* real $/ cache */ |
199 | SV *irssv_sv; /* real $/ cache */ |
199 | |
200 | |
200 | #define VAR(name,type) type name; |
201 | #define VAR(name,type) type name; |
201 | # include "state.h" |
202 | # include "state.h" |
… | |
… | |
375 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
376 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
376 | } |
377 | } |
377 | |
378 | |
378 | /** load & save, init *******************************************************/ |
379 | /** load & save, init *******************************************************/ |
379 | |
380 | |
380 | #define SB do { |
|
|
381 | #define SE } while (0) |
|
|
382 | |
|
|
383 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
|
|
384 | |
|
|
385 | static void |
381 | static void |
386 | load_perl (pTHX_ Coro__State c) |
382 | load_perl (pTHX_ Coro__State c) |
387 | { |
383 | { |
388 | #define VAR(name,type) PL_ ## name = c->name; |
384 | #define VAR(name,type) PL_ ## name = c->name; |
389 | # include "state.h" |
385 | # include "state.h" |
390 | #undef VAR |
386 | #undef VAR |
391 | |
387 | |
392 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
388 | GvSV (PL_defgv) = c->defsv; |
393 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
389 | GvAV (PL_defgv) = c->defav; |
394 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
390 | GvSV (PL_errgv) = c->errsv; |
395 | if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh); |
391 | PL_defoutgv = c->deffh; |
396 | |
392 | PL_rs = c->irssv; |
397 | if (c->irssv) |
393 | GvSV (irsgv) = c->irssv_sv; |
398 | { |
|
|
399 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
|
|
400 | { |
|
|
401 | SvREFCNT_dec (c->irssv); |
|
|
402 | c->irssv = 0; |
|
|
403 | } |
|
|
404 | else |
|
|
405 | { |
|
|
406 | REPLACE_SV (PL_rs, c->irssv); |
|
|
407 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
|
|
408 | sv_setsv (c->irssv_sv, PL_rs); |
|
|
409 | } |
|
|
410 | } |
|
|
411 | |
394 | |
412 | { |
395 | { |
413 | dSP; |
396 | dSP; |
414 | CV *cv; |
397 | CV *cv; |
415 | |
398 | |
… | |
… | |
473 | } |
456 | } |
474 | |
457 | |
475 | PUTBACK; |
458 | PUTBACK; |
476 | } |
459 | } |
477 | |
460 | |
478 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
461 | c->defav = GvAV (PL_defgv); |
479 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
462 | c->defsv = DEFSV; |
480 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
463 | c->errsv = ERRSV; |
481 | c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0; |
464 | c->deffh = PL_defoutgv; |
482 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
465 | c->irssv = PL_rs; |
|
|
466 | c->irssv_sv = GvSV (irsgv); |
483 | |
467 | |
484 | #define VAR(name,type)c->name = PL_ ## name; |
468 | #define VAR(name,type)c->name = PL_ ## name; |
485 | # include "state.h" |
469 | # include "state.h" |
486 | #undef VAR |
470 | #undef VAR |
487 | } |
471 | } |
… | |
… | |
540 | * destroy the stacks, the callchain etc... |
524 | * destroy the stacks, the callchain etc... |
541 | */ |
525 | */ |
542 | static void |
526 | static void |
543 | coro_destroy_stacks (pTHX) |
527 | coro_destroy_stacks (pTHX) |
544 | { |
528 | { |
545 | if (!IN_DESTRUCT) |
|
|
546 | { |
|
|
547 | /* restore all saved variables and stuff */ |
|
|
548 | LEAVE_SCOPE (0); |
|
|
549 | assert (PL_tmps_floor == -1); |
|
|
550 | |
|
|
551 | /* free all temporaries */ |
|
|
552 | FREETMPS; |
|
|
553 | assert (PL_tmps_ix == -1); |
|
|
554 | |
|
|
555 | /* unwind all extra stacks */ |
|
|
556 | POPSTACK_TO (PL_mainstack); |
|
|
557 | |
|
|
558 | /* unwind main stack */ |
|
|
559 | dounwind (-1); |
|
|
560 | } |
|
|
561 | |
|
|
562 | while (PL_curstackinfo->si_next) |
529 | while (PL_curstackinfo->si_next) |
563 | PL_curstackinfo = PL_curstackinfo->si_next; |
530 | PL_curstackinfo = PL_curstackinfo->si_next; |
564 | |
531 | |
565 | while (PL_curstackinfo) |
532 | while (PL_curstackinfo) |
566 | { |
533 | { |
… | |
… | |
615 | } |
582 | } |
616 | |
583 | |
617 | /** coroutine stack handling ************************************************/ |
584 | /** coroutine stack handling ************************************************/ |
618 | |
585 | |
619 | static void |
586 | static void |
620 | setup_coro (pTHX_ struct coro *coro) |
587 | coro_setup (pTHX_ struct coro *coro) |
621 | { |
588 | { |
622 | /* |
589 | /* |
623 | * emulate part of the perl startup here. |
590 | * emulate part of the perl startup here. |
624 | */ |
591 | */ |
625 | coro_init_stacks (aTHX); |
592 | coro_init_stacks (aTHX); |
… | |
… | |
630 | PL_comppad = 0; |
597 | PL_comppad = 0; |
631 | PL_curpm = 0; |
598 | PL_curpm = 0; |
632 | PL_localizing = 0; |
599 | PL_localizing = 0; |
633 | PL_dirty = 0; |
600 | PL_dirty = 0; |
634 | PL_restartop = 0; |
601 | PL_restartop = 0; |
|
|
602 | |
|
|
603 | GvSV (PL_defgv) = NEWSV (0, 0); |
|
|
604 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
|
|
605 | GvSV (PL_errgv) = NEWSV (0, 0); |
|
|
606 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
|
|
607 | PL_rs = newSVsv (GvSV (irsgv)); |
|
|
608 | |
|
|
609 | { |
|
|
610 | IO *io = newIO (); |
|
|
611 | PL_defoutgv = newGVgen ("Coro"); |
|
|
612 | GvIOp(PL_defoutgv) = io; |
|
|
613 | IoTYPE (io) = IoTYPE_WRONLY; |
|
|
614 | IoOFP (io) = IoIFP (io) = PerlIO_stdout (); |
|
|
615 | IoFLAGS (io) |= IOf_FLUSH; |
|
|
616 | } |
635 | |
617 | |
636 | { |
618 | { |
637 | dSP; |
619 | dSP; |
638 | LOGOP myop; |
620 | LOGOP myop; |
639 | |
|
|
640 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
641 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
|
|
642 | |
621 | |
643 | Zero (&myop, 1, LOGOP); |
622 | Zero (&myop, 1, LOGOP); |
644 | myop.op_next = Nullop; |
623 | myop.op_next = Nullop; |
645 | myop.op_flags = OPf_WANT_VOID; |
624 | myop.op_flags = OPf_WANT_VOID; |
646 | |
625 | |
… | |
… | |
651 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
630 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
652 | SPAGAIN; |
631 | SPAGAIN; |
653 | } |
632 | } |
654 | |
633 | |
655 | ENTER; /* necessary e.g. for dounwind */ |
634 | ENTER; /* necessary e.g. for dounwind */ |
|
|
635 | } |
|
|
636 | |
|
|
637 | static void |
|
|
638 | coro_destroy (pTHX_ struct coro *coro) |
|
|
639 | { |
|
|
640 | if (!IN_DESTRUCT) |
|
|
641 | { |
|
|
642 | /* restore all saved variables and stuff */ |
|
|
643 | LEAVE_SCOPE (0); |
|
|
644 | assert (PL_tmps_floor == -1); |
|
|
645 | |
|
|
646 | /* free all temporaries */ |
|
|
647 | FREETMPS; |
|
|
648 | assert (PL_tmps_ix == -1); |
|
|
649 | |
|
|
650 | /* unwind all extra stacks */ |
|
|
651 | POPSTACK_TO (PL_mainstack); |
|
|
652 | |
|
|
653 | /* unwind main stack */ |
|
|
654 | dounwind (-1); |
|
|
655 | } |
|
|
656 | |
|
|
657 | SvREFCNT_dec (GvSV (PL_defgv)); |
|
|
658 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
659 | SvREFCNT_dec (GvSV (PL_errgv)); |
|
|
660 | SvREFCNT_dec (PL_defoutgv); |
|
|
661 | SvREFCNT_dec (PL_rs); |
|
|
662 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
663 | |
|
|
664 | coro_destroy_stacks (aTHX); |
656 | } |
665 | } |
657 | |
666 | |
658 | static void |
667 | static void |
659 | free_coro_mortal (pTHX) |
668 | free_coro_mortal (pTHX) |
660 | { |
669 | { |
… | |
… | |
1009 | /* need to start coroutine */ |
1018 | /* need to start coroutine */ |
1010 | next->flags &= ~CF_NEW; |
1019 | next->flags &= ~CF_NEW; |
1011 | /* first get rid of the old state */ |
1020 | /* first get rid of the old state */ |
1012 | save_perl (aTHX_ prev); |
1021 | save_perl (aTHX_ prev); |
1013 | /* setup coroutine call */ |
1022 | /* setup coroutine call */ |
1014 | setup_coro (aTHX_ next); |
1023 | coro_setup (aTHX_ next); |
1015 | } |
1024 | } |
1016 | else |
1025 | else |
1017 | { |
1026 | { |
1018 | /* coroutine already started */ |
1027 | /* coroutine already started */ |
1019 | save_perl (aTHX_ prev); |
1028 | save_perl (aTHX_ prev); |
… | |
… | |
1081 | |
1090 | |
1082 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1091 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1083 | { |
1092 | { |
1084 | struct coro temp; |
1093 | struct coro temp; |
1085 | |
1094 | |
1086 | assert (!(coro->flags & CF_RUNNING)); |
|
|
1087 | |
|
|
1088 | Zero (&temp, 1, struct coro); |
|
|
1089 | temp.save = CORO_SAVE_ALL; |
|
|
1090 | |
|
|
1091 | if (coro->flags & CF_RUNNING) |
1095 | if (coro->flags & CF_RUNNING) |
1092 | croak ("FATAL: tried to destroy currently running coroutine"); |
1096 | croak ("FATAL: tried to destroy currently running coroutine"); |
1093 | |
1097 | |
1094 | save_perl (aTHX_ &temp); |
1098 | save_perl (aTHX_ &temp); |
1095 | load_perl (aTHX_ coro); |
1099 | load_perl (aTHX_ coro); |
1096 | |
1100 | |
1097 | coro_destroy_stacks (aTHX); |
1101 | coro_destroy (aTHX_ coro); |
1098 | |
1102 | |
1099 | load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ |
1103 | load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ |
1100 | |
1104 | |
1101 | coro->mainstack = 0; |
1105 | coro->mainstack = 0; |
1102 | } |
1106 | } |
… | |
… | |
1165 | |
1169 | |
1166 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1170 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1167 | TRANSFER (ta); |
1171 | TRANSFER (ta); |
1168 | } |
1172 | } |
1169 | |
1173 | |
1170 | static int |
|
|
1171 | api_save (SV *coro_sv, int new_save) |
|
|
1172 | { |
|
|
1173 | dTHX; |
|
|
1174 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1175 | int old_save = coro->save; |
|
|
1176 | |
|
|
1177 | if (new_save >= 0) |
|
|
1178 | coro->save = new_save; |
|
|
1179 | |
|
|
1180 | return old_save; |
|
|
1181 | } |
|
|
1182 | |
|
|
1183 | /** Coro ********************************************************************/ |
1174 | /** Coro ********************************************************************/ |
1184 | |
1175 | |
1185 | static void |
1176 | static void |
1186 | coro_enq (pTHX_ SV *coro_sv) |
1177 | coro_enq (pTHX_ SV *coro_sv) |
1187 | { |
1178 | { |
… | |
… | |
1391 | #ifdef USE_ITHREADS |
1382 | #ifdef USE_ITHREADS |
1392 | MUTEX_INIT (&coro_mutex); |
1383 | MUTEX_INIT (&coro_mutex); |
1393 | #endif |
1384 | #endif |
1394 | BOOT_PAGESIZE; |
1385 | BOOT_PAGESIZE; |
1395 | |
1386 | |
|
|
1387 | irsgv = gv_fetchpv ("/", 1, SVt_PV); |
|
|
1388 | |
1396 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1389 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1397 | |
1390 | |
1398 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1391 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1399 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1392 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1400 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
1393 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
1401 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
1394 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
1402 | |
1395 | |
1403 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
|
|
1404 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
|
|
1405 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
|
|
1406 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1407 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
|
|
1408 | newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); |
|
|
1409 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
|
|
1410 | |
|
|
1411 | main_mainstack = PL_mainstack; |
1396 | main_mainstack = PL_mainstack; |
1412 | main_top_env = PL_top_env; |
1397 | main_top_env = PL_top_env; |
1413 | |
1398 | |
1414 | while (main_top_env->je_prev) |
1399 | while (main_top_env->je_prev) |
1415 | main_top_env = main_top_env->je_prev; |
1400 | main_top_env = main_top_env->je_prev; |
… | |
… | |
1427 | struct coro *coro; |
1412 | struct coro *coro; |
1428 | HV *hv; |
1413 | HV *hv; |
1429 | int i; |
1414 | int i; |
1430 | |
1415 | |
1431 | Newz (0, coro, 1, struct coro); |
1416 | Newz (0, coro, 1, struct coro); |
1432 | coro->args = newAV (); |
1417 | coro->args = newAV (); |
1433 | coro->save = CORO_SAVE_DEF; |
|
|
1434 | coro->flags = CF_NEW; |
1418 | coro->flags = CF_NEW; |
1435 | |
1419 | |
1436 | if (coro_first) coro_first->prev = coro; |
1420 | if (coro_first) coro_first->prev = coro; |
1437 | coro->next = coro_first; |
1421 | coro->next = coro_first; |
1438 | coro_first = coro; |
1422 | coro_first = coro; |
… | |
… | |
1443 | |
1427 | |
1444 | for (i = 1; i < items; i++) |
1428 | for (i = 1; i < items; i++) |
1445 | av_push (coro->args, newSVsv (ST (i))); |
1429 | av_push (coro->args, newSVsv (ST (i))); |
1446 | } |
1430 | } |
1447 | OUTPUT: |
1431 | OUTPUT: |
1448 | RETVAL |
|
|
1449 | |
|
|
1450 | int |
|
|
1451 | save (SV *coro, int new_save = -1) |
|
|
1452 | CODE: |
|
|
1453 | RETVAL = api_save (coro, new_save); |
|
|
1454 | OUTPUT: |
|
|
1455 | RETVAL |
|
|
1456 | |
|
|
1457 | int |
|
|
1458 | save_also (SV *coro_sv, int save_also) |
|
|
1459 | CODE: |
|
|
1460 | { |
|
|
1461 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1462 | RETVAL = coro->save; |
|
|
1463 | coro->save |= save_also; |
|
|
1464 | } |
|
|
1465 | OUTPUT: |
|
|
1466 | RETVAL |
1432 | RETVAL |
1467 | |
1433 | |
1468 | # these not obviously related functions are all rolled into the same xs |
1434 | # these not obviously related functions are all rolled into the same xs |
1469 | # function to increase chances that they all will call transfer with the same |
1435 | # function to increase chances that they all will call transfer with the same |
1470 | # stack offset |
1436 | # stack offset |
… | |
… | |
1570 | { |
1536 | { |
1571 | if (coro->mainstack) |
1537 | if (coro->mainstack) |
1572 | { |
1538 | { |
1573 | struct coro temp; |
1539 | struct coro temp; |
1574 | Zero (&temp, 1, struct coro); |
1540 | Zero (&temp, 1, struct coro); |
1575 | temp.save = CORO_SAVE_ALL; |
|
|
1576 | |
1541 | |
1577 | if (!(coro->flags & CF_RUNNING)) |
1542 | if (!(coro->flags & CF_RUNNING)) |
1578 | { |
1543 | { |
1579 | save_perl (aTHX_ &temp); |
1544 | save_perl (aTHX_ &temp); |
1580 | load_perl (aTHX_ coro); |
1545 | load_perl (aTHX_ coro); |
… | |
… | |
1678 | |
1643 | |
1679 | { |
1644 | { |
1680 | SV *sv = perl_get_sv("Coro::API", 1); |
1645 | SV *sv = perl_get_sv("Coro::API", 1); |
1681 | |
1646 | |
1682 | coroapi.schedule = api_schedule; |
1647 | coroapi.schedule = api_schedule; |
1683 | coroapi.save = api_save; |
|
|
1684 | coroapi.cede = api_cede; |
1648 | coroapi.cede = api_cede; |
1685 | coroapi.cede_notself = api_cede_notself; |
1649 | coroapi.cede_notself = api_cede_notself; |
1686 | coroapi.ready = api_ready; |
1650 | coroapi.ready = api_ready; |
1687 | coroapi.is_ready = api_is_ready; |
1651 | coroapi.is_ready = api_is_ready; |
1688 | coroapi.nready = &coro_nready; |
1652 | coroapi.nready = &coro_nready; |
… | |
… | |
1785 | |
1749 | |
1786 | av_clear (GvAV (PL_defgv)); |
1750 | av_clear (GvAV (PL_defgv)); |
1787 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1751 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1788 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1752 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1789 | |
1753 | |
1790 | coro->save = CORO_SAVE_DEF; |
|
|
1791 | coro->prio = 0; |
1754 | coro->prio = 0; |
1792 | |
1755 | |
1793 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
1756 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
1794 | api_trace (coro_current, 0); |
1757 | api_trace (coro_current, 0); |
1795 | |
1758 | |