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