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