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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines