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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines