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