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 |
… | |
… | |
14 | #include "perliol.h" |
14 | #include "perliol.h" |
15 | |
15 | |
16 | #include "schmorp.h" |
16 | #include "schmorp.h" |
17 | |
17 | |
18 | #define ECB_NO_THREADS 1 |
18 | #define ECB_NO_THREADS 1 |
|
|
19 | #define ECB_NO_LIBM 1 |
19 | #include "ecb.h" |
20 | #include "ecb.h" |
20 | |
21 | |
21 | #include <stddef.h> |
22 | #include <stddef.h> |
22 | #include <stdio.h> |
23 | #include <stdio.h> |
23 | #include <errno.h> |
24 | #include <errno.h> |
… | |
… | |
26 | #ifndef SVs_PADSTALE |
27 | #ifndef SVs_PADSTALE |
27 | # define SVs_PADSTALE 0 |
28 | # define SVs_PADSTALE 0 |
28 | #endif |
29 | #endif |
29 | |
30 | |
30 | #ifdef PadARRAY |
31 | #ifdef PadARRAY |
31 | # define NEWPADAPI |
32 | # define NEWPADAPI 1 |
32 | # define newPADLIST(var) (Newz (0, var, 1, PADLIST), Newx (PadlistARRAY (var), 2, PAD *)) |
33 | # define newPADLIST(var) (Newz (0, var, 1, PADLIST), Newx (PadlistARRAY (var), 2, PAD *)) |
33 | #else |
34 | #else |
34 | typedef AV PADNAMELIST; |
35 | typedef AV PADNAMELIST; |
35 | # if !PERL_VERSION_ATLEAST(5,8,0) |
36 | # if !PERL_VERSION_ATLEAST(5,8,0) |
36 | typedef AV PADLIST; |
37 | typedef AV PADLIST; |
… | |
… | |
41 | # define PadlistNAMES(pl) (*PadlistARRAY (pl)) |
42 | # define PadlistNAMES(pl) (*PadlistARRAY (pl)) |
42 | # define PadARRAY AvARRAY |
43 | # define PadARRAY AvARRAY |
43 | # define PadMAX AvFILLp |
44 | # define PadMAX AvFILLp |
44 | # define newPADLIST(var) ((var) = newAV (), av_extend (var, 1)) |
45 | # define newPADLIST(var) ((var) = newAV (), av_extend (var, 1)) |
45 | #endif |
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 |
|
|
58 | #endif |
46 | |
59 | |
47 | #if defined(_WIN32) |
60 | #if defined(_WIN32) |
48 | # undef HAS_GETTIMEOFDAY |
61 | # undef HAS_GETTIMEOFDAY |
49 | # undef setjmp |
62 | # undef setjmp |
50 | # undef longjmp |
63 | # undef longjmp |
… | |
… | |
52 | # define setjmp _setjmp /* deep magic */ |
65 | # define setjmp _setjmp /* deep magic */ |
53 | #else |
66 | #else |
54 | # include <inttypes.h> /* most portable stdint.h */ |
67 | # include <inttypes.h> /* most portable stdint.h */ |
55 | #endif |
68 | #endif |
56 | |
69 | |
57 | #if HAVE_MMAP |
|
|
58 | # include <unistd.h> |
|
|
59 | # include <sys/mman.h> |
|
|
60 | # ifndef MAP_ANONYMOUS |
|
|
61 | # ifdef MAP_ANON |
|
|
62 | # define MAP_ANONYMOUS MAP_ANON |
|
|
63 | # else |
|
|
64 | # undef HAVE_MMAP |
|
|
65 | # endif |
|
|
66 | # endif |
|
|
67 | # include <limits.h> |
|
|
68 | # ifndef PAGESIZE |
|
|
69 | # define PAGESIZE pagesize |
|
|
70 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
|
|
71 | static long pagesize; |
|
|
72 | # else |
|
|
73 | # define BOOT_PAGESIZE (void)0 |
|
|
74 | # endif |
|
|
75 | #else |
|
|
76 | # define PAGESIZE 0 |
|
|
77 | # define BOOT_PAGESIZE (void)0 |
|
|
78 | #endif |
|
|
79 | |
|
|
80 | #if CORO_USE_VALGRIND |
|
|
81 | # include <valgrind/valgrind.h> |
|
|
82 | #endif |
|
|
83 | |
|
|
84 | /* the maximum number of idle cctx that will be pooled */ |
70 | /* the maximum number of idle cctx that will be pooled */ |
85 | static int cctx_max_idle = 4; |
71 | static int cctx_max_idle = 4; |
86 | |
72 | |
87 | #if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0) |
73 | #if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0) |
88 | # define HAS_SCOPESTACK_NAME 1 |
74 | # define HAS_SCOPESTACK_NAME 1 |
89 | #endif |
|
|
90 | |
|
|
91 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
|
|
92 | # undef CORO_STACKGUARD |
|
|
93 | #endif |
|
|
94 | |
|
|
95 | #ifndef CORO_STACKGUARD |
|
|
96 | # define CORO_STACKGUARD 0 |
|
|
97 | #endif |
75 | #endif |
98 | |
76 | |
99 | /* prefer perl internal functions over our own? */ |
77 | /* prefer perl internal functions over our own? */ |
100 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
78 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
101 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
79 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
… | |
… | |
192 | typedef struct coro_cctx |
170 | typedef struct coro_cctx |
193 | { |
171 | { |
194 | struct coro_cctx *next; |
172 | struct coro_cctx *next; |
195 | |
173 | |
196 | /* the stack */ |
174 | /* the stack */ |
197 | void *sptr; |
175 | struct coro_stack stack; |
198 | size_t ssize; |
|
|
199 | |
176 | |
200 | /* cpu state */ |
177 | /* cpu state */ |
201 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
178 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
202 | #ifndef NDEBUG |
179 | #ifndef NDEBUG |
203 | JMPENV *idle_te; /* same as idle_sp, but for top_env */ |
180 | JMPENV *idle_te; /* same as idle_sp, but for top_env */ |
… | |
… | |
312 | #define coro_nready coroapi.nready |
289 | #define coro_nready coroapi.nready |
313 | |
290 | |
314 | /** JIT *********************************************************************/ |
291 | /** JIT *********************************************************************/ |
315 | |
292 | |
316 | #if CORO_JIT |
293 | #if CORO_JIT |
317 | /* APPLE doesn't have HAVE_MMAP though */ |
294 | /* APPLE doesn't have mmap though */ |
318 | #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) |
295 | #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) |
319 | #ifndef CORO_JIT_TYPE |
296 | #ifndef CORO_JIT_TYPE |
320 | #if __x86_64 && CORO_JIT_UNIXY |
297 | #if ECB_AMD64 && CORO_JIT_UNIXY |
321 | #define CORO_JIT_TYPE "amd64-unix" |
298 | #define CORO_JIT_TYPE "amd64-unix" |
322 | #elif __i386 && CORO_JIT_UNIXY |
299 | #elif __i386 && CORO_JIT_UNIXY |
323 | #define CORO_JIT_TYPE "x86-unix" |
300 | #define CORO_JIT_TYPE "x86-unix" |
324 | #endif |
301 | #endif |
325 | #endif |
302 | #endif |
326 | #endif |
303 | #endif |
327 | |
304 | |
328 | #if !defined(CORO_JIT_TYPE) || !HAVE_MMAP |
305 | #if !defined(CORO_JIT_TYPE) || _POSIX_MEMORY_PROTECTION <= 0 |
329 | #undef CORO_JIT |
306 | #undef CORO_JIT |
330 | #endif |
307 | #endif |
331 | |
308 | |
332 | #if CORO_JIT |
309 | #if CORO_JIT |
333 | typedef void (*load_save_perl_slots_type)(perl_slots *); |
310 | typedef void (*load_save_perl_slots_type)(perl_slots *); |
… | |
… | |
546 | ecb_inline PADLIST * |
523 | ecb_inline PADLIST * |
547 | coro_derive_padlist (pTHX_ CV *cv) |
524 | coro_derive_padlist (pTHX_ CV *cv) |
548 | { |
525 | { |
549 | PADLIST *padlist = CvPADLIST (cv); |
526 | PADLIST *padlist = CvPADLIST (cv); |
550 | PADLIST *newpadlist; |
527 | PADLIST *newpadlist; |
|
|
528 | PADNAMELIST *padnames; |
551 | PAD *newpad; |
529 | PAD *newpad; |
552 | PADOFFSET const off = PadlistMAX (padlist) + 1; |
530 | PADOFFSET off = PadlistMAX (padlist) + 1; |
553 | |
531 | |
554 | newPADLIST(newpadlist); |
532 | #if NEWPADAPI |
555 | #if !PERL_VERSION_ATLEAST(5,15,3) |
533 | |
556 | /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */ |
534 | /* if we had the original CvDEPTH, we might be able to steal the CvDEPTH+1 entry instead */ |
557 | AvREAL_off (newpadlist); |
535 | /* 20131102194744.GA6705@schmorp.de, 20131102195825.2013.qmail@lists-nntp.develooper.com */ |
558 | #endif |
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 | |
559 | #if PERL_VERSION_ATLEAST (5,10,0) |
546 | #if PERL_VERSION_ATLEAST (5,10,0) |
560 | Perl_pad_push (aTHX_ padlist, off); |
547 | Perl_pad_push (aTHX_ padlist, off); |
561 | #else |
548 | #else |
562 | Perl_pad_push (aTHX_ padlist, off, 1); |
549 | Perl_pad_push (aTHX_ padlist, off, 1); |
563 | #endif |
550 | #endif |
|
|
551 | |
564 | newpad = PadlistARRAY (padlist)[off]; |
552 | newpad = PadlistARRAY (padlist)[off]; |
565 | PadlistMAX (padlist) = off - 1; |
553 | PadlistMAX (padlist) = off - 1; |
566 | |
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. */ |
|
|
560 | AvREAL_off (newpadlist); |
|
|
561 | #endif |
|
|
562 | |
567 | /* Already extended to 2 elements by newPADLIST. */ |
563 | /* Already extended to 2 elements by newPADLIST. */ |
568 | PadlistMAX (newpadlist) = 1; |
564 | PadlistMAX (newpadlist) = 1; |
569 | PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist)); |
565 | |
|
|
566 | padnames = PadlistNAMES (padlist); |
|
|
567 | ++PadnamelistREFCNT (padnames); |
|
|
568 | PadlistNAMES (newpadlist) = padnames; |
|
|
569 | |
570 | PadlistARRAY (newpadlist)[1] = newpad; |
570 | PadlistARRAY (newpadlist)[1] = newpad; |
571 | |
571 | |
572 | return newpadlist; |
572 | return newpadlist; |
573 | } |
573 | } |
574 | |
574 | |
… | |
… | |
582 | |
582 | |
583 | while (i > 0) /* special-case index 0 */ |
583 | while (i > 0) /* special-case index 0 */ |
584 | { |
584 | { |
585 | /* we try to be extra-careful here */ |
585 | /* we try to be extra-careful here */ |
586 | PAD *pad = PadlistARRAY (padlist)[i--]; |
586 | PAD *pad = PadlistARRAY (padlist)[i--]; |
|
|
587 | |
|
|
588 | if (pad) |
|
|
589 | { |
587 | I32 j = PadMAX (pad); |
590 | I32 j = PadMAX (pad); |
588 | |
591 | |
589 | while (j >= 0) |
592 | while (j >= 0) |
590 | SvREFCNT_dec (PadARRAY (pad)[j--]); |
593 | SvREFCNT_dec (PadARRAY (pad)[j--]); |
591 | |
594 | |
592 | PadMAX (pad) = -1; |
595 | PadMAX (pad) = -1; |
593 | SvREFCNT_dec (pad); |
596 | SvREFCNT_dec (pad); |
|
|
597 | } |
594 | } |
598 | } |
595 | |
599 | |
596 | SvREFCNT_dec (PadlistNAMES (padlist)); |
600 | PadnamelistREFCNT_dec (PadlistNAMES (padlist)); |
597 | |
601 | |
598 | #ifdef NEWPADAPI |
602 | #if NEWPADAPI |
599 | Safefree (PadlistARRAY (padlist)); |
603 | Safefree (PadlistARRAY (padlist)); |
600 | Safefree (padlist); |
604 | Safefree (padlist); |
601 | #else |
605 | #else |
602 | AvFILLp (padlist) = -1; |
606 | AvFILLp (padlist) = -1; |
603 | AvREAL_off (padlist); |
607 | AvREAL_off (padlist); |
… | |
… | |
1145 | |
1149 | |
1146 | GvSV (PL_defgv) = newSV (0); |
1150 | GvSV (PL_defgv) = newSV (0); |
1147 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1151 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1148 | GvSV (PL_errgv) = newSV (0); |
1152 | GvSV (PL_errgv) = newSV (0); |
1149 | 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); |
1150 | 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 |
1151 | PL_rs = newSVsv (GvSV (irsgv)); |
1158 | PL_rs = newSVsv (GvSV (irsgv)); |
1152 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
1159 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
1153 | |
1160 | |
1154 | { |
1161 | { |
1155 | dSP; |
1162 | dSP; |
… | |
… | |
1337 | |
1344 | |
1338 | if (PL_curcop != &PL_compiling) |
1345 | if (PL_curcop != &PL_compiling) |
1339 | { |
1346 | { |
1340 | SV **cb; |
1347 | SV **cb; |
1341 | |
1348 | |
1342 | if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB) |
1349 | if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB && cxstack_ix >= 0) |
1343 | { |
1350 | { |
1344 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1351 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1345 | |
1352 | |
1346 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1353 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1347 | { |
1354 | { |
… | |
… | |
1519 | static coro_cctx * |
1526 | static coro_cctx * |
1520 | cctx_new_empty (void) |
1527 | cctx_new_empty (void) |
1521 | { |
1528 | { |
1522 | coro_cctx *cctx = cctx_new (); |
1529 | coro_cctx *cctx = cctx_new (); |
1523 | |
1530 | |
1524 | cctx->sptr = 0; |
1531 | cctx->stack.sptr = 0; |
1525 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
1532 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
1526 | |
1533 | |
1527 | return cctx; |
1534 | return cctx; |
1528 | } |
1535 | } |
1529 | |
1536 | |
1530 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1537 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1531 | static coro_cctx * |
1538 | static coro_cctx * |
1532 | cctx_new_run (void) |
1539 | cctx_new_run (void) |
1533 | { |
1540 | { |
1534 | coro_cctx *cctx = cctx_new (); |
1541 | coro_cctx *cctx = cctx_new (); |
1535 | void *stack_start; |
|
|
1536 | size_t stack_size; |
|
|
1537 | |
1542 | |
1538 | #if CORO_FIBER |
1543 | if (!coro_stack_alloc (&cctx->stack, cctx_stacksize)) |
1539 | |
|
|
1540 | cctx->ssize = cctx_stacksize * sizeof (long); |
|
|
1541 | cctx->sptr = 0; |
|
|
1542 | |
|
|
1543 | #else |
|
|
1544 | |
|
|
1545 | #if HAVE_MMAP |
|
|
1546 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
|
|
1547 | /* mmap supposedly does allocate-on-write for us */ |
|
|
1548 | cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, -1, 0); |
|
|
1549 | |
|
|
1550 | if (cctx->sptr != (void *)-1) |
|
|
1551 | { |
1544 | { |
1552 | #if CORO_STACKGUARD |
|
|
1553 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
|
|
1554 | #endif |
|
|
1555 | stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE; |
|
|
1556 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
|
|
1557 | cctx->flags |= CC_MAPPED; |
|
|
1558 | } |
|
|
1559 | else |
|
|
1560 | #endif |
|
|
1561 | { |
|
|
1562 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
|
|
1563 | New (0, cctx->sptr, cctx_stacksize, long); |
|
|
1564 | |
|
|
1565 | if (!cctx->sptr) |
|
|
1566 | { |
|
|
1567 | perror ("FATAL: unable to allocate stack for coroutine, exiting."); |
1545 | perror ("FATAL: unable to allocate stack for coroutine, exiting."); |
1568 | _exit (EXIT_FAILURE); |
1546 | _exit (EXIT_FAILURE); |
1569 | } |
|
|
1570 | |
|
|
1571 | stack_start = cctx->sptr; |
|
|
1572 | stack_size = cctx->ssize; |
|
|
1573 | } |
1547 | } |
1574 | #endif |
|
|
1575 | |
1548 | |
1576 | #if CORO_USE_VALGRIND |
|
|
1577 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size); |
|
|
1578 | #endif |
|
|
1579 | |
|
|
1580 | 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); |
1581 | |
1550 | |
1582 | return cctx; |
1551 | return cctx; |
1583 | } |
1552 | } |
1584 | |
1553 | |
1585 | static void |
1554 | static void |
… | |
… | |
1591 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current)); |
1560 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current)); |
1592 | |
1561 | |
1593 | --cctx_count; |
1562 | --cctx_count; |
1594 | coro_destroy (&cctx->cctx); |
1563 | coro_destroy (&cctx->cctx); |
1595 | |
1564 | |
1596 | /* coro_transfer creates new, empty cctx's */ |
1565 | coro_stack_free (&cctx->stack); |
1597 | if (cctx->sptr) |
|
|
1598 | { |
|
|
1599 | #if CORO_USE_VALGRIND |
|
|
1600 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
|
|
1601 | #endif |
|
|
1602 | |
|
|
1603 | #if HAVE_MMAP |
|
|
1604 | if (cctx->flags & CC_MAPPED) |
|
|
1605 | munmap (cctx->sptr, cctx->ssize); |
|
|
1606 | else |
|
|
1607 | #endif |
|
|
1608 | Safefree (cctx->sptr); |
|
|
1609 | } |
|
|
1610 | |
1566 | |
1611 | Safefree (cctx); |
1567 | Safefree (cctx); |
1612 | } |
1568 | } |
1613 | |
1569 | |
1614 | /* wether this cctx should be destructed */ |
1570 | /* wether this cctx should be destructed */ |
… | |
… | |
1633 | } |
1589 | } |
1634 | |
1590 | |
1635 | static void |
1591 | static void |
1636 | cctx_put (coro_cctx *cctx) |
1592 | cctx_put (coro_cctx *cctx) |
1637 | { |
1593 | { |
1638 | 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)); |
1639 | |
1595 | |
1640 | /* free another cctx if overlimit */ |
1596 | /* free another cctx if overlimit */ |
1641 | if (ecb_expect_false (cctx_idle >= cctx_max_idle)) |
1597 | if (ecb_expect_false (cctx_idle >= cctx_max_idle)) |
1642 | { |
1598 | { |
1643 | coro_cctx *first = cctx_first; |
1599 | coro_cctx *first = cctx_first; |
… | |
… | |
1975 | /* nothing to schedule: call the idle handler */ |
1931 | /* nothing to schedule: call the idle handler */ |
1976 | if (SvROK (sv_idle) |
1932 | if (SvROK (sv_idle) |
1977 | && SvOBJECT (SvRV (sv_idle))) |
1933 | && SvOBJECT (SvRV (sv_idle))) |
1978 | { |
1934 | { |
1979 | 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); |
1980 | 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 | } |
1981 | |
1954 | |
1982 | ++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 */ |
1983 | api_ready (aTHX_ SvRV (sv_idle)); |
1956 | api_ready (aTHX_ SvRV (sv_idle)); |
1984 | --coro_nready; |
1957 | --coro_nready; |
1985 | } |
1958 | } |
… | |
… | |
2183 | AV *od = coro->on_destroy; |
2156 | AV *od = coro->on_destroy; |
2184 | |
2157 | |
2185 | if (!od) |
2158 | if (!od) |
2186 | return; |
2159 | return; |
2187 | |
2160 | |
|
|
2161 | coro->on_destroy = 0; |
|
|
2162 | sv_2mortal ((SV *)od); |
|
|
2163 | |
2188 | while (AvFILLp (od) >= 0) |
2164 | while (AvFILLp (od) >= 0) |
2189 | { |
2165 | { |
2190 | SV *cb = sv_2mortal (av_pop (od)); |
2166 | SV *cb = sv_2mortal (av_pop (od)); |
2191 | |
2167 | |
2192 | /* coro hv's (and only hv's at the moment) are supported as well */ |
2168 | /* coro hv's (and only hv's at the moment) are supported as well */ |
… | |
… | |
2286 | slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); |
2262 | slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); |
2287 | } |
2263 | } |
2288 | else |
2264 | else |
2289 | { |
2265 | { |
2290 | struct coro *self = SvSTATE_current; |
2266 | struct coro *self = SvSTATE_current; |
|
|
2267 | |
|
|
2268 | if (!self) |
|
|
2269 | croak ("Coro::cancel called outside of thread content,"); |
2291 | |
2270 | |
2292 | /* otherwise we cancel directly, purely for speed reasons |
2271 | /* otherwise we cancel directly, purely for speed reasons |
2293 | * unfortunately, this requires some magic trickery, as |
2272 | * unfortunately, this requires some magic trickery, as |
2294 | * 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. |
2295 | * this is ugly, and hopefully fully worth the extra speed. |
2274 | * this is ugly, and hopefully fully worth the extra speed. |
… | |
… | |
2637 | |
2616 | |
2638 | /* "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 */ |
2639 | static void |
2618 | static void |
2640 | slf_destroy (pTHX_ struct coro *coro) |
2619 | slf_destroy (pTHX_ struct coro *coro) |
2641 | { |
2620 | { |
2642 | /* this callback is reserved for slf functions needing to do cleanup */ |
2621 | struct CoroSLF frame = coro->slf_frame; |
2643 | if (coro->slf_frame.destroy && coro->slf_frame.prepare && !PL_dirty) |
|
|
2644 | coro->slf_frame.destroy (aTHX_ &coro->slf_frame); |
|
|
2645 | |
2622 | |
2646 | /* |
2623 | /* |
2647 | * The on_destroy above most likely is from an SLF call. |
2624 | * The on_destroy below most likely is from an SLF call. |
2648 | * 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 |
2649 | * the coro, we will have to force-finish it here, otherwise |
2626 | * the coro, we will have to force-finish it here, otherwise |
2650 | * cleanup functions cannot call SLF functions. |
2627 | * cleanup functions cannot call SLF functions. |
2651 | */ |
2628 | */ |
2652 | 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); |
2653 | } |
2634 | } |
2654 | |
2635 | |
2655 | /* |
2636 | /* |
2656 | * these not obviously related functions are all rolled into one |
2637 | * these not obviously related functions are all rolled into one |
2657 | * 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 |
… | |
… | |
2997 | { |
2978 | { |
2998 | AV *av = (AV *)frame->data; |
2979 | AV *av = (AV *)frame->data; |
2999 | SV *count_sv = AvARRAY (av)[0]; |
2980 | SV *count_sv = AvARRAY (av)[0]; |
3000 | SV *coro_hv = SvRV (coro_current); |
2981 | SV *coro_hv = SvRV (coro_current); |
3001 | |
2982 | |
|
|
2983 | frame->destroy = 0; |
|
|
2984 | |
3002 | /* 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 */ |
3003 | 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 | |
3004 | return 0; |
2991 | return 0; |
|
|
2992 | } |
3005 | else if (SvIVX (count_sv) > 0) |
2993 | else if (SvIVX (count_sv) > 0) |
3006 | { |
2994 | { |
3007 | frame->destroy = 0; |
|
|
3008 | |
|
|
3009 | if (acquire) |
2995 | if (acquire) |
3010 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2996 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
3011 | else |
2997 | else |
3012 | coro_semaphore_adjust (aTHX_ av, 0); |
2998 | coro_semaphore_adjust (aTHX_ av, 0); |
3013 | |
2999 | |
… | |
… | |
3235 | /* it quickly returns */ |
3221 | /* it quickly returns */ |
3236 | if (CORO_THROW) |
3222 | if (CORO_THROW) |
3237 | return 0; |
3223 | return 0; |
3238 | |
3224 | |
3239 | /* one element that is an RV? repeat! */ |
3225 | /* one element that is an RV? repeat! */ |
3240 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
3226 | if (AvFILLp (state) == 0 && SvTYPE (AvARRAY (state)[0]) != SVt_PV) |
3241 | return 1; |
3227 | return 1; |
3242 | |
3228 | |
3243 | /* restore status */ |
3229 | /* restore status */ |
3244 | { |
3230 | { |
3245 | SV *data_sv = av_pop (state); |
3231 | SV *data_sv = av_pop (state); |
… | |
… | |
3473 | |
3459 | |
3474 | PROTOTYPES: DISABLE |
3460 | PROTOTYPES: DISABLE |
3475 | |
3461 | |
3476 | BOOT: |
3462 | BOOT: |
3477 | { |
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" |
3478 | #ifdef USE_ITHREADS |
3466 | #ifdef USE_ITHREADS |
3479 | # if CORO_PTHREAD |
3467 | # if CORO_PTHREAD |
3480 | coro_thx = PERL_GET_CONTEXT; |
3468 | coro_thx = PERL_GET_CONTEXT; |
3481 | # endif |
3469 | # endif |
3482 | #endif |
3470 | #endif |
3483 | BOOT_PAGESIZE; |
|
|
3484 | |
|
|
3485 | /* 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 */ |
3486 | /* 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 */ |
3487 | /* programs trying to delete them, but... */ |
3473 | /* programs trying to delete them, but... */ |
3488 | /* 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 */ |
3489 | DEFSV; |
3475 | DEFSV; |
… | |
… | |
3559 | void |
3545 | void |
3560 | transfer (...) |
3546 | transfer (...) |
3561 | PROTOTYPE: $$ |
3547 | PROTOTYPE: $$ |
3562 | CODE: |
3548 | CODE: |
3563 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3549 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3564 | |
|
|
3565 | void |
|
|
3566 | _exit (int code) |
|
|
3567 | PROTOTYPE: $ |
|
|
3568 | CODE: |
|
|
3569 | _exit (code); |
|
|
3570 | |
3550 | |
3571 | SV * |
3551 | SV * |
3572 | clone (Coro::State coro) |
3552 | clone (Coro::State coro) |
3573 | CODE: |
3553 | CODE: |
3574 | { |
3554 | { |
… | |
… | |
4326 | { |
4306 | { |
4327 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
4307 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
4328 | coro_old_pp_sselect = 0; |
4308 | coro_old_pp_sselect = 0; |
4329 | } |
4309 | } |
4330 | |
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 | |