… | |
… | |
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; |
… | |
… | |
181 | static struct CoroAPI coroapi; |
166 | static struct CoroAPI coroapi; |
182 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
167 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
183 | static JMPENV *main_top_env; |
168 | static JMPENV *main_top_env; |
184 | static HV *coro_state_stash, *coro_stash; |
169 | static HV *coro_state_stash, *coro_stash; |
185 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
170 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
186 | static volatile struct coro *transfer_next; |
|
|
187 | |
171 | |
188 | static GV *irsgv; /* $/ */ |
172 | static GV *irsgv; /* $/ */ |
189 | static GV *stdoutgv; /* *STDOUT */ |
173 | static GV *stdoutgv; /* *STDOUT */ |
190 | static SV *rv_diehook; |
174 | static SV *rv_diehook; |
191 | static SV *rv_warnhook; |
175 | static SV *rv_warnhook; |
… | |
… | |
266 | |
250 | |
267 | AV *args; /* data associated with this coroutine (initial args) */ |
251 | AV *args; /* data associated with this coroutine (initial args) */ |
268 | int refcnt; /* coroutines are refcounted, yes */ |
252 | int refcnt; /* coroutines are refcounted, yes */ |
269 | int flags; /* CF_ flags */ |
253 | int flags; /* CF_ flags */ |
270 | HV *hv; /* the perl hash associated with this coro, if any */ |
254 | HV *hv; /* the perl hash associated with this coro, if any */ |
|
|
255 | void (*on_destroy)(pTHX_ struct coro *coro); |
271 | |
256 | |
272 | /* statistics */ |
257 | /* statistics */ |
273 | int usecount; /* number of transfers to this coro */ |
258 | int usecount; /* number of transfers to this coro */ |
274 | |
259 | |
275 | /* coro process data */ |
260 | /* coro process data */ |
… | |
… | |
403 | static MGVTBL coro_cv_vtbl = { |
388 | static MGVTBL coro_cv_vtbl = { |
404 | 0, 0, 0, 0, |
389 | 0, 0, 0, 0, |
405 | coro_cv_free |
390 | coro_cv_free |
406 | }; |
391 | }; |
407 | |
392 | |
408 | #define CORO_MAGIC(sv, type) \ |
393 | #define CORO_MAGIC(sv, type) \ |
409 | SvMAGIC (sv) \ |
394 | expect_true (SvMAGIC (sv)) \ |
410 | ? SvMAGIC (sv)->mg_type == type \ |
395 | ? expect_true (SvMAGIC (sv)->mg_type == type) \ |
411 | ? SvMAGIC (sv) \ |
396 | ? SvMAGIC (sv) \ |
412 | : mg_find (sv, type) \ |
397 | : mg_find (sv, type) \ |
413 | : 0 |
398 | : 0 |
414 | |
399 | |
415 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
400 | #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) |
401 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
417 | |
402 | |
… | |
… | |
438 | mg = CORO_MAGIC_state (coro); |
423 | mg = CORO_MAGIC_state (coro); |
439 | return (struct coro *)mg->mg_ptr; |
424 | return (struct coro *)mg->mg_ptr; |
440 | } |
425 | } |
441 | |
426 | |
442 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
427 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
428 | |
|
|
429 | /* fastert than SvSTATE, but expects a coroutine hv */ |
|
|
430 | INLINE struct coro * |
|
|
431 | SvSTATE_hv (SV *sv) |
|
|
432 | { |
|
|
433 | MAGIC *mg = expect_true (SvMAGIC (sv)->mg_type == CORO_MAGIC_type_state) |
|
|
434 | ? SvMAGIC (sv) |
|
|
435 | : mg_find (sv, CORO_MAGIC_type_state); |
|
|
436 | |
|
|
437 | return (struct coro *)mg->mg_ptr; |
|
|
438 | } |
|
|
439 | |
|
|
440 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
443 | |
441 | |
444 | /* the next two functions merely cache the padlists */ |
442 | /* the next two functions merely cache the padlists */ |
445 | static void |
443 | static void |
446 | get_padlist (pTHX_ CV *cv) |
444 | get_padlist (pTHX_ CV *cv) |
447 | { |
445 | { |
… | |
… | |
811 | |
809 | |
812 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
810 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
813 | } |
811 | } |
814 | |
812 | |
815 | static void |
813 | static void |
816 | prepare_nop (aTHX_ struct coro_transfer_args *ta) |
814 | prepare_nop (pTHX_ struct coro_transfer_args *ta) |
817 | { |
815 | { |
818 | /* kind of mega-hacky, but works */ |
816 | /* kind of mega-hacky, but works */ |
819 | ta->next = ta->prev = (struct coro *)ta; |
817 | ta->next = ta->prev = (struct coro *)ta; |
820 | } |
818 | } |
821 | |
819 | |
822 | static int |
820 | static int |
823 | slf_check_nop (aTHX) |
821 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
824 | { |
822 | { |
825 | return 0; |
823 | return 0; |
826 | } |
824 | } |
827 | |
825 | |
828 | static void |
826 | static void |
… | |
… | |
929 | static int |
927 | static int |
930 | runops_trace (pTHX) |
928 | runops_trace (pTHX) |
931 | { |
929 | { |
932 | COP *oldcop = 0; |
930 | COP *oldcop = 0; |
933 | int oldcxix = -2; |
931 | int oldcxix = -2; |
934 | struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ |
932 | struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */ |
935 | coro_cctx *cctx = coro->cctx; |
933 | coro_cctx *cctx = coro->cctx; |
936 | |
934 | |
937 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
935 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
938 | { |
936 | { |
939 | PERL_ASYNC_CHECK (); |
937 | PERL_ASYNC_CHECK (); |
… | |
… | |
1088 | |
1086 | |
1089 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1087 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1090 | INLINE void |
1088 | INLINE void |
1091 | transfer_tail (pTHX) |
1089 | transfer_tail (pTHX) |
1092 | { |
1090 | { |
1093 | struct coro *next = (struct coro *)transfer_next; |
|
|
1094 | 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)); |
|
|
1096 | |
|
|
1097 | free_coro_mortal (aTHX); |
1091 | free_coro_mortal (aTHX); |
1098 | UNLOCK; |
|
|
1099 | |
|
|
1100 | if (expect_false (next->throw)) |
|
|
1101 | { |
|
|
1102 | SV *exception = sv_2mortal (next->throw); |
|
|
1103 | |
|
|
1104 | next->throw = 0; |
|
|
1105 | sv_setsv (ERRSV, exception); |
|
|
1106 | croak (0); |
|
|
1107 | } |
|
|
1108 | } |
1092 | } |
1109 | |
1093 | |
1110 | /* |
1094 | /* |
1111 | * this is a _very_ stripped down perl interpreter ;) |
1095 | * this is a _very_ stripped down perl interpreter ;) |
1112 | */ |
1096 | */ |
… | |
… | |
1127 | |
1111 | |
1128 | /* inject a fake subroutine call to cctx_init */ |
1112 | /* inject a fake subroutine call to cctx_init */ |
1129 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1113 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1130 | |
1114 | |
1131 | /* cctx_run is the alternative tail of transfer() */ |
1115 | /* cctx_run is the alternative tail of transfer() */ |
1132 | /* TODO: throwing an exception here might be deadly, VERIFY */ |
|
|
1133 | transfer_tail (aTHX); |
1116 | transfer_tail (aTHX); |
1134 | |
1117 | |
1135 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1118 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1136 | PL_restartop = PL_op; |
1119 | PL_restartop = PL_op; |
1137 | perl_run (PL_curinterp); |
1120 | perl_run (PL_curinterp); |
… | |
… | |
1320 | dSTACKLEVEL; |
1303 | dSTACKLEVEL; |
1321 | |
1304 | |
1322 | /* sometimes transfer is only called to set idle_sp */ |
1305 | /* sometimes transfer is only called to set idle_sp */ |
1323 | if (expect_false (!next)) |
1306 | if (expect_false (!next)) |
1324 | { |
1307 | { |
1325 | ((coro_cctx *)prev)->idle_sp = stacklevel; |
1308 | ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; |
1326 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1309 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1327 | } |
1310 | } |
1328 | else if (expect_true (prev != next)) |
1311 | else if (expect_true (prev != next)) |
1329 | { |
1312 | { |
1330 | coro_cctx *prev__cctx; |
1313 | coro_cctx *prev__cctx; |
… | |
… | |
1337 | prev->flags |= CF_RUNNING; |
1320 | prev->flags |= CF_RUNNING; |
1338 | } |
1321 | } |
1339 | |
1322 | |
1340 | prev->flags &= ~CF_RUNNING; |
1323 | prev->flags &= ~CF_RUNNING; |
1341 | next->flags |= CF_RUNNING; |
1324 | next->flags |= CF_RUNNING; |
1342 | |
|
|
1343 | LOCK; |
|
|
1344 | |
1325 | |
1345 | /* first get rid of the old state */ |
1326 | /* first get rid of the old state */ |
1346 | save_perl (aTHX_ prev); |
1327 | save_perl (aTHX_ prev); |
1347 | |
1328 | |
1348 | if (expect_false (next->flags & CF_NEW)) |
1329 | if (expect_false (next->flags & CF_NEW)) |
… | |
… | |
1357 | |
1338 | |
1358 | prev__cctx = prev->cctx; |
1339 | prev__cctx = prev->cctx; |
1359 | |
1340 | |
1360 | /* possibly untie and reuse the cctx */ |
1341 | /* possibly untie and reuse the cctx */ |
1361 | if (expect_true ( |
1342 | if (expect_true ( |
1362 | prev__cctx->idle_sp == stacklevel |
1343 | prev__cctx->idle_sp == (void *)stacklevel |
1363 | && !(prev__cctx->flags & CC_TRACE) |
1344 | && !(prev__cctx->flags & CC_TRACE) |
1364 | && !force_cctx |
1345 | && !force_cctx |
1365 | )) |
1346 | )) |
1366 | { |
1347 | { |
1367 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1348 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
… | |
… | |
1381 | ++next->usecount; |
1362 | ++next->usecount; |
1382 | |
1363 | |
1383 | if (expect_true (!next->cctx)) |
1364 | if (expect_true (!next->cctx)) |
1384 | next->cctx = cctx_get (aTHX); |
1365 | next->cctx = cctx_get (aTHX); |
1385 | |
1366 | |
1386 | assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next)); |
|
|
1387 | transfer_next = next; |
|
|
1388 | |
|
|
1389 | if (expect_false (prev__cctx != next->cctx)) |
1367 | if (expect_false (prev__cctx != next->cctx)) |
1390 | { |
1368 | { |
1391 | prev__cctx->top_env = PL_top_env; |
1369 | prev__cctx->top_env = PL_top_env; |
1392 | PL_top_env = next->cctx->top_env; |
1370 | PL_top_env = next->cctx->top_env; |
1393 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1371 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
… | |
… | |
1406 | coro_state_destroy (pTHX_ struct coro *coro) |
1384 | coro_state_destroy (pTHX_ struct coro *coro) |
1407 | { |
1385 | { |
1408 | if (coro->flags & CF_DESTROYED) |
1386 | if (coro->flags & CF_DESTROYED) |
1409 | return 0; |
1387 | return 0; |
1410 | |
1388 | |
|
|
1389 | if (coro->on_destroy) |
|
|
1390 | coro->on_destroy (aTHX_ coro); |
|
|
1391 | |
1411 | coro->flags |= CF_DESTROYED; |
1392 | coro->flags |= CF_DESTROYED; |
1412 | |
1393 | |
1413 | if (coro->flags & CF_READY) |
1394 | if (coro->flags & CF_READY) |
1414 | { |
1395 | { |
1415 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1396 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1416 | /* alternative: look through all ready queues and remove the coro */ |
1397 | /* alternative: look through all ready queues and remove the coro */ |
1417 | LOCK; |
|
|
1418 | --coro_nready; |
1398 | --coro_nready; |
1419 | UNLOCK; |
|
|
1420 | } |
1399 | } |
1421 | else |
1400 | else |
1422 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1401 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1423 | |
1402 | |
1424 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1403 | if (coro->mainstack && coro->mainstack != main_mainstack) |
… | |
… | |
1502 | TRANSFER (ta, 1); |
1481 | TRANSFER (ta, 1); |
1503 | } |
1482 | } |
1504 | |
1483 | |
1505 | /** Coro ********************************************************************/ |
1484 | /** Coro ********************************************************************/ |
1506 | |
1485 | |
1507 | static void |
1486 | INLINE void |
1508 | coro_enq (pTHX_ SV *coro_sv) |
1487 | coro_enq (pTHX_ struct coro *coro) |
1509 | { |
1488 | { |
1510 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1489 | av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); |
1511 | } |
1490 | } |
1512 | |
1491 | |
1513 | static SV * |
1492 | INLINE SV * |
1514 | coro_deq (pTHX) |
1493 | coro_deq (pTHX) |
1515 | { |
1494 | { |
1516 | int prio; |
1495 | int prio; |
1517 | |
1496 | |
1518 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1497 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1537 | if (coro->flags & CF_READY) |
1516 | if (coro->flags & CF_READY) |
1538 | return 0; |
1517 | return 0; |
1539 | |
1518 | |
1540 | coro->flags |= CF_READY; |
1519 | coro->flags |= CF_READY; |
1541 | |
1520 | |
1542 | LOCK; |
|
|
1543 | |
|
|
1544 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1521 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1545 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1522 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1546 | |
1523 | |
1547 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1524 | coro_enq (aTHX_ coro); |
1548 | ++coro_nready; |
1525 | ++coro_nready; |
1549 | |
1526 | |
1550 | UNLOCK; |
|
|
1551 | |
|
|
1552 | if (sv_hook) |
1527 | if (sv_hook) |
1553 | { |
1528 | { |
1554 | dSP; |
1529 | dSP; |
1555 | |
1530 | |
1556 | ENTER; |
1531 | ENTER; |
… | |
… | |
1582 | { |
1557 | { |
1583 | SV *prev_sv, *next_sv; |
1558 | SV *prev_sv, *next_sv; |
1584 | |
1559 | |
1585 | for (;;) |
1560 | for (;;) |
1586 | { |
1561 | { |
1587 | LOCK; |
|
|
1588 | next_sv = coro_deq (aTHX); |
1562 | next_sv = coro_deq (aTHX); |
1589 | |
1563 | |
1590 | /* nothing to schedule: call the idle handler */ |
1564 | /* nothing to schedule: call the idle handler */ |
1591 | if (expect_false (!next_sv)) |
1565 | if (expect_false (!next_sv)) |
1592 | { |
1566 | { |
1593 | dSP; |
1567 | dSP; |
1594 | UNLOCK; |
|
|
1595 | |
1568 | |
1596 | ENTER; |
1569 | ENTER; |
1597 | SAVETMPS; |
1570 | SAVETMPS; |
1598 | |
1571 | |
1599 | PUSHMARK (SP); |
1572 | PUSHMARK (SP); |
… | |
… | |
1604 | FREETMPS; |
1577 | FREETMPS; |
1605 | LEAVE; |
1578 | LEAVE; |
1606 | continue; |
1579 | continue; |
1607 | } |
1580 | } |
1608 | |
1581 | |
1609 | ta->next = SvSTATE (next_sv); |
1582 | ta->next = SvSTATE_hv (next_sv); |
1610 | |
1583 | |
1611 | /* cannot transfer to destroyed coros, skip and look for next */ |
1584 | /* cannot transfer to destroyed coros, skip and look for next */ |
1612 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1585 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1613 | { |
1586 | { |
1614 | UNLOCK; |
|
|
1615 | SvREFCNT_dec (next_sv); |
1587 | SvREFCNT_dec (next_sv); |
1616 | /* coro_nready has already been taken care of by destroy */ |
1588 | /* coro_nready has already been taken care of by destroy */ |
1617 | continue; |
1589 | continue; |
1618 | } |
1590 | } |
1619 | |
1591 | |
1620 | --coro_nready; |
1592 | --coro_nready; |
1621 | UNLOCK; |
|
|
1622 | break; |
1593 | break; |
1623 | } |
1594 | } |
1624 | |
1595 | |
1625 | /* free this only after the transfer */ |
1596 | /* free this only after the transfer */ |
1626 | prev_sv = SvRV (coro_current); |
1597 | prev_sv = SvRV (coro_current); |
1627 | ta->prev = SvSTATE (prev_sv); |
1598 | ta->prev = SvSTATE_hv (prev_sv); |
1628 | TRANSFER_CHECK (*ta); |
1599 | TRANSFER_CHECK (*ta); |
1629 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1600 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1630 | ta->next->flags &= ~CF_READY; |
1601 | ta->next->flags &= ~CF_READY; |
1631 | SvRV_set (coro_current, next_sv); |
1602 | SvRV_set (coro_current, next_sv); |
1632 | |
1603 | |
1633 | LOCK; |
|
|
1634 | free_coro_mortal (aTHX); |
1604 | free_coro_mortal (aTHX); |
1635 | coro_mortal = prev_sv; |
1605 | coro_mortal = prev_sv; |
1636 | UNLOCK; |
|
|
1637 | } |
1606 | } |
1638 | |
1607 | |
1639 | INLINE void |
1608 | INLINE void |
1640 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1609 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1641 | { |
1610 | { |
… | |
… | |
1719 | PL_runops = RUNOPS_DEFAULT; |
1688 | PL_runops = RUNOPS_DEFAULT; |
1720 | else |
1689 | else |
1721 | coro->slot->runops = RUNOPS_DEFAULT; |
1690 | coro->slot->runops = RUNOPS_DEFAULT; |
1722 | } |
1691 | } |
1723 | } |
1692 | } |
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 | |
1693 | |
1743 | /*****************************************************************************/ |
1694 | /*****************************************************************************/ |
1744 | /* PerlIO::cede */ |
1695 | /* PerlIO::cede */ |
1745 | |
1696 | |
1746 | typedef struct |
1697 | typedef struct |
… | |
… | |
1815 | PerlIOBuf_set_ptrcnt, |
1766 | PerlIOBuf_set_ptrcnt, |
1816 | }; |
1767 | }; |
1817 | |
1768 | |
1818 | /*****************************************************************************/ |
1769 | /*****************************************************************************/ |
1819 | |
1770 | |
1820 | static const CV *slf_cv; /* for quick consistency check */ |
|
|
1821 | |
|
|
1822 | static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ |
1771 | static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ |
|
|
1772 | static const CV *slf_cv; |
1823 | static SV *slf_arg0; |
1773 | static SV *slf_arg0; |
1824 | static SV *slf_arg1; |
1774 | static SV *slf_arg1; |
1825 | static SV *slf_arg2; |
1775 | static SV *slf_arg2; |
|
|
1776 | static I32 slf_ax; /* top of stack, for restore */ |
1826 | |
1777 | |
1827 | /* this restores the stack in the case we patched the entersub, to */ |
1778 | /* this restores the stack in the case we patched the entersub, to */ |
1828 | /* recreate the stack frame as perl will on following calls */ |
1779 | /* recreate the stack frame as perl will on following calls */ |
1829 | /* since entersub cleared the stack */ |
1780 | /* since entersub cleared the stack */ |
1830 | static OP * |
1781 | static OP * |
1831 | pp_restore (pTHX) |
1782 | pp_restore (pTHX) |
1832 | { |
1783 | { |
1833 | dSP; |
1784 | SV **SP = PL_stack_base + slf_ax; |
1834 | |
1785 | |
1835 | PUSHMARK (SP); |
1786 | PUSHMARK (SP); |
1836 | |
1787 | |
1837 | EXTEND (SP, 3); |
1788 | EXTEND (SP, 3); |
1838 | if (slf_arg0) PUSHs (sv_2mortal (slf_arg0)); |
1789 | if (slf_arg0) PUSHs (sv_2mortal (slf_arg0)); |
… | |
… | |
1848 | { |
1799 | { |
1849 | prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data); |
1800 | prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data); |
1850 | } |
1801 | } |
1851 | |
1802 | |
1852 | static void |
1803 | static void |
1853 | slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
1804 | slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1854 | { |
1805 | { |
1855 | assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1)); |
1806 | assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1)); |
1856 | |
1807 | |
1857 | frame->prepare = slf_prepare_set_stacklevel; |
1808 | frame->prepare = slf_prepare_set_stacklevel; |
1858 | frame->check = slf_check_nop; |
1809 | frame->check = slf_check_nop; |
… | |
… | |
1862 | static void |
1813 | static void |
1863 | slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) |
1814 | slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) |
1864 | { |
1815 | { |
1865 | SV **arg = (SV **)slf_frame.data; |
1816 | SV **arg = (SV **)slf_frame.data; |
1866 | |
1817 | |
1867 | prepare_transfer (ta, arg [0], arg [1]); |
1818 | prepare_transfer (aTHX_ ta, arg [0], arg [1]); |
1868 | } |
|
|
1869 | |
1819 | |
|
|
1820 | /* if the destination has ->throw set, then copy it */ |
|
|
1821 | /* into the current coro's throw slot, so it will be raised */ |
|
|
1822 | /* after the schedule */ |
|
|
1823 | if (expect_false (ta->next->throw)) |
|
|
1824 | { |
|
|
1825 | struct coro *coro = SvSTATE_current; |
|
|
1826 | SvREFCNT_dec (coro->throw); |
|
|
1827 | coro->throw = ta->next->throw; |
|
|
1828 | ta->next->throw = 0; |
|
|
1829 | } |
|
|
1830 | } |
|
|
1831 | |
1870 | static void |
1832 | static void |
1871 | slf_init_transfer (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
1833 | slf_init_transfer (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1872 | { |
1834 | { |
1873 | if (items != 2) |
1835 | if (items != 2) |
1874 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d,", items); |
1836 | croak ("Coro::State::transfer (prev, next) expects two arguments, not %d,", items); |
1875 | |
1837 | |
1876 | frame->prepare = slf_prepare_transfer; |
1838 | frame->prepare = slf_prepare_transfer; |
1877 | frame->check = slf_check_nop; |
1839 | frame->check = slf_check_nop; |
1878 | frame->data = (void *)arg; /* let's hope it will stay valid */ |
1840 | frame->data = (void *)arg; /* let's hope it will stay valid */ |
1879 | } |
1841 | } |
1880 | |
1842 | |
1881 | static void |
1843 | static void |
1882 | slf_init_schedule (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
1844 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1883 | { |
1845 | { |
1884 | frame->prepare = prepare_schedule; |
1846 | frame->prepare = prepare_schedule; |
1885 | frame->check = slf_check_nop; |
1847 | frame->check = slf_check_nop; |
1886 | } |
1848 | } |
1887 | |
1849 | |
1888 | static void |
1850 | static void |
1889 | slf_init_cede (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
1851 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1890 | { |
1852 | { |
1891 | frame->prepare = prepare_cede; |
1853 | frame->prepare = prepare_cede; |
1892 | frame->check = slf_check_nop; |
1854 | frame->check = slf_check_nop; |
1893 | } |
1855 | } |
1894 | |
1856 | |
1895 | static void |
1857 | static void |
1896 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
1858 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1897 | { |
1859 | { |
1898 | frame->prepare = prepare_cede_notself; |
1860 | frame->prepare = prepare_cede_notself; |
1899 | frame->check = slf_check_nop; |
1861 | frame->check = slf_check_nop; |
1900 | } |
1862 | } |
1901 | |
1863 | |
… | |
… | |
1927 | /* do a quick consistency check on the "function" object, and if it isn't */ |
1889 | /* do a quick consistency check on the "function" object, and if it isn't */ |
1928 | /* for us, divert to the real entersub */ |
1890 | /* for us, divert to the real entersub */ |
1929 | if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF)) |
1891 | if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF)) |
1930 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
1892 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
1931 | |
1893 | |
1932 | /* pop args */ |
|
|
1933 | SP = PL_stack_base + POPMARK; |
|
|
1934 | |
|
|
1935 | if (!(PL_op->op_flags & OPf_STACKED)) |
1894 | if (!(PL_op->op_flags & OPf_STACKED)) |
1936 | { |
1895 | { |
1937 | /* ampersand-form of call, use @_ instead of stack */ |
1896 | /* ampersand-form of call, use @_ instead of stack */ |
1938 | AV *av = GvAV (PL_defgv); |
1897 | AV *av = GvAV (PL_defgv); |
1939 | arg = AvARRAY (av); |
1898 | arg = AvARRAY (av); |
1940 | items = AvFILLp (av) + 1; |
1899 | items = AvFILLp (av) + 1; |
1941 | } |
1900 | } |
1942 | |
1901 | |
|
|
1902 | /* now call the init function, which needs to set up slf_frame */ |
|
|
1903 | ((coro_slf_cb)CvXSUBANY (GvCV (gv)).any_ptr) |
|
|
1904 | (aTHX_ &slf_frame, GvCV (gv), arg, items); |
|
|
1905 | |
|
|
1906 | /* pop args */ |
|
|
1907 | SP = PL_stack_base + POPMARK; |
|
|
1908 | |
1943 | PUTBACK; |
1909 | PUTBACK; |
1944 | |
|
|
1945 | ((coro_slf_cb)CvXSUBANY (GvCV (gv)).any_ptr) (aTHX_ &slf_frame, arg, items); |
|
|
1946 | } |
1910 | } |
1947 | |
1911 | |
1948 | /* now interpret the slf_frame */ |
1912 | /* now that we have a slf_frame, interpret it! */ |
1949 | /* we use a callback system not to make the code needlessly */ |
1913 | /* we use a callback system not to make the code needlessly */ |
1950 | /* complicated, but so we can run multiple perl coros from one cctx */ |
1914 | /* complicated, but so we can run multiple perl coros from one cctx */ |
1951 | |
1915 | |
1952 | do |
1916 | do |
1953 | { |
1917 | { |
… | |
… | |
1963 | { |
1927 | { |
1964 | dSP; |
1928 | dSP; |
1965 | SV **bot = PL_stack_base + checkmark; |
1929 | SV **bot = PL_stack_base + checkmark; |
1966 | int gimme = GIMME_V; |
1930 | int gimme = GIMME_V; |
1967 | |
1931 | |
1968 | slf_frame.prepare = 0; /* signal pp_slf that we need a new frame */ |
1932 | slf_frame.prepare = 0; /* invalidate the frame, so it gets initialised again next time */ |
1969 | |
1933 | |
1970 | /* make sure we put something on the stack in scalar context */ |
1934 | /* make sure we put something on the stack in scalar context */ |
1971 | if (gimme == G_SCALAR) |
1935 | if (gimme == G_SCALAR) |
1972 | { |
1936 | { |
1973 | if (sp == bot) |
1937 | if (sp == bot) |
… | |
… | |
1977 | } |
1941 | } |
1978 | |
1942 | |
1979 | PUTBACK; |
1943 | PUTBACK; |
1980 | } |
1944 | } |
1981 | |
1945 | |
|
|
1946 | { |
|
|
1947 | struct coro *coro = SvSTATE_current; |
|
|
1948 | |
|
|
1949 | if (expect_false (coro->throw)) |
|
|
1950 | { |
|
|
1951 | SV *exception = sv_2mortal (coro->throw); |
|
|
1952 | |
|
|
1953 | coro->throw = 0; |
|
|
1954 | sv_setsv (ERRSV, exception); |
|
|
1955 | croak (0); |
|
|
1956 | } |
|
|
1957 | } |
|
|
1958 | |
1982 | return NORMAL; |
1959 | return NORMAL; |
1983 | } |
1960 | } |
1984 | |
1961 | |
1985 | static void |
1962 | static void |
1986 | api_execute_slf (pTHX_ CV *cv, coro_slf_cb init_cb, SV **arg, int items) |
1963 | api_execute_slf (pTHX_ CV *cv, coro_slf_cb init_cb, I32 ax) |
1987 | { |
1964 | { |
|
|
1965 | SV **arg = PL_stack_base + ax; |
|
|
1966 | int items = PL_stack_sp - arg + 1; |
|
|
1967 | |
1988 | assert (("FATAL: SLF call with illegal CV value", !CvANON (cv))); |
1968 | assert (("FATAL: SLF call with illegal CV value", !CvANON (cv))); |
1989 | |
1969 | |
1990 | if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] |
1970 | if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] |
1991 | && PL_op->op_ppaddr != pp_slf) |
1971 | && PL_op->op_ppaddr != pp_slf) |
1992 | croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught"); |
1972 | croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught"); |
1993 | |
1973 | |
|
|
1974 | #if 0 |
1994 | if (items > 3) |
1975 | if (items > 3) |
1995 | croak ("Coro only supports up to three arguments to SLF functions currently (not %d), caught", items); |
1976 | croak ("Coro only supports up to three arguments to SLF functions currently (not %d), caught", items); |
|
|
1977 | #endif |
1996 | |
1978 | |
1997 | CvFLAGS (cv) |= CVf_SLF; |
1979 | CvFLAGS (cv) |= CVf_SLF; |
1998 | CvXSUBANY (cv).any_ptr = (void *)init_cb; |
1980 | CvXSUBANY (cv).any_ptr = (void *)init_cb; |
1999 | slf_cv = cv; |
1981 | slf_cv = cv; |
2000 | |
1982 | |
2001 | /* we patch the op, and then re-run the whole call */ |
1983 | /* 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 */ |
1984 | /* we have to put the same argument on the stack for this to work */ |
2003 | /* and this will be done by pp_restore */ |
1985 | /* and this will be done by pp_restore */ |
2004 | slf_restore.op_next = (OP *)&slf_restore; |
1986 | slf_restore.op_next = (OP *)&slf_restore; |
2005 | slf_restore.op_type = OP_NULL; |
1987 | slf_restore.op_type = OP_CUSTOM; |
2006 | slf_restore.op_ppaddr = pp_restore; |
1988 | slf_restore.op_ppaddr = pp_restore; |
2007 | slf_restore.op_first = PL_op; |
1989 | slf_restore.op_first = PL_op; |
2008 | |
1990 | |
|
|
1991 | slf_ax = ax - 1; /* undo the ax++ inside dAXMARK */ |
2009 | slf_arg0 = items > 0 ? SvREFCNT_inc (arg [0]) : 0; |
1992 | slf_arg0 = items > 0 ? SvREFCNT_inc (arg [0]) : 0; |
2010 | slf_arg1 = items > 1 ? SvREFCNT_inc (arg [1]) : 0; |
1993 | slf_arg1 = items > 1 ? SvREFCNT_inc (arg [1]) : 0; |
2011 | slf_arg2 = items > 2 ? SvREFCNT_inc (arg [2]) : 0; |
1994 | slf_arg2 = items > 2 ? SvREFCNT_inc (arg [2]) : 0; |
2012 | |
1995 | |
2013 | PL_op->op_ppaddr = pp_slf; |
1996 | PL_op->op_ppaddr = pp_slf; |
|
|
1997 | PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ |
2014 | |
1998 | |
2015 | PL_op = (OP *)&slf_restore; |
1999 | PL_op = (OP *)&slf_restore; |
2016 | } |
2000 | } |
2017 | |
2001 | |
2018 | /*****************************************************************************/ |
2002 | /*****************************************************************************/ |
|
|
2003 | |
|
|
2004 | static void |
|
|
2005 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
|
|
2006 | { |
|
|
2007 | SV *count_sv = AvARRAY (av)[0]; |
|
|
2008 | IV count = SvIVX (count_sv); |
|
|
2009 | |
|
|
2010 | count += adjust; |
|
|
2011 | SvIVX (count_sv) = count; |
|
|
2012 | |
|
|
2013 | /* now wake up as many waiters as are expected to lock */ |
|
|
2014 | while (count > 0 && AvFILLp (av) > 0) |
|
|
2015 | { |
|
|
2016 | SV *cb; |
|
|
2017 | |
|
|
2018 | /* swap first two elements so we can shift a waiter */ |
|
|
2019 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
|
|
2020 | AvARRAY (av)[1] = count_sv; |
|
|
2021 | cb = av_shift (av); |
|
|
2022 | |
|
|
2023 | if (SvOBJECT (cb)) |
|
|
2024 | api_ready (aTHX_ cb); |
|
|
2025 | else |
|
|
2026 | croak ("callbacks not yet supported"); |
|
|
2027 | |
|
|
2028 | SvREFCNT_dec (cb); |
|
|
2029 | |
|
|
2030 | --count; |
|
|
2031 | } |
|
|
2032 | } |
|
|
2033 | |
|
|
2034 | static void |
|
|
2035 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
|
|
2036 | { |
|
|
2037 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
|
|
2038 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
|
|
2039 | } |
2019 | |
2040 | |
2020 | static int |
2041 | static int |
2021 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
2042 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
2022 | { |
2043 | { |
2023 | AV *av = (AV *)frame->data; |
2044 | AV *av = (AV *)frame->data; |
2024 | SV *count_sv = AvARRAY (av)[0]; |
2045 | SV *count_sv = AvARRAY (av)[0]; |
2025 | |
2046 | |
2026 | if (SvIVX (count_sv) > 0) |
2047 | if (SvIVX (count_sv) > 0) |
2027 | { |
2048 | { |
|
|
2049 | SvSTATE_current->on_destroy = 0; |
2028 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2050 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2029 | return 0; |
2051 | return 0; |
2030 | } |
2052 | } |
2031 | else |
2053 | else |
2032 | { |
2054 | { |
… | |
… | |
2043 | return 1; |
2065 | return 1; |
2044 | } |
2066 | } |
2045 | } |
2067 | } |
2046 | |
2068 | |
2047 | static void |
2069 | static void |
2048 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
2070 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2049 | { |
2071 | { |
2050 | AV *av = (AV *)SvRV (arg [0]); |
2072 | AV *av = (AV *)SvRV (arg [0]); |
2051 | |
2073 | |
2052 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2074 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2053 | { |
2075 | { |
2054 | frame->data = (void *)av; |
2076 | frame->data = (void *)av; |
2055 | frame->prepare = prepare_nop; |
2077 | frame->prepare = prepare_nop; |
|
|
2078 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2056 | } |
2079 | } |
2057 | else |
2080 | else |
2058 | { |
2081 | { |
2059 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2082 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2060 | |
2083 | |
2061 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2084 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2062 | frame->prepare = prepare_schedule; |
2085 | frame->prepare = prepare_schedule; |
|
|
2086 | |
|
|
2087 | /* to avoid race conditions when a woken-up coro gets terminated */ |
|
|
2088 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
|
|
2089 | assert (!SvSTATE_current->on_destroy);//D |
|
|
2090 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2063 | } |
2091 | } |
2064 | |
2092 | |
2065 | frame->check = slf_check_semaphore_down; |
2093 | frame->check = slf_check_semaphore_down; |
2066 | |
2094 | |
2067 | } |
2095 | } |
2068 | |
2096 | |
|
|
2097 | /*****************************************************************************/ |
|
|
2098 | |
|
|
2099 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
2100 | |
|
|
2101 | /* create a closure from XS, returns a code reference */ |
|
|
2102 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
2103 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
2104 | static SV * |
|
|
2105 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
2106 | { |
|
|
2107 | CV *cv = (CV *)NEWSV (0, 0); |
|
|
2108 | |
|
|
2109 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
2110 | |
|
|
2111 | CvANON_on (cv); |
|
|
2112 | CvISXSUB_on (cv); |
|
|
2113 | CvXSUB (cv) = xsub; |
|
|
2114 | GENSUB_ARG = arg; |
|
|
2115 | |
|
|
2116 | return newRV_noinc ((SV *)cv); |
|
|
2117 | } |
|
|
2118 | |
|
|
2119 | /*****************************************************************************/ |
|
|
2120 | |
2069 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2121 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2070 | |
2122 | |
2071 | PROTOTYPES: DISABLE |
2123 | PROTOTYPES: DISABLE |
2072 | |
2124 | |
2073 | BOOT: |
2125 | BOOT: |
2074 | { |
2126 | { |
2075 | #ifdef USE_ITHREADS |
2127 | #ifdef USE_ITHREADS |
2076 | MUTEX_INIT (&coro_lock); |
|
|
2077 | # if CORO_PTHREAD |
2128 | # if CORO_PTHREAD |
2078 | coro_thx = PERL_GET_CONTEXT; |
2129 | coro_thx = PERL_GET_CONTEXT; |
2079 | # endif |
2130 | # endif |
2080 | #endif |
2131 | #endif |
2081 | BOOT_PAGESIZE; |
2132 | BOOT_PAGESIZE; |
… | |
… | |
2101 | main_mainstack = PL_mainstack; |
2152 | main_mainstack = PL_mainstack; |
2102 | main_top_env = PL_top_env; |
2153 | main_top_env = PL_top_env; |
2103 | |
2154 | |
2104 | while (main_top_env->je_prev) |
2155 | while (main_top_env->je_prev) |
2105 | main_top_env = main_top_env->je_prev; |
2156 | main_top_env = main_top_env->je_prev; |
|
|
2157 | |
|
|
2158 | { |
|
|
2159 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
|
|
2160 | |
|
|
2161 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
|
|
2162 | hv_store_ent (PL_custom_op_names, slf, |
|
|
2163 | newSVpv ("coro_slf", 0), 0); |
|
|
2164 | |
|
|
2165 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
|
|
2166 | hv_store_ent (PL_custom_op_descs, slf, |
|
|
2167 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2168 | } |
2106 | |
2169 | |
2107 | coroapi.ver = CORO_API_VERSION; |
2170 | coroapi.ver = CORO_API_VERSION; |
2108 | coroapi.rev = CORO_API_REVISION; |
2171 | coroapi.rev = CORO_API_REVISION; |
2109 | |
2172 | |
2110 | coroapi.transfer = api_transfer; |
2173 | coroapi.transfer = api_transfer; |
… | |
… | |
2158 | RETVAL |
2221 | RETVAL |
2159 | |
2222 | |
2160 | void |
2223 | void |
2161 | _set_stacklevel (...) |
2224 | _set_stacklevel (...) |
2162 | CODE: |
2225 | CODE: |
2163 | api_execute_slf (aTHX_ cv, slf_init_set_stacklevel, &ST (0), items); |
2226 | CORO_EXECUTE_SLF_XS (slf_init_set_stacklevel); |
2164 | |
2227 | |
2165 | void |
2228 | void |
2166 | transfer (...) |
2229 | transfer (...) |
2167 | PROTOTYPE: $$ |
2230 | PROTOTYPE: $$ |
2168 | CODE: |
2231 | CODE: |
2169 | api_execute_slf (aTHX_ cv, slf_init_transfer, &ST (0), items); |
2232 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
2170 | |
2233 | |
2171 | bool |
2234 | bool |
2172 | _destroy (SV *coro_sv) |
2235 | _destroy (SV *coro_sv) |
2173 | CODE: |
2236 | CODE: |
2174 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
2237 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
… | |
… | |
2334 | |
2397 | |
2335 | void |
2398 | void |
2336 | force_cctx () |
2399 | force_cctx () |
2337 | PROTOTYPE: |
2400 | PROTOTYPE: |
2338 | CODE: |
2401 | CODE: |
2339 | struct coro *coro = SvSTATE (coro_current); |
|
|
2340 | coro->cctx->idle_sp = 0; |
2402 | SvSTATE_current->cctx->idle_sp = 0; |
2341 | |
2403 | |
2342 | void |
2404 | void |
2343 | swap_defsv (Coro::State self) |
2405 | swap_defsv (Coro::State self) |
2344 | PROTOTYPE: $ |
2406 | PROTOTYPE: $ |
2345 | ALIAS: |
2407 | ALIAS: |
… | |
… | |
2398 | } |
2460 | } |
2399 | |
2461 | |
2400 | void |
2462 | void |
2401 | schedule (...) |
2463 | schedule (...) |
2402 | CODE: |
2464 | CODE: |
2403 | api_execute_slf (aTHX_ cv, slf_init_schedule, &ST (0), 0); |
2465 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
2404 | |
2466 | |
2405 | void |
2467 | void |
2406 | cede (...) |
2468 | cede (...) |
2407 | CODE: |
2469 | CODE: |
2408 | api_execute_slf (aTHX_ cv, slf_init_cede, &ST (0), 0); |
2470 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
2409 | |
2471 | |
2410 | void |
2472 | void |
2411 | cede_notself (...) |
2473 | cede_notself (...) |
2412 | CODE: |
2474 | CODE: |
2413 | api_execute_slf (aTHX_ cv, slf_init_cede_notself, &ST (0), 0); |
2475 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
2414 | |
2476 | |
2415 | void |
2477 | void |
2416 | _set_current (SV *current) |
2478 | _set_current (SV *current) |
2417 | PROTOTYPE: $ |
2479 | PROTOTYPE: $ |
2418 | CODE: |
2480 | CODE: |
… | |
… | |
2421 | |
2483 | |
2422 | void |
2484 | void |
2423 | _set_readyhook (SV *hook) |
2485 | _set_readyhook (SV *hook) |
2424 | PROTOTYPE: $ |
2486 | PROTOTYPE: $ |
2425 | CODE: |
2487 | CODE: |
2426 | LOCK; |
|
|
2427 | SvREFCNT_dec (coro_readyhook); |
2488 | SvREFCNT_dec (coro_readyhook); |
2428 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
2489 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
2429 | UNLOCK; |
|
|
2430 | |
2490 | |
2431 | int |
2491 | int |
2432 | prio (Coro::State coro, int newprio = 0) |
2492 | prio (Coro::State coro, int newprio = 0) |
2433 | PROTOTYPE: $;$ |
2493 | PROTOTYPE: $;$ |
2434 | ALIAS: |
2494 | ALIAS: |
… | |
… | |
2470 | # for async_pool speedup |
2530 | # for async_pool speedup |
2471 | void |
2531 | void |
2472 | _pool_1 (SV *cb) |
2532 | _pool_1 (SV *cb) |
2473 | CODE: |
2533 | CODE: |
2474 | { |
2534 | { |
2475 | struct coro *coro = SvSTATE (coro_current); |
|
|
2476 | HV *hv = (HV *)SvRV (coro_current); |
2535 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2536 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2477 | AV *defav = GvAV (PL_defgv); |
2537 | AV *defav = GvAV (PL_defgv); |
2478 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
2538 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
2479 | AV *invoke_av; |
2539 | AV *invoke_av; |
2480 | int i, len; |
2540 | int i, len; |
2481 | |
2541 | |
… | |
… | |
2508 | |
2568 | |
2509 | void |
2569 | void |
2510 | _pool_2 (SV *cb) |
2570 | _pool_2 (SV *cb) |
2511 | CODE: |
2571 | CODE: |
2512 | { |
2572 | { |
2513 | struct coro *coro = SvSTATE (coro_current); |
2573 | struct coro *coro = SvSTATE_current; |
2514 | |
2574 | |
2515 | sv_setsv (cb, &PL_sv_undef); |
2575 | sv_setsv (cb, &PL_sv_undef); |
2516 | |
2576 | |
2517 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2577 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2518 | coro->saved_deffh = 0; |
2578 | coro->saved_deffh = 0; |
… | |
… | |
2536 | api_trace (aTHX_ coro_current, 0); |
2596 | api_trace (aTHX_ coro_current, 0); |
2537 | |
2597 | |
2538 | av_push (av_async_pool, newSVsv (coro_current)); |
2598 | av_push (av_async_pool, newSVsv (coro_current)); |
2539 | } |
2599 | } |
2540 | |
2600 | |
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 | |
2601 | |
2590 | MODULE = Coro::State PACKAGE = Coro::AIO |
2602 | MODULE = Coro::State PACKAGE = Coro::AIO |
2591 | |
2603 | |
2592 | void |
2604 | void |
2593 | _get_state (SV *self) |
2605 | _get_state (SV *self) |
… | |
… | |
2642 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2654 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2643 | |
2655 | |
2644 | BOOT: |
2656 | BOOT: |
2645 | sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); |
2657 | sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); |
2646 | |
2658 | |
2647 | SV * |
2659 | void |
2648 | _schedule (...) |
2660 | _schedule (...) |
2649 | CODE: |
2661 | CODE: |
2650 | { |
2662 | { |
2651 | static int incede; |
2663 | static int incede; |
2652 | |
2664 | |
… | |
… | |
2698 | void |
2710 | void |
2699 | up (SV *self, int adjust = 1) |
2711 | up (SV *self, int adjust = 1) |
2700 | ALIAS: |
2712 | ALIAS: |
2701 | adjust = 1 |
2713 | adjust = 1 |
2702 | CODE: |
2714 | CODE: |
2703 | { |
2715 | 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 (cb); |
|
|
2723 | else |
|
|
2724 | croak ("callbacks not yet supported"); |
|
|
2725 | |
|
|
2726 | SvREFCNT_dec (cb); |
|
|
2727 | } |
|
|
2728 | } |
|
|
2729 | |
2716 | |
2730 | void |
2717 | void |
2731 | down (SV *self) |
2718 | down (SV *self) |
2732 | CODE: |
2719 | CODE: |
2733 | api_execute_slf (aTHX_ cv, slf_init_semaphore_down, &ST (0), 1); |
2720 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
2734 | |
2721 | |
2735 | void |
2722 | void |
2736 | try (SV *self) |
2723 | try (SV *self) |
2737 | PPCODE: |
2724 | PPCODE: |
2738 | { |
2725 | { |