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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines