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.46 by pcg, Wed Nov 5 20:02:46 2003 UTC vs.
Revision 1.63 by root, Mon Mar 21 14:35:22 2005 UTC

1#define PERL_NO_GET_CONTEXT
2
3#include "libcoro/coro.c"
4
1#include "EXTERN.h" 5#include "EXTERN.h"
2#include "perl.h" 6#include "perl.h"
3#include "XSUB.h" 7#include "XSUB.h"
4 8
5#include "patchlevel.h" 9#include "patchlevel.h"
23# ifndef IS_PADCONST 27# ifndef IS_PADCONST
24# define IS_PADCONST(v) 0 28# define IS_PADCONST(v) 0
25# endif 29# endif
26#endif 30#endif
27 31
28#include "libcoro/coro.c"
29
30#include <signal.h> 32#include <signal.h>
31 33
32#ifdef HAVE_MMAP 34#ifdef HAVE_MMAP
33# include <unistd.h> 35# include <unistd.h>
34# include <sys/mman.h> 36# include <sys/mman.h>
39# undef HAVE_MMAP 41# undef HAVE_MMAP
40# endif 42# endif
41# endif 43# endif
42#endif 44#endif
43 45
44#define MAY_FLUSH /* increases codesize and is rarely used */
45
46#define SUB_INIT "Coro::State::initialize" 46#define SUB_INIT "Coro::State::initialize"
47#define UCORO_STATE "_coro_state" 47#define UCORO_STATE "_coro_state"
48 48
49/* 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
50 * 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
55 55
56#define labs(l) ((l) >= 0 ? (l) : -(l)) 56#define labs(l) ((l) >= 0 ? (l) : -(l))
57 57
58#include "CoroAPI.h" 58#include "CoroAPI.h"
59 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
60static 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 */
61 75
62/* 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... */
63typedef struct { 77typedef struct {
64 int refcnt; /* pointer reference counter */ 78 int refcnt; /* pointer reference counter */
65 int usecnt; /* shared by how many coroutines */ 79 int usecnt; /* shared by how many coroutines */
95 AV *mainstack; 109 AV *mainstack;
96 SV **stack_sp; 110 SV **stack_sp;
97 OP *op; 111 OP *op;
98 SV **curpad; 112 SV **curpad;
99 AV *comppad; 113 AV *comppad;
114 CV *compcv;
100 SV **stack_base; 115 SV **stack_base;
101 SV **stack_max; 116 SV **stack_max;
102 SV **tmps_stack; 117 SV **tmps_stack;
103 I32 tmps_floor; 118 I32 tmps_floor;
104 I32 tmps_ix; 119 I32 tmps_ix;
123}; 138};
124 139
125typedef struct coro *Coro__State; 140typedef struct coro *Coro__State;
126typedef struct coro *Coro__State_or_hashref; 141typedef struct coro *Coro__State_or_hashref;
127 142
128static AV *main_mainstack; /* used to differentiate between $main and others */
129static HV *coro_state_stash;
130static SV *ucoro_state_sv;
131static U32 ucoro_state_hash;
132static HV *padlist_cache;
133static SV *coro_mortal; /* will be freed after next transfer */
134
135/* mostly copied from op.c:cv_clone2 */ 143/* mostly copied from op.c:cv_clone2 */
136STATIC AV * 144STATIC AV *
137clone_padlist (AV *protopadlist) 145clone_padlist (pTHX_ AV *protopadlist)
138{ 146{
139 AV *av; 147 AV *av;
140 I32 ix; 148 I32 ix;
141 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); 149 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE);
142 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); 150 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE);
166 AvFLAGS (av) = AVf_REIFY; 174 AvFLAGS (av) = AVf_REIFY;
167 175
168 for (ix = fpad; ix > 0; ix--) 176 for (ix = fpad; ix > 0; ix--)
169 { 177 {
170 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; 178 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv;
179
171 if (namesv && namesv != &PL_sv_undef) 180 if (namesv && namesv != &PL_sv_undef)
172 { 181 {
173 char *name = SvPVX (namesv); /* XXX */ 182 char *name = SvPVX (namesv); /* XXX */
183
174 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') 184 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&')
175 { /* lexical from outside? */ 185 { /* lexical from outside? */
176 npad[ix] = SvREFCNT_inc (ppad[ix]); 186 npad[ix] = SvREFCNT_inc (ppad[ix]);
177 } 187 }
178 else 188 else
184 sv = (SV *) newAV (); 194 sv = (SV *) newAV ();
185 else if (*name == '%') 195 else if (*name == '%')
186 sv = (SV *) newHV (); 196 sv = (SV *) newHV ();
187 else 197 else
188 sv = NEWSV (0, 0); 198 sv = NEWSV (0, 0);
199
200#ifdef SvPADBUSY
189 if (!SvPADBUSY (sv)) 201 if (!SvPADBUSY (sv))
202#endif
190 SvPADMY_on (sv); 203 SvPADMY_on (sv);
204
191 npad[ix] = sv; 205 npad[ix] = sv;
192 } 206 }
193 } 207 }
194 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) 208 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix]))
195 { 209 {
224#endif 238#endif
225 239
226 return newpadlist; 240 return newpadlist;
227} 241}
228 242
229#ifdef MAY_FLUSH
230STATIC void 243STATIC void
231free_padlist (AV *padlist) 244free_padlist (pTHX_ AV *padlist)
232{ 245{
233 /* may be during global destruction */ 246 /* may be during global destruction */
234 if (SvREFCNT(padlist)) 247 if (SvREFCNT (padlist))
235 { 248 {
236 I32 i = AvFILLp(padlist); 249 I32 i = AvFILLp (padlist);
237 while (i >= 0) 250 while (i >= 0)
238 { 251 {
239 SV **svp = av_fetch(padlist, i--, FALSE); 252 SV **svp = av_fetch (padlist, i--, FALSE);
240 SV *sv = svp ? *svp : Nullsv;
241 if (sv) 253 if (svp)
254 {
255 SV *sv;
256 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp)))
242 SvREFCNT_dec(sv); 257 SvREFCNT_dec (sv);
258
259 SvREFCNT_dec (*svp);
260 }
243 } 261 }
244 262
245 SvREFCNT_dec((SV*)padlist); 263 SvREFCNT_dec ((SV*)padlist);
246 } 264 }
247} 265}
248#endif 266
267STATIC int
268coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
269{
270 AV *padlist;
271 AV *av = (AV *)mg->mg_obj;
272
273 /* casting is fun. */
274 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
275 free_padlist (aTHX_ padlist);
276
277 SvREFCNT_dec (av);
278}
279
280#define PERL_MAGIC_coro PERL_MAGIC_ext
281
282static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
249 283
250/* the next two functions merely cache the padlists */ 284/* the next two functions merely cache the padlists */
251STATIC void 285STATIC void
252get_padlist (CV *cv) 286get_padlist (pTHX_ CV *cv)
253{ 287{
254 SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 0); 288 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
255 289
256 if (he && AvFILLp ((AV *)*he) >= 0) 290 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
257 CvPADLIST (cv) = (AV *)av_pop ((AV *)*he); 291 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
258 else 292 else
259 CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); 293 CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv));
260} 294}
261 295
262STATIC void 296STATIC void
263put_padlist (CV *cv) 297put_padlist (pTHX_ CV *cv)
264{ 298{
265 SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 1); 299 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
266 300
267 if (SvTYPE (*he) != SVt_PVAV) 301 if (!mg)
268 { 302 {
269 SvREFCNT_dec (*he); 303 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
304 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
305 mg->mg_virtual = &vtbl_coro;
270 *he = (SV *)newAV (); 306 mg->mg_obj = (SV *)newAV ();
271 } 307 }
272 308
273 av_push ((AV *)*he, (SV *)CvPADLIST (cv)); 309 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv));
274} 310}
275
276#ifdef MAY_FLUSH
277STATIC void
278flush_padlist_cache ()
279{
280 HV *hv = padlist_cache;
281 padlist_cache = newHV ();
282
283 if (hv_iterinit (hv))
284 {
285 HE *he;
286 AV *padlist;
287
288 while (!!(he = hv_iternext (hv)))
289 {
290 AV *av = (AV *)HeVAL(he);
291
292 /* casting is fun. */
293 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
294 free_padlist (padlist);
295 }
296 }
297
298 SvREFCNT_dec (hv);
299}
300#endif
301 311
302#define SB do { 312#define SB do {
303#define SE } while (0) 313#define SE } while (0)
304 314
305#define LOAD(state) load_state(aTHX_ (state)); 315#define LOAD(state) load_state(aTHX_ (state));
306#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); 316#define SAVE(state,flags) save_state(aTHX_ (state),(flags));
307 317
308#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE 318#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
309 319
310static void 320static void
311load_state(pTHX_ Coro__State c) 321load_state(pTHX_ Coro__State c)
312{ 322{
313 PL_dowarn = c->dowarn; 323 PL_dowarn = c->dowarn;
318 PL_mainstack = c->mainstack; 328 PL_mainstack = c->mainstack;
319 PL_stack_sp = c->stack_sp; 329 PL_stack_sp = c->stack_sp;
320 PL_op = c->op; 330 PL_op = c->op;
321 PL_curpad = c->curpad; 331 PL_curpad = c->curpad;
322 PL_comppad = c->comppad; 332 PL_comppad = c->comppad;
333 PL_compcv = c->compcv;
323 PL_stack_base = c->stack_base; 334 PL_stack_base = c->stack_base;
324 PL_stack_max = c->stack_max; 335 PL_stack_max = c->stack_max;
325 PL_tmps_stack = c->tmps_stack; 336 PL_tmps_stack = c->tmps_stack;
326 PL_tmps_floor = c->tmps_floor; 337 PL_tmps_floor = c->tmps_floor;
327 PL_tmps_ix = c->tmps_ix; 338 PL_tmps_ix = c->tmps_ix;
354 { 365 {
355 AV *padlist = (AV *)POPs; 366 AV *padlist = (AV *)POPs;
356 367
357 if (padlist) 368 if (padlist)
358 { 369 {
359 put_padlist (cv); /* mark this padlist as available */ 370 put_padlist (aTHX_ cv); /* mark this padlist as available */
360 CvPADLIST(cv) = padlist; 371 CvPADLIST(cv) = padlist;
361#ifdef USE_THREADS
362 /*CvOWNER(cv) = (struct perl_thread *)POPs;*/
363#endif
364 } 372 }
365 373
366 ++CvDEPTH(cv); 374 ++CvDEPTH(cv);
367 } 375 }
368 376
395 if (CxTYPE(cx) == CXt_SUB) 403 if (CxTYPE(cx) == CXt_SUB)
396 { 404 {
397 CV *cv = cx->blk_sub.cv; 405 CV *cv = cx->blk_sub.cv;
398 if (CvDEPTH(cv)) 406 if (CvDEPTH(cv))
399 { 407 {
400#ifdef USE_THREADS
401 /*XPUSHs ((SV *)CvOWNER(cv));*/
402 /*CvOWNER(cv) = 0;*/
403 /*error must unlock this cv etc.. etc...*/
404#endif
405 EXTEND (SP, CvDEPTH(cv)*2); 408 EXTEND (SP, CvDEPTH(cv)*2);
406 409
407 while (--CvDEPTH(cv)) 410 while (--CvDEPTH(cv))
408 { 411 {
409 /* this tells the restore code to increment CvDEPTH */ 412 /* this tells the restore code to increment CvDEPTH */
412 } 415 }
413 416
414 PUSHs ((SV *)CvPADLIST(cv)); 417 PUSHs ((SV *)CvPADLIST(cv));
415 PUSHs ((SV *)cv); 418 PUSHs ((SV *)cv);
416 419
417 get_padlist (cv); /* this is a monster */ 420 get_padlist (aTHX_ cv); /* this is a monster */
418 } 421 }
419 } 422 }
420#ifdef CXt_FORMAT 423#ifdef CXt_FORMAT
421 else if (CxTYPE(cx) == CXt_FORMAT) 424 else if (CxTYPE(cx) == CXt_FORMAT)
422 { 425 {
450 c->mainstack = PL_mainstack; 453 c->mainstack = PL_mainstack;
451 c->stack_sp = PL_stack_sp; 454 c->stack_sp = PL_stack_sp;
452 c->op = PL_op; 455 c->op = PL_op;
453 c->curpad = PL_curpad; 456 c->curpad = PL_curpad;
454 c->comppad = PL_comppad; 457 c->comppad = PL_comppad;
458 c->compcv = PL_compcv;
455 c->stack_base = PL_stack_base; 459 c->stack_base = PL_stack_base;
456 c->stack_max = PL_stack_max; 460 c->stack_max = PL_stack_max;
457 c->tmps_stack = PL_tmps_stack; 461 c->tmps_stack = PL_tmps_stack;
458 c->tmps_floor = PL_tmps_floor; 462 c->tmps_floor = PL_tmps_floor;
459 c->tmps_ix = PL_tmps_ix; 463 c->tmps_ix = PL_tmps_ix;
475} 479}
476 480
477/* 481/*
478 * allocate various perl stacks. This is an exact copy 482 * allocate various perl stacks. This is an exact copy
479 * of perl.c:init_stacks, except that it uses less memory 483 * of perl.c:init_stacks, except that it uses less memory
480 * on the assumption that coroutines do not usually need 484 * on the (sometimes correct) assumption that coroutines do
481 * a lot of stackspace. 485 * not usually need a lot of stackspace.
482 */ 486 */
483STATIC void 487STATIC void
484coro_init_stacks (pTHX) 488coro_init_stacks (pTHX)
485{ 489{
490 LOCK;
491
486 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 492 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
487 PL_curstackinfo->si_type = PERLSI_MAIN; 493 PL_curstackinfo->si_type = PERLSI_MAIN;
488 PL_curstack = PL_curstackinfo->si_stack; 494 PL_curstack = PL_curstackinfo->si_stack;
489 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 495 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
490 496
514 PL_savestack_max = 96; 520 PL_savestack_max = 96;
515 521
516 New(54,PL_retstack,8,OP*); 522 New(54,PL_retstack,8,OP*);
517 PL_retstack_ix = 0; 523 PL_retstack_ix = 0;
518 PL_retstack_max = 8; 524 PL_retstack_max = 8;
525
526 UNLOCK;
519} 527}
520 528
521/* 529/*
522 * destroy the stacks, the callchain etc... 530 * destroy the stacks, the callchain etc...
523 * still there is a memleak of 128 bytes...
524 */ 531 */
525STATIC void 532STATIC void
526destroy_stacks(pTHX) 533destroy_stacks(pTHX)
527{ 534{
528 if (!IN_DESTRUCT) 535 if (!IN_DESTRUCT)
529 { 536 {
530 /* is this ugly, I ask? */ 537 /* is this ugly, I ask? */
531 while (PL_scopestack_ix) 538 LEAVE_SCOPE (0);
532 LEAVE;
533 539
534 /* sure it is, but more important: is it correct?? :/ */ 540 /* sure it is, but more important: is it correct?? :/ */
535 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
536 FREETMPS; 541 FREETMPS;
542
543 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/
537 } 544 }
538 545
539 while (PL_curstackinfo->si_next) 546 while (PL_curstackinfo->si_next)
540 PL_curstackinfo = PL_curstackinfo->si_next; 547 PL_curstackinfo = PL_curstackinfo->si_next;
541 548
542 while (PL_curstackinfo) 549 while (PL_curstackinfo)
543 { 550 {
544 PERL_SI *p = PL_curstackinfo->si_prev; 551 PERL_SI *p = PL_curstackinfo->si_prev;
545 552
546 { 553 { /*D*//*remove*/
547 dSP; 554 dSP;
548 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 555 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
549 PUTBACK; /* possibly superfluous */ 556 PUTBACK; /* possibly superfluous */
550 } 557 }
551 558
552 if (!IN_DESTRUCT) 559 if (!IN_DESTRUCT)
553 { 560 {
554 dounwind(-1); 561 dounwind (-1);/*D*//*remove*/
555 SvREFCNT_dec(PL_curstackinfo->si_stack); 562 SvREFCNT_dec (PL_curstackinfo->si_stack);
556 } 563 }
557 564
558 Safefree(PL_curstackinfo->si_cxstack); 565 Safefree (PL_curstackinfo->si_cxstack);
559 Safefree(PL_curstackinfo); 566 Safefree (PL_curstackinfo);
560 PL_curstackinfo = p; 567 PL_curstackinfo = p;
561 } 568 }
562 569
563 Safefree(PL_tmps_stack); 570 Safefree (PL_tmps_stack);
564 Safefree(PL_markstack); 571 Safefree (PL_markstack);
565 Safefree(PL_scopestack); 572 Safefree (PL_scopestack);
566 Safefree(PL_savestack); 573 Safefree (PL_savestack);
567 Safefree(PL_retstack); 574 Safefree (PL_retstack);
568} 575}
569 576
570static void 577static void
571allocate_stack (Coro__State ctx, int alloc) 578allocate_stack (Coro__State ctx, int alloc)
572{ 579{
575 New (0, stack, 1, coro_stack); 582 New (0, stack, 1, coro_stack);
576 583
577 stack->refcnt = 1; 584 stack->refcnt = 1;
578 stack->usecnt = 1; 585 stack->usecnt = 1;
579 stack->gencnt = ctx->gencnt = 0; 586 stack->gencnt = ctx->gencnt = 0;
587
580 if (alloc) 588 if (alloc)
581 { 589 {
582#if HAVE_MMAP 590#if HAVE_MMAP
583 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ 591 stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */
584 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 592 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
585 if (stack->sptr == (void *)-1) 593 if (stack->sptr == (void *)-1)
586#endif 594#endif
587 { 595 {
588 /*FIXME*//*D*//* reasonable stack size! */ 596 /*FIXME*//*D*//* reasonable stack size! */
589 stack->ssize = -4096 * sizeof (long); 597 stack->ssize = - (8192 * sizeof (long));
590 New (0, stack->sptr, 4096, long); 598 New (0, stack->sptr, 8192, long);
591 } 599 }
592 } 600 }
593 else 601 else
594 stack->sptr = 0; 602 stack->sptr = 0;
595 603
609 { 617 {
610#ifdef HAVE_MMAP 618#ifdef HAVE_MMAP
611 if (stack->ssize > 0 && stack->sptr) 619 if (stack->ssize > 0 && stack->sptr)
612 munmap (stack->sptr, stack->ssize); 620 munmap (stack->sptr, stack->ssize);
613 else 621 else
614#else 622#endif
615 Safefree (stack->sptr); 623 Safefree (stack->sptr);
616#endif 624
617 Safefree (stack); 625 Safefree (stack);
618 } 626 }
619 else if (ctx->gencnt == stack->gencnt) 627 else if (ctx->gencnt == stack->gencnt)
620 --stack->usecnt; 628 --stack->usecnt;
621 } 629 }
625setup_coro (void *arg) 633setup_coro (void *arg)
626{ 634{
627 /* 635 /*
628 * emulate part of the perl startup here. 636 * emulate part of the perl startup here.
629 */ 637 */
638 dTHX;
630 dSP; 639 dSP;
631 Coro__State ctx = (Coro__State)arg; 640 Coro__State ctx = (Coro__State)arg;
632 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 641 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE);
633 642
634 coro_init_stacks (aTHX); 643 coro_init_stacks (aTHX);
635 /*PL_curcop = 0;*/ 644 /*PL_curcop = 0;*/
636 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 645 /*PL_in_eval = PL_in_eval;*/ /* inherit */
637 SvREFCNT_dec (GvAV (PL_defgv)); 646 SvREFCNT_dec (GvAV (PL_defgv));
638 GvAV (PL_defgv) = ctx->args; 647 GvAV (PL_defgv) = ctx->args; ctx->args = 0;
639 648
640 SPAGAIN; 649 SPAGAIN;
641 650
642 if (ctx->stack) 651 if (ctx->stack)
643 { 652 {
683continue_coro (void *arg) 692continue_coro (void *arg)
684{ 693{
685 /* 694 /*
686 * this is a _very_ stripped down perl interpreter ;) 695 * this is a _very_ stripped down perl interpreter ;)
687 */ 696 */
697 dTHX;
688 Coro__State ctx = (Coro__State)arg; 698 Coro__State ctx = (Coro__State)arg;
689 JMPENV coro_start_env; 699 JMPENV coro_start_env;
690 700
691 PL_top_env = &ctx->start_env; 701 PL_top_env = &ctx->start_env;
692 702
696 706
697 abort (); 707 abort ();
698} 708}
699 709
700STATIC void 710STATIC void
701transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 711transfer (pTHX_ struct coro *prev, struct coro *next, int flags)
702{ 712{
703 dSTACKLEVEL; 713 dSTACKLEVEL;
704 static struct coro *xnext;
705 714
706 if (prev != next) 715 if (prev != next)
707 { 716 {
708 xnext = next;
709
710 if (next->mainstack) 717 if (next->mainstack)
711 { 718 {
719 LOCK;
712 SAVE (prev, flags); 720 SAVE (prev, flags);
713 LOAD (next); 721 LOAD (next);
722 UNLOCK;
714 723
715 /* mark this state as in-use */ 724 /* mark this state as in-use */
716 next->mainstack = 0; 725 next->mainstack = 0;
717 next->tmps_ix = -2; 726 next->tmps_ix = -2;
718 727
737 continue_coro, (void *)next, 746 continue_coro, (void *)next,
738 next->stack->sptr, labs (next->stack->ssize)); 747 next->stack->sptr, labs (next->stack->ssize));
739 } 748 }
740 749
741 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 750 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
751 prev->cursp = stacklevel;
742 /* don't add any code here */ 752 /* don't add any code here */
743 } 753 }
744 754 else
755 next->cursp = stacklevel;
745 } 756 }
746 else if (next->tmps_ix == -2) 757 else if (next->tmps_ix == -2)
747 croak ("tried to transfer to running coroutine"); 758 croak ("tried to transfer to running coroutine");
748 else 759 else
749 { 760 {
761 LOCK;
750 SAVE (prev, -1); /* first get rid of the old state */ 762 SAVE (prev, -1); /* first get rid of the old state */
763 UNLOCK;
751 764
752 if (flags & TRANSFER_SAVE_CCTXT) 765 if (flags & TRANSFER_SAVE_CCTXT)
753 { 766 {
754 if (!prev->stack) 767 if (!prev->stack)
755 allocate_stack (prev, 0); 768 allocate_stack (prev, 0);
757 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 770 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
758 { 771 {
759 PL_top_env = &next->start_env; 772 PL_top_env = &next->start_env;
760 773
761 setup_coro (next); 774 setup_coro (next);
775 next->cursp = stacklevel;
762 776
763 prev->stack->refcnt++; 777 prev->stack->refcnt++;
764 prev->stack->usecnt++; 778 prev->stack->usecnt++;
765 next->stack = prev->stack; 779 next->stack = prev->stack;
766 next->gencnt = prev->gencnt; 780 next->gencnt = prev->gencnt;
767 } 781 }
768 else 782 else
769 { 783 {
784 assert (!next->stack);
770 allocate_stack (next, 1); 785 allocate_stack (next, 1);
771 coro_create (&(next->stack->cctx), 786 coro_create (&(next->stack->cctx),
772 setup_coro, (void *)next, 787 setup_coro, (void *)next,
773 next->stack->sptr, labs (next->stack->ssize)); 788 next->stack->sptr, labs (next->stack->ssize));
774 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 789 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
790 prev->cursp = stacklevel;
775 /* don't add any code here */ 791 /* don't add any code here */
776 } 792 }
777 } 793 }
778 else 794 else
795 {
779 setup_coro (next); 796 setup_coro (next);
797 next->cursp = stacklevel;
798 }
780 } 799 }
781
782 /*
783 * xnext is now either prev or next, depending on wether
784 * we switched the c stack or not. that's why I use a global
785 * variable, that should become thread-specific at one point.
786 */
787 xnext->cursp = stacklevel;
788 } 800 }
789 801
802 LOCK;
790 if (coro_mortal) 803 if (coro_mortal)
791 { 804 {
792 SvREFCNT_dec (coro_mortal); 805 SvREFCNT_dec (coro_mortal);
793 coro_mortal = 0; 806 coro_mortal = 0;
794 } 807 }
808 UNLOCK;
795} 809}
796 810
797#define SV_CORO(sv,func) \ 811#define SV_CORO(sv,func) \
798 do { \ 812 do { \
799 if (SvROK (sv)) \ 813 if (SvROK (sv)) \
800 sv = SvRV (sv); \ 814 sv = SvRV (sv); \
801 \ 815 \
802 if (SvTYPE(sv) == SVt_PVHV) \ 816 if (SvTYPE (sv) == SVt_PVHV) \
803 { \ 817 { \
804 HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ 818 HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
805 \ 819 \
806 if (!he) \ 820 if (!he) \
807 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ 821 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
808 \ 822 \
809 (sv) = SvRV (HeVAL(he)); \ 823 (sv) = SvRV (HeVAL(he)); \
810 } \ 824 } \
811 \ 825 \
812 /* must also be changed inside Coro::Cont::yield */ \ 826 /* must also be changed inside Coro::Cont::yield */ \
813 if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ 827 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
814 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ 828 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
815 \ 829 \
816 } while(0) 830 } while(0)
817 831
818#define SvSTATE(sv) (struct coro *)SvIV (sv) 832#define SvSTATE(sv) (struct coro *)SvIV (sv)
821api_transfer(pTHX_ SV *prev, SV *next, int flags) 835api_transfer(pTHX_ SV *prev, SV *next, int flags)
822{ 836{
823 SV_CORO (prev, "Coro::transfer"); 837 SV_CORO (prev, "Coro::transfer");
824 SV_CORO (next, "Coro::transfer"); 838 SV_CORO (next, "Coro::transfer");
825 839
826 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); 840 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
827} 841}
828 842
829/** Coro ********************************************************************/ 843/** Coro ********************************************************************/
830 844
831#define PRIO_MAX 3 845#define PRIO_MAX 3
839static GV *coro_current, *coro_idle; 853static GV *coro_current, *coro_idle;
840static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 854static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
841static int coro_nready; 855static int coro_nready;
842 856
843static void 857static void
844coro_enq (SV *sv) 858coro_enq (pTHX_ SV *sv)
845{ 859{
846 if (SvTYPE (sv) == SVt_PVHV) 860 if (SvTYPE (sv) == SVt_PVHV)
847 { 861 {
848 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); 862 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
849 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 863 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
860 874
861 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 875 croak ("Coro::ready tried to enqueue something that is not a coroutine");
862} 876}
863 877
864static SV * 878static SV *
865coro_deq (int min_prio) 879coro_deq (pTHX_ int min_prio)
866{ 880{
867 int prio = PRIO_MAX - PRIO_MIN; 881 int prio = PRIO_MAX - PRIO_MIN;
868 882
869 min_prio -= PRIO_MIN; 883 min_prio -= PRIO_MIN;
870 if (min_prio < 0) 884 if (min_prio < 0)
881} 895}
882 896
883static void 897static void
884api_ready (SV *coro) 898api_ready (SV *coro)
885{ 899{
900 dTHX;
901
886 if (SvROK (coro)) 902 if (SvROK (coro))
887 coro = SvRV (coro); 903 coro = SvRV (coro);
888 904
905 LOCK;
889 coro_enq (SvREFCNT_inc (coro)); 906 coro_enq (aTHX_ SvREFCNT_inc (coro));
907 UNLOCK;
890} 908}
891 909
892static void 910static void
893api_schedule (void) 911api_schedule (void)
894{ 912{
913 dTHX;
914
895 SV *prev, *next; 915 SV *prev, *next;
896 916
917 LOCK;
918
897 prev = SvRV (GvSV (coro_current)); 919 prev = SvRV (GvSV (coro_current));
898 next = coro_deq (PRIO_MIN); 920 next = coro_deq (aTHX_ PRIO_MIN);
899 921
900 if (!next) 922 if (!next)
901 next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); 923 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
902 924
903 /* free this only after the transfer */ 925 /* free this only after the transfer */
906 928
907 SvRV (GvSV (coro_current)) = next; 929 SvRV (GvSV (coro_current)) = next;
908 930
909 SV_CORO (next, "Coro::schedule"); 931 SV_CORO (next, "Coro::schedule");
910 932
933 UNLOCK;
934
911 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), 935 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
912 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 936 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
913} 937}
914 938
915static void 939static void
916api_cede (void) 940api_cede (void)
917{ 941{
942 dTHX;
943
944 LOCK;
918 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); 945 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current))));
946 UNLOCK;
919 947
920 api_schedule (); 948 api_schedule ();
921} 949}
922 950
923MODULE = Coro::State PACKAGE = Coro::State 951MODULE = Coro::State PACKAGE = Coro::State
924 952
925PROTOTYPES: ENABLE 953PROTOTYPES: ENABLE
926 954
927BOOT: 955BOOT:
928{ /* {} necessary for stoopid perl-5.6.x */ 956{ /* {} necessary for stoopid perl-5.6.x */
957#ifdef USE_ITHREADS
958 MUTEX_INIT (&coro_mutex);
959#endif
960
929 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 961 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
930 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 962 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
931 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 963 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
932 964
933 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 965 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
934 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 966 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
935 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 967 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
936 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); 968 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
937
938 if (!padlist_cache)
939 padlist_cache = newHV ();
940 969
941 main_mainstack = PL_mainstack; 970 main_mainstack = PL_mainstack;
942 971
943 coroapi.ver = CORO_API_VERSION; 972 coroapi.ver = CORO_API_VERSION;
944 coroapi.transfer = api_transfer; 973 coroapi.transfer = api_transfer;
952 Coro__State coro; 981 Coro__State coro;
953 982
954 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) 983 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV)
955 croak ("Coro::State::_newprocess expects an arrayref"); 984 croak ("Coro::State::_newprocess expects an arrayref");
956 985
957 New (0, coro, 1, struct coro); 986 Newz (0, coro, 1, struct coro);
958 987
959 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 988 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
960 coro->mainstack = 0; /* actual work is done inside transfer */ 989 /*coro->mainstack = 0; *//*actual work is done inside transfer */
961 coro->stack = 0; 990 /*coro->stack = 0;*/
962 991
963 /* same as JMPENV_BOOTSTRAP */ 992 /* same as JMPENV_BOOTSTRAP */
964 /* we might be able to recycle start_env, but safe is safe */ 993 /* we might be able to recycle start_env, but safe is safe */
965 Zero(&coro->start_env, 1, JMPENV); 994 /*Zero(&coro->start_env, 1, JMPENV);*/
966 coro->start_env.je_ret = -1; 995 coro->start_env.je_ret = -1;
967 coro->start_env.je_mustcatch = TRUE; 996 coro->start_env.je_mustcatch = TRUE;
968 997
969 RETVAL = coro; 998 RETVAL = coro;
970 OUTPUT: 999 OUTPUT:
991 if (coro->mainstack && coro->mainstack != main_mainstack) 1020 if (coro->mainstack && coro->mainstack != main_mainstack)
992 { 1021 {
993 struct coro temp; 1022 struct coro temp;
994 1023
995 PUTBACK; 1024 PUTBACK;
996 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 1025 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
997 LOAD(aTHX_ coro); 1026 LOAD (aTHX_ coro);
998 SPAGAIN; 1027 SPAGAIN;
999 1028
1000 destroy_stacks (aTHX); 1029 destroy_stacks (aTHX);
1001 1030
1002 LOAD((&temp)); /* this will get rid of defsv etc.. */ 1031 LOAD ((&temp)); /* this will get rid of defsv etc.. */
1003 SPAGAIN; 1032 SPAGAIN;
1004 1033
1005 coro->mainstack = 0; 1034 coro->mainstack = 0;
1006 } 1035 }
1007 1036
1008 deallocate_stack (coro); 1037 deallocate_stack (coro);
1009 1038 SvREFCNT_dec (coro->args);
1010 Safefree (coro); 1039 Safefree (coro);
1011
1012void
1013flush()
1014 CODE:
1015#ifdef MAY_FLUSH
1016 flush_padlist_cache ();
1017#endif
1018 1040
1019void 1041void
1020_exit(code) 1042_exit(code)
1021 int code 1043 int code
1022 PROTOTYPE: $ 1044 PROTOTYPE: $
1023 CODE: 1045 CODE:
1024#if defined(__GLIBC__) || _POSIX_C_SOURCE
1025 _exit (code); 1046 _exit (code);
1026#else
1027 signal (SIGTERM, SIG_DFL);
1028 raise (SIGTERM);
1029 exit (code);
1030#endif
1031 1047
1032MODULE = Coro::State PACKAGE = Coro::Cont 1048MODULE = Coro::State PACKAGE = Coro::Cont
1033 1049
1034# this is slightly dirty (should expose a c-level api) 1050# this is slightly dirty (should expose a c-level api)
1035 1051
1049 av_clear (defav); 1065 av_clear (defav);
1050 av_fill (defav, items - 1); 1066 av_fill (defav, items - 1);
1051 while (items--) 1067 while (items--)
1052 av_store (defav, items, SvREFCNT_inc (ST(items))); 1068 av_store (defav, items, SvREFCNT_inc (ST(items)));
1053 1069
1054 mg_get (returnstk); /* isn't documentation wrong for mg_get? */ 1070 SvGETMAGIC (returnstk); /* isn't documentation wrong for mg_get? */
1055 sv = av_pop ((AV *)SvRV (returnstk)); 1071 sv = av_pop ((AV *)SvRV (returnstk));
1056 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 1072 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)));
1057 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1073 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
1058 SvREFCNT_dec (sv); 1074 SvREFCNT_dec (sv);
1059 1075
1060 transfer(aTHX_ prev, next, 0); 1076 transfer (aTHX_ prev, next, 0);
1061 1077
1062MODULE = Coro::State PACKAGE = Coro 1078MODULE = Coro::State PACKAGE = Coro
1063 1079
1064# this is slightly dirty (should expose a c-level api) 1080# this is slightly dirty (should expose a c-level api)
1065 1081
1094 sv_setiv(sv, (IV)&coroapi); 1110 sv_setiv(sv, (IV)&coroapi);
1095 SvREADONLY_on(sv); 1111 SvREADONLY_on(sv);
1096 } 1112 }
1097} 1113}
1098 1114
1115#if !PERL_MICRO
1116
1099void 1117void
1100ready(self) 1118ready(self)
1101 SV * self 1119 SV * self
1102 PROTOTYPE: $ 1120 PROTOTYPE: $
1103 CODE: 1121 CODE:
1104 api_ready (self); 1122 api_ready (self);
1123
1124#endif
1105 1125
1106int 1126int
1107nready(...) 1127nready(...)
1108 PROTOTYPE: 1128 PROTOTYPE:
1109 CODE: 1129 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines