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.405 by root, Sat Jun 11 13:01:26 2011 UTC vs.
Revision 1.411 by root, Mon Jun 13 08:38:15 2011 UTC

33# define setjmp _setjmp /* deep magic */ 33# define setjmp _setjmp /* deep magic */
34#else 34#else
35# include <inttypes.h> /* most portable stdint.h */ 35# include <inttypes.h> /* most portable stdint.h */
36#endif 36#endif
37 37
38#ifdef HAVE_MMAP 38#if HAVE_MMAP
39# include <unistd.h> 39# include <unistd.h>
40# include <sys/mman.h> 40# include <sys/mman.h>
41# ifndef MAP_ANONYMOUS 41# ifndef MAP_ANONYMOUS
42# ifdef MAP_ANON 42# ifdef MAP_ANON
43# define MAP_ANONYMOUS MAP_ANON 43# define MAP_ANONYMOUS MAP_ANON
212#define VARx(name,expr,type) type name; 212#define VARx(name,expr,type) type name;
213# include "state.h" 213# include "state.h"
214#undef VARx 214#undef VARx
215} perl_slots; 215} perl_slots;
216 216
217// how many context stack entries do we need for perl_slots 217/* 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)) 218#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
219 219
220/* this is a structure representing a perl-level coroutine */ 220/* this is a structure representing a perl-level coroutine */
221struct coro 221struct coro
222{ 222{
291#define coro_nready coroapi.nready 291#define coro_nready coroapi.nready
292 292
293/** JIT *********************************************************************/ 293/** JIT *********************************************************************/
294 294
295#if CORO_JIT 295#if CORO_JIT
296 /* APPLE doesn't have HAVE_MMAP though */
297 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__)
296 #ifndef CORO_JIT_TYPE 298 #ifndef CORO_JIT_TYPE
297 #if __linux && __amd64 299 #if __x86_64 && CORO_JIT_UNIXY
298 #define CORO_JIT_TYPE "amd64-unix" 300 #define CORO_JIT_TYPE "amd64-unix"
299 typedef void (*load_save_perl_slots_type)(perl_slots *); 301 #elif __i386 && CORO_JIT_UNIXY
300 #else 302 #define CORO_JIT_TYPE "x86-unix"
301 #undef CORO_JIT
302 #endif 303 #endif
303 #endif 304 #endif
304#endif 305#endif
305 306
307#if !defined(CORO_JIT_TYPE) || !HAVE_MMAP
308 #undef CORO_JIT
309#endif
310
306#if CORO_JIT 311#if CORO_JIT
307 312 typedef void (*load_save_perl_slots_type)(perl_slots *);
308static load_save_perl_slots_type load_perl_slots, save_perl_slots; 313 static load_save_perl_slots_type load_perl_slots, save_perl_slots;
309
310#endif 314#endif
311 315
312/** Coro::Select ************************************************************/ 316/** Coro::Select ************************************************************/
313 317
314static OP *(*coro_old_pp_sselect) (pTHX); 318static OP *(*coro_old_pp_sselect) (pTHX);
342 ret [0] = tv.tv_sec; 346 ret [0] = tv.tv_sec;
343 ret [1] = tv.tv_usec; 347 ret [1] = tv.tv_usec;
344} 348}
345 349
346ECB_INLINE double 350ECB_INLINE double
347coro_nvtime () 351coro_nvtime (void)
348{ 352{
349 struct timeval tv; 353 struct timeval tv;
350 gettimeofday (&tv, 0); 354 gettimeofday (&tv, 0);
351 355
352 return tv.tv_sec + tv.tv_usec * 1e-6; 356 return tv.tv_sec + tv.tv_usec * 1e-6;
412#endif 416#endif
413 return get_hv (name, create); 417 return get_hv (name, create);
414} 418}
415 419
416ECB_INLINE void 420ECB_INLINE void
417coro_times_update () 421coro_times_update (void)
418{ 422{
419#ifdef coro_clock_gettime 423#ifdef coro_clock_gettime
420 struct timespec ts; 424 struct timespec ts;
421 425
422 ts.tv_sec = ts.tv_nsec = 0; 426 ts.tv_sec = ts.tv_nsec = 0;
813 817
814 PUTBACK; 818 PUTBACK;
815 } 819 }
816 820
817 /* allocate some space on the context stack for our purposes */ 821 /* allocate some space on the context stack for our purposes */
818 if (ecb_expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) 822 if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max))
819 { 823 {
820 unsigned int i; 824 unsigned int i;
821 825
822 for (i = 0; i < SLOT_COUNT; ++i) 826 for (i = 0; i < SLOT_COUNT; ++i)
823 CXINC; 827 CXINC;
1186 /* restore swapped sv's */ 1190 /* restore swapped sv's */
1187 SWAP_SVS (coro); 1191 SWAP_SVS (coro);
1188 1192
1189 coro_destruct_stacks (aTHX); 1193 coro_destruct_stacks (aTHX);
1190 1194
1191 // now save some sv's to be free'd later 1195 /* now save some sv's to be free'd later */
1192 svf [0] = GvSV (PL_defgv); 1196 svf [0] = GvSV (PL_defgv);
1193 svf [1] = (SV *)GvAV (PL_defgv); 1197 svf [1] = (SV *)GvAV (PL_defgv);
1194 svf [2] = GvSV (PL_errgv); 1198 svf [2] = GvSV (PL_errgv);
1195 svf [3] = (SV *)PL_defoutgv; 1199 svf [3] = (SV *)PL_defoutgv;
1196 svf [4] = PL_rs; 1200 svf [4] = PL_rs;
1439 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1443 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1440 } 1444 }
1441} 1445}
1442 1446
1443static coro_cctx * 1447static coro_cctx *
1444cctx_new () 1448cctx_new (void)
1445{ 1449{
1446 coro_cctx *cctx; 1450 coro_cctx *cctx;
1447 1451
1448 ++cctx_count; 1452 ++cctx_count;
1449 New (0, cctx, 1, coro_cctx); 1453 New (0, cctx, 1, coro_cctx);
1455 return cctx; 1459 return cctx;
1456} 1460}
1457 1461
1458/* create a new cctx only suitable as source */ 1462/* create a new cctx only suitable as source */
1459static coro_cctx * 1463static coro_cctx *
1460cctx_new_empty () 1464cctx_new_empty (void)
1461{ 1465{
1462 coro_cctx *cctx = cctx_new (); 1466 coro_cctx *cctx = cctx_new ();
1463 1467
1464 cctx->sptr = 0; 1468 cctx->sptr = 0;
1465 coro_create (&cctx->cctx, 0, 0, 0, 0); 1469 coro_create (&cctx->cctx, 0, 0, 0, 0);
1467 return cctx; 1471 return cctx;
1468} 1472}
1469 1473
1470/* create a new cctx suitable as destination/running a perl interpreter */ 1474/* create a new cctx suitable as destination/running a perl interpreter */
1471static coro_cctx * 1475static coro_cctx *
1472cctx_new_run () 1476cctx_new_run (void)
1473{ 1477{
1474 coro_cctx *cctx = cctx_new (); 1478 coro_cctx *cctx = cctx_new ();
1475 void *stack_start; 1479 void *stack_start;
1476 size_t stack_size; 1480 size_t stack_size;
1477 1481
1478#if HAVE_MMAP 1482#if HAVE_MMAP
1479 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1483 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1480 /* mmap supposedly does allocate-on-write for us */ 1484 /* mmap supposedly does allocate-on-write for us */
1481 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1485 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, 0, 0);
1482 1486
1483 if (cctx->sptr != (void *)-1) 1487 if (cctx->sptr != (void *)-1)
1484 { 1488 {
1485 #if CORO_STACKGUARD 1489 #if CORO_STACKGUARD
1486 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1490 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
2949 { 2953 {
2950 int i; 2954 int i;
2951 /* if we were woken up but can't down, we look through the whole */ 2955 /* if we were woken up but can't down, we look through the whole */
2952 /* waiters list and only add us if we aren't in there already */ 2956 /* waiters list and only add us if we aren't in there already */
2953 /* this avoids some degenerate memory usage cases */ 2957 /* this avoids some degenerate memory usage cases */
2954 for (i = AvFILLp (av); i > 0; --i) // i > 0 is not an off-by-one bug 2958 for (i = AvFILLp (av); i > 0; --i) /* i > 0 is not an off-by-one bug */
2955 if (AvARRAY (av)[i] == coro_hv) 2959 if (AvARRAY (av)[i] == coro_hv)
2956 return 1; 2960 return 1;
2957 2961
2958 av_push (av, SvREFCNT_inc (coro_hv)); 2962 av_push (av, SvREFCNT_inc (coro_hv));
2959 return 1; 2963 return 1;
3339#endif 3343#endif
3340 3344
3341#if CORO_JIT 3345#if CORO_JIT
3342 3346
3343static void ecb_noinline ecb_cold 3347static void ecb_noinline ecb_cold
3344pushav_3uv (pTHX_ UV a, UV b, UV c) 3348pushav_4uv (pTHX_ UV a, UV b, UV c, UV d)
3345{ 3349{
3346 dSP; 3350 dSP;
3347 AV *av = newAV (); 3351 AV *av = newAV ();
3348 3352
3353 av_store (av, 3, newSVuv (d));
3349 av_store (av, 2, newSVuv (c)); 3354 av_store (av, 2, newSVuv (c));
3350 av_store (av, 1, newSVuv (b)); 3355 av_store (av, 1, newSVuv (b));
3351 av_store (av, 0, newSVuv (a)); 3356 av_store (av, 0, newSVuv (a));
3352 3357
3353 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); 3358 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
3360{ 3365{
3361 dSP; 3366 dSP;
3362 SV *load, *save; 3367 SV *load, *save;
3363 char *map_base; 3368 char *map_base;
3364 char *load_ptr, *save_ptr; 3369 char *load_ptr, *save_ptr;
3365 STRLEN load_len, save_len; 3370 STRLEN load_len, save_len, map_len;
3366 int count; 3371 int count;
3367 3372
3373 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1);
3374
3368 PUSHMARK (SP); 3375 PUSHMARK (SP);
3369 PUTBACK;
3370#define VARx(name,expr,type) pushav_3uv (aTHX_ (UV)&(expr), offsetof (perl_slots, name), sizeof (type)); 3376#define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type));
3371# include "state.h" 3377# include "state.h"
3372#undef VARx 3378#undef VARx
3373 count = call_pv ("Coro::State::_jit", G_ARRAY); 3379 count = call_pv ("Coro::State::_jit", G_ARRAY);
3374 SPAGAIN; 3380 SPAGAIN;
3375 3381
3376 save = POPs; save_ptr = SvPVbyte (save, save_len); 3382 save = POPs; save_ptr = SvPVbyte (save, save_len);
3377 load = POPs; load_ptr = SvPVbyte (load, load_len); 3383 load = POPs; load_ptr = SvPVbyte (load, load_len);
3378 3384
3385 map_len = load_len + save_len + 16;
3386
3379 map_base = mmap (0, load_len + save_len + 16, PROT_EXEC | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 3387 map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3380 3388
3381 assert (("Coro: unable mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED)); 3389 assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED));
3382 3390
3383 load_perl_slots = (load_save_perl_slots_type)map_base; 3391 load_perl_slots = (load_save_perl_slots_type)map_base;
3384 memcpy (map_base, load_ptr, load_len); 3392 memcpy (map_base, load_ptr, load_len);
3385 3393
3386 map_base += (load_len + 15) & ~15; 3394 map_base += (load_len + 15) & ~15;
3387 3395
3388 save_perl_slots = (load_save_perl_slots_type)map_base; 3396 save_perl_slots = (load_save_perl_slots_type)map_base;
3389 memcpy (map_base, save_ptr, save_len); 3397 memcpy (map_base, save_ptr, save_len);
3398
3399 /* we are good citizens and try to make the page read-only, so the evil evil */
3400 /* hackers might have it a bit more difficult */
3401 mprotect (map_base, map_len, PROT_READ | PROT_EXEC);
3402
3403 PUTBACK;
3404 eval_pv ("undef &Coro::State::_jit", 1);
3390} 3405}
3391 3406
3392#endif 3407#endif
3393 3408
3394MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 3409MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
3462 time_init (aTHX); 3477 time_init (aTHX);
3463 3478
3464 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3479 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3465#if CORO_JIT 3480#if CORO_JIT
3466 PUTBACK; 3481 PUTBACK;
3467 require_pv ("Coro/jit-" CORO_JIT_TYPE ".pl");
3468 jit_init (aTHX); 3482 jit_init (aTHX);
3469 perl_eval_pv ("undef &Coro::State::_jit", 1);
3470 SPAGAIN; 3483 SPAGAIN;
3471#endif 3484#endif
3472} 3485}
3473 3486
3474SV * 3487SV *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines