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.44 by root, Sat Mar 22 18:50:32 2003 UTC vs.
Revision 1.61 by root, Tue Aug 10 01:56:30 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines