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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines