… | |
… | |
142 | #define NOINLINE attribute ((noinline)) |
142 | #define NOINLINE attribute ((noinline)) |
143 | |
143 | |
144 | #include "CoroAPI.h" |
144 | #include "CoroAPI.h" |
145 | |
145 | |
146 | #ifdef USE_ITHREADS |
146 | #ifdef USE_ITHREADS |
147 | |
|
|
148 | static perl_mutex coro_lock; |
|
|
149 | # define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) |
|
|
150 | # define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) |
|
|
151 | # if CORO_PTHREAD |
147 | # if CORO_PTHREAD |
152 | static void *coro_thx; |
148 | static void *coro_thx; |
153 | # endif |
149 | # endif |
154 | |
|
|
155 | #else |
|
|
156 | |
|
|
157 | # define LOCK (void)0 |
|
|
158 | # define UNLOCK (void)0 |
|
|
159 | |
|
|
160 | #endif |
150 | #endif |
161 | |
|
|
162 | # undef LOCK |
|
|
163 | # define LOCK (void)0 |
|
|
164 | # undef UNLOCK |
|
|
165 | # define UNLOCK (void)0 |
|
|
166 | |
151 | |
167 | /* helper storage struct for Coro::AIO */ |
152 | /* helper storage struct for Coro::AIO */ |
168 | struct io_state |
153 | struct io_state |
169 | { |
154 | { |
170 | AV *res; |
155 | AV *res; |
… | |
… | |
266 | |
251 | |
267 | AV *args; /* data associated with this coroutine (initial args) */ |
252 | AV *args; /* data associated with this coroutine (initial args) */ |
268 | int refcnt; /* coroutines are refcounted, yes */ |
253 | int refcnt; /* coroutines are refcounted, yes */ |
269 | int flags; /* CF_ flags */ |
254 | int flags; /* CF_ flags */ |
270 | HV *hv; /* the perl hash associated with this coro, if any */ |
255 | HV *hv; /* the perl hash associated with this coro, if any */ |
|
|
256 | void (*on_destroy)(pTHX_ struct coro *coro); |
271 | |
257 | |
272 | /* statistics */ |
258 | /* statistics */ |
273 | int usecount; /* number of transfers to this coro */ |
259 | int usecount; /* number of transfers to this coro */ |
274 | |
260 | |
275 | /* coro process data */ |
261 | /* coro process data */ |
… | |
… | |
403 | static MGVTBL coro_cv_vtbl = { |
389 | static MGVTBL coro_cv_vtbl = { |
404 | 0, 0, 0, 0, |
390 | 0, 0, 0, 0, |
405 | coro_cv_free |
391 | coro_cv_free |
406 | }; |
392 | }; |
407 | |
393 | |
408 | #define CORO_MAGIC(sv, type) \ |
394 | #define CORO_MAGIC(sv, type) \ |
409 | SvMAGIC (sv) \ |
395 | expect_true (SvMAGIC (sv)) \ |
410 | ? SvMAGIC (sv)->mg_type == type \ |
396 | ? expect_true (SvMAGIC (sv)->mg_type == type) \ |
411 | ? SvMAGIC (sv) \ |
397 | ? SvMAGIC (sv) \ |
412 | : mg_find (sv, type) \ |
398 | : mg_find (sv, type) \ |
413 | : 0 |
399 | : 0 |
414 | |
400 | |
415 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
401 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
416 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
402 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
417 | |
403 | |
… | |
… | |
438 | mg = CORO_MAGIC_state (coro); |
424 | mg = CORO_MAGIC_state (coro); |
439 | return (struct coro *)mg->mg_ptr; |
425 | return (struct coro *)mg->mg_ptr; |
440 | } |
426 | } |
441 | |
427 | |
442 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
428 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
429 | |
|
|
430 | /* fastert than SvSTATE, but expects a coroutine hv */ |
|
|
431 | INLINE struct coro * |
|
|
432 | SvSTATE_hv (SV *sv) |
|
|
433 | { |
|
|
434 | MAGIC *mg = expect_true (SvMAGIC (sv)->mg_type == CORO_MAGIC_type_state) |
|
|
435 | ? SvMAGIC (sv) |
|
|
436 | : mg_find (sv, CORO_MAGIC_type_state); |
|
|
437 | |
|
|
438 | return (struct coro *)mg->mg_ptr; |
|
|
439 | } |
|
|
440 | |
|
|
441 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
443 | |
442 | |
444 | /* the next two functions merely cache the padlists */ |
443 | /* the next two functions merely cache the padlists */ |
445 | static void |
444 | static void |
446 | get_padlist (pTHX_ CV *cv) |
445 | get_padlist (pTHX_ CV *cv) |
447 | { |
446 | { |
… | |
… | |
929 | static int |
928 | static int |
930 | runops_trace (pTHX) |
929 | runops_trace (pTHX) |
931 | { |
930 | { |
932 | COP *oldcop = 0; |
931 | COP *oldcop = 0; |
933 | int oldcxix = -2; |
932 | int oldcxix = -2; |
934 | struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ |
933 | struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */ |
935 | coro_cctx *cctx = coro->cctx; |
934 | coro_cctx *cctx = coro->cctx; |
936 | |
935 | |
937 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
936 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
938 | { |
937 | { |
939 | PERL_ASYNC_CHECK (); |
938 | PERL_ASYNC_CHECK (); |
… | |
… | |
1093 | struct coro *next = (struct coro *)transfer_next; |
1092 | struct coro *next = (struct coro *)transfer_next; |
1094 | assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */ |
1093 | assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */ |
1095 | assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next)); |
1094 | assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next)); |
1096 | |
1095 | |
1097 | free_coro_mortal (aTHX); |
1096 | free_coro_mortal (aTHX); |
1098 | UNLOCK; |
|
|
1099 | |
1097 | |
1100 | if (expect_false (next->throw)) |
1098 | if (expect_false (next->throw)) |
1101 | { |
1099 | { |
1102 | SV *exception = sv_2mortal (next->throw); |
1100 | SV *exception = sv_2mortal (next->throw); |
1103 | |
1101 | |
… | |
… | |
1338 | } |
1336 | } |
1339 | |
1337 | |
1340 | prev->flags &= ~CF_RUNNING; |
1338 | prev->flags &= ~CF_RUNNING; |
1341 | next->flags |= CF_RUNNING; |
1339 | next->flags |= CF_RUNNING; |
1342 | |
1340 | |
1343 | LOCK; |
|
|
1344 | |
|
|
1345 | /* first get rid of the old state */ |
1341 | /* first get rid of the old state */ |
1346 | save_perl (aTHX_ prev); |
1342 | save_perl (aTHX_ prev); |
1347 | |
1343 | |
1348 | if (expect_false (next->flags & CF_NEW)) |
1344 | if (expect_false (next->flags & CF_NEW)) |
1349 | { |
1345 | { |
… | |
… | |
1406 | coro_state_destroy (pTHX_ struct coro *coro) |
1402 | coro_state_destroy (pTHX_ struct coro *coro) |
1407 | { |
1403 | { |
1408 | if (coro->flags & CF_DESTROYED) |
1404 | if (coro->flags & CF_DESTROYED) |
1409 | return 0; |
1405 | return 0; |
1410 | |
1406 | |
|
|
1407 | if (coro->on_destroy) |
|
|
1408 | coro->on_destroy (aTHX_ coro); |
|
|
1409 | |
1411 | coro->flags |= CF_DESTROYED; |
1410 | coro->flags |= CF_DESTROYED; |
1412 | |
1411 | |
1413 | if (coro->flags & CF_READY) |
1412 | if (coro->flags & CF_READY) |
1414 | { |
1413 | { |
1415 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1414 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1416 | /* alternative: look through all ready queues and remove the coro */ |
1415 | /* alternative: look through all ready queues and remove the coro */ |
1417 | LOCK; |
|
|
1418 | --coro_nready; |
1416 | --coro_nready; |
1419 | UNLOCK; |
|
|
1420 | } |
1417 | } |
1421 | else |
1418 | else |
1422 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1419 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1423 | |
1420 | |
1424 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1421 | if (coro->mainstack && coro->mainstack != main_mainstack) |
… | |
… | |
1502 | TRANSFER (ta, 1); |
1499 | TRANSFER (ta, 1); |
1503 | } |
1500 | } |
1504 | |
1501 | |
1505 | /** Coro ********************************************************************/ |
1502 | /** Coro ********************************************************************/ |
1506 | |
1503 | |
1507 | static void |
1504 | INLINE void |
1508 | coro_enq (pTHX_ SV *coro_sv) |
1505 | coro_enq (pTHX_ struct coro *coro) |
1509 | { |
1506 | { |
1510 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1507 | av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); |
1511 | } |
1508 | } |
1512 | |
1509 | |
1513 | static SV * |
1510 | INLINE SV * |
1514 | coro_deq (pTHX) |
1511 | coro_deq (pTHX) |
1515 | { |
1512 | { |
1516 | int prio; |
1513 | int prio; |
1517 | |
1514 | |
1518 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1515 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1537 | if (coro->flags & CF_READY) |
1534 | if (coro->flags & CF_READY) |
1538 | return 0; |
1535 | return 0; |
1539 | |
1536 | |
1540 | coro->flags |= CF_READY; |
1537 | coro->flags |= CF_READY; |
1541 | |
1538 | |
1542 | LOCK; |
|
|
1543 | |
|
|
1544 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1539 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1545 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1540 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1546 | |
1541 | |
1547 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1542 | coro_enq (aTHX_ coro); |
1548 | ++coro_nready; |
1543 | ++coro_nready; |
1549 | |
1544 | |
1550 | UNLOCK; |
|
|
1551 | |
|
|
1552 | if (sv_hook) |
1545 | if (sv_hook) |
1553 | { |
1546 | { |
1554 | dSP; |
1547 | dSP; |
1555 | |
1548 | |
1556 | ENTER; |
1549 | ENTER; |
… | |
… | |
1582 | { |
1575 | { |
1583 | SV *prev_sv, *next_sv; |
1576 | SV *prev_sv, *next_sv; |
1584 | |
1577 | |
1585 | for (;;) |
1578 | for (;;) |
1586 | { |
1579 | { |
1587 | LOCK; |
|
|
1588 | next_sv = coro_deq (aTHX); |
1580 | next_sv = coro_deq (aTHX); |
1589 | |
1581 | |
1590 | /* nothing to schedule: call the idle handler */ |
1582 | /* nothing to schedule: call the idle handler */ |
1591 | if (expect_false (!next_sv)) |
1583 | if (expect_false (!next_sv)) |
1592 | { |
1584 | { |
1593 | dSP; |
1585 | dSP; |
1594 | UNLOCK; |
|
|
1595 | |
1586 | |
1596 | ENTER; |
1587 | ENTER; |
1597 | SAVETMPS; |
1588 | SAVETMPS; |
1598 | |
1589 | |
1599 | PUSHMARK (SP); |
1590 | PUSHMARK (SP); |
… | |
… | |
1604 | FREETMPS; |
1595 | FREETMPS; |
1605 | LEAVE; |
1596 | LEAVE; |
1606 | continue; |
1597 | continue; |
1607 | } |
1598 | } |
1608 | |
1599 | |
1609 | ta->next = SvSTATE (next_sv); |
1600 | ta->next = SvSTATE_hv (next_sv); |
1610 | |
1601 | |
1611 | /* cannot transfer to destroyed coros, skip and look for next */ |
1602 | /* cannot transfer to destroyed coros, skip and look for next */ |
1612 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1603 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1613 | { |
1604 | { |
1614 | UNLOCK; |
|
|
1615 | SvREFCNT_dec (next_sv); |
1605 | SvREFCNT_dec (next_sv); |
1616 | /* coro_nready has already been taken care of by destroy */ |
1606 | /* coro_nready has already been taken care of by destroy */ |
1617 | continue; |
1607 | continue; |
1618 | } |
1608 | } |
1619 | |
1609 | |
1620 | --coro_nready; |
1610 | --coro_nready; |
1621 | UNLOCK; |
|
|
1622 | break; |
1611 | break; |
1623 | } |
1612 | } |
1624 | |
1613 | |
1625 | /* free this only after the transfer */ |
1614 | /* free this only after the transfer */ |
1626 | prev_sv = SvRV (coro_current); |
1615 | prev_sv = SvRV (coro_current); |
1627 | ta->prev = SvSTATE (prev_sv); |
1616 | ta->prev = SvSTATE_hv (prev_sv); |
1628 | TRANSFER_CHECK (*ta); |
1617 | TRANSFER_CHECK (*ta); |
1629 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1618 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1630 | ta->next->flags &= ~CF_READY; |
1619 | ta->next->flags &= ~CF_READY; |
1631 | SvRV_set (coro_current, next_sv); |
1620 | SvRV_set (coro_current, next_sv); |
1632 | |
1621 | |
1633 | LOCK; |
|
|
1634 | free_coro_mortal (aTHX); |
1622 | free_coro_mortal (aTHX); |
1635 | coro_mortal = prev_sv; |
1623 | coro_mortal = prev_sv; |
1636 | UNLOCK; |
|
|
1637 | } |
1624 | } |
1638 | |
1625 | |
1639 | INLINE void |
1626 | INLINE void |
1640 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1627 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1641 | { |
1628 | { |
… | |
… | |
1719 | PL_runops = RUNOPS_DEFAULT; |
1706 | PL_runops = RUNOPS_DEFAULT; |
1720 | else |
1707 | else |
1721 | coro->slot->runops = RUNOPS_DEFAULT; |
1708 | coro->slot->runops = RUNOPS_DEFAULT; |
1722 | } |
1709 | } |
1723 | } |
1710 | } |
1724 | |
|
|
1725 | #if 0 |
|
|
1726 | static int |
|
|
1727 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
1728 | { |
|
|
1729 | AV *padlist; |
|
|
1730 | AV *av = (AV *)mg->mg_obj; |
|
|
1731 | |
|
|
1732 | abort (); |
|
|
1733 | |
|
|
1734 | return 0; |
|
|
1735 | } |
|
|
1736 | |
|
|
1737 | static MGVTBL coro_gensub_vtbl = { |
|
|
1738 | 0, 0, 0, 0, |
|
|
1739 | coro_gensub_free |
|
|
1740 | }; |
|
|
1741 | #endif |
|
|
1742 | |
1711 | |
1743 | /*****************************************************************************/ |
1712 | /*****************************************************************************/ |
1744 | /* PerlIO::cede */ |
1713 | /* PerlIO::cede */ |
1745 | |
1714 | |
1746 | typedef struct |
1715 | typedef struct |
… | |
… | |
1848 | { |
1817 | { |
1849 | prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data); |
1818 | prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data); |
1850 | } |
1819 | } |
1851 | |
1820 | |
1852 | static void |
1821 | static void |
1853 | slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
1822 | slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1854 | { |
1823 | { |
1855 | assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1)); |
1824 | assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1)); |
1856 | |
1825 | |
1857 | frame->prepare = slf_prepare_set_stacklevel; |
1826 | frame->prepare = slf_prepare_set_stacklevel; |
1858 | frame->check = slf_check_nop; |
1827 | frame->check = slf_check_nop; |
… | |
… | |
1866 | |
1835 | |
1867 | prepare_transfer (aTHX_ ta, arg [0], arg [1]); |
1836 | prepare_transfer (aTHX_ ta, arg [0], arg [1]); |
1868 | } |
1837 | } |
1869 | |
1838 | |
1870 | static void |
1839 | static void |
1871 | slf_init_transfer (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
1840 | slf_init_transfer (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1872 | { |
1841 | { |
1873 | if (items != 2) |
1842 | if (items != 2) |
1874 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d,", items); |
1843 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d,", items); |
1875 | |
1844 | |
1876 | frame->prepare = slf_prepare_transfer; |
1845 | frame->prepare = slf_prepare_transfer; |
1877 | frame->check = slf_check_nop; |
1846 | frame->check = slf_check_nop; |
1878 | frame->data = (void *)arg; /* let's hope it will stay valid */ |
1847 | frame->data = (void *)arg; /* let's hope it will stay valid */ |
1879 | } |
1848 | } |
1880 | |
1849 | |
1881 | static void |
1850 | static void |
1882 | slf_init_schedule (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
1851 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1883 | { |
1852 | { |
1884 | frame->prepare = prepare_schedule; |
1853 | frame->prepare = prepare_schedule; |
1885 | frame->check = slf_check_nop; |
1854 | frame->check = slf_check_nop; |
1886 | } |
1855 | } |
1887 | |
1856 | |
1888 | static void |
1857 | static void |
1889 | slf_init_cede (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
1858 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1890 | { |
1859 | { |
1891 | frame->prepare = prepare_cede; |
1860 | frame->prepare = prepare_cede; |
1892 | frame->check = slf_check_nop; |
1861 | frame->check = slf_check_nop; |
1893 | } |
1862 | } |
1894 | |
1863 | |
1895 | static void |
1864 | static void |
1896 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
1865 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1897 | { |
1866 | { |
1898 | frame->prepare = prepare_cede_notself; |
1867 | frame->prepare = prepare_cede_notself; |
1899 | frame->check = slf_check_nop; |
1868 | frame->check = slf_check_nop; |
1900 | } |
1869 | } |
1901 | |
1870 | |
… | |
… | |
1940 | items = AvFILLp (av) + 1; |
1909 | items = AvFILLp (av) + 1; |
1941 | } |
1910 | } |
1942 | |
1911 | |
1943 | PUTBACK; |
1912 | PUTBACK; |
1944 | |
1913 | |
|
|
1914 | /* now call the init function, which needs to set up slf_frame */ |
1945 | ((coro_slf_cb)CvXSUBANY (GvCV (gv)).any_ptr) (aTHX_ &slf_frame, arg, items); |
1915 | ((coro_slf_cb)CvXSUBANY (GvCV (gv)).any_ptr) |
|
|
1916 | (aTHX_ &slf_frame, GvCV (gv), arg, items); |
1946 | } |
1917 | } |
1947 | |
1918 | |
1948 | /* now interpret the slf_frame */ |
1919 | /* now that we have a slf_frame, interpret it! */ |
1949 | /* we use a callback system not to make the code needlessly */ |
1920 | /* we use a callback system not to make the code needlessly */ |
1950 | /* complicated, but so we can run multiple perl coros from one cctx */ |
1921 | /* complicated, but so we can run multiple perl coros from one cctx */ |
1951 | |
1922 | |
1952 | do |
1923 | do |
1953 | { |
1924 | { |
… | |
… | |
1963 | { |
1934 | { |
1964 | dSP; |
1935 | dSP; |
1965 | SV **bot = PL_stack_base + checkmark; |
1936 | SV **bot = PL_stack_base + checkmark; |
1966 | int gimme = GIMME_V; |
1937 | int gimme = GIMME_V; |
1967 | |
1938 | |
1968 | slf_frame.prepare = 0; /* signal pp_slf that we need a new frame */ |
1939 | slf_frame.prepare = 0; /* invalidate the frame, so it gets initialised again next time */ |
1969 | |
1940 | |
1970 | /* make sure we put something on the stack in scalar context */ |
1941 | /* make sure we put something on the stack in scalar context */ |
1971 | if (gimme == G_SCALAR) |
1942 | if (gimme == G_SCALAR) |
1972 | { |
1943 | { |
1973 | if (sp == bot) |
1944 | if (sp == bot) |
… | |
… | |
2000 | |
1971 | |
2001 | /* we patch the op, and then re-run the whole call */ |
1972 | /* we patch the op, and then re-run the whole call */ |
2002 | /* we have to put the same argument on the stack for this to work */ |
1973 | /* we have to put the same argument on the stack for this to work */ |
2003 | /* and this will be done by pp_restore */ |
1974 | /* and this will be done by pp_restore */ |
2004 | slf_restore.op_next = (OP *)&slf_restore; |
1975 | slf_restore.op_next = (OP *)&slf_restore; |
2005 | slf_restore.op_type = OP_NULL; |
1976 | slf_restore.op_type = OP_CUSTOM; |
2006 | slf_restore.op_ppaddr = pp_restore; |
1977 | slf_restore.op_ppaddr = pp_restore; |
2007 | slf_restore.op_first = PL_op; |
1978 | slf_restore.op_first = PL_op; |
2008 | |
1979 | |
2009 | slf_arg0 = items > 0 ? SvREFCNT_inc (arg [0]) : 0; |
1980 | slf_arg0 = items > 0 ? SvREFCNT_inc (arg [0]) : 0; |
2010 | slf_arg1 = items > 1 ? SvREFCNT_inc (arg [1]) : 0; |
1981 | slf_arg1 = items > 1 ? SvREFCNT_inc (arg [1]) : 0; |
… | |
… | |
2015 | PL_op = (OP *)&slf_restore; |
1986 | PL_op = (OP *)&slf_restore; |
2016 | } |
1987 | } |
2017 | |
1988 | |
2018 | /*****************************************************************************/ |
1989 | /*****************************************************************************/ |
2019 | |
1990 | |
|
|
1991 | static void |
|
|
1992 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
|
|
1993 | { |
|
|
1994 | SV *count_sv = AvARRAY (av)[0]; |
|
|
1995 | IV count = SvIVX (count_sv); |
|
|
1996 | |
|
|
1997 | count += adjust; |
|
|
1998 | SvIVX (count_sv) = count; |
|
|
1999 | |
|
|
2000 | /* now wake up as many waiters as are expected to lock */ |
|
|
2001 | while (count > 0 && AvFILLp (av) > 0) |
|
|
2002 | { |
|
|
2003 | SV *cb; |
|
|
2004 | |
|
|
2005 | /* swap first two elements so we can shift a waiter */ |
|
|
2006 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
|
|
2007 | AvARRAY (av)[1] = count_sv; |
|
|
2008 | cb = av_shift (av); |
|
|
2009 | |
|
|
2010 | if (SvOBJECT (cb)) |
|
|
2011 | api_ready (aTHX_ cb); |
|
|
2012 | else |
|
|
2013 | croak ("callbacks not yet supported"); |
|
|
2014 | |
|
|
2015 | SvREFCNT_dec (cb); |
|
|
2016 | |
|
|
2017 | --count; |
|
|
2018 | } |
|
|
2019 | } |
|
|
2020 | |
|
|
2021 | static void |
|
|
2022 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
|
|
2023 | { |
|
|
2024 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
|
|
2025 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
|
|
2026 | } |
|
|
2027 | |
2020 | static int |
2028 | static int |
2021 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
2029 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
2022 | { |
2030 | { |
2023 | AV *av = (AV *)frame->data; |
2031 | AV *av = (AV *)frame->data; |
2024 | SV *count_sv = AvARRAY (av)[0]; |
2032 | SV *count_sv = AvARRAY (av)[0]; |
2025 | |
2033 | |
2026 | if (SvIVX (count_sv) > 0) |
2034 | if (SvIVX (count_sv) > 0) |
2027 | { |
2035 | { |
|
|
2036 | SvSTATE_current->on_destroy = 0; |
2028 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2037 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2029 | return 0; |
2038 | return 0; |
2030 | } |
2039 | } |
2031 | else |
2040 | else |
2032 | { |
2041 | { |
… | |
… | |
2043 | return 1; |
2052 | return 1; |
2044 | } |
2053 | } |
2045 | } |
2054 | } |
2046 | |
2055 | |
2047 | static void |
2056 | static void |
2048 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
2057 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2049 | { |
2058 | { |
2050 | AV *av = (AV *)SvRV (arg [0]); |
2059 | AV *av = (AV *)SvRV (arg [0]); |
2051 | |
2060 | |
2052 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2061 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2053 | { |
2062 | { |
2054 | frame->data = (void *)av; |
2063 | frame->data = (void *)av; |
2055 | frame->prepare = prepare_nop; |
2064 | frame->prepare = prepare_nop; |
|
|
2065 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2056 | } |
2066 | } |
2057 | else |
2067 | else |
2058 | { |
2068 | { |
2059 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2069 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2060 | |
2070 | |
2061 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2071 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2062 | frame->prepare = prepare_schedule; |
2072 | frame->prepare = prepare_schedule; |
|
|
2073 | |
|
|
2074 | /* to avoid race conditions when a woken-up coro gets terminated */ |
|
|
2075 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
|
|
2076 | assert (!SvSTATE_current->on_destroy);//D |
|
|
2077 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2063 | } |
2078 | } |
2064 | |
2079 | |
2065 | frame->check = slf_check_semaphore_down; |
2080 | frame->check = slf_check_semaphore_down; |
2066 | |
2081 | |
2067 | } |
2082 | } |
2068 | |
2083 | |
|
|
2084 | /*****************************************************************************/ |
|
|
2085 | |
|
|
2086 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
2087 | |
|
|
2088 | /* create a closure from XS, returns a code reference */ |
|
|
2089 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
2090 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
2091 | static SV * |
|
|
2092 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
2093 | { |
|
|
2094 | CV *cv = (CV *)NEWSV (0, 0); |
|
|
2095 | |
|
|
2096 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
2097 | |
|
|
2098 | CvANON_on (cv); |
|
|
2099 | CvISXSUB_on (cv); |
|
|
2100 | CvXSUB (cv) = xsub; |
|
|
2101 | GENSUB_ARG = arg; |
|
|
2102 | |
|
|
2103 | return newRV_noinc ((SV *)cv); |
|
|
2104 | } |
|
|
2105 | |
|
|
2106 | /*****************************************************************************/ |
|
|
2107 | |
2069 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2108 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2070 | |
2109 | |
2071 | PROTOTYPES: DISABLE |
2110 | PROTOTYPES: DISABLE |
2072 | |
2111 | |
2073 | BOOT: |
2112 | BOOT: |
2074 | { |
2113 | { |
2075 | #ifdef USE_ITHREADS |
2114 | #ifdef USE_ITHREADS |
2076 | MUTEX_INIT (&coro_lock); |
|
|
2077 | # if CORO_PTHREAD |
2115 | # if CORO_PTHREAD |
2078 | coro_thx = PERL_GET_CONTEXT; |
2116 | coro_thx = PERL_GET_CONTEXT; |
2079 | # endif |
2117 | # endif |
2080 | #endif |
2118 | #endif |
2081 | BOOT_PAGESIZE; |
2119 | BOOT_PAGESIZE; |
… | |
… | |
2101 | main_mainstack = PL_mainstack; |
2139 | main_mainstack = PL_mainstack; |
2102 | main_top_env = PL_top_env; |
2140 | main_top_env = PL_top_env; |
2103 | |
2141 | |
2104 | while (main_top_env->je_prev) |
2142 | while (main_top_env->je_prev) |
2105 | main_top_env = main_top_env->je_prev; |
2143 | main_top_env = main_top_env->je_prev; |
|
|
2144 | |
|
|
2145 | { |
|
|
2146 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
|
|
2147 | |
|
|
2148 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
|
|
2149 | hv_store_ent (PL_custom_op_names, slf, |
|
|
2150 | newSVpv ("coro_slf", 0), 0); |
|
|
2151 | |
|
|
2152 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
|
|
2153 | hv_store_ent (PL_custom_op_descs, slf, |
|
|
2154 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2155 | } |
2106 | |
2156 | |
2107 | coroapi.ver = CORO_API_VERSION; |
2157 | coroapi.ver = CORO_API_VERSION; |
2108 | coroapi.rev = CORO_API_REVISION; |
2158 | coroapi.rev = CORO_API_REVISION; |
2109 | |
2159 | |
2110 | coroapi.transfer = api_transfer; |
2160 | coroapi.transfer = api_transfer; |
… | |
… | |
2334 | |
2384 | |
2335 | void |
2385 | void |
2336 | force_cctx () |
2386 | force_cctx () |
2337 | PROTOTYPE: |
2387 | PROTOTYPE: |
2338 | CODE: |
2388 | CODE: |
2339 | struct coro *coro = SvSTATE (coro_current); |
|
|
2340 | coro->cctx->idle_sp = 0; |
2389 | SvSTATE_current->cctx->idle_sp = 0; |
2341 | |
2390 | |
2342 | void |
2391 | void |
2343 | swap_defsv (Coro::State self) |
2392 | swap_defsv (Coro::State self) |
2344 | PROTOTYPE: $ |
2393 | PROTOTYPE: $ |
2345 | ALIAS: |
2394 | ALIAS: |
… | |
… | |
2421 | |
2470 | |
2422 | void |
2471 | void |
2423 | _set_readyhook (SV *hook) |
2472 | _set_readyhook (SV *hook) |
2424 | PROTOTYPE: $ |
2473 | PROTOTYPE: $ |
2425 | CODE: |
2474 | CODE: |
2426 | LOCK; |
|
|
2427 | SvREFCNT_dec (coro_readyhook); |
2475 | SvREFCNT_dec (coro_readyhook); |
2428 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
2476 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
2429 | UNLOCK; |
|
|
2430 | |
2477 | |
2431 | int |
2478 | int |
2432 | prio (Coro::State coro, int newprio = 0) |
2479 | prio (Coro::State coro, int newprio = 0) |
2433 | PROTOTYPE: $;$ |
2480 | PROTOTYPE: $;$ |
2434 | ALIAS: |
2481 | ALIAS: |
… | |
… | |
2470 | # for async_pool speedup |
2517 | # for async_pool speedup |
2471 | void |
2518 | void |
2472 | _pool_1 (SV *cb) |
2519 | _pool_1 (SV *cb) |
2473 | CODE: |
2520 | CODE: |
2474 | { |
2521 | { |
2475 | struct coro *coro = SvSTATE (coro_current); |
|
|
2476 | HV *hv = (HV *)SvRV (coro_current); |
2522 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2523 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2477 | AV *defav = GvAV (PL_defgv); |
2524 | AV *defav = GvAV (PL_defgv); |
2478 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
2525 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
2479 | AV *invoke_av; |
2526 | AV *invoke_av; |
2480 | int i, len; |
2527 | int i, len; |
2481 | |
2528 | |
… | |
… | |
2508 | |
2555 | |
2509 | void |
2556 | void |
2510 | _pool_2 (SV *cb) |
2557 | _pool_2 (SV *cb) |
2511 | CODE: |
2558 | CODE: |
2512 | { |
2559 | { |
2513 | struct coro *coro = SvSTATE (coro_current); |
2560 | struct coro *coro = SvSTATE_current; |
2514 | |
2561 | |
2515 | sv_setsv (cb, &PL_sv_undef); |
2562 | sv_setsv (cb, &PL_sv_undef); |
2516 | |
2563 | |
2517 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2564 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2518 | coro->saved_deffh = 0; |
2565 | coro->saved_deffh = 0; |
… | |
… | |
2536 | api_trace (aTHX_ coro_current, 0); |
2583 | api_trace (aTHX_ coro_current, 0); |
2537 | |
2584 | |
2538 | av_push (av_async_pool, newSVsv (coro_current)); |
2585 | av_push (av_async_pool, newSVsv (coro_current)); |
2539 | } |
2586 | } |
2540 | |
2587 | |
2541 | #if 0 |
|
|
2542 | |
|
|
2543 | void |
|
|
2544 | _generator_call (...) |
|
|
2545 | PROTOTYPE: @ |
|
|
2546 | PPCODE: |
|
|
2547 | fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr); |
|
|
2548 | xxxx |
|
|
2549 | abort (); |
|
|
2550 | |
|
|
2551 | SV * |
|
|
2552 | gensub (SV *sub, ...) |
|
|
2553 | PROTOTYPE: &;@ |
|
|
2554 | CODE: |
|
|
2555 | { |
|
|
2556 | struct coro *coro; |
|
|
2557 | MAGIC *mg; |
|
|
2558 | CV *xcv; |
|
|
2559 | CV *ncv = (CV *)newSV_type (SVt_PVCV); |
|
|
2560 | int i; |
|
|
2561 | |
|
|
2562 | CvGV (ncv) = CvGV (cv); |
|
|
2563 | CvFILE (ncv) = CvFILE (cv); |
|
|
2564 | |
|
|
2565 | Newz (0, coro, 1, struct coro); |
|
|
2566 | coro->args = newAV (); |
|
|
2567 | coro->flags = CF_NEW; |
|
|
2568 | |
|
|
2569 | av_extend (coro->args, items - 1); |
|
|
2570 | for (i = 1; i < items; i++) |
|
|
2571 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2572 | |
|
|
2573 | CvISXSUB_on (ncv); |
|
|
2574 | CvXSUBANY (ncv).any_ptr = (void *)coro; |
|
|
2575 | |
|
|
2576 | xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV)); |
|
|
2577 | |
|
|
2578 | CvXSUB (ncv) = CvXSUB (xcv); |
|
|
2579 | CvANON_on (ncv); |
|
|
2580 | |
|
|
2581 | mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0); |
|
|
2582 | RETVAL = newRV_noinc ((SV *)ncv); |
|
|
2583 | } |
|
|
2584 | OUTPUT: |
|
|
2585 | RETVAL |
|
|
2586 | |
|
|
2587 | #endif |
|
|
2588 | |
|
|
2589 | |
2588 | |
2590 | MODULE = Coro::State PACKAGE = Coro::AIO |
2589 | MODULE = Coro::State PACKAGE = Coro::AIO |
2591 | |
2590 | |
2592 | void |
2591 | void |
2593 | _get_state (SV *self) |
2592 | _get_state (SV *self) |
… | |
… | |
2698 | void |
2697 | void |
2699 | up (SV *self, int adjust = 1) |
2698 | up (SV *self, int adjust = 1) |
2700 | ALIAS: |
2699 | ALIAS: |
2701 | adjust = 1 |
2700 | adjust = 1 |
2702 | CODE: |
2701 | CODE: |
2703 | { |
2702 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
2704 | AV *av = (AV *)SvRV (self); |
|
|
2705 | SV *count_sv = AvARRAY (av)[0]; |
|
|
2706 | IV count = SvIVX (count_sv); |
|
|
2707 | |
|
|
2708 | count += ix ? adjust : 1; |
|
|
2709 | SvIVX (count_sv) = count; |
|
|
2710 | |
|
|
2711 | /* now wake up as many waiters as possible */ |
|
|
2712 | while (count > 0 && AvFILLp (av) >= count) |
|
|
2713 | { |
|
|
2714 | SV *cb; |
|
|
2715 | |
|
|
2716 | /* swap first two elements so we can shift a waiter */ |
|
|
2717 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
|
|
2718 | AvARRAY (av)[1] = count_sv; |
|
|
2719 | cb = av_shift (av); |
|
|
2720 | |
|
|
2721 | if (SvOBJECT (cb)) |
|
|
2722 | api_ready (aTHX_ cb); |
|
|
2723 | else |
|
|
2724 | croak ("callbacks not yet supported"); |
|
|
2725 | |
|
|
2726 | SvREFCNT_dec (cb); |
|
|
2727 | } |
|
|
2728 | } |
|
|
2729 | |
2703 | |
2730 | void |
2704 | void |
2731 | down (SV *self) |
2705 | down (SV *self) |
2732 | CODE: |
2706 | CODE: |
2733 | api_execute_slf (aTHX_ cv, slf_init_semaphore_down, &ST (0), 1); |
2707 | api_execute_slf (aTHX_ cv, slf_init_semaphore_down, &ST (0), 1); |