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; |
… | |
… | |
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 |
46 | |
47 | |
|
|
48 | /* 5.19.something has replaced SVt_BIND by SVt_INVLIST */ |
|
|
49 | /* we just alias it to SVt_IV, as that is sufficient for swap_sv for now */ |
|
|
50 | #if PERL_VERSION_ATLEAST(5,19,0) |
|
|
51 | # define SVt_BIND SVt_IV |
|
|
52 | #endif |
|
|
53 | |
47 | #if defined(_WIN32) |
54 | #if defined(_WIN32) |
48 | # undef HAS_GETTIMEOFDAY |
55 | # undef HAS_GETTIMEOFDAY |
49 | # undef setjmp |
56 | # undef setjmp |
50 | # undef longjmp |
57 | # undef longjmp |
51 | # undef _exit |
58 | # undef _exit |
52 | # define setjmp _setjmp /* deep magic */ |
59 | # define setjmp _setjmp /* deep magic */ |
53 | #else |
60 | #else |
54 | # include <inttypes.h> /* most portable stdint.h */ |
61 | # include <inttypes.h> /* most portable stdint.h */ |
55 | #endif |
62 | #endif |
56 | |
63 | |
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 */ |
64 | /* the maximum number of idle cctx that will be pooled */ |
85 | static int cctx_max_idle = 4; |
65 | static int cctx_max_idle = 4; |
86 | |
66 | |
87 | #if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0) |
67 | #if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0) |
88 | # define HAS_SCOPESTACK_NAME 1 |
68 | # 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 |
69 | #endif |
98 | |
70 | |
99 | /* prefer perl internal functions over our own? */ |
71 | /* prefer perl internal functions over our own? */ |
100 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
72 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
101 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
73 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
… | |
… | |
192 | typedef struct coro_cctx |
164 | typedef struct coro_cctx |
193 | { |
165 | { |
194 | struct coro_cctx *next; |
166 | struct coro_cctx *next; |
195 | |
167 | |
196 | /* the stack */ |
168 | /* the stack */ |
197 | void *sptr; |
169 | struct coro_stack stack; |
198 | size_t ssize; |
|
|
199 | |
170 | |
200 | /* cpu state */ |
171 | /* cpu state */ |
201 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
172 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
202 | #ifndef NDEBUG |
173 | #ifndef NDEBUG |
203 | JMPENV *idle_te; /* same as idle_sp, but for top_env */ |
174 | JMPENV *idle_te; /* same as idle_sp, but for top_env */ |
… | |
… | |
312 | #define coro_nready coroapi.nready |
283 | #define coro_nready coroapi.nready |
313 | |
284 | |
314 | /** JIT *********************************************************************/ |
285 | /** JIT *********************************************************************/ |
315 | |
286 | |
316 | #if CORO_JIT |
287 | #if CORO_JIT |
317 | /* APPLE doesn't have HAVE_MMAP though */ |
288 | /* APPLE doesn't have mmap though */ |
318 | #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) |
289 | #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) |
319 | #ifndef CORO_JIT_TYPE |
290 | #ifndef CORO_JIT_TYPE |
320 | #if __x86_64 && CORO_JIT_UNIXY |
291 | #if ECB_AMD64 && CORO_JIT_UNIXY |
321 | #define CORO_JIT_TYPE "amd64-unix" |
292 | #define CORO_JIT_TYPE "amd64-unix" |
322 | #elif __i386 && CORO_JIT_UNIXY |
293 | #elif __i386 && CORO_JIT_UNIXY |
323 | #define CORO_JIT_TYPE "x86-unix" |
294 | #define CORO_JIT_TYPE "x86-unix" |
324 | #endif |
295 | #endif |
325 | #endif |
296 | #endif |
326 | #endif |
297 | #endif |
327 | |
298 | |
328 | #if !defined(CORO_JIT_TYPE) || !HAVE_MMAP |
299 | #if !defined(CORO_JIT_TYPE) || _POSIX_MEMORY_PROTECTION <= 0 |
329 | #undef CORO_JIT |
300 | #undef CORO_JIT |
330 | #endif |
301 | #endif |
331 | |
302 | |
332 | #if CORO_JIT |
303 | #if CORO_JIT |
333 | typedef void (*load_save_perl_slots_type)(perl_slots *); |
304 | typedef void (*load_save_perl_slots_type)(perl_slots *); |
… | |
… | |
547 | coro_derive_padlist (pTHX_ CV *cv) |
518 | coro_derive_padlist (pTHX_ CV *cv) |
548 | { |
519 | { |
549 | PADLIST *padlist = CvPADLIST (cv); |
520 | PADLIST *padlist = CvPADLIST (cv); |
550 | PADLIST *newpadlist; |
521 | PADLIST *newpadlist; |
551 | PAD *newpad; |
522 | PAD *newpad; |
552 | PADOFFSET const off = PadlistMAX (padlist) + 1; |
523 | PADOFFSET off = PadlistMAX (padlist) + 1; |
553 | |
524 | |
554 | newPADLIST(newpadlist); |
525 | #if NEWPADAPI |
555 | #if !PERL_VERSION_ATLEAST(5,15,3) |
526 | |
556 | /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */ |
527 | /* if we had the original CvDEPTH, we might be able to steal the CvDEPTH+1 entry instead */ |
557 | AvREAL_off (newpadlist); |
528 | /* 20131102194744.GA6705@schmorp.de, 20131102195825.2013.qmail@lists-nntp.develooper.com */ |
558 | #endif |
529 | while (!PadlistARRAY (padlist)[off - 1]) |
|
|
530 | --off; |
|
|
531 | |
|
|
532 | Perl_pad_push (aTHX_ padlist, off); |
|
|
533 | |
|
|
534 | newpad = PadlistARRAY (padlist)[off]; |
|
|
535 | PadlistARRAY (padlist)[off] = 0; |
|
|
536 | |
|
|
537 | #else |
|
|
538 | |
559 | #if PERL_VERSION_ATLEAST (5,10,0) |
539 | #if PERL_VERSION_ATLEAST (5,10,0) |
560 | Perl_pad_push (aTHX_ padlist, off); |
540 | Perl_pad_push (aTHX_ padlist, off); |
561 | #else |
541 | #else |
562 | Perl_pad_push (aTHX_ padlist, off, 1); |
542 | Perl_pad_push (aTHX_ padlist, off, 1); |
563 | #endif |
543 | #endif |
|
|
544 | |
564 | newpad = PadlistARRAY (padlist)[off]; |
545 | newpad = PadlistARRAY (padlist)[off]; |
565 | PadlistMAX (padlist) = off - 1; |
546 | PadlistMAX (padlist) = off - 1; |
|
|
547 | |
|
|
548 | #endif |
|
|
549 | |
|
|
550 | newPADLIST (newpadlist); |
|
|
551 | #if !PERL_VERSION_ATLEAST(5,15,3) |
|
|
552 | /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */ |
|
|
553 | AvREAL_off (newpadlist); |
|
|
554 | #endif |
566 | |
555 | |
567 | /* Already extended to 2 elements by newPADLIST. */ |
556 | /* Already extended to 2 elements by newPADLIST. */ |
568 | PadlistMAX (newpadlist) = 1; |
557 | PadlistMAX (newpadlist) = 1; |
569 | PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist)); |
558 | PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist)); |
570 | PadlistARRAY (newpadlist)[1] = newpad; |
559 | PadlistARRAY (newpadlist)[1] = newpad; |
… | |
… | |
582 | |
571 | |
583 | while (i > 0) /* special-case index 0 */ |
572 | while (i > 0) /* special-case index 0 */ |
584 | { |
573 | { |
585 | /* we try to be extra-careful here */ |
574 | /* we try to be extra-careful here */ |
586 | PAD *pad = PadlistARRAY (padlist)[i--]; |
575 | PAD *pad = PadlistARRAY (padlist)[i--]; |
|
|
576 | |
|
|
577 | if (pad) |
|
|
578 | { |
587 | I32 j = PadMAX (pad); |
579 | I32 j = PadMAX (pad); |
588 | |
580 | |
589 | while (j >= 0) |
581 | while (j >= 0) |
590 | SvREFCNT_dec (PadARRAY (pad)[j--]); |
582 | SvREFCNT_dec (PadARRAY (pad)[j--]); |
591 | |
583 | |
592 | PadMAX (pad) = -1; |
584 | PadMAX (pad) = -1; |
593 | SvREFCNT_dec (pad); |
585 | SvREFCNT_dec (pad); |
|
|
586 | } |
594 | } |
587 | } |
595 | |
588 | |
596 | SvREFCNT_dec (PadlistNAMES (padlist)); |
589 | SvREFCNT_dec (PadlistNAMES (padlist)); |
597 | |
590 | |
598 | #ifdef NEWPADAPI |
591 | #if NEWPADAPI |
599 | Safefree (PadlistARRAY (padlist)); |
592 | Safefree (PadlistARRAY (padlist)); |
600 | Safefree (padlist); |
593 | Safefree (padlist); |
601 | #else |
594 | #else |
602 | AvFILLp (padlist) = -1; |
595 | AvFILLp (padlist) = -1; |
603 | AvREAL_off (padlist); |
596 | AvREAL_off (padlist); |
… | |
… | |
1519 | static coro_cctx * |
1512 | static coro_cctx * |
1520 | cctx_new_empty (void) |
1513 | cctx_new_empty (void) |
1521 | { |
1514 | { |
1522 | coro_cctx *cctx = cctx_new (); |
1515 | coro_cctx *cctx = cctx_new (); |
1523 | |
1516 | |
1524 | cctx->sptr = 0; |
1517 | cctx->stack.sptr = 0; |
1525 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
1518 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
1526 | |
1519 | |
1527 | return cctx; |
1520 | return cctx; |
1528 | } |
1521 | } |
1529 | |
1522 | |
1530 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1523 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1531 | static coro_cctx * |
1524 | static coro_cctx * |
1532 | cctx_new_run (void) |
1525 | cctx_new_run (void) |
1533 | { |
1526 | { |
1534 | coro_cctx *cctx = cctx_new (); |
1527 | coro_cctx *cctx = cctx_new (); |
1535 | void *stack_start; |
|
|
1536 | size_t stack_size; |
|
|
1537 | |
1528 | |
1538 | #if HAVE_MMAP |
1529 | if (!coro_stack_alloc (&cctx->stack, cctx_stacksize)) |
1539 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
|
|
1540 | /* mmap supposedly does allocate-on-write for us */ |
|
|
1541 | cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, -1, 0); |
|
|
1542 | |
|
|
1543 | if (cctx->sptr != (void *)-1) |
|
|
1544 | { |
|
|
1545 | #if CORO_STACKGUARD |
|
|
1546 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
|
|
1547 | #endif |
|
|
1548 | stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE; |
|
|
1549 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
|
|
1550 | cctx->flags |= CC_MAPPED; |
|
|
1551 | } |
1530 | { |
1552 | else |
|
|
1553 | #endif |
|
|
1554 | { |
|
|
1555 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
|
|
1556 | New (0, cctx->sptr, cctx_stacksize, long); |
|
|
1557 | |
|
|
1558 | if (!cctx->sptr) |
|
|
1559 | { |
|
|
1560 | perror ("FATAL: unable to allocate stack for coroutine, exiting."); |
1531 | perror ("FATAL: unable to allocate stack for coroutine, exiting."); |
1561 | _exit (EXIT_FAILURE); |
1532 | _exit (EXIT_FAILURE); |
1562 | } |
|
|
1563 | |
|
|
1564 | stack_start = cctx->sptr; |
|
|
1565 | stack_size = cctx->ssize; |
|
|
1566 | } |
1533 | } |
1567 | |
1534 | |
1568 | #if CORO_USE_VALGRIND |
|
|
1569 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size); |
|
|
1570 | #endif |
|
|
1571 | |
|
|
1572 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
1535 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, cctx->stack.sptr, cctx->stack.ssze); |
1573 | |
1536 | |
1574 | return cctx; |
1537 | return cctx; |
1575 | } |
1538 | } |
1576 | |
1539 | |
1577 | static void |
1540 | static void |
… | |
… | |
1583 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current)); |
1546 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current)); |
1584 | |
1547 | |
1585 | --cctx_count; |
1548 | --cctx_count; |
1586 | coro_destroy (&cctx->cctx); |
1549 | coro_destroy (&cctx->cctx); |
1587 | |
1550 | |
1588 | /* coro_transfer creates new, empty cctx's */ |
1551 | coro_stack_free (&cctx->stack); |
1589 | if (cctx->sptr) |
|
|
1590 | { |
|
|
1591 | #if CORO_USE_VALGRIND |
|
|
1592 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
|
|
1593 | #endif |
|
|
1594 | |
|
|
1595 | #if HAVE_MMAP |
|
|
1596 | if (cctx->flags & CC_MAPPED) |
|
|
1597 | munmap (cctx->sptr, cctx->ssize); |
|
|
1598 | else |
|
|
1599 | #endif |
|
|
1600 | Safefree (cctx->sptr); |
|
|
1601 | } |
|
|
1602 | |
1552 | |
1603 | Safefree (cctx); |
1553 | Safefree (cctx); |
1604 | } |
1554 | } |
1605 | |
1555 | |
1606 | /* wether this cctx should be destructed */ |
1556 | /* wether this cctx should be destructed */ |
… | |
… | |
1625 | } |
1575 | } |
1626 | |
1576 | |
1627 | static void |
1577 | static void |
1628 | cctx_put (coro_cctx *cctx) |
1578 | cctx_put (coro_cctx *cctx) |
1629 | { |
1579 | { |
1630 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); |
1580 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->stack.sptr)); |
1631 | |
1581 | |
1632 | /* free another cctx if overlimit */ |
1582 | /* free another cctx if overlimit */ |
1633 | if (ecb_expect_false (cctx_idle >= cctx_max_idle)) |
1583 | if (ecb_expect_false (cctx_idle >= cctx_max_idle)) |
1634 | { |
1584 | { |
1635 | coro_cctx *first = cctx_first; |
1585 | coro_cctx *first = cctx_first; |
… | |
… | |
1967 | /* nothing to schedule: call the idle handler */ |
1917 | /* nothing to schedule: call the idle handler */ |
1968 | if (SvROK (sv_idle) |
1918 | if (SvROK (sv_idle) |
1969 | && SvOBJECT (SvRV (sv_idle))) |
1919 | && SvOBJECT (SvRV (sv_idle))) |
1970 | { |
1920 | { |
1971 | if (SvRV (sv_idle) == SvRV (coro_current)) |
1921 | if (SvRV (sv_idle) == SvRV (coro_current)) |
|
|
1922 | { |
|
|
1923 | require_pv ("Carp"); |
|
|
1924 | |
|
|
1925 | { |
|
|
1926 | dSP; |
|
|
1927 | |
|
|
1928 | ENTER; |
|
|
1929 | SAVETMPS; |
|
|
1930 | |
|
|
1931 | PUSHMARK (SP); |
1972 | croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught"); |
1932 | XPUSHs (sv_2mortal (newSVpv ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught", 0))); |
|
|
1933 | PUTBACK; |
|
|
1934 | call_pv ("Carp::confess", G_VOID | G_DISCARD); |
|
|
1935 | |
|
|
1936 | FREETMPS; |
|
|
1937 | LEAVE; |
|
|
1938 | } |
|
|
1939 | } |
1973 | |
1940 | |
1974 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1941 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1975 | api_ready (aTHX_ SvRV (sv_idle)); |
1942 | api_ready (aTHX_ SvRV (sv_idle)); |
1976 | --coro_nready; |
1943 | --coro_nready; |
1977 | } |
1944 | } |
… | |
… | |
2175 | AV *od = coro->on_destroy; |
2142 | AV *od = coro->on_destroy; |
2176 | |
2143 | |
2177 | if (!od) |
2144 | if (!od) |
2178 | return; |
2145 | return; |
2179 | |
2146 | |
|
|
2147 | coro->on_destroy = 0; |
|
|
2148 | sv_2mortal ((SV *)od); |
|
|
2149 | |
2180 | while (AvFILLp (od) >= 0) |
2150 | while (AvFILLp (od) >= 0) |
2181 | { |
2151 | { |
2182 | SV *cb = sv_2mortal (av_pop (od)); |
2152 | SV *cb = sv_2mortal (av_pop (od)); |
2183 | |
2153 | |
2184 | /* coro hv's (and only hv's at the moment) are supported as well */ |
2154 | /* coro hv's (and only hv's at the moment) are supported as well */ |
… | |
… | |
2278 | slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); |
2248 | slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); |
2279 | } |
2249 | } |
2280 | else |
2250 | else |
2281 | { |
2251 | { |
2282 | struct coro *self = SvSTATE_current; |
2252 | struct coro *self = SvSTATE_current; |
|
|
2253 | |
|
|
2254 | if (!self) |
|
|
2255 | croak ("Coro::cancel called outside of thread content,"); |
2283 | |
2256 | |
2284 | /* otherwise we cancel directly, purely for speed reasons |
2257 | /* otherwise we cancel directly, purely for speed reasons |
2285 | * unfortunately, this requires some magic trickery, as |
2258 | * unfortunately, this requires some magic trickery, as |
2286 | * somebody else could cancel us, so we have to fight the cancellation. |
2259 | * somebody else could cancel us, so we have to fight the cancellation. |
2287 | * this is ugly, and hopefully fully worth the extra speed. |
2260 | * this is ugly, and hopefully fully worth the extra speed. |
… | |
… | |
2629 | |
2602 | |
2630 | /* "undo"/cancel a running slf call - used when cancelling a coro, mainly */ |
2603 | /* "undo"/cancel a running slf call - used when cancelling a coro, mainly */ |
2631 | static void |
2604 | static void |
2632 | slf_destroy (pTHX_ struct coro *coro) |
2605 | slf_destroy (pTHX_ struct coro *coro) |
2633 | { |
2606 | { |
2634 | /* this callback is reserved for slf functions needing to do cleanup */ |
2607 | struct CoroSLF frame = coro->slf_frame; |
2635 | if (coro->slf_frame.destroy && coro->slf_frame.prepare && !PL_dirty) |
|
|
2636 | coro->slf_frame.destroy (aTHX_ &coro->slf_frame); |
|
|
2637 | |
2608 | |
2638 | /* |
2609 | /* |
2639 | * The on_destroy above most likely is from an SLF call. |
2610 | * The on_destroy below most likely is from an SLF call. |
2640 | * Since by definition the SLF call will not finish when we destroy |
2611 | * Since by definition the SLF call will not finish when we destroy |
2641 | * the coro, we will have to force-finish it here, otherwise |
2612 | * the coro, we will have to force-finish it here, otherwise |
2642 | * cleanup functions cannot call SLF functions. |
2613 | * cleanup functions cannot call SLF functions. |
2643 | */ |
2614 | */ |
2644 | coro->slf_frame.prepare = 0; |
2615 | coro->slf_frame.prepare = 0; |
|
|
2616 | |
|
|
2617 | /* this callback is reserved for slf functions needing to do cleanup */ |
|
|
2618 | if (frame.destroy && frame.prepare && !PL_dirty) |
|
|
2619 | frame.destroy (aTHX_ &frame); |
2645 | } |
2620 | } |
2646 | |
2621 | |
2647 | /* |
2622 | /* |
2648 | * these not obviously related functions are all rolled into one |
2623 | * these not obviously related functions are all rolled into one |
2649 | * function to increase chances that they all will call transfer with the same |
2624 | * function to increase chances that they all will call transfer with the same |
… | |
… | |
2989 | { |
2964 | { |
2990 | AV *av = (AV *)frame->data; |
2965 | AV *av = (AV *)frame->data; |
2991 | SV *count_sv = AvARRAY (av)[0]; |
2966 | SV *count_sv = AvARRAY (av)[0]; |
2992 | SV *coro_hv = SvRV (coro_current); |
2967 | SV *coro_hv = SvRV (coro_current); |
2993 | |
2968 | |
|
|
2969 | frame->destroy = 0; |
|
|
2970 | |
2994 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
2971 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
2995 | if (CORO_THROW) |
2972 | if (ecb_expect_false (CORO_THROW)) |
|
|
2973 | { |
|
|
2974 | /* we still might be responsible for the semaphore, so wake up others */ |
|
|
2975 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2976 | |
2996 | return 0; |
2977 | return 0; |
|
|
2978 | } |
2997 | else if (SvIVX (count_sv) > 0) |
2979 | else if (SvIVX (count_sv) > 0) |
2998 | { |
2980 | { |
2999 | frame->destroy = 0; |
|
|
3000 | |
|
|
3001 | if (acquire) |
2981 | if (acquire) |
3002 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2982 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
3003 | else |
2983 | else |
3004 | coro_semaphore_adjust (aTHX_ av, 0); |
2984 | coro_semaphore_adjust (aTHX_ av, 0); |
3005 | |
2985 | |
… | |
… | |
3227 | /* it quickly returns */ |
3207 | /* it quickly returns */ |
3228 | if (CORO_THROW) |
3208 | if (CORO_THROW) |
3229 | return 0; |
3209 | return 0; |
3230 | |
3210 | |
3231 | /* one element that is an RV? repeat! */ |
3211 | /* one element that is an RV? repeat! */ |
3232 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
3212 | if (AvFILLp (state) == 0 && SvTYPE (AvARRAY (state)[0]) != SVt_PV) |
3233 | return 1; |
3213 | return 1; |
3234 | |
3214 | |
3235 | /* restore status */ |
3215 | /* restore status */ |
3236 | { |
3216 | { |
3237 | SV *data_sv = av_pop (state); |
3217 | SV *data_sv = av_pop (state); |
… | |
… | |
3465 | |
3445 | |
3466 | PROTOTYPES: DISABLE |
3446 | PROTOTYPES: DISABLE |
3467 | |
3447 | |
3468 | BOOT: |
3448 | BOOT: |
3469 | { |
3449 | { |
|
|
3450 | #define VARx(name,expr,type) if (sizeof (type) < sizeof (expr)) croak ("FATAL: Coro thread context slot '" # name "' too small for this version of perl."); |
|
|
3451 | #include "state.h" |
3470 | #ifdef USE_ITHREADS |
3452 | #ifdef USE_ITHREADS |
3471 | # if CORO_PTHREAD |
3453 | # if CORO_PTHREAD |
3472 | coro_thx = PERL_GET_CONTEXT; |
3454 | coro_thx = PERL_GET_CONTEXT; |
3473 | # endif |
3455 | # endif |
3474 | #endif |
3456 | #endif |
3475 | BOOT_PAGESIZE; |
|
|
3476 | |
|
|
3477 | /* perl defines these to check for existance first, but why it doesn't */ |
3457 | /* perl defines these to check for existance first, but why it doesn't */ |
3478 | /* just create them one at init time is not clear to me, except for */ |
3458 | /* just create them one at init time is not clear to me, except for */ |
3479 | /* programs trying to delete them, but... */ |
3459 | /* programs trying to delete them, but... */ |
3480 | /* anyway, we declare this as invalid and make sure they are initialised here */ |
3460 | /* anyway, we declare this as invalid and make sure they are initialised here */ |
3481 | DEFSV; |
3461 | DEFSV; |
… | |
… | |
3551 | void |
3531 | void |
3552 | transfer (...) |
3532 | transfer (...) |
3553 | PROTOTYPE: $$ |
3533 | PROTOTYPE: $$ |
3554 | CODE: |
3534 | CODE: |
3555 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3535 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3556 | |
|
|
3557 | void |
|
|
3558 | _exit (int code) |
|
|
3559 | PROTOTYPE: $ |
|
|
3560 | CODE: |
|
|
3561 | _exit (code); |
|
|
3562 | |
3536 | |
3563 | SV * |
3537 | SV * |
3564 | clone (Coro::State coro) |
3538 | clone (Coro::State coro) |
3565 | CODE: |
3539 | CODE: |
3566 | { |
3540 | { |
… | |
… | |
4318 | { |
4292 | { |
4319 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
4293 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
4320 | coro_old_pp_sselect = 0; |
4294 | coro_old_pp_sselect = 0; |
4321 | } |
4295 | } |
4322 | |
4296 | |
|
|
4297 | MODULE = Coro::State PACKAGE = Coro::Util |
|
|
4298 | |
|
|
4299 | void |
|
|
4300 | _exit (int code) |
|
|
4301 | CODE: |
|
|
4302 | _exit (code); |
|
|
4303 | |
|
|
4304 | NV |
|
|
4305 | time () |
|
|
4306 | CODE: |
|
|
4307 | RETVAL = nvtime (aTHX); |
|
|
4308 | OUTPUT: |
|
|
4309 | RETVAL |
|
|
4310 | |
|
|
4311 | NV |
|
|
4312 | gettimeofday () |
|
|
4313 | PPCODE: |
|
|
4314 | { |
|
|
4315 | UV tv [2]; |
|
|
4316 | u2time (aTHX_ tv); |
|
|
4317 | EXTEND (SP, 2); |
|
|
4318 | PUSHs (sv_2mortal (newSVuv (tv [0]))); |
|
|
4319 | PUSHs (sv_2mortal (newSVuv (tv [1]))); |
|
|
4320 | } |
|
|
4321 | |