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.352 by root, Sat Jun 20 08:58:50 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{
260/* this is a structure representing a perl-level coroutine */ 261/* this is a structure representing a perl-level coroutine */
261struct coro { 262struct coro {
262 /* the C coroutine allocated to this perl coroutine, if any */ 263 /* the C coroutine allocated to this perl coroutine, if any */
263 coro_cctx *cctx; 264 coro_cctx *cctx;
264 265
266 /* ready queue */
267 struct coro *next_ready;
268
265 /* state data */ 269 /* state data */
266 struct CoroSLF slf_frame; /* saved slf frame */ 270 struct CoroSLF slf_frame; /* saved slf frame */
267 AV *mainstack; 271 AV *mainstack;
268 perl_slots *slot; /* basically the saved sp */ 272 perl_slots *slot; /* basically the saved sp */
269 273
285 /* async_pool */ 289 /* async_pool */
286 SV *saved_deffh; 290 SV *saved_deffh;
287 SV *invoke_cb; 291 SV *invoke_cb;
288 AV *invoke_av; 292 AV *invoke_av;
289 293
294 /* on_enter/on_leave */
295 AV *on_enter;
296 AV *on_leave;
297
290 /* linked list */ 298 /* linked list */
291 struct coro *next, *prev; 299 struct coro *next, *prev;
292}; 300};
293 301
294typedef struct coro *Coro__State; 302typedef struct coro *Coro__State;
309#define PRIO_MIN -4 317#define PRIO_MIN -4
310 318
311/* for Coro.pm */ 319/* for Coro.pm */
312static SV *coro_current; 320static SV *coro_current;
313static SV *coro_readyhook; 321static SV *coro_readyhook;
314static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 322static struct coro *coro_ready [PRIO_MAX - PRIO_MIN + 1][2]; /* head|tail */
315static CV *cv_coro_run, *cv_coro_terminate; 323static CV *cv_coro_run, *cv_coro_terminate;
316static struct coro *coro_first; 324static struct coro *coro_first;
317#define coro_nready coroapi.nready 325#define coro_nready coroapi.nready
318 326
319/** lowlevel stuff **********************************************************/ 327/** lowlevel stuff **********************************************************/
352INLINE CV * 360INLINE CV *
353coro_sv_2cv (pTHX_ SV *sv) 361coro_sv_2cv (pTHX_ SV *sv)
354{ 362{
355 HV *st; 363 HV *st;
356 GV *gvp; 364 GV *gvp;
357 return sv_2cv (sv, &st, &gvp, 0); 365 CV *cv = sv_2cv (sv, &st, &gvp, 0);
366
367 if (!cv)
368 croak ("code reference expected");
369
370 return cv;
358} 371}
372
373/*****************************************************************************/
374/* magic glue */
375
376#define CORO_MAGIC_type_cv 26
377#define CORO_MAGIC_type_state PERL_MAGIC_ext
378
379#define CORO_MAGIC_NN(sv, type) \
380 (expect_true (SvMAGIC (sv)->mg_type == type) \
381 ? SvMAGIC (sv) \
382 : mg_find (sv, type))
383
384#define CORO_MAGIC(sv, type) \
385 (expect_true (SvMAGIC (sv)) \
386 ? CORO_MAGIC_NN (sv, type) \
387 : 0)
388
389#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
390#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
391
392INLINE struct coro *
393SvSTATE_ (pTHX_ SV *coro)
394{
395 HV *stash;
396 MAGIC *mg;
397
398 if (SvROK (coro))
399 coro = SvRV (coro);
400
401 if (expect_false (SvTYPE (coro) != SVt_PVHV))
402 croak ("Coro::State object required");
403
404 stash = SvSTASH (coro);
405 if (expect_false (stash != coro_stash && stash != coro_state_stash))
406 {
407 /* very slow, but rare, check */
408 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
409 croak ("Coro::State object required");
410 }
411
412 mg = CORO_MAGIC_state (coro);
413 return (struct coro *)mg->mg_ptr;
414}
415
416#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
417
418/* faster than SvSTATE, but expects a coroutine hv */
419#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
420#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
421
422/*****************************************************************************/
423/* padlist management and caching */
359 424
360static AV * 425static AV *
361coro_derive_padlist (pTHX_ CV *cv) 426coro_derive_padlist (pTHX_ CV *cv)
362{ 427{
363 AV *padlist = CvPADLIST (cv); 428 AV *padlist = CvPADLIST (cv);
371 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 436 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
372#endif 437#endif
373 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 438 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
374 --AvFILLp (padlist); 439 --AvFILLp (padlist);
375 440
376 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 441 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
377 av_store (newpadlist, 1, (SV *)newpad); 442 av_store (newpadlist, 1, (SV *)newpad);
378 443
379 return newpadlist; 444 return newpadlist;
380} 445}
381 446
385 /* may be during global destruction */ 450 /* may be during global destruction */
386 if (!IN_DESTRUCT) 451 if (!IN_DESTRUCT)
387 { 452 {
388 I32 i = AvFILLp (padlist); 453 I32 i = AvFILLp (padlist);
389 454
390 while (i >= 0) 455 while (i > 0) /* special-case index 0 */
391 { 456 {
392 /* we try to be extra-careful here */ 457 /* we try to be extra-careful here */
393 AV *av = (AV *)AvARRAY (padlist)[i--]; 458 AV *av = (AV *)AvARRAY (padlist)[i--];
394
395 I32 i = AvFILLp (av); 459 I32 j = AvFILLp (av);
396 460
397 while (i >= 0) 461 while (j >= 0)
398 SvREFCNT_dec (AvARRAY (av)[i--]); 462 SvREFCNT_dec (AvARRAY (av)[j--]);
399 463
400 AvFILLp (av) = -1; 464 AvFILLp (av) = -1;
401 SvREFCNT_dec (av); 465 SvREFCNT_dec (av);
402 } 466 }
403 467
468 SvREFCNT_dec (AvARRAY (padlist)[0]);
469
404 AvFILLp (padlist) = -1; 470 AvFILLp (padlist) = -1;
405 SvREFCNT_dec ((SV*)padlist); 471 SvREFCNT_dec ((SV*)padlist);
406 } 472 }
407} 473}
408 474
418 484
419 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 485 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
420 486
421 return 0; 487 return 0;
422} 488}
423
424#define CORO_MAGIC_type_cv 26
425#define CORO_MAGIC_type_state PERL_MAGIC_ext
426 489
427static MGVTBL coro_cv_vtbl = { 490static MGVTBL coro_cv_vtbl = {
428 0, 0, 0, 0, 491 0, 0, 0, 0,
429 coro_cv_free 492 coro_cv_free
430}; 493};
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 494
475/* the next two functions merely cache the padlists */ 495/* the next two functions merely cache the padlists */
476static void 496static void
477get_padlist (pTHX_ CV *cv) 497get_padlist (pTHX_ CV *cv)
478{ 498{
514} 534}
515 535
516/** load & save, init *******************************************************/ 536/** load & save, init *******************************************************/
517 537
518static void 538static void
539on_enterleave_call (pTHX_ SV *cb);
540
541static void
519load_perl (pTHX_ Coro__State c) 542load_perl (pTHX_ Coro__State c)
520{ 543{
521 perl_slots *slot = c->slot; 544 perl_slots *slot = c->slot;
522 c->slot = 0; 545 c->slot = 0;
523 546
549 PUTBACK; 572 PUTBACK;
550 } 573 }
551 574
552 slf_frame = c->slf_frame; 575 slf_frame = c->slf_frame;
553 CORO_THROW = c->except; 576 CORO_THROW = c->except;
577
578 if (expect_false (c->on_enter))
579 {
580 int i;
581
582 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
583 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
584 }
554} 585}
555 586
556static void 587static void
557save_perl (pTHX_ Coro__State c) 588save_perl (pTHX_ Coro__State c)
558{ 589{
590 if (expect_false (c->on_leave))
591 {
592 int i;
593
594 for (i = AvFILLp (c->on_leave); i >= 0; --i)
595 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
596 }
597
559 c->except = CORO_THROW; 598 c->except = CORO_THROW;
560 c->slf_frame = slf_frame; 599 c->slf_frame = slf_frame;
561 600
562 { 601 {
563 dSP; 602 dSP;
762#endif 801#endif
763 802
764/* 803/*
765 * This overrides the default magic get method of %SIG elements. 804 * 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 805 * 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 806 * and instead of trying to save and restore the hash elements, we just provide
768 * readback here. 807 * 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 */ 808 */
773static int 809static int
774coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 810coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
775{ 811{
776 const char *s = MgPV_nolen_const (mg); 812 const char *s = MgPV_nolen_const (mg);
829 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 865 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
830 866
831 if (svp) 867 if (svp)
832 { 868 {
833 SV *old = *svp; 869 SV *old = *svp;
834 *svp = newSVsv (sv); 870 *svp = SvOK (sv) ? newSVsv (sv) : 0;
835 SvREFCNT_dec (old); 871 SvREFCNT_dec (old);
836 return 0; 872 return 0;
837 } 873 }
838 } 874 }
839 875
931 /* copy throw, in case it was set before coro_setup */ 967 /* copy throw, in case it was set before coro_setup */
932 CORO_THROW = coro->except; 968 CORO_THROW = coro->except;
933} 969}
934 970
935static void 971static void
936coro_destruct (pTHX_ struct coro *coro) 972coro_unwind_stacks (pTHX)
937{ 973{
938 if (!IN_DESTRUCT) 974 if (!IN_DESTRUCT)
939 { 975 {
940 /* restore all saved variables and stuff */ 976 /* restore all saved variables and stuff */
941 LEAVE_SCOPE (0); 977 LEAVE_SCOPE (0);
949 POPSTACK_TO (PL_mainstack); 985 POPSTACK_TO (PL_mainstack);
950 986
951 /* unwind main stack */ 987 /* unwind main stack */
952 dounwind (-1); 988 dounwind (-1);
953 } 989 }
990}
954 991
955 SvREFCNT_dec (GvSV (PL_defgv)); 992static void
956 SvREFCNT_dec (GvAV (PL_defgv)); 993coro_destruct_perl (pTHX_ struct coro *coro)
957 SvREFCNT_dec (GvSV (PL_errgv)); 994{
958 SvREFCNT_dec (PL_defoutgv); 995 SV *svf [9];
959 SvREFCNT_dec (PL_rs);
960 SvREFCNT_dec (GvSV (irsgv));
961 SvREFCNT_dec (GvHV (PL_hintgv));
962 996
963 SvREFCNT_dec (PL_diehook);
964 SvREFCNT_dec (PL_warnhook);
965 997 {
998 struct coro *current = SvSTATE_current;
999
1000 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1001
1002 save_perl (aTHX_ current);
1003 load_perl (aTHX_ coro);
1004
1005 coro_unwind_stacks (aTHX);
1006 coro_destruct_stacks (aTHX);
1007
1008 // now save some sv's to be free'd later
1009 svf [0] = GvSV (PL_defgv);
1010 svf [1] = (SV *)GvAV (PL_defgv);
1011 svf [2] = GvSV (PL_errgv);
1012 svf [3] = (SV *)PL_defoutgv;
1013 svf [4] = PL_rs;
1014 svf [5] = GvSV (irsgv);
1015 svf [6] = (SV *)GvHV (PL_hintgv);
1016 svf [7] = PL_diehook;
1017 svf [8] = PL_warnhook;
1018 assert (9 == sizeof (svf) / sizeof (*svf));
1019
1020 load_perl (aTHX_ current);
1021 }
1022
1023 {
1024 int i;
1025
1026 for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i)
1027 SvREFCNT_dec (svf [i]);
1028
966 SvREFCNT_dec (coro->saved_deffh); 1029 SvREFCNT_dec (coro->saved_deffh);
967 SvREFCNT_dec (coro->rouse_cb); 1030 SvREFCNT_dec (coro->rouse_cb);
968 SvREFCNT_dec (coro->invoke_cb); 1031 SvREFCNT_dec (coro->invoke_cb);
969 SvREFCNT_dec (coro->invoke_av); 1032 SvREFCNT_dec (coro->invoke_av);
970 1033 }
971 coro_destruct_stacks (aTHX);
972} 1034}
973 1035
974INLINE void 1036INLINE void
975free_coro_mortal (pTHX) 1037free_coro_mortal (pTHX)
976{ 1038{
1270cctx_destroy (coro_cctx *cctx) 1332cctx_destroy (coro_cctx *cctx)
1271{ 1333{
1272 if (!cctx) 1334 if (!cctx)
1273 return; 1335 return;
1274 1336
1275 assert (cctx != cctx_current);//D temporary 1337 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1276 1338
1277 --cctx_count; 1339 --cctx_count;
1278 coro_destroy (&cctx->cctx); 1340 coro_destroy (&cctx->cctx);
1279 1341
1280 /* coro_transfer creates new, empty cctx's */ 1342 /* coro_transfer creates new, empty cctx's */
1344 /* TODO: throwing up here is considered harmful */ 1406 /* TODO: throwing up here is considered harmful */
1345 1407
1346 if (expect_true (prev != next)) 1408 if (expect_true (prev != next))
1347 { 1409 {
1348 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1410 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,"); 1411 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1350 1412
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)) 1413 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,"); 1414 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1356 1415
1357#if !PERL_VERSION_ATLEAST (5,10,0) 1416#if !PERL_VERSION_ATLEAST (5,10,0)
1358 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1417 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,"); 1418 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1360#endif 1419#endif
1398 coro_setup (aTHX_ next); 1457 coro_setup (aTHX_ next);
1399 } 1458 }
1400 else 1459 else
1401 load_perl (aTHX_ next); 1460 load_perl (aTHX_ next);
1402 1461
1403 assert (!prev->cctx);//D temporary
1404
1405 /* possibly untie and reuse the cctx */ 1462 /* possibly untie and reuse the cctx */
1406 if (expect_true ( 1463 if (expect_true (
1407 cctx_current->idle_sp == STACKLEVEL 1464 cctx_current->idle_sp == STACKLEVEL
1408 && !(cctx_current->flags & CC_TRACE) 1465 && !(cctx_current->flags & CC_TRACE)
1409 && !force_cctx 1466 && !force_cctx
1450coro_state_destroy (pTHX_ struct coro *coro) 1507coro_state_destroy (pTHX_ struct coro *coro)
1451{ 1508{
1452 if (coro->flags & CF_DESTROYED) 1509 if (coro->flags & CF_DESTROYED)
1453 return 0; 1510 return 0;
1454 1511
1455 if (coro->on_destroy) 1512 if (coro->on_destroy && !PL_dirty)
1456 coro->on_destroy (aTHX_ coro); 1513 coro->on_destroy (aTHX_ coro);
1457 1514
1458 coro->flags |= CF_DESTROYED; 1515 coro->flags |= CF_DESTROYED;
1459 1516
1460 if (coro->flags & CF_READY) 1517 if (coro->flags & CF_READY)
1464 --coro_nready; 1521 --coro_nready;
1465 } 1522 }
1466 else 1523 else
1467 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1524 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1468 1525
1469 if (coro->mainstack && coro->mainstack != main_mainstack) 1526 if (coro->mainstack
1470 { 1527 && coro->mainstack != main_mainstack
1471 struct coro temp; 1528 && coro->slot
1472 1529 && !PL_dirty)
1473 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING)));
1474
1475 save_perl (aTHX_ &temp);
1476 load_perl (aTHX_ coro);
1477
1478 coro_destruct (aTHX_ coro); 1530 coro_destruct_perl (aTHX_ coro);
1479
1480 load_perl (aTHX_ &temp);
1481
1482 coro->slot = 0;
1483 }
1484 1531
1485 cctx_destroy (coro->cctx); 1532 cctx_destroy (coro->cctx);
1486 SvREFCNT_dec (coro->startcv); 1533 SvREFCNT_dec (coro->startcv);
1487 SvREFCNT_dec (coro->args); 1534 SvREFCNT_dec (coro->args);
1488 SvREFCNT_dec (CORO_THROW); 1535 SvREFCNT_dec (CORO_THROW);
1575/** Coro ********************************************************************/ 1622/** Coro ********************************************************************/
1576 1623
1577INLINE void 1624INLINE void
1578coro_enq (pTHX_ struct coro *coro) 1625coro_enq (pTHX_ struct coro *coro)
1579{ 1626{
1580 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); 1627 struct coro **ready = coro_ready [coro->prio - PRIO_MIN];
1581}
1582 1628
1583INLINE SV * 1629 SvREFCNT_inc_NN (coro->hv);
1630
1631 coro->next_ready = 0;
1632 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1633 ready [1] = coro;
1634}
1635
1636INLINE struct coro *
1584coro_deq (pTHX) 1637coro_deq (pTHX)
1585{ 1638{
1586 int prio; 1639 int prio;
1587 1640
1588 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1641 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1589 if (AvFILLp (coro_ready [prio]) >= 0) 1642 {
1590 return av_shift (coro_ready [prio]); 1643 struct coro **ready = coro_ready [prio];
1644
1645 if (ready [0])
1646 {
1647 struct coro *coro = ready [0];
1648 ready [0] = coro->next_ready;
1649 return coro;
1650 }
1651 }
1591 1652
1592 return 0; 1653 return 0;
1593} 1654}
1594 1655
1595static int 1656static int
1659static void 1720static void
1660prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1721prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1661{ 1722{
1662 for (;;) 1723 for (;;)
1663 { 1724 {
1664 SV *next_sv = coro_deq (aTHX); 1725 struct coro *next = coro_deq (aTHX);
1665 1726
1666 if (expect_true (next_sv)) 1727 if (expect_true (next))
1667 { 1728 {
1668 struct coro *next = SvSTATE_hv (next_sv);
1669
1670 /* cannot transfer to destroyed coros, skip and look for next */ 1729 /* cannot transfer to destroyed coros, skip and look for next */
1671 if (expect_false (next->flags & CF_DESTROYED)) 1730 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1672 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1731 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1673 else 1732 else
1674 { 1733 {
1675 next->flags &= ~CF_READY; 1734 next->flags &= ~CF_READY;
1676 --coro_nready; 1735 --coro_nready;
1677 1736
1855 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 1914 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1856 api_ready (aTHX_ sv_manager); 1915 api_ready (aTHX_ sv_manager);
1857 1916
1858 frame->prepare = prepare_schedule; 1917 frame->prepare = prepare_schedule;
1859 frame->check = slf_check_repeat; 1918 frame->check = slf_check_repeat;
1919
1920 /* as a minor optimisation, we could unwind all stacks here */
1921 /* but that puts extra pressure on pp_slf, and is not worth much */
1922 /*coro_unwind_stacks (aTHX);*/
1860} 1923}
1861 1924
1862/*****************************************************************************/ 1925/*****************************************************************************/
1863/* async pool handler */ 1926/* async pool handler */
1864 1927
1947 SV *data = (SV *)GENSUB_ARG; 2010 SV *data = (SV *)GENSUB_ARG;
1948 2011
1949 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2012 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1950 { 2013 {
1951 /* first call, set args */ 2014 /* first call, set args */
2015 SV *coro = SvRV (data);
1952 AV *av = newAV (); 2016 AV *av = newAV ();
1953 SV *coro = SvRV (data);
1954 2017
1955 SvRV_set (data, (SV *)av); 2018 SvRV_set (data, (SV *)av);
1956 api_ready (aTHX_ coro);
1957 SvREFCNT_dec (coro);
1958 2019
1959 /* better take a full copy of the arguments */ 2020 /* better take a full copy of the arguments */
1960 while (items--) 2021 while (items--)
1961 av_store (av, items, newSVsv (ST (items))); 2022 av_store (av, items, newSVsv (ST (items)));
2023
2024 api_ready (aTHX_ coro);
2025 SvREFCNT_dec (coro);
1962 } 2026 }
1963 2027
1964 XSRETURN_EMPTY; 2028 XSRETURN_EMPTY;
1965} 2029}
1966 2030
1983 2047
1984 EXTEND (SP, AvFILLp (av) + 1); 2048 EXTEND (SP, AvFILLp (av) + 1);
1985 for (i = 0; i <= AvFILLp (av); ++i) 2049 for (i = 0; i <= AvFILLp (av); ++i)
1986 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2050 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1987 2051
1988 /* we have stolen the elements, so ste length to zero and free */ 2052 /* we have stolen the elements, so set length to zero and free */
1989 AvFILLp (av) = -1; 2053 AvFILLp (av) = -1;
1990 av_undef (av); 2054 av_undef (av);
1991 2055
1992 PUTBACK; 2056 PUTBACK;
1993 } 2057 }
2283 2347
2284 PL_op->op_ppaddr = pp_slf; 2348 PL_op->op_ppaddr = pp_slf;
2285 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2349 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2286 2350
2287 PL_op = (OP *)&slf_restore; 2351 PL_op = (OP *)&slf_restore;
2352}
2353
2354/*****************************************************************************/
2355/* dynamic wind */
2356
2357static void
2358on_enterleave_call (pTHX_ SV *cb)
2359{
2360 dSP;
2361
2362 PUSHSTACK;
2363
2364 PUSHMARK (SP);
2365 PUTBACK;
2366 call_sv (cb, G_VOID | G_DISCARD);
2367 SPAGAIN;
2368
2369 POPSTACK;
2370}
2371
2372static SV *
2373coro_avp_pop_and_free (pTHX_ AV **avp)
2374{
2375 AV *av = *avp;
2376 SV *res = av_pop (av);
2377
2378 if (AvFILLp (av) < 0)
2379 {
2380 *avp = 0;
2381 SvREFCNT_dec (av);
2382 }
2383
2384 return res;
2385}
2386
2387static void
2388coro_pop_on_enter (pTHX_ void *coro)
2389{
2390 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2391 SvREFCNT_dec (cb);
2392}
2393
2394static void
2395coro_pop_on_leave (pTHX_ void *coro)
2396{
2397 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2398 on_enterleave_call (aTHX_ sv_2mortal (cb));
2288} 2399}
2289 2400
2290/*****************************************************************************/ 2401/*****************************************************************************/
2291/* PerlIO::cede */ 2402/* PerlIO::cede */
2292 2403
2918SV * 3029SV *
2919clone (Coro::State coro) 3030clone (Coro::State coro)
2920 CODE: 3031 CODE:
2921{ 3032{
2922#if CORO_CLONE 3033#if CORO_CLONE
2923 struct coro *ncoro = coro_clone (coro); 3034 struct coro *ncoro = coro_clone (aTHX_ coro);
2924 MAGIC *mg; 3035 MAGIC *mg;
2925 /* TODO: too much duplication */ 3036 /* TODO: too much duplication */
2926 ncoro->hv = newHV (); 3037 ncoro->hv = newHV ();
2927 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); 3038 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2928 mg->mg_flags |= MGf_DUP; 3039 mg->mg_flags |= MGf_DUP;
2990 eval = 1 3101 eval = 1
2991 CODE: 3102 CODE:
2992{ 3103{
2993 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3104 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2994 { 3105 {
2995 struct coro temp; 3106 struct coro *current = SvSTATE_current;
2996 3107
2997 if (!(coro->flags & CF_RUNNING)) 3108 if (current != coro)
2998 { 3109 {
2999 PUTBACK; 3110 PUTBACK;
3000 save_perl (aTHX_ &temp); 3111 save_perl (aTHX_ current);
3001 load_perl (aTHX_ coro); 3112 load_perl (aTHX_ coro);
3113 SPAGAIN;
3002 } 3114 }
3003 3115
3004 {
3005 dSP;
3006 ENTER;
3007 SAVETMPS;
3008 PUTBACK;
3009 PUSHSTACK; 3116 PUSHSTACK;
3117
3010 PUSHMARK (SP); 3118 PUSHMARK (SP);
3119 PUTBACK;
3011 3120
3012 if (ix) 3121 if (ix)
3013 eval_sv (coderef, 0); 3122 eval_sv (coderef, 0);
3014 else 3123 else
3015 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3124 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
3016 3125
3017 POPSTACK; 3126 POPSTACK;
3018 SPAGAIN; 3127 SPAGAIN;
3019 FREETMPS;
3020 LEAVE;
3021 PUTBACK;
3022 }
3023 3128
3024 if (!(coro->flags & CF_RUNNING)) 3129 if (current != coro)
3025 { 3130 {
3131 PUTBACK;
3026 save_perl (aTHX_ coro); 3132 save_perl (aTHX_ coro);
3027 load_perl (aTHX_ &temp); 3133 load_perl (aTHX_ current);
3028 SPAGAIN; 3134 SPAGAIN;
3029 } 3135 }
3030 } 3136 }
3031} 3137}
3032 3138
3036 ALIAS: 3142 ALIAS:
3037 is_ready = CF_READY 3143 is_ready = CF_READY
3038 is_running = CF_RUNNING 3144 is_running = CF_RUNNING
3039 is_new = CF_NEW 3145 is_new = CF_NEW
3040 is_destroyed = CF_DESTROYED 3146 is_destroyed = CF_DESTROYED
3147 is_suspended = CF_SUSPENDED
3041 CODE: 3148 CODE:
3042 RETVAL = boolSV (coro->flags & ix); 3149 RETVAL = boolSV (coro->flags & ix);
3043 OUTPUT: 3150 OUTPUT:
3044 RETVAL 3151 RETVAL
3045 3152
3110 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3217 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3111 3218
3112 SV *tmp = *src; *src = *dst; *dst = tmp; 3219 SV *tmp = *src; *src = *dst; *dst = tmp;
3113 } 3220 }
3114 3221
3222void
3223cancel (Coro::State self)
3224 CODE:
3225 coro_state_destroy (aTHX_ self);
3226 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3227
3115 3228
3116MODULE = Coro::State PACKAGE = Coro 3229MODULE = Coro::State PACKAGE = Coro
3117 3230
3118BOOT: 3231BOOT:
3119{ 3232{
3120 int i;
3121
3122 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3233 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3123 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3234 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3124 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3235 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3125 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3236 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3126 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3237 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); 3238 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3128 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3239 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3129 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3240 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3140 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3251 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
3141 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3252 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
3142 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3253 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
3143 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3254 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
3144 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3255 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
3145
3146 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3147 coro_ready[i] = newAV ();
3148 3256
3149 { 3257 {
3150 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3258 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3151 3259
3152 coroapi.schedule = api_schedule; 3260 coroapi.schedule = api_schedule;
3193cede_notself (...) 3301cede_notself (...)
3194 CODE: 3302 CODE:
3195 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3303 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3196 3304
3197void 3305void
3198_cancel (Coro::State self)
3199 CODE:
3200 coro_state_destroy (aTHX_ self);
3201 coro_call_on_destroy (aTHX_ self);
3202
3203void
3204_set_current (SV *current) 3306_set_current (SV *current)
3205 PROTOTYPE: $ 3307 PROTOTYPE: $
3206 CODE: 3308 CODE:
3207 SvREFCNT_dec (SvRV (coro_current)); 3309 SvREFCNT_dec (SvRV (coro_current));
3208 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3310 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3250 PROTOTYPE: 3352 PROTOTYPE:
3251 CODE: 3353 CODE:
3252 RETVAL = coro_nready; 3354 RETVAL = coro_nready;
3253 OUTPUT: 3355 OUTPUT:
3254 RETVAL 3356 RETVAL
3357
3358void
3359suspend (Coro::State self)
3360 PROTOTYPE: $
3361 CODE:
3362 self->flags |= CF_SUSPENDED;
3363
3364void
3365resume (Coro::State self)
3366 PROTOTYPE: $
3367 CODE:
3368 self->flags &= ~CF_SUSPENDED;
3255 3369
3256void 3370void
3257_pool_handler (...) 3371_pool_handler (...)
3258 CODE: 3372 CODE:
3259 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3373 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3314rouse_wait (...) 3428rouse_wait (...)
3315 PROTOTYPE: ;$ 3429 PROTOTYPE: ;$
3316 PPCODE: 3430 PPCODE:
3317 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3431 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3318 3432
3433void
3434on_enter (SV *block)
3435 ALIAS:
3436 on_leave = 1
3437 PROTOTYPE: &
3438 CODE:
3439{
3440 struct coro *coro = SvSTATE_current;
3441 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3442
3443 block = (SV *)coro_sv_2cv (aTHX_ block);
3444
3445 if (!*avp)
3446 *avp = newAV ();
3447
3448 av_push (*avp, SvREFCNT_inc (block));
3449
3450 if (!ix)
3451 on_enterleave_call (aTHX_ block);
3452
3453 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3454 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3455 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3456}
3457
3319 3458
3320MODULE = Coro::State PACKAGE = PerlIO::cede 3459MODULE = Coro::State PACKAGE = PerlIO::cede
3321 3460
3322BOOT: 3461BOOT:
3323 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3462 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3333 GvSTASH (CvGV (cv)) 3472 GvSTASH (CvGV (cv))
3334 ); 3473 );
3335 OUTPUT: 3474 OUTPUT:
3336 RETVAL 3475 RETVAL
3337 3476
3338# helper for Coro::Channel 3477# helper for Coro::Channel and others
3339SV * 3478SV *
3340_alloc (int count) 3479_alloc (int count)
3341 CODE: 3480 CODE:
3342 RETVAL = coro_waitarray_new (aTHX_ count); 3481 RETVAL = coro_waitarray_new (aTHX_ count);
3343 OUTPUT: 3482 OUTPUT:
3401 for (i = 1; i <= wcount; ++i) 3540 for (i = 1; i <= wcount; ++i)
3402 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3541 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3403 } 3542 }
3404} 3543}
3405 3544
3545MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3546
3547void
3548_may_delete (SV *sem, int count, int extra_refs)
3549 PPCODE:
3550{
3551 AV *av = (AV *)SvRV (sem);
3552
3553 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3554 && AvFILLp (av) == 0 /* no waiters, just count */
3555 && SvIV (AvARRAY (av)[0]) == count)
3556 XSRETURN_YES;
3557
3558 XSRETURN_NO;
3559}
3560
3406MODULE = Coro::State PACKAGE = Coro::Signal 3561MODULE = Coro::State PACKAGE = Coro::Signal
3407 3562
3408SV * 3563SV *
3409new (SV *klass) 3564new (SV *klass)
3410 CODE: 3565 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines