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.128 by root, Fri Dec 22 04:22:28 2006 UTC vs.
Revision 1.152 by root, Thu Sep 20 12:02:25 2007 UTC

1#include "libcoro/coro.c" 1#include "libcoro/coro.c"
2
3#define PERL_NO_GET_CONTEXT
2 4
3#include "EXTERN.h" 5#include "EXTERN.h"
4#include "perl.h" 6#include "perl.h"
5#include "XSUB.h" 7#include "XSUB.h"
6 8
31#else 33#else
32# define PAGESIZE 0 34# define PAGESIZE 0
33# define BOOT_PAGESIZE (void)0 35# define BOOT_PAGESIZE (void)0
34#endif 36#endif
35 37
36#if USE_VALGRIND 38#if CORO_USE_VALGRIND
37# include <valgrind/valgrind.h> 39# include <valgrind/valgrind.h>
40# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end))
41#else
42# define REGISTER_STACK(cctx,start,end)
38#endif 43#endif
39 44
40/* the maximum number of idle cctx that will be pooled */ 45/* the maximum number of idle cctx that will be pooled */
41#define MAX_IDLE_CCTX 8 46#define MAX_IDLE_CCTX 8
42 47
71#ifndef SvRV_set 76#ifndef SvRV_set
72# define SvRV_set(s,v) SvRV(s) = (v) 77# define SvRV_set(s,v) SvRV(s) = (v)
73#endif 78#endif
74 79
75#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 80#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
76# undef STACKGUARD 81# undef CORO_STACKGUARD
77#endif 82#endif
78 83
79#ifndef STACKGUARD 84#ifndef CORO_STACKGUARD
80# define STACKGUARD 0 85# define CORO_STACKGUARD 0
81#endif 86#endif
82 87
83/* prefer perl internal functions over our own? */ 88/* prefer perl internal functions over our own? */
84#ifndef PREFER_PERL_FUNCTIONS 89#ifndef CORO_PREFER_PERL_FUNCTIONS
85# define PREFER_PERL_FUNCTIONS 0 90# define CORO_PREFER_PERL_FUNCTIONS 0
86#endif 91#endif
87 92
88/* The next macro should declare a variable stacklevel that contains and approximation 93/* The next macro should declare a variable stacklevel that contains and approximation
89 * to the current C stack pointer. Its property is that it changes with each call 94 * to the current C stack pointer. Its property is that it changes with each call
90 * and should be unique. */ 95 * and should be unique. */
93 98
94#define IN_DESTRUCT (PL_main_cv == Nullcv) 99#define IN_DESTRUCT (PL_main_cv == Nullcv)
95 100
96#if __GNUC__ >= 3 101#if __GNUC__ >= 3
97# define attribute(x) __attribute__(x) 102# define attribute(x) __attribute__(x)
103# define BARRIER __asm__ __volatile__ ("" : : : "memory")
98#else 104#else
99# define attribute(x) 105# define attribute(x)
106# define BARRIER
100#endif 107#endif
101 108
102#define NOINLINE attribute ((noinline)) 109#define NOINLINE attribute ((noinline))
103 110
104#include "CoroAPI.h" 111#include "CoroAPI.h"
110#else 117#else
111# define LOCK (void)0 118# define LOCK (void)0
112# define UNLOCK (void)0 119# define UNLOCK (void)0
113#endif 120#endif
114 121
122/* helper storage struct for Coro::AIO */
115struct io_state 123struct io_state
116{ 124{
117 int errorno; 125 int errorno;
118 I32 laststype; 126 I32 laststype;
119 int laststatval; 127 int laststatval;
120 Stat_t statcache; 128 Stat_t statcache;
121}; 129};
122 130
131static size_t coro_stacksize = CORO_STACKSIZE;
123static struct CoroAPI coroapi; 132static struct CoroAPI coroapi;
124static AV *main_mainstack; /* used to differentiate between $main and others */ 133static AV *main_mainstack; /* used to differentiate between $main and others */
134static JMPENV *main_top_env;
125static HV *coro_state_stash, *coro_stash; 135static HV *coro_state_stash, *coro_stash;
126static SV *coro_mortal; /* will be freed after next transfer */ 136static SV *coro_mortal; /* will be freed after next transfer */
127 137
128static struct coro_cctx *cctx_first; 138static struct coro_cctx *cctx_first;
129static int cctx_count, cctx_idle; 139static int cctx_count, cctx_idle;
132typedef struct coro_cctx { 142typedef struct coro_cctx {
133 struct coro_cctx *next; 143 struct coro_cctx *next;
134 144
135 /* the stack */ 145 /* the stack */
136 void *sptr; 146 void *sptr;
137 long ssize; /* positive == mmap, otherwise malloc */ 147 size_t ssize;
138 148
139 /* cpu state */ 149 /* cpu state */
140 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 150 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
151 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
141 JMPENV *top_env; 152 JMPENV *top_env;
142 coro_context cctx; 153 coro_context cctx;
143 154
144 int inuse;
145
146#if USE_VALGRIND 155#if CORO_USE_VALGRIND
147 int valgrind_id; 156 int valgrind_id;
148#endif 157#endif
158 char inuse, mapped;
149} coro_cctx; 159} coro_cctx;
150 160
151enum { 161enum {
152 CF_RUNNING = 0x0001, /* coroutine is running */ 162 CF_RUNNING = 0x0001, /* coroutine is running */
153 CF_READY = 0x0002, /* coroutine is ready */ 163 CF_READY = 0x0002, /* coroutine is ready */
154 CF_NEW = 0x0004, /* ahs never been switched to */ 164 CF_NEW = 0x0004, /* has never been switched to */
165 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
155}; 166};
156 167
157/* this is a structure representing a perl-level coroutine */ 168/* this is a structure representing a perl-level coroutine */
158struct coro { 169struct coro {
159 /* the c coroutine allocated to this perl coroutine, if any */ 170 /* the c coroutine allocated to this perl coroutine, if any */
167 178
168 /* optionally saved, might be zero */ 179 /* optionally saved, might be zero */
169 AV *defav; /* @_ */ 180 AV *defav; /* @_ */
170 SV *defsv; /* $_ */ 181 SV *defsv; /* $_ */
171 SV *errsv; /* $@ */ 182 SV *errsv; /* $@ */
183 GV *deffh; /* default filehandle */
172 SV *irssv; /* $/ */ 184 SV *irssv; /* $/ */
173 SV *irssv_sv; /* real $/ cache */ 185 SV *irssv_sv; /* real $/ cache */
174 186
175#define VAR(name,type) type name; 187#define VAR(name,type) type name;
176# include "state.h" 188# include "state.h"
177#undef VAR 189#undef VAR
178 190
179 /* coro process data */ 191 /* coro process data */
180 int prio; 192 int prio;
193
194 /* linked list */
195 struct coro *next, *prev;
196 HV *hv; /* the perl hash associated with this coro, if any */
181}; 197};
182 198
183typedef struct coro *Coro__State; 199typedef struct coro *Coro__State;
184typedef struct coro *Coro__State_or_hashref; 200typedef struct coro *Coro__State_or_hashref;
185 201
202/** Coro ********************************************************************/
203
204#define PRIO_MAX 3
205#define PRIO_HIGH 1
206#define PRIO_NORMAL 0
207#define PRIO_LOW -1
208#define PRIO_IDLE -3
209#define PRIO_MIN -4
210
211/* for Coro.pm */
212static SV *coro_current;
213static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
214static int coro_nready;
215static struct coro *first;
216
217/** lowlevel stuff **********************************************************/
218
186static AV * 219static AV *
187coro_clone_padlist (CV *cv) 220coro_clone_padlist (pTHX_ CV *cv)
188{ 221{
189 AV *padlist = CvPADLIST (cv); 222 AV *padlist = CvPADLIST (cv);
190 AV *newpadlist, *newpad; 223 AV *newpadlist, *newpad;
191 224
192 newpadlist = newAV (); 225 newpadlist = newAV ();
204 237
205 return newpadlist; 238 return newpadlist;
206} 239}
207 240
208static void 241static void
209free_padlist (AV *padlist) 242free_padlist (pTHX_ AV *padlist)
210{ 243{
211 /* may be during global destruction */ 244 /* may be during global destruction */
212 if (SvREFCNT (padlist)) 245 if (SvREFCNT (padlist))
213 { 246 {
214 I32 i = AvFILLp (padlist); 247 I32 i = AvFILLp (padlist);
235 AV *padlist; 268 AV *padlist;
236 AV *av = (AV *)mg->mg_obj; 269 AV *av = (AV *)mg->mg_obj;
237 270
238 /* casting is fun. */ 271 /* casting is fun. */
239 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 272 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
240 free_padlist (padlist); 273 free_padlist (aTHX_ padlist);
241 274
242 SvREFCNT_dec (av); 275 SvREFCNT_dec (av);
243 276
244 return 0; 277 return 0;
245} 278}
255 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 288 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
256 : 0 289 : 0
257 290
258/* the next two functions merely cache the padlists */ 291/* the next two functions merely cache the padlists */
259static void 292static void
260get_padlist (CV *cv) 293get_padlist (pTHX_ CV *cv)
261{ 294{
262 MAGIC *mg = CORO_MAGIC (cv); 295 MAGIC *mg = CORO_MAGIC (cv);
263 AV *av; 296 AV *av;
264 297
265 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 298 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
266 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 299 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
267 else 300 else
268 { 301 {
269#if PREFER_PERL_FUNCTIONS 302#if CORO_PREFER_PERL_FUNCTIONS
270 /* this is probably cleaner, but also slower? */ 303 /* this is probably cleaner, but also slower? */
271 CV *cp = Perl_cv_clone (cv); 304 CV *cp = Perl_cv_clone (cv);
272 CvPADLIST (cv) = CvPADLIST (cp); 305 CvPADLIST (cv) = CvPADLIST (cp);
273 CvPADLIST (cp) = 0; 306 CvPADLIST (cp) = 0;
274 SvREFCNT_dec (cp); 307 SvREFCNT_dec (cp);
275#else 308#else
276 CvPADLIST (cv) = coro_clone_padlist (cv); 309 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
277#endif 310#endif
278 } 311 }
279} 312}
280 313
281static void 314static void
282put_padlist (CV *cv) 315put_padlist (pTHX_ CV *cv)
283{ 316{
284 MAGIC *mg = CORO_MAGIC (cv); 317 MAGIC *mg = CORO_MAGIC (cv);
285 AV *av; 318 AV *av;
286 319
287 if (!mg) 320 if (!mg)
298 av_extend (av, AvMAX (av) + 1); 331 av_extend (av, AvMAX (av) + 1);
299 332
300 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 333 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
301} 334}
302 335
336/** load & save, init *******************************************************/
337
303#define SB do { 338#define SB do {
304#define SE } while (0) 339#define SE } while (0)
305 340
306#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE 341#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
307 342
308static void 343static void
309load_perl (Coro__State c) 344load_perl (pTHX_ Coro__State c)
310{ 345{
311#define VAR(name,type) PL_ ## name = c->name; 346#define VAR(name,type) PL_ ## name = c->name;
312# include "state.h" 347# include "state.h"
313#undef VAR 348#undef VAR
314 349
315 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 350 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
316 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 351 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
317 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 352 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
353 if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh);
354
318 if (c->irssv) 355 if (c->irssv)
319 { 356 {
320 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) 357 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
358 {
321 SvREFCNT_dec (c->irssv); 359 SvREFCNT_dec (c->irssv);
360 c->irssv = 0;
361 }
322 else 362 else
323 { 363 {
324 REPLACE_SV (PL_rs, c->irssv); 364 REPLACE_SV (PL_rs, c->irssv);
325 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); 365 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
326 sv_setsv (c->irssv_sv, PL_rs); 366 sv_setsv (c->irssv_sv, PL_rs);
332 CV *cv; 372 CV *cv;
333 373
334 /* now do the ugly restore mess */ 374 /* now do the ugly restore mess */
335 while ((cv = (CV *)POPs)) 375 while ((cv = (CV *)POPs))
336 { 376 {
337 put_padlist (cv); /* mark this padlist as available */ 377 put_padlist (aTHX_ cv); /* mark this padlist as available */
338 CvDEPTH (cv) = PTR2IV (POPs); 378 CvDEPTH (cv) = PTR2IV (POPs);
339 CvPADLIST (cv) = (AV *)POPs; 379 CvPADLIST (cv) = (AV *)POPs;
340 } 380 }
341 381
342 PUTBACK; 382 PUTBACK;
343 } 383 }
344} 384}
345 385
346static void 386static void
347save_perl (Coro__State c) 387save_perl (pTHX_ Coro__State c)
348{ 388{
349 { 389 {
350 dSP; 390 dSP;
351 I32 cxix = cxstack_ix; 391 I32 cxix = cxstack_ix;
352 PERL_CONTEXT *ccstk = cxstack; 392 PERL_CONTEXT *ccstk = cxstack;
364 { 404 {
365 while (cxix >= 0) 405 while (cxix >= 0)
366 { 406 {
367 PERL_CONTEXT *cx = &ccstk[cxix--]; 407 PERL_CONTEXT *cx = &ccstk[cxix--];
368 408
369 if (CxTYPE (cx) == CXt_SUB) 409 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)
370 { 410 {
371 CV *cv = cx->blk_sub.cv; 411 CV *cv = cx->blk_sub.cv;
372 412
373 if (CvDEPTH (cv)) 413 if (CvDEPTH (cv))
374 { 414 {
376 PUSHs ((SV *)CvPADLIST (cv)); 416 PUSHs ((SV *)CvPADLIST (cv));
377 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 417 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
378 PUSHs ((SV *)cv); 418 PUSHs ((SV *)cv);
379 419
380 CvDEPTH (cv) = 0; 420 CvDEPTH (cv) = 0;
381 get_padlist (cv); 421 get_padlist (aTHX_ cv);
382 } 422 }
383 } 423 }
384 } 424 }
385 425
386 if (top_si->si_type == PERLSI_MAIN) 426 if (top_si->si_type == PERLSI_MAIN)
395 } 435 }
396 436
397 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 437 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
398 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 438 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
399 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 439 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
440 c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0;
400 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; 441 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
401 442
402#define VAR(name,type)c->name = PL_ ## name; 443#define VAR(name,type)c->name = PL_ ## name;
403# include "state.h" 444# include "state.h"
404#undef VAR 445#undef VAR
408 * allocate various perl stacks. This is an exact copy 449 * allocate various perl stacks. This is an exact copy
409 * of perl.c:init_stacks, except that it uses less memory 450 * of perl.c:init_stacks, except that it uses less memory
410 * on the (sometimes correct) assumption that coroutines do 451 * on the (sometimes correct) assumption that coroutines do
411 * not usually need a lot of stackspace. 452 * not usually need a lot of stackspace.
412 */ 453 */
413#if PREFER_PERL_FUNCTIONS 454#if CORO_PREFER_PERL_FUNCTIONS
414# define coro_init_stacks init_stacks 455# define coro_init_stacks init_stacks
415#else 456#else
416static void 457static void
417coro_init_stacks () 458coro_init_stacks (pTHX)
418{ 459{
419 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 460 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
420 PL_curstackinfo->si_type = PERLSI_MAIN; 461 PL_curstackinfo->si_type = PERLSI_MAIN;
421 PL_curstack = PL_curstackinfo->si_stack; 462 PL_curstack = PL_curstackinfo->si_stack;
422 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 463 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
456 497
457/* 498/*
458 * destroy the stacks, the callchain etc... 499 * destroy the stacks, the callchain etc...
459 */ 500 */
460static void 501static void
461coro_destroy_stacks () 502coro_destroy_stacks (pTHX)
462{ 503{
463 if (!IN_DESTRUCT) 504 if (!IN_DESTRUCT)
464 { 505 {
465 /* restore all saved variables and stuff */ 506 /* restore all saved variables and stuff */
466 LEAVE_SCOPE (0); 507 LEAVE_SCOPE (0);
468 509
469 /* free all temporaries */ 510 /* free all temporaries */
470 FREETMPS; 511 FREETMPS;
471 assert (PL_tmps_ix == -1); 512 assert (PL_tmps_ix == -1);
472 513
514 /* unwind all extra stacks */
473 POPSTACK_TO (PL_mainstack); 515 POPSTACK_TO (PL_mainstack);
516
517 /* unwind main stack */
518 dounwind (-1);
474 } 519 }
475 520
476 while (PL_curstackinfo->si_next) 521 while (PL_curstackinfo->si_next)
477 PL_curstackinfo = PL_curstackinfo->si_next; 522 PL_curstackinfo = PL_curstackinfo->si_next;
478 523
495#if !PERL_VERSION_ATLEAST (5,9,0) 540#if !PERL_VERSION_ATLEAST (5,9,0)
496 Safefree (PL_retstack); 541 Safefree (PL_retstack);
497#endif 542#endif
498} 543}
499 544
545static size_t
546coro_rss (struct coro *coro)
547{
548 size_t rss = sizeof (coro);
549
550 if (coro->mainstack)
551 {
552 rss += sizeof (coro->curstackinfo);
553 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
554 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
555
556 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
557
558 rss += coro->tmps_max * sizeof (SV *);
559
560 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
561
562 rss += coro->scopestack_max * sizeof (I32);
563
564 rss += coro->savestack_max * sizeof (ANY);
565
566#if !PERL_VERSION_ATLEAST (5,9,0)
567 rss += coro->retstack_max * sizeof (OP *);
568#endif
569 }
570
571 return rss;
572}
573
574/** coroutine stack handling ************************************************/
575
500static void 576static void
501setup_coro (struct coro *coro) 577setup_coro (pTHX_ struct coro *coro)
502{ 578{
503 /* 579 /*
504 * emulate part of the perl startup here. 580 * emulate part of the perl startup here.
505 */ 581 */
506
507 coro_init_stacks (); 582 coro_init_stacks (aTHX);
508 583
509 PL_curcop = &PL_compiling; 584 PL_curcop = &PL_compiling;
510 PL_in_eval = EVAL_NULL; 585 PL_in_eval = EVAL_NULL;
586 PL_comppad = 0;
511 PL_curpm = 0; 587 PL_curpm = 0;
512 PL_localizing = 0; 588 PL_localizing = 0;
513 PL_dirty = 0; 589 PL_dirty = 0;
514 PL_restartop = 0; 590 PL_restartop = 0;
515 591
523 Zero (&myop, 1, LOGOP); 599 Zero (&myop, 1, LOGOP);
524 myop.op_next = Nullop; 600 myop.op_next = Nullop;
525 myop.op_flags = OPf_WANT_VOID; 601 myop.op_flags = OPf_WANT_VOID;
526 602
527 PUSHMARK (SP); 603 PUSHMARK (SP);
528 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 604 XPUSHs (av_shift (GvAV (PL_defgv)));
529 PUTBACK; 605 PUTBACK;
530 PL_op = (OP *)&myop; 606 PL_op = (OP *)&myop;
531 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 607 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
532 SPAGAIN; 608 SPAGAIN;
533 } 609 }
534 610
535 ENTER; /* necessary e.g. for dounwind */ 611 ENTER; /* necessary e.g. for dounwind */
536} 612}
537 613
538static void 614static void
539free_coro_mortal () 615free_coro_mortal (pTHX)
540{ 616{
541 if (coro_mortal) 617 if (coro_mortal)
542 { 618 {
543 SvREFCNT_dec (coro_mortal); 619 SvREFCNT_dec (coro_mortal);
544 coro_mortal = 0; 620 coro_mortal = 0;
545 } 621 }
546} 622}
547 623
548/* inject a fake call to Coro::State::_cctx_init into the execution */ 624/* inject a fake call to Coro::State::_cctx_init into the execution */
625/* _cctx_init should be careful, as it could be called at almost any time */
626/* during execution of a perl program */
549static void NOINLINE 627static void NOINLINE
550prepare_cctx (coro_cctx *cctx) 628prepare_cctx (pTHX_ coro_cctx *cctx)
551{ 629{
552 dSP; 630 dSP;
553 LOGOP myop; 631 LOGOP myop;
554 632
555 Zero (&myop, 1, LOGOP); 633 Zero (&myop, 1, LOGOP);
564 PL_op = (OP *)&myop; 642 PL_op = (OP *)&myop;
565 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 643 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
566 SPAGAIN; 644 SPAGAIN;
567} 645}
568 646
647/*
648 * this is a _very_ stripped down perl interpreter ;)
649 */
569static void 650static void
570coro_run (void *arg) 651coro_run (void *arg)
571{ 652{
653 dTHX;
654
572 /* coro_run is the alternative tail of transfer(), so unlock here. */ 655 /* coro_run is the alternative tail of transfer(), so unlock here. */
573 UNLOCK; 656 UNLOCK;
574 657
575 /*
576 * this is a _very_ stripped down perl interpreter ;)
577 */
578 PL_top_env = &PL_start_env; 658 PL_top_env = &PL_start_env;
579 659
580 /* inject call to cctx_init */ 660 /* inject a fake subroutine call to cctx_init */
581 prepare_cctx ((coro_cctx *)arg); 661 prepare_cctx (aTHX_ (coro_cctx *)arg);
582 662
583 /* somebody will hit me for both perl_run and PL_restartop */ 663 /* somebody or something will hit me for both perl_run and PL_restartop */
584 PL_restartop = PL_op; 664 PL_restartop = PL_op;
585 perl_run (PL_curinterp); 665 perl_run (PL_curinterp);
586 666
587 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 667 /*
588 abort (); 668 * If perl-run returns we assume exit() was being called or the coro
669 * fell off the end, which seems to be the only valid (non-bug)
670 * reason for perl_run to return. We try to exit by jumping to the
671 * bootstrap-time "top" top_env, as we cannot restore the "main"
672 * coroutine as Coro has no such concept
673 */
674 PL_top_env = main_top_env;
675 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
589} 676}
590 677
591static coro_cctx * 678static coro_cctx *
592cctx_new () 679cctx_new ()
593{ 680{
594 coro_cctx *cctx; 681 coro_cctx *cctx;
682 void *stack_start;
683 size_t stack_size;
595 684
596 ++cctx_count; 685 ++cctx_count;
597 686
598 Newz (0, cctx, 1, coro_cctx); 687 Newz (0, cctx, 1, coro_cctx);
599 688
600#if HAVE_MMAP 689#if HAVE_MMAP
601 690
602 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 691 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
603 /* mmap supposedly does allocate-on-write for us */ 692 /* mmap supposedly does allocate-on-write for us */
604 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 693 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
605 694
606 if (cctx->sptr == (void *)-1) 695 if (cctx->sptr != (void *)-1)
607 {
608 perror ("FATAL: unable to mmap stack for coroutine");
609 _exit (EXIT_FAILURE);
610 } 696 {
611
612# if STACKGUARD 697# if CORO_STACKGUARD
613 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 698 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
614# endif 699# endif
700 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
701 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
702 cctx->mapped = 1;
703 }
704 else
705#endif
706 {
707 cctx->ssize = coro_stacksize * (long)sizeof (long);
708 New (0, cctx->sptr, coro_stacksize, long);
615 709
616#else
617
618 cctx->ssize = STACKSIZE * (long)sizeof (long);
619 New (0, cctx->sptr, STACKSIZE, long);
620
621 if (!cctx->sptr) 710 if (!cctx->sptr)
622 { 711 {
623 perror ("FATAL: unable to malloc stack for coroutine"); 712 perror ("FATAL: unable to allocate stack for coroutine");
624 _exit (EXIT_FAILURE); 713 _exit (EXIT_FAILURE);
625 } 714 }
626 715
627#endif 716 stack_start = cctx->sptr;
717 stack_size = cctx->ssize;
718 }
628 719
629#if USE_VALGRIND 720 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size);
630 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
631 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
632 cctx->ssize + (char *)cctx->sptr
633 );
634#endif
635
636 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); 721 coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size);
637 722
638 return cctx; 723 return cctx;
639} 724}
640 725
641static void 726static void
644 if (!cctx) 729 if (!cctx)
645 return; 730 return;
646 731
647 --cctx_count; 732 --cctx_count;
648 733
649#if USE_VALGRIND 734#if CORO_USE_VALGRIND
650 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 735 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
651#endif 736#endif
652 737
653#if HAVE_MMAP 738#if HAVE_MMAP
739 if (cctx->mapped)
654 munmap (cctx->sptr, cctx->ssize); 740 munmap (cctx->sptr, cctx->ssize);
655#else 741 else
742#endif
656 Safefree (cctx->sptr); 743 Safefree (cctx->sptr);
657#endif
658 744
659 Safefree (cctx); 745 Safefree (cctx);
660} 746}
661 747
662static coro_cctx * 748static coro_cctx *
663cctx_get () 749cctx_get (pTHX)
664{ 750{
665 coro_cctx *cctx;
666
667 if (cctx_first) 751 while (cctx_first)
668 { 752 {
669 cctx = cctx_first; 753 coro_cctx *cctx = cctx_first;
670 cctx_first = cctx->next; 754 cctx_first = cctx->next;
671 --cctx_idle; 755 --cctx_idle;
756
757 if (cctx->ssize >= coro_stacksize)
758 return cctx;
759
760 cctx_destroy (cctx);
672 } 761 }
673 else 762
674 {
675 cctx = cctx_new ();
676 PL_op = PL_op->op_next; 763 PL_op = PL_op->op_next;
677 }
678
679 return cctx; 764 return cctx_new ();
680} 765}
681 766
682static void 767static void
683cctx_put (coro_cctx *cctx) 768cctx_put (coro_cctx *cctx)
684{ 769{
696 ++cctx_idle; 781 ++cctx_idle;
697 cctx->next = cctx_first; 782 cctx->next = cctx_first;
698 cctx_first = cctx; 783 cctx_first = cctx;
699} 784}
700 785
786/** coroutine switching *****************************************************/
787
701/* never call directly, always through the coro_state_transfer global variable */ 788/* never call directly, always through the coro_state_transfer global variable */
702static void NOINLINE 789static void NOINLINE
703transfer (struct coro *prev, struct coro *next) 790transfer (pTHX_ struct coro *prev, struct coro *next)
704{ 791{
705 dSTACKLEVEL; 792 dSTACKLEVEL;
706 793
707 /* sometimes transfer is only called to set idle_sp */ 794 /* sometimes transfer is only called to set idle_sp */
708 if (!next) 795 if (!next)
709 { 796 {
710 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 797 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
711 assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */ 798 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
712 } 799 }
713 else if (prev != next) 800 else if (prev != next)
714 { 801 {
715 coro_cctx *prev__cctx; 802 coro_cctx *prev__cctx;
716 803
728 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 815 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
729 816
730 if (next->flags & CF_RUNNING) 817 if (next->flags & CF_RUNNING)
731 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 818 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
732 819
820 if (next->flags & CF_DESTROYED)
821 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
822
733 prev->flags &= ~CF_RUNNING; 823 prev->flags &= ~CF_RUNNING;
734 next->flags |= CF_RUNNING; 824 next->flags |= CF_RUNNING;
735 825
736 LOCK; 826 LOCK;
737 827
738 if (next->flags & CF_NEW) 828 if (next->flags & CF_NEW)
739 { 829 {
740 /* need to start coroutine */ 830 /* need to start coroutine */
741 next->flags &= ~CF_NEW; 831 next->flags &= ~CF_NEW;
742 /* first get rid of the old state */ 832 /* first get rid of the old state */
743 save_perl (prev); 833 save_perl (aTHX_ prev);
744 /* setup coroutine call */ 834 /* setup coroutine call */
745 setup_coro (next); 835 setup_coro (aTHX_ next);
746 /* need a new stack */ 836 /* need a new stack */
747 assert (!next->cctx); 837 assert (!next->cctx);
748 } 838 }
749 else 839 else
750 { 840 {
751 /* coroutine already started */ 841 /* coroutine already started */
752 save_perl (prev); 842 save_perl (aTHX_ prev);
753 load_perl (next); 843 load_perl (aTHX_ next);
754 } 844 }
755 845
756 prev__cctx = prev->cctx; 846 prev__cctx = prev->cctx;
757 847
758 /* possibly "free" the cctx */ 848 /* possibly "free" the cctx */
759 if (prev__cctx->idle_sp == STACKLEVEL) 849 if (prev__cctx->idle_sp == STACKLEVEL)
760 { 850 {
761 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 851 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
762 assert (PL_top_env == prev__cctx->top_env); 852 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
763 853
764 prev->cctx = 0; 854 prev->cctx = 0;
765 855
766 cctx_put (prev__cctx); 856 cctx_put (prev__cctx);
767 prev__cctx->inuse = 0; 857 prev__cctx->inuse = 0;
768 } 858 }
769 859
770 if (!next->cctx) 860 if (!next->cctx)
771 { 861 {
772 next->cctx = cctx_get (); 862 next->cctx = cctx_get (aTHX);
773 assert (!next->cctx->inuse); 863 assert (!next->cctx->inuse);
774 next->cctx->inuse = 1; 864 next->cctx->inuse = 1;
775 } 865 }
776 866
777 if (prev__cctx != next->cctx) 867 if (prev__cctx != next->cctx)
779 prev__cctx->top_env = PL_top_env; 869 prev__cctx->top_env = PL_top_env;
780 PL_top_env = next->cctx->top_env; 870 PL_top_env = next->cctx->top_env;
781 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 871 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
782 } 872 }
783 873
784 free_coro_mortal (); 874 free_coro_mortal (aTHX);
785
786 UNLOCK; 875 UNLOCK;
787 } 876 }
788} 877}
789 878
790struct transfer_args 879struct transfer_args
791{ 880{
792 struct coro *prev, *next; 881 struct coro *prev, *next;
793}; 882};
794 883
795#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 884#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
796 885
886/** high level stuff ********************************************************/
887
797static void 888static int
798coro_state_destroy (struct coro *coro) 889coro_state_destroy (pTHX_ struct coro *coro)
799{ 890{
800 if (coro->refcnt--) 891 if (coro->flags & CF_DESTROYED)
801 return; 892 return 0;
893
894 coro->flags |= CF_DESTROYED;
895
896 if (coro->flags & CF_READY)
897 {
898 /* reduce nready, as destroying a ready coro effectively unreadies it */
899 /* alternative: look through all ready queues and remove the coro */
900 LOCK;
901 --coro_nready;
902 UNLOCK;
903 }
904 else
905 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
802 906
803 if (coro->mainstack && coro->mainstack != main_mainstack) 907 if (coro->mainstack && coro->mainstack != main_mainstack)
804 { 908 {
805 struct coro temp; 909 struct coro temp;
910
911 assert (!(coro->flags & CF_RUNNING));
912
806 Zero (&temp, 1, struct coro); 913 Zero (&temp, 1, struct coro);
807 temp.save = CORO_SAVE_ALL; 914 temp.save = CORO_SAVE_ALL;
808 915
809 if (coro->flags & CF_RUNNING) 916 if (coro->flags & CF_RUNNING)
810 croak ("FATAL: tried to destroy currently running coroutine"); 917 croak ("FATAL: tried to destroy currently running coroutine");
811 918
812 save_perl (&temp); 919 save_perl (aTHX_ &temp);
813 load_perl (coro); 920 load_perl (aTHX_ coro);
814 921
815 coro_destroy_stacks (); 922 coro_destroy_stacks (aTHX);
816 923
817 load_perl (&temp); /* this will get rid of defsv etc.. */ 924 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
818 925
819 coro->mainstack = 0; 926 coro->mainstack = 0;
820 } 927 }
821 928
822 cctx_destroy (coro->cctx); 929 cctx_destroy (coro->cctx);
823 SvREFCNT_dec (coro->args); 930 SvREFCNT_dec (coro->args);
824 Safefree (coro); 931
932 if (coro->next) coro->next->prev = coro->prev;
933 if (coro->prev) coro->prev->next = coro->next;
934 if (coro == first) first = coro->next;
935
936 return 1;
825} 937}
826 938
827static int 939static int
828coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 940coro_state_free (pTHX_ SV *sv, MAGIC *mg)
829{ 941{
830 struct coro *coro = (struct coro *)mg->mg_ptr; 942 struct coro *coro = (struct coro *)mg->mg_ptr;
831 mg->mg_ptr = 0; 943 mg->mg_ptr = 0;
832 944
945 coro->hv = 0;
946
947 if (--coro->refcnt < 0)
948 {
833 coro_state_destroy (coro); 949 coro_state_destroy (aTHX_ coro);
950 Safefree (coro);
951 }
834 952
835 return 0; 953 return 0;
836} 954}
837 955
838static int 956static int
845 return 0; 963 return 0;
846} 964}
847 965
848static MGVTBL coro_state_vtbl = { 966static MGVTBL coro_state_vtbl = {
849 0, 0, 0, 0, 967 0, 0, 0, 0,
850 coro_state_clear, 968 coro_state_free,
851 0, 969 0,
852#ifdef MGf_DUP 970#ifdef MGf_DUP
853 coro_state_dup, 971 coro_state_dup,
854#else 972#else
855# define MGf_DUP 0 973# define MGf_DUP 0
856#endif 974#endif
857}; 975};
858 976
859static struct coro * 977static struct coro *
860SvSTATE (SV *coro) 978SvSTATE_ (pTHX_ SV *coro)
861{ 979{
862 HV *stash; 980 HV *stash;
863 MAGIC *mg; 981 MAGIC *mg;
864 982
865 if (SvROK (coro)) 983 if (SvROK (coro))
871 /* very slow, but rare, check */ 989 /* very slow, but rare, check */
872 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 990 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
873 croak ("Coro::State object required"); 991 croak ("Coro::State object required");
874 } 992 }
875 993
876 mg = SvMAGIC (coro); 994 mg = CORO_MAGIC (coro);
877 assert (mg->mg_type == PERL_MAGIC_ext);
878 return (struct coro *)mg->mg_ptr; 995 return (struct coro *)mg->mg_ptr;
879} 996}
880 997
998#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
999
881static void 1000static void
882prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1001prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
883{ 1002{
884 ta->prev = SvSTATE (prev_sv); 1003 ta->prev = SvSTATE (prev_sv);
885 ta->next = SvSTATE (next_sv); 1004 ta->next = SvSTATE (next_sv);
886} 1005}
887 1006
888static void 1007static void
889api_transfer (SV *prev_sv, SV *next_sv) 1008api_transfer (SV *prev_sv, SV *next_sv)
890{ 1009{
1010 dTHX;
891 struct transfer_args ta; 1011 struct transfer_args ta;
892 1012
893 prepare_transfer (&ta, prev_sv, next_sv); 1013 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
894 TRANSFER (ta); 1014 TRANSFER (ta);
895} 1015}
896 1016
897static int 1017static int
898api_save (SV *coro_sv, int new_save) 1018api_save (SV *coro_sv, int new_save)
899{ 1019{
1020 dTHX;
900 struct coro *coro = SvSTATE (coro_sv); 1021 struct coro *coro = SvSTATE (coro_sv);
901 int old_save = coro->save; 1022 int old_save = coro->save;
902 1023
903 if (new_save >= 0) 1024 if (new_save >= 0)
904 coro->save = new_save; 1025 coro->save = new_save;
906 return old_save; 1027 return old_save;
907} 1028}
908 1029
909/** Coro ********************************************************************/ 1030/** Coro ********************************************************************/
910 1031
911#define PRIO_MAX 3
912#define PRIO_HIGH 1
913#define PRIO_NORMAL 0
914#define PRIO_LOW -1
915#define PRIO_IDLE -3
916#define PRIO_MIN -4
917
918/* for Coro.pm */
919static SV *coro_current;
920static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
921static int coro_nready;
922
923static void 1032static void
924coro_enq (SV *coro_sv) 1033coro_enq (pTHX_ SV *coro_sv)
925{ 1034{
926 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 1035 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
927 coro_nready++;
928} 1036}
929 1037
930static SV * 1038static SV *
931coro_deq (int min_prio) 1039coro_deq (pTHX_ int min_prio)
932{ 1040{
933 int prio = PRIO_MAX - PRIO_MIN; 1041 int prio = PRIO_MAX - PRIO_MIN;
934 1042
935 min_prio -= PRIO_MIN; 1043 min_prio -= PRIO_MIN;
936 if (min_prio < 0) 1044 if (min_prio < 0)
937 min_prio = 0; 1045 min_prio = 0;
938 1046
939 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 1047 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
940 if (AvFILLp (coro_ready [prio]) >= 0) 1048 if (AvFILLp (coro_ready [prio]) >= 0)
941 {
942 coro_nready--;
943 return av_shift (coro_ready [prio]); 1049 return av_shift (coro_ready [prio]);
944 }
945 1050
946 return 0; 1051 return 0;
947} 1052}
948 1053
949static int 1054static int
950api_ready (SV *coro_sv) 1055api_ready (SV *coro_sv)
951{ 1056{
1057 dTHX;
952 struct coro *coro; 1058 struct coro *coro;
953 1059
954 if (SvROK (coro_sv)) 1060 if (SvROK (coro_sv))
955 coro_sv = SvRV (coro_sv); 1061 coro_sv = SvRV (coro_sv);
956 1062
960 return 0; 1066 return 0;
961 1067
962 coro->flags |= CF_READY; 1068 coro->flags |= CF_READY;
963 1069
964 LOCK; 1070 LOCK;
965 coro_enq (SvREFCNT_inc (coro_sv)); 1071 coro_enq (aTHX_ SvREFCNT_inc (coro_sv));
1072 ++coro_nready;
966 UNLOCK; 1073 UNLOCK;
967 1074
968 return 1; 1075 return 1;
969} 1076}
970 1077
971static int 1078static int
972api_is_ready (SV *coro_sv) 1079api_is_ready (SV *coro_sv)
973{ 1080{
1081 dTHX;
974 return !!SvSTATE (coro_sv)->flags & CF_READY; 1082 return !!(SvSTATE (coro_sv)->flags & CF_READY);
975} 1083}
976 1084
977static void 1085static void
978prepare_schedule (struct transfer_args *ta) 1086prepare_schedule (pTHX_ struct transfer_args *ta)
979{ 1087{
980 SV *prev, *next; 1088 SV *prev_sv, *next_sv;
981 1089
982 for (;;) 1090 for (;;)
983 { 1091 {
984 LOCK; 1092 LOCK;
985 next = coro_deq (PRIO_MIN); 1093 next_sv = coro_deq (aTHX_ PRIO_MIN);
1094
1095 /* nothing to schedule: call the idle handler */
1096 if (!next_sv)
1097 {
1098 dSP;
1099 UNLOCK;
1100
1101 ENTER;
1102 SAVETMPS;
1103
1104 PUSHMARK (SP);
1105 PUTBACK;
1106 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1107
1108 FREETMPS;
1109 LEAVE;
1110 continue;
1111 }
1112
1113 ta->next = SvSTATE (next_sv);
1114
1115 /* cannot transfer to destroyed coros, skip and look for next */
1116 if (ta->next->flags & CF_DESTROYED)
1117 {
1118 UNLOCK;
1119 SvREFCNT_dec (next_sv);
1120 /* coro_nready is already taken care of by destroy */
1121 continue;
1122 }
1123
1124 --coro_nready;
986 UNLOCK; 1125 UNLOCK;
987
988 if (next)
989 break; 1126 break;
990
991 {
992 dSP;
993
994 ENTER;
995 SAVETMPS;
996
997 PUSHMARK (SP);
998 PUTBACK;
999 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1000
1001 FREETMPS;
1002 LEAVE;
1003 } 1127 }
1004 }
1005
1006 prev = SvRV (coro_current);
1007 SvRV_set (coro_current, next);
1008 1128
1009 /* free this only after the transfer */ 1129 /* free this only after the transfer */
1010 LOCK; 1130 prev_sv = SvRV (coro_current);
1011 free_coro_mortal (); 1131 SvRV_set (coro_current, next_sv);
1012 UNLOCK;
1013 coro_mortal = prev;
1014
1015 assert (!SvROK(prev));//D
1016 assert (!SvROK(next));//D
1017
1018 ta->prev = SvSTATE (prev); 1132 ta->prev = SvSTATE (prev_sv);
1019 ta->next = SvSTATE (next);
1020 1133
1021 assert (ta->next->flags & CF_READY); 1134 assert (ta->next->flags & CF_READY);
1022 ta->next->flags &= ~CF_READY; 1135 ta->next->flags &= ~CF_READY;
1023}
1024 1136
1137 LOCK;
1138 free_coro_mortal (aTHX);
1139 coro_mortal = prev_sv;
1140 UNLOCK;
1141}
1142
1025static void 1143static void
1026prepare_cede (struct transfer_args *ta) 1144prepare_cede (pTHX_ struct transfer_args *ta)
1027{ 1145{
1028 api_ready (coro_current); 1146 api_ready (coro_current);
1029
1030 prepare_schedule (ta); 1147 prepare_schedule (aTHX_ ta);
1148}
1149
1150static int
1151prepare_cede_notself (pTHX_ struct transfer_args *ta)
1152{
1153 if (coro_nready)
1154 {
1155 SV *prev = SvRV (coro_current);
1156 prepare_schedule (aTHX_ ta);
1157 api_ready (prev);
1158 return 1;
1159 }
1160 else
1161 return 0;
1031} 1162}
1032 1163
1033static void 1164static void
1034api_schedule (void) 1165api_schedule (void)
1035{ 1166{
1167 dTHX;
1036 struct transfer_args ta; 1168 struct transfer_args ta;
1037 1169
1038 prepare_schedule (&ta); 1170 prepare_schedule (aTHX_ &ta);
1039 TRANSFER (ta); 1171 TRANSFER (ta);
1040} 1172}
1041 1173
1042static void 1174static int
1043api_cede (void) 1175api_cede (void)
1044{ 1176{
1177 dTHX;
1045 struct transfer_args ta; 1178 struct transfer_args ta;
1046 1179
1047 prepare_cede (&ta); 1180 prepare_cede (aTHX_ &ta);
1181
1182 if (ta.prev != ta.next)
1183 {
1048 TRANSFER (ta); 1184 TRANSFER (ta);
1185 return 1;
1186 }
1187 else
1188 return 0;
1189}
1190
1191static int
1192api_cede_notself (void)
1193{
1194 dTHX;
1195 struct transfer_args ta;
1196
1197 if (prepare_cede_notself (aTHX_ &ta))
1198 {
1199 TRANSFER (ta);
1200 return 1;
1201 }
1202 else
1203 return 0;
1049} 1204}
1050 1205
1051MODULE = Coro::State PACKAGE = Coro::State 1206MODULE = Coro::State PACKAGE = Coro::State
1052 1207
1053PROTOTYPES: DISABLE 1208PROTOTYPES: DISABLE
1063 1218
1064 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1219 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1065 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1220 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1066 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1221 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1067 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1222 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1223 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1224 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF));
1068 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1225 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1069 1226
1070 main_mainstack = PL_mainstack; 1227 main_mainstack = PL_mainstack;
1228 main_top_env = PL_top_env;
1229
1230 while (main_top_env->je_prev)
1231 main_top_env = main_top_env->je_prev;
1071 1232
1072 coroapi.ver = CORO_API_VERSION; 1233 coroapi.ver = CORO_API_VERSION;
1073 coroapi.transfer = api_transfer; 1234 coroapi.transfer = api_transfer;
1074 1235
1075 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1236 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1083 HV *hv; 1244 HV *hv;
1084 int i; 1245 int i;
1085 1246
1086 Newz (0, coro, 1, struct coro); 1247 Newz (0, coro, 1, struct coro);
1087 coro->args = newAV (); 1248 coro->args = newAV ();
1088 coro->save = CORO_SAVE_ALL; 1249 coro->save = CORO_SAVE_DEF;
1089 coro->flags = CF_NEW; 1250 coro->flags = CF_NEW;
1090 1251
1252 if (first) first->prev = coro;
1253 coro->next = first;
1254 first = coro;
1255
1091 hv = newHV (); 1256 coro->hv = hv = newHV ();
1092 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1257 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1093 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1258 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1094 1259
1095 for (i = 1; i < items; i++) 1260 for (i = 1; i < items; i++)
1096 av_push (coro->args, newSVsv (ST (i))); 1261 av_push (coro->args, newSVsv (ST (i)));
1103 CODE: 1268 CODE:
1104 RETVAL = api_save (coro, new_save); 1269 RETVAL = api_save (coro, new_save);
1105 OUTPUT: 1270 OUTPUT:
1106 RETVAL 1271 RETVAL
1107 1272
1273int
1274save_also (SV *coro_sv, int save_also)
1275 CODE:
1276{
1277 struct coro *coro = SvSTATE (coro_sv);
1278 RETVAL = coro->save;
1279 coro->save |= save_also;
1280}
1281 OUTPUT:
1282 RETVAL
1283
1108void 1284void
1109_set_stacklevel (...) 1285_set_stacklevel (...)
1110 ALIAS: 1286 ALIAS:
1111 Coro::State::transfer = 1 1287 Coro::State::transfer = 1
1112 Coro::schedule = 2 1288 Coro::schedule = 2
1113 Coro::cede = 3 1289 Coro::cede = 3
1290 Coro::cede_notself = 4
1114 CODE: 1291 CODE:
1115{ 1292{
1116 struct transfer_args ta; 1293 struct transfer_args ta;
1117 1294
1118 switch (ix) 1295 switch (ix)
1124 1301
1125 case 1: 1302 case 1:
1126 if (items != 2) 1303 if (items != 2)
1127 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1304 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1128 1305
1129 prepare_transfer (&ta, ST (0), ST (1)); 1306 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1130 break; 1307 break;
1131 1308
1132 case 2: 1309 case 2:
1133 prepare_schedule (&ta); 1310 prepare_schedule (aTHX_ &ta);
1134 break; 1311 break;
1135 1312
1136 case 3: 1313 case 3:
1137 prepare_cede (&ta); 1314 prepare_cede (aTHX_ &ta);
1315 break;
1316
1317 case 4:
1318 if (!prepare_cede_notself (aTHX_ &ta))
1319 XSRETURN_EMPTY;
1320
1138 break; 1321 break;
1139 } 1322 }
1140 1323
1324 BARRIER;
1141 TRANSFER (ta); 1325 TRANSFER (ta);
1142}
1143 1326
1144void 1327 if (GIMME_V != G_VOID && ta.next != ta.prev)
1145_clone_state_from (SV *dst, SV *src) 1328 XSRETURN_YES;
1329}
1330
1331bool
1332_destroy (SV *coro_sv)
1146 CODE: 1333 CODE:
1147{ 1334 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1148 struct coro *coro_src = SvSTATE (src); 1335 OUTPUT:
1149 1336 RETVAL
1150 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1151
1152 ++coro_src->refcnt;
1153 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1154}
1155 1337
1156void 1338void
1157_exit (code) 1339_exit (code)
1158 int code 1340 int code
1159 PROTOTYPE: $ 1341 PROTOTYPE: $
1160 CODE: 1342 CODE:
1161 _exit (code); 1343 _exit (code);
1162 1344
1163int 1345int
1346cctx_stacksize (int new_stacksize = 0)
1347 CODE:
1348 RETVAL = coro_stacksize;
1349 if (new_stacksize)
1350 coro_stacksize = new_stacksize;
1351 OUTPUT:
1352 RETVAL
1353
1354int
1164cctx_count () 1355cctx_count ()
1165 CODE: 1356 CODE:
1166 RETVAL = cctx_count; 1357 RETVAL = cctx_count;
1167 OUTPUT: 1358 OUTPUT:
1168 RETVAL 1359 RETVAL
1172 CODE: 1363 CODE:
1173 RETVAL = cctx_idle; 1364 RETVAL = cctx_idle;
1174 OUTPUT: 1365 OUTPUT:
1175 RETVAL 1366 RETVAL
1176 1367
1368void
1369list ()
1370 PPCODE:
1371{
1372 struct coro *coro;
1373 for (coro = first; coro; coro = coro->next)
1374 if (coro->hv)
1375 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1376}
1377
1378void
1379_eval (Coro::State coro, SV *coderef)
1380 CODE:
1381{
1382 if (coro->mainstack)
1383 {
1384 struct coro temp;
1385 Zero (&temp, 1, struct coro);
1386 temp.save = CORO_SAVE_ALL;
1387
1388 if (!(coro->flags & CF_RUNNING))
1389 {
1390 save_perl (aTHX_ &temp);
1391 load_perl (aTHX_ coro);
1392 }
1393
1394 {
1395 dSP;
1396 ENTER;
1397 SAVETMPS;
1398 PUSHMARK (SP);
1399 PUTBACK;
1400 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1401 SPAGAIN;
1402 FREETMPS;
1403 LEAVE;
1404 PUTBACK;
1405 }
1406
1407 if (!(coro->flags & CF_RUNNING))
1408 {
1409 save_perl (aTHX_ coro);
1410 load_perl (aTHX_ &temp);
1411 }
1412 }
1413}
1414
1415SV *
1416is_ready (Coro::State coro)
1417 PROTOTYPE: $
1418 ALIAS:
1419 is_ready = CF_READY
1420 is_running = CF_RUNNING
1421 is_new = CF_NEW
1422 is_destroyed = CF_DESTROYED
1423 CODE:
1424 RETVAL = boolSV (coro->flags & ix);
1425 OUTPUT:
1426 RETVAL
1427
1428IV
1429rss (Coro::State coro)
1430 PROTOTYPE: $
1431 CODE:
1432 RETVAL = coro_rss (coro);
1433 OUTPUT:
1434 RETVAL
1435
1436
1177MODULE = Coro::State PACKAGE = Coro 1437MODULE = Coro::State PACKAGE = Coro
1178 1438
1179BOOT: 1439BOOT:
1180{ 1440{
1181 int i; 1441 int i;
1196 coro_ready[i] = newAV (); 1456 coro_ready[i] = newAV ();
1197 1457
1198 { 1458 {
1199 SV *sv = perl_get_sv("Coro::API", 1); 1459 SV *sv = perl_get_sv("Coro::API", 1);
1200 1460
1201 coroapi.schedule = api_schedule; 1461 coroapi.schedule = api_schedule;
1202 coroapi.save = api_save; 1462 coroapi.save = api_save;
1203 coroapi.cede = api_cede; 1463 coroapi.cede = api_cede;
1464 coroapi.cede_notself = api_cede_notself;
1204 coroapi.ready = api_ready; 1465 coroapi.ready = api_ready;
1205 coroapi.is_ready = api_is_ready; 1466 coroapi.is_ready = api_is_ready;
1206 coroapi.nready = &coro_nready; 1467 coroapi.nready = &coro_nready;
1207 coroapi.current = coro_current; 1468 coroapi.current = coro_current;
1208 1469
1209 GCoroAPI = &coroapi; 1470 GCoroAPI = &coroapi;
1210 sv_setiv (sv, (IV)&coroapi); 1471 sv_setiv (sv, (IV)&coroapi);
1211 SvREADONLY_on (sv); 1472 SvREADONLY_on (sv);
1212 } 1473 }
1228 RETVAL = coro->prio; 1489 RETVAL = coro->prio;
1229 1490
1230 if (items > 1) 1491 if (items > 1)
1231 { 1492 {
1232 if (ix) 1493 if (ix)
1233 newprio += coro->prio; 1494 newprio = coro->prio - newprio;
1234 1495
1235 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1496 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1236 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1497 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1237 1498
1238 coro->prio = newprio; 1499 coro->prio = newprio;
1239 } 1500 }
1240} 1501}
1502 OUTPUT:
1503 RETVAL
1241 1504
1242SV * 1505SV *
1243ready (SV *self) 1506ready (SV *self)
1244 PROTOTYPE: $ 1507 PROTOTYPE: $
1245 CODE: 1508 CODE:
1246 RETVAL = boolSV (api_ready (self)); 1509 RETVAL = boolSV (api_ready (self));
1247 OUTPUT: 1510 OUTPUT:
1248 RETVAL 1511 RETVAL
1249 1512
1250SV *
1251is_ready (SV *self)
1252 PROTOTYPE: $
1253 CODE:
1254 RETVAL = boolSV (api_is_ready (self));
1255 OUTPUT:
1256 RETVAL
1257
1258int 1513int
1259nready (...) 1514nready (...)
1260 PROTOTYPE: 1515 PROTOTYPE:
1261 CODE: 1516 CODE:
1262 RETVAL = coro_nready; 1517 RETVAL = coro_nready;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines