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.321 by root, Sat Nov 22 02:09:54 2008 UTC vs.
Revision 1.351 by root, Sat Jun 20 08:58:25 2009 UTC

1#define CORO_CLONE 1 //D
2
3#include "libcoro/coro.c" 1#include "libcoro/coro.c"
4 2
5#define PERL_NO_GET_CONTEXT 3#define PERL_NO_GET_CONTEXT
6#define PERL_EXT 4#define PERL_EXT
7 5
135#else 133#else
136# define dSTACKLEVEL volatile void *stacklevel 134# define dSTACKLEVEL volatile void *stacklevel
137# define STACKLEVEL ((void *)&stacklevel) 135# define STACKLEVEL ((void *)&stacklevel)
138#endif 136#endif
139 137
140#define IN_DESTRUCT (PL_main_cv == Nullcv) 138#define IN_DESTRUCT PL_dirty
141 139
142#if __GNUC__ >= 3 140#if __GNUC__ >= 3
143# define attribute(x) __attribute__(x) 141# define attribute(x) __attribute__(x)
144# define expect(expr,value) __builtin_expect ((expr),(value)) 142# define expect(expr,value) __builtin_expect ((expr),(value))
145# define INLINE static inline 143# define INLINE static inline
177static HV *coro_state_stash, *coro_stash; 175static HV *coro_state_stash, *coro_stash;
178static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
179 177
180static AV *av_destroy; /* destruction queue */ 178static AV *av_destroy; /* destruction queue */
181static SV *sv_manager; /* the manager coro */ 179static SV *sv_manager; /* the manager coro */
180static SV *sv_idle; /* $Coro::idle */
182 181
183static GV *irsgv; /* $/ */ 182static GV *irsgv; /* $/ */
184static GV *stdoutgv; /* *STDOUT */ 183static GV *stdoutgv; /* *STDOUT */
185static SV *rv_diehook; 184static SV *rv_diehook;
186static SV *rv_warnhook; 185static SV *rv_warnhook;
230 int valgrind_id; 229 int valgrind_id;
231#endif 230#endif
232 unsigned char flags; 231 unsigned char flags;
233} coro_cctx; 232} coro_cctx;
234 233
234coro_cctx *cctx_current; /* the currently running cctx */
235
236/*****************************************************************************/
237
235enum { 238enum {
236 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
237 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
238 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
239 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 */
240}; 244};
241 245
242/* 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 */
243typedef struct 247typedef struct
244{ 248{
245 SV *defsv; 249 SV *defsv;
246 AV *defav; 250 AV *defav;
247 SV *errsv; 251 SV *errsv;
248 SV *irsgv; 252 SV *irsgv;
253 HV *hinthv;
249#define VAR(name,type) type name; 254#define VAR(name,type) type name;
250# include "state.h" 255# include "state.h"
251#undef VAR 256#undef VAR
252} perl_slots; 257} perl_slots;
253 258
255 260
256/* this is a structure representing a perl-level coroutine */ 261/* this is a structure representing a perl-level coroutine */
257struct coro { 262struct coro {
258 /* the C coroutine allocated to this perl coroutine, if any */ 263 /* the C coroutine allocated to this perl coroutine, if any */
259 coro_cctx *cctx; 264 coro_cctx *cctx;
265
266 /* ready queue */
267 struct coro *next_ready;
260 268
261 /* state data */ 269 /* state data */
262 struct CoroSLF slf_frame; /* saved slf frame */ 270 struct CoroSLF slf_frame; /* saved slf frame */
263 AV *mainstack; 271 AV *mainstack;
264 perl_slots *slot; /* basically the saved sp */ 272 perl_slots *slot; /* basically the saved sp */
281 /* async_pool */ 289 /* async_pool */
282 SV *saved_deffh; 290 SV *saved_deffh;
283 SV *invoke_cb; 291 SV *invoke_cb;
284 AV *invoke_av; 292 AV *invoke_av;
285 293
294 /* on_enter/on_leave */
295 AV *on_enter;
296 AV *on_leave;
297
286 /* linked list */ 298 /* linked list */
287 struct coro *next, *prev; 299 struct coro *next, *prev;
288}; 300};
289 301
290typedef struct coro *Coro__State; 302typedef struct coro *Coro__State;
305#define PRIO_MIN -4 317#define PRIO_MIN -4
306 318
307/* for Coro.pm */ 319/* for Coro.pm */
308static SV *coro_current; 320static SV *coro_current;
309static SV *coro_readyhook; 321static SV *coro_readyhook;
310static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 322static struct coro *coro_ready [PRIO_MAX - PRIO_MIN + 1][2]; /* head|tail */
311static CV *cv_coro_run, *cv_coro_terminate; 323static CV *cv_coro_run, *cv_coro_terminate;
312static struct coro *coro_first; 324static struct coro *coro_first;
313#define coro_nready coroapi.nready 325#define coro_nready coroapi.nready
314 326
315/** lowlevel stuff **********************************************************/ 327/** lowlevel stuff **********************************************************/
348INLINE CV * 360INLINE CV *
349coro_sv_2cv (pTHX_ SV *sv) 361coro_sv_2cv (pTHX_ SV *sv)
350{ 362{
351 HV *st; 363 HV *st;
352 GV *gvp; 364 GV *gvp;
353 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;
354} 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 */
355 424
356static AV * 425static AV *
357coro_derive_padlist (pTHX_ CV *cv) 426coro_derive_padlist (pTHX_ CV *cv)
358{ 427{
359 AV *padlist = CvPADLIST (cv); 428 AV *padlist = CvPADLIST (cv);
367 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 436 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
368#endif 437#endif
369 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 438 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
370 --AvFILLp (padlist); 439 --AvFILLp (padlist);
371 440
372 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 441 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
373 av_store (newpadlist, 1, (SV *)newpad); 442 av_store (newpadlist, 1, (SV *)newpad);
374 443
375 return newpadlist; 444 return newpadlist;
376} 445}
377 446
378static void 447static void
379free_padlist (pTHX_ AV *padlist) 448free_padlist (pTHX_ AV *padlist)
380{ 449{
381 /* may be during global destruction */ 450 /* may be during global destruction */
382 if (SvREFCNT (padlist)) 451 if (!IN_DESTRUCT)
383 { 452 {
384 I32 i = AvFILLp (padlist); 453 I32 i = AvFILLp (padlist);
385 while (i >= 0) 454
455 while (i > 0) /* special-case index 0 */
386 { 456 {
387 SV **svp = av_fetch (padlist, i--, FALSE); 457 /* we try to be extra-careful here */
388 if (svp) 458 AV *av = (AV *)AvARRAY (padlist)[i--];
389 { 459 I32 j = AvFILLp (av);
390 SV *sv; 460
391 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 461 while (j >= 0)
462 SvREFCNT_dec (AvARRAY (av)[j--]);
463
464 AvFILLp (av) = -1;
392 SvREFCNT_dec (sv); 465 SvREFCNT_dec (av);
393
394 SvREFCNT_dec (*svp);
395 }
396 } 466 }
397 467
468 SvREFCNT_dec (AvARRAY (padlist)[0]);
469
470 AvFILLp (padlist) = -1;
398 SvREFCNT_dec ((SV*)padlist); 471 SvREFCNT_dec ((SV*)padlist);
399 } 472 }
400} 473}
401 474
402static int 475static int
411 484
412 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 485 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
413 486
414 return 0; 487 return 0;
415} 488}
416
417#define CORO_MAGIC_type_cv 26
418#define CORO_MAGIC_type_state PERL_MAGIC_ext
419 489
420static MGVTBL coro_cv_vtbl = { 490static MGVTBL coro_cv_vtbl = {
421 0, 0, 0, 0, 491 0, 0, 0, 0,
422 coro_cv_free 492 coro_cv_free
423}; 493};
424
425#define CORO_MAGIC_NN(sv, type) \
426 (expect_true (SvMAGIC (sv)->mg_type == type) \
427 ? SvMAGIC (sv) \
428 : mg_find (sv, type))
429
430#define CORO_MAGIC(sv, type) \
431 (expect_true (SvMAGIC (sv)) \
432 ? CORO_MAGIC_NN (sv, type) \
433 : 0)
434
435#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
436#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
437
438INLINE struct coro *
439SvSTATE_ (pTHX_ SV *coro)
440{
441 HV *stash;
442 MAGIC *mg;
443
444 if (SvROK (coro))
445 coro = SvRV (coro);
446
447 if (expect_false (SvTYPE (coro) != SVt_PVHV))
448 croak ("Coro::State object required");
449
450 stash = SvSTASH (coro);
451 if (expect_false (stash != coro_stash && stash != coro_state_stash))
452 {
453 /* very slow, but rare, check */
454 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
455 croak ("Coro::State object required");
456 }
457
458 mg = CORO_MAGIC_state (coro);
459 return (struct coro *)mg->mg_ptr;
460}
461
462#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
463
464/* faster than SvSTATE, but expects a coroutine hv */
465#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
466#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
467 494
468/* the next two functions merely cache the padlists */ 495/* the next two functions merely cache the padlists */
469static void 496static void
470get_padlist (pTHX_ CV *cv) 497get_padlist (pTHX_ CV *cv)
471{ 498{
499 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);
500 527
501 av = (AV *)mg->mg_obj; 528 av = (AV *)mg->mg_obj;
502 529
503 if (expect_false (AvFILLp (av) >= AvMAX (av))) 530 if (expect_false (AvFILLp (av) >= AvMAX (av)))
504 av_extend (av, AvMAX (av) + 1); 531 av_extend (av, AvFILLp (av) + 1);
505 532
506 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 533 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
507} 534}
508 535
509/** load & save, init *******************************************************/ 536/** load & save, init *******************************************************/
537
538static void
539on_enterleave_call (pTHX_ SV *cb);
510 540
511static void 541static void
512load_perl (pTHX_ Coro__State c) 542load_perl (pTHX_ Coro__State c)
513{ 543{
514 perl_slots *slot = c->slot; 544 perl_slots *slot = c->slot;
515 c->slot = 0; 545 c->slot = 0;
516 546
517 PL_mainstack = c->mainstack; 547 PL_mainstack = c->mainstack;
518 548
519 GvSV (PL_defgv) = slot->defsv; 549 GvSV (PL_defgv) = slot->defsv;
520 GvAV (PL_defgv) = slot->defav; 550 GvAV (PL_defgv) = slot->defav;
521 GvSV (PL_errgv) = slot->errsv; 551 GvSV (PL_errgv) = slot->errsv;
522 GvSV (irsgv) = slot->irsgv; 552 GvSV (irsgv) = slot->irsgv;
553 GvHV (PL_hintgv) = slot->hinthv;
523 554
524 #define VAR(name,type) PL_ ## name = slot->name; 555 #define VAR(name,type) PL_ ## name = slot->name;
525 # include "state.h" 556 # include "state.h"
526 #undef VAR 557 #undef VAR
527 558
541 PUTBACK; 572 PUTBACK;
542 } 573 }
543 574
544 slf_frame = c->slf_frame; 575 slf_frame = c->slf_frame;
545 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 }
546} 585}
547 586
548static void 587static void
549save_perl (pTHX_ Coro__State c) 588save_perl (pTHX_ Coro__State c)
550{ 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
551 c->except = CORO_THROW; 598 c->except = CORO_THROW;
552 c->slf_frame = slf_frame; 599 c->slf_frame = slf_frame;
553 600
554 { 601 {
555 dSP; 602 dSP;
613 c->mainstack = PL_mainstack; 660 c->mainstack = PL_mainstack;
614 661
615 { 662 {
616 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 663 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
617 664
618 slot->defav = GvAV (PL_defgv); 665 slot->defav = GvAV (PL_defgv);
619 slot->defsv = DEFSV; 666 slot->defsv = DEFSV;
620 slot->errsv = ERRSV; 667 slot->errsv = ERRSV;
621 slot->irsgv = GvSV (irsgv); 668 slot->irsgv = GvSV (irsgv);
669 slot->hinthv = GvHV (PL_hintgv);
622 670
623 #define VAR(name,type) slot->name = PL_ ## name; 671 #define VAR(name,type) slot->name = PL_ ## name;
624 # include "state.h" 672 # include "state.h"
625 #undef VAR 673 #undef VAR
626 } 674 }
753#endif 801#endif
754 802
755/* 803/*
756 * This overrides the default magic get method of %SIG elements. 804 * This overrides the default magic get method of %SIG elements.
757 * 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
758 * 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
759 * readback here. 807 * readback here.
760 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
761 * not expecting this to actually change the hook. This is a potential problem
762 * when a schedule happens then, but we ignore this.
763 */ 808 */
764static int 809static int
765coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 810coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
766{ 811{
767 const char *s = MgPV_nolen_const (mg); 812 const char *s = MgPV_nolen_const (mg);
820 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 865 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
821 866
822 if (svp) 867 if (svp)
823 { 868 {
824 SV *old = *svp; 869 SV *old = *svp;
825 *svp = newSVsv (sv); 870 *svp = SvOK (sv) ? newSVsv (sv) : 0;
826 SvREFCNT_dec (old); 871 SvREFCNT_dec (old);
827 return 0; 872 return 0;
828 } 873 }
829 } 874 }
830 875
862 907
863 PL_runops = RUNOPS_DEFAULT; 908 PL_runops = RUNOPS_DEFAULT;
864 PL_curcop = &PL_compiling; 909 PL_curcop = &PL_compiling;
865 PL_in_eval = EVAL_NULL; 910 PL_in_eval = EVAL_NULL;
866 PL_comppad = 0; 911 PL_comppad = 0;
912 PL_comppad_name = 0;
913 PL_comppad_name_fill = 0;
914 PL_comppad_name_floor = 0;
867 PL_curpm = 0; 915 PL_curpm = 0;
868 PL_curpad = 0; 916 PL_curpad = 0;
869 PL_localizing = 0; 917 PL_localizing = 0;
870 PL_dirty = 0; 918 PL_dirty = 0;
871 PL_restartop = 0; 919 PL_restartop = 0;
872#if PERL_VERSION_ATLEAST (5,10,0) 920#if PERL_VERSION_ATLEAST (5,10,0)
873 PL_parser = 0; 921 PL_parser = 0;
874#endif 922#endif
923 PL_hints = 0;
875 924
876 /* recreate the die/warn hooks */ 925 /* recreate the die/warn hooks */
877 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 );
878 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);
879 928
880 GvSV (PL_defgv) = newSV (0); 929 GvSV (PL_defgv) = newSV (0);
881 GvAV (PL_defgv) = coro->args; coro->args = 0; 930 GvAV (PL_defgv) = coro->args; coro->args = 0;
882 GvSV (PL_errgv) = newSV (0); 931 GvSV (PL_errgv) = newSV (0);
883 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;
884 PL_rs = newSVsv (GvSV (irsgv)); 934 PL_rs = newSVsv (GvSV (irsgv));
885 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 935 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
886 936
887 { 937 {
888 dSP; 938 dSP;
917 /* copy throw, in case it was set before coro_setup */ 967 /* copy throw, in case it was set before coro_setup */
918 CORO_THROW = coro->except; 968 CORO_THROW = coro->except;
919} 969}
920 970
921static void 971static void
922coro_destruct (pTHX_ struct coro *coro) 972coro_unwind_stacks (pTHX)
923{ 973{
924 if (!IN_DESTRUCT) 974 if (!IN_DESTRUCT)
925 { 975 {
926 /* restore all saved variables and stuff */ 976 /* restore all saved variables and stuff */
927 LEAVE_SCOPE (0); 977 LEAVE_SCOPE (0);
935 POPSTACK_TO (PL_mainstack); 985 POPSTACK_TO (PL_mainstack);
936 986
937 /* unwind main stack */ 987 /* unwind main stack */
938 dounwind (-1); 988 dounwind (-1);
939 } 989 }
990}
940 991
941 SvREFCNT_dec (GvSV (PL_defgv)); 992static void
942 SvREFCNT_dec (GvAV (PL_defgv)); 993coro_destruct_perl (pTHX_ struct coro *coro)
943 SvREFCNT_dec (GvSV (PL_errgv)); 994{
944 SvREFCNT_dec (PL_defoutgv); 995 SV *svf [9];
945 SvREFCNT_dec (PL_rs);
946 SvREFCNT_dec (GvSV (irsgv));
947 996
948 SvREFCNT_dec (PL_diehook);
949 SvREFCNT_dec (PL_warnhook);
950 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 coro_unwind_stacks (aTHX);
1005 coro_destruct_stacks (aTHX);
1006
1007 // now save some sv's to be free'd later
1008 svf [0] = GvSV (PL_defgv);
1009 svf [1] = (SV *)GvAV (PL_defgv);
1010 svf [2] = GvSV (PL_errgv);
1011 svf [3] = (SV *)PL_defoutgv;
1012 svf [4] = PL_rs;
1013 svf [5] = GvSV (irsgv);
1014 svf [6] = (SV *)GvHV (PL_hintgv);
1015 svf [7] = PL_diehook;
1016 svf [8] = PL_warnhook;
1017 assert (9 == sizeof (svf) / sizeof (*svf));
1018
1019 load_perl (aTHX_ current);
1020 }
1021
1022 {
1023 int i;
1024
1025 for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i)
1026 SvREFCNT_dec (svf [i]);
1027
951 SvREFCNT_dec (coro->saved_deffh); 1028 SvREFCNT_dec (coro->saved_deffh);
952 SvREFCNT_dec (coro->rouse_cb); 1029 SvREFCNT_dec (coro->rouse_cb);
953 SvREFCNT_dec (coro->invoke_cb); 1030 SvREFCNT_dec (coro->invoke_cb);
954 SvREFCNT_dec (coro->invoke_av); 1031 SvREFCNT_dec (coro->invoke_av);
955 1032 }
956 coro_destruct_stacks (aTHX);
957} 1033}
958 1034
959INLINE void 1035INLINE void
960free_coro_mortal (pTHX) 1036free_coro_mortal (pTHX)
961{ 1037{
969static int 1045static int
970runops_trace (pTHX) 1046runops_trace (pTHX)
971{ 1047{
972 COP *oldcop = 0; 1048 COP *oldcop = 0;
973 int oldcxix = -2; 1049 int oldcxix = -2;
974 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
975 coro_cctx *cctx = coro->cctx;
976 1050
977 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 1051 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
978 { 1052 {
979 PERL_ASYNC_CHECK (); 1053 PERL_ASYNC_CHECK ();
980 1054
981 if (cctx->flags & CC_TRACE_ALL) 1055 if (cctx_current->flags & CC_TRACE_ALL)
982 { 1056 {
983 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 1057 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
984 { 1058 {
985 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1059 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
986 SV **bot, **top; 1060 SV **bot, **top;
987 AV *av = newAV (); /* return values */ 1061 AV *av = newAV (); /* return values */
988 SV **cb; 1062 SV **cb;
1025 1099
1026 if (PL_curcop != &PL_compiling) 1100 if (PL_curcop != &PL_compiling)
1027 { 1101 {
1028 SV **cb; 1102 SV **cb;
1029 1103
1030 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1104 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1031 { 1105 {
1032 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1106 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1033 1107
1034 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1108 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1035 { 1109 {
1058 } 1132 }
1059 1133
1060 oldcxix = cxstack_ix; 1134 oldcxix = cxstack_ix;
1061 } 1135 }
1062 1136
1063 if (cctx->flags & CC_TRACE_LINE) 1137 if (cctx_current->flags & CC_TRACE_LINE)
1064 { 1138 {
1065 dSP; 1139 dSP;
1066 1140
1067 PL_runops = RUNOPS_DEFAULT; 1141 PL_runops = RUNOPS_DEFAULT;
1068 ENTER; 1142 ENTER;
1087 1161
1088 TAINT_NOT; 1162 TAINT_NOT;
1089 return 0; 1163 return 0;
1090} 1164}
1091 1165
1092static struct coro_cctx *cctx_ssl_cctx;
1093static struct CoroSLF cctx_ssl_frame; 1166static struct CoroSLF cctx_ssl_frame;
1094 1167
1095static void 1168static void
1096slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1169slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1097{ 1170{
1098 ta->prev = (struct coro *)cctx_ssl_cctx;
1099 ta->next = 0; 1171 ta->prev = 0;
1100} 1172}
1101 1173
1102static int 1174static int
1103slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1175slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1104{ 1176{
1107 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1179 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1108} 1180}
1109 1181
1110/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1182/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1111static void NOINLINE 1183static void NOINLINE
1112cctx_prepare (pTHX_ coro_cctx *cctx) 1184cctx_prepare (pTHX)
1113{ 1185{
1114 PL_top_env = &PL_start_env; 1186 PL_top_env = &PL_start_env;
1115 1187
1116 if (cctx->flags & CC_TRACE) 1188 if (cctx_current->flags & CC_TRACE)
1117 PL_runops = runops_trace; 1189 PL_runops = runops_trace;
1118 1190
1119 /* we already must be executing an SLF op, there is no other valid way 1191 /* we already must be executing an SLF op, there is no other valid way
1120 * that can lead to creation of a new cctx */ 1192 * that can lead to creation of a new cctx */
1121 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1193 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1122 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1194 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1123 1195
1124 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1196 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1125 cctx_ssl_cctx = cctx;
1126 cctx_ssl_frame = slf_frame; 1197 cctx_ssl_frame = slf_frame;
1127 1198
1128 slf_frame.prepare = slf_prepare_set_stacklevel; 1199 slf_frame.prepare = slf_prepare_set_stacklevel;
1129 slf_frame.check = slf_check_set_stacklevel; 1200 slf_frame.check = slf_check_set_stacklevel;
1130} 1201}
1153 /* normally we would need to skip the entersub here */ 1224 /* normally we would need to skip the entersub here */
1154 /* not doing so will re-execute it, which is exactly what we want */ 1225 /* not doing so will re-execute it, which is exactly what we want */
1155 /* PL_nop = PL_nop->op_next */ 1226 /* PL_nop = PL_nop->op_next */
1156 1227
1157 /* inject a fake subroutine call to cctx_init */ 1228 /* inject a fake subroutine call to cctx_init */
1158 cctx_prepare (aTHX_ (coro_cctx *)arg); 1229 cctx_prepare (aTHX);
1159 1230
1160 /* cctx_run is the alternative tail of transfer() */ 1231 /* cctx_run is the alternative tail of transfer() */
1161 transfer_tail (aTHX); 1232 transfer_tail (aTHX);
1162 1233
1163 /* somebody or something will hit me for both perl_run and PL_restartop */ 1234 /* somebody or something will hit me for both perl_run and PL_restartop */
1171 /* 1242 /*
1172 * If perl-run returns we assume exit() was being called or the coro 1243 * If perl-run returns we assume exit() was being called or the coro
1173 * fell off the end, which seems to be the only valid (non-bug) 1244 * fell off the end, which seems to be the only valid (non-bug)
1174 * reason for perl_run to return. We try to exit by jumping to the 1245 * reason for perl_run to return. We try to exit by jumping to the
1175 * bootstrap-time "top" top_env, as we cannot restore the "main" 1246 * bootstrap-time "top" top_env, as we cannot restore the "main"
1176 * coroutine as Coro has no such concept 1247 * coroutine as Coro has no such concept.
1248 * This actually isn't valid with the pthread backend, but OSes requiring
1249 * that backend are too broken to do it in a standards-compliant way.
1177 */ 1250 */
1178 PL_top_env = main_top_env; 1251 PL_top_env = main_top_env;
1179 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1252 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1180 } 1253 }
1181} 1254}
1258cctx_destroy (coro_cctx *cctx) 1331cctx_destroy (coro_cctx *cctx)
1259{ 1332{
1260 if (!cctx) 1333 if (!cctx)
1261 return; 1334 return;
1262 1335
1336 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1337
1263 --cctx_count; 1338 --cctx_count;
1264 coro_destroy (&cctx->cctx); 1339 coro_destroy (&cctx->cctx);
1265 1340
1266 /* coro_transfer creates new, empty cctx's */ 1341 /* coro_transfer creates new, empty cctx's */
1267 if (cctx->sptr) 1342 if (cctx->sptr)
1330 /* TODO: throwing up here is considered harmful */ 1405 /* TODO: throwing up here is considered harmful */
1331 1406
1332 if (expect_true (prev != next)) 1407 if (expect_true (prev != next))
1333 { 1408 {
1334 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1409 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1335 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); 1410 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1336 1411
1337 if (expect_false (next->flags & CF_RUNNING))
1338 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1339
1340 if (expect_false (next->flags & CF_DESTROYED)) 1412 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1341 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); 1413 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1342 1414
1343#if !PERL_VERSION_ATLEAST (5,10,0) 1415#if !PERL_VERSION_ATLEAST (5,10,0)
1344 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1416 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1345 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); 1417 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1346#endif 1418#endif
1352transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1424transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1353{ 1425{
1354 dSTACKLEVEL; 1426 dSTACKLEVEL;
1355 1427
1356 /* sometimes transfer is only called to set idle_sp */ 1428 /* sometimes transfer is only called to set idle_sp */
1357 if (expect_false (!next)) 1429 if (expect_false (!prev))
1358 { 1430 {
1359 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1431 cctx_current->idle_sp = STACKLEVEL;
1360 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1432 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1361 } 1433 }
1362 else if (expect_true (prev != next)) 1434 else if (expect_true (prev != next))
1363 { 1435 {
1364 coro_cctx *prev__cctx; 1436 coro_cctx *cctx_prev;
1365 1437
1366 if (expect_false (prev->flags & CF_NEW)) 1438 if (expect_false (prev->flags & CF_NEW))
1367 { 1439 {
1368 /* create a new empty/source context */ 1440 /* create a new empty/source context */
1369 prev->cctx = cctx_new_empty ();
1370 prev->flags &= ~CF_NEW; 1441 prev->flags &= ~CF_NEW;
1371 prev->flags |= CF_RUNNING; 1442 prev->flags |= CF_RUNNING;
1372 } 1443 }
1373 1444
1374 prev->flags &= ~CF_RUNNING; 1445 prev->flags &= ~CF_RUNNING;
1385 coro_setup (aTHX_ next); 1456 coro_setup (aTHX_ next);
1386 } 1457 }
1387 else 1458 else
1388 load_perl (aTHX_ next); 1459 load_perl (aTHX_ next);
1389 1460
1390 prev__cctx = prev->cctx;
1391
1392 /* possibly untie and reuse the cctx */ 1461 /* possibly untie and reuse the cctx */
1393 if (expect_true ( 1462 if (expect_true (
1394 prev__cctx->idle_sp == STACKLEVEL 1463 cctx_current->idle_sp == STACKLEVEL
1395 && !(prev__cctx->flags & CC_TRACE) 1464 && !(cctx_current->flags & CC_TRACE)
1396 && !force_cctx 1465 && !force_cctx
1397 )) 1466 ))
1398 { 1467 {
1399 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1468 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1400 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); 1469 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1401 1470
1402 prev->cctx = 0;
1403
1404 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1471 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1405 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1472 /* without this the next cctx_get might destroy the running cctx while still in use */
1406 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1473 if (expect_false (CCTX_EXPIRED (cctx_current)))
1407 if (!next->cctx) 1474 if (expect_true (!next->cctx))
1408 next->cctx = cctx_get (aTHX); 1475 next->cctx = cctx_get (aTHX);
1409 1476
1410 cctx_put (prev__cctx); 1477 cctx_put (cctx_current);
1411 } 1478 }
1479 else
1480 prev->cctx = cctx_current;
1412 1481
1413 ++next->usecount; 1482 ++next->usecount;
1414 1483
1415 if (expect_true (!next->cctx)) 1484 cctx_prev = cctx_current;
1416 next->cctx = cctx_get (aTHX); 1485 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1417 1486
1418 if (expect_false (prev__cctx != next->cctx)) 1487 next->cctx = 0;
1488
1489 if (expect_false (cctx_prev != cctx_current))
1419 { 1490 {
1420 prev__cctx->top_env = PL_top_env; 1491 cctx_prev->top_env = PL_top_env;
1421 PL_top_env = next->cctx->top_env; 1492 PL_top_env = cctx_current->top_env;
1422 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1493 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1423 } 1494 }
1424 1495
1425 transfer_tail (aTHX); 1496 transfer_tail (aTHX);
1426 } 1497 }
1427} 1498}
1435coro_state_destroy (pTHX_ struct coro *coro) 1506coro_state_destroy (pTHX_ struct coro *coro)
1436{ 1507{
1437 if (coro->flags & CF_DESTROYED) 1508 if (coro->flags & CF_DESTROYED)
1438 return 0; 1509 return 0;
1439 1510
1440 if (coro->on_destroy) 1511 if (coro->on_destroy && !PL_dirty)
1441 coro->on_destroy (aTHX_ coro); 1512 coro->on_destroy (aTHX_ coro);
1442 1513
1443 coro->flags |= CF_DESTROYED; 1514 coro->flags |= CF_DESTROYED;
1444 1515
1445 if (coro->flags & CF_READY) 1516 if (coro->flags & CF_READY)
1449 --coro_nready; 1520 --coro_nready;
1450 } 1521 }
1451 else 1522 else
1452 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1523 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1453 1524
1454 if (coro->mainstack && coro->mainstack != main_mainstack) 1525 if (coro->mainstack
1455 { 1526 && coro->mainstack != main_mainstack
1456 struct coro temp; 1527 && coro->slot
1457 1528 && !PL_dirty)
1458 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING)));
1459
1460 save_perl (aTHX_ &temp);
1461 load_perl (aTHX_ coro);
1462
1463 coro_destruct (aTHX_ coro); 1529 coro_destruct_perl (aTHX_ coro);
1464
1465 load_perl (aTHX_ &temp);
1466
1467 coro->slot = 0;
1468 }
1469 1530
1470 cctx_destroy (coro->cctx); 1531 cctx_destroy (coro->cctx);
1471 SvREFCNT_dec (coro->startcv); 1532 SvREFCNT_dec (coro->startcv);
1472 SvREFCNT_dec (coro->args); 1533 SvREFCNT_dec (coro->args);
1473 SvREFCNT_dec (CORO_THROW); 1534 SvREFCNT_dec (CORO_THROW);
1560/** Coro ********************************************************************/ 1621/** Coro ********************************************************************/
1561 1622
1562INLINE void 1623INLINE void
1563coro_enq (pTHX_ struct coro *coro) 1624coro_enq (pTHX_ struct coro *coro)
1564{ 1625{
1565 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); 1626 struct coro **ready = coro_ready [coro->prio - PRIO_MIN];
1566}
1567 1627
1568INLINE SV * 1628 SvREFCNT_inc_NN (coro->hv);
1629
1630 coro->next_ready = 0;
1631 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1632 ready [1] = coro;
1633}
1634
1635INLINE struct coro *
1569coro_deq (pTHX) 1636coro_deq (pTHX)
1570{ 1637{
1571 int prio; 1638 int prio;
1572 1639
1573 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1640 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1574 if (AvFILLp (coro_ready [prio]) >= 0) 1641 {
1575 return av_shift (coro_ready [prio]); 1642 struct coro **ready = coro_ready [prio];
1643
1644 if (ready [0])
1645 {
1646 struct coro *coro = ready [0];
1647 ready [0] = coro->next_ready;
1648 return coro;
1649 }
1650 }
1576 1651
1577 return 0; 1652 return 0;
1578} 1653}
1579 1654
1580static int 1655static int
1582{ 1657{
1583 struct coro *coro; 1658 struct coro *coro;
1584 SV *sv_hook; 1659 SV *sv_hook;
1585 void (*xs_hook)(void); 1660 void (*xs_hook)(void);
1586 1661
1587 if (SvROK (coro_sv))
1588 coro_sv = SvRV (coro_sv);
1589
1590 coro = SvSTATE (coro_sv); 1662 coro = SvSTATE (coro_sv);
1591 1663
1592 if (coro->flags & CF_READY) 1664 if (coro->flags & CF_READY)
1593 return 0; 1665 return 0;
1594 1666
1647static void 1719static void
1648prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1720prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1649{ 1721{
1650 for (;;) 1722 for (;;)
1651 { 1723 {
1652 SV *next_sv = coro_deq (aTHX); 1724 struct coro *next = coro_deq (aTHX);
1653 1725
1654 if (expect_true (next_sv)) 1726 if (expect_true (next))
1655 { 1727 {
1656 struct coro *next = SvSTATE_hv (next_sv);
1657
1658 /* cannot transfer to destroyed coros, skip and look for next */ 1728 /* cannot transfer to destroyed coros, skip and look for next */
1659 if (expect_false (next->flags & CF_DESTROYED)) 1729 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1660 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1730 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1661 else 1731 else
1662 { 1732 {
1663 next->flags &= ~CF_READY; 1733 next->flags &= ~CF_READY;
1664 --coro_nready; 1734 --coro_nready;
1665 1735
1668 } 1738 }
1669 } 1739 }
1670 else 1740 else
1671 { 1741 {
1672 /* nothing to schedule: call the idle handler */ 1742 /* nothing to schedule: call the idle handler */
1743 if (SvROK (sv_idle)
1744 && SvOBJECT (SvRV (sv_idle)))
1745 {
1746 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1747 api_ready (aTHX_ SvRV (sv_idle));
1748 --coro_nready;
1749 }
1750 else
1751 {
1673 dSP; 1752 dSP;
1674 1753
1675 ENTER; 1754 ENTER;
1676 SAVETMPS; 1755 SAVETMPS;
1677 1756
1678 PUSHMARK (SP); 1757 PUSHMARK (SP);
1679 PUTBACK; 1758 PUTBACK;
1680 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1759 call_sv (sv_idle, G_VOID | G_DISCARD);
1681 1760
1682 FREETMPS; 1761 FREETMPS;
1683 LEAVE; 1762 LEAVE;
1763 }
1684 } 1764 }
1685 } 1765 }
1686} 1766}
1687 1767
1688INLINE void 1768INLINE void
1759static void 1839static void
1760api_trace (pTHX_ SV *coro_sv, int flags) 1840api_trace (pTHX_ SV *coro_sv, int flags)
1761{ 1841{
1762 struct coro *coro = SvSTATE (coro_sv); 1842 struct coro *coro = SvSTATE (coro_sv);
1763 1843
1844 if (coro->flags & CF_RUNNING)
1845 croak ("cannot enable tracing on a running coroutine, caught");
1846
1764 if (flags & CC_TRACE) 1847 if (flags & CC_TRACE)
1765 { 1848 {
1766 if (!coro->cctx) 1849 if (!coro->cctx)
1767 coro->cctx = cctx_new_run (); 1850 coro->cctx = cctx_new_run ();
1768 else if (!(coro->cctx->flags & CC_TRACE)) 1851 else if (!(coro->cctx->flags & CC_TRACE))
1769 croak ("cannot enable tracing on coroutine with custom stack,"); 1852 croak ("cannot enable tracing on coroutine with custom stack, caught");
1770 1853
1771 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1854 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1772 } 1855 }
1773 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1856 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1774 { 1857 {
1830 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 1913 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1831 api_ready (aTHX_ sv_manager); 1914 api_ready (aTHX_ sv_manager);
1832 1915
1833 frame->prepare = prepare_schedule; 1916 frame->prepare = prepare_schedule;
1834 frame->check = slf_check_repeat; 1917 frame->check = slf_check_repeat;
1918
1919 /* as a minor optimisation, we could unwind all stacks here */
1920 /* but that puts extra pressure on pp_slf, and is not worth much */
1921 /*coro_unwind_stacks (aTHX);*/
1835} 1922}
1836 1923
1837/*****************************************************************************/ 1924/*****************************************************************************/
1838/* async pool handler */ 1925/* async pool handler */
1839 1926
1922 SV *data = (SV *)GENSUB_ARG; 2009 SV *data = (SV *)GENSUB_ARG;
1923 2010
1924 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2011 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1925 { 2012 {
1926 /* first call, set args */ 2013 /* first call, set args */
2014 SV *coro = SvRV (data);
1927 AV *av = newAV (); 2015 AV *av = newAV ();
1928 SV *coro = SvRV (data);
1929 2016
1930 SvRV_set (data, (SV *)av); 2017 SvRV_set (data, (SV *)av);
1931 api_ready (aTHX_ coro);
1932 SvREFCNT_dec (coro);
1933 2018
1934 /* better take a full copy of the arguments */ 2019 /* better take a full copy of the arguments */
1935 while (items--) 2020 while (items--)
1936 av_store (av, items, newSVsv (ST (items))); 2021 av_store (av, items, newSVsv (ST (items)));
2022
2023 api_ready (aTHX_ coro);
2024 SvREFCNT_dec (coro);
1937 } 2025 }
1938 2026
1939 XSRETURN_EMPTY; 2027 XSRETURN_EMPTY;
1940} 2028}
1941 2029
1958 2046
1959 EXTEND (SP, AvFILLp (av) + 1); 2047 EXTEND (SP, AvFILLp (av) + 1);
1960 for (i = 0; i <= AvFILLp (av); ++i) 2048 for (i = 0; i <= AvFILLp (av); ++i)
1961 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2049 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1962 2050
1963 /* we have stolen the elements, so ste length to zero and free */ 2051 /* we have stolen the elements, so set length to zero and free */
1964 AvFILLp (av) = -1; 2052 AvFILLp (av) = -1;
1965 av_undef (av); 2053 av_undef (av);
1966 2054
1967 PUTBACK; 2055 PUTBACK;
1968 } 2056 }
2258 2346
2259 PL_op->op_ppaddr = pp_slf; 2347 PL_op->op_ppaddr = pp_slf;
2260 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2348 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2261 2349
2262 PL_op = (OP *)&slf_restore; 2350 PL_op = (OP *)&slf_restore;
2351}
2352
2353/*****************************************************************************/
2354/* dynamic wind */
2355
2356static void
2357on_enterleave_call (pTHX_ SV *cb)
2358{
2359 dSP;
2360
2361 PUSHSTACK;
2362
2363 PUSHMARK (SP);
2364 PUTBACK;
2365 call_sv (cb, G_VOID | G_DISCARD);
2366 SPAGAIN;
2367
2368 POPSTACK;
2369}
2370
2371static SV *
2372coro_avp_pop_and_free (pTHX_ AV **avp)
2373{
2374 AV *av = *avp;
2375 SV *res = av_pop (av);
2376
2377 if (AvFILLp (av) < 0)
2378 {
2379 *avp = 0;
2380 SvREFCNT_dec (av);
2381 }
2382
2383 return res;
2384}
2385
2386static void
2387coro_pop_on_enter (pTHX_ void *coro)
2388{
2389 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2390 SvREFCNT_dec (cb);
2391}
2392
2393static void
2394coro_pop_on_leave (pTHX_ void *coro)
2395{
2396 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2397 on_enterleave_call (aTHX_ sv_2mortal (cb));
2263} 2398}
2264 2399
2265/*****************************************************************************/ 2400/*****************************************************************************/
2266/* PerlIO::cede */ 2401/* PerlIO::cede */
2267 2402
2348 SV **ary; 2483 SV **ary;
2349 2484
2350 /* unfortunately, building manually saves memory */ 2485 /* unfortunately, building manually saves memory */
2351 Newx (ary, 2, SV *); 2486 Newx (ary, 2, SV *);
2352 AvALLOC (av) = ary; 2487 AvALLOC (av) = ary;
2488#if PERL_VERSION_ATLEAST (5,10,0)
2353 /*AvARRAY (av) = ary;*/ 2489 AvARRAY (av) = ary;
2490#else
2354 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2491 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2492 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2493 SvPVX ((SV *)av) = (char *)ary;
2494#endif
2355 AvMAX (av) = 1; 2495 AvMAX (av) = 1;
2356 AvFILLp (av) = 0; 2496 AvFILLp (av) = 0;
2357 ary [0] = newSViv (count); 2497 ary [0] = newSViv (count);
2358 2498
2359 return newRV_noinc ((SV *)av); 2499 return newRV_noinc ((SV *)av);
2746 coro_thx = PERL_GET_CONTEXT; 2886 coro_thx = PERL_GET_CONTEXT;
2747# endif 2887# endif
2748#endif 2888#endif
2749 BOOT_PAGESIZE; 2889 BOOT_PAGESIZE;
2750 2890
2891 cctx_current = cctx_new_empty ();
2892
2751 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2893 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2752 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2894 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2753 2895
2754 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2896 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2755 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2897 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2881_exit (int code) 3023_exit (int code)
2882 PROTOTYPE: $ 3024 PROTOTYPE: $
2883 CODE: 3025 CODE:
2884 _exit (code); 3026 _exit (code);
2885 3027
2886#if CORO_CLONE
2887
2888SV * 3028SV *
2889clone (Coro::State coro) 3029clone (Coro::State coro)
2890 CODE: 3030 CODE:
2891{ 3031{
3032#if CORO_CLONE
2892 struct coro *ncoro = coro_clone (coro); 3033 struct coro *ncoro = coro_clone (aTHX_ coro);
2893 MAGIC *mg; 3034 MAGIC *mg;
2894 /* TODO: too much duplication */ 3035 /* TODO: too much duplication */
2895 ncoro->hv = newHV (); 3036 ncoro->hv = newHV ();
2896 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); 3037 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2897 mg->mg_flags |= MGf_DUP; 3038 mg->mg_flags |= MGf_DUP;
2898 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv)); 3039 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
3040#else
3041 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
3042#endif
2899} 3043}
2900 OUTPUT: 3044 OUTPUT:
2901 RETVAL 3045 RETVAL
2902
2903#endif
2904 3046
2905int 3047int
2906cctx_stacksize (int new_stacksize = 0) 3048cctx_stacksize (int new_stacksize = 0)
2907 PROTOTYPE: ;$ 3049 PROTOTYPE: ;$
2908 CODE: 3050 CODE:
2958 eval = 1 3100 eval = 1
2959 CODE: 3101 CODE:
2960{ 3102{
2961 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3103 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2962 { 3104 {
2963 struct coro temp; 3105 struct coro *current = SvSTATE_current;
2964 3106
2965 if (!(coro->flags & CF_RUNNING)) 3107 if (current != coro)
2966 { 3108 {
2967 PUTBACK; 3109 PUTBACK;
2968 save_perl (aTHX_ &temp); 3110 save_perl (aTHX_ current);
2969 load_perl (aTHX_ coro); 3111 load_perl (aTHX_ coro);
3112 SPAGAIN;
2970 } 3113 }
2971 3114
2972 {
2973 dSP;
2974 ENTER;
2975 SAVETMPS;
2976 PUTBACK;
2977 PUSHSTACK; 3115 PUSHSTACK;
3116
2978 PUSHMARK (SP); 3117 PUSHMARK (SP);
3118 PUTBACK;
2979 3119
2980 if (ix) 3120 if (ix)
2981 eval_sv (coderef, 0); 3121 eval_sv (coderef, 0);
2982 else 3122 else
2983 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3123 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2984 3124
2985 POPSTACK; 3125 POPSTACK;
2986 SPAGAIN; 3126 SPAGAIN;
2987 FREETMPS;
2988 LEAVE;
2989 PUTBACK;
2990 }
2991 3127
2992 if (!(coro->flags & CF_RUNNING)) 3128 if (current != coro)
2993 { 3129 {
3130 PUTBACK;
2994 save_perl (aTHX_ coro); 3131 save_perl (aTHX_ coro);
2995 load_perl (aTHX_ &temp); 3132 load_perl (aTHX_ current);
2996 SPAGAIN; 3133 SPAGAIN;
2997 } 3134 }
2998 } 3135 }
2999} 3136}
3000 3137
3004 ALIAS: 3141 ALIAS:
3005 is_ready = CF_READY 3142 is_ready = CF_READY
3006 is_running = CF_RUNNING 3143 is_running = CF_RUNNING
3007 is_new = CF_NEW 3144 is_new = CF_NEW
3008 is_destroyed = CF_DESTROYED 3145 is_destroyed = CF_DESTROYED
3146 is_suspended = CF_SUSPENDED
3009 CODE: 3147 CODE:
3010 RETVAL = boolSV (coro->flags & ix); 3148 RETVAL = boolSV (coro->flags & ix);
3011 OUTPUT: 3149 OUTPUT:
3012 RETVAL 3150 RETVAL
3013 3151
3029 3167
3030SV * 3168SV *
3031has_cctx (Coro::State coro) 3169has_cctx (Coro::State coro)
3032 PROTOTYPE: $ 3170 PROTOTYPE: $
3033 CODE: 3171 CODE:
3034 RETVAL = boolSV (!!coro->cctx); 3172 /* maybe manage the running flag differently */
3173 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
3035 OUTPUT: 3174 OUTPUT:
3036 RETVAL 3175 RETVAL
3037 3176
3038int 3177int
3039is_traced (Coro::State coro) 3178is_traced (Coro::State coro)
3059 3198
3060void 3199void
3061force_cctx () 3200force_cctx ()
3062 PROTOTYPE: 3201 PROTOTYPE:
3063 CODE: 3202 CODE:
3064 SvSTATE_current->cctx->idle_sp = 0; 3203 cctx_current->idle_sp = 0;
3065 3204
3066void 3205void
3067swap_defsv (Coro::State self) 3206swap_defsv (Coro::State self)
3068 PROTOTYPE: $ 3207 PROTOTYPE: $
3069 ALIAS: 3208 ALIAS:
3077 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3216 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3078 3217
3079 SV *tmp = *src; *src = *dst; *dst = tmp; 3218 SV *tmp = *src; *src = *dst; *dst = tmp;
3080 } 3219 }
3081 3220
3221void
3222cancel (Coro::State self)
3223 CODE:
3224 coro_state_destroy (aTHX_ self);
3225 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3226
3082 3227
3083MODULE = Coro::State PACKAGE = Coro 3228MODULE = Coro::State PACKAGE = Coro
3084 3229
3085BOOT: 3230BOOT:
3086{ 3231{
3087 int i;
3088
3089 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3232 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3090 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3233 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3091 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3234 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3092 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3235 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3093 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3236 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3094 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3237 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3095 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3238 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3096 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3239 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3240 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3097 3241
3098 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3242 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3099 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3243 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3100 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3244 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3101 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3245 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
3106 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3250 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
3107 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3251 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
3108 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3252 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
3109 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3253 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
3110 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3254 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
3111
3112 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3113 coro_ready[i] = newAV ();
3114 3255
3115 { 3256 {
3116 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3257 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3117 3258
3118 coroapi.schedule = api_schedule; 3259 coroapi.schedule = api_schedule;
3159cede_notself (...) 3300cede_notself (...)
3160 CODE: 3301 CODE:
3161 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3302 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3162 3303
3163void 3304void
3164_cancel (Coro::State self)
3165 CODE:
3166 coro_state_destroy (aTHX_ self);
3167 coro_call_on_destroy (aTHX_ self);
3168
3169void
3170_set_current (SV *current) 3305_set_current (SV *current)
3171 PROTOTYPE: $ 3306 PROTOTYPE: $
3172 CODE: 3307 CODE:
3173 SvREFCNT_dec (SvRV (coro_current)); 3308 SvREFCNT_dec (SvRV (coro_current));
3174 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3309 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3216 PROTOTYPE: 3351 PROTOTYPE:
3217 CODE: 3352 CODE:
3218 RETVAL = coro_nready; 3353 RETVAL = coro_nready;
3219 OUTPUT: 3354 OUTPUT:
3220 RETVAL 3355 RETVAL
3356
3357void
3358suspend (Coro::State self)
3359 PROTOTYPE: $
3360 CODE:
3361 self->flags |= CF_SUSPENDED;
3362
3363void
3364resume (Coro::State self)
3365 PROTOTYPE: $
3366 CODE:
3367 self->flags &= ~CF_SUSPENDED;
3221 3368
3222void 3369void
3223_pool_handler (...) 3370_pool_handler (...)
3224 CODE: 3371 CODE:
3225 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3372 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3280rouse_wait (...) 3427rouse_wait (...)
3281 PROTOTYPE: ;$ 3428 PROTOTYPE: ;$
3282 PPCODE: 3429 PPCODE:
3283 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3430 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3284 3431
3432void
3433on_enter (SV *block)
3434 ALIAS:
3435 on_leave = 1
3436 PROTOTYPE: &
3437 CODE:
3438{
3439 struct coro *coro = SvSTATE_current;
3440 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3441
3442 block = (SV *)coro_sv_2cv (aTHX_ block);
3443
3444 if (!*avp)
3445 *avp = newAV ();
3446
3447 av_push (*avp, SvREFCNT_inc (block));
3448
3449 if (!ix)
3450 on_enterleave_call (aTHX_ block);
3451
3452 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3453 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3454 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3455}
3456
3285 3457
3286MODULE = Coro::State PACKAGE = PerlIO::cede 3458MODULE = Coro::State PACKAGE = PerlIO::cede
3287 3459
3288BOOT: 3460BOOT:
3289 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3461 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3299 GvSTASH (CvGV (cv)) 3471 GvSTASH (CvGV (cv))
3300 ); 3472 );
3301 OUTPUT: 3473 OUTPUT:
3302 RETVAL 3474 RETVAL
3303 3475
3304# helper for Coro::Channel 3476# helper for Coro::Channel and others
3305SV * 3477SV *
3306_alloc (int count) 3478_alloc (int count)
3307 CODE: 3479 CODE:
3308 RETVAL = coro_waitarray_new (aTHX_ count); 3480 RETVAL = coro_waitarray_new (aTHX_ count);
3309 OUTPUT: 3481 OUTPUT:
3367 for (i = 1; i <= wcount; ++i) 3539 for (i = 1; i <= wcount; ++i)
3368 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3540 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3369 } 3541 }
3370} 3542}
3371 3543
3544MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3545
3546void
3547_may_delete (SV *sem, int count, int extra_refs)
3548 PPCODE:
3549{
3550 AV *av = (AV *)SvRV (sem);
3551
3552 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3553 && AvFILLp (av) == 0 /* no waiters, just count */
3554 && SvIV (AvARRAY (av)[0]) == count)
3555 XSRETURN_YES;
3556
3557 XSRETURN_NO;
3558}
3559
3372MODULE = Coro::State PACKAGE = Coro::Signal 3560MODULE = Coro::State PACKAGE = Coro::Signal
3373 3561
3374SV * 3562SV *
3375new (SV *klass) 3563new (SV *klass)
3376 CODE: 3564 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines