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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines