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.20 by root, Fri Aug 3 12:51:56 2001 UTC vs.
Revision 1.55 by pcg, Thu Apr 1 02:29:05 2004 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include "patchlevel.h"
6
7#if PATCHLEVEL < 6
8# ifndef PL_ppaddr
9# define PL_ppaddr ppaddr
10# endif
11# ifndef call_sv
12# define call_sv perl_call_sv
13# endif
14# ifndef get_sv
15# define get_sv perl_get_sv
16# endif
17# ifndef get_cv
18# define get_cv perl_get_cv
19# endif
20# ifndef IS_PADGV
21# define IS_PADGV(v) 0
22# endif
23# ifndef IS_PADCONST
24# define IS_PADCONST(v) 0
25# endif
26#endif
27
5#include "libcoro/coro.c" 28#include "libcoro/coro.c"
6 29
7#include <signal.h> 30#include <signal.h>
8 31
9#ifdef HAVE_MMAP 32#ifdef HAVE_MMAP
10# include <unistd.h> 33# include <unistd.h>
11# include <sys/mman.h> 34# include <sys/mman.h>
12# ifndef MAP_ANON
13# ifdef MAP_ANONYMOUS 35# ifndef MAP_ANONYMOUS
14# define MAP_ANON MAP_ANONYMOUS 36# ifdef MAP_ANON
37# define MAP_ANONYMOUS MAP_ANON
15# else 38# else
16# undef HAVE_MMAP 39# undef HAVE_MMAP
17# endif 40# endif
18# endif 41# endif
19#endif 42#endif
20 43
21#define MAY_FLUSH /* increases codesize */
22
23/* perl-related */
24#define TRANSFER_SAVE_DEFAV 0x00000001
25#define TRANSFER_SAVE_DEFSV 0x00000002
26#define TRANSFER_SAVE_ERRSV 0x00000004
27/* c-related */
28#define TRANSFER_SAVE_CCTXT 0x00000008
29#ifdef CORO_LAZY_STACK
30# define TRANSFER_LAZY_STACK 0x00000010
31#else
32# define TRANSFER_LAZY_STACK 0x00000000
33#endif
34
35#define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \
36 |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT)
37
38#define SUB_INIT "Coro::State::initialize" 44#define SUB_INIT "Coro::State::initialize"
39#define UCORO_STATE "_coro_state" 45#define UCORO_STATE "_coro_state"
40 46
41/* The next macro should delcare a variable stacklevel that contains and approximation 47/* The next macro should declare a variable stacklevel that contains and approximation
42 * to the current C stack pointer. It's property is that it changes with each call 48 * to the current C stack pointer. Its property is that it changes with each call
43 * and should be unique. */ 49 * and should be unique. */
44#define dSTACKLEVEL void *stacklevel = &stacklevel 50#define dSTACKLEVEL void *stacklevel = &stacklevel
45 51
52#define IN_DESTRUCT (PL_main_cv == Nullcv)
53
46#define labs(l) ((l) >= 0 ? (l) : -(l)) 54#define labs(l) ((l) >= 0 ? (l) : -(l))
55
56#include "CoroAPI.h"
57
58#ifdef USE_ITHREADS
59static perl_mutex coro_mutex;
60# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
61# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
62#else
63# define LOCK 0
64# define UNLOCK 0
65#endif
66
67static struct CoroAPI coroapi;
47 68
48/* this is actually not only the c stack but also c registers etc... */ 69/* this is actually not only the c stack but also c registers etc... */
49typedef struct { 70typedef struct {
50 int refcnt; /* pointer reference counter */ 71 int refcnt; /* pointer reference counter */
51 int usecnt; /* shared by how many coroutines */ 72 int usecnt; /* shared by how many coroutines */
56 void *sptr; 77 void *sptr;
57 long ssize; /* positive == mmap, otherwise malloc */ 78 long ssize; /* positive == mmap, otherwise malloc */
58} coro_stack; 79} coro_stack;
59 80
60struct coro { 81struct coro {
82 /* the top-level JMPENV for each coroutine, needed to catch dies. */
83 JMPENV start_env;
84
61 /* the optional C context */ 85 /* the optional C context */
62 coro_stack *stack; 86 coro_stack *stack;
63 void *cursp; 87 void *cursp;
64 int gencnt; 88 int gencnt;
65 89
77 AV *curstack; 101 AV *curstack;
78 AV *mainstack; 102 AV *mainstack;
79 SV **stack_sp; 103 SV **stack_sp;
80 OP *op; 104 OP *op;
81 SV **curpad; 105 SV **curpad;
106 AV *comppad;
82 SV **stack_base; 107 SV **stack_base;
83 SV **stack_max; 108 SV **stack_max;
84 SV **tmps_stack; 109 SV **tmps_stack;
85 I32 tmps_floor; 110 I32 tmps_floor;
86 I32 tmps_ix; 111 I32 tmps_ix;
109 134
110static AV *main_mainstack; /* used to differentiate between $main and others */ 135static AV *main_mainstack; /* used to differentiate between $main and others */
111static HV *coro_state_stash; 136static HV *coro_state_stash;
112static SV *ucoro_state_sv; 137static SV *ucoro_state_sv;
113static U32 ucoro_state_hash; 138static U32 ucoro_state_hash;
114static HV *padlist_cache; 139static SV *coro_mortal; /* will be freed after next transfer */
115 140
116/* mostly copied from op.c:cv_clone2 */ 141/* mostly copied from op.c:cv_clone2 */
117STATIC AV * 142STATIC AV *
118clone_padlist (AV *protopadlist) 143clone_padlist (AV *protopadlist)
119{ 144{
147 AvFLAGS (av) = AVf_REIFY; 172 AvFLAGS (av) = AVf_REIFY;
148 173
149 for (ix = fpad; ix > 0; ix--) 174 for (ix = fpad; ix > 0; ix--)
150 { 175 {
151 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; 176 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv;
177
152 if (namesv && namesv != &PL_sv_undef) 178 if (namesv && namesv != &PL_sv_undef)
153 { 179 {
154 char *name = SvPVX (namesv); /* XXX */ 180 char *name = SvPVX (namesv); /* XXX */
181
155 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') 182 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&')
156 { /* lexical from outside? */ 183 { /* lexical from outside? */
157 npad[ix] = SvREFCNT_inc (ppad[ix]); 184 npad[ix] = SvREFCNT_inc (ppad[ix]);
158 } 185 }
159 else 186 else
165 sv = (SV *) newAV (); 192 sv = (SV *) newAV ();
166 else if (*name == '%') 193 else if (*name == '%')
167 sv = (SV *) newHV (); 194 sv = (SV *) newHV ();
168 else 195 else
169 sv = NEWSV (0, 0); 196 sv = NEWSV (0, 0);
197
198#ifdef SvPADBUSY
170 if (!SvPADBUSY (sv)) 199 if (!SvPADBUSY (sv))
200#endif
171 SvPADMY_on (sv); 201 SvPADMY_on (sv);
202
172 npad[ix] = sv; 203 npad[ix] = sv;
173 } 204 }
174 } 205 }
175 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) 206 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix]))
176 { 207 {
205#endif 236#endif
206 237
207 return newpadlist; 238 return newpadlist;
208} 239}
209 240
210#ifdef MAY_FLUSH 241STATIC void
211STATIC AV *
212free_padlist (AV *padlist) 242free_padlist (AV *padlist)
213{ 243{
214 /* may be during global destruction */ 244 /* may be during global destruction */
215 if (SvREFCNT(padlist)) 245 if (SvREFCNT (padlist))
216 { 246 {
217 I32 i = AvFILLp(padlist); 247 I32 i = AvFILLp (padlist);
218 while (i >= 0) 248 while (i >= 0)
219 { 249 {
220 SV **svp = av_fetch(padlist, i--, FALSE); 250 SV **svp = av_fetch (padlist, i--, FALSE);
221 SV *sv = svp ? *svp : Nullsv;
222 if (sv) 251 if (svp)
252 {
253 SV *sv;
254 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp)))
223 SvREFCNT_dec(sv); 255 SvREFCNT_dec (sv);
256
257 SvREFCNT_dec (*svp);
258 }
224 } 259 }
225 260
226 SvREFCNT_dec((SV*)padlist); 261 SvREFCNT_dec ((SV*)padlist);
227 } 262 }
228} 263}
229#endif 264
265STATIC int
266coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
267{
268 AV *padlist;
269 AV *av = (AV *)mg->mg_obj;
270
271 /* casting is fun. */
272 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
273 free_padlist (padlist);
274}
275
276#define PERL_MAGIC_coro PERL_MAGIC_ext
277
278static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
230 279
231/* the next two functions merely cache the padlists */ 280/* the next two functions merely cache the padlists */
232STATIC void 281STATIC void
233get_padlist (CV *cv) 282get_padlist (CV *cv)
234{ 283{
235 SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 0); 284 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
236 285
237 if (he && AvFILLp ((AV *)*he) >= 0) 286 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
238 CvPADLIST (cv) = (AV *)av_pop ((AV *)*he); 287 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
239 else 288 else
240 CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); 289 CvPADLIST (cv) = clone_padlist (CvPADLIST (cv));
241} 290}
242 291
243STATIC void 292STATIC void
244put_padlist (CV *cv) 293put_padlist (CV *cv)
245{ 294{
246 SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 1); 295 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
247 296
248 if (SvTYPE (*he) != SVt_PVAV) 297 if (!mg)
249 { 298 {
250 SvREFCNT_dec (*he); 299 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
300 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
301 mg->mg_virtual = &vtbl_coro;
251 *he = (SV *)newAV (); 302 mg->mg_obj = (SV *)newAV ();
252 } 303 }
253 304
254 av_push ((AV *)*he, (SV *)CvPADLIST (cv)); 305 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv));
255} 306}
256
257#ifdef MAY_FLUSH
258STATIC void
259flush_padlist_cache ()
260{
261 HV *hv = padlist_cache;
262 padlist_cache = newHV ();
263
264 if (hv_iterinit (hv))
265 {
266 HE *he;
267 AV *padlist;
268
269 while (!!(he = hv_iternext (hv)))
270 {
271 AV *av = (AV *)HeVAL(he);
272
273 /* casting is fun. */
274 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
275 free_padlist (padlist);
276 }
277 }
278
279 SvREFCNT_dec (hv);
280}
281#endif
282 307
283#define SB do { 308#define SB do {
284#define SE } while (0) 309#define SE } while (0)
285 310
286#define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE 311#define LOAD(state) load_state(aTHX_ (state));
287#define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE 312#define SAVE(state,flags) save_state(aTHX_ (state),(flags));
288 313
289#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE 314#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
290 315
291static void 316static void
292load_state(pTHX_ Coro__State c) 317load_state(pTHX_ Coro__State c)
293{ 318{
294 PL_dowarn = c->dowarn; 319 PL_dowarn = c->dowarn;
298 PL_curstack = c->curstack; 323 PL_curstack = c->curstack;
299 PL_mainstack = c->mainstack; 324 PL_mainstack = c->mainstack;
300 PL_stack_sp = c->stack_sp; 325 PL_stack_sp = c->stack_sp;
301 PL_op = c->op; 326 PL_op = c->op;
302 PL_curpad = c->curpad; 327 PL_curpad = c->curpad;
328 PL_comppad = c->comppad;
303 PL_stack_base = c->stack_base; 329 PL_stack_base = c->stack_base;
304 PL_stack_max = c->stack_max; 330 PL_stack_max = c->stack_max;
305 PL_tmps_stack = c->tmps_stack; 331 PL_tmps_stack = c->tmps_stack;
306 PL_tmps_floor = c->tmps_floor; 332 PL_tmps_floor = c->tmps_floor;
307 PL_tmps_ix = c->tmps_ix; 333 PL_tmps_ix = c->tmps_ix;
336 362
337 if (padlist) 363 if (padlist)
338 { 364 {
339 put_padlist (cv); /* mark this padlist as available */ 365 put_padlist (cv); /* mark this padlist as available */
340 CvPADLIST(cv) = padlist; 366 CvPADLIST(cv) = padlist;
341#ifdef USE_THREADS
342 /*CvOWNER(cv) = (struct perl_thread *)POPs;*/
343#endif
344 } 367 }
345 368
346 ++CvDEPTH(cv); 369 ++CvDEPTH(cv);
347 } 370 }
348 371
366 389
367 PUSHs (Nullsv); 390 PUSHs (Nullsv);
368 /* this loop was inspired by pp_caller */ 391 /* this loop was inspired by pp_caller */
369 for (;;) 392 for (;;)
370 { 393 {
371 do 394 while (cxix >= 0)
372 { 395 {
373 PERL_CONTEXT *cx = &ccstk[cxix--]; 396 PERL_CONTEXT *cx = &ccstk[cxix--];
374 397
375 if (CxTYPE(cx) == CXt_SUB) 398 if (CxTYPE(cx) == CXt_SUB)
376 { 399 {
377 CV *cv = cx->blk_sub.cv; 400 CV *cv = cx->blk_sub.cv;
378 if (CvDEPTH(cv)) 401 if (CvDEPTH(cv))
379 { 402 {
380#ifdef USE_THREADS
381 /*XPUSHs ((SV *)CvOWNER(cv));*/
382 /*CvOWNER(cv) = 0;*/
383 /*error must unlock this cv etc.. etc...*/
384#endif
385 EXTEND (SP, CvDEPTH(cv)*2); 403 EXTEND (SP, CvDEPTH(cv)*2);
386 404
387 while (--CvDEPTH(cv)) 405 while (--CvDEPTH(cv))
388 { 406 {
389 /* this tells the restore code to increment CvDEPTH */ 407 /* this tells the restore code to increment CvDEPTH */
395 PUSHs ((SV *)cv); 413 PUSHs ((SV *)cv);
396 414
397 get_padlist (cv); /* this is a monster */ 415 get_padlist (cv); /* this is a monster */
398 } 416 }
399 } 417 }
418#ifdef CXt_FORMAT
400 else if (CxTYPE(cx) == CXt_FORMAT) 419 else if (CxTYPE(cx) == CXt_FORMAT)
401 { 420 {
402 /* I never used formats, so how should I know how these are implemented? */ 421 /* I never used formats, so how should I know how these are implemented? */
403 /* my bold guess is as a simple, plain sub... */ 422 /* my bold guess is as a simple, plain sub... */
404 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 423 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
405 } 424 }
425#endif
406 } 426 }
407 while (cxix >= 0);
408 427
409 if (top_si->si_type == PERLSI_MAIN) 428 if (top_si->si_type == PERLSI_MAIN)
410 break; 429 break;
411 430
412 top_si = top_si->si_prev; 431 top_si = top_si->si_prev;
418 } 437 }
419 438
420 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 439 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
421 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 440 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
422 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 441 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
423
424 /* I have not the slightest idea of why av_reify is necessary */
425 /* but if it's missing the defav contents magically get replaced sometimes */
426 if (c->defav)
427 av_reify (c->defav);
428 442
429 c->dowarn = PL_dowarn; 443 c->dowarn = PL_dowarn;
430 c->in_eval = PL_in_eval; 444 c->in_eval = PL_in_eval;
431 445
432 c->curstackinfo = PL_curstackinfo; 446 c->curstackinfo = PL_curstackinfo;
433 c->curstack = PL_curstack; 447 c->curstack = PL_curstack;
434 c->mainstack = PL_mainstack; 448 c->mainstack = PL_mainstack;
435 c->stack_sp = PL_stack_sp; 449 c->stack_sp = PL_stack_sp;
436 c->op = PL_op; 450 c->op = PL_op;
437 c->curpad = PL_curpad; 451 c->curpad = PL_curpad;
452 c->comppad = PL_comppad;
438 c->stack_base = PL_stack_base; 453 c->stack_base = PL_stack_base;
439 c->stack_max = PL_stack_max; 454 c->stack_max = PL_stack_max;
440 c->tmps_stack = PL_tmps_stack; 455 c->tmps_stack = PL_tmps_stack;
441 c->tmps_floor = PL_tmps_floor; 456 c->tmps_floor = PL_tmps_floor;
442 c->tmps_ix = PL_tmps_ix; 457 c->tmps_ix = PL_tmps_ix;
458} 473}
459 474
460/* 475/*
461 * allocate various perl stacks. This is an exact copy 476 * allocate various perl stacks. This is an exact copy
462 * of perl.c:init_stacks, except that it uses less memory 477 * of perl.c:init_stacks, except that it uses less memory
463 * on the assumption that coroutines do not usually need 478 * on the (sometimes correct) assumption that coroutines do
464 * a lot of stackspace. 479 * not usually need a lot of stackspace.
465 */ 480 */
466STATIC void 481STATIC void
467coro_init_stacks (pTHX) 482coro_init_stacks (pTHX)
468{ 483{
484 LOCK;
485
469 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 486 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
470 PL_curstackinfo->si_type = PERLSI_MAIN; 487 PL_curstackinfo->si_type = PERLSI_MAIN;
471 PL_curstack = PL_curstackinfo->si_stack; 488 PL_curstack = PL_curstackinfo->si_stack;
472 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 489 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
473 490
482 499
483 New(54,PL_markstack,16,I32); 500 New(54,PL_markstack,16,I32);
484 PL_markstack_ptr = PL_markstack; 501 PL_markstack_ptr = PL_markstack;
485 PL_markstack_max = PL_markstack + 16; 502 PL_markstack_max = PL_markstack + 16;
486 503
504#ifdef SET_MARK_OFFSET
487 SET_MARK_OFFSET; 505 SET_MARK_OFFSET;
506#endif
488 507
489 New(54,PL_scopestack,16,I32); 508 New(54,PL_scopestack,16,I32);
490 PL_scopestack_ix = 0; 509 PL_scopestack_ix = 0;
491 PL_scopestack_max = 16; 510 PL_scopestack_max = 16;
492 511
495 PL_savestack_max = 96; 514 PL_savestack_max = 96;
496 515
497 New(54,PL_retstack,8,OP*); 516 New(54,PL_retstack,8,OP*);
498 PL_retstack_ix = 0; 517 PL_retstack_ix = 0;
499 PL_retstack_max = 8; 518 PL_retstack_max = 8;
519
520 UNLOCK;
500} 521}
501 522
502/* 523/*
503 * destroy the stacks, the callchain etc... 524 * destroy the stacks, the callchain etc...
504 * still there is a memleak of 128 bytes...
505 */ 525 */
506STATIC void 526STATIC void
507destroy_stacks(pTHX) 527destroy_stacks(pTHX)
508{ 528{
529 if (!IN_DESTRUCT)
530 {
509 /* is this ugly, I ask? */ 531 /* is this ugly, I ask? */
510 while (PL_scopestack_ix) 532 LEAVE_SCOPE (0);
511 LEAVE;
512 533
513 /* sure it is, but more important: is it correct?? :/ */ 534 /* sure it is, but more important: is it correct?? :/ */
514 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
515 FREETMPS; 535 FREETMPS;
536 }
516 537
517 while (PL_curstackinfo->si_next) 538 while (PL_curstackinfo->si_next)
518 PL_curstackinfo = PL_curstackinfo->si_next; 539 PL_curstackinfo = PL_curstackinfo->si_next;
519 540
520 while (PL_curstackinfo) 541 while (PL_curstackinfo)
525 dSP; 546 dSP;
526 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 547 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
527 PUTBACK; /* possibly superfluous */ 548 PUTBACK; /* possibly superfluous */
528 } 549 }
529 550
551 if (!IN_DESTRUCT)
552 {
530 dounwind(-1); 553 dounwind(-1);
531
532 SvREFCNT_dec(PL_curstackinfo->si_stack); 554 SvREFCNT_dec(PL_curstackinfo->si_stack);
555 }
556
533 Safefree(PL_curstackinfo->si_cxstack); 557 Safefree(PL_curstackinfo->si_cxstack);
534 Safefree(PL_curstackinfo); 558 Safefree(PL_curstackinfo);
535 PL_curstackinfo = p; 559 PL_curstackinfo = p;
536 } 560 }
537 561
550 New (0, stack, 1, coro_stack); 574 New (0, stack, 1, coro_stack);
551 575
552 stack->refcnt = 1; 576 stack->refcnt = 1;
553 stack->usecnt = 1; 577 stack->usecnt = 1;
554 stack->gencnt = ctx->gencnt = 0; 578 stack->gencnt = ctx->gencnt = 0;
579
555 if (alloc) 580 if (alloc)
556 { 581 {
557#ifdef HAVE_MMAP 582#if HAVE_MMAP
558 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ 583 stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */
559 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 584 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
560 if (stack->sptr == (void *)-1) 585 if (stack->sptr == (void *)-1)
561#endif 586#endif
562 { 587 {
563 /*FIXME*//*D*//* reasonable stack size! */ 588 /*FIXME*//*D*//* reasonable stack size! */
564 stack->ssize = -4096 * sizeof (long); 589 stack->ssize = - (8192 * sizeof (long));
565 New (0, stack->sptr, 4096, long); 590 New (0, stack->sptr, 8192, long);
566 } 591 }
567 } 592 }
568 else 593 else
569 stack->sptr = 0; 594 stack->sptr = 0;
570 595
584 { 609 {
585#ifdef HAVE_MMAP 610#ifdef HAVE_MMAP
586 if (stack->ssize > 0 && stack->sptr) 611 if (stack->ssize > 0 && stack->sptr)
587 munmap (stack->sptr, stack->ssize); 612 munmap (stack->sptr, stack->ssize);
588 else 613 else
589#else 614#endif
590 Safefree (stack->sptr); 615 Safefree (stack->sptr);
591#endif 616
592 Safefree (stack); 617 Safefree (stack);
593 } 618 }
594 else if (ctx->gencnt == stack->gencnt) 619 else if (ctx->gencnt == stack->gencnt)
595 --stack->usecnt; 620 --stack->usecnt;
596 } 621 }
602 /* 627 /*
603 * emulate part of the perl startup here. 628 * emulate part of the perl startup here.
604 */ 629 */
605 dSP; 630 dSP;
606 Coro__State ctx = (Coro__State)arg; 631 Coro__State ctx = (Coro__State)arg;
607 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 632 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE);
608 633
609 coro_init_stacks (aTHX); 634 coro_init_stacks (aTHX);
610 /*PL_curcop = 0;*/ 635 /*PL_curcop = 0;*/
611 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 636 /*PL_in_eval = PL_in_eval;*/ /* inherit */
612 SvREFCNT_dec (GvAV (PL_defgv)); 637 SvREFCNT_dec (GvAV (PL_defgv));
613 GvAV (PL_defgv) = ctx->args; 638 GvAV (PL_defgv) = ctx->args; ctx->args = 0;
614 639
615 SPAGAIN; 640 SPAGAIN;
616 641
617 if (ctx->stack) 642 if (ctx->stack)
618 { 643 {
645 * that doesn't matter, though, since it is only 670 * that doesn't matter, though, since it is only
646 * pp_nextstate and we never return... 671 * pp_nextstate and we never return...
647 * ah yes, and I don't care anyways ;) 672 * ah yes, and I don't care anyways ;)
648 */ 673 */
649 PUTBACK; 674 PUTBACK;
650 PL_op = pp_entersub(); 675 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
651 SPAGAIN; 676 SPAGAIN;
652 677
653 ENTER; /* necessary e.g. for dounwind */ 678 ENTER; /* necessary e.g. for dounwind */
654 } 679 }
655} 680}
659{ 684{
660 /* 685 /*
661 * this is a _very_ stripped down perl interpreter ;) 686 * this is a _very_ stripped down perl interpreter ;)
662 */ 687 */
663 Coro__State ctx = (Coro__State)arg; 688 Coro__State ctx = (Coro__State)arg;
689 JMPENV coro_start_env;
664 690
665 /*FIXME*//* must set up top_env here */ 691 PL_top_env = &ctx->start_env;
692
666 ctx->cursp = 0; 693 ctx->cursp = 0;
667 PL_op = PL_op->op_next; 694 PL_op = PL_op->op_next;
668 CALLRUNOPS(aTHX); 695 CALLRUNOPS(aTHX);
669 696
670 abort (); 697 abort ();
671} 698}
672 699
673STATIC void 700STATIC void
674transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 701transfer (pTHX_ struct coro *prev, struct coro *next, int flags)
675{ 702{
676 dSP;
677 dSTACKLEVEL; 703 dSTACKLEVEL;
678 704
679 if (prev != next) 705 if (prev != next)
680 { 706 {
681 if (next->mainstack) 707 if (next->mainstack)
682 { 708 {
709 LOCK;
683 SAVE (prev, flags); 710 SAVE (prev, flags);
684 LOAD (next); 711 LOAD (next);
712 UNLOCK;
685 713
686 /* mark this state as in-use */ 714 /* mark this state as in-use */
687 next->mainstack = 0; 715 next->mainstack = 0;
688 next->tmps_ix = -2; 716 next->tmps_ix = -2;
689 717
708 continue_coro, (void *)next, 736 continue_coro, (void *)next,
709 next->stack->sptr, labs (next->stack->ssize)); 737 next->stack->sptr, labs (next->stack->ssize));
710 } 738 }
711 739
712 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 740 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
741 prev->cursp = stacklevel;
742 /* don't add any code here */
713 } 743 }
714 744 else
745 next->cursp = stacklevel;
715 } 746 }
716 else if (next->tmps_ix == -2) 747 else if (next->tmps_ix == -2)
717 croak ("tried to transfer to running coroutine"); 748 croak ("tried to transfer to running coroutine");
718 else 749 else
719 { 750 {
751 LOCK;
720 SAVE (prev, -1); /* first get rid of the old state */ 752 SAVE (prev, -1); /* first get rid of the old state */
753 UNLOCK;
721 754
722 if (flags & TRANSFER_SAVE_CCTXT) 755 if (flags & TRANSFER_SAVE_CCTXT)
723 { 756 {
724 if (!prev->stack) 757 if (!prev->stack)
725 allocate_stack (prev, 0); 758 allocate_stack (prev, 0);
726 759
727 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 760 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
728 { 761 {
762 PL_top_env = &next->start_env;
763
729 setup_coro (next); 764 setup_coro (next);
765 next->cursp = stacklevel;
730 766
731 prev->stack->refcnt++; 767 prev->stack->refcnt++;
732 prev->stack->usecnt++; 768 prev->stack->usecnt++;
733 next->stack = prev->stack; 769 next->stack = prev->stack;
734 next->gencnt = prev->gencnt; 770 next->gencnt = prev->gencnt;
735 } 771 }
736 else 772 else
737 { 773 {
774 assert (!next->stack);
738 allocate_stack (next, 1); 775 allocate_stack (next, 1);
739 coro_create (&(next->stack->cctx), 776 coro_create (&(next->stack->cctx),
740 setup_coro, (void *)next, 777 setup_coro, (void *)next,
741 next->stack->sptr, labs (next->stack->ssize)); 778 next->stack->sptr, labs (next->stack->ssize));
742 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 779 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
780 prev->cursp = stacklevel;
781 /* don't add any code here */
743 } 782 }
744 } 783 }
745 else 784 else
785 {
746 setup_coro (next); 786 setup_coro (next);
787 next->cursp = stacklevel;
788 }
747 } 789 }
748 } 790 }
749 791
750 next->cursp = stacklevel; 792 LOCK;
793 if (coro_mortal)
794 {
795 SvREFCNT_dec (coro_mortal);
796 coro_mortal = 0;
797 }
798 UNLOCK;
799}
800
801#define SV_CORO(sv,func) \
802 do { \
803 if (SvROK (sv)) \
804 sv = SvRV (sv); \
805 \
806 if (SvTYPE (sv) == SVt_PVHV) \
807 { \
808 HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
809 \
810 if (!he) \
811 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
812 \
813 (sv) = SvRV (HeVAL(he)); \
814 } \
815 \
816 /* must also be changed inside Coro::Cont::yield */ \
817 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
818 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
819 \
820 } while(0)
821
822#define SvSTATE(sv) (struct coro *)SvIV (sv)
823
824static void
825api_transfer(pTHX_ SV *prev, SV *next, int flags)
826{
827 SV_CORO (prev, "Coro::transfer");
828 SV_CORO (next, "Coro::transfer");
829
830 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags);
831}
832
833/** Coro ********************************************************************/
834
835#define PRIO_MAX 3
836#define PRIO_HIGH 1
837#define PRIO_NORMAL 0
838#define PRIO_LOW -1
839#define PRIO_IDLE -3
840#define PRIO_MIN -4
841
842/* for Coro.pm */
843static GV *coro_current, *coro_idle;
844static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
845static int coro_nready;
846
847static void
848coro_enq (SV *sv)
849{
850 if (SvTYPE (sv) == SVt_PVHV)
851 {
852 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
853 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
854
855 prio = prio > PRIO_MAX ? PRIO_MAX
856 : prio < PRIO_MIN ? PRIO_MIN
857 : prio;
858
859 av_push (coro_ready [prio - PRIO_MIN], sv);
860 coro_nready++;
861
862 return;
863 }
864
865 croak ("Coro::ready tried to enqueue something that is not a coroutine");
866}
867
868static SV *
869coro_deq (int min_prio)
870{
871 int prio = PRIO_MAX - PRIO_MIN;
872
873 min_prio -= PRIO_MIN;
874 if (min_prio < 0)
875 min_prio = 0;
876
877 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
878 if (av_len (coro_ready[prio]) >= 0)
879 {
880 coro_nready--;
881 return av_shift (coro_ready[prio]);
882 }
883
884 return 0;
885}
886
887static void
888api_ready (SV *coro)
889{
890 if (SvROK (coro))
891 coro = SvRV (coro);
892
893 LOCK;
894 coro_enq (SvREFCNT_inc (coro));
895 UNLOCK;
896}
897
898static void
899api_schedule (void)
900{
901 SV *prev, *next;
902
903 LOCK;
904
905 prev = SvRV (GvSV (coro_current));
906 next = coro_deq (PRIO_MIN);
907
908 if (!next)
909 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
910
911 /* free this only after the transfer */
912 coro_mortal = prev;
913 SV_CORO (prev, "Coro::schedule");
914
915 SvRV (GvSV (coro_current)) = next;
916
917 SV_CORO (next, "Coro::schedule");
918
919 UNLOCK;
920
921 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
922 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
923}
924
925static void
926api_cede (void)
927{
928 LOCK;
929 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
930 UNLOCK;
931
932 api_schedule ();
751} 933}
752 934
753MODULE = Coro::State PACKAGE = Coro::State 935MODULE = Coro::State PACKAGE = Coro::State
754 936
755PROTOTYPES: ENABLE 937PROTOTYPES: ENABLE
756 938
757BOOT: 939BOOT:
758{ /* {} necessary for stoopid perl-5.6.x */ 940{ /* {} necessary for stoopid perl-5.6.x */
941#ifdef USE_ITHREADS
942 MUTEX_INIT (&coro_mutex);
943#endif
944
759 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 945 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
760 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 946 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
761 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 947 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
762 948
763 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 949 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
764 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 950 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
765 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 951 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
766 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); 952 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
767 953
768 if (!padlist_cache)
769 padlist_cache = newHV ();
770
771 main_mainstack = PL_mainstack; 954 main_mainstack = PL_mainstack;
955
956 coroapi.ver = CORO_API_VERSION;
957 coroapi.transfer = api_transfer;
772} 958}
773 959
774Coro::State 960Coro::State
775_newprocess(args) 961_newprocess(args)
776 SV * args 962 SV * args
779 Coro__State coro; 965 Coro__State coro;
780 966
781 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) 967 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV)
782 croak ("Coro::State::_newprocess expects an arrayref"); 968 croak ("Coro::State::_newprocess expects an arrayref");
783 969
784 New (0, coro, 1, struct coro); 970 Newz (0, coro, 1, struct coro);
785 971
786 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 972 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
787 coro->mainstack = 0; /* actual work is done inside transfer */ 973 coro->mainstack = 0; /* actual work is done inside transfer */
788 coro->stack = 0; 974 coro->stack = 0;
789 975
976 /* same as JMPENV_BOOTSTRAP */
977 /* we might be able to recycle start_env, but safe is safe */
978 //Zero(&coro->start_env, 1, JMPENV);
979 coro->start_env.je_ret = -1;
980 coro->start_env.je_mustcatch = TRUE;
981
790 RETVAL = coro; 982 RETVAL = coro;
791 OUTPUT: 983 OUTPUT:
792 RETVAL 984 RETVAL
793 985
794void 986void
795transfer(prev, next, flags = TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK) 987transfer(prev, next, flags)
796 Coro::State_or_hashref prev 988 SV *prev
797 Coro::State_or_hashref next 989 SV *next
798 int flags 990 int flags
799 PROTOTYPE: @ 991 PROTOTYPE: @
800 CODE: 992 CODE:
801 PUTBACK; 993 PUTBACK;
994 SV_CORO (next, "Coro::transfer");
995 SV_CORO (prev, "Coro::transfer");
802 transfer (aTHX_ prev, next, flags); 996 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
803 SPAGAIN; 997 SPAGAIN;
804 998
805void 999void
806DESTROY(coro) 1000DESTROY(coro)
807 Coro::State coro 1001 Coro::State coro
809 1003
810 if (coro->mainstack && coro->mainstack != main_mainstack) 1004 if (coro->mainstack && coro->mainstack != main_mainstack)
811 { 1005 {
812 struct coro temp; 1006 struct coro temp;
813 1007
1008 PUTBACK;
814 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 1009 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL);
815 LOAD(aTHX_ coro); 1010 LOAD(aTHX_ coro);
1011 SPAGAIN;
816 1012
817 destroy_stacks (aTHX); 1013 destroy_stacks (aTHX);
818 1014
819 LOAD((&temp)); /* this will get rid of defsv etc.. */ 1015 LOAD((&temp)); /* this will get rid of defsv etc.. */
1016 SPAGAIN;
820 1017
821 coro->mainstack = 0; 1018 coro->mainstack = 0;
822 } 1019 }
823 1020
824 deallocate_stack (coro); 1021 deallocate_stack (coro);
825 1022 SvREFCNT_dec (coro->args);
826 Safefree (coro); 1023 Safefree (coro);
827
828void
829flush()
830 CODE:
831#ifdef MAY_FLUSH
832 flush_padlist_cache ();
833#endif
834 1024
835void 1025void
836_exit(code) 1026_exit(code)
837 int code 1027 int code
838 PROTOTYPE: $ 1028 PROTOTYPE: $
839 CODE: 1029 CODE:
840#if defined(__GLIBC__) || _POSIX_C_SOURCE
841 _exit (code); 1030 _exit (code);
842#else
843 signal (SIGTERM, SIG_DFL);
844 raise (SIGTERM);
845 exit (code);
846#endif
847 1031
848MODULE = Coro::State PACKAGE = Coro::Cont 1032MODULE = Coro::State PACKAGE = Coro::Cont
849 1033
850# this is slightly dirty 1034# this is slightly dirty (should expose a c-level api)
851 1035
852void 1036void
853yield(...) 1037yield(...)
854 PROTOTYPE: @ 1038 PROTOTYPE: @
855 CODE: 1039 CODE:
857 SV *sv; 1041 SV *sv;
858 AV *defav = GvAV (PL_defgv); 1042 AV *defav = GvAV (PL_defgv);
859 struct coro *prev, *next; 1043 struct coro *prev, *next;
860 1044
861 if (!returnstk) 1045 if (!returnstk)
862 returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); 1046 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE));
863 1047
864 /* set up @_ -- ugly */ 1048 /* set up @_ -- ugly */
865 av_clear (defav); 1049 av_clear (defav);
866 av_fill (defav, items - 1); 1050 av_fill (defav, items - 1);
867 while (items--) 1051 while (items--)
873 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1057 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
874 SvREFCNT_dec (sv); 1058 SvREFCNT_dec (sv);
875 1059
876 transfer(aTHX_ prev, next, 0); 1060 transfer(aTHX_ prev, next, 0);
877 1061
1062MODULE = Coro::State PACKAGE = Coro
1063
1064# this is slightly dirty (should expose a c-level api)
1065
1066BOOT:
1067{
1068 int i;
1069 HV *stash = gv_stashpv ("Coro", TRUE);
1070
1071 newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX));
1072 newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1073 newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1074 newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW));
1075 newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1076 newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN));
1077
1078 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
1079 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1080
1081 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1082 coro_ready[i] = newAV ();
1083
1084 {
1085 SV *sv = perl_get_sv("Coro::API", 1);
1086
1087 coroapi.schedule = api_schedule;
1088 coroapi.cede = api_cede;
1089 coroapi.ready = api_ready;
1090 coroapi.nready = &coro_nready;
1091 coroapi.current = coro_current;
1092
1093 GCoroAPI = &coroapi;
1094 sv_setiv(sv, (IV)&coroapi);
1095 SvREADONLY_on(sv);
1096 }
1097}
1098
1099#if !PERL_MICRO
1100
1101void
1102ready(self)
1103 SV * self
1104 PROTOTYPE: $
1105 CODE:
1106 api_ready (self);
1107
1108#endif
1109
1110int
1111nready(...)
1112 PROTOTYPE:
1113 CODE:
1114 RETVAL = coro_nready;
1115 OUTPUT:
1116 RETVAL
1117
1118void
1119schedule(...)
1120 PROTOTYPE:
1121 CODE:
1122 api_schedule ();
1123
1124void
1125cede(...)
1126 PROTOTYPE:
1127 CODE:
1128 api_cede ();
1129

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines