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.97 by root, Sun Nov 26 21:21:14 2006 UTC vs.
Revision 1.109 by root, Mon Nov 27 18:45:29 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; /* sp of top-level transfer/schedule/cede call */ 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 is probably cleaner, but also slower? */ 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{
571 if (coro_mortal) 512 if (coro_mortal)
572 { 513 {
573 dTHX;
574
575 SvREFCNT_dec (coro_mortal); 514 SvREFCNT_dec (coro_mortal);
576 coro_mortal = 0; 515 coro_mortal = 0;
577 } 516 }
578} 517}
579 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
580static void 538static void
581coro_run (void *arg) 539coro_run (void *arg)
582{ 540{
541 /* coro_run is the alternative epilogue of transfer() */
542 UNLOCK;
543
583 /* 544 /*
584 * this is a _very_ stripped down perl interpreter ;) 545 * this is a _very_ stripped down perl interpreter ;)
585 */ 546 */
586 dTHX;
587 int ret;
588
589 UNLOCK;
590
591 PL_top_env = &PL_start_env; 547 PL_top_env = &PL_start_env;
592 548
593 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); 549 /* inject call to cctx_init */
594 sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op)); 550 prepare_cctx ((coro_cctx *)arg);
595
596 /* continue at cctx_init, without entersub */
597 PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE));
598 551
599 /* somebody will hit me for both perl_run and PL_restartop */ 552 /* somebody will hit me for both perl_run and PL_restartop */
600 ret = perl_run (aTHX_ PERL_GET_CONTEXT); 553 perl_run (PL_curinterp);
601 printf ("ret %d\n", ret);//D
602 554
603 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);
604 abort (); 556 abort ();
605} 557}
606 558
607static coro_stack * 559static coro_cctx *
608stack_new () 560cctx_new ()
609{ 561{
610 coro_stack *stack; 562 coro_cctx *cctx;
611 563
564 ++cctx_count;
565
612 New (0, stack, 1, coro_stack); 566 New (0, cctx, 1, coro_cctx);
613 567
614#if HAVE_MMAP 568#if HAVE_MMAP
615 569
616 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 570 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
617 /* mmap suppsedly does allocate-on-write for us */ 571 /* mmap suppsedly does allocate-on-write for us */
618 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);
619 573
620 if (stack->sptr == (void *)-1) 574 if (cctx->sptr == (void *)-1)
621 { 575 {
622 perror ("FATAL: unable to mmap stack for coroutine"); 576 perror ("FATAL: unable to mmap stack for coroutine");
623 _exit (EXIT_FAILURE); 577 _exit (EXIT_FAILURE);
624 } 578 }
625 579
626# if STACKGUARD 580# if STACKGUARD
627 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 581 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
628# endif 582# endif
629 583
630#else 584#else
631 585
632 stack->ssize = STACKSIZE * (long)sizeof (long); 586 cctx->ssize = STACKSIZE * (long)sizeof (long);
633 New (0, stack->sptr, STACKSIZE, long); 587 New (0, cctx->sptr, STACKSIZE, long);
634 588
635 if (!stack->sptr) 589 if (!cctx->sptr)
636 { 590 {
637 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 591 perror ("FATAL: unable to malloc stack for coroutine");
638 _exit (EXIT_FAILURE); 592 _exit (EXIT_FAILURE);
639 } 593 }
640 594
641#endif 595#endif
642 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
643 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);
644 605
645 return stack; 606 return cctx;
646} 607}
647 608
648static void 609static void
649stack_free (coro_stack *stack) 610cctx_free (coro_cctx *cctx)
650{ 611{
651 if (!stack) 612 if (!cctx)
652 return; 613 return;
653 614
615 --cctx_count;
616
617#if USE_VALGRIND
618 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
619#endif
620
654#if HAVE_MMAP 621#if HAVE_MMAP
655 munmap (stack->sptr, stack->ssize); 622 munmap (cctx->sptr, cctx->ssize);
656#else 623#else
657 Safefree (stack->sptr); 624 Safefree (cctx->sptr);
658#endif 625#endif
659 626
660 Safefree (stack); 627 Safefree (cctx);
661} 628}
662 629
663static coro_stack *stack_first;
664
665static coro_stack * 630static coro_cctx *
666stack_get () 631cctx_get ()
667{ 632{
668 coro_stack *stack; 633 coro_cctx *cctx;
669 634
670 if (stack_first) 635 if (cctx_first)
671 { 636 {
672 stack = stack_first; 637 --cctx_idle;
638 cctx = cctx_first;
673 stack_first = stack->next; 639 cctx_first = cctx->next;
674 } 640 }
675 else 641 else
676 { 642 {
677 stack = stack_new (); 643 cctx = cctx_new ();
678 PL_op = PL_op->op_next; 644 PL_op = PL_op->op_next;
679 } 645 }
680 646
681 return stack; 647 return cctx;
682} 648}
683 649
684static void 650static void
685stack_put (coro_stack *stack) 651cctx_put (coro_cctx *cctx)
686{ 652{
687 stack->next = stack_first; 653 ++cctx_idle;
688 stack_first = stack; 654 cctx->next = cctx_first;
655 cctx_first = cctx;
689} 656}
690 657
691/* never call directly, always through the coro_state_transfer global variable */ 658/* never call directly, always through the coro_state_transfer global variable */
692static void 659static void NOINLINE
693transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 660transfer (struct coro *prev, struct coro *next, int flags)
694{ 661{
695 dSTACKLEVEL; 662 dSTACKLEVEL;
696 663
697 /* sometimes transfer is only called to set idle_sp */ 664 /* sometimes transfer is only called to set idle_sp */
698 if (flags == TRANSFER_SET_STACKLEVEL) 665 if (flags == TRANSFER_SET_STACKLEVEL)
699 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 666 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
700 else if (prev != next) 667 else if (prev != next)
701 { 668 {
702 coro_stack *prev__stack; 669 coro_cctx *prev__cctx;
703 670
704 LOCK; 671 LOCK;
705 672
706 if (next->mainstack) 673 if (next->mainstack)
707 { 674 {
715 /* first get rid of the old state */ 682 /* first get rid of the old state */
716 SAVE (prev, -1); 683 SAVE (prev, -1);
717 /* setup coroutine call */ 684 /* setup coroutine call */
718 setup_coro (next); 685 setup_coro (next);
719 /* need a stack */ 686 /* need a stack */
720 next->stack = 0; 687 next->cctx = 0;
721 } 688 }
722 689
723 if (!prev->stack) 690 if (!prev->cctx)
724 /* create a new empty context */ 691 /* create a new empty context */
725 Newz (0, prev->stack, 1, coro_stack); 692 Newz (0, prev->cctx, 1, coro_cctx);
726 693
727 prev__stack = prev->stack; 694 prev__cctx = prev->cctx;
728 695
729 /* possibly "free" the stack */ 696 /* possibly "free" the cctx */
730 if (prev__stack->idle_sp == STACKLEVEL) 697 if (prev__cctx->idle_sp == STACKLEVEL)
731 { 698 {
732 stack_put (prev__stack); 699 cctx_put (prev__cctx);
733 prev->stack = 0; 700 prev->cctx = 0;
734 } 701 }
735 702
736 if (!next->stack) 703 if (!next->cctx)
737 next->stack = stack_get (); 704 next->cctx = cctx_get ();
738 705
739 if (prev__stack != next->stack) 706 if (prev__cctx != next->cctx)
740 { 707 {
741 prev__stack->top_env = PL_top_env; 708 prev__cctx->top_env = PL_top_env;
742 PL_top_env = next->stack->top_env; 709 PL_top_env = next->cctx->top_env;
743 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 710 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
744 } 711 }
745 712
746 free_coro_mortal (); 713 free_coro_mortal ();
747 714
748 UNLOCK; 715 UNLOCK;
749 } 716 }
750} 717}
751
752/* use this function pointer to call the above function */
753/* this is done to increase chances of the compiler not inlining the call */
754/* not static to make it even harder for the compiler (and theoretically impossible in most cases */
755void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl;
756 718
757struct transfer_args 719struct transfer_args
758{ 720{
759 struct coro *prev, *next; 721 struct coro *prev, *next;
760 int flags; 722 int flags;
761}; 723};
762 724
763#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) 725#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
764 726
765static void 727static void
766coro_state_destroy (struct coro *coro) 728coro_state_destroy (struct coro *coro)
767{ 729{
768 if (coro->refcnt--) 730 if (coro->refcnt--)
770 732
771 if (coro->mainstack && coro->mainstack != main_mainstack) 733 if (coro->mainstack && coro->mainstack != main_mainstack)
772 { 734 {
773 struct coro temp; 735 struct coro temp;
774 736
775 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 737 SAVE ((&temp), TRANSFER_SAVE_ALL);
776 LOAD (aTHX_ coro); 738 LOAD (coro);
777 739
778 destroy_stacks (aTHX); 740 coro_destroy_stacks ();
779 741
780 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 742 LOAD ((&temp)); /* this will get rid of defsv etc.. */
781 743
782 coro->mainstack = 0; 744 coro->mainstack = 0;
783 } 745 }
784 746
785 stack_free (coro->stack); 747 cctx_free (coro->cctx);
786 SvREFCNT_dec (coro->args); 748 SvREFCNT_dec (coro->args);
787 Safefree (coro); 749 Safefree (coro);
788} 750}
789 751
790static int 752static int
791coro_state_clear (SV *sv, MAGIC *mg) 753coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
792{ 754{
793 struct coro *coro = (struct coro *)mg->mg_ptr; 755 struct coro *coro = (struct coro *)mg->mg_ptr;
794 mg->mg_ptr = 0; 756 mg->mg_ptr = 0;
795 757
796 coro_state_destroy (coro); 758 coro_state_destroy (coro);
797 759
798 return 0; 760 return 0;
799} 761}
800 762
801static int 763static int
802coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 764coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
803{ 765{
804 struct coro *coro = (struct coro *)mg->mg_ptr; 766 struct coro *coro = (struct coro *)mg->mg_ptr;
805 767
806 ++coro->refcnt; 768 ++coro->refcnt;
807 769
808 return 0; 770 return 0;
809} 771}
810 772
811static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 773static MGVTBL coro_state_vtbl = {
774 0, 0, 0, 0,
775 coro_state_clear,
776 0,
777#ifdef MGf_DUP
778 coro_state_dup,
779#else
780# define MGf_DUP 0
781#endif
782};
812 783
813static struct coro * 784static struct coro *
814SvSTATE (SV *coro) 785SvSTATE (SV *coro)
815{ 786{
816 HV *stash; 787 HV *stash;
831 assert (mg->mg_type == PERL_MAGIC_ext); 802 assert (mg->mg_type == PERL_MAGIC_ext);
832 return (struct coro *)mg->mg_ptr; 803 return (struct coro *)mg->mg_ptr;
833} 804}
834 805
835static void 806static void
836prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) 807prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
837{ 808{
838 ta->prev = SvSTATE (prev); 809 ta->prev = SvSTATE (prev);
839 ta->next = SvSTATE (next); 810 ta->next = SvSTATE (next);
840 ta->flags = flags; 811 ta->flags = flags;
841} 812}
844api_transfer (SV *prev, SV *next, int flags) 815api_transfer (SV *prev, SV *next, int flags)
845{ 816{
846 dTHX; 817 dTHX;
847 struct transfer_args ta; 818 struct transfer_args ta;
848 819
849 prepare_transfer (aTHX_ &ta, prev, next, flags); 820 prepare_transfer (&ta, prev, next, flags);
850 TRANSFER (ta); 821 TRANSFER (ta);
851} 822}
852 823
853/** Coro ********************************************************************/ 824/** Coro ********************************************************************/
854 825
863static GV *coro_current, *coro_idle; 834static GV *coro_current, *coro_idle;
864static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 835static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
865static int coro_nready; 836static int coro_nready;
866 837
867static void 838static void
868coro_enq (pTHX_ SV *sv) 839coro_enq (SV *sv)
869{ 840{
870 int prio; 841 int prio;
871 842
872 if (SvTYPE (sv) != SVt_PVHV) 843 if (SvTYPE (sv) != SVt_PVHV)
873 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 844 croak ("Coro::ready tried to enqueue something that is not a coroutine");
877 av_push (coro_ready [prio - PRIO_MIN], sv); 848 av_push (coro_ready [prio - PRIO_MIN], sv);
878 coro_nready++; 849 coro_nready++;
879} 850}
880 851
881static SV * 852static SV *
882coro_deq (pTHX_ int min_prio) 853coro_deq (int min_prio)
883{ 854{
884 int prio = PRIO_MAX - PRIO_MIN; 855 int prio = PRIO_MAX - PRIO_MIN;
885 856
886 min_prio -= PRIO_MIN; 857 min_prio -= PRIO_MIN;
887 if (min_prio < 0) 858 if (min_prio < 0)
904 875
905 if (SvROK (coro)) 876 if (SvROK (coro))
906 coro = SvRV (coro); 877 coro = SvRV (coro);
907 878
908 LOCK; 879 LOCK;
909 coro_enq (aTHX_ SvREFCNT_inc (coro)); 880 coro_enq (SvREFCNT_inc (coro));
910 UNLOCK; 881 UNLOCK;
911} 882}
912 883
913static void 884static void
914prepare_schedule (aTHX_ struct transfer_args *ta) 885prepare_schedule (struct transfer_args *ta)
915{ 886{
916 SV *current, *prev, *next; 887 SV *current, *prev, *next;
917
918 LOCK;
919 888
920 current = GvSV (coro_current); 889 current = GvSV (coro_current);
921 890
922 for (;;) 891 for (;;)
923 { 892 {
924 LOCK; 893 LOCK;
925
926 next = coro_deq (aTHX_ PRIO_MIN); 894 next = coro_deq (PRIO_MIN);
895 UNLOCK;
927 896
928 if (next) 897 if (next)
929 break; 898 break;
930
931 UNLOCK;
932 899
933 { 900 {
934 dSP; 901 dSP;
935 902
936 ENTER; 903 ENTER;
947 914
948 prev = SvRV (current); 915 prev = SvRV (current);
949 SvRV (current) = next; 916 SvRV (current) = next;
950 917
951 /* free this only after the transfer */ 918 /* free this only after the transfer */
919 LOCK;
952 free_coro_mortal (); 920 free_coro_mortal ();
921 UNLOCK;
953 coro_mortal = prev; 922 coro_mortal = prev;
954 923
955 ta->prev = SvSTATE (prev); 924 ta->prev = SvSTATE (prev);
956 ta->next = SvSTATE (next); 925 ta->next = SvSTATE (next);
957 ta->flags = TRANSFER_SAVE_ALL; 926 ta->flags = TRANSFER_SAVE_ALL;
958
959 UNLOCK;
960} 927}
961 928
962static void 929static void
963prepare_cede (aTHX_ struct transfer_args *ta) 930prepare_cede (struct transfer_args *ta)
964{ 931{
965 LOCK; 932 LOCK;
966 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); 933 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
967 UNLOCK; 934 UNLOCK;
968 935
969 prepare_schedule (ta); 936 prepare_schedule (ta);
970} 937}
971 938
1029 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 996 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1030 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 997 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1031 998
1032 for (i = 1; i < items; i++) 999 for (i = 1; i < items; i++)
1033 av_push (coro->args, newSVsv (ST (i))); 1000 av_push (coro->args, newSVsv (ST (i)));
1034
1035 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1036 /*coro->stack = 0;*/
1037} 1001}
1038 OUTPUT: 1002 OUTPUT:
1039 RETVAL 1003 RETVAL
1040 1004
1041void 1005void
1050 struct transfer_args ta; 1014 struct transfer_args ta;
1051 1015
1052 switch (ix) 1016 switch (ix)
1053 { 1017 {
1054 case 0: 1018 case 0:
1055 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1019 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1056 ta.next = 0; 1020 ta.next = 0;
1057 ta.flags = TRANSFER_SET_STACKLEVEL; 1021 ta.flags = TRANSFER_SET_STACKLEVEL;
1058 break; 1022 break;
1059 1023
1060 case 1: 1024 case 1:
1114 ++coro_src->refcnt; 1078 ++coro_src->refcnt;
1115 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1079 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1116} 1080}
1117 1081
1118void 1082void
1119_nonlocal_goto (IV nextop)
1120 CODE:
1121 /* uuh, somebody will kill me again for this */
1122 PL_op->op_next = INT2PTR (OP *, nextop);
1123
1124void
1125_exit (code) 1083_exit (code)
1126 int code 1084 int code
1127 PROTOTYPE: $ 1085 PROTOTYPE: $
1128 CODE: 1086 CODE:
1129 _exit (code); 1087 _exit (code);
1088
1089int
1090cctx_count ()
1091 CODE:
1092 RETVAL = cctx_count;
1093 OUTPUT:
1094 RETVAL
1095
1096int
1097cctx_idle ()
1098 CODE:
1099 RETVAL = cctx_idle;
1100 OUTPUT:
1101 RETVAL
1130 1102
1131MODULE = Coro::State PACKAGE = Coro 1103MODULE = Coro::State PACKAGE = Coro
1132 1104
1133BOOT: 1105BOOT:
1134{ 1106{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines