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.407 by root, Sat Jun 11 15:30:21 2011 UTC vs.
Revision 1.410 by root, Sun Jun 12 08:14:12 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 __x86_64 && (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris) 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 #elif __i386 && (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris)
301 #define CORO_JIT_TYPE "x86-unix" 302 #define CORO_JIT_TYPE "x86-unix"
302 typedef void (*load_save_perl_slots_type)(perl_slots *);
303 #else
304 #undef CORO_JIT
305 #endif 303 #endif
306 #endif 304 #endif
307#endif 305#endif
308 306
307#if !defined(CORO_JIT_TYPE) || !HAVE_MMAP
308 #undef CORO_JIT
309#endif
310
309#if CORO_JIT 311#if CORO_JIT
312 typedef void (*load_save_perl_slots_type)(perl_slots *);
310static load_save_perl_slots_type load_perl_slots, save_perl_slots; 313 static load_save_perl_slots_type load_perl_slots, save_perl_slots;
311#endif 314#endif
312 315
313/** Coro::Select ************************************************************/ 316/** Coro::Select ************************************************************/
314 317
315static OP *(*coro_old_pp_sselect) (pTHX); 318static OP *(*coro_old_pp_sselect) (pTHX);
343 ret [0] = tv.tv_sec; 346 ret [0] = tv.tv_sec;
344 ret [1] = tv.tv_usec; 347 ret [1] = tv.tv_usec;
345} 348}
346 349
347ECB_INLINE double 350ECB_INLINE double
348coro_nvtime () 351coro_nvtime (void)
349{ 352{
350 struct timeval tv; 353 struct timeval tv;
351 gettimeofday (&tv, 0); 354 gettimeofday (&tv, 0);
352 355
353 return tv.tv_sec + tv.tv_usec * 1e-6; 356 return tv.tv_sec + tv.tv_usec * 1e-6;
413#endif 416#endif
414 return get_hv (name, create); 417 return get_hv (name, create);
415} 418}
416 419
417ECB_INLINE void 420ECB_INLINE void
418coro_times_update () 421coro_times_update (void)
419{ 422{
420#ifdef coro_clock_gettime 423#ifdef coro_clock_gettime
421 struct timespec ts; 424 struct timespec ts;
422 425
423 ts.tv_sec = ts.tv_nsec = 0; 426 ts.tv_sec = ts.tv_nsec = 0;
1187 /* restore swapped sv's */ 1190 /* restore swapped sv's */
1188 SWAP_SVS (coro); 1191 SWAP_SVS (coro);
1189 1192
1190 coro_destruct_stacks (aTHX); 1193 coro_destruct_stacks (aTHX);
1191 1194
1192 // now save some sv's to be free'd later 1195 /* now save some sv's to be free'd later */
1193 svf [0] = GvSV (PL_defgv); 1196 svf [0] = GvSV (PL_defgv);
1194 svf [1] = (SV *)GvAV (PL_defgv); 1197 svf [1] = (SV *)GvAV (PL_defgv);
1195 svf [2] = GvSV (PL_errgv); 1198 svf [2] = GvSV (PL_errgv);
1196 svf [3] = (SV *)PL_defoutgv; 1199 svf [3] = (SV *)PL_defoutgv;
1197 svf [4] = PL_rs; 1200 svf [4] = PL_rs;
1440 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 */
1441 } 1444 }
1442} 1445}
1443 1446
1444static coro_cctx * 1447static coro_cctx *
1445cctx_new () 1448cctx_new (void)
1446{ 1449{
1447 coro_cctx *cctx; 1450 coro_cctx *cctx;
1448 1451
1449 ++cctx_count; 1452 ++cctx_count;
1450 New (0, cctx, 1, coro_cctx); 1453 New (0, cctx, 1, coro_cctx);
1456 return cctx; 1459 return cctx;
1457} 1460}
1458 1461
1459/* create a new cctx only suitable as source */ 1462/* create a new cctx only suitable as source */
1460static coro_cctx * 1463static coro_cctx *
1461cctx_new_empty () 1464cctx_new_empty (void)
1462{ 1465{
1463 coro_cctx *cctx = cctx_new (); 1466 coro_cctx *cctx = cctx_new ();
1464 1467
1465 cctx->sptr = 0; 1468 cctx->sptr = 0;
1466 coro_create (&cctx->cctx, 0, 0, 0, 0); 1469 coro_create (&cctx->cctx, 0, 0, 0, 0);
1468 return cctx; 1471 return cctx;
1469} 1472}
1470 1473
1471/* create a new cctx suitable as destination/running a perl interpreter */ 1474/* create a new cctx suitable as destination/running a perl interpreter */
1472static coro_cctx * 1475static coro_cctx *
1473cctx_new_run () 1476cctx_new_run (void)
1474{ 1477{
1475 coro_cctx *cctx = cctx_new (); 1478 coro_cctx *cctx = cctx_new ();
1476 void *stack_start; 1479 void *stack_start;
1477 size_t stack_size; 1480 size_t stack_size;
1478 1481
1479#if HAVE_MMAP 1482#if HAVE_MMAP
1480 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;
1481 /* mmap supposedly does allocate-on-write for us */ 1484 /* mmap supposedly does allocate-on-write for us */
1482 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);
1483 1486
1484 if (cctx->sptr != (void *)-1) 1487 if (cctx->sptr != (void *)-1)
1485 { 1488 {
1486 #if CORO_STACKGUARD 1489 #if CORO_STACKGUARD
1487 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1490 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
2950 { 2953 {
2951 int i; 2954 int i;
2952 /* 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 */
2953 /* 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 */
2954 /* this avoids some degenerate memory usage cases */ 2957 /* this avoids some degenerate memory usage cases */
2955 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 */
2956 if (AvARRAY (av)[i] == coro_hv) 2959 if (AvARRAY (av)[i] == coro_hv)
2957 return 1; 2960 return 1;
2958 2961
2959 av_push (av, SvREFCNT_inc (coro_hv)); 2962 av_push (av, SvREFCNT_inc (coro_hv));
2960 return 1; 2963 return 1;
3361{ 3364{
3362 dSP; 3365 dSP;
3363 SV *load, *save; 3366 SV *load, *save;
3364 char *map_base; 3367 char *map_base;
3365 char *load_ptr, *save_ptr; 3368 char *load_ptr, *save_ptr;
3366 STRLEN load_len, save_len; 3369 STRLEN load_len, save_len, map_len;
3367 int count; 3370 int count;
3368 3371
3372 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1);
3373
3369 PUSHMARK (SP); 3374 PUSHMARK (SP);
3370 PUTBACK;
3371#define VARx(name,expr,type) pushav_3uv (aTHX_ (UV)&(expr), offsetof (perl_slots, name), sizeof (type)); 3375#define VARx(name,expr,type) pushav_3uv (aTHX_ (UV)&(expr), offsetof (perl_slots, name), sizeof (type));
3372# include "state.h" 3376# include "state.h"
3373#undef VARx 3377#undef VARx
3374 count = call_pv ("Coro::State::_jit", G_ARRAY); 3378 count = call_pv ("Coro::State::_jit", G_ARRAY);
3375 SPAGAIN; 3379 SPAGAIN;
3376 3380
3377 save = POPs; save_ptr = SvPVbyte (save, save_len); 3381 save = POPs; save_ptr = SvPVbyte (save, save_len);
3378 load = POPs; load_ptr = SvPVbyte (load, load_len); 3382 load = POPs; load_ptr = SvPVbyte (load, load_len);
3379 3383
3384 map_len = load_len + save_len + 16;
3385
3380 map_base = mmap (0, load_len + save_len + 16, PROT_EXEC | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 3386 map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3381 3387
3382 assert (("Coro: unable mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED)); 3388 assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED));
3383 3389
3384 load_perl_slots = (load_save_perl_slots_type)map_base; 3390 load_perl_slots = (load_save_perl_slots_type)map_base;
3385 memcpy (map_base, load_ptr, load_len); 3391 memcpy (map_base, load_ptr, load_len);
3386 3392
3387 map_base += (load_len + 15) & ~15; 3393 map_base += (load_len + 15) & ~15;
3388 3394
3389 save_perl_slots = (load_save_perl_slots_type)map_base; 3395 save_perl_slots = (load_save_perl_slots_type)map_base;
3390 memcpy (map_base, save_ptr, save_len); 3396 memcpy (map_base, save_ptr, save_len);
3397
3398 /* we are good citizens and try to make the page read-only, so the evil evil */
3399 /* hackers might have it a bit more difficult */
3400 mprotect (map_base, map_len, PROT_READ | PROT_EXEC);
3401
3402 PUTBACK;
3403 eval_pv ("undef &Coro::State::_jit", 1);
3391} 3404}
3392 3405
3393#endif 3406#endif
3394 3407
3395MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 3408MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
3463 time_init (aTHX); 3476 time_init (aTHX);
3464 3477
3465 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3478 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3466#if CORO_JIT 3479#if CORO_JIT
3467 PUTBACK; 3480 PUTBACK;
3468 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1);
3469 jit_init (aTHX); 3481 jit_init (aTHX);
3470 perl_eval_pv ("undef &Coro::State::_jit", 1);
3471 SPAGAIN; 3482 SPAGAIN;
3472#endif 3483#endif
3473} 3484}
3474 3485
3475SV * 3486SV *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines