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.400 by root, Thu May 12 23:55:39 2011 UTC vs.
Revision 1.423 by root, Mon Oct 8 23:13:36 2012 UTC

13#include "XSUB.h" 13#include "XSUB.h"
14#include "perliol.h" 14#include "perliol.h"
15 15
16#include "schmorp.h" 16#include "schmorp.h"
17 17
18#define ECB_NO_THREADS 1
19#include "ecb.h"
20
21#include <stddef.h>
18#include <stdio.h> 22#include <stdio.h>
19#include <errno.h> 23#include <errno.h>
20#include <assert.h> 24#include <assert.h>
21 25
22#ifndef SVs_PADSTALE 26#ifndef SVs_PADSTALE
31# define setjmp _setjmp /* deep magic */ 35# define setjmp _setjmp /* deep magic */
32#else 36#else
33# include <inttypes.h> /* most portable stdint.h */ 37# include <inttypes.h> /* most portable stdint.h */
34#endif 38#endif
35 39
36#ifdef HAVE_MMAP 40#if HAVE_MMAP
37# include <unistd.h> 41# include <unistd.h>
38# include <sys/mman.h> 42# include <sys/mman.h>
39# ifndef MAP_ANONYMOUS 43# ifndef MAP_ANONYMOUS
40# ifdef MAP_ANON 44# ifdef MAP_ANON
41# define MAP_ANONYMOUS MAP_ANON 45# define MAP_ANONYMOUS MAP_ANON
61#endif 65#endif
62 66
63/* the maximum number of idle cctx that will be pooled */ 67/* the maximum number of idle cctx that will be pooled */
64static int cctx_max_idle = 4; 68static int cctx_max_idle = 4;
65 69
70#if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0)
71# define HAS_SCOPESTACK_NAME 1
72#endif
73
66#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 74#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
67# undef CORO_STACKGUARD 75# undef CORO_STACKGUARD
68#endif 76#endif
69 77
70#ifndef CORO_STACKGUARD 78#ifndef CORO_STACKGUARD
86# define STACKLEVEL ((void *)&stacklevel) 94# define STACKLEVEL ((void *)&stacklevel)
87#endif 95#endif
88 96
89#define IN_DESTRUCT PL_dirty 97#define IN_DESTRUCT PL_dirty
90 98
91#if __GNUC__ >= 3
92# define attribute(x) __attribute__(x)
93# define expect(expr,value) __builtin_expect ((expr), (value))
94# define INLINE static inline
95#else
96# define attribute(x)
97# define expect(expr,value) (expr)
98# define INLINE static
99#endif
100
101#define expect_false(expr) expect ((expr) != 0, 0)
102#define expect_true(expr) expect ((expr) != 0, 1)
103
104#define NOINLINE attribute ((noinline))
105
106#include "CoroAPI.h" 99#include "CoroAPI.h"
107#define GCoroAPI (&coroapi) /* very sneaky */ 100#define GCoroAPI (&coroapi) /* very sneaky */
108 101
109#ifdef USE_ITHREADS 102#ifdef USE_ITHREADS
110# if CORO_PTHREAD 103# if CORO_PTHREAD
187 void *sptr; 180 void *sptr;
188 size_t ssize; 181 size_t ssize;
189 182
190 /* cpu state */ 183 /* cpu state */
191 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 184 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
185#ifndef NDEBUG
192 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 186 JMPENV *idle_te; /* same as idle_sp, but for top_env */
187#endif
193 JMPENV *top_env; 188 JMPENV *top_env;
194 coro_context cctx; 189 coro_context cctx;
195 190
196 U32 gen; 191 U32 gen;
197#if CORO_USE_VALGRIND 192#if CORO_USE_VALGRIND
217}; 212};
218 213
219/* the structure where most of the perl state is stored, overlaid on the cxstack */ 214/* the structure where most of the perl state is stored, overlaid on the cxstack */
220typedef struct 215typedef struct
221{ 216{
222 SV *defsv;
223 AV *defav;
224 SV *errsv;
225 SV *irsgv;
226 HV *hinthv;
227#define VAR(name,type) type name; 217 #define VARx(name,expr,type) type name;
228# include "state.h" 218 #include "state.h"
229#undef VAR
230} perl_slots; 219} perl_slots;
231 220
232// how many context stack entries do we need for perl_slots 221/* how many context stack entries do we need for perl_slots */
233#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) 222#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
234 223
235/* this is a structure representing a perl-level coroutine */ 224/* this is a structure representing a perl-level coroutine */
236struct coro 225struct coro
237{ 226{
303static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ 292static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
304static CV *cv_coro_run; 293static CV *cv_coro_run;
305static struct coro *coro_first; 294static struct coro *coro_first;
306#define coro_nready coroapi.nready 295#define coro_nready coroapi.nready
307 296
297/** JIT *********************************************************************/
298
299#if CORO_JIT
300 /* APPLE doesn't have HAVE_MMAP though */
301 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__)
302 #ifndef CORO_JIT_TYPE
303 #if __x86_64 && CORO_JIT_UNIXY
304 #define CORO_JIT_TYPE "amd64-unix"
305 #elif __i386 && CORO_JIT_UNIXY
306 #define CORO_JIT_TYPE "x86-unix"
307 #endif
308 #endif
309#endif
310
311#if !defined(CORO_JIT_TYPE) || !HAVE_MMAP
312 #undef CORO_JIT
313#endif
314
315#if CORO_JIT
316 typedef void (*load_save_perl_slots_type)(perl_slots *);
317 static load_save_perl_slots_type load_perl_slots, save_perl_slots;
318#endif
319
308/** Coro::Select ************************************************************/ 320/** Coro::Select ************************************************************/
309 321
310static OP *(*coro_old_pp_sselect) (pTHX); 322static OP *(*coro_old_pp_sselect) (pTHX);
311static SV *coro_select_select; 323static SV *coro_select_select;
312 324
327 339
328/** time stuff **************************************************************/ 340/** time stuff **************************************************************/
329 341
330#ifdef HAS_GETTIMEOFDAY 342#ifdef HAS_GETTIMEOFDAY
331 343
332static void 344ecb_inline void
333coro_u2time (pTHX_ UV ret[2]) 345coro_u2time (pTHX_ UV ret[2])
334{ 346{
335 struct timeval tv; 347 struct timeval tv;
336 gettimeofday (&tv, 0); 348 gettimeofday (&tv, 0);
337 349
338 ret [0] = tv.tv_sec; 350 ret [0] = tv.tv_sec;
339 ret [1] = tv.tv_usec; 351 ret [1] = tv.tv_usec;
340} 352}
341 353
342static double 354ecb_inline double
343coro_nvtime () 355coro_nvtime (void)
344{ 356{
345 struct timeval tv; 357 struct timeval tv;
346 gettimeofday (&tv, 0); 358 gettimeofday (&tv, 0);
347 359
348 return tv.tv_sec + tv.tv_usec * 1e-6; 360 return tv.tv_sec + tv.tv_usec * 1e-6;
349} 361}
350 362
351static void 363ecb_inline void
352time_init (pTHX) 364time_init (pTHX)
353{ 365{
354 nvtime = coro_nvtime; 366 nvtime = coro_nvtime;
355 u2time = coro_u2time; 367 u2time = coro_u2time;
356} 368}
357 369
358#else 370#else
359 371
360static void 372ecb_inline void
361time_init (pTHX) 373time_init (pTHX)
362{ 374{
363 SV **svp; 375 SV **svp;
364 376
365 require_pv ("Time/HiRes.pm"); 377 require_pv ("Time/HiRes.pm");
377 389
378#endif 390#endif
379 391
380/** lowlevel stuff **********************************************************/ 392/** lowlevel stuff **********************************************************/
381 393
382static SV * 394static SV * ecb_noinline
383coro_get_sv (pTHX_ const char *name, int create) 395coro_get_sv (pTHX_ const char *name, int create)
384{ 396{
385#if PERL_VERSION_ATLEAST (5,10,0) 397#if PERL_VERSION_ATLEAST (5,10,0)
386 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 398 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
387 get_sv (name, create); 399 get_sv (name, create);
388#endif 400#endif
389 return get_sv (name, create); 401 return get_sv (name, create);
390} 402}
391 403
392static AV * 404static AV * ecb_noinline
393coro_get_av (pTHX_ const char *name, int create) 405coro_get_av (pTHX_ const char *name, int create)
394{ 406{
395#if PERL_VERSION_ATLEAST (5,10,0) 407#if PERL_VERSION_ATLEAST (5,10,0)
396 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 408 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
397 get_av (name, create); 409 get_av (name, create);
398#endif 410#endif
399 return get_av (name, create); 411 return get_av (name, create);
400} 412}
401 413
402static HV * 414static HV * ecb_noinline
403coro_get_hv (pTHX_ const char *name, int create) 415coro_get_hv (pTHX_ const char *name, int create)
404{ 416{
405#if PERL_VERSION_ATLEAST (5,10,0) 417#if PERL_VERSION_ATLEAST (5,10,0)
406 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 418 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
407 get_hv (name, create); 419 get_hv (name, create);
408#endif 420#endif
409 return get_hv (name, create); 421 return get_hv (name, create);
410} 422}
411 423
412INLINE void 424ecb_inline void
413coro_times_update () 425coro_times_update (void)
414{ 426{
415#ifdef coro_clock_gettime 427#ifdef coro_clock_gettime
416 struct timespec ts; 428 struct timespec ts;
417 429
418 ts.tv_sec = ts.tv_nsec = 0; 430 ts.tv_sec = ts.tv_nsec = 0;
430 time_real [0] = tv [0]; 442 time_real [0] = tv [0];
431 time_real [1] = tv [1] * 1000; 443 time_real [1] = tv [1] * 1000;
432#endif 444#endif
433} 445}
434 446
435INLINE void 447ecb_inline void
436coro_times_add (struct coro *c) 448coro_times_add (struct coro *c)
437{ 449{
438 c->t_real [1] += time_real [1]; 450 c->t_real [1] += time_real [1];
439 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } 451 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; }
440 c->t_real [0] += time_real [0]; 452 c->t_real [0] += time_real [0];
442 c->t_cpu [1] += time_cpu [1]; 454 c->t_cpu [1] += time_cpu [1];
443 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } 455 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; }
444 c->t_cpu [0] += time_cpu [0]; 456 c->t_cpu [0] += time_cpu [0];
445} 457}
446 458
447INLINE void 459ecb_inline void
448coro_times_sub (struct coro *c) 460coro_times_sub (struct coro *c)
449{ 461{
450 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } 462 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; }
451 c->t_real [1] -= time_real [1]; 463 c->t_real [1] -= time_real [1];
452 c->t_real [0] -= time_real [0]; 464 c->t_real [0] -= time_real [0];
460/* magic glue */ 472/* magic glue */
461 473
462#define CORO_MAGIC_type_cv 26 474#define CORO_MAGIC_type_cv 26
463#define CORO_MAGIC_type_state PERL_MAGIC_ext 475#define CORO_MAGIC_type_state PERL_MAGIC_ext
464 476
465#define CORO_MAGIC_NN(sv, type) \ 477#define CORO_MAGIC_NN(sv, type) \
466 (expect_true (SvMAGIC (sv)->mg_type == type) \ 478 (ecb_expect_true (SvMAGIC (sv)->mg_type == type) \
467 ? SvMAGIC (sv) \ 479 ? SvMAGIC (sv) \
468 : mg_find (sv, type)) 480 : mg_find (sv, type))
469 481
470#define CORO_MAGIC(sv, type) \ 482#define CORO_MAGIC(sv, type) \
471 (expect_true (SvMAGIC (sv)) \ 483 (ecb_expect_true (SvMAGIC (sv)) \
472 ? CORO_MAGIC_NN (sv, type) \ 484 ? CORO_MAGIC_NN (sv, type) \
473 : 0) 485 : 0)
474 486
475#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 487#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
476#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) 488#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
477 489
478INLINE MAGIC * 490ecb_inline MAGIC *
479SvSTATEhv_p (pTHX_ SV *coro) 491SvSTATEhv_p (pTHX_ SV *coro)
480{ 492{
481 MAGIC *mg; 493 MAGIC *mg;
482 494
483 if (expect_true ( 495 if (ecb_expect_true (
484 SvTYPE (coro) == SVt_PVHV 496 SvTYPE (coro) == SVt_PVHV
485 && (mg = CORO_MAGIC_state (coro)) 497 && (mg = CORO_MAGIC_state (coro))
486 && mg->mg_virtual == &coro_state_vtbl 498 && mg->mg_virtual == &coro_state_vtbl
487 )) 499 ))
488 return mg; 500 return mg;
489 501
490 return 0; 502 return 0;
491} 503}
492 504
493INLINE struct coro * 505ecb_inline struct coro *
494SvSTATE_ (pTHX_ SV *coro) 506SvSTATE_ (pTHX_ SV *coro)
495{ 507{
496 MAGIC *mg; 508 MAGIC *mg;
497 509
498 if (SvROK (coro)) 510 if (SvROK (coro))
499 coro = SvRV (coro); 511 coro = SvRV (coro);
500 512
501 mg = SvSTATEhv_p (coro); 513 mg = SvSTATEhv_p (aTHX_ coro);
502 if (!mg) 514 if (!mg)
503 croak ("Coro::State object required"); 515 croak ("Coro::State object required");
504 516
505 return (struct coro *)mg->mg_ptr; 517 return (struct coro *)mg->mg_ptr;
506} 518}
512#define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) 524#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
513 525
514/*****************************************************************************/ 526/*****************************************************************************/
515/* padlist management and caching */ 527/* padlist management and caching */
516 528
517static AV * 529ecb_inline AV *
518coro_derive_padlist (pTHX_ CV *cv) 530coro_derive_padlist (pTHX_ CV *cv)
519{ 531{
520 AV *padlist = CvPADLIST (cv); 532 AV *padlist = CvPADLIST (cv);
521 AV *newpadlist, *newpad; 533 AV *newpadlist, *newpad;
522 534
534 av_store (newpadlist, 1, (SV *)newpad); 546 av_store (newpadlist, 1, (SV *)newpad);
535 547
536 return newpadlist; 548 return newpadlist;
537} 549}
538 550
539static void 551ecb_inline void
540free_padlist (pTHX_ AV *padlist) 552free_padlist (pTHX_ AV *padlist)
541{ 553{
542 /* may be during global destruction */ 554 /* may be during global destruction */
543 if (!IN_DESTRUCT) 555 if (!IN_DESTRUCT)
544 { 556 {
587 0, 0, 0, 0, 599 0, 0, 0, 0,
588 coro_cv_free 600 coro_cv_free
589}; 601};
590 602
591/* the next two functions merely cache the padlists */ 603/* the next two functions merely cache the padlists */
592static void 604ecb_inline void
593get_padlist (pTHX_ CV *cv) 605get_padlist (pTHX_ CV *cv)
594{ 606{
595 MAGIC *mg = CORO_MAGIC_cv (cv); 607 MAGIC *mg = CORO_MAGIC_cv (cv);
596 AV *av; 608 AV *av;
597 609
598 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 610 if (ecb_expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
599 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 611 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
600 else 612 else
601 { 613 {
602#if CORO_PREFER_PERL_FUNCTIONS 614#if CORO_PREFER_PERL_FUNCTIONS
603 /* this is probably cleaner? but also slower! */ 615 /* this is probably cleaner? but also slower! */
610 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); 622 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
611#endif 623#endif
612 } 624 }
613} 625}
614 626
615static void 627ecb_inline void
616put_padlist (pTHX_ CV *cv) 628put_padlist (pTHX_ CV *cv)
617{ 629{
618 MAGIC *mg = CORO_MAGIC_cv (cv); 630 MAGIC *mg = CORO_MAGIC_cv (cv);
619 AV *av; 631 AV *av;
620 632
621 if (expect_false (!mg)) 633 if (ecb_expect_false (!mg))
622 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 634 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
623 635
624 av = (AV *)mg->mg_obj; 636 av = (AV *)mg->mg_obj;
625 637
626 if (expect_false (AvFILLp (av) >= AvMAX (av))) 638 if (ecb_expect_false (AvFILLp (av) >= AvMAX (av)))
627 av_extend (av, AvFILLp (av) + 1); 639 av_extend (av, AvFILLp (av) + 1);
628 640
629 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 641 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
630} 642}
631 643
632/** load & save, init *******************************************************/ 644/** load & save, init *******************************************************/
633 645
646ecb_inline void
647swap_sv (SV *a, SV *b)
648{
649 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
650 SV tmp;
651
652 /* swap sv_any */
653 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
654
655 /* swap sv_flags */
656 SvFLAGS (&tmp) = SvFLAGS (a);
657 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
658 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
659
660#if PERL_VERSION_ATLEAST (5,10,0)
661 /* perl 5.10 and later complicates this _quite_ a bit, but it also
662 * is much faster, so no quarrels here. alternatively, we could
663 * sv_upgrade to avoid this.
664 */
665 {
666 /* swap sv_u */
667 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
668
669 /* if SvANY points to the head, we need to adjust the pointers,
670 * as the pointer for a still points to b, and maybe vice versa.
671 */
672 #define svany_in_head(type) \
673 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
674
675 if (svany_in_head (SvTYPE (a)))
676 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
677
678 if (svany_in_head (SvTYPE (b)))
679 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
680 }
681#endif
682}
683
634/* swap sv heads, at least logically */ 684/* swap sv heads, at least logically */
635static void 685static void
636swap_svs (pTHX_ Coro__State c) 686swap_svs (pTHX_ Coro__State c)
637{ 687{
638 int i; 688 int i;
639 689
640 for (i = 0; i <= AvFILLp (c->swap_sv); ) 690 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2)
641 { 691 swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]);
642 SV *a = AvARRAY (c->swap_sv)[i++];
643 SV *b = AvARRAY (c->swap_sv)[i++];
644
645 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
646 SV tmp;
647
648 /* swap sv_any */
649 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
650
651 /* swap sv_flags */
652 SvFLAGS (&tmp) = SvFLAGS (a);
653 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
654 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
655
656#if PERL_VERSION_ATLEAST (5,10,0)
657 /* perl 5.10 complicates this _quite_ a bit, but it also is
658 * much faster, so no quarrels here. alternatively, we could
659 * sv_upgrade to avoid this.
660 */
661 {
662 /* swap sv_u */
663 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
664
665 /* if SvANY points to the head, we need to adjust the pointers,
666 * as the pointer for a still points to b, and maybe vice versa.
667 */
668 #define svany_in_head(type) \
669 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
670
671 if (svany_in_head (SvTYPE (a)))
672 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
673
674 if (svany_in_head (SvTYPE (b)))
675 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
676 }
677#endif
678 }
679} 692}
680 693
681#define SWAP_SVS(coro) \ 694#define SWAP_SVS(coro) \
682 if (expect_false ((coro)->swap_sv)) \ 695 if (ecb_expect_false ((coro)->swap_sv)) \
683 swap_svs (aTHX_ (coro)) 696 swap_svs (aTHX_ (coro))
684 697
685static void 698static void
686on_enterleave_call (pTHX_ SV *cb); 699on_enterleave_call (pTHX_ SV *cb);
687 700
691 perl_slots *slot = c->slot; 704 perl_slots *slot = c->slot;
692 c->slot = 0; 705 c->slot = 0;
693 706
694 PL_mainstack = c->mainstack; 707 PL_mainstack = c->mainstack;
695 708
696 GvSV (PL_defgv) = slot->defsv; 709#if CORO_JIT
697 GvAV (PL_defgv) = slot->defav; 710 load_perl_slots (slot);
698 GvSV (PL_errgv) = slot->errsv; 711#else
699 GvSV (irsgv) = slot->irsgv;
700 GvHV (PL_hintgv) = slot->hinthv;
701
702 #define VAR(name,type) PL_ ## name = slot->name; 712 #define VARx(name,expr,type) expr = slot->name;
703 # include "state.h" 713 #include "state.h"
704 #undef VAR 714#endif
705 715
706 { 716 {
707 dSP; 717 dSP;
708 718
709 CV *cv; 719 CV *cv;
710 720
711 /* now do the ugly restore mess */ 721 /* now do the ugly restore mess */
712 while (expect_true (cv = (CV *)POPs)) 722 while (ecb_expect_true (cv = (CV *)POPs))
713 { 723 {
714 put_padlist (aTHX_ cv); /* mark this padlist as available */ 724 put_padlist (aTHX_ cv); /* mark this padlist as available */
715 CvDEPTH (cv) = PTR2IV (POPs); 725 CvDEPTH (cv) = PTR2IV (POPs);
716 CvPADLIST (cv) = (AV *)POPs; 726 CvPADLIST (cv) = (AV *)POPs;
717 } 727 }
720 } 730 }
721 731
722 slf_frame = c->slf_frame; 732 slf_frame = c->slf_frame;
723 CORO_THROW = c->except; 733 CORO_THROW = c->except;
724 734
725 if (expect_false (enable_times)) 735 if (ecb_expect_false (enable_times))
726 { 736 {
727 if (expect_false (!times_valid)) 737 if (ecb_expect_false (!times_valid))
728 coro_times_update (); 738 coro_times_update ();
729 739
730 coro_times_sub (c); 740 coro_times_sub (c);
731 } 741 }
732 742
733 if (expect_false (c->on_enter)) 743 if (ecb_expect_false (c->on_enter))
734 { 744 {
735 int i; 745 int i;
736 746
737 for (i = 0; i <= AvFILLp (c->on_enter); ++i) 747 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
738 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); 748 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
744static void 754static void
745save_perl (pTHX_ Coro__State c) 755save_perl (pTHX_ Coro__State c)
746{ 756{
747 SWAP_SVS (c); 757 SWAP_SVS (c);
748 758
749 if (expect_false (c->on_leave)) 759 if (ecb_expect_false (c->on_leave))
750 { 760 {
751 int i; 761 int i;
752 762
753 for (i = AvFILLp (c->on_leave); i >= 0; --i) 763 for (i = AvFILLp (c->on_leave); i >= 0; --i)
754 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); 764 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
755 } 765 }
756 766
757 times_valid = 0; 767 times_valid = 0;
758 768
759 if (expect_false (enable_times)) 769 if (ecb_expect_false (enable_times))
760 { 770 {
761 coro_times_update (); times_valid = 1; 771 coro_times_update (); times_valid = 1;
762 coro_times_add (c); 772 coro_times_add (c);
763 } 773 }
764 774
778 788
779 XPUSHs (Nullsv); 789 XPUSHs (Nullsv);
780 /* this loop was inspired by pp_caller */ 790 /* this loop was inspired by pp_caller */
781 for (;;) 791 for (;;)
782 { 792 {
783 while (expect_true (cxix >= 0)) 793 while (ecb_expect_true (cxix >= 0))
784 { 794 {
785 PERL_CONTEXT *cx = &ccstk[cxix--]; 795 PERL_CONTEXT *cx = &ccstk[cxix--];
786 796
787 if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT)) 797 if (ecb_expect_true (CxTYPE (cx) == CXt_SUB) || ecb_expect_false (CxTYPE (cx) == CXt_FORMAT))
788 { 798 {
789 CV *cv = cx->blk_sub.cv; 799 CV *cv = cx->blk_sub.cv;
790 800
791 if (expect_true (CvDEPTH (cv))) 801 if (ecb_expect_true (CvDEPTH (cv)))
792 { 802 {
793 EXTEND (SP, 3); 803 EXTEND (SP, 3);
794 PUSHs ((SV *)CvPADLIST (cv)); 804 PUSHs ((SV *)CvPADLIST (cv));
795 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); 805 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
796 PUSHs ((SV *)cv); 806 PUSHs ((SV *)cv);
799 get_padlist (aTHX_ cv); 809 get_padlist (aTHX_ cv);
800 } 810 }
801 } 811 }
802 } 812 }
803 813
804 if (expect_true (top_si->si_type == PERLSI_MAIN)) 814 if (ecb_expect_true (top_si->si_type == PERLSI_MAIN))
805 break; 815 break;
806 816
807 top_si = top_si->si_prev; 817 top_si = top_si->si_prev;
808 ccstk = top_si->si_cxstack; 818 ccstk = top_si->si_cxstack;
809 cxix = top_si->si_cxix; 819 cxix = top_si->si_cxix;
811 821
812 PUTBACK; 822 PUTBACK;
813 } 823 }
814 824
815 /* allocate some space on the context stack for our purposes */ 825 /* allocate some space on the context stack for our purposes */
816 if (expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) 826 if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max))
817 { 827 {
818 unsigned int i; 828 unsigned int i;
819 829
820 for (i = 0; i < SLOT_COUNT; ++i) 830 for (i = 0; i < SLOT_COUNT; ++i)
821 CXINC; 831 CXINC;
826 c->mainstack = PL_mainstack; 836 c->mainstack = PL_mainstack;
827 837
828 { 838 {
829 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 839 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
830 840
831 slot->defav = GvAV (PL_defgv); 841#if CORO_JIT
832 slot->defsv = DEFSV; 842 save_perl_slots (slot);
833 slot->errsv = ERRSV; 843#else
834 slot->irsgv = GvSV (irsgv);
835 slot->hinthv = GvHV (PL_hintgv);
836
837 #define VAR(name,type) slot->name = PL_ ## name; 844 #define VARx(name,expr,type) slot->name = expr;
838 # include "state.h" 845 #include "state.h"
839 #undef VAR 846#endif
840 } 847 }
841} 848}
842 849
843/* 850/*
844 * allocate various perl stacks. This is almost an exact copy 851 * allocate various perl stacks. This is almost an exact copy
875#endif 882#endif
876 883
877 New(54,PL_scopestack,8,I32); 884 New(54,PL_scopestack,8,I32);
878 PL_scopestack_ix = 0; 885 PL_scopestack_ix = 0;
879 PL_scopestack_max = 8; 886 PL_scopestack_max = 8;
887#if HAS_SCOPESTACK_NAME
888 New(54,PL_scopestack_name,8,const char*);
889#endif
880 890
881 New(54,PL_savestack,24,ANY); 891 New(54,PL_savestack,24,ANY);
882 PL_savestack_ix = 0; 892 PL_savestack_ix = 0;
883 PL_savestack_max = 24; 893 PL_savestack_max = 24;
884 894
912 } 922 }
913 923
914 Safefree (PL_tmps_stack); 924 Safefree (PL_tmps_stack);
915 Safefree (PL_markstack); 925 Safefree (PL_markstack);
916 Safefree (PL_scopestack); 926 Safefree (PL_scopestack);
927#if HAS_SCOPESTACK_NAME
928 Safefree (PL_scopestack_name);
929#endif
917 Safefree (PL_savestack); 930 Safefree (PL_savestack);
918#if !PERL_VERSION_ATLEAST (5,10,0) 931#if !PERL_VERSION_ATLEAST (5,10,0)
919 Safefree (PL_retstack); 932 Safefree (PL_retstack);
920#endif 933#endif
921} 934}
967#endif 980#endif
968 981
969/* 982/*
970 * This overrides the default magic get method of %SIG elements. 983 * This overrides the default magic get method of %SIG elements.
971 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 984 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
972 * and instead of trying to save and restore the hash elements, we just provide 985 * and instead of trying to save and restore the hash elements (extremely slow),
973 * readback here. 986 * we just provide our own readback here.
974 */ 987 */
975static int 988static int ecb_cold
976coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 989coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
977{ 990{
978 const char *s = MgPV_nolen_const (mg); 991 const char *s = MgPV_nolen_const (mg);
979 992
980 if (*s == '_') 993 if (*s == '_')
984 if (strEQ (s, "__DIE__" )) svp = &PL_diehook; 997 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
985 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 998 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
986 999
987 if (svp) 1000 if (svp)
988 { 1001 {
989 sv_setsv (sv, *svp ? *svp : &PL_sv_undef); 1002 SV *ssv;
1003
1004 if (!*svp)
1005 ssv = &PL_sv_undef;
1006 else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */
1007 ssv = sv_2mortal (newRV_inc (*svp));
1008 else
1009 ssv = *svp;
1010
1011 sv_setsv (sv, ssv);
990 return 0; 1012 return 0;
991 } 1013 }
992 } 1014 }
993 1015
994 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; 1016 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
995} 1017}
996 1018
997static int 1019static int ecb_cold
998coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) 1020coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
999{ 1021{
1000 const char *s = MgPV_nolen_const (mg); 1022 const char *s = MgPV_nolen_const (mg);
1001 1023
1002 if (*s == '_') 1024 if (*s == '_')
1016 } 1038 }
1017 1039
1018 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; 1040 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0;
1019} 1041}
1020 1042
1021static int 1043static int ecb_cold
1022coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) 1044coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
1023{ 1045{
1024 const char *s = MgPV_nolen_const (mg); 1046 const char *s = MgPV_nolen_const (mg);
1025 1047
1026 if (*s == '_') 1048 if (*s == '_')
1061 return 1; 1083 return 1;
1062} 1084}
1063 1085
1064static UNOP init_perl_op; 1086static UNOP init_perl_op;
1065 1087
1066static void NOINLINE /* noinline to keep it out of the transfer fast path */ 1088ecb_noinline static void /* noinline to keep it out of the transfer fast path */
1067init_perl (pTHX_ struct coro *coro) 1089init_perl (pTHX_ struct coro *coro)
1068{ 1090{
1069 /* 1091 /*
1070 * emulate part of the perl startup here. 1092 * emulate part of the perl startup here.
1071 */ 1093 */
1086 PL_parser = 0; 1108 PL_parser = 0;
1087#endif 1109#endif
1088 PL_hints = 0; 1110 PL_hints = 0;
1089 1111
1090 /* recreate the die/warn hooks */ 1112 /* recreate the die/warn hooks */
1091 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 1113 PL_diehook = SvREFCNT_inc (rv_diehook);
1092 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); 1114 PL_warnhook = SvREFCNT_inc (rv_warnhook);
1093 1115
1094 GvSV (PL_defgv) = newSV (0); 1116 GvSV (PL_defgv) = newSV (0);
1095 GvAV (PL_defgv) = coro->args; coro->args = 0; 1117 GvAV (PL_defgv) = coro->args; coro->args = 0;
1096 GvSV (PL_errgv) = newSV (0); 1118 GvSV (PL_errgv) = newSV (0);
1097 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 1119 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
1133 /* copy throw, in case it was set before init_perl */ 1155 /* copy throw, in case it was set before init_perl */
1134 CORO_THROW = coro->except; 1156 CORO_THROW = coro->except;
1135 1157
1136 SWAP_SVS (coro); 1158 SWAP_SVS (coro);
1137 1159
1138 if (expect_false (enable_times)) 1160 if (ecb_expect_false (enable_times))
1139 { 1161 {
1140 coro_times_update (); 1162 coro_times_update ();
1141 coro_times_sub (coro); 1163 coro_times_sub (coro);
1142 } 1164 }
1143} 1165}
1186 /* restore swapped sv's */ 1208 /* restore swapped sv's */
1187 SWAP_SVS (coro); 1209 SWAP_SVS (coro);
1188 1210
1189 coro_destruct_stacks (aTHX); 1211 coro_destruct_stacks (aTHX);
1190 1212
1191 // now save some sv's to be free'd later 1213 /* now save some sv's to be free'd later */
1192 svf [0] = GvSV (PL_defgv); 1214 svf [0] = GvSV (PL_defgv);
1193 svf [1] = (SV *)GvAV (PL_defgv); 1215 svf [1] = (SV *)GvAV (PL_defgv);
1194 svf [2] = GvSV (PL_errgv); 1216 svf [2] = GvSV (PL_errgv);
1195 svf [3] = (SV *)PL_defoutgv; 1217 svf [3] = (SV *)PL_defoutgv;
1196 svf [4] = PL_rs; 1218 svf [4] = PL_rs;
1216 SvREFCNT_dec (coro->invoke_cb); 1238 SvREFCNT_dec (coro->invoke_cb);
1217 SvREFCNT_dec (coro->invoke_av); 1239 SvREFCNT_dec (coro->invoke_av);
1218 } 1240 }
1219} 1241}
1220 1242
1221INLINE void 1243ecb_inline void
1222free_coro_mortal (pTHX) 1244free_coro_mortal (pTHX)
1223{ 1245{
1224 if (expect_true (coro_mortal)) 1246 if (ecb_expect_true (coro_mortal))
1225 { 1247 {
1226 SvREFCNT_dec ((SV *)coro_mortal); 1248 SvREFCNT_dec ((SV *)coro_mortal);
1227 coro_mortal = 0; 1249 coro_mortal = 0;
1228 } 1250 }
1229} 1251}
1364 1386
1365 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1387 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1366} 1388}
1367 1389
1368/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1390/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1369static void NOINLINE 1391static void ecb_noinline
1370cctx_prepare (pTHX) 1392cctx_prepare (pTHX)
1371{ 1393{
1372 PL_top_env = &PL_start_env; 1394 PL_top_env = &PL_start_env;
1373 1395
1374 if (cctx_current->flags & CC_TRACE) 1396 if (cctx_current->flags & CC_TRACE)
1385 slf_frame.prepare = slf_prepare_set_stacklevel; 1407 slf_frame.prepare = slf_prepare_set_stacklevel;
1386 slf_frame.check = slf_check_set_stacklevel; 1408 slf_frame.check = slf_check_set_stacklevel;
1387} 1409}
1388 1410
1389/* the tail of transfer: execute stuff we can only do after a transfer */ 1411/* the tail of transfer: execute stuff we can only do after a transfer */
1390INLINE void 1412ecb_inline void
1391transfer_tail (pTHX) 1413transfer_tail (pTHX)
1392{ 1414{
1393 free_coro_mortal (aTHX); 1415 free_coro_mortal (aTHX);
1416}
1417
1418/* try to exit the same way perl's main function would do */
1419/* we do not bother resetting the environment or other things *7
1420/* that are not, uhm, essential */
1421/* this obviously also doesn't work when perl is embedded */
1422static void ecb_noinline ecb_cold
1423perlish_exit (pTHX)
1424{
1425 int exitstatus = perl_destruct (PL_curinterp);
1426 perl_free (PL_curinterp);
1427 exit (exitstatus);
1394} 1428}
1395 1429
1396/* 1430/*
1397 * this is a _very_ stripped down perl interpreter ;) 1431 * this is a _very_ stripped down perl interpreter ;)
1398 */ 1432 */
1427 */ 1461 */
1428 1462
1429 /* 1463 /*
1430 * If perl-run returns we assume exit() was being called or the coro 1464 * If perl-run returns we assume exit() was being called or the coro
1431 * fell off the end, which seems to be the only valid (non-bug) 1465 * fell off the end, which seems to be the only valid (non-bug)
1432 * reason for perl_run to return. We try to exit by jumping to the 1466 * reason for perl_run to return. We try to mimic whatever perl is normally
1433 * bootstrap-time "top" top_env, as we cannot restore the "main" 1467 * doing in that case. YMMV.
1434 * coroutine as Coro has no such concept.
1435 * This actually isn't valid with the pthread backend, but OSes requiring
1436 * that backend are too broken to do it in a standards-compliant way.
1437 */ 1468 */
1438 PL_top_env = main_top_env; 1469 perlish_exit (aTHX);
1439 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1440 } 1470 }
1441} 1471}
1442 1472
1443static coro_cctx * 1473static coro_cctx *
1444cctx_new () 1474cctx_new (void)
1445{ 1475{
1446 coro_cctx *cctx; 1476 coro_cctx *cctx;
1447 1477
1448 ++cctx_count; 1478 ++cctx_count;
1449 New (0, cctx, 1, coro_cctx); 1479 New (0, cctx, 1, coro_cctx);
1455 return cctx; 1485 return cctx;
1456} 1486}
1457 1487
1458/* create a new cctx only suitable as source */ 1488/* create a new cctx only suitable as source */
1459static coro_cctx * 1489static coro_cctx *
1460cctx_new_empty () 1490cctx_new_empty (void)
1461{ 1491{
1462 coro_cctx *cctx = cctx_new (); 1492 coro_cctx *cctx = cctx_new ();
1463 1493
1464 cctx->sptr = 0; 1494 cctx->sptr = 0;
1465 coro_create (&cctx->cctx, 0, 0, 0, 0); 1495 coro_create (&cctx->cctx, 0, 0, 0, 0);
1467 return cctx; 1497 return cctx;
1468} 1498}
1469 1499
1470/* create a new cctx suitable as destination/running a perl interpreter */ 1500/* create a new cctx suitable as destination/running a perl interpreter */
1471static coro_cctx * 1501static coro_cctx *
1472cctx_new_run () 1502cctx_new_run (void)
1473{ 1503{
1474 coro_cctx *cctx = cctx_new (); 1504 coro_cctx *cctx = cctx_new ();
1475 void *stack_start; 1505 void *stack_start;
1476 size_t stack_size; 1506 size_t stack_size;
1477 1507
1478#if HAVE_MMAP 1508#if HAVE_MMAP
1479 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1509 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1480 /* mmap supposedly does allocate-on-write for us */ 1510 /* mmap supposedly does allocate-on-write for us */
1481 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1511 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, -1, 0);
1482 1512
1483 if (cctx->sptr != (void *)-1) 1513 if (cctx->sptr != (void *)-1)
1484 { 1514 {
1485 #if CORO_STACKGUARD 1515 #if CORO_STACKGUARD
1486 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1516 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1547#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) 1577#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1548 1578
1549static coro_cctx * 1579static coro_cctx *
1550cctx_get (pTHX) 1580cctx_get (pTHX)
1551{ 1581{
1552 while (expect_true (cctx_first)) 1582 while (ecb_expect_true (cctx_first))
1553 { 1583 {
1554 coro_cctx *cctx = cctx_first; 1584 coro_cctx *cctx = cctx_first;
1555 cctx_first = cctx->next; 1585 cctx_first = cctx->next;
1556 --cctx_idle; 1586 --cctx_idle;
1557 1587
1558 if (expect_true (!CCTX_EXPIRED (cctx))) 1588 if (ecb_expect_true (!CCTX_EXPIRED (cctx)))
1559 return cctx; 1589 return cctx;
1560 1590
1561 cctx_destroy (cctx); 1591 cctx_destroy (cctx);
1562 } 1592 }
1563 1593
1568cctx_put (coro_cctx *cctx) 1598cctx_put (coro_cctx *cctx)
1569{ 1599{
1570 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); 1600 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
1571 1601
1572 /* free another cctx if overlimit */ 1602 /* free another cctx if overlimit */
1573 if (expect_false (cctx_idle >= cctx_max_idle)) 1603 if (ecb_expect_false (cctx_idle >= cctx_max_idle))
1574 { 1604 {
1575 coro_cctx *first = cctx_first; 1605 coro_cctx *first = cctx_first;
1576 cctx_first = first->next; 1606 cctx_first = first->next;
1577 --cctx_idle; 1607 --cctx_idle;
1578 1608
1589static void 1619static void
1590transfer_check (pTHX_ struct coro *prev, struct coro *next) 1620transfer_check (pTHX_ struct coro *prev, struct coro *next)
1591{ 1621{
1592 /* TODO: throwing up here is considered harmful */ 1622 /* TODO: throwing up here is considered harmful */
1593 1623
1594 if (expect_true (prev != next)) 1624 if (ecb_expect_true (prev != next))
1595 { 1625 {
1596 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1626 if (ecb_expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1597 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); 1627 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1598 1628
1599 if (expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED))) 1629 if (ecb_expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED)))
1600 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); 1630 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1601 1631
1602#if !PERL_VERSION_ATLEAST (5,10,0) 1632#if !PERL_VERSION_ATLEAST (5,10,0)
1603 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1633 if (ecb_expect_false (PL_lex_state != LEX_NOTPARSING))
1604 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); 1634 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1605#endif 1635#endif
1606 } 1636 }
1607} 1637}
1608 1638
1609/* always use the TRANSFER macro */ 1639/* always use the TRANSFER macro */
1610static void NOINLINE /* noinline so we have a fixed stackframe */ 1640static void ecb_noinline /* noinline so we have a fixed stackframe */
1611transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1641transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1612{ 1642{
1613 dSTACKLEVEL; 1643 dSTACKLEVEL;
1614 1644
1615 /* sometimes transfer is only called to set idle_sp */ 1645 /* sometimes transfer is only called to set idle_sp */
1616 if (expect_false (!prev)) 1646 if (ecb_expect_false (!prev))
1617 { 1647 {
1618 cctx_current->idle_sp = STACKLEVEL; 1648 cctx_current->idle_sp = STACKLEVEL;
1619 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1649 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1620 } 1650 }
1621 else if (expect_true (prev != next)) 1651 else if (ecb_expect_true (prev != next))
1622 { 1652 {
1623 coro_cctx *cctx_prev; 1653 coro_cctx *cctx_prev;
1624 1654
1625 if (expect_false (prev->flags & CF_NEW)) 1655 if (ecb_expect_false (prev->flags & CF_NEW))
1626 { 1656 {
1627 /* create a new empty/source context */ 1657 /* create a new empty/source context */
1628 prev->flags &= ~CF_NEW; 1658 prev->flags &= ~CF_NEW;
1629 prev->flags |= CF_RUNNING; 1659 prev->flags |= CF_RUNNING;
1630 } 1660 }
1633 next->flags |= CF_RUNNING; 1663 next->flags |= CF_RUNNING;
1634 1664
1635 /* first get rid of the old state */ 1665 /* first get rid of the old state */
1636 save_perl (aTHX_ prev); 1666 save_perl (aTHX_ prev);
1637 1667
1638 if (expect_false (next->flags & CF_NEW)) 1668 if (ecb_expect_false (next->flags & CF_NEW))
1639 { 1669 {
1640 /* need to start coroutine */ 1670 /* need to start coroutine */
1641 next->flags &= ~CF_NEW; 1671 next->flags &= ~CF_NEW;
1642 /* setup coroutine call */ 1672 /* setup coroutine call */
1643 init_perl (aTHX_ next); 1673 init_perl (aTHX_ next);
1644 } 1674 }
1645 else 1675 else
1646 load_perl (aTHX_ next); 1676 load_perl (aTHX_ next);
1647 1677
1648 /* possibly untie and reuse the cctx */ 1678 /* possibly untie and reuse the cctx */
1649 if (expect_true ( 1679 if (ecb_expect_true (
1650 cctx_current->idle_sp == STACKLEVEL 1680 cctx_current->idle_sp == STACKLEVEL
1651 && !(cctx_current->flags & CC_TRACE) 1681 && !(cctx_current->flags & CC_TRACE)
1652 && !force_cctx 1682 && !force_cctx
1653 )) 1683 ))
1654 { 1684 {
1655 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1685 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1656 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); 1686 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1657 1687
1658 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ 1688 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1659 /* without this the next cctx_get might destroy the running cctx while still in use */ 1689 /* without this the next cctx_get might destroy the running cctx while still in use */
1660 if (expect_false (CCTX_EXPIRED (cctx_current))) 1690 if (ecb_expect_false (CCTX_EXPIRED (cctx_current)))
1661 if (expect_true (!next->cctx)) 1691 if (ecb_expect_true (!next->cctx))
1662 next->cctx = cctx_get (aTHX); 1692 next->cctx = cctx_get (aTHX);
1663 1693
1664 cctx_put (cctx_current); 1694 cctx_put (cctx_current);
1665 } 1695 }
1666 else 1696 else
1667 prev->cctx = cctx_current; 1697 prev->cctx = cctx_current;
1668 1698
1669 ++next->usecount; 1699 ++next->usecount;
1670 1700
1671 cctx_prev = cctx_current; 1701 cctx_prev = cctx_current;
1672 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); 1702 cctx_current = ecb_expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1673 1703
1674 next->cctx = 0; 1704 next->cctx = 0;
1675 1705
1676 if (expect_false (cctx_prev != cctx_current)) 1706 if (ecb_expect_false (cctx_prev != cctx_current))
1677 { 1707 {
1678 cctx_prev->top_env = PL_top_env; 1708 cctx_prev->top_env = PL_top_env;
1679 PL_top_env = cctx_current->top_env; 1709 PL_top_env = cctx_current->top_env;
1680 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); 1710 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1681 } 1711 }
1727 SvREFCNT_dec (coro->startcv); 1757 SvREFCNT_dec (coro->startcv);
1728 SvREFCNT_dec (coro->args); 1758 SvREFCNT_dec (coro->args);
1729 SvREFCNT_dec (coro->swap_sv); 1759 SvREFCNT_dec (coro->swap_sv);
1730 SvREFCNT_dec (CORO_THROW); 1760 SvREFCNT_dec (CORO_THROW);
1731 1761
1732 coro_call_on_destroy (coro); 1762 coro_call_on_destroy (aTHX_ coro);
1733 1763
1734 /* more destruction mayhem in coro_state_free */ 1764 /* more destruction mayhem in coro_state_free */
1735} 1765}
1736 1766
1737static int 1767static int
1738coro_state_free (pTHX_ SV *sv, MAGIC *mg) 1768coro_state_free (pTHX_ SV *sv, MAGIC *mg)
1739{ 1769{
1740 struct coro *coro = (struct coro *)mg->mg_ptr; 1770 struct coro *coro = (struct coro *)mg->mg_ptr;
1741 mg->mg_ptr = 0; 1771 mg->mg_ptr = 0;
1742 1772
1743 coro_state_destroy (coro); 1773 coro_state_destroy (aTHX_ coro);
1744 SvREFCNT_dec (coro->on_destroy); 1774 SvREFCNT_dec (coro->on_destroy);
1745 SvREFCNT_dec (coro->status); 1775 SvREFCNT_dec (coro->status);
1746 1776
1747 Safefree (coro); 1777 Safefree (coro);
1748 1778
1749 return 0; 1779 return 0;
1750} 1780}
1751 1781
1752static int 1782static int ecb_cold
1753coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) 1783coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
1754{ 1784{
1755 /* called when perl clones the current process the slow way (windows process emulation) */ 1785 /* called when perl clones the current process the slow way (windows process emulation) */
1756 /* WE SIMply nuke the pointers in the copy, causing perl to croak */ 1786 /* WE SIMply nuke the pointers in the copy, causing perl to croak */
1757 mg->mg_ptr = 0; 1787 mg->mg_ptr = 0;
1788 TRANSFER (ta, 1); 1818 TRANSFER (ta, 1);
1789} 1819}
1790 1820
1791/** Coro ********************************************************************/ 1821/** Coro ********************************************************************/
1792 1822
1793INLINE void 1823ecb_inline void
1794coro_enq (pTHX_ struct coro *coro) 1824coro_enq (pTHX_ struct coro *coro)
1795{ 1825{
1796 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; 1826 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN];
1797 1827
1798 SvREFCNT_inc_NN (coro->hv); 1828 SvREFCNT_inc_NN (coro->hv);
1800 coro->next_ready = 0; 1830 coro->next_ready = 0;
1801 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; 1831 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1802 ready [1] = coro; 1832 ready [1] = coro;
1803} 1833}
1804 1834
1805INLINE struct coro * 1835ecb_inline struct coro *
1806coro_deq (pTHX) 1836coro_deq (pTHX)
1807{ 1837{
1808 int prio; 1838 int prio;
1809 1839
1810 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) 1840 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; )
1863{ 1893{
1864 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1894 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1865} 1895}
1866 1896
1867/* expects to own a reference to next->hv */ 1897/* expects to own a reference to next->hv */
1868INLINE void 1898ecb_inline void
1869prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) 1899prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1870{ 1900{
1871 SV *prev_sv = SvRV (coro_current); 1901 SV *prev_sv = SvRV (coro_current);
1872 1902
1873 ta->prev = SvSTATE_hv (prev_sv); 1903 ta->prev = SvSTATE_hv (prev_sv);
1886{ 1916{
1887 for (;;) 1917 for (;;)
1888 { 1918 {
1889 struct coro *next = coro_deq (aTHX); 1919 struct coro *next = coro_deq (aTHX);
1890 1920
1891 if (expect_true (next)) 1921 if (ecb_expect_true (next))
1892 { 1922 {
1893 /* cannot transfer to destroyed coros, skip and look for next */ 1923 /* cannot transfer to destroyed coros, skip and look for next */
1894 if (expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED))) 1924 if (ecb_expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED)))
1895 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ 1925 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1896 else 1926 else
1897 { 1927 {
1898 next->flags &= ~CF_READY; 1928 next->flags &= ~CF_READY;
1899 --coro_nready; 1929 --coro_nready;
1932 } 1962 }
1933 } 1963 }
1934 } 1964 }
1935} 1965}
1936 1966
1937INLINE void 1967ecb_inline void
1938prepare_cede (pTHX_ struct coro_transfer_args *ta) 1968prepare_cede (pTHX_ struct coro_transfer_args *ta)
1939{ 1969{
1940 api_ready (aTHX_ coro_current); 1970 api_ready (aTHX_ coro_current);
1941 prepare_schedule (aTHX_ ta); 1971 prepare_schedule (aTHX_ ta);
1942} 1972}
1943 1973
1944INLINE void 1974ecb_inline void
1945prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) 1975prepare_cede_notself (pTHX_ struct coro_transfer_args *ta)
1946{ 1976{
1947 SV *prev = SvRV (coro_current); 1977 SV *prev = SvRV (coro_current);
1948 1978
1949 if (coro_nready) 1979 if (coro_nready)
1979{ 2009{
1980 struct coro_transfer_args ta; 2010 struct coro_transfer_args ta;
1981 2011
1982 prepare_cede (aTHX_ &ta); 2012 prepare_cede (aTHX_ &ta);
1983 2013
1984 if (expect_true (ta.prev != ta.next)) 2014 if (ecb_expect_true (ta.prev != ta.next))
1985 { 2015 {
1986 TRANSFER (ta, 1); 2016 TRANSFER (ta, 1);
1987 return 1; 2017 return 1;
1988 } 2018 }
1989 else 2019 else
2054 PUTBACK; 2084 PUTBACK;
2055 } 2085 }
2056} 2086}
2057 2087
2058static void 2088static void
2059coro_push_on_destroy (aTHX_ struct coro *coro, SV *cb) 2089coro_push_on_destroy (pTHX_ struct coro *coro, SV *cb)
2060{ 2090{
2061 if (!coro->on_destroy) 2091 if (!coro->on_destroy)
2062 coro->on_destroy = newAV (); 2092 coro->on_destroy = newAV ();
2063 2093
2064 av_push (coro->on_destroy, cb); 2094 av_push (coro->on_destroy, cb);
2078 if (!coro->status) 2108 if (!coro->status)
2079 return 1; 2109 return 1;
2080 2110
2081 frame->destroy = 0; 2111 frame->destroy = 0;
2082 2112
2083 coro_push_av (coro->status, GIMME_V); 2113 coro_push_av (aTHX_ coro->status, GIMME_V);
2084 2114
2085 SvREFCNT_dec ((SV *)coro->hv); 2115 SvREFCNT_dec ((SV *)coro->hv);
2086 2116
2087 return 0; 2117 return 0;
2088} 2118}
2202 coro = SvSTATE (arg [0]); 2232 coro = SvSTATE (arg [0]);
2203 coro_hv = coro->hv; 2233 coro_hv = coro->hv;
2204 2234
2205 coro_set_status (aTHX_ coro, arg + 1, items - 1); 2235 coro_set_status (aTHX_ coro, arg + 1, items - 1);
2206 2236
2207 if (expect_false (coro->flags & CF_NOCANCEL)) 2237 if (ecb_expect_false (coro->flags & CF_NOCANCEL))
2208 { 2238 {
2209 /* coro currently busy cancelling something, so just notify it */ 2239 /* coro currently busy cancelling something, so just notify it */
2210 coro->slf_frame.data = (void *)coro; 2240 coro->slf_frame.data = (void *)coro;
2211 2241
2212 frame->prepare = prepare_nop; 2242 frame->prepare = prepare_nop;
2320slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2350slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2321{ 2351{
2322 HV *hv = (HV *)SvRV (coro_current); 2352 HV *hv = (HV *)SvRV (coro_current);
2323 struct coro *coro = SvSTATE_hv ((SV *)hv); 2353 struct coro *coro = SvSTATE_hv ((SV *)hv);
2324 2354
2325 if (expect_true (coro->saved_deffh)) 2355 if (ecb_expect_true (coro->saved_deffh))
2326 { 2356 {
2327 /* subsequent iteration */ 2357 /* subsequent iteration */
2328 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2358 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2329 coro->saved_deffh = 0; 2359 coro->saved_deffh = 0;
2330 2360
2596 I32 checkmark; /* mark SP to see how many elements check has pushed */ 2626 I32 checkmark; /* mark SP to see how many elements check has pushed */
2597 2627
2598 /* set up the slf frame, unless it has already been set-up */ 2628 /* set up the slf frame, unless it has already been set-up */
2599 /* the latter happens when a new coro has been started */ 2629 /* the latter happens when a new coro has been started */
2600 /* or when a new cctx was attached to an existing coroutine */ 2630 /* or when a new cctx was attached to an existing coroutine */
2601 if (expect_true (!slf_frame.prepare)) 2631 if (ecb_expect_true (!slf_frame.prepare))
2602 { 2632 {
2603 /* first iteration */ 2633 /* first iteration */
2604 dSP; 2634 dSP;
2605 SV **arg = PL_stack_base + TOPMARK + 1; 2635 SV **arg = PL_stack_base + TOPMARK + 1;
2606 int items = SP - arg; /* args without function object */ 2636 int items = SP - arg; /* args without function object */
2647 while (slf_frame.check (aTHX_ &slf_frame)); 2677 while (slf_frame.check (aTHX_ &slf_frame));
2648 2678
2649 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ 2679 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */
2650 2680
2651 /* exception handling */ 2681 /* exception handling */
2652 if (expect_false (CORO_THROW)) 2682 if (ecb_expect_false (CORO_THROW))
2653 { 2683 {
2654 SV *exception = sv_2mortal (CORO_THROW); 2684 SV *exception = sv_2mortal (CORO_THROW);
2655 2685
2656 CORO_THROW = 0; 2686 CORO_THROW = 0;
2657 sv_setsv (ERRSV, exception); 2687 sv_setsv (ERRSV, exception);
2659 } 2689 }
2660 2690
2661 /* return value handling - mostly like entersub */ 2691 /* return value handling - mostly like entersub */
2662 /* make sure we put something on the stack in scalar context */ 2692 /* make sure we put something on the stack in scalar context */
2663 if (GIMME_V == G_SCALAR 2693 if (GIMME_V == G_SCALAR
2664 && expect_false (PL_stack_sp != PL_stack_base + checkmark + 1)) 2694 && ecb_expect_false (PL_stack_sp != PL_stack_base + checkmark + 1))
2665 { 2695 {
2666 dSP; 2696 dSP;
2667 SV **bot = PL_stack_base + checkmark; 2697 SV **bot = PL_stack_base + checkmark;
2668 2698
2669 if (sp == bot) /* too few, push undef */ 2699 if (sp == bot) /* too few, push undef */
2783{ 2813{
2784 PerlIOBuf base; 2814 PerlIOBuf base;
2785 NV next, every; 2815 NV next, every;
2786} PerlIOCede; 2816} PerlIOCede;
2787 2817
2788static IV 2818static IV ecb_cold
2789PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) 2819PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2790{ 2820{
2791 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 2821 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
2792 2822
2793 self->every = SvCUR (arg) ? SvNV (arg) : 0.01; 2823 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
2794 self->next = nvtime () + self->every; 2824 self->next = nvtime () + self->every;
2795 2825
2796 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); 2826 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
2797} 2827}
2798 2828
2799static SV * 2829static SV * ecb_cold
2800PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) 2830PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
2801{ 2831{
2802 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 2832 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
2803 2833
2804 return newSVnv (self->every); 2834 return newSVnv (self->every);
2949 { 2979 {
2950 int i; 2980 int i;
2951 /* if we were woken up but can't down, we look through the whole */ 2981 /* if we were woken up but can't down, we look through the whole */
2952 /* waiters list and only add us if we aren't in there already */ 2982 /* waiters list and only add us if we aren't in there already */
2953 /* this avoids some degenerate memory usage cases */ 2983 /* this avoids some degenerate memory usage cases */
2954 for (i = AvFILLp (av); i > 0; --i) // i > 0 is not an off-by-one bug 2984 for (i = AvFILLp (av); i > 0; --i) /* i > 0 is not an off-by-one bug */
2955 if (AvARRAY (av)[i] == coro_hv) 2985 if (AvARRAY (av)[i] == coro_hv)
2956 return 1; 2986 return 1;
2957 2987
2958 av_push (av, SvREFCNT_inc (coro_hv)); 2988 av_push (av, SvREFCNT_inc (coro_hv));
2959 return 1; 2989 return 1;
3216 dSP; 3246 dSP;
3217 3247
3218 static SV *prio_cv; 3248 static SV *prio_cv;
3219 static SV *prio_sv; 3249 static SV *prio_sv;
3220 3250
3221 if (expect_false (!prio_cv)) 3251 if (ecb_expect_false (!prio_cv))
3222 { 3252 {
3223 prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0); 3253 prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0);
3224 prio_sv = newSViv (0); 3254 prio_sv = newSViv (0);
3225 } 3255 }
3226 3256
3332 } 3362 }
3333 3363
3334 return coro_sv; 3364 return coro_sv;
3335} 3365}
3336 3366
3367#ifndef __cplusplus
3368ecb_cold XS(boot_Coro__State);
3369#endif
3370
3371#if CORO_JIT
3372
3373static void ecb_noinline ecb_cold
3374pushav_4uv (pTHX_ UV a, UV b, UV c, UV d)
3375{
3376 dSP;
3377 AV *av = newAV ();
3378
3379 av_store (av, 3, newSVuv (d));
3380 av_store (av, 2, newSVuv (c));
3381 av_store (av, 1, newSVuv (b));
3382 av_store (av, 0, newSVuv (a));
3383
3384 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
3385
3386 PUTBACK;
3387}
3388
3389static void ecb_noinline ecb_cold
3390jit_init (pTHX)
3391{
3392 dSP;
3393 SV *load, *save;
3394 char *map_base;
3395 char *load_ptr, *save_ptr;
3396 STRLEN load_len, save_len, map_len;
3397 int count;
3398
3399 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1);
3400
3401 PUSHMARK (SP);
3402 #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type));
3403 #include "state.h"
3404 count = call_pv ("Coro::State::_jit", G_ARRAY);
3405 SPAGAIN;
3406
3407 save = POPs; save_ptr = SvPVbyte (save, save_len);
3408 load = POPs; load_ptr = SvPVbyte (load, load_len);
3409
3410 map_len = load_len + save_len + 16;
3411
3412 map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3413
3414 assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED));
3415
3416 load_perl_slots = (load_save_perl_slots_type)map_base;
3417 memcpy (map_base, load_ptr, load_len);
3418
3419 map_base += (load_len + 15) & ~15;
3420
3421 save_perl_slots = (load_save_perl_slots_type)map_base;
3422 memcpy (map_base, save_ptr, save_len);
3423
3424 /* we are good citizens and try to make the page read-only, so the evil evil */
3425 /* hackers might have it a bit more difficult */
3426 mprotect (map_base, map_len, PROT_READ | PROT_EXEC);
3427
3428 PUTBACK;
3429 eval_pv ("undef &Coro::State::_jit", 1);
3430}
3431
3432#endif
3433
3337MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 3434MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
3338 3435
3339PROTOTYPES: DISABLE 3436PROTOTYPES: DISABLE
3340 3437
3341BOOT: 3438BOOT:
3344# if CORO_PTHREAD 3441# if CORO_PTHREAD
3345 coro_thx = PERL_GET_CONTEXT; 3442 coro_thx = PERL_GET_CONTEXT;
3346# endif 3443# endif
3347#endif 3444#endif
3348 BOOT_PAGESIZE; 3445 BOOT_PAGESIZE;
3446
3447 /* perl defines these to check for existance first, but why it doesn't */
3448 /* just create them one at init time is not clear to me, except for */
3449 /* programs trying to delete them, but... */
3450 /* anyway, we declare this as invalid and make sure they are initialised here */
3451 DEFSV;
3452 ERRSV;
3349 3453
3350 cctx_current = cctx_new_empty (); 3454 cctx_current = cctx_new_empty ();
3351 3455
3352 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 3456 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
3353 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 3457 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
3396 coroapi.prepare_cede_notself = prepare_cede_notself; 3500 coroapi.prepare_cede_notself = prepare_cede_notself;
3397 3501
3398 time_init (aTHX); 3502 time_init (aTHX);
3399 3503
3400 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3504 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3505#if CORO_JIT
3506 PUTBACK;
3507 jit_init (aTHX);
3508 SPAGAIN;
3509#endif
3401} 3510}
3402 3511
3403SV * 3512SV *
3404new (SV *klass, ...) 3513new (SV *klass, ...)
3405 ALIAS: 3514 ALIAS:
3556 RETVAL = boolSV (coro->flags & ix); 3665 RETVAL = boolSV (coro->flags & ix);
3557 OUTPUT: 3666 OUTPUT:
3558 RETVAL 3667 RETVAL
3559 3668
3560void 3669void
3561throw (Coro::State self, SV *exception = &PL_sv_undef) 3670throw (SV *self, SV *exception = &PL_sv_undef)
3562 PROTOTYPE: $;$ 3671 PROTOTYPE: $;$
3563 CODE: 3672 CODE:
3564{ 3673{
3674 struct coro *coro = SvSTATE (self);
3565 struct coro *current = SvSTATE_current; 3675 struct coro *current = SvSTATE_current;
3566 SV **exceptionp = self == current ? &CORO_THROW : &self->except; 3676 SV **exceptionp = coro == current ? &CORO_THROW : &coro->except;
3567 SvREFCNT_dec (*exceptionp); 3677 SvREFCNT_dec (*exceptionp);
3568 SvGETMAGIC (exception); 3678 SvGETMAGIC (exception);
3569 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; 3679 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3680
3681 api_ready (aTHX_ self);
3570} 3682}
3571 3683
3572void 3684void
3573api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3685api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3574 PROTOTYPE: $;$ 3686 PROTOTYPE: $;$
3653times (Coro::State self) 3765times (Coro::State self)
3654 PPCODE: 3766 PPCODE:
3655{ 3767{
3656 struct coro *current = SvSTATE (coro_current); 3768 struct coro *current = SvSTATE (coro_current);
3657 3769
3658 if (expect_false (current == self)) 3770 if (ecb_expect_false (current == self))
3659 { 3771 {
3660 coro_times_update (); 3772 coro_times_update ();
3661 coro_times_add (SvSTATE (coro_current)); 3773 coro_times_add (SvSTATE (coro_current));
3662 } 3774 }
3663 3775
3664 EXTEND (SP, 2); 3776 EXTEND (SP, 2);
3665 PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); 3777 PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9)));
3666 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); 3778 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9)));
3667 3779
3668 if (expect_false (current == self)) 3780 if (ecb_expect_false (current == self))
3669 coro_times_sub (SvSTATE (coro_current)); 3781 coro_times_sub (SvSTATE (coro_current));
3670} 3782}
3671 3783
3672void 3784void
3673swap_sv (Coro::State coro, SV *sv, SV *swapsv) 3785swap_sv (Coro::State coro, SV *sv, SV *swapsv)
4055} 4167}
4056 4168
4057MODULE = Coro::State PACKAGE = Coro::SemaphoreSet 4169MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
4058 4170
4059void 4171void
4060_may_delete (SV *sem, int count, int extra_refs) 4172_may_delete (SV *sem, int count, unsigned int extra_refs)
4061 PPCODE: 4173 PPCODE:
4062{ 4174{
4063 AV *av = (AV *)SvRV (sem); 4175 AV *av = (AV *)SvRV (sem);
4064 4176
4065 if (SvREFCNT ((SV *)av) == 1 + extra_refs 4177 if (SvREFCNT ((SV *)av) == 1 + extra_refs

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines