… | |
… | |
238 | enum { |
238 | enum { |
239 | CF_RUNNING = 0x0001, /* coroutine is running */ |
239 | CF_RUNNING = 0x0001, /* coroutine is running */ |
240 | CF_READY = 0x0002, /* coroutine is ready */ |
240 | CF_READY = 0x0002, /* coroutine is ready */ |
241 | CF_NEW = 0x0004, /* has never been switched to */ |
241 | CF_NEW = 0x0004, /* has never been switched to */ |
242 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
242 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
|
|
243 | CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */ |
243 | }; |
244 | }; |
244 | |
245 | |
245 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
246 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
246 | typedef struct |
247 | typedef struct |
247 | { |
248 | { |
… | |
… | |
260 | /* this is a structure representing a perl-level coroutine */ |
261 | /* this is a structure representing a perl-level coroutine */ |
261 | struct coro { |
262 | struct coro { |
262 | /* the C coroutine allocated to this perl coroutine, if any */ |
263 | /* the C coroutine allocated to this perl coroutine, if any */ |
263 | coro_cctx *cctx; |
264 | coro_cctx *cctx; |
264 | |
265 | |
|
|
266 | /* ready queue */ |
|
|
267 | struct coro *next_ready; |
|
|
268 | |
265 | /* state data */ |
269 | /* state data */ |
266 | struct CoroSLF slf_frame; /* saved slf frame */ |
270 | struct CoroSLF slf_frame; /* saved slf frame */ |
267 | AV *mainstack; |
271 | AV *mainstack; |
268 | perl_slots *slot; /* basically the saved sp */ |
272 | perl_slots *slot; /* basically the saved sp */ |
269 | |
273 | |
… | |
… | |
285 | /* async_pool */ |
289 | /* async_pool */ |
286 | SV *saved_deffh; |
290 | SV *saved_deffh; |
287 | SV *invoke_cb; |
291 | SV *invoke_cb; |
288 | AV *invoke_av; |
292 | AV *invoke_av; |
289 | |
293 | |
|
|
294 | /* on_enter/on_leave */ |
|
|
295 | AV *on_enter; |
|
|
296 | AV *on_leave; |
|
|
297 | |
290 | /* linked list */ |
298 | /* linked list */ |
291 | struct coro *next, *prev; |
299 | struct coro *next, *prev; |
292 | }; |
300 | }; |
293 | |
301 | |
294 | typedef struct coro *Coro__State; |
302 | typedef struct coro *Coro__State; |
… | |
… | |
309 | #define PRIO_MIN -4 |
317 | #define PRIO_MIN -4 |
310 | |
318 | |
311 | /* for Coro.pm */ |
319 | /* for Coro.pm */ |
312 | static SV *coro_current; |
320 | static SV *coro_current; |
313 | static SV *coro_readyhook; |
321 | static SV *coro_readyhook; |
314 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
322 | static struct coro *coro_ready [PRIO_MAX - PRIO_MIN + 1][2]; /* head|tail */ |
315 | static CV *cv_coro_run, *cv_coro_terminate; |
323 | static CV *cv_coro_run, *cv_coro_terminate; |
316 | static struct coro *coro_first; |
324 | static struct coro *coro_first; |
317 | #define coro_nready coroapi.nready |
325 | #define coro_nready coroapi.nready |
318 | |
326 | |
319 | /** lowlevel stuff **********************************************************/ |
327 | /** lowlevel stuff **********************************************************/ |
… | |
… | |
352 | INLINE CV * |
360 | INLINE CV * |
353 | coro_sv_2cv (pTHX_ SV *sv) |
361 | coro_sv_2cv (pTHX_ SV *sv) |
354 | { |
362 | { |
355 | HV *st; |
363 | HV *st; |
356 | GV *gvp; |
364 | GV *gvp; |
357 | return sv_2cv (sv, &st, &gvp, 0); |
365 | CV *cv = sv_2cv (sv, &st, &gvp, 0); |
|
|
366 | |
|
|
367 | if (!cv) |
|
|
368 | croak ("code reference expected"); |
|
|
369 | |
|
|
370 | return cv; |
358 | } |
371 | } |
359 | |
372 | |
360 | /*****************************************************************************/ |
373 | /*****************************************************************************/ |
361 | /* magic glue */ |
374 | /* magic glue */ |
362 | |
375 | |
… | |
… | |
521 | } |
534 | } |
522 | |
535 | |
523 | /** load & save, init *******************************************************/ |
536 | /** load & save, init *******************************************************/ |
524 | |
537 | |
525 | static void |
538 | static void |
|
|
539 | on_enterleave_call (pTHX_ SV *cb); |
|
|
540 | |
|
|
541 | static void |
526 | load_perl (pTHX_ Coro__State c) |
542 | load_perl (pTHX_ Coro__State c) |
527 | { |
543 | { |
528 | perl_slots *slot = c->slot; |
544 | perl_slots *slot = c->slot; |
529 | c->slot = 0; |
545 | c->slot = 0; |
530 | |
546 | |
… | |
… | |
556 | PUTBACK; |
572 | PUTBACK; |
557 | } |
573 | } |
558 | |
574 | |
559 | slf_frame = c->slf_frame; |
575 | slf_frame = c->slf_frame; |
560 | CORO_THROW = c->except; |
576 | CORO_THROW = c->except; |
|
|
577 | |
|
|
578 | if (expect_false (c->on_enter)) |
|
|
579 | { |
|
|
580 | int i; |
|
|
581 | |
|
|
582 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
|
|
583 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
|
|
584 | } |
561 | } |
585 | } |
562 | |
586 | |
563 | static void |
587 | static void |
564 | save_perl (pTHX_ Coro__State c) |
588 | save_perl (pTHX_ Coro__State c) |
565 | { |
589 | { |
|
|
590 | if (expect_false (c->on_leave)) |
|
|
591 | { |
|
|
592 | int i; |
|
|
593 | |
|
|
594 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
|
|
595 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
|
|
596 | } |
|
|
597 | |
566 | c->except = CORO_THROW; |
598 | c->except = CORO_THROW; |
567 | c->slf_frame = slf_frame; |
599 | c->slf_frame = slf_frame; |
568 | |
600 | |
569 | { |
601 | { |
570 | dSP; |
602 | dSP; |
… | |
… | |
769 | #endif |
801 | #endif |
770 | |
802 | |
771 | /* |
803 | /* |
772 | * This overrides the default magic get method of %SIG elements. |
804 | * This overrides the default magic get method of %SIG elements. |
773 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
805 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
774 | * and instead of tryign to save and restore the hash elements, we just provide |
806 | * and instead of trying to save and restore the hash elements, we just provide |
775 | * readback here. |
807 | * readback here. |
776 | * We only do this when the hook is != 0, as they are often set to 0 temporarily, |
|
|
777 | * not expecting this to actually change the hook. This is a potential problem |
|
|
778 | * when a schedule happens then, but we ignore this. |
|
|
779 | */ |
808 | */ |
780 | static int |
809 | static int |
781 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
810 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
782 | { |
811 | { |
783 | const char *s = MgPV_nolen_const (mg); |
812 | const char *s = MgPV_nolen_const (mg); |
… | |
… | |
836 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
865 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
837 | |
866 | |
838 | if (svp) |
867 | if (svp) |
839 | { |
868 | { |
840 | SV *old = *svp; |
869 | SV *old = *svp; |
841 | *svp = newSVsv (sv); |
870 | *svp = SvOK (sv) ? newSVsv (sv) : 0; |
842 | SvREFCNT_dec (old); |
871 | SvREFCNT_dec (old); |
843 | return 0; |
872 | return 0; |
844 | } |
873 | } |
845 | } |
874 | } |
846 | |
875 | |
… | |
… | |
961 | } |
990 | } |
962 | |
991 | |
963 | static void |
992 | static void |
964 | coro_destruct_perl (pTHX_ struct coro *coro) |
993 | coro_destruct_perl (pTHX_ struct coro *coro) |
965 | { |
994 | { |
|
|
995 | SV *svf [9]; |
|
|
996 | |
|
|
997 | { |
|
|
998 | struct coro *current = SvSTATE_current; |
|
|
999 | |
|
|
1000 | assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); |
|
|
1001 | |
|
|
1002 | save_perl (aTHX_ current); |
|
|
1003 | load_perl (aTHX_ coro); |
966 | coro_unwind_stacks (aTHX); |
1004 | coro_unwind_stacks (aTHX); |
|
|
1005 | coro_destruct_stacks (aTHX); |
967 | |
1006 | |
968 | SvREFCNT_dec (GvSV (PL_defgv)); |
1007 | // now save some sv's to be free'd later |
969 | SvREFCNT_dec (GvAV (PL_defgv)); |
1008 | svf [0] = GvSV (PL_defgv); |
970 | SvREFCNT_dec (GvSV (PL_errgv)); |
1009 | svf [1] = (SV *)GvAV (PL_defgv); |
971 | SvREFCNT_dec (PL_defoutgv); |
1010 | svf [2] = GvSV (PL_errgv); |
972 | SvREFCNT_dec (PL_rs); |
1011 | svf [3] = (SV *)PL_defoutgv; |
973 | SvREFCNT_dec (GvSV (irsgv)); |
1012 | svf [4] = PL_rs; |
974 | SvREFCNT_dec (GvHV (PL_hintgv)); |
1013 | svf [5] = GvSV (irsgv); |
|
|
1014 | svf [6] = (SV *)GvHV (PL_hintgv); |
|
|
1015 | svf [7] = PL_diehook; |
|
|
1016 | svf [8] = PL_warnhook; |
975 | |
1017 | |
976 | SvREFCNT_dec (PL_diehook); |
1018 | assert (9 == sizeof (svf) / sizeof (*svf)); |
977 | SvREFCNT_dec (PL_warnhook); |
1019 | |
|
|
1020 | load_perl (aTHX_ current); |
978 | |
1021 | } |
|
|
1022 | |
|
|
1023 | { |
|
|
1024 | int i; |
|
|
1025 | |
|
|
1026 | for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i) |
|
|
1027 | SvREFCNT_dec (svf [i]); |
|
|
1028 | |
979 | SvREFCNT_dec (coro->saved_deffh); |
1029 | SvREFCNT_dec (coro->saved_deffh); |
980 | SvREFCNT_dec (coro->rouse_cb); |
1030 | SvREFCNT_dec (coro->rouse_cb); |
981 | SvREFCNT_dec (coro->invoke_cb); |
1031 | SvREFCNT_dec (coro->invoke_cb); |
982 | SvREFCNT_dec (coro->invoke_av); |
1032 | SvREFCNT_dec (coro->invoke_av); |
983 | |
1033 | } |
984 | coro_destruct_stacks (aTHX); |
|
|
985 | } |
1034 | } |
986 | |
1035 | |
987 | INLINE void |
1036 | INLINE void |
988 | free_coro_mortal (pTHX) |
1037 | free_coro_mortal (pTHX) |
989 | { |
1038 | { |
… | |
… | |
1357 | /* TODO: throwing up here is considered harmful */ |
1406 | /* TODO: throwing up here is considered harmful */ |
1358 | |
1407 | |
1359 | if (expect_true (prev != next)) |
1408 | if (expect_true (prev != next)) |
1360 | { |
1409 | { |
1361 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1410 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1362 | croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); |
1411 | croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); |
1363 | |
1412 | |
1364 | if (expect_false (next->flags & CF_RUNNING)) |
|
|
1365 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
|
|
1366 | |
|
|
1367 | if (expect_false (next->flags & CF_DESTROYED)) |
1413 | if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED))) |
1368 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); |
1414 | croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); |
1369 | |
1415 | |
1370 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1416 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1371 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1417 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1372 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1418 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1373 | #endif |
1419 | #endif |
… | |
… | |
1461 | coro_state_destroy (pTHX_ struct coro *coro) |
1507 | coro_state_destroy (pTHX_ struct coro *coro) |
1462 | { |
1508 | { |
1463 | if (coro->flags & CF_DESTROYED) |
1509 | if (coro->flags & CF_DESTROYED) |
1464 | return 0; |
1510 | return 0; |
1465 | |
1511 | |
1466 | if (coro->on_destroy) |
1512 | if (coro->on_destroy && !PL_dirty) |
1467 | coro->on_destroy (aTHX_ coro); |
1513 | coro->on_destroy (aTHX_ coro); |
1468 | |
1514 | |
1469 | coro->flags |= CF_DESTROYED; |
1515 | coro->flags |= CF_DESTROYED; |
1470 | |
1516 | |
1471 | if (coro->flags & CF_READY) |
1517 | if (coro->flags & CF_READY) |
… | |
… | |
1479 | |
1525 | |
1480 | if (coro->mainstack |
1526 | if (coro->mainstack |
1481 | && coro->mainstack != main_mainstack |
1527 | && coro->mainstack != main_mainstack |
1482 | && coro->slot |
1528 | && coro->slot |
1483 | && !PL_dirty) |
1529 | && !PL_dirty) |
1484 | { |
|
|
1485 | struct coro temp; |
|
|
1486 | |
|
|
1487 | assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); |
|
|
1488 | |
|
|
1489 | save_perl (aTHX_ &temp); |
|
|
1490 | load_perl (aTHX_ coro); |
|
|
1491 | |
|
|
1492 | coro_destruct_perl (aTHX_ coro); |
1530 | coro_destruct_perl (aTHX_ coro); |
1493 | |
|
|
1494 | load_perl (aTHX_ &temp); |
|
|
1495 | |
|
|
1496 | coro->slot = 0; |
|
|
1497 | } |
|
|
1498 | |
1531 | |
1499 | cctx_destroy (coro->cctx); |
1532 | cctx_destroy (coro->cctx); |
1500 | SvREFCNT_dec (coro->startcv); |
1533 | SvREFCNT_dec (coro->startcv); |
1501 | SvREFCNT_dec (coro->args); |
1534 | SvREFCNT_dec (coro->args); |
1502 | SvREFCNT_dec (CORO_THROW); |
1535 | SvREFCNT_dec (CORO_THROW); |
… | |
… | |
1589 | /** Coro ********************************************************************/ |
1622 | /** Coro ********************************************************************/ |
1590 | |
1623 | |
1591 | INLINE void |
1624 | INLINE void |
1592 | coro_enq (pTHX_ struct coro *coro) |
1625 | coro_enq (pTHX_ struct coro *coro) |
1593 | { |
1626 | { |
1594 | av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); |
1627 | struct coro **ready = coro_ready [coro->prio - PRIO_MIN]; |
1595 | } |
|
|
1596 | |
1628 | |
1597 | INLINE SV * |
1629 | SvREFCNT_inc_NN (coro->hv); |
|
|
1630 | |
|
|
1631 | coro->next_ready = 0; |
|
|
1632 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
|
|
1633 | ready [1] = coro; |
|
|
1634 | } |
|
|
1635 | |
|
|
1636 | INLINE struct coro * |
1598 | coro_deq (pTHX) |
1637 | coro_deq (pTHX) |
1599 | { |
1638 | { |
1600 | int prio; |
1639 | int prio; |
1601 | |
1640 | |
1602 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1641 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1603 | if (AvFILLp (coro_ready [prio]) >= 0) |
1642 | { |
1604 | return av_shift (coro_ready [prio]); |
1643 | struct coro **ready = coro_ready [prio]; |
|
|
1644 | |
|
|
1645 | if (ready [0]) |
|
|
1646 | { |
|
|
1647 | struct coro *coro = ready [0]; |
|
|
1648 | ready [0] = coro->next_ready; |
|
|
1649 | return coro; |
|
|
1650 | } |
|
|
1651 | } |
1605 | |
1652 | |
1606 | return 0; |
1653 | return 0; |
1607 | } |
1654 | } |
1608 | |
1655 | |
1609 | static int |
1656 | static int |
… | |
… | |
1673 | static void |
1720 | static void |
1674 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1721 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1675 | { |
1722 | { |
1676 | for (;;) |
1723 | for (;;) |
1677 | { |
1724 | { |
1678 | SV *next_sv = coro_deq (aTHX); |
1725 | struct coro *next = coro_deq (aTHX); |
1679 | |
1726 | |
1680 | if (expect_true (next_sv)) |
1727 | if (expect_true (next)) |
1681 | { |
1728 | { |
1682 | struct coro *next = SvSTATE_hv (next_sv); |
|
|
1683 | |
|
|
1684 | /* cannot transfer to destroyed coros, skip and look for next */ |
1729 | /* cannot transfer to destroyed coros, skip and look for next */ |
1685 | if (expect_false (next->flags & CF_DESTROYED)) |
1730 | if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED))) |
1686 | SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ |
1731 | SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ |
1687 | else |
1732 | else |
1688 | { |
1733 | { |
1689 | next->flags &= ~CF_READY; |
1734 | next->flags &= ~CF_READY; |
1690 | --coro_nready; |
1735 | --coro_nready; |
1691 | |
1736 | |
… | |
… | |
1965 | SV *data = (SV *)GENSUB_ARG; |
2010 | SV *data = (SV *)GENSUB_ARG; |
1966 | |
2011 | |
1967 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2012 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
1968 | { |
2013 | { |
1969 | /* first call, set args */ |
2014 | /* first call, set args */ |
|
|
2015 | SV *coro = SvRV (data); |
1970 | AV *av = newAV (); |
2016 | AV *av = newAV (); |
1971 | SV *coro = SvRV (data); |
|
|
1972 | |
2017 | |
1973 | SvRV_set (data, (SV *)av); |
2018 | SvRV_set (data, (SV *)av); |
1974 | api_ready (aTHX_ coro); |
|
|
1975 | SvREFCNT_dec (coro); |
|
|
1976 | |
2019 | |
1977 | /* better take a full copy of the arguments */ |
2020 | /* better take a full copy of the arguments */ |
1978 | while (items--) |
2021 | while (items--) |
1979 | av_store (av, items, newSVsv (ST (items))); |
2022 | av_store (av, items, newSVsv (ST (items))); |
|
|
2023 | |
|
|
2024 | api_ready (aTHX_ coro); |
|
|
2025 | SvREFCNT_dec (coro); |
1980 | } |
2026 | } |
1981 | |
2027 | |
1982 | XSRETURN_EMPTY; |
2028 | XSRETURN_EMPTY; |
1983 | } |
2029 | } |
1984 | |
2030 | |
… | |
… | |
2001 | |
2047 | |
2002 | EXTEND (SP, AvFILLp (av) + 1); |
2048 | EXTEND (SP, AvFILLp (av) + 1); |
2003 | for (i = 0; i <= AvFILLp (av); ++i) |
2049 | for (i = 0; i <= AvFILLp (av); ++i) |
2004 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
2050 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
2005 | |
2051 | |
2006 | /* we have stolen the elements, so ste length to zero and free */ |
2052 | /* we have stolen the elements, so set length to zero and free */ |
2007 | AvFILLp (av) = -1; |
2053 | AvFILLp (av) = -1; |
2008 | av_undef (av); |
2054 | av_undef (av); |
2009 | |
2055 | |
2010 | PUTBACK; |
2056 | PUTBACK; |
2011 | } |
2057 | } |
… | |
… | |
2301 | |
2347 | |
2302 | PL_op->op_ppaddr = pp_slf; |
2348 | PL_op->op_ppaddr = pp_slf; |
2303 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2349 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2304 | |
2350 | |
2305 | PL_op = (OP *)&slf_restore; |
2351 | PL_op = (OP *)&slf_restore; |
|
|
2352 | } |
|
|
2353 | |
|
|
2354 | /*****************************************************************************/ |
|
|
2355 | /* dynamic wind */ |
|
|
2356 | |
|
|
2357 | static void |
|
|
2358 | on_enterleave_call (pTHX_ SV *cb) |
|
|
2359 | { |
|
|
2360 | dSP; |
|
|
2361 | |
|
|
2362 | PUSHSTACK; |
|
|
2363 | |
|
|
2364 | PUSHMARK (SP); |
|
|
2365 | PUTBACK; |
|
|
2366 | call_sv (cb, G_VOID | G_DISCARD); |
|
|
2367 | SPAGAIN; |
|
|
2368 | |
|
|
2369 | POPSTACK; |
|
|
2370 | } |
|
|
2371 | |
|
|
2372 | static SV * |
|
|
2373 | coro_avp_pop_and_free (pTHX_ AV **avp) |
|
|
2374 | { |
|
|
2375 | AV *av = *avp; |
|
|
2376 | SV *res = av_pop (av); |
|
|
2377 | |
|
|
2378 | if (AvFILLp (av) < 0) |
|
|
2379 | { |
|
|
2380 | *avp = 0; |
|
|
2381 | SvREFCNT_dec (av); |
|
|
2382 | } |
|
|
2383 | |
|
|
2384 | return res; |
|
|
2385 | } |
|
|
2386 | |
|
|
2387 | static void |
|
|
2388 | coro_pop_on_enter (pTHX_ void *coro) |
|
|
2389 | { |
|
|
2390 | SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter); |
|
|
2391 | SvREFCNT_dec (cb); |
|
|
2392 | } |
|
|
2393 | |
|
|
2394 | static void |
|
|
2395 | coro_pop_on_leave (pTHX_ void *coro) |
|
|
2396 | { |
|
|
2397 | SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave); |
|
|
2398 | on_enterleave_call (aTHX_ sv_2mortal (cb)); |
2306 | } |
2399 | } |
2307 | |
2400 | |
2308 | /*****************************************************************************/ |
2401 | /*****************************************************************************/ |
2309 | /* PerlIO::cede */ |
2402 | /* PerlIO::cede */ |
2310 | |
2403 | |
… | |
… | |
3008 | eval = 1 |
3101 | eval = 1 |
3009 | CODE: |
3102 | CODE: |
3010 | { |
3103 | { |
3011 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
3104 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
3012 | { |
3105 | { |
3013 | struct coro temp; |
3106 | struct coro *current = SvSTATE_current; |
3014 | |
3107 | |
3015 | if (!(coro->flags & CF_RUNNING)) |
3108 | if (current != coro) |
3016 | { |
3109 | { |
3017 | PUTBACK; |
3110 | PUTBACK; |
3018 | save_perl (aTHX_ &temp); |
3111 | save_perl (aTHX_ current); |
3019 | load_perl (aTHX_ coro); |
3112 | load_perl (aTHX_ coro); |
|
|
3113 | SPAGAIN; |
3020 | } |
3114 | } |
3021 | |
3115 | |
3022 | { |
|
|
3023 | dSP; |
|
|
3024 | ENTER; |
|
|
3025 | SAVETMPS; |
|
|
3026 | PUTBACK; |
|
|
3027 | PUSHSTACK; |
3116 | PUSHSTACK; |
|
|
3117 | |
3028 | PUSHMARK (SP); |
3118 | PUSHMARK (SP); |
|
|
3119 | PUTBACK; |
3029 | |
3120 | |
3030 | if (ix) |
3121 | if (ix) |
3031 | eval_sv (coderef, 0); |
3122 | eval_sv (coderef, 0); |
3032 | else |
3123 | else |
3033 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
3124 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
3034 | |
3125 | |
3035 | POPSTACK; |
3126 | POPSTACK; |
3036 | SPAGAIN; |
3127 | SPAGAIN; |
3037 | FREETMPS; |
|
|
3038 | LEAVE; |
|
|
3039 | PUTBACK; |
|
|
3040 | } |
|
|
3041 | |
3128 | |
3042 | if (!(coro->flags & CF_RUNNING)) |
3129 | if (current != coro) |
3043 | { |
3130 | { |
|
|
3131 | PUTBACK; |
3044 | save_perl (aTHX_ coro); |
3132 | save_perl (aTHX_ coro); |
3045 | load_perl (aTHX_ &temp); |
3133 | load_perl (aTHX_ current); |
3046 | SPAGAIN; |
3134 | SPAGAIN; |
3047 | } |
3135 | } |
3048 | } |
3136 | } |
3049 | } |
3137 | } |
3050 | |
3138 | |
… | |
… | |
3054 | ALIAS: |
3142 | ALIAS: |
3055 | is_ready = CF_READY |
3143 | is_ready = CF_READY |
3056 | is_running = CF_RUNNING |
3144 | is_running = CF_RUNNING |
3057 | is_new = CF_NEW |
3145 | is_new = CF_NEW |
3058 | is_destroyed = CF_DESTROYED |
3146 | is_destroyed = CF_DESTROYED |
|
|
3147 | is_suspended = CF_SUSPENDED |
3059 | CODE: |
3148 | CODE: |
3060 | RETVAL = boolSV (coro->flags & ix); |
3149 | RETVAL = boolSV (coro->flags & ix); |
3061 | OUTPUT: |
3150 | OUTPUT: |
3062 | RETVAL |
3151 | RETVAL |
3063 | |
3152 | |
… | |
… | |
3128 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
3217 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
3129 | |
3218 | |
3130 | SV *tmp = *src; *src = *dst; *dst = tmp; |
3219 | SV *tmp = *src; *src = *dst; *dst = tmp; |
3131 | } |
3220 | } |
3132 | |
3221 | |
|
|
3222 | void |
|
|
3223 | cancel (Coro::State self) |
|
|
3224 | CODE: |
|
|
3225 | coro_state_destroy (aTHX_ self); |
|
|
3226 | coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ |
|
|
3227 | |
3133 | |
3228 | |
3134 | MODULE = Coro::State PACKAGE = Coro |
3229 | MODULE = Coro::State PACKAGE = Coro |
3135 | |
3230 | |
3136 | BOOT: |
3231 | BOOT: |
3137 | { |
3232 | { |
3138 | int i; |
|
|
3139 | |
|
|
3140 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3233 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3141 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3234 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3142 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3235 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3143 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3236 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3144 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3237 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
… | |
… | |
3159 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
3252 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
3160 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
3253 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
3161 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
3254 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
3162 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
3255 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
3163 | |
3256 | |
3164 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
|
|
3165 | coro_ready[i] = newAV (); |
|
|
3166 | |
|
|
3167 | { |
3257 | { |
3168 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3258 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3169 | |
3259 | |
3170 | coroapi.schedule = api_schedule; |
3260 | coroapi.schedule = api_schedule; |
3171 | coroapi.schedule_to = api_schedule_to; |
3261 | coroapi.schedule_to = api_schedule_to; |
… | |
… | |
3211 | cede_notself (...) |
3301 | cede_notself (...) |
3212 | CODE: |
3302 | CODE: |
3213 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3303 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3214 | |
3304 | |
3215 | void |
3305 | void |
3216 | _cancel (Coro::State self) |
|
|
3217 | CODE: |
|
|
3218 | coro_state_destroy (aTHX_ self); |
|
|
3219 | coro_call_on_destroy (aTHX_ self); |
|
|
3220 | |
|
|
3221 | void |
|
|
3222 | _set_current (SV *current) |
3306 | _set_current (SV *current) |
3223 | PROTOTYPE: $ |
3307 | PROTOTYPE: $ |
3224 | CODE: |
3308 | CODE: |
3225 | SvREFCNT_dec (SvRV (coro_current)); |
3309 | SvREFCNT_dec (SvRV (coro_current)); |
3226 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
3310 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
… | |
… | |
3268 | PROTOTYPE: |
3352 | PROTOTYPE: |
3269 | CODE: |
3353 | CODE: |
3270 | RETVAL = coro_nready; |
3354 | RETVAL = coro_nready; |
3271 | OUTPUT: |
3355 | OUTPUT: |
3272 | RETVAL |
3356 | RETVAL |
|
|
3357 | |
|
|
3358 | void |
|
|
3359 | suspend (Coro::State self) |
|
|
3360 | PROTOTYPE: $ |
|
|
3361 | CODE: |
|
|
3362 | self->flags |= CF_SUSPENDED; |
|
|
3363 | |
|
|
3364 | void |
|
|
3365 | resume (Coro::State self) |
|
|
3366 | PROTOTYPE: $ |
|
|
3367 | CODE: |
|
|
3368 | self->flags &= ~CF_SUSPENDED; |
3273 | |
3369 | |
3274 | void |
3370 | void |
3275 | _pool_handler (...) |
3371 | _pool_handler (...) |
3276 | CODE: |
3372 | CODE: |
3277 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
3373 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
… | |
… | |
3332 | rouse_wait (...) |
3428 | rouse_wait (...) |
3333 | PROTOTYPE: ;$ |
3429 | PROTOTYPE: ;$ |
3334 | PPCODE: |
3430 | PPCODE: |
3335 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3431 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3336 | |
3432 | |
|
|
3433 | void |
|
|
3434 | on_enter (SV *block) |
|
|
3435 | ALIAS: |
|
|
3436 | on_leave = 1 |
|
|
3437 | PROTOTYPE: & |
|
|
3438 | CODE: |
|
|
3439 | { |
|
|
3440 | struct coro *coro = SvSTATE_current; |
|
|
3441 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
|
|
3442 | |
|
|
3443 | block = (SV *)coro_sv_2cv (aTHX_ block); |
|
|
3444 | |
|
|
3445 | if (!*avp) |
|
|
3446 | *avp = newAV (); |
|
|
3447 | |
|
|
3448 | av_push (*avp, SvREFCNT_inc (block)); |
|
|
3449 | |
|
|
3450 | if (!ix) |
|
|
3451 | on_enterleave_call (aTHX_ block); |
|
|
3452 | |
|
|
3453 | LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */ |
|
|
3454 | SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro); |
|
|
3455 | ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */ |
|
|
3456 | } |
|
|
3457 | |
3337 | |
3458 | |
3338 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3459 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3339 | |
3460 | |
3340 | BOOT: |
3461 | BOOT: |
3341 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
3462 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
… | |
… | |
3351 | GvSTASH (CvGV (cv)) |
3472 | GvSTASH (CvGV (cv)) |
3352 | ); |
3473 | ); |
3353 | OUTPUT: |
3474 | OUTPUT: |
3354 | RETVAL |
3475 | RETVAL |
3355 | |
3476 | |
3356 | # helper for Coro::Channel |
3477 | # helper for Coro::Channel and others |
3357 | SV * |
3478 | SV * |
3358 | _alloc (int count) |
3479 | _alloc (int count) |
3359 | CODE: |
3480 | CODE: |
3360 | RETVAL = coro_waitarray_new (aTHX_ count); |
3481 | RETVAL = coro_waitarray_new (aTHX_ count); |
3361 | OUTPUT: |
3482 | OUTPUT: |
… | |
… | |
3419 | for (i = 1; i <= wcount; ++i) |
3540 | for (i = 1; i <= wcount; ++i) |
3420 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
3541 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
3421 | } |
3542 | } |
3422 | } |
3543 | } |
3423 | |
3544 | |
|
|
3545 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
|
|
3546 | |
|
|
3547 | void |
|
|
3548 | _may_delete (SV *sem, int count, int extra_refs) |
|
|
3549 | PPCODE: |
|
|
3550 | { |
|
|
3551 | AV *av = (AV *)SvRV (sem); |
|
|
3552 | |
|
|
3553 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
|
|
3554 | && AvFILLp (av) == 0 /* no waiters, just count */ |
|
|
3555 | && SvIV (AvARRAY (av)[0]) == count) |
|
|
3556 | XSRETURN_YES; |
|
|
3557 | |
|
|
3558 | XSRETURN_NO; |
|
|
3559 | } |
|
|
3560 | |
3424 | MODULE = Coro::State PACKAGE = Coro::Signal |
3561 | MODULE = Coro::State PACKAGE = Coro::Signal |
3425 | |
3562 | |
3426 | SV * |
3563 | SV * |
3427 | new (SV *klass) |
3564 | new (SV *klass) |
3428 | CODE: |
3565 | CODE: |