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.98 by root, Sun Nov 26 21:25:53 2006 UTC vs.
Revision 1.109 by root, Mon Nov 27 18:45:29 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#define PERL_VERSION_ATLEAST(a,b,c) \
14 (PERL_REVISION > (a) \
15 || (PERL_REVISION == (a) \
16 && (PERL_VERSION > (b) \
17 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
18
19#if !PERL_VERSION_ATLEAST (5,6,0)
10# ifndef PL_ppaddr 20# ifndef PL_ppaddr
11# define PL_ppaddr ppaddr 21# define PL_ppaddr ppaddr
12# endif 22# endif
13# ifndef call_sv 23# ifndef call_sv
14# define call_sv perl_call_sv 24# define call_sv perl_call_sv
52# ifndef PAGESIZE 62# ifndef PAGESIZE
53# define PAGESIZE pagesize 63# define PAGESIZE pagesize
54# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) 64# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
55static long pagesize; 65static long pagesize;
56# else 66# else
57# define BOOT_PAGESIZE 67# define BOOT_PAGESIZE (void)0
58# endif 68# endif
69#else
70# define PAGESIZE 0
71# define BOOT_PAGESIZE (void)0
59#endif 72#endif
60 73
61/* The next macro should declare a variable stacklevel that contains and approximation 74/* The next macro should declare a variable stacklevel that contains and approximation
62 * to the current C stack pointer. Its property is that it changes with each call 75 * to the current C stack pointer. Its property is that it changes with each call
63 * and should be unique. */ 76 * and should be unique. */
64#define dSTACKLEVEL int stacklevel 77#define dSTACKLEVEL int stacklevel
65#define STACKLEVEL ((void *)&stacklevel) 78#define STACKLEVEL ((void *)&stacklevel)
66 79
67#define IN_DESTRUCT (PL_main_cv == Nullcv) 80#define IN_DESTRUCT (PL_main_cv == Nullcv)
81
82#if __GNUC__ >= 3
83# define attribute(x) __attribute__(x)
84#else
85# define attribute(x)
86#endif
87
88#define NOINLINE attribute ((noinline))
68 89
69#include "CoroAPI.h" 90#include "CoroAPI.h"
70 91
71#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ 92#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
72 93
82static struct CoroAPI coroapi; 103static struct CoroAPI coroapi;
83static AV *main_mainstack; /* used to differentiate between $main and others */ 104static AV *main_mainstack; /* used to differentiate between $main and others */
84static HV *coro_state_stash, *coro_stash; 105static HV *coro_state_stash, *coro_stash;
85static SV *coro_mortal; /* will be freed after next transfer */ 106static SV *coro_mortal; /* will be freed after next transfer */
86 107
108static struct coro_cctx *cctx_first;
109static int cctx_count, cctx_idle;
110
87/* this is a structure representing a c-level coroutine */ 111/* this is a structure representing a c-level coroutine */
88typedef struct coro_stack { 112typedef struct coro_cctx {
89 struct coro_stack *next; 113 struct coro_cctx *next;
90 114
91 /* the stack */ 115 /* the stack */
92 void *sptr; 116 void *sptr;
93 long ssize; /* positive == mmap, otherwise malloc */ 117 long ssize; /* positive == mmap, otherwise malloc */
94 118
95 /* cpu state */ 119 /* cpu state */
96 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 120 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
97 JMPENV *top_env; 121 JMPENV *top_env;
98 coro_context cctx; 122 coro_context cctx;
99} coro_stack; 123
124#if USE_VALGRIND
125 int valgrind_id;
126#endif
127} coro_cctx;
100 128
101/* this is a structure representing a perl-level coroutine */ 129/* this is a structure representing a perl-level coroutine */
102struct coro { 130struct coro {
103 /* the c coroutine allocated to this perl coroutine, if any */ 131 /* the c coroutine allocated to this perl coroutine, if any */
104 coro_stack *stack; 132 coro_cctx *cctx;
105 133
106 /* data associated with this coroutine (initial args) */ 134 /* data associated with this coroutine (initial args) */
107 AV *args; 135 AV *args;
108 int refcnt; 136 int refcnt;
109 137
110 /* optionally saved, might be zero */ 138 /* optionally saved, might be zero */
111 AV *defav; 139 AV *defav;
112 SV *defsv; 140 SV *defsv;
113 SV *errsv; 141 SV *errsv;
114 142
115 /* saved global state not related to stacks */ 143#define VAR(name,type) type name;
116 U8 dowarn; 144# include "state.h"
117 I32 in_eval; 145#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 146
149 /* coro process data */ 147 /* coro process data */
150 int prio; 148 int prio;
151}; 149};
152 150
159 AV *padlist = CvPADLIST (cv); 157 AV *padlist = CvPADLIST (cv);
160 AV *newpadlist, *newpad; 158 AV *newpadlist, *newpad;
161 159
162 newpadlist = newAV (); 160 newpadlist = newAV ();
163 AvREAL_off (newpadlist); 161 AvREAL_off (newpadlist);
164#if PERL_VERSION < 9 162#if PERL_VERSION_ATLEAST (5,9,0)
163 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
164#else
165 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 165 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
166#else
167 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
168#endif 166#endif
169 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 167 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
170 --AvFILLp (padlist); 168 --AvFILLp (padlist);
171 169
172 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 170 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
216 214
217#define PERL_MAGIC_coro PERL_MAGIC_ext 215#define PERL_MAGIC_coro PERL_MAGIC_ext
218 216
219static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 217static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
220 218
219#define CORO_MAGIC(cv) \
220 SvMAGIC (cv) \
221 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
222 ? SvMAGIC (cv) \
223 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
224 : 0
225
221/* the next two functions merely cache the padlists */ 226/* the next two functions merely cache the padlists */
222static void 227static void
223get_padlist (CV *cv) 228get_padlist (CV *cv)
224{ 229{
225 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 230 MAGIC *mg = CORO_MAGIC (cv);
231 AV *av;
226 232
227 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 233 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
228 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 234 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
229 else 235 else
230 { 236 {
231#if 0 237#if 0
232 /* this is probably cleaner, but also slower? */ 238 /* this is probably cleaner, but also slower? */
233 CV *cp = Perl_cv_clone (cv); 239 CV *cp = Perl_cv_clone (cv);
241} 247}
242 248
243static void 249static void
244put_padlist (CV *cv) 250put_padlist (CV *cv)
245{ 251{
246 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 252 MAGIC *mg = CORO_MAGIC (cv);
253 AV *av;
247 254
248 if (!mg) 255 if (!mg)
249 { 256 {
250 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 257 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
251 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 258 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
252 mg->mg_virtual = &vtbl_coro; 259 mg->mg_virtual = &vtbl_coro;
253 mg->mg_obj = (SV *)newAV (); 260 mg->mg_obj = (SV *)newAV ();
254 } 261 }
255 262
256 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); 263 av = (AV *)mg->mg_obj;
264
265 if (AvFILLp (av) >= AvMAX (av))
266 av_extend (av, AvMAX (av) + 1);
267
268 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
257} 269}
258 270
259#define SB do { 271#define SB do {
260#define SE } while (0) 272#define SE } while (0)
261 273
265#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 277#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
266 278
267static void 279static void
268load_state(Coro__State c) 280load_state(Coro__State c)
269{ 281{
270 PL_dowarn = c->dowarn; 282#define VAR(name,type) PL_ ## name = c->name;
271 PL_in_eval = c->in_eval; 283# include "state.h"
272 284#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 285
304 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 286 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
305 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 287 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
306 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 288 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
307 289
310 CV *cv; 292 CV *cv;
311 293
312 /* now do the ugly restore mess */ 294 /* now do the ugly restore mess */
313 while ((cv = (CV *)POPs)) 295 while ((cv = (CV *)POPs))
314 { 296 {
315 AV *padlist = (AV *)POPs;
316
317 if (padlist)
318 {
319 put_padlist (cv); /* mark this padlist as available */ 297 put_padlist (cv); /* mark this padlist as available */
320 CvPADLIST(cv) = padlist; 298 CvDEPTH (cv) = PTR2IV (POPs);
321 } 299 CvPADLIST (cv) = (AV *)POPs;
322
323 ++CvDEPTH(cv);
324 } 300 }
325 301
326 PUTBACK; 302 PUTBACK;
327 } 303 }
328} 304}
350 PERL_CONTEXT *cx = &ccstk[cxix--]; 326 PERL_CONTEXT *cx = &ccstk[cxix--];
351 327
352 if (CxTYPE(cx) == CXt_SUB) 328 if (CxTYPE(cx) == CXt_SUB)
353 { 329 {
354 CV *cv = cx->blk_sub.cv; 330 CV *cv = cx->blk_sub.cv;
331
355 if (CvDEPTH(cv)) 332 if (CvDEPTH (cv))
356 { 333 {
357 EXTEND (SP, CvDEPTH(cv)*2); 334 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 335
366 PUSHs ((SV *)CvPADLIST(cv)); 336 PUSHs ((SV *)CvPADLIST(cv));
337 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
367 PUSHs ((SV *)cv); 338 PUSHs ((SV *)cv);
368 339
340 CvDEPTH (cv) = 0;
369 get_padlist (cv); 341 get_padlist (cv);
370 } 342 }
371 } 343 }
372#ifdef CXt_FORMAT 344#ifdef CXt_FORMAT
373 else if (CxTYPE(cx) == CXt_FORMAT) 345 else if (CxTYPE(cx) == CXt_FORMAT)
392 364
393 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 365 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
394 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 366 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
395 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 367 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
396 368
397 c->dowarn = PL_dowarn; 369#define VAR(name,type)c->name = PL_ ## name;
398 c->in_eval = PL_in_eval; 370# include "state.h"
399 371#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} 372}
431 373
432/* 374/*
433 * allocate various perl stacks. This is an exact copy 375 * allocate various perl stacks. This is an exact copy
434 * of perl.c:init_stacks, except that it uses less memory 376 * of perl.c:init_stacks, except that it uses less memory
436 * not usually need a lot of stackspace. 378 * not usually need a lot of stackspace.
437 */ 379 */
438static void 380static void
439coro_init_stacks () 381coro_init_stacks ()
440{ 382{
441 LOCK;
442
443 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 383 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
444 PL_curstackinfo->si_type = PERLSI_MAIN; 384 PL_curstackinfo->si_type = PERLSI_MAIN;
445 PL_curstack = PL_curstackinfo->si_stack; 385 PL_curstack = PL_curstackinfo->si_stack;
446 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 386 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
447 387
468 408
469 New(54,PL_savestack,96,ANY); 409 New(54,PL_savestack,96,ANY);
470 PL_savestack_ix = 0; 410 PL_savestack_ix = 0;
471 PL_savestack_max = 96; 411 PL_savestack_max = 96;
472 412
473#if PERL_VERSION < 9 413#if !PERL_VERSION_ATLEAST (5,9,0)
474 New(54,PL_retstack,8,OP*); 414 New(54,PL_retstack,8,OP*);
475 PL_retstack_ix = 0; 415 PL_retstack_ix = 0;
476 PL_retstack_max = 8; 416 PL_retstack_max = 8;
477#endif 417#endif
478
479 UNLOCK;
480} 418}
481 419
482/* 420/*
483 * destroy the stacks, the callchain etc... 421 * destroy the stacks, the callchain etc...
484 */ 422 */
485static void 423static void
486destroy_stacks() 424coro_destroy_stacks ()
487{ 425{
488 if (!IN_DESTRUCT) 426 if (!IN_DESTRUCT)
489 { 427 {
490 /* is this ugly, I ask? */ 428 /* is this ugly, I ask? */
491 LEAVE_SCOPE (0); 429 LEAVE_SCOPE (0);
522 460
523 Safefree (PL_tmps_stack); 461 Safefree (PL_tmps_stack);
524 Safefree (PL_markstack); 462 Safefree (PL_markstack);
525 Safefree (PL_scopestack); 463 Safefree (PL_scopestack);
526 Safefree (PL_savestack); 464 Safefree (PL_savestack);
527#if PERL_VERSION < 9 465#if !PERL_VERSION_ATLEAST (5,9,0)
528 Safefree (PL_retstack); 466 Safefree (PL_retstack);
529#endif 467#endif
530} 468}
531 469
532static void 470static void
533setup_coro (struct coro *coro) 471setup_coro (struct coro *coro)
534{ 472{
535 /* 473 /*
536 * emulate part of the perl startup here. 474 * emulate part of the perl startup here.
537 */ 475 */
538 dTHX;
539 dSP;
540 UNOP myop;
541 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
542 476
543 coro_init_stacks (); 477 coro_init_stacks ();
478
544 /*PL_curcop = 0;*/ 479 PL_curcop = 0;
545 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 480 PL_in_eval = 0;
481 PL_curpm = 0;
482
483 {
484 dSP;
485 LOGOP myop;
486
487 /* I have no idea why this is needed, but it is */
488 PUSHMARK (SP);
489
546 SvREFCNT_dec (GvAV (PL_defgv)); 490 SvREFCNT_dec (GvAV (PL_defgv));
547 GvAV (PL_defgv) = coro->args; coro->args = 0; 491 GvAV (PL_defgv) = coro->args; coro->args = 0;
548 492
549 SPAGAIN;
550
551 Zero (&myop, 1, UNOP); 493 Zero (&myop, 1, LOGOP);
552 myop.op_next = Nullop; 494 myop.op_next = Nullop;
553 myop.op_flags = OPf_WANT_VOID; 495 myop.op_flags = OPf_WANT_VOID;
554 496
555 PL_op = (OP *)&myop; 497 PL_op = (OP *)&myop;
556 498
557 PUSHMARK(SP); 499 PUSHMARK (SP);
558 XPUSHs (sub_init); 500 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
559 PUTBACK; 501 PUTBACK;
560 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 502 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
561 SPAGAIN; 503 SPAGAIN;
562 504
563 ENTER; /* necessary e.g. for dounwind */ 505 ENTER; /* necessary e.g. for dounwind */
506 }
564} 507}
565 508
566static void 509static void
567free_coro_mortal () 510free_coro_mortal ()
568{ 511{
571 SvREFCNT_dec (coro_mortal); 514 SvREFCNT_dec (coro_mortal);
572 coro_mortal = 0; 515 coro_mortal = 0;
573 } 516 }
574} 517}
575 518
519static void NOINLINE
520prepare_cctx (coro_cctx *cctx)
521{
522 dSP;
523 LOGOP myop;
524
525 Zero (&myop, 1, LOGOP);
526 myop.op_next = PL_op;
527 myop.op_flags = OPf_WANT_VOID;
528
529 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
530
531 PUSHMARK (SP);
532 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
533 PUTBACK;
534 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
535 SPAGAIN;
536}
537
576static void 538static void
577coro_run (void *arg) 539coro_run (void *arg)
578{ 540{
541 /* coro_run is the alternative epilogue of transfer() */
542 UNLOCK;
543
579 /* 544 /*
580 * this is a _very_ stripped down perl interpreter ;) 545 * this is a _very_ stripped down perl interpreter ;)
581 */ 546 */
582 dTHX;
583 int ret;
584
585 UNLOCK;
586
587 PL_top_env = &PL_start_env; 547 PL_top_env = &PL_start_env;
588 548
589 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); 549 /* inject call to cctx_init */
590 sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op)); 550 prepare_cctx ((coro_cctx *)arg);
591
592 /* continue at cctx_init, without entersub */
593 PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE));
594 551
595 /* somebody will hit me for both perl_run and PL_restartop */ 552 /* somebody will hit me for both perl_run and PL_restartop */
596 ret = perl_run (PERL_GET_CONTEXT); 553 perl_run (PL_curinterp);
597 printf ("ret %d\n", ret);//D
598 554
599 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); 555 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
600 abort (); 556 abort ();
601} 557}
602 558
603static coro_stack * 559static coro_cctx *
604stack_new () 560cctx_new ()
605{ 561{
606 coro_stack *stack; 562 coro_cctx *cctx;
607 563
564 ++cctx_count;
565
608 New (0, stack, 1, coro_stack); 566 New (0, cctx, 1, coro_cctx);
609 567
610#if HAVE_MMAP 568#if HAVE_MMAP
611 569
612 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 570 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
613 /* mmap suppsedly does allocate-on-write for us */ 571 /* mmap suppsedly does allocate-on-write for us */
614 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 572 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
615 573
616 if (stack->sptr == (void *)-1) 574 if (cctx->sptr == (void *)-1)
617 { 575 {
618 perror ("FATAL: unable to mmap stack for coroutine"); 576 perror ("FATAL: unable to mmap stack for coroutine");
619 _exit (EXIT_FAILURE); 577 _exit (EXIT_FAILURE);
620 } 578 }
621 579
622# if STACKGUARD 580# if STACKGUARD
623 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 581 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
624# endif 582# endif
625 583
626#else 584#else
627 585
628 stack->ssize = STACKSIZE * (long)sizeof (long); 586 cctx->ssize = STACKSIZE * (long)sizeof (long);
629 New (0, stack->sptr, STACKSIZE, long); 587 New (0, cctx->sptr, STACKSIZE, long);
630 588
631 if (!stack->sptr) 589 if (!cctx->sptr)
632 { 590 {
633 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 591 perror ("FATAL: unable to malloc stack for coroutine");
634 _exit (EXIT_FAILURE); 592 _exit (EXIT_FAILURE);
635 } 593 }
636 594
637#endif 595#endif
638 596
597#if USE_VALGRIND
598 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
599 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
600 cctx->ssize + (char *)cctx->sptr
601 );
602#endif
603
639 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); 604 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
640 605
641 return stack; 606 return cctx;
642} 607}
643 608
644static void 609static void
645stack_free (coro_stack *stack) 610cctx_free (coro_cctx *cctx)
646{ 611{
647 if (!stack) 612 if (!cctx)
648 return; 613 return;
649 614
615 --cctx_count;
616
617#if USE_VALGRIND
618 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
619#endif
620
650#if HAVE_MMAP 621#if HAVE_MMAP
651 munmap (stack->sptr, stack->ssize); 622 munmap (cctx->sptr, cctx->ssize);
652#else 623#else
653 Safefree (stack->sptr); 624 Safefree (cctx->sptr);
654#endif 625#endif
655 626
656 Safefree (stack); 627 Safefree (cctx);
657} 628}
658 629
659static coro_stack *stack_first;
660
661static coro_stack * 630static coro_cctx *
662stack_get () 631cctx_get ()
663{ 632{
664 coro_stack *stack; 633 coro_cctx *cctx;
665 634
666 if (stack_first) 635 if (cctx_first)
667 { 636 {
668 stack = stack_first; 637 --cctx_idle;
638 cctx = cctx_first;
669 stack_first = stack->next; 639 cctx_first = cctx->next;
670 } 640 }
671 else 641 else
672 { 642 {
673 stack = stack_new (); 643 cctx = cctx_new ();
674 PL_op = PL_op->op_next; 644 PL_op = PL_op->op_next;
675 } 645 }
676 646
677 return stack; 647 return cctx;
678} 648}
679 649
680static void 650static void
681stack_put (coro_stack *stack) 651cctx_put (coro_cctx *cctx)
682{ 652{
683 stack->next = stack_first; 653 ++cctx_idle;
684 stack_first = stack; 654 cctx->next = cctx_first;
655 cctx_first = cctx;
685} 656}
686 657
687/* never call directly, always through the coro_state_transfer global variable */ 658/* never call directly, always through the coro_state_transfer global variable */
688static void 659static void NOINLINE
689transfer_impl (struct coro *prev, struct coro *next, int flags) 660transfer (struct coro *prev, struct coro *next, int flags)
690{ 661{
691 dSTACKLEVEL; 662 dSTACKLEVEL;
692 663
693 /* sometimes transfer is only called to set idle_sp */ 664 /* sometimes transfer is only called to set idle_sp */
694 if (flags == TRANSFER_SET_STACKLEVEL) 665 if (flags == TRANSFER_SET_STACKLEVEL)
695 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 666 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
696 else if (prev != next) 667 else if (prev != next)
697 { 668 {
698 coro_stack *prev__stack; 669 coro_cctx *prev__cctx;
699 670
700 LOCK; 671 LOCK;
701 672
702 if (next->mainstack) 673 if (next->mainstack)
703 { 674 {
711 /* first get rid of the old state */ 682 /* first get rid of the old state */
712 SAVE (prev, -1); 683 SAVE (prev, -1);
713 /* setup coroutine call */ 684 /* setup coroutine call */
714 setup_coro (next); 685 setup_coro (next);
715 /* need a stack */ 686 /* need a stack */
716 next->stack = 0; 687 next->cctx = 0;
717 } 688 }
718 689
719 if (!prev->stack) 690 if (!prev->cctx)
720 /* create a new empty context */ 691 /* create a new empty context */
721 Newz (0, prev->stack, 1, coro_stack); 692 Newz (0, prev->cctx, 1, coro_cctx);
722 693
723 prev__stack = prev->stack; 694 prev__cctx = prev->cctx;
724 695
725 /* possibly "free" the stack */ 696 /* possibly "free" the cctx */
726 if (prev__stack->idle_sp == STACKLEVEL) 697 if (prev__cctx->idle_sp == STACKLEVEL)
727 { 698 {
728 stack_put (prev__stack); 699 cctx_put (prev__cctx);
729 prev->stack = 0; 700 prev->cctx = 0;
730 } 701 }
731 702
732 if (!next->stack) 703 if (!next->cctx)
733 next->stack = stack_get (); 704 next->cctx = cctx_get ();
734 705
735 if (prev__stack != next->stack) 706 if (prev__cctx != next->cctx)
736 { 707 {
737 prev__stack->top_env = PL_top_env; 708 prev__cctx->top_env = PL_top_env;
738 PL_top_env = next->stack->top_env; 709 PL_top_env = next->cctx->top_env;
739 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 710 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
740 } 711 }
741 712
742 free_coro_mortal (); 713 free_coro_mortal ();
743 714
744 UNLOCK; 715 UNLOCK;
745 } 716 }
746} 717}
747
748/* use this function pointer to call the above function */
749/* this is done to increase chances of the compiler not inlining the call */
750/* not static to make it even harder for the compiler (and theoretically impossible in most cases */
751void (*coro_state_transfer)(struct coro *prev, struct coro *next, int flags) = transfer_impl;
752 718
753struct transfer_args 719struct transfer_args
754{ 720{
755 struct coro *prev, *next; 721 struct coro *prev, *next;
756 int flags; 722 int flags;
757}; 723};
758 724
759#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) 725#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
760 726
761static void 727static void
762coro_state_destroy (struct coro *coro) 728coro_state_destroy (struct coro *coro)
763{ 729{
764 if (coro->refcnt--) 730 if (coro->refcnt--)
769 struct coro temp; 735 struct coro temp;
770 736
771 SAVE ((&temp), TRANSFER_SAVE_ALL); 737 SAVE ((&temp), TRANSFER_SAVE_ALL);
772 LOAD (coro); 738 LOAD (coro);
773 739
774 destroy_stacks (); 740 coro_destroy_stacks ();
775 741
776 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 742 LOAD ((&temp)); /* this will get rid of defsv etc.. */
777 743
778 coro->mainstack = 0; 744 coro->mainstack = 0;
779 } 745 }
780 746
781 stack_free (coro->stack); 747 cctx_free (coro->cctx);
782 SvREFCNT_dec (coro->args); 748 SvREFCNT_dec (coro->args);
783 Safefree (coro); 749 Safefree (coro);
784} 750}
785 751
786static int 752static int
802 ++coro->refcnt; 768 ++coro->refcnt;
803 769
804 return 0; 770 return 0;
805} 771}
806 772
807static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 773static MGVTBL coro_state_vtbl = {
774 0, 0, 0, 0,
775 coro_state_clear,
776 0,
777#ifdef MGf_DUP
778 coro_state_dup,
779#else
780# define MGf_DUP 0
781#endif
782};
808 783
809static struct coro * 784static struct coro *
810SvSTATE (SV *coro) 785SvSTATE (SV *coro)
811{ 786{
812 HV *stash; 787 HV *stash;
909static void 884static void
910prepare_schedule (struct transfer_args *ta) 885prepare_schedule (struct transfer_args *ta)
911{ 886{
912 SV *current, *prev, *next; 887 SV *current, *prev, *next;
913 888
914 LOCK;
915
916 current = GvSV (coro_current); 889 current = GvSV (coro_current);
917 890
918 for (;;) 891 for (;;)
919 { 892 {
920 LOCK; 893 LOCK;
921
922 next = coro_deq (PRIO_MIN); 894 next = coro_deq (PRIO_MIN);
895 UNLOCK;
923 896
924 if (next) 897 if (next)
925 break; 898 break;
926
927 UNLOCK;
928 899
929 { 900 {
930 dSP; 901 dSP;
931 902
932 ENTER; 903 ENTER;
943 914
944 prev = SvRV (current); 915 prev = SvRV (current);
945 SvRV (current) = next; 916 SvRV (current) = next;
946 917
947 /* free this only after the transfer */ 918 /* free this only after the transfer */
919 LOCK;
948 free_coro_mortal (); 920 free_coro_mortal ();
921 UNLOCK;
949 coro_mortal = prev; 922 coro_mortal = prev;
950 923
951 ta->prev = SvSTATE (prev); 924 ta->prev = SvSTATE (prev);
952 ta->next = SvSTATE (next); 925 ta->next = SvSTATE (next);
953 ta->flags = TRANSFER_SAVE_ALL; 926 ta->flags = TRANSFER_SAVE_ALL;
954
955 UNLOCK;
956} 927}
957 928
958static void 929static void
959prepare_cede (struct transfer_args *ta) 930prepare_cede (struct transfer_args *ta)
960{ 931{
1025 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 996 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1026 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 997 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1027 998
1028 for (i = 1; i < items; i++) 999 for (i = 1; i < items; i++)
1029 av_push (coro->args, newSVsv (ST (i))); 1000 av_push (coro->args, newSVsv (ST (i)));
1030
1031 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1032 /*coro->stack = 0;*/
1033} 1001}
1034 OUTPUT: 1002 OUTPUT:
1035 RETVAL 1003 RETVAL
1036 1004
1037void 1005void
1046 struct transfer_args ta; 1014 struct transfer_args ta;
1047 1015
1048 switch (ix) 1016 switch (ix)
1049 { 1017 {
1050 case 0: 1018 case 0:
1051 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1019 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1052 ta.next = 0; 1020 ta.next = 0;
1053 ta.flags = TRANSFER_SET_STACKLEVEL; 1021 ta.flags = TRANSFER_SET_STACKLEVEL;
1054 break; 1022 break;
1055 1023
1056 case 1: 1024 case 1:
1110 ++coro_src->refcnt; 1078 ++coro_src->refcnt;
1111 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1079 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1112} 1080}
1113 1081
1114void 1082void
1115_nonlocal_goto (IV nextop)
1116 CODE:
1117 /* uuh, somebody will kill me again for this */
1118 PL_op->op_next = INT2PTR (OP *, nextop);
1119
1120void
1121_exit (code) 1083_exit (code)
1122 int code 1084 int code
1123 PROTOTYPE: $ 1085 PROTOTYPE: $
1124 CODE: 1086 CODE:
1125 _exit (code); 1087 _exit (code);
1088
1089int
1090cctx_count ()
1091 CODE:
1092 RETVAL = cctx_count;
1093 OUTPUT:
1094 RETVAL
1095
1096int
1097cctx_idle ()
1098 CODE:
1099 RETVAL = cctx_idle;
1100 OUTPUT:
1101 RETVAL
1126 1102
1127MODULE = Coro::State PACKAGE = Coro 1103MODULE = Coro::State PACKAGE = Coro
1128 1104
1129BOOT: 1105BOOT:
1130{ 1106{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines