ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.336 by root, Thu Dec 4 17:29:40 2008 UTC vs.
Revision 1.339 by root, Mon Dec 15 00:28:30 2008 UTC

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
294typedef struct coro *Coro__State; 298typedef struct coro *Coro__State;
352INLINE CV * 356INLINE CV *
353coro_sv_2cv (pTHX_ SV *sv) 357coro_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}
359 368
360/*****************************************************************************/ 369/*****************************************************************************/
361/* magic glue */ 370/* magic glue */
362 371
521} 530}
522 531
523/** load & save, init *******************************************************/ 532/** load & save, init *******************************************************/
524 533
525static void 534static void
535on_enterleave_call (pTHX_ SV *cb);
536
537static void
526load_perl (pTHX_ Coro__State c) 538load_perl (pTHX_ Coro__State c)
527{ 539{
528 perl_slots *slot = c->slot; 540 perl_slots *slot = c->slot;
529 c->slot = 0; 541 c->slot = 0;
530 542
556 PUTBACK; 568 PUTBACK;
557 } 569 }
558 570
559 slf_frame = c->slf_frame; 571 slf_frame = c->slf_frame;
560 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 (AvARRAY (c->on_enter)[i]);
580 }
561} 581}
562 582
563static void 583static void
564save_perl (pTHX_ Coro__State c) 584save_perl (pTHX_ Coro__State c)
565{ 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 (AvARRAY (c->on_leave)[i]);
592 }
593
566 c->except = CORO_THROW; 594 c->except = CORO_THROW;
567 c->slf_frame = slf_frame; 595 c->slf_frame = slf_frame;
568 596
569 { 597 {
570 dSP; 598 dSP;
769#endif 797#endif
770 798
771/* 799/*
772 * This overrides the default magic get method of %SIG elements. 800 * This overrides the default magic get method of %SIG elements.
773 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 801 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
774 * and instead of tryign to save and restore the hash elements, we just provide 802 * and instead of trying to save and restore the hash elements, we just provide
775 * readback here. 803 * readback here.
776 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
777 * not expecting this to actually change the hook. This is a potential problem
778 * when a schedule happens then, but we ignore this.
779 */ 804 */
780static int 805static int
781coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 806coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
782{ 807{
783 const char *s = MgPV_nolen_const (mg); 808 const char *s = MgPV_nolen_const (mg);
836 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 861 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
837 862
838 if (svp) 863 if (svp)
839 { 864 {
840 SV *old = *svp; 865 SV *old = *svp;
841 *svp = newSVsv (sv); 866 *svp = SvOK (sv) ? newSVsv (sv) : 0;
842 SvREFCNT_dec (old); 867 SvREFCNT_dec (old);
843 return 0; 868 return 0;
844 } 869 }
845 } 870 }
846 871
938 /* copy throw, in case it was set before coro_setup */ 963 /* copy throw, in case it was set before coro_setup */
939 CORO_THROW = coro->except; 964 CORO_THROW = coro->except;
940} 965}
941 966
942static void 967static void
943coro_destruct_perl (pTHX_ struct coro *coro) 968coro_unwind_stacks (pTHX)
944{ 969{
945 if (!IN_DESTRUCT) 970 if (!IN_DESTRUCT)
946 { 971 {
947 /* restore all saved variables and stuff */ 972 /* restore all saved variables and stuff */
948 LEAVE_SCOPE (0); 973 LEAVE_SCOPE (0);
956 POPSTACK_TO (PL_mainstack); 981 POPSTACK_TO (PL_mainstack);
957 982
958 /* unwind main stack */ 983 /* unwind main stack */
959 dounwind (-1); 984 dounwind (-1);
960 } 985 }
986}
987
988static void
989coro_destruct_perl (pTHX_ struct coro *coro)
990{
991 coro_unwind_stacks (aTHX);
961 992
962 SvREFCNT_dec (GvSV (PL_defgv)); 993 SvREFCNT_dec (GvSV (PL_defgv));
963 SvREFCNT_dec (GvAV (PL_defgv)); 994 SvREFCNT_dec (GvAV (PL_defgv));
964 SvREFCNT_dec (GvSV (PL_errgv)); 995 SvREFCNT_dec (GvSV (PL_errgv));
965 SvREFCNT_dec (PL_defoutgv); 996 SvREFCNT_dec (PL_defoutgv);
1474 if (coro->mainstack 1505 if (coro->mainstack
1475 && coro->mainstack != main_mainstack 1506 && coro->mainstack != main_mainstack
1476 && coro->slot 1507 && coro->slot
1477 && !PL_dirty) 1508 && !PL_dirty)
1478 { 1509 {
1479 struct coro temp; 1510 struct coro *current = SvSTATE_current;
1480 1511
1481 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); 1512 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1482 1513
1483 save_perl (aTHX_ &temp); 1514 save_perl (aTHX_ current);
1484 load_perl (aTHX_ coro); 1515 load_perl (aTHX_ coro);
1485 1516
1486 coro_destruct_perl (aTHX_ coro); 1517 coro_destruct_perl (aTHX_ coro);
1487 1518
1488 load_perl (aTHX_ &temp); 1519 load_perl (aTHX_ current);
1489 1520
1490 coro->slot = 0; 1521 coro->slot = 0;
1491 } 1522 }
1492 1523
1493 cctx_destroy (coro->cctx); 1524 cctx_destroy (coro->cctx);
1863 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 */
1864 api_ready (aTHX_ sv_manager); 1895 api_ready (aTHX_ sv_manager);
1865 1896
1866 frame->prepare = prepare_schedule; 1897 frame->prepare = prepare_schedule;
1867 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);*/
1868} 1903}
1869 1904
1870/*****************************************************************************/ 1905/*****************************************************************************/
1871/* async pool handler */ 1906/* async pool handler */
1872 1907
2291 2326
2292 PL_op->op_ppaddr = pp_slf; 2327 PL_op->op_ppaddr = pp_slf;
2293 /*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 */
2294 2329
2295 PL_op = (OP *)&slf_restore; 2330 PL_op = (OP *)&slf_restore;
2331}
2332
2333/*****************************************************************************/
2334/* dynamic wind */
2335
2336static void
2337on_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
2351static SV *
2352coro_avp_pop_and_free (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
2366static void
2367coro_pop_on_enter (pTHX_ void *coro)
2368{
2369 SV *cb = coro_avp_pop_and_free (&((struct coro *)coro)->on_enter);
2370 SvREFCNT_dec (cb);
2371}
2372
2373static void
2374coro_pop_on_leave (pTHX_ void *coro)
2375{
2376 SV *cb = coro_avp_pop_and_free (&((struct coro *)coro)->on_leave);
2377 on_enterleave_call (sv_2mortal (cb));
2296} 2378}
2297 2379
2298/*****************************************************************************/ 2380/*****************************************************************************/
2299/* PerlIO::cede */ 2381/* PerlIO::cede */
2300 2382
2998 eval = 1 3080 eval = 1
2999 CODE: 3081 CODE:
3000{ 3082{
3001 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3083 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
3002 { 3084 {
3003 struct coro temp; 3085 struct coro *current = SvSTATE_current;
3004 3086
3005 if (!(coro->flags & CF_RUNNING)) 3087 if (current != coro)
3006 { 3088 {
3007 PUTBACK; 3089 PUTBACK;
3008 save_perl (aTHX_ &temp); 3090 save_perl (aTHX_ current);
3009 load_perl (aTHX_ coro); 3091 load_perl (aTHX_ coro);
3092 SPAGAIN;
3010 } 3093 }
3011 3094
3012 {
3013 dSP;
3014 ENTER;
3015 SAVETMPS;
3016 PUTBACK;
3017 PUSHSTACK; 3095 PUSHSTACK;
3096
3018 PUSHMARK (SP); 3097 PUSHMARK (SP);
3098 PUTBACK;
3019 3099
3020 if (ix) 3100 if (ix)
3021 eval_sv (coderef, 0); 3101 eval_sv (coderef, 0);
3022 else 3102 else
3023 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3103 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
3024 3104
3105 SPAGAIN;
3025 POPSTACK; 3106 POPSTACK;
3026 SPAGAIN;
3027 FREETMPS;
3028 LEAVE;
3029 PUTBACK;
3030 }
3031 3107
3032 if (!(coro->flags & CF_RUNNING)) 3108 if (current != coro)
3033 { 3109 {
3110 PUTBACK;
3034 save_perl (aTHX_ coro); 3111 save_perl (aTHX_ coro);
3035 load_perl (aTHX_ &temp); 3112 load_perl (aTHX_ current);
3036 SPAGAIN; 3113 SPAGAIN;
3037 } 3114 }
3038 } 3115 }
3039} 3116}
3040 3117
3322rouse_wait (...) 3399rouse_wait (...)
3323 PROTOTYPE: ;$ 3400 PROTOTYPE: ;$
3324 PPCODE: 3401 PPCODE:
3325 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3402 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3326 3403
3404void
3405on_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 (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
3327 3429
3328MODULE = Coro::State PACKAGE = PerlIO::cede 3430MODULE = Coro::State PACKAGE = PerlIO::cede
3329 3431
3330BOOT: 3432BOOT:
3331 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3433 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines