… | |
… | |
133 | #else |
133 | #else |
134 | # define dSTACKLEVEL volatile void *stacklevel |
134 | # define dSTACKLEVEL volatile void *stacklevel |
135 | # define STACKLEVEL ((void *)&stacklevel) |
135 | # define STACKLEVEL ((void *)&stacklevel) |
136 | #endif |
136 | #endif |
137 | |
137 | |
138 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
138 | #define IN_DESTRUCT PL_dirty |
139 | |
139 | |
140 | #if __GNUC__ >= 3 |
140 | #if __GNUC__ >= 3 |
141 | # define attribute(x) __attribute__(x) |
141 | # define attribute(x) __attribute__(x) |
142 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
142 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
143 | # define INLINE static inline |
143 | # define INLINE static inline |
… | |
… | |
285 | /* async_pool */ |
285 | /* async_pool */ |
286 | SV *saved_deffh; |
286 | SV *saved_deffh; |
287 | SV *invoke_cb; |
287 | SV *invoke_cb; |
288 | AV *invoke_av; |
288 | AV *invoke_av; |
289 | |
289 | |
|
|
290 | /* on_enter/on_leave */ |
|
|
291 | AV *on_enter; |
|
|
292 | AV *on_leave; |
|
|
293 | |
290 | /* linked list */ |
294 | /* linked list */ |
291 | struct coro *next, *prev; |
295 | struct coro *next, *prev; |
292 | }; |
296 | }; |
293 | |
297 | |
294 | typedef struct coro *Coro__State; |
298 | typedef struct coro *Coro__State; |
… | |
… | |
352 | INLINE CV * |
356 | INLINE CV * |
353 | coro_sv_2cv (pTHX_ SV *sv) |
357 | coro_sv_2cv (pTHX_ SV *sv) |
354 | { |
358 | { |
355 | HV *st; |
359 | HV *st; |
356 | GV *gvp; |
360 | GV *gvp; |
357 | return sv_2cv (sv, &st, &gvp, 0); |
361 | CV *cv = sv_2cv (sv, &st, &gvp, 0); |
|
|
362 | |
|
|
363 | if (!cv) |
|
|
364 | croak ("code reference expected"); |
|
|
365 | |
|
|
366 | return cv; |
358 | } |
367 | } |
|
|
368 | |
|
|
369 | /*****************************************************************************/ |
|
|
370 | /* magic glue */ |
|
|
371 | |
|
|
372 | #define CORO_MAGIC_type_cv 26 |
|
|
373 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
374 | |
|
|
375 | #define CORO_MAGIC_NN(sv, type) \ |
|
|
376 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
|
|
377 | ? SvMAGIC (sv) \ |
|
|
378 | : mg_find (sv, type)) |
|
|
379 | |
|
|
380 | #define CORO_MAGIC(sv, type) \ |
|
|
381 | (expect_true (SvMAGIC (sv)) \ |
|
|
382 | ? CORO_MAGIC_NN (sv, type) \ |
|
|
383 | : 0) |
|
|
384 | |
|
|
385 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
386 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
|
|
387 | |
|
|
388 | INLINE struct coro * |
|
|
389 | SvSTATE_ (pTHX_ SV *coro) |
|
|
390 | { |
|
|
391 | HV *stash; |
|
|
392 | MAGIC *mg; |
|
|
393 | |
|
|
394 | if (SvROK (coro)) |
|
|
395 | coro = SvRV (coro); |
|
|
396 | |
|
|
397 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
398 | croak ("Coro::State object required"); |
|
|
399 | |
|
|
400 | stash = SvSTASH (coro); |
|
|
401 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
402 | { |
|
|
403 | /* very slow, but rare, check */ |
|
|
404 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
405 | croak ("Coro::State object required"); |
|
|
406 | } |
|
|
407 | |
|
|
408 | mg = CORO_MAGIC_state (coro); |
|
|
409 | return (struct coro *)mg->mg_ptr; |
|
|
410 | } |
|
|
411 | |
|
|
412 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
413 | |
|
|
414 | /* faster than SvSTATE, but expects a coroutine hv */ |
|
|
415 | #define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) |
|
|
416 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
|
|
417 | |
|
|
418 | /*****************************************************************************/ |
|
|
419 | /* padlist management and caching */ |
359 | |
420 | |
360 | static AV * |
421 | static AV * |
361 | coro_derive_padlist (pTHX_ CV *cv) |
422 | coro_derive_padlist (pTHX_ CV *cv) |
362 | { |
423 | { |
363 | AV *padlist = CvPADLIST (cv); |
424 | AV *padlist = CvPADLIST (cv); |
… | |
… | |
371 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
432 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
372 | #endif |
433 | #endif |
373 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
434 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
374 | --AvFILLp (padlist); |
435 | --AvFILLp (padlist); |
375 | |
436 | |
376 | av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); |
437 | av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); |
377 | av_store (newpadlist, 1, (SV *)newpad); |
438 | av_store (newpadlist, 1, (SV *)newpad); |
378 | |
439 | |
379 | return newpadlist; |
440 | return newpadlist; |
380 | } |
441 | } |
381 | |
442 | |
382 | static void |
443 | static void |
383 | free_padlist (pTHX_ AV *padlist) |
444 | free_padlist (pTHX_ AV *padlist) |
384 | { |
445 | { |
385 | /* may be during global destruction */ |
446 | /* may be during global destruction */ |
386 | if (SvREFCNT (padlist)) |
447 | if (!IN_DESTRUCT) |
387 | { |
448 | { |
388 | I32 i = AvFILLp (padlist); |
449 | I32 i = AvFILLp (padlist); |
389 | while (i >= 0) |
450 | |
|
|
451 | while (i > 0) /* special-case index 0 */ |
390 | { |
452 | { |
391 | SV **svp = av_fetch (padlist, i--, FALSE); |
453 | /* we try to be extra-careful here */ |
392 | if (svp) |
454 | AV *av = (AV *)AvARRAY (padlist)[i--]; |
393 | { |
455 | I32 j = AvFILLp (av); |
394 | SV *sv; |
456 | |
395 | while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) |
457 | while (j >= 0) |
|
|
458 | SvREFCNT_dec (AvARRAY (av)[j--]); |
|
|
459 | |
|
|
460 | AvFILLp (av) = -1; |
396 | SvREFCNT_dec (sv); |
461 | SvREFCNT_dec (av); |
397 | |
|
|
398 | SvREFCNT_dec (*svp); |
|
|
399 | } |
|
|
400 | } |
462 | } |
401 | |
463 | |
|
|
464 | SvREFCNT_dec (AvARRAY (padlist)[0]); |
|
|
465 | |
|
|
466 | AvFILLp (padlist) = -1; |
402 | SvREFCNT_dec ((SV*)padlist); |
467 | SvREFCNT_dec ((SV*)padlist); |
403 | } |
468 | } |
404 | } |
469 | } |
405 | |
470 | |
406 | static int |
471 | static int |
… | |
… | |
415 | |
480 | |
416 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
481 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
417 | |
482 | |
418 | return 0; |
483 | return 0; |
419 | } |
484 | } |
420 | |
|
|
421 | #define CORO_MAGIC_type_cv 26 |
|
|
422 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
423 | |
485 | |
424 | static MGVTBL coro_cv_vtbl = { |
486 | static MGVTBL coro_cv_vtbl = { |
425 | 0, 0, 0, 0, |
487 | 0, 0, 0, 0, |
426 | coro_cv_free |
488 | coro_cv_free |
427 | }; |
489 | }; |
428 | |
|
|
429 | #define CORO_MAGIC_NN(sv, type) \ |
|
|
430 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
|
|
431 | ? SvMAGIC (sv) \ |
|
|
432 | : mg_find (sv, type)) |
|
|
433 | |
|
|
434 | #define CORO_MAGIC(sv, type) \ |
|
|
435 | (expect_true (SvMAGIC (sv)) \ |
|
|
436 | ? CORO_MAGIC_NN (sv, type) \ |
|
|
437 | : 0) |
|
|
438 | |
|
|
439 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
440 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
|
|
441 | |
|
|
442 | INLINE struct coro * |
|
|
443 | SvSTATE_ (pTHX_ SV *coro) |
|
|
444 | { |
|
|
445 | HV *stash; |
|
|
446 | MAGIC *mg; |
|
|
447 | |
|
|
448 | if (SvROK (coro)) |
|
|
449 | coro = SvRV (coro); |
|
|
450 | |
|
|
451 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
452 | croak ("Coro::State object required"); |
|
|
453 | |
|
|
454 | stash = SvSTASH (coro); |
|
|
455 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
456 | { |
|
|
457 | /* very slow, but rare, check */ |
|
|
458 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
459 | croak ("Coro::State object required"); |
|
|
460 | } |
|
|
461 | |
|
|
462 | mg = CORO_MAGIC_state (coro); |
|
|
463 | return (struct coro *)mg->mg_ptr; |
|
|
464 | } |
|
|
465 | |
|
|
466 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
467 | |
|
|
468 | /* faster than SvSTATE, but expects a coroutine hv */ |
|
|
469 | #define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) |
|
|
470 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
|
|
471 | |
490 | |
472 | /* the next two functions merely cache the padlists */ |
491 | /* the next two functions merely cache the padlists */ |
473 | static void |
492 | static void |
474 | get_padlist (pTHX_ CV *cv) |
493 | get_padlist (pTHX_ CV *cv) |
475 | { |
494 | { |
… | |
… | |
503 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
522 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
504 | |
523 | |
505 | av = (AV *)mg->mg_obj; |
524 | av = (AV *)mg->mg_obj; |
506 | |
525 | |
507 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
526 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
508 | av_extend (av, AvMAX (av) + 1); |
527 | av_extend (av, AvFILLp (av) + 1); |
509 | |
528 | |
510 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
529 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
511 | } |
530 | } |
512 | |
531 | |
513 | /** load & save, init *******************************************************/ |
532 | /** load & save, init *******************************************************/ |
|
|
533 | |
|
|
534 | static void |
|
|
535 | on_enterleave_call (pTHX_ SV *cb); |
514 | |
536 | |
515 | static void |
537 | static void |
516 | load_perl (pTHX_ Coro__State c) |
538 | load_perl (pTHX_ Coro__State c) |
517 | { |
539 | { |
518 | perl_slots *slot = c->slot; |
540 | perl_slots *slot = c->slot; |
… | |
… | |
546 | PUTBACK; |
568 | PUTBACK; |
547 | } |
569 | } |
548 | |
570 | |
549 | slf_frame = c->slf_frame; |
571 | slf_frame = c->slf_frame; |
550 | CORO_THROW = c->except; |
572 | CORO_THROW = c->except; |
|
|
573 | |
|
|
574 | if (expect_false (c->on_enter)) |
|
|
575 | { |
|
|
576 | int i; |
|
|
577 | |
|
|
578 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
|
|
579 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
|
|
580 | } |
551 | } |
581 | } |
552 | |
582 | |
553 | static void |
583 | static void |
554 | save_perl (pTHX_ Coro__State c) |
584 | save_perl (pTHX_ Coro__State c) |
555 | { |
585 | { |
|
|
586 | if (expect_false (c->on_leave)) |
|
|
587 | { |
|
|
588 | int i; |
|
|
589 | |
|
|
590 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
|
|
591 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
|
|
592 | } |
|
|
593 | |
556 | c->except = CORO_THROW; |
594 | c->except = CORO_THROW; |
557 | c->slf_frame = slf_frame; |
595 | c->slf_frame = slf_frame; |
558 | |
596 | |
559 | { |
597 | { |
560 | dSP; |
598 | dSP; |
… | |
… | |
759 | #endif |
797 | #endif |
760 | |
798 | |
761 | /* |
799 | /* |
762 | * This overrides the default magic get method of %SIG elements. |
800 | * This overrides the default magic get method of %SIG elements. |
763 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
801 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
764 | * and instead of tryign to save and restore the hash elements, we just provide |
802 | * and instead of trying to save and restore the hash elements, we just provide |
765 | * readback here. |
803 | * readback here. |
766 | * We only do this when the hook is != 0, as they are often set to 0 temporarily, |
|
|
767 | * not expecting this to actually change the hook. This is a potential problem |
|
|
768 | * when a schedule happens then, but we ignore this. |
|
|
769 | */ |
804 | */ |
770 | static int |
805 | static int |
771 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
806 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
772 | { |
807 | { |
773 | const char *s = MgPV_nolen_const (mg); |
808 | const char *s = MgPV_nolen_const (mg); |
… | |
… | |
826 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
861 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
827 | |
862 | |
828 | if (svp) |
863 | if (svp) |
829 | { |
864 | { |
830 | SV *old = *svp; |
865 | SV *old = *svp; |
831 | *svp = newSVsv (sv); |
866 | *svp = SvOK (sv) ? newSVsv (sv) : 0; |
832 | SvREFCNT_dec (old); |
867 | SvREFCNT_dec (old); |
833 | return 0; |
868 | return 0; |
834 | } |
869 | } |
835 | } |
870 | } |
836 | |
871 | |
… | |
… | |
928 | /* copy throw, in case it was set before coro_setup */ |
963 | /* copy throw, in case it was set before coro_setup */ |
929 | CORO_THROW = coro->except; |
964 | CORO_THROW = coro->except; |
930 | } |
965 | } |
931 | |
966 | |
932 | static void |
967 | static void |
933 | coro_destruct (pTHX_ struct coro *coro) |
968 | coro_unwind_stacks (pTHX) |
934 | { |
969 | { |
935 | if (!IN_DESTRUCT) |
970 | if (!IN_DESTRUCT) |
936 | { |
971 | { |
937 | /* restore all saved variables and stuff */ |
972 | /* restore all saved variables and stuff */ |
938 | LEAVE_SCOPE (0); |
973 | LEAVE_SCOPE (0); |
… | |
… | |
946 | POPSTACK_TO (PL_mainstack); |
981 | POPSTACK_TO (PL_mainstack); |
947 | |
982 | |
948 | /* unwind main stack */ |
983 | /* unwind main stack */ |
949 | dounwind (-1); |
984 | dounwind (-1); |
950 | } |
985 | } |
|
|
986 | } |
|
|
987 | |
|
|
988 | static void |
|
|
989 | coro_destruct_perl (pTHX_ struct coro *coro) |
|
|
990 | { |
|
|
991 | coro_unwind_stacks (aTHX); |
951 | |
992 | |
952 | SvREFCNT_dec (GvSV (PL_defgv)); |
993 | SvREFCNT_dec (GvSV (PL_defgv)); |
953 | SvREFCNT_dec (GvAV (PL_defgv)); |
994 | SvREFCNT_dec (GvAV (PL_defgv)); |
954 | SvREFCNT_dec (GvSV (PL_errgv)); |
995 | SvREFCNT_dec (GvSV (PL_errgv)); |
955 | SvREFCNT_dec (PL_defoutgv); |
996 | SvREFCNT_dec (PL_defoutgv); |
… | |
… | |
1178 | /* |
1219 | /* |
1179 | * If perl-run returns we assume exit() was being called or the coro |
1220 | * If perl-run returns we assume exit() was being called or the coro |
1180 | * fell off the end, which seems to be the only valid (non-bug) |
1221 | * fell off the end, which seems to be the only valid (non-bug) |
1181 | * reason for perl_run to return. We try to exit by jumping to the |
1222 | * reason for perl_run to return. We try to exit by jumping to the |
1182 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1223 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1183 | * coroutine as Coro has no such concept |
1224 | * coroutine as Coro has no such concept. |
|
|
1225 | * This actually isn't valid with the pthread backend, but OSes requiring |
|
|
1226 | * that backend are too broken to do it in a standards-compliant way. |
1184 | */ |
1227 | */ |
1185 | PL_top_env = main_top_env; |
1228 | PL_top_env = main_top_env; |
1186 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1229 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1187 | } |
1230 | } |
1188 | } |
1231 | } |
… | |
… | |
1265 | cctx_destroy (coro_cctx *cctx) |
1308 | cctx_destroy (coro_cctx *cctx) |
1266 | { |
1309 | { |
1267 | if (!cctx) |
1310 | if (!cctx) |
1268 | return; |
1311 | return; |
1269 | |
1312 | |
1270 | assert (cctx != cctx_current);//D temporary |
1313 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary? |
1271 | |
1314 | |
1272 | --cctx_count; |
1315 | --cctx_count; |
1273 | coro_destroy (&cctx->cctx); |
1316 | coro_destroy (&cctx->cctx); |
1274 | |
1317 | |
1275 | /* coro_transfer creates new, empty cctx's */ |
1318 | /* coro_transfer creates new, empty cctx's */ |
… | |
… | |
1393 | coro_setup (aTHX_ next); |
1436 | coro_setup (aTHX_ next); |
1394 | } |
1437 | } |
1395 | else |
1438 | else |
1396 | load_perl (aTHX_ next); |
1439 | load_perl (aTHX_ next); |
1397 | |
1440 | |
1398 | assert (!prev->cctx);//D temporary |
|
|
1399 | |
|
|
1400 | /* possibly untie and reuse the cctx */ |
1441 | /* possibly untie and reuse the cctx */ |
1401 | if (expect_true ( |
1442 | if (expect_true ( |
1402 | cctx_current->idle_sp == STACKLEVEL |
1443 | cctx_current->idle_sp == STACKLEVEL |
1403 | && !(cctx_current->flags & CC_TRACE) |
1444 | && !(cctx_current->flags & CC_TRACE) |
1404 | && !force_cctx |
1445 | && !force_cctx |
… | |
… | |
1459 | --coro_nready; |
1500 | --coro_nready; |
1460 | } |
1501 | } |
1461 | else |
1502 | else |
1462 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1503 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1463 | |
1504 | |
1464 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1505 | if (coro->mainstack |
|
|
1506 | && coro->mainstack != main_mainstack |
|
|
1507 | && coro->slot |
|
|
1508 | && !PL_dirty) |
1465 | { |
1509 | { |
1466 | struct coro temp; |
1510 | struct coro *current = SvSTATE_current; |
1467 | |
1511 | |
1468 | assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); |
1512 | assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); |
1469 | |
1513 | |
1470 | save_perl (aTHX_ &temp); |
1514 | save_perl (aTHX_ current); |
1471 | load_perl (aTHX_ coro); |
1515 | load_perl (aTHX_ coro); |
1472 | |
1516 | |
1473 | coro_destruct (aTHX_ coro); |
1517 | coro_destruct_perl (aTHX_ coro); |
1474 | |
1518 | |
1475 | load_perl (aTHX_ &temp); |
1519 | load_perl (aTHX_ current); |
1476 | |
1520 | |
1477 | coro->slot = 0; |
1521 | coro->slot = 0; |
1478 | } |
1522 | } |
1479 | |
1523 | |
1480 | cctx_destroy (coro->cctx); |
1524 | cctx_destroy (coro->cctx); |
… | |
… | |
1850 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1894 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1851 | api_ready (aTHX_ sv_manager); |
1895 | api_ready (aTHX_ sv_manager); |
1852 | |
1896 | |
1853 | frame->prepare = prepare_schedule; |
1897 | frame->prepare = prepare_schedule; |
1854 | frame->check = slf_check_repeat; |
1898 | frame->check = slf_check_repeat; |
|
|
1899 | |
|
|
1900 | /* as a minor optimisation, we could unwind all stacks here */ |
|
|
1901 | /* but that puts extra pressure on pp_slf, and is not worth much */ |
|
|
1902 | /*coro_unwind_stacks (aTHX);*/ |
1855 | } |
1903 | } |
1856 | |
1904 | |
1857 | /*****************************************************************************/ |
1905 | /*****************************************************************************/ |
1858 | /* async pool handler */ |
1906 | /* async pool handler */ |
1859 | |
1907 | |
… | |
… | |
2278 | |
2326 | |
2279 | PL_op->op_ppaddr = pp_slf; |
2327 | PL_op->op_ppaddr = pp_slf; |
2280 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2328 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2281 | |
2329 | |
2282 | PL_op = (OP *)&slf_restore; |
2330 | PL_op = (OP *)&slf_restore; |
|
|
2331 | } |
|
|
2332 | |
|
|
2333 | /*****************************************************************************/ |
|
|
2334 | /* dynamic wind */ |
|
|
2335 | |
|
|
2336 | static void |
|
|
2337 | on_enterleave_call (pTHX_ SV *cb) |
|
|
2338 | { |
|
|
2339 | dSP; |
|
|
2340 | |
|
|
2341 | PUSHSTACK; |
|
|
2342 | |
|
|
2343 | PUSHMARK (SP); |
|
|
2344 | PUTBACK; |
|
|
2345 | call_sv (cb, G_VOID | G_DISCARD); |
|
|
2346 | SPAGAIN; |
|
|
2347 | |
|
|
2348 | POPSTACK; |
|
|
2349 | } |
|
|
2350 | |
|
|
2351 | static SV * |
|
|
2352 | coro_avp_pop_and_free (pTHX_ AV **avp) |
|
|
2353 | { |
|
|
2354 | AV *av = *avp; |
|
|
2355 | SV *res = av_pop (av); |
|
|
2356 | |
|
|
2357 | if (AvFILLp (av) < 0) |
|
|
2358 | { |
|
|
2359 | *avp = 0; |
|
|
2360 | SvREFCNT_dec (av); |
|
|
2361 | } |
|
|
2362 | |
|
|
2363 | return res; |
|
|
2364 | } |
|
|
2365 | |
|
|
2366 | static void |
|
|
2367 | coro_pop_on_enter (pTHX_ void *coro) |
|
|
2368 | { |
|
|
2369 | SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter); |
|
|
2370 | SvREFCNT_dec (cb); |
|
|
2371 | } |
|
|
2372 | |
|
|
2373 | static void |
|
|
2374 | coro_pop_on_leave (pTHX_ void *coro) |
|
|
2375 | { |
|
|
2376 | SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave); |
|
|
2377 | on_enterleave_call (aTHX_ sv_2mortal (cb)); |
2283 | } |
2378 | } |
2284 | |
2379 | |
2285 | /*****************************************************************************/ |
2380 | /*****************************************************************************/ |
2286 | /* PerlIO::cede */ |
2381 | /* PerlIO::cede */ |
2287 | |
2382 | |
… | |
… | |
2368 | SV **ary; |
2463 | SV **ary; |
2369 | |
2464 | |
2370 | /* unfortunately, building manually saves memory */ |
2465 | /* unfortunately, building manually saves memory */ |
2371 | Newx (ary, 2, SV *); |
2466 | Newx (ary, 2, SV *); |
2372 | AvALLOC (av) = ary; |
2467 | AvALLOC (av) = ary; |
|
|
2468 | #if PERL_VERSION_ATLEAST (5,10,0) |
2373 | /*AvARRAY (av) = ary;*/ |
2469 | AvARRAY (av) = ary; |
|
|
2470 | #else |
2374 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
2471 | /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */ |
|
|
2472 | /* -DDEBUGGING flags this as a bug, despite it perfectly working */ |
|
|
2473 | SvPVX ((SV *)av) = (char *)ary; |
|
|
2474 | #endif |
2375 | AvMAX (av) = 1; |
2475 | AvMAX (av) = 1; |
2376 | AvFILLp (av) = 0; |
2476 | AvFILLp (av) = 0; |
2377 | ary [0] = newSViv (count); |
2477 | ary [0] = newSViv (count); |
2378 | |
2478 | |
2379 | return newRV_noinc ((SV *)av); |
2479 | return newRV_noinc ((SV *)av); |
… | |
… | |
2908 | SV * |
3008 | SV * |
2909 | clone (Coro::State coro) |
3009 | clone (Coro::State coro) |
2910 | CODE: |
3010 | CODE: |
2911 | { |
3011 | { |
2912 | #if CORO_CLONE |
3012 | #if CORO_CLONE |
2913 | struct coro *ncoro = coro_clone (coro); |
3013 | struct coro *ncoro = coro_clone (aTHX_ coro); |
2914 | MAGIC *mg; |
3014 | MAGIC *mg; |
2915 | /* TODO: too much duplication */ |
3015 | /* TODO: too much duplication */ |
2916 | ncoro->hv = newHV (); |
3016 | ncoro->hv = newHV (); |
2917 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
3017 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
2918 | mg->mg_flags |= MGf_DUP; |
3018 | mg->mg_flags |= MGf_DUP; |
… | |
… | |
2980 | eval = 1 |
3080 | eval = 1 |
2981 | CODE: |
3081 | CODE: |
2982 | { |
3082 | { |
2983 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
3083 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
2984 | { |
3084 | { |
2985 | struct coro temp; |
3085 | struct coro *current = SvSTATE_current; |
2986 | |
3086 | |
2987 | if (!(coro->flags & CF_RUNNING)) |
3087 | if (current != coro) |
2988 | { |
3088 | { |
2989 | PUTBACK; |
3089 | PUTBACK; |
2990 | save_perl (aTHX_ &temp); |
3090 | save_perl (aTHX_ current); |
2991 | load_perl (aTHX_ coro); |
3091 | load_perl (aTHX_ coro); |
|
|
3092 | SPAGAIN; |
2992 | } |
3093 | } |
2993 | |
3094 | |
2994 | { |
|
|
2995 | dSP; |
|
|
2996 | ENTER; |
|
|
2997 | SAVETMPS; |
|
|
2998 | PUTBACK; |
|
|
2999 | PUSHSTACK; |
3095 | PUSHSTACK; |
|
|
3096 | |
3000 | PUSHMARK (SP); |
3097 | PUSHMARK (SP); |
|
|
3098 | PUTBACK; |
3001 | |
3099 | |
3002 | if (ix) |
3100 | if (ix) |
3003 | eval_sv (coderef, 0); |
3101 | eval_sv (coderef, 0); |
3004 | else |
3102 | else |
3005 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
3103 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
3006 | |
3104 | |
|
|
3105 | SPAGAIN; |
3007 | POPSTACK; |
3106 | POPSTACK; |
3008 | SPAGAIN; |
|
|
3009 | FREETMPS; |
|
|
3010 | LEAVE; |
|
|
3011 | PUTBACK; |
|
|
3012 | } |
|
|
3013 | |
3107 | |
3014 | if (!(coro->flags & CF_RUNNING)) |
3108 | if (current != coro) |
3015 | { |
3109 | { |
|
|
3110 | PUTBACK; |
3016 | save_perl (aTHX_ coro); |
3111 | save_perl (aTHX_ coro); |
3017 | load_perl (aTHX_ &temp); |
3112 | load_perl (aTHX_ current); |
3018 | SPAGAIN; |
3113 | SPAGAIN; |
3019 | } |
3114 | } |
3020 | } |
3115 | } |
3021 | } |
3116 | } |
3022 | |
3117 | |
… | |
… | |
3109 | { |
3204 | { |
3110 | int i; |
3205 | int i; |
3111 | |
3206 | |
3112 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3207 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3113 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3208 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3114 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
3209 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3115 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3210 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3116 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3211 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3117 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3212 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3118 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3213 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3119 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
3214 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
… | |
… | |
3304 | rouse_wait (...) |
3399 | rouse_wait (...) |
3305 | PROTOTYPE: ;$ |
3400 | PROTOTYPE: ;$ |
3306 | PPCODE: |
3401 | PPCODE: |
3307 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3402 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3308 | |
3403 | |
|
|
3404 | void |
|
|
3405 | on_enter (SV *block) |
|
|
3406 | ALIAS: |
|
|
3407 | on_leave = 1 |
|
|
3408 | PROTOTYPE: & |
|
|
3409 | CODE: |
|
|
3410 | { |
|
|
3411 | struct coro *coro = SvSTATE_current; |
|
|
3412 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
|
|
3413 | |
|
|
3414 | block = (SV *)coro_sv_2cv (aTHX_ block); |
|
|
3415 | |
|
|
3416 | if (!*avp) |
|
|
3417 | *avp = newAV (); |
|
|
3418 | |
|
|
3419 | av_push (*avp, SvREFCNT_inc (block)); |
|
|
3420 | |
|
|
3421 | if (!ix) |
|
|
3422 | on_enterleave_call (aTHX_ block); |
|
|
3423 | |
|
|
3424 | LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */ |
|
|
3425 | SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro); |
|
|
3426 | ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */ |
|
|
3427 | } |
|
|
3428 | |
3309 | |
3429 | |
3310 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3430 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3311 | |
3431 | |
3312 | BOOT: |
3432 | BOOT: |
3313 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
3433 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |