ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.414 by root, Wed Jun 29 16:08:31 2011 UTC vs.
Revision 1.424 by root, Fri Nov 30 19:27:28 2012 UTC

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
17#include "ecb.h" 19#include "ecb.h"
18 20
19#include <stddef.h> 21#include <stddef.h>
20#include <stdio.h> 22#include <stdio.h>
21#include <errno.h> 23#include <errno.h>
101# if CORO_PTHREAD 103# if CORO_PTHREAD
102static void *coro_thx; 104static void *coro_thx;
103# endif 105# endif
104#endif 106#endif
105 107
106/* used in state.h */
107#define VAR(name,type) VARx(name, PL_ ## name, type)
108
109#ifdef __linux 108#ifdef __linux
110# include <time.h> /* for timespec */ 109# include <time.h> /* for timespec */
111# include <syscall.h> /* for SYS_* */ 110# include <syscall.h> /* for SYS_* */
112# ifdef SYS_clock_gettime 111# ifdef SYS_clock_gettime
113# define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) 112# define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
181 void *sptr; 180 void *sptr;
182 size_t ssize; 181 size_t ssize;
183 182
184 /* cpu state */ 183 /* cpu state */
185 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 184 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
185#ifndef NDEBUG
186 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 186 JMPENV *idle_te; /* same as idle_sp, but for top_env */
187#endif
187 JMPENV *top_env; 188 JMPENV *top_env;
188 coro_context cctx; 189 coro_context cctx;
189 190
190 U32 gen; 191 U32 gen;
191#if CORO_USE_VALGRIND 192#if CORO_USE_VALGRIND
211}; 212};
212 213
213/* the structure where most of the perl state is stored, overlaid on the cxstack */ 214/* the structure where most of the perl state is stored, overlaid on the cxstack */
214typedef struct 215typedef struct
215{ 216{
216#define VARx(name,expr,type) type name; 217 #define VARx(name,expr,type) type name;
217# include "state.h" 218 #include "state.h"
218#undef VARx
219} perl_slots; 219} perl_slots;
220 220
221/* how many context stack entries do we need for perl_slots */ 221/* how many context stack entries do we need for perl_slots */
222#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) 222#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
223 223
373time_init (pTHX) 373time_init (pTHX)
374{ 374{
375 SV **svp; 375 SV **svp;
376 376
377 require_pv ("Time/HiRes.pm"); 377 require_pv ("Time/HiRes.pm");
378 378
379 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); 379 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
380 380
381 if (!svp) croak ("Time::HiRes is required, but missing. Caught"); 381 if (!svp) croak ("Time::HiRes is required, but missing. Caught");
382 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught"); 382 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught");
383 383
641 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 641 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
642} 642}
643 643
644/** load & save, init *******************************************************/ 644/** load & save, init *******************************************************/
645 645
646ecb_inline void
647swap_sv (SV *a, SV *b)
648{
649 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
650 SV tmp;
651
652 /* swap sv_any */
653 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
654
655 /* swap sv_flags */
656 SvFLAGS (&tmp) = SvFLAGS (a);
657 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
658 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
659
660#if PERL_VERSION_ATLEAST (5,10,0)
661 /* perl 5.10 and later complicates this _quite_ a bit, but it also
662 * is much faster, so no quarrels here. alternatively, we could
663 * sv_upgrade to avoid this.
664 */
665 {
666 /* swap sv_u */
667 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
668
669 /* if SvANY points to the head, we need to adjust the pointers,
670 * as the pointer for a still points to b, and maybe vice versa.
671 */
672 #define svany_in_head(type) \
673 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
674
675 if (svany_in_head (SvTYPE (a)))
676 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
677
678 if (svany_in_head (SvTYPE (b)))
679 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
680 }
681#endif
682}
683
646/* swap sv heads, at least logically */ 684/* swap sv heads, at least logically */
647static void 685static void
648swap_svs (pTHX_ Coro__State c) 686swap_svs (pTHX_ Coro__State c)
649{ 687{
650 int i; 688 int i;
651 689
652 for (i = 0; i <= AvFILLp (c->swap_sv); ) 690 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2)
653 { 691 swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]);
654 SV *a = AvARRAY (c->swap_sv)[i++];
655 SV *b = AvARRAY (c->swap_sv)[i++];
656
657 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
658 SV tmp;
659
660 /* swap sv_any */
661 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
662
663 /* swap sv_flags */
664 SvFLAGS (&tmp) = SvFLAGS (a);
665 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
666 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
667
668#if PERL_VERSION_ATLEAST (5,10,0)
669 /* perl 5.10 complicates this _quite_ a bit, but it also is
670 * much faster, so no quarrels here. alternatively, we could
671 * sv_upgrade to avoid this.
672 */
673 {
674 /* swap sv_u */
675 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
676
677 /* if SvANY points to the head, we need to adjust the pointers,
678 * as the pointer for a still points to b, and maybe vice versa.
679 */
680 #define svany_in_head(type) \
681 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
682
683 if (svany_in_head (SvTYPE (a)))
684 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
685
686 if (svany_in_head (SvTYPE (b)))
687 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
688 }
689#endif
690 }
691} 692}
692 693
693#define SWAP_SVS(coro) \ 694#define SWAP_SVS(coro) \
694 if (ecb_expect_false ((coro)->swap_sv)) \ 695 if (ecb_expect_false ((coro)->swap_sv)) \
695 swap_svs (aTHX_ (coro)) 696 swap_svs (aTHX_ (coro))
707 708
708#if CORO_JIT 709#if CORO_JIT
709 load_perl_slots (slot); 710 load_perl_slots (slot);
710#else 711#else
711 #define VARx(name,expr,type) expr = slot->name; 712 #define VARx(name,expr,type) expr = slot->name;
712 # include "state.h" 713 #include "state.h"
713 #undef VARx
714#endif 714#endif
715 715
716 { 716 {
717 dSP; 717 dSP;
718 718
840 840
841#if CORO_JIT 841#if CORO_JIT
842 save_perl_slots (slot); 842 save_perl_slots (slot);
843#else 843#else
844 #define VARx(name,expr,type) slot->name = expr; 844 #define VARx(name,expr,type) slot->name = expr;
845 # include "state.h" 845 #include "state.h"
846 #undef VARx
847#endif 846#endif
848 } 847 }
849} 848}
850 849
851/* 850/*
995 { 994 {
996 SV **svp = 0; 995 SV **svp = 0;
997 996
998 if (strEQ (s, "__DIE__" )) svp = &PL_diehook; 997 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
999 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 998 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
1000 999
1001 if (svp) 1000 if (svp)
1002 { 1001 {
1003 sv_setsv (sv, *svp ? *svp : &PL_sv_undef); 1002 SV *ssv;
1003
1004 if (!*svp)
1005 ssv = &PL_sv_undef;
1006 else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */
1007 ssv = sv_2mortal (newRV_inc (*svp));
1008 else
1009 ssv = *svp;
1010
1011 sv_setsv (sv, ssv);
1004 return 0; 1012 return 0;
1005 } 1013 }
1006 } 1014 }
1007 1015
1008 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; 1016 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
1102 PL_hints = 0; 1110 PL_hints = 0;
1103 1111
1104 /* recreate the die/warn hooks */ 1112 /* recreate the die/warn hooks */
1105 PL_diehook = SvREFCNT_inc (rv_diehook); 1113 PL_diehook = SvREFCNT_inc (rv_diehook);
1106 PL_warnhook = SvREFCNT_inc (rv_warnhook); 1114 PL_warnhook = SvREFCNT_inc (rv_warnhook);
1107 1115
1108 GvSV (PL_defgv) = newSV (0); 1116 GvSV (PL_defgv) = newSV (0);
1109 GvAV (PL_defgv) = coro->args; coro->args = 0; 1117 GvAV (PL_defgv) = coro->args; coro->args = 0;
1110 GvSV (PL_errgv) = newSV (0); 1118 GvSV (PL_errgv) = newSV (0);
1111 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 1119 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
1112 GvHV (PL_hintgv) = 0; 1120 GvHV (PL_hintgv) = 0;
1405transfer_tail (pTHX) 1413transfer_tail (pTHX)
1406{ 1414{
1407 free_coro_mortal (aTHX); 1415 free_coro_mortal (aTHX);
1408} 1416}
1409 1417
1418/* try to exit the same way perl's main function would do */
1419/* we do not bother resetting the environment or other things *7
1420/* that are not, uhm, essential */
1421/* this obviously also doesn't work when perl is embedded */
1422static void ecb_noinline ecb_cold
1423perlish_exit (pTHX)
1424{
1425 int exitstatus = perl_destruct (PL_curinterp);
1426 perl_free (PL_curinterp);
1427 exit (exitstatus);
1428}
1429
1410/* 1430/*
1411 * this is a _very_ stripped down perl interpreter ;) 1431 * this is a _very_ stripped down perl interpreter ;)
1412 */ 1432 */
1413static void 1433static void
1414cctx_run (void *arg) 1434cctx_run (void *arg)
1441 */ 1461 */
1442 1462
1443 /* 1463 /*
1444 * If perl-run returns we assume exit() was being called or the coro 1464 * If perl-run returns we assume exit() was being called or the coro
1445 * fell off the end, which seems to be the only valid (non-bug) 1465 * fell off the end, which seems to be the only valid (non-bug)
1446 * reason for perl_run to return. We try to exit by jumping to the 1466 * reason for perl_run to return. We try to mimic whatever perl is normally
1447 * bootstrap-time "top" top_env, as we cannot restore the "main" 1467 * doing in that case. YMMV.
1448 * coroutine as Coro has no such concept.
1449 * This actually isn't valid with the pthread backend, but OSes requiring
1450 * that backend are too broken to do it in a standards-compliant way.
1451 */ 1468 */
1452 PL_top_env = main_top_env; 1469 perlish_exit (aTHX);
1453 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1454 } 1470 }
1455} 1471}
1456 1472
1457static coro_cctx * 1473static coro_cctx *
1458cctx_new (void) 1474cctx_new (void)
1490 size_t stack_size; 1506 size_t stack_size;
1491 1507
1492#if HAVE_MMAP 1508#if HAVE_MMAP
1493 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1509 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1494 /* mmap supposedly does allocate-on-write for us */ 1510 /* mmap supposedly does allocate-on-write for us */
1495 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, 0, 0); 1511 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, -1, 0);
1496 1512
1497 if (cctx->sptr != (void *)-1) 1513 if (cctx->sptr != (void *)-1)
1498 { 1514 {
1499 #if CORO_STACKGUARD 1515 #if CORO_STACKGUARD
1500 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1516 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1715 return; 1731 return;
1716 1732
1717 slf_destroy (aTHX_ coro); 1733 slf_destroy (aTHX_ coro);
1718 1734
1719 coro->flags |= CF_ZOMBIE; 1735 coro->flags |= CF_ZOMBIE;
1720 1736
1721 if (coro->flags & CF_READY) 1737 if (coro->flags & CF_READY)
1722 { 1738 {
1723 /* reduce nready, as destroying a ready coro effectively unreadies it */ 1739 /* reduce nready, as destroying a ready coro effectively unreadies it */
1724 /* alternative: look through all ready queues and remove the coro */ 1740 /* alternative: look through all ready queues and remove the coro */
1725 --coro_nready; 1741 --coro_nready;
2158 2174
2159static void 2175static void
2160coro_set_status (pTHX_ struct coro *coro, SV **arg, int items) 2176coro_set_status (pTHX_ struct coro *coro, SV **arg, int items)
2161{ 2177{
2162 AV *av; 2178 AV *av;
2163 2179
2164 if (coro->status) 2180 if (coro->status)
2165 { 2181 {
2166 av = coro->status; 2182 av = coro->status;
2167 av_clear (av); 2183 av_clear (av);
2168 } 2184 }
2215 2231
2216 coro = SvSTATE (arg [0]); 2232 coro = SvSTATE (arg [0]);
2217 coro_hv = coro->hv; 2233 coro_hv = coro->hv;
2218 2234
2219 coro_set_status (aTHX_ coro, arg + 1, items - 1); 2235 coro_set_status (aTHX_ coro, arg + 1, items - 1);
2220 2236
2221 if (ecb_expect_false (coro->flags & CF_NOCANCEL)) 2237 if (ecb_expect_false (coro->flags & CF_NOCANCEL))
2222 { 2238 {
2223 /* coro currently busy cancelling something, so just notify it */ 2239 /* coro currently busy cancelling something, so just notify it */
2224 coro->slf_frame.data = (void *)coro; 2240 coro->slf_frame.data = (void *)coro;
2225 2241
2404 2420
2405static int 2421static int
2406slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) 2422slf_check_rouse_wait (pTHX_ struct CoroSLF *frame)
2407{ 2423{
2408 SV *data = (SV *)frame->data; 2424 SV *data = (SV *)frame->data;
2409 2425
2410 if (CORO_THROW) 2426 if (CORO_THROW)
2411 return 0; 2427 return 0;
2412 2428
2413 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2429 if (SvTYPE (SvRV (data)) != SVt_PVAV)
2414 return 1; 2430 return 1;
2450 cb = sv_2mortal (coro->rouse_cb); 2466 cb = sv_2mortal (coro->rouse_cb);
2451 coro->rouse_cb = 0; 2467 coro->rouse_cb = 0;
2452 } 2468 }
2453 2469
2454 if (!SvROK (cb) 2470 if (!SvROK (cb)
2455 || SvTYPE (SvRV (cb)) != SVt_PVCV 2471 || SvTYPE (SvRV (cb)) != SVt_PVCV
2456 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) 2472 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
2457 croak ("Coro::rouse_wait called with illegal callback argument,"); 2473 croak ("Coro::rouse_wait called with illegal callback argument,");
2458 2474
2459 { 2475 {
2460 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ 2476 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */
3381 int count; 3397 int count;
3382 3398
3383 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); 3399 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1);
3384 3400
3385 PUSHMARK (SP); 3401 PUSHMARK (SP);
3386#define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type)); 3402 #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type));
3387# include "state.h" 3403 #include "state.h"
3388#undef VARx
3389 count = call_pv ("Coro::State::_jit", G_ARRAY); 3404 count = call_pv ("Coro::State::_jit", G_ARRAY);
3390 SPAGAIN; 3405 SPAGAIN;
3391 3406
3392 save = POPs; save_ptr = SvPVbyte (save, save_len); 3407 save = POPs; save_ptr = SvPVbyte (save, save_len);
3393 load = POPs; load_ptr = SvPVbyte (load, load_len); 3408 load = POPs; load_ptr = SvPVbyte (load, load_len);
3650 RETVAL = boolSV (coro->flags & ix); 3665 RETVAL = boolSV (coro->flags & ix);
3651 OUTPUT: 3666 OUTPUT:
3652 RETVAL 3667 RETVAL
3653 3668
3654void 3669void
3655throw (Coro::State self, SV *exception = &PL_sv_undef) 3670throw (SV *self, SV *exception = &PL_sv_undef)
3656 PROTOTYPE: $;$ 3671 PROTOTYPE: $;$
3657 CODE: 3672 CODE:
3658{ 3673{
3674 struct coro *coro = SvSTATE (self);
3659 struct coro *current = SvSTATE_current; 3675 struct coro *current = SvSTATE_current;
3660 SV **exceptionp = self == current ? &CORO_THROW : &self->except; 3676 SV **exceptionp = coro == current ? &CORO_THROW : &coro->except;
3661 SvREFCNT_dec (*exceptionp); 3677 SvREFCNT_dec (*exceptionp);
3662 SvGETMAGIC (exception); 3678 SvGETMAGIC (exception);
3663 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; 3679 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3680
3681 api_ready (aTHX_ self);
3664} 3682}
3665 3683
3666void 3684void
3667api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3685api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3668 PROTOTYPE: $;$ 3686 PROTOTYPE: $;$
3798 3816
3799 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3817 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3800 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3818 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3801 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3819 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3802 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ 3820 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */
3803 3821
3804 coro_stash = gv_stashpv ("Coro", TRUE); 3822 coro_stash = gv_stashpv ("Coro", TRUE);
3805 3823
3806 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); 3824 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3807 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); 3825 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3808 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); 3826 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines