1 | #define PERL_NO_GET_CONTEXT |
|
|
2 | |
|
|
3 | #include "libcoro/coro.c" |
1 | #include "libcoro/coro.c" |
4 | |
2 | |
5 | #include "EXTERN.h" |
3 | #include "EXTERN.h" |
6 | #include "perl.h" |
4 | #include "perl.h" |
7 | #include "XSUB.h" |
5 | #include "XSUB.h" |
8 | |
6 | |
9 | #include "patchlevel.h" |
7 | #include "patchlevel.h" |
10 | |
8 | |
11 | #if PERL_VERSION < 6 |
9 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
|
|
10 | (PERL_REVISION > (a) \ |
|
|
11 | || (PERL_REVISION == (a) \ |
|
|
12 | && (PERL_VERSION > (b) \ |
|
|
13 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
|
|
14 | |
|
|
15 | #if !PERL_VERSION_ATLEAST (5,6,0) |
12 | # ifndef PL_ppaddr |
16 | # ifndef PL_ppaddr |
13 | # define PL_ppaddr ppaddr |
17 | # define PL_ppaddr ppaddr |
14 | # endif |
18 | # endif |
15 | # ifndef call_sv |
19 | # ifndef call_sv |
16 | # define call_sv perl_call_sv |
20 | # define call_sv perl_call_sv |
… | |
… | |
66 | #define dSTACKLEVEL int stacklevel |
70 | #define dSTACKLEVEL int stacklevel |
67 | #define STACKLEVEL ((void *)&stacklevel) |
71 | #define STACKLEVEL ((void *)&stacklevel) |
68 | |
72 | |
69 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
73 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
70 | |
74 | |
|
|
75 | #if __GNUC__ >= 3 |
|
|
76 | # define attribute(x) __attribute__(x) |
|
|
77 | #else |
|
|
78 | # define attribute(x) |
|
|
79 | #endif |
|
|
80 | |
|
|
81 | #define NOINLINE attribute ((noinline)) |
|
|
82 | |
71 | #include "CoroAPI.h" |
83 | #include "CoroAPI.h" |
72 | |
84 | |
73 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
85 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
74 | |
86 | |
75 | #ifdef USE_ITHREADS |
87 | #ifdef USE_ITHREADS |
… | |
… | |
93 | /* the stack */ |
105 | /* the stack */ |
94 | void *sptr; |
106 | void *sptr; |
95 | long ssize; /* positive == mmap, otherwise malloc */ |
107 | long ssize; /* positive == mmap, otherwise malloc */ |
96 | |
108 | |
97 | /* cpu state */ |
109 | /* cpu state */ |
98 | void *idle_sp; /* original stacklevel when coroutine was created */ |
110 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
99 | JMPENV *top_env; |
111 | JMPENV *top_env; |
100 | coro_context cctx; |
112 | coro_context cctx; |
101 | } coro_stack; |
113 | } coro_stack; |
102 | |
|
|
103 | /* the (fake) coro_stack representing the main program */ |
|
|
104 | static coro_stack *main_stack; |
|
|
105 | |
114 | |
106 | /* this is a structure representing a perl-level coroutine */ |
115 | /* this is a structure representing a perl-level coroutine */ |
107 | struct coro { |
116 | struct coro { |
108 | /* the c coroutine allocated to this perl coroutine, if any */ |
117 | /* the c coroutine allocated to this perl coroutine, if any */ |
109 | coro_stack *stack; |
118 | coro_stack *stack; |
… | |
… | |
157 | |
166 | |
158 | typedef struct coro *Coro__State; |
167 | typedef struct coro *Coro__State; |
159 | typedef struct coro *Coro__State_or_hashref; |
168 | typedef struct coro *Coro__State_or_hashref; |
160 | |
169 | |
161 | static AV * |
170 | static AV * |
162 | coro_clone_padlist (pTHX_ CV *cv) |
171 | coro_clone_padlist (CV *cv) |
163 | { |
172 | { |
164 | AV *padlist = CvPADLIST (cv); |
173 | AV *padlist = CvPADLIST (cv); |
165 | AV *newpadlist, *newpad; |
174 | AV *newpadlist, *newpad; |
166 | |
175 | |
167 | newpadlist = newAV (); |
176 | newpadlist = newAV (); |
168 | AvREAL_off (newpadlist); |
177 | AvREAL_off (newpadlist); |
169 | #if PERL_VERSION < 9 |
178 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
179 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
180 | #else |
170 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
181 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
171 | #else |
|
|
172 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
173 | #endif |
182 | #endif |
174 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
183 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
175 | --AvFILLp (padlist); |
184 | --AvFILLp (padlist); |
176 | |
185 | |
177 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
186 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
… | |
… | |
179 | |
188 | |
180 | return newpadlist; |
189 | return newpadlist; |
181 | } |
190 | } |
182 | |
191 | |
183 | static void |
192 | static void |
184 | free_padlist (pTHX_ AV *padlist) |
193 | free_padlist (AV *padlist) |
185 | { |
194 | { |
186 | /* may be during global destruction */ |
195 | /* may be during global destruction */ |
187 | if (SvREFCNT (padlist)) |
196 | if (SvREFCNT (padlist)) |
188 | { |
197 | { |
189 | I32 i = AvFILLp (padlist); |
198 | I32 i = AvFILLp (padlist); |
… | |
… | |
210 | AV *padlist; |
219 | AV *padlist; |
211 | AV *av = (AV *)mg->mg_obj; |
220 | AV *av = (AV *)mg->mg_obj; |
212 | |
221 | |
213 | /* casting is fun. */ |
222 | /* casting is fun. */ |
214 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
223 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
215 | free_padlist (aTHX_ padlist); |
224 | free_padlist (padlist); |
216 | |
225 | |
217 | SvREFCNT_dec (av); |
226 | SvREFCNT_dec (av); |
218 | |
227 | |
219 | return 0; |
228 | return 0; |
220 | } |
229 | } |
… | |
… | |
223 | |
232 | |
224 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
233 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
225 | |
234 | |
226 | /* the next two functions merely cache the padlists */ |
235 | /* the next two functions merely cache the padlists */ |
227 | static void |
236 | static void |
228 | get_padlist (pTHX_ CV *cv) |
237 | get_padlist (CV *cv) |
229 | { |
238 | { |
230 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
239 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
231 | |
240 | |
232 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
241 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
233 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
242 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
234 | else |
243 | else |
235 | { |
244 | { |
236 | #if 0 |
245 | #if 0 |
237 | /* this should work - but it doesn't :( */ |
246 | /* this is probably cleaner, but also slower? */ |
238 | CV *cp = Perl_cv_clone (aTHX_ cv); |
247 | CV *cp = Perl_cv_clone (cv); |
239 | CvPADLIST (cv) = CvPADLIST (cp); |
248 | CvPADLIST (cv) = CvPADLIST (cp); |
240 | CvPADLIST (cp) = 0; |
249 | CvPADLIST (cp) = 0; |
241 | SvREFCNT_dec (cp); |
250 | SvREFCNT_dec (cp); |
242 | #else |
251 | #else |
243 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
252 | CvPADLIST (cv) = coro_clone_padlist (cv); |
244 | #endif |
253 | #endif |
245 | } |
254 | } |
246 | } |
255 | } |
247 | |
256 | |
248 | static void |
257 | static void |
249 | put_padlist (pTHX_ CV *cv) |
258 | put_padlist (CV *cv) |
250 | { |
259 | { |
251 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
260 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
252 | |
261 | |
253 | if (!mg) |
262 | if (!mg) |
254 | { |
263 | { |
… | |
… | |
262 | } |
271 | } |
263 | |
272 | |
264 | #define SB do { |
273 | #define SB do { |
265 | #define SE } while (0) |
274 | #define SE } while (0) |
266 | |
275 | |
267 | #define LOAD(state) load_state(aTHX_ (state)); |
276 | #define LOAD(state) load_state((state)); |
268 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
277 | #define SAVE(state,flags) save_state((state),(flags)); |
269 | |
278 | |
270 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
279 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
271 | |
280 | |
272 | static void |
281 | static void |
273 | load_state(pTHX_ Coro__State c) |
282 | load_state(Coro__State c) |
274 | { |
283 | { |
275 | PL_dowarn = c->dowarn; |
284 | PL_dowarn = c->dowarn; |
276 | PL_in_eval = c->in_eval; |
285 | PL_in_eval = c->in_eval; |
277 | |
286 | |
278 | PL_curstackinfo = c->curstackinfo; |
287 | PL_curstackinfo = c->curstackinfo; |
… | |
… | |
296 | PL_scopestack_ix = c->scopestack_ix; |
305 | PL_scopestack_ix = c->scopestack_ix; |
297 | PL_scopestack_max = c->scopestack_max; |
306 | PL_scopestack_max = c->scopestack_max; |
298 | PL_savestack = c->savestack; |
307 | PL_savestack = c->savestack; |
299 | PL_savestack_ix = c->savestack_ix; |
308 | PL_savestack_ix = c->savestack_ix; |
300 | PL_savestack_max = c->savestack_max; |
309 | PL_savestack_max = c->savestack_max; |
301 | #if PERL_VERSION < 9 |
310 | #if !PERL_VERSION_ATLEAST (5,9,0) |
302 | PL_retstack = c->retstack; |
311 | PL_retstack = c->retstack; |
303 | PL_retstack_ix = c->retstack_ix; |
312 | PL_retstack_ix = c->retstack_ix; |
304 | PL_retstack_max = c->retstack_max; |
313 | PL_retstack_max = c->retstack_max; |
305 | #endif |
314 | #endif |
306 | PL_curpm = c->curpm; |
315 | PL_curpm = c->curpm; |
… | |
… | |
319 | { |
328 | { |
320 | AV *padlist = (AV *)POPs; |
329 | AV *padlist = (AV *)POPs; |
321 | |
330 | |
322 | if (padlist) |
331 | if (padlist) |
323 | { |
332 | { |
324 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
333 | put_padlist (cv); /* mark this padlist as available */ |
325 | CvPADLIST(cv) = padlist; |
334 | CvPADLIST(cv) = padlist; |
326 | } |
335 | } |
327 | |
336 | |
328 | ++CvDEPTH(cv); |
337 | ++CvDEPTH(cv); |
329 | } |
338 | } |
… | |
… | |
331 | PUTBACK; |
340 | PUTBACK; |
332 | } |
341 | } |
333 | } |
342 | } |
334 | |
343 | |
335 | static void |
344 | static void |
336 | save_state(pTHX_ Coro__State c, int flags) |
345 | save_state(Coro__State c, int flags) |
337 | { |
346 | { |
338 | { |
347 | { |
339 | dSP; |
348 | dSP; |
340 | I32 cxix = cxstack_ix; |
349 | I32 cxix = cxstack_ix; |
341 | PERL_CONTEXT *ccstk = cxstack; |
350 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
369 | } |
378 | } |
370 | |
379 | |
371 | PUSHs ((SV *)CvPADLIST(cv)); |
380 | PUSHs ((SV *)CvPADLIST(cv)); |
372 | PUSHs ((SV *)cv); |
381 | PUSHs ((SV *)cv); |
373 | |
382 | |
374 | get_padlist (aTHX_ cv); |
383 | get_padlist (cv); |
375 | } |
384 | } |
376 | } |
385 | } |
377 | #ifdef CXt_FORMAT |
386 | #ifdef CXt_FORMAT |
378 | else if (CxTYPE(cx) == CXt_FORMAT) |
387 | else if (CxTYPE(cx) == CXt_FORMAT) |
379 | { |
388 | { |
… | |
… | |
423 | c->scopestack_ix = PL_scopestack_ix; |
432 | c->scopestack_ix = PL_scopestack_ix; |
424 | c->scopestack_max = PL_scopestack_max; |
433 | c->scopestack_max = PL_scopestack_max; |
425 | c->savestack = PL_savestack; |
434 | c->savestack = PL_savestack; |
426 | c->savestack_ix = PL_savestack_ix; |
435 | c->savestack_ix = PL_savestack_ix; |
427 | c->savestack_max = PL_savestack_max; |
436 | c->savestack_max = PL_savestack_max; |
428 | #if PERL_VERSION < 9 |
437 | #if !PERL_VERSION_ATLEAST (5,9,0) |
429 | c->retstack = PL_retstack; |
438 | c->retstack = PL_retstack; |
430 | c->retstack_ix = PL_retstack_ix; |
439 | c->retstack_ix = PL_retstack_ix; |
431 | c->retstack_max = PL_retstack_max; |
440 | c->retstack_max = PL_retstack_max; |
432 | #endif |
441 | #endif |
433 | c->curpm = PL_curpm; |
442 | c->curpm = PL_curpm; |
… | |
… | |
439 | * of perl.c:init_stacks, except that it uses less memory |
448 | * of perl.c:init_stacks, except that it uses less memory |
440 | * on the (sometimes correct) assumption that coroutines do |
449 | * on the (sometimes correct) assumption that coroutines do |
441 | * not usually need a lot of stackspace. |
450 | * not usually need a lot of stackspace. |
442 | */ |
451 | */ |
443 | static void |
452 | static void |
444 | coro_init_stacks (pTHX) |
453 | coro_init_stacks () |
445 | { |
454 | { |
446 | LOCK; |
|
|
447 | |
|
|
448 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
455 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
449 | PL_curstackinfo->si_type = PERLSI_MAIN; |
456 | PL_curstackinfo->si_type = PERLSI_MAIN; |
450 | PL_curstack = PL_curstackinfo->si_stack; |
457 | PL_curstack = PL_curstackinfo->si_stack; |
451 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
458 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
452 | |
459 | |
… | |
… | |
473 | |
480 | |
474 | New(54,PL_savestack,96,ANY); |
481 | New(54,PL_savestack,96,ANY); |
475 | PL_savestack_ix = 0; |
482 | PL_savestack_ix = 0; |
476 | PL_savestack_max = 96; |
483 | PL_savestack_max = 96; |
477 | |
484 | |
478 | #if PERL_VERSION < 9 |
485 | #if !PERL_VERSION_ATLEAST (5,9,0) |
479 | New(54,PL_retstack,8,OP*); |
486 | New(54,PL_retstack,8,OP*); |
480 | PL_retstack_ix = 0; |
487 | PL_retstack_ix = 0; |
481 | PL_retstack_max = 8; |
488 | PL_retstack_max = 8; |
482 | #endif |
489 | #endif |
483 | |
|
|
484 | UNLOCK; |
|
|
485 | } |
490 | } |
486 | |
491 | |
487 | /* |
492 | /* |
488 | * destroy the stacks, the callchain etc... |
493 | * destroy the stacks, the callchain etc... |
489 | */ |
494 | */ |
490 | static void |
495 | static void |
491 | destroy_stacks(pTHX) |
496 | destroy_stacks() |
492 | { |
497 | { |
493 | if (!IN_DESTRUCT) |
498 | if (!IN_DESTRUCT) |
494 | { |
499 | { |
495 | /* is this ugly, I ask? */ |
500 | /* is this ugly, I ask? */ |
496 | LEAVE_SCOPE (0); |
501 | LEAVE_SCOPE (0); |
… | |
… | |
527 | |
532 | |
528 | Safefree (PL_tmps_stack); |
533 | Safefree (PL_tmps_stack); |
529 | Safefree (PL_markstack); |
534 | Safefree (PL_markstack); |
530 | Safefree (PL_scopestack); |
535 | Safefree (PL_scopestack); |
531 | Safefree (PL_savestack); |
536 | Safefree (PL_savestack); |
532 | #if PERL_VERSION < 9 |
537 | #if !PERL_VERSION_ATLEAST (5,9,0) |
533 | Safefree (PL_retstack); |
538 | Safefree (PL_retstack); |
534 | #endif |
539 | #endif |
535 | } |
540 | } |
536 | |
541 | |
537 | static void |
542 | static void |
… | |
… | |
543 | dTHX; |
548 | dTHX; |
544 | dSP; |
549 | dSP; |
545 | UNOP myop; |
550 | UNOP myop; |
546 | SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); |
551 | SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); |
547 | |
552 | |
548 | coro_init_stacks (aTHX); |
553 | coro_init_stacks (); |
549 | /*PL_curcop = 0;*/ |
554 | /*PL_curcop = 0;*/ |
550 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
555 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
551 | SvREFCNT_dec (GvAV (PL_defgv)); |
556 | SvREFCNT_dec (GvAV (PL_defgv)); |
552 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
557 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
553 | |
558 | |
… | |
… | |
576 | SvREFCNT_dec (coro_mortal); |
581 | SvREFCNT_dec (coro_mortal); |
577 | coro_mortal = 0; |
582 | coro_mortal = 0; |
578 | } |
583 | } |
579 | } |
584 | } |
580 | |
585 | |
|
|
586 | static void NOINLINE |
|
|
587 | prepare_cctx (coro_stack *cctx) |
|
|
588 | { |
|
|
589 | dSP; |
|
|
590 | UNOP myop; |
|
|
591 | |
|
|
592 | Zero (&myop, 1, UNOP); |
|
|
593 | myop.op_next = PL_op; |
|
|
594 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
|
|
595 | |
|
|
596 | PUSHMARK(SP); |
|
|
597 | EXTEND (SP, 2); |
|
|
598 | PUSHs (newSViv (PTR2IV (cctx))); |
|
|
599 | PUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); |
|
|
600 | PUTBACK; |
|
|
601 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
602 | SPAGAIN; |
|
|
603 | } |
|
|
604 | |
581 | static void |
605 | static void |
582 | coro_run (void *arg) |
606 | coro_run (void *arg) |
583 | { |
607 | { |
584 | /* |
608 | /* |
585 | * this is a _very_ stripped down perl interpreter ;) |
609 | * this is a _very_ stripped down perl interpreter ;) |
586 | */ |
610 | */ |
587 | dTHX; |
|
|
588 | int ret; |
|
|
589 | |
|
|
590 | UNLOCK; |
611 | UNLOCK; |
591 | |
612 | |
592 | PL_top_env = &PL_start_env; |
613 | PL_top_env = &PL_start_env; |
593 | |
614 | prepare_cctx ((coro_stack *)arg); |
594 | sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); |
|
|
595 | sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op)); |
|
|
596 | |
|
|
597 | /* continue at cctx_init, without entersub */ |
|
|
598 | PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE)); |
|
|
599 | |
615 | |
600 | /* somebody will hit me for both perl_run and PL_restartop */ |
616 | /* somebody will hit me for both perl_run and PL_restartop */ |
601 | ret = perl_run (aTHX_ PERL_GET_CONTEXT); |
617 | perl_run (PERL_GET_CONTEXT); |
602 | printf ("ret %d\n", ret);//D |
|
|
603 | |
618 | |
604 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); |
619 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); |
605 | abort (); |
620 | abort (); |
606 | } |
621 | } |
607 | |
622 | |
… | |
… | |
647 | } |
662 | } |
648 | |
663 | |
649 | static void |
664 | static void |
650 | stack_free (coro_stack *stack) |
665 | stack_free (coro_stack *stack) |
651 | { |
666 | { |
652 | if (!stack || stack == main_stack) |
667 | if (!stack) |
653 | return; |
668 | return; |
654 | |
669 | |
655 | #if HAVE_MMAP |
670 | #if HAVE_MMAP |
656 | munmap (stack->sptr, stack->ssize); |
671 | munmap (stack->sptr, stack->ssize); |
657 | #else |
672 | #else |
… | |
… | |
688 | stack->next = stack_first; |
703 | stack->next = stack_first; |
689 | stack_first = stack; |
704 | stack_first = stack; |
690 | } |
705 | } |
691 | |
706 | |
692 | /* never call directly, always through the coro_state_transfer global variable */ |
707 | /* never call directly, always through the coro_state_transfer global variable */ |
693 | static void |
708 | static void NOINLINE |
694 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
709 | transfer (struct coro *prev, struct coro *next, int flags) |
695 | { |
710 | { |
696 | dSTACKLEVEL; |
711 | dSTACKLEVEL; |
697 | |
712 | |
698 | /* sometimes transfer is only called to set idle_sp */ |
713 | /* sometimes transfer is only called to set idle_sp */ |
699 | if (flags == TRANSFER_SET_STACKLEVEL) |
714 | if (flags == TRANSFER_SET_STACKLEVEL) |
… | |
… | |
719 | setup_coro (next); |
734 | setup_coro (next); |
720 | /* need a stack */ |
735 | /* need a stack */ |
721 | next->stack = 0; |
736 | next->stack = 0; |
722 | } |
737 | } |
723 | |
738 | |
|
|
739 | if (!prev->stack) |
|
|
740 | /* create a new empty context */ |
|
|
741 | Newz (0, prev->stack, 1, coro_stack); |
|
|
742 | |
724 | prev__stack = prev->stack; |
743 | prev__stack = prev->stack; |
725 | |
744 | |
726 | /* possibly "free" the stack */ |
745 | /* possibly "free" the stack */ |
727 | if (prev__stack->idle_sp == STACKLEVEL) |
746 | if (prev__stack->idle_sp == STACKLEVEL) |
728 | { |
747 | { |
… | |
… | |
735 | |
754 | |
736 | if (prev__stack != next->stack) |
755 | if (prev__stack != next->stack) |
737 | { |
756 | { |
738 | prev__stack->top_env = PL_top_env; |
757 | prev__stack->top_env = PL_top_env; |
739 | PL_top_env = next->stack->top_env; |
758 | PL_top_env = next->stack->top_env; |
740 | printf ("stacksw %p %p\n", prev__stack->idle_sp, next->stack->idle_sp);//D |
|
|
741 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
759 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
742 | } |
760 | } |
743 | |
761 | |
744 | free_coro_mortal (); |
762 | free_coro_mortal (); |
745 | |
763 | |
746 | UNLOCK; |
764 | UNLOCK; |
747 | } |
765 | } |
748 | } |
766 | } |
749 | |
|
|
750 | /* use this function pointer to call the above function */ |
|
|
751 | /* this is done to increase chances of the compiler not inlining the call */ |
|
|
752 | /* not static to make it even harder for the compiler (and theoretically impossible in most cases */ |
|
|
753 | void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; |
|
|
754 | |
767 | |
755 | struct transfer_args |
768 | struct transfer_args |
756 | { |
769 | { |
757 | struct coro *prev, *next; |
770 | struct coro *prev, *next; |
758 | int flags; |
771 | int flags; |
759 | }; |
772 | }; |
760 | |
773 | |
761 | #define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) |
774 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) |
762 | |
775 | |
763 | static void |
776 | static void |
764 | coro_state_destroy (struct coro *coro) |
777 | coro_state_destroy (struct coro *coro) |
765 | { |
778 | { |
766 | if (coro->refcnt--) |
779 | if (coro->refcnt--) |
… | |
… | |
768 | |
781 | |
769 | if (coro->mainstack && coro->mainstack != main_mainstack) |
782 | if (coro->mainstack && coro->mainstack != main_mainstack) |
770 | { |
783 | { |
771 | struct coro temp; |
784 | struct coro temp; |
772 | |
785 | |
773 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
786 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
774 | LOAD (aTHX_ coro); |
787 | LOAD (coro); |
775 | |
788 | |
776 | destroy_stacks (aTHX); |
789 | destroy_stacks (); |
777 | |
790 | |
778 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
791 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
779 | |
792 | |
780 | coro->mainstack = 0; |
793 | coro->mainstack = 0; |
781 | } |
794 | } |
… | |
… | |
784 | SvREFCNT_dec (coro->args); |
797 | SvREFCNT_dec (coro->args); |
785 | Safefree (coro); |
798 | Safefree (coro); |
786 | } |
799 | } |
787 | |
800 | |
788 | static int |
801 | static int |
789 | coro_state_clear (SV *sv, MAGIC *mg) |
802 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
790 | { |
803 | { |
791 | struct coro *coro = (struct coro *)mg->mg_ptr; |
804 | struct coro *coro = (struct coro *)mg->mg_ptr; |
792 | mg->mg_ptr = 0; |
805 | mg->mg_ptr = 0; |
793 | |
806 | |
794 | coro_state_destroy (coro); |
807 | coro_state_destroy (coro); |
795 | |
808 | |
796 | return 0; |
809 | return 0; |
797 | } |
810 | } |
798 | |
811 | |
799 | static int |
812 | static int |
800 | coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) |
813 | coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) |
801 | { |
814 | { |
802 | struct coro *coro = (struct coro *)mg->mg_ptr; |
815 | struct coro *coro = (struct coro *)mg->mg_ptr; |
803 | |
816 | |
804 | ++coro->refcnt; |
817 | ++coro->refcnt; |
805 | |
818 | |
806 | return 0; |
819 | return 0; |
807 | } |
820 | } |
808 | |
821 | |
809 | static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; |
822 | static MGVTBL coro_state_vtbl = { |
|
|
823 | 0, 0, 0, 0, |
|
|
824 | coro_state_clear, |
|
|
825 | 0, |
|
|
826 | #ifdef MGf_DUP |
|
|
827 | coro_state_dup, |
|
|
828 | #endif |
|
|
829 | }; |
810 | |
830 | |
811 | static struct coro * |
831 | static struct coro * |
812 | SvSTATE (SV *coro) |
832 | SvSTATE (SV *coro) |
813 | { |
833 | { |
814 | HV *stash; |
834 | HV *stash; |
… | |
… | |
829 | assert (mg->mg_type == PERL_MAGIC_ext); |
849 | assert (mg->mg_type == PERL_MAGIC_ext); |
830 | return (struct coro *)mg->mg_ptr; |
850 | return (struct coro *)mg->mg_ptr; |
831 | } |
851 | } |
832 | |
852 | |
833 | static void |
853 | static void |
834 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) |
854 | prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) |
835 | { |
855 | { |
836 | ta->prev = SvSTATE (prev); |
856 | ta->prev = SvSTATE (prev); |
837 | ta->next = SvSTATE (next); |
857 | ta->next = SvSTATE (next); |
838 | ta->flags = flags; |
858 | ta->flags = flags; |
839 | } |
859 | } |
… | |
… | |
842 | api_transfer (SV *prev, SV *next, int flags) |
862 | api_transfer (SV *prev, SV *next, int flags) |
843 | { |
863 | { |
844 | dTHX; |
864 | dTHX; |
845 | struct transfer_args ta; |
865 | struct transfer_args ta; |
846 | |
866 | |
847 | prepare_transfer (aTHX_ &ta, prev, next, flags); |
867 | prepare_transfer (&ta, prev, next, flags); |
848 | TRANSFER (ta); |
868 | TRANSFER (ta); |
849 | } |
869 | } |
850 | |
870 | |
851 | /** Coro ********************************************************************/ |
871 | /** Coro ********************************************************************/ |
852 | |
872 | |
… | |
… | |
861 | static GV *coro_current, *coro_idle; |
881 | static GV *coro_current, *coro_idle; |
862 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
882 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
863 | static int coro_nready; |
883 | static int coro_nready; |
864 | |
884 | |
865 | static void |
885 | static void |
866 | coro_enq (pTHX_ SV *sv) |
886 | coro_enq (SV *sv) |
867 | { |
887 | { |
868 | int prio; |
888 | int prio; |
869 | |
889 | |
870 | if (SvTYPE (sv) != SVt_PVHV) |
890 | if (SvTYPE (sv) != SVt_PVHV) |
871 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
891 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
… | |
… | |
875 | av_push (coro_ready [prio - PRIO_MIN], sv); |
895 | av_push (coro_ready [prio - PRIO_MIN], sv); |
876 | coro_nready++; |
896 | coro_nready++; |
877 | } |
897 | } |
878 | |
898 | |
879 | static SV * |
899 | static SV * |
880 | coro_deq (pTHX_ int min_prio) |
900 | coro_deq (int min_prio) |
881 | { |
901 | { |
882 | int prio = PRIO_MAX - PRIO_MIN; |
902 | int prio = PRIO_MAX - PRIO_MIN; |
883 | |
903 | |
884 | min_prio -= PRIO_MIN; |
904 | min_prio -= PRIO_MIN; |
885 | if (min_prio < 0) |
905 | if (min_prio < 0) |
… | |
… | |
902 | |
922 | |
903 | if (SvROK (coro)) |
923 | if (SvROK (coro)) |
904 | coro = SvRV (coro); |
924 | coro = SvRV (coro); |
905 | |
925 | |
906 | LOCK; |
926 | LOCK; |
907 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
927 | coro_enq (SvREFCNT_inc (coro)); |
908 | UNLOCK; |
928 | UNLOCK; |
909 | } |
929 | } |
910 | |
930 | |
911 | static void |
931 | static void |
912 | prepare_schedule (aTHX_ struct transfer_args *ta) |
932 | prepare_schedule (struct transfer_args *ta) |
913 | { |
933 | { |
914 | SV *current, *prev, *next; |
934 | SV *current, *prev, *next; |
915 | |
|
|
916 | LOCK; |
|
|
917 | |
935 | |
918 | current = GvSV (coro_current); |
936 | current = GvSV (coro_current); |
919 | |
937 | |
920 | for (;;) |
938 | for (;;) |
921 | { |
939 | { |
922 | LOCK; |
940 | LOCK; |
923 | |
|
|
924 | next = coro_deq (aTHX_ PRIO_MIN); |
941 | next = coro_deq (PRIO_MIN); |
|
|
942 | UNLOCK; |
925 | |
943 | |
926 | if (next) |
944 | if (next) |
927 | break; |
945 | break; |
928 | |
|
|
929 | UNLOCK; |
|
|
930 | |
946 | |
931 | { |
947 | { |
932 | dSP; |
948 | dSP; |
933 | |
949 | |
934 | ENTER; |
950 | ENTER; |
… | |
… | |
945 | |
961 | |
946 | prev = SvRV (current); |
962 | prev = SvRV (current); |
947 | SvRV (current) = next; |
963 | SvRV (current) = next; |
948 | |
964 | |
949 | /* free this only after the transfer */ |
965 | /* free this only after the transfer */ |
|
|
966 | LOCK; |
950 | free_coro_mortal (); |
967 | free_coro_mortal (); |
|
|
968 | UNLOCK; |
951 | coro_mortal = prev; |
969 | coro_mortal = prev; |
952 | |
970 | |
953 | ta->prev = SvSTATE (prev); |
971 | ta->prev = SvSTATE (prev); |
954 | ta->next = SvSTATE (next); |
972 | ta->next = SvSTATE (next); |
955 | ta->flags = TRANSFER_SAVE_ALL; |
973 | ta->flags = TRANSFER_SAVE_ALL; |
956 | |
|
|
957 | UNLOCK; |
|
|
958 | } |
974 | } |
959 | |
975 | |
960 | static void |
976 | static void |
961 | prepare_cede (aTHX_ struct transfer_args *ta) |
977 | prepare_cede (struct transfer_args *ta) |
962 | { |
978 | { |
963 | LOCK; |
979 | LOCK; |
964 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
980 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
965 | UNLOCK; |
981 | UNLOCK; |
966 | |
982 | |
967 | prepare_schedule (ta); |
983 | prepare_schedule (ta); |
968 | } |
984 | } |
969 | |
985 | |
… | |
… | |
1007 | main_mainstack = PL_mainstack; |
1023 | main_mainstack = PL_mainstack; |
1008 | |
1024 | |
1009 | coroapi.ver = CORO_API_VERSION; |
1025 | coroapi.ver = CORO_API_VERSION; |
1010 | coroapi.transfer = api_transfer; |
1026 | coroapi.transfer = api_transfer; |
1011 | |
1027 | |
1012 | Newz (0, main_stack, 1, coro_stack); |
|
|
1013 | main_stack->idle_sp = (void *)-1; |
|
|
1014 | |
|
|
1015 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1028 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1016 | } |
1029 | } |
1017 | |
1030 | |
1018 | SV * |
1031 | SV * |
1019 | new (char *klass, ...) |
1032 | new (char *klass, ...) |
… | |
… | |
1031 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1044 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1032 | |
1045 | |
1033 | for (i = 1; i < items; i++) |
1046 | for (i = 1; i < items; i++) |
1034 | av_push (coro->args, newSVsv (ST (i))); |
1047 | av_push (coro->args, newSVsv (ST (i))); |
1035 | |
1048 | |
1036 | coro->stack = main_stack; |
|
|
1037 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
1049 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
1038 | /*coro->stack = 0;*/ |
1050 | /*coro->stack = 0;*/ |
1039 | } |
1051 | } |
1040 | OUTPUT: |
1052 | OUTPUT: |
1041 | RETVAL |
1053 | RETVAL |
… | |
… | |
1116 | ++coro_src->refcnt; |
1128 | ++coro_src->refcnt; |
1117 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1129 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1118 | } |
1130 | } |
1119 | |
1131 | |
1120 | void |
1132 | void |
1121 | _nonlocal_goto (IV nextop) |
|
|
1122 | CODE: |
|
|
1123 | /* uuh, somebody will kill me again for this */ |
|
|
1124 | PL_op->op_next = INT2PTR (OP *, nextop); |
|
|
1125 | |
|
|
1126 | void |
|
|
1127 | _exit (code) |
1133 | _exit (code) |
1128 | int code |
1134 | int code |
1129 | PROTOTYPE: $ |
1135 | PROTOTYPE: $ |
1130 | CODE: |
1136 | CODE: |
1131 | _exit (code); |
1137 | _exit (code); |