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.334 by root, Fri Nov 28 23:30:55 2008 UTC vs.
Revision 1.340 by root, Mon Dec 15 00:30:40 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 (aTHX_ 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 (aTHX_ 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);
1277cctx_destroy (coro_cctx *cctx) 1308cctx_destroy (coro_cctx *cctx)
1278{ 1309{
1279 if (!cctx) 1310 if (!cctx)
1280 return; 1311 return;
1281 1312
1282 assert (cctx != cctx_current);//D temporary 1313 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1283 1314
1284 --cctx_count; 1315 --cctx_count;
1285 coro_destroy (&cctx->cctx); 1316 coro_destroy (&cctx->cctx);
1286 1317
1287 /* coro_transfer creates new, empty cctx's */ 1318 /* coro_transfer creates new, empty cctx's */
1405 coro_setup (aTHX_ next); 1436 coro_setup (aTHX_ next);
1406 } 1437 }
1407 else 1438 else
1408 load_perl (aTHX_ next); 1439 load_perl (aTHX_ next);
1409 1440
1410 assert (!prev->cctx);//D temporary
1411
1412 /* possibly untie and reuse the cctx */ 1441 /* possibly untie and reuse the cctx */
1413 if (expect_true ( 1442 if (expect_true (
1414 cctx_current->idle_sp == STACKLEVEL 1443 cctx_current->idle_sp == STACKLEVEL
1415 && !(cctx_current->flags & CC_TRACE) 1444 && !(cctx_current->flags & CC_TRACE)
1416 && !force_cctx 1445 && !force_cctx
1473 else 1502 else
1474 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 */
1475 1504
1476 if (coro->mainstack 1505 if (coro->mainstack
1477 && coro->mainstack != main_mainstack 1506 && coro->mainstack != main_mainstack
1507 && coro->slot
1478 && !PL_dirty) 1508 && !PL_dirty)
1479 { 1509 {
1480 struct coro temp; 1510 struct coro *current = SvSTATE_current;
1481 1511
1482 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));
1483 1513
1484 save_perl (aTHX_ &temp); 1514 save_perl (aTHX_ current);
1485 load_perl (aTHX_ coro); 1515 load_perl (aTHX_ coro);
1486 1516
1487 coro_destruct_perl (aTHX_ coro); 1517 coro_destruct_perl (aTHX_ coro);
1488 1518
1489 load_perl (aTHX_ &temp); 1519 load_perl (aTHX_ current);
1490 1520
1491 coro->slot = 0; 1521 coro->slot = 0;
1492 } 1522 }
1493 1523
1494 cctx_destroy (coro->cctx); 1524 cctx_destroy (coro->cctx);
1864 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 */
1865 api_ready (aTHX_ sv_manager); 1895 api_ready (aTHX_ sv_manager);
1866 1896
1867 frame->prepare = prepare_schedule; 1897 frame->prepare = prepare_schedule;
1868 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);*/
1869} 1903}
1870 1904
1871/*****************************************************************************/ 1905/*****************************************************************************/
1872/* async pool handler */ 1906/* async pool handler */
1873 1907
2292 2326
2293 PL_op->op_ppaddr = pp_slf; 2327 PL_op->op_ppaddr = pp_slf;
2294 /*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 */
2295 2329
2296 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 (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
2366static void
2367coro_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
2373static void
2374coro_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));
2297} 2378}
2298 2379
2299/*****************************************************************************/ 2380/*****************************************************************************/
2300/* PerlIO::cede */ 2381/* PerlIO::cede */
2301 2382
2999 eval = 1 3080 eval = 1
3000 CODE: 3081 CODE:
3001{ 3082{
3002 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3083 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
3003 { 3084 {
3004 struct coro temp; 3085 struct coro *current = SvSTATE_current;
3005 3086
3006 if (!(coro->flags & CF_RUNNING)) 3087 if (current != coro)
3007 { 3088 {
3008 PUTBACK; 3089 PUTBACK;
3009 save_perl (aTHX_ &temp); 3090 save_perl (aTHX_ current);
3010 load_perl (aTHX_ coro); 3091 load_perl (aTHX_ coro);
3092 SPAGAIN;
3011 } 3093 }
3012 3094
3013 {
3014 dSP;
3015 ENTER;
3016 SAVETMPS;
3017 PUTBACK;
3018 PUSHSTACK; 3095 PUSHSTACK;
3096
3019 PUSHMARK (SP); 3097 PUSHMARK (SP);
3098 PUTBACK;
3020 3099
3021 if (ix) 3100 if (ix)
3022 eval_sv (coderef, 0); 3101 eval_sv (coderef, 0);
3023 else 3102 else
3024 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3103 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
3025 3104
3105 SPAGAIN;
3026 POPSTACK; 3106 POPSTACK;
3027 SPAGAIN;
3028 FREETMPS;
3029 LEAVE;
3030 PUTBACK;
3031 }
3032 3107
3033 if (!(coro->flags & CF_RUNNING)) 3108 if (current != coro)
3034 { 3109 {
3110 PUTBACK;
3035 save_perl (aTHX_ coro); 3111 save_perl (aTHX_ coro);
3036 load_perl (aTHX_ &temp); 3112 load_perl (aTHX_ current);
3037 SPAGAIN; 3113 SPAGAIN;
3038 } 3114 }
3039 } 3115 }
3040} 3116}
3041 3117
3128{ 3204{
3129 int i; 3205 int i;
3130 3206
3131 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3207 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3132 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3208 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3133 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3209 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3134 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3210 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3135 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);
3136 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3212 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3137 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3213 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3138 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3214 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3323rouse_wait (...) 3399rouse_wait (...)
3324 PROTOTYPE: ;$ 3400 PROTOTYPE: ;$
3325 PPCODE: 3401 PPCODE:
3326 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3402 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3327 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 (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
3328 3429
3329MODULE = Coro::State PACKAGE = PerlIO::cede 3430MODULE = Coro::State PACKAGE = PerlIO::cede
3330 3431
3331BOOT: 3432BOOT:
3332 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3433 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines