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.94 by root, Sun Nov 26 17:14:15 2006 UTC vs.
Revision 1.108 by root, Mon Nov 27 18:15:47 2006 UTC

1#define PERL_NO_GET_CONTEXT
2
3#include "libcoro/coro.c" 1#include "libcoro/coro.c"
4 2
5#include "EXTERN.h" 3#include "EXTERN.h"
6#include "perl.h" 4#include "perl.h"
7#include "XSUB.h" 5#include "XSUB.h"
8 6
9#include "patchlevel.h" 7#include "patchlevel.h"
10 8
11#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)
12# ifndef PL_ppaddr 20# ifndef PL_ppaddr
13# define PL_ppaddr ppaddr 21# define PL_ppaddr ppaddr
14# endif 22# endif
15# ifndef call_sv 23# ifndef call_sv
16# define call_sv perl_call_sv 24# define call_sv perl_call_sv
54# ifndef PAGESIZE 62# ifndef PAGESIZE
55# define PAGESIZE pagesize 63# define PAGESIZE pagesize
56# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) 64# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
57static long pagesize; 65static long pagesize;
58# else 66# else
59# define BOOT_PAGESIZE 67# define BOOT_PAGESIZE (void)0
60# endif 68# endif
69#else
70# define PAGESIZE 0
71# define BOOT_PAGESIZE (void)0
61#endif 72#endif
62 73
63/* 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
64 * 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
65 * and should be unique. */ 76 * and should be unique. */
66#define dSTACKLEVEL int stacklevel 77#define dSTACKLEVEL int stacklevel
67#define STACKLEVEL ((void *)&stacklevel) 78#define STACKLEVEL ((void *)&stacklevel)
68 79
69#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))
70 89
71#include "CoroAPI.h" 90#include "CoroAPI.h"
72 91
73#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ 92#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
74 93
84static struct CoroAPI coroapi; 103static struct CoroAPI coroapi;
85static AV *main_mainstack; /* used to differentiate between $main and others */ 104static AV *main_mainstack; /* used to differentiate between $main and others */
86static HV *coro_state_stash, *coro_stash; 105static HV *coro_state_stash, *coro_stash;
87static SV *coro_mortal; /* will be freed after next transfer */ 106static SV *coro_mortal; /* will be freed after next transfer */
88 107
108static struct coro_cctx *cctx_first;
109static int cctx_count, cctx_idle;
110
89/* this is a structure representing a c-level coroutine */ 111/* this is a structure representing a c-level coroutine */
90typedef struct coro_stack { 112typedef struct coro_cctx {
91 struct coro_stack *next; 113 struct coro_cctx *next;
92 114
93 /* the stack */ 115 /* the stack */
94 void *sptr; 116 void *sptr;
95 long ssize; /* positive == mmap, otherwise malloc */ 117 long ssize; /* positive == mmap, otherwise malloc */
96 118
97 /* cpu state */ 119 /* cpu state */
98 void *idle_sp; /* original stacklevel when coroutine was created */ 120 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
99 JMPENV *top_env; 121 JMPENV *top_env;
100 coro_context cctx; 122 coro_context cctx;
101} coro_stack;
102 123
103/* the (fake) coro_stack representing the main program */ 124#if USE_VALGRIND
104static coro_stack *main_stack; 125 int valgrind_id;
126#endif
127} coro_cctx;
105 128
106/* this is a structure representing a perl-level coroutine */ 129/* this is a structure representing a perl-level coroutine */
107struct coro { 130struct coro {
108 /* the c coroutine allocated to this perl coroutine, if any */ 131 /* the c coroutine allocated to this perl coroutine, if any */
109 coro_stack *stack; 132 coro_cctx *cctx;
110 133
111 /* data associated with this coroutine (initial args) */ 134 /* data associated with this coroutine (initial args) */
112 AV *args; 135 AV *args;
113 int refcnt; 136 int refcnt;
114 137
115 /* optionally saved, might be zero */ 138 /* optionally saved, might be zero */
116 AV *defav; 139 AV *defav;
117 SV *defsv; 140 SV *defsv;
118 SV *errsv; 141 SV *errsv;
119 142
120 /* saved global state not related to stacks */ 143#define VAR(name,type) type name;
121 U8 dowarn; 144# include "state.h"
122 I32 in_eval; 145#undef VAR
123
124 /* the stacks and related info (callchain etc..) */
125 PERL_SI *curstackinfo;
126 AV *curstack;
127 AV *mainstack;
128 SV **stack_sp;
129 OP *op;
130 SV **curpad;
131 AV *comppad;
132 CV *compcv;
133 SV **stack_base;
134 SV **stack_max;
135 SV **tmps_stack;
136 I32 tmps_floor;
137 I32 tmps_ix;
138 I32 tmps_max;
139 I32 *markstack;
140 I32 *markstack_ptr;
141 I32 *markstack_max;
142 I32 *scopestack;
143 I32 scopestack_ix;
144 I32 scopestack_max;
145 ANY *savestack;
146 I32 savestack_ix;
147 I32 savestack_max;
148 OP **retstack;
149 I32 retstack_ix;
150 I32 retstack_max;
151 PMOP *curpm;
152 COP *curcop;
153 146
154 /* coro process data */ 147 /* coro process data */
155 int prio; 148 int prio;
156}; 149};
157 150
158typedef struct coro *Coro__State; 151typedef struct coro *Coro__State;
159typedef struct coro *Coro__State_or_hashref; 152typedef struct coro *Coro__State_or_hashref;
160 153
161static AV * 154static AV *
162coro_clone_padlist (pTHX_ CV *cv) 155coro_clone_padlist (CV *cv)
163{ 156{
164 AV *padlist = CvPADLIST (cv); 157 AV *padlist = CvPADLIST (cv);
165 AV *newpadlist, *newpad; 158 AV *newpadlist, *newpad;
166 159
167 newpadlist = newAV (); 160 newpadlist = newAV ();
168 AvREAL_off (newpadlist); 161 AvREAL_off (newpadlist);
169#if PERL_VERSION < 9 162#if PERL_VERSION_ATLEAST (5,9,0)
163 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
164#else
170 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 165 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
171#else
172 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
173#endif 166#endif
174 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 167 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
175 --AvFILLp (padlist); 168 --AvFILLp (padlist);
176 169
177 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 170 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
179 172
180 return newpadlist; 173 return newpadlist;
181} 174}
182 175
183static void 176static void
184free_padlist (pTHX_ AV *padlist) 177free_padlist (AV *padlist)
185{ 178{
186 /* may be during global destruction */ 179 /* may be during global destruction */
187 if (SvREFCNT (padlist)) 180 if (SvREFCNT (padlist))
188 { 181 {
189 I32 i = AvFILLp (padlist); 182 I32 i = AvFILLp (padlist);
210 AV *padlist; 203 AV *padlist;
211 AV *av = (AV *)mg->mg_obj; 204 AV *av = (AV *)mg->mg_obj;
212 205
213 /* casting is fun. */ 206 /* casting is fun. */
214 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 207 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
215 free_padlist (aTHX_ padlist); 208 free_padlist (padlist);
216 209
217 SvREFCNT_dec (av); 210 SvREFCNT_dec (av);
218 211
219 return 0; 212 return 0;
220} 213}
223 216
224static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 217static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
225 218
226/* the next two functions merely cache the padlists */ 219/* the next two functions merely cache the padlists */
227static void 220static void
228get_padlist (pTHX_ CV *cv) 221get_padlist (CV *cv)
229{ 222{
230 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 223 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
231 224
232 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 225 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
233 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 226 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
234 else 227 else
235 { 228 {
236#if 0 229#if 0
237 /* this should work - but it doesn't :( */ 230 /* this is probably cleaner, but also slower? */
238 CV *cp = Perl_cv_clone (aTHX_ cv); 231 CV *cp = Perl_cv_clone (cv);
239 CvPADLIST (cv) = CvPADLIST (cp); 232 CvPADLIST (cv) = CvPADLIST (cp);
240 CvPADLIST (cp) = 0; 233 CvPADLIST (cp) = 0;
241 SvREFCNT_dec (cp); 234 SvREFCNT_dec (cp);
242#else 235#else
243 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 236 CvPADLIST (cv) = coro_clone_padlist (cv);
244#endif 237#endif
245 } 238 }
246} 239}
247 240
248static void 241static void
249put_padlist (pTHX_ CV *cv) 242put_padlist (CV *cv)
250{ 243{
251 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 244 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
252 245
253 if (!mg) 246 if (!mg)
254 { 247 {
262} 255}
263 256
264#define SB do { 257#define SB do {
265#define SE } while (0) 258#define SE } while (0)
266 259
267#define LOAD(state) load_state(aTHX_ (state)); 260#define LOAD(state) load_state((state));
268#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); 261#define SAVE(state,flags) save_state((state),(flags));
269 262
270#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 263#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
271 264
272static void 265static void
273load_state(pTHX_ Coro__State c) 266load_state(Coro__State c)
274{ 267{
275 PL_dowarn = c->dowarn; 268#define VAR(name,type) PL_ ## name = c->name;
276 PL_in_eval = c->in_eval; 269# include "state.h"
277 270#undef VAR
278 PL_curstackinfo = c->curstackinfo;
279 PL_curstack = c->curstack;
280 PL_mainstack = c->mainstack;
281 PL_stack_sp = c->stack_sp;
282 PL_op = c->op;
283 PL_curpad = c->curpad;
284 PL_comppad = c->comppad;
285 PL_compcv = c->compcv;
286 PL_stack_base = c->stack_base;
287 PL_stack_max = c->stack_max;
288 PL_tmps_stack = c->tmps_stack;
289 PL_tmps_floor = c->tmps_floor;
290 PL_tmps_ix = c->tmps_ix;
291 PL_tmps_max = c->tmps_max;
292 PL_markstack = c->markstack;
293 PL_markstack_ptr = c->markstack_ptr;
294 PL_markstack_max = c->markstack_max;
295 PL_scopestack = c->scopestack;
296 PL_scopestack_ix = c->scopestack_ix;
297 PL_scopestack_max = c->scopestack_max;
298 PL_savestack = c->savestack;
299 PL_savestack_ix = c->savestack_ix;
300 PL_savestack_max = c->savestack_max;
301#if PERL_VERSION < 9
302 PL_retstack = c->retstack;
303 PL_retstack_ix = c->retstack_ix;
304 PL_retstack_max = c->retstack_max;
305#endif
306 PL_curpm = c->curpm;
307 PL_curcop = c->curcop;
308 271
309 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 272 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
310 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 273 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
311 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 274 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
312 275
319 { 282 {
320 AV *padlist = (AV *)POPs; 283 AV *padlist = (AV *)POPs;
321 284
322 if (padlist) 285 if (padlist)
323 { 286 {
324 put_padlist (aTHX_ cv); /* mark this padlist as available */ 287 put_padlist (cv); /* mark this padlist as available */
325 CvPADLIST(cv) = padlist; 288 CvPADLIST(cv) = padlist;
326 } 289 }
327 290
328 ++CvDEPTH(cv); 291 ++CvDEPTH(cv);
329 } 292 }
331 PUTBACK; 294 PUTBACK;
332 } 295 }
333} 296}
334 297
335static void 298static void
336save_state(pTHX_ Coro__State c, int flags) 299save_state(Coro__State c, int flags)
337{ 300{
338 { 301 {
339 dSP; 302 dSP;
340 I32 cxix = cxstack_ix; 303 I32 cxix = cxstack_ix;
341 PERL_CONTEXT *ccstk = cxstack; 304 PERL_CONTEXT *ccstk = cxstack;
369 } 332 }
370 333
371 PUSHs ((SV *)CvPADLIST(cv)); 334 PUSHs ((SV *)CvPADLIST(cv));
372 PUSHs ((SV *)cv); 335 PUSHs ((SV *)cv);
373 336
374 get_padlist (aTHX_ cv); 337 get_padlist (cv);
375 } 338 }
376 } 339 }
377#ifdef CXt_FORMAT 340#ifdef CXt_FORMAT
378 else if (CxTYPE(cx) == CXt_FORMAT) 341 else if (CxTYPE(cx) == CXt_FORMAT)
379 { 342 {
397 360
398 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 361 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
399 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 362 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
400 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 363 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
401 364
402 c->dowarn = PL_dowarn; 365#define VAR(name,type)c->name = PL_ ## name;
403 c->in_eval = PL_in_eval; 366# include "state.h"
404 367#undef VAR
405 c->curstackinfo = PL_curstackinfo;
406 c->curstack = PL_curstack;
407 c->mainstack = PL_mainstack;
408 c->stack_sp = PL_stack_sp;
409 c->op = PL_op;
410 c->curpad = PL_curpad;
411 c->comppad = PL_comppad;
412 c->compcv = PL_compcv;
413 c->stack_base = PL_stack_base;
414 c->stack_max = PL_stack_max;
415 c->tmps_stack = PL_tmps_stack;
416 c->tmps_floor = PL_tmps_floor;
417 c->tmps_ix = PL_tmps_ix;
418 c->tmps_max = PL_tmps_max;
419 c->markstack = PL_markstack;
420 c->markstack_ptr = PL_markstack_ptr;
421 c->markstack_max = PL_markstack_max;
422 c->scopestack = PL_scopestack;
423 c->scopestack_ix = PL_scopestack_ix;
424 c->scopestack_max = PL_scopestack_max;
425 c->savestack = PL_savestack;
426 c->savestack_ix = PL_savestack_ix;
427 c->savestack_max = PL_savestack_max;
428#if PERL_VERSION < 9
429 c->retstack = PL_retstack;
430 c->retstack_ix = PL_retstack_ix;
431 c->retstack_max = PL_retstack_max;
432#endif
433 c->curpm = PL_curpm;
434 c->curcop = PL_curcop;
435} 368}
436 369
437/* 370/*
438 * allocate various perl stacks. This is an exact copy 371 * allocate various perl stacks. This is an exact copy
439 * of perl.c:init_stacks, except that it uses less memory 372 * of perl.c:init_stacks, except that it uses less memory
440 * on the (sometimes correct) assumption that coroutines do 373 * on the (sometimes correct) assumption that coroutines do
441 * not usually need a lot of stackspace. 374 * not usually need a lot of stackspace.
442 */ 375 */
443static void 376static void
444coro_init_stacks (pTHX) 377coro_init_stacks ()
445{ 378{
446 LOCK;
447
448 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 379 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
449 PL_curstackinfo->si_type = PERLSI_MAIN; 380 PL_curstackinfo->si_type = PERLSI_MAIN;
450 PL_curstack = PL_curstackinfo->si_stack; 381 PL_curstack = PL_curstackinfo->si_stack;
451 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 382 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
452 383
473 404
474 New(54,PL_savestack,96,ANY); 405 New(54,PL_savestack,96,ANY);
475 PL_savestack_ix = 0; 406 PL_savestack_ix = 0;
476 PL_savestack_max = 96; 407 PL_savestack_max = 96;
477 408
478#if PERL_VERSION < 9 409#if !PERL_VERSION_ATLEAST (5,9,0)
479 New(54,PL_retstack,8,OP*); 410 New(54,PL_retstack,8,OP*);
480 PL_retstack_ix = 0; 411 PL_retstack_ix = 0;
481 PL_retstack_max = 8; 412 PL_retstack_max = 8;
482#endif 413#endif
483
484 UNLOCK;
485} 414}
486 415
487/* 416/*
488 * destroy the stacks, the callchain etc... 417 * destroy the stacks, the callchain etc...
489 */ 418 */
490static void 419static void
491destroy_stacks(pTHX) 420coro_destroy_stacks ()
492{ 421{
493 if (!IN_DESTRUCT) 422 if (!IN_DESTRUCT)
494 { 423 {
495 /* is this ugly, I ask? */ 424 /* is this ugly, I ask? */
496 LEAVE_SCOPE (0); 425 LEAVE_SCOPE (0);
527 456
528 Safefree (PL_tmps_stack); 457 Safefree (PL_tmps_stack);
529 Safefree (PL_markstack); 458 Safefree (PL_markstack);
530 Safefree (PL_scopestack); 459 Safefree (PL_scopestack);
531 Safefree (PL_savestack); 460 Safefree (PL_savestack);
532#if PERL_VERSION < 9 461#if !PERL_VERSION_ATLEAST (5,9,0)
533 Safefree (PL_retstack); 462 Safefree (PL_retstack);
534#endif 463#endif
535} 464}
536 465
537static void 466static void
538setup_coro (struct coro *coro) 467setup_coro (struct coro *coro)
539{ 468{
540 /* 469 /*
541 * emulate part of the perl startup here. 470 * emulate part of the perl startup here.
542 */ 471 */
543 dTHX;
544 dSP;
545 UNOP myop;
546 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
547 472
548 coro_init_stacks (aTHX); 473 coro_init_stacks ();
474
549 /*PL_curcop = 0;*/ 475 PL_curcop = 0;
550 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 476 PL_in_eval = 0;
477 PL_curpm = 0;
478
479 {
480 dSP;
481 LOGOP myop;
482
483 /* I have no idea why this is needed, but it is */
484 PUSHMARK (SP);
485
551 SvREFCNT_dec (GvAV (PL_defgv)); 486 SvREFCNT_dec (GvAV (PL_defgv));
552 GvAV (PL_defgv) = coro->args; coro->args = 0; 487 GvAV (PL_defgv) = coro->args; coro->args = 0;
553 488
554 SPAGAIN;
555
556 Zero (&myop, 1, UNOP); 489 Zero (&myop, 1, LOGOP);
557 myop.op_next = Nullop; 490 myop.op_next = Nullop;
558 myop.op_flags = OPf_WANT_VOID; 491 myop.op_flags = OPf_WANT_VOID;
559 492
560 PL_op = (OP *)&myop; 493 PL_op = (OP *)&myop;
561 494
562 PUSHMARK(SP); 495 PUSHMARK (SP);
563 XPUSHs (sub_init); 496 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
564 PUTBACK; 497 PUTBACK;
565 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 498 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
566 SPAGAIN; 499 SPAGAIN;
567 500
568 ENTER; /* necessary e.g. for dounwind */ 501 ENTER; /* necessary e.g. for dounwind */
502 }
569} 503}
570 504
571static void 505static void
572free_coro_mortal () 506free_coro_mortal ()
573{ 507{
576 SvREFCNT_dec (coro_mortal); 510 SvREFCNT_dec (coro_mortal);
577 coro_mortal = 0; 511 coro_mortal = 0;
578 } 512 }
579} 513}
580 514
515static void NOINLINE
516prepare_cctx (coro_cctx *cctx)
517{
518 dSP;
519 LOGOP myop;
520
521 Zero (&myop, 1, LOGOP);
522 myop.op_next = PL_op;
523 myop.op_flags = OPf_WANT_VOID;
524
525 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
526
527 PUSHMARK (SP);
528 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
529 PUTBACK;
530 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
531 SPAGAIN;
532}
533
581static void 534static void
582coro_run (void *arg) 535coro_run (void *arg)
583{ 536{
537 /* coro_run is the alternative epilogue of transfer() */
538 UNLOCK;
539
584 /* 540 /*
585 * this is a _very_ stripped down perl interpreter ;) 541 * this is a _very_ stripped down perl interpreter ;)
586 */ 542 */
587 dTHX;
588 int ret;
589
590 UNLOCK;
591
592 PL_top_env = &PL_start_env; 543 PL_top_env = &PL_start_env;
593 544
594 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); 545 /* inject call to cctx_init */
595 sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op)); 546 prepare_cctx ((coro_cctx *)arg);
596
597 /* continue at cctx_init, without entersub */
598 PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE));
599 547
600 /* somebody will hit me for both perl_run and PL_restartop */ 548 /* somebody will hit me for both perl_run and PL_restartop */
601 ret = perl_run (aTHX_ PERL_GET_CONTEXT); 549 perl_run (PL_curinterp);
602 printf ("ret %d\n", ret);//D
603 550
604 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); 551 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
605 abort (); 552 abort ();
606} 553}
607 554
608static coro_stack * 555static coro_cctx *
609stack_new () 556cctx_new ()
610{ 557{
611 coro_stack *stack; 558 coro_cctx *cctx;
612 559
560 ++cctx_count;
561
613 New (0, stack, 1, coro_stack); 562 New (0, cctx, 1, coro_cctx);
614 563
615#if HAVE_MMAP 564#if HAVE_MMAP
616 565
617 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 566 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
618 /* mmap suppsedly does allocate-on-write for us */ 567 /* mmap suppsedly does allocate-on-write for us */
619 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 568 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
620 569
621 if (stack->sptr == (void *)-1) 570 if (cctx->sptr == (void *)-1)
622 { 571 {
623 perror ("FATAL: unable to mmap stack for coroutine"); 572 perror ("FATAL: unable to mmap stack for coroutine");
624 _exit (EXIT_FAILURE); 573 _exit (EXIT_FAILURE);
625 } 574 }
626 575
627# if STACKGUARD 576# if STACKGUARD
628 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 577 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
629# endif 578# endif
630 579
631#else 580#else
632 581
633 stack->ssize = STACKSIZE * (long)sizeof (long); 582 cctx->ssize = STACKSIZE * (long)sizeof (long);
634 New (0, stack->sptr, STACKSIZE, long); 583 New (0, cctx->sptr, STACKSIZE, long);
635 584
636 if (!stack->sptr) 585 if (!cctx->sptr)
637 { 586 {
638 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 587 perror ("FATAL: unable to malloc stack for coroutine");
639 _exit (EXIT_FAILURE); 588 _exit (EXIT_FAILURE);
640 } 589 }
641 590
642#endif 591#endif
643 592
593#if USE_VALGRIND
594 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
595 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
596 cctx->ssize + (char *)cctx->sptr
597 );
598#endif
599
644 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); 600 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
645 601
646 return stack; 602 return cctx;
647} 603}
648 604
649static void 605static void
650stack_free (coro_stack *stack) 606cctx_free (coro_cctx *cctx)
651{ 607{
652 if (!stack || stack == main_stack) 608 if (!cctx)
653 return; 609 return;
654 610
611 --cctx_count;
612
613#if USE_VALGRIND
614 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
615#endif
616
655#if HAVE_MMAP 617#if HAVE_MMAP
656 munmap (stack->sptr, stack->ssize); 618 munmap (cctx->sptr, cctx->ssize);
657#else 619#else
658 Safefree (stack->sptr); 620 Safefree (cctx->sptr);
659#endif 621#endif
660 622
661 Safefree (stack); 623 Safefree (cctx);
662} 624}
663 625
664static coro_stack *stack_first;
665
666static coro_stack * 626static coro_cctx *
667stack_get () 627cctx_get ()
668{ 628{
669 coro_stack *stack; 629 coro_cctx *cctx;
670 630
671 if (stack_first) 631 if (cctx_first)
672 { 632 {
673 stack = stack_first; 633 --cctx_idle;
634 cctx = cctx_first;
674 stack_first = stack->next; 635 cctx_first = cctx->next;
675 } 636 }
676 else 637 else
677 { 638 {
678 stack = stack_new (); 639 cctx = cctx_new ();
679 PL_op = PL_op->op_next; 640 PL_op = PL_op->op_next;
680 } 641 }
681 642
682 return stack; 643 return cctx;
683} 644}
684 645
685static void 646static void
686stack_put (coro_stack *stack) 647cctx_put (coro_cctx *cctx)
687{ 648{
688 stack->next = stack_first; 649 ++cctx_idle;
689 stack_first = stack; 650 cctx->next = cctx_first;
651 cctx_first = cctx;
690} 652}
691 653
692/* never call directly, always through the coro_state_transfer global variable */ 654/* never call directly, always through the coro_state_transfer global variable */
693static void 655static void NOINLINE
694transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 656transfer (struct coro *prev, struct coro *next, int flags)
695{ 657{
696 dSTACKLEVEL; 658 dSTACKLEVEL;
697 659
698 /* sometimes transfer is only called to set idle_sp */ 660 /* sometimes transfer is only called to set idle_sp */
699 if (flags == TRANSFER_SET_STACKLEVEL) 661 if (flags == TRANSFER_SET_STACKLEVEL)
700 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 662 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
701 else if (prev != next) 663 else if (prev != next)
702 { 664 {
703 coro_stack *prev__stack; 665 coro_cctx *prev__cctx;
704 666
705 LOCK; 667 LOCK;
706 668
707 if (next->mainstack) 669 if (next->mainstack)
708 { 670 {
716 /* first get rid of the old state */ 678 /* first get rid of the old state */
717 SAVE (prev, -1); 679 SAVE (prev, -1);
718 /* setup coroutine call */ 680 /* setup coroutine call */
719 setup_coro (next); 681 setup_coro (next);
720 /* need a stack */ 682 /* need a stack */
721 next->stack = 0; 683 next->cctx = 0;
722 } 684 }
723 685
686 if (!prev->cctx)
687 /* create a new empty context */
688 Newz (0, prev->cctx, 1, coro_cctx);
689
724 prev__stack = prev->stack; 690 prev__cctx = prev->cctx;
725 691
726 /* possibly "free" the stack */ 692 /* possibly "free" the cctx */
727 if (prev__stack->idle_sp == STACKLEVEL) 693 if (prev__cctx->idle_sp == STACKLEVEL)
728 { 694 {
729 stack_put (prev__stack); 695 cctx_put (prev__cctx);
730 prev->stack = 0; 696 prev->cctx = 0;
731 } 697 }
732 698
733 if (!next->stack) 699 if (!next->cctx)
734 next->stack = stack_get (); 700 next->cctx = cctx_get ();
735 701
736 if (prev__stack != next->stack) 702 if (prev__cctx != next->cctx)
737 { 703 {
738 prev__stack->top_env = PL_top_env; 704 prev__cctx->top_env = PL_top_env;
739 PL_top_env = next->stack->top_env; 705 PL_top_env = next->cctx->top_env;
740 printf ("stacksw %p %p\n", prev__stack->idle_sp, next->stack->idle_sp);//D
741 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 706 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
742 } 707 }
743 708
744 free_coro_mortal (); 709 free_coro_mortal ();
745 710
746 UNLOCK; 711 UNLOCK;
747 } 712 }
748} 713}
749
750/* use this function pointer to call the above function */
751/* this is done to increase chances of the compiler not inlining the call */
752/* not static to make it even harder for the compiler (and theoretically impossible in most cases */
753void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl;
754 714
755struct transfer_args 715struct transfer_args
756{ 716{
757 struct coro *prev, *next; 717 struct coro *prev, *next;
758 int flags; 718 int flags;
759}; 719};
760 720
761#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) 721#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
762 722
763static void 723static void
764coro_state_destroy (struct coro *coro) 724coro_state_destroy (struct coro *coro)
765{ 725{
766 if (coro->refcnt--) 726 if (coro->refcnt--)
768 728
769 if (coro->mainstack && coro->mainstack != main_mainstack) 729 if (coro->mainstack && coro->mainstack != main_mainstack)
770 { 730 {
771 struct coro temp; 731 struct coro temp;
772 732
773 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 733 SAVE ((&temp), TRANSFER_SAVE_ALL);
774 LOAD (aTHX_ coro); 734 LOAD (coro);
775 735
776 destroy_stacks (aTHX); 736 coro_destroy_stacks ();
777 737
778 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 738 LOAD ((&temp)); /* this will get rid of defsv etc.. */
779 739
780 coro->mainstack = 0; 740 coro->mainstack = 0;
781 } 741 }
782 742
783 stack_free (coro->stack); 743 cctx_free (coro->cctx);
784 SvREFCNT_dec (coro->args); 744 SvREFCNT_dec (coro->args);
785 Safefree (coro); 745 Safefree (coro);
786} 746}
787 747
788static int 748static int
789coro_state_clear (SV *sv, MAGIC *mg) 749coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
790{ 750{
791 struct coro *coro = (struct coro *)mg->mg_ptr; 751 struct coro *coro = (struct coro *)mg->mg_ptr;
792 mg->mg_ptr = 0; 752 mg->mg_ptr = 0;
793 753
794 coro_state_destroy (coro); 754 coro_state_destroy (coro);
795 755
796 return 0; 756 return 0;
797} 757}
798 758
799static int 759static int
800coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 760coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
801{ 761{
802 struct coro *coro = (struct coro *)mg->mg_ptr; 762 struct coro *coro = (struct coro *)mg->mg_ptr;
803 763
804 ++coro->refcnt; 764 ++coro->refcnt;
805 765
806 return 0; 766 return 0;
807} 767}
808 768
809static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 769static MGVTBL coro_state_vtbl = {
770 0, 0, 0, 0,
771 coro_state_clear,
772 0,
773#ifdef MGf_DUP
774 coro_state_dup,
775#else
776# define MGf_DUP 0
777#endif
778};
810 779
811static struct coro * 780static struct coro *
812SvSTATE (SV *coro) 781SvSTATE (SV *coro)
813{ 782{
814 HV *stash; 783 HV *stash;
829 assert (mg->mg_type == PERL_MAGIC_ext); 798 assert (mg->mg_type == PERL_MAGIC_ext);
830 return (struct coro *)mg->mg_ptr; 799 return (struct coro *)mg->mg_ptr;
831} 800}
832 801
833static void 802static void
834prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) 803prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
835{ 804{
836 ta->prev = SvSTATE (prev); 805 ta->prev = SvSTATE (prev);
837 ta->next = SvSTATE (next); 806 ta->next = SvSTATE (next);
838 ta->flags = flags; 807 ta->flags = flags;
839} 808}
842api_transfer (SV *prev, SV *next, int flags) 811api_transfer (SV *prev, SV *next, int flags)
843{ 812{
844 dTHX; 813 dTHX;
845 struct transfer_args ta; 814 struct transfer_args ta;
846 815
847 prepare_transfer (aTHX_ &ta, prev, next, flags); 816 prepare_transfer (&ta, prev, next, flags);
848 TRANSFER (ta); 817 TRANSFER (ta);
849} 818}
850 819
851/** Coro ********************************************************************/ 820/** Coro ********************************************************************/
852 821
861static GV *coro_current, *coro_idle; 830static GV *coro_current, *coro_idle;
862static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 831static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
863static int coro_nready; 832static int coro_nready;
864 833
865static void 834static void
866coro_enq (pTHX_ SV *sv) 835coro_enq (SV *sv)
867{ 836{
868 int prio; 837 int prio;
869 838
870 if (SvTYPE (sv) != SVt_PVHV) 839 if (SvTYPE (sv) != SVt_PVHV)
871 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 840 croak ("Coro::ready tried to enqueue something that is not a coroutine");
875 av_push (coro_ready [prio - PRIO_MIN], sv); 844 av_push (coro_ready [prio - PRIO_MIN], sv);
876 coro_nready++; 845 coro_nready++;
877} 846}
878 847
879static SV * 848static SV *
880coro_deq (pTHX_ int min_prio) 849coro_deq (int min_prio)
881{ 850{
882 int prio = PRIO_MAX - PRIO_MIN; 851 int prio = PRIO_MAX - PRIO_MIN;
883 852
884 min_prio -= PRIO_MIN; 853 min_prio -= PRIO_MIN;
885 if (min_prio < 0) 854 if (min_prio < 0)
902 871
903 if (SvROK (coro)) 872 if (SvROK (coro))
904 coro = SvRV (coro); 873 coro = SvRV (coro);
905 874
906 LOCK; 875 LOCK;
907 coro_enq (aTHX_ SvREFCNT_inc (coro)); 876 coro_enq (SvREFCNT_inc (coro));
908 UNLOCK; 877 UNLOCK;
909} 878}
910 879
911static void 880static void
912prepare_schedule (aTHX_ struct transfer_args *ta) 881prepare_schedule (struct transfer_args *ta)
913{ 882{
914 SV *current, *prev, *next; 883 SV *current, *prev, *next;
915
916 LOCK;
917 884
918 current = GvSV (coro_current); 885 current = GvSV (coro_current);
919 886
920 for (;;) 887 for (;;)
921 { 888 {
922 LOCK; 889 LOCK;
923
924 next = coro_deq (aTHX_ PRIO_MIN); 890 next = coro_deq (PRIO_MIN);
891 UNLOCK;
925 892
926 if (next) 893 if (next)
927 break; 894 break;
928
929 UNLOCK;
930 895
931 { 896 {
932 dSP; 897 dSP;
933 898
934 ENTER; 899 ENTER;
945 910
946 prev = SvRV (current); 911 prev = SvRV (current);
947 SvRV (current) = next; 912 SvRV (current) = next;
948 913
949 /* free this only after the transfer */ 914 /* free this only after the transfer */
915 LOCK;
950 free_coro_mortal (); 916 free_coro_mortal ();
917 UNLOCK;
951 coro_mortal = prev; 918 coro_mortal = prev;
952 919
953 ta->prev = SvSTATE (prev); 920 ta->prev = SvSTATE (prev);
954 ta->next = SvSTATE (next); 921 ta->next = SvSTATE (next);
955 ta->flags = TRANSFER_SAVE_ALL; 922 ta->flags = TRANSFER_SAVE_ALL;
956
957 UNLOCK;
958} 923}
959 924
960static void 925static void
961prepare_cede (aTHX_ struct transfer_args *ta) 926prepare_cede (struct transfer_args *ta)
962{ 927{
963 LOCK; 928 LOCK;
964 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); 929 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
965 UNLOCK; 930 UNLOCK;
966 931
967 prepare_schedule (ta); 932 prepare_schedule (ta);
968} 933}
969 934
1007 main_mainstack = PL_mainstack; 972 main_mainstack = PL_mainstack;
1008 973
1009 coroapi.ver = CORO_API_VERSION; 974 coroapi.ver = CORO_API_VERSION;
1010 coroapi.transfer = api_transfer; 975 coroapi.transfer = api_transfer;
1011 976
1012 Newz (0, main_stack, 1, coro_stack);
1013 main_stack->idle_sp = (void *)-1;
1014
1015 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 977 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1016} 978}
1017 979
1018SV * 980SV *
1019new (char *klass, ...) 981new (char *klass, ...)
1030 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 992 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1031 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 993 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1032 994
1033 for (i = 1; i < items; i++) 995 for (i = 1; i < items; i++)
1034 av_push (coro->args, newSVsv (ST (i))); 996 av_push (coro->args, newSVsv (ST (i)));
1035
1036 coro->stack = main_stack;
1037 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1038 /*coro->stack = 0;*/
1039} 997}
1040 OUTPUT: 998 OUTPUT:
1041 RETVAL 999 RETVAL
1042 1000
1043void 1001void
1052 struct transfer_args ta; 1010 struct transfer_args ta;
1053 1011
1054 switch (ix) 1012 switch (ix)
1055 { 1013 {
1056 case 0: 1014 case 0:
1057 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1015 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1058 ta.next = 0; 1016 ta.next = 0;
1059 ta.flags = TRANSFER_SET_STACKLEVEL; 1017 ta.flags = TRANSFER_SET_STACKLEVEL;
1060 break; 1018 break;
1061 1019
1062 case 1: 1020 case 1:
1116 ++coro_src->refcnt; 1074 ++coro_src->refcnt;
1117 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1075 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1118} 1076}
1119 1077
1120void 1078void
1121_nonlocal_goto (IV nextop)
1122 CODE:
1123 /* uuh, somebody will kill me again for this */
1124 PL_op->op_next = INT2PTR (OP *, nextop);
1125
1126void
1127_exit (code) 1079_exit (code)
1128 int code 1080 int code
1129 PROTOTYPE: $ 1081 PROTOTYPE: $
1130 CODE: 1082 CODE:
1131 _exit (code); 1083 _exit (code);
1084
1085int
1086cctx_count ()
1087 CODE:
1088 RETVAL = cctx_count;
1089 OUTPUT:
1090 RETVAL
1091
1092int
1093cctx_idle ()
1094 CODE:
1095 RETVAL = cctx_idle;
1096 OUTPUT:
1097 RETVAL
1132 1098
1133MODULE = Coro::State PACKAGE = Coro 1099MODULE = Coro::State PACKAGE = Coro
1134 1100
1135BOOT: 1101BOOT:
1136{ 1102{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines