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.101 by root, Mon Nov 27 00:08:18 2006 UTC vs.
Revision 1.119 by root, Sat Dec 2 15:41:28 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines