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.329 by root, Tue Nov 25 20:48:41 2008 UTC vs.
Revision 1.348 by root, Wed Jun 10 00:20:26 2009 UTC

133#else 133#else
134# define dSTACKLEVEL volatile void *stacklevel 134# define dSTACKLEVEL volatile void *stacklevel
135# define STACKLEVEL ((void *)&stacklevel) 135# define STACKLEVEL ((void *)&stacklevel)
136#endif 136#endif
137 137
138#define IN_DESTRUCT (PL_main_cv == Nullcv) 138#define IN_DESTRUCT PL_dirty
139 139
140#if __GNUC__ >= 3 140#if __GNUC__ >= 3
141# define attribute(x) __attribute__(x) 141# define attribute(x) __attribute__(x)
142# define expect(expr,value) __builtin_expect ((expr),(value)) 142# define expect(expr,value) __builtin_expect ((expr),(value))
143# define INLINE static inline 143# define INLINE static inline
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
382static void 447static void
383free_padlist (pTHX_ AV *padlist) 448free_padlist (pTHX_ AV *padlist)
384{ 449{
385 /* may be during global destruction */ 450 /* may be during global destruction */
386 if (SvREFCNT (padlist)) 451 if (!IN_DESTRUCT)
387 { 452 {
388 I32 i = AvFILLp (padlist); 453 I32 i = AvFILLp (padlist);
389 while (i >= 0) 454
455 while (i > 0) /* special-case index 0 */
390 { 456 {
391 SV **svp = av_fetch (padlist, i--, FALSE); 457 /* we try to be extra-careful here */
392 if (svp) 458 AV *av = (AV *)AvARRAY (padlist)[i--];
393 { 459 I32 j = AvFILLp (av);
394 SV *sv; 460
395 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 461 while (j >= 0)
462 SvREFCNT_dec (AvARRAY (av)[j--]);
463
464 AvFILLp (av) = -1;
396 SvREFCNT_dec (sv); 465 SvREFCNT_dec (av);
397
398 SvREFCNT_dec (*svp);
399 }
400 } 466 }
401 467
468 SvREFCNT_dec (AvARRAY (padlist)[0]);
469
470 AvFILLp (padlist) = -1;
402 SvREFCNT_dec ((SV*)padlist); 471 SvREFCNT_dec ((SV*)padlist);
403 } 472 }
404} 473}
405 474
406static int 475static int
415 484
416 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 485 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
417 486
418 return 0; 487 return 0;
419} 488}
420
421#define CORO_MAGIC_type_cv 26
422#define CORO_MAGIC_type_state PERL_MAGIC_ext
423 489
424static MGVTBL coro_cv_vtbl = { 490static MGVTBL coro_cv_vtbl = {
425 0, 0, 0, 0, 491 0, 0, 0, 0,
426 coro_cv_free 492 coro_cv_free
427}; 493};
428
429#define CORO_MAGIC_NN(sv, type) \
430 (expect_true (SvMAGIC (sv)->mg_type == type) \
431 ? SvMAGIC (sv) \
432 : mg_find (sv, type))
433
434#define CORO_MAGIC(sv, type) \
435 (expect_true (SvMAGIC (sv)) \
436 ? CORO_MAGIC_NN (sv, type) \
437 : 0)
438
439#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
440#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
441
442INLINE struct coro *
443SvSTATE_ (pTHX_ SV *coro)
444{
445 HV *stash;
446 MAGIC *mg;
447
448 if (SvROK (coro))
449 coro = SvRV (coro);
450
451 if (expect_false (SvTYPE (coro) != SVt_PVHV))
452 croak ("Coro::State object required");
453
454 stash = SvSTASH (coro);
455 if (expect_false (stash != coro_stash && stash != coro_state_stash))
456 {
457 /* very slow, but rare, check */
458 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
459 croak ("Coro::State object required");
460 }
461
462 mg = CORO_MAGIC_state (coro);
463 return (struct coro *)mg->mg_ptr;
464}
465
466#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
467
468/* faster than SvSTATE, but expects a coroutine hv */
469#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
470#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
471 494
472/* the next two functions merely cache the padlists */ 495/* the next two functions merely cache the padlists */
473static void 496static void
474get_padlist (pTHX_ CV *cv) 497get_padlist (pTHX_ CV *cv)
475{ 498{
503 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 526 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
504 527
505 av = (AV *)mg->mg_obj; 528 av = (AV *)mg->mg_obj;
506 529
507 if (expect_false (AvFILLp (av) >= AvMAX (av))) 530 if (expect_false (AvFILLp (av) >= AvMAX (av)))
508 av_extend (av, AvMAX (av) + 1); 531 av_extend (av, AvFILLp (av) + 1);
509 532
510 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 533 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
511} 534}
512 535
513/** load & save, init *******************************************************/ 536/** load & save, init *******************************************************/
537
538static void
539on_enterleave_call (pTHX_ SV *cb);
514 540
515static void 541static void
516load_perl (pTHX_ Coro__State c) 542load_perl (pTHX_ Coro__State c)
517{ 543{
518 perl_slots *slot = c->slot; 544 perl_slots *slot = c->slot;
546 PUTBACK; 572 PUTBACK;
547 } 573 }
548 574
549 slf_frame = c->slf_frame; 575 slf_frame = c->slf_frame;
550 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 }
551} 585}
552 586
553static void 587static void
554save_perl (pTHX_ Coro__State c) 588save_perl (pTHX_ Coro__State c)
555{ 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
556 c->except = CORO_THROW; 598 c->except = CORO_THROW;
557 c->slf_frame = slf_frame; 599 c->slf_frame = slf_frame;
558 600
559 { 601 {
560 dSP; 602 dSP;
759#endif 801#endif
760 802
761/* 803/*
762 * This overrides the default magic get method of %SIG elements. 804 * This overrides the default magic get method of %SIG elements.
763 * 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
764 * 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
765 * readback here. 807 * readback here.
766 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
767 * not expecting this to actually change the hook. This is a potential problem
768 * when a schedule happens then, but we ignore this.
769 */ 808 */
770static int 809static int
771coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 810coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
772{ 811{
773 const char *s = MgPV_nolen_const (mg); 812 const char *s = MgPV_nolen_const (mg);
826 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 865 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
827 866
828 if (svp) 867 if (svp)
829 { 868 {
830 SV *old = *svp; 869 SV *old = *svp;
831 *svp = newSVsv (sv); 870 *svp = SvOK (sv) ? newSVsv (sv) : 0;
832 SvREFCNT_dec (old); 871 SvREFCNT_dec (old);
833 return 0; 872 return 0;
834 } 873 }
835 } 874 }
836 875
928 /* copy throw, in case it was set before coro_setup */ 967 /* copy throw, in case it was set before coro_setup */
929 CORO_THROW = coro->except; 968 CORO_THROW = coro->except;
930} 969}
931 970
932static void 971static void
933coro_destruct (pTHX_ struct coro *coro) 972coro_unwind_stacks (pTHX)
934{ 973{
935 if (!IN_DESTRUCT) 974 if (!IN_DESTRUCT)
936 { 975 {
937 /* restore all saved variables and stuff */ 976 /* restore all saved variables and stuff */
938 LEAVE_SCOPE (0); 977 LEAVE_SCOPE (0);
946 POPSTACK_TO (PL_mainstack); 985 POPSTACK_TO (PL_mainstack);
947 986
948 /* unwind main stack */ 987 /* unwind main stack */
949 dounwind (-1); 988 dounwind (-1);
950 } 989 }
990}
991
992static void
993coro_destruct_perl (pTHX_ struct coro *coro)
994{
995 coro_unwind_stacks (aTHX);
951 996
952 SvREFCNT_dec (GvSV (PL_defgv)); 997 SvREFCNT_dec (GvSV (PL_defgv));
953 SvREFCNT_dec (GvAV (PL_defgv)); 998 SvREFCNT_dec (GvAV (PL_defgv));
954 SvREFCNT_dec (GvSV (PL_errgv)); 999 SvREFCNT_dec (GvSV (PL_errgv));
955 SvREFCNT_dec (PL_defoutgv); 1000 SvREFCNT_dec (PL_defoutgv);
1267cctx_destroy (coro_cctx *cctx) 1312cctx_destroy (coro_cctx *cctx)
1268{ 1313{
1269 if (!cctx) 1314 if (!cctx)
1270 return; 1315 return;
1271 1316
1272 assert (cctx != cctx_current);//D temporary 1317 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1273 1318
1274 --cctx_count; 1319 --cctx_count;
1275 coro_destroy (&cctx->cctx); 1320 coro_destroy (&cctx->cctx);
1276 1321
1277 /* coro_transfer creates new, empty cctx's */ 1322 /* coro_transfer creates new, empty cctx's */
1341 /* TODO: throwing up here is considered harmful */ 1386 /* TODO: throwing up here is considered harmful */
1342 1387
1343 if (expect_true (prev != next)) 1388 if (expect_true (prev != next))
1344 { 1389 {
1345 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1390 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1346 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); 1391 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1347 1392
1348 if (expect_false (next->flags & CF_RUNNING))
1349 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1350
1351 if (expect_false (next->flags & CF_DESTROYED)) 1393 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1352 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); 1394 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1353 1395
1354#if !PERL_VERSION_ATLEAST (5,10,0) 1396#if !PERL_VERSION_ATLEAST (5,10,0)
1355 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1397 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1356 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); 1398 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1357#endif 1399#endif
1395 coro_setup (aTHX_ next); 1437 coro_setup (aTHX_ next);
1396 } 1438 }
1397 else 1439 else
1398 load_perl (aTHX_ next); 1440 load_perl (aTHX_ next);
1399 1441
1400 assert (!prev->cctx);//D temporary
1401
1402 /* possibly untie and reuse the cctx */ 1442 /* possibly untie and reuse the cctx */
1403 if (expect_true ( 1443 if (expect_true (
1404 cctx_current->idle_sp == STACKLEVEL 1444 cctx_current->idle_sp == STACKLEVEL
1405 && !(cctx_current->flags & CC_TRACE) 1445 && !(cctx_current->flags & CC_TRACE)
1406 && !force_cctx 1446 && !force_cctx
1447coro_state_destroy (pTHX_ struct coro *coro) 1487coro_state_destroy (pTHX_ struct coro *coro)
1448{ 1488{
1449 if (coro->flags & CF_DESTROYED) 1489 if (coro->flags & CF_DESTROYED)
1450 return 0; 1490 return 0;
1451 1491
1452 if (coro->on_destroy) 1492 if (coro->on_destroy && !PL_dirty)
1453 coro->on_destroy (aTHX_ coro); 1493 coro->on_destroy (aTHX_ coro);
1454 1494
1455 coro->flags |= CF_DESTROYED; 1495 coro->flags |= CF_DESTROYED;
1456 1496
1457 if (coro->flags & CF_READY) 1497 if (coro->flags & CF_READY)
1461 --coro_nready; 1501 --coro_nready;
1462 } 1502 }
1463 else 1503 else
1464 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1504 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1465 1505
1466 if (coro->mainstack && coro->mainstack != main_mainstack) 1506 if (coro->mainstack
1507 && coro->mainstack != main_mainstack
1508 && coro->slot
1509 && !PL_dirty)
1467 { 1510 {
1468 struct coro temp; 1511 struct coro *current = SvSTATE_current;
1469 1512
1470 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); 1513 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1471 1514
1472 save_perl (aTHX_ &temp); 1515 save_perl (aTHX_ current);
1473 load_perl (aTHX_ coro); 1516 load_perl (aTHX_ coro);
1474 1517
1475 coro_destruct (aTHX_ coro); 1518 coro_destruct_perl (aTHX_ coro);
1476 1519
1477 load_perl (aTHX_ &temp); 1520 load_perl (aTHX_ current);
1478 1521
1479 coro->slot = 0; 1522 coro->slot = 0;
1480 } 1523 }
1481 1524
1482 cctx_destroy (coro->cctx); 1525 cctx_destroy (coro->cctx);
1572/** Coro ********************************************************************/ 1615/** Coro ********************************************************************/
1573 1616
1574INLINE void 1617INLINE void
1575coro_enq (pTHX_ struct coro *coro) 1618coro_enq (pTHX_ struct coro *coro)
1576{ 1619{
1577 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); 1620 struct coro **ready = coro_ready [coro->prio - PRIO_MIN];
1578}
1579 1621
1580INLINE SV * 1622 SvREFCNT_inc_NN (coro->hv);
1623
1624 coro->next_ready = 0;
1625 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1626 ready [1] = coro;
1627}
1628
1629INLINE struct coro *
1581coro_deq (pTHX) 1630coro_deq (pTHX)
1582{ 1631{
1583 int prio; 1632 int prio;
1584 1633
1585 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1634 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1586 if (AvFILLp (coro_ready [prio]) >= 0) 1635 {
1587 return av_shift (coro_ready [prio]); 1636 struct coro **ready = coro_ready [prio];
1637
1638 if (ready [0])
1639 {
1640 struct coro *coro = ready [0];
1641 ready [0] = coro->next_ready;
1642 return coro;
1643 }
1644 }
1588 1645
1589 return 0; 1646 return 0;
1590} 1647}
1591 1648
1592static int 1649static int
1656static void 1713static void
1657prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1714prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1658{ 1715{
1659 for (;;) 1716 for (;;)
1660 { 1717 {
1661 SV *next_sv = coro_deq (aTHX); 1718 struct coro *next = coro_deq (aTHX);
1662 1719
1663 if (expect_true (next_sv)) 1720 if (expect_true (next))
1664 { 1721 {
1665 struct coro *next = SvSTATE_hv (next_sv);
1666
1667 /* cannot transfer to destroyed coros, skip and look for next */ 1722 /* cannot transfer to destroyed coros, skip and look for next */
1668 if (expect_false (next->flags & CF_DESTROYED)) 1723 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1669 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1724 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1670 else 1725 else
1671 { 1726 {
1672 next->flags &= ~CF_READY; 1727 next->flags &= ~CF_READY;
1673 --coro_nready; 1728 --coro_nready;
1674 1729
1852 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 1907 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1853 api_ready (aTHX_ sv_manager); 1908 api_ready (aTHX_ sv_manager);
1854 1909
1855 frame->prepare = prepare_schedule; 1910 frame->prepare = prepare_schedule;
1856 frame->check = slf_check_repeat; 1911 frame->check = slf_check_repeat;
1912
1913 /* as a minor optimisation, we could unwind all stacks here */
1914 /* but that puts extra pressure on pp_slf, and is not worth much */
1915 /*coro_unwind_stacks (aTHX);*/
1857} 1916}
1858 1917
1859/*****************************************************************************/ 1918/*****************************************************************************/
1860/* async pool handler */ 1919/* async pool handler */
1861 1920
1944 SV *data = (SV *)GENSUB_ARG; 2003 SV *data = (SV *)GENSUB_ARG;
1945 2004
1946 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2005 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1947 { 2006 {
1948 /* first call, set args */ 2007 /* first call, set args */
2008 SV *coro = SvRV (data);
1949 AV *av = newAV (); 2009 AV *av = newAV ();
1950 SV *coro = SvRV (data);
1951 2010
1952 SvRV_set (data, (SV *)av); 2011 SvRV_set (data, (SV *)av);
1953 api_ready (aTHX_ coro);
1954 SvREFCNT_dec (coro);
1955 2012
1956 /* better take a full copy of the arguments */ 2013 /* better take a full copy of the arguments */
1957 while (items--) 2014 while (items--)
1958 av_store (av, items, newSVsv (ST (items))); 2015 av_store (av, items, newSVsv (ST (items)));
2016
2017 api_ready (aTHX_ coro);
2018 SvREFCNT_dec (coro);
1959 } 2019 }
1960 2020
1961 XSRETURN_EMPTY; 2021 XSRETURN_EMPTY;
1962} 2022}
1963 2023
1980 2040
1981 EXTEND (SP, AvFILLp (av) + 1); 2041 EXTEND (SP, AvFILLp (av) + 1);
1982 for (i = 0; i <= AvFILLp (av); ++i) 2042 for (i = 0; i <= AvFILLp (av); ++i)
1983 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2043 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1984 2044
1985 /* we have stolen the elements, so ste length to zero and free */ 2045 /* we have stolen the elements, so set length to zero and free */
1986 AvFILLp (av) = -1; 2046 AvFILLp (av) = -1;
1987 av_undef (av); 2047 av_undef (av);
1988 2048
1989 PUTBACK; 2049 PUTBACK;
1990 } 2050 }
2280 2340
2281 PL_op->op_ppaddr = pp_slf; 2341 PL_op->op_ppaddr = pp_slf;
2282 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2342 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2283 2343
2284 PL_op = (OP *)&slf_restore; 2344 PL_op = (OP *)&slf_restore;
2345}
2346
2347/*****************************************************************************/
2348/* dynamic wind */
2349
2350static void
2351on_enterleave_call (pTHX_ SV *cb)
2352{
2353 dSP;
2354
2355 PUSHSTACK;
2356
2357 PUSHMARK (SP);
2358 PUTBACK;
2359 call_sv (cb, G_VOID | G_DISCARD);
2360 SPAGAIN;
2361
2362 POPSTACK;
2363}
2364
2365static SV *
2366coro_avp_pop_and_free (pTHX_ AV **avp)
2367{
2368 AV *av = *avp;
2369 SV *res = av_pop (av);
2370
2371 if (AvFILLp (av) < 0)
2372 {
2373 *avp = 0;
2374 SvREFCNT_dec (av);
2375 }
2376
2377 return res;
2378}
2379
2380static void
2381coro_pop_on_enter (pTHX_ void *coro)
2382{
2383 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2384 SvREFCNT_dec (cb);
2385}
2386
2387static void
2388coro_pop_on_leave (pTHX_ void *coro)
2389{
2390 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2391 on_enterleave_call (aTHX_ sv_2mortal (cb));
2285} 2392}
2286 2393
2287/*****************************************************************************/ 2394/*****************************************************************************/
2288/* PerlIO::cede */ 2395/* PerlIO::cede */
2289 2396
2915SV * 3022SV *
2916clone (Coro::State coro) 3023clone (Coro::State coro)
2917 CODE: 3024 CODE:
2918{ 3025{
2919#if CORO_CLONE 3026#if CORO_CLONE
2920 struct coro *ncoro = coro_clone (coro); 3027 struct coro *ncoro = coro_clone (aTHX_ coro);
2921 MAGIC *mg; 3028 MAGIC *mg;
2922 /* TODO: too much duplication */ 3029 /* TODO: too much duplication */
2923 ncoro->hv = newHV (); 3030 ncoro->hv = newHV ();
2924 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); 3031 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2925 mg->mg_flags |= MGf_DUP; 3032 mg->mg_flags |= MGf_DUP;
2987 eval = 1 3094 eval = 1
2988 CODE: 3095 CODE:
2989{ 3096{
2990 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3097 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2991 { 3098 {
2992 struct coro temp; 3099 struct coro *current = SvSTATE_current;
2993 3100
2994 if (!(coro->flags & CF_RUNNING)) 3101 if (current != coro)
2995 { 3102 {
2996 PUTBACK; 3103 PUTBACK;
2997 save_perl (aTHX_ &temp); 3104 save_perl (aTHX_ current);
2998 load_perl (aTHX_ coro); 3105 load_perl (aTHX_ coro);
3106 SPAGAIN;
2999 } 3107 }
3000 3108
3001 {
3002 dSP;
3003 ENTER;
3004 SAVETMPS;
3005 PUTBACK;
3006 PUSHSTACK; 3109 PUSHSTACK;
3110
3007 PUSHMARK (SP); 3111 PUSHMARK (SP);
3112 PUTBACK;
3008 3113
3009 if (ix) 3114 if (ix)
3010 eval_sv (coderef, 0); 3115 eval_sv (coderef, 0);
3011 else 3116 else
3012 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3117 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
3013 3118
3014 POPSTACK; 3119 POPSTACK;
3015 SPAGAIN; 3120 SPAGAIN;
3016 FREETMPS;
3017 LEAVE;
3018 PUTBACK;
3019 }
3020 3121
3021 if (!(coro->flags & CF_RUNNING)) 3122 if (current != coro)
3022 { 3123 {
3124 PUTBACK;
3023 save_perl (aTHX_ coro); 3125 save_perl (aTHX_ coro);
3024 load_perl (aTHX_ &temp); 3126 load_perl (aTHX_ current);
3025 SPAGAIN; 3127 SPAGAIN;
3026 } 3128 }
3027 } 3129 }
3028} 3130}
3029 3131
3033 ALIAS: 3135 ALIAS:
3034 is_ready = CF_READY 3136 is_ready = CF_READY
3035 is_running = CF_RUNNING 3137 is_running = CF_RUNNING
3036 is_new = CF_NEW 3138 is_new = CF_NEW
3037 is_destroyed = CF_DESTROYED 3139 is_destroyed = CF_DESTROYED
3140 is_suspended = CF_SUSPENDED
3038 CODE: 3141 CODE:
3039 RETVAL = boolSV (coro->flags & ix); 3142 RETVAL = boolSV (coro->flags & ix);
3040 OUTPUT: 3143 OUTPUT:
3041 RETVAL 3144 RETVAL
3042 3145
3107 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3210 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3108 3211
3109 SV *tmp = *src; *src = *dst; *dst = tmp; 3212 SV *tmp = *src; *src = *dst; *dst = tmp;
3110 } 3213 }
3111 3214
3215void
3216cancel (Coro::State self)
3217 CODE:
3218 coro_state_destroy (aTHX_ self);
3219 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3220
3112 3221
3113MODULE = Coro::State PACKAGE = Coro 3222MODULE = Coro::State PACKAGE = Coro
3114 3223
3115BOOT: 3224BOOT:
3116{ 3225{
3117 int i; 3226 int i;
3118 3227
3119 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3228 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3120 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3229 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3121 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3230 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3122 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3231 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3123 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3232 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3124 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3233 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3125 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3234 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3126 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3235 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3137 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3246 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
3138 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3247 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
3139 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3248 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
3140 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3249 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
3141 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3250 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
3142
3143 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3144 coro_ready[i] = newAV ();
3145 3251
3146 { 3252 {
3147 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3253 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3148 3254
3149 coroapi.schedule = api_schedule; 3255 coroapi.schedule = api_schedule;
3190cede_notself (...) 3296cede_notself (...)
3191 CODE: 3297 CODE:
3192 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3298 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3193 3299
3194void 3300void
3195_cancel (Coro::State self)
3196 CODE:
3197 coro_state_destroy (aTHX_ self);
3198 coro_call_on_destroy (aTHX_ self);
3199
3200void
3201_set_current (SV *current) 3301_set_current (SV *current)
3202 PROTOTYPE: $ 3302 PROTOTYPE: $
3203 CODE: 3303 CODE:
3204 SvREFCNT_dec (SvRV (coro_current)); 3304 SvREFCNT_dec (SvRV (coro_current));
3205 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3305 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3247 PROTOTYPE: 3347 PROTOTYPE:
3248 CODE: 3348 CODE:
3249 RETVAL = coro_nready; 3349 RETVAL = coro_nready;
3250 OUTPUT: 3350 OUTPUT:
3251 RETVAL 3351 RETVAL
3352
3353void
3354suspend (Coro::State self)
3355 PROTOTYPE: $
3356 CODE:
3357 self->flags |= CF_SUSPENDED;
3358
3359void
3360resume (Coro::State self)
3361 PROTOTYPE: $
3362 CODE:
3363 self->flags &= ~CF_SUSPENDED;
3252 3364
3253void 3365void
3254_pool_handler (...) 3366_pool_handler (...)
3255 CODE: 3367 CODE:
3256 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3368 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3311rouse_wait (...) 3423rouse_wait (...)
3312 PROTOTYPE: ;$ 3424 PROTOTYPE: ;$
3313 PPCODE: 3425 PPCODE:
3314 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3426 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3315 3427
3428void
3429on_enter (SV *block)
3430 ALIAS:
3431 on_leave = 1
3432 PROTOTYPE: &
3433 CODE:
3434{
3435 struct coro *coro = SvSTATE_current;
3436 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3437
3438 block = (SV *)coro_sv_2cv (aTHX_ block);
3439
3440 if (!*avp)
3441 *avp = newAV ();
3442
3443 av_push (*avp, SvREFCNT_inc (block));
3444
3445 if (!ix)
3446 on_enterleave_call (aTHX_ block);
3447
3448 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3449 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3450 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3451}
3452
3316 3453
3317MODULE = Coro::State PACKAGE = PerlIO::cede 3454MODULE = Coro::State PACKAGE = PerlIO::cede
3318 3455
3319BOOT: 3456BOOT:
3320 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3457 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3330 GvSTASH (CvGV (cv)) 3467 GvSTASH (CvGV (cv))
3331 ); 3468 );
3332 OUTPUT: 3469 OUTPUT:
3333 RETVAL 3470 RETVAL
3334 3471
3335# helper for Coro::Channel 3472# helper for Coro::Channel and others
3336SV * 3473SV *
3337_alloc (int count) 3474_alloc (int count)
3338 CODE: 3475 CODE:
3339 RETVAL = coro_waitarray_new (aTHX_ count); 3476 RETVAL = coro_waitarray_new (aTHX_ count);
3340 OUTPUT: 3477 OUTPUT:
3398 for (i = 1; i <= wcount; ++i) 3535 for (i = 1; i <= wcount; ++i)
3399 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3536 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3400 } 3537 }
3401} 3538}
3402 3539
3540MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3541
3542void
3543_may_delete (SV *sem, int count, int extra_refs)
3544 PPCODE:
3545{
3546 AV *av = (AV *)SvRV (sem);
3547
3548 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3549 && AvFILLp (av) == 0 /* no waiters, just count */
3550 && SvIV (AvARRAY (av)[0]) == count)
3551 XSRETURN_YES;
3552
3553 XSRETURN_NO;
3554}
3555
3403MODULE = Coro::State PACKAGE = Coro::Signal 3556MODULE = Coro::State PACKAGE = Coro::Signal
3404 3557
3405SV * 3558SV *
3406new (SV *klass) 3559new (SV *klass)
3407 CODE: 3560 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines