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