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.403 by root, Sun May 22 20:13:33 2011 UTC vs.
Revision 1.426 by root, Wed Dec 5 00:19:57 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
18#include <stddef.h> 21#include <stddef.h>
19#include <stdio.h> 22#include <stdio.h>
20#include <errno.h> 23#include <errno.h>
21#include <assert.h> 24#include <assert.h>
22 25
23#ifndef SVs_PADSTALE 26#ifndef SVs_PADSTALE
24# define SVs_PADSTALE 0 27# define SVs_PADSTALE 0
28#endif
29
30#ifdef PadARRAY
31# define NEWPADAPI
32# define newPADLIST(var) (Newz (0, var, 1, PADLIST), Newx (PadlistARRAY (var), 2, PAD *))
33#else
34typedef AV PADNAMELIST;
35# if !PERL_VERSION_ATLEAST(5,8,0)
36typedef AV PADLIST;
37typedef AV PAD;
38# endif
39# define PadlistARRAY(pl) ((PAD **)AvARRAY (pl))
40# define PadlistMAX(pl) AvFILLp (pl)
41# define PadlistNAMES(pl) (*PadlistARRAY (pl))
42# define PadARRAY AvARRAY
43# define PadMAX AvFILLp
44# define newPADLIST(var) ((var) = newAV (), av_extend (var, 1))
25#endif 45#endif
26 46
27#if defined(_WIN32) 47#if defined(_WIN32)
28# undef HAS_GETTIMEOFDAY 48# undef HAS_GETTIMEOFDAY
29# undef setjmp 49# undef setjmp
32# define setjmp _setjmp /* deep magic */ 52# define setjmp _setjmp /* deep magic */
33#else 53#else
34# include <inttypes.h> /* most portable stdint.h */ 54# include <inttypes.h> /* most portable stdint.h */
35#endif 55#endif
36 56
37#ifdef HAVE_MMAP 57#if HAVE_MMAP
38# include <unistd.h> 58# include <unistd.h>
39# include <sys/mman.h> 59# include <sys/mman.h>
40# ifndef MAP_ANONYMOUS 60# ifndef MAP_ANONYMOUS
41# ifdef MAP_ANON 61# ifdef MAP_ANON
42# define MAP_ANONYMOUS MAP_ANON 62# define MAP_ANONYMOUS MAP_ANON
62#endif 82#endif
63 83
64/* the maximum number of idle cctx that will be pooled */ 84/* the maximum number of idle cctx that will be pooled */
65static int cctx_max_idle = 4; 85static int cctx_max_idle = 4;
66 86
87#if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0)
88# define HAS_SCOPESTACK_NAME 1
89#endif
90
67#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 91#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
68# undef CORO_STACKGUARD 92# undef CORO_STACKGUARD
69#endif 93#endif
70 94
71#ifndef CORO_STACKGUARD 95#ifndef CORO_STACKGUARD
87# define STACKLEVEL ((void *)&stacklevel) 111# define STACKLEVEL ((void *)&stacklevel)
88#endif 112#endif
89 113
90#define IN_DESTRUCT PL_dirty 114#define IN_DESTRUCT PL_dirty
91 115
92#if __GNUC__ >= 3
93# define attribute(x) __attribute__(x)
94# define expect(expr,value) __builtin_expect ((expr), (value))
95# define INLINE static inline
96#else
97# define attribute(x)
98# define expect(expr,value) (expr)
99# define INLINE static
100#endif
101
102#define expect_false(expr) expect ((expr) != 0, 0)
103#define expect_true(expr) expect ((expr) != 0, 1)
104
105#define NOINLINE attribute ((noinline))
106
107#include "CoroAPI.h" 116#include "CoroAPI.h"
108#define GCoroAPI (&coroapi) /* very sneaky */ 117#define GCoroAPI (&coroapi) /* very sneaky */
109 118
110#ifdef USE_ITHREADS 119#ifdef USE_ITHREADS
111# if CORO_PTHREAD 120# if CORO_PTHREAD
188 void *sptr; 197 void *sptr;
189 size_t ssize; 198 size_t ssize;
190 199
191 /* cpu state */ 200 /* cpu state */
192 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 201 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
202#ifndef NDEBUG
193 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 203 JMPENV *idle_te; /* same as idle_sp, but for top_env */
204#endif
194 JMPENV *top_env; 205 JMPENV *top_env;
195 coro_context cctx; 206 coro_context cctx;
196 207
197 U32 gen; 208 U32 gen;
198#if CORO_USE_VALGRIND 209#if CORO_USE_VALGRIND
218}; 229};
219 230
220/* the structure where most of the perl state is stored, overlaid on the cxstack */ 231/* the structure where most of the perl state is stored, overlaid on the cxstack */
221typedef struct 232typedef struct
222{ 233{
223 SV *defsv;
224 AV *defav;
225 SV *errsv;
226 SV *irsgv;
227 HV *hinthv;
228#define VAR(name,type) type name; 234 #define VARx(name,expr,type) type name;
229# include "state.h" 235 #include "state.h"
230#undef VAR
231} perl_slots; 236} perl_slots;
232 237
233// how many context stack entries do we need for perl_slots 238/* how many context stack entries do we need for perl_slots */
234#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) 239#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
235 240
236/* this is a structure representing a perl-level coroutine */ 241/* this is a structure representing a perl-level coroutine */
237struct coro 242struct coro
238{ 243{
304static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ 309static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
305static CV *cv_coro_run; 310static CV *cv_coro_run;
306static struct coro *coro_first; 311static struct coro *coro_first;
307#define coro_nready coroapi.nready 312#define coro_nready coroapi.nready
308 313
314/** JIT *********************************************************************/
315
316#if CORO_JIT
317 /* APPLE doesn't have HAVE_MMAP though */
318 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__)
319 #ifndef CORO_JIT_TYPE
320 #if __x86_64 && CORO_JIT_UNIXY
321 #define CORO_JIT_TYPE "amd64-unix"
322 #elif __i386 && CORO_JIT_UNIXY
323 #define CORO_JIT_TYPE "x86-unix"
324 #endif
325 #endif
326#endif
327
328#if !defined(CORO_JIT_TYPE) || !HAVE_MMAP
329 #undef CORO_JIT
330#endif
331
332#if CORO_JIT
333 typedef void (*load_save_perl_slots_type)(perl_slots *);
334 static load_save_perl_slots_type load_perl_slots, save_perl_slots;
335#endif
336
309/** Coro::Select ************************************************************/ 337/** Coro::Select ************************************************************/
310 338
311static OP *(*coro_old_pp_sselect) (pTHX); 339static OP *(*coro_old_pp_sselect) (pTHX);
312static SV *coro_select_select; 340static SV *coro_select_select;
313 341
328 356
329/** time stuff **************************************************************/ 357/** time stuff **************************************************************/
330 358
331#ifdef HAS_GETTIMEOFDAY 359#ifdef HAS_GETTIMEOFDAY
332 360
333static void 361ecb_inline void
334coro_u2time (pTHX_ UV ret[2]) 362coro_u2time (pTHX_ UV ret[2])
335{ 363{
336 struct timeval tv; 364 struct timeval tv;
337 gettimeofday (&tv, 0); 365 gettimeofday (&tv, 0);
338 366
339 ret [0] = tv.tv_sec; 367 ret [0] = tv.tv_sec;
340 ret [1] = tv.tv_usec; 368 ret [1] = tv.tv_usec;
341} 369}
342 370
343static double 371ecb_inline double
344coro_nvtime () 372coro_nvtime (void)
345{ 373{
346 struct timeval tv; 374 struct timeval tv;
347 gettimeofday (&tv, 0); 375 gettimeofday (&tv, 0);
348 376
349 return tv.tv_sec + tv.tv_usec * 1e-6; 377 return tv.tv_sec + tv.tv_usec * 1e-6;
350} 378}
351 379
352static void 380ecb_inline void
353time_init (pTHX) 381time_init (pTHX)
354{ 382{
355 nvtime = coro_nvtime; 383 nvtime = coro_nvtime;
356 u2time = coro_u2time; 384 u2time = coro_u2time;
357} 385}
358 386
359#else 387#else
360 388
361static void 389ecb_inline void
362time_init (pTHX) 390time_init (pTHX)
363{ 391{
364 SV **svp; 392 SV **svp;
365 393
366 require_pv ("Time/HiRes.pm"); 394 require_pv ("Time/HiRes.pm");
367 395
368 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); 396 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
369 397
370 if (!svp) croak ("Time::HiRes is required, but missing. Caught"); 398 if (!svp) croak ("Time::HiRes is required, but missing. Caught");
371 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught"); 399 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught");
372 400
378 406
379#endif 407#endif
380 408
381/** lowlevel stuff **********************************************************/ 409/** lowlevel stuff **********************************************************/
382 410
383static SV * 411static SV * ecb_noinline
384coro_get_sv (pTHX_ const char *name, int create) 412coro_get_sv (pTHX_ const char *name, int create)
385{ 413{
386#if PERL_VERSION_ATLEAST (5,10,0) 414#if PERL_VERSION_ATLEAST (5,10,0)
387 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 415 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
388 get_sv (name, create); 416 get_sv (name, create);
389#endif 417#endif
390 return get_sv (name, create); 418 return get_sv (name, create);
391} 419}
392 420
393static AV * 421static AV * ecb_noinline
394coro_get_av (pTHX_ const char *name, int create) 422coro_get_av (pTHX_ const char *name, int create)
395{ 423{
396#if PERL_VERSION_ATLEAST (5,10,0) 424#if PERL_VERSION_ATLEAST (5,10,0)
397 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 425 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
398 get_av (name, create); 426 get_av (name, create);
399#endif 427#endif
400 return get_av (name, create); 428 return get_av (name, create);
401} 429}
402 430
403static HV * 431static HV * ecb_noinline
404coro_get_hv (pTHX_ const char *name, int create) 432coro_get_hv (pTHX_ const char *name, int create)
405{ 433{
406#if PERL_VERSION_ATLEAST (5,10,0) 434#if PERL_VERSION_ATLEAST (5,10,0)
407 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 435 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
408 get_hv (name, create); 436 get_hv (name, create);
409#endif 437#endif
410 return get_hv (name, create); 438 return get_hv (name, create);
411} 439}
412 440
413INLINE void 441ecb_inline void
414coro_times_update () 442coro_times_update (void)
415{ 443{
416#ifdef coro_clock_gettime 444#ifdef coro_clock_gettime
417 struct timespec ts; 445 struct timespec ts;
418 446
419 ts.tv_sec = ts.tv_nsec = 0; 447 ts.tv_sec = ts.tv_nsec = 0;
431 time_real [0] = tv [0]; 459 time_real [0] = tv [0];
432 time_real [1] = tv [1] * 1000; 460 time_real [1] = tv [1] * 1000;
433#endif 461#endif
434} 462}
435 463
436INLINE void 464ecb_inline void
437coro_times_add (struct coro *c) 465coro_times_add (struct coro *c)
438{ 466{
439 c->t_real [1] += time_real [1]; 467 c->t_real [1] += time_real [1];
440 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } 468 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; }
441 c->t_real [0] += time_real [0]; 469 c->t_real [0] += time_real [0];
443 c->t_cpu [1] += time_cpu [1]; 471 c->t_cpu [1] += time_cpu [1];
444 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } 472 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; }
445 c->t_cpu [0] += time_cpu [0]; 473 c->t_cpu [0] += time_cpu [0];
446} 474}
447 475
448INLINE void 476ecb_inline void
449coro_times_sub (struct coro *c) 477coro_times_sub (struct coro *c)
450{ 478{
451 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } 479 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; }
452 c->t_real [1] -= time_real [1]; 480 c->t_real [1] -= time_real [1];
453 c->t_real [0] -= time_real [0]; 481 c->t_real [0] -= time_real [0];
461/* magic glue */ 489/* magic glue */
462 490
463#define CORO_MAGIC_type_cv 26 491#define CORO_MAGIC_type_cv 26
464#define CORO_MAGIC_type_state PERL_MAGIC_ext 492#define CORO_MAGIC_type_state PERL_MAGIC_ext
465 493
466#define CORO_MAGIC_NN(sv, type) \ 494#define CORO_MAGIC_NN(sv, type) \
467 (expect_true (SvMAGIC (sv)->mg_type == type) \ 495 (ecb_expect_true (SvMAGIC (sv)->mg_type == type) \
468 ? SvMAGIC (sv) \ 496 ? SvMAGIC (sv) \
469 : mg_find (sv, type)) 497 : mg_find (sv, type))
470 498
471#define CORO_MAGIC(sv, type) \ 499#define CORO_MAGIC(sv, type) \
472 (expect_true (SvMAGIC (sv)) \ 500 (ecb_expect_true (SvMAGIC (sv)) \
473 ? CORO_MAGIC_NN (sv, type) \ 501 ? CORO_MAGIC_NN (sv, type) \
474 : 0) 502 : 0)
475 503
476#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 504#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
477#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) 505#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
478 506
479INLINE MAGIC * 507ecb_inline MAGIC *
480SvSTATEhv_p (pTHX_ SV *coro) 508SvSTATEhv_p (pTHX_ SV *coro)
481{ 509{
482 MAGIC *mg; 510 MAGIC *mg;
483 511
484 if (expect_true ( 512 if (ecb_expect_true (
485 SvTYPE (coro) == SVt_PVHV 513 SvTYPE (coro) == SVt_PVHV
486 && (mg = CORO_MAGIC_state (coro)) 514 && (mg = CORO_MAGIC_state (coro))
487 && mg->mg_virtual == &coro_state_vtbl 515 && mg->mg_virtual == &coro_state_vtbl
488 )) 516 ))
489 return mg; 517 return mg;
490 518
491 return 0; 519 return 0;
492} 520}
493 521
494INLINE struct coro * 522ecb_inline struct coro *
495SvSTATE_ (pTHX_ SV *coro) 523SvSTATE_ (pTHX_ SV *coro)
496{ 524{
497 MAGIC *mg; 525 MAGIC *mg;
498 526
499 if (SvROK (coro)) 527 if (SvROK (coro))
513#define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) 541#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
514 542
515/*****************************************************************************/ 543/*****************************************************************************/
516/* padlist management and caching */ 544/* padlist management and caching */
517 545
518static AV * 546ecb_inline PADLIST *
519coro_derive_padlist (pTHX_ CV *cv) 547coro_derive_padlist (pTHX_ CV *cv)
520{ 548{
521 AV *padlist = CvPADLIST (cv); 549 PADLIST *padlist = CvPADLIST (cv);
522 AV *newpadlist, *newpad; 550 PADLIST *newpadlist;
551 PAD *newpad;
552 PADOFFSET const off = PadlistMAX (padlist) + 1;
523 553
524 newpadlist = newAV (); 554 newPADLIST(newpadlist);
555#if !PERL_VERSION_ATLEAST(5,15,3)
556 /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */
525 AvREAL_off (newpadlist); 557 AvREAL_off (newpadlist);
558#endif
526#if PERL_VERSION_ATLEAST (5,10,0) 559#if PERL_VERSION_ATLEAST (5,10,0)
527 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); 560 Perl_pad_push (aTHX_ padlist, off);
528#else 561#else
529 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 562 Perl_pad_push (aTHX_ padlist, off, 1);
530#endif 563#endif
531 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 564 newpad = PadlistARRAY (padlist)[off];
532 --AvFILLp (padlist); 565 PadlistMAX (padlist) = off - 1;
533 566
534 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); 567 /* Already extended to 2 elements by newPADLIST. */
535 av_store (newpadlist, 1, (SV *)newpad); 568 PadlistMAX (newpadlist) = 1;
569 PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist));
570 PadlistARRAY (newpadlist)[1] = newpad;
536 571
537 return newpadlist; 572 return newpadlist;
538} 573}
539 574
540static void 575ecb_inline void
541free_padlist (pTHX_ AV *padlist) 576free_padlist (pTHX_ PADLIST *padlist)
542{ 577{
543 /* may be during global destruction */ 578 /* may be during global destruction */
544 if (!IN_DESTRUCT) 579 if (!IN_DESTRUCT)
545 { 580 {
546 I32 i = AvFILLp (padlist); 581 I32 i = PadlistMAX (padlist);
547 582
548 while (i > 0) /* special-case index 0 */ 583 while (i > 0) /* special-case index 0 */
549 { 584 {
550 /* we try to be extra-careful here */ 585 /* we try to be extra-careful here */
551 AV *av = (AV *)AvARRAY (padlist)[i--]; 586 PAD *pad = PadlistARRAY (padlist)[i--];
552 I32 j = AvFILLp (av); 587 I32 j = PadMAX (pad);
553 588
554 while (j >= 0) 589 while (j >= 0)
555 SvREFCNT_dec (AvARRAY (av)[j--]); 590 SvREFCNT_dec (PadARRAY (pad)[j--]);
556 591
557 AvFILLp (av) = -1; 592 PadMAX (pad) = -1;
558 SvREFCNT_dec (av); 593 SvREFCNT_dec (pad);
559 } 594 }
560 595
561 SvREFCNT_dec (AvARRAY (padlist)[0]); 596 SvREFCNT_dec (PadlistNAMES (padlist));
562 597
598#ifdef NEWPADAPI
599 Safefree (PadlistARRAY (padlist));
600 Safefree (padlist);
601#else
563 AvFILLp (padlist) = -1; 602 AvFILLp (padlist) = -1;
603 AvREAL_off (padlist);
564 SvREFCNT_dec ((SV*)padlist); 604 SvREFCNT_dec ((SV*)padlist);
605#endif
565 } 606 }
566} 607}
567 608
568static int 609static int
569coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 610coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
570{ 611{
571 AV *padlist; 612 PADLIST *padlist;
572 AV *av = (AV *)mg->mg_obj; 613 PADLIST **padlists = (PADLIST **)(mg->mg_ptr + sizeof(size_t));
614 size_t len = *(size_t *)mg->mg_ptr;
573 615
574 /* perl manages to free our internal AV and _then_ call us */ 616 /* perl manages to free our internal AV and _then_ call us */
575 if (IN_DESTRUCT) 617 if (IN_DESTRUCT)
576 return 0; 618 return 0;
577 619
578 /* casting is fun. */ 620 while (len--)
579 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
580 free_padlist (aTHX_ padlist); 621 free_padlist (aTHX_ padlists[len]);
581
582 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
583 622
584 return 0; 623 return 0;
585} 624}
586 625
587static MGVTBL coro_cv_vtbl = { 626static MGVTBL coro_cv_vtbl = {
588 0, 0, 0, 0, 627 0, 0, 0, 0,
589 coro_cv_free 628 coro_cv_free
590}; 629};
591 630
592/* the next two functions merely cache the padlists */ 631/* the next two functions merely cache the padlists */
593static void 632ecb_inline void
594get_padlist (pTHX_ CV *cv) 633get_padlist (pTHX_ CV *cv)
595{ 634{
596 MAGIC *mg = CORO_MAGIC_cv (cv); 635 MAGIC *mg = CORO_MAGIC_cv (cv);
597 AV *av; 636 size_t *lenp;
598 637
599 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 638 if (ecb_expect_true (mg && *(lenp = (size_t *)mg->mg_ptr)))
600 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 639 CvPADLIST (cv) = ((PADLIST **)(mg->mg_ptr + sizeof(size_t)))[--*lenp];
601 else 640 else
602 { 641 {
603#if CORO_PREFER_PERL_FUNCTIONS 642#if CORO_PREFER_PERL_FUNCTIONS
604 /* this is probably cleaner? but also slower! */ 643 /* this is probably cleaner? but also slower! */
605 /* in practise, it seems to be less stable */ 644 /* in practise, it seems to be less stable */
611 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); 650 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
612#endif 651#endif
613 } 652 }
614} 653}
615 654
616static void 655ecb_inline void
617put_padlist (pTHX_ CV *cv) 656put_padlist (pTHX_ CV *cv)
618{ 657{
619 MAGIC *mg = CORO_MAGIC_cv (cv); 658 MAGIC *mg = CORO_MAGIC_cv (cv);
620 AV *av;
621 659
622 if (expect_false (!mg)) 660 if (ecb_expect_false (!mg))
661 {
623 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 662 mg = sv_magicext ((SV *)cv, 0, CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
663 Newz (0, mg->mg_ptr ,sizeof (size_t) + sizeof (PADLIST *), char);
664 mg->mg_len = 1; /* so mg_free frees mg_ptr */
665 }
666 else
667 Renew (mg->mg_ptr,
668 sizeof(size_t) + (*(size_t *)mg->mg_ptr + 1) * sizeof(PADLIST *),
669 char);
624 670
625 av = (AV *)mg->mg_obj; 671 ((PADLIST **)(mg->mg_ptr + sizeof (size_t))) [(*(size_t *)mg->mg_ptr)++] = CvPADLIST (cv);
626
627 if (expect_false (AvFILLp (av) >= AvMAX (av)))
628 av_extend (av, AvFILLp (av) + 1);
629
630 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
631} 672}
632 673
633/** load & save, init *******************************************************/ 674/** load & save, init *******************************************************/
634 675
676ecb_inline void
677swap_sv (SV *a, SV *b)
678{
679 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
680 SV tmp;
681
682 /* swap sv_any */
683 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
684
685 /* swap sv_flags */
686 SvFLAGS (&tmp) = SvFLAGS (a);
687 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
688 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
689
690#if PERL_VERSION_ATLEAST (5,10,0)
691 /* perl 5.10 and later complicates this _quite_ a bit, but it also
692 * is much faster, so no quarrels here. alternatively, we could
693 * sv_upgrade to avoid this.
694 */
695 {
696 /* swap sv_u */
697 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
698
699 /* if SvANY points to the head, we need to adjust the pointers,
700 * as the pointer for a still points to b, and maybe vice versa.
701 */
702 #define svany_in_head(type) \
703 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
704
705 if (svany_in_head (SvTYPE (a)))
706 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
707
708 if (svany_in_head (SvTYPE (b)))
709 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
710 }
711#endif
712}
713
635/* swap sv heads, at least logically */ 714/* swap sv heads, at least logically */
636static void 715static void
637swap_svs (pTHX_ Coro__State c) 716swap_svs (pTHX_ Coro__State c)
638{ 717{
639 int i; 718 int i;
640 719
641 for (i = 0; i <= AvFILLp (c->swap_sv); ) 720 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2)
642 { 721 swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]);
643 SV *a = AvARRAY (c->swap_sv)[i++];
644 SV *b = AvARRAY (c->swap_sv)[i++];
645
646 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
647 SV tmp;
648
649 /* swap sv_any */
650 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
651
652 /* swap sv_flags */
653 SvFLAGS (&tmp) = SvFLAGS (a);
654 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
655 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
656
657#if PERL_VERSION_ATLEAST (5,10,0)
658 /* perl 5.10 complicates this _quite_ a bit, but it also is
659 * much faster, so no quarrels here. alternatively, we could
660 * sv_upgrade to avoid this.
661 */
662 {
663 /* swap sv_u */
664 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
665
666 /* if SvANY points to the head, we need to adjust the pointers,
667 * as the pointer for a still points to b, and maybe vice versa.
668 */
669 #define svany_in_head(type) \
670 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
671
672 if (svany_in_head (SvTYPE (a)))
673 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
674
675 if (svany_in_head (SvTYPE (b)))
676 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
677 }
678#endif
679 }
680} 722}
681 723
682#define SWAP_SVS(coro) \ 724#define SWAP_SVS(coro) \
683 if (expect_false ((coro)->swap_sv)) \ 725 if (ecb_expect_false ((coro)->swap_sv)) \
684 swap_svs (aTHX_ (coro)) 726 swap_svs (aTHX_ (coro))
685 727
686static void 728static void
687on_enterleave_call (pTHX_ SV *cb); 729on_enterleave_call (pTHX_ SV *cb);
688 730
692 perl_slots *slot = c->slot; 734 perl_slots *slot = c->slot;
693 c->slot = 0; 735 c->slot = 0;
694 736
695 PL_mainstack = c->mainstack; 737 PL_mainstack = c->mainstack;
696 738
697 GvSV (PL_defgv) = slot->defsv; 739#if CORO_JIT
698 GvAV (PL_defgv) = slot->defav; 740 load_perl_slots (slot);
699 GvSV (PL_errgv) = slot->errsv; 741#else
700 GvSV (irsgv) = slot->irsgv;
701 GvHV (PL_hintgv) = slot->hinthv;
702
703 #define VAR(name,type) PL_ ## name = slot->name; 742 #define VARx(name,expr,type) expr = slot->name;
704 # include "state.h" 743 #include "state.h"
705 #undef VAR 744#endif
706 745
707 { 746 {
708 dSP; 747 dSP;
709 748
710 CV *cv; 749 CV *cv;
711 750
712 /* now do the ugly restore mess */ 751 /* now do the ugly restore mess */
713 while (expect_true (cv = (CV *)POPs)) 752 while (ecb_expect_true (cv = (CV *)POPs))
714 { 753 {
715 put_padlist (aTHX_ cv); /* mark this padlist as available */ 754 put_padlist (aTHX_ cv); /* mark this padlist as available */
716 CvDEPTH (cv) = PTR2IV (POPs); 755 CvDEPTH (cv) = PTR2IV (POPs);
717 CvPADLIST (cv) = (AV *)POPs; 756 CvPADLIST (cv) = (PADLIST *)POPs;
718 } 757 }
719 758
720 PUTBACK; 759 PUTBACK;
721 } 760 }
722 761
723 slf_frame = c->slf_frame; 762 slf_frame = c->slf_frame;
724 CORO_THROW = c->except; 763 CORO_THROW = c->except;
725 764
726 if (expect_false (enable_times)) 765 if (ecb_expect_false (enable_times))
727 { 766 {
728 if (expect_false (!times_valid)) 767 if (ecb_expect_false (!times_valid))
729 coro_times_update (); 768 coro_times_update ();
730 769
731 coro_times_sub (c); 770 coro_times_sub (c);
732 } 771 }
733 772
734 if (expect_false (c->on_enter)) 773 if (ecb_expect_false (c->on_enter))
735 { 774 {
736 int i; 775 int i;
737 776
738 for (i = 0; i <= AvFILLp (c->on_enter); ++i) 777 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
739 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); 778 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
745static void 784static void
746save_perl (pTHX_ Coro__State c) 785save_perl (pTHX_ Coro__State c)
747{ 786{
748 SWAP_SVS (c); 787 SWAP_SVS (c);
749 788
750 if (expect_false (c->on_leave)) 789 if (ecb_expect_false (c->on_leave))
751 { 790 {
752 int i; 791 int i;
753 792
754 for (i = AvFILLp (c->on_leave); i >= 0; --i) 793 for (i = AvFILLp (c->on_leave); i >= 0; --i)
755 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); 794 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
756 } 795 }
757 796
758 times_valid = 0; 797 times_valid = 0;
759 798
760 if (expect_false (enable_times)) 799 if (ecb_expect_false (enable_times))
761 { 800 {
762 coro_times_update (); times_valid = 1; 801 coro_times_update (); times_valid = 1;
763 coro_times_add (c); 802 coro_times_add (c);
764 } 803 }
765 804
779 818
780 XPUSHs (Nullsv); 819 XPUSHs (Nullsv);
781 /* this loop was inspired by pp_caller */ 820 /* this loop was inspired by pp_caller */
782 for (;;) 821 for (;;)
783 { 822 {
784 while (expect_true (cxix >= 0)) 823 while (ecb_expect_true (cxix >= 0))
785 { 824 {
786 PERL_CONTEXT *cx = &ccstk[cxix--]; 825 PERL_CONTEXT *cx = &ccstk[cxix--];
787 826
788 if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT)) 827 if (ecb_expect_true (CxTYPE (cx) == CXt_SUB) || ecb_expect_false (CxTYPE (cx) == CXt_FORMAT))
789 { 828 {
790 CV *cv = cx->blk_sub.cv; 829 CV *cv = cx->blk_sub.cv;
791 830
792 if (expect_true (CvDEPTH (cv))) 831 if (ecb_expect_true (CvDEPTH (cv)))
793 { 832 {
794 EXTEND (SP, 3); 833 EXTEND (SP, 3);
795 PUSHs ((SV *)CvPADLIST (cv)); 834 PUSHs ((SV *)CvPADLIST (cv));
796 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); 835 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
797 PUSHs ((SV *)cv); 836 PUSHs ((SV *)cv);
800 get_padlist (aTHX_ cv); 839 get_padlist (aTHX_ cv);
801 } 840 }
802 } 841 }
803 } 842 }
804 843
805 if (expect_true (top_si->si_type == PERLSI_MAIN)) 844 if (ecb_expect_true (top_si->si_type == PERLSI_MAIN))
806 break; 845 break;
807 846
808 top_si = top_si->si_prev; 847 top_si = top_si->si_prev;
809 ccstk = top_si->si_cxstack; 848 ccstk = top_si->si_cxstack;
810 cxix = top_si->si_cxix; 849 cxix = top_si->si_cxix;
812 851
813 PUTBACK; 852 PUTBACK;
814 } 853 }
815 854
816 /* allocate some space on the context stack for our purposes */ 855 /* allocate some space on the context stack for our purposes */
817 if (expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) 856 if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max))
818 { 857 {
819 unsigned int i; 858 unsigned int i;
820 859
821 for (i = 0; i < SLOT_COUNT; ++i) 860 for (i = 0; i < SLOT_COUNT; ++i)
822 CXINC; 861 CXINC;
827 c->mainstack = PL_mainstack; 866 c->mainstack = PL_mainstack;
828 867
829 { 868 {
830 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 869 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
831 870
832 slot->defav = GvAV (PL_defgv); 871#if CORO_JIT
833 slot->defsv = DEFSV; 872 save_perl_slots (slot);
834 slot->errsv = ERRSV; 873#else
835 slot->irsgv = GvSV (irsgv);
836 slot->hinthv = GvHV (PL_hintgv);
837
838 #define VAR(name,type) slot->name = PL_ ## name; 874 #define VARx(name,expr,type) slot->name = expr;
839 # include "state.h" 875 #include "state.h"
840 #undef VAR 876#endif
841 } 877 }
842} 878}
843 879
844/* 880/*
845 * allocate various perl stacks. This is almost an exact copy 881 * allocate various perl stacks. This is almost an exact copy
876#endif 912#endif
877 913
878 New(54,PL_scopestack,8,I32); 914 New(54,PL_scopestack,8,I32);
879 PL_scopestack_ix = 0; 915 PL_scopestack_ix = 0;
880 PL_scopestack_max = 8; 916 PL_scopestack_max = 8;
917#if HAS_SCOPESTACK_NAME
918 New(54,PL_scopestack_name,8,const char*);
919#endif
881 920
882 New(54,PL_savestack,24,ANY); 921 New(54,PL_savestack,24,ANY);
883 PL_savestack_ix = 0; 922 PL_savestack_ix = 0;
884 PL_savestack_max = 24; 923 PL_savestack_max = 24;
885 924
913 } 952 }
914 953
915 Safefree (PL_tmps_stack); 954 Safefree (PL_tmps_stack);
916 Safefree (PL_markstack); 955 Safefree (PL_markstack);
917 Safefree (PL_scopestack); 956 Safefree (PL_scopestack);
957#if HAS_SCOPESTACK_NAME
958 Safefree (PL_scopestack_name);
959#endif
918 Safefree (PL_savestack); 960 Safefree (PL_savestack);
919#if !PERL_VERSION_ATLEAST (5,10,0) 961#if !PERL_VERSION_ATLEAST (5,10,0)
920 Safefree (PL_retstack); 962 Safefree (PL_retstack);
921#endif 963#endif
922} 964}
971 * This overrides the default magic get method of %SIG elements. 1013 * This overrides the default magic get method of %SIG elements.
972 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 1014 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
973 * and instead of trying to save and restore the hash elements (extremely slow), 1015 * and instead of trying to save and restore the hash elements (extremely slow),
974 * we just provide our own readback here. 1016 * we just provide our own readback here.
975 */ 1017 */
976static int 1018static int ecb_cold
977coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 1019coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
978{ 1020{
979 const char *s = MgPV_nolen_const (mg); 1021 const char *s = MgPV_nolen_const (mg);
980 1022
981 if (*s == '_') 1023 if (*s == '_')
982 { 1024 {
983 SV **svp = 0; 1025 SV **svp = 0;
984 1026
985 if (strEQ (s, "__DIE__" )) svp = &PL_diehook; 1027 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
986 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 1028 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
987 1029
988 if (svp) 1030 if (svp)
989 { 1031 {
990 sv_setsv (sv, *svp ? *svp : &PL_sv_undef); 1032 SV *ssv;
1033
1034 if (!*svp)
1035 ssv = &PL_sv_undef;
1036 else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */
1037 ssv = sv_2mortal (newRV_inc (*svp));
1038 else
1039 ssv = *svp;
1040
1041 sv_setsv (sv, ssv);
991 return 0; 1042 return 0;
992 } 1043 }
993 } 1044 }
994 1045
995 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; 1046 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
996} 1047}
997 1048
998static int 1049static int ecb_cold
999coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) 1050coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
1000{ 1051{
1001 const char *s = MgPV_nolen_const (mg); 1052 const char *s = MgPV_nolen_const (mg);
1002 1053
1003 if (*s == '_') 1054 if (*s == '_')
1017 } 1068 }
1018 1069
1019 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; 1070 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0;
1020} 1071}
1021 1072
1022static int 1073static int ecb_cold
1023coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) 1074coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
1024{ 1075{
1025 const char *s = MgPV_nolen_const (mg); 1076 const char *s = MgPV_nolen_const (mg);
1026 1077
1027 if (*s == '_') 1078 if (*s == '_')
1062 return 1; 1113 return 1;
1063} 1114}
1064 1115
1065static UNOP init_perl_op; 1116static UNOP init_perl_op;
1066 1117
1067static void NOINLINE /* noinline to keep it out of the transfer fast path */ 1118ecb_noinline static void /* noinline to keep it out of the transfer fast path */
1068init_perl (pTHX_ struct coro *coro) 1119init_perl (pTHX_ struct coro *coro)
1069{ 1120{
1070 /* 1121 /*
1071 * emulate part of the perl startup here. 1122 * emulate part of the perl startup here.
1072 */ 1123 */
1089 PL_hints = 0; 1140 PL_hints = 0;
1090 1141
1091 /* recreate the die/warn hooks */ 1142 /* recreate the die/warn hooks */
1092 PL_diehook = SvREFCNT_inc (rv_diehook); 1143 PL_diehook = SvREFCNT_inc (rv_diehook);
1093 PL_warnhook = SvREFCNT_inc (rv_warnhook); 1144 PL_warnhook = SvREFCNT_inc (rv_warnhook);
1094 1145
1095 GvSV (PL_defgv) = newSV (0); 1146 GvSV (PL_defgv) = newSV (0);
1096 GvAV (PL_defgv) = coro->args; coro->args = 0; 1147 GvAV (PL_defgv) = coro->args; coro->args = 0;
1097 GvSV (PL_errgv) = newSV (0); 1148 GvSV (PL_errgv) = newSV (0);
1098 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 1149 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
1099 GvHV (PL_hintgv) = 0; 1150 GvHV (PL_hintgv) = 0;
1134 /* copy throw, in case it was set before init_perl */ 1185 /* copy throw, in case it was set before init_perl */
1135 CORO_THROW = coro->except; 1186 CORO_THROW = coro->except;
1136 1187
1137 SWAP_SVS (coro); 1188 SWAP_SVS (coro);
1138 1189
1139 if (expect_false (enable_times)) 1190 if (ecb_expect_false (enable_times))
1140 { 1191 {
1141 coro_times_update (); 1192 coro_times_update ();
1142 coro_times_sub (coro); 1193 coro_times_sub (coro);
1143 } 1194 }
1144} 1195}
1187 /* restore swapped sv's */ 1238 /* restore swapped sv's */
1188 SWAP_SVS (coro); 1239 SWAP_SVS (coro);
1189 1240
1190 coro_destruct_stacks (aTHX); 1241 coro_destruct_stacks (aTHX);
1191 1242
1192 // now save some sv's to be free'd later 1243 /* now save some sv's to be free'd later */
1193 svf [0] = GvSV (PL_defgv); 1244 svf [0] = GvSV (PL_defgv);
1194 svf [1] = (SV *)GvAV (PL_defgv); 1245 svf [1] = (SV *)GvAV (PL_defgv);
1195 svf [2] = GvSV (PL_errgv); 1246 svf [2] = GvSV (PL_errgv);
1196 svf [3] = (SV *)PL_defoutgv; 1247 svf [3] = (SV *)PL_defoutgv;
1197 svf [4] = PL_rs; 1248 svf [4] = PL_rs;
1217 SvREFCNT_dec (coro->invoke_cb); 1268 SvREFCNT_dec (coro->invoke_cb);
1218 SvREFCNT_dec (coro->invoke_av); 1269 SvREFCNT_dec (coro->invoke_av);
1219 } 1270 }
1220} 1271}
1221 1272
1222INLINE void 1273ecb_inline void
1223free_coro_mortal (pTHX) 1274free_coro_mortal (pTHX)
1224{ 1275{
1225 if (expect_true (coro_mortal)) 1276 if (ecb_expect_true (coro_mortal))
1226 { 1277 {
1227 SvREFCNT_dec ((SV *)coro_mortal); 1278 SvREFCNT_dec ((SV *)coro_mortal);
1228 coro_mortal = 0; 1279 coro_mortal = 0;
1229 } 1280 }
1230} 1281}
1365 1416
1366 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1417 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1367} 1418}
1368 1419
1369/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1420/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1370static void NOINLINE 1421static void ecb_noinline
1371cctx_prepare (pTHX) 1422cctx_prepare (pTHX)
1372{ 1423{
1373 PL_top_env = &PL_start_env; 1424 PL_top_env = &PL_start_env;
1374 1425
1375 if (cctx_current->flags & CC_TRACE) 1426 if (cctx_current->flags & CC_TRACE)
1386 slf_frame.prepare = slf_prepare_set_stacklevel; 1437 slf_frame.prepare = slf_prepare_set_stacklevel;
1387 slf_frame.check = slf_check_set_stacklevel; 1438 slf_frame.check = slf_check_set_stacklevel;
1388} 1439}
1389 1440
1390/* the tail of transfer: execute stuff we can only do after a transfer */ 1441/* the tail of transfer: execute stuff we can only do after a transfer */
1391INLINE void 1442ecb_inline void
1392transfer_tail (pTHX) 1443transfer_tail (pTHX)
1393{ 1444{
1394 free_coro_mortal (aTHX); 1445 free_coro_mortal (aTHX);
1446}
1447
1448/* try to exit the same way perl's main function would do */
1449/* we do not bother resetting the environment or other things *7
1450/* that are not, uhm, essential */
1451/* this obviously also doesn't work when perl is embedded */
1452static void ecb_noinline ecb_cold
1453perlish_exit (pTHX)
1454{
1455 int exitstatus = perl_destruct (PL_curinterp);
1456 perl_free (PL_curinterp);
1457 exit (exitstatus);
1395} 1458}
1396 1459
1397/* 1460/*
1398 * this is a _very_ stripped down perl interpreter ;) 1461 * this is a _very_ stripped down perl interpreter ;)
1399 */ 1462 */
1428 */ 1491 */
1429 1492
1430 /* 1493 /*
1431 * If perl-run returns we assume exit() was being called or the coro 1494 * If perl-run returns we assume exit() was being called or the coro
1432 * fell off the end, which seems to be the only valid (non-bug) 1495 * fell off the end, which seems to be the only valid (non-bug)
1433 * reason for perl_run to return. We try to exit by jumping to the 1496 * reason for perl_run to return. We try to mimic whatever perl is normally
1434 * bootstrap-time "top" top_env, as we cannot restore the "main" 1497 * doing in that case. YMMV.
1435 * coroutine as Coro has no such concept.
1436 * This actually isn't valid with the pthread backend, but OSes requiring
1437 * that backend are too broken to do it in a standards-compliant way.
1438 */ 1498 */
1439 PL_top_env = main_top_env; 1499 perlish_exit (aTHX);
1440 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1441 } 1500 }
1442} 1501}
1443 1502
1444static coro_cctx * 1503static coro_cctx *
1445cctx_new () 1504cctx_new (void)
1446{ 1505{
1447 coro_cctx *cctx; 1506 coro_cctx *cctx;
1448 1507
1449 ++cctx_count; 1508 ++cctx_count;
1450 New (0, cctx, 1, coro_cctx); 1509 New (0, cctx, 1, coro_cctx);
1456 return cctx; 1515 return cctx;
1457} 1516}
1458 1517
1459/* create a new cctx only suitable as source */ 1518/* create a new cctx only suitable as source */
1460static coro_cctx * 1519static coro_cctx *
1461cctx_new_empty () 1520cctx_new_empty (void)
1462{ 1521{
1463 coro_cctx *cctx = cctx_new (); 1522 coro_cctx *cctx = cctx_new ();
1464 1523
1465 cctx->sptr = 0; 1524 cctx->sptr = 0;
1466 coro_create (&cctx->cctx, 0, 0, 0, 0); 1525 coro_create (&cctx->cctx, 0, 0, 0, 0);
1468 return cctx; 1527 return cctx;
1469} 1528}
1470 1529
1471/* create a new cctx suitable as destination/running a perl interpreter */ 1530/* create a new cctx suitable as destination/running a perl interpreter */
1472static coro_cctx * 1531static coro_cctx *
1473cctx_new_run () 1532cctx_new_run (void)
1474{ 1533{
1475 coro_cctx *cctx = cctx_new (); 1534 coro_cctx *cctx = cctx_new ();
1476 void *stack_start; 1535 void *stack_start;
1477 size_t stack_size; 1536 size_t stack_size;
1478 1537
1479#if HAVE_MMAP 1538#if HAVE_MMAP
1480 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1539 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1481 /* mmap supposedly does allocate-on-write for us */ 1540 /* mmap supposedly does allocate-on-write for us */
1482 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1541 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, -1, 0);
1483 1542
1484 if (cctx->sptr != (void *)-1) 1543 if (cctx->sptr != (void *)-1)
1485 { 1544 {
1486 #if CORO_STACKGUARD 1545 #if CORO_STACKGUARD
1487 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1546 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1548#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) 1607#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1549 1608
1550static coro_cctx * 1609static coro_cctx *
1551cctx_get (pTHX) 1610cctx_get (pTHX)
1552{ 1611{
1553 while (expect_true (cctx_first)) 1612 while (ecb_expect_true (cctx_first))
1554 { 1613 {
1555 coro_cctx *cctx = cctx_first; 1614 coro_cctx *cctx = cctx_first;
1556 cctx_first = cctx->next; 1615 cctx_first = cctx->next;
1557 --cctx_idle; 1616 --cctx_idle;
1558 1617
1559 if (expect_true (!CCTX_EXPIRED (cctx))) 1618 if (ecb_expect_true (!CCTX_EXPIRED (cctx)))
1560 return cctx; 1619 return cctx;
1561 1620
1562 cctx_destroy (cctx); 1621 cctx_destroy (cctx);
1563 } 1622 }
1564 1623
1569cctx_put (coro_cctx *cctx) 1628cctx_put (coro_cctx *cctx)
1570{ 1629{
1571 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); 1630 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
1572 1631
1573 /* free another cctx if overlimit */ 1632 /* free another cctx if overlimit */
1574 if (expect_false (cctx_idle >= cctx_max_idle)) 1633 if (ecb_expect_false (cctx_idle >= cctx_max_idle))
1575 { 1634 {
1576 coro_cctx *first = cctx_first; 1635 coro_cctx *first = cctx_first;
1577 cctx_first = first->next; 1636 cctx_first = first->next;
1578 --cctx_idle; 1637 --cctx_idle;
1579 1638
1590static void 1649static void
1591transfer_check (pTHX_ struct coro *prev, struct coro *next) 1650transfer_check (pTHX_ struct coro *prev, struct coro *next)
1592{ 1651{
1593 /* TODO: throwing up here is considered harmful */ 1652 /* TODO: throwing up here is considered harmful */
1594 1653
1595 if (expect_true (prev != next)) 1654 if (ecb_expect_true (prev != next))
1596 { 1655 {
1597 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1656 if (ecb_expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1598 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); 1657 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1599 1658
1600 if (expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED))) 1659 if (ecb_expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED)))
1601 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); 1660 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1602 1661
1603#if !PERL_VERSION_ATLEAST (5,10,0) 1662#if !PERL_VERSION_ATLEAST (5,10,0)
1604 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1663 if (ecb_expect_false (PL_lex_state != LEX_NOTPARSING))
1605 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); 1664 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1606#endif 1665#endif
1607 } 1666 }
1608} 1667}
1609 1668
1610/* always use the TRANSFER macro */ 1669/* always use the TRANSFER macro */
1611static void NOINLINE /* noinline so we have a fixed stackframe */ 1670static void ecb_noinline /* noinline so we have a fixed stackframe */
1612transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1671transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1613{ 1672{
1614 dSTACKLEVEL; 1673 dSTACKLEVEL;
1615 1674
1616 /* sometimes transfer is only called to set idle_sp */ 1675 /* sometimes transfer is only called to set idle_sp */
1617 if (expect_false (!prev)) 1676 if (ecb_expect_false (!prev))
1618 { 1677 {
1619 cctx_current->idle_sp = STACKLEVEL; 1678 cctx_current->idle_sp = STACKLEVEL;
1620 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1679 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1621 } 1680 }
1622 else if (expect_true (prev != next)) 1681 else if (ecb_expect_true (prev != next))
1623 { 1682 {
1624 coro_cctx *cctx_prev; 1683 coro_cctx *cctx_prev;
1625 1684
1626 if (expect_false (prev->flags & CF_NEW)) 1685 if (ecb_expect_false (prev->flags & CF_NEW))
1627 { 1686 {
1628 /* create a new empty/source context */ 1687 /* create a new empty/source context */
1629 prev->flags &= ~CF_NEW; 1688 prev->flags &= ~CF_NEW;
1630 prev->flags |= CF_RUNNING; 1689 prev->flags |= CF_RUNNING;
1631 } 1690 }
1634 next->flags |= CF_RUNNING; 1693 next->flags |= CF_RUNNING;
1635 1694
1636 /* first get rid of the old state */ 1695 /* first get rid of the old state */
1637 save_perl (aTHX_ prev); 1696 save_perl (aTHX_ prev);
1638 1697
1639 if (expect_false (next->flags & CF_NEW)) 1698 if (ecb_expect_false (next->flags & CF_NEW))
1640 { 1699 {
1641 /* need to start coroutine */ 1700 /* need to start coroutine */
1642 next->flags &= ~CF_NEW; 1701 next->flags &= ~CF_NEW;
1643 /* setup coroutine call */ 1702 /* setup coroutine call */
1644 init_perl (aTHX_ next); 1703 init_perl (aTHX_ next);
1645 } 1704 }
1646 else 1705 else
1647 load_perl (aTHX_ next); 1706 load_perl (aTHX_ next);
1648 1707
1649 /* possibly untie and reuse the cctx */ 1708 /* possibly untie and reuse the cctx */
1650 if (expect_true ( 1709 if (ecb_expect_true (
1651 cctx_current->idle_sp == STACKLEVEL 1710 cctx_current->idle_sp == STACKLEVEL
1652 && !(cctx_current->flags & CC_TRACE) 1711 && !(cctx_current->flags & CC_TRACE)
1653 && !force_cctx 1712 && !force_cctx
1654 )) 1713 ))
1655 { 1714 {
1656 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1715 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1657 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); 1716 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1658 1717
1659 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ 1718 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1660 /* without this the next cctx_get might destroy the running cctx while still in use */ 1719 /* without this the next cctx_get might destroy the running cctx while still in use */
1661 if (expect_false (CCTX_EXPIRED (cctx_current))) 1720 if (ecb_expect_false (CCTX_EXPIRED (cctx_current)))
1662 if (expect_true (!next->cctx)) 1721 if (ecb_expect_true (!next->cctx))
1663 next->cctx = cctx_get (aTHX); 1722 next->cctx = cctx_get (aTHX);
1664 1723
1665 cctx_put (cctx_current); 1724 cctx_put (cctx_current);
1666 } 1725 }
1667 else 1726 else
1668 prev->cctx = cctx_current; 1727 prev->cctx = cctx_current;
1669 1728
1670 ++next->usecount; 1729 ++next->usecount;
1671 1730
1672 cctx_prev = cctx_current; 1731 cctx_prev = cctx_current;
1673 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); 1732 cctx_current = ecb_expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1674 1733
1675 next->cctx = 0; 1734 next->cctx = 0;
1676 1735
1677 if (expect_false (cctx_prev != cctx_current)) 1736 if (ecb_expect_false (cctx_prev != cctx_current))
1678 { 1737 {
1679 cctx_prev->top_env = PL_top_env; 1738 cctx_prev->top_env = PL_top_env;
1680 PL_top_env = cctx_current->top_env; 1739 PL_top_env = cctx_current->top_env;
1681 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); 1740 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1682 } 1741 }
1702 return; 1761 return;
1703 1762
1704 slf_destroy (aTHX_ coro); 1763 slf_destroy (aTHX_ coro);
1705 1764
1706 coro->flags |= CF_ZOMBIE; 1765 coro->flags |= CF_ZOMBIE;
1707 1766
1708 if (coro->flags & CF_READY) 1767 if (coro->flags & CF_READY)
1709 { 1768 {
1710 /* reduce nready, as destroying a ready coro effectively unreadies it */ 1769 /* reduce nready, as destroying a ready coro effectively unreadies it */
1711 /* alternative: look through all ready queues and remove the coro */ 1770 /* alternative: look through all ready queues and remove the coro */
1712 --coro_nready; 1771 --coro_nready;
1748 Safefree (coro); 1807 Safefree (coro);
1749 1808
1750 return 0; 1809 return 0;
1751} 1810}
1752 1811
1753static int 1812static int ecb_cold
1754coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) 1813coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
1755{ 1814{
1756 /* called when perl clones the current process the slow way (windows process emulation) */ 1815 /* called when perl clones the current process the slow way (windows process emulation) */
1757 /* WE SIMply nuke the pointers in the copy, causing perl to croak */ 1816 /* WE SIMply nuke the pointers in the copy, causing perl to croak */
1758 mg->mg_ptr = 0; 1817 mg->mg_ptr = 0;
1789 TRANSFER (ta, 1); 1848 TRANSFER (ta, 1);
1790} 1849}
1791 1850
1792/** Coro ********************************************************************/ 1851/** Coro ********************************************************************/
1793 1852
1794INLINE void 1853ecb_inline void
1795coro_enq (pTHX_ struct coro *coro) 1854coro_enq (pTHX_ struct coro *coro)
1796{ 1855{
1797 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; 1856 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN];
1798 1857
1799 SvREFCNT_inc_NN (coro->hv); 1858 SvREFCNT_inc_NN (coro->hv);
1801 coro->next_ready = 0; 1860 coro->next_ready = 0;
1802 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; 1861 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1803 ready [1] = coro; 1862 ready [1] = coro;
1804} 1863}
1805 1864
1806INLINE struct coro * 1865ecb_inline struct coro *
1807coro_deq (pTHX) 1866coro_deq (pTHX)
1808{ 1867{
1809 int prio; 1868 int prio;
1810 1869
1811 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) 1870 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; )
1864{ 1923{
1865 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1924 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1866} 1925}
1867 1926
1868/* expects to own a reference to next->hv */ 1927/* expects to own a reference to next->hv */
1869INLINE void 1928ecb_inline void
1870prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) 1929prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1871{ 1930{
1872 SV *prev_sv = SvRV (coro_current); 1931 SV *prev_sv = SvRV (coro_current);
1873 1932
1874 ta->prev = SvSTATE_hv (prev_sv); 1933 ta->prev = SvSTATE_hv (prev_sv);
1887{ 1946{
1888 for (;;) 1947 for (;;)
1889 { 1948 {
1890 struct coro *next = coro_deq (aTHX); 1949 struct coro *next = coro_deq (aTHX);
1891 1950
1892 if (expect_true (next)) 1951 if (ecb_expect_true (next))
1893 { 1952 {
1894 /* cannot transfer to destroyed coros, skip and look for next */ 1953 /* cannot transfer to destroyed coros, skip and look for next */
1895 if (expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED))) 1954 if (ecb_expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED)))
1896 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ 1955 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1897 else 1956 else
1898 { 1957 {
1899 next->flags &= ~CF_READY; 1958 next->flags &= ~CF_READY;
1900 --coro_nready; 1959 --coro_nready;
1933 } 1992 }
1934 } 1993 }
1935 } 1994 }
1936} 1995}
1937 1996
1938INLINE void 1997ecb_inline void
1939prepare_cede (pTHX_ struct coro_transfer_args *ta) 1998prepare_cede (pTHX_ struct coro_transfer_args *ta)
1940{ 1999{
1941 api_ready (aTHX_ coro_current); 2000 api_ready (aTHX_ coro_current);
1942 prepare_schedule (aTHX_ ta); 2001 prepare_schedule (aTHX_ ta);
1943} 2002}
1944 2003
1945INLINE void 2004ecb_inline void
1946prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) 2005prepare_cede_notself (pTHX_ struct coro_transfer_args *ta)
1947{ 2006{
1948 SV *prev = SvRV (coro_current); 2007 SV *prev = SvRV (coro_current);
1949 2008
1950 if (coro_nready) 2009 if (coro_nready)
1980{ 2039{
1981 struct coro_transfer_args ta; 2040 struct coro_transfer_args ta;
1982 2041
1983 prepare_cede (aTHX_ &ta); 2042 prepare_cede (aTHX_ &ta);
1984 2043
1985 if (expect_true (ta.prev != ta.next)) 2044 if (ecb_expect_true (ta.prev != ta.next))
1986 { 2045 {
1987 TRANSFER (ta, 1); 2046 TRANSFER (ta, 1);
1988 return 1; 2047 return 1;
1989 } 2048 }
1990 else 2049 else
2145 2204
2146static void 2205static void
2147coro_set_status (pTHX_ struct coro *coro, SV **arg, int items) 2206coro_set_status (pTHX_ struct coro *coro, SV **arg, int items)
2148{ 2207{
2149 AV *av; 2208 AV *av;
2150 2209
2151 if (coro->status) 2210 if (coro->status)
2152 { 2211 {
2153 av = coro->status; 2212 av = coro->status;
2154 av_clear (av); 2213 av_clear (av);
2155 } 2214 }
2202 2261
2203 coro = SvSTATE (arg [0]); 2262 coro = SvSTATE (arg [0]);
2204 coro_hv = coro->hv; 2263 coro_hv = coro->hv;
2205 2264
2206 coro_set_status (aTHX_ coro, arg + 1, items - 1); 2265 coro_set_status (aTHX_ coro, arg + 1, items - 1);
2207 2266
2208 if (expect_false (coro->flags & CF_NOCANCEL)) 2267 if (ecb_expect_false (coro->flags & CF_NOCANCEL))
2209 { 2268 {
2210 /* coro currently busy cancelling something, so just notify it */ 2269 /* coro currently busy cancelling something, so just notify it */
2211 coro->slf_frame.data = (void *)coro; 2270 coro->slf_frame.data = (void *)coro;
2212 2271
2213 frame->prepare = prepare_nop; 2272 frame->prepare = prepare_nop;
2321slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2380slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2322{ 2381{
2323 HV *hv = (HV *)SvRV (coro_current); 2382 HV *hv = (HV *)SvRV (coro_current);
2324 struct coro *coro = SvSTATE_hv ((SV *)hv); 2383 struct coro *coro = SvSTATE_hv ((SV *)hv);
2325 2384
2326 if (expect_true (coro->saved_deffh)) 2385 if (ecb_expect_true (coro->saved_deffh))
2327 { 2386 {
2328 /* subsequent iteration */ 2387 /* subsequent iteration */
2329 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2388 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2330 coro->saved_deffh = 0; 2389 coro->saved_deffh = 0;
2331 2390
2391 2450
2392static int 2451static int
2393slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) 2452slf_check_rouse_wait (pTHX_ struct CoroSLF *frame)
2394{ 2453{
2395 SV *data = (SV *)frame->data; 2454 SV *data = (SV *)frame->data;
2396 2455
2397 if (CORO_THROW) 2456 if (CORO_THROW)
2398 return 0; 2457 return 0;
2399 2458
2400 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2459 if (SvTYPE (SvRV (data)) != SVt_PVAV)
2401 return 1; 2460 return 1;
2437 cb = sv_2mortal (coro->rouse_cb); 2496 cb = sv_2mortal (coro->rouse_cb);
2438 coro->rouse_cb = 0; 2497 coro->rouse_cb = 0;
2439 } 2498 }
2440 2499
2441 if (!SvROK (cb) 2500 if (!SvROK (cb)
2442 || SvTYPE (SvRV (cb)) != SVt_PVCV 2501 || SvTYPE (SvRV (cb)) != SVt_PVCV
2443 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) 2502 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
2444 croak ("Coro::rouse_wait called with illegal callback argument,"); 2503 croak ("Coro::rouse_wait called with illegal callback argument,");
2445 2504
2446 { 2505 {
2447 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ 2506 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */
2597 I32 checkmark; /* mark SP to see how many elements check has pushed */ 2656 I32 checkmark; /* mark SP to see how many elements check has pushed */
2598 2657
2599 /* set up the slf frame, unless it has already been set-up */ 2658 /* set up the slf frame, unless it has already been set-up */
2600 /* the latter happens when a new coro has been started */ 2659 /* the latter happens when a new coro has been started */
2601 /* or when a new cctx was attached to an existing coroutine */ 2660 /* or when a new cctx was attached to an existing coroutine */
2602 if (expect_true (!slf_frame.prepare)) 2661 if (ecb_expect_true (!slf_frame.prepare))
2603 { 2662 {
2604 /* first iteration */ 2663 /* first iteration */
2605 dSP; 2664 dSP;
2606 SV **arg = PL_stack_base + TOPMARK + 1; 2665 SV **arg = PL_stack_base + TOPMARK + 1;
2607 int items = SP - arg; /* args without function object */ 2666 int items = SP - arg; /* args without function object */
2648 while (slf_frame.check (aTHX_ &slf_frame)); 2707 while (slf_frame.check (aTHX_ &slf_frame));
2649 2708
2650 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ 2709 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */
2651 2710
2652 /* exception handling */ 2711 /* exception handling */
2653 if (expect_false (CORO_THROW)) 2712 if (ecb_expect_false (CORO_THROW))
2654 { 2713 {
2655 SV *exception = sv_2mortal (CORO_THROW); 2714 SV *exception = sv_2mortal (CORO_THROW);
2656 2715
2657 CORO_THROW = 0; 2716 CORO_THROW = 0;
2658 sv_setsv (ERRSV, exception); 2717 sv_setsv (ERRSV, exception);
2660 } 2719 }
2661 2720
2662 /* return value handling - mostly like entersub */ 2721 /* return value handling - mostly like entersub */
2663 /* make sure we put something on the stack in scalar context */ 2722 /* make sure we put something on the stack in scalar context */
2664 if (GIMME_V == G_SCALAR 2723 if (GIMME_V == G_SCALAR
2665 && expect_false (PL_stack_sp != PL_stack_base + checkmark + 1)) 2724 && ecb_expect_false (PL_stack_sp != PL_stack_base + checkmark + 1))
2666 { 2725 {
2667 dSP; 2726 dSP;
2668 SV **bot = PL_stack_base + checkmark; 2727 SV **bot = PL_stack_base + checkmark;
2669 2728
2670 if (sp == bot) /* too few, push undef */ 2729 if (sp == bot) /* too few, push undef */
2784{ 2843{
2785 PerlIOBuf base; 2844 PerlIOBuf base;
2786 NV next, every; 2845 NV next, every;
2787} PerlIOCede; 2846} PerlIOCede;
2788 2847
2789static IV 2848static IV ecb_cold
2790PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) 2849PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2791{ 2850{
2792 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 2851 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
2793 2852
2794 self->every = SvCUR (arg) ? SvNV (arg) : 0.01; 2853 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
2795 self->next = nvtime () + self->every; 2854 self->next = nvtime () + self->every;
2796 2855
2797 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); 2856 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
2798} 2857}
2799 2858
2800static SV * 2859static SV * ecb_cold
2801PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) 2860PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
2802{ 2861{
2803 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 2862 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
2804 2863
2805 return newSVnv (self->every); 2864 return newSVnv (self->every);
2950 { 3009 {
2951 int i; 3010 int i;
2952 /* if we were woken up but can't down, we look through the whole */ 3011 /* if we were woken up but can't down, we look through the whole */
2953 /* waiters list and only add us if we aren't in there already */ 3012 /* waiters list and only add us if we aren't in there already */
2954 /* this avoids some degenerate memory usage cases */ 3013 /* this avoids some degenerate memory usage cases */
2955 for (i = AvFILLp (av); i > 0; --i) // i > 0 is not an off-by-one bug 3014 for (i = AvFILLp (av); i > 0; --i) /* i > 0 is not an off-by-one bug */
2956 if (AvARRAY (av)[i] == coro_hv) 3015 if (AvARRAY (av)[i] == coro_hv)
2957 return 1; 3016 return 1;
2958 3017
2959 av_push (av, SvREFCNT_inc (coro_hv)); 3018 av_push (av, SvREFCNT_inc (coro_hv));
2960 return 1; 3019 return 1;
3217 dSP; 3276 dSP;
3218 3277
3219 static SV *prio_cv; 3278 static SV *prio_cv;
3220 static SV *prio_sv; 3279 static SV *prio_sv;
3221 3280
3222 if (expect_false (!prio_cv)) 3281 if (ecb_expect_false (!prio_cv))
3223 { 3282 {
3224 prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0); 3283 prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0);
3225 prio_sv = newSViv (0); 3284 prio_sv = newSViv (0);
3226 } 3285 }
3227 3286
3333 } 3392 }
3334 3393
3335 return coro_sv; 3394 return coro_sv;
3336} 3395}
3337 3396
3397#ifndef __cplusplus
3398ecb_cold XS(boot_Coro__State);
3399#endif
3400
3401#if CORO_JIT
3402
3403static void ecb_noinline ecb_cold
3404pushav_4uv (pTHX_ UV a, UV b, UV c, UV d)
3405{
3406 dSP;
3407 AV *av = newAV ();
3408
3409 av_store (av, 3, newSVuv (d));
3410 av_store (av, 2, newSVuv (c));
3411 av_store (av, 1, newSVuv (b));
3412 av_store (av, 0, newSVuv (a));
3413
3414 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
3415
3416 PUTBACK;
3417}
3418
3419static void ecb_noinline ecb_cold
3420jit_init (pTHX)
3421{
3422 dSP;
3423 SV *load, *save;
3424 char *map_base;
3425 char *load_ptr, *save_ptr;
3426 STRLEN load_len, save_len, map_len;
3427 int count;
3428
3429 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1);
3430
3431 PUSHMARK (SP);
3432 #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type));
3433 #include "state.h"
3434 count = call_pv ("Coro::State::_jit", G_ARRAY);
3435 SPAGAIN;
3436
3437 save = POPs; save_ptr = SvPVbyte (save, save_len);
3438 load = POPs; load_ptr = SvPVbyte (load, load_len);
3439
3440 map_len = load_len + save_len + 16;
3441
3442 map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3443
3444 assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED));
3445
3446 load_perl_slots = (load_save_perl_slots_type)map_base;
3447 memcpy (map_base, load_ptr, load_len);
3448
3449 map_base += (load_len + 15) & ~15;
3450
3451 save_perl_slots = (load_save_perl_slots_type)map_base;
3452 memcpy (map_base, save_ptr, save_len);
3453
3454 /* we are good citizens and try to make the page read-only, so the evil evil */
3455 /* hackers might have it a bit more difficult */
3456 mprotect (map_base, map_len, PROT_READ | PROT_EXEC);
3457
3458 PUTBACK;
3459 eval_pv ("undef &Coro::State::_jit", 1);
3460}
3461
3462#endif
3463
3338MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 3464MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
3339 3465
3340PROTOTYPES: DISABLE 3466PROTOTYPES: DISABLE
3341 3467
3342BOOT: 3468BOOT:
3345# if CORO_PTHREAD 3471# if CORO_PTHREAD
3346 coro_thx = PERL_GET_CONTEXT; 3472 coro_thx = PERL_GET_CONTEXT;
3347# endif 3473# endif
3348#endif 3474#endif
3349 BOOT_PAGESIZE; 3475 BOOT_PAGESIZE;
3476
3477 /* perl defines these to check for existance first, but why it doesn't */
3478 /* just create them one at init time is not clear to me, except for */
3479 /* programs trying to delete them, but... */
3480 /* anyway, we declare this as invalid and make sure they are initialised here */
3481 DEFSV;
3482 ERRSV;
3350 3483
3351 cctx_current = cctx_new_empty (); 3484 cctx_current = cctx_new_empty ();
3352 3485
3353 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 3486 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
3354 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 3487 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
3397 coroapi.prepare_cede_notself = prepare_cede_notself; 3530 coroapi.prepare_cede_notself = prepare_cede_notself;
3398 3531
3399 time_init (aTHX); 3532 time_init (aTHX);
3400 3533
3401 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3534 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3535#if CORO_JIT
3536 PUTBACK;
3537 jit_init (aTHX);
3538 SPAGAIN;
3539#endif
3402} 3540}
3403 3541
3404SV * 3542SV *
3405new (SV *klass, ...) 3543new (SV *klass, ...)
3406 ALIAS: 3544 ALIAS:
3483void 3621void
3484list () 3622list ()
3485 PROTOTYPE: 3623 PROTOTYPE:
3486 PPCODE: 3624 PPCODE:
3487{ 3625{
3488 struct coro *coro; 3626 struct coro *coro;
3489 for (coro = coro_first; coro; coro = coro->next) 3627 for (coro = coro_first; coro; coro = coro->next)
3490 if (coro->hv) 3628 if (coro->hv)
3491 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 3629 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
3492} 3630}
3493 3631
3557 RETVAL = boolSV (coro->flags & ix); 3695 RETVAL = boolSV (coro->flags & ix);
3558 OUTPUT: 3696 OUTPUT:
3559 RETVAL 3697 RETVAL
3560 3698
3561void 3699void
3562throw (Coro::State self, SV *exception = &PL_sv_undef) 3700throw (SV *self, SV *exception = &PL_sv_undef)
3563 PROTOTYPE: $;$ 3701 PROTOTYPE: $;$
3564 CODE: 3702 CODE:
3565{ 3703{
3704 struct coro *coro = SvSTATE (self);
3566 struct coro *current = SvSTATE_current; 3705 struct coro *current = SvSTATE_current;
3567 SV **exceptionp = self == current ? &CORO_THROW : &self->except; 3706 SV **exceptionp = coro == current ? &CORO_THROW : &coro->except;
3568 SvREFCNT_dec (*exceptionp); 3707 SvREFCNT_dec (*exceptionp);
3569 SvGETMAGIC (exception); 3708 SvGETMAGIC (exception);
3570 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; 3709 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3710
3711 api_ready (aTHX_ self);
3571} 3712}
3572 3713
3573void 3714void
3574api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3715api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3575 PROTOTYPE: $;$ 3716 PROTOTYPE: $;$
3652 3793
3653void 3794void
3654times (Coro::State self) 3795times (Coro::State self)
3655 PPCODE: 3796 PPCODE:
3656{ 3797{
3657 struct coro *current = SvSTATE (coro_current); 3798 struct coro *current = SvSTATE (coro_current);
3658 3799
3659 if (expect_false (current == self)) 3800 if (ecb_expect_false (current == self))
3660 { 3801 {
3661 coro_times_update (); 3802 coro_times_update ();
3662 coro_times_add (SvSTATE (coro_current)); 3803 coro_times_add (SvSTATE (coro_current));
3663 } 3804 }
3664 3805
3665 EXTEND (SP, 2); 3806 EXTEND (SP, 2);
3666 PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); 3807 PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9)));
3667 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); 3808 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9)));
3668 3809
3669 if (expect_false (current == self)) 3810 if (ecb_expect_false (current == self))
3670 coro_times_sub (SvSTATE (coro_current)); 3811 coro_times_sub (SvSTATE (coro_current));
3671} 3812}
3672 3813
3673void 3814void
3674swap_sv (Coro::State coro, SV *sv, SV *swapsv) 3815swap_sv (Coro::State coro, SV *sv, SV *swapsv)
3705 3846
3706 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3847 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3707 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3848 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3708 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3849 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3709 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ 3850 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */
3710 3851
3711 coro_stash = gv_stashpv ("Coro", TRUE); 3852 coro_stash = gv_stashpv ("Coro", TRUE);
3712 3853
3713 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); 3854 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3714 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); 3855 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3715 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); 3856 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));
3937 on_leave = 1 4078 on_leave = 1
3938 PROTOTYPE: & 4079 PROTOTYPE: &
3939 CODE: 4080 CODE:
3940{ 4081{
3941 struct coro *coro = SvSTATE_current; 4082 struct coro *coro = SvSTATE_current;
3942 AV **avp = ix ? &coro->on_leave : &coro->on_enter; 4083 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3943 4084
3944 block = s_get_cv_croak (block); 4085 block = s_get_cv_croak (block);
3945 4086
3946 if (!*avp) 4087 if (!*avp)
3947 *avp = newAV (); 4088 *avp = newAV ();
3967 4108
3968SV * 4109SV *
3969new (SV *klass, SV *count = 0) 4110new (SV *klass, SV *count = 0)
3970 CODE: 4111 CODE:
3971{ 4112{
3972 int semcnt = 1; 4113 int semcnt = 1;
3973 4114
3974 if (count) 4115 if (count)
3975 { 4116 {
3976 SvGETMAGIC (count); 4117 SvGETMAGIC (count);
3977 4118
4037 XSRETURN_NO; 4178 XSRETURN_NO;
4038} 4179}
4039 4180
4040void 4181void
4041waiters (SV *self) 4182waiters (SV *self)
4042 PPCODE: 4183 PPCODE:
4043{ 4184{
4044 AV *av = (AV *)SvRV (self); 4185 AV *av = (AV *)SvRV (self);
4045 int wcount = AvFILLp (av) + 1 - 1; 4186 int wcount = AvFILLp (av) + 1 - 1;
4046 4187
4047 if (GIMME_V == G_SCALAR) 4188 if (GIMME_V == G_SCALAR)
4056} 4197}
4057 4198
4058MODULE = Coro::State PACKAGE = Coro::SemaphoreSet 4199MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
4059 4200
4060void 4201void
4061_may_delete (SV *sem, int count, int extra_refs) 4202_may_delete (SV *sem, int count, unsigned int extra_refs)
4062 PPCODE: 4203 PPCODE:
4063{ 4204{
4064 AV *av = (AV *)SvRV (sem); 4205 AV *av = (AV *)SvRV (sem);
4065 4206
4066 if (SvREFCNT ((SV *)av) == 1 + extra_refs 4207 if (SvREFCNT ((SV *)av) == 1 + extra_refs
4067 && AvFILLp (av) == 0 /* no waiters, just count */ 4208 && AvFILLp (av) == 0 /* no waiters, just count */
4068 && SvIV (AvARRAY (av)[0]) == count) 4209 && SvIV (AvARRAY (av)[0]) == count)
4069 XSRETURN_YES; 4210 XSRETURN_YES;
4090 4231
4091void 4232void
4092broadcast (SV *self) 4233broadcast (SV *self)
4093 CODE: 4234 CODE:
4094{ 4235{
4095 AV *av = (AV *)SvRV (self); 4236 AV *av = (AV *)SvRV (self);
4096 coro_signal_wake (aTHX_ av, AvFILLp (av)); 4237 coro_signal_wake (aTHX_ av, AvFILLp (av));
4097} 4238}
4098 4239
4099void 4240void
4100send (SV *self) 4241send (SV *self)
4108 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ 4249 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */
4109} 4250}
4110 4251
4111IV 4252IV
4112awaited (SV *self) 4253awaited (SV *self)
4113 CODE: 4254 CODE:
4114 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; 4255 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1;
4115 OUTPUT: 4256 OUTPUT:
4116 RETVAL 4257 RETVAL
4117 4258
4118 4259
4123 4264
4124void 4265void
4125_schedule (...) 4266_schedule (...)
4126 CODE: 4267 CODE:
4127{ 4268{
4128 static int incede; 4269 static int incede;
4129 4270
4130 api_cede_notself (aTHX); 4271 api_cede_notself (aTHX);
4131 4272
4132 ++incede; 4273 ++incede;
4133 while (coro_nready >= incede && api_cede (aTHX)) 4274 while (coro_nready >= incede && api_cede (aTHX))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines