… | |
… | |
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; |
… | |
… | |
257 | /* this is a structure representing a perl-level coroutine */ |
241 | /* this is a structure representing a perl-level coroutine */ |
258 | struct coro { |
242 | struct coro { |
259 | /* the C coroutine allocated to this perl coroutine, if any */ |
243 | /* the C coroutine allocated to this perl coroutine, if any */ |
260 | coro_cctx *cctx; |
244 | coro_cctx *cctx; |
261 | |
245 | |
262 | /* process data */ |
246 | /* state data */ |
263 | struct CoroSLF slf_frame; /* saved slf frame */ |
247 | struct CoroSLF slf_frame; /* saved slf frame */ |
264 | AV *mainstack; |
248 | AV *mainstack; |
265 | perl_slots *slot; /* basically the saved sp */ |
249 | perl_slots *slot; /* basically the saved sp */ |
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 */ |
… | |
… | |
284 | }; |
269 | }; |
285 | |
270 | |
286 | typedef struct coro *Coro__State; |
271 | typedef struct coro *Coro__State; |
287 | typedef struct coro *Coro__State_or_hashref; |
272 | typedef struct coro *Coro__State_or_hashref; |
288 | |
273 | |
|
|
274 | /* the following variables are effectively part of the perl context */ |
|
|
275 | /* and get copied between struct coro and these variables */ |
|
|
276 | /* the mainr easonw e don't support windows process emulation */ |
289 | static struct CoroSLF slf_frame; /* the current slf frame */ |
277 | static struct CoroSLF slf_frame; /* the current slf frame */ |
|
|
278 | static SV *coro_throw; |
290 | |
279 | |
291 | /** Coro ********************************************************************/ |
280 | /** Coro ********************************************************************/ |
292 | |
281 | |
293 | #define PRIO_MAX 3 |
282 | #define PRIO_MAX 3 |
294 | #define PRIO_HIGH 1 |
283 | #define PRIO_HIGH 1 |
… | |
… | |
403 | static MGVTBL coro_cv_vtbl = { |
392 | static MGVTBL coro_cv_vtbl = { |
404 | 0, 0, 0, 0, |
393 | 0, 0, 0, 0, |
405 | coro_cv_free |
394 | coro_cv_free |
406 | }; |
395 | }; |
407 | |
396 | |
408 | #define CORO_MAGIC(sv, type) \ |
397 | #define CORO_MAGIC(sv, type) \ |
409 | SvMAGIC (sv) \ |
398 | expect_true (SvMAGIC (sv)) \ |
410 | ? SvMAGIC (sv)->mg_type == type \ |
399 | ? expect_true (SvMAGIC (sv)->mg_type == type) \ |
411 | ? SvMAGIC (sv) \ |
400 | ? SvMAGIC (sv) \ |
412 | : mg_find (sv, type) \ |
401 | : mg_find (sv, type) \ |
413 | : 0 |
402 | : 0 |
414 | |
403 | |
415 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
404 | #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) |
405 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
417 | |
406 | |
… | |
… | |
438 | mg = CORO_MAGIC_state (coro); |
427 | mg = CORO_MAGIC_state (coro); |
439 | return (struct coro *)mg->mg_ptr; |
428 | return (struct coro *)mg->mg_ptr; |
440 | } |
429 | } |
441 | |
430 | |
442 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
431 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
432 | |
|
|
433 | /* fastert than SvSTATE, but expects a coroutine hv */ |
|
|
434 | INLINE struct coro * |
|
|
435 | SvSTATE_hv (SV *sv) |
|
|
436 | { |
|
|
437 | MAGIC *mg = expect_true (SvMAGIC (sv)->mg_type == CORO_MAGIC_type_state) |
|
|
438 | ? SvMAGIC (sv) |
|
|
439 | : mg_find (sv, CORO_MAGIC_type_state); |
|
|
440 | |
|
|
441 | return (struct coro *)mg->mg_ptr; |
|
|
442 | } |
|
|
443 | |
|
|
444 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
443 | |
445 | |
444 | /* the next two functions merely cache the padlists */ |
446 | /* the next two functions merely cache the padlists */ |
445 | static void |
447 | static void |
446 | get_padlist (pTHX_ CV *cv) |
448 | get_padlist (pTHX_ CV *cv) |
447 | { |
449 | { |
… | |
… | |
515 | } |
517 | } |
516 | |
518 | |
517 | PUTBACK; |
519 | PUTBACK; |
518 | } |
520 | } |
519 | |
521 | |
520 | slf_frame = c->slf_frame; |
522 | slf_frame = c->slf_frame; |
|
|
523 | coro_throw = c->throw; |
521 | } |
524 | } |
522 | |
525 | |
523 | static void |
526 | static void |
524 | save_perl (pTHX_ Coro__State c) |
527 | save_perl (pTHX_ Coro__State c) |
525 | { |
528 | { |
|
|
529 | c->throw = coro_throw; |
526 | c->slf_frame = slf_frame; |
530 | c->slf_frame = slf_frame; |
527 | |
531 | |
528 | { |
532 | { |
529 | dSP; |
533 | dSP; |
530 | I32 cxix = cxstack_ix; |
534 | I32 cxix = cxstack_ix; |
… | |
… | |
823 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
827 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
824 | { |
828 | { |
825 | return 0; |
829 | return 0; |
826 | } |
830 | } |
827 | |
831 | |
828 | static void |
832 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
829 | coro_setup (pTHX_ struct coro *coro) |
833 | coro_setup (pTHX_ struct coro *coro) |
830 | { |
834 | { |
831 | /* |
835 | /* |
832 | * emulate part of the perl startup here. |
836 | * emulate part of the perl startup here. |
833 | */ |
837 | */ |
… | |
… | |
876 | /* this newly created coroutine might be run on an existing cctx which most |
880 | /* this newly created coroutine might be run on an existing cctx which most |
877 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
881 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
878 | */ |
882 | */ |
879 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
883 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
880 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
884 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
|
|
885 | |
|
|
886 | coro_throw = coro->throw; |
881 | } |
887 | } |
882 | |
888 | |
883 | static void |
889 | static void |
884 | coro_destruct (pTHX_ struct coro *coro) |
890 | coro_destruct (pTHX_ struct coro *coro) |
885 | { |
891 | { |
… | |
… | |
909 | |
915 | |
910 | SvREFCNT_dec (PL_diehook); |
916 | SvREFCNT_dec (PL_diehook); |
911 | SvREFCNT_dec (PL_warnhook); |
917 | SvREFCNT_dec (PL_warnhook); |
912 | |
918 | |
913 | SvREFCNT_dec (coro->saved_deffh); |
919 | SvREFCNT_dec (coro->saved_deffh); |
914 | SvREFCNT_dec (coro->throw); |
920 | SvREFCNT_dec (coro_throw); |
915 | |
921 | |
916 | coro_destruct_stacks (aTHX); |
922 | coro_destruct_stacks (aTHX); |
917 | } |
923 | } |
918 | |
924 | |
919 | INLINE void |
925 | INLINE void |
… | |
… | |
929 | static int |
935 | static int |
930 | runops_trace (pTHX) |
936 | runops_trace (pTHX) |
931 | { |
937 | { |
932 | COP *oldcop = 0; |
938 | COP *oldcop = 0; |
933 | int oldcxix = -2; |
939 | int oldcxix = -2; |
934 | struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ |
940 | struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */ |
935 | coro_cctx *cctx = coro->cctx; |
941 | coro_cctx *cctx = coro->cctx; |
936 | |
942 | |
937 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
943 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
938 | { |
944 | { |
939 | PERL_ASYNC_CHECK (); |
945 | PERL_ASYNC_CHECK (); |
… | |
… | |
1088 | |
1094 | |
1089 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1095 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1090 | INLINE void |
1096 | INLINE void |
1091 | transfer_tail (pTHX) |
1097 | transfer_tail (pTHX) |
1092 | { |
1098 | { |
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); |
1099 | 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 | } |
1100 | } |
1109 | |
1101 | |
1110 | /* |
1102 | /* |
1111 | * this is a _very_ stripped down perl interpreter ;) |
1103 | * this is a _very_ stripped down perl interpreter ;) |
1112 | */ |
1104 | */ |
… | |
… | |
1127 | |
1119 | |
1128 | /* inject a fake subroutine call to cctx_init */ |
1120 | /* inject a fake subroutine call to cctx_init */ |
1129 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1121 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1130 | |
1122 | |
1131 | /* cctx_run is the alternative tail of transfer() */ |
1123 | /* cctx_run is the alternative tail of transfer() */ |
1132 | /* TODO: throwing an exception here might be deadly, VERIFY */ |
|
|
1133 | transfer_tail (aTHX); |
1124 | transfer_tail (aTHX); |
1134 | |
1125 | |
1135 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1126 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1136 | PL_restartop = PL_op; |
1127 | PL_restartop = PL_op; |
1137 | perl_run (PL_curinterp); |
1128 | perl_run (PL_curinterp); |
… | |
… | |
1293 | /** coroutine switching *****************************************************/ |
1284 | /** coroutine switching *****************************************************/ |
1294 | |
1285 | |
1295 | static void |
1286 | static void |
1296 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1287 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1297 | { |
1288 | { |
|
|
1289 | /* TODO: throwing up here is considered harmful */ |
|
|
1290 | |
1298 | if (expect_true (prev != next)) |
1291 | if (expect_true (prev != next)) |
1299 | { |
1292 | { |
1300 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1293 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1301 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); |
1294 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); |
1302 | |
1295 | |
… | |
… | |
1312 | #endif |
1305 | #endif |
1313 | } |
1306 | } |
1314 | } |
1307 | } |
1315 | |
1308 | |
1316 | /* always use the TRANSFER macro */ |
1309 | /* always use the TRANSFER macro */ |
1317 | static void NOINLINE |
1310 | static void NOINLINE /* noinline so we have a fixed stackframe */ |
1318 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1311 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1319 | { |
1312 | { |
1320 | dSTACKLEVEL; |
1313 | dSTACKLEVEL; |
1321 | |
1314 | |
1322 | /* sometimes transfer is only called to set idle_sp */ |
1315 | /* sometimes transfer is only called to set idle_sp */ |
… | |
… | |
1337 | prev->flags |= CF_RUNNING; |
1330 | prev->flags |= CF_RUNNING; |
1338 | } |
1331 | } |
1339 | |
1332 | |
1340 | prev->flags &= ~CF_RUNNING; |
1333 | prev->flags &= ~CF_RUNNING; |
1341 | next->flags |= CF_RUNNING; |
1334 | next->flags |= CF_RUNNING; |
1342 | |
|
|
1343 | LOCK; |
|
|
1344 | |
1335 | |
1345 | /* first get rid of the old state */ |
1336 | /* first get rid of the old state */ |
1346 | save_perl (aTHX_ prev); |
1337 | save_perl (aTHX_ prev); |
1347 | |
1338 | |
1348 | if (expect_false (next->flags & CF_NEW)) |
1339 | if (expect_false (next->flags & CF_NEW)) |
… | |
… | |
1381 | ++next->usecount; |
1372 | ++next->usecount; |
1382 | |
1373 | |
1383 | if (expect_true (!next->cctx)) |
1374 | if (expect_true (!next->cctx)) |
1384 | next->cctx = cctx_get (aTHX); |
1375 | next->cctx = cctx_get (aTHX); |
1385 | |
1376 | |
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)) |
1377 | if (expect_false (prev__cctx != next->cctx)) |
1390 | { |
1378 | { |
1391 | prev__cctx->top_env = PL_top_env; |
1379 | prev__cctx->top_env = PL_top_env; |
1392 | PL_top_env = next->cctx->top_env; |
1380 | PL_top_env = next->cctx->top_env; |
1393 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1381 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
… | |
… | |
1406 | coro_state_destroy (pTHX_ struct coro *coro) |
1394 | coro_state_destroy (pTHX_ struct coro *coro) |
1407 | { |
1395 | { |
1408 | if (coro->flags & CF_DESTROYED) |
1396 | if (coro->flags & CF_DESTROYED) |
1409 | return 0; |
1397 | return 0; |
1410 | |
1398 | |
|
|
1399 | if (coro->on_destroy) |
|
|
1400 | coro->on_destroy (aTHX_ coro); |
|
|
1401 | |
1411 | coro->flags |= CF_DESTROYED; |
1402 | coro->flags |= CF_DESTROYED; |
1412 | |
1403 | |
1413 | if (coro->flags & CF_READY) |
1404 | if (coro->flags & CF_READY) |
1414 | { |
1405 | { |
1415 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1406 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1416 | /* alternative: look through all ready queues and remove the coro */ |
1407 | /* alternative: look through all ready queues and remove the coro */ |
1417 | LOCK; |
|
|
1418 | --coro_nready; |
1408 | --coro_nready; |
1419 | UNLOCK; |
|
|
1420 | } |
1409 | } |
1421 | else |
1410 | else |
1422 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1411 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1423 | |
1412 | |
1424 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1413 | if (coro->mainstack && coro->mainstack != main_mainstack) |
… | |
… | |
1502 | TRANSFER (ta, 1); |
1491 | TRANSFER (ta, 1); |
1503 | } |
1492 | } |
1504 | |
1493 | |
1505 | /** Coro ********************************************************************/ |
1494 | /** Coro ********************************************************************/ |
1506 | |
1495 | |
1507 | static void |
1496 | INLINE void |
1508 | coro_enq (pTHX_ SV *coro_sv) |
1497 | coro_enq (pTHX_ struct coro *coro) |
1509 | { |
1498 | { |
1510 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1499 | av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); |
1511 | } |
1500 | } |
1512 | |
1501 | |
1513 | static SV * |
1502 | INLINE SV * |
1514 | coro_deq (pTHX) |
1503 | coro_deq (pTHX) |
1515 | { |
1504 | { |
1516 | int prio; |
1505 | int prio; |
1517 | |
1506 | |
1518 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1507 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1537 | if (coro->flags & CF_READY) |
1526 | if (coro->flags & CF_READY) |
1538 | return 0; |
1527 | return 0; |
1539 | |
1528 | |
1540 | coro->flags |= CF_READY; |
1529 | coro->flags |= CF_READY; |
1541 | |
1530 | |
1542 | LOCK; |
|
|
1543 | |
|
|
1544 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1531 | sv_hook = coro_nready ? 0 : coro_readyhook; |
1545 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1532 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
1546 | |
1533 | |
1547 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1534 | coro_enq (aTHX_ coro); |
1548 | ++coro_nready; |
1535 | ++coro_nready; |
1549 | |
1536 | |
1550 | UNLOCK; |
|
|
1551 | |
|
|
1552 | if (sv_hook) |
1537 | if (sv_hook) |
1553 | { |
1538 | { |
1554 | dSP; |
1539 | dSP; |
1555 | |
1540 | |
1556 | ENTER; |
1541 | ENTER; |
… | |
… | |
1582 | { |
1567 | { |
1583 | SV *prev_sv, *next_sv; |
1568 | SV *prev_sv, *next_sv; |
1584 | |
1569 | |
1585 | for (;;) |
1570 | for (;;) |
1586 | { |
1571 | { |
1587 | LOCK; |
|
|
1588 | next_sv = coro_deq (aTHX); |
1572 | next_sv = coro_deq (aTHX); |
1589 | |
1573 | |
1590 | /* nothing to schedule: call the idle handler */ |
1574 | /* nothing to schedule: call the idle handler */ |
1591 | if (expect_false (!next_sv)) |
1575 | if (expect_false (!next_sv)) |
1592 | { |
1576 | { |
1593 | dSP; |
1577 | dSP; |
1594 | UNLOCK; |
|
|
1595 | |
1578 | |
1596 | ENTER; |
1579 | ENTER; |
1597 | SAVETMPS; |
1580 | SAVETMPS; |
1598 | |
1581 | |
1599 | PUSHMARK (SP); |
1582 | PUSHMARK (SP); |
… | |
… | |
1604 | FREETMPS; |
1587 | FREETMPS; |
1605 | LEAVE; |
1588 | LEAVE; |
1606 | continue; |
1589 | continue; |
1607 | } |
1590 | } |
1608 | |
1591 | |
1609 | ta->next = SvSTATE (next_sv); |
1592 | ta->next = SvSTATE_hv (next_sv); |
1610 | |
1593 | |
1611 | /* cannot transfer to destroyed coros, skip and look for next */ |
1594 | /* cannot transfer to destroyed coros, skip and look for next */ |
1612 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1595 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
1613 | { |
1596 | { |
1614 | UNLOCK; |
|
|
1615 | SvREFCNT_dec (next_sv); |
1597 | SvREFCNT_dec (next_sv); |
1616 | /* coro_nready has already been taken care of by destroy */ |
1598 | /* coro_nready has already been taken care of by destroy */ |
1617 | continue; |
1599 | continue; |
1618 | } |
1600 | } |
1619 | |
1601 | |
1620 | --coro_nready; |
1602 | --coro_nready; |
1621 | UNLOCK; |
|
|
1622 | break; |
1603 | break; |
1623 | } |
1604 | } |
1624 | |
1605 | |
1625 | /* free this only after the transfer */ |
1606 | /* free this only after the transfer */ |
1626 | prev_sv = SvRV (coro_current); |
1607 | prev_sv = SvRV (coro_current); |
1627 | ta->prev = SvSTATE (prev_sv); |
1608 | ta->prev = SvSTATE_hv (prev_sv); |
1628 | TRANSFER_CHECK (*ta); |
1609 | TRANSFER_CHECK (*ta); |
1629 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1610 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1630 | ta->next->flags &= ~CF_READY; |
1611 | ta->next->flags &= ~CF_READY; |
1631 | SvRV_set (coro_current, next_sv); |
1612 | SvRV_set (coro_current, next_sv); |
1632 | |
1613 | |
1633 | LOCK; |
|
|
1634 | free_coro_mortal (aTHX); |
1614 | free_coro_mortal (aTHX); |
1635 | coro_mortal = prev_sv; |
1615 | coro_mortal = prev_sv; |
1636 | UNLOCK; |
|
|
1637 | } |
1616 | } |
1638 | |
1617 | |
1639 | INLINE void |
1618 | INLINE void |
1640 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1619 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1641 | { |
1620 | { |
… | |
… | |
1797 | PerlIOBuf_set_ptrcnt, |
1776 | PerlIOBuf_set_ptrcnt, |
1798 | }; |
1777 | }; |
1799 | |
1778 | |
1800 | /*****************************************************************************/ |
1779 | /*****************************************************************************/ |
1801 | |
1780 | |
1802 | static const CV *slf_cv; /* for quick consistency check */ |
|
|
1803 | |
|
|
1804 | static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ |
1781 | static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ |
|
|
1782 | static const CV *slf_cv; |
1805 | static SV *slf_arg0; |
1783 | static SV **slf_argv; |
1806 | static SV *slf_arg1; |
1784 | static int slf_argc, slf_arga; /* count, allocated */ |
1807 | static SV *slf_arg2; |
1785 | static I32 slf_ax; /* top of stack, for restore */ |
1808 | |
1786 | |
1809 | /* this restores the stack in the case we patched the entersub, to */ |
1787 | /* this restores the stack in the case we patched the entersub, to */ |
1810 | /* recreate the stack frame as perl will on following calls */ |
1788 | /* recreate the stack frame as perl will on following calls */ |
1811 | /* since entersub cleared the stack */ |
1789 | /* since entersub cleared the stack */ |
1812 | static OP * |
1790 | static OP * |
1813 | pp_restore (pTHX) |
1791 | pp_restore (pTHX) |
1814 | { |
1792 | { |
1815 | dSP; |
1793 | int i; |
|
|
1794 | SV **SP = PL_stack_base + slf_ax; |
1816 | |
1795 | |
1817 | PUSHMARK (SP); |
1796 | PUSHMARK (SP); |
1818 | |
1797 | |
1819 | EXTEND (SP, 3); |
1798 | EXTEND (SP, slf_argc + 1); |
|
|
1799 | |
|
|
1800 | for (i = 0; i < slf_argc; ++i) |
1820 | if (slf_arg0) PUSHs (sv_2mortal (slf_arg0)); |
1801 | PUSHs (sv_2mortal (slf_argv [i])); |
1821 | if (slf_arg1) PUSHs (sv_2mortal (slf_arg1)); |
1802 | |
1822 | if (slf_arg2) PUSHs (sv_2mortal (slf_arg2)); |
|
|
1823 | PUSHs ((SV *)CvGV (slf_cv)); |
1803 | PUSHs ((SV *)CvGV (slf_cv)); |
1824 | |
1804 | |
1825 | RETURNOP (slf_restore.op_first); |
1805 | RETURNOP (slf_restore.op_first); |
1826 | } |
1806 | } |
1827 | |
1807 | |
… | |
… | |
1830 | { |
1810 | { |
1831 | prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data); |
1811 | prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data); |
1832 | } |
1812 | } |
1833 | |
1813 | |
1834 | static void |
1814 | static void |
1835 | slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
1815 | slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1836 | { |
1816 | { |
1837 | assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1)); |
1817 | assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1)); |
1838 | |
1818 | |
1839 | frame->prepare = slf_prepare_set_stacklevel; |
1819 | frame->prepare = slf_prepare_set_stacklevel; |
1840 | frame->check = slf_check_nop; |
1820 | frame->check = slf_check_nop; |
… | |
… | |
1848 | |
1828 | |
1849 | prepare_transfer (aTHX_ ta, arg [0], arg [1]); |
1829 | prepare_transfer (aTHX_ ta, arg [0], arg [1]); |
1850 | } |
1830 | } |
1851 | |
1831 | |
1852 | static void |
1832 | static void |
1853 | 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) |
1854 | { |
1834 | { |
1855 | if (items != 2) |
1835 | if (items != 2) |
1856 | 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); |
1857 | |
1837 | |
1858 | frame->prepare = slf_prepare_transfer; |
1838 | frame->prepare = slf_prepare_transfer; |
1859 | frame->check = slf_check_nop; |
1839 | frame->check = slf_check_nop; |
1860 | frame->data = (void *)arg; /* let's hope it will stay valid */ |
1840 | frame->data = (void *)arg; /* let's hope it will stay valid */ |
1861 | } |
1841 | } |
1862 | |
1842 | |
1863 | static void |
1843 | static void |
1864 | 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) |
1865 | { |
1845 | { |
1866 | frame->prepare = prepare_schedule; |
1846 | frame->prepare = prepare_schedule; |
1867 | frame->check = slf_check_nop; |
1847 | frame->check = slf_check_nop; |
1868 | } |
1848 | } |
1869 | |
1849 | |
1870 | static void |
1850 | static void |
1871 | 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) |
1872 | { |
1852 | { |
1873 | frame->prepare = prepare_cede; |
1853 | frame->prepare = prepare_cede; |
1874 | frame->check = slf_check_nop; |
1854 | frame->check = slf_check_nop; |
1875 | } |
1855 | } |
1876 | |
1856 | |
1877 | static void |
1857 | static void |
1878 | 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) |
1879 | { |
1859 | { |
1880 | frame->prepare = prepare_cede_notself; |
1860 | frame->prepare = prepare_cede_notself; |
1881 | frame->check = slf_check_nop; |
1861 | frame->check = slf_check_nop; |
1882 | } |
1862 | } |
1883 | |
1863 | |
… | |
… | |
1909 | /* 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 */ |
1910 | /* for us, divert to the real entersub */ |
1890 | /* for us, divert to the real entersub */ |
1911 | if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF)) |
1891 | if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF)) |
1912 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
1892 | return PL_ppaddr[OP_ENTERSUB](aTHX); |
1913 | |
1893 | |
1914 | /* pop args */ |
|
|
1915 | SP = PL_stack_base + POPMARK; |
|
|
1916 | |
|
|
1917 | if (!(PL_op->op_flags & OPf_STACKED)) |
1894 | if (!(PL_op->op_flags & OPf_STACKED)) |
1918 | { |
1895 | { |
1919 | /* ampersand-form of call, use @_ instead of stack */ |
1896 | /* ampersand-form of call, use @_ instead of stack */ |
1920 | AV *av = GvAV (PL_defgv); |
1897 | AV *av = GvAV (PL_defgv); |
1921 | arg = AvARRAY (av); |
1898 | arg = AvARRAY (av); |
1922 | items = AvFILLp (av) + 1; |
1899 | items = AvFILLp (av) + 1; |
1923 | } |
1900 | } |
1924 | |
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 | |
1925 | PUTBACK; |
1909 | PUTBACK; |
1926 | |
|
|
1927 | ((coro_slf_cb)CvXSUBANY (GvCV (gv)).any_ptr) (aTHX_ &slf_frame, arg, items); |
|
|
1928 | } |
1910 | } |
1929 | |
1911 | |
1930 | /* now interpret the slf_frame */ |
1912 | /* now that we have a slf_frame, interpret it! */ |
1931 | /* we use a callback system not to make the code needlessly */ |
1913 | /* we use a callback system not to make the code needlessly */ |
1932 | /* 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 */ |
1933 | |
1915 | |
1934 | do |
1916 | do |
1935 | { |
1917 | { |
… | |
… | |
1940 | |
1922 | |
1941 | checkmark = PL_stack_sp - PL_stack_base; |
1923 | checkmark = PL_stack_sp - PL_stack_base; |
1942 | } |
1924 | } |
1943 | while (slf_frame.check (aTHX_ &slf_frame)); |
1925 | while (slf_frame.check (aTHX_ &slf_frame)); |
1944 | |
1926 | |
|
|
1927 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
|
|
1928 | |
|
|
1929 | /* return value handling - mostly like entersub */ |
1945 | { |
1930 | { |
1946 | dSP; |
1931 | dSP; |
1947 | SV **bot = PL_stack_base + checkmark; |
1932 | SV **bot = PL_stack_base + checkmark; |
1948 | int gimme = GIMME_V; |
1933 | int gimme = GIMME_V; |
1949 | |
|
|
1950 | slf_frame.prepare = 0; /* signal pp_slf that we need a new frame */ |
|
|
1951 | |
1934 | |
1952 | /* make sure we put something on the stack in scalar context */ |
1935 | /* make sure we put something on the stack in scalar context */ |
1953 | if (gimme == G_SCALAR) |
1936 | if (gimme == G_SCALAR) |
1954 | { |
1937 | { |
1955 | if (sp == bot) |
1938 | if (sp == bot) |
… | |
… | |
1959 | } |
1942 | } |
1960 | |
1943 | |
1961 | PUTBACK; |
1944 | PUTBACK; |
1962 | } |
1945 | } |
1963 | |
1946 | |
|
|
1947 | /* exception handling */ |
|
|
1948 | if (expect_false (coro_throw)) |
|
|
1949 | { |
|
|
1950 | SV *exception = sv_2mortal (coro_throw); |
|
|
1951 | |
|
|
1952 | coro_throw = 0; |
|
|
1953 | sv_setsv (ERRSV, exception); |
|
|
1954 | croak (0); |
|
|
1955 | } |
|
|
1956 | |
1964 | return NORMAL; |
1957 | return NORMAL; |
1965 | } |
1958 | } |
1966 | |
1959 | |
1967 | static void |
1960 | static void |
1968 | api_execute_slf (pTHX_ CV *cv, coro_slf_cb init_cb, SV **arg, int items) |
1961 | api_execute_slf (pTHX_ CV *cv, coro_slf_cb init_cb, I32 ax) |
1969 | { |
1962 | { |
|
|
1963 | int i; |
|
|
1964 | SV **arg = PL_stack_base + ax; |
|
|
1965 | int items = PL_stack_sp - arg + 1; |
|
|
1966 | |
1970 | assert (("FATAL: SLF call with illegal CV value", !CvANON (cv))); |
1967 | assert (("FATAL: SLF call with illegal CV value", !CvANON (cv))); |
1971 | |
1968 | |
1972 | if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] |
1969 | if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] |
1973 | && PL_op->op_ppaddr != pp_slf) |
1970 | && PL_op->op_ppaddr != pp_slf) |
1974 | croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught"); |
1971 | croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught"); |
1975 | |
1972 | |
1976 | if (items > 3) |
|
|
1977 | croak ("Coro only supports up to three arguments to SLF functions currently (not %d), caught", items); |
|
|
1978 | |
|
|
1979 | CvFLAGS (cv) |= CVf_SLF; |
1973 | CvFLAGS (cv) |= CVf_SLF; |
1980 | CvXSUBANY (cv).any_ptr = (void *)init_cb; |
1974 | CvXSUBANY (cv).any_ptr = (void *)init_cb; |
1981 | slf_cv = cv; |
1975 | slf_cv = cv; |
1982 | |
1976 | |
1983 | /* we patch the op, and then re-run the whole call */ |
1977 | /* we patch the op, and then re-run the whole call */ |
1984 | /* we have to put the same argument on the stack for this to work */ |
1978 | /* we have to put the same argument on the stack for this to work */ |
1985 | /* and this will be done by pp_restore */ |
1979 | /* and this will be done by pp_restore */ |
1986 | slf_restore.op_next = (OP *)&slf_restore; |
1980 | slf_restore.op_next = (OP *)&slf_restore; |
1987 | slf_restore.op_type = OP_NULL; |
1981 | slf_restore.op_type = OP_CUSTOM; |
1988 | slf_restore.op_ppaddr = pp_restore; |
1982 | slf_restore.op_ppaddr = pp_restore; |
1989 | slf_restore.op_first = PL_op; |
1983 | slf_restore.op_first = PL_op; |
1990 | |
1984 | |
1991 | slf_arg0 = items > 0 ? SvREFCNT_inc (arg [0]) : 0; |
1985 | slf_ax = ax - 1; /* undo the ax++ inside dAXMARK */ |
1992 | slf_arg1 = items > 1 ? SvREFCNT_inc (arg [1]) : 0; |
1986 | |
1993 | slf_arg2 = items > 2 ? SvREFCNT_inc (arg [2]) : 0; |
1987 | if (PL_op->op_flags & OPf_STACKED) |
|
|
1988 | { |
|
|
1989 | if (items > slf_arga) |
|
|
1990 | { |
|
|
1991 | slf_arga = items; |
|
|
1992 | free (slf_argv); |
|
|
1993 | slf_argv = malloc (slf_arga * sizeof (SV *)); |
|
|
1994 | } |
|
|
1995 | |
|
|
1996 | slf_argc = items; |
|
|
1997 | |
|
|
1998 | for (i = 0; i < items; ++i) |
|
|
1999 | slf_argv [i] = SvREFCNT_inc (arg [i]); |
|
|
2000 | } |
|
|
2001 | else |
|
|
2002 | slf_argc = 0; |
1994 | |
2003 | |
1995 | PL_op->op_ppaddr = pp_slf; |
2004 | PL_op->op_ppaddr = pp_slf; |
|
|
2005 | PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ |
1996 | |
2006 | |
1997 | PL_op = (OP *)&slf_restore; |
2007 | PL_op = (OP *)&slf_restore; |
1998 | } |
2008 | } |
1999 | |
2009 | |
2000 | /*****************************************************************************/ |
2010 | /*****************************************************************************/ |
|
|
2011 | |
|
|
2012 | static void |
|
|
2013 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
|
|
2014 | { |
|
|
2015 | SV *count_sv = AvARRAY (av)[0]; |
|
|
2016 | IV count = SvIVX (count_sv); |
|
|
2017 | |
|
|
2018 | count += adjust; |
|
|
2019 | SvIVX (count_sv) = count; |
|
|
2020 | |
|
|
2021 | /* now wake up as many waiters as are expected to lock */ |
|
|
2022 | while (count > 0 && AvFILLp (av) > 0) |
|
|
2023 | { |
|
|
2024 | SV *cb; |
|
|
2025 | |
|
|
2026 | /* swap first two elements so we can shift a waiter */ |
|
|
2027 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
|
|
2028 | AvARRAY (av)[1] = count_sv; |
|
|
2029 | cb = av_shift (av); |
|
|
2030 | |
|
|
2031 | if (SvOBJECT (cb)) |
|
|
2032 | api_ready (aTHX_ cb); |
|
|
2033 | else |
|
|
2034 | croak ("callbacks not yet supported"); |
|
|
2035 | |
|
|
2036 | SvREFCNT_dec (cb); |
|
|
2037 | |
|
|
2038 | --count; |
|
|
2039 | } |
|
|
2040 | } |
|
|
2041 | |
|
|
2042 | static void |
|
|
2043 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
|
|
2044 | { |
|
|
2045 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
|
|
2046 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
|
|
2047 | } |
2001 | |
2048 | |
2002 | static int |
2049 | static int |
2003 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
2050 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
2004 | { |
2051 | { |
2005 | AV *av = (AV *)frame->data; |
2052 | AV *av = (AV *)frame->data; |
2006 | SV *count_sv = AvARRAY (av)[0]; |
2053 | SV *count_sv = AvARRAY (av)[0]; |
2007 | |
2054 | |
2008 | if (SvIVX (count_sv) > 0) |
2055 | if (SvIVX (count_sv) > 0) |
2009 | { |
2056 | { |
|
|
2057 | SvSTATE_current->on_destroy = 0; |
2010 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2058 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2011 | return 0; |
2059 | return 0; |
2012 | } |
2060 | } |
2013 | else |
2061 | else |
2014 | { |
2062 | { |
… | |
… | |
2025 | return 1; |
2073 | return 1; |
2026 | } |
2074 | } |
2027 | } |
2075 | } |
2028 | |
2076 | |
2029 | static void |
2077 | static void |
2030 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, SV **arg, int items) |
2078 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2031 | { |
2079 | { |
2032 | AV *av = (AV *)SvRV (arg [0]); |
2080 | AV *av = (AV *)SvRV (arg [0]); |
2033 | |
2081 | |
2034 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2082 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2035 | { |
2083 | { |
2036 | frame->data = (void *)av; |
2084 | frame->data = (void *)av; |
2037 | frame->prepare = prepare_nop; |
2085 | frame->prepare = prepare_nop; |
|
|
2086 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2038 | } |
2087 | } |
2039 | else |
2088 | else |
2040 | { |
2089 | { |
2041 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2090 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2042 | |
2091 | |
2043 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2092 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2044 | frame->prepare = prepare_schedule; |
2093 | frame->prepare = prepare_schedule; |
|
|
2094 | |
|
|
2095 | /* to avoid race conditions when a woken-up coro gets terminated */ |
|
|
2096 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
|
|
2097 | assert (!SvSTATE_current->on_destroy);//D |
|
|
2098 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2045 | } |
2099 | } |
2046 | |
2100 | |
2047 | frame->check = slf_check_semaphore_down; |
2101 | frame->check = slf_check_semaphore_down; |
2048 | |
2102 | |
2049 | } |
2103 | } |
… | |
… | |
2077 | PROTOTYPES: DISABLE |
2131 | PROTOTYPES: DISABLE |
2078 | |
2132 | |
2079 | BOOT: |
2133 | BOOT: |
2080 | { |
2134 | { |
2081 | #ifdef USE_ITHREADS |
2135 | #ifdef USE_ITHREADS |
2082 | MUTEX_INIT (&coro_lock); |
|
|
2083 | # if CORO_PTHREAD |
2136 | # if CORO_PTHREAD |
2084 | coro_thx = PERL_GET_CONTEXT; |
2137 | coro_thx = PERL_GET_CONTEXT; |
2085 | # endif |
2138 | # endif |
2086 | #endif |
2139 | #endif |
2087 | BOOT_PAGESIZE; |
2140 | BOOT_PAGESIZE; |
… | |
… | |
2107 | main_mainstack = PL_mainstack; |
2160 | main_mainstack = PL_mainstack; |
2108 | main_top_env = PL_top_env; |
2161 | main_top_env = PL_top_env; |
2109 | |
2162 | |
2110 | while (main_top_env->je_prev) |
2163 | while (main_top_env->je_prev) |
2111 | main_top_env = main_top_env->je_prev; |
2164 | main_top_env = main_top_env->je_prev; |
|
|
2165 | |
|
|
2166 | { |
|
|
2167 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
|
|
2168 | |
|
|
2169 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
|
|
2170 | hv_store_ent (PL_custom_op_names, slf, |
|
|
2171 | newSVpv ("coro_slf", 0), 0); |
|
|
2172 | |
|
|
2173 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
|
|
2174 | hv_store_ent (PL_custom_op_descs, slf, |
|
|
2175 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2176 | } |
2112 | |
2177 | |
2113 | coroapi.ver = CORO_API_VERSION; |
2178 | coroapi.ver = CORO_API_VERSION; |
2114 | coroapi.rev = CORO_API_REVISION; |
2179 | coroapi.rev = CORO_API_REVISION; |
2115 | |
2180 | |
2116 | coroapi.transfer = api_transfer; |
2181 | coroapi.transfer = api_transfer; |
… | |
… | |
2164 | RETVAL |
2229 | RETVAL |
2165 | |
2230 | |
2166 | void |
2231 | void |
2167 | _set_stacklevel (...) |
2232 | _set_stacklevel (...) |
2168 | CODE: |
2233 | CODE: |
2169 | api_execute_slf (aTHX_ cv, slf_init_set_stacklevel, &ST (0), items); |
2234 | CORO_EXECUTE_SLF_XS (slf_init_set_stacklevel); |
2170 | |
2235 | |
2171 | void |
2236 | void |
2172 | transfer (...) |
2237 | transfer (...) |
2173 | PROTOTYPE: $$ |
2238 | PROTOTYPE: $$ |
2174 | CODE: |
2239 | CODE: |
2175 | api_execute_slf (aTHX_ cv, slf_init_transfer, &ST (0), items); |
2240 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
2176 | |
2241 | |
2177 | bool |
2242 | bool |
2178 | _destroy (SV *coro_sv) |
2243 | _destroy (SV *coro_sv) |
2179 | CODE: |
2244 | CODE: |
2180 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
2245 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
… | |
… | |
2298 | |
2363 | |
2299 | void |
2364 | void |
2300 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2365 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2301 | PROTOTYPE: $;$ |
2366 | PROTOTYPE: $;$ |
2302 | CODE: |
2367 | CODE: |
|
|
2368 | { |
|
|
2369 | struct coro *current = SvSTATE_current; |
|
|
2370 | SV **throwp = self == current ? &coro_throw : &self->throw; |
2303 | SvREFCNT_dec (self->throw); |
2371 | SvREFCNT_dec (*throwp); |
2304 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
2372 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2373 | } |
2305 | |
2374 | |
2306 | void |
2375 | void |
2307 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
2376 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
2308 | PROTOTYPE: $;$ |
2377 | PROTOTYPE: $;$ |
2309 | C_ARGS: aTHX_ coro, flags |
2378 | C_ARGS: aTHX_ coro, flags |
… | |
… | |
2340 | |
2409 | |
2341 | void |
2410 | void |
2342 | force_cctx () |
2411 | force_cctx () |
2343 | PROTOTYPE: |
2412 | PROTOTYPE: |
2344 | CODE: |
2413 | CODE: |
2345 | struct coro *coro = SvSTATE (coro_current); |
|
|
2346 | coro->cctx->idle_sp = 0; |
2414 | SvSTATE_current->cctx->idle_sp = 0; |
2347 | |
2415 | |
2348 | void |
2416 | void |
2349 | swap_defsv (Coro::State self) |
2417 | swap_defsv (Coro::State self) |
2350 | PROTOTYPE: $ |
2418 | PROTOTYPE: $ |
2351 | ALIAS: |
2419 | ALIAS: |
… | |
… | |
2404 | } |
2472 | } |
2405 | |
2473 | |
2406 | void |
2474 | void |
2407 | schedule (...) |
2475 | schedule (...) |
2408 | CODE: |
2476 | CODE: |
2409 | api_execute_slf (aTHX_ cv, slf_init_schedule, &ST (0), 0); |
2477 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
2410 | |
2478 | |
2411 | void |
2479 | void |
2412 | cede (...) |
2480 | cede (...) |
2413 | CODE: |
2481 | CODE: |
2414 | api_execute_slf (aTHX_ cv, slf_init_cede, &ST (0), 0); |
2482 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
2415 | |
2483 | |
2416 | void |
2484 | void |
2417 | cede_notself (...) |
2485 | cede_notself (...) |
2418 | CODE: |
2486 | CODE: |
2419 | api_execute_slf (aTHX_ cv, slf_init_cede_notself, &ST (0), 0); |
2487 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
2420 | |
2488 | |
2421 | void |
2489 | void |
2422 | _set_current (SV *current) |
2490 | _set_current (SV *current) |
2423 | PROTOTYPE: $ |
2491 | PROTOTYPE: $ |
2424 | CODE: |
2492 | CODE: |
… | |
… | |
2427 | |
2495 | |
2428 | void |
2496 | void |
2429 | _set_readyhook (SV *hook) |
2497 | _set_readyhook (SV *hook) |
2430 | PROTOTYPE: $ |
2498 | PROTOTYPE: $ |
2431 | CODE: |
2499 | CODE: |
2432 | LOCK; |
|
|
2433 | SvREFCNT_dec (coro_readyhook); |
2500 | SvREFCNT_dec (coro_readyhook); |
2434 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
2501 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
2435 | UNLOCK; |
|
|
2436 | |
2502 | |
2437 | int |
2503 | int |
2438 | prio (Coro::State coro, int newprio = 0) |
2504 | prio (Coro::State coro, int newprio = 0) |
2439 | PROTOTYPE: $;$ |
2505 | PROTOTYPE: $;$ |
2440 | ALIAS: |
2506 | ALIAS: |
… | |
… | |
2476 | # for async_pool speedup |
2542 | # for async_pool speedup |
2477 | void |
2543 | void |
2478 | _pool_1 (SV *cb) |
2544 | _pool_1 (SV *cb) |
2479 | CODE: |
2545 | CODE: |
2480 | { |
2546 | { |
2481 | struct coro *coro = SvSTATE (coro_current); |
|
|
2482 | HV *hv = (HV *)SvRV (coro_current); |
2547 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2548 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2483 | AV *defav = GvAV (PL_defgv); |
2549 | AV *defav = GvAV (PL_defgv); |
2484 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
2550 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
2485 | AV *invoke_av; |
2551 | AV *invoke_av; |
2486 | int i, len; |
2552 | int i, len; |
2487 | |
2553 | |
… | |
… | |
2514 | |
2580 | |
2515 | void |
2581 | void |
2516 | _pool_2 (SV *cb) |
2582 | _pool_2 (SV *cb) |
2517 | CODE: |
2583 | CODE: |
2518 | { |
2584 | { |
2519 | struct coro *coro = SvSTATE (coro_current); |
2585 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2586 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2520 | |
2587 | |
2521 | sv_setsv (cb, &PL_sv_undef); |
2588 | sv_setsv (cb, &PL_sv_undef); |
2522 | |
2589 | |
2523 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2590 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2524 | coro->saved_deffh = 0; |
2591 | coro->saved_deffh = 0; |
… | |
… | |
2531 | SvREFCNT_dec (old); |
2598 | SvREFCNT_dec (old); |
2532 | croak ("\3async_pool terminate\2\n"); |
2599 | croak ("\3async_pool terminate\2\n"); |
2533 | } |
2600 | } |
2534 | |
2601 | |
2535 | av_clear (GvAV (PL_defgv)); |
2602 | av_clear (GvAV (PL_defgv)); |
2536 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
2603 | hv_store (hv, "desc", sizeof ("desc") - 1, |
2537 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
2604 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
2538 | |
2605 | |
2539 | coro->prio = 0; |
2606 | coro->prio = 0; |
2540 | |
2607 | |
2541 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
2608 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
… | |
… | |
2656 | void |
2723 | void |
2657 | up (SV *self, int adjust = 1) |
2724 | up (SV *self, int adjust = 1) |
2658 | ALIAS: |
2725 | ALIAS: |
2659 | adjust = 1 |
2726 | adjust = 1 |
2660 | CODE: |
2727 | CODE: |
2661 | { |
2728 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
2662 | AV *av = (AV *)SvRV (self); |
|
|
2663 | SV *count_sv = AvARRAY (av)[0]; |
|
|
2664 | IV count = SvIVX (count_sv); |
|
|
2665 | |
|
|
2666 | count += ix ? adjust : 1; |
|
|
2667 | SvIVX (count_sv) = count; |
|
|
2668 | |
|
|
2669 | /* now wake up as many waiters as possible */ |
|
|
2670 | while (count > 0 && AvFILLp (av) >= count) |
|
|
2671 | { |
|
|
2672 | SV *cb; |
|
|
2673 | |
|
|
2674 | /* swap first two elements so we can shift a waiter */ |
|
|
2675 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
|
|
2676 | AvARRAY (av)[1] = count_sv; |
|
|
2677 | cb = av_shift (av); |
|
|
2678 | |
|
|
2679 | if (SvOBJECT (cb)) |
|
|
2680 | api_ready (aTHX_ cb); |
|
|
2681 | else |
|
|
2682 | croak ("callbacks not yet supported"); |
|
|
2683 | |
|
|
2684 | SvREFCNT_dec (cb); |
|
|
2685 | } |
|
|
2686 | } |
|
|
2687 | |
2729 | |
2688 | void |
2730 | void |
2689 | down (SV *self) |
2731 | down (SV *self) |
2690 | CODE: |
2732 | CODE: |
2691 | api_execute_slf (aTHX_ cv, slf_init_semaphore_down, &ST (0), 1); |
2733 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
2692 | |
2734 | |
2693 | void |
2735 | void |
2694 | try (SV *self) |
2736 | try (SV *self) |
2695 | PPCODE: |
2737 | PPCODE: |
2696 | { |
2738 | { |