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.99 by root, Sun Nov 26 23:53:20 2006 UTC vs.
Revision 1.115 by root, Fri Dec 1 13:36:08 2006 UTC

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 8
9#if PERL_VERSION < 6 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
15
16#define PERL_VERSION_ATLEAST(a,b,c) \
17 (PERL_REVISION > (a) \
18 || (PERL_REVISION == (a) \
19 && (PERL_VERSION > (b) \
20 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
21
22#if !PERL_VERSION_ATLEAST (5,6,0)
10# ifndef PL_ppaddr 23# ifndef PL_ppaddr
11# define PL_ppaddr ppaddr 24# define PL_ppaddr ppaddr
12# endif 25# endif
13# ifndef call_sv 26# ifndef call_sv
14# define call_sv perl_call_sv 27# define call_sv perl_call_sv
52# ifndef PAGESIZE 65# ifndef PAGESIZE
53# define PAGESIZE pagesize 66# define PAGESIZE pagesize
54# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) 67# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
55static long pagesize; 68static long pagesize;
56# else 69# else
57# define BOOT_PAGESIZE 70# define BOOT_PAGESIZE (void)0
58# endif 71# endif
72#else
73# define PAGESIZE 0
74# define BOOT_PAGESIZE (void)0
59#endif 75#endif
60 76
61/* 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
62 * 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
63 * and should be unique. */ 79 * and should be unique. */
64#define dSTACKLEVEL int stacklevel 80#define dSTACKLEVEL int stacklevel
65#define STACKLEVEL ((void *)&stacklevel) 81#define STACKLEVEL ((void *)&stacklevel)
66 82
67#define IN_DESTRUCT (PL_main_cv == Nullcv) 83#define IN_DESTRUCT (PL_main_cv == Nullcv)
84
85#if __GNUC__ >= 3
86# define attribute(x) __attribute__(x)
87#else
88# define attribute(x)
89#endif
90
91#define NOINLINE attribute ((noinline))
68 92
69#include "CoroAPI.h" 93#include "CoroAPI.h"
70 94
71#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ 95#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
72 96
82static struct CoroAPI coroapi; 106static struct CoroAPI coroapi;
83static AV *main_mainstack; /* used to differentiate between $main and others */ 107static AV *main_mainstack; /* used to differentiate between $main and others */
84static HV *coro_state_stash, *coro_stash; 108static HV *coro_state_stash, *coro_stash;
85static SV *coro_mortal; /* will be freed after next transfer */ 109static SV *coro_mortal; /* will be freed after next transfer */
86 110
111static struct coro_cctx *cctx_first;
112static int cctx_count, cctx_idle;
113
87/* this is a structure representing a c-level coroutine */ 114/* this is a structure representing a c-level coroutine */
88typedef struct coro_stack { 115typedef struct coro_cctx {
89 struct coro_stack *next; 116 struct coro_cctx *next;
90 117
91 /* the stack */ 118 /* the stack */
92 void *sptr; 119 void *sptr;
93 long ssize; /* positive == mmap, otherwise malloc */ 120 long ssize; /* positive == mmap, otherwise malloc */
94 121
95 /* cpu state */ 122 /* cpu state */
96 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 123 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
97 JMPENV *top_env; 124 JMPENV *top_env;
98 coro_context cctx; 125 coro_context cctx;
99} 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, /* coroutine is running */
136 CF_READY, /* coroutine is ready */
137};
100 138
101/* this is a structure representing a perl-level coroutine */ 139/* this is a structure representing a perl-level coroutine */
102struct coro { 140struct coro {
103 /* the c coroutine allocated to this perl coroutine, if any */ 141 /* the c coroutine allocated to this perl coroutine, if any */
104 coro_stack *stack; 142 coro_cctx *cctx;
105 143
106 /* data associated with this coroutine (initial args) */ 144 /* data associated with this coroutine (initial args) */
107 AV *args; 145 AV *args;
108 int refcnt; 146 int refcnt;
147 int flags;
109 148
110 /* optionally saved, might be zero */ 149 /* optionally saved, might be zero */
111 AV *defav; 150 AV *defav;
112 SV *defsv; 151 SV *defsv;
113 SV *errsv; 152 SV *errsv;
114 153
115 /* saved global state not related to stacks */ 154#define VAR(name,type) type name;
116 U8 dowarn; 155# include "state.h"
117 I32 in_eval; 156#undef VAR
118
119 /* the stacks and related info (callchain etc..) */
120 PERL_SI *curstackinfo;
121 AV *curstack;
122 AV *mainstack;
123 SV **stack_sp;
124 OP *op;
125 SV **curpad;
126 AV *comppad;
127 CV *compcv;
128 SV **stack_base;
129 SV **stack_max;
130 SV **tmps_stack;
131 I32 tmps_floor;
132 I32 tmps_ix;
133 I32 tmps_max;
134 I32 *markstack;
135 I32 *markstack_ptr;
136 I32 *markstack_max;
137 I32 *scopestack;
138 I32 scopestack_ix;
139 I32 scopestack_max;
140 ANY *savestack;
141 I32 savestack_ix;
142 I32 savestack_max;
143 OP **retstack;
144 I32 retstack_ix;
145 I32 retstack_max;
146 PMOP *curpm;
147 COP *curcop;
148 157
149 /* coro process data */ 158 /* coro process data */
150 int prio; 159 int prio;
151}; 160};
152 161
159 AV *padlist = CvPADLIST (cv); 168 AV *padlist = CvPADLIST (cv);
160 AV *newpadlist, *newpad; 169 AV *newpadlist, *newpad;
161 170
162 newpadlist = newAV (); 171 newpadlist = newAV ();
163 AvREAL_off (newpadlist); 172 AvREAL_off (newpadlist);
164#if PERL_VERSION < 9 173#if PERL_VERSION_ATLEAST (5,9,0)
174 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
175#else
165 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 176 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
166#else
167 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
168#endif 177#endif
169 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 178 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
170 --AvFILLp (padlist); 179 --AvFILLp (padlist);
171 180
172 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 181 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
216 225
217#define PERL_MAGIC_coro PERL_MAGIC_ext 226#define PERL_MAGIC_coro PERL_MAGIC_ext
218 227
219static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 228static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
220 229
230#define CORO_MAGIC(cv) \
231 SvMAGIC (cv) \
232 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
233 ? SvMAGIC (cv) \
234 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
235 : 0
236
221/* the next two functions merely cache the padlists */ 237/* the next two functions merely cache the padlists */
222static void 238static void
223get_padlist (CV *cv) 239get_padlist (CV *cv)
224{ 240{
225 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 241 MAGIC *mg = CORO_MAGIC (cv);
242 AV *av;
226 243
227 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 244 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
228 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 245 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
229 else 246 else
230 { 247 {
231#if 0 248#if 0
232 /* this is probably cleaner, but also slower? */ 249 /* this is probably cleaner, but also slower? */
233 CV *cp = Perl_cv_clone (cv); 250 CV *cp = Perl_cv_clone (cv);
241} 258}
242 259
243static void 260static void
244put_padlist (CV *cv) 261put_padlist (CV *cv)
245{ 262{
246 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 263 MAGIC *mg = CORO_MAGIC (cv);
264 AV *av;
247 265
248 if (!mg) 266 if (!mg)
249 { 267 {
250 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 268 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
251 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 269 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
252 mg->mg_virtual = &vtbl_coro; 270 mg->mg_virtual = &vtbl_coro;
253 mg->mg_obj = (SV *)newAV (); 271 mg->mg_obj = (SV *)newAV ();
254 } 272 }
255 273
256 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); 274 av = (AV *)mg->mg_obj;
275
276 if (AvFILLp (av) >= AvMAX (av))
277 av_extend (av, AvMAX (av) + 1);
278
279 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
257} 280}
258 281
259#define SB do { 282#define SB do {
260#define SE } while (0) 283#define SE } while (0)
261 284
265#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 288#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
266 289
267static void 290static void
268load_state(Coro__State c) 291load_state(Coro__State c)
269{ 292{
270 PL_dowarn = c->dowarn; 293#define VAR(name,type) PL_ ## name = c->name;
271 PL_in_eval = c->in_eval; 294# include "state.h"
272 295#undef VAR
273 PL_curstackinfo = c->curstackinfo;
274 PL_curstack = c->curstack;
275 PL_mainstack = c->mainstack;
276 PL_stack_sp = c->stack_sp;
277 PL_op = c->op;
278 PL_curpad = c->curpad;
279 PL_comppad = c->comppad;
280 PL_compcv = c->compcv;
281 PL_stack_base = c->stack_base;
282 PL_stack_max = c->stack_max;
283 PL_tmps_stack = c->tmps_stack;
284 PL_tmps_floor = c->tmps_floor;
285 PL_tmps_ix = c->tmps_ix;
286 PL_tmps_max = c->tmps_max;
287 PL_markstack = c->markstack;
288 PL_markstack_ptr = c->markstack_ptr;
289 PL_markstack_max = c->markstack_max;
290 PL_scopestack = c->scopestack;
291 PL_scopestack_ix = c->scopestack_ix;
292 PL_scopestack_max = c->scopestack_max;
293 PL_savestack = c->savestack;
294 PL_savestack_ix = c->savestack_ix;
295 PL_savestack_max = c->savestack_max;
296#if PERL_VERSION < 9
297 PL_retstack = c->retstack;
298 PL_retstack_ix = c->retstack_ix;
299 PL_retstack_max = c->retstack_max;
300#endif
301 PL_curpm = c->curpm;
302 PL_curcop = c->curcop;
303 296
304 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 297 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
305 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 298 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
306 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 299 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
307 300
310 CV *cv; 303 CV *cv;
311 304
312 /* now do the ugly restore mess */ 305 /* now do the ugly restore mess */
313 while ((cv = (CV *)POPs)) 306 while ((cv = (CV *)POPs))
314 { 307 {
315 AV *padlist = (AV *)POPs;
316
317 if (padlist)
318 {
319 put_padlist (cv); /* mark this padlist as available */ 308 put_padlist (cv); /* mark this padlist as available */
320 CvPADLIST(cv) = padlist; 309 CvDEPTH (cv) = PTR2IV (POPs);
321 } 310 CvPADLIST (cv) = (AV *)POPs;
322
323 ++CvDEPTH(cv);
324 } 311 }
325 312
326 PUTBACK; 313 PUTBACK;
327 } 314 }
328} 315}
350 PERL_CONTEXT *cx = &ccstk[cxix--]; 337 PERL_CONTEXT *cx = &ccstk[cxix--];
351 338
352 if (CxTYPE(cx) == CXt_SUB) 339 if (CxTYPE(cx) == CXt_SUB)
353 { 340 {
354 CV *cv = cx->blk_sub.cv; 341 CV *cv = cx->blk_sub.cv;
342
355 if (CvDEPTH(cv)) 343 if (CvDEPTH (cv))
356 { 344 {
357 EXTEND (SP, CvDEPTH(cv)*2); 345 EXTEND (SP, 3);
358
359 while (--CvDEPTH(cv))
360 {
361 /* this tells the restore code to increment CvDEPTH */
362 PUSHs (Nullsv);
363 PUSHs ((SV *)cv);
364 }
365 346
366 PUSHs ((SV *)CvPADLIST(cv)); 347 PUSHs ((SV *)CvPADLIST(cv));
348 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
367 PUSHs ((SV *)cv); 349 PUSHs ((SV *)cv);
368 350
351 CvDEPTH (cv) = 0;
369 get_padlist (cv); 352 get_padlist (cv);
370 } 353 }
371 } 354 }
372#ifdef CXt_FORMAT 355#ifdef CXt_FORMAT
373 else if (CxTYPE(cx) == CXt_FORMAT) 356 else if (CxTYPE(cx) == CXt_FORMAT)
392 375
393 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 376 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
394 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 377 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
395 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 378 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
396 379
397 c->dowarn = PL_dowarn; 380#define VAR(name,type)c->name = PL_ ## name;
398 c->in_eval = PL_in_eval; 381# include "state.h"
399 382#undef VAR
400 c->curstackinfo = PL_curstackinfo;
401 c->curstack = PL_curstack;
402 c->mainstack = PL_mainstack;
403 c->stack_sp = PL_stack_sp;
404 c->op = PL_op;
405 c->curpad = PL_curpad;
406 c->comppad = PL_comppad;
407 c->compcv = PL_compcv;
408 c->stack_base = PL_stack_base;
409 c->stack_max = PL_stack_max;
410 c->tmps_stack = PL_tmps_stack;
411 c->tmps_floor = PL_tmps_floor;
412 c->tmps_ix = PL_tmps_ix;
413 c->tmps_max = PL_tmps_max;
414 c->markstack = PL_markstack;
415 c->markstack_ptr = PL_markstack_ptr;
416 c->markstack_max = PL_markstack_max;
417 c->scopestack = PL_scopestack;
418 c->scopestack_ix = PL_scopestack_ix;
419 c->scopestack_max = PL_scopestack_max;
420 c->savestack = PL_savestack;
421 c->savestack_ix = PL_savestack_ix;
422 c->savestack_max = PL_savestack_max;
423#if PERL_VERSION < 9
424 c->retstack = PL_retstack;
425 c->retstack_ix = PL_retstack_ix;
426 c->retstack_max = PL_retstack_max;
427#endif
428 c->curpm = PL_curpm;
429 c->curcop = PL_curcop;
430} 383}
431 384
432/* 385/*
433 * allocate various perl stacks. This is an exact copy 386 * allocate various perl stacks. This is an exact copy
434 * of perl.c:init_stacks, except that it uses less memory 387 * of perl.c:init_stacks, except that it uses less memory
466 419
467 New(54,PL_savestack,96,ANY); 420 New(54,PL_savestack,96,ANY);
468 PL_savestack_ix = 0; 421 PL_savestack_ix = 0;
469 PL_savestack_max = 96; 422 PL_savestack_max = 96;
470 423
471#if PERL_VERSION < 9 424#if !PERL_VERSION_ATLEAST (5,9,0)
472 New(54,PL_retstack,8,OP*); 425 New(54,PL_retstack,8,OP*);
473 PL_retstack_ix = 0; 426 PL_retstack_ix = 0;
474 PL_retstack_max = 8; 427 PL_retstack_max = 8;
475#endif 428#endif
476} 429}
477 430
478/* 431/*
479 * destroy the stacks, the callchain etc... 432 * destroy the stacks, the callchain etc...
480 */ 433 */
481static void 434static void
482destroy_stacks() 435coro_destroy_stacks ()
483{ 436{
484 if (!IN_DESTRUCT) 437 if (!IN_DESTRUCT)
485 { 438 {
486 /* is this ugly, I ask? */ 439 /* is this ugly, I ask? */
487 LEAVE_SCOPE (0); 440 LEAVE_SCOPE (0);
518 471
519 Safefree (PL_tmps_stack); 472 Safefree (PL_tmps_stack);
520 Safefree (PL_markstack); 473 Safefree (PL_markstack);
521 Safefree (PL_scopestack); 474 Safefree (PL_scopestack);
522 Safefree (PL_savestack); 475 Safefree (PL_savestack);
523#if PERL_VERSION < 9 476#if !PERL_VERSION_ATLEAST (5,9,0)
524 Safefree (PL_retstack); 477 Safefree (PL_retstack);
525#endif 478#endif
526} 479}
527 480
528static void 481static void
529setup_coro (struct coro *coro) 482setup_coro (struct coro *coro)
530{ 483{
531 /* 484 /*
532 * emulate part of the perl startup here. 485 * emulate part of the perl startup here.
533 */ 486 */
534 dTHX;
535 dSP;
536 UNOP myop;
537 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
538 487
539 coro_init_stacks (); 488 coro_init_stacks ();
489
540 /*PL_curcop = 0;*/ 490 PL_curcop = 0;
541 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 491 PL_in_eval = 0;
492 PL_curpm = 0;
493
494 {
495 dSP;
496 LOGOP myop;
497
498 /* I have no idea why this is needed, but it is */
499 PUSHMARK (SP);
500
542 SvREFCNT_dec (GvAV (PL_defgv)); 501 SvREFCNT_dec (GvAV (PL_defgv));
543 GvAV (PL_defgv) = coro->args; coro->args = 0; 502 GvAV (PL_defgv) = coro->args; coro->args = 0;
544 503
545 SPAGAIN;
546
547 Zero (&myop, 1, UNOP); 504 Zero (&myop, 1, LOGOP);
548 myop.op_next = Nullop; 505 myop.op_next = Nullop;
549 myop.op_flags = OPf_WANT_VOID; 506 myop.op_flags = OPf_WANT_VOID;
550 507
551 PL_op = (OP *)&myop; 508 PL_op = (OP *)&myop;
552 509
553 PUSHMARK(SP); 510 PUSHMARK (SP);
554 XPUSHs (sub_init); 511 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
555 PUTBACK; 512 PUTBACK;
556 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 513 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
557 SPAGAIN; 514 SPAGAIN;
558 515
559 ENTER; /* necessary e.g. for dounwind */ 516 ENTER; /* necessary e.g. for dounwind */
517 }
560} 518}
561 519
562static void 520static void
563free_coro_mortal () 521free_coro_mortal ()
564{ 522{
567 SvREFCNT_dec (coro_mortal); 525 SvREFCNT_dec (coro_mortal);
568 coro_mortal = 0; 526 coro_mortal = 0;
569 } 527 }
570} 528}
571 529
572static void 530static void NOINLINE
573prepare_cctx (coro_stack *cctx) 531prepare_cctx (coro_cctx *cctx)
574{ 532{
575 dSP; 533 dSP;
576 UNOP myop; 534 LOGOP myop;
577 535
578 Zero (&myop, 1, UNOP); 536 Zero (&myop, 1, LOGOP);
579 myop.op_next = PL_op; 537 myop.op_next = PL_op;
580 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 538 myop.op_flags = OPf_WANT_VOID;
581 539
540 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
541
582 PUSHMARK(SP); 542 PUSHMARK (SP);
583 EXTEND (SP, 2);
584 PUSHs (newSViv (PTR2IV (cctx)));
585 PUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); 543 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
586 PUTBACK; 544 PUTBACK;
587 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 545 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
588 SPAGAIN; 546 SPAGAIN;
589} 547}
590 548
591static void 549static void
592coro_run (void *arg) 550coro_run (void *arg)
593{ 551{
552 /* coro_run is the alternative epilogue of transfer() */
553 UNLOCK;
554
594 /* 555 /*
595 * this is a _very_ stripped down perl interpreter ;) 556 * this is a _very_ stripped down perl interpreter ;)
596 */ 557 */
597 UNLOCK;
598
599 PL_top_env = &PL_start_env; 558 PL_top_env = &PL_start_env;
559
560 /* inject call to cctx_init */
600 prepare_cctx ((coro_stack *)arg); 561 prepare_cctx ((coro_cctx *)arg);
601 562
602 /* somebody will hit me for both perl_run and PL_restartop */ 563 /* somebody will hit me for both perl_run and PL_restartop */
603 perl_run (PERL_GET_CONTEXT); 564 perl_run (PL_curinterp);
604 565
605 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); 566 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
606 abort (); 567 abort ();
607} 568}
608 569
609static coro_stack * 570static coro_cctx *
610stack_new () 571cctx_new ()
611{ 572{
612 coro_stack *stack; 573 coro_cctx *cctx;
613 574
575 ++cctx_count;
576
614 New (0, stack, 1, coro_stack); 577 New (0, cctx, 1, coro_cctx);
615 578
616#if HAVE_MMAP 579#if HAVE_MMAP
617 580
618 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 581 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
619 /* mmap suppsedly does allocate-on-write for us */ 582 /* mmap suppsedly does allocate-on-write for us */
620 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 583 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
621 584
622 if (stack->sptr == (void *)-1) 585 if (cctx->sptr == (void *)-1)
623 { 586 {
624 perror ("FATAL: unable to mmap stack for coroutine"); 587 perror ("FATAL: unable to mmap stack for coroutine");
625 _exit (EXIT_FAILURE); 588 _exit (EXIT_FAILURE);
626 } 589 }
627 590
628# if STACKGUARD 591# if STACKGUARD
629 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 592 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
630# endif 593# endif
631 594
632#else 595#else
633 596
634 stack->ssize = STACKSIZE * (long)sizeof (long); 597 cctx->ssize = STACKSIZE * (long)sizeof (long);
635 New (0, stack->sptr, STACKSIZE, long); 598 New (0, cctx->sptr, STACKSIZE, long);
636 599
637 if (!stack->sptr) 600 if (!cctx->sptr)
638 { 601 {
639 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 602 perror ("FATAL: unable to malloc stack for coroutine");
640 _exit (EXIT_FAILURE); 603 _exit (EXIT_FAILURE);
641 } 604 }
642 605
643#endif 606#endif
644 607
608#if USE_VALGRIND
609 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
610 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
611 cctx->ssize + (char *)cctx->sptr
612 );
613#endif
614
645 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); 615 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
646 616
647 return stack; 617 return cctx;
648} 618}
649 619
650static void 620static void
651stack_free (coro_stack *stack) 621cctx_destroy (coro_cctx *cctx)
652{ 622{
653 if (!stack) 623 if (!cctx)
654 return; 624 return;
655 625
626 --cctx_count;
627
628#if USE_VALGRIND
629 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
630#endif
631
656#if HAVE_MMAP 632#if HAVE_MMAP
657 munmap (stack->sptr, stack->ssize); 633 munmap (cctx->sptr, cctx->ssize);
658#else 634#else
659 Safefree (stack->sptr); 635 Safefree (cctx->sptr);
660#endif 636#endif
661 637
662 Safefree (stack); 638 Safefree (cctx);
663} 639}
664 640
665static coro_stack *stack_first;
666
667static coro_stack * 641static coro_cctx *
668stack_get () 642cctx_get ()
669{ 643{
670 coro_stack *stack; 644 coro_cctx *cctx;
671 645
672 if (stack_first) 646 if (cctx_first)
673 { 647 {
674 stack = stack_first; 648 cctx = cctx_first;
675 stack_first = stack->next; 649 cctx_first = cctx->next;
650 --cctx_idle;
676 } 651 }
677 else 652 else
678 { 653 {
679 stack = stack_new (); 654 cctx = cctx_new ();
680 PL_op = PL_op->op_next; 655 PL_op = PL_op->op_next;
681 } 656 }
682 657
683 return stack; 658 return cctx;
684} 659}
685 660
686static void 661static void
687stack_put (coro_stack *stack) 662cctx_put (coro_cctx *cctx)
688{ 663{
689 stack->next = stack_first; 664 /* free another cctx if overlimit */
690 stack_first = stack; 665 if (cctx_idle >= MAX_IDLE_CCTX)
666 {
667 coro_cctx *first = cctx_first;
668 cctx_first = first->next;
669 --cctx_idle;
670
671 assert (!first->inuse);
672 cctx_destroy (first);
673 }
674
675 ++cctx_idle;
676 cctx->next = cctx_first;
677 cctx_first = cctx;
691} 678}
692 679
693/* never call directly, always through the coro_state_transfer global variable */ 680/* never call directly, always through the coro_state_transfer global variable */
694static void 681static void NOINLINE
695transfer_impl (struct coro *prev, struct coro *next, int flags) 682transfer (struct coro *prev, struct coro *next, int flags)
696{ 683{
697 dSTACKLEVEL; 684 dSTACKLEVEL;
698 685
699 /* sometimes transfer is only called to set idle_sp */ 686 /* sometimes transfer is only called to set idle_sp */
700 if (flags == TRANSFER_SET_STACKLEVEL) 687 if (flags == TRANSFER_SET_STACKLEVEL)
701 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 688 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
702 else if (prev != next) 689 else if (prev != next)
703 { 690 {
704 coro_stack *prev__stack; 691 coro_cctx *prev__cctx;
692
693 if (!prev->cctx)
694 {
695 /* create a new empty context */
696 Newz (0, prev->cctx, 1, coro_cctx);
697 prev->cctx->inuse = 1;
698 prev->flags |= CF_RUNNING;
699 }
700
701 if (!prev->flags & CF_RUNNING)
702 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
703
704 if (next->flags & CF_RUNNING)
705 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
706
707 prev->flags &= ~CF_RUNNING;
708 next->flags |= CF_RUNNING;
705 709
706 LOCK; 710 LOCK;
707 711
708 if (next->mainstack) 712 if (next->mainstack)
709 { 713 {
716 /* need to start coroutine */ 720 /* need to start coroutine */
717 /* first get rid of the old state */ 721 /* first get rid of the old state */
718 SAVE (prev, -1); 722 SAVE (prev, -1);
719 /* setup coroutine call */ 723 /* setup coroutine call */
720 setup_coro (next); 724 setup_coro (next);
721 /* need a stack */ 725 /* need a new stack */
722 next->stack = 0; 726 assert (!next->stack);
723 } 727 }
724 728
725 if (!prev->stack)
726 /* create a new empty context */
727 Newz (0, prev->stack, 1, coro_stack);
728
729 prev__stack = prev->stack; 729 prev__cctx = prev->cctx;
730 730
731 /* possibly "free" the stack */ 731 /* possibly "free" the cctx */
732 if (prev__stack->idle_sp == STACKLEVEL) 732 if (prev__cctx->idle_sp == STACKLEVEL)
733 { 733 {
734 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
735 assert (PL_top_env == prev__cctx->top_env);
736
734 stack_put (prev__stack); 737 cctx_put (prev__cctx);
735 prev->stack = 0; 738 prev->cctx = 0;
736 } 739 }
737 740
738 if (!next->stack) 741 if (!next->cctx)
739 next->stack = stack_get (); 742 next->cctx = cctx_get ();
740 743
741 if (prev__stack != next->stack) 744 if (prev__cctx != next->cctx)
742 { 745 {
746 assert ( prev__cctx->inuse);
747 assert (!next->cctx->inuse);
748
749 prev__cctx->inuse = 0;
750 next->cctx->inuse = 1;
751
743 prev__stack->top_env = PL_top_env; 752 prev__cctx->top_env = PL_top_env;
744 PL_top_env = next->stack->top_env; 753 PL_top_env = next->cctx->top_env;
745 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 754 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
746 } 755 }
747 756
748 free_coro_mortal (); 757 free_coro_mortal ();
749 758
750 UNLOCK; 759 UNLOCK;
751 } 760 }
752} 761}
753
754/* use this function pointer to call the above function */
755/* this is done to increase chances of the compiler not inlining the call */
756/* not static to make it even harder for the compiler (and theoretically impossible in most cases */
757void (*coro_state_transfer)(struct coro *prev, struct coro *next, int flags) = transfer_impl;
758 762
759struct transfer_args 763struct transfer_args
760{ 764{
761 struct coro *prev, *next; 765 struct coro *prev, *next;
762 int flags; 766 int flags;
763}; 767};
764 768
765#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) 769#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
766 770
767static void 771static void
768coro_state_destroy (struct coro *coro) 772coro_state_destroy (struct coro *coro)
769{ 773{
770 if (coro->refcnt--) 774 if (coro->refcnt--)
771 return; 775 return;
776
777 if (coro->flags & CF_RUNNING)
778 croak ("FATAL: tried to destroy currently running coroutine");
772 779
773 if (coro->mainstack && coro->mainstack != main_mainstack) 780 if (coro->mainstack && coro->mainstack != main_mainstack)
774 { 781 {
775 struct coro temp; 782 struct coro temp;
776 783
777 SAVE ((&temp), TRANSFER_SAVE_ALL); 784 SAVE ((&temp), TRANSFER_SAVE_ALL);
778 LOAD (coro); 785 LOAD (coro);
779 786
780 destroy_stacks (); 787 coro_destroy_stacks ();
781 788
782 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 789 LOAD ((&temp)); /* this will get rid of defsv etc.. */
783 790
784 coro->mainstack = 0; 791 coro->mainstack = 0;
785 } 792 }
786 793
787 stack_free (coro->stack); 794 cctx_destroy (coro->cctx);
788 SvREFCNT_dec (coro->args); 795 SvREFCNT_dec (coro->args);
789 Safefree (coro); 796 Safefree (coro);
790} 797}
791 798
792static int 799static int
808 ++coro->refcnt; 815 ++coro->refcnt;
809 816
810 return 0; 817 return 0;
811} 818}
812 819
813static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 820static MGVTBL coro_state_vtbl = {
821 0, 0, 0, 0,
822 coro_state_clear,
823 0,
824#ifdef MGf_DUP
825 coro_state_dup,
826#else
827# define MGf_DUP 0
828#endif
829};
814 830
815static struct coro * 831static struct coro *
816SvSTATE (SV *coro) 832SvSTATE (SV *coro)
817{ 833{
818 HV *stash; 834 HV *stash;
865static GV *coro_current, *coro_idle; 881static GV *coro_current, *coro_idle;
866static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 882static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
867static int coro_nready; 883static int coro_nready;
868 884
869static void 885static void
870coro_enq (SV *sv) 886coro_enq (SV *coro_sv)
871{ 887{
872 int prio;
873
874 if (SvTYPE (sv) != SVt_PVHV)
875 croak ("Coro::ready tried to enqueue something that is not a coroutine");
876
877 prio = SvSTATE (sv)->prio;
878
879 av_push (coro_ready [prio - PRIO_MIN], sv); 888 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
880 coro_nready++; 889 coro_nready++;
881} 890}
882 891
883static SV * 892static SV *
884coro_deq (int min_prio) 893coro_deq (int min_prio)
897 } 906 }
898 907
899 return 0; 908 return 0;
900} 909}
901 910
902static void 911static int
903api_ready (SV *coro) 912api_ready (SV *coro_sv)
904{ 913{
905 dTHX; 914 struct coro *coro;
906 915
907 if (SvROK (coro)) 916 if (SvROK (coro_sv))
908 coro = SvRV (coro); 917 coro_sv = SvRV (coro_sv);
918
919 coro = SvSTATE (coro_sv);
920
921 if (coro->flags & CF_READY)
922 return 0;
923
924 if (coro->flags & CF_RUNNING)
925 croak ("Coro::ready called on currently running coroutine");
926
927 coro->flags |= CF_READY;
909 928
910 LOCK; 929 LOCK;
911 coro_enq (SvREFCNT_inc (coro)); 930 coro_enq (SvREFCNT_inc (coro_sv));
912 UNLOCK; 931 UNLOCK;
932
933 return 1;
934}
935
936static int
937api_is_ready (SV *coro_sv)
938{
939 return !!SvSTATE (coro_sv)->flags & CF_READY;
913} 940}
914 941
915static void 942static void
916prepare_schedule (struct transfer_args *ta) 943prepare_schedule (struct transfer_args *ta)
917{ 944{
953 coro_mortal = prev; 980 coro_mortal = prev;
954 981
955 ta->prev = SvSTATE (prev); 982 ta->prev = SvSTATE (prev);
956 ta->next = SvSTATE (next); 983 ta->next = SvSTATE (next);
957 ta->flags = TRANSFER_SAVE_ALL; 984 ta->flags = TRANSFER_SAVE_ALL;
985
986 ta->next->flags &= ~CF_READY;
958} 987}
959 988
960static void 989static void
961prepare_cede (struct transfer_args *ta) 990prepare_cede (struct transfer_args *ta)
962{ 991{
963 LOCK; 992 api_ready (GvSV (coro_current));
964 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
965 UNLOCK;
966 993
967 prepare_schedule (ta); 994 prepare_schedule (ta);
968} 995}
969 996
970static void 997static void
1027 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1054 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)); 1055 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1029 1056
1030 for (i = 1; i < items; i++) 1057 for (i = 1; i < items; i++)
1031 av_push (coro->args, newSVsv (ST (i))); 1058 av_push (coro->args, newSVsv (ST (i)));
1032
1033 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1034 /*coro->stack = 0;*/
1035} 1059}
1036 OUTPUT: 1060 OUTPUT:
1037 RETVAL 1061 RETVAL
1038 1062
1039void 1063void
1048 struct transfer_args ta; 1072 struct transfer_args ta;
1049 1073
1050 switch (ix) 1074 switch (ix)
1051 { 1075 {
1052 case 0: 1076 case 0:
1053 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1077 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1054 ta.next = 0; 1078 ta.next = 0;
1055 ta.flags = TRANSFER_SET_STACKLEVEL; 1079 ta.flags = TRANSFER_SET_STACKLEVEL;
1056 break; 1080 break;
1057 1081
1058 case 1: 1082 case 1:
1112 ++coro_src->refcnt; 1136 ++coro_src->refcnt;
1113 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1137 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1114} 1138}
1115 1139
1116void 1140void
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) 1141_exit (code)
1124 int code 1142 int code
1125 PROTOTYPE: $ 1143 PROTOTYPE: $
1126 CODE: 1144 CODE:
1127 _exit (code); 1145 _exit (code);
1146
1147int
1148cctx_count ()
1149 CODE:
1150 RETVAL = cctx_count;
1151 OUTPUT:
1152 RETVAL
1153
1154int
1155cctx_idle ()
1156 CODE:
1157 RETVAL = cctx_idle;
1158 OUTPUT:
1159 RETVAL
1128 1160
1129MODULE = Coro::State PACKAGE = Coro 1161MODULE = Coro::State PACKAGE = Coro
1130 1162
1131BOOT: 1163BOOT:
1132{ 1164{
1151 SV *sv = perl_get_sv("Coro::API", 1); 1183 SV *sv = perl_get_sv("Coro::API", 1);
1152 1184
1153 coroapi.schedule = api_schedule; 1185 coroapi.schedule = api_schedule;
1154 coroapi.cede = api_cede; 1186 coroapi.cede = api_cede;
1155 coroapi.ready = api_ready; 1187 coroapi.ready = api_ready;
1188 coroapi.is_ready = api_is_ready;
1156 coroapi.nready = &coro_nready; 1189 coroapi.nready = &coro_nready;
1157 coroapi.current = coro_current; 1190 coroapi.current = coro_current;
1158 1191
1159 GCoroAPI = &coroapi; 1192 GCoroAPI = &coroapi;
1160 sv_setiv (sv, (IV)&coroapi); 1193 sv_setiv (sv, (IV)&coroapi);
1180 1213
1181 coro->prio = newprio; 1214 coro->prio = newprio;
1182 } 1215 }
1183} 1216}
1184 1217
1185void 1218SV *
1186ready (SV *self) 1219ready (SV *self)
1187 PROTOTYPE: $ 1220 PROTOTYPE: $
1188 CODE: 1221 CODE:
1189 api_ready (self); 1222 RETVAL = boolSV (api_ready (self));
1223 OUTPUT:
1224 RETVAL
1225
1226SV *
1227is_ready (SV *self)
1228 PROTOTYPE: $
1229 CODE:
1230 RETVAL = boolSV (api_is_ready (self));
1231 OUTPUT:
1232 RETVAL
1190 1233
1191int 1234int
1192nready (...) 1235nready (...)
1193 PROTOTYPE: 1236 PROTOTYPE:
1194 CODE: 1237 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines