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.344 by root, Mon Dec 15 19:39:40 2008 UTC

238enum { 238enum {
239 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
240 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
241 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
242 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 242 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
243 CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */
243}; 244};
244 245
245/* the structure where most of the perl state is stored, overlaid on the cxstack */ 246/* the structure where most of the perl state is stored, overlaid on the cxstack */
246typedef struct 247typedef struct
247{ 248{
285 /* async_pool */ 286 /* async_pool */
286 SV *saved_deffh; 287 SV *saved_deffh;
287 SV *invoke_cb; 288 SV *invoke_cb;
288 AV *invoke_av; 289 AV *invoke_av;
289 290
291 /* on_enter/on_leave */
292 AV *on_enter;
293 AV *on_leave;
294
290 /* linked list */ 295 /* linked list */
291 struct coro *next, *prev; 296 struct coro *next, *prev;
292}; 297};
293 298
294typedef struct coro *Coro__State; 299typedef struct coro *Coro__State;
352INLINE CV * 357INLINE CV *
353coro_sv_2cv (pTHX_ SV *sv) 358coro_sv_2cv (pTHX_ SV *sv)
354{ 359{
355 HV *st; 360 HV *st;
356 GV *gvp; 361 GV *gvp;
357 return sv_2cv (sv, &st, &gvp, 0); 362 CV *cv = sv_2cv (sv, &st, &gvp, 0);
363
364 if (!cv)
365 croak ("code reference expected");
366
367 return cv;
358} 368}
359 369
360/*****************************************************************************/ 370/*****************************************************************************/
361/* magic glue */ 371/* magic glue */
362 372
521} 531}
522 532
523/** load & save, init *******************************************************/ 533/** load & save, init *******************************************************/
524 534
525static void 535static void
536on_enterleave_call (pTHX_ SV *cb);
537
538static void
526load_perl (pTHX_ Coro__State c) 539load_perl (pTHX_ Coro__State c)
527{ 540{
528 perl_slots *slot = c->slot; 541 perl_slots *slot = c->slot;
529 c->slot = 0; 542 c->slot = 0;
530 543
556 PUTBACK; 569 PUTBACK;
557 } 570 }
558 571
559 slf_frame = c->slf_frame; 572 slf_frame = c->slf_frame;
560 CORO_THROW = c->except; 573 CORO_THROW = c->except;
574
575 if (expect_false (c->on_enter))
576 {
577 int i;
578
579 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
580 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
581 }
561} 582}
562 583
563static void 584static void
564save_perl (pTHX_ Coro__State c) 585save_perl (pTHX_ Coro__State c)
565{ 586{
587 if (expect_false (c->on_leave))
588 {
589 int i;
590
591 for (i = AvFILLp (c->on_leave); i >= 0; --i)
592 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
593 }
594
566 c->except = CORO_THROW; 595 c->except = CORO_THROW;
567 c->slf_frame = slf_frame; 596 c->slf_frame = slf_frame;
568 597
569 { 598 {
570 dSP; 599 dSP;
769#endif 798#endif
770 799
771/* 800/*
772 * This overrides the default magic get method of %SIG elements. 801 * 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 802 * 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 803 * and instead of trying to save and restore the hash elements, we just provide
775 * readback here. 804 * 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 */ 805 */
780static int 806static int
781coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 807coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
782{ 808{
783 const char *s = MgPV_nolen_const (mg); 809 const char *s = MgPV_nolen_const (mg);
836 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 862 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
837 863
838 if (svp) 864 if (svp)
839 { 865 {
840 SV *old = *svp; 866 SV *old = *svp;
841 *svp = newSVsv (sv); 867 *svp = SvOK (sv) ? newSVsv (sv) : 0;
842 SvREFCNT_dec (old); 868 SvREFCNT_dec (old);
843 return 0; 869 return 0;
844 } 870 }
845 } 871 }
846 872
938 /* copy throw, in case it was set before coro_setup */ 964 /* copy throw, in case it was set before coro_setup */
939 CORO_THROW = coro->except; 965 CORO_THROW = coro->except;
940} 966}
941 967
942static void 968static void
943coro_destruct_perl (pTHX_ struct coro *coro) 969coro_unwind_stacks (pTHX)
944{ 970{
945 if (!IN_DESTRUCT) 971 if (!IN_DESTRUCT)
946 { 972 {
947 /* restore all saved variables and stuff */ 973 /* restore all saved variables and stuff */
948 LEAVE_SCOPE (0); 974 LEAVE_SCOPE (0);
956 POPSTACK_TO (PL_mainstack); 982 POPSTACK_TO (PL_mainstack);
957 983
958 /* unwind main stack */ 984 /* unwind main stack */
959 dounwind (-1); 985 dounwind (-1);
960 } 986 }
987}
988
989static void
990coro_destruct_perl (pTHX_ struct coro *coro)
991{
992 coro_unwind_stacks (aTHX);
961 993
962 SvREFCNT_dec (GvSV (PL_defgv)); 994 SvREFCNT_dec (GvSV (PL_defgv));
963 SvREFCNT_dec (GvAV (PL_defgv)); 995 SvREFCNT_dec (GvAV (PL_defgv));
964 SvREFCNT_dec (GvSV (PL_errgv)); 996 SvREFCNT_dec (GvSV (PL_errgv));
965 SvREFCNT_dec (PL_defoutgv); 997 SvREFCNT_dec (PL_defoutgv);
1351 /* TODO: throwing up here is considered harmful */ 1383 /* TODO: throwing up here is considered harmful */
1352 1384
1353 if (expect_true (prev != next)) 1385 if (expect_true (prev != next))
1354 { 1386 {
1355 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1387 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1356 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); 1388 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1357 1389
1358 if (expect_false (next->flags & CF_RUNNING))
1359 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1360
1361 if (expect_false (next->flags & CF_DESTROYED)) 1390 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1362 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); 1391 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1363 1392
1364#if !PERL_VERSION_ATLEAST (5,10,0) 1393#if !PERL_VERSION_ATLEAST (5,10,0)
1365 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1394 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1366 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); 1395 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1367#endif 1396#endif
1455coro_state_destroy (pTHX_ struct coro *coro) 1484coro_state_destroy (pTHX_ struct coro *coro)
1456{ 1485{
1457 if (coro->flags & CF_DESTROYED) 1486 if (coro->flags & CF_DESTROYED)
1458 return 0; 1487 return 0;
1459 1488
1460 if (coro->on_destroy) 1489 if (coro->on_destroy && !PL_dirty)
1461 coro->on_destroy (aTHX_ coro); 1490 coro->on_destroy (aTHX_ coro);
1462 1491
1463 coro->flags |= CF_DESTROYED; 1492 coro->flags |= CF_DESTROYED;
1464 1493
1465 if (coro->flags & CF_READY) 1494 if (coro->flags & CF_READY)
1474 if (coro->mainstack 1503 if (coro->mainstack
1475 && coro->mainstack != main_mainstack 1504 && coro->mainstack != main_mainstack
1476 && coro->slot 1505 && coro->slot
1477 && !PL_dirty) 1506 && !PL_dirty)
1478 { 1507 {
1479 struct coro temp; 1508 struct coro *current = SvSTATE_current;
1480 1509
1481 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); 1510 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1482 1511
1483 save_perl (aTHX_ &temp); 1512 save_perl (aTHX_ current);
1484 load_perl (aTHX_ coro); 1513 load_perl (aTHX_ coro);
1485 1514
1486 coro_destruct_perl (aTHX_ coro); 1515 coro_destruct_perl (aTHX_ coro);
1487 1516
1488 load_perl (aTHX_ &temp); 1517 load_perl (aTHX_ current);
1489 1518
1490 coro->slot = 0; 1519 coro->slot = 0;
1491 } 1520 }
1492 1521
1493 cctx_destroy (coro->cctx); 1522 cctx_destroy (coro->cctx);
1674 if (expect_true (next_sv)) 1703 if (expect_true (next_sv))
1675 { 1704 {
1676 struct coro *next = SvSTATE_hv (next_sv); 1705 struct coro *next = SvSTATE_hv (next_sv);
1677 1706
1678 /* cannot transfer to destroyed coros, skip and look for next */ 1707 /* cannot transfer to destroyed coros, skip and look for next */
1679 if (expect_false (next->flags & CF_DESTROYED)) 1708 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1680 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1709 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1681 else 1710 else
1682 { 1711 {
1683 next->flags &= ~CF_READY; 1712 next->flags &= ~CF_READY;
1684 --coro_nready; 1713 --coro_nready;
1863 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 1892 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1864 api_ready (aTHX_ sv_manager); 1893 api_ready (aTHX_ sv_manager);
1865 1894
1866 frame->prepare = prepare_schedule; 1895 frame->prepare = prepare_schedule;
1867 frame->check = slf_check_repeat; 1896 frame->check = slf_check_repeat;
1897
1898 /* as a minor optimisation, we could unwind all stacks here */
1899 /* but that puts extra pressure on pp_slf, and is not worth much */
1900 /*coro_unwind_stacks (aTHX);*/
1868} 1901}
1869 1902
1870/*****************************************************************************/ 1903/*****************************************************************************/
1871/* async pool handler */ 1904/* async pool handler */
1872 1905
2291 2324
2292 PL_op->op_ppaddr = pp_slf; 2325 PL_op->op_ppaddr = pp_slf;
2293 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2326 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2294 2327
2295 PL_op = (OP *)&slf_restore; 2328 PL_op = (OP *)&slf_restore;
2329}
2330
2331/*****************************************************************************/
2332/* dynamic wind */
2333
2334static void
2335on_enterleave_call (pTHX_ SV *cb)
2336{
2337 dSP;
2338
2339 PUSHSTACK;
2340
2341 PUSHMARK (SP);
2342 PUTBACK;
2343 call_sv (cb, G_VOID | G_DISCARD);
2344 SPAGAIN;
2345
2346 POPSTACK;
2347}
2348
2349static SV *
2350coro_avp_pop_and_free (pTHX_ AV **avp)
2351{
2352 AV *av = *avp;
2353 SV *res = av_pop (av);
2354
2355 if (AvFILLp (av) < 0)
2356 {
2357 *avp = 0;
2358 SvREFCNT_dec (av);
2359 }
2360
2361 return res;
2362}
2363
2364static void
2365coro_pop_on_enter (pTHX_ void *coro)
2366{
2367 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2368 SvREFCNT_dec (cb);
2369}
2370
2371static void
2372coro_pop_on_leave (pTHX_ void *coro)
2373{
2374 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2375 on_enterleave_call (aTHX_ sv_2mortal (cb));
2296} 2376}
2297 2377
2298/*****************************************************************************/ 2378/*****************************************************************************/
2299/* PerlIO::cede */ 2379/* PerlIO::cede */
2300 2380
2998 eval = 1 3078 eval = 1
2999 CODE: 3079 CODE:
3000{ 3080{
3001 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3081 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
3002 { 3082 {
3003 struct coro temp; 3083 struct coro *current = SvSTATE_current;
3004 3084
3005 if (!(coro->flags & CF_RUNNING)) 3085 if (current != coro)
3006 { 3086 {
3007 PUTBACK; 3087 PUTBACK;
3008 save_perl (aTHX_ &temp); 3088 save_perl (aTHX_ current);
3009 load_perl (aTHX_ coro); 3089 load_perl (aTHX_ coro);
3090 SPAGAIN;
3010 } 3091 }
3011 3092
3012 {
3013 dSP;
3014 ENTER;
3015 SAVETMPS;
3016 PUTBACK;
3017 PUSHSTACK; 3093 PUSHSTACK;
3094
3018 PUSHMARK (SP); 3095 PUSHMARK (SP);
3096 PUTBACK;
3019 3097
3020 if (ix) 3098 if (ix)
3021 eval_sv (coderef, 0); 3099 eval_sv (coderef, 0);
3022 else 3100 else
3023 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3101 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
3024 3102
3025 POPSTACK; 3103 POPSTACK;
3026 SPAGAIN; 3104 SPAGAIN;
3027 FREETMPS;
3028 LEAVE;
3029 PUTBACK;
3030 }
3031 3105
3032 if (!(coro->flags & CF_RUNNING)) 3106 if (current != coro)
3033 { 3107 {
3108 PUTBACK;
3034 save_perl (aTHX_ coro); 3109 save_perl (aTHX_ coro);
3035 load_perl (aTHX_ &temp); 3110 load_perl (aTHX_ current);
3036 SPAGAIN; 3111 SPAGAIN;
3037 } 3112 }
3038 } 3113 }
3039} 3114}
3040 3115
3044 ALIAS: 3119 ALIAS:
3045 is_ready = CF_READY 3120 is_ready = CF_READY
3046 is_running = CF_RUNNING 3121 is_running = CF_RUNNING
3047 is_new = CF_NEW 3122 is_new = CF_NEW
3048 is_destroyed = CF_DESTROYED 3123 is_destroyed = CF_DESTROYED
3124 is_suspended = CF_SUSPENDED
3049 CODE: 3125 CODE:
3050 RETVAL = boolSV (coro->flags & ix); 3126 RETVAL = boolSV (coro->flags & ix);
3051 OUTPUT: 3127 OUTPUT:
3052 RETVAL 3128 RETVAL
3053 3129
3118 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3194 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3119 3195
3120 SV *tmp = *src; *src = *dst; *dst = tmp; 3196 SV *tmp = *src; *src = *dst; *dst = tmp;
3121 } 3197 }
3122 3198
3199void
3200cancel (Coro::State self)
3201 CODE:
3202 coro_state_destroy (aTHX_ self);
3203 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3204
3123 3205
3124MODULE = Coro::State PACKAGE = Coro 3206MODULE = Coro::State PACKAGE = Coro
3125 3207
3126BOOT: 3208BOOT:
3127{ 3209{
3201cede_notself (...) 3283cede_notself (...)
3202 CODE: 3284 CODE:
3203 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3285 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3204 3286
3205void 3287void
3206_cancel (Coro::State self)
3207 CODE:
3208 coro_state_destroy (aTHX_ self);
3209 coro_call_on_destroy (aTHX_ self);
3210
3211void
3212_set_current (SV *current) 3288_set_current (SV *current)
3213 PROTOTYPE: $ 3289 PROTOTYPE: $
3214 CODE: 3290 CODE:
3215 SvREFCNT_dec (SvRV (coro_current)); 3291 SvREFCNT_dec (SvRV (coro_current));
3216 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3292 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3322rouse_wait (...) 3398rouse_wait (...)
3323 PROTOTYPE: ;$ 3399 PROTOTYPE: ;$
3324 PPCODE: 3400 PPCODE:
3325 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3401 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3326 3402
3403void
3404on_enter (SV *block)
3405 ALIAS:
3406 on_leave = 1
3407 PROTOTYPE: &
3408 CODE:
3409{
3410 struct coro *coro = SvSTATE_current;
3411 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3412
3413 block = (SV *)coro_sv_2cv (aTHX_ block);
3414
3415 if (!*avp)
3416 *avp = newAV ();
3417
3418 av_push (*avp, SvREFCNT_inc (block));
3419
3420 if (!ix)
3421 on_enterleave_call (aTHX_ block);
3422
3423 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */
3424 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3425 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */
3426}
3427
3327 3428
3328MODULE = Coro::State PACKAGE = PerlIO::cede 3429MODULE = Coro::State PACKAGE = PerlIO::cede
3329 3430
3330BOOT: 3431BOOT:
3331 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3432 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3341 GvSTASH (CvGV (cv)) 3442 GvSTASH (CvGV (cv))
3342 ); 3443 );
3343 OUTPUT: 3444 OUTPUT:
3344 RETVAL 3445 RETVAL
3345 3446
3346# helper for Coro::Channel 3447# helper for Coro::Channel and others
3347SV * 3448SV *
3348_alloc (int count) 3449_alloc (int count)
3349 CODE: 3450 CODE:
3350 RETVAL = coro_waitarray_new (aTHX_ count); 3451 RETVAL = coro_waitarray_new (aTHX_ count);
3351 OUTPUT: 3452 OUTPUT:
3409 for (i = 1; i <= wcount; ++i) 3510 for (i = 1; i <= wcount; ++i)
3410 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3511 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3411 } 3512 }
3412} 3513}
3413 3514
3515MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3516
3517void
3518_may_delete (SV *sem, int count, int extra_refs)
3519 PPCODE:
3520{
3521 AV *av = (AV *)SvRV (sem);
3522
3523 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3524 && AvFILLp (av) == 0 /* no waiters, just count */
3525 && SvIV (AvARRAY (av)[0]) == count)
3526 XSRETURN_YES;
3527
3528 XSRETURN_NO;
3529}
3530
3414MODULE = Coro::State PACKAGE = Coro::Signal 3531MODULE = Coro::State PACKAGE = Coro::Signal
3415 3532
3416SV * 3533SV *
3417new (SV *klass) 3534new (SV *klass)
3418 CODE: 3535 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines