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.92 by root, Sun Nov 26 02:16:19 2006 UTC vs.
Revision 1.116 by root, Fri Dec 1 14:01:43 2006 UTC

1#define PERL_NO_GET_CONTEXT
2
3#include "libcoro/coro.c" 1#include "libcoro/coro.c"
4 2
5#include "EXTERN.h" 3#include "EXTERN.h"
6#include "perl.h" 4#include "perl.h"
7#include "XSUB.h" 5#include "XSUB.h"
8 6
9#include "patchlevel.h" 7#include "patchlevel.h"
10 8
11#if PERL_VERSION < 6 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)
12# ifndef PL_ppaddr 23# ifndef PL_ppaddr
13# define PL_ppaddr ppaddr 24# define PL_ppaddr ppaddr
14# endif 25# endif
15# ifndef call_sv 26# ifndef call_sv
16# define call_sv perl_call_sv 27# define call_sv perl_call_sv
54# ifndef PAGESIZE 65# ifndef PAGESIZE
55# define PAGESIZE pagesize 66# define PAGESIZE pagesize
56# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) 67# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
57static long pagesize; 68static long pagesize;
58# else 69# else
59# define BOOT_PAGESIZE 70# define BOOT_PAGESIZE (void)0
60# endif 71# endif
72#else
73# define PAGESIZE 0
74# define BOOT_PAGESIZE (void)0
61#endif 75#endif
62 76
63/* The next macro should declare a variable stacklevel that contains and approximation 77/* The next macro should declare a variable stacklevel that contains and approximation
64 * to the current C stack pointer. Its property is that it changes with each call 78 * to the current C stack pointer. Its property is that it changes with each call
65 * and should be unique. */ 79 * and should be unique. */
66#define dSTACKLEVEL int stacklevel 80#define dSTACKLEVEL int stacklevel
67#define STACKLEVEL ((void *)&stacklevel) 81#define STACKLEVEL ((void *)&stacklevel)
68 82
69#define IN_DESTRUCT (PL_main_cv == Nullcv) 83#define IN_DESTRUCT (PL_main_cv == Nullcv)
70 84
71#define labs(l) ((l) >= 0 ? (l) : -(l)) 85#if __GNUC__ >= 3
86# define attribute(x) __attribute__(x)
87#else
88# define attribute(x)
89#endif
90
91#define NOINLINE attribute ((noinline))
72 92
73#include "CoroAPI.h" 93#include "CoroAPI.h"
74 94
75#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ 95#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
76 96
86static struct CoroAPI coroapi; 106static struct CoroAPI coroapi;
87static AV *main_mainstack; /* used to differentiate between $main and others */ 107static AV *main_mainstack; /* used to differentiate between $main and others */
88static HV *coro_state_stash, *coro_stash; 108static HV *coro_state_stash, *coro_stash;
89static SV *coro_mortal; /* will be freed after next transfer */ 109static SV *coro_mortal; /* will be freed after next transfer */
90 110
111static struct coro_cctx *cctx_first;
112static int cctx_count, cctx_idle;
113
91/* this is a structure representing a c-level coroutine */ 114/* this is a structure representing a c-level coroutine */
92typedef struct coro_stack { 115typedef struct coro_cctx {
93 struct coro_stack *next; 116 struct coro_cctx *next;
94 117
95 void *idle_sp; /* original stacklevel when coroutine was created */ 118 /* the stack */
96 void *sptr; 119 void *sptr;
97 long ssize; /* positive == mmap, otherwise malloc */ 120 long ssize; /* positive == mmap, otherwise malloc */
98 121
99 /* cpu state */ 122 /* cpu state */
123 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
124 JMPENV *top_env;
100 coro_context cctx; 125 coro_context cctx;
101 JMPENV *top_env;
102} coro_stack;
103 126
104/* the (fake) coro_stack representing the main program */ 127 int inuse;
105static coro_stack *main_stack; 128
129#if USE_VALGRIND
130 int valgrind_id;
131#endif
132} coro_cctx;
133
134enum {
135 CF_RUNNING, /* coroutine is running */
136 CF_READY, /* coroutine is ready */
137};
106 138
107/* this is a structure representing a perl-level coroutine */ 139/* this is a structure representing a perl-level coroutine */
108struct coro { 140struct coro {
109 /* the c coroutine allocated to this perl coroutine, if any */ 141 /* the c coroutine allocated to this perl coroutine, if any */
110 coro_stack *stack; 142 coro_cctx *cctx;
111 143
112 /* data associated with this coroutine (initial args) */ 144 /* data associated with this coroutine (initial args) */
113 AV *args; 145 AV *args;
114 int refcnt; 146 int refcnt;
147 int flags;
115 148
116 /* optionally saved, might be zero */ 149 /* optionally saved, might be zero */
117 AV *defav; 150 AV *defav;
118 SV *defsv; 151 SV *defsv;
119 SV *errsv; 152 SV *errsv;
120 153
121 /* saved global state not related to stacks */ 154#define VAR(name,type) type name;
122 U8 dowarn; 155# include "state.h"
123 I32 in_eval; 156#undef VAR
124
125 /* the stacks and related info (callchain etc..) */
126 PERL_SI *curstackinfo;
127 AV *curstack;
128 AV *mainstack;
129 SV **stack_sp;
130 OP *op;
131 SV **curpad;
132 AV *comppad;
133 CV *compcv;
134 SV **stack_base;
135 SV **stack_max;
136 SV **tmps_stack;
137 I32 tmps_floor;
138 I32 tmps_ix;
139 I32 tmps_max;
140 I32 *markstack;
141 I32 *markstack_ptr;
142 I32 *markstack_max;
143 I32 *scopestack;
144 I32 scopestack_ix;
145 I32 scopestack_max;
146 ANY *savestack;
147 I32 savestack_ix;
148 I32 savestack_max;
149 OP **retstack;
150 I32 retstack_ix;
151 I32 retstack_max;
152 PMOP *curpm;
153 COP *curcop;
154 157
155 /* coro process data */ 158 /* coro process data */
156 int prio; 159 int prio;
157}; 160};
158 161
159typedef struct coro *Coro__State; 162typedef struct coro *Coro__State;
160typedef struct coro *Coro__State_or_hashref; 163typedef struct coro *Coro__State_or_hashref;
161 164
162static AV * 165static AV *
163coro_clone_padlist (pTHX_ CV *cv) 166coro_clone_padlist (CV *cv)
164{ 167{
165 AV *padlist = CvPADLIST (cv); 168 AV *padlist = CvPADLIST (cv);
166 AV *newpadlist, *newpad; 169 AV *newpadlist, *newpad;
167 170
168 newpadlist = newAV (); 171 newpadlist = newAV ();
169 AvREAL_off (newpadlist); 172 AvREAL_off (newpadlist);
170#if PERL_VERSION < 9 173#if PERL_VERSION_ATLEAST (5,9,0)
174 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
175#else
171 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 176 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
172#else
173 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
174#endif 177#endif
175 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 178 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
176 --AvFILLp (padlist); 179 --AvFILLp (padlist);
177 180
178 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 181 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
180 183
181 return newpadlist; 184 return newpadlist;
182} 185}
183 186
184static void 187static void
185free_padlist (pTHX_ AV *padlist) 188free_padlist (AV *padlist)
186{ 189{
187 /* may be during global destruction */ 190 /* may be during global destruction */
188 if (SvREFCNT (padlist)) 191 if (SvREFCNT (padlist))
189 { 192 {
190 I32 i = AvFILLp (padlist); 193 I32 i = AvFILLp (padlist);
211 AV *padlist; 214 AV *padlist;
212 AV *av = (AV *)mg->mg_obj; 215 AV *av = (AV *)mg->mg_obj;
213 216
214 /* casting is fun. */ 217 /* casting is fun. */
215 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 218 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
216 free_padlist (aTHX_ padlist); 219 free_padlist (padlist);
217 220
218 SvREFCNT_dec (av); 221 SvREFCNT_dec (av);
219 222
220 return 0; 223 return 0;
221} 224}
222 225
223#define PERL_MAGIC_coro PERL_MAGIC_ext 226#define PERL_MAGIC_coro PERL_MAGIC_ext
224 227
225static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 228static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
226 229
230#define CORO_MAGIC(cv) \
231 SvMAGIC (cv) \
232 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
233 ? SvMAGIC (cv) \
234 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
235 : 0
236
227/* the next two functions merely cache the padlists */ 237/* the next two functions merely cache the padlists */
228static void 238static void
229get_padlist (pTHX_ CV *cv) 239get_padlist (CV *cv)
230{ 240{
231 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 241 MAGIC *mg = CORO_MAGIC (cv);
242 AV *av;
232 243
233 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 244 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
234 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 245 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
235 else 246 else
236 { 247 {
237#if 0 248#if 0
238 /* this should work - but it doesn't :( */ 249 /* this is probably cleaner, but also slower? */
239 CV *cp = Perl_cv_clone (aTHX_ cv); 250 CV *cp = Perl_cv_clone (cv);
240 CvPADLIST (cv) = CvPADLIST (cp); 251 CvPADLIST (cv) = CvPADLIST (cp);
241 CvPADLIST (cp) = 0; 252 CvPADLIST (cp) = 0;
242 SvREFCNT_dec (cp); 253 SvREFCNT_dec (cp);
243#else 254#else
244 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 255 CvPADLIST (cv) = coro_clone_padlist (cv);
245#endif 256#endif
246 } 257 }
247} 258}
248 259
249static void 260static void
250put_padlist (pTHX_ CV *cv) 261put_padlist (CV *cv)
251{ 262{
252 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 263 MAGIC *mg = CORO_MAGIC (cv);
264 AV *av;
253 265
254 if (!mg) 266 if (!mg)
255 { 267 {
256 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 268 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
257 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 269 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
258 mg->mg_virtual = &vtbl_coro; 270 mg->mg_virtual = &vtbl_coro;
259 mg->mg_obj = (SV *)newAV (); 271 mg->mg_obj = (SV *)newAV ();
260 } 272 }
261 273
262 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); 274 av = (AV *)mg->mg_obj;
275
276 if (AvFILLp (av) >= AvMAX (av))
277 av_extend (av, AvMAX (av) + 1);
278
279 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
263} 280}
264 281
265#define SB do { 282#define SB do {
266#define SE } while (0) 283#define SE } while (0)
267 284
268#define LOAD(state) load_state(aTHX_ (state)); 285#define LOAD(state) load_state((state));
269#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); 286#define SAVE(state,flags) save_state((state),(flags));
270 287
271#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 288#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
272 289
273static void 290static void
274load_state(pTHX_ Coro__State c) 291load_state(Coro__State c)
275{ 292{
276 PL_dowarn = c->dowarn; 293#define VAR(name,type) PL_ ## name = c->name;
277 PL_in_eval = c->in_eval; 294# include "state.h"
278 295#undef VAR
279 PL_curstackinfo = c->curstackinfo;
280 PL_curstack = c->curstack;
281 PL_mainstack = c->mainstack;
282 PL_stack_sp = c->stack_sp;
283 PL_op = c->op;
284 PL_curpad = c->curpad;
285 PL_comppad = c->comppad;
286 PL_compcv = c->compcv;
287 PL_stack_base = c->stack_base;
288 PL_stack_max = c->stack_max;
289 PL_tmps_stack = c->tmps_stack;
290 PL_tmps_floor = c->tmps_floor;
291 PL_tmps_ix = c->tmps_ix;
292 PL_tmps_max = c->tmps_max;
293 PL_markstack = c->markstack;
294 PL_markstack_ptr = c->markstack_ptr;
295 PL_markstack_max = c->markstack_max;
296 PL_scopestack = c->scopestack;
297 PL_scopestack_ix = c->scopestack_ix;
298 PL_scopestack_max = c->scopestack_max;
299 PL_savestack = c->savestack;
300 PL_savestack_ix = c->savestack_ix;
301 PL_savestack_max = c->savestack_max;
302#if PERL_VERSION < 9
303 PL_retstack = c->retstack;
304 PL_retstack_ix = c->retstack_ix;
305 PL_retstack_max = c->retstack_max;
306#endif
307 PL_curpm = c->curpm;
308 PL_curcop = c->curcop;
309 296
310 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 297 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
311 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 298 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
312 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 299 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
313 300
316 CV *cv; 303 CV *cv;
317 304
318 /* now do the ugly restore mess */ 305 /* now do the ugly restore mess */
319 while ((cv = (CV *)POPs)) 306 while ((cv = (CV *)POPs))
320 { 307 {
321 AV *padlist = (AV *)POPs;
322
323 if (padlist)
324 {
325 put_padlist (aTHX_ cv); /* mark this padlist as available */ 308 put_padlist (cv); /* mark this padlist as available */
326 CvPADLIST(cv) = padlist; 309 CvDEPTH (cv) = PTR2IV (POPs);
327 } 310 CvPADLIST (cv) = (AV *)POPs;
328
329 ++CvDEPTH(cv);
330 } 311 }
331 312
332 PUTBACK; 313 PUTBACK;
333 } 314 }
334} 315}
335 316
336static void 317static void
337save_state(pTHX_ Coro__State c, int flags) 318save_state(Coro__State c, int flags)
338{ 319{
339 { 320 {
340 dSP; 321 dSP;
341 I32 cxix = cxstack_ix; 322 I32 cxix = cxstack_ix;
342 PERL_CONTEXT *ccstk = cxstack; 323 PERL_CONTEXT *ccstk = cxstack;
356 PERL_CONTEXT *cx = &ccstk[cxix--]; 337 PERL_CONTEXT *cx = &ccstk[cxix--];
357 338
358 if (CxTYPE(cx) == CXt_SUB) 339 if (CxTYPE(cx) == CXt_SUB)
359 { 340 {
360 CV *cv = cx->blk_sub.cv; 341 CV *cv = cx->blk_sub.cv;
342
361 if (CvDEPTH(cv)) 343 if (CvDEPTH (cv))
362 { 344 {
363 EXTEND (SP, CvDEPTH(cv)*2); 345 EXTEND (SP, 3);
364
365 while (--CvDEPTH(cv))
366 {
367 /* this tells the restore code to increment CvDEPTH */
368 PUSHs (Nullsv);
369 PUSHs ((SV *)cv);
370 }
371 346
372 PUSHs ((SV *)CvPADLIST(cv)); 347 PUSHs ((SV *)CvPADLIST(cv));
348 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
373 PUSHs ((SV *)cv); 349 PUSHs ((SV *)cv);
374 350
351 CvDEPTH (cv) = 0;
375 get_padlist (aTHX_ cv); 352 get_padlist (cv);
376 } 353 }
377 } 354 }
378#ifdef CXt_FORMAT 355#ifdef CXt_FORMAT
379 else if (CxTYPE(cx) == CXt_FORMAT) 356 else if (CxTYPE(cx) == CXt_FORMAT)
380 { 357 {
398 375
399 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 376 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
400 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 377 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
401 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 378 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
402 379
403 c->dowarn = PL_dowarn; 380#define VAR(name,type)c->name = PL_ ## name;
404 c->in_eval = PL_in_eval; 381# include "state.h"
405 382#undef VAR
406 c->curstackinfo = PL_curstackinfo;
407 c->curstack = PL_curstack;
408 c->mainstack = PL_mainstack;
409 c->stack_sp = PL_stack_sp;
410 c->op = PL_op;
411 c->curpad = PL_curpad;
412 c->comppad = PL_comppad;
413 c->compcv = PL_compcv;
414 c->stack_base = PL_stack_base;
415 c->stack_max = PL_stack_max;
416 c->tmps_stack = PL_tmps_stack;
417 c->tmps_floor = PL_tmps_floor;
418 c->tmps_ix = PL_tmps_ix;
419 c->tmps_max = PL_tmps_max;
420 c->markstack = PL_markstack;
421 c->markstack_ptr = PL_markstack_ptr;
422 c->markstack_max = PL_markstack_max;
423 c->scopestack = PL_scopestack;
424 c->scopestack_ix = PL_scopestack_ix;
425 c->scopestack_max = PL_scopestack_max;
426 c->savestack = PL_savestack;
427 c->savestack_ix = PL_savestack_ix;
428 c->savestack_max = PL_savestack_max;
429#if PERL_VERSION < 9
430 c->retstack = PL_retstack;
431 c->retstack_ix = PL_retstack_ix;
432 c->retstack_max = PL_retstack_max;
433#endif
434 c->curpm = PL_curpm;
435 c->curcop = PL_curcop;
436} 383}
437 384
438/* 385/*
439 * allocate various perl stacks. This is an exact copy 386 * allocate various perl stacks. This is an exact copy
440 * of perl.c:init_stacks, except that it uses less memory 387 * of perl.c:init_stacks, except that it uses less memory
441 * on the (sometimes correct) assumption that coroutines do 388 * on the (sometimes correct) assumption that coroutines do
442 * not usually need a lot of stackspace. 389 * not usually need a lot of stackspace.
443 */ 390 */
444static void 391static void
445coro_init_stacks (pTHX) 392coro_init_stacks ()
446{ 393{
447 LOCK;
448
449 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 394 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
450 PL_curstackinfo->si_type = PERLSI_MAIN; 395 PL_curstackinfo->si_type = PERLSI_MAIN;
451 PL_curstack = PL_curstackinfo->si_stack; 396 PL_curstack = PL_curstackinfo->si_stack;
452 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 397 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
453 398
454 PL_stack_base = AvARRAY(PL_curstack); 399 PL_stack_base = AvARRAY(PL_curstack);
455 PL_stack_sp = PL_stack_base; 400 PL_stack_sp = PL_stack_base;
456 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 401 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
457 402
458 New(50,PL_tmps_stack,96,SV*); 403 New(50,PL_tmps_stack,128,SV*);
459 PL_tmps_floor = -1; 404 PL_tmps_floor = -1;
460 PL_tmps_ix = -1; 405 PL_tmps_ix = -1;
461 PL_tmps_max = 96; 406 PL_tmps_max = 128;
462 407
463 New(54,PL_markstack,16,I32); 408 New(54,PL_markstack,32,I32);
464 PL_markstack_ptr = PL_markstack; 409 PL_markstack_ptr = PL_markstack;
465 PL_markstack_max = PL_markstack + 16; 410 PL_markstack_max = PL_markstack + 32;
466 411
467#ifdef SET_MARK_OFFSET 412#ifdef SET_MARK_OFFSET
468 SET_MARK_OFFSET; 413 SET_MARK_OFFSET;
469#endif 414#endif
470 415
471 New(54,PL_scopestack,16,I32); 416 New(54,PL_scopestack,32,I32);
472 PL_scopestack_ix = 0; 417 PL_scopestack_ix = 0;
473 PL_scopestack_max = 16; 418 PL_scopestack_max = 32;
474 419
475 New(54,PL_savestack,96,ANY); 420 New(54,PL_savestack,64,ANY);
476 PL_savestack_ix = 0; 421 PL_savestack_ix = 0;
477 PL_savestack_max = 96; 422 PL_savestack_max = 64;
478 423
479#if PERL_VERSION < 9 424#if !PERL_VERSION_ATLEAST (5,9,0)
480 New(54,PL_retstack,8,OP*); 425 New(54,PL_retstack,16,OP*);
481 PL_retstack_ix = 0; 426 PL_retstack_ix = 0;
482 PL_retstack_max = 8; 427 PL_retstack_max = 16;
483#endif 428#endif
484
485 UNLOCK;
486} 429}
487 430
488/* 431/*
489 * destroy the stacks, the callchain etc... 432 * destroy the stacks, the callchain etc...
490 */ 433 */
491static void 434static void
492destroy_stacks(pTHX) 435coro_destroy_stacks ()
493{ 436{
494 if (!IN_DESTRUCT) 437 if (!IN_DESTRUCT)
495 { 438 {
496 /* is this ugly, I ask? */ 439 /* is this ugly, I ask? */
497 LEAVE_SCOPE (0); 440 LEAVE_SCOPE (0);
528 471
529 Safefree (PL_tmps_stack); 472 Safefree (PL_tmps_stack);
530 Safefree (PL_markstack); 473 Safefree (PL_markstack);
531 Safefree (PL_scopestack); 474 Safefree (PL_scopestack);
532 Safefree (PL_savestack); 475 Safefree (PL_savestack);
533#if PERL_VERSION < 9 476#if !PERL_VERSION_ATLEAST (5,9,0)
534 Safefree (PL_retstack); 477 Safefree (PL_retstack);
535#endif 478#endif
536} 479}
537 480
538static void 481static void
539setup_coro (struct coro *coro) 482setup_coro (struct coro *coro)
540{ 483{
541 /* 484 /*
542 * emulate part of the perl startup here. 485 * emulate part of the perl startup here.
543 */ 486 */
544 dTHX;
545 dSP;
546 UNOP myop;
547 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
548 487
549 coro_init_stacks (aTHX); 488 coro_init_stacks ();
489
550 /*PL_curcop = 0;*/ 490 PL_curcop = 0;
551 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 491 PL_in_eval = 0;
492 PL_curpm = 0;
493
494 {
495 dSP;
496 LOGOP myop;
497
498 /* I have no idea why this is needed, but it is */
499 PUSHMARK (SP);
500
552 SvREFCNT_dec (GvAV (PL_defgv)); 501 SvREFCNT_dec (GvAV (PL_defgv));
553 GvAV (PL_defgv) = coro->args; coro->args = 0; 502 GvAV (PL_defgv) = coro->args; coro->args = 0;
554 503
555 SPAGAIN;
556
557 Zero (&myop, 1, UNOP); 504 Zero (&myop, 1, LOGOP);
558 myop.op_next = Nullop; 505 myop.op_next = Nullop;
559 myop.op_flags = OPf_WANT_VOID; 506 myop.op_flags = OPf_WANT_VOID;
560 507
561 PL_op = (OP *)&myop; 508 PL_op = (OP *)&myop;
562 509
563 PUSHMARK(SP); 510 PUSHMARK (SP);
564 XPUSHs (sub_init); 511 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
565 PUTBACK; 512 PUTBACK;
566 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 513 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
567 SPAGAIN; 514 SPAGAIN;
568 515
569 ENTER; /* necessary e.g. for dounwind */ 516 ENTER; /* necessary e.g. for dounwind */
517 }
570} 518}
571 519
572static void 520static void
573free_coro_mortal () 521free_coro_mortal ()
574{ 522{
577 SvREFCNT_dec (coro_mortal); 525 SvREFCNT_dec (coro_mortal);
578 coro_mortal = 0; 526 coro_mortal = 0;
579 } 527 }
580} 528}
581 529
530static void NOINLINE
531prepare_cctx (coro_cctx *cctx)
532{
533 dSP;
534 LOGOP myop;
535
536 Zero (&myop, 1, LOGOP);
537 myop.op_next = PL_op;
538 myop.op_flags = OPf_WANT_VOID;
539
540 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
541
542 PUSHMARK (SP);
543 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
544 PUTBACK;
545 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
546 SPAGAIN;
547}
548
582static void 549static void
583coro_run (void *arg) 550coro_run (void *arg)
584{ 551{
552 /* coro_run is the alternative epilogue of transfer() */
553 UNLOCK;
554
585 /* 555 /*
586 * this is a _very_ stripped down perl interpreter ;) 556 * this is a _very_ stripped down perl interpreter ;)
587 */ 557 */
588 dTHX;
589 PL_top_env = &PL_start_env; 558 PL_top_env = &PL_start_env;
590 559
591 UNLOCK; 560 /* inject call to cctx_init */
561 prepare_cctx ((coro_cctx *)arg);
592 562
593 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg));
594 sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op));
595 PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE));
596
597 /* somebody will hit me for both perl_run and PL_restart_op */ 563 /* somebody will hit me for both perl_run and PL_restartop */
598 perl_run (aTHX_ PERL_GET_CONTEXT); 564 perl_run (PL_curinterp);
599 565
566 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
600 abort (); 567 abort ();
601} 568}
602 569
603static coro_stack * 570static coro_cctx *
604stack_new () 571cctx_new ()
605{ 572{
606 coro_stack *stack; 573 coro_cctx *cctx;
607 574
575 ++cctx_count;
576
608 New (0, stack, 1, coro_stack); 577 New (0, cctx, 1, coro_cctx);
609 578
610#if HAVE_MMAP 579#if HAVE_MMAP
611 580
612 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ 581 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
582 /* mmap suppsedly does allocate-on-write for us */
613 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 583 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
614 584
615 if (stack->sptr == (void *)-1) 585 if (cctx->sptr == (void *)-1)
616 { 586 {
617 perror ("FATAL: unable to mmap stack for coroutine"); 587 perror ("FATAL: unable to mmap stack for coroutine");
618 _exit (EXIT_FAILURE); 588 _exit (EXIT_FAILURE);
619 } 589 }
620 590
591# if STACKGUARD
621 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 592 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
593# endif
622 594
623#else 595#else
624 596
625 stack->ssize = STACKSIZE * (long)sizeof (long); 597 cctx->ssize = STACKSIZE * (long)sizeof (long);
626 New (0, stack->sptr, STACKSIZE, long); 598 New (0, cctx->sptr, STACKSIZE, long);
627 599
628 if (!stack->sptr) 600 if (!cctx->sptr)
629 { 601 {
630 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 602 perror ("FATAL: unable to malloc stack for coroutine");
631 _exit (EXIT_FAILURE); 603 _exit (EXIT_FAILURE);
632 } 604 }
633 605
634#endif 606#endif
635 607
608#if USE_VALGRIND
609 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
610 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
611 cctx->ssize + (char *)cctx->sptr
612 );
613#endif
614
636 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); 615 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
637 616
638 return stack; 617 return cctx;
639} 618}
640 619
641static void 620static void
642stack_free (coro_stack *stack) 621cctx_destroy (coro_cctx *cctx)
643{ 622{
644 if (!stack || stack == main_stack) 623 if (!cctx)
645 return; 624 return;
646 625
626 --cctx_count;
627
628#if USE_VALGRIND
629 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
630#endif
631
647#if HAVE_MMAP 632#if HAVE_MMAP
648 munmap (stack->sptr, stack->ssize); 633 munmap (cctx->sptr, cctx->ssize);
649#else 634#else
650 Safefree (stack->sptr); 635 Safefree (cctx->sptr);
651#endif 636#endif
652 637
653 Safefree (stack); 638 Safefree (cctx);
654} 639}
655 640
656static coro_stack *stack_first;
657
658static coro_stack * 641static coro_cctx *
659stack_get () 642cctx_get ()
660{ 643{
661 coro_stack *stack; 644 coro_cctx *cctx;
662 645
663 if (stack_first) 646 if (cctx_first)
664 { 647 {
665 stack = stack_first; 648 cctx = cctx_first;
666 stack_first = stack->next; 649 cctx_first = cctx->next;
650 --cctx_idle;
667 } 651 }
668 else 652 else
669 { 653 {
670 stack = stack_new (); 654 cctx = cctx_new ();
671 PL_op = PL_op->op_next; 655 PL_op = PL_op->op_next;
672 } 656 }
673 657
674 return stack; 658 return cctx;
675} 659}
676 660
677static void 661static void
678stack_put (coro_stack *stack) 662cctx_put (coro_cctx *cctx)
679{ 663{
680 stack->next = stack_first; 664 /* free another cctx if overlimit */
681 stack_first = stack; 665 if (cctx_idle >= MAX_IDLE_CCTX)
666 {
667 coro_cctx *first = cctx_first;
668 cctx_first = first->next;
669 --cctx_idle;
670
671 assert (!first->inuse);
672 cctx_destroy (first);
673 }
674
675 ++cctx_idle;
676 cctx->next = cctx_first;
677 cctx_first = cctx;
682} 678}
683 679
684/* never call directly, always through the coro_state_transfer global variable */ 680/* never call directly, always through the coro_state_transfer global variable */
685static void 681static void NOINLINE
686transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 682transfer (struct coro *prev, struct coro *next, int flags)
687{ 683{
688 dSTACKLEVEL; 684 dSTACKLEVEL;
689 685
690 /* sometimes transfer is only called to set idle_sp */ 686 /* sometimes transfer is only called to set idle_sp */
691 if (flags == TRANSFER_SET_STACKLEVEL) 687 if (flags == TRANSFER_SET_STACKLEVEL)
692 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 688 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
693 else if (prev != next) 689 else if (prev != next)
694 { 690 {
695 coro_stack *prev__stack; 691 coro_cctx *prev__cctx;
692
693 if (!prev->cctx)
694 {
695 /* create a new empty context */
696 Newz (0, prev->cctx, 1, coro_cctx);
697 prev->cctx->inuse = 1;
698 prev->flags |= CF_RUNNING;
699 }
700
701 if (!prev->flags & CF_RUNNING)
702 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
703
704 if (next->flags & CF_RUNNING)
705 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
706
707 prev->flags &= ~CF_RUNNING;
708 next->flags |= CF_RUNNING;
696 709
697 LOCK; 710 LOCK;
698 711
699 if (next->mainstack) 712 if (next->mainstack)
700 { 713 {
707 /* need to start coroutine */ 720 /* need to start coroutine */
708 /* first get rid of the old state */ 721 /* first get rid of the old state */
709 SAVE (prev, -1); 722 SAVE (prev, -1);
710 /* setup coroutine call */ 723 /* setup coroutine call */
711 setup_coro (next); 724 setup_coro (next);
712 /* need a stack */ 725 /* need a new stack */
713 next->stack = 0; 726 assert (!next->stack);
714 } 727 }
715 728
716 prev__stack = prev->stack; 729 prev__cctx = prev->cctx;
717 730
718 /* possibly "free" the stack */ 731 /* possibly "free" the cctx */
719 if (prev__stack->idle_sp == STACKLEVEL) 732 if (prev__cctx->idle_sp == STACKLEVEL)
720 { 733 {
734 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
735 assert (PL_top_env == prev__cctx->top_env);
736
721 stack_put (prev__stack); 737 cctx_put (prev__cctx);
722 prev->stack = 0; 738 prev->cctx = 0;
723 } 739 }
724 740
725 if (!next->stack) 741 if (!next->cctx)
726 next->stack = stack_get (); 742 next->cctx = cctx_get ();
727 743
728 if (prev__stack != next->stack) 744 if (prev__cctx != next->cctx)
729 { 745 {
746 assert ( prev__cctx->inuse);
747 assert (!next->cctx->inuse);
748
749 prev__cctx->inuse = 0;
750 next->cctx->inuse = 1;
751
730 prev__stack->top_env = PL_top_env; 752 prev__cctx->top_env = PL_top_env;
731 PL_top_env = next->stack->top_env; 753 PL_top_env = next->cctx->top_env;
732 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 754 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
733 } 755 }
734 756
735 free_coro_mortal (); 757 free_coro_mortal ();
736 758
737 UNLOCK; 759 UNLOCK;
738 } 760 }
739} 761}
740
741/* use this function pointer to call the above function */
742/* this is done to increase chances of the compiler not inlining the call */
743/* not static to make it even harder for the compiler (and theoretically impossible in most cases */
744void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl;
745 762
746struct transfer_args 763struct transfer_args
747{ 764{
748 struct coro *prev, *next; 765 struct coro *prev, *next;
749 int flags; 766 int flags;
750}; 767};
751 768
752#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) 769#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
753 770
754static void 771static void
755coro_state_destroy (struct coro *coro) 772coro_state_destroy (struct coro *coro)
756{ 773{
757 if (coro->refcnt--) 774 if (coro->refcnt--)
758 return; 775 return;
776
777 if (coro->flags & CF_RUNNING)
778 croak ("FATAL: tried to destroy currently running coroutine");
759 779
760 if (coro->mainstack && coro->mainstack != main_mainstack) 780 if (coro->mainstack && coro->mainstack != main_mainstack)
761 { 781 {
762 struct coro temp; 782 struct coro temp;
763 783
764 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 784 SAVE ((&temp), TRANSFER_SAVE_ALL);
765 LOAD (aTHX_ coro); 785 LOAD (coro);
766 786
767 destroy_stacks (aTHX); 787 coro_destroy_stacks ();
768 788
769 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 789 LOAD ((&temp)); /* this will get rid of defsv etc.. */
770 790
771 coro->mainstack = 0; 791 coro->mainstack = 0;
772 } 792 }
773 793
774 stack_free (coro->stack); 794 cctx_destroy (coro->cctx);
775 SvREFCNT_dec (coro->args); 795 SvREFCNT_dec (coro->args);
776 Safefree (coro); 796 Safefree (coro);
777} 797}
778 798
779static int 799static int
780coro_state_clear (SV *sv, MAGIC *mg) 800coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
781{ 801{
782 struct coro *coro = (struct coro *)mg->mg_ptr; 802 struct coro *coro = (struct coro *)mg->mg_ptr;
783 mg->mg_ptr = 0; 803 mg->mg_ptr = 0;
784 804
785 coro_state_destroy (coro); 805 coro_state_destroy (coro);
786 806
787 return 0; 807 return 0;
788} 808}
789 809
790static int 810static int
791coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 811coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
792{ 812{
793 struct coro *coro = (struct coro *)mg->mg_ptr; 813 struct coro *coro = (struct coro *)mg->mg_ptr;
794 814
795 ++coro->refcnt; 815 ++coro->refcnt;
796 816
797 return 0; 817 return 0;
798} 818}
799 819
800static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 820static MGVTBL coro_state_vtbl = {
821 0, 0, 0, 0,
822 coro_state_clear,
823 0,
824#ifdef MGf_DUP
825 coro_state_dup,
826#else
827# define MGf_DUP 0
828#endif
829};
801 830
802static struct coro * 831static struct coro *
803SvSTATE (SV *coro) 832SvSTATE (SV *coro)
804{ 833{
805 HV *stash; 834 HV *stash;
820 assert (mg->mg_type == PERL_MAGIC_ext); 849 assert (mg->mg_type == PERL_MAGIC_ext);
821 return (struct coro *)mg->mg_ptr; 850 return (struct coro *)mg->mg_ptr;
822} 851}
823 852
824static void 853static void
825prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) 854prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
826{ 855{
827 ta->prev = SvSTATE (prev); 856 ta->prev = SvSTATE (prev);
828 ta->next = SvSTATE (next); 857 ta->next = SvSTATE (next);
829 ta->flags = flags; 858 ta->flags = flags;
830} 859}
833api_transfer (SV *prev, SV *next, int flags) 862api_transfer (SV *prev, SV *next, int flags)
834{ 863{
835 dTHX; 864 dTHX;
836 struct transfer_args ta; 865 struct transfer_args ta;
837 866
838 prepare_transfer (aTHX_ &ta, prev, next, flags); 867 prepare_transfer (&ta, prev, next, flags);
839 TRANSFER (ta); 868 TRANSFER (ta);
840} 869}
841 870
842/** Coro ********************************************************************/ 871/** Coro ********************************************************************/
843 872
852static GV *coro_current, *coro_idle; 881static GV *coro_current, *coro_idle;
853static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 882static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
854static int coro_nready; 883static int coro_nready;
855 884
856static void 885static void
857coro_enq (pTHX_ SV *sv) 886coro_enq (SV *coro_sv)
858{ 887{
859 int prio;
860
861 if (SvTYPE (sv) != SVt_PVHV)
862 croak ("Coro::ready tried to enqueue something that is not a coroutine");
863
864 prio = SvSTATE (sv)->prio;
865
866 av_push (coro_ready [prio - PRIO_MIN], sv); 888 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
867 coro_nready++; 889 coro_nready++;
868} 890}
869 891
870static SV * 892static SV *
871coro_deq (pTHX_ int min_prio) 893coro_deq (int min_prio)
872{ 894{
873 int prio = PRIO_MAX - PRIO_MIN; 895 int prio = PRIO_MAX - PRIO_MIN;
874 896
875 min_prio -= PRIO_MIN; 897 min_prio -= PRIO_MIN;
876 if (min_prio < 0) 898 if (min_prio < 0)
884 } 906 }
885 907
886 return 0; 908 return 0;
887} 909}
888 910
889static void 911static int
890api_ready (SV *coro) 912api_ready (SV *coro_sv)
891{ 913{
892 dTHX; 914 struct coro *coro;
893 915
894 if (SvROK (coro)) 916 if (SvROK (coro_sv))
895 coro = SvRV (coro); 917 coro_sv = SvRV (coro_sv);
918
919 coro = SvSTATE (coro_sv);
920
921 if (coro->flags & CF_READY)
922 return 0;
923
924 if (coro->flags & CF_RUNNING)
925 croak ("Coro::ready called on currently running coroutine");
926
927 coro->flags |= CF_READY;
896 928
897 LOCK; 929 LOCK;
898 coro_enq (aTHX_ SvREFCNT_inc (coro)); 930 coro_enq (SvREFCNT_inc (coro_sv));
899 UNLOCK; 931 UNLOCK;
900}
901 932
933 return 1;
934}
935
902static void 936static int
937api_is_ready (SV *coro_sv)
938{
939 return !!SvSTATE (coro_sv)->flags & CF_READY;
940}
941
942static void
903prepare_schedule (aTHX_ struct transfer_args *ta) 943prepare_schedule (struct transfer_args *ta)
904{ 944{
905 SV *current, *prev, *next; 945 SV *current, *prev, *next;
906
907 LOCK;
908 946
909 current = GvSV (coro_current); 947 current = GvSV (coro_current);
910 948
911 for (;;) 949 for (;;)
912 { 950 {
913 LOCK; 951 LOCK;
914
915 next = coro_deq (aTHX_ PRIO_MIN); 952 next = coro_deq (PRIO_MIN);
953 UNLOCK;
916 954
917 if (next) 955 if (next)
918 break; 956 break;
919
920 UNLOCK;
921 957
922 { 958 {
923 dSP; 959 dSP;
924 960
925 ENTER; 961 ENTER;
936 972
937 prev = SvRV (current); 973 prev = SvRV (current);
938 SvRV (current) = next; 974 SvRV (current) = next;
939 975
940 /* free this only after the transfer */ 976 /* free this only after the transfer */
977 LOCK;
941 free_coro_mortal (); 978 free_coro_mortal ();
979 UNLOCK;
942 coro_mortal = prev; 980 coro_mortal = prev;
943 981
944 ta->prev = SvSTATE (prev); 982 ta->prev = SvSTATE (prev);
945 ta->next = SvSTATE (next); 983 ta->next = SvSTATE (next);
946 ta->flags = TRANSFER_SAVE_ALL; 984 ta->flags = TRANSFER_SAVE_ALL;
947 985
948 UNLOCK; 986 ta->next->flags &= ~CF_READY;
949} 987}
950 988
951static void 989static void
952prepare_cede (aTHX_ struct transfer_args *ta) 990prepare_cede (struct transfer_args *ta)
953{ 991{
954 LOCK; 992 api_ready (GvSV (coro_current));
955 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current))));
956 UNLOCK;
957 993
958 prepare_schedule (ta); 994 prepare_schedule (ta);
959} 995}
960 996
961static void 997static void
998 main_mainstack = PL_mainstack; 1034 main_mainstack = PL_mainstack;
999 1035
1000 coroapi.ver = CORO_API_VERSION; 1036 coroapi.ver = CORO_API_VERSION;
1001 coroapi.transfer = api_transfer; 1037 coroapi.transfer = api_transfer;
1002 1038
1003 Newz (0, main_stack, 1, coro_stack);
1004 main_stack->idle_sp = (void *)-1;
1005
1006 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1039 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1007} 1040}
1008 1041
1009SV * 1042SV *
1010new (char *klass, ...) 1043new (char *klass, ...)
1021 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1054 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1022 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1055 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1023 1056
1024 for (i = 1; i < items; i++) 1057 for (i = 1; i < items; i++)
1025 av_push (coro->args, newSVsv (ST (i))); 1058 av_push (coro->args, newSVsv (ST (i)));
1026
1027 coro->stack = main_stack;
1028 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1029 /*coro->stack = 0;*/
1030} 1059}
1031 OUTPUT: 1060 OUTPUT:
1032 RETVAL 1061 RETVAL
1033 1062
1034void 1063void
1035transfer (...) 1064_set_stacklevel (...)
1036 ALIAS: 1065 ALIAS:
1037 Coro::schedule = 1 1066 Coro::State::transfer = 1
1038 Coro::cede = 2 1067 Coro::schedule = 2
1039 _set_stacklevel = 3 1068 Coro::cede = 3
1069 Coro::Cont::yield = 4
1040 CODE: 1070 CODE:
1041{ 1071{
1042 struct transfer_args ta; 1072 struct transfer_args ta;
1043 1073
1044 switch (ix) 1074 switch (ix)
1045 { 1075 {
1046 case 0: 1076 case 0:
1047 if (items != 3)
1048 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items);
1049
1050 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1051 break;
1052
1053 case 1:
1054 prepare_schedule (&ta);
1055 break;
1056
1057 case 2:
1058 prepare_cede (&ta);
1059 break;
1060
1061 case 3:
1062 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1077 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1063 ta.next = 0; 1078 ta.next = 0;
1064 ta.flags = TRANSFER_SET_STACKLEVEL; 1079 ta.flags = TRANSFER_SET_STACKLEVEL;
1065 break; 1080 break;
1081
1082 case 1:
1083 if (items != 3)
1084 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items);
1085
1086 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1087 break;
1088
1089 case 2:
1090 prepare_schedule (&ta);
1091 break;
1092
1093 case 3:
1094 prepare_cede (&ta);
1095 break;
1096
1097 case 4:
1098 {
1099 SV *yieldstack;
1100 SV *sv;
1101 AV *defav = GvAV (PL_defgv);
1102
1103 yieldstack = *hv_fetch (
1104 (HV *)SvRV (GvSV (coro_current)),
1105 "yieldstack", sizeof ("yieldstack") - 1,
1106 0
1107 );
1108
1109 /* set up @_ -- ugly */
1110 av_clear (defav);
1111 av_fill (defav, items - 1);
1112 while (items--)
1113 av_store (defav, items, SvREFCNT_inc (ST(items)));
1114
1115 sv = av_pop ((AV *)SvRV (yieldstack));
1116 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1117 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1118 ta.flags = 0;
1119 SvREFCNT_dec (sv);
1120 }
1121 break;
1122
1066 } 1123 }
1067 1124
1068 TRANSFER (ta); 1125 TRANSFER (ta);
1069} 1126}
1070 1127
1077 sv_unmagic (SvRV (dst), PERL_MAGIC_ext); 1134 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1078 1135
1079 ++coro_src->refcnt; 1136 ++coro_src->refcnt;
1080 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1137 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1081} 1138}
1082
1083void
1084_nonlocal_goto (IV nextop)
1085 CODE:
1086 /* uuh, somebody will kill me again for this */
1087 PL_op->op_next = INT2PTR (OP *, nextop);
1088 1139
1089void 1140void
1090_exit (code) 1141_exit (code)
1091 int code 1142 int code
1092 PROTOTYPE: $ 1143 PROTOTYPE: $
1093 CODE: 1144 CODE:
1094 _exit (code); 1145 _exit (code);
1095 1146
1096MODULE = Coro::State PACKAGE = Coro::Cont 1147int
1148cctx_count ()
1149 CODE:
1150 RETVAL = cctx_count;
1151 OUTPUT:
1152 RETVAL
1097 1153
1098void 1154int
1099yield (...) 1155cctx_idle ()
1100 PROTOTYPE: @ 1156 CODE:
1101 CODE: 1157 RETVAL = cctx_idle;
1102{ 1158 OUTPUT:
1103 SV *yieldstack; 1159 RETVAL
1104 SV *sv;
1105 AV *defav = GvAV (PL_defgv);
1106 struct coro *prev, *next;
1107
1108 yieldstack = *hv_fetch (
1109 (HV *)SvRV (GvSV (coro_current)),
1110 "yieldstack", sizeof ("yieldstack") - 1,
1111 0
1112 );
1113
1114 /* set up @_ -- ugly */
1115 av_clear (defav);
1116 av_fill (defav, items - 1);
1117 while (items--)
1118 av_store (defav, items, SvREFCNT_inc (ST(items)));
1119
1120 sv = av_pop ((AV *)SvRV (yieldstack));
1121 prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1122 next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1123 SvREFCNT_dec (sv);
1124
1125 coro_state_transfer (aTHX_ prev, next, 0);
1126}
1127 1160
1128MODULE = Coro::State PACKAGE = Coro 1161MODULE = Coro::State PACKAGE = Coro
1129 1162
1130BOOT: 1163BOOT:
1131{ 1164{
1150 SV *sv = perl_get_sv("Coro::API", 1); 1183 SV *sv = perl_get_sv("Coro::API", 1);
1151 1184
1152 coroapi.schedule = api_schedule; 1185 coroapi.schedule = api_schedule;
1153 coroapi.cede = api_cede; 1186 coroapi.cede = api_cede;
1154 coroapi.ready = api_ready; 1187 coroapi.ready = api_ready;
1188 coroapi.is_ready = api_is_ready;
1155 coroapi.nready = &coro_nready; 1189 coroapi.nready = &coro_nready;
1156 coroapi.current = coro_current; 1190 coroapi.current = coro_current;
1157 1191
1158 GCoroAPI = &coroapi; 1192 GCoroAPI = &coroapi;
1159 sv_setiv (sv, (IV)&coroapi); 1193 sv_setiv (sv, (IV)&coroapi);
1179 1213
1180 coro->prio = newprio; 1214 coro->prio = newprio;
1181 } 1215 }
1182} 1216}
1183 1217
1184void 1218SV *
1185ready (SV *self) 1219ready (SV *self)
1186 PROTOTYPE: $ 1220 PROTOTYPE: $
1187 CODE: 1221 CODE:
1188 api_ready (self); 1222 RETVAL = boolSV (api_ready (self));
1223 OUTPUT:
1224 RETVAL
1225
1226SV *
1227is_ready (SV *self)
1228 PROTOTYPE: $
1229 CODE:
1230 RETVAL = boolSV (api_is_ready (self));
1231 OUTPUT:
1232 RETVAL
1189 1233
1190int 1234int
1191nready (...) 1235nready (...)
1192 PROTOTYPE: 1236 PROTOTYPE:
1193 CODE: 1237 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines