1 | #define PERL_NO_GET_CONTEXT |
|
|
2 | |
|
|
3 | #include "libcoro/coro.c" |
1 | #include "libcoro/coro.c" |
4 | |
2 | |
5 | #include "EXTERN.h" |
3 | #include "EXTERN.h" |
6 | #include "perl.h" |
4 | #include "perl.h" |
7 | #include "XSUB.h" |
5 | #include "XSUB.h" |
… | |
… | |
27 | # ifndef IS_PADCONST |
25 | # ifndef IS_PADCONST |
28 | # define IS_PADCONST(v) 0 |
26 | # define IS_PADCONST(v) 0 |
29 | # endif |
27 | # endif |
30 | #endif |
28 | #endif |
31 | |
29 | |
|
|
30 | #include <stdio.h> |
32 | #include <errno.h> |
31 | #include <errno.h> |
33 | |
32 | |
34 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
33 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
35 | # undef STACKGUARD |
34 | # undef STACKGUARD |
36 | #endif |
35 | #endif |
… | |
… | |
57 | # else |
56 | # else |
58 | # define BOOT_PAGESIZE |
57 | # define BOOT_PAGESIZE |
59 | # endif |
58 | # endif |
60 | #endif |
59 | #endif |
61 | |
60 | |
62 | #define SUB_INIT "Coro::State::initialize" |
|
|
63 | |
|
|
64 | /* The next macro should declare a variable stacklevel that contains and approximation |
61 | /* The next macro should declare a variable stacklevel that contains and approximation |
65 | * to the current C stack pointer. Its property is that it changes with each call |
62 | * to the current C stack pointer. Its property is that it changes with each call |
66 | * and should be unique. */ |
63 | * and should be unique. */ |
|
|
64 | #define dSTACKLEVEL int stacklevel |
67 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
65 | #define STACKLEVEL ((void *)&stacklevel) |
68 | |
66 | |
69 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
67 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
70 | |
68 | |
71 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
|
|
72 | |
|
|
73 | #include "CoroAPI.h" |
69 | #include "CoroAPI.h" |
|
|
70 | |
|
|
71 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
74 | |
72 | |
75 | #ifdef USE_ITHREADS |
73 | #ifdef USE_ITHREADS |
76 | static perl_mutex coro_mutex; |
74 | static perl_mutex coro_mutex; |
77 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
75 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
78 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
76 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
… | |
… | |
84 | static struct CoroAPI coroapi; |
82 | static struct CoroAPI coroapi; |
85 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
83 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
86 | static HV *coro_state_stash, *coro_stash; |
84 | static HV *coro_state_stash, *coro_stash; |
87 | static SV *coro_mortal; /* will be freed after next transfer */ |
85 | static SV *coro_mortal; /* will be freed after next transfer */ |
88 | |
86 | |
89 | /* this is actually not just the c stack but also c registers etc... */ |
87 | /* this is a structure representing a c-level coroutine */ |
90 | typedef struct coro_stack { |
88 | typedef struct coro_stack { |
91 | struct coro_stack *next; |
89 | struct coro_stack *next; |
92 | |
90 | |
93 | void *idle_sp; |
91 | /* the stack */ |
94 | void *sptr; |
92 | void *sptr; |
95 | long ssize; /* positive == mmap, otherwise malloc */ |
93 | long ssize; /* positive == mmap, otherwise malloc */ |
96 | |
94 | |
97 | /* cpu state */ |
95 | /* cpu state */ |
|
|
96 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
97 | JMPENV *top_env; |
98 | coro_context cctx; |
98 | coro_context cctx; |
99 | } coro_stack; |
99 | } coro_stack; |
100 | |
100 | |
101 | static coro_stack *main_stack; |
101 | /* this is a structure representing a perl-level coroutine */ |
102 | |
|
|
103 | struct coro { |
102 | struct coro { |
|
|
103 | /* the c coroutine allocated to this perl coroutine, if any */ |
|
|
104 | coro_stack *stack; |
|
|
105 | |
|
|
106 | /* data associated with this coroutine (initial args) */ |
|
|
107 | AV *args; |
|
|
108 | int refcnt; |
|
|
109 | |
104 | /* optionally saved, might be zero */ |
110 | /* optionally saved, might be zero */ |
105 | AV *defav; |
111 | AV *defav; |
106 | SV *defsv; |
112 | SV *defsv; |
107 | SV *errsv; |
113 | SV *errsv; |
108 | |
114 | |
109 | /* saved global state not related to stacks */ |
115 | /* saved global state not related to stacks */ |
110 | U8 dowarn; |
116 | U8 dowarn; |
111 | I32 in_eval; |
117 | I32 in_eval; |
112 | |
|
|
113 | /* the c stack, if any */ |
|
|
114 | coro_stack *stack; |
|
|
115 | |
118 | |
116 | /* the stacks and related info (callchain etc..) */ |
119 | /* the stacks and related info (callchain etc..) */ |
117 | PERL_SI *curstackinfo; |
120 | PERL_SI *curstackinfo; |
118 | AV *curstack; |
121 | AV *curstack; |
119 | AV *mainstack; |
122 | AV *mainstack; |
… | |
… | |
140 | OP **retstack; |
143 | OP **retstack; |
141 | I32 retstack_ix; |
144 | I32 retstack_ix; |
142 | I32 retstack_max; |
145 | I32 retstack_max; |
143 | PMOP *curpm; |
146 | PMOP *curpm; |
144 | COP *curcop; |
147 | COP *curcop; |
145 | JMPENV *top_env; |
|
|
146 | |
148 | |
147 | /* coro process data */ |
149 | /* coro process data */ |
148 | int prio; |
150 | int prio; |
149 | |
|
|
150 | /* data associated with this coroutine (initial args) */ |
|
|
151 | AV *args; |
|
|
152 | int refcnt; |
|
|
153 | }; |
151 | }; |
154 | |
152 | |
155 | typedef struct coro *Coro__State; |
153 | typedef struct coro *Coro__State; |
156 | typedef struct coro *Coro__State_or_hashref; |
154 | typedef struct coro *Coro__State_or_hashref; |
157 | |
155 | |
158 | static AV * |
156 | static AV * |
159 | coro_clone_padlist (pTHX_ CV *cv) |
157 | coro_clone_padlist (CV *cv) |
160 | { |
158 | { |
161 | AV *padlist = CvPADLIST (cv); |
159 | AV *padlist = CvPADLIST (cv); |
162 | AV *newpadlist, *newpad; |
160 | AV *newpadlist, *newpad; |
163 | |
161 | |
164 | newpadlist = newAV (); |
162 | newpadlist = newAV (); |
… | |
… | |
176 | |
174 | |
177 | return newpadlist; |
175 | return newpadlist; |
178 | } |
176 | } |
179 | |
177 | |
180 | static void |
178 | static void |
181 | free_padlist (pTHX_ AV *padlist) |
179 | free_padlist (AV *padlist) |
182 | { |
180 | { |
183 | /* may be during global destruction */ |
181 | /* may be during global destruction */ |
184 | if (SvREFCNT (padlist)) |
182 | if (SvREFCNT (padlist)) |
185 | { |
183 | { |
186 | I32 i = AvFILLp (padlist); |
184 | I32 i = AvFILLp (padlist); |
… | |
… | |
207 | AV *padlist; |
205 | AV *padlist; |
208 | AV *av = (AV *)mg->mg_obj; |
206 | AV *av = (AV *)mg->mg_obj; |
209 | |
207 | |
210 | /* casting is fun. */ |
208 | /* casting is fun. */ |
211 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
209 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
212 | free_padlist (aTHX_ padlist); |
210 | free_padlist (padlist); |
213 | |
211 | |
214 | SvREFCNT_dec (av); |
212 | SvREFCNT_dec (av); |
215 | |
213 | |
216 | return 0; |
214 | return 0; |
217 | } |
215 | } |
… | |
… | |
220 | |
218 | |
221 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
219 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
222 | |
220 | |
223 | /* the next two functions merely cache the padlists */ |
221 | /* the next two functions merely cache the padlists */ |
224 | static void |
222 | static void |
225 | get_padlist (pTHX_ CV *cv) |
223 | get_padlist (CV *cv) |
226 | { |
224 | { |
227 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
225 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
228 | |
226 | |
229 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
227 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
230 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
228 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
231 | else |
229 | else |
232 | { |
230 | { |
233 | #if 0 |
231 | #if 0 |
234 | /* this should work - but it doesn't :( */ |
232 | /* this is probably cleaner, but also slower? */ |
235 | CV *cp = Perl_cv_clone (aTHX_ cv); |
233 | CV *cp = Perl_cv_clone (cv); |
236 | CvPADLIST (cv) = CvPADLIST (cp); |
234 | CvPADLIST (cv) = CvPADLIST (cp); |
237 | CvPADLIST (cp) = 0; |
235 | CvPADLIST (cp) = 0; |
238 | SvREFCNT_dec (cp); |
236 | SvREFCNT_dec (cp); |
239 | #else |
237 | #else |
240 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
238 | CvPADLIST (cv) = coro_clone_padlist (cv); |
241 | #endif |
239 | #endif |
242 | } |
240 | } |
243 | } |
241 | } |
244 | |
242 | |
245 | static void |
243 | static void |
246 | put_padlist (pTHX_ CV *cv) |
244 | put_padlist (CV *cv) |
247 | { |
245 | { |
248 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
246 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
249 | |
247 | |
250 | if (!mg) |
248 | if (!mg) |
251 | { |
249 | { |
… | |
… | |
259 | } |
257 | } |
260 | |
258 | |
261 | #define SB do { |
259 | #define SB do { |
262 | #define SE } while (0) |
260 | #define SE } while (0) |
263 | |
261 | |
264 | #define LOAD(state) load_state(aTHX_ (state)); |
262 | #define LOAD(state) load_state((state)); |
265 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
263 | #define SAVE(state,flags) save_state((state),(flags)); |
266 | |
264 | |
267 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
265 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
268 | |
266 | |
269 | static void |
267 | static void |
270 | load_state(pTHX_ Coro__State c) |
268 | load_state(Coro__State c) |
271 | { |
269 | { |
272 | PL_dowarn = c->dowarn; |
270 | PL_dowarn = c->dowarn; |
273 | PL_in_eval = c->in_eval; |
271 | PL_in_eval = c->in_eval; |
274 | |
272 | |
275 | PL_curstackinfo = c->curstackinfo; |
273 | PL_curstackinfo = c->curstackinfo; |
… | |
… | |
300 | PL_retstack_ix = c->retstack_ix; |
298 | PL_retstack_ix = c->retstack_ix; |
301 | PL_retstack_max = c->retstack_max; |
299 | PL_retstack_max = c->retstack_max; |
302 | #endif |
300 | #endif |
303 | PL_curpm = c->curpm; |
301 | PL_curpm = c->curpm; |
304 | PL_curcop = c->curcop; |
302 | PL_curcop = c->curcop; |
305 | PL_top_env = c->top_env; |
|
|
306 | |
303 | |
307 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
304 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
308 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
305 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
309 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
306 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
310 | |
307 | |
… | |
… | |
317 | { |
314 | { |
318 | AV *padlist = (AV *)POPs; |
315 | AV *padlist = (AV *)POPs; |
319 | |
316 | |
320 | if (padlist) |
317 | if (padlist) |
321 | { |
318 | { |
322 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
319 | put_padlist (cv); /* mark this padlist as available */ |
323 | CvPADLIST(cv) = padlist; |
320 | CvPADLIST(cv) = padlist; |
324 | } |
321 | } |
325 | |
322 | |
326 | ++CvDEPTH(cv); |
323 | ++CvDEPTH(cv); |
327 | } |
324 | } |
… | |
… | |
329 | PUTBACK; |
326 | PUTBACK; |
330 | } |
327 | } |
331 | } |
328 | } |
332 | |
329 | |
333 | static void |
330 | static void |
334 | save_state(pTHX_ Coro__State c, int flags) |
331 | save_state(Coro__State c, int flags) |
335 | { |
332 | { |
336 | { |
333 | { |
337 | dSP; |
334 | dSP; |
338 | I32 cxix = cxstack_ix; |
335 | I32 cxix = cxstack_ix; |
339 | PERL_CONTEXT *ccstk = cxstack; |
336 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
367 | } |
364 | } |
368 | |
365 | |
369 | PUSHs ((SV *)CvPADLIST(cv)); |
366 | PUSHs ((SV *)CvPADLIST(cv)); |
370 | PUSHs ((SV *)cv); |
367 | PUSHs ((SV *)cv); |
371 | |
368 | |
372 | get_padlist (aTHX_ cv); |
369 | get_padlist (cv); |
373 | } |
370 | } |
374 | } |
371 | } |
375 | #ifdef CXt_FORMAT |
372 | #ifdef CXt_FORMAT |
376 | else if (CxTYPE(cx) == CXt_FORMAT) |
373 | else if (CxTYPE(cx) == CXt_FORMAT) |
377 | { |
374 | { |
… | |
… | |
428 | c->retstack_ix = PL_retstack_ix; |
425 | c->retstack_ix = PL_retstack_ix; |
429 | c->retstack_max = PL_retstack_max; |
426 | c->retstack_max = PL_retstack_max; |
430 | #endif |
427 | #endif |
431 | c->curpm = PL_curpm; |
428 | c->curpm = PL_curpm; |
432 | c->curcop = PL_curcop; |
429 | c->curcop = PL_curcop; |
433 | c->top_env = PL_top_env; |
|
|
434 | } |
430 | } |
435 | |
431 | |
436 | /* |
432 | /* |
437 | * allocate various perl stacks. This is an exact copy |
433 | * allocate various perl stacks. This is an exact copy |
438 | * of perl.c:init_stacks, except that it uses less memory |
434 | * of perl.c:init_stacks, except that it uses less memory |
439 | * on the (sometimes correct) assumption that coroutines do |
435 | * on the (sometimes correct) assumption that coroutines do |
440 | * not usually need a lot of stackspace. |
436 | * not usually need a lot of stackspace. |
441 | */ |
437 | */ |
442 | static void |
438 | static void |
443 | coro_init_stacks (pTHX) |
439 | coro_init_stacks () |
444 | { |
440 | { |
445 | LOCK; |
|
|
446 | |
|
|
447 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
441 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
448 | PL_curstackinfo->si_type = PERLSI_MAIN; |
442 | PL_curstackinfo->si_type = PERLSI_MAIN; |
449 | PL_curstack = PL_curstackinfo->si_stack; |
443 | PL_curstack = PL_curstackinfo->si_stack; |
450 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
444 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
451 | |
445 | |
… | |
… | |
477 | #if PERL_VERSION < 9 |
471 | #if PERL_VERSION < 9 |
478 | New(54,PL_retstack,8,OP*); |
472 | New(54,PL_retstack,8,OP*); |
479 | PL_retstack_ix = 0; |
473 | PL_retstack_ix = 0; |
480 | PL_retstack_max = 8; |
474 | PL_retstack_max = 8; |
481 | #endif |
475 | #endif |
482 | |
|
|
483 | UNLOCK; |
|
|
484 | } |
476 | } |
485 | |
477 | |
486 | /* |
478 | /* |
487 | * destroy the stacks, the callchain etc... |
479 | * destroy the stacks, the callchain etc... |
488 | */ |
480 | */ |
489 | static void |
481 | static void |
490 | destroy_stacks(pTHX) |
482 | destroy_stacks() |
491 | { |
483 | { |
492 | if (!IN_DESTRUCT) |
484 | if (!IN_DESTRUCT) |
493 | { |
485 | { |
494 | /* is this ugly, I ask? */ |
486 | /* is this ugly, I ask? */ |
495 | LEAVE_SCOPE (0); |
487 | LEAVE_SCOPE (0); |
… | |
… | |
540 | * emulate part of the perl startup here. |
532 | * emulate part of the perl startup here. |
541 | */ |
533 | */ |
542 | dTHX; |
534 | dTHX; |
543 | dSP; |
535 | dSP; |
544 | UNOP myop; |
536 | UNOP myop; |
545 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
537 | SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); |
546 | |
538 | |
547 | coro_init_stacks (aTHX); |
539 | coro_init_stacks (); |
548 | /*PL_curcop = 0;*/ |
540 | /*PL_curcop = 0;*/ |
549 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
541 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
550 | SvREFCNT_dec (GvAV (PL_defgv)); |
542 | SvREFCNT_dec (GvAV (PL_defgv)); |
551 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
543 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
552 | |
544 | |
… | |
… | |
566 | |
558 | |
567 | ENTER; /* necessary e.g. for dounwind */ |
559 | ENTER; /* necessary e.g. for dounwind */ |
568 | } |
560 | } |
569 | |
561 | |
570 | static void |
562 | static void |
571 | transfer_tail () |
563 | free_coro_mortal () |
572 | { |
564 | { |
573 | if (coro_mortal) |
565 | if (coro_mortal) |
574 | { |
566 | { |
575 | SvREFCNT_dec (coro_mortal); |
567 | SvREFCNT_dec (coro_mortal); |
576 | coro_mortal = 0; |
568 | coro_mortal = 0; |
577 | } |
569 | } |
|
|
570 | } |
578 | |
571 | |
579 | UNLOCK; |
572 | static void |
|
|
573 | prepare_cctx (coro_stack *cctx) |
|
|
574 | { |
|
|
575 | dSP; |
|
|
576 | UNOP myop; |
|
|
577 | |
|
|
578 | Zero (&myop, 1, UNOP); |
|
|
579 | myop.op_next = PL_op; |
|
|
580 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
|
|
581 | |
|
|
582 | PUSHMARK(SP); |
|
|
583 | EXTEND (SP, 2); |
|
|
584 | PUSHs (newSViv (PTR2IV (cctx))); |
|
|
585 | PUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); |
|
|
586 | PUTBACK; |
|
|
587 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
588 | SPAGAIN; |
580 | } |
589 | } |
581 | |
590 | |
582 | static void |
591 | static void |
583 | coro_run (void *arg) |
592 | coro_run (void *arg) |
584 | { |
593 | { |
585 | /* |
594 | /* |
586 | * this is a _very_ stripped down perl interpreter ;) |
595 | * this is a _very_ stripped down perl interpreter ;) |
587 | */ |
596 | */ |
588 | dTHX; |
597 | UNLOCK; |
589 | |
|
|
590 | transfer_tail (); |
|
|
591 | |
598 | |
592 | PL_top_env = &PL_start_env; |
599 | PL_top_env = &PL_start_env; |
593 | PL_restartop = PL_op->op_next; |
600 | prepare_cctx ((coro_stack *)arg); |
|
|
601 | |
594 | /* somebody will hit me for both perl_run and PL_restart_top */ |
602 | /* somebody will hit me for both perl_run and PL_restartop */ |
595 | perl_run (aTHX_ PERL_GET_CONTEXT); |
603 | perl_run (PERL_GET_CONTEXT); |
596 | |
604 | |
|
|
605 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); |
597 | abort (); |
606 | abort (); |
598 | } |
607 | } |
599 | |
608 | |
600 | static coro_stack * |
609 | static coro_stack * |
601 | stack_new () |
610 | stack_new () |
… | |
… | |
604 | |
613 | |
605 | New (0, stack, 1, coro_stack); |
614 | New (0, stack, 1, coro_stack); |
606 | |
615 | |
607 | #if HAVE_MMAP |
616 | #if HAVE_MMAP |
608 | |
617 | |
609 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ |
618 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
|
|
619 | /* mmap suppsedly does allocate-on-write for us */ |
610 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
620 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
611 | |
621 | |
612 | if (stack->sptr == (void *)-1) |
622 | if (stack->sptr == (void *)-1) |
613 | { |
623 | { |
614 | fprintf (stderr, "FATAL: unable to mmap stack for coroutine\n"); |
624 | perror ("FATAL: unable to mmap stack for coroutine"); |
615 | _exit (EXIT_FAILURE); |
625 | _exit (EXIT_FAILURE); |
616 | } |
626 | } |
617 | |
627 | |
|
|
628 | # if STACKGUARD |
618 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
629 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
|
|
630 | # endif |
619 | |
631 | |
620 | #else |
632 | #else |
621 | |
633 | |
622 | stack->ssize = STACKSIZE * (long)sizeof (long); |
634 | stack->ssize = STACKSIZE * (long)sizeof (long); |
623 | New (0, stack->sptr, STACKSIZE, long); |
635 | New (0, stack->sptr, STACKSIZE, long); |
624 | |
636 | |
625 | if (!stack->sptr) |
637 | if (!stack->sptr) |
626 | { |
638 | { |
627 | fprintf (stderr, "FATAL: unable to malloc stack for coroutine\n"); |
639 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
628 | _exit (EXIT_FAILURE); |
640 | _exit (EXIT_FAILURE); |
629 | } |
641 | } |
630 | |
642 | |
631 | #endif |
643 | #endif |
632 | |
644 | |
633 | coro_create (&stack->cctx, coro_run, 0, stack->sptr, stack->ssize); |
645 | coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); |
634 | |
646 | |
635 | return stack; |
647 | return stack; |
636 | } |
648 | } |
637 | |
649 | |
638 | static void |
650 | static void |
639 | stack_free (coro_stack *stack) |
651 | stack_free (coro_stack *stack) |
640 | { |
652 | { |
641 | if (!stack || stack == main_stack) |
653 | if (!stack) |
642 | return; |
654 | return; |
643 | |
655 | |
644 | #if HAVE_MMAP |
656 | #if HAVE_MMAP |
645 | munmap (stack->sptr, stack->ssize); |
657 | munmap (stack->sptr, stack->ssize); |
646 | #else |
658 | #else |
… | |
… | |
650 | Safefree (stack); |
662 | Safefree (stack); |
651 | } |
663 | } |
652 | |
664 | |
653 | static coro_stack *stack_first; |
665 | static coro_stack *stack_first; |
654 | |
666 | |
655 | static void |
667 | static coro_stack * |
656 | stack_get (struct coro *coro) |
668 | stack_get () |
657 | { |
669 | { |
|
|
670 | coro_stack *stack; |
|
|
671 | |
658 | if (stack_first) |
672 | if (stack_first) |
659 | { |
673 | { |
660 | coro->stack = stack_first; |
674 | stack = stack_first; |
661 | stack_first = stack_first->next; |
675 | stack_first = stack->next; |
662 | } |
676 | } |
663 | else |
677 | else |
|
|
678 | { |
664 | coro->stack = stack_new (); |
679 | stack = stack_new (); |
|
|
680 | PL_op = PL_op->op_next; |
|
|
681 | } |
|
|
682 | |
|
|
683 | return stack; |
665 | } |
684 | } |
666 | |
685 | |
667 | static void |
686 | static void |
668 | stack_put (coro_stack *stack) |
687 | stack_put (coro_stack *stack) |
669 | { |
688 | { |
… | |
… | |
671 | stack_first = stack; |
690 | stack_first = stack; |
672 | } |
691 | } |
673 | |
692 | |
674 | /* never call directly, always through the coro_state_transfer global variable */ |
693 | /* never call directly, always through the coro_state_transfer global variable */ |
675 | static void |
694 | static void |
676 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
695 | transfer_impl (struct coro *prev, struct coro *next, int flags) |
677 | { |
696 | { |
678 | dSTACKLEVEL; |
697 | dSTACKLEVEL; |
679 | |
698 | |
680 | /* sometimes transfer is only called to set idle_sp */ |
699 | /* sometimes transfer is only called to set idle_sp */ |
681 | if (!prev->stack->idle_sp) |
700 | if (flags == TRANSFER_SET_STACKLEVEL) |
682 | prev->stack->idle_sp = stacklevel; |
701 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
683 | |
|
|
684 | LOCK; |
|
|
685 | |
|
|
686 | if (prev != next) |
702 | else if (prev != next) |
687 | { |
703 | { |
688 | coro_stack *prev_stack = prev->stack; |
704 | coro_stack *prev__stack; |
689 | |
705 | |
690 | /* possibly "free" the stack */ |
706 | LOCK; |
691 | if (prev_stack->idle_sp == stacklevel) |
|
|
692 | { |
|
|
693 | stack_put (prev_stack); |
|
|
694 | prev->stack = 0; |
|
|
695 | } |
|
|
696 | |
707 | |
697 | if (next->mainstack) |
708 | if (next->mainstack) |
698 | { |
709 | { |
699 | /* coroutine already started */ |
710 | /* coroutine already started */ |
700 | SAVE (prev, flags); |
711 | SAVE (prev, flags); |
… | |
… | |
705 | /* need to start coroutine */ |
716 | /* need to start coroutine */ |
706 | /* first get rid of the old state */ |
717 | /* first get rid of the old state */ |
707 | SAVE (prev, -1); |
718 | SAVE (prev, -1); |
708 | /* setup coroutine call */ |
719 | /* setup coroutine call */ |
709 | setup_coro (next); |
720 | setup_coro (next); |
710 | /* need to change stack from main_stack to real one */ |
721 | /* need a stack */ |
711 | next->stack = 0; |
722 | next->stack = 0; |
712 | } |
723 | } |
713 | |
724 | |
|
|
725 | if (!prev->stack) |
|
|
726 | /* create a new empty context */ |
|
|
727 | Newz (0, prev->stack, 1, coro_stack); |
|
|
728 | |
|
|
729 | prev__stack = prev->stack; |
|
|
730 | |
|
|
731 | /* possibly "free" the stack */ |
|
|
732 | if (prev__stack->idle_sp == STACKLEVEL) |
|
|
733 | { |
|
|
734 | stack_put (prev__stack); |
|
|
735 | prev->stack = 0; |
|
|
736 | } |
|
|
737 | |
714 | if (!next->stack) |
738 | if (!next->stack) |
715 | stack_get (next); |
739 | next->stack = stack_get (); |
716 | |
740 | |
717 | if (prev_stack != next->stack) |
741 | if (prev__stack != next->stack) |
|
|
742 | { |
|
|
743 | prev__stack->top_env = PL_top_env; |
|
|
744 | PL_top_env = next->stack->top_env; |
718 | coro_transfer (&prev_stack->cctx, &next->stack->cctx); |
745 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
|
|
746 | } |
|
|
747 | |
|
|
748 | free_coro_mortal (); |
|
|
749 | |
|
|
750 | UNLOCK; |
719 | } |
751 | } |
720 | |
|
|
721 | transfer_tail (); |
|
|
722 | } |
752 | } |
723 | |
753 | |
724 | /* use this function pointer to call the above function */ |
754 | /* use this function pointer to call the above function */ |
725 | /* this is done to increase chances of the compiler not inlining the call */ |
755 | /* this is done to increase chances of the compiler not inlining the call */ |
|
|
756 | /* not static to make it even harder for the compiler (and theoretically impossible in most cases */ |
726 | void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; |
757 | void (*coro_state_transfer)(struct coro *prev, struct coro *next, int flags) = transfer_impl; |
|
|
758 | |
|
|
759 | struct transfer_args |
|
|
760 | { |
|
|
761 | struct coro *prev, *next; |
|
|
762 | int flags; |
|
|
763 | }; |
|
|
764 | |
|
|
765 | #define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) |
727 | |
766 | |
728 | static void |
767 | static void |
729 | coro_state_destroy (struct coro *coro) |
768 | coro_state_destroy (struct coro *coro) |
730 | { |
769 | { |
731 | if (coro->refcnt--) |
770 | if (coro->refcnt--) |
… | |
… | |
733 | |
772 | |
734 | if (coro->mainstack && coro->mainstack != main_mainstack) |
773 | if (coro->mainstack && coro->mainstack != main_mainstack) |
735 | { |
774 | { |
736 | struct coro temp; |
775 | struct coro temp; |
737 | |
776 | |
738 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
777 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
739 | LOAD (aTHX_ coro); |
778 | LOAD (coro); |
740 | |
779 | |
741 | destroy_stacks (aTHX); |
780 | destroy_stacks (); |
742 | |
781 | |
743 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
782 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
744 | |
783 | |
745 | coro->mainstack = 0; |
784 | coro->mainstack = 0; |
746 | } |
785 | } |
… | |
… | |
749 | SvREFCNT_dec (coro->args); |
788 | SvREFCNT_dec (coro->args); |
750 | Safefree (coro); |
789 | Safefree (coro); |
751 | } |
790 | } |
752 | |
791 | |
753 | static int |
792 | static int |
754 | coro_state_clear (SV *sv, MAGIC *mg) |
793 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
755 | { |
794 | { |
756 | struct coro *coro = (struct coro *)mg->mg_ptr; |
795 | struct coro *coro = (struct coro *)mg->mg_ptr; |
757 | mg->mg_ptr = 0; |
796 | mg->mg_ptr = 0; |
758 | |
797 | |
759 | coro_state_destroy (coro); |
798 | coro_state_destroy (coro); |
760 | |
799 | |
761 | return 0; |
800 | return 0; |
762 | } |
801 | } |
763 | |
802 | |
764 | static int |
803 | static int |
765 | coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) |
804 | coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) |
766 | { |
805 | { |
767 | struct coro *coro = (struct coro *)mg->mg_ptr; |
806 | struct coro *coro = (struct coro *)mg->mg_ptr; |
768 | |
807 | |
769 | ++coro->refcnt; |
808 | ++coro->refcnt; |
770 | |
809 | |
… | |
… | |
794 | assert (mg->mg_type == PERL_MAGIC_ext); |
833 | assert (mg->mg_type == PERL_MAGIC_ext); |
795 | return (struct coro *)mg->mg_ptr; |
834 | return (struct coro *)mg->mg_ptr; |
796 | } |
835 | } |
797 | |
836 | |
798 | static void |
837 | static void |
|
|
838 | prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) |
|
|
839 | { |
|
|
840 | ta->prev = SvSTATE (prev); |
|
|
841 | ta->next = SvSTATE (next); |
|
|
842 | ta->flags = flags; |
|
|
843 | } |
|
|
844 | |
|
|
845 | static void |
799 | api_transfer (pTHX_ SV *prev, SV *next, int flags) |
846 | api_transfer (SV *prev, SV *next, int flags) |
800 | { |
847 | { |
801 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
848 | dTHX; |
|
|
849 | struct transfer_args ta; |
|
|
850 | |
|
|
851 | prepare_transfer (&ta, prev, next, flags); |
|
|
852 | TRANSFER (ta); |
802 | } |
853 | } |
803 | |
854 | |
804 | /** Coro ********************************************************************/ |
855 | /** Coro ********************************************************************/ |
805 | |
856 | |
806 | #define PRIO_MAX 3 |
857 | #define PRIO_MAX 3 |
… | |
… | |
814 | static GV *coro_current, *coro_idle; |
865 | static GV *coro_current, *coro_idle; |
815 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
866 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
816 | static int coro_nready; |
867 | static int coro_nready; |
817 | |
868 | |
818 | static void |
869 | static void |
819 | coro_enq (pTHX_ SV *sv) |
870 | coro_enq (SV *sv) |
820 | { |
871 | { |
821 | int prio; |
872 | int prio; |
822 | |
873 | |
823 | if (SvTYPE (sv) != SVt_PVHV) |
874 | if (SvTYPE (sv) != SVt_PVHV) |
824 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
875 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
… | |
… | |
828 | av_push (coro_ready [prio - PRIO_MIN], sv); |
879 | av_push (coro_ready [prio - PRIO_MIN], sv); |
829 | coro_nready++; |
880 | coro_nready++; |
830 | } |
881 | } |
831 | |
882 | |
832 | static SV * |
883 | static SV * |
833 | coro_deq (pTHX_ int min_prio) |
884 | coro_deq (int min_prio) |
834 | { |
885 | { |
835 | int prio = PRIO_MAX - PRIO_MIN; |
886 | int prio = PRIO_MAX - PRIO_MIN; |
836 | |
887 | |
837 | min_prio -= PRIO_MIN; |
888 | min_prio -= PRIO_MIN; |
838 | if (min_prio < 0) |
889 | if (min_prio < 0) |
… | |
… | |
855 | |
906 | |
856 | if (SvROK (coro)) |
907 | if (SvROK (coro)) |
857 | coro = SvRV (coro); |
908 | coro = SvRV (coro); |
858 | |
909 | |
859 | LOCK; |
910 | LOCK; |
860 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
911 | coro_enq (SvREFCNT_inc (coro)); |
861 | UNLOCK; |
912 | UNLOCK; |
862 | } |
913 | } |
863 | |
914 | |
864 | static void |
915 | static void |
865 | api_schedule (void) |
916 | prepare_schedule (struct transfer_args *ta) |
866 | { |
917 | { |
867 | dTHX; |
|
|
868 | |
|
|
869 | SV *prev, *next; |
918 | SV *current, *prev, *next; |
|
|
919 | |
870 | SV *current = GvSV (coro_current); |
920 | current = GvSV (coro_current); |
871 | |
921 | |
872 | for (;;) |
922 | for (;;) |
873 | { |
923 | { |
874 | LOCK; |
924 | LOCK; |
875 | |
|
|
876 | next = coro_deq (aTHX_ PRIO_MIN); |
925 | next = coro_deq (PRIO_MIN); |
|
|
926 | UNLOCK; |
877 | |
927 | |
878 | if (next) |
928 | if (next) |
879 | break; |
929 | break; |
880 | |
|
|
881 | UNLOCK; |
|
|
882 | |
930 | |
883 | { |
931 | { |
884 | dSP; |
932 | dSP; |
885 | |
933 | |
886 | ENTER; |
934 | ENTER; |
… | |
… | |
897 | |
945 | |
898 | prev = SvRV (current); |
946 | prev = SvRV (current); |
899 | SvRV (current) = next; |
947 | SvRV (current) = next; |
900 | |
948 | |
901 | /* free this only after the transfer */ |
949 | /* free this only after the transfer */ |
|
|
950 | LOCK; |
|
|
951 | free_coro_mortal (); |
|
|
952 | UNLOCK; |
902 | coro_mortal = prev; |
953 | coro_mortal = prev; |
903 | |
954 | |
|
|
955 | ta->prev = SvSTATE (prev); |
|
|
956 | ta->next = SvSTATE (next); |
|
|
957 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
958 | } |
|
|
959 | |
|
|
960 | static void |
|
|
961 | prepare_cede (struct transfer_args *ta) |
|
|
962 | { |
|
|
963 | LOCK; |
|
|
964 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
904 | UNLOCK; |
965 | UNLOCK; |
905 | |
966 | |
906 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), TRANSFER_SAVE_ALL); |
967 | prepare_schedule (ta); |
907 | } |
968 | } |
908 | |
969 | |
909 | static int coro_cede_self; |
970 | static void |
|
|
971 | api_schedule (void) |
|
|
972 | { |
|
|
973 | dTHX; |
|
|
974 | struct transfer_args ta; |
|
|
975 | |
|
|
976 | prepare_schedule (&ta); |
|
|
977 | TRANSFER (ta); |
|
|
978 | } |
910 | |
979 | |
911 | static void |
980 | static void |
912 | api_cede (void) |
981 | api_cede (void) |
913 | { |
982 | { |
914 | dTHX; |
983 | dTHX; |
915 | SV *current = SvREFCNT_inc (SvRV (GvSV (coro_current))); |
984 | struct transfer_args ta; |
916 | |
985 | |
917 | LOCK; |
986 | prepare_cede (&ta); |
918 | |
987 | TRANSFER (ta); |
919 | if (coro_cede_self) |
|
|
920 | { |
|
|
921 | AV *runqueue = coro_ready [PRIO_MAX - PRIO_MIN]; |
|
|
922 | av_unshift (runqueue, 1); |
|
|
923 | av_store (runqueue, 0, current); |
|
|
924 | coro_nready++; |
|
|
925 | coro_cede_self = 0; |
|
|
926 | } |
|
|
927 | else |
|
|
928 | coro_enq (aTHX_ current); |
|
|
929 | |
|
|
930 | UNLOCK; |
|
|
931 | |
|
|
932 | api_schedule (); |
|
|
933 | } |
988 | } |
934 | |
989 | |
935 | MODULE = Coro::State PACKAGE = Coro::State |
990 | MODULE = Coro::State PACKAGE = Coro::State |
936 | |
991 | |
937 | PROTOTYPES: DISABLE |
992 | PROTOTYPES: DISABLE |
… | |
… | |
952 | main_mainstack = PL_mainstack; |
1007 | main_mainstack = PL_mainstack; |
953 | |
1008 | |
954 | coroapi.ver = CORO_API_VERSION; |
1009 | coroapi.ver = CORO_API_VERSION; |
955 | coroapi.transfer = api_transfer; |
1010 | coroapi.transfer = api_transfer; |
956 | |
1011 | |
957 | Newz (0, main_stack, 1, coro_stack); |
|
|
958 | main_stack->idle_sp = (void *)-1; |
|
|
959 | |
|
|
960 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1012 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
961 | } |
1013 | } |
962 | |
1014 | |
963 | SV * |
1015 | SV * |
964 | new (char *klass, ...) |
1016 | new (char *klass, ...) |
… | |
… | |
973 | |
1025 | |
974 | hv = newHV (); |
1026 | hv = newHV (); |
975 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1027 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
976 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1028 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
977 | |
1029 | |
978 | av_push (coro->args, newSVsv (RETVAL)); |
|
|
979 | for (i = 1; i < items; i++) |
1030 | for (i = 1; i < items; i++) |
980 | av_push (coro->args, newSVsv (ST (i))); |
1031 | av_push (coro->args, newSVsv (ST (i))); |
981 | |
1032 | |
982 | coro->stack = main_stack; |
|
|
983 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
1033 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
984 | /*coro->stack = 0;*/ |
1034 | /*coro->stack = 0;*/ |
985 | } |
1035 | } |
986 | OUTPUT: |
1036 | OUTPUT: |
987 | RETVAL |
1037 | RETVAL |
988 | |
1038 | |
989 | void |
1039 | void |
990 | transfer (prev, next, flags) |
1040 | _set_stacklevel (...) |
991 | SV *prev |
1041 | ALIAS: |
992 | SV *next |
1042 | Coro::State::transfer = 1 |
993 | int flags |
1043 | Coro::schedule = 2 |
|
|
1044 | Coro::cede = 3 |
|
|
1045 | Coro::Cont::yield = 4 |
994 | CODE: |
1046 | CODE: |
995 | PUTBACK; |
|
|
996 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
|
|
997 | SPAGAIN; |
|
|
998 | |
|
|
999 | int |
|
|
1000 | prio (Coro::State coro, int newprio = 0) |
|
|
1001 | ALIAS: |
|
|
1002 | nice = 1 |
|
|
1003 | CODE: |
|
|
1004 | { |
1047 | { |
1005 | RETVAL = coro->prio; |
1048 | struct transfer_args ta; |
1006 | |
1049 | |
1007 | if (items > 1) |
1050 | switch (ix) |
1008 | { |
1051 | { |
|
|
1052 | case 0: |
|
|
1053 | ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); |
|
|
1054 | ta.next = 0; |
|
|
1055 | ta.flags = TRANSFER_SET_STACKLEVEL; |
|
|
1056 | break; |
|
|
1057 | |
|
|
1058 | case 1: |
1009 | if (ix) |
1059 | if (items != 3) |
1010 | newprio += coro->prio; |
1060 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1011 | |
1061 | |
1012 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1062 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1013 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1063 | break; |
1014 | |
1064 | |
1015 | coro->prio = newprio; |
1065 | case 2: |
|
|
1066 | prepare_schedule (&ta); |
|
|
1067 | break; |
|
|
1068 | |
|
|
1069 | case 3: |
|
|
1070 | prepare_cede (&ta); |
|
|
1071 | break; |
|
|
1072 | |
|
|
1073 | case 4: |
|
|
1074 | { |
|
|
1075 | SV *yieldstack; |
|
|
1076 | SV *sv; |
|
|
1077 | AV *defav = GvAV (PL_defgv); |
|
|
1078 | |
|
|
1079 | yieldstack = *hv_fetch ( |
|
|
1080 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1081 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1082 | 0 |
|
|
1083 | ); |
|
|
1084 | |
|
|
1085 | /* set up @_ -- ugly */ |
|
|
1086 | av_clear (defav); |
|
|
1087 | av_fill (defav, items - 1); |
|
|
1088 | while (items--) |
|
|
1089 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1090 | |
|
|
1091 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1092 | ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1093 | ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1094 | ta.flags = 0; |
|
|
1095 | SvREFCNT_dec (sv); |
|
|
1096 | } |
|
|
1097 | break; |
|
|
1098 | |
1016 | } |
1099 | } |
1017 | } |
|
|
1018 | |
1100 | |
1019 | void |
1101 | TRANSFER (ta); |
1020 | _clear_idle_sp (Coro::State self) |
1102 | } |
1021 | CODE: |
|
|
1022 | self->stack->idle_sp = 0; |
|
|
1023 | |
1103 | |
1024 | void |
1104 | void |
1025 | _clone_state_from (SV *dst, SV *src) |
1105 | _clone_state_from (SV *dst, SV *src) |
1026 | CODE: |
1106 | CODE: |
1027 | { |
1107 | { |
… | |
… | |
1030 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1110 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1031 | |
1111 | |
1032 | ++coro_src->refcnt; |
1112 | ++coro_src->refcnt; |
1033 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1113 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1034 | } |
1114 | } |
|
|
1115 | |
|
|
1116 | void |
|
|
1117 | _nonlocal_goto (IV nextop) |
|
|
1118 | CODE: |
|
|
1119 | /* uuh, somebody will kill me again for this */ |
|
|
1120 | PL_op->op_next = INT2PTR (OP *, nextop); |
1035 | |
1121 | |
1036 | void |
1122 | void |
1037 | _exit (code) |
1123 | _exit (code) |
1038 | int code |
1124 | int code |
1039 | PROTOTYPE: $ |
1125 | PROTOTYPE: $ |
1040 | CODE: |
1126 | CODE: |
1041 | _exit (code); |
1127 | _exit (code); |
1042 | |
1128 | |
1043 | MODULE = Coro::State PACKAGE = Coro::Cont |
|
|
1044 | |
|
|
1045 | void |
|
|
1046 | yield (...) |
|
|
1047 | PROTOTYPE: @ |
|
|
1048 | CODE: |
|
|
1049 | { |
|
|
1050 | SV *yieldstack; |
|
|
1051 | SV *sv; |
|
|
1052 | AV *defav = GvAV (PL_defgv); |
|
|
1053 | struct coro *prev, *next; |
|
|
1054 | |
|
|
1055 | yieldstack = *hv_fetch ( |
|
|
1056 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1057 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1058 | 0 |
|
|
1059 | ); |
|
|
1060 | |
|
|
1061 | /* set up @_ -- ugly */ |
|
|
1062 | av_clear (defav); |
|
|
1063 | av_fill (defav, items - 1); |
|
|
1064 | while (items--) |
|
|
1065 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1066 | |
|
|
1067 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1068 | prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1069 | next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1070 | SvREFCNT_dec (sv); |
|
|
1071 | |
|
|
1072 | coro_state_transfer (aTHX_ prev, next, 0); |
|
|
1073 | } |
|
|
1074 | |
|
|
1075 | MODULE = Coro::State PACKAGE = Coro |
1129 | MODULE = Coro::State PACKAGE = Coro |
1076 | |
1130 | |
1077 | BOOT: |
1131 | BOOT: |
1078 | { |
1132 | { |
1079 | int i; |
1133 | int i; |
… | |
… | |
1104 | |
1158 | |
1105 | GCoroAPI = &coroapi; |
1159 | GCoroAPI = &coroapi; |
1106 | sv_setiv (sv, (IV)&coroapi); |
1160 | sv_setiv (sv, (IV)&coroapi); |
1107 | SvREADONLY_on (sv); |
1161 | SvREADONLY_on (sv); |
1108 | } |
1162 | } |
|
|
1163 | } |
|
|
1164 | |
|
|
1165 | int |
|
|
1166 | prio (Coro::State coro, int newprio = 0) |
|
|
1167 | ALIAS: |
|
|
1168 | nice = 1 |
|
|
1169 | CODE: |
|
|
1170 | { |
|
|
1171 | RETVAL = coro->prio; |
|
|
1172 | |
|
|
1173 | if (items > 1) |
|
|
1174 | { |
|
|
1175 | if (ix) |
|
|
1176 | newprio += coro->prio; |
|
|
1177 | |
|
|
1178 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
|
|
1179 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
|
|
1180 | |
|
|
1181 | coro->prio = newprio; |
|
|
1182 | } |
1109 | } |
1183 | } |
1110 | |
1184 | |
1111 | void |
1185 | void |
1112 | ready (SV *self) |
1186 | ready (SV *self) |
1113 | PROTOTYPE: $ |
1187 | PROTOTYPE: $ |
… | |
… | |
1120 | CODE: |
1194 | CODE: |
1121 | RETVAL = coro_nready; |
1195 | RETVAL = coro_nready; |
1122 | OUTPUT: |
1196 | OUTPUT: |
1123 | RETVAL |
1197 | RETVAL |
1124 | |
1198 | |
1125 | void |
|
|
1126 | schedule (...) |
|
|
1127 | PROTOTYPE: |
|
|
1128 | CODE: |
|
|
1129 | api_schedule (); |
|
|
1130 | |
|
|
1131 | void |
|
|
1132 | _set_cede_self () |
|
|
1133 | CODE: |
|
|
1134 | coro_cede_self = 1; |
|
|
1135 | |
|
|
1136 | void |
|
|
1137 | cede (...) |
|
|
1138 | PROTOTYPE: |
|
|
1139 | CODE: |
|
|
1140 | api_cede (); |
|
|
1141 | |
|
|
1142 | MODULE = Coro::State PACKAGE = Coro::AIO |
1199 | MODULE = Coro::State PACKAGE = Coro::AIO |
1143 | |
1200 | |
1144 | SV * |
1201 | SV * |
1145 | _get_state () |
1202 | _get_state () |
1146 | CODE: |
1203 | CODE: |