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.118 by root, Fri Dec 1 19:58:53 2006 UTC vs.
Revision 1.140 by root, Sat Jan 20 19:52:15 2007 UTC

4#include "perl.h" 4#include "perl.h"
5#include "XSUB.h" 5#include "XSUB.h"
6 6
7#include "patchlevel.h" 7#include "patchlevel.h"
8 8
9#if USE_VALGRIND
10# include <valgrind/valgrind.h>
11#endif
12
13/* the maximum number of idle cctx that will be pooled */
14#define MAX_IDLE_CCTX 8
15
16#define PERL_VERSION_ATLEAST(a,b,c) \
17 (PERL_REVISION > (a) \
18 || (PERL_REVISION == (a) \
19 && (PERL_VERSION > (b) \
20 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
21
22#if !PERL_VERSION_ATLEAST (5,6,0)
23# ifndef PL_ppaddr
24# define PL_ppaddr ppaddr
25# endif
26# ifndef call_sv
27# define call_sv perl_call_sv
28# endif
29# ifndef get_sv
30# define get_sv perl_get_sv
31# endif
32# ifndef get_cv
33# define get_cv perl_get_cv
34# endif
35# ifndef IS_PADGV
36# define IS_PADGV(v) 0
37# endif
38# ifndef IS_PADCONST
39# define IS_PADCONST(v) 0
40# endif
41#endif
42
43#include <stdio.h> 9#include <stdio.h>
44#include <errno.h> 10#include <errno.h>
45 11#include <assert.h>
46#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
47# undef STACKGUARD
48#endif
49
50#ifndef STACKGUARD
51# define STACKGUARD 0
52#endif
53 12
54#ifdef HAVE_MMAP 13#ifdef HAVE_MMAP
55# include <unistd.h> 14# include <unistd.h>
56# include <sys/mman.h> 15# include <sys/mman.h>
57# ifndef MAP_ANONYMOUS 16# ifndef MAP_ANONYMOUS
72#else 31#else
73# define PAGESIZE 0 32# define PAGESIZE 0
74# define BOOT_PAGESIZE (void)0 33# define BOOT_PAGESIZE (void)0
75#endif 34#endif
76 35
36#if USE_VALGRIND
37# include <valgrind/valgrind.h>
38#endif
39
40/* the maximum number of idle cctx that will be pooled */
41#define MAX_IDLE_CCTX 8
42
43#define PERL_VERSION_ATLEAST(a,b,c) \
44 (PERL_REVISION > (a) \
45 || (PERL_REVISION == (a) \
46 && (PERL_VERSION > (b) \
47 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
48
49#if !PERL_VERSION_ATLEAST (5,6,0)
50# ifndef PL_ppaddr
51# define PL_ppaddr ppaddr
52# endif
53# ifndef call_sv
54# define call_sv perl_call_sv
55# endif
56# ifndef get_sv
57# define get_sv perl_get_sv
58# endif
59# ifndef get_cv
60# define get_cv perl_get_cv
61# endif
62# ifndef IS_PADGV
63# define IS_PADGV(v) 0
64# endif
65# ifndef IS_PADCONST
66# define IS_PADCONST(v) 0
67# endif
68#endif
69
70/* 5.8.7 */
71#ifndef SvRV_set
72# define SvRV_set(s,v) SvRV(s) = (v)
73#endif
74
75#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
76# undef STACKGUARD
77#endif
78
79#ifndef STACKGUARD
80# define STACKGUARD 0
81#endif
82
83/* prefer perl internal functions over our own? */
84#ifndef PREFER_PERL_FUNCTIONS
85# define PREFER_PERL_FUNCTIONS 0
86#endif
87
77/* The next macro should declare a variable stacklevel that contains and approximation 88/* The next macro should declare a variable stacklevel that contains and approximation
78 * to the current C stack pointer. Its property is that it changes with each call 89 * to the current C stack pointer. Its property is that it changes with each call
79 * and should be unique. */ 90 * and should be unique. */
80#define dSTACKLEVEL int stacklevel 91#define dSTACKLEVEL int stacklevel
81#define STACKLEVEL ((void *)&stacklevel) 92#define STACKLEVEL ((void *)&stacklevel)
82 93
83#define IN_DESTRUCT (PL_main_cv == Nullcv) 94#define IN_DESTRUCT (PL_main_cv == Nullcv)
84 95
85#if __GNUC__ >= 3 96#if __GNUC__ >= 3
86# define attribute(x) __attribute__(x) 97# define attribute(x) __attribute__(x)
98# define BARRIER __asm__ __volatile__ ("" : : : "memory")
87#else 99#else
88# define attribute(x) 100# define attribute(x)
101# define BARRIER
89#endif 102#endif
90 103
91#define NOINLINE attribute ((noinline)) 104#define NOINLINE attribute ((noinline))
92 105
93#include "CoroAPI.h" 106#include "CoroAPI.h"
94
95#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
96 107
97#ifdef USE_ITHREADS 108#ifdef USE_ITHREADS
98static perl_mutex coro_mutex; 109static perl_mutex coro_mutex;
99# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 110# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
100# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 111# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
101#else 112#else
102# define LOCK (void)0 113# define LOCK (void)0
103# define UNLOCK (void)0 114# define UNLOCK (void)0
104#endif 115#endif
105 116
117/* helper storage struct for Coro::AIO */
118struct io_state
119{
120 int errorno;
121 I32 laststype;
122 int laststatval;
123 Stat_t statcache;
124};
125
106static struct CoroAPI coroapi; 126static struct CoroAPI coroapi;
107static AV *main_mainstack; /* used to differentiate between $main and others */ 127static AV *main_mainstack; /* used to differentiate between $main and others */
108static HV *coro_state_stash, *coro_stash; 128static HV *coro_state_stash, *coro_stash;
109static SV *coro_mortal; /* will be freed after next transfer */ 129static SV *coro_mortal; /* will be freed after next transfer */
110 130
118 /* the stack */ 138 /* the stack */
119 void *sptr; 139 void *sptr;
120 long ssize; /* positive == mmap, otherwise malloc */ 140 long ssize; /* positive == mmap, otherwise malloc */
121 141
122 /* cpu state */ 142 /* cpu state */
123 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 143 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
144 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
124 JMPENV *top_env; 145 JMPENV *top_env;
125 coro_context cctx; 146 coro_context cctx;
126 147
127 int inuse; 148 int inuse;
128 149
130 int valgrind_id; 151 int valgrind_id;
131#endif 152#endif
132} coro_cctx; 153} coro_cctx;
133 154
134enum { 155enum {
135 CF_RUNNING = 0x0001, /* coroutine is running */ 156 CF_RUNNING = 0x0001, /* coroutine is running */
136 CF_READY = 0x0002, /* coroutine is ready */ 157 CF_READY = 0x0002, /* coroutine is ready */
137 CF_NEW = 0x0004, /* ahs never been switched to */ 158 CF_NEW = 0x0004, /* has never been switched to */
159 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
138}; 160};
139 161
140/* this is a structure representing a perl-level coroutine */ 162/* this is a structure representing a perl-level coroutine */
141struct coro { 163struct coro {
142 /* the c coroutine allocated to this perl coroutine, if any */ 164 /* the c coroutine allocated to this perl coroutine, if any */
143 coro_cctx *cctx; 165 coro_cctx *cctx;
144 166
145 /* data associated with this coroutine (initial args) */ 167 /* data associated with this coroutine (initial args) */
146 AV *args; 168 AV *args;
147 int refcnt; 169 int refcnt;
148 int flags; 170 int save; /* CORO_SAVE flags */
171 int flags; /* CF_ flags */
149 172
150 /* optionally saved, might be zero */ 173 /* optionally saved, might be zero */
151 AV *defav; 174 AV *defav; /* @_ */
152 SV *defsv; 175 SV *defsv; /* $_ */
153 SV *errsv; 176 SV *errsv; /* $@ */
177 SV *irssv; /* $/ */
178 SV *irssv_sv; /* real $/ cache */
154 179
155#define VAR(name,type) type name; 180#define VAR(name,type) type name;
156# include "state.h" 181# include "state.h"
157#undef VAR 182#undef VAR
158 183
160 int prio; 185 int prio;
161}; 186};
162 187
163typedef struct coro *Coro__State; 188typedef struct coro *Coro__State;
164typedef struct coro *Coro__State_or_hashref; 189typedef struct coro *Coro__State_or_hashref;
190
191/** Coro ********************************************************************/
192
193#define PRIO_MAX 3
194#define PRIO_HIGH 1
195#define PRIO_NORMAL 0
196#define PRIO_LOW -1
197#define PRIO_IDLE -3
198#define PRIO_MIN -4
199
200/* for Coro.pm */
201static SV *coro_current;
202static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
203static int coro_nready;
204
205/** lowlevel stuff **********************************************************/
165 206
166static AV * 207static AV *
167coro_clone_padlist (CV *cv) 208coro_clone_padlist (CV *cv)
168{ 209{
169 AV *padlist = CvPADLIST (cv); 210 AV *padlist = CvPADLIST (cv);
244 285
245 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 286 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
246 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 287 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
247 else 288 else
248 { 289 {
249#if 0 290#if PREFER_PERL_FUNCTIONS
250 /* this is probably cleaner, but also slower? */ 291 /* this is probably cleaner, but also slower? */
251 CV *cp = Perl_cv_clone (cv); 292 CV *cp = Perl_cv_clone (cv);
252 CvPADLIST (cv) = CvPADLIST (cp); 293 CvPADLIST (cv) = CvPADLIST (cp);
253 CvPADLIST (cp) = 0; 294 CvPADLIST (cp) = 0;
254 SvREFCNT_dec (cp); 295 SvREFCNT_dec (cp);
278 av_extend (av, AvMAX (av) + 1); 319 av_extend (av, AvMAX (av) + 1);
279 320
280 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 321 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
281} 322}
282 323
324/** load & save, init *******************************************************/
325
283#define SB do { 326#define SB do {
284#define SE } while (0) 327#define SE } while (0)
285 328
286#define LOAD(state) load_state ((state))
287#define SAVE(state,flags) save_state ((state),(flags))
288
289#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE 329#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
290 330
291static void 331static void
292load_state (Coro__State c) 332load_perl (Coro__State c)
293{ 333{
294#define VAR(name,type) PL_ ## name = c->name; 334#define VAR(name,type) PL_ ## name = c->name;
295# include "state.h" 335# include "state.h"
296#undef VAR 336#undef VAR
297 337
298 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 338 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
299 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 339 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
300 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 340 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
341 if (c->irssv)
342 {
343 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
344 SvREFCNT_dec (c->irssv);
345 else
346 {
347 REPLACE_SV (PL_rs, c->irssv);
348 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
349 sv_setsv (c->irssv_sv, PL_rs);
350 }
351 }
301 352
302 { 353 {
303 dSP; 354 dSP;
304 CV *cv; 355 CV *cv;
305 356
314 PUTBACK; 365 PUTBACK;
315 } 366 }
316} 367}
317 368
318static void 369static void
319save_state (Coro__State c, int flags) 370save_perl (Coro__State c)
320{ 371{
321 { 372 {
322 dSP; 373 dSP;
323 I32 cxix = cxstack_ix; 374 I32 cxix = cxstack_ix;
324 PERL_CONTEXT *ccstk = cxstack; 375 PERL_CONTEXT *ccstk = cxstack;
327 /* 378 /*
328 * the worst thing you can imagine happens first - we have to save 379 * the worst thing you can imagine happens first - we have to save
329 * (and reinitialize) all cv's in the whole callchain :( 380 * (and reinitialize) all cv's in the whole callchain :(
330 */ 381 */
331 382
383 EXTEND (SP, 3 + 1);
332 PUSHs (Nullsv); 384 PUSHs (Nullsv);
333 /* this loop was inspired by pp_caller */ 385 /* this loop was inspired by pp_caller */
334 for (;;) 386 for (;;)
335 { 387 {
336 while (cxix >= 0) 388 while (cxix >= 0)
342 CV *cv = cx->blk_sub.cv; 394 CV *cv = cx->blk_sub.cv;
343 395
344 if (CvDEPTH (cv)) 396 if (CvDEPTH (cv))
345 { 397 {
346 EXTEND (SP, 3); 398 EXTEND (SP, 3);
347
348 PUSHs ((SV *)CvPADLIST (cv)); 399 PUSHs ((SV *)CvPADLIST (cv));
349 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 400 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
350 PUSHs ((SV *)cv); 401 PUSHs ((SV *)cv);
351 402
352 CvDEPTH (cv) = 0; 403 CvDEPTH (cv) = 0;
353 get_padlist (cv); 404 get_padlist (cv);
354 } 405 }
355 } 406 }
356#ifdef CXt_FORMAT
357 else if (CxTYPE (cx) == CXt_FORMAT)
358 {
359 /* I never used formats, so how should I know how these are implemented? */
360 /* my bold guess is as a simple, plain sub... */
361 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
362 }
363#endif
364 } 407 }
365 408
366 if (top_si->si_type == PERLSI_MAIN) 409 if (top_si->si_type == PERLSI_MAIN)
367 break; 410 break;
368 411
372 } 415 }
373 416
374 PUTBACK; 417 PUTBACK;
375 } 418 }
376 419
377 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 420 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
378 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 421 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
379 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 422 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
423 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
380 424
381#define VAR(name,type)c->name = PL_ ## name; 425#define VAR(name,type)c->name = PL_ ## name;
382# include "state.h" 426# include "state.h"
383#undef VAR 427#undef VAR
384} 428}
387 * allocate various perl stacks. This is an exact copy 431 * allocate various perl stacks. This is an exact copy
388 * of perl.c:init_stacks, except that it uses less memory 432 * of perl.c:init_stacks, except that it uses less memory
389 * on the (sometimes correct) assumption that coroutines do 433 * on the (sometimes correct) assumption that coroutines do
390 * not usually need a lot of stackspace. 434 * not usually need a lot of stackspace.
391 */ 435 */
436#if PREFER_PERL_FUNCTIONS
437# define coro_init_stacks init_stacks
438#else
392static void 439static void
393coro_init_stacks () 440coro_init_stacks ()
394{ 441{
395 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 442 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
396 PL_curstackinfo->si_type = PERLSI_MAIN; 443 PL_curstackinfo->si_type = PERLSI_MAIN;
426 New(54,PL_retstack,16,OP*); 473 New(54,PL_retstack,16,OP*);
427 PL_retstack_ix = 0; 474 PL_retstack_ix = 0;
428 PL_retstack_max = 16; 475 PL_retstack_max = 16;
429#endif 476#endif
430} 477}
478#endif
431 479
432/* 480/*
433 * destroy the stacks, the callchain etc... 481 * destroy the stacks, the callchain etc...
434 */ 482 */
435static void 483static void
436coro_destroy_stacks () 484coro_destroy_stacks ()
437{ 485{
438 if (!IN_DESTRUCT) 486 if (!IN_DESTRUCT)
439 { 487 {
440 /* is this ugly, I ask? */ 488 /* restore all saved variables and stuff */
441 LEAVE_SCOPE (0); 489 LEAVE_SCOPE (0);
490 assert (PL_tmps_floor == -1);
442 491
443 /* sure it is, but more important: is it correct?? :/ */ 492 /* free all temporaries */
444 FREETMPS; 493 FREETMPS;
494 assert (PL_tmps_ix == -1);
445 495
496 /* unwind all extra stacks */
446 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ 497 POPSTACK_TO (PL_mainstack);
498
499 /* unwind main stack */
500 dounwind (-1);
447 } 501 }
448 502
449 while (PL_curstackinfo->si_next) 503 while (PL_curstackinfo->si_next)
450 PL_curstackinfo = PL_curstackinfo->si_next; 504 PL_curstackinfo = PL_curstackinfo->si_next;
451 505
452 while (PL_curstackinfo) 506 while (PL_curstackinfo)
453 { 507 {
454 PERL_SI *p = PL_curstackinfo->si_prev; 508 PERL_SI *p = PL_curstackinfo->si_prev;
455 509
456 { /*D*//*remove*/
457 dSP;
458 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
459 PUTBACK; /* possibly superfluous */
460 }
461
462 if (!IN_DESTRUCT) 510 if (!IN_DESTRUCT)
463 {
464 dounwind (-1);/*D*//*remove*/
465 SvREFCNT_dec (PL_curstackinfo->si_stack); 511 SvREFCNT_dec (PL_curstackinfo->si_stack);
466 }
467 512
468 Safefree (PL_curstackinfo->si_cxstack); 513 Safefree (PL_curstackinfo->si_cxstack);
469 Safefree (PL_curstackinfo); 514 Safefree (PL_curstackinfo);
470 PL_curstackinfo = p; 515 PL_curstackinfo = p;
471 } 516 }
477#if !PERL_VERSION_ATLEAST (5,9,0) 522#if !PERL_VERSION_ATLEAST (5,9,0)
478 Safefree (PL_retstack); 523 Safefree (PL_retstack);
479#endif 524#endif
480} 525}
481 526
527/** coroutine stack handling ************************************************/
528
482static void 529static void
483setup_coro (struct coro *coro) 530setup_coro (struct coro *coro)
484{ 531{
485 /* 532 /*
486 * emulate part of the perl startup here. 533 * emulate part of the perl startup here.
497 544
498 { 545 {
499 dSP; 546 dSP;
500 LOGOP myop; 547 LOGOP myop;
501 548
502 /* I have no idea why this is needed, but it is */
503 PUSHMARK (SP);
504
505 SvREFCNT_dec (GvAV (PL_defgv)); 549 SvREFCNT_dec (GvAV (PL_defgv));
506 GvAV (PL_defgv) = coro->args; coro->args = 0; 550 GvAV (PL_defgv) = coro->args; coro->args = 0;
507 551
508 Zero (&myop, 1, LOGOP); 552 Zero (&myop, 1, LOGOP);
509 myop.op_next = Nullop; 553 myop.op_next = Nullop;
510 myop.op_flags = OPf_WANT_VOID; 554 myop.op_flags = OPf_WANT_VOID;
511 555
512 PL_op = (OP *)&myop;
513
514 PUSHMARK (SP); 556 PUSHMARK (SP);
515 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 557 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
516 PUTBACK; 558 PUTBACK;
559 PL_op = (OP *)&myop;
517 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 560 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
518 SPAGAIN; 561 SPAGAIN;
519 } 562 }
520 563
521 ENTER; /* necessary e.g. for dounwind */ 564 ENTER; /* necessary e.g. for dounwind */
529 SvREFCNT_dec (coro_mortal); 572 SvREFCNT_dec (coro_mortal);
530 coro_mortal = 0; 573 coro_mortal = 0;
531 } 574 }
532} 575}
533 576
577/* inject a fake call to Coro::State::_cctx_init into the execution */
534static void NOINLINE 578static void NOINLINE
535prepare_cctx (coro_cctx *cctx) 579prepare_cctx (coro_cctx *cctx)
536{ 580{
537 dSP; 581 dSP;
538 LOGOP myop; 582 LOGOP myop;
539 583
540 Zero (&myop, 1, LOGOP); 584 Zero (&myop, 1, LOGOP);
541 myop.op_next = PL_op; 585 myop.op_next = PL_op;
542 myop.op_flags = OPf_WANT_VOID; 586 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
543
544 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
545 587
546 PUSHMARK (SP); 588 PUSHMARK (SP);
589 EXTEND (SP, 2);
590 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx))));
547 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 591 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
548 PUTBACK; 592 PUTBACK;
593 PL_op = (OP *)&myop;
549 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 594 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
550 SPAGAIN; 595 SPAGAIN;
551} 596}
552 597
553static void 598static void
554coro_run (void *arg) 599coro_run (void *arg)
555{ 600{
556 /* coro_run is the alternative epilogue of transfer() */ 601 /* coro_run is the alternative tail of transfer(), so unlock here. */
557 UNLOCK; 602 UNLOCK;
558 603
559 /* 604 /*
560 * this is a _very_ stripped down perl interpreter ;) 605 * this is a _very_ stripped down perl interpreter ;)
561 */ 606 */
563 608
564 /* inject call to cctx_init */ 609 /* inject call to cctx_init */
565 prepare_cctx ((coro_cctx *)arg); 610 prepare_cctx ((coro_cctx *)arg);
566 611
567 /* somebody will hit me for both perl_run and PL_restartop */ 612 /* somebody will hit me for both perl_run and PL_restartop */
613 PL_restartop = PL_op;
568 perl_run (PL_curinterp); 614 perl_run (PL_curinterp);
569 615
570 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 616 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
571 abort (); 617 abort ();
572} 618}
576{ 622{
577 coro_cctx *cctx; 623 coro_cctx *cctx;
578 624
579 ++cctx_count; 625 ++cctx_count;
580 626
581 New (0, cctx, 1, coro_cctx); 627 Newz (0, cctx, 1, coro_cctx);
582 628
583#if HAVE_MMAP 629#if HAVE_MMAP
584 630
585 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 631 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
586 /* mmap suppsedly does allocate-on-write for us */ 632 /* mmap supposedly does allocate-on-write for us */
587 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 633 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
588 634
589 if (cctx->sptr == (void *)-1) 635 if (cctx->sptr == (void *)-1)
590 { 636 {
591 perror ("FATAL: unable to mmap stack for coroutine"); 637 perror ("FATAL: unable to mmap stack for coroutine");
679 ++cctx_idle; 725 ++cctx_idle;
680 cctx->next = cctx_first; 726 cctx->next = cctx_first;
681 cctx_first = cctx; 727 cctx_first = cctx;
682} 728}
683 729
730/** coroutine switching *****************************************************/
731
684/* never call directly, always through the coro_state_transfer global variable */ 732/* never call directly, always through the coro_state_transfer global variable */
685static void NOINLINE 733static void NOINLINE
686transfer (struct coro *prev, struct coro *next, int flags) 734transfer (struct coro *prev, struct coro *next)
687{ 735{
688 dSTACKLEVEL; 736 dSTACKLEVEL;
689 737
690 /* sometimes transfer is only called to set idle_sp */ 738 /* sometimes transfer is only called to set idle_sp */
691 if (flags == TRANSFER_SET_STACKLEVEL) 739 if (!next)
740 {
692 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 741 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
742 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
743 }
693 else if (prev != next) 744 else if (prev != next)
694 { 745 {
695 coro_cctx *prev__cctx; 746 coro_cctx *prev__cctx;
696 747
697 if (prev->flags & CF_NEW) 748 if (prev->flags & CF_NEW)
708 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 759 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
709 760
710 if (next->flags & CF_RUNNING) 761 if (next->flags & CF_RUNNING)
711 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 762 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
712 763
764 if (next->flags & CF_DESTROYED)
765 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
766
713 prev->flags &= ~CF_RUNNING; 767 prev->flags &= ~CF_RUNNING;
714 next->flags |= CF_RUNNING; 768 next->flags |= CF_RUNNING;
715 769
716 LOCK; 770 LOCK;
717 771
718 if (next->flags & CF_NEW) 772 if (next->flags & CF_NEW)
719 { 773 {
720 /* need to start coroutine */ 774 /* need to start coroutine */
721 next->flags &= ~CF_NEW; 775 next->flags &= ~CF_NEW;
722 /* first get rid of the old state */ 776 /* first get rid of the old state */
723 SAVE (prev, -1); 777 save_perl (prev);
724 /* setup coroutine call */ 778 /* setup coroutine call */
725 setup_coro (next); 779 setup_coro (next);
726 /* need a new stack */ 780 /* need a new stack */
727 assert (!next->stack); 781 assert (!next->cctx);
728 } 782 }
729 else 783 else
730 { 784 {
731 /* coroutine already started */ 785 /* coroutine already started */
732 SAVE (prev, flags); 786 save_perl (prev);
733 LOAD (next); 787 load_perl (next);
734 } 788 }
735 789
736 prev__cctx = prev->cctx; 790 prev__cctx = prev->cctx;
737 791
738 /* possibly "free" the cctx */ 792 /* possibly "free" the cctx */
739 if (prev__cctx->idle_sp == STACKLEVEL && 0) 793 if (prev__cctx->idle_sp == STACKLEVEL)
740 { 794 {
741 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 795 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
742 assert (PL_top_env == prev__cctx->top_env); 796 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
743 797
744 prev->cctx = 0; 798 prev->cctx = 0;
745 799
746 cctx_put (prev__cctx); 800 cctx_put (prev__cctx);
747 prev__cctx->inuse = 0; 801 prev__cctx->inuse = 0;
760 PL_top_env = next->cctx->top_env; 814 PL_top_env = next->cctx->top_env;
761 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 815 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
762 } 816 }
763 817
764 free_coro_mortal (); 818 free_coro_mortal ();
765
766 UNLOCK; 819 UNLOCK;
767 } 820 }
768} 821}
769 822
770struct transfer_args 823struct transfer_args
771{ 824{
772 struct coro *prev, *next; 825 struct coro *prev, *next;
773 int flags;
774}; 826};
775 827
776#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) 828#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
777 829
830/** high level stuff ********************************************************/
831
778static void 832static int
779coro_state_destroy (struct coro *coro) 833coro_state_destroy (struct coro *coro)
780{ 834{
781 if (coro->refcnt--) 835 if (coro->flags & CF_DESTROYED)
782 return; 836 return 0;
837
838 coro->flags |= CF_DESTROYED;
839
840 if (coro->flags & CF_READY)
841 {
842 /* reduce nready, as destroying a ready coro effectively unreadies it */
843 /* alternative: look through all ready queues and remove the coro */
844 LOCK;
845 --coro_nready;
846 UNLOCK;
847 }
848 else
849 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
783 850
784 if (coro->mainstack && coro->mainstack != main_mainstack) 851 if (coro->mainstack && coro->mainstack != main_mainstack)
785 { 852 {
786 struct coro temp; 853 struct coro temp;
854
855 assert (!(coro->flags & CF_RUNNING));
856
857 Zero (&temp, 1, struct coro);
858 temp.save = CORO_SAVE_ALL;
787 859
788 if (coro->flags & CF_RUNNING) 860 if (coro->flags & CF_RUNNING)
789 croak ("FATAL: tried to destroy currently running coroutine"); 861 croak ("FATAL: tried to destroy currently running coroutine");
790 862
791 SAVE ((&temp), TRANSFER_SAVE_ALL); 863 save_perl (&temp);
792 LOAD (coro); 864 load_perl (coro);
793 865
794 coro_destroy_stacks (); 866 coro_destroy_stacks ();
795 867
796 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 868 load_perl (&temp); /* this will get rid of defsv etc.. */
797 869
798 coro->mainstack = 0; 870 coro->mainstack = 0;
799 } 871 }
800 872
801 cctx_destroy (coro->cctx); 873 cctx_destroy (coro->cctx);
802 SvREFCNT_dec (coro->args); 874 SvREFCNT_dec (coro->args);
803 Safefree (coro); 875
876 return 1;
804} 877}
805 878
806static int 879static int
807coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 880coro_state_free (pTHX_ SV *sv, MAGIC *mg)
808{ 881{
809 struct coro *coro = (struct coro *)mg->mg_ptr; 882 struct coro *coro = (struct coro *)mg->mg_ptr;
810 mg->mg_ptr = 0; 883 mg->mg_ptr = 0;
811 884
885 if (--coro->refcnt < 0)
886 {
812 coro_state_destroy (coro); 887 coro_state_destroy (coro);
888 Safefree (coro);
889 }
813 890
814 return 0; 891 return 0;
815} 892}
816 893
817static int 894static int
824 return 0; 901 return 0;
825} 902}
826 903
827static MGVTBL coro_state_vtbl = { 904static MGVTBL coro_state_vtbl = {
828 0, 0, 0, 0, 905 0, 0, 0, 0,
829 coro_state_clear, 906 coro_state_free,
830 0, 907 0,
831#ifdef MGf_DUP 908#ifdef MGf_DUP
832 coro_state_dup, 909 coro_state_dup,
833#else 910#else
834# define MGf_DUP 0 911# define MGf_DUP 0
856 assert (mg->mg_type == PERL_MAGIC_ext); 933 assert (mg->mg_type == PERL_MAGIC_ext);
857 return (struct coro *)mg->mg_ptr; 934 return (struct coro *)mg->mg_ptr;
858} 935}
859 936
860static void 937static void
861prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) 938prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
862{ 939{
863 ta->prev = SvSTATE (prev); 940 ta->prev = SvSTATE (prev_sv);
864 ta->next = SvSTATE (next); 941 ta->next = SvSTATE (next_sv);
865 ta->flags = flags;
866} 942}
867 943
868static void 944static void
869api_transfer (SV *prev, SV *next, int flags) 945api_transfer (SV *prev_sv, SV *next_sv)
870{ 946{
871 struct transfer_args ta; 947 struct transfer_args ta;
872 948
873 prepare_transfer (&ta, prev, next, flags); 949 prepare_transfer (&ta, prev_sv, next_sv);
874 TRANSFER (ta); 950 TRANSFER (ta);
875} 951}
876 952
953static int
954api_save (SV *coro_sv, int new_save)
955{
956 struct coro *coro = SvSTATE (coro_sv);
957 int old_save = coro->save;
958
959 if (new_save >= 0)
960 coro->save = new_save;
961
962 return old_save;
963}
964
877/** Coro ********************************************************************/ 965/** Coro ********************************************************************/
878 966
879#define PRIO_MAX 3
880#define PRIO_HIGH 1
881#define PRIO_NORMAL 0
882#define PRIO_LOW -1
883#define PRIO_IDLE -3
884#define PRIO_MIN -4
885
886/* for Coro.pm */
887static SV *coro_current;
888static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
889static int coro_nready;
890
891static void 967static void
892coro_enq (SV *coro_sv) 968coro_enq (SV *coro_sv)
893{ 969{
894 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 970 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
895 coro_nready++;
896} 971}
897 972
898static SV * 973static SV *
899coro_deq (int min_prio) 974coro_deq (int min_prio)
900{ 975{
904 if (min_prio < 0) 979 if (min_prio < 0)
905 min_prio = 0; 980 min_prio = 0;
906 981
907 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 982 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
908 if (AvFILLp (coro_ready [prio]) >= 0) 983 if (AvFILLp (coro_ready [prio]) >= 0)
909 {
910 coro_nready--;
911 return av_shift (coro_ready [prio]); 984 return av_shift (coro_ready [prio]);
912 }
913 985
914 return 0; 986 return 0;
915} 987}
916 988
917static int 989static int
925 coro = SvSTATE (coro_sv); 997 coro = SvSTATE (coro_sv);
926 998
927 if (coro->flags & CF_READY) 999 if (coro->flags & CF_READY)
928 return 0; 1000 return 0;
929 1001
930#if 0 /* this is actually harmless */
931 if (coro->flags & CF_RUNNING)
932 croak ("Coro::ready called on currently running coroutine");
933#endif
934
935 coro->flags |= CF_READY; 1002 coro->flags |= CF_READY;
936 1003
937 LOCK; 1004 LOCK;
938 coro_enq (SvREFCNT_inc (coro_sv)); 1005 coro_enq (SvREFCNT_inc (coro_sv));
1006 ++coro_nready;
939 UNLOCK; 1007 UNLOCK;
940 1008
941 return 1; 1009 return 1;
942} 1010}
943 1011
944static int 1012static int
945api_is_ready (SV *coro_sv) 1013api_is_ready (SV *coro_sv)
946{ 1014{
947 return !!SvSTATE (coro_sv)->flags & CF_READY; 1015 return !!(SvSTATE (coro_sv)->flags & CF_READY);
948} 1016}
949 1017
950static void 1018static void
951prepare_schedule (struct transfer_args *ta) 1019prepare_schedule (struct transfer_args *ta)
952{ 1020{
953 SV *prev, *next; 1021 SV *prev_sv, *next_sv;
954 1022
955 for (;;) 1023 for (;;)
956 { 1024 {
957 LOCK; 1025 LOCK;
958 next = coro_deq (PRIO_MIN); 1026 next_sv = coro_deq (PRIO_MIN);
1027
1028 /* nothing to schedule: call the idle handler */
1029 if (!next_sv)
1030 {
1031 dSP;
1032 UNLOCK;
1033
1034 ENTER;
1035 SAVETMPS;
1036
1037 PUSHMARK (SP);
1038 PUTBACK;
1039 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1040
1041 FREETMPS;
1042 LEAVE;
1043 continue;
1044 }
1045
1046 ta->next = SvSTATE (next_sv);
1047
1048 /* cannot transfer to destroyed coros, skip and look for next */
1049 if (ta->next->flags & CF_DESTROYED)
1050 {
1051 UNLOCK;
1052 SvREFCNT_dec (next_sv);
1053 /* coro_nready is already taken care of by destroy */
1054 continue;
1055 }
1056
1057 --coro_nready;
959 UNLOCK; 1058 UNLOCK;
960
961 if (next)
962 break; 1059 break;
963
964 {
965 dSP;
966
967 ENTER;
968 SAVETMPS;
969
970 PUSHMARK (SP);
971 PUTBACK;
972 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
973
974 FREETMPS;
975 LEAVE;
976 } 1060 }
977 }
978
979 prev = SvRV (coro_current);
980 SvRV_set (coro_current, next);
981 1061
982 /* free this only after the transfer */ 1062 /* free this only after the transfer */
1063 prev_sv = SvRV (coro_current);
1064 SvRV_set (coro_current, next_sv);
1065 ta->prev = SvSTATE (prev_sv);
1066
1067 assert (ta->next->flags & CF_READY);
1068 ta->next->flags &= ~CF_READY;
1069
983 LOCK; 1070 LOCK;
984 free_coro_mortal (); 1071 free_coro_mortal ();
1072 coro_mortal = prev_sv;
985 UNLOCK; 1073 UNLOCK;
986 coro_mortal = prev;
987
988 assert (!SvROK(prev));//D
989 assert (!SvROK(next));//D
990
991 ta->prev = SvSTATE (prev);
992 ta->next = SvSTATE (next);
993 ta->flags = TRANSFER_SAVE_ALL;
994
995 assert (ta->flags & CF_READY);
996 ta->next->flags &= ~CF_READY;
997} 1074}
998 1075
999static void 1076static void
1000prepare_cede (struct transfer_args *ta) 1077prepare_cede (struct transfer_args *ta)
1001{ 1078{
1002 api_ready (coro_current); 1079 api_ready (coro_current);
1003
1004 prepare_schedule (ta); 1080 prepare_schedule (ta);
1081}
1082
1083static int
1084prepare_cede_notself (struct transfer_args *ta)
1085{
1086 if (coro_nready)
1087 {
1088 SV *prev = SvRV (coro_current);
1089 prepare_schedule (ta);
1090 api_ready (prev);
1091 return 1;
1092 }
1093 else
1094 return 0;
1005} 1095}
1006 1096
1007static void 1097static void
1008api_schedule (void) 1098api_schedule (void)
1009{ 1099{
1011 1101
1012 prepare_schedule (&ta); 1102 prepare_schedule (&ta);
1013 TRANSFER (ta); 1103 TRANSFER (ta);
1014} 1104}
1015 1105
1016static void 1106static int
1017api_cede (void) 1107api_cede (void)
1018{ 1108{
1019 struct transfer_args ta; 1109 struct transfer_args ta;
1020 1110
1021 prepare_cede (&ta); 1111 prepare_cede (&ta);
1112
1113 if (ta.prev != ta.next)
1114 {
1022 TRANSFER (ta); 1115 TRANSFER (ta);
1116 return 1;
1117 }
1118 else
1119 return 0;
1120}
1121
1122static int
1123api_cede_notself (void)
1124{
1125 struct transfer_args ta;
1126
1127 if (prepare_cede_notself (&ta))
1128 {
1129 TRANSFER (ta);
1130 return 1;
1131 }
1132 else
1133 return 0;
1023} 1134}
1024 1135
1025MODULE = Coro::State PACKAGE = Coro::State 1136MODULE = Coro::State PACKAGE = Coro::State
1026 1137
1027PROTOTYPES: DISABLE 1138PROTOTYPES: DISABLE
1033#endif 1144#endif
1034 BOOT_PAGESIZE; 1145 BOOT_PAGESIZE;
1035 1146
1036 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1147 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1037 1148
1038 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1149 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1039 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1150 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1040 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 1151 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1152 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1153 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1041 1154
1042 main_mainstack = PL_mainstack; 1155 main_mainstack = PL_mainstack;
1043 1156
1044 coroapi.ver = CORO_API_VERSION; 1157 coroapi.ver = CORO_API_VERSION;
1045 coroapi.transfer = api_transfer; 1158 coroapi.transfer = api_transfer;
1055 HV *hv; 1168 HV *hv;
1056 int i; 1169 int i;
1057 1170
1058 Newz (0, coro, 1, struct coro); 1171 Newz (0, coro, 1, struct coro);
1059 coro->args = newAV (); 1172 coro->args = newAV ();
1173 coro->save = CORO_SAVE_ALL;
1060 coro->flags = CF_NEW; 1174 coro->flags = CF_NEW;
1061 1175
1062 hv = newHV (); 1176 hv = newHV ();
1063 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1177 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1064 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1178 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1065 1179
1066 for (i = 1; i < items; i++) 1180 for (i = 1; i < items; i++)
1067 av_push (coro->args, newSVsv (ST (i))); 1181 av_push (coro->args, newSVsv (ST (i)));
1068} 1182}
1069 OUTPUT: 1183 OUTPUT:
1184 RETVAL
1185
1186int
1187save (SV *coro, int new_save = -1)
1188 CODE:
1189 RETVAL = api_save (coro, new_save);
1190 OUTPUT:
1070 RETVAL 1191 RETVAL
1071 1192
1072void 1193void
1073_set_stacklevel (...) 1194_set_stacklevel (...)
1074 ALIAS: 1195 ALIAS:
1075 Coro::State::transfer = 1 1196 Coro::State::transfer = 1
1076 Coro::schedule = 2 1197 Coro::schedule = 2
1077 Coro::cede = 3 1198 Coro::cede = 3
1199 Coro::cede_notself = 4
1078 CODE: 1200 CODE:
1079{ 1201{
1080 struct transfer_args ta; 1202 struct transfer_args ta;
1081 1203
1082 switch (ix) 1204 switch (ix)
1083 { 1205 {
1084 case 0: 1206 case 0:
1085 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1207 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1086 ta.next = 0; 1208 ta.next = 0;
1087 ta.flags = TRANSFER_SET_STACKLEVEL;
1088 break; 1209 break;
1089 1210
1090 case 1: 1211 case 1:
1091 if (items != 3) 1212 if (items != 2)
1092 croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items); 1213 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1093 1214
1094 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1215 prepare_transfer (&ta, ST (0), ST (1));
1095 break; 1216 break;
1096 1217
1097 case 2: 1218 case 2:
1098 prepare_schedule (&ta); 1219 prepare_schedule (&ta);
1099 break; 1220 break;
1100 1221
1101 case 3: 1222 case 3:
1102 prepare_cede (&ta); 1223 prepare_cede (&ta);
1103 break; 1224 break;
1225
1226 case 4:
1227 if (!prepare_cede_notself (&ta))
1228 XSRETURN_EMPTY;
1229
1230 break;
1104 } 1231 }
1105 1232
1233 BARRIER;
1106 TRANSFER (ta); 1234 TRANSFER (ta);
1107}
1108 1235
1109void 1236 if (GIMME_V != G_VOID && ta.next != ta.prev)
1110_clone_state_from (SV *dst, SV *src) 1237 XSRETURN_YES;
1238}
1239
1240bool
1241_destroy (SV *coro_sv)
1111 CODE: 1242 CODE:
1112{ 1243 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1113 struct coro *coro_src = SvSTATE (src); 1244 OUTPUT:
1114 1245 RETVAL
1115 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1116
1117 ++coro_src->refcnt;
1118 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1119}
1120 1246
1121void 1247void
1122_exit (code) 1248_exit (code)
1123 int code 1249 int code
1124 PROTOTYPE: $ 1250 PROTOTYPE: $
1161 coro_ready[i] = newAV (); 1287 coro_ready[i] = newAV ();
1162 1288
1163 { 1289 {
1164 SV *sv = perl_get_sv("Coro::API", 1); 1290 SV *sv = perl_get_sv("Coro::API", 1);
1165 1291
1166 coroapi.schedule = api_schedule; 1292 coroapi.schedule = api_schedule;
1293 coroapi.save = api_save;
1167 coroapi.cede = api_cede; 1294 coroapi.cede = api_cede;
1295 coroapi.cede_notself = api_cede_notself;
1168 coroapi.ready = api_ready; 1296 coroapi.ready = api_ready;
1169 coroapi.is_ready = api_is_ready; 1297 coroapi.is_ready = api_is_ready;
1170 coroapi.nready = &coro_nready; 1298 coroapi.nready = &coro_nready;
1171 coroapi.current = coro_current; 1299 coroapi.current = coro_current;
1172 1300
1173 GCoroAPI = &coroapi; 1301 GCoroAPI = &coroapi;
1174 sv_setiv (sv, (IV)&coroapi); 1302 sv_setiv (sv, (IV)&coroapi);
1175 SvREADONLY_on (sv); 1303 SvREADONLY_on (sv);
1176 } 1304 }
1177} 1305}
1306
1307void
1308_set_current (SV *current)
1309 PROTOTYPE: $
1310 CODE:
1311 SvREFCNT_dec (SvRV (coro_current));
1312 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1178 1313
1179int 1314int
1180prio (Coro::State coro, int newprio = 0) 1315prio (Coro::State coro, int newprio = 0)
1181 ALIAS: 1316 ALIAS:
1182 nice = 1 1317 nice = 1
1185 RETVAL = coro->prio; 1320 RETVAL = coro->prio;
1186 1321
1187 if (items > 1) 1322 if (items > 1)
1188 { 1323 {
1189 if (ix) 1324 if (ix)
1190 newprio += coro->prio; 1325 newprio = coro->prio - newprio;
1191 1326
1192 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1327 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1193 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1328 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1194 1329
1195 coro->prio = newprio; 1330 coro->prio = newprio;
1196 } 1331 }
1197} 1332}
1333 OUTPUT:
1334 RETVAL
1198 1335
1199SV * 1336SV *
1200ready (SV *self) 1337ready (SV *self)
1201 PROTOTYPE: $ 1338 PROTOTYPE: $
1202 CODE: 1339 CODE:
1218 CODE: 1355 CODE:
1219 RETVAL = coro_nready; 1356 RETVAL = coro_nready;
1220 OUTPUT: 1357 OUTPUT:
1221 RETVAL 1358 RETVAL
1222 1359
1223void
1224_set_current (SV *current)
1225 PROTOTYPE: $
1226 CODE:
1227 SvREFCNT_dec (SvRV (coro_current));
1228 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1229
1230MODULE = Coro::State PACKAGE = Coro::AIO 1360MODULE = Coro::State PACKAGE = Coro::AIO
1231 1361
1232SV * 1362SV *
1233_get_state () 1363_get_state ()
1234 CODE: 1364 CODE:
1235{ 1365{
1236 struct { 1366 struct io_state *data;
1237 int errorno;
1238 int laststype;
1239 int laststatval;
1240 Stat_t statcache;
1241 } data;
1242 1367
1368 RETVAL = newSV (sizeof (struct io_state));
1369 data = (struct io_state *)SvPVX (RETVAL);
1370 SvCUR_set (RETVAL, sizeof (struct io_state));
1371 SvPOK_only (RETVAL);
1372
1243 data.errorno = errno; 1373 data->errorno = errno;
1244 data.laststype = PL_laststype; 1374 data->laststype = PL_laststype;
1245 data.laststatval = PL_laststatval; 1375 data->laststatval = PL_laststatval;
1246 data.statcache = PL_statcache; 1376 data->statcache = PL_statcache;
1247
1248 RETVAL = newSVpvn ((char *)&data, sizeof data);
1249} 1377}
1250 OUTPUT: 1378 OUTPUT:
1251 RETVAL 1379 RETVAL
1252 1380
1253void 1381void
1254_set_state (char *data_) 1382_set_state (char *data_)
1255 PROTOTYPE: $ 1383 PROTOTYPE: $
1256 CODE: 1384 CODE:
1257{ 1385{
1258 struct { 1386 struct io_state *data = (void *)data_;
1259 int errorno;
1260 int laststype;
1261 int laststatval;
1262 Stat_t statcache;
1263 } *data = (void *)data_;
1264 1387
1265 errno = data->errorno; 1388 errno = data->errorno;
1266 PL_laststype = data->laststype; 1389 PL_laststype = data->laststype;
1267 PL_laststatval = data->laststatval; 1390 PL_laststatval = data->laststatval;
1268 PL_statcache = data->statcache; 1391 PL_statcache = data->statcache;
1269} 1392}
1393

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines