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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines