… | |
… | |
7 | #include "patchlevel.h" |
7 | #include "patchlevel.h" |
8 | |
8 | |
9 | #if USE_VALGRIND |
9 | #if USE_VALGRIND |
10 | # include <valgrind/valgrind.h> |
10 | # include <valgrind/valgrind.h> |
11 | #endif |
11 | #endif |
|
|
12 | |
|
|
13 | /* the maximum number of idle cctx that will be pooled */ |
|
|
14 | #define MAX_IDLE_CCTX 8 |
12 | |
15 | |
13 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
16 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
14 | (PERL_REVISION > (a) \ |
17 | (PERL_REVISION > (a) \ |
15 | || (PERL_REVISION == (a) \ |
18 | || (PERL_REVISION == (a) \ |
16 | && (PERL_VERSION > (b) \ |
19 | && (PERL_VERSION > (b) \ |
… | |
… | |
62 | # ifndef PAGESIZE |
65 | # ifndef PAGESIZE |
63 | # define PAGESIZE pagesize |
66 | # define PAGESIZE pagesize |
64 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
67 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
65 | static long pagesize; |
68 | static long pagesize; |
66 | # else |
69 | # else |
67 | # define BOOT_PAGESIZE |
70 | # define BOOT_PAGESIZE (void)0 |
68 | # endif |
71 | # endif |
|
|
72 | #else |
|
|
73 | # define PAGESIZE 0 |
|
|
74 | # define BOOT_PAGESIZE (void)0 |
69 | #endif |
75 | #endif |
70 | |
76 | |
71 | /* 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 |
72 | * 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 |
73 | * and should be unique. */ |
79 | * and should be unique. */ |
… | |
… | |
100 | static struct CoroAPI coroapi; |
106 | static struct CoroAPI coroapi; |
101 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
107 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
102 | static HV *coro_state_stash, *coro_stash; |
108 | static HV *coro_state_stash, *coro_stash; |
103 | static SV *coro_mortal; /* will be freed after next transfer */ |
109 | static SV *coro_mortal; /* will be freed after next transfer */ |
104 | |
110 | |
|
|
111 | static struct coro_cctx *cctx_first; |
|
|
112 | static int cctx_count, cctx_idle; |
|
|
113 | |
105 | /* this is a structure representing a c-level coroutine */ |
114 | /* this is a structure representing a c-level coroutine */ |
106 | typedef struct coro_stack { |
115 | typedef struct coro_cctx { |
107 | struct coro_stack *next; |
116 | struct coro_cctx *next; |
108 | |
117 | |
109 | /* the stack */ |
118 | /* the stack */ |
110 | void *sptr; |
119 | void *sptr; |
111 | long ssize; /* positive == mmap, otherwise malloc */ |
120 | long ssize; /* positive == mmap, otherwise malloc */ |
112 | |
121 | |
113 | /* cpu state */ |
122 | /* cpu state */ |
114 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
123 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
115 | JMPENV *top_env; |
124 | JMPENV *top_env; |
116 | coro_context cctx; |
125 | coro_context cctx; |
117 | |
126 | |
|
|
127 | int inuse; |
|
|
128 | |
118 | #if USE_VALGRIND |
129 | #if USE_VALGRIND |
119 | int valgrind_id; |
130 | int valgrind_id; |
120 | #endif |
131 | #endif |
121 | } coro_stack; |
132 | } coro_cctx; |
|
|
133 | |
|
|
134 | enum { |
|
|
135 | CF_RUNNING, /* coroutine is running */ |
|
|
136 | CF_READY, /* coroutine is ready */ |
|
|
137 | }; |
122 | |
138 | |
123 | /* this is a structure representing a perl-level coroutine */ |
139 | /* this is a structure representing a perl-level coroutine */ |
124 | struct coro { |
140 | struct coro { |
125 | /* the c coroutine allocated to this perl coroutine, if any */ |
141 | /* the c coroutine allocated to this perl coroutine, if any */ |
126 | coro_stack *stack; |
142 | coro_cctx *cctx; |
127 | |
143 | |
128 | /* data associated with this coroutine (initial args) */ |
144 | /* data associated with this coroutine (initial args) */ |
129 | AV *args; |
145 | AV *args; |
130 | int refcnt; |
146 | int refcnt; |
|
|
147 | int flags; |
131 | |
148 | |
132 | /* optionally saved, might be zero */ |
149 | /* optionally saved, might be zero */ |
133 | AV *defav; |
150 | AV *defav; |
134 | SV *defsv; |
151 | SV *defsv; |
135 | SV *errsv; |
152 | SV *errsv; |
136 | |
153 | |
137 | /* saved global state not related to stacks */ |
154 | #define VAR(name,type) type name; |
138 | U8 dowarn; |
155 | # include "state.h" |
139 | I32 in_eval; |
156 | #undef VAR |
140 | |
|
|
141 | /* the stacks and related info (callchain etc..) */ |
|
|
142 | PERL_SI *curstackinfo; |
|
|
143 | AV *curstack; |
|
|
144 | AV *mainstack; |
|
|
145 | SV **stack_sp; |
|
|
146 | OP *op; |
|
|
147 | SV **curpad; |
|
|
148 | AV *comppad; |
|
|
149 | CV *compcv; |
|
|
150 | SV **stack_base; |
|
|
151 | SV **stack_max; |
|
|
152 | SV **tmps_stack; |
|
|
153 | I32 tmps_floor; |
|
|
154 | I32 tmps_ix; |
|
|
155 | I32 tmps_max; |
|
|
156 | I32 *markstack; |
|
|
157 | I32 *markstack_ptr; |
|
|
158 | I32 *markstack_max; |
|
|
159 | I32 *scopestack; |
|
|
160 | I32 scopestack_ix; |
|
|
161 | I32 scopestack_max; |
|
|
162 | ANY *savestack; |
|
|
163 | I32 savestack_ix; |
|
|
164 | I32 savestack_max; |
|
|
165 | OP **retstack; |
|
|
166 | I32 retstack_ix; |
|
|
167 | I32 retstack_max; |
|
|
168 | PMOP *curpm; |
|
|
169 | COP *curcop; |
|
|
170 | |
157 | |
171 | /* coro process data */ |
158 | /* coro process data */ |
172 | int prio; |
159 | int prio; |
173 | }; |
160 | }; |
174 | |
161 | |
… | |
… | |
238 | |
225 | |
239 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
226 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
240 | |
227 | |
241 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
228 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
242 | |
229 | |
|
|
230 | #define CORO_MAGIC(cv) \ |
|
|
231 | SvMAGIC (cv) \ |
|
|
232 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
|
|
233 | ? SvMAGIC (cv) \ |
|
|
234 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
|
|
235 | : 0 |
|
|
236 | |
243 | /* the next two functions merely cache the padlists */ |
237 | /* the next two functions merely cache the padlists */ |
244 | static void |
238 | static void |
245 | get_padlist (CV *cv) |
239 | get_padlist (CV *cv) |
246 | { |
240 | { |
247 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
241 | MAGIC *mg = CORO_MAGIC (cv); |
|
|
242 | AV *av; |
248 | |
243 | |
249 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
244 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
250 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
245 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
251 | else |
246 | else |
252 | { |
247 | { |
253 | #if 0 |
248 | #if 0 |
254 | /* this is probably cleaner, but also slower? */ |
249 | /* this is probably cleaner, but also slower? */ |
255 | CV *cp = Perl_cv_clone (cv); |
250 | CV *cp = Perl_cv_clone (cv); |
… | |
… | |
263 | } |
258 | } |
264 | |
259 | |
265 | static void |
260 | static void |
266 | put_padlist (CV *cv) |
261 | put_padlist (CV *cv) |
267 | { |
262 | { |
268 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
263 | MAGIC *mg = CORO_MAGIC (cv); |
|
|
264 | AV *av; |
269 | |
265 | |
270 | if (!mg) |
266 | if (!mg) |
271 | { |
267 | { |
272 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
268 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
273 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
269 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
274 | mg->mg_virtual = &vtbl_coro; |
270 | mg->mg_virtual = &vtbl_coro; |
275 | mg->mg_obj = (SV *)newAV (); |
271 | mg->mg_obj = (SV *)newAV (); |
276 | } |
272 | } |
277 | |
273 | |
278 | av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); |
274 | av = (AV *)mg->mg_obj; |
|
|
275 | |
|
|
276 | if (AvFILLp (av) >= AvMAX (av)) |
|
|
277 | av_extend (av, AvMAX (av) + 1); |
|
|
278 | |
|
|
279 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
279 | } |
280 | } |
280 | |
281 | |
281 | #define SB do { |
282 | #define SB do { |
282 | #define SE } while (0) |
283 | #define SE } while (0) |
283 | |
284 | |
… | |
… | |
287 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
288 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
288 | |
289 | |
289 | static void |
290 | static void |
290 | load_state(Coro__State c) |
291 | load_state(Coro__State c) |
291 | { |
292 | { |
292 | PL_dowarn = c->dowarn; |
293 | #define VAR(name,type) PL_ ## name = c->name; |
293 | PL_in_eval = c->in_eval; |
294 | # include "state.h" |
294 | |
295 | #undef VAR |
295 | PL_curstackinfo = c->curstackinfo; |
|
|
296 | PL_curstack = c->curstack; |
|
|
297 | PL_mainstack = c->mainstack; |
|
|
298 | PL_stack_sp = c->stack_sp; |
|
|
299 | PL_op = c->op; |
|
|
300 | PL_curpad = c->curpad; |
|
|
301 | PL_comppad = c->comppad; |
|
|
302 | PL_compcv = c->compcv; |
|
|
303 | PL_stack_base = c->stack_base; |
|
|
304 | PL_stack_max = c->stack_max; |
|
|
305 | PL_tmps_stack = c->tmps_stack; |
|
|
306 | PL_tmps_floor = c->tmps_floor; |
|
|
307 | PL_tmps_ix = c->tmps_ix; |
|
|
308 | PL_tmps_max = c->tmps_max; |
|
|
309 | PL_markstack = c->markstack; |
|
|
310 | PL_markstack_ptr = c->markstack_ptr; |
|
|
311 | PL_markstack_max = c->markstack_max; |
|
|
312 | PL_scopestack = c->scopestack; |
|
|
313 | PL_scopestack_ix = c->scopestack_ix; |
|
|
314 | PL_scopestack_max = c->scopestack_max; |
|
|
315 | PL_savestack = c->savestack; |
|
|
316 | PL_savestack_ix = c->savestack_ix; |
|
|
317 | PL_savestack_max = c->savestack_max; |
|
|
318 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
319 | PL_retstack = c->retstack; |
|
|
320 | PL_retstack_ix = c->retstack_ix; |
|
|
321 | PL_retstack_max = c->retstack_max; |
|
|
322 | #endif |
|
|
323 | PL_curpm = c->curpm; |
|
|
324 | PL_curcop = c->curcop; |
|
|
325 | |
296 | |
326 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
297 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
327 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
298 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
328 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
299 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
329 | |
300 | |
… | |
… | |
332 | CV *cv; |
303 | CV *cv; |
333 | |
304 | |
334 | /* now do the ugly restore mess */ |
305 | /* now do the ugly restore mess */ |
335 | while ((cv = (CV *)POPs)) |
306 | while ((cv = (CV *)POPs)) |
336 | { |
307 | { |
337 | AV *padlist = (AV *)POPs; |
|
|
338 | |
|
|
339 | if (padlist) |
|
|
340 | { |
|
|
341 | put_padlist (cv); /* mark this padlist as available */ |
308 | put_padlist (cv); /* mark this padlist as available */ |
342 | CvPADLIST(cv) = padlist; |
309 | CvDEPTH (cv) = PTR2IV (POPs); |
343 | } |
310 | CvPADLIST (cv) = (AV *)POPs; |
344 | |
|
|
345 | ++CvDEPTH(cv); |
|
|
346 | } |
311 | } |
347 | |
312 | |
348 | PUTBACK; |
313 | PUTBACK; |
349 | } |
314 | } |
350 | } |
315 | } |
… | |
… | |
372 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
337 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
373 | |
338 | |
374 | if (CxTYPE(cx) == CXt_SUB) |
339 | if (CxTYPE(cx) == CXt_SUB) |
375 | { |
340 | { |
376 | CV *cv = cx->blk_sub.cv; |
341 | CV *cv = cx->blk_sub.cv; |
|
|
342 | |
377 | if (CvDEPTH(cv)) |
343 | if (CvDEPTH (cv)) |
378 | { |
344 | { |
379 | EXTEND (SP, CvDEPTH(cv)*2); |
345 | EXTEND (SP, 3); |
380 | |
|
|
381 | while (--CvDEPTH(cv)) |
|
|
382 | { |
|
|
383 | /* this tells the restore code to increment CvDEPTH */ |
|
|
384 | PUSHs (Nullsv); |
|
|
385 | PUSHs ((SV *)cv); |
|
|
386 | } |
|
|
387 | |
346 | |
388 | PUSHs ((SV *)CvPADLIST(cv)); |
347 | PUSHs ((SV *)CvPADLIST(cv)); |
|
|
348 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
389 | PUSHs ((SV *)cv); |
349 | PUSHs ((SV *)cv); |
390 | |
350 | |
|
|
351 | CvDEPTH (cv) = 0; |
391 | get_padlist (cv); |
352 | get_padlist (cv); |
392 | } |
353 | } |
393 | } |
354 | } |
394 | #ifdef CXt_FORMAT |
355 | #ifdef CXt_FORMAT |
395 | else if (CxTYPE(cx) == CXt_FORMAT) |
356 | else if (CxTYPE(cx) == CXt_FORMAT) |
… | |
… | |
414 | |
375 | |
415 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
376 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
416 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
377 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
417 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
378 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
418 | |
379 | |
419 | c->dowarn = PL_dowarn; |
380 | #define VAR(name,type)c->name = PL_ ## name; |
420 | c->in_eval = PL_in_eval; |
381 | # include "state.h" |
421 | |
382 | #undef VAR |
422 | c->curstackinfo = PL_curstackinfo; |
|
|
423 | c->curstack = PL_curstack; |
|
|
424 | c->mainstack = PL_mainstack; |
|
|
425 | c->stack_sp = PL_stack_sp; |
|
|
426 | c->op = PL_op; |
|
|
427 | c->curpad = PL_curpad; |
|
|
428 | c->comppad = PL_comppad; |
|
|
429 | c->compcv = PL_compcv; |
|
|
430 | c->stack_base = PL_stack_base; |
|
|
431 | c->stack_max = PL_stack_max; |
|
|
432 | c->tmps_stack = PL_tmps_stack; |
|
|
433 | c->tmps_floor = PL_tmps_floor; |
|
|
434 | c->tmps_ix = PL_tmps_ix; |
|
|
435 | c->tmps_max = PL_tmps_max; |
|
|
436 | c->markstack = PL_markstack; |
|
|
437 | c->markstack_ptr = PL_markstack_ptr; |
|
|
438 | c->markstack_max = PL_markstack_max; |
|
|
439 | c->scopestack = PL_scopestack; |
|
|
440 | c->scopestack_ix = PL_scopestack_ix; |
|
|
441 | c->scopestack_max = PL_scopestack_max; |
|
|
442 | c->savestack = PL_savestack; |
|
|
443 | c->savestack_ix = PL_savestack_ix; |
|
|
444 | c->savestack_max = PL_savestack_max; |
|
|
445 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
446 | c->retstack = PL_retstack; |
|
|
447 | c->retstack_ix = PL_retstack_ix; |
|
|
448 | c->retstack_max = PL_retstack_max; |
|
|
449 | #endif |
|
|
450 | c->curpm = PL_curpm; |
|
|
451 | c->curcop = PL_curcop; |
|
|
452 | } |
383 | } |
453 | |
384 | |
454 | /* |
385 | /* |
455 | * allocate various perl stacks. This is an exact copy |
386 | * allocate various perl stacks. This is an exact copy |
456 | * of perl.c:init_stacks, except that it uses less memory |
387 | * of perl.c:init_stacks, except that it uses less memory |
… | |
… | |
499 | |
430 | |
500 | /* |
431 | /* |
501 | * destroy the stacks, the callchain etc... |
432 | * destroy the stacks, the callchain etc... |
502 | */ |
433 | */ |
503 | static void |
434 | static void |
504 | coro_destroy_stacks() |
435 | coro_destroy_stacks () |
505 | { |
436 | { |
506 | if (!IN_DESTRUCT) |
437 | if (!IN_DESTRUCT) |
507 | { |
438 | { |
508 | /* is this ugly, I ask? */ |
439 | /* is this ugly, I ask? */ |
509 | LEAVE_SCOPE (0); |
440 | LEAVE_SCOPE (0); |
… | |
… | |
575 | myop.op_flags = OPf_WANT_VOID; |
506 | myop.op_flags = OPf_WANT_VOID; |
576 | |
507 | |
577 | PL_op = (OP *)&myop; |
508 | PL_op = (OP *)&myop; |
578 | |
509 | |
579 | PUSHMARK (SP); |
510 | PUSHMARK (SP); |
580 | XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE)); |
511 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
581 | PUTBACK; |
512 | PUTBACK; |
582 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
513 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
583 | SPAGAIN; |
514 | SPAGAIN; |
584 | |
515 | |
585 | ENTER; /* necessary e.g. for dounwind */ |
516 | ENTER; /* necessary e.g. for dounwind */ |
… | |
… | |
595 | coro_mortal = 0; |
526 | coro_mortal = 0; |
596 | } |
527 | } |
597 | } |
528 | } |
598 | |
529 | |
599 | static void NOINLINE |
530 | static void NOINLINE |
600 | prepare_cctx (coro_stack *cctx) |
531 | prepare_cctx (coro_cctx *cctx) |
601 | { |
532 | { |
602 | dSP; |
533 | dSP; |
603 | LOGOP myop; |
534 | LOGOP myop; |
604 | |
535 | |
605 | Zero (&myop, 1, LOGOP); |
536 | Zero (&myop, 1, LOGOP); |
606 | myop.op_next = PL_op; |
537 | myop.op_next = PL_op; |
607 | myop.op_flags = OPf_WANT_VOID; |
538 | myop.op_flags = OPf_WANT_VOID; |
608 | |
539 | |
609 | sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx)); |
540 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
610 | |
541 | |
611 | PUSHMARK (SP); |
542 | PUSHMARK (SP); |
612 | XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); |
543 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
613 | PUTBACK; |
544 | PUTBACK; |
614 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
545 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
615 | SPAGAIN; |
546 | SPAGAIN; |
616 | } |
547 | } |
617 | |
548 | |
… | |
… | |
623 | |
554 | |
624 | /* |
555 | /* |
625 | * this is a _very_ stripped down perl interpreter ;) |
556 | * this is a _very_ stripped down perl interpreter ;) |
626 | */ |
557 | */ |
627 | PL_top_env = &PL_start_env; |
558 | PL_top_env = &PL_start_env; |
|
|
559 | |
628 | /* inject call to cctx_init */ |
560 | /* inject call to cctx_init */ |
629 | prepare_cctx ((coro_stack *)arg); |
561 | prepare_cctx ((coro_cctx *)arg); |
630 | |
562 | |
631 | /* somebody will hit me for both perl_run and PL_restartop */ |
563 | /* somebody will hit me for both perl_run and PL_restartop */ |
632 | perl_run (PL_curinterp); |
564 | perl_run (PL_curinterp); |
633 | |
565 | |
634 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); |
566 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
635 | abort (); |
567 | abort (); |
636 | } |
568 | } |
637 | |
569 | |
638 | static coro_stack * |
570 | static coro_cctx * |
639 | stack_new () |
571 | cctx_new () |
640 | { |
572 | { |
641 | coro_stack *stack; |
573 | coro_cctx *cctx; |
642 | |
574 | |
|
|
575 | ++cctx_count; |
|
|
576 | |
643 | New (0, stack, 1, coro_stack); |
577 | New (0, cctx, 1, coro_cctx); |
644 | |
578 | |
645 | #if HAVE_MMAP |
579 | #if HAVE_MMAP |
646 | |
580 | |
647 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
581 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
648 | /* mmap suppsedly does allocate-on-write for us */ |
582 | /* mmap suppsedly does allocate-on-write for us */ |
649 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
583 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
650 | |
584 | |
651 | if (stack->sptr == (void *)-1) |
585 | if (cctx->sptr == (void *)-1) |
652 | { |
586 | { |
653 | perror ("FATAL: unable to mmap stack for coroutine"); |
587 | perror ("FATAL: unable to mmap stack for coroutine"); |
654 | _exit (EXIT_FAILURE); |
588 | _exit (EXIT_FAILURE); |
655 | } |
589 | } |
656 | |
590 | |
657 | # if STACKGUARD |
591 | # if STACKGUARD |
658 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
592 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
659 | # endif |
593 | # endif |
660 | |
594 | |
661 | #else |
595 | #else |
662 | |
596 | |
663 | stack->ssize = STACKSIZE * (long)sizeof (long); |
597 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
664 | New (0, stack->sptr, STACKSIZE, long); |
598 | New (0, cctx->sptr, STACKSIZE, long); |
665 | |
599 | |
666 | if (!stack->sptr) |
600 | if (!cctx->sptr) |
667 | { |
601 | { |
668 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
602 | perror ("FATAL: unable to malloc stack for coroutine"); |
669 | _exit (EXIT_FAILURE); |
603 | _exit (EXIT_FAILURE); |
670 | } |
604 | } |
671 | |
605 | |
672 | #endif |
606 | #endif |
673 | |
607 | |
674 | #if USE_VALGRIND |
608 | #if USE_VALGRIND |
675 | stack->valgrind_id = VALGRIND_STACK_REGISTER ( |
609 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
676 | STACKGUARD * PAGESIZE + (char *)stack->sptr, |
610 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
677 | stack->ssize + (char *)stack->sptr |
611 | cctx->ssize + (char *)cctx->sptr |
678 | ); |
612 | ); |
679 | #endif |
613 | #endif |
680 | |
614 | |
681 | coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); |
615 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
682 | |
616 | |
683 | return stack; |
617 | return cctx; |
684 | } |
618 | } |
685 | |
619 | |
686 | static void |
620 | static void |
687 | stack_free (coro_stack *stack) |
621 | cctx_destroy (coro_cctx *cctx) |
688 | { |
622 | { |
689 | if (!stack) |
623 | if (!cctx) |
690 | return; |
624 | return; |
691 | |
625 | |
|
|
626 | --cctx_count; |
|
|
627 | |
692 | #if USE_VALGRIND |
628 | #if USE_VALGRIND |
693 | VALGRIND_STACK_DEREGISTER (stack->valgrind_id); |
629 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
694 | #endif |
630 | #endif |
695 | |
631 | |
696 | #if HAVE_MMAP |
632 | #if HAVE_MMAP |
697 | munmap (stack->sptr, stack->ssize); |
633 | munmap (cctx->sptr, cctx->ssize); |
698 | #else |
634 | #else |
699 | Safefree (stack->sptr); |
635 | Safefree (cctx->sptr); |
700 | #endif |
636 | #endif |
701 | |
637 | |
702 | Safefree (stack); |
638 | Safefree (cctx); |
703 | } |
639 | } |
704 | |
640 | |
705 | static coro_stack *stack_first; |
|
|
706 | static int cctx_count, cctx_idle; |
|
|
707 | |
|
|
708 | static coro_stack * |
641 | static coro_cctx * |
709 | stack_get () |
642 | cctx_get () |
710 | { |
643 | { |
711 | coro_stack *stack; |
644 | coro_cctx *cctx; |
712 | |
645 | |
713 | if (stack_first) |
646 | if (cctx_first) |
714 | { |
647 | { |
|
|
648 | cctx = cctx_first; |
|
|
649 | cctx_first = cctx->next; |
715 | --cctx_idle; |
650 | --cctx_idle; |
716 | stack = stack_first; |
|
|
717 | stack_first = stack->next; |
|
|
718 | } |
651 | } |
719 | else |
652 | else |
720 | { |
653 | { |
721 | ++cctx_count; |
654 | cctx = cctx_new (); |
722 | stack = stack_new (); |
|
|
723 | PL_op = PL_op->op_next; |
655 | PL_op = PL_op->op_next; |
724 | } |
656 | } |
725 | |
657 | |
726 | return stack; |
658 | return cctx; |
727 | } |
659 | } |
728 | |
660 | |
729 | static void |
661 | static void |
730 | stack_put (coro_stack *stack) |
662 | cctx_put (coro_cctx *cctx) |
731 | { |
663 | { |
|
|
664 | /* free another cctx if overlimit */ |
|
|
665 | if (cctx_idle >= MAX_IDLE_CCTX) |
|
|
666 | { |
|
|
667 | coro_cctx *first = cctx_first; |
|
|
668 | cctx_first = first->next; |
|
|
669 | --cctx_idle; |
|
|
670 | |
|
|
671 | assert (!first->inuse); |
|
|
672 | cctx_destroy (first); |
|
|
673 | } |
|
|
674 | |
732 | ++cctx_idle; |
675 | ++cctx_idle; |
733 | stack->next = stack_first; |
676 | cctx->next = cctx_first; |
734 | stack_first = stack; |
677 | cctx_first = cctx; |
735 | } |
678 | } |
736 | |
679 | |
737 | /* never call directly, always through the coro_state_transfer global variable */ |
680 | /* never call directly, always through the coro_state_transfer global variable */ |
738 | static void NOINLINE |
681 | static void NOINLINE |
739 | transfer (struct coro *prev, struct coro *next, int flags) |
682 | transfer (struct coro *prev, struct coro *next, int flags) |
740 | { |
683 | { |
741 | dSTACKLEVEL; |
684 | dSTACKLEVEL; |
742 | |
685 | |
743 | /* sometimes transfer is only called to set idle_sp */ |
686 | /* sometimes transfer is only called to set idle_sp */ |
744 | if (flags == TRANSFER_SET_STACKLEVEL) |
687 | if (flags == TRANSFER_SET_STACKLEVEL) |
745 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
688 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
746 | else if (prev != next) |
689 | else if (prev != next) |
747 | { |
690 | { |
748 | coro_stack *prev__stack; |
691 | coro_cctx *prev__cctx; |
|
|
692 | |
|
|
693 | if (!prev->cctx) |
|
|
694 | { |
|
|
695 | /* create a new empty context */ |
|
|
696 | Newz (0, prev->cctx, 1, coro_cctx); |
|
|
697 | prev->cctx->inuse = 1; |
|
|
698 | prev->flags |= CF_RUNNING; |
|
|
699 | } |
|
|
700 | |
|
|
701 | if (!prev->flags & CF_RUNNING) |
|
|
702 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
|
|
703 | |
|
|
704 | if (next->flags & CF_RUNNING) |
|
|
705 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
706 | |
|
|
707 | prev->flags &= ~CF_RUNNING; |
|
|
708 | next->flags |= CF_RUNNING; |
749 | |
709 | |
750 | LOCK; |
710 | LOCK; |
751 | |
711 | |
752 | if (next->mainstack) |
712 | if (next->mainstack) |
753 | { |
713 | { |
… | |
… | |
760 | /* need to start coroutine */ |
720 | /* need to start coroutine */ |
761 | /* first get rid of the old state */ |
721 | /* first get rid of the old state */ |
762 | SAVE (prev, -1); |
722 | SAVE (prev, -1); |
763 | /* setup coroutine call */ |
723 | /* setup coroutine call */ |
764 | setup_coro (next); |
724 | setup_coro (next); |
765 | /* need a stack */ |
725 | /* need a new stack */ |
766 | next->stack = 0; |
726 | assert (!next->stack); |
767 | } |
727 | } |
768 | |
728 | |
769 | if (!prev->stack) |
|
|
770 | /* create a new empty context */ |
|
|
771 | Newz (0, prev->stack, 1, coro_stack); |
|
|
772 | |
|
|
773 | prev__stack = prev->stack; |
729 | prev__cctx = prev->cctx; |
774 | |
730 | |
775 | /* possibly "free" the stack */ |
731 | /* possibly "free" the cctx */ |
776 | if (prev__stack->idle_sp == STACKLEVEL) |
732 | if (prev__cctx->idle_sp == STACKLEVEL) |
777 | { |
733 | { |
|
|
734 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
|
|
735 | assert (PL_top_env == prev__cctx->top_env); |
|
|
736 | |
778 | stack_put (prev__stack); |
737 | cctx_put (prev__cctx); |
779 | prev->stack = 0; |
738 | prev->cctx = 0; |
780 | } |
739 | } |
781 | |
740 | |
782 | if (!next->stack) |
741 | if (!next->cctx) |
783 | next->stack = stack_get (); |
742 | next->cctx = cctx_get (); |
784 | |
743 | |
785 | if (prev__stack != next->stack) |
744 | if (prev__cctx != next->cctx) |
786 | { |
745 | { |
|
|
746 | assert ( prev__cctx->inuse); |
|
|
747 | assert (!next->cctx->inuse); |
|
|
748 | |
|
|
749 | prev__cctx->inuse = 0; |
|
|
750 | next->cctx->inuse = 1; |
|
|
751 | |
787 | prev__stack->top_env = PL_top_env; |
752 | prev__cctx->top_env = PL_top_env; |
788 | PL_top_env = next->stack->top_env; |
753 | PL_top_env = next->cctx->top_env; |
789 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
754 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
790 | } |
755 | } |
791 | |
756 | |
792 | free_coro_mortal (); |
757 | free_coro_mortal (); |
793 | |
758 | |
794 | UNLOCK; |
759 | UNLOCK; |
… | |
… | |
807 | coro_state_destroy (struct coro *coro) |
772 | coro_state_destroy (struct coro *coro) |
808 | { |
773 | { |
809 | if (coro->refcnt--) |
774 | if (coro->refcnt--) |
810 | return; |
775 | return; |
811 | |
776 | |
|
|
777 | if (coro->flags & CF_RUNNING) |
|
|
778 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
779 | |
812 | if (coro->mainstack && coro->mainstack != main_mainstack) |
780 | if (coro->mainstack && coro->mainstack != main_mainstack) |
813 | { |
781 | { |
814 | struct coro temp; |
782 | struct coro temp; |
815 | |
783 | |
816 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
784 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
… | |
… | |
821 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
789 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
822 | |
790 | |
823 | coro->mainstack = 0; |
791 | coro->mainstack = 0; |
824 | } |
792 | } |
825 | |
793 | |
826 | stack_free (coro->stack); |
794 | cctx_destroy (coro->cctx); |
827 | SvREFCNT_dec (coro->args); |
795 | SvREFCNT_dec (coro->args); |
828 | Safefree (coro); |
796 | Safefree (coro); |
829 | } |
797 | } |
830 | |
798 | |
831 | static int |
799 | static int |
… | |
… | |
913 | static GV *coro_current, *coro_idle; |
881 | static GV *coro_current, *coro_idle; |
914 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
882 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
915 | static int coro_nready; |
883 | static int coro_nready; |
916 | |
884 | |
917 | static void |
885 | static void |
918 | coro_enq (SV *sv) |
886 | coro_enq (SV *coro_sv) |
919 | { |
887 | { |
920 | int prio; |
|
|
921 | |
|
|
922 | if (SvTYPE (sv) != SVt_PVHV) |
|
|
923 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
924 | |
|
|
925 | prio = SvSTATE (sv)->prio; |
|
|
926 | |
|
|
927 | av_push (coro_ready [prio - PRIO_MIN], sv); |
888 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
928 | coro_nready++; |
889 | coro_nready++; |
929 | } |
890 | } |
930 | |
891 | |
931 | static SV * |
892 | static SV * |
932 | coro_deq (int min_prio) |
893 | coro_deq (int min_prio) |
… | |
… | |
945 | } |
906 | } |
946 | |
907 | |
947 | return 0; |
908 | return 0; |
948 | } |
909 | } |
949 | |
910 | |
950 | static void |
911 | static int |
951 | api_ready (SV *coro) |
912 | api_ready (SV *coro_sv) |
952 | { |
913 | { |
953 | dTHX; |
914 | struct coro *coro; |
954 | |
915 | |
955 | if (SvROK (coro)) |
916 | if (SvROK (coro_sv)) |
956 | coro = SvRV (coro); |
917 | coro_sv = SvRV (coro_sv); |
|
|
918 | |
|
|
919 | coro = SvSTATE (coro_sv); |
|
|
920 | |
|
|
921 | if (coro->flags & CF_READY) |
|
|
922 | return 0; |
|
|
923 | |
|
|
924 | if (coro->flags & CF_RUNNING) |
|
|
925 | croak ("Coro::ready called on currently running coroutine"); |
|
|
926 | |
|
|
927 | coro->flags |= CF_READY; |
957 | |
928 | |
958 | LOCK; |
929 | LOCK; |
959 | coro_enq (SvREFCNT_inc (coro)); |
930 | coro_enq (SvREFCNT_inc (coro_sv)); |
960 | UNLOCK; |
931 | UNLOCK; |
|
|
932 | |
|
|
933 | return 1; |
|
|
934 | } |
|
|
935 | |
|
|
936 | static int |
|
|
937 | api_is_ready (SV *coro_sv) |
|
|
938 | { |
|
|
939 | return !!SvSTATE (coro_sv)->flags & CF_READY; |
961 | } |
940 | } |
962 | |
941 | |
963 | static void |
942 | static void |
964 | prepare_schedule (struct transfer_args *ta) |
943 | prepare_schedule (struct transfer_args *ta) |
965 | { |
944 | { |
… | |
… | |
1001 | coro_mortal = prev; |
980 | coro_mortal = prev; |
1002 | |
981 | |
1003 | ta->prev = SvSTATE (prev); |
982 | ta->prev = SvSTATE (prev); |
1004 | ta->next = SvSTATE (next); |
983 | ta->next = SvSTATE (next); |
1005 | ta->flags = TRANSFER_SAVE_ALL; |
984 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
985 | |
|
|
986 | ta->next->flags &= ~CF_READY; |
1006 | } |
987 | } |
1007 | |
988 | |
1008 | static void |
989 | static void |
1009 | prepare_cede (struct transfer_args *ta) |
990 | prepare_cede (struct transfer_args *ta) |
1010 | { |
991 | { |
1011 | LOCK; |
992 | api_ready (GvSV (coro_current)); |
1012 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
1013 | UNLOCK; |
|
|
1014 | |
993 | |
1015 | prepare_schedule (ta); |
994 | prepare_schedule (ta); |
1016 | } |
995 | } |
1017 | |
996 | |
1018 | static void |
997 | static void |
… | |
… | |
1075 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1054 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1076 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1055 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1077 | |
1056 | |
1078 | for (i = 1; i < items; i++) |
1057 | for (i = 1; i < items; i++) |
1079 | av_push (coro->args, newSVsv (ST (i))); |
1058 | av_push (coro->args, newSVsv (ST (i))); |
1080 | |
|
|
1081 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
|
|
1082 | /*coro->stack = 0;*/ |
|
|
1083 | } |
1059 | } |
1084 | OUTPUT: |
1060 | OUTPUT: |
1085 | RETVAL |
1061 | RETVAL |
1086 | |
1062 | |
1087 | void |
1063 | void |
… | |
… | |
1096 | struct transfer_args ta; |
1072 | struct transfer_args ta; |
1097 | |
1073 | |
1098 | switch (ix) |
1074 | switch (ix) |
1099 | { |
1075 | { |
1100 | case 0: |
1076 | case 0: |
1101 | ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); |
1077 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1102 | ta.next = 0; |
1078 | ta.next = 0; |
1103 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1079 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1104 | break; |
1080 | break; |
1105 | |
1081 | |
1106 | case 1: |
1082 | case 1: |
… | |
… | |
1166 | int code |
1142 | int code |
1167 | PROTOTYPE: $ |
1143 | PROTOTYPE: $ |
1168 | CODE: |
1144 | CODE: |
1169 | _exit (code); |
1145 | _exit (code); |
1170 | |
1146 | |
|
|
1147 | int |
|
|
1148 | cctx_count () |
|
|
1149 | CODE: |
|
|
1150 | RETVAL = cctx_count; |
|
|
1151 | OUTPUT: |
|
|
1152 | RETVAL |
|
|
1153 | |
|
|
1154 | int |
|
|
1155 | cctx_idle () |
|
|
1156 | CODE: |
|
|
1157 | RETVAL = cctx_idle; |
|
|
1158 | OUTPUT: |
|
|
1159 | RETVAL |
|
|
1160 | |
1171 | MODULE = Coro::State PACKAGE = Coro |
1161 | MODULE = Coro::State PACKAGE = Coro |
1172 | |
1162 | |
1173 | BOOT: |
1163 | BOOT: |
1174 | { |
1164 | { |
1175 | int i; |
1165 | int i; |
… | |
… | |
1193 | SV *sv = perl_get_sv("Coro::API", 1); |
1183 | SV *sv = perl_get_sv("Coro::API", 1); |
1194 | |
1184 | |
1195 | coroapi.schedule = api_schedule; |
1185 | coroapi.schedule = api_schedule; |
1196 | coroapi.cede = api_cede; |
1186 | coroapi.cede = api_cede; |
1197 | coroapi.ready = api_ready; |
1187 | coroapi.ready = api_ready; |
|
|
1188 | coroapi.is_ready = api_is_ready; |
1198 | coroapi.nready = &coro_nready; |
1189 | coroapi.nready = &coro_nready; |
1199 | coroapi.current = coro_current; |
1190 | coroapi.current = coro_current; |
1200 | |
1191 | |
1201 | GCoroAPI = &coroapi; |
1192 | GCoroAPI = &coroapi; |
1202 | sv_setiv (sv, (IV)&coroapi); |
1193 | sv_setiv (sv, (IV)&coroapi); |
… | |
… | |
1222 | |
1213 | |
1223 | coro->prio = newprio; |
1214 | coro->prio = newprio; |
1224 | } |
1215 | } |
1225 | } |
1216 | } |
1226 | |
1217 | |
1227 | void |
1218 | SV * |
1228 | ready (SV *self) |
1219 | ready (SV *self) |
1229 | PROTOTYPE: $ |
1220 | PROTOTYPE: $ |
1230 | CODE: |
1221 | CODE: |
1231 | api_ready (self); |
1222 | RETVAL = boolSV (api_ready (self)); |
|
|
1223 | OUTPUT: |
|
|
1224 | RETVAL |
|
|
1225 | |
|
|
1226 | SV * |
|
|
1227 | is_ready (SV *self) |
|
|
1228 | PROTOTYPE: $ |
|
|
1229 | CODE: |
|
|
1230 | RETVAL = boolSV (api_is_ready (self)); |
|
|
1231 | OUTPUT: |
|
|
1232 | RETVAL |
1232 | |
1233 | |
1233 | int |
1234 | int |
1234 | nready (...) |
1235 | nready (...) |
1235 | PROTOTYPE: |
1236 | PROTOTYPE: |
1236 | CODE: |
1237 | CODE: |