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.337 by root, Fri Dec 5 18:26:10 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
1480 if (coro->mainstack 1505 if (coro->mainstack
1481 && coro->mainstack != main_mainstack 1506 && coro->mainstack != main_mainstack
1482 && coro->slot 1507 && coro->slot
1483 && !PL_dirty) 1508 && !PL_dirty)
1484 { 1509 {
1485 struct coro temp; 1510 struct coro *current = SvSTATE_current;
1486 1511
1487 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));
1488 1513
1489 save_perl (aTHX_ &temp); 1514 save_perl (aTHX_ current);
1490 load_perl (aTHX_ coro); 1515 load_perl (aTHX_ coro);
1491 1516
1492 coro_destruct_perl (aTHX_ coro); 1517 coro_destruct_perl (aTHX_ coro);
1493 1518
1494 load_perl (aTHX_ &temp); 1519 load_perl (aTHX_ current);
1495 1520
1496 coro->slot = 0; 1521 coro->slot = 0;
1497 } 1522 }
1498 1523
1499 cctx_destroy (coro->cctx); 1524 cctx_destroy (coro->cctx);
2301 2326
2302 PL_op->op_ppaddr = pp_slf; 2327 PL_op->op_ppaddr = pp_slf;
2303 /*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 */
2304 2329
2305 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));
2306} 2378}
2307 2379
2308/*****************************************************************************/ 2380/*****************************************************************************/
2309/* PerlIO::cede */ 2381/* PerlIO::cede */
2310 2382
3008 eval = 1 3080 eval = 1
3009 CODE: 3081 CODE:
3010{ 3082{
3011 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3083 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
3012 { 3084 {
3013 struct coro temp; 3085 struct coro *current = SvSTATE_current;
3014 3086
3015 if (!(coro->flags & CF_RUNNING)) 3087 if (current != coro)
3016 { 3088 {
3017 PUTBACK; 3089 PUTBACK;
3018 save_perl (aTHX_ &temp); 3090 save_perl (aTHX_ current);
3019 load_perl (aTHX_ coro); 3091 load_perl (aTHX_ coro);
3092 SPAGAIN;
3020 } 3093 }
3021 3094
3022 {
3023 dSP;
3024 ENTER;
3025 SAVETMPS;
3026 PUTBACK;
3027 PUSHSTACK; 3095 PUSHSTACK;
3096
3028 PUSHMARK (SP); 3097 PUSHMARK (SP);
3098 PUTBACK;
3029 3099
3030 if (ix) 3100 if (ix)
3031 eval_sv (coderef, 0); 3101 eval_sv (coderef, 0);
3032 else 3102 else
3033 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3103 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
3034 3104
3105 SPAGAIN;
3035 POPSTACK; 3106 POPSTACK;
3036 SPAGAIN;
3037 FREETMPS;
3038 LEAVE;
3039 PUTBACK;
3040 }
3041 3107
3042 if (!(coro->flags & CF_RUNNING)) 3108 if (current != coro)
3043 { 3109 {
3110 PUTBACK;
3044 save_perl (aTHX_ coro); 3111 save_perl (aTHX_ coro);
3045 load_perl (aTHX_ &temp); 3112 load_perl (aTHX_ current);
3046 SPAGAIN; 3113 SPAGAIN;
3047 } 3114 }
3048 } 3115 }
3049} 3116}
3050 3117
3332rouse_wait (...) 3399rouse_wait (...)
3333 PROTOTYPE: ;$ 3400 PROTOTYPE: ;$
3334 PPCODE: 3401 PPCODE:
3335 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3402 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3336 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
3337 3429
3338MODULE = Coro::State PACKAGE = PerlIO::cede 3430MODULE = Coro::State PACKAGE = PerlIO::cede
3339 3431
3340BOOT: 3432BOOT:
3341 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3433 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines