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.331 by root, Thu Nov 27 12:00:59 2008 UTC vs.
Revision 1.343 by root, Mon Dec 15 16:41:43 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}
369
370/*****************************************************************************/
371/* magic glue */
372
373#define CORO_MAGIC_type_cv 26
374#define CORO_MAGIC_type_state PERL_MAGIC_ext
375
376#define CORO_MAGIC_NN(sv, type) \
377 (expect_true (SvMAGIC (sv)->mg_type == type) \
378 ? SvMAGIC (sv) \
379 : mg_find (sv, type))
380
381#define CORO_MAGIC(sv, type) \
382 (expect_true (SvMAGIC (sv)) \
383 ? CORO_MAGIC_NN (sv, type) \
384 : 0)
385
386#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
387#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
388
389INLINE struct coro *
390SvSTATE_ (pTHX_ SV *coro)
391{
392 HV *stash;
393 MAGIC *mg;
394
395 if (SvROK (coro))
396 coro = SvRV (coro);
397
398 if (expect_false (SvTYPE (coro) != SVt_PVHV))
399 croak ("Coro::State object required");
400
401 stash = SvSTASH (coro);
402 if (expect_false (stash != coro_stash && stash != coro_state_stash))
403 {
404 /* very slow, but rare, check */
405 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
406 croak ("Coro::State object required");
407 }
408
409 mg = CORO_MAGIC_state (coro);
410 return (struct coro *)mg->mg_ptr;
411}
412
413#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
414
415/* faster than SvSTATE, but expects a coroutine hv */
416#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
417#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
418
419/*****************************************************************************/
420/* padlist management and caching */
359 421
360static AV * 422static AV *
361coro_derive_padlist (pTHX_ CV *cv) 423coro_derive_padlist (pTHX_ CV *cv)
362{ 424{
363 AV *padlist = CvPADLIST (cv); 425 AV *padlist = CvPADLIST (cv);
371 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 433 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
372#endif 434#endif
373 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 435 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
374 --AvFILLp (padlist); 436 --AvFILLp (padlist);
375 437
376 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 438 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
377 av_store (newpadlist, 1, (SV *)newpad); 439 av_store (newpadlist, 1, (SV *)newpad);
378 440
379 return newpadlist; 441 return newpadlist;
380} 442}
381 443
385 /* may be during global destruction */ 447 /* may be during global destruction */
386 if (!IN_DESTRUCT) 448 if (!IN_DESTRUCT)
387 { 449 {
388 I32 i = AvFILLp (padlist); 450 I32 i = AvFILLp (padlist);
389 451
390 while (i >= 0) 452 while (i > 0) /* special-case index 0 */
391 { 453 {
392 /* we try to be extra-careful here */ 454 /* we try to be extra-careful here */
393 AV *av = (AV *)AvARRAY (padlist)[i--]; 455 AV *av = (AV *)AvARRAY (padlist)[i--];
394
395 I32 i = AvFILLp (av); 456 I32 j = AvFILLp (av);
396 457
397 while (i >= 0) 458 while (j >= 0)
398 SvREFCNT_dec (AvARRAY (av)[i--]); 459 SvREFCNT_dec (AvARRAY (av)[j--]);
399 460
400 AvFILLp (av) = -1; 461 AvFILLp (av) = -1;
401 SvREFCNT_dec (av); 462 SvREFCNT_dec (av);
402 } 463 }
403 464
465 SvREFCNT_dec (AvARRAY (padlist)[0]);
466
404 AvFILLp (padlist) = -1; 467 AvFILLp (padlist) = -1;
405 SvREFCNT_dec ((SV*)padlist); 468 SvREFCNT_dec ((SV*)padlist);
406 } 469 }
407} 470}
408 471
418 481
419 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 482 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
420 483
421 return 0; 484 return 0;
422} 485}
423
424#define CORO_MAGIC_type_cv 26
425#define CORO_MAGIC_type_state PERL_MAGIC_ext
426 486
427static MGVTBL coro_cv_vtbl = { 487static MGVTBL coro_cv_vtbl = {
428 0, 0, 0, 0, 488 0, 0, 0, 0,
429 coro_cv_free 489 coro_cv_free
430}; 490};
431
432#define CORO_MAGIC_NN(sv, type) \
433 (expect_true (SvMAGIC (sv)->mg_type == type) \
434 ? SvMAGIC (sv) \
435 : mg_find (sv, type))
436
437#define CORO_MAGIC(sv, type) \
438 (expect_true (SvMAGIC (sv)) \
439 ? CORO_MAGIC_NN (sv, type) \
440 : 0)
441
442#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
443#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
444
445INLINE struct coro *
446SvSTATE_ (pTHX_ SV *coro)
447{
448 HV *stash;
449 MAGIC *mg;
450
451 if (SvROK (coro))
452 coro = SvRV (coro);
453
454 if (expect_false (SvTYPE (coro) != SVt_PVHV))
455 croak ("Coro::State object required");
456
457 stash = SvSTASH (coro);
458 if (expect_false (stash != coro_stash && stash != coro_state_stash))
459 {
460 /* very slow, but rare, check */
461 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
462 croak ("Coro::State object required");
463 }
464
465 mg = CORO_MAGIC_state (coro);
466 return (struct coro *)mg->mg_ptr;
467}
468
469#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
470
471/* faster than SvSTATE, but expects a coroutine hv */
472#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
473#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
474 491
475/* the next two functions merely cache the padlists */ 492/* the next two functions merely cache the padlists */
476static void 493static void
477get_padlist (pTHX_ CV *cv) 494get_padlist (pTHX_ CV *cv)
478{ 495{
514} 531}
515 532
516/** load & save, init *******************************************************/ 533/** load & save, init *******************************************************/
517 534
518static void 535static void
536on_enterleave_call (pTHX_ SV *cb);
537
538static void
519load_perl (pTHX_ Coro__State c) 539load_perl (pTHX_ Coro__State c)
520{ 540{
521 perl_slots *slot = c->slot; 541 perl_slots *slot = c->slot;
522 c->slot = 0; 542 c->slot = 0;
523 543
549 PUTBACK; 569 PUTBACK;
550 } 570 }
551 571
552 slf_frame = c->slf_frame; 572 slf_frame = c->slf_frame;
553 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 }
554} 582}
555 583
556static void 584static void
557save_perl (pTHX_ Coro__State c) 585save_perl (pTHX_ Coro__State c)
558{ 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
559 c->except = CORO_THROW; 595 c->except = CORO_THROW;
560 c->slf_frame = slf_frame; 596 c->slf_frame = slf_frame;
561 597
562 { 598 {
563 dSP; 599 dSP;
762#endif 798#endif
763 799
764/* 800/*
765 * This overrides the default magic get method of %SIG elements. 801 * This overrides the default magic get method of %SIG elements.
766 * 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
767 * 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
768 * readback here. 804 * readback here.
769 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
770 * not expecting this to actually change the hook. This is a potential problem
771 * when a schedule happens then, but we ignore this.
772 */ 805 */
773static int 806static int
774coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 807coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
775{ 808{
776 const char *s = MgPV_nolen_const (mg); 809 const char *s = MgPV_nolen_const (mg);
829 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 862 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
830 863
831 if (svp) 864 if (svp)
832 { 865 {
833 SV *old = *svp; 866 SV *old = *svp;
834 *svp = newSVsv (sv); 867 *svp = SvOK (sv) ? newSVsv (sv) : 0;
835 SvREFCNT_dec (old); 868 SvREFCNT_dec (old);
836 return 0; 869 return 0;
837 } 870 }
838 } 871 }
839 872
931 /* copy throw, in case it was set before coro_setup */ 964 /* copy throw, in case it was set before coro_setup */
932 CORO_THROW = coro->except; 965 CORO_THROW = coro->except;
933} 966}
934 967
935static void 968static void
936coro_destruct (pTHX_ struct coro *coro) 969coro_unwind_stacks (pTHX)
937{ 970{
938 if (!IN_DESTRUCT) 971 if (!IN_DESTRUCT)
939 { 972 {
940 /* restore all saved variables and stuff */ 973 /* restore all saved variables and stuff */
941 LEAVE_SCOPE (0); 974 LEAVE_SCOPE (0);
949 POPSTACK_TO (PL_mainstack); 982 POPSTACK_TO (PL_mainstack);
950 983
951 /* unwind main stack */ 984 /* unwind main stack */
952 dounwind (-1); 985 dounwind (-1);
953 } 986 }
987}
988
989static void
990coro_destruct_perl (pTHX_ struct coro *coro)
991{
992 coro_unwind_stacks (aTHX);
954 993
955 SvREFCNT_dec (GvSV (PL_defgv)); 994 SvREFCNT_dec (GvSV (PL_defgv));
956 SvREFCNT_dec (GvAV (PL_defgv)); 995 SvREFCNT_dec (GvAV (PL_defgv));
957 SvREFCNT_dec (GvSV (PL_errgv)); 996 SvREFCNT_dec (GvSV (PL_errgv));
958 SvREFCNT_dec (PL_defoutgv); 997 SvREFCNT_dec (PL_defoutgv);
1270cctx_destroy (coro_cctx *cctx) 1309cctx_destroy (coro_cctx *cctx)
1271{ 1310{
1272 if (!cctx) 1311 if (!cctx)
1273 return; 1312 return;
1274 1313
1275 assert (cctx != cctx_current);//D temporary 1314 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1276 1315
1277 --cctx_count; 1316 --cctx_count;
1278 coro_destroy (&cctx->cctx); 1317 coro_destroy (&cctx->cctx);
1279 1318
1280 /* coro_transfer creates new, empty cctx's */ 1319 /* coro_transfer creates new, empty cctx's */
1344 /* TODO: throwing up here is considered harmful */ 1383 /* TODO: throwing up here is considered harmful */
1345 1384
1346 if (expect_true (prev != next)) 1385 if (expect_true (prev != next))
1347 { 1386 {
1348 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1387 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1349 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,");
1350 1389
1351 if (expect_false (next->flags & CF_RUNNING))
1352 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1353
1354 if (expect_false (next->flags & CF_DESTROYED)) 1390 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1355 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,");
1356 1392
1357#if !PERL_VERSION_ATLEAST (5,10,0) 1393#if !PERL_VERSION_ATLEAST (5,10,0)
1358 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1394 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1359 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,");
1360#endif 1396#endif
1398 coro_setup (aTHX_ next); 1434 coro_setup (aTHX_ next);
1399 } 1435 }
1400 else 1436 else
1401 load_perl (aTHX_ next); 1437 load_perl (aTHX_ next);
1402 1438
1403 assert (!prev->cctx);//D temporary
1404
1405 /* possibly untie and reuse the cctx */ 1439 /* possibly untie and reuse the cctx */
1406 if (expect_true ( 1440 if (expect_true (
1407 cctx_current->idle_sp == STACKLEVEL 1441 cctx_current->idle_sp == STACKLEVEL
1408 && !(cctx_current->flags & CC_TRACE) 1442 && !(cctx_current->flags & CC_TRACE)
1409 && !force_cctx 1443 && !force_cctx
1464 --coro_nready; 1498 --coro_nready;
1465 } 1499 }
1466 else 1500 else
1467 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 */
1468 1502
1469 if (coro->mainstack && coro->mainstack != main_mainstack) 1503 if (coro->mainstack
1504 && coro->mainstack != main_mainstack
1505 && coro->slot
1506 && !PL_dirty)
1470 { 1507 {
1471 struct coro temp; 1508 struct coro *current = SvSTATE_current;
1472 1509
1473 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));
1474 1511
1475 save_perl (aTHX_ &temp); 1512 save_perl (aTHX_ current);
1476 load_perl (aTHX_ coro); 1513 load_perl (aTHX_ coro);
1477 1514
1478 coro_destruct (aTHX_ coro); 1515 coro_destruct_perl (aTHX_ coro);
1479 1516
1480 load_perl (aTHX_ &temp); 1517 load_perl (aTHX_ current);
1481 1518
1482 coro->slot = 0; 1519 coro->slot = 0;
1483 } 1520 }
1484 1521
1485 cctx_destroy (coro->cctx); 1522 cctx_destroy (coro->cctx);
1666 if (expect_true (next_sv)) 1703 if (expect_true (next_sv))
1667 { 1704 {
1668 struct coro *next = SvSTATE_hv (next_sv); 1705 struct coro *next = SvSTATE_hv (next_sv);
1669 1706
1670 /* cannot transfer to destroyed coros, skip and look for next */ 1707 /* cannot transfer to destroyed coros, skip and look for next */
1671 if (expect_false (next->flags & CF_DESTROYED)) 1708 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1672 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 */
1673 else 1710 else
1674 { 1711 {
1675 next->flags &= ~CF_READY; 1712 next->flags &= ~CF_READY;
1676 --coro_nready; 1713 --coro_nready;
1855 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 */
1856 api_ready (aTHX_ sv_manager); 1893 api_ready (aTHX_ sv_manager);
1857 1894
1858 frame->prepare = prepare_schedule; 1895 frame->prepare = prepare_schedule;
1859 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);*/
1860} 1901}
1861 1902
1862/*****************************************************************************/ 1903/*****************************************************************************/
1863/* async pool handler */ 1904/* async pool handler */
1864 1905
2283 2324
2284 PL_op->op_ppaddr = pp_slf; 2325 PL_op->op_ppaddr = pp_slf;
2285 /*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 */
2286 2327
2287 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));
2288} 2376}
2289 2377
2290/*****************************************************************************/ 2378/*****************************************************************************/
2291/* PerlIO::cede */ 2379/* PerlIO::cede */
2292 2380
2918SV * 3006SV *
2919clone (Coro::State coro) 3007clone (Coro::State coro)
2920 CODE: 3008 CODE:
2921{ 3009{
2922#if CORO_CLONE 3010#if CORO_CLONE
2923 struct coro *ncoro = coro_clone (coro); 3011 struct coro *ncoro = coro_clone (aTHX_ coro);
2924 MAGIC *mg; 3012 MAGIC *mg;
2925 /* TODO: too much duplication */ 3013 /* TODO: too much duplication */
2926 ncoro->hv = newHV (); 3014 ncoro->hv = newHV ();
2927 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); 3015 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2928 mg->mg_flags |= MGf_DUP; 3016 mg->mg_flags |= MGf_DUP;
2990 eval = 1 3078 eval = 1
2991 CODE: 3079 CODE:
2992{ 3080{
2993 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3081 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2994 { 3082 {
2995 struct coro temp; 3083 struct coro *current = SvSTATE_current;
2996 3084
2997 if (!(coro->flags & CF_RUNNING)) 3085 if (current != coro)
2998 { 3086 {
2999 PUTBACK; 3087 PUTBACK;
3000 save_perl (aTHX_ &temp); 3088 save_perl (aTHX_ current);
3001 load_perl (aTHX_ coro); 3089 load_perl (aTHX_ coro);
3090 SPAGAIN;
3002 } 3091 }
3003 3092
3004 {
3005 dSP;
3006 ENTER;
3007 SAVETMPS;
3008 PUTBACK;
3009 PUSHSTACK; 3093 PUSHSTACK;
3094
3010 PUSHMARK (SP); 3095 PUSHMARK (SP);
3096 PUTBACK;
3011 3097
3012 if (ix) 3098 if (ix)
3013 eval_sv (coderef, 0); 3099 eval_sv (coderef, 0);
3014 else 3100 else
3015 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3101 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
3016 3102
3017 POPSTACK; 3103 POPSTACK;
3018 SPAGAIN; 3104 SPAGAIN;
3019 FREETMPS;
3020 LEAVE;
3021 PUTBACK;
3022 }
3023 3105
3024 if (!(coro->flags & CF_RUNNING)) 3106 if (current != coro)
3025 { 3107 {
3108 PUTBACK;
3026 save_perl (aTHX_ coro); 3109 save_perl (aTHX_ coro);
3027 load_perl (aTHX_ &temp); 3110 load_perl (aTHX_ current);
3028 SPAGAIN; 3111 SPAGAIN;
3029 } 3112 }
3030 } 3113 }
3031} 3114}
3032 3115
3036 ALIAS: 3119 ALIAS:
3037 is_ready = CF_READY 3120 is_ready = CF_READY
3038 is_running = CF_RUNNING 3121 is_running = CF_RUNNING
3039 is_new = CF_NEW 3122 is_new = CF_NEW
3040 is_destroyed = CF_DESTROYED 3123 is_destroyed = CF_DESTROYED
3124 is_suspended = CF_SUSPENDED
3041 CODE: 3125 CODE:
3042 RETVAL = boolSV (coro->flags & ix); 3126 RETVAL = boolSV (coro->flags & ix);
3043 OUTPUT: 3127 OUTPUT:
3044 RETVAL 3128 RETVAL
3045 3129
3110 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3194 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3111 3195
3112 SV *tmp = *src; *src = *dst; *dst = tmp; 3196 SV *tmp = *src; *src = *dst; *dst = tmp;
3113 } 3197 }
3114 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
3115 3205
3116MODULE = Coro::State PACKAGE = Coro 3206MODULE = Coro::State PACKAGE = Coro
3117 3207
3118BOOT: 3208BOOT:
3119{ 3209{
3120 int i; 3210 int i;
3121 3211
3122 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3212 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3123 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3213 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3124 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3214 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3125 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3215 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3126 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3216 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3127 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3217 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3128 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3218 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3129 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3219 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3191 3281
3192void 3282void
3193cede_notself (...) 3283cede_notself (...)
3194 CODE: 3284 CODE:
3195 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3285 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3196
3197void
3198_cancel (Coro::State self)
3199 CODE:
3200 coro_state_destroy (aTHX_ self);
3201 coro_call_on_destroy (aTHX_ self);
3202 3286
3203void 3287void
3204_set_current (SV *current) 3288_set_current (SV *current)
3205 PROTOTYPE: $ 3289 PROTOTYPE: $
3206 CODE: 3290 CODE:
3314rouse_wait (...) 3398rouse_wait (...)
3315 PROTOTYPE: ;$ 3399 PROTOTYPE: ;$
3316 PPCODE: 3400 PPCODE:
3317 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3401 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3318 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
3319 3428
3320MODULE = Coro::State PACKAGE = PerlIO::cede 3429MODULE = Coro::State PACKAGE = PerlIO::cede
3321 3430
3322BOOT: 3431BOOT:
3323 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3432 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3333 GvSTASH (CvGV (cv)) 3442 GvSTASH (CvGV (cv))
3334 ); 3443 );
3335 OUTPUT: 3444 OUTPUT:
3336 RETVAL 3445 RETVAL
3337 3446
3338# helper for Coro::Channel 3447# helper for Coro::Channel and others
3339SV * 3448SV *
3340_alloc (int count) 3449_alloc (int count)
3341 CODE: 3450 CODE:
3342 RETVAL = coro_waitarray_new (aTHX_ count); 3451 RETVAL = coro_waitarray_new (aTHX_ count);
3343 OUTPUT: 3452 OUTPUT:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines