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.320 by root, Fri Nov 21 07:29:04 2008 UTC vs.
Revision 1.347 by root, Wed Jun 10 00:00:02 2009 UTC

101# define CvISXSUB_on(cv) (void)cv 101# define CvISXSUB_on(cv) (void)cv
102#endif 102#endif
103#ifndef CvISXSUB 103#ifndef CvISXSUB
104# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) 104# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE)
105#endif 105#endif
106#ifndef Newx
107# define Newx(ptr,nitems,type) New (0,ptr,nitems,type)
108#endif
106 109
107/* 5.8.7 */ 110/* 5.8.7 */
108#ifndef SvRV_set 111#ifndef SvRV_set
109# define SvRV_set(s,v) SvRV(s) = (v) 112# define SvRV_set(s,v) SvRV(s) = (v)
110#endif 113#endif
130#else 133#else
131# define dSTACKLEVEL volatile void *stacklevel 134# define dSTACKLEVEL volatile void *stacklevel
132# define STACKLEVEL ((void *)&stacklevel) 135# define STACKLEVEL ((void *)&stacklevel)
133#endif 136#endif
134 137
135#define IN_DESTRUCT (PL_main_cv == Nullcv) 138#define IN_DESTRUCT PL_dirty
136 139
137#if __GNUC__ >= 3 140#if __GNUC__ >= 3
138# define attribute(x) __attribute__(x) 141# define attribute(x) __attribute__(x)
139# define expect(expr,value) __builtin_expect ((expr),(value)) 142# define expect(expr,value) __builtin_expect ((expr),(value))
140# define INLINE static inline 143# define INLINE static inline
172static HV *coro_state_stash, *coro_stash; 175static HV *coro_state_stash, *coro_stash;
173static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
174 177
175static AV *av_destroy; /* destruction queue */ 178static AV *av_destroy; /* destruction queue */
176static SV *sv_manager; /* the manager coro */ 179static SV *sv_manager; /* the manager coro */
180static SV *sv_idle; /* $Coro::idle */
177 181
178static GV *irsgv; /* $/ */ 182static GV *irsgv; /* $/ */
179static GV *stdoutgv; /* *STDOUT */ 183static GV *stdoutgv; /* *STDOUT */
180static SV *rv_diehook; 184static SV *rv_diehook;
181static SV *rv_warnhook; 185static SV *rv_warnhook;
225 int valgrind_id; 229 int valgrind_id;
226#endif 230#endif
227 unsigned char flags; 231 unsigned char flags;
228} coro_cctx; 232} coro_cctx;
229 233
234coro_cctx *cctx_current; /* the currently running cctx */
235
236/*****************************************************************************/
237
230enum { 238enum {
231 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
232 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
233 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
234 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 */
235}; 244};
236 245
237/* 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 */
238typedef struct 247typedef struct
239{ 248{
240 SV *defsv; 249 SV *defsv;
241 AV *defav; 250 AV *defav;
242 SV *errsv; 251 SV *errsv;
243 SV *irsgv; 252 SV *irsgv;
253 HV *hinthv;
244#define VAR(name,type) type name; 254#define VAR(name,type) type name;
245# include "state.h" 255# include "state.h"
246#undef VAR 256#undef VAR
247} perl_slots; 257} perl_slots;
248 258
250 260
251/* this is a structure representing a perl-level coroutine */ 261/* this is a structure representing a perl-level coroutine */
252struct coro { 262struct coro {
253 /* the C coroutine allocated to this perl coroutine, if any */ 263 /* the C coroutine allocated to this perl coroutine, if any */
254 coro_cctx *cctx; 264 coro_cctx *cctx;
265
266 /* ready queue */
267 struct coro *next_ready;
255 268
256 /* state data */ 269 /* state data */
257 struct CoroSLF slf_frame; /* saved slf frame */ 270 struct CoroSLF slf_frame; /* saved slf frame */
258 AV *mainstack; 271 AV *mainstack;
259 perl_slots *slot; /* basically the saved sp */ 272 perl_slots *slot; /* basically the saved sp */
276 /* async_pool */ 289 /* async_pool */
277 SV *saved_deffh; 290 SV *saved_deffh;
278 SV *invoke_cb; 291 SV *invoke_cb;
279 AV *invoke_av; 292 AV *invoke_av;
280 293
294 /* on_enter/on_leave */
295 AV *on_enter;
296 AV *on_leave;
297
281 /* linked list */ 298 /* linked list */
282 struct coro *next, *prev; 299 struct coro *next, *prev;
283}; 300};
284 301
285typedef struct coro *Coro__State; 302typedef struct coro *Coro__State;
300#define PRIO_MIN -4 317#define PRIO_MIN -4
301 318
302/* for Coro.pm */ 319/* for Coro.pm */
303static SV *coro_current; 320static SV *coro_current;
304static SV *coro_readyhook; 321static SV *coro_readyhook;
305static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 322static struct coro *coro_ready [PRIO_MAX - PRIO_MIN + 1][2]; /* head|tail */
306static CV *cv_coro_run, *cv_coro_terminate; 323static CV *cv_coro_run, *cv_coro_terminate;
307static struct coro *coro_first; 324static struct coro *coro_first;
308#define coro_nready coroapi.nready 325#define coro_nready coroapi.nready
309 326
310/** lowlevel stuff **********************************************************/ 327/** lowlevel stuff **********************************************************/
343INLINE CV * 360INLINE CV *
344coro_sv_2cv (pTHX_ SV *sv) 361coro_sv_2cv (pTHX_ SV *sv)
345{ 362{
346 HV *st; 363 HV *st;
347 GV *gvp; 364 GV *gvp;
348 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;
349} 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 */
350 424
351static AV * 425static AV *
352coro_clone_padlist (pTHX_ CV *cv) 426coro_derive_padlist (pTHX_ CV *cv)
353{ 427{
354 AV *padlist = CvPADLIST (cv); 428 AV *padlist = CvPADLIST (cv);
355 AV *newpadlist, *newpad; 429 AV *newpadlist, *newpad;
356 430
357 newpadlist = newAV (); 431 newpadlist = newAV ();
362 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 436 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
363#endif 437#endif
364 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 438 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
365 --AvFILLp (padlist); 439 --AvFILLp (padlist);
366 440
367 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 441 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
368 av_store (newpadlist, 1, (SV *)newpad); 442 av_store (newpadlist, 1, (SV *)newpad);
369 443
370 return newpadlist; 444 return newpadlist;
371} 445}
372 446
373static void 447static void
374free_padlist (pTHX_ AV *padlist) 448free_padlist (pTHX_ AV *padlist)
375{ 449{
376 /* may be during global destruction */ 450 /* may be during global destruction */
377 if (SvREFCNT (padlist)) 451 if (!IN_DESTRUCT)
378 { 452 {
379 I32 i = AvFILLp (padlist); 453 I32 i = AvFILLp (padlist);
380 while (i >= 0) 454
455 while (i > 0) /* special-case index 0 */
381 { 456 {
382 SV **svp = av_fetch (padlist, i--, FALSE); 457 /* we try to be extra-careful here */
383 if (svp) 458 AV *av = (AV *)AvARRAY (padlist)[i--];
384 { 459 I32 j = AvFILLp (av);
385 SV *sv; 460
386 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 461 while (j >= 0)
462 SvREFCNT_dec (AvARRAY (av)[j--]);
463
464 AvFILLp (av) = -1;
387 SvREFCNT_dec (sv); 465 SvREFCNT_dec (av);
388
389 SvREFCNT_dec (*svp);
390 }
391 } 466 }
392 467
468 SvREFCNT_dec (AvARRAY (padlist)[0]);
469
470 AvFILLp (padlist) = -1;
393 SvREFCNT_dec ((SV*)padlist); 471 SvREFCNT_dec ((SV*)padlist);
394 } 472 }
395} 473}
396 474
397static int 475static int
406 484
407 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 485 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
408 486
409 return 0; 487 return 0;
410} 488}
411
412#define CORO_MAGIC_type_cv 26
413#define CORO_MAGIC_type_state PERL_MAGIC_ext
414 489
415static MGVTBL coro_cv_vtbl = { 490static MGVTBL coro_cv_vtbl = {
416 0, 0, 0, 0, 491 0, 0, 0, 0,
417 coro_cv_free 492 coro_cv_free
418}; 493};
419
420#define CORO_MAGIC_NN(sv, type) \
421 (expect_true (SvMAGIC (sv)->mg_type == type) \
422 ? SvMAGIC (sv) \
423 : mg_find (sv, type))
424
425#define CORO_MAGIC(sv, type) \
426 (expect_true (SvMAGIC (sv)) \
427 ? CORO_MAGIC_NN (sv, type) \
428 : 0)
429
430#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
431#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
432
433INLINE struct coro *
434SvSTATE_ (pTHX_ SV *coro)
435{
436 HV *stash;
437 MAGIC *mg;
438
439 if (SvROK (coro))
440 coro = SvRV (coro);
441
442 if (expect_false (SvTYPE (coro) != SVt_PVHV))
443 croak ("Coro::State object required");
444
445 stash = SvSTASH (coro);
446 if (expect_false (stash != coro_stash && stash != coro_state_stash))
447 {
448 /* very slow, but rare, check */
449 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
450 croak ("Coro::State object required");
451 }
452
453 mg = CORO_MAGIC_state (coro);
454 return (struct coro *)mg->mg_ptr;
455}
456
457#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
458
459/* faster than SvSTATE, but expects a coroutine hv */
460#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
461#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
462 494
463/* the next two functions merely cache the padlists */ 495/* the next two functions merely cache the padlists */
464static void 496static void
465get_padlist (pTHX_ CV *cv) 497get_padlist (pTHX_ CV *cv)
466{ 498{
477 CV *cp = Perl_cv_clone (aTHX_ cv); 509 CV *cp = Perl_cv_clone (aTHX_ cv);
478 CvPADLIST (cv) = CvPADLIST (cp); 510 CvPADLIST (cv) = CvPADLIST (cp);
479 CvPADLIST (cp) = 0; 511 CvPADLIST (cp) = 0;
480 SvREFCNT_dec (cp); 512 SvREFCNT_dec (cp);
481#else 513#else
482 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 514 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
483#endif 515#endif
484 } 516 }
485} 517}
486 518
487static void 519static void
494 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);
495 527
496 av = (AV *)mg->mg_obj; 528 av = (AV *)mg->mg_obj;
497 529
498 if (expect_false (AvFILLp (av) >= AvMAX (av))) 530 if (expect_false (AvFILLp (av) >= AvMAX (av)))
499 av_extend (av, AvMAX (av) + 1); 531 av_extend (av, AvFILLp (av) + 1);
500 532
501 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 533 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
502} 534}
503 535
504/** load & save, init *******************************************************/ 536/** load & save, init *******************************************************/
537
538static void
539on_enterleave_call (pTHX_ SV *cb);
505 540
506static void 541static void
507load_perl (pTHX_ Coro__State c) 542load_perl (pTHX_ Coro__State c)
508{ 543{
509 perl_slots *slot = c->slot; 544 perl_slots *slot = c->slot;
510 c->slot = 0; 545 c->slot = 0;
511 546
512 PL_mainstack = c->mainstack; 547 PL_mainstack = c->mainstack;
513 548
514 GvSV (PL_defgv) = slot->defsv; 549 GvSV (PL_defgv) = slot->defsv;
515 GvAV (PL_defgv) = slot->defav; 550 GvAV (PL_defgv) = slot->defav;
516 GvSV (PL_errgv) = slot->errsv; 551 GvSV (PL_errgv) = slot->errsv;
517 GvSV (irsgv) = slot->irsgv; 552 GvSV (irsgv) = slot->irsgv;
553 GvHV (PL_hintgv) = slot->hinthv;
518 554
519 #define VAR(name,type) PL_ ## name = slot->name; 555 #define VAR(name,type) PL_ ## name = slot->name;
520 # include "state.h" 556 # include "state.h"
521 #undef VAR 557 #undef VAR
522 558
536 PUTBACK; 572 PUTBACK;
537 } 573 }
538 574
539 slf_frame = c->slf_frame; 575 slf_frame = c->slf_frame;
540 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 }
541} 585}
542 586
543static void 587static void
544save_perl (pTHX_ Coro__State c) 588save_perl (pTHX_ Coro__State c)
545{ 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
546 c->except = CORO_THROW; 598 c->except = CORO_THROW;
547 c->slf_frame = slf_frame; 599 c->slf_frame = slf_frame;
548 600
549 { 601 {
550 dSP; 602 dSP;
608 c->mainstack = PL_mainstack; 660 c->mainstack = PL_mainstack;
609 661
610 { 662 {
611 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 663 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
612 664
613 slot->defav = GvAV (PL_defgv); 665 slot->defav = GvAV (PL_defgv);
614 slot->defsv = DEFSV; 666 slot->defsv = DEFSV;
615 slot->errsv = ERRSV; 667 slot->errsv = ERRSV;
616 slot->irsgv = GvSV (irsgv); 668 slot->irsgv = GvSV (irsgv);
669 slot->hinthv = GvHV (PL_hintgv);
617 670
618 #define VAR(name,type) slot->name = PL_ ## name; 671 #define VAR(name,type) slot->name = PL_ ## name;
619 # include "state.h" 672 # include "state.h"
620 #undef VAR 673 #undef VAR
621 } 674 }
748#endif 801#endif
749 802
750/* 803/*
751 * This overrides the default magic get method of %SIG elements. 804 * This overrides the default magic get method of %SIG elements.
752 * 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
753 * 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
754 * readback here. 807 * readback here.
755 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
756 * not expecting this to actually change the hook. This is a potential problem
757 * when a schedule happens then, but we ignore this.
758 */ 808 */
759static int 809static int
760coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 810coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
761{ 811{
762 const char *s = MgPV_nolen_const (mg); 812 const char *s = MgPV_nolen_const (mg);
815 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 865 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
816 866
817 if (svp) 867 if (svp)
818 { 868 {
819 SV *old = *svp; 869 SV *old = *svp;
820 *svp = newSVsv (sv); 870 *svp = SvOK (sv) ? newSVsv (sv) : 0;
821 SvREFCNT_dec (old); 871 SvREFCNT_dec (old);
822 return 0; 872 return 0;
823 } 873 }
824 } 874 }
825 875
857 907
858 PL_runops = RUNOPS_DEFAULT; 908 PL_runops = RUNOPS_DEFAULT;
859 PL_curcop = &PL_compiling; 909 PL_curcop = &PL_compiling;
860 PL_in_eval = EVAL_NULL; 910 PL_in_eval = EVAL_NULL;
861 PL_comppad = 0; 911 PL_comppad = 0;
912 PL_comppad_name = 0;
913 PL_comppad_name_fill = 0;
914 PL_comppad_name_floor = 0;
862 PL_curpm = 0; 915 PL_curpm = 0;
863 PL_curpad = 0; 916 PL_curpad = 0;
864 PL_localizing = 0; 917 PL_localizing = 0;
865 PL_dirty = 0; 918 PL_dirty = 0;
866 PL_restartop = 0; 919 PL_restartop = 0;
867#if PERL_VERSION_ATLEAST (5,10,0) 920#if PERL_VERSION_ATLEAST (5,10,0)
868 PL_parser = 0; 921 PL_parser = 0;
869#endif 922#endif
923 PL_hints = 0;
870 924
871 /* recreate the die/warn hooks */ 925 /* recreate the die/warn hooks */
872 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 926 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook );
873 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); 927 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
874 928
875 GvSV (PL_defgv) = newSV (0); 929 GvSV (PL_defgv) = newSV (0);
876 GvAV (PL_defgv) = coro->args; coro->args = 0; 930 GvAV (PL_defgv) = coro->args; coro->args = 0;
877 GvSV (PL_errgv) = newSV (0); 931 GvSV (PL_errgv) = newSV (0);
878 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 932 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
933 GvHV (PL_hintgv) = 0;
879 PL_rs = newSVsv (GvSV (irsgv)); 934 PL_rs = newSVsv (GvSV (irsgv));
880 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 935 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
881 936
882 { 937 {
883 dSP; 938 dSP;
912 /* copy throw, in case it was set before coro_setup */ 967 /* copy throw, in case it was set before coro_setup */
913 CORO_THROW = coro->except; 968 CORO_THROW = coro->except;
914} 969}
915 970
916static void 971static void
917coro_destruct (pTHX_ struct coro *coro) 972coro_unwind_stacks (pTHX)
918{ 973{
919 if (!IN_DESTRUCT) 974 if (!IN_DESTRUCT)
920 { 975 {
921 /* restore all saved variables and stuff */ 976 /* restore all saved variables and stuff */
922 LEAVE_SCOPE (0); 977 LEAVE_SCOPE (0);
930 POPSTACK_TO (PL_mainstack); 985 POPSTACK_TO (PL_mainstack);
931 986
932 /* unwind main stack */ 987 /* unwind main stack */
933 dounwind (-1); 988 dounwind (-1);
934 } 989 }
990}
991
992static void
993coro_destruct_perl (pTHX_ struct coro *coro)
994{
995 coro_unwind_stacks (aTHX);
935 996
936 SvREFCNT_dec (GvSV (PL_defgv)); 997 SvREFCNT_dec (GvSV (PL_defgv));
937 SvREFCNT_dec (GvAV (PL_defgv)); 998 SvREFCNT_dec (GvAV (PL_defgv));
938 SvREFCNT_dec (GvSV (PL_errgv)); 999 SvREFCNT_dec (GvSV (PL_errgv));
939 SvREFCNT_dec (PL_defoutgv); 1000 SvREFCNT_dec (PL_defoutgv);
940 SvREFCNT_dec (PL_rs); 1001 SvREFCNT_dec (PL_rs);
941 SvREFCNT_dec (GvSV (irsgv)); 1002 SvREFCNT_dec (GvSV (irsgv));
1003 SvREFCNT_dec (GvHV (PL_hintgv));
942 1004
943 SvREFCNT_dec (PL_diehook); 1005 SvREFCNT_dec (PL_diehook);
944 SvREFCNT_dec (PL_warnhook); 1006 SvREFCNT_dec (PL_warnhook);
945 1007
946 SvREFCNT_dec (coro->saved_deffh); 1008 SvREFCNT_dec (coro->saved_deffh);
964static int 1026static int
965runops_trace (pTHX) 1027runops_trace (pTHX)
966{ 1028{
967 COP *oldcop = 0; 1029 COP *oldcop = 0;
968 int oldcxix = -2; 1030 int oldcxix = -2;
969 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
970 coro_cctx *cctx = coro->cctx;
971 1031
972 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 1032 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
973 { 1033 {
974 PERL_ASYNC_CHECK (); 1034 PERL_ASYNC_CHECK ();
975 1035
976 if (cctx->flags & CC_TRACE_ALL) 1036 if (cctx_current->flags & CC_TRACE_ALL)
977 { 1037 {
978 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 1038 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
979 { 1039 {
980 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1040 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
981 SV **bot, **top; 1041 SV **bot, **top;
982 AV *av = newAV (); /* return values */ 1042 AV *av = newAV (); /* return values */
983 SV **cb; 1043 SV **cb;
1020 1080
1021 if (PL_curcop != &PL_compiling) 1081 if (PL_curcop != &PL_compiling)
1022 { 1082 {
1023 SV **cb; 1083 SV **cb;
1024 1084
1025 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1085 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1026 { 1086 {
1027 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1087 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1028 1088
1029 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1089 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1030 { 1090 {
1053 } 1113 }
1054 1114
1055 oldcxix = cxstack_ix; 1115 oldcxix = cxstack_ix;
1056 } 1116 }
1057 1117
1058 if (cctx->flags & CC_TRACE_LINE) 1118 if (cctx_current->flags & CC_TRACE_LINE)
1059 { 1119 {
1060 dSP; 1120 dSP;
1061 1121
1062 PL_runops = RUNOPS_DEFAULT; 1122 PL_runops = RUNOPS_DEFAULT;
1063 ENTER; 1123 ENTER;
1082 1142
1083 TAINT_NOT; 1143 TAINT_NOT;
1084 return 0; 1144 return 0;
1085} 1145}
1086 1146
1087static struct coro_cctx *cctx_ssl_cctx;
1088static struct CoroSLF cctx_ssl_frame; 1147static struct CoroSLF cctx_ssl_frame;
1089 1148
1090static void 1149static void
1091slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1150slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1092{ 1151{
1093 ta->prev = (struct coro *)cctx_ssl_cctx;
1094 ta->next = 0; 1152 ta->prev = 0;
1095} 1153}
1096 1154
1097static int 1155static int
1098slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1156slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1099{ 1157{
1102 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1160 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1103} 1161}
1104 1162
1105/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1163/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1106static void NOINLINE 1164static void NOINLINE
1107cctx_prepare (pTHX_ coro_cctx *cctx) 1165cctx_prepare (pTHX)
1108{ 1166{
1109 PL_top_env = &PL_start_env; 1167 PL_top_env = &PL_start_env;
1110 1168
1111 if (cctx->flags & CC_TRACE) 1169 if (cctx_current->flags & CC_TRACE)
1112 PL_runops = runops_trace; 1170 PL_runops = runops_trace;
1113 1171
1114 /* we already must be executing an SLF op, there is no other valid way 1172 /* we already must be executing an SLF op, there is no other valid way
1115 * that can lead to creation of a new cctx */ 1173 * that can lead to creation of a new cctx */
1116 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1174 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1117 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1175 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1118 1176
1119 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1177 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1120 cctx_ssl_cctx = cctx;
1121 cctx_ssl_frame = slf_frame; 1178 cctx_ssl_frame = slf_frame;
1122 1179
1123 slf_frame.prepare = slf_prepare_set_stacklevel; 1180 slf_frame.prepare = slf_prepare_set_stacklevel;
1124 slf_frame.check = slf_check_set_stacklevel; 1181 slf_frame.check = slf_check_set_stacklevel;
1125} 1182}
1148 /* normally we would need to skip the entersub here */ 1205 /* normally we would need to skip the entersub here */
1149 /* not doing so will re-execute it, which is exactly what we want */ 1206 /* not doing so will re-execute it, which is exactly what we want */
1150 /* PL_nop = PL_nop->op_next */ 1207 /* PL_nop = PL_nop->op_next */
1151 1208
1152 /* inject a fake subroutine call to cctx_init */ 1209 /* inject a fake subroutine call to cctx_init */
1153 cctx_prepare (aTHX_ (coro_cctx *)arg); 1210 cctx_prepare (aTHX);
1154 1211
1155 /* cctx_run is the alternative tail of transfer() */ 1212 /* cctx_run is the alternative tail of transfer() */
1156 transfer_tail (aTHX); 1213 transfer_tail (aTHX);
1157 1214
1158 /* somebody or something will hit me for both perl_run and PL_restartop */ 1215 /* somebody or something will hit me for both perl_run and PL_restartop */
1166 /* 1223 /*
1167 * If perl-run returns we assume exit() was being called or the coro 1224 * If perl-run returns we assume exit() was being called or the coro
1168 * fell off the end, which seems to be the only valid (non-bug) 1225 * fell off the end, which seems to be the only valid (non-bug)
1169 * reason for perl_run to return. We try to exit by jumping to the 1226 * reason for perl_run to return. We try to exit by jumping to the
1170 * bootstrap-time "top" top_env, as we cannot restore the "main" 1227 * bootstrap-time "top" top_env, as we cannot restore the "main"
1171 * coroutine as Coro has no such concept 1228 * coroutine as Coro has no such concept.
1229 * This actually isn't valid with the pthread backend, but OSes requiring
1230 * that backend are too broken to do it in a standards-compliant way.
1172 */ 1231 */
1173 PL_top_env = main_top_env; 1232 PL_top_env = main_top_env;
1174 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1233 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1175 } 1234 }
1176} 1235}
1253cctx_destroy (coro_cctx *cctx) 1312cctx_destroy (coro_cctx *cctx)
1254{ 1313{
1255 if (!cctx) 1314 if (!cctx)
1256 return; 1315 return;
1257 1316
1317 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1318
1258 --cctx_count; 1319 --cctx_count;
1259 coro_destroy (&cctx->cctx); 1320 coro_destroy (&cctx->cctx);
1260 1321
1261 /* coro_transfer creates new, empty cctx's */ 1322 /* coro_transfer creates new, empty cctx's */
1262 if (cctx->sptr) 1323 if (cctx->sptr)
1325 /* TODO: throwing up here is considered harmful */ 1386 /* TODO: throwing up here is considered harmful */
1326 1387
1327 if (expect_true (prev != next)) 1388 if (expect_true (prev != next))
1328 { 1389 {
1329 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1390 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1330 croak ("Coro::State::transfer called with non-running/new 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,");
1331 1392
1332 if (expect_false (next->flags & CF_RUNNING))
1333 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1334
1335 if (expect_false (next->flags & CF_DESTROYED)) 1393 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1336 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,");
1337 1395
1338#if !PERL_VERSION_ATLEAST (5,10,0) 1396#if !PERL_VERSION_ATLEAST (5,10,0)
1339 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1397 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1340 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,");
1341#endif 1399#endif
1347transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1405transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1348{ 1406{
1349 dSTACKLEVEL; 1407 dSTACKLEVEL;
1350 1408
1351 /* sometimes transfer is only called to set idle_sp */ 1409 /* sometimes transfer is only called to set idle_sp */
1352 if (expect_false (!next)) 1410 if (expect_false (!prev))
1353 { 1411 {
1354 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1412 cctx_current->idle_sp = STACKLEVEL;
1355 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1413 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1356 } 1414 }
1357 else if (expect_true (prev != next)) 1415 else if (expect_true (prev != next))
1358 { 1416 {
1359 coro_cctx *prev__cctx; 1417 coro_cctx *cctx_prev;
1360 1418
1361 if (expect_false (prev->flags & CF_NEW)) 1419 if (expect_false (prev->flags & CF_NEW))
1362 { 1420 {
1363 /* create a new empty/source context */ 1421 /* create a new empty/source context */
1364 prev->cctx = cctx_new_empty ();
1365 prev->flags &= ~CF_NEW; 1422 prev->flags &= ~CF_NEW;
1366 prev->flags |= CF_RUNNING; 1423 prev->flags |= CF_RUNNING;
1367 } 1424 }
1368 1425
1369 prev->flags &= ~CF_RUNNING; 1426 prev->flags &= ~CF_RUNNING;
1380 coro_setup (aTHX_ next); 1437 coro_setup (aTHX_ next);
1381 } 1438 }
1382 else 1439 else
1383 load_perl (aTHX_ next); 1440 load_perl (aTHX_ next);
1384 1441
1385 prev__cctx = prev->cctx;
1386
1387 /* possibly untie and reuse the cctx */ 1442 /* possibly untie and reuse the cctx */
1388 if (expect_true ( 1443 if (expect_true (
1389 prev__cctx->idle_sp == STACKLEVEL 1444 cctx_current->idle_sp == STACKLEVEL
1390 && !(prev__cctx->flags & CC_TRACE) 1445 && !(cctx_current->flags & CC_TRACE)
1391 && !force_cctx 1446 && !force_cctx
1392 )) 1447 ))
1393 { 1448 {
1394 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1449 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1395 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); 1450 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1396 1451
1397 prev->cctx = 0;
1398
1399 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1452 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1400 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1453 /* without this the next cctx_get might destroy the running cctx while still in use */
1401 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1454 if (expect_false (CCTX_EXPIRED (cctx_current)))
1402 if (!next->cctx) 1455 if (expect_true (!next->cctx))
1403 next->cctx = cctx_get (aTHX); 1456 next->cctx = cctx_get (aTHX);
1404 1457
1405 cctx_put (prev__cctx); 1458 cctx_put (cctx_current);
1406 } 1459 }
1460 else
1461 prev->cctx = cctx_current;
1407 1462
1408 ++next->usecount; 1463 ++next->usecount;
1409 1464
1410 if (expect_true (!next->cctx)) 1465 cctx_prev = cctx_current;
1411 next->cctx = cctx_get (aTHX); 1466 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1412 1467
1413 if (expect_false (prev__cctx != next->cctx)) 1468 next->cctx = 0;
1469
1470 if (expect_false (cctx_prev != cctx_current))
1414 { 1471 {
1415 prev__cctx->top_env = PL_top_env; 1472 cctx_prev->top_env = PL_top_env;
1416 PL_top_env = next->cctx->top_env; 1473 PL_top_env = cctx_current->top_env;
1417 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1474 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1418 } 1475 }
1419 1476
1420 transfer_tail (aTHX); 1477 transfer_tail (aTHX);
1421 } 1478 }
1422} 1479}
1430coro_state_destroy (pTHX_ struct coro *coro) 1487coro_state_destroy (pTHX_ struct coro *coro)
1431{ 1488{
1432 if (coro->flags & CF_DESTROYED) 1489 if (coro->flags & CF_DESTROYED)
1433 return 0; 1490 return 0;
1434 1491
1435 if (coro->on_destroy) 1492 if (coro->on_destroy && !PL_dirty)
1436 coro->on_destroy (aTHX_ coro); 1493 coro->on_destroy (aTHX_ coro);
1437 1494
1438 coro->flags |= CF_DESTROYED; 1495 coro->flags |= CF_DESTROYED;
1439 1496
1440 if (coro->flags & CF_READY) 1497 if (coro->flags & CF_READY)
1444 --coro_nready; 1501 --coro_nready;
1445 } 1502 }
1446 else 1503 else
1447 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 */
1448 1505
1449 if (coro->mainstack && coro->mainstack != main_mainstack) 1506 if (coro->mainstack
1507 && coro->mainstack != main_mainstack
1508 && coro->slot
1509 && !PL_dirty)
1450 { 1510 {
1451 struct coro temp; 1511 struct coro *current = SvSTATE_current;
1452 1512
1453 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));
1454 1514
1455 save_perl (aTHX_ &temp); 1515 save_perl (aTHX_ current);
1456 load_perl (aTHX_ coro); 1516 load_perl (aTHX_ coro);
1457 1517
1458 coro_destruct (aTHX_ coro); 1518 coro_destruct_perl (aTHX_ coro);
1459 1519
1460 load_perl (aTHX_ &temp); 1520 load_perl (aTHX_ current);
1461 1521
1462 coro->slot = 0; 1522 coro->slot = 0;
1463 } 1523 }
1464 1524
1465 cctx_destroy (coro->cctx); 1525 cctx_destroy (coro->cctx);
1555/** Coro ********************************************************************/ 1615/** Coro ********************************************************************/
1556 1616
1557INLINE void 1617INLINE void
1558coro_enq (pTHX_ struct coro *coro) 1618coro_enq (pTHX_ struct coro *coro)
1559{ 1619{
1560 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); 1620 struct coro **ready = coro_ready [coro->prio - PRIO_MIN];
1561}
1562 1621
1563INLINE 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 *
1564coro_deq (pTHX) 1630coro_deq (pTHX)
1565{ 1631{
1566 int prio; 1632 int prio;
1567 1633
1568 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1634 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1569 if (AvFILLp (coro_ready [prio]) >= 0) 1635 {
1570 return av_shift (coro_ready [prio]); 1636 struct coro **ready = coro_ready [prio];
1571 1637
1572 return 0; 1638 if (ready [0])
1639 {
1640 struct coro *coro = ready [0];
1641 ready [0] = coro->next_ready;
1642 return coro;
1643 }
1644 }
1573} 1645}
1574 1646
1575static int 1647static int
1576api_ready (pTHX_ SV *coro_sv) 1648api_ready (pTHX_ SV *coro_sv)
1577{ 1649{
1578 struct coro *coro; 1650 struct coro *coro;
1579 SV *sv_hook; 1651 SV *sv_hook;
1580 void (*xs_hook)(void); 1652 void (*xs_hook)(void);
1581 1653
1582 if (SvROK (coro_sv))
1583 coro_sv = SvRV (coro_sv);
1584
1585 coro = SvSTATE (coro_sv); 1654 coro = SvSTATE (coro_sv);
1586 1655
1587 if (coro->flags & CF_READY) 1656 if (coro->flags & CF_READY)
1588 return 0; 1657 return 0;
1589 1658
1642static void 1711static void
1643prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1712prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1644{ 1713{
1645 for (;;) 1714 for (;;)
1646 { 1715 {
1647 SV *next_sv = coro_deq (aTHX); 1716 struct coro *next = coro_deq (aTHX);
1648 1717
1649 if (expect_true (next_sv)) 1718 if (expect_true (next))
1650 { 1719 {
1651 struct coro *next = SvSTATE_hv (next_sv);
1652
1653 /* cannot transfer to destroyed coros, skip and look for next */ 1720 /* cannot transfer to destroyed coros, skip and look for next */
1654 if (expect_false (next->flags & CF_DESTROYED)) 1721 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1655 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1722 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1656 else 1723 else
1657 { 1724 {
1658 next->flags &= ~CF_READY; 1725 next->flags &= ~CF_READY;
1659 --coro_nready; 1726 --coro_nready;
1660 1727
1663 } 1730 }
1664 } 1731 }
1665 else 1732 else
1666 { 1733 {
1667 /* nothing to schedule: call the idle handler */ 1734 /* nothing to schedule: call the idle handler */
1735 if (SvROK (sv_idle)
1736 && SvOBJECT (SvRV (sv_idle)))
1737 {
1738 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1739 api_ready (aTHX_ SvRV (sv_idle));
1740 --coro_nready;
1741 }
1742 else
1743 {
1668 dSP; 1744 dSP;
1669 1745
1670 ENTER; 1746 ENTER;
1671 SAVETMPS; 1747 SAVETMPS;
1672 1748
1673 PUSHMARK (SP); 1749 PUSHMARK (SP);
1674 PUTBACK; 1750 PUTBACK;
1675 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1751 call_sv (sv_idle, G_VOID | G_DISCARD);
1676 1752
1677 FREETMPS; 1753 FREETMPS;
1678 LEAVE; 1754 LEAVE;
1755 }
1679 } 1756 }
1680 } 1757 }
1681} 1758}
1682 1759
1683INLINE void 1760INLINE void
1754static void 1831static void
1755api_trace (pTHX_ SV *coro_sv, int flags) 1832api_trace (pTHX_ SV *coro_sv, int flags)
1756{ 1833{
1757 struct coro *coro = SvSTATE (coro_sv); 1834 struct coro *coro = SvSTATE (coro_sv);
1758 1835
1836 if (coro->flags & CF_RUNNING)
1837 croak ("cannot enable tracing on a running coroutine, caught");
1838
1759 if (flags & CC_TRACE) 1839 if (flags & CC_TRACE)
1760 { 1840 {
1761 if (!coro->cctx) 1841 if (!coro->cctx)
1762 coro->cctx = cctx_new_run (); 1842 coro->cctx = cctx_new_run ();
1763 else if (!(coro->cctx->flags & CC_TRACE)) 1843 else if (!(coro->cctx->flags & CC_TRACE))
1764 croak ("cannot enable tracing on coroutine with custom stack,"); 1844 croak ("cannot enable tracing on coroutine with custom stack, caught");
1765 1845
1766 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1846 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1767 } 1847 }
1768 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1848 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1769 { 1849 {
1825 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 1905 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1826 api_ready (aTHX_ sv_manager); 1906 api_ready (aTHX_ sv_manager);
1827 1907
1828 frame->prepare = prepare_schedule; 1908 frame->prepare = prepare_schedule;
1829 frame->check = slf_check_repeat; 1909 frame->check = slf_check_repeat;
1910
1911 /* as a minor optimisation, we could unwind all stacks here */
1912 /* but that puts extra pressure on pp_slf, and is not worth much */
1913 /*coro_unwind_stacks (aTHX);*/
1830} 1914}
1831 1915
1832/*****************************************************************************/ 1916/*****************************************************************************/
1833/* async pool handler */ 1917/* async pool handler */
1834 1918
1917 SV *data = (SV *)GENSUB_ARG; 2001 SV *data = (SV *)GENSUB_ARG;
1918 2002
1919 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2003 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1920 { 2004 {
1921 /* first call, set args */ 2005 /* first call, set args */
2006 SV *coro = SvRV (data);
1922 AV *av = newAV (); 2007 AV *av = newAV ();
1923 SV *coro = SvRV (data);
1924 2008
1925 SvRV_set (data, (SV *)av); 2009 SvRV_set (data, (SV *)av);
1926 api_ready (aTHX_ coro);
1927 SvREFCNT_dec (coro);
1928 2010
1929 /* better take a full copy of the arguments */ 2011 /* better take a full copy of the arguments */
1930 while (items--) 2012 while (items--)
1931 av_store (av, items, newSVsv (ST (items))); 2013 av_store (av, items, newSVsv (ST (items)));
2014
2015 api_ready (aTHX_ coro);
2016 SvREFCNT_dec (coro);
1932 } 2017 }
1933 2018
1934 XSRETURN_EMPTY; 2019 XSRETURN_EMPTY;
1935} 2020}
1936 2021
1953 2038
1954 EXTEND (SP, AvFILLp (av) + 1); 2039 EXTEND (SP, AvFILLp (av) + 1);
1955 for (i = 0; i <= AvFILLp (av); ++i) 2040 for (i = 0; i <= AvFILLp (av); ++i)
1956 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2041 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1957 2042
1958 /* we have stolen the elements, so ste length to zero and free */ 2043 /* we have stolen the elements, so set length to zero and free */
1959 AvFILLp (av) = -1; 2044 AvFILLp (av) = -1;
1960 av_undef (av); 2045 av_undef (av);
1961 2046
1962 PUTBACK; 2047 PUTBACK;
1963 } 2048 }
2253 2338
2254 PL_op->op_ppaddr = pp_slf; 2339 PL_op->op_ppaddr = pp_slf;
2255 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2340 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2256 2341
2257 PL_op = (OP *)&slf_restore; 2342 PL_op = (OP *)&slf_restore;
2343}
2344
2345/*****************************************************************************/
2346/* dynamic wind */
2347
2348static void
2349on_enterleave_call (pTHX_ SV *cb)
2350{
2351 dSP;
2352
2353 PUSHSTACK;
2354
2355 PUSHMARK (SP);
2356 PUTBACK;
2357 call_sv (cb, G_VOID | G_DISCARD);
2358 SPAGAIN;
2359
2360 POPSTACK;
2361}
2362
2363static SV *
2364coro_avp_pop_and_free (pTHX_ AV **avp)
2365{
2366 AV *av = *avp;
2367 SV *res = av_pop (av);
2368
2369 if (AvFILLp (av) < 0)
2370 {
2371 *avp = 0;
2372 SvREFCNT_dec (av);
2373 }
2374
2375 return res;
2376}
2377
2378static void
2379coro_pop_on_enter (pTHX_ void *coro)
2380{
2381 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2382 SvREFCNT_dec (cb);
2383}
2384
2385static void
2386coro_pop_on_leave (pTHX_ void *coro)
2387{
2388 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2389 on_enterleave_call (aTHX_ sv_2mortal (cb));
2258} 2390}
2259 2391
2260/*****************************************************************************/ 2392/*****************************************************************************/
2261/* PerlIO::cede */ 2393/* PerlIO::cede */
2262 2394
2343 SV **ary; 2475 SV **ary;
2344 2476
2345 /* unfortunately, building manually saves memory */ 2477 /* unfortunately, building manually saves memory */
2346 Newx (ary, 2, SV *); 2478 Newx (ary, 2, SV *);
2347 AvALLOC (av) = ary; 2479 AvALLOC (av) = ary;
2480#if PERL_VERSION_ATLEAST (5,10,0)
2348 /*AvARRAY (av) = ary;*/ 2481 AvARRAY (av) = ary;
2482#else
2349 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2483 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2484 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2485 SvPVX ((SV *)av) = (char *)ary;
2486#endif
2350 AvMAX (av) = 1; 2487 AvMAX (av) = 1;
2351 AvFILLp (av) = 0; 2488 AvFILLp (av) = 0;
2352 ary [0] = newSViv (count); 2489 ary [0] = newSViv (count);
2353 2490
2354 return newRV_noinc ((SV *)av); 2491 return newRV_noinc ((SV *)av);
2724 XSRETURN_EMPTY; 2861 XSRETURN_EMPTY;
2725} 2862}
2726 2863
2727/*****************************************************************************/ 2864/*****************************************************************************/
2728 2865
2866#if CORO_CLONE
2867# include "clone.c"
2868#endif
2869
2729MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2870MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2730 2871
2731PROTOTYPES: DISABLE 2872PROTOTYPES: DISABLE
2732 2873
2733BOOT: 2874BOOT:
2736# if CORO_PTHREAD 2877# if CORO_PTHREAD
2737 coro_thx = PERL_GET_CONTEXT; 2878 coro_thx = PERL_GET_CONTEXT;
2738# endif 2879# endif
2739#endif 2880#endif
2740 BOOT_PAGESIZE; 2881 BOOT_PAGESIZE;
2882
2883 cctx_current = cctx_new_empty ();
2741 2884
2742 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2885 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2743 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2886 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2744 2887
2745 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2888 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2871void 3014void
2872_exit (int code) 3015_exit (int code)
2873 PROTOTYPE: $ 3016 PROTOTYPE: $
2874 CODE: 3017 CODE:
2875 _exit (code); 3018 _exit (code);
3019
3020SV *
3021clone (Coro::State coro)
3022 CODE:
3023{
3024#if CORO_CLONE
3025 struct coro *ncoro = coro_clone (aTHX_ coro);
3026 MAGIC *mg;
3027 /* TODO: too much duplication */
3028 ncoro->hv = newHV ();
3029 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
3030 mg->mg_flags |= MGf_DUP;
3031 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
3032#else
3033 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
3034#endif
3035}
3036 OUTPUT:
3037 RETVAL
2876 3038
2877int 3039int
2878cctx_stacksize (int new_stacksize = 0) 3040cctx_stacksize (int new_stacksize = 0)
2879 PROTOTYPE: ;$ 3041 PROTOTYPE: ;$
2880 CODE: 3042 CODE:
2930 eval = 1 3092 eval = 1
2931 CODE: 3093 CODE:
2932{ 3094{
2933 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3095 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2934 { 3096 {
2935 struct coro temp; 3097 struct coro *current = SvSTATE_current;
2936 3098
2937 if (!(coro->flags & CF_RUNNING)) 3099 if (current != coro)
2938 { 3100 {
2939 PUTBACK; 3101 PUTBACK;
2940 save_perl (aTHX_ &temp); 3102 save_perl (aTHX_ current);
2941 load_perl (aTHX_ coro); 3103 load_perl (aTHX_ coro);
3104 SPAGAIN;
2942 } 3105 }
2943 3106
2944 {
2945 dSP;
2946 ENTER;
2947 SAVETMPS;
2948 PUTBACK;
2949 PUSHSTACK; 3107 PUSHSTACK;
3108
2950 PUSHMARK (SP); 3109 PUSHMARK (SP);
3110 PUTBACK;
2951 3111
2952 if (ix) 3112 if (ix)
2953 eval_sv (coderef, 0); 3113 eval_sv (coderef, 0);
2954 else 3114 else
2955 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3115 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2956 3116
2957 POPSTACK; 3117 POPSTACK;
2958 SPAGAIN; 3118 SPAGAIN;
2959 FREETMPS;
2960 LEAVE;
2961 PUTBACK;
2962 }
2963 3119
2964 if (!(coro->flags & CF_RUNNING)) 3120 if (current != coro)
2965 { 3121 {
3122 PUTBACK;
2966 save_perl (aTHX_ coro); 3123 save_perl (aTHX_ coro);
2967 load_perl (aTHX_ &temp); 3124 load_perl (aTHX_ current);
2968 SPAGAIN; 3125 SPAGAIN;
2969 } 3126 }
2970 } 3127 }
2971} 3128}
2972 3129
2976 ALIAS: 3133 ALIAS:
2977 is_ready = CF_READY 3134 is_ready = CF_READY
2978 is_running = CF_RUNNING 3135 is_running = CF_RUNNING
2979 is_new = CF_NEW 3136 is_new = CF_NEW
2980 is_destroyed = CF_DESTROYED 3137 is_destroyed = CF_DESTROYED
3138 is_suspended = CF_SUSPENDED
2981 CODE: 3139 CODE:
2982 RETVAL = boolSV (coro->flags & ix); 3140 RETVAL = boolSV (coro->flags & ix);
2983 OUTPUT: 3141 OUTPUT:
2984 RETVAL 3142 RETVAL
2985 3143
3001 3159
3002SV * 3160SV *
3003has_cctx (Coro::State coro) 3161has_cctx (Coro::State coro)
3004 PROTOTYPE: $ 3162 PROTOTYPE: $
3005 CODE: 3163 CODE:
3006 RETVAL = boolSV (!!coro->cctx); 3164 /* maybe manage the running flag differently */
3165 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
3007 OUTPUT: 3166 OUTPUT:
3008 RETVAL 3167 RETVAL
3009 3168
3010int 3169int
3011is_traced (Coro::State coro) 3170is_traced (Coro::State coro)
3031 3190
3032void 3191void
3033force_cctx () 3192force_cctx ()
3034 PROTOTYPE: 3193 PROTOTYPE:
3035 CODE: 3194 CODE:
3036 SvSTATE_current->cctx->idle_sp = 0; 3195 cctx_current->idle_sp = 0;
3037 3196
3038void 3197void
3039swap_defsv (Coro::State self) 3198swap_defsv (Coro::State self)
3040 PROTOTYPE: $ 3199 PROTOTYPE: $
3041 ALIAS: 3200 ALIAS:
3049 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3208 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3050 3209
3051 SV *tmp = *src; *src = *dst; *dst = tmp; 3210 SV *tmp = *src; *src = *dst; *dst = tmp;
3052 } 3211 }
3053 3212
3213void
3214cancel (Coro::State self)
3215 CODE:
3216 coro_state_destroy (aTHX_ self);
3217 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3218
3054 3219
3055MODULE = Coro::State PACKAGE = Coro 3220MODULE = Coro::State PACKAGE = Coro
3056 3221
3057BOOT: 3222BOOT:
3058{ 3223{
3059 int i; 3224 int i;
3060 3225
3061 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3226 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3062 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3227 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3063 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3228 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3064 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3229 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3065 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3230 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3066 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3231 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3067 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3232 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3068 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3233 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3234 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3069 3235
3070 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3236 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3071 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3237 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3072 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3238 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3073 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3239 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
3078 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3244 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
3079 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3245 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
3080 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3246 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
3081 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3247 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
3082 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3248 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
3083
3084 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3085 coro_ready[i] = newAV ();
3086 3249
3087 { 3250 {
3088 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3251 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3089 3252
3090 coroapi.schedule = api_schedule; 3253 coroapi.schedule = api_schedule;
3131cede_notself (...) 3294cede_notself (...)
3132 CODE: 3295 CODE:
3133 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3296 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3134 3297
3135void 3298void
3136_cancel (Coro::State self)
3137 CODE:
3138 coro_state_destroy (aTHX_ self);
3139 coro_call_on_destroy (aTHX_ self);
3140
3141void
3142_set_current (SV *current) 3299_set_current (SV *current)
3143 PROTOTYPE: $ 3300 PROTOTYPE: $
3144 CODE: 3301 CODE:
3145 SvREFCNT_dec (SvRV (coro_current)); 3302 SvREFCNT_dec (SvRV (coro_current));
3146 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3303 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3188 PROTOTYPE: 3345 PROTOTYPE:
3189 CODE: 3346 CODE:
3190 RETVAL = coro_nready; 3347 RETVAL = coro_nready;
3191 OUTPUT: 3348 OUTPUT:
3192 RETVAL 3349 RETVAL
3350
3351void
3352suspend (Coro::State self)
3353 PROTOTYPE: $
3354 CODE:
3355 self->flags |= CF_SUSPENDED;
3356
3357void
3358resume (Coro::State self)
3359 PROTOTYPE: $
3360 CODE:
3361 self->flags &= ~CF_SUSPENDED;
3193 3362
3194void 3363void
3195_pool_handler (...) 3364_pool_handler (...)
3196 CODE: 3365 CODE:
3197 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3366 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3252rouse_wait (...) 3421rouse_wait (...)
3253 PROTOTYPE: ;$ 3422 PROTOTYPE: ;$
3254 PPCODE: 3423 PPCODE:
3255 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3424 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3256 3425
3426void
3427on_enter (SV *block)
3428 ALIAS:
3429 on_leave = 1
3430 PROTOTYPE: &
3431 CODE:
3432{
3433 struct coro *coro = SvSTATE_current;
3434 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3435
3436 block = (SV *)coro_sv_2cv (aTHX_ block);
3437
3438 if (!*avp)
3439 *avp = newAV ();
3440
3441 av_push (*avp, SvREFCNT_inc (block));
3442
3443 if (!ix)
3444 on_enterleave_call (aTHX_ block);
3445
3446 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3447 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3448 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3449}
3450
3257 3451
3258MODULE = Coro::State PACKAGE = PerlIO::cede 3452MODULE = Coro::State PACKAGE = PerlIO::cede
3259 3453
3260BOOT: 3454BOOT:
3261 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3455 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3271 GvSTASH (CvGV (cv)) 3465 GvSTASH (CvGV (cv))
3272 ); 3466 );
3273 OUTPUT: 3467 OUTPUT:
3274 RETVAL 3468 RETVAL
3275 3469
3276# helper for Coro::Channel 3470# helper for Coro::Channel and others
3277SV * 3471SV *
3278_alloc (int count) 3472_alloc (int count)
3279 CODE: 3473 CODE:
3280 RETVAL = coro_waitarray_new (aTHX_ count); 3474 RETVAL = coro_waitarray_new (aTHX_ count);
3281 OUTPUT: 3475 OUTPUT:
3339 for (i = 1; i <= wcount; ++i) 3533 for (i = 1; i <= wcount; ++i)
3340 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3534 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3341 } 3535 }
3342} 3536}
3343 3537
3538MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3539
3540void
3541_may_delete (SV *sem, int count, int extra_refs)
3542 PPCODE:
3543{
3544 AV *av = (AV *)SvRV (sem);
3545
3546 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3547 && AvFILLp (av) == 0 /* no waiters, just count */
3548 && SvIV (AvARRAY (av)[0]) == count)
3549 XSRETURN_YES;
3550
3551 XSRETURN_NO;
3552}
3553
3344MODULE = Coro::State PACKAGE = Coro::Signal 3554MODULE = Coro::State PACKAGE = Coro::Signal
3345 3555
3346SV * 3556SV *
3347new (SV *klass) 3557new (SV *klass)
3348 CODE: 3558 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines