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 |
… | |
… | |
52 | # define setjmp _setjmp /* deep magic */ |
52 | # define setjmp _setjmp /* deep magic */ |
53 | #else |
53 | #else |
54 | # include <inttypes.h> /* most portable stdint.h */ |
54 | # include <inttypes.h> /* most portable stdint.h */ |
55 | #endif |
55 | #endif |
56 | |
56 | |
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 */ |
57 | /* the maximum number of idle cctx that will be pooled */ |
85 | static int cctx_max_idle = 4; |
58 | static int cctx_max_idle = 4; |
86 | |
59 | |
87 | #if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0) |
60 | #if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0) |
88 | # define HAS_SCOPESTACK_NAME 1 |
61 | # 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 |
62 | #endif |
98 | |
63 | |
99 | /* prefer perl internal functions over our own? */ |
64 | /* prefer perl internal functions over our own? */ |
100 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
65 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
101 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
66 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
… | |
… | |
192 | typedef struct coro_cctx |
157 | typedef struct coro_cctx |
193 | { |
158 | { |
194 | struct coro_cctx *next; |
159 | struct coro_cctx *next; |
195 | |
160 | |
196 | /* the stack */ |
161 | /* the stack */ |
197 | void *sptr; |
162 | struct coro_stack stack; |
198 | size_t ssize; |
|
|
199 | |
163 | |
200 | /* cpu state */ |
164 | /* cpu state */ |
201 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
165 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
202 | #ifndef NDEBUG |
166 | #ifndef NDEBUG |
203 | JMPENV *idle_te; /* same as idle_sp, but for top_env */ |
167 | JMPENV *idle_te; /* same as idle_sp, but for top_env */ |
… | |
… | |
312 | #define coro_nready coroapi.nready |
276 | #define coro_nready coroapi.nready |
313 | |
277 | |
314 | /** JIT *********************************************************************/ |
278 | /** JIT *********************************************************************/ |
315 | |
279 | |
316 | #if CORO_JIT |
280 | #if CORO_JIT |
317 | /* APPLE doesn't have HAVE_MMAP though */ |
281 | /* APPLE doesn't have mmap though */ |
318 | #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) |
282 | #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) |
319 | #ifndef CORO_JIT_TYPE |
283 | #ifndef CORO_JIT_TYPE |
320 | #if __x86_64 && CORO_JIT_UNIXY |
284 | #if ECB_AMD64 && CORO_JIT_UNIXY |
321 | #define CORO_JIT_TYPE "amd64-unix" |
285 | #define CORO_JIT_TYPE "amd64-unix" |
322 | #elif __i386 && CORO_JIT_UNIXY |
286 | #elif __i386 && CORO_JIT_UNIXY |
323 | #define CORO_JIT_TYPE "x86-unix" |
287 | #define CORO_JIT_TYPE "x86-unix" |
324 | #endif |
288 | #endif |
325 | #endif |
289 | #endif |
326 | #endif |
290 | #endif |
327 | |
291 | |
328 | #if !defined(CORO_JIT_TYPE) || !HAVE_MMAP |
292 | #if !defined(CORO_JIT_TYPE) || _POSIX_MEMORY_PROTECTION <= 0 |
329 | #undef CORO_JIT |
293 | #undef CORO_JIT |
330 | #endif |
294 | #endif |
331 | |
295 | |
332 | #if CORO_JIT |
296 | #if CORO_JIT |
333 | typedef void (*load_save_perl_slots_type)(perl_slots *); |
297 | typedef void (*load_save_perl_slots_type)(perl_slots *); |
… | |
… | |
1519 | static coro_cctx * |
1483 | static coro_cctx * |
1520 | cctx_new_empty (void) |
1484 | cctx_new_empty (void) |
1521 | { |
1485 | { |
1522 | coro_cctx *cctx = cctx_new (); |
1486 | coro_cctx *cctx = cctx_new (); |
1523 | |
1487 | |
1524 | cctx->sptr = 0; |
1488 | cctx->stack.sptr = 0; |
1525 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
1489 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
1526 | |
1490 | |
1527 | return cctx; |
1491 | return cctx; |
1528 | } |
1492 | } |
1529 | |
1493 | |
1530 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1494 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1531 | static coro_cctx * |
1495 | static coro_cctx * |
1532 | cctx_new_run (void) |
1496 | cctx_new_run (void) |
1533 | { |
1497 | { |
1534 | coro_cctx *cctx = cctx_new (); |
1498 | coro_cctx *cctx = cctx_new (); |
1535 | void *stack_start; |
|
|
1536 | size_t stack_size; |
|
|
1537 | |
1499 | |
1538 | #if HAVE_MMAP |
1500 | 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 | } |
1501 | { |
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."); |
1502 | perror ("FATAL: unable to allocate stack for coroutine, exiting."); |
1561 | _exit (EXIT_FAILURE); |
1503 | _exit (EXIT_FAILURE); |
1562 | } |
|
|
1563 | |
|
|
1564 | stack_start = cctx->sptr; |
|
|
1565 | stack_size = cctx->ssize; |
|
|
1566 | } |
1504 | } |
1567 | |
1505 | |
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); |
1506 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, cctx->stack.sptr, cctx->stack.ssze); |
1573 | |
1507 | |
1574 | return cctx; |
1508 | return cctx; |
1575 | } |
1509 | } |
1576 | |
1510 | |
1577 | static void |
1511 | static void |
… | |
… | |
1583 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current)); |
1517 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current)); |
1584 | |
1518 | |
1585 | --cctx_count; |
1519 | --cctx_count; |
1586 | coro_destroy (&cctx->cctx); |
1520 | coro_destroy (&cctx->cctx); |
1587 | |
1521 | |
1588 | /* coro_transfer creates new, empty cctx's */ |
1522 | 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 | |
1523 | |
1603 | Safefree (cctx); |
1524 | Safefree (cctx); |
1604 | } |
1525 | } |
1605 | |
1526 | |
1606 | /* wether this cctx should be destructed */ |
1527 | /* wether this cctx should be destructed */ |
… | |
… | |
1625 | } |
1546 | } |
1626 | |
1547 | |
1627 | static void |
1548 | static void |
1628 | cctx_put (coro_cctx *cctx) |
1549 | cctx_put (coro_cctx *cctx) |
1629 | { |
1550 | { |
1630 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); |
1551 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->stack.sptr)); |
1631 | |
1552 | |
1632 | /* free another cctx if overlimit */ |
1553 | /* free another cctx if overlimit */ |
1633 | if (ecb_expect_false (cctx_idle >= cctx_max_idle)) |
1554 | if (ecb_expect_false (cctx_idle >= cctx_max_idle)) |
1634 | { |
1555 | { |
1635 | coro_cctx *first = cctx_first; |
1556 | coro_cctx *first = cctx_first; |
… | |
… | |
2174 | { |
2095 | { |
2175 | AV *od = coro->on_destroy; |
2096 | AV *od = coro->on_destroy; |
2176 | |
2097 | |
2177 | if (!od) |
2098 | if (!od) |
2178 | return; |
2099 | return; |
|
|
2100 | |
|
|
2101 | coro->on_destroy = 0; |
|
|
2102 | sv_2mortal ((SV *)od); |
2179 | |
2103 | |
2180 | while (AvFILLp (od) >= 0) |
2104 | while (AvFILLp (od) >= 0) |
2181 | { |
2105 | { |
2182 | SV *cb = sv_2mortal (av_pop (od)); |
2106 | SV *cb = sv_2mortal (av_pop (od)); |
2183 | |
2107 | |
… | |
… | |
3470 | #ifdef USE_ITHREADS |
3394 | #ifdef USE_ITHREADS |
3471 | # if CORO_PTHREAD |
3395 | # if CORO_PTHREAD |
3472 | coro_thx = PERL_GET_CONTEXT; |
3396 | coro_thx = PERL_GET_CONTEXT; |
3473 | # endif |
3397 | # endif |
3474 | #endif |
3398 | #endif |
3475 | BOOT_PAGESIZE; |
|
|
3476 | |
|
|
3477 | /* perl defines these to check for existance first, but why it doesn't */ |
3399 | /* 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 */ |
3400 | /* just create them one at init time is not clear to me, except for */ |
3479 | /* programs trying to delete them, but... */ |
3401 | /* programs trying to delete them, but... */ |
3480 | /* anyway, we declare this as invalid and make sure they are initialised here */ |
3402 | /* anyway, we declare this as invalid and make sure they are initialised here */ |
3481 | DEFSV; |
3403 | DEFSV; |
… | |
… | |
3551 | void |
3473 | void |
3552 | transfer (...) |
3474 | transfer (...) |
3553 | PROTOTYPE: $$ |
3475 | PROTOTYPE: $$ |
3554 | CODE: |
3476 | CODE: |
3555 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3477 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3556 | |
|
|
3557 | void |
|
|
3558 | _exit (int code) |
|
|
3559 | PROTOTYPE: $ |
|
|
3560 | CODE: |
|
|
3561 | _exit (code); |
|
|
3562 | |
3478 | |
3563 | SV * |
3479 | SV * |
3564 | clone (Coro::State coro) |
3480 | clone (Coro::State coro) |
3565 | CODE: |
3481 | CODE: |
3566 | { |
3482 | { |
… | |
… | |
3621 | void |
3537 | void |
3622 | list () |
3538 | list () |
3623 | PROTOTYPE: |
3539 | PROTOTYPE: |
3624 | PPCODE: |
3540 | PPCODE: |
3625 | { |
3541 | { |
3626 | struct coro *coro; |
3542 | struct coro *coro; |
3627 | for (coro = coro_first; coro; coro = coro->next) |
3543 | for (coro = coro_first; coro; coro = coro->next) |
3628 | if (coro->hv) |
3544 | if (coro->hv) |
3629 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
3545 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
3630 | } |
3546 | } |
3631 | |
3547 | |
… | |
… | |
3793 | |
3709 | |
3794 | void |
3710 | void |
3795 | times (Coro::State self) |
3711 | times (Coro::State self) |
3796 | PPCODE: |
3712 | PPCODE: |
3797 | { |
3713 | { |
3798 | struct coro *current = SvSTATE (coro_current); |
3714 | struct coro *current = SvSTATE (coro_current); |
3799 | |
3715 | |
3800 | if (ecb_expect_false (current == self)) |
3716 | if (ecb_expect_false (current == self)) |
3801 | { |
3717 | { |
3802 | coro_times_update (); |
3718 | coro_times_update (); |
3803 | coro_times_add (SvSTATE (coro_current)); |
3719 | coro_times_add (SvSTATE (coro_current)); |
… | |
… | |
4078 | on_leave = 1 |
3994 | on_leave = 1 |
4079 | PROTOTYPE: & |
3995 | PROTOTYPE: & |
4080 | CODE: |
3996 | CODE: |
4081 | { |
3997 | { |
4082 | struct coro *coro = SvSTATE_current; |
3998 | struct coro *coro = SvSTATE_current; |
4083 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
3999 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
4084 | |
4000 | |
4085 | block = s_get_cv_croak (block); |
4001 | block = s_get_cv_croak (block); |
4086 | |
4002 | |
4087 | if (!*avp) |
4003 | if (!*avp) |
4088 | *avp = newAV (); |
4004 | *avp = newAV (); |
… | |
… | |
4108 | |
4024 | |
4109 | SV * |
4025 | SV * |
4110 | new (SV *klass, SV *count = 0) |
4026 | new (SV *klass, SV *count = 0) |
4111 | CODE: |
4027 | CODE: |
4112 | { |
4028 | { |
4113 | int semcnt = 1; |
4029 | int semcnt = 1; |
4114 | |
4030 | |
4115 | if (count) |
4031 | if (count) |
4116 | { |
4032 | { |
4117 | SvGETMAGIC (count); |
4033 | SvGETMAGIC (count); |
4118 | |
4034 | |
… | |
… | |
4178 | XSRETURN_NO; |
4094 | XSRETURN_NO; |
4179 | } |
4095 | } |
4180 | |
4096 | |
4181 | void |
4097 | void |
4182 | waiters (SV *self) |
4098 | waiters (SV *self) |
4183 | PPCODE: |
4099 | PPCODE: |
4184 | { |
4100 | { |
4185 | AV *av = (AV *)SvRV (self); |
4101 | AV *av = (AV *)SvRV (self); |
4186 | int wcount = AvFILLp (av) + 1 - 1; |
4102 | int wcount = AvFILLp (av) + 1 - 1; |
4187 | |
4103 | |
4188 | if (GIMME_V == G_SCALAR) |
4104 | if (GIMME_V == G_SCALAR) |
… | |
… | |
4200 | |
4116 | |
4201 | void |
4117 | void |
4202 | _may_delete (SV *sem, int count, unsigned int extra_refs) |
4118 | _may_delete (SV *sem, int count, unsigned int extra_refs) |
4203 | PPCODE: |
4119 | PPCODE: |
4204 | { |
4120 | { |
4205 | AV *av = (AV *)SvRV (sem); |
4121 | AV *av = (AV *)SvRV (sem); |
4206 | |
4122 | |
4207 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
4123 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
4208 | && AvFILLp (av) == 0 /* no waiters, just count */ |
4124 | && AvFILLp (av) == 0 /* no waiters, just count */ |
4209 | && SvIV (AvARRAY (av)[0]) == count) |
4125 | && SvIV (AvARRAY (av)[0]) == count) |
4210 | XSRETURN_YES; |
4126 | XSRETURN_YES; |
… | |
… | |
4231 | |
4147 | |
4232 | void |
4148 | void |
4233 | broadcast (SV *self) |
4149 | broadcast (SV *self) |
4234 | CODE: |
4150 | CODE: |
4235 | { |
4151 | { |
4236 | AV *av = (AV *)SvRV (self); |
4152 | AV *av = (AV *)SvRV (self); |
4237 | coro_signal_wake (aTHX_ av, AvFILLp (av)); |
4153 | coro_signal_wake (aTHX_ av, AvFILLp (av)); |
4238 | } |
4154 | } |
4239 | |
4155 | |
4240 | void |
4156 | void |
4241 | send (SV *self) |
4157 | send (SV *self) |
… | |
… | |
4249 | SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ |
4165 | SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ |
4250 | } |
4166 | } |
4251 | |
4167 | |
4252 | IV |
4168 | IV |
4253 | awaited (SV *self) |
4169 | awaited (SV *self) |
4254 | CODE: |
4170 | CODE: |
4255 | RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; |
4171 | RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; |
4256 | OUTPUT: |
4172 | OUTPUT: |
4257 | RETVAL |
4173 | RETVAL |
4258 | |
4174 | |
4259 | |
4175 | |
… | |
… | |
4264 | |
4180 | |
4265 | void |
4181 | void |
4266 | _schedule (...) |
4182 | _schedule (...) |
4267 | CODE: |
4183 | CODE: |
4268 | { |
4184 | { |
4269 | static int incede; |
4185 | static int incede; |
4270 | |
4186 | |
4271 | api_cede_notself (aTHX); |
4187 | api_cede_notself (aTHX); |
4272 | |
4188 | |
4273 | ++incede; |
4189 | ++incede; |
4274 | while (coro_nready >= incede && api_cede (aTHX)) |
4190 | while (coro_nready >= incede && api_cede (aTHX)) |
… | |
… | |
4318 | { |
4234 | { |
4319 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
4235 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
4320 | coro_old_pp_sselect = 0; |
4236 | coro_old_pp_sselect = 0; |
4321 | } |
4237 | } |
4322 | |
4238 | |
|
|
4239 | MODULE = Coro::State PACKAGE = Coro::Util |
|
|
4240 | |
|
|
4241 | void |
|
|
4242 | _exit (int code) |
|
|
4243 | CODE: |
|
|
4244 | _exit (code); |
|
|
4245 | |
|
|
4246 | NV |
|
|
4247 | time () |
|
|
4248 | CODE: |
|
|
4249 | RETVAL = nvtime (aTHX); |
|
|
4250 | OUTPUT: |
|
|
4251 | RETVAL |
|
|
4252 | |
|
|
4253 | NV |
|
|
4254 | gettimeofday () |
|
|
4255 | PPCODE: |
|
|
4256 | { |
|
|
4257 | UV tv [2]; |
|
|
4258 | u2time (aTHX_ tv); |
|
|
4259 | EXTEND (SP, 2); |
|
|
4260 | PUSHs (sv_2mortal (newSVuv (tv [0]))); |
|
|
4261 | PUSHs (sv_2mortal (newSVuv (tv [1]))); |
|
|
4262 | } |
|
|
4263 | |