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.346 by root, Tue Jun 9 18:56:45 2009 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);
1277cctx_destroy (coro_cctx *cctx) 1309cctx_destroy (coro_cctx *cctx)
1278{ 1310{
1279 if (!cctx) 1311 if (!cctx)
1280 return; 1312 return;
1281 1313
1282 assert (cctx != cctx_current);//D temporary 1314 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1283 1315
1284 --cctx_count; 1316 --cctx_count;
1285 coro_destroy (&cctx->cctx); 1317 coro_destroy (&cctx->cctx);
1286 1318
1287 /* coro_transfer creates new, empty cctx's */ 1319 /* coro_transfer creates new, empty cctx's */
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
1405 coro_setup (aTHX_ next); 1434 coro_setup (aTHX_ next);
1406 } 1435 }
1407 else 1436 else
1408 load_perl (aTHX_ next); 1437 load_perl (aTHX_ next);
1409 1438
1410 assert (!prev->cctx);//D temporary
1411
1412 /* possibly untie and reuse the cctx */ 1439 /* possibly untie and reuse the cctx */
1413 if (expect_true ( 1440 if (expect_true (
1414 cctx_current->idle_sp == STACKLEVEL 1441 cctx_current->idle_sp == STACKLEVEL
1415 && !(cctx_current->flags & CC_TRACE) 1442 && !(cctx_current->flags & CC_TRACE)
1416 && !force_cctx 1443 && !force_cctx
1457coro_state_destroy (pTHX_ struct coro *coro) 1484coro_state_destroy (pTHX_ struct coro *coro)
1458{ 1485{
1459 if (coro->flags & CF_DESTROYED) 1486 if (coro->flags & CF_DESTROYED)
1460 return 0; 1487 return 0;
1461 1488
1462 if (coro->on_destroy) 1489 if (coro->on_destroy && !PL_dirty)
1463 coro->on_destroy (aTHX_ coro); 1490 coro->on_destroy (aTHX_ coro);
1464 1491
1465 coro->flags |= CF_DESTROYED; 1492 coro->flags |= CF_DESTROYED;
1466 1493
1467 if (coro->flags & CF_READY) 1494 if (coro->flags & CF_READY)
1473 else 1500 else
1474 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1501 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1475 1502
1476 if (coro->mainstack 1503 if (coro->mainstack
1477 && coro->mainstack != main_mainstack 1504 && coro->mainstack != main_mainstack
1505 && coro->slot
1478 && !PL_dirty) 1506 && !PL_dirty)
1479 { 1507 {
1480 struct coro temp; 1508 struct coro *current = SvSTATE_current;
1481 1509
1482 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); 1510 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1483 1511
1484 save_perl (aTHX_ &temp); 1512 save_perl (aTHX_ current);
1485 load_perl (aTHX_ coro); 1513 load_perl (aTHX_ coro);
1486 1514
1487 coro_destruct_perl (aTHX_ coro); 1515 coro_destruct_perl (aTHX_ coro);
1488 1516
1489 load_perl (aTHX_ &temp); 1517 load_perl (aTHX_ current);
1490 1518
1491 coro->slot = 0; 1519 coro->slot = 0;
1492 } 1520 }
1493 1521
1494 cctx_destroy (coro->cctx); 1522 cctx_destroy (coro->cctx);
1675 if (expect_true (next_sv)) 1703 if (expect_true (next_sv))
1676 { 1704 {
1677 struct coro *next = SvSTATE_hv (next_sv); 1705 struct coro *next = SvSTATE_hv (next_sv);
1678 1706
1679 /* cannot transfer to destroyed coros, skip and look for next */ 1707 /* cannot transfer to destroyed coros, skip and look for next */
1680 if (expect_false (next->flags & CF_DESTROYED)) 1708 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1681 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 */
1682 else 1710 else
1683 { 1711 {
1684 next->flags &= ~CF_READY; 1712 next->flags &= ~CF_READY;
1685 --coro_nready; 1713 --coro_nready;
1864 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 */
1865 api_ready (aTHX_ sv_manager); 1893 api_ready (aTHX_ sv_manager);
1866 1894
1867 frame->prepare = prepare_schedule; 1895 frame->prepare = prepare_schedule;
1868 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);*/
1869} 1901}
1870 1902
1871/*****************************************************************************/ 1903/*****************************************************************************/
1872/* async pool handler */ 1904/* async pool handler */
1873 1905
1956 SV *data = (SV *)GENSUB_ARG; 1988 SV *data = (SV *)GENSUB_ARG;
1957 1989
1958 if (SvTYPE (SvRV (data)) != SVt_PVAV) 1990 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1959 { 1991 {
1960 /* first call, set args */ 1992 /* first call, set args */
1993 SV *coro = SvRV (data);
1961 AV *av = newAV (); 1994 AV *av = newAV ();
1962 SV *coro = SvRV (data);
1963 1995
1964 SvRV_set (data, (SV *)av); 1996 SvRV_set (data, (SV *)av);
1965 api_ready (aTHX_ coro);
1966 SvREFCNT_dec (coro);
1967 1997
1968 /* better take a full copy of the arguments */ 1998 /* better take a full copy of the arguments */
1969 while (items--) 1999 while (items--)
1970 av_store (av, items, newSVsv (ST (items))); 2000 av_store (av, items, newSVsv (ST (items)));
2001
2002 api_ready (aTHX_ coro);
2003 SvREFCNT_dec (coro);
1971 } 2004 }
1972 2005
1973 XSRETURN_EMPTY; 2006 XSRETURN_EMPTY;
1974} 2007}
1975 2008
1992 2025
1993 EXTEND (SP, AvFILLp (av) + 1); 2026 EXTEND (SP, AvFILLp (av) + 1);
1994 for (i = 0; i <= AvFILLp (av); ++i) 2027 for (i = 0; i <= AvFILLp (av); ++i)
1995 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2028 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1996 2029
1997 /* we have stolen the elements, so ste length to zero and free */ 2030 /* we have stolen the elements, so set length to zero and free */
1998 AvFILLp (av) = -1; 2031 AvFILLp (av) = -1;
1999 av_undef (av); 2032 av_undef (av);
2000 2033
2001 PUTBACK; 2034 PUTBACK;
2002 } 2035 }
2292 2325
2293 PL_op->op_ppaddr = pp_slf; 2326 PL_op->op_ppaddr = pp_slf;
2294 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2327 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2295 2328
2296 PL_op = (OP *)&slf_restore; 2329 PL_op = (OP *)&slf_restore;
2330}
2331
2332/*****************************************************************************/
2333/* dynamic wind */
2334
2335static void
2336on_enterleave_call (pTHX_ SV *cb)
2337{
2338 dSP;
2339
2340 PUSHSTACK;
2341
2342 PUSHMARK (SP);
2343 PUTBACK;
2344 call_sv (cb, G_VOID | G_DISCARD);
2345 SPAGAIN;
2346
2347 POPSTACK;
2348}
2349
2350static SV *
2351coro_avp_pop_and_free (pTHX_ AV **avp)
2352{
2353 AV *av = *avp;
2354 SV *res = av_pop (av);
2355
2356 if (AvFILLp (av) < 0)
2357 {
2358 *avp = 0;
2359 SvREFCNT_dec (av);
2360 }
2361
2362 return res;
2363}
2364
2365static void
2366coro_pop_on_enter (pTHX_ void *coro)
2367{
2368 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2369 SvREFCNT_dec (cb);
2370}
2371
2372static void
2373coro_pop_on_leave (pTHX_ void *coro)
2374{
2375 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2376 on_enterleave_call (aTHX_ sv_2mortal (cb));
2297} 2377}
2298 2378
2299/*****************************************************************************/ 2379/*****************************************************************************/
2300/* PerlIO::cede */ 2380/* PerlIO::cede */
2301 2381
2999 eval = 1 3079 eval = 1
3000 CODE: 3080 CODE:
3001{ 3081{
3002 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3082 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
3003 { 3083 {
3004 struct coro temp; 3084 struct coro *current = SvSTATE_current;
3005 3085
3006 if (!(coro->flags & CF_RUNNING)) 3086 if (current != coro)
3007 { 3087 {
3008 PUTBACK; 3088 PUTBACK;
3009 save_perl (aTHX_ &temp); 3089 save_perl (aTHX_ current);
3010 load_perl (aTHX_ coro); 3090 load_perl (aTHX_ coro);
3091 SPAGAIN;
3011 } 3092 }
3012 3093
3013 {
3014 dSP;
3015 ENTER;
3016 SAVETMPS;
3017 PUTBACK;
3018 PUSHSTACK; 3094 PUSHSTACK;
3095
3019 PUSHMARK (SP); 3096 PUSHMARK (SP);
3097 PUTBACK;
3020 3098
3021 if (ix) 3099 if (ix)
3022 eval_sv (coderef, 0); 3100 eval_sv (coderef, 0);
3023 else 3101 else
3024 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3102 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
3025 3103
3026 POPSTACK; 3104 POPSTACK;
3027 SPAGAIN; 3105 SPAGAIN;
3028 FREETMPS;
3029 LEAVE;
3030 PUTBACK;
3031 }
3032 3106
3033 if (!(coro->flags & CF_RUNNING)) 3107 if (current != coro)
3034 { 3108 {
3109 PUTBACK;
3035 save_perl (aTHX_ coro); 3110 save_perl (aTHX_ coro);
3036 load_perl (aTHX_ &temp); 3111 load_perl (aTHX_ current);
3037 SPAGAIN; 3112 SPAGAIN;
3038 } 3113 }
3039 } 3114 }
3040} 3115}
3041 3116
3045 ALIAS: 3120 ALIAS:
3046 is_ready = CF_READY 3121 is_ready = CF_READY
3047 is_running = CF_RUNNING 3122 is_running = CF_RUNNING
3048 is_new = CF_NEW 3123 is_new = CF_NEW
3049 is_destroyed = CF_DESTROYED 3124 is_destroyed = CF_DESTROYED
3125 is_suspended = CF_SUSPENDED
3050 CODE: 3126 CODE:
3051 RETVAL = boolSV (coro->flags & ix); 3127 RETVAL = boolSV (coro->flags & ix);
3052 OUTPUT: 3128 OUTPUT:
3053 RETVAL 3129 RETVAL
3054 3130
3119 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3195 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3120 3196
3121 SV *tmp = *src; *src = *dst; *dst = tmp; 3197 SV *tmp = *src; *src = *dst; *dst = tmp;
3122 } 3198 }
3123 3199
3200void
3201cancel (Coro::State self)
3202 CODE:
3203 coro_state_destroy (aTHX_ self);
3204 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3205
3124 3206
3125MODULE = Coro::State PACKAGE = Coro 3207MODULE = Coro::State PACKAGE = Coro
3126 3208
3127BOOT: 3209BOOT:
3128{ 3210{
3129 int i; 3211 int i;
3130 3212
3131 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3213 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3132 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3214 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3133 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3215 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3134 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3216 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3135 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3217 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); 3218 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3137 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3219 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3138 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3220 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3202cede_notself (...) 3284cede_notself (...)
3203 CODE: 3285 CODE:
3204 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3286 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3205 3287
3206void 3288void
3207_cancel (Coro::State self)
3208 CODE:
3209 coro_state_destroy (aTHX_ self);
3210 coro_call_on_destroy (aTHX_ self);
3211
3212void
3213_set_current (SV *current) 3289_set_current (SV *current)
3214 PROTOTYPE: $ 3290 PROTOTYPE: $
3215 CODE: 3291 CODE:
3216 SvREFCNT_dec (SvRV (coro_current)); 3292 SvREFCNT_dec (SvRV (coro_current));
3217 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3293 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3259 PROTOTYPE: 3335 PROTOTYPE:
3260 CODE: 3336 CODE:
3261 RETVAL = coro_nready; 3337 RETVAL = coro_nready;
3262 OUTPUT: 3338 OUTPUT:
3263 RETVAL 3339 RETVAL
3340
3341void
3342suspend (Coro::State self)
3343 PROTOTYPE: $
3344 CODE:
3345 self->flags |= CF_SUSPENDED;
3346
3347void
3348resume (Coro::State self)
3349 PROTOTYPE: $
3350 CODE:
3351 self->flags &= ~CF_SUSPENDED;
3264 3352
3265void 3353void
3266_pool_handler (...) 3354_pool_handler (...)
3267 CODE: 3355 CODE:
3268 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3356 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3323rouse_wait (...) 3411rouse_wait (...)
3324 PROTOTYPE: ;$ 3412 PROTOTYPE: ;$
3325 PPCODE: 3413 PPCODE:
3326 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3414 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3327 3415
3416void
3417on_enter (SV *block)
3418 ALIAS:
3419 on_leave = 1
3420 PROTOTYPE: &
3421 CODE:
3422{
3423 struct coro *coro = SvSTATE_current;
3424 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3425
3426 block = (SV *)coro_sv_2cv (aTHX_ block);
3427
3428 if (!*avp)
3429 *avp = newAV ();
3430
3431 av_push (*avp, SvREFCNT_inc (block));
3432
3433 if (!ix)
3434 on_enterleave_call (aTHX_ block);
3435
3436 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3437 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3438 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3439}
3440
3328 3441
3329MODULE = Coro::State PACKAGE = PerlIO::cede 3442MODULE = Coro::State PACKAGE = PerlIO::cede
3330 3443
3331BOOT: 3444BOOT:
3332 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3445 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3342 GvSTASH (CvGV (cv)) 3455 GvSTASH (CvGV (cv))
3343 ); 3456 );
3344 OUTPUT: 3457 OUTPUT:
3345 RETVAL 3458 RETVAL
3346 3459
3347# helper for Coro::Channel 3460# helper for Coro::Channel and others
3348SV * 3461SV *
3349_alloc (int count) 3462_alloc (int count)
3350 CODE: 3463 CODE:
3351 RETVAL = coro_waitarray_new (aTHX_ count); 3464 RETVAL = coro_waitarray_new (aTHX_ count);
3352 OUTPUT: 3465 OUTPUT:
3410 for (i = 1; i <= wcount; ++i) 3523 for (i = 1; i <= wcount; ++i)
3411 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3524 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3412 } 3525 }
3413} 3526}
3414 3527
3528MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3529
3530void
3531_may_delete (SV *sem, int count, int extra_refs)
3532 PPCODE:
3533{
3534 AV *av = (AV *)SvRV (sem);
3535
3536 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3537 && AvFILLp (av) == 0 /* no waiters, just count */
3538 && SvIV (AvARRAY (av)[0]) == count)
3539 XSRETURN_YES;
3540
3541 XSRETURN_NO;
3542}
3543
3415MODULE = Coro::State PACKAGE = Coro::Signal 3544MODULE = Coro::State PACKAGE = Coro::Signal
3416 3545
3417SV * 3546SV *
3418new (SV *klass) 3547new (SV *klass)
3419 CODE: 3548 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines