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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines