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.93 by root, Sun Nov 26 03:22:50 2006 UTC vs.
Revision 1.121 by root, Sat Dec 2 20:40:25 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/* the maximum number of idle cctx that will be pooled */
14#define MAX_IDLE_CCTX 8
15
16#define PERL_VERSION_ATLEAST(a,b,c) \
17 (PERL_REVISION > (a) \
18 || (PERL_REVISION == (a) \
19 && (PERL_VERSION > (b) \
20 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
21
22#if !PERL_VERSION_ATLEAST (5,6,0)
12# ifndef PL_ppaddr 23# ifndef PL_ppaddr
13# define PL_ppaddr ppaddr 24# define PL_ppaddr ppaddr
14# endif 25# endif
15# ifndef call_sv 26# ifndef call_sv
16# define call_sv perl_call_sv 27# define call_sv perl_call_sv
54# ifndef PAGESIZE 65# ifndef PAGESIZE
55# define PAGESIZE pagesize 66# define PAGESIZE pagesize
56# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) 67# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
57static long pagesize; 68static long pagesize;
58# else 69# else
59# define BOOT_PAGESIZE 70# define BOOT_PAGESIZE (void)0
60# endif 71# endif
72#else
73# define PAGESIZE 0
74# define BOOT_PAGESIZE (void)0
61#endif 75#endif
62 76
63/* The next macro should declare a variable stacklevel that contains and approximation 77/* 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 78 * to the current C stack pointer. Its property is that it changes with each call
65 * and should be unique. */ 79 * and should be unique. */
66#define dSTACKLEVEL int stacklevel 80#define dSTACKLEVEL int stacklevel
67#define STACKLEVEL ((void *)&stacklevel) 81#define STACKLEVEL ((void *)&stacklevel)
68 82
69#define IN_DESTRUCT (PL_main_cv == Nullcv) 83#define IN_DESTRUCT (PL_main_cv == Nullcv)
70 84
71#define labs(l) ((l) >= 0 ? (l) : -(l)) 85#if __GNUC__ >= 3
86# define attribute(x) __attribute__(x)
87#else
88# define attribute(x)
89#endif
90
91#define NOINLINE attribute ((noinline))
72 92
73#include "CoroAPI.h" 93#include "CoroAPI.h"
74 94
75#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ 95#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
76 96
81#else 101#else
82# define LOCK (void)0 102# define LOCK (void)0
83# define UNLOCK (void)0 103# define UNLOCK (void)0
84#endif 104#endif
85 105
106struct io_state
107{
108 int errorno;
109 I32 laststype;
110 int laststatval;
111 Stat_t statcache;
112};
113
86static struct CoroAPI coroapi; 114static struct CoroAPI coroapi;
87static AV *main_mainstack; /* used to differentiate between $main and others */ 115static AV *main_mainstack; /* used to differentiate between $main and others */
88static HV *coro_state_stash, *coro_stash; 116static HV *coro_state_stash, *coro_stash;
89static SV *coro_mortal; /* will be freed after next transfer */ 117static SV *coro_mortal; /* will be freed after next transfer */
90 118
119static struct coro_cctx *cctx_first;
120static int cctx_count, cctx_idle;
121
91/* this is a structure representing a c-level coroutine */ 122/* this is a structure representing a c-level coroutine */
92typedef struct coro_stack { 123typedef struct coro_cctx {
93 struct coro_stack *next; 124 struct coro_cctx *next;
94 125
95 /* the stack */ 126 /* the stack */
96 void *sptr; 127 void *sptr;
97 long ssize; /* positive == mmap, otherwise malloc */ 128 long ssize; /* positive == mmap, otherwise malloc */
98 129
99 /* cpu state */ 130 /* cpu state */
100 void *idle_sp; /* original stacklevel when coroutine was created */ 131 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
101 JMPENV *top_env; 132 JMPENV *top_env;
102 coro_context cctx; 133 coro_context cctx;
103} coro_stack;
104 134
105/* the (fake) coro_stack representing the main program */ 135 int inuse;
106static coro_stack *main_stack; 136
137#if USE_VALGRIND
138 int valgrind_id;
139#endif
140} coro_cctx;
141
142enum {
143 CF_RUNNING = 0x0001, /* coroutine is running */
144 CF_READY = 0x0002, /* coroutine is ready */
145 CF_NEW = 0x0004, /* ahs never been switched to */
146};
107 147
108/* this is a structure representing a perl-level coroutine */ 148/* this is a structure representing a perl-level coroutine */
109struct coro { 149struct coro {
110 /* the c coroutine allocated to this perl coroutine, if any */ 150 /* the c coroutine allocated to this perl coroutine, if any */
111 coro_stack *stack; 151 coro_cctx *cctx;
112 152
113 /* data associated with this coroutine (initial args) */ 153 /* data associated with this coroutine (initial args) */
114 AV *args; 154 AV *args;
115 int refcnt; 155 int refcnt;
156 int flags;
116 157
117 /* optionally saved, might be zero */ 158 /* optionally saved, might be zero */
118 AV *defav; 159 AV *defav;
119 SV *defsv; 160 SV *defsv;
120 SV *errsv; 161 SV *errsv;
121 162
122 /* saved global state not related to stacks */ 163#define VAR(name,type) type name;
123 U8 dowarn; 164# include "state.h"
124 I32 in_eval; 165#undef VAR
125
126 /* the stacks and related info (callchain etc..) */
127 PERL_SI *curstackinfo;
128 AV *curstack;
129 AV *mainstack;
130 SV **stack_sp;
131 OP *op;
132 SV **curpad;
133 AV *comppad;
134 CV *compcv;
135 SV **stack_base;
136 SV **stack_max;
137 SV **tmps_stack;
138 I32 tmps_floor;
139 I32 tmps_ix;
140 I32 tmps_max;
141 I32 *markstack;
142 I32 *markstack_ptr;
143 I32 *markstack_max;
144 I32 *scopestack;
145 I32 scopestack_ix;
146 I32 scopestack_max;
147 ANY *savestack;
148 I32 savestack_ix;
149 I32 savestack_max;
150 OP **retstack;
151 I32 retstack_ix;
152 I32 retstack_max;
153 PMOP *curpm;
154 COP *curcop;
155 166
156 /* coro process data */ 167 /* coro process data */
157 int prio; 168 int prio;
158}; 169};
159 170
160typedef struct coro *Coro__State; 171typedef struct coro *Coro__State;
161typedef struct coro *Coro__State_or_hashref; 172typedef struct coro *Coro__State_or_hashref;
162 173
163static AV * 174static AV *
164coro_clone_padlist (pTHX_ CV *cv) 175coro_clone_padlist (CV *cv)
165{ 176{
166 AV *padlist = CvPADLIST (cv); 177 AV *padlist = CvPADLIST (cv);
167 AV *newpadlist, *newpad; 178 AV *newpadlist, *newpad;
168 179
169 newpadlist = newAV (); 180 newpadlist = newAV ();
170 AvREAL_off (newpadlist); 181 AvREAL_off (newpadlist);
171#if PERL_VERSION < 9 182#if PERL_VERSION_ATLEAST (5,9,0)
183 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
184#else
172 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 185 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
173#else
174 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
175#endif 186#endif
176 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 187 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
177 --AvFILLp (padlist); 188 --AvFILLp (padlist);
178 189
179 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 190 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
181 192
182 return newpadlist; 193 return newpadlist;
183} 194}
184 195
185static void 196static void
186free_padlist (pTHX_ AV *padlist) 197free_padlist (AV *padlist)
187{ 198{
188 /* may be during global destruction */ 199 /* may be during global destruction */
189 if (SvREFCNT (padlist)) 200 if (SvREFCNT (padlist))
190 { 201 {
191 I32 i = AvFILLp (padlist); 202 I32 i = AvFILLp (padlist);
212 AV *padlist; 223 AV *padlist;
213 AV *av = (AV *)mg->mg_obj; 224 AV *av = (AV *)mg->mg_obj;
214 225
215 /* casting is fun. */ 226 /* casting is fun. */
216 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 227 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
217 free_padlist (aTHX_ padlist); 228 free_padlist (padlist);
218 229
219 SvREFCNT_dec (av); 230 SvREFCNT_dec (av);
220 231
221 return 0; 232 return 0;
222} 233}
223 234
224#define PERL_MAGIC_coro PERL_MAGIC_ext 235#define PERL_MAGIC_coro PERL_MAGIC_ext
225 236
226static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 237static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
227 238
239#define CORO_MAGIC(cv) \
240 SvMAGIC (cv) \
241 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
242 ? SvMAGIC (cv) \
243 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
244 : 0
245
228/* the next two functions merely cache the padlists */ 246/* the next two functions merely cache the padlists */
229static void 247static void
230get_padlist (pTHX_ CV *cv) 248get_padlist (CV *cv)
231{ 249{
232 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 250 MAGIC *mg = CORO_MAGIC (cv);
251 AV *av;
233 252
234 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 253 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
235 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 254 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
236 else 255 else
237 { 256 {
238#if 0 257#if 0
239 /* this should work - but it doesn't :( */ 258 /* this is probably cleaner, but also slower? */
240 CV *cp = Perl_cv_clone (aTHX_ cv); 259 CV *cp = Perl_cv_clone (cv);
241 CvPADLIST (cv) = CvPADLIST (cp); 260 CvPADLIST (cv) = CvPADLIST (cp);
242 CvPADLIST (cp) = 0; 261 CvPADLIST (cp) = 0;
243 SvREFCNT_dec (cp); 262 SvREFCNT_dec (cp);
244#else 263#else
245 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 264 CvPADLIST (cv) = coro_clone_padlist (cv);
246#endif 265#endif
247 } 266 }
248} 267}
249 268
250static void 269static void
251put_padlist (pTHX_ CV *cv) 270put_padlist (CV *cv)
252{ 271{
253 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 272 MAGIC *mg = CORO_MAGIC (cv);
273 AV *av;
254 274
255 if (!mg) 275 if (!mg)
256 { 276 {
257 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 277 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
258 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 278 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
259 mg->mg_virtual = &vtbl_coro; 279 mg->mg_virtual = &vtbl_coro;
260 mg->mg_obj = (SV *)newAV (); 280 mg->mg_obj = (SV *)newAV ();
261 } 281 }
262 282
263 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); 283 av = (AV *)mg->mg_obj;
284
285 if (AvFILLp (av) >= AvMAX (av))
286 av_extend (av, AvMAX (av) + 1);
287
288 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
264} 289}
265 290
266#define SB do { 291#define SB do {
267#define SE } while (0) 292#define SE } while (0)
268 293
269#define LOAD(state) load_state(aTHX_ (state)); 294#define LOAD(state) load_state ((state))
270#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); 295#define SAVE(state,flags) save_state ((state),(flags))
271 296
272#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 297#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
273 298
274static void 299static void
275load_state(pTHX_ Coro__State c) 300load_state (Coro__State c)
276{ 301{
277 PL_dowarn = c->dowarn; 302#define VAR(name,type) PL_ ## name = c->name;
278 PL_in_eval = c->in_eval; 303# include "state.h"
279 304#undef VAR
280 PL_curstackinfo = c->curstackinfo;
281 PL_curstack = c->curstack;
282 PL_mainstack = c->mainstack;
283 PL_stack_sp = c->stack_sp;
284 PL_op = c->op;
285 PL_curpad = c->curpad;
286 PL_comppad = c->comppad;
287 PL_compcv = c->compcv;
288 PL_stack_base = c->stack_base;
289 PL_stack_max = c->stack_max;
290 PL_tmps_stack = c->tmps_stack;
291 PL_tmps_floor = c->tmps_floor;
292 PL_tmps_ix = c->tmps_ix;
293 PL_tmps_max = c->tmps_max;
294 PL_markstack = c->markstack;
295 PL_markstack_ptr = c->markstack_ptr;
296 PL_markstack_max = c->markstack_max;
297 PL_scopestack = c->scopestack;
298 PL_scopestack_ix = c->scopestack_ix;
299 PL_scopestack_max = c->scopestack_max;
300 PL_savestack = c->savestack;
301 PL_savestack_ix = c->savestack_ix;
302 PL_savestack_max = c->savestack_max;
303#if PERL_VERSION < 9
304 PL_retstack = c->retstack;
305 PL_retstack_ix = c->retstack_ix;
306 PL_retstack_max = c->retstack_max;
307#endif
308 PL_curpm = c->curpm;
309 PL_curcop = c->curcop;
310 305
311 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 306 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
312 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 307 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
313 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 308 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
314 309
317 CV *cv; 312 CV *cv;
318 313
319 /* now do the ugly restore mess */ 314 /* now do the ugly restore mess */
320 while ((cv = (CV *)POPs)) 315 while ((cv = (CV *)POPs))
321 { 316 {
322 AV *padlist = (AV *)POPs;
323
324 if (padlist)
325 {
326 put_padlist (aTHX_ cv); /* mark this padlist as available */ 317 put_padlist (cv); /* mark this padlist as available */
327 CvPADLIST(cv) = padlist; 318 CvDEPTH (cv) = PTR2IV (POPs);
328 } 319 CvPADLIST (cv) = (AV *)POPs;
329
330 ++CvDEPTH(cv);
331 } 320 }
332 321
333 PUTBACK; 322 PUTBACK;
334 } 323 }
335} 324}
336 325
337static void 326static void
338save_state(pTHX_ Coro__State c, int flags) 327save_state (Coro__State c, int flags)
339{ 328{
340 { 329 {
341 dSP; 330 dSP;
342 I32 cxix = cxstack_ix; 331 I32 cxix = cxstack_ix;
343 PERL_CONTEXT *ccstk = cxstack; 332 PERL_CONTEXT *ccstk = cxstack;
354 { 343 {
355 while (cxix >= 0) 344 while (cxix >= 0)
356 { 345 {
357 PERL_CONTEXT *cx = &ccstk[cxix--]; 346 PERL_CONTEXT *cx = &ccstk[cxix--];
358 347
359 if (CxTYPE(cx) == CXt_SUB) 348 if (CxTYPE (cx) == CXt_SUB)
360 { 349 {
361 CV *cv = cx->blk_sub.cv; 350 CV *cv = cx->blk_sub.cv;
351
362 if (CvDEPTH(cv)) 352 if (CvDEPTH (cv))
363 { 353 {
364 EXTEND (SP, CvDEPTH(cv)*2); 354 EXTEND (SP, 3);
365 355
366 while (--CvDEPTH(cv))
367 {
368 /* this tells the restore code to increment CvDEPTH */
369 PUSHs (Nullsv);
370 PUSHs ((SV *)cv);
371 }
372
373 PUSHs ((SV *)CvPADLIST(cv)); 356 PUSHs ((SV *)CvPADLIST (cv));
357 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
374 PUSHs ((SV *)cv); 358 PUSHs ((SV *)cv);
375 359
360 CvDEPTH (cv) = 0;
376 get_padlist (aTHX_ cv); 361 get_padlist (cv);
377 } 362 }
378 } 363 }
379#ifdef CXt_FORMAT 364#ifdef CXt_FORMAT
380 else if (CxTYPE(cx) == CXt_FORMAT) 365 else if (CxTYPE (cx) == CXt_FORMAT)
381 { 366 {
382 /* I never used formats, so how should I know how these are implemented? */ 367 /* I never used formats, so how should I know how these are implemented? */
383 /* my bold guess is as a simple, plain sub... */ 368 /* my bold guess is as a simple, plain sub... */
384 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 369 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
385 } 370 }
399 384
400 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 385 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
401 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 386 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
402 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 387 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
403 388
404 c->dowarn = PL_dowarn; 389#define VAR(name,type)c->name = PL_ ## name;
405 c->in_eval = PL_in_eval; 390# include "state.h"
406 391#undef VAR
407 c->curstackinfo = PL_curstackinfo;
408 c->curstack = PL_curstack;
409 c->mainstack = PL_mainstack;
410 c->stack_sp = PL_stack_sp;
411 c->op = PL_op;
412 c->curpad = PL_curpad;
413 c->comppad = PL_comppad;
414 c->compcv = PL_compcv;
415 c->stack_base = PL_stack_base;
416 c->stack_max = PL_stack_max;
417 c->tmps_stack = PL_tmps_stack;
418 c->tmps_floor = PL_tmps_floor;
419 c->tmps_ix = PL_tmps_ix;
420 c->tmps_max = PL_tmps_max;
421 c->markstack = PL_markstack;
422 c->markstack_ptr = PL_markstack_ptr;
423 c->markstack_max = PL_markstack_max;
424 c->scopestack = PL_scopestack;
425 c->scopestack_ix = PL_scopestack_ix;
426 c->scopestack_max = PL_scopestack_max;
427 c->savestack = PL_savestack;
428 c->savestack_ix = PL_savestack_ix;
429 c->savestack_max = PL_savestack_max;
430#if PERL_VERSION < 9
431 c->retstack = PL_retstack;
432 c->retstack_ix = PL_retstack_ix;
433 c->retstack_max = PL_retstack_max;
434#endif
435 c->curpm = PL_curpm;
436 c->curcop = PL_curcop;
437} 392}
438 393
439/* 394/*
440 * allocate various perl stacks. This is an exact copy 395 * allocate various perl stacks. This is an exact copy
441 * of perl.c:init_stacks, except that it uses less memory 396 * of perl.c:init_stacks, except that it uses less memory
442 * on the (sometimes correct) assumption that coroutines do 397 * on the (sometimes correct) assumption that coroutines do
443 * not usually need a lot of stackspace. 398 * not usually need a lot of stackspace.
444 */ 399 */
445static void 400static void
446coro_init_stacks (pTHX) 401coro_init_stacks ()
447{ 402{
448 LOCK;
449
450 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 403 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
451 PL_curstackinfo->si_type = PERLSI_MAIN; 404 PL_curstackinfo->si_type = PERLSI_MAIN;
452 PL_curstack = PL_curstackinfo->si_stack; 405 PL_curstack = PL_curstackinfo->si_stack;
453 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 406 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
454 407
455 PL_stack_base = AvARRAY(PL_curstack); 408 PL_stack_base = AvARRAY(PL_curstack);
456 PL_stack_sp = PL_stack_base; 409 PL_stack_sp = PL_stack_base;
457 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 410 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
458 411
459 New(50,PL_tmps_stack,96,SV*); 412 New(50,PL_tmps_stack,128,SV*);
460 PL_tmps_floor = -1; 413 PL_tmps_floor = -1;
461 PL_tmps_ix = -1; 414 PL_tmps_ix = -1;
462 PL_tmps_max = 96; 415 PL_tmps_max = 128;
463 416
464 New(54,PL_markstack,16,I32); 417 New(54,PL_markstack,32,I32);
465 PL_markstack_ptr = PL_markstack; 418 PL_markstack_ptr = PL_markstack;
466 PL_markstack_max = PL_markstack + 16; 419 PL_markstack_max = PL_markstack + 32;
467 420
468#ifdef SET_MARK_OFFSET 421#ifdef SET_MARK_OFFSET
469 SET_MARK_OFFSET; 422 SET_MARK_OFFSET;
470#endif 423#endif
471 424
472 New(54,PL_scopestack,16,I32); 425 New(54,PL_scopestack,32,I32);
473 PL_scopestack_ix = 0; 426 PL_scopestack_ix = 0;
474 PL_scopestack_max = 16; 427 PL_scopestack_max = 32;
475 428
476 New(54,PL_savestack,96,ANY); 429 New(54,PL_savestack,64,ANY);
477 PL_savestack_ix = 0; 430 PL_savestack_ix = 0;
478 PL_savestack_max = 96; 431 PL_savestack_max = 64;
479 432
480#if PERL_VERSION < 9 433#if !PERL_VERSION_ATLEAST (5,9,0)
481 New(54,PL_retstack,8,OP*); 434 New(54,PL_retstack,16,OP*);
482 PL_retstack_ix = 0; 435 PL_retstack_ix = 0;
483 PL_retstack_max = 8; 436 PL_retstack_max = 16;
484#endif 437#endif
485
486 UNLOCK;
487} 438}
488 439
489/* 440/*
490 * destroy the stacks, the callchain etc... 441 * destroy the stacks, the callchain etc...
491 */ 442 */
492static void 443static void
493destroy_stacks(pTHX) 444coro_destroy_stacks ()
494{ 445{
495 if (!IN_DESTRUCT) 446 if (!IN_DESTRUCT)
496 { 447 {
497 /* is this ugly, I ask? */ 448 /* is this ugly, I ask? */
498 LEAVE_SCOPE (0); 449 LEAVE_SCOPE (0);
529 480
530 Safefree (PL_tmps_stack); 481 Safefree (PL_tmps_stack);
531 Safefree (PL_markstack); 482 Safefree (PL_markstack);
532 Safefree (PL_scopestack); 483 Safefree (PL_scopestack);
533 Safefree (PL_savestack); 484 Safefree (PL_savestack);
534#if PERL_VERSION < 9 485#if !PERL_VERSION_ATLEAST (5,9,0)
535 Safefree (PL_retstack); 486 Safefree (PL_retstack);
536#endif 487#endif
537} 488}
538 489
539static void 490static void
540setup_coro (struct coro *coro) 491setup_coro (struct coro *coro)
541{ 492{
542 /* 493 /*
543 * emulate part of the perl startup here. 494 * emulate part of the perl startup here.
544 */ 495 */
545 dTHX;
546 dSP;
547 UNOP myop;
548 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
549 496
550 coro_init_stacks (aTHX); 497 coro_init_stacks ();
551 /*PL_curcop = 0;*/ 498
552 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 499 PL_curcop = &PL_compiling;
500 PL_in_eval = EVAL_NULL;
501 PL_curpm = 0;
502 PL_localizing = 0;
503 PL_dirty = 0;
504 PL_restartop = 0;
505
506 {
507 dSP;
508 LOGOP myop;
509
553 SvREFCNT_dec (GvAV (PL_defgv)); 510 SvREFCNT_dec (GvAV (PL_defgv));
554 GvAV (PL_defgv) = coro->args; coro->args = 0; 511 GvAV (PL_defgv) = coro->args; coro->args = 0;
555 512
556 SPAGAIN;
557
558 Zero (&myop, 1, UNOP); 513 Zero (&myop, 1, LOGOP);
559 myop.op_next = Nullop; 514 myop.op_next = Nullop;
560 myop.op_flags = OPf_WANT_VOID; 515 myop.op_flags = OPf_WANT_VOID;
561 516
517 PUSHMARK (SP);
518 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
519 PUTBACK;
562 PL_op = (OP *)&myop; 520 PL_op = (OP *)&myop;
563
564 PUSHMARK(SP);
565 XPUSHs (sub_init);
566 PUTBACK;
567 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 521 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
568 SPAGAIN; 522 SPAGAIN;
523 }
569 524
570 ENTER; /* necessary e.g. for dounwind */ 525 ENTER; /* necessary e.g. for dounwind */
571} 526}
572 527
573static void 528static void
578 SvREFCNT_dec (coro_mortal); 533 SvREFCNT_dec (coro_mortal);
579 coro_mortal = 0; 534 coro_mortal = 0;
580 } 535 }
581} 536}
582 537
538/* inject a fake call to Coro::State::_cctx_init into the execution */
539static void NOINLINE
540prepare_cctx (coro_cctx *cctx)
541{
542 dSP;
543 LOGOP myop;
544
545 Zero (&myop, 1, LOGOP);
546 myop.op_next = PL_op;
547 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
548
549 PUSHMARK (SP);
550 EXTEND (SP, 2);
551 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx))));
552 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
553 PUTBACK;
554 PL_op = (OP *)&myop;
555 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
556 SPAGAIN;
557}
558
583static void 559static void
584coro_run (void *arg) 560coro_run (void *arg)
585{ 561{
562 /* coro_run is the alternative tail of transfer(), so unlock here. */
563 UNLOCK;
564
586 /* 565 /*
587 * this is a _very_ stripped down perl interpreter ;) 566 * this is a _very_ stripped down perl interpreter ;)
588 */ 567 */
589 dTHX;
590
591 UNLOCK;
592
593 PL_top_env = &PL_start_env; 568 PL_top_env = &PL_start_env;
594 569
595 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); 570 /* inject call to cctx_init */
596 sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op)); 571 prepare_cctx ((coro_cctx *)arg);
597 572
598 /* continue at cctx_init, without entersub */
599 PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE));
600
601 /* somebody will hit me for both perl_run and PL_restart_op */ 573 /* somebody will hit me for both perl_run and PL_restartop */
602 perl_run (aTHX_ PERL_GET_CONTEXT); 574 PL_restartop = PL_op;
575 perl_run (PL_curinterp);
603 576
577 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
604 abort (); 578 abort ();
605} 579}
606 580
607static coro_stack * 581static coro_cctx *
608stack_new () 582cctx_new ()
609{ 583{
610 coro_stack *stack; 584 coro_cctx *cctx;
611 585
586 ++cctx_count;
587
612 New (0, stack, 1, coro_stack); 588 New (0, cctx, 1, coro_cctx);
613 589
614#if HAVE_MMAP 590#if HAVE_MMAP
615 591
616 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ 592 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
593 /* mmap supposedly does allocate-on-write for us */
617 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 594 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
618 595
619 if (stack->sptr == (void *)-1) 596 if (cctx->sptr == (void *)-1)
620 { 597 {
621 perror ("FATAL: unable to mmap stack for coroutine"); 598 perror ("FATAL: unable to mmap stack for coroutine");
622 _exit (EXIT_FAILURE); 599 _exit (EXIT_FAILURE);
623 } 600 }
624 601
602# if STACKGUARD
625 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 603 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
604# endif
626 605
627#else 606#else
628 607
629 stack->ssize = STACKSIZE * (long)sizeof (long); 608 cctx->ssize = STACKSIZE * (long)sizeof (long);
630 New (0, stack->sptr, STACKSIZE, long); 609 New (0, cctx->sptr, STACKSIZE, long);
631 610
632 if (!stack->sptr) 611 if (!cctx->sptr)
633 { 612 {
634 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 613 perror ("FATAL: unable to malloc stack for coroutine");
635 _exit (EXIT_FAILURE); 614 _exit (EXIT_FAILURE);
636 } 615 }
637 616
638#endif 617#endif
639 618
619#if USE_VALGRIND
620 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
621 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
622 cctx->ssize + (char *)cctx->sptr
623 );
624#endif
625
640 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); 626 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
641 627
642 return stack; 628 return cctx;
643} 629}
644 630
645static void 631static void
646stack_free (coro_stack *stack) 632cctx_destroy (coro_cctx *cctx)
647{ 633{
648 if (!stack || stack == main_stack) 634 if (!cctx)
649 return; 635 return;
650 636
637 --cctx_count;
638
639#if USE_VALGRIND
640 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
641#endif
642
651#if HAVE_MMAP 643#if HAVE_MMAP
652 munmap (stack->sptr, stack->ssize); 644 munmap (cctx->sptr, cctx->ssize);
653#else 645#else
654 Safefree (stack->sptr); 646 Safefree (cctx->sptr);
655#endif 647#endif
656 648
657 Safefree (stack); 649 Safefree (cctx);
658} 650}
659 651
660static coro_stack *stack_first;
661
662static coro_stack * 652static coro_cctx *
663stack_get () 653cctx_get ()
664{ 654{
665 coro_stack *stack; 655 coro_cctx *cctx;
666 656
667 if (stack_first) 657 if (cctx_first)
668 { 658 {
669 stack = stack_first; 659 cctx = cctx_first;
670 stack_first = stack->next; 660 cctx_first = cctx->next;
661 --cctx_idle;
671 } 662 }
672 else 663 else
673 { 664 {
674 stack = stack_new (); 665 cctx = cctx_new ();
675 PL_op = PL_op->op_next; 666 PL_op = PL_op->op_next;
676 } 667 }
677 668
678 return stack; 669 return cctx;
679} 670}
680 671
681static void 672static void
682stack_put (coro_stack *stack) 673cctx_put (coro_cctx *cctx)
683{ 674{
684 stack->next = stack_first; 675 /* free another cctx if overlimit */
685 stack_first = stack; 676 if (cctx_idle >= MAX_IDLE_CCTX)
677 {
678 coro_cctx *first = cctx_first;
679 cctx_first = first->next;
680 --cctx_idle;
681
682 assert (!first->inuse);
683 cctx_destroy (first);
684 }
685
686 ++cctx_idle;
687 cctx->next = cctx_first;
688 cctx_first = cctx;
686} 689}
687 690
688/* never call directly, always through the coro_state_transfer global variable */ 691/* never call directly, always through the coro_state_transfer global variable */
689static void 692static void NOINLINE
690transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 693transfer (struct coro *prev, struct coro *next, int flags)
691{ 694{
692 dSTACKLEVEL; 695 dSTACKLEVEL;
693 696
694 /* sometimes transfer is only called to set idle_sp */ 697 /* sometimes transfer is only called to set idle_sp */
695 if (flags == TRANSFER_SET_STACKLEVEL) 698 if (flags == TRANSFER_SET_STACKLEVEL)
696 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 699 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
697 else if (prev != next) 700 else if (prev != next)
698 { 701 {
699 coro_stack *prev__stack; 702 coro_cctx *prev__cctx;
703
704 if (prev->flags & CF_NEW)
705 {
706 /* create a new empty context */
707 Newz (0, prev->cctx, 1, coro_cctx);
708 prev->cctx->inuse = 1;
709 prev->flags &= ~CF_NEW;
710 prev->flags |= CF_RUNNING;
711 }
712
713 /*TODO: must not croak here */
714 if (!prev->flags & CF_RUNNING)
715 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
716
717 if (next->flags & CF_RUNNING)
718 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
719
720 prev->flags &= ~CF_RUNNING;
721 next->flags |= CF_RUNNING;
700 722
701 LOCK; 723 LOCK;
702 724
703 if (next->mainstack) 725 if (next->flags & CF_NEW)
726 {
727 /* need to start coroutine */
728 next->flags &= ~CF_NEW;
729 /* first get rid of the old state */
730 SAVE (prev, -1);
731 /* setup coroutine call */
732 setup_coro (next);
733 /* need a new stack */
734 assert (!next->stack);
735 }
736 else
704 { 737 {
705 /* coroutine already started */ 738 /* coroutine already started */
706 SAVE (prev, flags); 739 SAVE (prev, flags);
707 LOAD (next); 740 LOAD (next);
708 } 741 }
709 else 742
743 prev__cctx = prev->cctx;
744
745 /* possibly "free" the cctx */
746 if (prev__cctx->idle_sp == STACKLEVEL)
710 { 747 {
711 /* need to start coroutine */ 748 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
712 /* first get rid of the old state */ 749 assert (PL_top_env == prev__cctx->top_env);
713 SAVE (prev, -1); 750
714 /* setup coroutine call */ 751 prev->cctx = 0;
715 setup_coro (next); 752
716 /* need a stack */ 753 cctx_put (prev__cctx);
717 next->stack = 0; 754 prev__cctx->inuse = 0;
718 } 755 }
719 756
720 prev__stack = prev->stack; 757 if (!next->cctx)
721
722 /* possibly "free" the stack */
723 if (prev__stack->idle_sp == STACKLEVEL)
724 { 758 {
725 stack_put (prev__stack); 759 next->cctx = cctx_get ();
726 prev->stack = 0; 760 assert (!next->cctx->inuse);
761 next->cctx->inuse = 1;
727 } 762 }
728 763
729 if (!next->stack)
730 next->stack = stack_get ();
731
732 if (prev__stack != next->stack) 764 if (prev__cctx != next->cctx)
733 { 765 {
734 prev__stack->top_env = PL_top_env; 766 prev__cctx->top_env = PL_top_env;
735 PL_top_env = next->stack->top_env; 767 PL_top_env = next->cctx->top_env;
736 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 768 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
737 } 769 }
738 770
739 free_coro_mortal (); 771 free_coro_mortal ();
740 772
741 UNLOCK; 773 UNLOCK;
742 } 774 }
743} 775}
744
745/* use this function pointer to call the above function */
746/* this is done to increase chances of the compiler not inlining the call */
747/* not static to make it even harder for the compiler (and theoretically impossible in most cases */
748void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl;
749 776
750struct transfer_args 777struct transfer_args
751{ 778{
752 struct coro *prev, *next; 779 struct coro *prev, *next;
753 int flags; 780 int flags;
754}; 781};
755 782
756#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) 783#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
757 784
758static void 785static void
759coro_state_destroy (struct coro *coro) 786coro_state_destroy (struct coro *coro)
760{ 787{
761 if (coro->refcnt--) 788 if (coro->refcnt--)
763 790
764 if (coro->mainstack && coro->mainstack != main_mainstack) 791 if (coro->mainstack && coro->mainstack != main_mainstack)
765 { 792 {
766 struct coro temp; 793 struct coro temp;
767 794
795 if (coro->flags & CF_RUNNING)
796 croak ("FATAL: tried to destroy currently running coroutine");
797
768 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 798 SAVE ((&temp), TRANSFER_SAVE_ALL);
769 LOAD (aTHX_ coro); 799 LOAD (coro);
770 800
771 destroy_stacks (aTHX); 801 coro_destroy_stacks ();
772 802
773 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 803 LOAD ((&temp)); /* this will get rid of defsv etc.. */
774 804
775 coro->mainstack = 0; 805 coro->mainstack = 0;
776 } 806 }
777 807
778 stack_free (coro->stack); 808 cctx_destroy (coro->cctx);
779 SvREFCNT_dec (coro->args); 809 SvREFCNT_dec (coro->args);
780 Safefree (coro); 810 Safefree (coro);
781} 811}
782 812
783static int 813static int
784coro_state_clear (SV *sv, MAGIC *mg) 814coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
785{ 815{
786 struct coro *coro = (struct coro *)mg->mg_ptr; 816 struct coro *coro = (struct coro *)mg->mg_ptr;
787 mg->mg_ptr = 0; 817 mg->mg_ptr = 0;
788 818
789 coro_state_destroy (coro); 819 coro_state_destroy (coro);
790 820
791 return 0; 821 return 0;
792} 822}
793 823
794static int 824static int
795coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 825coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
796{ 826{
797 struct coro *coro = (struct coro *)mg->mg_ptr; 827 struct coro *coro = (struct coro *)mg->mg_ptr;
798 828
799 ++coro->refcnt; 829 ++coro->refcnt;
800 830
801 return 0; 831 return 0;
802} 832}
803 833
804static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 834static MGVTBL coro_state_vtbl = {
835 0, 0, 0, 0,
836 coro_state_clear,
837 0,
838#ifdef MGf_DUP
839 coro_state_dup,
840#else
841# define MGf_DUP 0
842#endif
843};
805 844
806static struct coro * 845static struct coro *
807SvSTATE (SV *coro) 846SvSTATE (SV *coro)
808{ 847{
809 HV *stash; 848 HV *stash;
824 assert (mg->mg_type == PERL_MAGIC_ext); 863 assert (mg->mg_type == PERL_MAGIC_ext);
825 return (struct coro *)mg->mg_ptr; 864 return (struct coro *)mg->mg_ptr;
826} 865}
827 866
828static void 867static void
829prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) 868prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
830{ 869{
831 ta->prev = SvSTATE (prev); 870 ta->prev = SvSTATE (prev);
832 ta->next = SvSTATE (next); 871 ta->next = SvSTATE (next);
833 ta->flags = flags; 872 ta->flags = flags;
834} 873}
835 874
836static void 875static void
837api_transfer (SV *prev, SV *next, int flags) 876api_transfer (SV *prev, SV *next, int flags)
838{ 877{
839 dTHX;
840 struct transfer_args ta; 878 struct transfer_args ta;
841 879
842 prepare_transfer (aTHX_ &ta, prev, next, flags); 880 prepare_transfer (&ta, prev, next, flags);
843 TRANSFER (ta); 881 TRANSFER (ta);
844} 882}
845 883
846/** Coro ********************************************************************/ 884/** Coro ********************************************************************/
847 885
851#define PRIO_LOW -1 889#define PRIO_LOW -1
852#define PRIO_IDLE -3 890#define PRIO_IDLE -3
853#define PRIO_MIN -4 891#define PRIO_MIN -4
854 892
855/* for Coro.pm */ 893/* for Coro.pm */
856static GV *coro_current, *coro_idle; 894static SV *coro_current;
857static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 895static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
858static int coro_nready; 896static int coro_nready;
859 897
860static void 898static void
861coro_enq (pTHX_ SV *sv) 899coro_enq (SV *coro_sv)
862{ 900{
863 int prio;
864
865 if (SvTYPE (sv) != SVt_PVHV)
866 croak ("Coro::ready tried to enqueue something that is not a coroutine");
867
868 prio = SvSTATE (sv)->prio;
869
870 av_push (coro_ready [prio - PRIO_MIN], sv); 901 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
871 coro_nready++; 902 coro_nready++;
872} 903}
873 904
874static SV * 905static SV *
875coro_deq (pTHX_ int min_prio) 906coro_deq (int min_prio)
876{ 907{
877 int prio = PRIO_MAX - PRIO_MIN; 908 int prio = PRIO_MAX - PRIO_MIN;
878 909
879 min_prio -= PRIO_MIN; 910 min_prio -= PRIO_MIN;
880 if (min_prio < 0) 911 if (min_prio < 0)
888 } 919 }
889 920
890 return 0; 921 return 0;
891} 922}
892 923
893static void 924static int
894api_ready (SV *coro) 925api_ready (SV *coro_sv)
895{ 926{
896 dTHX; 927 struct coro *coro;
897 928
898 if (SvROK (coro)) 929 if (SvROK (coro_sv))
899 coro = SvRV (coro); 930 coro_sv = SvRV (coro_sv);
931
932 coro = SvSTATE (coro_sv);
933
934 if (coro->flags & CF_READY)
935 return 0;
936
937#if 0 /* this is actually harmless */
938 if (coro->flags & CF_RUNNING)
939 croak ("Coro::ready called on currently running coroutine");
940#endif
941
942 coro->flags |= CF_READY;
900 943
901 LOCK; 944 LOCK;
902 coro_enq (aTHX_ SvREFCNT_inc (coro)); 945 coro_enq (SvREFCNT_inc (coro_sv));
903 UNLOCK; 946 UNLOCK;
904}
905 947
948 return 1;
949}
950
906static void 951static int
952api_is_ready (SV *coro_sv)
953{
954 return !!SvSTATE (coro_sv)->flags & CF_READY;
955}
956
957static void
907prepare_schedule (aTHX_ struct transfer_args *ta) 958prepare_schedule (struct transfer_args *ta)
908{ 959{
909 SV *current, *prev, *next; 960 SV *prev, *next;
910
911 LOCK;
912
913 current = GvSV (coro_current);
914 961
915 for (;;) 962 for (;;)
916 { 963 {
917 LOCK; 964 LOCK;
918
919 next = coro_deq (aTHX_ PRIO_MIN); 965 next = coro_deq (PRIO_MIN);
966 UNLOCK;
920 967
921 if (next) 968 if (next)
922 break; 969 break;
923 970
924 UNLOCK;
925
926 { 971 {
927 dSP; 972 dSP;
928 973
929 ENTER; 974 ENTER;
930 SAVETMPS; 975 SAVETMPS;
931 976
932 PUSHMARK (SP); 977 PUSHMARK (SP);
933 PUTBACK; 978 PUTBACK;
934 call_sv (GvSV (coro_idle), G_DISCARD); 979 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
935 980
936 FREETMPS; 981 FREETMPS;
937 LEAVE; 982 LEAVE;
938 } 983 }
939 } 984 }
940 985
941 prev = SvRV (current); 986 prev = SvRV (coro_current);
942 SvRV (current) = next; 987 SvRV_set (coro_current, next);
943 988
944 /* free this only after the transfer */ 989 /* free this only after the transfer */
990 LOCK;
945 free_coro_mortal (); 991 free_coro_mortal ();
992 UNLOCK;
946 coro_mortal = prev; 993 coro_mortal = prev;
947 994
995 assert (!SvROK(prev));//D
996 assert (!SvROK(next));//D
997
948 ta->prev = SvSTATE (prev); 998 ta->prev = SvSTATE (prev);
949 ta->next = SvSTATE (next); 999 ta->next = SvSTATE (next);
950 ta->flags = TRANSFER_SAVE_ALL; 1000 ta->flags = TRANSFER_SAVE_ALL;
951 1001
952 UNLOCK; 1002 assert (ta->flags & CF_READY);
1003 ta->next->flags &= ~CF_READY;
953} 1004}
954 1005
955static void 1006static void
956prepare_cede (aTHX_ struct transfer_args *ta) 1007prepare_cede (struct transfer_args *ta)
957{ 1008{
958 LOCK; 1009 api_ready (coro_current);
959 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current))));
960 UNLOCK;
961 1010
962 prepare_schedule (ta); 1011 prepare_schedule (ta);
963} 1012}
964 1013
965static void 1014static void
966api_schedule (void) 1015api_schedule (void)
967{ 1016{
968 dTHX;
969 struct transfer_args ta; 1017 struct transfer_args ta;
970 1018
971 prepare_schedule (&ta); 1019 prepare_schedule (&ta);
972 TRANSFER (ta); 1020 TRANSFER (ta);
973} 1021}
974 1022
975static void 1023static void
976api_cede (void) 1024api_cede (void)
977{ 1025{
978 dTHX;
979 struct transfer_args ta; 1026 struct transfer_args ta;
980 1027
981 prepare_cede (&ta); 1028 prepare_cede (&ta);
982 TRANSFER (ta); 1029 TRANSFER (ta);
983} 1030}
1002 main_mainstack = PL_mainstack; 1049 main_mainstack = PL_mainstack;
1003 1050
1004 coroapi.ver = CORO_API_VERSION; 1051 coroapi.ver = CORO_API_VERSION;
1005 coroapi.transfer = api_transfer; 1052 coroapi.transfer = api_transfer;
1006 1053
1007 Newz (0, main_stack, 1, coro_stack);
1008 main_stack->idle_sp = (void *)-1;
1009
1010 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1054 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1011} 1055}
1012 1056
1013SV * 1057SV *
1014new (char *klass, ...) 1058new (char *klass, ...)
1018 HV *hv; 1062 HV *hv;
1019 int i; 1063 int i;
1020 1064
1021 Newz (0, coro, 1, struct coro); 1065 Newz (0, coro, 1, struct coro);
1022 coro->args = newAV (); 1066 coro->args = newAV ();
1067 coro->flags = CF_NEW;
1023 1068
1024 hv = newHV (); 1069 hv = newHV ();
1025 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1070 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1026 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1071 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1027 1072
1028 for (i = 1; i < items; i++) 1073 for (i = 1; i < items; i++)
1029 av_push (coro->args, newSVsv (ST (i))); 1074 av_push (coro->args, newSVsv (ST (i)));
1030
1031 coro->stack = main_stack;
1032 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1033 /*coro->stack = 0;*/
1034} 1075}
1035 OUTPUT: 1076 OUTPUT:
1036 RETVAL 1077 RETVAL
1037 1078
1038void 1079void
1039transfer (...) 1080_set_stacklevel (...)
1040 ALIAS: 1081 ALIAS:
1041 Coro::schedule = 1 1082 Coro::State::transfer = 1
1042 Coro::cede = 2 1083 Coro::schedule = 2
1043 _set_stacklevel = 3 1084 Coro::cede = 3
1044 CODE: 1085 CODE:
1045{ 1086{
1046 struct transfer_args ta; 1087 struct transfer_args ta;
1047 1088
1048 switch (ix) 1089 switch (ix)
1049 { 1090 {
1050 case 0: 1091 case 0:
1051 if (items != 3)
1052 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items);
1053
1054 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1055 break;
1056
1057 case 1:
1058 prepare_schedule (&ta);
1059 break;
1060
1061 case 2:
1062 prepare_cede (&ta);
1063 break;
1064
1065 case 3:
1066 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1092 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1067 ta.next = 0; 1093 ta.next = 0;
1068 ta.flags = TRANSFER_SET_STACKLEVEL; 1094 ta.flags = TRANSFER_SET_STACKLEVEL;
1069 break; 1095 break;
1096
1097 case 1:
1098 if (items != 3)
1099 croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items);
1100
1101 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1102 break;
1103
1104 case 2:
1105 prepare_schedule (&ta);
1106 break;
1107
1108 case 3:
1109 prepare_cede (&ta);
1110 break;
1070 } 1111 }
1071 1112
1072 TRANSFER (ta); 1113 TRANSFER (ta);
1073} 1114}
1074 1115
1081 sv_unmagic (SvRV (dst), PERL_MAGIC_ext); 1122 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1082 1123
1083 ++coro_src->refcnt; 1124 ++coro_src->refcnt;
1084 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1125 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1085} 1126}
1086
1087void
1088_nonlocal_goto (IV nextop)
1089 CODE:
1090 /* uuh, somebody will kill me again for this */
1091 PL_op->op_next = INT2PTR (OP *, nextop);
1092 1127
1093void 1128void
1094_exit (code) 1129_exit (code)
1095 int code 1130 int code
1096 PROTOTYPE: $ 1131 PROTOTYPE: $
1097 CODE: 1132 CODE:
1098 _exit (code); 1133 _exit (code);
1099 1134
1100MODULE = Coro::State PACKAGE = Coro::Cont 1135int
1136cctx_count ()
1137 CODE:
1138 RETVAL = cctx_count;
1139 OUTPUT:
1140 RETVAL
1101 1141
1102void 1142int
1103yield (...) 1143cctx_idle ()
1104 PROTOTYPE: @ 1144 CODE:
1105 CODE: 1145 RETVAL = cctx_idle;
1106{ 1146 OUTPUT:
1107 SV *yieldstack; 1147 RETVAL
1108 SV *sv;
1109 AV *defav = GvAV (PL_defgv);
1110 struct coro *prev, *next;
1111
1112 yieldstack = *hv_fetch (
1113 (HV *)SvRV (GvSV (coro_current)),
1114 "yieldstack", sizeof ("yieldstack") - 1,
1115 0
1116 );
1117
1118 /* set up @_ -- ugly */
1119 av_clear (defav);
1120 av_fill (defav, items - 1);
1121 while (items--)
1122 av_store (defav, items, SvREFCNT_inc (ST(items)));
1123
1124 sv = av_pop ((AV *)SvRV (yieldstack));
1125 prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1126 next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1127 SvREFCNT_dec (sv);
1128
1129 coro_state_transfer (aTHX_ prev, next, 0);
1130}
1131 1148
1132MODULE = Coro::State PACKAGE = Coro 1149MODULE = Coro::State PACKAGE = Coro
1133 1150
1134BOOT: 1151BOOT:
1135{ 1152{
1142 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1159 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1143 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1160 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1144 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1161 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1145 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1162 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1146 1163
1147 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1164 coro_current = get_sv ("Coro::current", FALSE);
1148 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1165 SvREADONLY_on (coro_current);
1149 1166
1150 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1167 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1151 coro_ready[i] = newAV (); 1168 coro_ready[i] = newAV ();
1152 1169
1153 { 1170 {
1154 SV *sv = perl_get_sv("Coro::API", 1); 1171 SV *sv = perl_get_sv("Coro::API", 1);
1155 1172
1156 coroapi.schedule = api_schedule; 1173 coroapi.schedule = api_schedule;
1157 coroapi.cede = api_cede; 1174 coroapi.cede = api_cede;
1158 coroapi.ready = api_ready; 1175 coroapi.ready = api_ready;
1176 coroapi.is_ready = api_is_ready;
1159 coroapi.nready = &coro_nready; 1177 coroapi.nready = &coro_nready;
1160 coroapi.current = coro_current; 1178 coroapi.current = coro_current;
1161 1179
1162 GCoroAPI = &coroapi; 1180 GCoroAPI = &coroapi;
1163 sv_setiv (sv, (IV)&coroapi); 1181 sv_setiv (sv, (IV)&coroapi);
1183 1201
1184 coro->prio = newprio; 1202 coro->prio = newprio;
1185 } 1203 }
1186} 1204}
1187 1205
1188void 1206SV *
1189ready (SV *self) 1207ready (SV *self)
1190 PROTOTYPE: $ 1208 PROTOTYPE: $
1191 CODE: 1209 CODE:
1192 api_ready (self); 1210 RETVAL = boolSV (api_ready (self));
1211 OUTPUT:
1212 RETVAL
1213
1214SV *
1215is_ready (SV *self)
1216 PROTOTYPE: $
1217 CODE:
1218 RETVAL = boolSV (api_is_ready (self));
1219 OUTPUT:
1220 RETVAL
1193 1221
1194int 1222int
1195nready (...) 1223nready (...)
1196 PROTOTYPE: 1224 PROTOTYPE:
1197 CODE: 1225 CODE:
1198 RETVAL = coro_nready; 1226 RETVAL = coro_nready;
1199 OUTPUT: 1227 OUTPUT:
1200 RETVAL 1228 RETVAL
1201 1229
1230void
1231_set_current (SV *current)
1232 PROTOTYPE: $
1233 CODE:
1234 SvREFCNT_dec (SvRV (coro_current));
1235 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1236
1202MODULE = Coro::State PACKAGE = Coro::AIO 1237MODULE = Coro::State PACKAGE = Coro::AIO
1203 1238
1204SV * 1239SV *
1205_get_state () 1240_get_state ()
1206 CODE: 1241 CODE:
1207{ 1242{
1208 struct { 1243 struct io_state *data;
1209 int errorno;
1210 int laststype;
1211 int laststatval;
1212 Stat_t statcache;
1213 } data;
1214 1244
1245 RETVAL = newSV (sizeof (struct io_state));
1246 data = (struct io_state *)SvPVX (RETVAL);
1247 SvCUR_set (RETVAL, sizeof (struct io_state));
1248 SvPOK_only (RETVAL);
1249
1215 data.errorno = errno; 1250 data->errorno = errno;
1216 data.laststype = PL_laststype; 1251 data->laststype = PL_laststype;
1217 data.laststatval = PL_laststatval; 1252 data->laststatval = PL_laststatval;
1218 data.statcache = PL_statcache; 1253 data->statcache = PL_statcache;
1219
1220 RETVAL = newSVpvn ((char *)&data, sizeof data);
1221} 1254}
1222 OUTPUT: 1255 OUTPUT:
1223 RETVAL 1256 RETVAL
1224 1257
1225void 1258void
1226_set_state (char *data_) 1259_set_state (char *data_)
1227 PROTOTYPE: $ 1260 PROTOTYPE: $
1228 CODE: 1261 CODE:
1229{ 1262{
1230 struct { 1263 struct io_state *data = (void *)data_;
1231 int errorno;
1232 int laststype;
1233 int laststatval;
1234 Stat_t statcache;
1235 } *data = (void *)data_;
1236 1264
1237 errno = data->errorno; 1265 errno = data->errorno;
1238 PL_laststype = data->laststype; 1266 PL_laststype = data->laststype;
1239 PL_laststatval = data->laststatval; 1267 PL_laststatval = data->laststatval;
1240 PL_statcache = data->statcache; 1268 PL_statcache = data->statcache;
1241} 1269}
1270

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines