1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
|
|
5 | #include "patchlevel.h" |
|
|
6 | |
|
|
7 | #if PATCHLEVEL < 6 |
|
|
8 | # ifndef PL_ppaddr |
|
|
9 | # define PL_ppaddr ppaddr |
|
|
10 | # endif |
|
|
11 | # ifndef call_sv |
|
|
12 | # define call_sv perl_call_sv |
|
|
13 | # endif |
|
|
14 | # ifndef get_sv |
|
|
15 | # define get_sv perl_get_sv |
|
|
16 | # endif |
|
|
17 | # ifndef get_cv |
|
|
18 | # define get_cv perl_get_cv |
|
|
19 | # endif |
|
|
20 | # ifndef IS_PADGV |
|
|
21 | # define IS_PADGV(v) 0 |
|
|
22 | # endif |
|
|
23 | # ifndef IS_PADCONST |
|
|
24 | # define IS_PADCONST(v) 0 |
|
|
25 | # endif |
|
|
26 | #endif |
|
|
27 | |
5 | #include "libcoro/coro.c" |
28 | #include "libcoro/coro.c" |
|
|
29 | |
|
|
30 | #include <signal.h> |
6 | |
31 | |
7 | #ifdef HAVE_MMAP |
32 | #ifdef HAVE_MMAP |
8 | # include <unistd.h> |
33 | # include <unistd.h> |
9 | # include <sys/mman.h> |
34 | # include <sys/mman.h> |
|
|
35 | # ifndef MAP_ANONYMOUS |
|
|
36 | # ifdef MAP_ANON |
|
|
37 | # define MAP_ANONYMOUS MAP_ANON |
|
|
38 | # else |
|
|
39 | # undef HAVE_MMAP |
|
|
40 | # endif |
10 | #endif |
41 | # endif |
|
|
42 | #endif |
11 | |
43 | |
12 | #define MAY_FLUSH /* increases codesize */ |
44 | #define MAY_FLUSH /* increases codesize and is rarely used */ |
13 | |
|
|
14 | /* perl-related */ |
|
|
15 | #define TRANSFER_SAVE_DEFAV 0x00000001 |
|
|
16 | #define TRANSFER_SAVE_DEFSV 0x00000002 |
|
|
17 | #define TRANSFER_SAVE_ERRSV 0x00000004 |
|
|
18 | /* c-related */ |
|
|
19 | #define TRANSFER_SAVE_CCTXT 0x00000008 |
|
|
20 | #ifdef CORO_LAZY_STACK |
|
|
21 | # define TRANSFER_LAZY_STACK 0x00000010 |
|
|
22 | #else |
|
|
23 | # define TRANSFER_LAZY_STACK 0x00000000 |
|
|
24 | #endif |
|
|
25 | |
|
|
26 | #define TRANSFER_SAVE_ALL (TRANSFER_SAVE_DEFAV|TRANSFER_SAVE_DEFSV \ |
|
|
27 | |TRANSFER_SAVE_ERRSV|TRANSFER_SAVE_CCTXT) |
|
|
28 | |
45 | |
29 | #define SUB_INIT "Coro::State::initialize" |
46 | #define SUB_INIT "Coro::State::initialize" |
30 | #define UCORO_STATE "_coro_state" |
47 | #define UCORO_STATE "_coro_state" |
31 | |
48 | |
32 | /* The next macro should delcare a variable stacklevel that contains and approximation |
49 | /* The next macro should declare a variable stacklevel that contains and approximation |
33 | * to the current C stack pointer. It's property is that it changes with each call |
50 | * to the current C stack pointer. Its property is that it changes with each call |
34 | * and should be unique. */ |
51 | * and should be unique. */ |
35 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
52 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
36 | |
53 | |
|
|
54 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
|
|
55 | |
37 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
56 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
|
|
57 | |
|
|
58 | #include "CoroAPI.h" |
|
|
59 | |
|
|
60 | static struct CoroAPI coroapi; |
38 | |
61 | |
39 | /* this is actually not only the c stack but also c registers etc... */ |
62 | /* this is actually not only the c stack but also c registers etc... */ |
40 | typedef struct { |
63 | typedef struct { |
41 | int refcnt; /* pointer reference counter */ |
64 | int refcnt; /* pointer reference counter */ |
42 | int usecnt; /* shared by how many coroutines */ |
65 | int usecnt; /* shared by how many coroutines */ |
… | |
… | |
46 | |
69 | |
47 | void *sptr; |
70 | void *sptr; |
48 | long ssize; /* positive == mmap, otherwise malloc */ |
71 | long ssize; /* positive == mmap, otherwise malloc */ |
49 | } coro_stack; |
72 | } coro_stack; |
50 | |
73 | |
51 | static coro_stack main_stack = { 1, 0, 0 }; |
|
|
52 | |
|
|
53 | struct coro { |
74 | struct coro { |
|
|
75 | /* the top-level JMPENV for each coroutine, needed to catch dies. */ |
|
|
76 | JMPENV start_env; |
|
|
77 | |
54 | /* the optional C context */ |
78 | /* the optional C context */ |
55 | coro_stack *stack; |
79 | coro_stack *stack; |
56 | void *cursp; |
80 | void *cursp; |
57 | int gencnt; |
81 | int gencnt; |
58 | |
82 | |
… | |
… | |
61 | SV *defsv; |
85 | SV *defsv; |
62 | SV *errsv; |
86 | SV *errsv; |
63 | |
87 | |
64 | /* saved global state not related to stacks */ |
88 | /* saved global state not related to stacks */ |
65 | U8 dowarn; |
89 | U8 dowarn; |
|
|
90 | I32 in_eval; |
66 | |
91 | |
67 | /* the stacks and related info (callchain etc..) */ |
92 | /* the stacks and related info (callchain etc..) */ |
68 | PERL_SI *curstackinfo; |
93 | PERL_SI *curstackinfo; |
69 | AV *curstack; |
94 | AV *curstack; |
70 | AV *mainstack; |
95 | AV *mainstack; |
71 | SV **stack_sp; |
96 | SV **stack_sp; |
72 | OP *op; |
97 | OP *op; |
73 | SV **curpad; |
98 | SV **curpad; |
|
|
99 | AV *comppad; |
74 | SV **stack_base; |
100 | SV **stack_base; |
75 | SV **stack_max; |
101 | SV **stack_max; |
76 | SV **tmps_stack; |
102 | SV **tmps_stack; |
77 | I32 tmps_floor; |
103 | I32 tmps_floor; |
78 | I32 tmps_ix; |
104 | I32 tmps_ix; |
… | |
… | |
88 | I32 savestack_max; |
114 | I32 savestack_max; |
89 | OP **retstack; |
115 | OP **retstack; |
90 | I32 retstack_ix; |
116 | I32 retstack_ix; |
91 | I32 retstack_max; |
117 | I32 retstack_max; |
92 | COP *curcop; |
118 | COP *curcop; |
93 | JMPENV start_env; |
|
|
94 | JMPENV *top_env; |
119 | JMPENV *top_env; |
95 | |
120 | |
96 | /* data associated with this coroutine (initial args) */ |
121 | /* data associated with this coroutine (initial args) */ |
97 | AV *args; |
122 | AV *args; |
98 | }; |
123 | }; |
… | |
… | |
103 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
128 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
104 | static HV *coro_state_stash; |
129 | static HV *coro_state_stash; |
105 | static SV *ucoro_state_sv; |
130 | static SV *ucoro_state_sv; |
106 | static U32 ucoro_state_hash; |
131 | static U32 ucoro_state_hash; |
107 | static HV *padlist_cache; |
132 | static HV *padlist_cache; |
|
|
133 | static SV *coro_mortal; /* will be freed after next transfer */ |
108 | |
134 | |
109 | /* mostly copied from op.c:cv_clone2 */ |
135 | /* mostly copied from op.c:cv_clone2 */ |
110 | STATIC AV * |
136 | STATIC AV * |
111 | clone_padlist (AV *protopadlist) |
137 | clone_padlist (AV *protopadlist) |
112 | { |
138 | { |
… | |
… | |
199 | |
225 | |
200 | return newpadlist; |
226 | return newpadlist; |
201 | } |
227 | } |
202 | |
228 | |
203 | #ifdef MAY_FLUSH |
229 | #ifdef MAY_FLUSH |
204 | STATIC AV * |
230 | STATIC void |
205 | free_padlist (AV *padlist) |
231 | free_padlist (AV *padlist) |
206 | { |
232 | { |
207 | /* may be during global destruction */ |
233 | /* may be during global destruction */ |
208 | if (SvREFCNT(padlist)) |
234 | if (SvREFCNT(padlist)) |
209 | { |
235 | { |
… | |
… | |
274 | #endif |
300 | #endif |
275 | |
301 | |
276 | #define SB do { |
302 | #define SB do { |
277 | #define SE } while (0) |
303 | #define SE } while (0) |
278 | |
304 | |
279 | #define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE |
305 | #define LOAD(state) load_state(aTHX_ (state)); |
280 | #define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE |
306 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
281 | |
307 | |
282 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE |
308 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
283 | |
309 | |
284 | static void |
310 | static void |
285 | load_state(pTHX_ Coro__State c) |
311 | load_state(pTHX_ Coro__State c) |
286 | { |
312 | { |
287 | PL_dowarn = c->dowarn; |
313 | PL_dowarn = c->dowarn; |
|
|
314 | PL_in_eval = c->in_eval; |
288 | |
315 | |
289 | PL_curstackinfo = c->curstackinfo; |
316 | PL_curstackinfo = c->curstackinfo; |
290 | PL_curstack = c->curstack; |
317 | PL_curstack = c->curstack; |
291 | PL_mainstack = c->mainstack; |
318 | PL_mainstack = c->mainstack; |
292 | PL_stack_sp = c->stack_sp; |
319 | PL_stack_sp = c->stack_sp; |
293 | PL_op = c->op; |
320 | PL_op = c->op; |
294 | PL_curpad = c->curpad; |
321 | PL_curpad = c->curpad; |
|
|
322 | PL_comppad = c->comppad; |
295 | PL_stack_base = c->stack_base; |
323 | PL_stack_base = c->stack_base; |
296 | PL_stack_max = c->stack_max; |
324 | PL_stack_max = c->stack_max; |
297 | PL_tmps_stack = c->tmps_stack; |
325 | PL_tmps_stack = c->tmps_stack; |
298 | PL_tmps_floor = c->tmps_floor; |
326 | PL_tmps_floor = c->tmps_floor; |
299 | PL_tmps_ix = c->tmps_ix; |
327 | PL_tmps_ix = c->tmps_ix; |
… | |
… | |
309 | PL_savestack_max = c->savestack_max; |
337 | PL_savestack_max = c->savestack_max; |
310 | PL_retstack = c->retstack; |
338 | PL_retstack = c->retstack; |
311 | PL_retstack_ix = c->retstack_ix; |
339 | PL_retstack_ix = c->retstack_ix; |
312 | PL_retstack_max = c->retstack_max; |
340 | PL_retstack_max = c->retstack_max; |
313 | PL_curcop = c->curcop; |
341 | PL_curcop = c->curcop; |
314 | PL_start_env = c->start_env; |
|
|
315 | PL_top_env = c->top_env; |
342 | PL_top_env = c->top_env; |
316 | |
343 | |
317 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
344 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
318 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
345 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
319 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
346 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
… | |
… | |
359 | |
386 | |
360 | PUSHs (Nullsv); |
387 | PUSHs (Nullsv); |
361 | /* this loop was inspired by pp_caller */ |
388 | /* this loop was inspired by pp_caller */ |
362 | for (;;) |
389 | for (;;) |
363 | { |
390 | { |
364 | do |
391 | while (cxix >= 0) |
365 | { |
392 | { |
366 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
393 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
367 | |
394 | |
368 | if (CxTYPE(cx) == CXt_SUB) |
395 | if (CxTYPE(cx) == CXt_SUB) |
369 | { |
396 | { |
… | |
… | |
388 | PUSHs ((SV *)cv); |
415 | PUSHs ((SV *)cv); |
389 | |
416 | |
390 | get_padlist (cv); /* this is a monster */ |
417 | get_padlist (cv); /* this is a monster */ |
391 | } |
418 | } |
392 | } |
419 | } |
|
|
420 | #ifdef CXt_FORMAT |
393 | else if (CxTYPE(cx) == CXt_FORMAT) |
421 | else if (CxTYPE(cx) == CXt_FORMAT) |
394 | { |
422 | { |
395 | /* I never used formats, so how should I know how these are implemented? */ |
423 | /* I never used formats, so how should I know how these are implemented? */ |
396 | /* my bold guess is as a simple, plain sub... */ |
424 | /* my bold guess is as a simple, plain sub... */ |
397 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
425 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
398 | } |
426 | } |
|
|
427 | #endif |
399 | } |
428 | } |
400 | while (cxix >= 0); |
|
|
401 | |
429 | |
402 | if (top_si->si_type == PERLSI_MAIN) |
430 | if (top_si->si_type == PERLSI_MAIN) |
403 | break; |
431 | break; |
404 | |
432 | |
405 | top_si = top_si->si_prev; |
433 | top_si = top_si->si_prev; |
… | |
… | |
412 | |
440 | |
413 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
441 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
414 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
442 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
415 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
443 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
416 | |
444 | |
417 | /* I have not the slightest idea of why av_reify is necessary */ |
|
|
418 | /* but if it's missing the defav contents magically get replaced sometimes */ |
|
|
419 | if (c->defav) |
|
|
420 | av_reify (c->defav); |
|
|
421 | |
|
|
422 | c->dowarn = PL_dowarn; |
445 | c->dowarn = PL_dowarn; |
|
|
446 | c->in_eval = PL_in_eval; |
423 | |
447 | |
424 | c->curstackinfo = PL_curstackinfo; |
448 | c->curstackinfo = PL_curstackinfo; |
425 | c->curstack = PL_curstack; |
449 | c->curstack = PL_curstack; |
426 | c->mainstack = PL_mainstack; |
450 | c->mainstack = PL_mainstack; |
427 | c->stack_sp = PL_stack_sp; |
451 | c->stack_sp = PL_stack_sp; |
428 | c->op = PL_op; |
452 | c->op = PL_op; |
429 | c->curpad = PL_curpad; |
453 | c->curpad = PL_curpad; |
|
|
454 | c->comppad = PL_comppad; |
430 | c->stack_base = PL_stack_base; |
455 | c->stack_base = PL_stack_base; |
431 | c->stack_max = PL_stack_max; |
456 | c->stack_max = PL_stack_max; |
432 | c->tmps_stack = PL_tmps_stack; |
457 | c->tmps_stack = PL_tmps_stack; |
433 | c->tmps_floor = PL_tmps_floor; |
458 | c->tmps_floor = PL_tmps_floor; |
434 | c->tmps_ix = PL_tmps_ix; |
459 | c->tmps_ix = PL_tmps_ix; |
… | |
… | |
444 | c->savestack_max = PL_savestack_max; |
469 | c->savestack_max = PL_savestack_max; |
445 | c->retstack = PL_retstack; |
470 | c->retstack = PL_retstack; |
446 | c->retstack_ix = PL_retstack_ix; |
471 | c->retstack_ix = PL_retstack_ix; |
447 | c->retstack_max = PL_retstack_max; |
472 | c->retstack_max = PL_retstack_max; |
448 | c->curcop = PL_curcop; |
473 | c->curcop = PL_curcop; |
449 | c->start_env = PL_start_env; |
|
|
450 | c->top_env = PL_top_env; |
474 | c->top_env = PL_top_env; |
451 | } |
475 | } |
452 | |
476 | |
453 | /* |
477 | /* |
454 | * allocate various perl stacks. This is an exact copy |
478 | * allocate various perl stacks. This is an exact copy |
… | |
… | |
475 | |
499 | |
476 | New(54,PL_markstack,16,I32); |
500 | New(54,PL_markstack,16,I32); |
477 | PL_markstack_ptr = PL_markstack; |
501 | PL_markstack_ptr = PL_markstack; |
478 | PL_markstack_max = PL_markstack + 16; |
502 | PL_markstack_max = PL_markstack + 16; |
479 | |
503 | |
|
|
504 | #ifdef SET_MARK_OFFSET |
480 | SET_MARK_OFFSET; |
505 | SET_MARK_OFFSET; |
|
|
506 | #endif |
481 | |
507 | |
482 | New(54,PL_scopestack,16,I32); |
508 | New(54,PL_scopestack,16,I32); |
483 | PL_scopestack_ix = 0; |
509 | PL_scopestack_ix = 0; |
484 | PL_scopestack_max = 16; |
510 | PL_scopestack_max = 16; |
485 | |
511 | |
… | |
… | |
492 | PL_retstack_max = 8; |
518 | PL_retstack_max = 8; |
493 | } |
519 | } |
494 | |
520 | |
495 | /* |
521 | /* |
496 | * destroy the stacks, the callchain etc... |
522 | * destroy the stacks, the callchain etc... |
497 | * still there is a memleak of 128 bytes... |
|
|
498 | */ |
523 | */ |
499 | STATIC void |
524 | STATIC void |
500 | destroy_stacks(pTHX) |
525 | destroy_stacks(pTHX) |
501 | { |
526 | { |
|
|
527 | if (!IN_DESTRUCT) |
|
|
528 | { |
502 | /* is this ugly, I ask? */ |
529 | /* is this ugly, I ask? */ |
503 | while (PL_scopestack_ix) |
530 | while (PL_scopestack_ix) |
504 | LEAVE; |
531 | LEAVE; |
505 | |
532 | |
506 | /* sure it is, but more important: is it correct?? :/ */ |
533 | /* sure it is, but more important: is it correct?? :/ */ |
507 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
534 | while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ |
508 | FREETMPS; |
535 | FREETMPS; |
|
|
536 | } |
509 | |
537 | |
510 | while (PL_curstackinfo->si_next) |
538 | while (PL_curstackinfo->si_next) |
511 | PL_curstackinfo = PL_curstackinfo->si_next; |
539 | PL_curstackinfo = PL_curstackinfo->si_next; |
512 | |
540 | |
513 | while (PL_curstackinfo) |
541 | while (PL_curstackinfo) |
… | |
… | |
518 | dSP; |
546 | dSP; |
519 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
547 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
520 | PUTBACK; /* possibly superfluous */ |
548 | PUTBACK; /* possibly superfluous */ |
521 | } |
549 | } |
522 | |
550 | |
|
|
551 | if (!IN_DESTRUCT) |
|
|
552 | { |
523 | dounwind(-1); |
553 | dounwind(-1); |
524 | |
|
|
525 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
554 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
|
|
555 | } |
|
|
556 | |
526 | Safefree(PL_curstackinfo->si_cxstack); |
557 | Safefree(PL_curstackinfo->si_cxstack); |
527 | Safefree(PL_curstackinfo); |
558 | Safefree(PL_curstackinfo); |
528 | PL_curstackinfo = p; |
559 | PL_curstackinfo = p; |
529 | } |
560 | } |
530 | |
561 | |
… | |
… | |
545 | stack->refcnt = 1; |
576 | stack->refcnt = 1; |
546 | stack->usecnt = 1; |
577 | stack->usecnt = 1; |
547 | stack->gencnt = ctx->gencnt = 0; |
578 | stack->gencnt = ctx->gencnt = 0; |
548 | if (alloc) |
579 | if (alloc) |
549 | { |
580 | { |
550 | #ifdef HAVE_MMAP |
581 | #if HAVE_MMAP |
551 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-use */ |
582 | stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ |
552 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); |
583 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
553 | if (stack->sptr == (void *)-1) |
584 | if (stack->sptr == (void *)-1) |
554 | #endif |
585 | #endif |
555 | { |
586 | { |
556 | /*FIXME*//*D*//* reasonable stack size! */ |
587 | /*FIXME*//*D*//* reasonable stack size! */ |
557 | stack->ssize = -4096 * sizeof (long); |
588 | stack->ssize = -4096 * sizeof (long); |
… | |
… | |
577 | { |
608 | { |
578 | #ifdef HAVE_MMAP |
609 | #ifdef HAVE_MMAP |
579 | if (stack->ssize > 0 && stack->sptr) |
610 | if (stack->ssize > 0 && stack->sptr) |
580 | munmap (stack->sptr, stack->ssize); |
611 | munmap (stack->sptr, stack->ssize); |
581 | else |
612 | else |
582 | #else |
613 | #endif |
583 | Safefree (stack->sptr); |
614 | Safefree (stack->sptr); |
584 | #endif |
615 | |
585 | Safefree (stack); |
616 | Safefree (stack); |
586 | } |
617 | } |
587 | else if (ctx->gencnt == stack->gencnt) |
618 | else if (ctx->gencnt == stack->gencnt) |
588 | --stack->usecnt; |
619 | --stack->usecnt; |
589 | } |
620 | } |
… | |
… | |
598 | dSP; |
629 | dSP; |
599 | Coro__State ctx = (Coro__State)arg; |
630 | Coro__State ctx = (Coro__State)arg; |
600 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
631 | SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); |
601 | |
632 | |
602 | coro_init_stacks (aTHX); |
633 | coro_init_stacks (aTHX); |
603 | JMPENV_BOOTSTRAP; |
634 | /*PL_curcop = 0;*/ |
|
|
635 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
|
|
636 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
637 | GvAV (PL_defgv) = ctx->args; ctx->args = 0; |
|
|
638 | |
604 | SPAGAIN; |
639 | SPAGAIN; |
605 | |
|
|
606 | /*PL_curcop = 0;*/ |
|
|
607 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
608 | GvAV (PL_defgv) = ctx->args; |
|
|
609 | |
640 | |
610 | if (ctx->stack) |
641 | if (ctx->stack) |
611 | { |
642 | { |
612 | ctx->cursp = 0; |
643 | ctx->cursp = 0; |
613 | |
644 | |
614 | PUSHMARK(SP); |
645 | PUSHMARK(SP); |
615 | PUTBACK; |
646 | PUTBACK; |
616 | (void) call_sv (sub_init, G_VOID|G_NOARGS); |
647 | (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL); |
|
|
648 | |
|
|
649 | if (SvTRUE (ERRSV)) |
|
|
650 | croak (NULL); |
|
|
651 | else |
617 | croak ("FATAL: CCTXT coroutine returned!"); |
652 | croak ("FATAL: CCTXT coroutine returned!"); |
618 | } |
653 | } |
619 | else |
654 | else |
620 | { |
655 | { |
621 | UNOP myop; |
656 | UNOP myop; |
622 | |
657 | |
… | |
… | |
634 | * that doesn't matter, though, since it is only |
669 | * that doesn't matter, though, since it is only |
635 | * pp_nextstate and we never return... |
670 | * pp_nextstate and we never return... |
636 | * ah yes, and I don't care anyways ;) |
671 | * ah yes, and I don't care anyways ;) |
637 | */ |
672 | */ |
638 | PUTBACK; |
673 | PUTBACK; |
639 | PL_op = pp_entersub(); |
674 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
640 | SPAGAIN; |
675 | SPAGAIN; |
641 | |
676 | |
642 | ENTER; /* necessary e.g. for dounwind */ |
677 | ENTER; /* necessary e.g. for dounwind */ |
643 | } |
678 | } |
644 | } |
679 | } |
… | |
… | |
648 | { |
683 | { |
649 | /* |
684 | /* |
650 | * this is a _very_ stripped down perl interpreter ;) |
685 | * this is a _very_ stripped down perl interpreter ;) |
651 | */ |
686 | */ |
652 | Coro__State ctx = (Coro__State)arg; |
687 | Coro__State ctx = (Coro__State)arg; |
|
|
688 | JMPENV coro_start_env; |
|
|
689 | |
|
|
690 | PL_top_env = &ctx->start_env; |
653 | |
691 | |
654 | ctx->cursp = 0; |
692 | ctx->cursp = 0; |
655 | PL_op = PL_op->op_next; |
693 | PL_op = PL_op->op_next; |
656 | CALLRUNOPS(aTHX); |
694 | CALLRUNOPS(aTHX); |
657 | /*NORETURN*/ |
695 | |
658 | abort (); |
696 | abort (); |
659 | } |
697 | } |
660 | |
698 | |
661 | STATIC void |
699 | STATIC void |
662 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
700 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
663 | { |
701 | { |
664 | dSP; |
|
|
665 | dSTACKLEVEL; |
702 | dSTACKLEVEL; |
|
|
703 | static struct coro *xnext; |
666 | |
704 | |
667 | if (prev != next) |
705 | if (prev != next) |
668 | { |
706 | { |
|
|
707 | xnext = next; |
|
|
708 | |
669 | if (next->mainstack) |
709 | if (next->mainstack) |
670 | { |
710 | { |
671 | SAVE (prev, flags); |
711 | SAVE (prev, flags); |
672 | LOAD (next); |
712 | LOAD (next); |
673 | |
713 | |
… | |
… | |
696 | continue_coro, (void *)next, |
736 | continue_coro, (void *)next, |
697 | next->stack->sptr, labs (next->stack->ssize)); |
737 | next->stack->sptr, labs (next->stack->ssize)); |
698 | } |
738 | } |
699 | |
739 | |
700 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
740 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
741 | /* don't add any code here */ |
701 | } |
742 | } |
702 | |
743 | |
703 | } |
744 | } |
704 | else if (next->tmps_ix == -2) |
745 | else if (next->tmps_ix == -2) |
705 | croak ("tried to transfer to running coroutine"); |
746 | croak ("tried to transfer to running coroutine"); |
… | |
… | |
712 | if (!prev->stack) |
753 | if (!prev->stack) |
713 | allocate_stack (prev, 0); |
754 | allocate_stack (prev, 0); |
714 | |
755 | |
715 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
756 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
716 | { |
757 | { |
|
|
758 | PL_top_env = &next->start_env; |
|
|
759 | |
717 | setup_coro (next); |
760 | setup_coro (next); |
718 | |
761 | |
719 | prev->stack->refcnt++; |
762 | prev->stack->refcnt++; |
720 | prev->stack->usecnt++; |
763 | prev->stack->usecnt++; |
721 | next->stack = prev->stack; |
764 | next->stack = prev->stack; |
722 | next->gencnt = prev->gencnt; |
765 | next->gencnt = prev->gencnt; |
723 | } |
766 | } |
724 | else |
767 | else |
725 | { |
768 | { |
|
|
769 | assert (!next->stack); |
726 | allocate_stack (next, 1); |
770 | allocate_stack (next, 1); |
727 | coro_create (&(next->stack->cctx), |
771 | coro_create (&(next->stack->cctx), |
728 | setup_coro, (void *)next, |
772 | setup_coro, (void *)next, |
729 | next->stack->sptr, labs (next->stack->ssize)); |
773 | next->stack->sptr, labs (next->stack->ssize)); |
730 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
774 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
775 | /* don't add any code here */ |
731 | } |
776 | } |
732 | } |
777 | } |
733 | else |
778 | else |
734 | setup_coro (next); |
779 | setup_coro (next); |
735 | } |
780 | } |
|
|
781 | |
|
|
782 | /* |
|
|
783 | * xnext is now either prev or next, depending on wether |
|
|
784 | * we switched the c stack or not. that's why I use a global |
|
|
785 | * variable, that should become thread-specific at one point. |
|
|
786 | */ |
|
|
787 | xnext->cursp = stacklevel; |
736 | } |
788 | } |
737 | |
789 | |
738 | next->cursp = stacklevel; |
790 | if (coro_mortal) |
|
|
791 | { |
|
|
792 | SvREFCNT_dec (coro_mortal); |
|
|
793 | coro_mortal = 0; |
|
|
794 | } |
|
|
795 | } |
|
|
796 | |
|
|
797 | #define SV_CORO(sv,func) \ |
|
|
798 | do { \ |
|
|
799 | if (SvROK (sv)) \ |
|
|
800 | sv = SvRV (sv); \ |
|
|
801 | \ |
|
|
802 | if (SvTYPE(sv) == SVt_PVHV) \ |
|
|
803 | { \ |
|
|
804 | HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
|
|
805 | \ |
|
|
806 | if (!he) \ |
|
|
807 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
|
|
808 | \ |
|
|
809 | (sv) = SvRV (HeVAL(he)); \ |
|
|
810 | } \ |
|
|
811 | \ |
|
|
812 | /* must also be changed inside Coro::Cont::yield */ \ |
|
|
813 | if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ |
|
|
814 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
|
|
815 | \ |
|
|
816 | } while(0) |
|
|
817 | |
|
|
818 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
|
|
819 | |
|
|
820 | static void |
|
|
821 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
|
|
822 | { |
|
|
823 | SV_CORO (prev, "Coro::transfer"); |
|
|
824 | SV_CORO (next, "Coro::transfer"); |
|
|
825 | |
|
|
826 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
|
|
827 | } |
|
|
828 | |
|
|
829 | /** Coro ********************************************************************/ |
|
|
830 | |
|
|
831 | #define PRIO_MAX 3 |
|
|
832 | #define PRIO_HIGH 1 |
|
|
833 | #define PRIO_NORMAL 0 |
|
|
834 | #define PRIO_LOW -1 |
|
|
835 | #define PRIO_IDLE -3 |
|
|
836 | #define PRIO_MIN -4 |
|
|
837 | |
|
|
838 | /* for Coro.pm */ |
|
|
839 | static GV *coro_current, *coro_idle; |
|
|
840 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
|
|
841 | static int coro_nready; |
|
|
842 | |
|
|
843 | static void |
|
|
844 | coro_enq (SV *sv) |
|
|
845 | { |
|
|
846 | if (SvTYPE (sv) == SVt_PVHV) |
|
|
847 | { |
|
|
848 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
|
|
849 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
850 | |
|
|
851 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
852 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
853 | : prio; |
|
|
854 | |
|
|
855 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
856 | coro_nready++; |
|
|
857 | |
|
|
858 | return; |
|
|
859 | } |
|
|
860 | |
|
|
861 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
862 | } |
|
|
863 | |
|
|
864 | static SV * |
|
|
865 | coro_deq (int min_prio) |
|
|
866 | { |
|
|
867 | int prio = PRIO_MAX - PRIO_MIN; |
|
|
868 | |
|
|
869 | min_prio -= PRIO_MIN; |
|
|
870 | if (min_prio < 0) |
|
|
871 | min_prio = 0; |
|
|
872 | |
|
|
873 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
|
|
874 | if (av_len (coro_ready[prio]) >= 0) |
|
|
875 | { |
|
|
876 | coro_nready--; |
|
|
877 | return av_shift (coro_ready[prio]); |
|
|
878 | } |
|
|
879 | |
|
|
880 | return 0; |
|
|
881 | } |
|
|
882 | |
|
|
883 | static void |
|
|
884 | api_ready (SV *coro) |
|
|
885 | { |
|
|
886 | if (SvROK (coro)) |
|
|
887 | coro = SvRV (coro); |
|
|
888 | |
|
|
889 | coro_enq (SvREFCNT_inc (coro)); |
|
|
890 | } |
|
|
891 | |
|
|
892 | static void |
|
|
893 | api_schedule (void) |
|
|
894 | { |
|
|
895 | SV *prev, *next; |
|
|
896 | |
|
|
897 | prev = SvRV (GvSV (coro_current)); |
|
|
898 | next = coro_deq (PRIO_MIN); |
|
|
899 | |
|
|
900 | if (!next) |
|
|
901 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
|
|
902 | |
|
|
903 | /* free this only after the transfer */ |
|
|
904 | coro_mortal = prev; |
|
|
905 | SV_CORO (prev, "Coro::schedule"); |
|
|
906 | |
|
|
907 | SvRV (GvSV (coro_current)) = next; |
|
|
908 | |
|
|
909 | SV_CORO (next, "Coro::schedule"); |
|
|
910 | |
|
|
911 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
|
|
912 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
|
|
913 | } |
|
|
914 | |
|
|
915 | static void |
|
|
916 | api_cede (void) |
|
|
917 | { |
|
|
918 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
919 | |
|
|
920 | api_schedule (); |
739 | } |
921 | } |
740 | |
922 | |
741 | MODULE = Coro::State PACKAGE = Coro::State |
923 | MODULE = Coro::State PACKAGE = Coro::State |
742 | |
924 | |
743 | PROTOTYPES: ENABLE |
925 | PROTOTYPES: ENABLE |
… | |
… | |
755 | |
937 | |
756 | if (!padlist_cache) |
938 | if (!padlist_cache) |
757 | padlist_cache = newHV (); |
939 | padlist_cache = newHV (); |
758 | |
940 | |
759 | main_mainstack = PL_mainstack; |
941 | main_mainstack = PL_mainstack; |
|
|
942 | |
|
|
943 | coroapi.ver = CORO_API_VERSION; |
|
|
944 | coroapi.transfer = api_transfer; |
760 | } |
945 | } |
761 | |
946 | |
762 | Coro::State |
947 | Coro::State |
763 | _newprocess(args) |
948 | _newprocess(args) |
764 | SV * args |
949 | SV * args |
… | |
… | |
767 | Coro__State coro; |
952 | Coro__State coro; |
768 | |
953 | |
769 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
954 | if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) |
770 | croak ("Coro::State::_newprocess expects an arrayref"); |
955 | croak ("Coro::State::_newprocess expects an arrayref"); |
771 | |
956 | |
772 | New (0, coro, 1, struct coro); |
957 | Newz (0, coro, 1, struct coro); |
773 | |
958 | |
774 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
959 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
775 | coro->mainstack = 0; /* actual work is done inside transfer */ |
960 | coro->mainstack = 0; /* actual work is done inside transfer */ |
776 | coro->stack = 0; |
961 | coro->stack = 0; |
777 | |
962 | |
|
|
963 | /* same as JMPENV_BOOTSTRAP */ |
|
|
964 | /* we might be able to recycle start_env, but safe is safe */ |
|
|
965 | //Zero(&coro->start_env, 1, JMPENV); |
|
|
966 | coro->start_env.je_ret = -1; |
|
|
967 | coro->start_env.je_mustcatch = TRUE; |
|
|
968 | |
778 | RETVAL = coro; |
969 | RETVAL = coro; |
779 | OUTPUT: |
970 | OUTPUT: |
780 | RETVAL |
971 | RETVAL |
781 | |
972 | |
782 | void |
973 | void |
783 | transfer(prev, next, flags = TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK) |
974 | transfer(prev, next, flags) |
784 | Coro::State_or_hashref prev |
975 | SV *prev |
785 | Coro::State_or_hashref next |
976 | SV *next |
786 | int flags |
977 | int flags |
787 | PROTOTYPE: @ |
978 | PROTOTYPE: @ |
788 | CODE: |
979 | CODE: |
|
|
980 | PUTBACK; |
|
|
981 | SV_CORO (next, "Coro::transfer"); |
|
|
982 | SV_CORO (prev, "Coro::transfer"); |
789 | transfer (aTHX_ prev, next, flags); |
983 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
|
|
984 | SPAGAIN; |
790 | |
985 | |
791 | void |
986 | void |
792 | DESTROY(coro) |
987 | DESTROY(coro) |
793 | Coro::State coro |
988 | Coro::State coro |
794 | CODE: |
989 | CODE: |
795 | |
990 | |
796 | if (coro->mainstack && coro->mainstack != main_mainstack) |
991 | if (coro->mainstack && coro->mainstack != main_mainstack) |
797 | { |
992 | { |
798 | struct coro temp; |
993 | struct coro temp; |
799 | |
994 | |
|
|
995 | PUTBACK; |
800 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
996 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
801 | LOAD(aTHX_ coro); |
997 | LOAD(aTHX_ coro); |
|
|
998 | SPAGAIN; |
802 | |
999 | |
803 | destroy_stacks (aTHX); |
1000 | destroy_stacks (aTHX); |
804 | |
1001 | |
805 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
1002 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
|
|
1003 | SPAGAIN; |
806 | |
1004 | |
807 | coro->mainstack = 0; |
1005 | coro->mainstack = 0; |
808 | } |
1006 | } |
809 | |
1007 | |
810 | deallocate_stack (coro); |
1008 | deallocate_stack (coro); |
811 | |
1009 | SvREFCNT_dec (coro->args); |
812 | Safefree (coro); |
1010 | Safefree (coro); |
813 | |
1011 | |
814 | void |
1012 | void |
815 | flush() |
1013 | flush() |
816 | CODE: |
1014 | CODE: |
817 | #ifdef MAY_FLUSH |
1015 | #ifdef MAY_FLUSH |
818 | flush_padlist_cache (); |
1016 | flush_padlist_cache (); |
819 | #endif |
1017 | #endif |
820 | |
1018 | |
|
|
1019 | void |
|
|
1020 | _exit(code) |
|
|
1021 | int code |
|
|
1022 | PROTOTYPE: $ |
|
|
1023 | CODE: |
|
|
1024 | #if defined(__GLIBC__) || _POSIX_C_SOURCE |
|
|
1025 | _exit (code); |
|
|
1026 | #else |
|
|
1027 | signal (SIGTERM, SIG_DFL); |
|
|
1028 | raise (SIGTERM); |
|
|
1029 | exit (code); |
|
|
1030 | #endif |
|
|
1031 | |
821 | MODULE = Coro::State PACKAGE = Coro::Cont |
1032 | MODULE = Coro::State PACKAGE = Coro::Cont |
822 | |
1033 | |
823 | # this is slightly dirty |
1034 | # this is slightly dirty (should expose a c-level api) |
824 | |
1035 | |
825 | void |
1036 | void |
826 | yield(...) |
1037 | yield(...) |
827 | PROTOTYPE: @ |
1038 | PROTOTYPE: @ |
828 | CODE: |
1039 | CODE: |
… | |
… | |
830 | SV *sv; |
1041 | SV *sv; |
831 | AV *defav = GvAV (PL_defgv); |
1042 | AV *defav = GvAV (PL_defgv); |
832 | struct coro *prev, *next; |
1043 | struct coro *prev, *next; |
833 | |
1044 | |
834 | if (!returnstk) |
1045 | if (!returnstk) |
835 | returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); |
1046 | returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); |
836 | |
1047 | |
837 | /* set up @_ -- ugly */ |
1048 | /* set up @_ -- ugly */ |
838 | av_clear (defav); |
1049 | av_clear (defav); |
839 | av_fill (defav, items - 1); |
1050 | av_fill (defav, items - 1); |
840 | while (items--) |
1051 | while (items--) |
… | |
… | |
846 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1057 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
847 | SvREFCNT_dec (sv); |
1058 | SvREFCNT_dec (sv); |
848 | |
1059 | |
849 | transfer(aTHX_ prev, next, 0); |
1060 | transfer(aTHX_ prev, next, 0); |
850 | |
1061 | |
|
|
1062 | MODULE = Coro::State PACKAGE = Coro |
|
|
1063 | |
|
|
1064 | # this is slightly dirty (should expose a c-level api) |
|
|
1065 | |
|
|
1066 | BOOT: |
|
|
1067 | { |
|
|
1068 | int i; |
|
|
1069 | HV *stash = gv_stashpv ("Coro", TRUE); |
|
|
1070 | |
|
|
1071 | newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
|
|
1072 | newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
|
|
1073 | newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
|
|
1074 | newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
|
|
1075 | newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
|
|
1076 | newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
|
|
1077 | |
|
|
1078 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
|
|
1079 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
|
|
1080 | |
|
|
1081 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
|
|
1082 | coro_ready[i] = newAV (); |
|
|
1083 | |
|
|
1084 | { |
|
|
1085 | SV *sv = perl_get_sv("Coro::API", 1); |
|
|
1086 | |
|
|
1087 | coroapi.schedule = api_schedule; |
|
|
1088 | coroapi.cede = api_cede; |
|
|
1089 | coroapi.ready = api_ready; |
|
|
1090 | coroapi.nready = &coro_nready; |
|
|
1091 | coroapi.current = coro_current; |
|
|
1092 | |
|
|
1093 | GCoroAPI = &coroapi; |
|
|
1094 | sv_setiv(sv, (IV)&coroapi); |
|
|
1095 | SvREADONLY_on(sv); |
|
|
1096 | } |
|
|
1097 | } |
|
|
1098 | |
|
|
1099 | void |
|
|
1100 | ready(self) |
|
|
1101 | SV * self |
|
|
1102 | PROTOTYPE: $ |
|
|
1103 | CODE: |
|
|
1104 | api_ready (self); |
|
|
1105 | |
|
|
1106 | int |
|
|
1107 | nready(...) |
|
|
1108 | PROTOTYPE: |
|
|
1109 | CODE: |
|
|
1110 | RETVAL = coro_nready; |
|
|
1111 | OUTPUT: |
|
|
1112 | RETVAL |
|
|
1113 | |
|
|
1114 | void |
|
|
1115 | schedule(...) |
|
|
1116 | PROTOTYPE: |
|
|
1117 | CODE: |
|
|
1118 | api_schedule (); |
|
|
1119 | |
|
|
1120 | void |
|
|
1121 | cede(...) |
|
|
1122 | PROTOTYPE: |
|
|
1123 | CODE: |
|
|
1124 | api_cede (); |
|
|
1125 | |