1 | /* this works around a bug in mingw32 providing a non-working setjmp */ |
1 | /* this works around a bug in mingw32 providing a non-working setjmp */ |
2 | #define USE_NO_MINGW_SETJMP_TWO_ARGS |
2 | #define USE_NO_MINGW_SETJMP_TWO_ARGS |
3 | |
3 | |
4 | #define NDEBUG 1 |
4 | #define NDEBUG 1 /* perl usually disables NDEBUG later */ |
5 | |
5 | |
6 | #include "libcoro/coro.c" |
6 | #include "libcoro/coro.c" |
7 | |
7 | |
8 | #define PERL_NO_GET_CONTEXT |
8 | #define PERL_NO_GET_CONTEXT |
9 | #define PERL_EXT |
9 | #define PERL_EXT |
… | |
… | |
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 | |
|
|
18 | #define ECB_NO_THREADS 1 |
|
|
19 | #define ECB_NO_LIBM 1 |
17 | #include "ecb.h" |
20 | #include "ecb.h" |
18 | |
21 | |
19 | #include <stddef.h> |
22 | #include <stddef.h> |
20 | #include <stdio.h> |
23 | #include <stdio.h> |
21 | #include <errno.h> |
24 | #include <errno.h> |
22 | #include <assert.h> |
25 | #include <assert.h> |
23 | |
26 | |
24 | #ifndef SVs_PADSTALE |
27 | #ifndef SVs_PADSTALE |
25 | # define SVs_PADSTALE 0 |
28 | # define SVs_PADSTALE 0 |
|
|
29 | #endif |
|
|
30 | |
|
|
31 | #ifdef PadARRAY |
|
|
32 | # define NEWPADAPI 1 |
|
|
33 | # define newPADLIST(var) (Newz (0, var, 1, PADLIST), Newx (PadlistARRAY (var), 2, PAD *)) |
|
|
34 | #else |
|
|
35 | typedef AV PADNAMELIST; |
|
|
36 | # if !PERL_VERSION_ATLEAST(5,8,0) |
|
|
37 | typedef AV PADLIST; |
|
|
38 | typedef AV PAD; |
|
|
39 | # endif |
|
|
40 | # define PadlistARRAY(pl) ((PAD **)AvARRAY (pl)) |
|
|
41 | # define PadlistMAX(pl) AvFILLp (pl) |
|
|
42 | # define PadlistNAMES(pl) (*PadlistARRAY (pl)) |
|
|
43 | # define PadARRAY AvARRAY |
|
|
44 | # define PadMAX AvFILLp |
|
|
45 | # define newPADLIST(var) ((var) = newAV (), av_extend (var, 1)) |
|
|
46 | #endif |
|
|
47 | #ifndef PadnamelistREFCNT |
|
|
48 | # define PadnamelistREFCNT(pnl) SvREFCNT (pnl) |
|
|
49 | #endif |
|
|
50 | #ifndef PadnamelistREFCNT_dec |
|
|
51 | # define PadnamelistREFCNT_dec(pnl) SvREFCNT_dec (pnl) |
|
|
52 | #endif |
|
|
53 | |
|
|
54 | /* 5.19.something has replaced SVt_BIND by SVt_INVLIST */ |
|
|
55 | /* we just alias it to SVt_IV, as that is sufficient for swap_sv for now */ |
|
|
56 | #if PERL_VERSION_ATLEAST(5,19,0) |
|
|
57 | # define SVt_BIND SVt_IV |
26 | #endif |
58 | #endif |
27 | |
59 | |
28 | #if defined(_WIN32) |
60 | #if defined(_WIN32) |
29 | # undef HAS_GETTIMEOFDAY |
61 | # undef HAS_GETTIMEOFDAY |
30 | # undef setjmp |
62 | # undef setjmp |
… | |
… | |
33 | # define setjmp _setjmp /* deep magic */ |
65 | # define setjmp _setjmp /* deep magic */ |
34 | #else |
66 | #else |
35 | # include <inttypes.h> /* most portable stdint.h */ |
67 | # include <inttypes.h> /* most portable stdint.h */ |
36 | #endif |
68 | #endif |
37 | |
69 | |
38 | #if HAVE_MMAP |
|
|
39 | # include <unistd.h> |
|
|
40 | # include <sys/mman.h> |
|
|
41 | # ifndef MAP_ANONYMOUS |
|
|
42 | # ifdef MAP_ANON |
|
|
43 | # define MAP_ANONYMOUS MAP_ANON |
|
|
44 | # else |
|
|
45 | # undef HAVE_MMAP |
|
|
46 | # endif |
|
|
47 | # endif |
|
|
48 | # include <limits.h> |
|
|
49 | # ifndef PAGESIZE |
|
|
50 | # define PAGESIZE pagesize |
|
|
51 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
|
|
52 | static long pagesize; |
|
|
53 | # else |
|
|
54 | # define BOOT_PAGESIZE (void)0 |
|
|
55 | # endif |
|
|
56 | #else |
|
|
57 | # define PAGESIZE 0 |
|
|
58 | # define BOOT_PAGESIZE (void)0 |
|
|
59 | #endif |
|
|
60 | |
|
|
61 | #if CORO_USE_VALGRIND |
|
|
62 | # include <valgrind/valgrind.h> |
|
|
63 | #endif |
|
|
64 | |
|
|
65 | /* the maximum number of idle cctx that will be pooled */ |
70 | /* the maximum number of idle cctx that will be pooled */ |
66 | static int cctx_max_idle = 4; |
71 | static int cctx_max_idle = 4; |
67 | |
72 | |
68 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
73 | #if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0) |
69 | # undef CORO_STACKGUARD |
74 | # define HAS_SCOPESTACK_NAME 1 |
70 | #endif |
|
|
71 | |
|
|
72 | #ifndef CORO_STACKGUARD |
|
|
73 | # define CORO_STACKGUARD 0 |
|
|
74 | #endif |
75 | #endif |
75 | |
76 | |
76 | /* prefer perl internal functions over our own? */ |
77 | /* prefer perl internal functions over our own? */ |
77 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
78 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
78 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
79 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
… | |
… | |
97 | # if CORO_PTHREAD |
98 | # if CORO_PTHREAD |
98 | static void *coro_thx; |
99 | static void *coro_thx; |
99 | # endif |
100 | # endif |
100 | #endif |
101 | #endif |
101 | |
102 | |
102 | /* used in state.h */ |
|
|
103 | #define VAR(name,type) VARx(name, PL_ ## name, type) |
|
|
104 | |
|
|
105 | #ifdef __linux |
103 | #ifdef __linux |
106 | # include <time.h> /* for timespec */ |
104 | # include <time.h> /* for timespec */ |
107 | # include <syscall.h> /* for SYS_* */ |
105 | # include <syscall.h> /* for SYS_* */ |
108 | # ifdef SYS_clock_gettime |
106 | # ifdef SYS_clock_gettime |
109 | # define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
107 | # define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
… | |
… | |
172 | typedef struct coro_cctx |
170 | typedef struct coro_cctx |
173 | { |
171 | { |
174 | struct coro_cctx *next; |
172 | struct coro_cctx *next; |
175 | |
173 | |
176 | /* the stack */ |
174 | /* the stack */ |
177 | void *sptr; |
175 | struct coro_stack stack; |
178 | size_t ssize; |
|
|
179 | |
176 | |
180 | /* cpu state */ |
177 | /* cpu state */ |
181 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
178 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
179 | #ifndef NDEBUG |
182 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
180 | JMPENV *idle_te; /* same as idle_sp, but for top_env */ |
|
|
181 | #endif |
183 | JMPENV *top_env; |
182 | JMPENV *top_env; |
184 | coro_context cctx; |
183 | coro_context cctx; |
185 | |
184 | |
186 | U32 gen; |
185 | U32 gen; |
187 | #if CORO_USE_VALGRIND |
186 | #if CORO_USE_VALGRIND |
… | |
… | |
207 | }; |
206 | }; |
208 | |
207 | |
209 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
208 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
210 | typedef struct |
209 | typedef struct |
211 | { |
210 | { |
212 | #define VARx(name,expr,type) type name; |
211 | #define VARx(name,expr,type) type name; |
213 | # include "state.h" |
212 | #include "state.h" |
214 | #undef VARx |
|
|
215 | } perl_slots; |
213 | } perl_slots; |
216 | |
214 | |
217 | /* how many context stack entries do we need for perl_slots */ |
215 | /* how many context stack entries do we need for perl_slots */ |
218 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
216 | #define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) |
219 | |
217 | |
… | |
… | |
291 | #define coro_nready coroapi.nready |
289 | #define coro_nready coroapi.nready |
292 | |
290 | |
293 | /** JIT *********************************************************************/ |
291 | /** JIT *********************************************************************/ |
294 | |
292 | |
295 | #if CORO_JIT |
293 | #if CORO_JIT |
296 | /* APPLE doesn't have HAVE_MMAP though */ |
294 | /* APPLE doesn't have mmap though */ |
297 | #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) |
295 | #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) |
298 | #ifndef CORO_JIT_TYPE |
296 | #ifndef CORO_JIT_TYPE |
299 | #if __x86_64 && CORO_JIT_UNIXY |
297 | #if ECB_AMD64 && CORO_JIT_UNIXY |
300 | #define CORO_JIT_TYPE "amd64-unix" |
298 | #define CORO_JIT_TYPE "amd64-unix" |
301 | #elif __i386 && CORO_JIT_UNIXY |
299 | #elif __i386 && CORO_JIT_UNIXY |
302 | #define CORO_JIT_TYPE "x86-unix" |
300 | #define CORO_JIT_TYPE "x86-unix" |
303 | #endif |
301 | #endif |
304 | #endif |
302 | #endif |
305 | #endif |
303 | #endif |
306 | |
304 | |
307 | #if !defined(CORO_JIT_TYPE) || !HAVE_MMAP |
305 | #if !defined(CORO_JIT_TYPE) || _POSIX_MEMORY_PROTECTION <= 0 |
308 | #undef CORO_JIT |
306 | #undef CORO_JIT |
309 | #endif |
307 | #endif |
310 | |
308 | |
311 | #if CORO_JIT |
309 | #if CORO_JIT |
312 | typedef void (*load_save_perl_slots_type)(perl_slots *); |
310 | typedef void (*load_save_perl_slots_type)(perl_slots *); |
… | |
… | |
335 | |
333 | |
336 | /** time stuff **************************************************************/ |
334 | /** time stuff **************************************************************/ |
337 | |
335 | |
338 | #ifdef HAS_GETTIMEOFDAY |
336 | #ifdef HAS_GETTIMEOFDAY |
339 | |
337 | |
340 | ECB_INLINE void |
338 | ecb_inline void |
341 | coro_u2time (pTHX_ UV ret[2]) |
339 | coro_u2time (pTHX_ UV ret[2]) |
342 | { |
340 | { |
343 | struct timeval tv; |
341 | struct timeval tv; |
344 | gettimeofday (&tv, 0); |
342 | gettimeofday (&tv, 0); |
345 | |
343 | |
346 | ret [0] = tv.tv_sec; |
344 | ret [0] = tv.tv_sec; |
347 | ret [1] = tv.tv_usec; |
345 | ret [1] = tv.tv_usec; |
348 | } |
346 | } |
349 | |
347 | |
350 | ECB_INLINE double |
348 | ecb_inline double |
351 | coro_nvtime (void) |
349 | coro_nvtime (void) |
352 | { |
350 | { |
353 | struct timeval tv; |
351 | struct timeval tv; |
354 | gettimeofday (&tv, 0); |
352 | gettimeofday (&tv, 0); |
355 | |
353 | |
356 | return tv.tv_sec + tv.tv_usec * 1e-6; |
354 | return tv.tv_sec + tv.tv_usec * 1e-6; |
357 | } |
355 | } |
358 | |
356 | |
359 | ECB_INLINE void |
357 | ecb_inline void |
360 | time_init (pTHX) |
358 | time_init (pTHX) |
361 | { |
359 | { |
362 | nvtime = coro_nvtime; |
360 | nvtime = coro_nvtime; |
363 | u2time = coro_u2time; |
361 | u2time = coro_u2time; |
364 | } |
362 | } |
365 | |
363 | |
366 | #else |
364 | #else |
367 | |
365 | |
368 | ECB_INLINE void |
366 | ecb_inline void |
369 | time_init (pTHX) |
367 | time_init (pTHX) |
370 | { |
368 | { |
371 | SV **svp; |
369 | SV **svp; |
372 | |
370 | |
373 | require_pv ("Time/HiRes.pm"); |
371 | require_pv ("Time/HiRes.pm"); |
374 | |
372 | |
375 | svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
373 | svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
376 | |
374 | |
377 | if (!svp) croak ("Time::HiRes is required, but missing. Caught"); |
375 | if (!svp) croak ("Time::HiRes is required, but missing. Caught"); |
378 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught"); |
376 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught"); |
379 | |
377 | |
… | |
… | |
415 | get_hv (name, create); |
413 | get_hv (name, create); |
416 | #endif |
414 | #endif |
417 | return get_hv (name, create); |
415 | return get_hv (name, create); |
418 | } |
416 | } |
419 | |
417 | |
420 | ECB_INLINE void |
418 | ecb_inline void |
421 | coro_times_update (void) |
419 | coro_times_update (void) |
422 | { |
420 | { |
423 | #ifdef coro_clock_gettime |
421 | #ifdef coro_clock_gettime |
424 | struct timespec ts; |
422 | struct timespec ts; |
425 | |
423 | |
… | |
… | |
438 | time_real [0] = tv [0]; |
436 | time_real [0] = tv [0]; |
439 | time_real [1] = tv [1] * 1000; |
437 | time_real [1] = tv [1] * 1000; |
440 | #endif |
438 | #endif |
441 | } |
439 | } |
442 | |
440 | |
443 | ECB_INLINE void |
441 | ecb_inline void |
444 | coro_times_add (struct coro *c) |
442 | coro_times_add (struct coro *c) |
445 | { |
443 | { |
446 | c->t_real [1] += time_real [1]; |
444 | c->t_real [1] += time_real [1]; |
447 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
445 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
448 | c->t_real [0] += time_real [0]; |
446 | c->t_real [0] += time_real [0]; |
… | |
… | |
450 | c->t_cpu [1] += time_cpu [1]; |
448 | c->t_cpu [1] += time_cpu [1]; |
451 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
449 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
452 | c->t_cpu [0] += time_cpu [0]; |
450 | c->t_cpu [0] += time_cpu [0]; |
453 | } |
451 | } |
454 | |
452 | |
455 | ECB_INLINE void |
453 | ecb_inline void |
456 | coro_times_sub (struct coro *c) |
454 | coro_times_sub (struct coro *c) |
457 | { |
455 | { |
458 | if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } |
456 | if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } |
459 | c->t_real [1] -= time_real [1]; |
457 | c->t_real [1] -= time_real [1]; |
460 | c->t_real [0] -= time_real [0]; |
458 | c->t_real [0] -= time_real [0]; |
… | |
… | |
481 | : 0) |
479 | : 0) |
482 | |
480 | |
483 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
481 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
484 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
482 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
485 | |
483 | |
486 | ECB_INLINE MAGIC * |
484 | ecb_inline MAGIC * |
487 | SvSTATEhv_p (pTHX_ SV *coro) |
485 | SvSTATEhv_p (pTHX_ SV *coro) |
488 | { |
486 | { |
489 | MAGIC *mg; |
487 | MAGIC *mg; |
490 | |
488 | |
491 | if (ecb_expect_true ( |
489 | if (ecb_expect_true ( |
… | |
… | |
496 | return mg; |
494 | return mg; |
497 | |
495 | |
498 | return 0; |
496 | return 0; |
499 | } |
497 | } |
500 | |
498 | |
501 | ECB_INLINE struct coro * |
499 | ecb_inline struct coro * |
502 | SvSTATE_ (pTHX_ SV *coro) |
500 | SvSTATE_ (pTHX_ SV *coro) |
503 | { |
501 | { |
504 | MAGIC *mg; |
502 | MAGIC *mg; |
505 | |
503 | |
506 | if (SvROK (coro)) |
504 | if (SvROK (coro)) |
… | |
… | |
520 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
518 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
521 | |
519 | |
522 | /*****************************************************************************/ |
520 | /*****************************************************************************/ |
523 | /* padlist management and caching */ |
521 | /* padlist management and caching */ |
524 | |
522 | |
525 | ECB_INLINE AV * |
523 | ecb_inline PADLIST * |
526 | coro_derive_padlist (pTHX_ CV *cv) |
524 | coro_derive_padlist (pTHX_ CV *cv) |
527 | { |
525 | { |
528 | AV *padlist = CvPADLIST (cv); |
526 | PADLIST *padlist = CvPADLIST (cv); |
529 | AV *newpadlist, *newpad; |
527 | PADLIST *newpadlist; |
|
|
528 | PADNAMELIST *padnames; |
|
|
529 | PAD *newpad; |
|
|
530 | PADOFFSET off = PadlistMAX (padlist) + 1; |
530 | |
531 | |
531 | newpadlist = newAV (); |
532 | #if NEWPADAPI |
|
|
533 | |
|
|
534 | /* if we had the original CvDEPTH, we might be able to steal the CvDEPTH+1 entry instead */ |
|
|
535 | /* 20131102194744.GA6705@schmorp.de, 20131102195825.2013.qmail@lists-nntp.develooper.com */ |
|
|
536 | while (!PadlistARRAY (padlist)[off - 1]) |
|
|
537 | --off; |
|
|
538 | |
|
|
539 | Perl_pad_push (aTHX_ padlist, off); |
|
|
540 | |
|
|
541 | newpad = PadlistARRAY (padlist)[off]; |
|
|
542 | PadlistARRAY (padlist)[off] = 0; |
|
|
543 | |
|
|
544 | #else |
|
|
545 | |
|
|
546 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
547 | Perl_pad_push (aTHX_ padlist, off); |
|
|
548 | #else |
|
|
549 | Perl_pad_push (aTHX_ padlist, off, 1); |
|
|
550 | #endif |
|
|
551 | |
|
|
552 | newpad = PadlistARRAY (padlist)[off]; |
|
|
553 | PadlistMAX (padlist) = off - 1; |
|
|
554 | |
|
|
555 | #endif |
|
|
556 | |
|
|
557 | newPADLIST (newpadlist); |
|
|
558 | #if !PERL_VERSION_ATLEAST(5,15,3) |
|
|
559 | /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */ |
532 | AvREAL_off (newpadlist); |
560 | AvREAL_off (newpadlist); |
533 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
534 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
535 | #else |
|
|
536 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
|
|
537 | #endif |
561 | #endif |
538 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
|
|
539 | --AvFILLp (padlist); |
|
|
540 | |
562 | |
541 | av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); |
563 | /* Already extended to 2 elements by newPADLIST. */ |
542 | av_store (newpadlist, 1, (SV *)newpad); |
564 | PadlistMAX (newpadlist) = 1; |
|
|
565 | |
|
|
566 | padnames = PadlistNAMES (padlist); |
|
|
567 | ++PadnamelistREFCNT (padnames); |
|
|
568 | PadlistNAMES (newpadlist) = padnames; |
|
|
569 | |
|
|
570 | PadlistARRAY (newpadlist)[1] = newpad; |
543 | |
571 | |
544 | return newpadlist; |
572 | return newpadlist; |
545 | } |
573 | } |
546 | |
574 | |
547 | ECB_INLINE void |
575 | ecb_inline void |
548 | free_padlist (pTHX_ AV *padlist) |
576 | free_padlist (pTHX_ PADLIST *padlist) |
549 | { |
577 | { |
550 | /* may be during global destruction */ |
578 | /* may be during global destruction */ |
551 | if (!IN_DESTRUCT) |
579 | if (!IN_DESTRUCT) |
552 | { |
580 | { |
553 | I32 i = AvFILLp (padlist); |
581 | I32 i = PadlistMAX (padlist); |
554 | |
582 | |
555 | while (i > 0) /* special-case index 0 */ |
583 | while (i > 0) /* special-case index 0 */ |
556 | { |
584 | { |
557 | /* we try to be extra-careful here */ |
585 | /* we try to be extra-careful here */ |
558 | AV *av = (AV *)AvARRAY (padlist)[i--]; |
586 | PAD *pad = PadlistARRAY (padlist)[i--]; |
559 | I32 j = AvFILLp (av); |
|
|
560 | |
587 | |
|
|
588 | if (pad) |
|
|
589 | { |
|
|
590 | I32 j = PadMAX (pad); |
|
|
591 | |
561 | while (j >= 0) |
592 | while (j >= 0) |
562 | SvREFCNT_dec (AvARRAY (av)[j--]); |
593 | SvREFCNT_dec (PadARRAY (pad)[j--]); |
563 | |
594 | |
564 | AvFILLp (av) = -1; |
595 | PadMAX (pad) = -1; |
565 | SvREFCNT_dec (av); |
596 | SvREFCNT_dec (pad); |
|
|
597 | } |
566 | } |
598 | } |
567 | |
599 | |
568 | SvREFCNT_dec (AvARRAY (padlist)[0]); |
600 | PadnamelistREFCNT_dec (PadlistNAMES (padlist)); |
569 | |
601 | |
|
|
602 | #if NEWPADAPI |
|
|
603 | Safefree (PadlistARRAY (padlist)); |
|
|
604 | Safefree (padlist); |
|
|
605 | #else |
570 | AvFILLp (padlist) = -1; |
606 | AvFILLp (padlist) = -1; |
|
|
607 | AvREAL_off (padlist); |
571 | SvREFCNT_dec ((SV*)padlist); |
608 | SvREFCNT_dec ((SV*)padlist); |
|
|
609 | #endif |
572 | } |
610 | } |
573 | } |
611 | } |
574 | |
612 | |
575 | static int |
613 | static int |
576 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
614 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
577 | { |
615 | { |
578 | AV *padlist; |
616 | PADLIST *padlist; |
579 | AV *av = (AV *)mg->mg_obj; |
617 | PADLIST **padlists = (PADLIST **)(mg->mg_ptr + sizeof(size_t)); |
|
|
618 | size_t len = *(size_t *)mg->mg_ptr; |
580 | |
619 | |
581 | /* perl manages to free our internal AV and _then_ call us */ |
620 | /* perl manages to free our internal AV and _then_ call us */ |
582 | if (IN_DESTRUCT) |
621 | if (IN_DESTRUCT) |
583 | return 0; |
622 | return 0; |
584 | |
623 | |
585 | /* casting is fun. */ |
624 | while (len--) |
586 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
|
|
587 | free_padlist (aTHX_ padlist); |
625 | free_padlist (aTHX_ padlists[len]); |
588 | |
|
|
589 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
|
|
590 | |
626 | |
591 | return 0; |
627 | return 0; |
592 | } |
628 | } |
593 | |
629 | |
594 | static MGVTBL coro_cv_vtbl = { |
630 | static MGVTBL coro_cv_vtbl = { |
595 | 0, 0, 0, 0, |
631 | 0, 0, 0, 0, |
596 | coro_cv_free |
632 | coro_cv_free |
597 | }; |
633 | }; |
598 | |
634 | |
599 | /* the next two functions merely cache the padlists */ |
635 | /* the next two functions merely cache the padlists */ |
600 | ECB_INLINE void |
636 | ecb_inline void |
601 | get_padlist (pTHX_ CV *cv) |
637 | get_padlist (pTHX_ CV *cv) |
602 | { |
638 | { |
603 | MAGIC *mg = CORO_MAGIC_cv (cv); |
639 | MAGIC *mg = CORO_MAGIC_cv (cv); |
604 | AV *av; |
640 | size_t *lenp; |
605 | |
641 | |
606 | if (ecb_expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
642 | if (ecb_expect_true (mg && *(lenp = (size_t *)mg->mg_ptr))) |
607 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
643 | CvPADLIST (cv) = ((PADLIST **)(mg->mg_ptr + sizeof(size_t)))[--*lenp]; |
608 | else |
644 | else |
609 | { |
645 | { |
610 | #if CORO_PREFER_PERL_FUNCTIONS |
646 | #if CORO_PREFER_PERL_FUNCTIONS |
611 | /* this is probably cleaner? but also slower! */ |
647 | /* this is probably cleaner? but also slower! */ |
612 | /* in practise, it seems to be less stable */ |
648 | /* in practise, it seems to be less stable */ |
… | |
… | |
618 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
654 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
619 | #endif |
655 | #endif |
620 | } |
656 | } |
621 | } |
657 | } |
622 | |
658 | |
623 | ECB_INLINE void |
659 | ecb_inline void |
624 | put_padlist (pTHX_ CV *cv) |
660 | put_padlist (pTHX_ CV *cv) |
625 | { |
661 | { |
626 | MAGIC *mg = CORO_MAGIC_cv (cv); |
662 | MAGIC *mg = CORO_MAGIC_cv (cv); |
627 | AV *av; |
|
|
628 | |
663 | |
629 | if (ecb_expect_false (!mg)) |
664 | if (ecb_expect_false (!mg)) |
|
|
665 | { |
630 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
666 | mg = sv_magicext ((SV *)cv, 0, CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
|
|
667 | Newz (0, mg->mg_ptr ,sizeof (size_t) + sizeof (PADLIST *), char); |
|
|
668 | mg->mg_len = 1; /* so mg_free frees mg_ptr */ |
|
|
669 | } |
|
|
670 | else |
|
|
671 | Renew (mg->mg_ptr, |
|
|
672 | sizeof(size_t) + (*(size_t *)mg->mg_ptr + 1) * sizeof(PADLIST *), |
|
|
673 | char); |
631 | |
674 | |
632 | av = (AV *)mg->mg_obj; |
675 | ((PADLIST **)(mg->mg_ptr + sizeof (size_t))) [(*(size_t *)mg->mg_ptr)++] = CvPADLIST (cv); |
633 | |
|
|
634 | if (ecb_expect_false (AvFILLp (av) >= AvMAX (av))) |
|
|
635 | av_extend (av, AvFILLp (av) + 1); |
|
|
636 | |
|
|
637 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
|
|
638 | } |
676 | } |
639 | |
677 | |
640 | /** load & save, init *******************************************************/ |
678 | /** load & save, init *******************************************************/ |
641 | |
679 | |
|
|
680 | ecb_inline void |
|
|
681 | swap_sv (SV *a, SV *b) |
|
|
682 | { |
|
|
683 | const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */ |
|
|
684 | SV tmp; |
|
|
685 | |
|
|
686 | /* swap sv_any */ |
|
|
687 | SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp); |
|
|
688 | |
|
|
689 | /* swap sv_flags */ |
|
|
690 | SvFLAGS (&tmp) = SvFLAGS (a); |
|
|
691 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
|
|
692 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
|
|
693 | |
|
|
694 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
695 | /* perl 5.10 and later complicates this _quite_ a bit, but it also |
|
|
696 | * is much faster, so no quarrels here. alternatively, we could |
|
|
697 | * sv_upgrade to avoid this. |
|
|
698 | */ |
|
|
699 | { |
|
|
700 | /* swap sv_u */ |
|
|
701 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
|
|
702 | |
|
|
703 | /* if SvANY points to the head, we need to adjust the pointers, |
|
|
704 | * as the pointer for a still points to b, and maybe vice versa. |
|
|
705 | */ |
|
|
706 | #define svany_in_head(type) \ |
|
|
707 | (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type))) |
|
|
708 | |
|
|
709 | if (svany_in_head (SvTYPE (a))) |
|
|
710 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
|
|
711 | |
|
|
712 | if (svany_in_head (SvTYPE (b))) |
|
|
713 | SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b); |
|
|
714 | } |
|
|
715 | #endif |
|
|
716 | } |
|
|
717 | |
642 | /* swap sv heads, at least logically */ |
718 | /* swap sv heads, at least logically */ |
643 | static void |
719 | static void |
644 | swap_svs (pTHX_ Coro__State c) |
720 | swap_svs (pTHX_ Coro__State c) |
645 | { |
721 | { |
646 | int i; |
722 | int i; |
647 | |
723 | |
648 | for (i = 0; i <= AvFILLp (c->swap_sv); ) |
724 | for (i = 0; i <= AvFILLp (c->swap_sv); i += 2) |
649 | { |
725 | swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]); |
650 | SV *a = AvARRAY (c->swap_sv)[i++]; |
|
|
651 | SV *b = AvARRAY (c->swap_sv)[i++]; |
|
|
652 | |
|
|
653 | const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */ |
|
|
654 | SV tmp; |
|
|
655 | |
|
|
656 | /* swap sv_any */ |
|
|
657 | SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp); |
|
|
658 | |
|
|
659 | /* swap sv_flags */ |
|
|
660 | SvFLAGS (&tmp) = SvFLAGS (a); |
|
|
661 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
|
|
662 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
|
|
663 | |
|
|
664 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
665 | /* perl 5.10 complicates this _quite_ a bit, but it also is |
|
|
666 | * much faster, so no quarrels here. alternatively, we could |
|
|
667 | * sv_upgrade to avoid this. |
|
|
668 | */ |
|
|
669 | { |
|
|
670 | /* swap sv_u */ |
|
|
671 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
|
|
672 | |
|
|
673 | /* if SvANY points to the head, we need to adjust the pointers, |
|
|
674 | * as the pointer for a still points to b, and maybe vice versa. |
|
|
675 | */ |
|
|
676 | #define svany_in_head(type) \ |
|
|
677 | (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type))) |
|
|
678 | |
|
|
679 | if (svany_in_head (SvTYPE (a))) |
|
|
680 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
|
|
681 | |
|
|
682 | if (svany_in_head (SvTYPE (b))) |
|
|
683 | SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b); |
|
|
684 | } |
|
|
685 | #endif |
|
|
686 | } |
|
|
687 | } |
726 | } |
688 | |
727 | |
689 | #define SWAP_SVS(coro) \ |
728 | #define SWAP_SVS(coro) \ |
690 | if (ecb_expect_false ((coro)->swap_sv)) \ |
729 | if (ecb_expect_false ((coro)->swap_sv)) \ |
691 | swap_svs (aTHX_ (coro)) |
730 | swap_svs (aTHX_ (coro)) |
… | |
… | |
703 | |
742 | |
704 | #if CORO_JIT |
743 | #if CORO_JIT |
705 | load_perl_slots (slot); |
744 | load_perl_slots (slot); |
706 | #else |
745 | #else |
707 | #define VARx(name,expr,type) expr = slot->name; |
746 | #define VARx(name,expr,type) expr = slot->name; |
708 | # include "state.h" |
747 | #include "state.h" |
709 | #undef VARx |
|
|
710 | #endif |
748 | #endif |
711 | |
749 | |
712 | { |
750 | { |
713 | dSP; |
751 | dSP; |
714 | |
752 | |
… | |
… | |
717 | /* now do the ugly restore mess */ |
755 | /* now do the ugly restore mess */ |
718 | while (ecb_expect_true (cv = (CV *)POPs)) |
756 | while (ecb_expect_true (cv = (CV *)POPs)) |
719 | { |
757 | { |
720 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
758 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
721 | CvDEPTH (cv) = PTR2IV (POPs); |
759 | CvDEPTH (cv) = PTR2IV (POPs); |
722 | CvPADLIST (cv) = (AV *)POPs; |
760 | CvPADLIST (cv) = (PADLIST *)POPs; |
723 | } |
761 | } |
724 | |
762 | |
725 | PUTBACK; |
763 | PUTBACK; |
726 | } |
764 | } |
727 | |
765 | |
… | |
… | |
836 | |
874 | |
837 | #if CORO_JIT |
875 | #if CORO_JIT |
838 | save_perl_slots (slot); |
876 | save_perl_slots (slot); |
839 | #else |
877 | #else |
840 | #define VARx(name,expr,type) slot->name = expr; |
878 | #define VARx(name,expr,type) slot->name = expr; |
841 | # include "state.h" |
879 | #include "state.h" |
842 | #undef VARx |
|
|
843 | #endif |
880 | #endif |
844 | } |
881 | } |
845 | } |
882 | } |
846 | |
883 | |
847 | /* |
884 | /* |
… | |
… | |
879 | #endif |
916 | #endif |
880 | |
917 | |
881 | New(54,PL_scopestack,8,I32); |
918 | New(54,PL_scopestack,8,I32); |
882 | PL_scopestack_ix = 0; |
919 | PL_scopestack_ix = 0; |
883 | PL_scopestack_max = 8; |
920 | PL_scopestack_max = 8; |
|
|
921 | #if HAS_SCOPESTACK_NAME |
|
|
922 | New(54,PL_scopestack_name,8,const char*); |
|
|
923 | #endif |
884 | |
924 | |
885 | New(54,PL_savestack,24,ANY); |
925 | New(54,PL_savestack,24,ANY); |
886 | PL_savestack_ix = 0; |
926 | PL_savestack_ix = 0; |
887 | PL_savestack_max = 24; |
927 | PL_savestack_max = 24; |
888 | |
928 | |
… | |
… | |
916 | } |
956 | } |
917 | |
957 | |
918 | Safefree (PL_tmps_stack); |
958 | Safefree (PL_tmps_stack); |
919 | Safefree (PL_markstack); |
959 | Safefree (PL_markstack); |
920 | Safefree (PL_scopestack); |
960 | Safefree (PL_scopestack); |
|
|
961 | #if HAS_SCOPESTACK_NAME |
|
|
962 | Safefree (PL_scopestack_name); |
|
|
963 | #endif |
921 | Safefree (PL_savestack); |
964 | Safefree (PL_savestack); |
922 | #if !PERL_VERSION_ATLEAST (5,10,0) |
965 | #if !PERL_VERSION_ATLEAST (5,10,0) |
923 | Safefree (PL_retstack); |
966 | Safefree (PL_retstack); |
924 | #endif |
967 | #endif |
925 | } |
968 | } |
… | |
… | |
985 | { |
1028 | { |
986 | SV **svp = 0; |
1029 | SV **svp = 0; |
987 | |
1030 | |
988 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
1031 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
989 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
1032 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
990 | |
1033 | |
991 | if (svp) |
1034 | if (svp) |
992 | { |
1035 | { |
993 | sv_setsv (sv, *svp ? *svp : &PL_sv_undef); |
1036 | SV *ssv; |
|
|
1037 | |
|
|
1038 | if (!*svp) |
|
|
1039 | ssv = &PL_sv_undef; |
|
|
1040 | else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */ |
|
|
1041 | ssv = sv_2mortal (newRV_inc (*svp)); |
|
|
1042 | else |
|
|
1043 | ssv = *svp; |
|
|
1044 | |
|
|
1045 | sv_setsv (sv, ssv); |
994 | return 0; |
1046 | return 0; |
995 | } |
1047 | } |
996 | } |
1048 | } |
997 | |
1049 | |
998 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
1050 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
… | |
… | |
1092 | PL_hints = 0; |
1144 | PL_hints = 0; |
1093 | |
1145 | |
1094 | /* recreate the die/warn hooks */ |
1146 | /* recreate the die/warn hooks */ |
1095 | PL_diehook = SvREFCNT_inc (rv_diehook); |
1147 | PL_diehook = SvREFCNT_inc (rv_diehook); |
1096 | PL_warnhook = SvREFCNT_inc (rv_warnhook); |
1148 | PL_warnhook = SvREFCNT_inc (rv_warnhook); |
1097 | |
1149 | |
1098 | GvSV (PL_defgv) = newSV (0); |
1150 | GvSV (PL_defgv) = newSV (0); |
1099 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1151 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1100 | GvSV (PL_errgv) = newSV (0); |
1152 | GvSV (PL_errgv) = newSV (0); |
1101 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
1153 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
1102 | GvHV (PL_hintgv) = 0; |
1154 | GvHV (PL_hintgv) = newHV (); |
|
|
1155 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
1156 | hv_magic (GvHV (PL_hintgv), 0, PERL_MAGIC_hints); |
|
|
1157 | #endif |
1103 | PL_rs = newSVsv (GvSV (irsgv)); |
1158 | PL_rs = newSVsv (GvSV (irsgv)); |
1104 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
1159 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
1105 | |
1160 | |
1106 | { |
1161 | { |
1107 | dSP; |
1162 | dSP; |
… | |
… | |
1220 | SvREFCNT_dec (coro->invoke_cb); |
1275 | SvREFCNT_dec (coro->invoke_cb); |
1221 | SvREFCNT_dec (coro->invoke_av); |
1276 | SvREFCNT_dec (coro->invoke_av); |
1222 | } |
1277 | } |
1223 | } |
1278 | } |
1224 | |
1279 | |
1225 | ECB_INLINE void |
1280 | ecb_inline void |
1226 | free_coro_mortal (pTHX) |
1281 | free_coro_mortal (pTHX) |
1227 | { |
1282 | { |
1228 | if (ecb_expect_true (coro_mortal)) |
1283 | if (ecb_expect_true (coro_mortal)) |
1229 | { |
1284 | { |
1230 | SvREFCNT_dec ((SV *)coro_mortal); |
1285 | SvREFCNT_dec ((SV *)coro_mortal); |
… | |
… | |
1389 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1444 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1390 | slf_frame.check = slf_check_set_stacklevel; |
1445 | slf_frame.check = slf_check_set_stacklevel; |
1391 | } |
1446 | } |
1392 | |
1447 | |
1393 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1448 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1394 | ECB_INLINE void |
1449 | ecb_inline void |
1395 | transfer_tail (pTHX) |
1450 | transfer_tail (pTHX) |
1396 | { |
1451 | { |
1397 | free_coro_mortal (aTHX); |
1452 | free_coro_mortal (aTHX); |
|
|
1453 | } |
|
|
1454 | |
|
|
1455 | /* try to exit the same way perl's main function would do */ |
|
|
1456 | /* we do not bother resetting the environment or other things *7 |
|
|
1457 | /* that are not, uhm, essential */ |
|
|
1458 | /* this obviously also doesn't work when perl is embedded */ |
|
|
1459 | static void ecb_noinline ecb_cold |
|
|
1460 | perlish_exit (pTHX) |
|
|
1461 | { |
|
|
1462 | int exitstatus = perl_destruct (PL_curinterp); |
|
|
1463 | perl_free (PL_curinterp); |
|
|
1464 | exit (exitstatus); |
1398 | } |
1465 | } |
1399 | |
1466 | |
1400 | /* |
1467 | /* |
1401 | * this is a _very_ stripped down perl interpreter ;) |
1468 | * this is a _very_ stripped down perl interpreter ;) |
1402 | */ |
1469 | */ |
… | |
… | |
1431 | */ |
1498 | */ |
1432 | |
1499 | |
1433 | /* |
1500 | /* |
1434 | * If perl-run returns we assume exit() was being called or the coro |
1501 | * If perl-run returns we assume exit() was being called or the coro |
1435 | * fell off the end, which seems to be the only valid (non-bug) |
1502 | * fell off the end, which seems to be the only valid (non-bug) |
1436 | * reason for perl_run to return. We try to exit by jumping to the |
1503 | * reason for perl_run to return. We try to mimic whatever perl is normally |
1437 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1504 | * doing in that case. YMMV. |
1438 | * coroutine as Coro has no such concept. |
|
|
1439 | * This actually isn't valid with the pthread backend, but OSes requiring |
|
|
1440 | * that backend are too broken to do it in a standards-compliant way. |
|
|
1441 | */ |
1505 | */ |
1442 | PL_top_env = main_top_env; |
1506 | perlish_exit (aTHX); |
1443 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
|
|
1444 | } |
1507 | } |
1445 | } |
1508 | } |
1446 | |
1509 | |
1447 | static coro_cctx * |
1510 | static coro_cctx * |
1448 | cctx_new (void) |
1511 | cctx_new (void) |
… | |
… | |
1463 | static coro_cctx * |
1526 | static coro_cctx * |
1464 | cctx_new_empty (void) |
1527 | cctx_new_empty (void) |
1465 | { |
1528 | { |
1466 | coro_cctx *cctx = cctx_new (); |
1529 | coro_cctx *cctx = cctx_new (); |
1467 | |
1530 | |
1468 | cctx->sptr = 0; |
1531 | cctx->stack.sptr = 0; |
1469 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
1532 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
1470 | |
1533 | |
1471 | return cctx; |
1534 | return cctx; |
1472 | } |
1535 | } |
1473 | |
1536 | |
1474 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1537 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1475 | static coro_cctx * |
1538 | static coro_cctx * |
1476 | cctx_new_run (void) |
1539 | cctx_new_run (void) |
1477 | { |
1540 | { |
1478 | coro_cctx *cctx = cctx_new (); |
1541 | coro_cctx *cctx = cctx_new (); |
1479 | void *stack_start; |
|
|
1480 | size_t stack_size; |
|
|
1481 | |
1542 | |
1482 | #if HAVE_MMAP |
1543 | if (!coro_stack_alloc (&cctx->stack, cctx_stacksize)) |
1483 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
|
|
1484 | /* mmap supposedly does allocate-on-write for us */ |
|
|
1485 | cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, 0, 0); |
|
|
1486 | |
|
|
1487 | if (cctx->sptr != (void *)-1) |
|
|
1488 | { |
|
|
1489 | #if CORO_STACKGUARD |
|
|
1490 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
|
|
1491 | #endif |
|
|
1492 | stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE; |
|
|
1493 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
|
|
1494 | cctx->flags |= CC_MAPPED; |
|
|
1495 | } |
1544 | { |
1496 | else |
|
|
1497 | #endif |
|
|
1498 | { |
|
|
1499 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
|
|
1500 | New (0, cctx->sptr, cctx_stacksize, long); |
|
|
1501 | |
|
|
1502 | if (!cctx->sptr) |
|
|
1503 | { |
|
|
1504 | perror ("FATAL: unable to allocate stack for coroutine, exiting."); |
1545 | perror ("FATAL: unable to allocate stack for coroutine, exiting."); |
1505 | _exit (EXIT_FAILURE); |
1546 | _exit (EXIT_FAILURE); |
1506 | } |
|
|
1507 | |
|
|
1508 | stack_start = cctx->sptr; |
|
|
1509 | stack_size = cctx->ssize; |
|
|
1510 | } |
1547 | } |
1511 | |
1548 | |
1512 | #if CORO_USE_VALGRIND |
|
|
1513 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size); |
|
|
1514 | #endif |
|
|
1515 | |
|
|
1516 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
1549 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, cctx->stack.sptr, cctx->stack.ssze); |
1517 | |
1550 | |
1518 | return cctx; |
1551 | return cctx; |
1519 | } |
1552 | } |
1520 | |
1553 | |
1521 | static void |
1554 | static void |
… | |
… | |
1527 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current)); |
1560 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current)); |
1528 | |
1561 | |
1529 | --cctx_count; |
1562 | --cctx_count; |
1530 | coro_destroy (&cctx->cctx); |
1563 | coro_destroy (&cctx->cctx); |
1531 | |
1564 | |
1532 | /* coro_transfer creates new, empty cctx's */ |
1565 | coro_stack_free (&cctx->stack); |
1533 | if (cctx->sptr) |
|
|
1534 | { |
|
|
1535 | #if CORO_USE_VALGRIND |
|
|
1536 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
|
|
1537 | #endif |
|
|
1538 | |
|
|
1539 | #if HAVE_MMAP |
|
|
1540 | if (cctx->flags & CC_MAPPED) |
|
|
1541 | munmap (cctx->sptr, cctx->ssize); |
|
|
1542 | else |
|
|
1543 | #endif |
|
|
1544 | Safefree (cctx->sptr); |
|
|
1545 | } |
|
|
1546 | |
1566 | |
1547 | Safefree (cctx); |
1567 | Safefree (cctx); |
1548 | } |
1568 | } |
1549 | |
1569 | |
1550 | /* wether this cctx should be destructed */ |
1570 | /* wether this cctx should be destructed */ |
… | |
… | |
1569 | } |
1589 | } |
1570 | |
1590 | |
1571 | static void |
1591 | static void |
1572 | cctx_put (coro_cctx *cctx) |
1592 | cctx_put (coro_cctx *cctx) |
1573 | { |
1593 | { |
1574 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); |
1594 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->stack.sptr)); |
1575 | |
1595 | |
1576 | /* free another cctx if overlimit */ |
1596 | /* free another cctx if overlimit */ |
1577 | if (ecb_expect_false (cctx_idle >= cctx_max_idle)) |
1597 | if (ecb_expect_false (cctx_idle >= cctx_max_idle)) |
1578 | { |
1598 | { |
1579 | coro_cctx *first = cctx_first; |
1599 | coro_cctx *first = cctx_first; |
… | |
… | |
1705 | return; |
1725 | return; |
1706 | |
1726 | |
1707 | slf_destroy (aTHX_ coro); |
1727 | slf_destroy (aTHX_ coro); |
1708 | |
1728 | |
1709 | coro->flags |= CF_ZOMBIE; |
1729 | coro->flags |= CF_ZOMBIE; |
1710 | |
1730 | |
1711 | if (coro->flags & CF_READY) |
1731 | if (coro->flags & CF_READY) |
1712 | { |
1732 | { |
1713 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1733 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1714 | /* alternative: look through all ready queues and remove the coro */ |
1734 | /* alternative: look through all ready queues and remove the coro */ |
1715 | --coro_nready; |
1735 | --coro_nready; |
… | |
… | |
1792 | TRANSFER (ta, 1); |
1812 | TRANSFER (ta, 1); |
1793 | } |
1813 | } |
1794 | |
1814 | |
1795 | /** Coro ********************************************************************/ |
1815 | /** Coro ********************************************************************/ |
1796 | |
1816 | |
1797 | ECB_INLINE void |
1817 | ecb_inline void |
1798 | coro_enq (pTHX_ struct coro *coro) |
1818 | coro_enq (pTHX_ struct coro *coro) |
1799 | { |
1819 | { |
1800 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1820 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1801 | |
1821 | |
1802 | SvREFCNT_inc_NN (coro->hv); |
1822 | SvREFCNT_inc_NN (coro->hv); |
… | |
… | |
1804 | coro->next_ready = 0; |
1824 | coro->next_ready = 0; |
1805 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1825 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1806 | ready [1] = coro; |
1826 | ready [1] = coro; |
1807 | } |
1827 | } |
1808 | |
1828 | |
1809 | ECB_INLINE struct coro * |
1829 | ecb_inline struct coro * |
1810 | coro_deq (pTHX) |
1830 | coro_deq (pTHX) |
1811 | { |
1831 | { |
1812 | int prio; |
1832 | int prio; |
1813 | |
1833 | |
1814 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
1834 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1867 | { |
1887 | { |
1868 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1888 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1869 | } |
1889 | } |
1870 | |
1890 | |
1871 | /* expects to own a reference to next->hv */ |
1891 | /* expects to own a reference to next->hv */ |
1872 | ECB_INLINE void |
1892 | ecb_inline void |
1873 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1893 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1874 | { |
1894 | { |
1875 | SV *prev_sv = SvRV (coro_current); |
1895 | SV *prev_sv = SvRV (coro_current); |
1876 | |
1896 | |
1877 | ta->prev = SvSTATE_hv (prev_sv); |
1897 | ta->prev = SvSTATE_hv (prev_sv); |
… | |
… | |
1911 | /* nothing to schedule: call the idle handler */ |
1931 | /* nothing to schedule: call the idle handler */ |
1912 | if (SvROK (sv_idle) |
1932 | if (SvROK (sv_idle) |
1913 | && SvOBJECT (SvRV (sv_idle))) |
1933 | && SvOBJECT (SvRV (sv_idle))) |
1914 | { |
1934 | { |
1915 | if (SvRV (sv_idle) == SvRV (coro_current)) |
1935 | if (SvRV (sv_idle) == SvRV (coro_current)) |
|
|
1936 | { |
|
|
1937 | require_pv ("Carp"); |
|
|
1938 | |
|
|
1939 | { |
|
|
1940 | dSP; |
|
|
1941 | |
|
|
1942 | ENTER; |
|
|
1943 | SAVETMPS; |
|
|
1944 | |
|
|
1945 | PUSHMARK (SP); |
1916 | croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught"); |
1946 | XPUSHs (sv_2mortal (newSVpv ("FATAL: $Coro::idle blocked itself - did you try to block inside an event loop callback? Caught", 0))); |
|
|
1947 | PUTBACK; |
|
|
1948 | call_pv ("Carp::confess", G_VOID | G_DISCARD); |
|
|
1949 | |
|
|
1950 | FREETMPS; |
|
|
1951 | LEAVE; |
|
|
1952 | } |
|
|
1953 | } |
1917 | |
1954 | |
1918 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1955 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1919 | api_ready (aTHX_ SvRV (sv_idle)); |
1956 | api_ready (aTHX_ SvRV (sv_idle)); |
1920 | --coro_nready; |
1957 | --coro_nready; |
1921 | } |
1958 | } |
… | |
… | |
1936 | } |
1973 | } |
1937 | } |
1974 | } |
1938 | } |
1975 | } |
1939 | } |
1976 | } |
1940 | |
1977 | |
1941 | ECB_INLINE void |
1978 | ecb_inline void |
1942 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1979 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1943 | { |
1980 | { |
1944 | api_ready (aTHX_ coro_current); |
1981 | api_ready (aTHX_ coro_current); |
1945 | prepare_schedule (aTHX_ ta); |
1982 | prepare_schedule (aTHX_ ta); |
1946 | } |
1983 | } |
1947 | |
1984 | |
1948 | ECB_INLINE void |
1985 | ecb_inline void |
1949 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1986 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1950 | { |
1987 | { |
1951 | SV *prev = SvRV (coro_current); |
1988 | SV *prev = SvRV (coro_current); |
1952 | |
1989 | |
1953 | if (coro_nready) |
1990 | if (coro_nready) |
… | |
… | |
2118 | { |
2155 | { |
2119 | AV *od = coro->on_destroy; |
2156 | AV *od = coro->on_destroy; |
2120 | |
2157 | |
2121 | if (!od) |
2158 | if (!od) |
2122 | return; |
2159 | return; |
|
|
2160 | |
|
|
2161 | coro->on_destroy = 0; |
|
|
2162 | sv_2mortal ((SV *)od); |
2123 | |
2163 | |
2124 | while (AvFILLp (od) >= 0) |
2164 | while (AvFILLp (od) >= 0) |
2125 | { |
2165 | { |
2126 | SV *cb = sv_2mortal (av_pop (od)); |
2166 | SV *cb = sv_2mortal (av_pop (od)); |
2127 | |
2167 | |
… | |
… | |
2148 | |
2188 | |
2149 | static void |
2189 | static void |
2150 | coro_set_status (pTHX_ struct coro *coro, SV **arg, int items) |
2190 | coro_set_status (pTHX_ struct coro *coro, SV **arg, int items) |
2151 | { |
2191 | { |
2152 | AV *av; |
2192 | AV *av; |
2153 | |
2193 | |
2154 | if (coro->status) |
2194 | if (coro->status) |
2155 | { |
2195 | { |
2156 | av = coro->status; |
2196 | av = coro->status; |
2157 | av_clear (av); |
2197 | av_clear (av); |
2158 | } |
2198 | } |
… | |
… | |
2205 | |
2245 | |
2206 | coro = SvSTATE (arg [0]); |
2246 | coro = SvSTATE (arg [0]); |
2207 | coro_hv = coro->hv; |
2247 | coro_hv = coro->hv; |
2208 | |
2248 | |
2209 | coro_set_status (aTHX_ coro, arg + 1, items - 1); |
2249 | coro_set_status (aTHX_ coro, arg + 1, items - 1); |
2210 | |
2250 | |
2211 | if (ecb_expect_false (coro->flags & CF_NOCANCEL)) |
2251 | if (ecb_expect_false (coro->flags & CF_NOCANCEL)) |
2212 | { |
2252 | { |
2213 | /* coro currently busy cancelling something, so just notify it */ |
2253 | /* coro currently busy cancelling something, so just notify it */ |
2214 | coro->slf_frame.data = (void *)coro; |
2254 | coro->slf_frame.data = (void *)coro; |
2215 | |
2255 | |
… | |
… | |
2222 | slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); |
2262 | slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); |
2223 | } |
2263 | } |
2224 | else |
2264 | else |
2225 | { |
2265 | { |
2226 | struct coro *self = SvSTATE_current; |
2266 | struct coro *self = SvSTATE_current; |
|
|
2267 | |
|
|
2268 | if (!self) |
|
|
2269 | croak ("Coro::cancel called outside of thread content,"); |
2227 | |
2270 | |
2228 | /* otherwise we cancel directly, purely for speed reasons |
2271 | /* otherwise we cancel directly, purely for speed reasons |
2229 | * unfortunately, this requires some magic trickery, as |
2272 | * unfortunately, this requires some magic trickery, as |
2230 | * somebody else could cancel us, so we have to fight the cancellation. |
2273 | * somebody else could cancel us, so we have to fight the cancellation. |
2231 | * this is ugly, and hopefully fully worth the extra speed. |
2274 | * this is ugly, and hopefully fully worth the extra speed. |
… | |
… | |
2394 | |
2437 | |
2395 | static int |
2438 | static int |
2396 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
2439 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
2397 | { |
2440 | { |
2398 | SV *data = (SV *)frame->data; |
2441 | SV *data = (SV *)frame->data; |
2399 | |
2442 | |
2400 | if (CORO_THROW) |
2443 | if (CORO_THROW) |
2401 | return 0; |
2444 | return 0; |
2402 | |
2445 | |
2403 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2446 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2404 | return 1; |
2447 | return 1; |
… | |
… | |
2440 | cb = sv_2mortal (coro->rouse_cb); |
2483 | cb = sv_2mortal (coro->rouse_cb); |
2441 | coro->rouse_cb = 0; |
2484 | coro->rouse_cb = 0; |
2442 | } |
2485 | } |
2443 | |
2486 | |
2444 | if (!SvROK (cb) |
2487 | if (!SvROK (cb) |
2445 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
2488 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
2446 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
2489 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
2447 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
2490 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
2448 | |
2491 | |
2449 | { |
2492 | { |
2450 | CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ |
2493 | CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ |
… | |
… | |
2573 | |
2616 | |
2574 | /* "undo"/cancel a running slf call - used when cancelling a coro, mainly */ |
2617 | /* "undo"/cancel a running slf call - used when cancelling a coro, mainly */ |
2575 | static void |
2618 | static void |
2576 | slf_destroy (pTHX_ struct coro *coro) |
2619 | slf_destroy (pTHX_ struct coro *coro) |
2577 | { |
2620 | { |
2578 | /* this callback is reserved for slf functions needing to do cleanup */ |
2621 | struct CoroSLF frame = coro->slf_frame; |
2579 | if (coro->slf_frame.destroy && coro->slf_frame.prepare && !PL_dirty) |
|
|
2580 | coro->slf_frame.destroy (aTHX_ &coro->slf_frame); |
|
|
2581 | |
2622 | |
2582 | /* |
2623 | /* |
2583 | * The on_destroy above most likely is from an SLF call. |
2624 | * The on_destroy below most likely is from an SLF call. |
2584 | * Since by definition the SLF call will not finish when we destroy |
2625 | * Since by definition the SLF call will not finish when we destroy |
2585 | * the coro, we will have to force-finish it here, otherwise |
2626 | * the coro, we will have to force-finish it here, otherwise |
2586 | * cleanup functions cannot call SLF functions. |
2627 | * cleanup functions cannot call SLF functions. |
2587 | */ |
2628 | */ |
2588 | coro->slf_frame.prepare = 0; |
2629 | coro->slf_frame.prepare = 0; |
|
|
2630 | |
|
|
2631 | /* this callback is reserved for slf functions needing to do cleanup */ |
|
|
2632 | if (frame.destroy && frame.prepare && !PL_dirty) |
|
|
2633 | frame.destroy (aTHX_ &frame); |
2589 | } |
2634 | } |
2590 | |
2635 | |
2591 | /* |
2636 | /* |
2592 | * these not obviously related functions are all rolled into one |
2637 | * these not obviously related functions are all rolled into one |
2593 | * function to increase chances that they all will call transfer with the same |
2638 | * function to increase chances that they all will call transfer with the same |
… | |
… | |
2933 | { |
2978 | { |
2934 | AV *av = (AV *)frame->data; |
2979 | AV *av = (AV *)frame->data; |
2935 | SV *count_sv = AvARRAY (av)[0]; |
2980 | SV *count_sv = AvARRAY (av)[0]; |
2936 | SV *coro_hv = SvRV (coro_current); |
2981 | SV *coro_hv = SvRV (coro_current); |
2937 | |
2982 | |
|
|
2983 | frame->destroy = 0; |
|
|
2984 | |
2938 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
2985 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
2939 | if (CORO_THROW) |
2986 | if (ecb_expect_false (CORO_THROW)) |
|
|
2987 | { |
|
|
2988 | /* we still might be responsible for the semaphore, so wake up others */ |
|
|
2989 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2990 | |
2940 | return 0; |
2991 | return 0; |
|
|
2992 | } |
2941 | else if (SvIVX (count_sv) > 0) |
2993 | else if (SvIVX (count_sv) > 0) |
2942 | { |
2994 | { |
2943 | frame->destroy = 0; |
|
|
2944 | |
|
|
2945 | if (acquire) |
2995 | if (acquire) |
2946 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2996 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2947 | else |
2997 | else |
2948 | coro_semaphore_adjust (aTHX_ av, 0); |
2998 | coro_semaphore_adjust (aTHX_ av, 0); |
2949 | |
2999 | |
… | |
… | |
3171 | /* it quickly returns */ |
3221 | /* it quickly returns */ |
3172 | if (CORO_THROW) |
3222 | if (CORO_THROW) |
3173 | return 0; |
3223 | return 0; |
3174 | |
3224 | |
3175 | /* one element that is an RV? repeat! */ |
3225 | /* one element that is an RV? repeat! */ |
3176 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
3226 | if (AvFILLp (state) == 0 && SvTYPE (AvARRAY (state)[0]) != SVt_PV) |
3177 | return 1; |
3227 | return 1; |
3178 | |
3228 | |
3179 | /* restore status */ |
3229 | /* restore status */ |
3180 | { |
3230 | { |
3181 | SV *data_sv = av_pop (state); |
3231 | SV *data_sv = av_pop (state); |
… | |
… | |
3371 | int count; |
3421 | int count; |
3372 | |
3422 | |
3373 | eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); |
3423 | eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); |
3374 | |
3424 | |
3375 | PUSHMARK (SP); |
3425 | PUSHMARK (SP); |
3376 | #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type)); |
3426 | #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type)); |
3377 | # include "state.h" |
3427 | #include "state.h" |
3378 | #undef VARx |
|
|
3379 | count = call_pv ("Coro::State::_jit", G_ARRAY); |
3428 | count = call_pv ("Coro::State::_jit", G_ARRAY); |
3380 | SPAGAIN; |
3429 | SPAGAIN; |
3381 | |
3430 | |
3382 | save = POPs; save_ptr = SvPVbyte (save, save_len); |
3431 | save = POPs; save_ptr = SvPVbyte (save, save_len); |
3383 | load = POPs; load_ptr = SvPVbyte (load, load_len); |
3432 | load = POPs; load_ptr = SvPVbyte (load, load_len); |
… | |
… | |
3410 | |
3459 | |
3411 | PROTOTYPES: DISABLE |
3460 | PROTOTYPES: DISABLE |
3412 | |
3461 | |
3413 | BOOT: |
3462 | BOOT: |
3414 | { |
3463 | { |
|
|
3464 | #define VARx(name,expr,type) if (sizeof (type) < sizeof (expr)) croak ("FATAL: Coro thread context slot '" # name "' too small for this version of perl."); |
|
|
3465 | #include "state.h" |
3415 | #ifdef USE_ITHREADS |
3466 | #ifdef USE_ITHREADS |
3416 | # if CORO_PTHREAD |
3467 | # if CORO_PTHREAD |
3417 | coro_thx = PERL_GET_CONTEXT; |
3468 | coro_thx = PERL_GET_CONTEXT; |
3418 | # endif |
3469 | # endif |
3419 | #endif |
3470 | #endif |
3420 | BOOT_PAGESIZE; |
|
|
3421 | |
|
|
3422 | /* perl defines these to check for existance first, but why it doesn't */ |
3471 | /* perl defines these to check for existance first, but why it doesn't */ |
3423 | /* just create them one at init time is not clear to me, except for */ |
3472 | /* just create them one at init time is not clear to me, except for */ |
3424 | /* programs trying to delete them, but... */ |
3473 | /* programs trying to delete them, but... */ |
3425 | /* anyway, we declare this as invalid and make sure they are initialised here */ |
3474 | /* anyway, we declare this as invalid and make sure they are initialised here */ |
3426 | DEFSV; |
3475 | DEFSV; |
… | |
… | |
3496 | void |
3545 | void |
3497 | transfer (...) |
3546 | transfer (...) |
3498 | PROTOTYPE: $$ |
3547 | PROTOTYPE: $$ |
3499 | CODE: |
3548 | CODE: |
3500 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3549 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3501 | |
|
|
3502 | void |
|
|
3503 | _exit (int code) |
|
|
3504 | PROTOTYPE: $ |
|
|
3505 | CODE: |
|
|
3506 | _exit (code); |
|
|
3507 | |
3550 | |
3508 | SV * |
3551 | SV * |
3509 | clone (Coro::State coro) |
3552 | clone (Coro::State coro) |
3510 | CODE: |
3553 | CODE: |
3511 | { |
3554 | { |
… | |
… | |
3566 | void |
3609 | void |
3567 | list () |
3610 | list () |
3568 | PROTOTYPE: |
3611 | PROTOTYPE: |
3569 | PPCODE: |
3612 | PPCODE: |
3570 | { |
3613 | { |
3571 | struct coro *coro; |
3614 | struct coro *coro; |
3572 | for (coro = coro_first; coro; coro = coro->next) |
3615 | for (coro = coro_first; coro; coro = coro->next) |
3573 | if (coro->hv) |
3616 | if (coro->hv) |
3574 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
3617 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
3575 | } |
3618 | } |
3576 | |
3619 | |
… | |
… | |
3640 | RETVAL = boolSV (coro->flags & ix); |
3683 | RETVAL = boolSV (coro->flags & ix); |
3641 | OUTPUT: |
3684 | OUTPUT: |
3642 | RETVAL |
3685 | RETVAL |
3643 | |
3686 | |
3644 | void |
3687 | void |
3645 | throw (Coro::State self, SV *exception = &PL_sv_undef) |
3688 | throw (SV *self, SV *exception = &PL_sv_undef) |
3646 | PROTOTYPE: $;$ |
3689 | PROTOTYPE: $;$ |
3647 | CODE: |
3690 | CODE: |
3648 | { |
3691 | { |
|
|
3692 | struct coro *coro = SvSTATE (self); |
3649 | struct coro *current = SvSTATE_current; |
3693 | struct coro *current = SvSTATE_current; |
3650 | SV **exceptionp = self == current ? &CORO_THROW : &self->except; |
3694 | SV **exceptionp = coro == current ? &CORO_THROW : &coro->except; |
3651 | SvREFCNT_dec (*exceptionp); |
3695 | SvREFCNT_dec (*exceptionp); |
3652 | SvGETMAGIC (exception); |
3696 | SvGETMAGIC (exception); |
3653 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
3697 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
|
|
3698 | |
|
|
3699 | api_ready (aTHX_ self); |
3654 | } |
3700 | } |
3655 | |
3701 | |
3656 | void |
3702 | void |
3657 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3703 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3658 | PROTOTYPE: $;$ |
3704 | PROTOTYPE: $;$ |
… | |
… | |
3735 | |
3781 | |
3736 | void |
3782 | void |
3737 | times (Coro::State self) |
3783 | times (Coro::State self) |
3738 | PPCODE: |
3784 | PPCODE: |
3739 | { |
3785 | { |
3740 | struct coro *current = SvSTATE (coro_current); |
3786 | struct coro *current = SvSTATE (coro_current); |
3741 | |
3787 | |
3742 | if (ecb_expect_false (current == self)) |
3788 | if (ecb_expect_false (current == self)) |
3743 | { |
3789 | { |
3744 | coro_times_update (); |
3790 | coro_times_update (); |
3745 | coro_times_add (SvSTATE (coro_current)); |
3791 | coro_times_add (SvSTATE (coro_current)); |
… | |
… | |
3788 | |
3834 | |
3789 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3835 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3790 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3836 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3791 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3837 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3792 | CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ |
3838 | CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ |
3793 | |
3839 | |
3794 | coro_stash = gv_stashpv ("Coro", TRUE); |
3840 | coro_stash = gv_stashpv ("Coro", TRUE); |
3795 | |
3841 | |
3796 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3842 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3797 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3843 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3798 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
3844 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
… | |
… | |
4020 | on_leave = 1 |
4066 | on_leave = 1 |
4021 | PROTOTYPE: & |
4067 | PROTOTYPE: & |
4022 | CODE: |
4068 | CODE: |
4023 | { |
4069 | { |
4024 | struct coro *coro = SvSTATE_current; |
4070 | struct coro *coro = SvSTATE_current; |
4025 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
4071 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
4026 | |
4072 | |
4027 | block = s_get_cv_croak (block); |
4073 | block = s_get_cv_croak (block); |
4028 | |
4074 | |
4029 | if (!*avp) |
4075 | if (!*avp) |
4030 | *avp = newAV (); |
4076 | *avp = newAV (); |
… | |
… | |
4050 | |
4096 | |
4051 | SV * |
4097 | SV * |
4052 | new (SV *klass, SV *count = 0) |
4098 | new (SV *klass, SV *count = 0) |
4053 | CODE: |
4099 | CODE: |
4054 | { |
4100 | { |
4055 | int semcnt = 1; |
4101 | int semcnt = 1; |
4056 | |
4102 | |
4057 | if (count) |
4103 | if (count) |
4058 | { |
4104 | { |
4059 | SvGETMAGIC (count); |
4105 | SvGETMAGIC (count); |
4060 | |
4106 | |
… | |
… | |
4120 | XSRETURN_NO; |
4166 | XSRETURN_NO; |
4121 | } |
4167 | } |
4122 | |
4168 | |
4123 | void |
4169 | void |
4124 | waiters (SV *self) |
4170 | waiters (SV *self) |
4125 | PPCODE: |
4171 | PPCODE: |
4126 | { |
4172 | { |
4127 | AV *av = (AV *)SvRV (self); |
4173 | AV *av = (AV *)SvRV (self); |
4128 | int wcount = AvFILLp (av) + 1 - 1; |
4174 | int wcount = AvFILLp (av) + 1 - 1; |
4129 | |
4175 | |
4130 | if (GIMME_V == G_SCALAR) |
4176 | if (GIMME_V == G_SCALAR) |
… | |
… | |
4139 | } |
4185 | } |
4140 | |
4186 | |
4141 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4187 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4142 | |
4188 | |
4143 | void |
4189 | void |
4144 | _may_delete (SV *sem, int count, int extra_refs) |
4190 | _may_delete (SV *sem, int count, unsigned int extra_refs) |
4145 | PPCODE: |
4191 | PPCODE: |
4146 | { |
4192 | { |
4147 | AV *av = (AV *)SvRV (sem); |
4193 | AV *av = (AV *)SvRV (sem); |
4148 | |
4194 | |
4149 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
4195 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
4150 | && AvFILLp (av) == 0 /* no waiters, just count */ |
4196 | && AvFILLp (av) == 0 /* no waiters, just count */ |
4151 | && SvIV (AvARRAY (av)[0]) == count) |
4197 | && SvIV (AvARRAY (av)[0]) == count) |
4152 | XSRETURN_YES; |
4198 | XSRETURN_YES; |
… | |
… | |
4173 | |
4219 | |
4174 | void |
4220 | void |
4175 | broadcast (SV *self) |
4221 | broadcast (SV *self) |
4176 | CODE: |
4222 | CODE: |
4177 | { |
4223 | { |
4178 | AV *av = (AV *)SvRV (self); |
4224 | AV *av = (AV *)SvRV (self); |
4179 | coro_signal_wake (aTHX_ av, AvFILLp (av)); |
4225 | coro_signal_wake (aTHX_ av, AvFILLp (av)); |
4180 | } |
4226 | } |
4181 | |
4227 | |
4182 | void |
4228 | void |
4183 | send (SV *self) |
4229 | send (SV *self) |
… | |
… | |
4191 | SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ |
4237 | SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ |
4192 | } |
4238 | } |
4193 | |
4239 | |
4194 | IV |
4240 | IV |
4195 | awaited (SV *self) |
4241 | awaited (SV *self) |
4196 | CODE: |
4242 | CODE: |
4197 | RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; |
4243 | RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; |
4198 | OUTPUT: |
4244 | OUTPUT: |
4199 | RETVAL |
4245 | RETVAL |
4200 | |
4246 | |
4201 | |
4247 | |
… | |
… | |
4206 | |
4252 | |
4207 | void |
4253 | void |
4208 | _schedule (...) |
4254 | _schedule (...) |
4209 | CODE: |
4255 | CODE: |
4210 | { |
4256 | { |
4211 | static int incede; |
4257 | static int incede; |
4212 | |
4258 | |
4213 | api_cede_notself (aTHX); |
4259 | api_cede_notself (aTHX); |
4214 | |
4260 | |
4215 | ++incede; |
4261 | ++incede; |
4216 | while (coro_nready >= incede && api_cede (aTHX)) |
4262 | while (coro_nready >= incede && api_cede (aTHX)) |
… | |
… | |
4260 | { |
4306 | { |
4261 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
4307 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
4262 | coro_old_pp_sselect = 0; |
4308 | coro_old_pp_sselect = 0; |
4263 | } |
4309 | } |
4264 | |
4310 | |
|
|
4311 | MODULE = Coro::State PACKAGE = Coro::Util |
|
|
4312 | |
|
|
4313 | void |
|
|
4314 | _exit (int code) |
|
|
4315 | CODE: |
|
|
4316 | _exit (code); |
|
|
4317 | |
|
|
4318 | NV |
|
|
4319 | time () |
|
|
4320 | CODE: |
|
|
4321 | RETVAL = nvtime (aTHX); |
|
|
4322 | OUTPUT: |
|
|
4323 | RETVAL |
|
|
4324 | |
|
|
4325 | NV |
|
|
4326 | gettimeofday () |
|
|
4327 | PPCODE: |
|
|
4328 | { |
|
|
4329 | UV tv [2]; |
|
|
4330 | u2time (aTHX_ tv); |
|
|
4331 | EXTEND (SP, 2); |
|
|
4332 | PUSHs (sv_2mortal (newSVuv (tv [0]))); |
|
|
4333 | PUSHs (sv_2mortal (newSVuv (tv [1]))); |
|
|
4334 | } |
|
|
4335 | |