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.110 by root, Tue Nov 28 23:08:07 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#if USE_VALGRIND
106static coro_stack *main_stack; 125 int valgrind_id;
126#endif
127} coro_cctx;
107 128
108/* this is a structure representing a perl-level coroutine */ 129/* this is a structure representing a perl-level coroutine */
109struct coro { 130struct coro {
110 /* the c coroutine allocated to this perl coroutine, if any */ 131 /* the c coroutine allocated to this perl coroutine, if any */
111 coro_stack *stack; 132 coro_cctx *cctx;
112 133
113 /* data associated with this coroutine (initial args) */ 134 /* data associated with this coroutine (initial args) */
114 AV *args; 135 AV *args;
115 int refcnt; 136 int refcnt;
116 137
117 /* optionally saved, might be zero */ 138 /* optionally saved, might be zero */
118 AV *defav; 139 AV *defav;
119 SV *defsv; 140 SV *defsv;
120 SV *errsv; 141 SV *errsv;
121 142
122 /* saved global state not related to stacks */ 143#define VAR(name,type) type name;
123 U8 dowarn; 144# include "state.h"
124 I32 in_eval; 145#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 146
156 /* coro process data */ 147 /* coro process data */
157 int prio; 148 int prio;
158}; 149};
159 150
160typedef struct coro *Coro__State; 151typedef struct coro *Coro__State;
161typedef struct coro *Coro__State_or_hashref; 152typedef struct coro *Coro__State_or_hashref;
162 153
163static AV * 154static AV *
164coro_clone_padlist (pTHX_ CV *cv) 155coro_clone_padlist (CV *cv)
165{ 156{
166 AV *padlist = CvPADLIST (cv); 157 AV *padlist = CvPADLIST (cv);
167 AV *newpadlist, *newpad; 158 AV *newpadlist, *newpad;
168 159
169 newpadlist = newAV (); 160 newpadlist = newAV ();
170 AvREAL_off (newpadlist); 161 AvREAL_off (newpadlist);
171#if PERL_VERSION < 9 162#if PERL_VERSION_ATLEAST (5,9,0)
163 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
164#else
172 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 165 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
173#else
174 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
175#endif 166#endif
176 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 167 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
177 --AvFILLp (padlist); 168 --AvFILLp (padlist);
178 169
179 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 170 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
181 172
182 return newpadlist; 173 return newpadlist;
183} 174}
184 175
185static void 176static void
186free_padlist (pTHX_ AV *padlist) 177free_padlist (AV *padlist)
187{ 178{
188 /* may be during global destruction */ 179 /* may be during global destruction */
189 if (SvREFCNT (padlist)) 180 if (SvREFCNT (padlist))
190 { 181 {
191 I32 i = AvFILLp (padlist); 182 I32 i = AvFILLp (padlist);
212 AV *padlist; 203 AV *padlist;
213 AV *av = (AV *)mg->mg_obj; 204 AV *av = (AV *)mg->mg_obj;
214 205
215 /* casting is fun. */ 206 /* casting is fun. */
216 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 207 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
217 free_padlist (aTHX_ padlist); 208 free_padlist (padlist);
218 209
219 SvREFCNT_dec (av); 210 SvREFCNT_dec (av);
220 211
221 return 0; 212 return 0;
222} 213}
223 214
224#define PERL_MAGIC_coro PERL_MAGIC_ext 215#define PERL_MAGIC_coro PERL_MAGIC_ext
225 216
226static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 217static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
227 218
219#define CORO_MAGIC(cv) \
220 SvMAGIC (cv) \
221 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
222 ? SvMAGIC (cv) \
223 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
224 : 0
225
228/* the next two functions merely cache the padlists */ 226/* the next two functions merely cache the padlists */
229static void 227static void
230get_padlist (pTHX_ CV *cv) 228get_padlist (CV *cv)
231{ 229{
232 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 230 MAGIC *mg = CORO_MAGIC (cv);
231 AV *av;
233 232
234 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 233 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
235 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 234 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
236 else 235 else
237 { 236 {
238#if 0 237#if 0
239 /* this should work - but it doesn't :( */ 238 /* this is probably cleaner, but also slower? */
240 CV *cp = Perl_cv_clone (aTHX_ cv); 239 CV *cp = Perl_cv_clone (cv);
241 CvPADLIST (cv) = CvPADLIST (cp); 240 CvPADLIST (cv) = CvPADLIST (cp);
242 CvPADLIST (cp) = 0; 241 CvPADLIST (cp) = 0;
243 SvREFCNT_dec (cp); 242 SvREFCNT_dec (cp);
244#else 243#else
245 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 244 CvPADLIST (cv) = coro_clone_padlist (cv);
246#endif 245#endif
247 } 246 }
248} 247}
249 248
250static void 249static void
251put_padlist (pTHX_ CV *cv) 250put_padlist (CV *cv)
252{ 251{
253 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 252 MAGIC *mg = CORO_MAGIC (cv);
253 AV *av;
254 254
255 if (!mg) 255 if (!mg)
256 { 256 {
257 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 257 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
258 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 258 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
259 mg->mg_virtual = &vtbl_coro; 259 mg->mg_virtual = &vtbl_coro;
260 mg->mg_obj = (SV *)newAV (); 260 mg->mg_obj = (SV *)newAV ();
261 } 261 }
262 262
263 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); 263 av = (AV *)mg->mg_obj;
264
265 if (AvFILLp (av) >= AvMAX (av))
266 av_extend (av, AvMAX (av) + 1);
267
268 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
264} 269}
265 270
266#define SB do { 271#define SB do {
267#define SE } while (0) 272#define SE } while (0)
268 273
269#define LOAD(state) load_state(aTHX_ (state)); 274#define LOAD(state) load_state((state));
270#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); 275#define SAVE(state,flags) save_state((state),(flags));
271 276
272#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 277#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
273 278
274static void 279static void
275load_state(pTHX_ Coro__State c) 280load_state(Coro__State c)
276{ 281{
277 PL_dowarn = c->dowarn; 282#define VAR(name,type) PL_ ## name = c->name;
278 PL_in_eval = c->in_eval; 283# include "state.h"
279 284#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 285
311 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 286 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
312 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 287 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
313 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 288 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
314 289
317 CV *cv; 292 CV *cv;
318 293
319 /* now do the ugly restore mess */ 294 /* now do the ugly restore mess */
320 while ((cv = (CV *)POPs)) 295 while ((cv = (CV *)POPs))
321 { 296 {
322 AV *padlist = (AV *)POPs;
323
324 if (padlist)
325 {
326 put_padlist (aTHX_ cv); /* mark this padlist as available */ 297 put_padlist (cv); /* mark this padlist as available */
327 CvPADLIST(cv) = padlist; 298 CvDEPTH (cv) = PTR2IV (POPs);
328 } 299 CvPADLIST (cv) = (AV *)POPs;
329
330 ++CvDEPTH(cv);
331 } 300 }
332 301
333 PUTBACK; 302 PUTBACK;
334 } 303 }
335} 304}
336 305
337static void 306static void
338save_state(pTHX_ Coro__State c, int flags) 307save_state(Coro__State c, int flags)
339{ 308{
340 { 309 {
341 dSP; 310 dSP;
342 I32 cxix = cxstack_ix; 311 I32 cxix = cxstack_ix;
343 PERL_CONTEXT *ccstk = cxstack; 312 PERL_CONTEXT *ccstk = cxstack;
357 PERL_CONTEXT *cx = &ccstk[cxix--]; 326 PERL_CONTEXT *cx = &ccstk[cxix--];
358 327
359 if (CxTYPE(cx) == CXt_SUB) 328 if (CxTYPE(cx) == CXt_SUB)
360 { 329 {
361 CV *cv = cx->blk_sub.cv; 330 CV *cv = cx->blk_sub.cv;
331
362 if (CvDEPTH(cv)) 332 if (CvDEPTH (cv))
363 { 333 {
364 EXTEND (SP, CvDEPTH(cv)*2); 334 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 335
373 PUSHs ((SV *)CvPADLIST(cv)); 336 PUSHs ((SV *)CvPADLIST(cv));
337 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
374 PUSHs ((SV *)cv); 338 PUSHs ((SV *)cv);
375 339
340 CvDEPTH (cv) = 0;
376 get_padlist (aTHX_ cv); 341 get_padlist (cv);
377 } 342 }
378 } 343 }
379#ifdef CXt_FORMAT 344#ifdef CXt_FORMAT
380 else if (CxTYPE(cx) == CXt_FORMAT) 345 else if (CxTYPE(cx) == CXt_FORMAT)
381 { 346 {
399 364
400 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 365 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
401 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 366 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
402 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 367 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
403 368
404 c->dowarn = PL_dowarn; 369#define VAR(name,type)c->name = PL_ ## name;
405 c->in_eval = PL_in_eval; 370# include "state.h"
406 371#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} 372}
438 373
439/* 374/*
440 * allocate various perl stacks. This is an exact copy 375 * allocate various perl stacks. This is an exact copy
441 * of perl.c:init_stacks, except that it uses less memory 376 * of perl.c:init_stacks, except that it uses less memory
442 * on the (sometimes correct) assumption that coroutines do 377 * on the (sometimes correct) assumption that coroutines do
443 * not usually need a lot of stackspace. 378 * not usually need a lot of stackspace.
444 */ 379 */
445static void 380static void
446coro_init_stacks (pTHX) 381coro_init_stacks ()
447{ 382{
448 LOCK;
449
450 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 383 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
451 PL_curstackinfo->si_type = PERLSI_MAIN; 384 PL_curstackinfo->si_type = PERLSI_MAIN;
452 PL_curstack = PL_curstackinfo->si_stack; 385 PL_curstack = PL_curstackinfo->si_stack;
453 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 386 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
454 387
475 408
476 New(54,PL_savestack,96,ANY); 409 New(54,PL_savestack,96,ANY);
477 PL_savestack_ix = 0; 410 PL_savestack_ix = 0;
478 PL_savestack_max = 96; 411 PL_savestack_max = 96;
479 412
480#if PERL_VERSION < 9 413#if !PERL_VERSION_ATLEAST (5,9,0)
481 New(54,PL_retstack,8,OP*); 414 New(54,PL_retstack,8,OP*);
482 PL_retstack_ix = 0; 415 PL_retstack_ix = 0;
483 PL_retstack_max = 8; 416 PL_retstack_max = 8;
484#endif 417#endif
485
486 UNLOCK;
487} 418}
488 419
489/* 420/*
490 * destroy the stacks, the callchain etc... 421 * destroy the stacks, the callchain etc...
491 */ 422 */
492static void 423static void
493destroy_stacks(pTHX) 424coro_destroy_stacks ()
494{ 425{
495 if (!IN_DESTRUCT) 426 if (!IN_DESTRUCT)
496 { 427 {
497 /* is this ugly, I ask? */ 428 /* is this ugly, I ask? */
498 LEAVE_SCOPE (0); 429 LEAVE_SCOPE (0);
529 460
530 Safefree (PL_tmps_stack); 461 Safefree (PL_tmps_stack);
531 Safefree (PL_markstack); 462 Safefree (PL_markstack);
532 Safefree (PL_scopestack); 463 Safefree (PL_scopestack);
533 Safefree (PL_savestack); 464 Safefree (PL_savestack);
534#if PERL_VERSION < 9 465#if !PERL_VERSION_ATLEAST (5,9,0)
535 Safefree (PL_retstack); 466 Safefree (PL_retstack);
536#endif 467#endif
537} 468}
538 469
539static void 470static void
540setup_coro (struct coro *coro) 471setup_coro (struct coro *coro)
541{ 472{
542 /* 473 /*
543 * emulate part of the perl startup here. 474 * emulate part of the perl startup here.
544 */ 475 */
545 dTHX;
546 dSP;
547 UNOP myop;
548 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
549 476
550 coro_init_stacks (aTHX); 477 coro_init_stacks ();
478
551 /*PL_curcop = 0;*/ 479 PL_curcop = 0;
552 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 480 PL_in_eval = 0;
481 PL_curpm = 0;
482
483 {
484 dSP;
485 LOGOP myop;
486
487 /* I have no idea why this is needed, but it is */
488 PUSHMARK (SP);
489
553 SvREFCNT_dec (GvAV (PL_defgv)); 490 SvREFCNT_dec (GvAV (PL_defgv));
554 GvAV (PL_defgv) = coro->args; coro->args = 0; 491 GvAV (PL_defgv) = coro->args; coro->args = 0;
555 492
556 SPAGAIN;
557
558 Zero (&myop, 1, UNOP); 493 Zero (&myop, 1, LOGOP);
559 myop.op_next = Nullop; 494 myop.op_next = Nullop;
560 myop.op_flags = OPf_WANT_VOID; 495 myop.op_flags = OPf_WANT_VOID;
561 496
562 PL_op = (OP *)&myop; 497 PL_op = (OP *)&myop;
563 498
564 PUSHMARK(SP); 499 PUSHMARK (SP);
565 XPUSHs (sub_init); 500 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
566 PUTBACK; 501 PUTBACK;
567 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 502 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
568 SPAGAIN; 503 SPAGAIN;
569 504
570 ENTER; /* necessary e.g. for dounwind */ 505 ENTER; /* necessary e.g. for dounwind */
506 }
571} 507}
572 508
573static void 509static void
574free_coro_mortal () 510free_coro_mortal ()
575{ 511{
578 SvREFCNT_dec (coro_mortal); 514 SvREFCNT_dec (coro_mortal);
579 coro_mortal = 0; 515 coro_mortal = 0;
580 } 516 }
581} 517}
582 518
519static void NOINLINE
520prepare_cctx (coro_cctx *cctx)
521{
522 dSP;
523 LOGOP myop;
524
525 Zero (&myop, 1, LOGOP);
526 myop.op_next = PL_op;
527 myop.op_flags = OPf_WANT_VOID;
528
529 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
530
531 PUSHMARK (SP);
532 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
533 PUTBACK;
534 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
535 SPAGAIN;
536}
537
583static void 538static void
584coro_run (void *arg) 539coro_run (void *arg)
585{ 540{
541 /* coro_run is the alternative epilogue of transfer() */
542 UNLOCK;
543
586 /* 544 /*
587 * this is a _very_ stripped down perl interpreter ;) 545 * this is a _very_ stripped down perl interpreter ;)
588 */ 546 */
589 dTHX;
590
591 UNLOCK;
592
593 PL_top_env = &PL_start_env; 547 PL_top_env = &PL_start_env;
594 548
595 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); 549 /* inject call to cctx_init */
596 sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op)); 550 prepare_cctx ((coro_cctx *)arg);
597 551
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 */ 552 /* somebody will hit me for both perl_run and PL_restartop */
602 perl_run (aTHX_ PERL_GET_CONTEXT); 553 perl_run (PL_curinterp);
603 554
555 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
604 abort (); 556 abort ();
605} 557}
606 558
607static coro_stack * 559static coro_cctx *
608stack_new () 560cctx_new ()
609{ 561{
610 coro_stack *stack; 562 coro_cctx *cctx;
611 563
564 ++cctx_count;
565
612 New (0, stack, 1, coro_stack); 566 New (0, cctx, 1, coro_cctx);
613 567
614#if HAVE_MMAP 568#if HAVE_MMAP
615 569
616 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ 570 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
571 /* 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); 572 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
618 573
619 if (stack->sptr == (void *)-1) 574 if (cctx->sptr == (void *)-1)
620 { 575 {
621 perror ("FATAL: unable to mmap stack for coroutine"); 576 perror ("FATAL: unable to mmap stack for coroutine");
622 _exit (EXIT_FAILURE); 577 _exit (EXIT_FAILURE);
623 } 578 }
624 579
580# if STACKGUARD
625 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 581 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
582# endif
626 583
627#else 584#else
628 585
629 stack->ssize = STACKSIZE * (long)sizeof (long); 586 cctx->ssize = STACKSIZE * (long)sizeof (long);
630 New (0, stack->sptr, STACKSIZE, long); 587 New (0, cctx->sptr, STACKSIZE, long);
631 588
632 if (!stack->sptr) 589 if (!cctx->sptr)
633 { 590 {
634 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 591 perror ("FATAL: unable to malloc stack for coroutine");
635 _exit (EXIT_FAILURE); 592 _exit (EXIT_FAILURE);
636 } 593 }
637 594
638#endif 595#endif
639 596
597#if USE_VALGRIND
598 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
599 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
600 cctx->ssize + (char *)cctx->sptr
601 );
602#endif
603
640 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); 604 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
641 605
642 return stack; 606 return cctx;
643} 607}
644 608
645static void 609static void
646stack_free (coro_stack *stack) 610cctx_free (coro_cctx *cctx)
647{ 611{
648 if (!stack || stack == main_stack) 612 if (!cctx)
649 return; 613 return;
650 614
615 --cctx_count;
616
617#if USE_VALGRIND
618 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
619#endif
620
651#if HAVE_MMAP 621#if HAVE_MMAP
652 munmap (stack->sptr, stack->ssize); 622 munmap (cctx->sptr, cctx->ssize);
653#else 623#else
654 Safefree (stack->sptr); 624 Safefree (cctx->sptr);
655#endif 625#endif
656 626
657 Safefree (stack); 627 Safefree (cctx);
658} 628}
659 629
660static coro_stack *stack_first;
661
662static coro_stack * 630static coro_cctx *
663stack_get () 631cctx_get ()
664{ 632{
665 coro_stack *stack; 633 coro_cctx *cctx;
666 634
667 if (stack_first) 635 if (cctx_first)
668 { 636 {
669 stack = stack_first; 637 --cctx_idle;
638 cctx = cctx_first;
670 stack_first = stack->next; 639 cctx_first = cctx->next;
671 } 640 }
672 else 641 else
673 { 642 {
674 stack = stack_new (); 643 cctx = cctx_new ();
675 PL_op = PL_op->op_next; 644 PL_op = PL_op->op_next;
676 } 645 }
677 646
678 return stack; 647 return cctx;
679} 648}
680 649
681static void 650static void
682stack_put (coro_stack *stack) 651cctx_put (coro_cctx *cctx)
683{ 652{
684 stack->next = stack_first; 653 ++cctx_idle;
685 stack_first = stack; 654 cctx->next = cctx_first;
655 cctx_first = cctx;
686} 656}
687 657
688/* never call directly, always through the coro_state_transfer global variable */ 658/* never call directly, always through the coro_state_transfer global variable */
689static void 659static void NOINLINE
690transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 660transfer (struct coro *prev, struct coro *next, int flags)
691{ 661{
692 dSTACKLEVEL; 662 dSTACKLEVEL;
693 663
694 /* sometimes transfer is only called to set idle_sp */ 664 /* sometimes transfer is only called to set idle_sp */
695 if (flags == TRANSFER_SET_STACKLEVEL) 665 if (flags == TRANSFER_SET_STACKLEVEL)
696 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 666 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
697 else if (prev != next) 667 else if (prev != next)
698 { 668 {
699 coro_stack *prev__stack; 669 coro_cctx *prev__cctx;
700 670
701 LOCK; 671 LOCK;
702 672
703 if (next->mainstack) 673 if (next->mainstack)
704 { 674 {
712 /* first get rid of the old state */ 682 /* first get rid of the old state */
713 SAVE (prev, -1); 683 SAVE (prev, -1);
714 /* setup coroutine call */ 684 /* setup coroutine call */
715 setup_coro (next); 685 setup_coro (next);
716 /* need a stack */ 686 /* need a stack */
717 next->stack = 0; 687 next->cctx = 0;
718 } 688 }
719 689
690 if (!prev->cctx)
691 /* create a new empty context */
692 Newz (0, prev->cctx, 1, coro_cctx);
693
720 prev__stack = prev->stack; 694 prev__cctx = prev->cctx;
721 695
722 /* possibly "free" the stack */ 696 /* possibly "free" the cctx */
723 if (prev__stack->idle_sp == STACKLEVEL) 697 if (prev__cctx->idle_sp == STACKLEVEL)
724 { 698 {
699 assert (PL_top_env == prev__cctx->top_env);//D
725 stack_put (prev__stack); 700 cctx_put (prev__cctx);
726 prev->stack = 0; 701 prev->cctx = 0;
727 } 702 }
728 703
729 if (!next->stack) 704 if (!next->cctx)
730 next->stack = stack_get (); 705 next->cctx = cctx_get ();
731 706
732 if (prev__stack != next->stack) 707 if (prev__cctx != next->cctx)
733 { 708 {
734 prev__stack->top_env = PL_top_env; 709 prev__cctx->top_env = PL_top_env;
735 PL_top_env = next->stack->top_env; 710 PL_top_env = next->cctx->top_env;
736 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 711 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
737 } 712 }
738 713
739 free_coro_mortal (); 714 free_coro_mortal ();
740 715
741 UNLOCK; 716 UNLOCK;
742 } 717 }
743} 718}
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 719
750struct transfer_args 720struct transfer_args
751{ 721{
752 struct coro *prev, *next; 722 struct coro *prev, *next;
753 int flags; 723 int flags;
754}; 724};
755 725
756#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) 726#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
757 727
758static void 728static void
759coro_state_destroy (struct coro *coro) 729coro_state_destroy (struct coro *coro)
760{ 730{
761 if (coro->refcnt--) 731 if (coro->refcnt--)
763 733
764 if (coro->mainstack && coro->mainstack != main_mainstack) 734 if (coro->mainstack && coro->mainstack != main_mainstack)
765 { 735 {
766 struct coro temp; 736 struct coro temp;
767 737
768 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 738 SAVE ((&temp), TRANSFER_SAVE_ALL);
769 LOAD (aTHX_ coro); 739 LOAD (coro);
770 740
771 destroy_stacks (aTHX); 741 coro_destroy_stacks ();
772 742
773 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 743 LOAD ((&temp)); /* this will get rid of defsv etc.. */
774 744
775 coro->mainstack = 0; 745 coro->mainstack = 0;
776 } 746 }
777 747
778 stack_free (coro->stack); 748 cctx_free (coro->cctx);
779 SvREFCNT_dec (coro->args); 749 SvREFCNT_dec (coro->args);
780 Safefree (coro); 750 Safefree (coro);
781} 751}
782 752
783static int 753static int
784coro_state_clear (SV *sv, MAGIC *mg) 754coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
785{ 755{
786 struct coro *coro = (struct coro *)mg->mg_ptr; 756 struct coro *coro = (struct coro *)mg->mg_ptr;
787 mg->mg_ptr = 0; 757 mg->mg_ptr = 0;
788 758
789 coro_state_destroy (coro); 759 coro_state_destroy (coro);
790 760
791 return 0; 761 return 0;
792} 762}
793 763
794static int 764static int
795coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 765coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
796{ 766{
797 struct coro *coro = (struct coro *)mg->mg_ptr; 767 struct coro *coro = (struct coro *)mg->mg_ptr;
798 768
799 ++coro->refcnt; 769 ++coro->refcnt;
800 770
801 return 0; 771 return 0;
802} 772}
803 773
804static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 774static MGVTBL coro_state_vtbl = {
775 0, 0, 0, 0,
776 coro_state_clear,
777 0,
778#ifdef MGf_DUP
779 coro_state_dup,
780#else
781# define MGf_DUP 0
782#endif
783};
805 784
806static struct coro * 785static struct coro *
807SvSTATE (SV *coro) 786SvSTATE (SV *coro)
808{ 787{
809 HV *stash; 788 HV *stash;
824 assert (mg->mg_type == PERL_MAGIC_ext); 803 assert (mg->mg_type == PERL_MAGIC_ext);
825 return (struct coro *)mg->mg_ptr; 804 return (struct coro *)mg->mg_ptr;
826} 805}
827 806
828static void 807static void
829prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) 808prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
830{ 809{
831 ta->prev = SvSTATE (prev); 810 ta->prev = SvSTATE (prev);
832 ta->next = SvSTATE (next); 811 ta->next = SvSTATE (next);
833 ta->flags = flags; 812 ta->flags = flags;
834} 813}
837api_transfer (SV *prev, SV *next, int flags) 816api_transfer (SV *prev, SV *next, int flags)
838{ 817{
839 dTHX; 818 dTHX;
840 struct transfer_args ta; 819 struct transfer_args ta;
841 820
842 prepare_transfer (aTHX_ &ta, prev, next, flags); 821 prepare_transfer (&ta, prev, next, flags);
843 TRANSFER (ta); 822 TRANSFER (ta);
844} 823}
845 824
846/** Coro ********************************************************************/ 825/** Coro ********************************************************************/
847 826
856static GV *coro_current, *coro_idle; 835static GV *coro_current, *coro_idle;
857static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 836static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
858static int coro_nready; 837static int coro_nready;
859 838
860static void 839static void
861coro_enq (pTHX_ SV *sv) 840coro_enq (SV *sv)
862{ 841{
863 int prio; 842 int prio;
864 843
865 if (SvTYPE (sv) != SVt_PVHV) 844 if (SvTYPE (sv) != SVt_PVHV)
866 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 845 croak ("Coro::ready tried to enqueue something that is not a coroutine");
870 av_push (coro_ready [prio - PRIO_MIN], sv); 849 av_push (coro_ready [prio - PRIO_MIN], sv);
871 coro_nready++; 850 coro_nready++;
872} 851}
873 852
874static SV * 853static SV *
875coro_deq (pTHX_ int min_prio) 854coro_deq (int min_prio)
876{ 855{
877 int prio = PRIO_MAX - PRIO_MIN; 856 int prio = PRIO_MAX - PRIO_MIN;
878 857
879 min_prio -= PRIO_MIN; 858 min_prio -= PRIO_MIN;
880 if (min_prio < 0) 859 if (min_prio < 0)
897 876
898 if (SvROK (coro)) 877 if (SvROK (coro))
899 coro = SvRV (coro); 878 coro = SvRV (coro);
900 879
901 LOCK; 880 LOCK;
902 coro_enq (aTHX_ SvREFCNT_inc (coro)); 881 coro_enq (SvREFCNT_inc (coro));
903 UNLOCK; 882 UNLOCK;
904} 883}
905 884
906static void 885static void
907prepare_schedule (aTHX_ struct transfer_args *ta) 886prepare_schedule (struct transfer_args *ta)
908{ 887{
909 SV *current, *prev, *next; 888 SV *current, *prev, *next;
910
911 LOCK;
912 889
913 current = GvSV (coro_current); 890 current = GvSV (coro_current);
914 891
915 for (;;) 892 for (;;)
916 { 893 {
917 LOCK; 894 LOCK;
918
919 next = coro_deq (aTHX_ PRIO_MIN); 895 next = coro_deq (PRIO_MIN);
896 UNLOCK;
920 897
921 if (next) 898 if (next)
922 break; 899 break;
923
924 UNLOCK;
925 900
926 { 901 {
927 dSP; 902 dSP;
928 903
929 ENTER; 904 ENTER;
940 915
941 prev = SvRV (current); 916 prev = SvRV (current);
942 SvRV (current) = next; 917 SvRV (current) = next;
943 918
944 /* free this only after the transfer */ 919 /* free this only after the transfer */
920 LOCK;
945 free_coro_mortal (); 921 free_coro_mortal ();
922 UNLOCK;
946 coro_mortal = prev; 923 coro_mortal = prev;
947 924
948 ta->prev = SvSTATE (prev); 925 ta->prev = SvSTATE (prev);
949 ta->next = SvSTATE (next); 926 ta->next = SvSTATE (next);
950 ta->flags = TRANSFER_SAVE_ALL; 927 ta->flags = TRANSFER_SAVE_ALL;
951
952 UNLOCK;
953} 928}
954 929
955static void 930static void
956prepare_cede (aTHX_ struct transfer_args *ta) 931prepare_cede (struct transfer_args *ta)
957{ 932{
958 LOCK; 933 LOCK;
959 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); 934 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
960 UNLOCK; 935 UNLOCK;
961 936
962 prepare_schedule (ta); 937 prepare_schedule (ta);
963} 938}
964 939
1002 main_mainstack = PL_mainstack; 977 main_mainstack = PL_mainstack;
1003 978
1004 coroapi.ver = CORO_API_VERSION; 979 coroapi.ver = CORO_API_VERSION;
1005 coroapi.transfer = api_transfer; 980 coroapi.transfer = api_transfer;
1006 981
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)); 982 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1011} 983}
1012 984
1013SV * 985SV *
1014new (char *klass, ...) 986new (char *klass, ...)
1025 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 997 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)); 998 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1027 999
1028 for (i = 1; i < items; i++) 1000 for (i = 1; i < items; i++)
1029 av_push (coro->args, newSVsv (ST (i))); 1001 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} 1002}
1035 OUTPUT: 1003 OUTPUT:
1036 RETVAL 1004 RETVAL
1037 1005
1038void 1006void
1039transfer (...) 1007_set_stacklevel (...)
1040 ALIAS: 1008 ALIAS:
1041 Coro::schedule = 1 1009 Coro::State::transfer = 1
1042 Coro::cede = 2 1010 Coro::schedule = 2
1043 _set_stacklevel = 3 1011 Coro::cede = 3
1012 Coro::Cont::yield = 4
1044 CODE: 1013 CODE:
1045{ 1014{
1046 struct transfer_args ta; 1015 struct transfer_args ta;
1047 1016
1048 switch (ix) 1017 switch (ix)
1049 { 1018 {
1050 case 0: 1019 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))); 1020 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1067 ta.next = 0; 1021 ta.next = 0;
1068 ta.flags = TRANSFER_SET_STACKLEVEL; 1022 ta.flags = TRANSFER_SET_STACKLEVEL;
1069 break; 1023 break;
1024
1025 case 1:
1026 if (items != 3)
1027 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items);
1028
1029 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1030 break;
1031
1032 case 2:
1033 prepare_schedule (&ta);
1034 break;
1035
1036 case 3:
1037 prepare_cede (&ta);
1038 break;
1039
1040 case 4:
1041 {
1042 SV *yieldstack;
1043 SV *sv;
1044 AV *defav = GvAV (PL_defgv);
1045
1046 yieldstack = *hv_fetch (
1047 (HV *)SvRV (GvSV (coro_current)),
1048 "yieldstack", sizeof ("yieldstack") - 1,
1049 0
1050 );
1051
1052 /* set up @_ -- ugly */
1053 av_clear (defav);
1054 av_fill (defav, items - 1);
1055 while (items--)
1056 av_store (defav, items, SvREFCNT_inc (ST(items)));
1057
1058 sv = av_pop ((AV *)SvRV (yieldstack));
1059 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1060 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1061 ta.flags = 0;
1062 SvREFCNT_dec (sv);
1063 }
1064 break;
1065
1070 } 1066 }
1071 1067
1072 TRANSFER (ta); 1068 TRANSFER (ta);
1073} 1069}
1074 1070
1081 sv_unmagic (SvRV (dst), PERL_MAGIC_ext); 1077 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1082 1078
1083 ++coro_src->refcnt; 1079 ++coro_src->refcnt;
1084 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1080 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1085} 1081}
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 1082
1093void 1083void
1094_exit (code) 1084_exit (code)
1095 int code 1085 int code
1096 PROTOTYPE: $ 1086 PROTOTYPE: $
1097 CODE: 1087 CODE:
1098 _exit (code); 1088 _exit (code);
1099 1089
1100MODULE = Coro::State PACKAGE = Coro::Cont 1090int
1091cctx_count ()
1092 CODE:
1093 RETVAL = cctx_count;
1094 OUTPUT:
1095 RETVAL
1101 1096
1102void 1097int
1103yield (...) 1098cctx_idle ()
1104 PROTOTYPE: @ 1099 CODE:
1105 CODE: 1100 RETVAL = cctx_idle;
1106{ 1101 OUTPUT:
1107 SV *yieldstack; 1102 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 1103
1132MODULE = Coro::State PACKAGE = Coro 1104MODULE = Coro::State PACKAGE = Coro
1133 1105
1134BOOT: 1106BOOT:
1135{ 1107{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines