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.108 by root, Mon Nov 27 18:15:47 2006 UTC vs.
Revision 1.123 by root, Mon Dec 4 21:56:00 2006 UTC

7#include "patchlevel.h" 7#include "patchlevel.h"
8 8
9#if USE_VALGRIND 9#if USE_VALGRIND
10# include <valgrind/valgrind.h> 10# include <valgrind/valgrind.h>
11#endif 11#endif
12
13/* the maximum number of idle cctx that will be pooled */
14#define MAX_IDLE_CCTX 8
12 15
13#define PERL_VERSION_ATLEAST(a,b,c) \ 16#define PERL_VERSION_ATLEAST(a,b,c) \
14 (PERL_REVISION > (a) \ 17 (PERL_REVISION > (a) \
15 || (PERL_REVISION == (a) \ 18 || (PERL_REVISION == (a) \
16 && (PERL_VERSION > (b) \ 19 && (PERL_VERSION > (b) \
87 90
88#define NOINLINE attribute ((noinline)) 91#define NOINLINE attribute ((noinline))
89 92
90#include "CoroAPI.h" 93#include "CoroAPI.h"
91 94
92#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
93
94#ifdef USE_ITHREADS 95#ifdef USE_ITHREADS
95static perl_mutex coro_mutex; 96static perl_mutex coro_mutex;
96# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 97# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
97# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 98# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
98#else 99#else
99# define LOCK (void)0 100# define LOCK (void)0
100# define UNLOCK (void)0 101# define UNLOCK (void)0
101#endif 102#endif
102 103
104struct io_state
105{
106 int errorno;
107 I32 laststype;
108 int laststatval;
109 Stat_t statcache;
110};
111
103static struct CoroAPI coroapi; 112static struct CoroAPI coroapi;
104static AV *main_mainstack; /* used to differentiate between $main and others */ 113static AV *main_mainstack; /* used to differentiate between $main and others */
105static HV *coro_state_stash, *coro_stash; 114static HV *coro_state_stash, *coro_stash;
106static SV *coro_mortal; /* will be freed after next transfer */ 115static SV *coro_mortal; /* will be freed after next transfer */
107 116
119 /* cpu state */ 128 /* cpu state */
120 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 129 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
121 JMPENV *top_env; 130 JMPENV *top_env;
122 coro_context cctx; 131 coro_context cctx;
123 132
133 int inuse;
134
124#if USE_VALGRIND 135#if USE_VALGRIND
125 int valgrind_id; 136 int valgrind_id;
126#endif 137#endif
127} coro_cctx; 138} coro_cctx;
139
140enum {
141 CF_RUNNING = 0x0001, /* coroutine is running */
142 CF_READY = 0x0002, /* coroutine is ready */
143 CF_NEW = 0x0004, /* ahs never been switched to */
144};
128 145
129/* this is a structure representing a perl-level coroutine */ 146/* this is a structure representing a perl-level coroutine */
130struct coro { 147struct coro {
131 /* the c coroutine allocated to this perl coroutine, if any */ 148 /* the c coroutine allocated to this perl coroutine, if any */
132 coro_cctx *cctx; 149 coro_cctx *cctx;
133 150
134 /* data associated with this coroutine (initial args) */ 151 /* data associated with this coroutine (initial args) */
135 AV *args; 152 AV *args;
136 int refcnt; 153 int refcnt;
154 int save; /* CORO_SAVE flags */
155 int flags; /* CF_ flags */
137 156
138 /* optionally saved, might be zero */ 157 /* optionally saved, might be zero */
139 AV *defav; 158 AV *defav; /* @_ */
140 SV *defsv; 159 SV *defsv; /* $_ */
141 SV *errsv; 160 SV *errsv; /* $@ */
161 SV *irssv; /* $/ */
162 SV *irssv_sv; /* real $/ cache */
142 163
143#define VAR(name,type) type name; 164#define VAR(name,type) type name;
144# include "state.h" 165# include "state.h"
145#undef VAR 166#undef VAR
146 167
214 235
215#define PERL_MAGIC_coro PERL_MAGIC_ext 236#define PERL_MAGIC_coro PERL_MAGIC_ext
216 237
217static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 238static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
218 239
240#define CORO_MAGIC(cv) \
241 SvMAGIC (cv) \
242 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
243 ? SvMAGIC (cv) \
244 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
245 : 0
246
219/* the next two functions merely cache the padlists */ 247/* the next two functions merely cache the padlists */
220static void 248static void
221get_padlist (CV *cv) 249get_padlist (CV *cv)
222{ 250{
223 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 251 MAGIC *mg = CORO_MAGIC (cv);
252 AV *av;
224 253
225 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 254 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
226 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 255 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
227 else 256 else
228 { 257 {
229#if 0 258#if 0
230 /* this is probably cleaner, but also slower? */ 259 /* this is probably cleaner, but also slower? */
231 CV *cp = Perl_cv_clone (cv); 260 CV *cp = Perl_cv_clone (cv);
239} 268}
240 269
241static void 270static void
242put_padlist (CV *cv) 271put_padlist (CV *cv)
243{ 272{
244 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 273 MAGIC *mg = CORO_MAGIC (cv);
274 AV *av;
245 275
246 if (!mg) 276 if (!mg)
247 { 277 {
248 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 278 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
249 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 279 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
250 mg->mg_virtual = &vtbl_coro; 280 mg->mg_virtual = &vtbl_coro;
251 mg->mg_obj = (SV *)newAV (); 281 mg->mg_obj = (SV *)newAV ();
252 } 282 }
253 283
254 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); 284 av = (AV *)mg->mg_obj;
285
286 if (AvFILLp (av) >= AvMAX (av))
287 av_extend (av, AvMAX (av) + 1);
288
289 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
255} 290}
256 291
257#define SB do { 292#define SB do {
258#define SE } while (0) 293#define SE } while (0)
259 294
260#define LOAD(state) load_state((state));
261#define SAVE(state,flags) save_state((state),(flags));
262
263#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 295#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
264 296
265static void 297static void
266load_state(Coro__State c) 298load_perl (Coro__State c)
267{ 299{
268#define VAR(name,type) PL_ ## name = c->name; 300#define VAR(name,type) PL_ ## name = c->name;
269# include "state.h" 301# include "state.h"
270#undef VAR 302#undef VAR
271 303
272 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 304 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
273 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 305 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
274 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 306 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
307 if (c->irssv)
308 {
309 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
310 SvREFCNT_dec (c->irssv);
311 else
312 {
313 REPLACE_SV (PL_rs, c->irssv);
314 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
315 sv_setsv (c->irssv_sv, PL_rs);
316 }
317 }
275 318
276 { 319 {
277 dSP; 320 dSP;
278 CV *cv; 321 CV *cv;
279 322
280 /* now do the ugly restore mess */ 323 /* now do the ugly restore mess */
281 while ((cv = (CV *)POPs)) 324 while ((cv = (CV *)POPs))
282 { 325 {
283 AV *padlist = (AV *)POPs;
284
285 if (padlist)
286 {
287 put_padlist (cv); /* mark this padlist as available */ 326 put_padlist (cv); /* mark this padlist as available */
288 CvPADLIST(cv) = padlist; 327 CvDEPTH (cv) = PTR2IV (POPs);
289 } 328 CvPADLIST (cv) = (AV *)POPs;
290
291 ++CvDEPTH(cv);
292 } 329 }
293 330
294 PUTBACK; 331 PUTBACK;
295 } 332 }
296} 333}
297 334
298static void 335static void
299save_state(Coro__State c, int flags) 336save_perl (Coro__State c)
300{ 337{
301 { 338 {
302 dSP; 339 dSP;
303 I32 cxix = cxstack_ix; 340 I32 cxix = cxstack_ix;
304 PERL_CONTEXT *ccstk = cxstack; 341 PERL_CONTEXT *ccstk = cxstack;
315 { 352 {
316 while (cxix >= 0) 353 while (cxix >= 0)
317 { 354 {
318 PERL_CONTEXT *cx = &ccstk[cxix--]; 355 PERL_CONTEXT *cx = &ccstk[cxix--];
319 356
320 if (CxTYPE(cx) == CXt_SUB) 357 if (CxTYPE (cx) == CXt_SUB)
321 { 358 {
322 CV *cv = cx->blk_sub.cv; 359 CV *cv = cx->blk_sub.cv;
360
323 if (CvDEPTH(cv)) 361 if (CvDEPTH (cv))
324 { 362 {
325 EXTEND (SP, CvDEPTH(cv)*2); 363 EXTEND (SP, 3);
326 364
327 while (--CvDEPTH(cv))
328 {
329 /* this tells the restore code to increment CvDEPTH */
330 PUSHs (Nullsv);
331 PUSHs ((SV *)cv);
332 }
333
334 PUSHs ((SV *)CvPADLIST(cv)); 365 PUSHs ((SV *)CvPADLIST (cv));
366 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
335 PUSHs ((SV *)cv); 367 PUSHs ((SV *)cv);
336 368
369 CvDEPTH (cv) = 0;
337 get_padlist (cv); 370 get_padlist (cv);
338 } 371 }
339 } 372 }
340#ifdef CXt_FORMAT 373#ifdef CXt_FORMAT
341 else if (CxTYPE(cx) == CXt_FORMAT) 374 else if (CxTYPE (cx) == CXt_FORMAT)
342 { 375 {
343 /* I never used formats, so how should I know how these are implemented? */ 376 /* I never used formats, so how should I know how these are implemented? */
344 /* my bold guess is as a simple, plain sub... */ 377 /* my bold guess is as a simple, plain sub... */
345 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 378 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
346 } 379 }
356 } 389 }
357 390
358 PUTBACK; 391 PUTBACK;
359 } 392 }
360 393
361 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 394 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
362 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 395 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
363 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 396 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
397 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
364 398
365#define VAR(name,type)c->name = PL_ ## name; 399#define VAR(name,type)c->name = PL_ ## name;
366# include "state.h" 400# include "state.h"
367#undef VAR 401#undef VAR
368} 402}
374 * not usually need a lot of stackspace. 408 * not usually need a lot of stackspace.
375 */ 409 */
376static void 410static void
377coro_init_stacks () 411coro_init_stacks ()
378{ 412{
379 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 413 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
380 PL_curstackinfo->si_type = PERLSI_MAIN; 414 PL_curstackinfo->si_type = PERLSI_MAIN;
381 PL_curstack = PL_curstackinfo->si_stack; 415 PL_curstack = PL_curstackinfo->si_stack;
382 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 416 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
383 417
384 PL_stack_base = AvARRAY(PL_curstack); 418 PL_stack_base = AvARRAY(PL_curstack);
385 PL_stack_sp = PL_stack_base; 419 PL_stack_sp = PL_stack_base;
386 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 420 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
387 421
388 New(50,PL_tmps_stack,96,SV*); 422 New(50,PL_tmps_stack,128,SV*);
389 PL_tmps_floor = -1; 423 PL_tmps_floor = -1;
390 PL_tmps_ix = -1; 424 PL_tmps_ix = -1;
391 PL_tmps_max = 96; 425 PL_tmps_max = 128;
392 426
393 New(54,PL_markstack,16,I32); 427 New(54,PL_markstack,32,I32);
394 PL_markstack_ptr = PL_markstack; 428 PL_markstack_ptr = PL_markstack;
395 PL_markstack_max = PL_markstack + 16; 429 PL_markstack_max = PL_markstack + 32;
396 430
397#ifdef SET_MARK_OFFSET 431#ifdef SET_MARK_OFFSET
398 SET_MARK_OFFSET; 432 SET_MARK_OFFSET;
399#endif 433#endif
400 434
401 New(54,PL_scopestack,16,I32); 435 New(54,PL_scopestack,32,I32);
402 PL_scopestack_ix = 0; 436 PL_scopestack_ix = 0;
403 PL_scopestack_max = 16; 437 PL_scopestack_max = 32;
404 438
405 New(54,PL_savestack,96,ANY); 439 New(54,PL_savestack,64,ANY);
406 PL_savestack_ix = 0; 440 PL_savestack_ix = 0;
407 PL_savestack_max = 96; 441 PL_savestack_max = 64;
408 442
409#if !PERL_VERSION_ATLEAST (5,9,0) 443#if !PERL_VERSION_ATLEAST (5,9,0)
410 New(54,PL_retstack,8,OP*); 444 New(54,PL_retstack,16,OP*);
411 PL_retstack_ix = 0; 445 PL_retstack_ix = 0;
412 PL_retstack_max = 8; 446 PL_retstack_max = 16;
413#endif 447#endif
414} 448}
415 449
416/* 450/*
417 * destroy the stacks, the callchain etc... 451 * destroy the stacks, the callchain etc...
470 * emulate part of the perl startup here. 504 * emulate part of the perl startup here.
471 */ 505 */
472 506
473 coro_init_stacks (); 507 coro_init_stacks ();
474 508
475 PL_curcop = 0; 509 PL_curcop = &PL_compiling;
476 PL_in_eval = 0; 510 PL_in_eval = EVAL_NULL;
477 PL_curpm = 0; 511 PL_curpm = 0;
512 PL_localizing = 0;
513 PL_dirty = 0;
514 PL_restartop = 0;
478 515
479 { 516 {
480 dSP; 517 dSP;
481 LOGOP myop; 518 LOGOP myop;
482 519
483 /* I have no idea why this is needed, but it is */
484 PUSHMARK (SP);
485
486 SvREFCNT_dec (GvAV (PL_defgv)); 520 SvREFCNT_dec (GvAV (PL_defgv));
487 GvAV (PL_defgv) = coro->args; coro->args = 0; 521 GvAV (PL_defgv) = coro->args; coro->args = 0;
488 522
489 Zero (&myop, 1, LOGOP); 523 Zero (&myop, 1, LOGOP);
490 myop.op_next = Nullop; 524 myop.op_next = Nullop;
491 myop.op_flags = OPf_WANT_VOID; 525 myop.op_flags = OPf_WANT_VOID;
492 526
493 PL_op = (OP *)&myop;
494
495 PUSHMARK (SP); 527 PUSHMARK (SP);
496 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 528 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
497 PUTBACK; 529 PUTBACK;
530 PL_op = (OP *)&myop;
498 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 531 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
499 SPAGAIN; 532 SPAGAIN;
500
501 ENTER; /* necessary e.g. for dounwind */
502 } 533 }
534
535 ENTER; /* necessary e.g. for dounwind */
503} 536}
504 537
505static void 538static void
506free_coro_mortal () 539free_coro_mortal ()
507{ 540{
510 SvREFCNT_dec (coro_mortal); 543 SvREFCNT_dec (coro_mortal);
511 coro_mortal = 0; 544 coro_mortal = 0;
512 } 545 }
513} 546}
514 547
548/* inject a fake call to Coro::State::_cctx_init into the execution */
515static void NOINLINE 549static void NOINLINE
516prepare_cctx (coro_cctx *cctx) 550prepare_cctx (coro_cctx *cctx)
517{ 551{
518 dSP; 552 dSP;
519 LOGOP myop; 553 LOGOP myop;
520 554
521 Zero (&myop, 1, LOGOP); 555 Zero (&myop, 1, LOGOP);
522 myop.op_next = PL_op; 556 myop.op_next = PL_op;
523 myop.op_flags = OPf_WANT_VOID; 557 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
524
525 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
526 558
527 PUSHMARK (SP); 559 PUSHMARK (SP);
560 EXTEND (SP, 2);
561 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx))));
528 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 562 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
529 PUTBACK; 563 PUTBACK;
564 PL_op = (OP *)&myop;
530 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 565 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
531 SPAGAIN; 566 SPAGAIN;
532} 567}
533 568
534static void 569static void
535coro_run (void *arg) 570coro_run (void *arg)
536{ 571{
537 /* coro_run is the alternative epilogue of transfer() */ 572 /* coro_run is the alternative tail of transfer(), so unlock here. */
538 UNLOCK; 573 UNLOCK;
539 574
540 /* 575 /*
541 * this is a _very_ stripped down perl interpreter ;) 576 * this is a _very_ stripped down perl interpreter ;)
542 */ 577 */
544 579
545 /* inject call to cctx_init */ 580 /* inject call to cctx_init */
546 prepare_cctx ((coro_cctx *)arg); 581 prepare_cctx ((coro_cctx *)arg);
547 582
548 /* somebody will hit me for both perl_run and PL_restartop */ 583 /* somebody will hit me for both perl_run and PL_restartop */
584 PL_restartop = PL_op;
549 perl_run (PL_curinterp); 585 perl_run (PL_curinterp);
550 586
551 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 587 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
552 abort (); 588 abort ();
553} 589}
562 New (0, cctx, 1, coro_cctx); 598 New (0, cctx, 1, coro_cctx);
563 599
564#if HAVE_MMAP 600#if HAVE_MMAP
565 601
566 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 602 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
567 /* mmap suppsedly does allocate-on-write for us */ 603 /* mmap supposedly does allocate-on-write for us */
568 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 604 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
569 605
570 if (cctx->sptr == (void *)-1) 606 if (cctx->sptr == (void *)-1)
571 { 607 {
572 perror ("FATAL: unable to mmap stack for coroutine"); 608 perror ("FATAL: unable to mmap stack for coroutine");
601 637
602 return cctx; 638 return cctx;
603} 639}
604 640
605static void 641static void
606cctx_free (coro_cctx *cctx) 642cctx_destroy (coro_cctx *cctx)
607{ 643{
608 if (!cctx) 644 if (!cctx)
609 return; 645 return;
610 646
611 --cctx_count; 647 --cctx_count;
628{ 664{
629 coro_cctx *cctx; 665 coro_cctx *cctx;
630 666
631 if (cctx_first) 667 if (cctx_first)
632 { 668 {
633 --cctx_idle;
634 cctx = cctx_first; 669 cctx = cctx_first;
635 cctx_first = cctx->next; 670 cctx_first = cctx->next;
671 --cctx_idle;
636 } 672 }
637 else 673 else
638 { 674 {
639 cctx = cctx_new (); 675 cctx = cctx_new ();
640 PL_op = PL_op->op_next; 676 PL_op = PL_op->op_next;
644} 680}
645 681
646static void 682static void
647cctx_put (coro_cctx *cctx) 683cctx_put (coro_cctx *cctx)
648{ 684{
685 /* free another cctx if overlimit */
686 if (cctx_idle >= MAX_IDLE_CCTX)
687 {
688 coro_cctx *first = cctx_first;
689 cctx_first = first->next;
690 --cctx_idle;
691
692 assert (!first->inuse);
693 cctx_destroy (first);
694 }
695
649 ++cctx_idle; 696 ++cctx_idle;
650 cctx->next = cctx_first; 697 cctx->next = cctx_first;
651 cctx_first = cctx; 698 cctx_first = cctx;
652} 699}
653 700
654/* never call directly, always through the coro_state_transfer global variable */ 701/* never call directly, always through the coro_state_transfer global variable */
655static void NOINLINE 702static void NOINLINE
656transfer (struct coro *prev, struct coro *next, int flags) 703transfer (struct coro *prev, struct coro *next)
657{ 704{
658 dSTACKLEVEL; 705 dSTACKLEVEL;
659 706
660 /* sometimes transfer is only called to set idle_sp */ 707 /* sometimes transfer is only called to set idle_sp */
661 if (flags == TRANSFER_SET_STACKLEVEL) 708 if (!next)
662 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 709 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
663 else if (prev != next) 710 else if (prev != next)
664 { 711 {
665 coro_cctx *prev__cctx; 712 coro_cctx *prev__cctx;
666 713
714 if (prev->flags & CF_NEW)
715 {
716 /* create a new empty context */
717 Newz (0, prev->cctx, 1, coro_cctx);
718 prev->cctx->inuse = 1;
719 prev->flags &= ~CF_NEW;
720 prev->flags |= CF_RUNNING;
721 }
722
723 /*TODO: must not croak here */
724 if (!prev->flags & CF_RUNNING)
725 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
726
727 if (next->flags & CF_RUNNING)
728 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
729
730 prev->flags &= ~CF_RUNNING;
731 next->flags |= CF_RUNNING;
732
667 LOCK; 733 LOCK;
668 734
669 if (next->mainstack) 735 if (next->flags & CF_NEW)
670 { 736 {
671 /* coroutine already started */ 737 /* need to start coroutine */
672 SAVE (prev, flags); 738 next->flags &= ~CF_NEW;
673 LOAD (next); 739 /* first get rid of the old state */
740 save_perl (prev);
741 /* setup coroutine call */
742 setup_coro (next);
743 /* need a new stack */
744 assert (!next->stack);
674 } 745 }
675 else 746 else
676 { 747 {
677 /* need to start coroutine */ 748 /* coroutine already started */
678 /* first get rid of the old state */ 749 save_perl (prev);
679 SAVE (prev, -1); 750 load_perl (next);
680 /* setup coroutine call */
681 setup_coro (next);
682 /* need a stack */
683 next->cctx = 0;
684 } 751 }
685
686 if (!prev->cctx)
687 /* create a new empty context */
688 Newz (0, prev->cctx, 1, coro_cctx);
689 752
690 prev__cctx = prev->cctx; 753 prev__cctx = prev->cctx;
691 754
692 /* possibly "free" the cctx */ 755 /* possibly "free" the cctx */
693 if (prev__cctx->idle_sp == STACKLEVEL) 756 if (prev__cctx->idle_sp == STACKLEVEL)
694 { 757 {
758 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
759 assert (PL_top_env == prev__cctx->top_env);
760
761 prev->cctx = 0;
762
695 cctx_put (prev__cctx); 763 cctx_put (prev__cctx);
696 prev->cctx = 0; 764 prev__cctx->inuse = 0;
697 } 765 }
698 766
699 if (!next->cctx) 767 if (!next->cctx)
768 {
700 next->cctx = cctx_get (); 769 next->cctx = cctx_get ();
770 assert (!next->cctx->inuse);
771 next->cctx->inuse = 1;
772 }
701 773
702 if (prev__cctx != next->cctx) 774 if (prev__cctx != next->cctx)
703 { 775 {
704 prev__cctx->top_env = PL_top_env; 776 prev__cctx->top_env = PL_top_env;
705 PL_top_env = next->cctx->top_env; 777 PL_top_env = next->cctx->top_env;
713} 785}
714 786
715struct transfer_args 787struct transfer_args
716{ 788{
717 struct coro *prev, *next; 789 struct coro *prev, *next;
718 int flags;
719}; 790};
720 791
721#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) 792#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
722 793
723static void 794static void
724coro_state_destroy (struct coro *coro) 795coro_state_destroy (struct coro *coro)
725{ 796{
726 if (coro->refcnt--) 797 if (coro->refcnt--)
727 return; 798 return;
728 799
729 if (coro->mainstack && coro->mainstack != main_mainstack) 800 if (coro->mainstack && coro->mainstack != main_mainstack)
730 { 801 {
731 struct coro temp; 802 struct coro temp;
803 Zero (&temp, 1, struct coro);
804 temp.save = CORO_SAVE_ALL;
732 805
733 SAVE ((&temp), TRANSFER_SAVE_ALL); 806 if (coro->flags & CF_RUNNING)
734 LOAD (coro); 807 croak ("FATAL: tried to destroy currently running coroutine");
808
809 save_perl (&temp);
810 load_perl (coro);
735 811
736 coro_destroy_stacks (); 812 coro_destroy_stacks ();
737 813
738 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 814 load_perl (&temp); /* this will get rid of defsv etc.. */
739 815
740 coro->mainstack = 0; 816 coro->mainstack = 0;
741 } 817 }
742 818
743 cctx_free (coro->cctx); 819 cctx_destroy (coro->cctx);
744 SvREFCNT_dec (coro->args); 820 SvREFCNT_dec (coro->args);
745 Safefree (coro); 821 Safefree (coro);
746} 822}
747 823
748static int 824static int
798 assert (mg->mg_type == PERL_MAGIC_ext); 874 assert (mg->mg_type == PERL_MAGIC_ext);
799 return (struct coro *)mg->mg_ptr; 875 return (struct coro *)mg->mg_ptr;
800} 876}
801 877
802static void 878static void
803prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) 879prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
804{ 880{
805 ta->prev = SvSTATE (prev); 881 ta->prev = SvSTATE (prev_sv);
806 ta->next = SvSTATE (next); 882 ta->next = SvSTATE (next_sv);
807 ta->flags = flags;
808} 883}
809 884
810static void 885static void
811api_transfer (SV *prev, SV *next, int flags) 886api_transfer (SV *prev_sv, SV *next_sv)
812{ 887{
813 dTHX;
814 struct transfer_args ta; 888 struct transfer_args ta;
815 889
816 prepare_transfer (&ta, prev, next, flags); 890 prepare_transfer (&ta, prev_sv, next_sv);
817 TRANSFER (ta); 891 TRANSFER (ta);
892}
893
894static int
895api_save (SV *coro_sv, int new_save)
896{
897 struct coro *coro = SvSTATE (coro_sv);
898 int old_save = coro->save;
899
900 if (new_save >= 0)
901 coro->save = new_save;
902
903 return old_save;
818} 904}
819 905
820/** Coro ********************************************************************/ 906/** Coro ********************************************************************/
821 907
822#define PRIO_MAX 3 908#define PRIO_MAX 3
825#define PRIO_LOW -1 911#define PRIO_LOW -1
826#define PRIO_IDLE -3 912#define PRIO_IDLE -3
827#define PRIO_MIN -4 913#define PRIO_MIN -4
828 914
829/* for Coro.pm */ 915/* for Coro.pm */
830static GV *coro_current, *coro_idle; 916static SV *coro_current;
831static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 917static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
832static int coro_nready; 918static int coro_nready;
833 919
834static void 920static void
835coro_enq (SV *sv) 921coro_enq (SV *coro_sv)
836{ 922{
837 int prio;
838
839 if (SvTYPE (sv) != SVt_PVHV)
840 croak ("Coro::ready tried to enqueue something that is not a coroutine");
841
842 prio = SvSTATE (sv)->prio;
843
844 av_push (coro_ready [prio - PRIO_MIN], sv); 923 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
845 coro_nready++; 924 coro_nready++;
846} 925}
847 926
848static SV * 927static SV *
849coro_deq (int min_prio) 928coro_deq (int min_prio)
862 } 941 }
863 942
864 return 0; 943 return 0;
865} 944}
866 945
867static void 946static int
868api_ready (SV *coro) 947api_ready (SV *coro_sv)
869{ 948{
870 dTHX; 949 struct coro *coro;
871 950
872 if (SvROK (coro)) 951 if (SvROK (coro_sv))
873 coro = SvRV (coro); 952 coro_sv = SvRV (coro_sv);
953
954 coro = SvSTATE (coro_sv);
955
956 if (coro->flags & CF_READY)
957 return 0;
958
959#if 0 /* this is actually harmless */
960 if (coro->flags & CF_RUNNING)
961 croak ("Coro::ready called on currently running coroutine");
962#endif
963
964 coro->flags |= CF_READY;
874 965
875 LOCK; 966 LOCK;
876 coro_enq (SvREFCNT_inc (coro)); 967 coro_enq (SvREFCNT_inc (coro_sv));
877 UNLOCK; 968 UNLOCK;
969
970 return 1;
971}
972
973static int
974api_is_ready (SV *coro_sv)
975{
976 return !!SvSTATE (coro_sv)->flags & CF_READY;
878} 977}
879 978
880static void 979static void
881prepare_schedule (struct transfer_args *ta) 980prepare_schedule (struct transfer_args *ta)
882{ 981{
883 SV *current, *prev, *next; 982 SV *prev, *next;
884
885 current = GvSV (coro_current);
886 983
887 for (;;) 984 for (;;)
888 { 985 {
889 LOCK; 986 LOCK;
890 next = coro_deq (PRIO_MIN); 987 next = coro_deq (PRIO_MIN);
899 ENTER; 996 ENTER;
900 SAVETMPS; 997 SAVETMPS;
901 998
902 PUSHMARK (SP); 999 PUSHMARK (SP);
903 PUTBACK; 1000 PUTBACK;
904 call_sv (GvSV (coro_idle), G_DISCARD); 1001 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
905 1002
906 FREETMPS; 1003 FREETMPS;
907 LEAVE; 1004 LEAVE;
908 } 1005 }
909 } 1006 }
910 1007
911 prev = SvRV (current); 1008 prev = SvRV (coro_current);
912 SvRV (current) = next; 1009 SvRV_set (coro_current, next);
913 1010
914 /* free this only after the transfer */ 1011 /* free this only after the transfer */
915 LOCK; 1012 LOCK;
916 free_coro_mortal (); 1013 free_coro_mortal ();
917 UNLOCK; 1014 UNLOCK;
918 coro_mortal = prev; 1015 coro_mortal = prev;
919 1016
1017 assert (!SvROK(prev));//D
1018 assert (!SvROK(next));//D
1019
920 ta->prev = SvSTATE (prev); 1020 ta->prev = SvSTATE (prev);
921 ta->next = SvSTATE (next); 1021 ta->next = SvSTATE (next);
922 ta->flags = TRANSFER_SAVE_ALL; 1022
1023 assert (ta->next->flags & CF_READY);
1024 ta->next->flags &= ~CF_READY;
923} 1025}
924 1026
925static void 1027static void
926prepare_cede (struct transfer_args *ta) 1028prepare_cede (struct transfer_args *ta)
927{ 1029{
928 LOCK; 1030 api_ready (coro_current);
929 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
930 UNLOCK;
931 1031
932 prepare_schedule (ta); 1032 prepare_schedule (ta);
933} 1033}
934 1034
935static void 1035static void
936api_schedule (void) 1036api_schedule (void)
937{ 1037{
938 dTHX;
939 struct transfer_args ta; 1038 struct transfer_args ta;
940 1039
941 prepare_schedule (&ta); 1040 prepare_schedule (&ta);
942 TRANSFER (ta); 1041 TRANSFER (ta);
943} 1042}
944 1043
945static void 1044static void
946api_cede (void) 1045api_cede (void)
947{ 1046{
948 dTHX;
949 struct transfer_args ta; 1047 struct transfer_args ta;
950 1048
951 prepare_cede (&ta); 1049 prepare_cede (&ta);
952 TRANSFER (ta); 1050 TRANSFER (ta);
953} 1051}
963#endif 1061#endif
964 BOOT_PAGESIZE; 1062 BOOT_PAGESIZE;
965 1063
966 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1064 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
967 1065
968 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1066 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
969 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1067 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
970 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 1068 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1069 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1070 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
971 1071
972 main_mainstack = PL_mainstack; 1072 main_mainstack = PL_mainstack;
973 1073
974 coroapi.ver = CORO_API_VERSION; 1074 coroapi.ver = CORO_API_VERSION;
975 coroapi.transfer = api_transfer; 1075 coroapi.transfer = api_transfer;
985 HV *hv; 1085 HV *hv;
986 int i; 1086 int i;
987 1087
988 Newz (0, coro, 1, struct coro); 1088 Newz (0, coro, 1, struct coro);
989 coro->args = newAV (); 1089 coro->args = newAV ();
1090 coro->save = CORO_SAVE_ALL;
1091 coro->flags = CF_NEW;
990 1092
991 hv = newHV (); 1093 hv = newHV ();
992 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1094 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
993 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1095 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
994 1096
995 for (i = 1; i < items; i++) 1097 for (i = 1; i < items; i++)
996 av_push (coro->args, newSVsv (ST (i))); 1098 av_push (coro->args, newSVsv (ST (i)));
997} 1099}
998 OUTPUT: 1100 OUTPUT:
1101 RETVAL
1102
1103int
1104save (SV *coro, int new_save = -1)
1105 CODE:
1106 RETVAL = api_save (coro, new_save);
1107 OUTPUT:
999 RETVAL 1108 RETVAL
1000 1109
1001void 1110void
1002_set_stacklevel (...) 1111_set_stacklevel (...)
1003 ALIAS: 1112 ALIAS:
1004 Coro::State::transfer = 1 1113 Coro::State::transfer = 1
1005 Coro::schedule = 2 1114 Coro::schedule = 2
1006 Coro::cede = 3 1115 Coro::cede = 3
1007 Coro::Cont::yield = 4
1008 CODE: 1116 CODE:
1009{ 1117{
1010 struct transfer_args ta; 1118 struct transfer_args ta;
1011 1119
1012 switch (ix) 1120 switch (ix)
1013 { 1121 {
1014 case 0: 1122 case 0:
1015 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1123 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1016 ta.next = 0; 1124 ta.next = 0;
1017 ta.flags = TRANSFER_SET_STACKLEVEL;
1018 break; 1125 break;
1019 1126
1020 case 1: 1127 case 1:
1021 if (items != 3) 1128 if (items != 2)
1022 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); 1129 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1023 1130
1024 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1131 prepare_transfer (&ta, ST (0), ST (1));
1025 break; 1132 break;
1026 1133
1027 case 2: 1134 case 2:
1028 prepare_schedule (&ta); 1135 prepare_schedule (&ta);
1029 break; 1136 break;
1030 1137
1031 case 3: 1138 case 3:
1032 prepare_cede (&ta); 1139 prepare_cede (&ta);
1033 break; 1140 break;
1034
1035 case 4:
1036 {
1037 SV *yieldstack;
1038 SV *sv;
1039 AV *defav = GvAV (PL_defgv);
1040
1041 yieldstack = *hv_fetch (
1042 (HV *)SvRV (GvSV (coro_current)),
1043 "yieldstack", sizeof ("yieldstack") - 1,
1044 0
1045 );
1046
1047 /* set up @_ -- ugly */
1048 av_clear (defav);
1049 av_fill (defav, items - 1);
1050 while (items--)
1051 av_store (defav, items, SvREFCNT_inc (ST(items)));
1052
1053 sv = av_pop ((AV *)SvRV (yieldstack));
1054 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1055 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1056 ta.flags = 0;
1057 SvREFCNT_dec (sv);
1058 }
1059 break;
1060
1061 } 1141 }
1062 1142
1063 TRANSFER (ta); 1143 TRANSFER (ta);
1064} 1144}
1065 1145
1109 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1189 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1110 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1190 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1111 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1191 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1112 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1192 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1113 1193
1114 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1194 coro_current = get_sv ("Coro::current", FALSE);
1115 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1195 SvREADONLY_on (coro_current);
1116 1196
1117 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1197 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1118 coro_ready[i] = newAV (); 1198 coro_ready[i] = newAV ();
1119 1199
1120 { 1200 {
1121 SV *sv = perl_get_sv("Coro::API", 1); 1201 SV *sv = perl_get_sv("Coro::API", 1);
1122 1202
1123 coroapi.schedule = api_schedule; 1203 coroapi.schedule = api_schedule;
1204 coroapi.save = api_save;
1124 coroapi.cede = api_cede; 1205 coroapi.cede = api_cede;
1125 coroapi.ready = api_ready; 1206 coroapi.ready = api_ready;
1207 coroapi.is_ready = api_is_ready;
1126 coroapi.nready = &coro_nready; 1208 coroapi.nready = &coro_nready;
1127 coroapi.current = coro_current; 1209 coroapi.current = coro_current;
1128 1210
1129 GCoroAPI = &coroapi; 1211 GCoroAPI = &coroapi;
1130 sv_setiv (sv, (IV)&coroapi); 1212 sv_setiv (sv, (IV)&coroapi);
1131 SvREADONLY_on (sv); 1213 SvREADONLY_on (sv);
1132 } 1214 }
1133} 1215}
1216
1217void
1218_set_current (SV *current)
1219 PROTOTYPE: $
1220 CODE:
1221 SvREFCNT_dec (SvRV (coro_current));
1222 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1134 1223
1135int 1224int
1136prio (Coro::State coro, int newprio = 0) 1225prio (Coro::State coro, int newprio = 0)
1137 ALIAS: 1226 ALIAS:
1138 nice = 1 1227 nice = 1
1150 1239
1151 coro->prio = newprio; 1240 coro->prio = newprio;
1152 } 1241 }
1153} 1242}
1154 1243
1155void 1244SV *
1156ready (SV *self) 1245ready (SV *self)
1157 PROTOTYPE: $ 1246 PROTOTYPE: $
1158 CODE: 1247 CODE:
1159 api_ready (self); 1248 RETVAL = boolSV (api_ready (self));
1249 OUTPUT:
1250 RETVAL
1251
1252SV *
1253is_ready (SV *self)
1254 PROTOTYPE: $
1255 CODE:
1256 RETVAL = boolSV (api_is_ready (self));
1257 OUTPUT:
1258 RETVAL
1160 1259
1161int 1260int
1162nready (...) 1261nready (...)
1163 PROTOTYPE: 1262 PROTOTYPE:
1164 CODE: 1263 CODE:
1170 1269
1171SV * 1270SV *
1172_get_state () 1271_get_state ()
1173 CODE: 1272 CODE:
1174{ 1273{
1175 struct { 1274 struct io_state *data;
1176 int errorno;
1177 int laststype;
1178 int laststatval;
1179 Stat_t statcache;
1180 } data;
1181 1275
1276 RETVAL = newSV (sizeof (struct io_state));
1277 data = (struct io_state *)SvPVX (RETVAL);
1278 SvCUR_set (RETVAL, sizeof (struct io_state));
1279 SvPOK_only (RETVAL);
1280
1182 data.errorno = errno; 1281 data->errorno = errno;
1183 data.laststype = PL_laststype; 1282 data->laststype = PL_laststype;
1184 data.laststatval = PL_laststatval; 1283 data->laststatval = PL_laststatval;
1185 data.statcache = PL_statcache; 1284 data->statcache = PL_statcache;
1186
1187 RETVAL = newSVpvn ((char *)&data, sizeof data);
1188} 1285}
1189 OUTPUT: 1286 OUTPUT:
1190 RETVAL 1287 RETVAL
1191 1288
1192void 1289void
1193_set_state (char *data_) 1290_set_state (char *data_)
1194 PROTOTYPE: $ 1291 PROTOTYPE: $
1195 CODE: 1292 CODE:
1196{ 1293{
1197 struct { 1294 struct io_state *data = (void *)data_;
1198 int errorno;
1199 int laststype;
1200 int laststatval;
1201 Stat_t statcache;
1202 } *data = (void *)data_;
1203 1295
1204 errno = data->errorno; 1296 errno = data->errorno;
1205 PL_laststype = data->laststype; 1297 PL_laststype = data->laststype;
1206 PL_laststatval = data->laststatval; 1298 PL_laststatval = data->laststatval;
1207 PL_statcache = data->statcache; 1299 PL_statcache = data->statcache;
1208} 1300}
1301

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines