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 | #if USE_VALGRIND |
|
|
10 | # include <valgrind/valgrind.h> |
|
|
11 | #endif |
|
|
12 | |
|
|
13 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
|
|
14 | (PERL_REVISION > (a) \ |
|
|
15 | || (PERL_REVISION == (a) \ |
|
|
16 | && (PERL_VERSION > (b) \ |
|
|
17 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
|
|
18 | |
|
|
19 | #if !PERL_VERSION_ATLEAST (5,6,0) |
12 | # ifndef PL_ppaddr |
20 | # ifndef PL_ppaddr |
13 | # define PL_ppaddr ppaddr |
21 | # define PL_ppaddr ppaddr |
14 | # endif |
22 | # endif |
15 | # ifndef call_sv |
23 | # ifndef call_sv |
16 | # define call_sv perl_call_sv |
24 | # define call_sv perl_call_sv |
… | |
… | |
54 | # ifndef PAGESIZE |
62 | # ifndef PAGESIZE |
55 | # define PAGESIZE pagesize |
63 | # define PAGESIZE pagesize |
56 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
64 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
57 | static long pagesize; |
65 | static long pagesize; |
58 | # else |
66 | # else |
59 | # define BOOT_PAGESIZE |
67 | # define BOOT_PAGESIZE (void)0 |
60 | # endif |
68 | # endif |
|
|
69 | #else |
|
|
70 | # define PAGESIZE 0 |
|
|
71 | # define BOOT_PAGESIZE (void)0 |
61 | #endif |
72 | #endif |
62 | |
73 | |
63 | /* The next macro should declare a variable stacklevel that contains and approximation |
74 | /* The next macro should declare a variable stacklevel that contains and approximation |
64 | * to the current C stack pointer. Its property is that it changes with each call |
75 | * to the current C stack pointer. Its property is that it changes with each call |
65 | * and should be unique. */ |
76 | * and should be unique. */ |
66 | #define dSTACKLEVEL int stacklevel |
77 | #define dSTACKLEVEL int stacklevel |
67 | #define STACKLEVEL ((void *)&stacklevel) |
78 | #define STACKLEVEL ((void *)&stacklevel) |
68 | |
79 | |
69 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
80 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
70 | |
81 | |
71 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
82 | #if __GNUC__ >= 3 |
|
|
83 | # define attribute(x) __attribute__(x) |
|
|
84 | #else |
|
|
85 | # define attribute(x) |
|
|
86 | #endif |
|
|
87 | |
|
|
88 | #define NOINLINE attribute ((noinline)) |
72 | |
89 | |
73 | #include "CoroAPI.h" |
90 | #include "CoroAPI.h" |
74 | |
91 | |
75 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
92 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
76 | |
93 | |
… | |
… | |
87 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
104 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
88 | static HV *coro_state_stash, *coro_stash; |
105 | static HV *coro_state_stash, *coro_stash; |
89 | static SV *coro_mortal; /* will be freed after next transfer */ |
106 | static SV *coro_mortal; /* will be freed after next transfer */ |
90 | |
107 | |
91 | /* this is a structure representing a c-level coroutine */ |
108 | /* this is a structure representing a c-level coroutine */ |
92 | typedef struct coro_stack { |
109 | typedef struct coro_cctx { |
93 | struct coro_stack *next; |
110 | struct coro_cctx *next; |
94 | |
111 | |
95 | void *idle_sp; /* original stacklevel when coroutine was created */ |
112 | /* the stack */ |
96 | void *sptr; |
113 | void *sptr; |
97 | long ssize; /* positive == mmap, otherwise malloc */ |
114 | long ssize; /* positive == mmap, otherwise malloc */ |
98 | |
115 | |
99 | /* cpu state */ |
116 | /* cpu state */ |
|
|
117 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
118 | JMPENV *top_env; |
100 | coro_context cctx; |
119 | coro_context cctx; |
101 | JMPENV *top_env; |
|
|
102 | } coro_stack; |
|
|
103 | |
120 | |
104 | /* the (fake) coro_stack representing the main program */ |
121 | #if USE_VALGRIND |
105 | static coro_stack *main_stack; |
122 | int valgrind_id; |
|
|
123 | #endif |
|
|
124 | } coro_cctx; |
106 | |
125 | |
107 | /* this is a structure representing a perl-level coroutine */ |
126 | /* this is a structure representing a perl-level coroutine */ |
108 | struct coro { |
127 | struct coro { |
109 | /* the c coroutine allocated to this perl coroutine, if any */ |
128 | /* the c coroutine allocated to this perl coroutine, if any */ |
110 | coro_stack *stack; |
129 | coro_cctx *cctx; |
111 | |
130 | |
112 | /* data associated with this coroutine (initial args) */ |
131 | /* data associated with this coroutine (initial args) */ |
113 | AV *args; |
132 | AV *args; |
114 | int refcnt; |
133 | int refcnt; |
115 | |
134 | |
… | |
… | |
158 | |
177 | |
159 | typedef struct coro *Coro__State; |
178 | typedef struct coro *Coro__State; |
160 | typedef struct coro *Coro__State_or_hashref; |
179 | typedef struct coro *Coro__State_or_hashref; |
161 | |
180 | |
162 | static AV * |
181 | static AV * |
163 | coro_clone_padlist (pTHX_ CV *cv) |
182 | coro_clone_padlist (CV *cv) |
164 | { |
183 | { |
165 | AV *padlist = CvPADLIST (cv); |
184 | AV *padlist = CvPADLIST (cv); |
166 | AV *newpadlist, *newpad; |
185 | AV *newpadlist, *newpad; |
167 | |
186 | |
168 | newpadlist = newAV (); |
187 | newpadlist = newAV (); |
169 | AvREAL_off (newpadlist); |
188 | AvREAL_off (newpadlist); |
170 | #if PERL_VERSION < 9 |
189 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
190 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
191 | #else |
171 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
192 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
172 | #else |
|
|
173 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
174 | #endif |
193 | #endif |
175 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
194 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
176 | --AvFILLp (padlist); |
195 | --AvFILLp (padlist); |
177 | |
196 | |
178 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
197 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
… | |
… | |
180 | |
199 | |
181 | return newpadlist; |
200 | return newpadlist; |
182 | } |
201 | } |
183 | |
202 | |
184 | static void |
203 | static void |
185 | free_padlist (pTHX_ AV *padlist) |
204 | free_padlist (AV *padlist) |
186 | { |
205 | { |
187 | /* may be during global destruction */ |
206 | /* may be during global destruction */ |
188 | if (SvREFCNT (padlist)) |
207 | if (SvREFCNT (padlist)) |
189 | { |
208 | { |
190 | I32 i = AvFILLp (padlist); |
209 | I32 i = AvFILLp (padlist); |
… | |
… | |
211 | AV *padlist; |
230 | AV *padlist; |
212 | AV *av = (AV *)mg->mg_obj; |
231 | AV *av = (AV *)mg->mg_obj; |
213 | |
232 | |
214 | /* casting is fun. */ |
233 | /* casting is fun. */ |
215 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
234 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
216 | free_padlist (aTHX_ padlist); |
235 | free_padlist (padlist); |
217 | |
236 | |
218 | SvREFCNT_dec (av); |
237 | SvREFCNT_dec (av); |
219 | |
238 | |
220 | return 0; |
239 | return 0; |
221 | } |
240 | } |
… | |
… | |
224 | |
243 | |
225 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
244 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
226 | |
245 | |
227 | /* the next two functions merely cache the padlists */ |
246 | /* the next two functions merely cache the padlists */ |
228 | static void |
247 | static void |
229 | get_padlist (pTHX_ CV *cv) |
248 | get_padlist (CV *cv) |
230 | { |
249 | { |
231 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
250 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
232 | |
251 | |
233 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
252 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
234 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
253 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
235 | else |
254 | else |
236 | { |
255 | { |
237 | #if 0 |
256 | #if 0 |
238 | /* this should work - but it doesn't :( */ |
257 | /* this is probably cleaner, but also slower? */ |
239 | CV *cp = Perl_cv_clone (aTHX_ cv); |
258 | CV *cp = Perl_cv_clone (cv); |
240 | CvPADLIST (cv) = CvPADLIST (cp); |
259 | CvPADLIST (cv) = CvPADLIST (cp); |
241 | CvPADLIST (cp) = 0; |
260 | CvPADLIST (cp) = 0; |
242 | SvREFCNT_dec (cp); |
261 | SvREFCNT_dec (cp); |
243 | #else |
262 | #else |
244 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
263 | CvPADLIST (cv) = coro_clone_padlist (cv); |
245 | #endif |
264 | #endif |
246 | } |
265 | } |
247 | } |
266 | } |
248 | |
267 | |
249 | static void |
268 | static void |
250 | put_padlist (pTHX_ CV *cv) |
269 | put_padlist (CV *cv) |
251 | { |
270 | { |
252 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
271 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
253 | |
272 | |
254 | if (!mg) |
273 | if (!mg) |
255 | { |
274 | { |
… | |
… | |
263 | } |
282 | } |
264 | |
283 | |
265 | #define SB do { |
284 | #define SB do { |
266 | #define SE } while (0) |
285 | #define SE } while (0) |
267 | |
286 | |
268 | #define LOAD(state) load_state(aTHX_ (state)); |
287 | #define LOAD(state) load_state((state)); |
269 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
288 | #define SAVE(state,flags) save_state((state),(flags)); |
270 | |
289 | |
271 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
290 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
272 | |
291 | |
273 | static void |
292 | static void |
274 | load_state(pTHX_ Coro__State c) |
293 | load_state(Coro__State c) |
275 | { |
294 | { |
276 | PL_dowarn = c->dowarn; |
295 | PL_dowarn = c->dowarn; |
277 | PL_in_eval = c->in_eval; |
296 | PL_in_eval = c->in_eval; |
278 | |
297 | |
279 | PL_curstackinfo = c->curstackinfo; |
298 | PL_curstackinfo = c->curstackinfo; |
… | |
… | |
297 | PL_scopestack_ix = c->scopestack_ix; |
316 | PL_scopestack_ix = c->scopestack_ix; |
298 | PL_scopestack_max = c->scopestack_max; |
317 | PL_scopestack_max = c->scopestack_max; |
299 | PL_savestack = c->savestack; |
318 | PL_savestack = c->savestack; |
300 | PL_savestack_ix = c->savestack_ix; |
319 | PL_savestack_ix = c->savestack_ix; |
301 | PL_savestack_max = c->savestack_max; |
320 | PL_savestack_max = c->savestack_max; |
302 | #if PERL_VERSION < 9 |
321 | #if !PERL_VERSION_ATLEAST (5,9,0) |
303 | PL_retstack = c->retstack; |
322 | PL_retstack = c->retstack; |
304 | PL_retstack_ix = c->retstack_ix; |
323 | PL_retstack_ix = c->retstack_ix; |
305 | PL_retstack_max = c->retstack_max; |
324 | PL_retstack_max = c->retstack_max; |
306 | #endif |
325 | #endif |
307 | PL_curpm = c->curpm; |
326 | PL_curpm = c->curpm; |
… | |
… | |
320 | { |
339 | { |
321 | AV *padlist = (AV *)POPs; |
340 | AV *padlist = (AV *)POPs; |
322 | |
341 | |
323 | if (padlist) |
342 | if (padlist) |
324 | { |
343 | { |
325 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
344 | put_padlist (cv); /* mark this padlist as available */ |
326 | CvPADLIST(cv) = padlist; |
345 | CvPADLIST(cv) = padlist; |
327 | } |
346 | } |
328 | |
347 | |
329 | ++CvDEPTH(cv); |
348 | ++CvDEPTH(cv); |
330 | } |
349 | } |
… | |
… | |
332 | PUTBACK; |
351 | PUTBACK; |
333 | } |
352 | } |
334 | } |
353 | } |
335 | |
354 | |
336 | static void |
355 | static void |
337 | save_state(pTHX_ Coro__State c, int flags) |
356 | save_state(Coro__State c, int flags) |
338 | { |
357 | { |
339 | { |
358 | { |
340 | dSP; |
359 | dSP; |
341 | I32 cxix = cxstack_ix; |
360 | I32 cxix = cxstack_ix; |
342 | PERL_CONTEXT *ccstk = cxstack; |
361 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
370 | } |
389 | } |
371 | |
390 | |
372 | PUSHs ((SV *)CvPADLIST(cv)); |
391 | PUSHs ((SV *)CvPADLIST(cv)); |
373 | PUSHs ((SV *)cv); |
392 | PUSHs ((SV *)cv); |
374 | |
393 | |
375 | get_padlist (aTHX_ cv); |
394 | get_padlist (cv); |
376 | } |
395 | } |
377 | } |
396 | } |
378 | #ifdef CXt_FORMAT |
397 | #ifdef CXt_FORMAT |
379 | else if (CxTYPE(cx) == CXt_FORMAT) |
398 | else if (CxTYPE(cx) == CXt_FORMAT) |
380 | { |
399 | { |
… | |
… | |
424 | c->scopestack_ix = PL_scopestack_ix; |
443 | c->scopestack_ix = PL_scopestack_ix; |
425 | c->scopestack_max = PL_scopestack_max; |
444 | c->scopestack_max = PL_scopestack_max; |
426 | c->savestack = PL_savestack; |
445 | c->savestack = PL_savestack; |
427 | c->savestack_ix = PL_savestack_ix; |
446 | c->savestack_ix = PL_savestack_ix; |
428 | c->savestack_max = PL_savestack_max; |
447 | c->savestack_max = PL_savestack_max; |
429 | #if PERL_VERSION < 9 |
448 | #if !PERL_VERSION_ATLEAST (5,9,0) |
430 | c->retstack = PL_retstack; |
449 | c->retstack = PL_retstack; |
431 | c->retstack_ix = PL_retstack_ix; |
450 | c->retstack_ix = PL_retstack_ix; |
432 | c->retstack_max = PL_retstack_max; |
451 | c->retstack_max = PL_retstack_max; |
433 | #endif |
452 | #endif |
434 | c->curpm = PL_curpm; |
453 | c->curpm = PL_curpm; |
… | |
… | |
440 | * of perl.c:init_stacks, except that it uses less memory |
459 | * of perl.c:init_stacks, except that it uses less memory |
441 | * on the (sometimes correct) assumption that coroutines do |
460 | * on the (sometimes correct) assumption that coroutines do |
442 | * not usually need a lot of stackspace. |
461 | * not usually need a lot of stackspace. |
443 | */ |
462 | */ |
444 | static void |
463 | static void |
445 | coro_init_stacks (pTHX) |
464 | coro_init_stacks () |
446 | { |
465 | { |
447 | LOCK; |
|
|
448 | |
|
|
449 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
466 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
450 | PL_curstackinfo->si_type = PERLSI_MAIN; |
467 | PL_curstackinfo->si_type = PERLSI_MAIN; |
451 | PL_curstack = PL_curstackinfo->si_stack; |
468 | PL_curstack = PL_curstackinfo->si_stack; |
452 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
469 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
453 | |
470 | |
… | |
… | |
474 | |
491 | |
475 | New(54,PL_savestack,96,ANY); |
492 | New(54,PL_savestack,96,ANY); |
476 | PL_savestack_ix = 0; |
493 | PL_savestack_ix = 0; |
477 | PL_savestack_max = 96; |
494 | PL_savestack_max = 96; |
478 | |
495 | |
479 | #if PERL_VERSION < 9 |
496 | #if !PERL_VERSION_ATLEAST (5,9,0) |
480 | New(54,PL_retstack,8,OP*); |
497 | New(54,PL_retstack,8,OP*); |
481 | PL_retstack_ix = 0; |
498 | PL_retstack_ix = 0; |
482 | PL_retstack_max = 8; |
499 | PL_retstack_max = 8; |
483 | #endif |
500 | #endif |
484 | |
|
|
485 | UNLOCK; |
|
|
486 | } |
501 | } |
487 | |
502 | |
488 | /* |
503 | /* |
489 | * destroy the stacks, the callchain etc... |
504 | * destroy the stacks, the callchain etc... |
490 | */ |
505 | */ |
491 | static void |
506 | static void |
492 | destroy_stacks(pTHX) |
507 | coro_destroy_stacks () |
493 | { |
508 | { |
494 | if (!IN_DESTRUCT) |
509 | if (!IN_DESTRUCT) |
495 | { |
510 | { |
496 | /* is this ugly, I ask? */ |
511 | /* is this ugly, I ask? */ |
497 | LEAVE_SCOPE (0); |
512 | LEAVE_SCOPE (0); |
… | |
… | |
528 | |
543 | |
529 | Safefree (PL_tmps_stack); |
544 | Safefree (PL_tmps_stack); |
530 | Safefree (PL_markstack); |
545 | Safefree (PL_markstack); |
531 | Safefree (PL_scopestack); |
546 | Safefree (PL_scopestack); |
532 | Safefree (PL_savestack); |
547 | Safefree (PL_savestack); |
533 | #if PERL_VERSION < 9 |
548 | #if !PERL_VERSION_ATLEAST (5,9,0) |
534 | Safefree (PL_retstack); |
549 | Safefree (PL_retstack); |
535 | #endif |
550 | #endif |
536 | } |
551 | } |
537 | |
552 | |
538 | static void |
553 | static void |
539 | setup_coro (struct coro *coro) |
554 | setup_coro (struct coro *coro) |
540 | { |
555 | { |
541 | /* |
556 | /* |
542 | * emulate part of the perl startup here. |
557 | * emulate part of the perl startup here. |
543 | */ |
558 | */ |
544 | dTHX; |
|
|
545 | dSP; |
|
|
546 | UNOP myop; |
|
|
547 | SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); |
|
|
548 | |
559 | |
549 | coro_init_stacks (aTHX); |
560 | coro_init_stacks (); |
|
|
561 | |
550 | /*PL_curcop = 0;*/ |
562 | PL_curcop = 0; |
551 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
563 | PL_in_eval = 0; |
|
|
564 | PL_curpm = 0; |
|
|
565 | |
|
|
566 | { |
|
|
567 | dSP; |
|
|
568 | LOGOP myop; |
|
|
569 | |
|
|
570 | /* I have no idea why this is needed, but it is */ |
|
|
571 | PUSHMARK (SP); |
|
|
572 | |
552 | SvREFCNT_dec (GvAV (PL_defgv)); |
573 | SvREFCNT_dec (GvAV (PL_defgv)); |
553 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
574 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
554 | |
575 | |
555 | SPAGAIN; |
|
|
556 | |
|
|
557 | Zero (&myop, 1, UNOP); |
576 | Zero (&myop, 1, LOGOP); |
558 | myop.op_next = Nullop; |
577 | myop.op_next = Nullop; |
559 | myop.op_flags = OPf_WANT_VOID; |
578 | myop.op_flags = OPf_WANT_VOID; |
560 | |
579 | |
561 | PL_op = (OP *)&myop; |
580 | PL_op = (OP *)&myop; |
562 | |
581 | |
563 | PUSHMARK(SP); |
582 | PUSHMARK (SP); |
564 | XPUSHs (sub_init); |
583 | XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE)); |
565 | PUTBACK; |
584 | PUTBACK; |
566 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
585 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
567 | SPAGAIN; |
586 | SPAGAIN; |
568 | |
587 | |
569 | ENTER; /* necessary e.g. for dounwind */ |
588 | ENTER; /* necessary e.g. for dounwind */ |
|
|
589 | } |
570 | } |
590 | } |
571 | |
591 | |
572 | static void |
592 | static void |
573 | free_coro_mortal () |
593 | free_coro_mortal () |
574 | { |
594 | { |
… | |
… | |
577 | SvREFCNT_dec (coro_mortal); |
597 | SvREFCNT_dec (coro_mortal); |
578 | coro_mortal = 0; |
598 | coro_mortal = 0; |
579 | } |
599 | } |
580 | } |
600 | } |
581 | |
601 | |
|
|
602 | static void NOINLINE |
|
|
603 | prepare_cctx (coro_cctx *cctx) |
|
|
604 | { |
|
|
605 | dSP; |
|
|
606 | LOGOP myop; |
|
|
607 | |
|
|
608 | Zero (&myop, 1, LOGOP); |
|
|
609 | myop.op_next = PL_op; |
|
|
610 | myop.op_flags = OPf_WANT_VOID; |
|
|
611 | |
|
|
612 | sv_setiv (get_sv ("Coro::State::cctx", FALSE), PTR2IV (cctx)); |
|
|
613 | |
|
|
614 | PUSHMARK (SP); |
|
|
615 | XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); |
|
|
616 | PUTBACK; |
|
|
617 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
618 | SPAGAIN; |
|
|
619 | } |
|
|
620 | |
582 | static void |
621 | static void |
583 | coro_run (void *arg) |
622 | coro_run (void *arg) |
584 | { |
623 | { |
|
|
624 | /* coro_run is the alternative epilogue of transfer() */ |
|
|
625 | UNLOCK; |
|
|
626 | |
585 | /* |
627 | /* |
586 | * this is a _very_ stripped down perl interpreter ;) |
628 | * this is a _very_ stripped down perl interpreter ;) |
587 | */ |
629 | */ |
588 | dTHX; |
|
|
589 | PL_top_env = &PL_start_env; |
630 | PL_top_env = &PL_start_env; |
|
|
631 | /* inject call to cctx_init */ |
|
|
632 | prepare_cctx ((coro_cctx *)arg); |
590 | |
633 | |
591 | UNLOCK; |
|
|
592 | |
|
|
593 | sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); |
|
|
594 | sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op)); |
|
|
595 | PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE)); |
|
|
596 | |
|
|
597 | /* somebody will hit me for both perl_run and PL_restart_op */ |
634 | /* somebody will hit me for both perl_run and PL_restartop */ |
598 | perl_run (aTHX_ PERL_GET_CONTEXT); |
635 | perl_run (PL_curinterp); |
599 | |
636 | |
|
|
637 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
600 | abort (); |
638 | abort (); |
601 | } |
639 | } |
602 | |
640 | |
603 | static coro_stack * |
641 | static coro_cctx * |
604 | stack_new () |
642 | cctx_new () |
605 | { |
643 | { |
606 | coro_stack *stack; |
644 | coro_cctx *cctx; |
607 | |
645 | |
608 | New (0, stack, 1, coro_stack); |
646 | New (0, cctx, 1, coro_cctx); |
609 | |
647 | |
610 | #if HAVE_MMAP |
648 | #if HAVE_MMAP |
611 | |
649 | |
612 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ |
650 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
|
|
651 | /* mmap suppsedly does allocate-on-write for us */ |
613 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
652 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
614 | |
653 | |
615 | if (stack->sptr == (void *)-1) |
654 | if (cctx->sptr == (void *)-1) |
616 | { |
655 | { |
617 | perror ("FATAL: unable to mmap stack for coroutine"); |
656 | perror ("FATAL: unable to mmap stack for coroutine"); |
618 | _exit (EXIT_FAILURE); |
657 | _exit (EXIT_FAILURE); |
619 | } |
658 | } |
620 | |
659 | |
|
|
660 | # if STACKGUARD |
621 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
661 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
|
|
662 | # endif |
622 | |
663 | |
623 | #else |
664 | #else |
624 | |
665 | |
625 | stack->ssize = STACKSIZE * (long)sizeof (long); |
666 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
626 | New (0, stack->sptr, STACKSIZE, long); |
667 | New (0, cctx->sptr, STACKSIZE, long); |
627 | |
668 | |
628 | if (!stack->sptr) |
669 | if (!cctx->sptr) |
629 | { |
670 | { |
630 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
671 | perror ("FATAL: unable to malloc stack for coroutine"); |
631 | _exit (EXIT_FAILURE); |
672 | _exit (EXIT_FAILURE); |
632 | } |
673 | } |
633 | |
674 | |
634 | #endif |
675 | #endif |
635 | |
676 | |
|
|
677 | #if USE_VALGRIND |
|
|
678 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
679 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
|
|
680 | cctx->ssize + (char *)cctx->sptr |
|
|
681 | ); |
|
|
682 | #endif |
|
|
683 | |
636 | coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); |
684 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
637 | |
685 | |
638 | return stack; |
686 | return cctx; |
639 | } |
687 | } |
640 | |
688 | |
641 | static void |
689 | static void |
642 | stack_free (coro_stack *stack) |
690 | cctx_free (coro_cctx *cctx) |
643 | { |
691 | { |
644 | if (!stack || stack == main_stack) |
692 | if (!cctx) |
645 | return; |
693 | return; |
646 | |
694 | |
|
|
695 | #if USE_VALGRIND |
|
|
696 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
|
|
697 | #endif |
|
|
698 | |
647 | #if HAVE_MMAP |
699 | #if HAVE_MMAP |
648 | munmap (stack->sptr, stack->ssize); |
700 | munmap (cctx->sptr, cctx->ssize); |
649 | #else |
701 | #else |
650 | Safefree (stack->sptr); |
702 | Safefree (cctx->sptr); |
651 | #endif |
703 | #endif |
652 | |
704 | |
653 | Safefree (stack); |
705 | Safefree (cctx); |
654 | } |
706 | } |
655 | |
707 | |
656 | static coro_stack *stack_first; |
708 | static coro_cctx *cctx_first; |
|
|
709 | static int cctx_count, cctx_idle; |
657 | |
710 | |
658 | static coro_stack * |
711 | static coro_cctx * |
659 | stack_get () |
712 | cctx_get () |
660 | { |
713 | { |
661 | coro_stack *stack; |
714 | coro_cctx *cctx; |
662 | |
715 | |
663 | if (stack_first) |
716 | if (cctx_first) |
664 | { |
717 | { |
665 | stack = stack_first; |
718 | --cctx_idle; |
|
|
719 | cctx = cctx_first; |
666 | stack_first = stack->next; |
720 | cctx_first = cctx->next; |
667 | } |
721 | } |
668 | else |
722 | else |
669 | { |
723 | { |
670 | stack = stack_new (); |
724 | ++cctx_count; |
|
|
725 | cctx = cctx_new (); |
671 | PL_op = PL_op->op_next; |
726 | PL_op = PL_op->op_next; |
672 | } |
727 | } |
673 | |
728 | |
674 | return stack; |
729 | return cctx; |
675 | } |
730 | } |
676 | |
731 | |
677 | static void |
732 | static void |
678 | stack_put (coro_stack *stack) |
733 | cctx_put (coro_cctx *cctx) |
679 | { |
734 | { |
680 | stack->next = stack_first; |
735 | ++cctx_idle; |
681 | stack_first = stack; |
736 | cctx->next = cctx_first; |
|
|
737 | cctx_first = cctx; |
682 | } |
738 | } |
683 | |
739 | |
684 | /* never call directly, always through the coro_state_transfer global variable */ |
740 | /* never call directly, always through the coro_state_transfer global variable */ |
685 | static void |
741 | static void NOINLINE |
686 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
742 | transfer (struct coro *prev, struct coro *next, int flags) |
687 | { |
743 | { |
688 | dSTACKLEVEL; |
744 | dSTACKLEVEL; |
689 | |
745 | |
690 | /* sometimes transfer is only called to set idle_sp */ |
746 | /* sometimes transfer is only called to set idle_sp */ |
691 | if (flags == TRANSFER_SET_STACKLEVEL) |
747 | if (flags == TRANSFER_SET_STACKLEVEL) |
692 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
748 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
693 | else if (prev != next) |
749 | else if (prev != next) |
694 | { |
750 | { |
695 | coro_stack *prev__stack; |
751 | coro_cctx *prev__cctx; |
696 | |
752 | |
697 | LOCK; |
753 | LOCK; |
698 | |
754 | |
699 | if (next->mainstack) |
755 | if (next->mainstack) |
700 | { |
756 | { |
… | |
… | |
708 | /* first get rid of the old state */ |
764 | /* first get rid of the old state */ |
709 | SAVE (prev, -1); |
765 | SAVE (prev, -1); |
710 | /* setup coroutine call */ |
766 | /* setup coroutine call */ |
711 | setup_coro (next); |
767 | setup_coro (next); |
712 | /* need a stack */ |
768 | /* need a stack */ |
713 | next->stack = 0; |
769 | next->cctx = 0; |
714 | } |
770 | } |
715 | |
771 | |
|
|
772 | if (!prev->cctx) |
|
|
773 | /* create a new empty context */ |
|
|
774 | Newz (0, prev->cctx, 1, coro_cctx); |
|
|
775 | |
716 | prev__stack = prev->stack; |
776 | prev__cctx = prev->cctx; |
717 | |
777 | |
718 | /* possibly "free" the stack */ |
778 | /* possibly "free" the cctx */ |
719 | if (prev__stack->idle_sp == STACKLEVEL) |
779 | if (prev__cctx->idle_sp == STACKLEVEL) |
720 | { |
780 | { |
721 | stack_put (prev__stack); |
781 | cctx_put (prev__cctx); |
722 | prev->stack = 0; |
782 | prev->cctx = 0; |
723 | } |
783 | } |
724 | |
784 | |
725 | if (!next->stack) |
785 | if (!next->cctx) |
726 | next->stack = stack_get (); |
786 | next->cctx = cctx_get (); |
727 | |
787 | |
728 | if (prev__stack != next->stack) |
788 | if (prev__cctx != next->cctx) |
729 | { |
789 | { |
730 | prev__stack->top_env = PL_top_env; |
790 | prev__cctx->top_env = PL_top_env; |
731 | PL_top_env = next->stack->top_env; |
791 | PL_top_env = next->cctx->top_env; |
732 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
792 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
733 | } |
793 | } |
734 | |
794 | |
735 | free_coro_mortal (); |
795 | free_coro_mortal (); |
736 | |
796 | |
737 | UNLOCK; |
797 | UNLOCK; |
738 | } |
798 | } |
739 | } |
799 | } |
740 | |
|
|
741 | /* use this function pointer to call the above function */ |
|
|
742 | /* this is done to increase chances of the compiler not inlining the call */ |
|
|
743 | /* not static to make it even harder for the compiler (and theoretically impossible in most cases */ |
|
|
744 | void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; |
|
|
745 | |
800 | |
746 | struct transfer_args |
801 | struct transfer_args |
747 | { |
802 | { |
748 | struct coro *prev, *next; |
803 | struct coro *prev, *next; |
749 | int flags; |
804 | int flags; |
750 | }; |
805 | }; |
751 | |
806 | |
752 | #define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) |
807 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) |
753 | |
808 | |
754 | static void |
809 | static void |
755 | coro_state_destroy (struct coro *coro) |
810 | coro_state_destroy (struct coro *coro) |
756 | { |
811 | { |
757 | if (coro->refcnt--) |
812 | if (coro->refcnt--) |
… | |
… | |
759 | |
814 | |
760 | if (coro->mainstack && coro->mainstack != main_mainstack) |
815 | if (coro->mainstack && coro->mainstack != main_mainstack) |
761 | { |
816 | { |
762 | struct coro temp; |
817 | struct coro temp; |
763 | |
818 | |
764 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
819 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
765 | LOAD (aTHX_ coro); |
820 | LOAD (coro); |
766 | |
821 | |
767 | destroy_stacks (aTHX); |
822 | coro_destroy_stacks (); |
768 | |
823 | |
769 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
824 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
770 | |
825 | |
771 | coro->mainstack = 0; |
826 | coro->mainstack = 0; |
772 | } |
827 | } |
773 | |
828 | |
774 | stack_free (coro->stack); |
829 | cctx_free (coro->cctx); |
775 | SvREFCNT_dec (coro->args); |
830 | SvREFCNT_dec (coro->args); |
776 | Safefree (coro); |
831 | Safefree (coro); |
777 | } |
832 | } |
778 | |
833 | |
779 | static int |
834 | static int |
780 | coro_state_clear (SV *sv, MAGIC *mg) |
835 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
781 | { |
836 | { |
782 | struct coro *coro = (struct coro *)mg->mg_ptr; |
837 | struct coro *coro = (struct coro *)mg->mg_ptr; |
783 | mg->mg_ptr = 0; |
838 | mg->mg_ptr = 0; |
784 | |
839 | |
785 | coro_state_destroy (coro); |
840 | coro_state_destroy (coro); |
786 | |
841 | |
787 | return 0; |
842 | return 0; |
788 | } |
843 | } |
789 | |
844 | |
790 | static int |
845 | static int |
791 | coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) |
846 | coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) |
792 | { |
847 | { |
793 | struct coro *coro = (struct coro *)mg->mg_ptr; |
848 | struct coro *coro = (struct coro *)mg->mg_ptr; |
794 | |
849 | |
795 | ++coro->refcnt; |
850 | ++coro->refcnt; |
796 | |
851 | |
797 | return 0; |
852 | return 0; |
798 | } |
853 | } |
799 | |
854 | |
800 | static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; |
855 | static MGVTBL coro_state_vtbl = { |
|
|
856 | 0, 0, 0, 0, |
|
|
857 | coro_state_clear, |
|
|
858 | 0, |
|
|
859 | #ifdef MGf_DUP |
|
|
860 | coro_state_dup, |
|
|
861 | #else |
|
|
862 | # define MGf_DUP 0 |
|
|
863 | #endif |
|
|
864 | }; |
801 | |
865 | |
802 | static struct coro * |
866 | static struct coro * |
803 | SvSTATE (SV *coro) |
867 | SvSTATE (SV *coro) |
804 | { |
868 | { |
805 | HV *stash; |
869 | HV *stash; |
… | |
… | |
820 | assert (mg->mg_type == PERL_MAGIC_ext); |
884 | assert (mg->mg_type == PERL_MAGIC_ext); |
821 | return (struct coro *)mg->mg_ptr; |
885 | return (struct coro *)mg->mg_ptr; |
822 | } |
886 | } |
823 | |
887 | |
824 | static void |
888 | static void |
825 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) |
889 | prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) |
826 | { |
890 | { |
827 | ta->prev = SvSTATE (prev); |
891 | ta->prev = SvSTATE (prev); |
828 | ta->next = SvSTATE (next); |
892 | ta->next = SvSTATE (next); |
829 | ta->flags = flags; |
893 | ta->flags = flags; |
830 | } |
894 | } |
… | |
… | |
833 | api_transfer (SV *prev, SV *next, int flags) |
897 | api_transfer (SV *prev, SV *next, int flags) |
834 | { |
898 | { |
835 | dTHX; |
899 | dTHX; |
836 | struct transfer_args ta; |
900 | struct transfer_args ta; |
837 | |
901 | |
838 | prepare_transfer (aTHX_ &ta, prev, next, flags); |
902 | prepare_transfer (&ta, prev, next, flags); |
839 | TRANSFER (ta); |
903 | TRANSFER (ta); |
840 | } |
904 | } |
841 | |
905 | |
842 | /** Coro ********************************************************************/ |
906 | /** Coro ********************************************************************/ |
843 | |
907 | |
… | |
… | |
852 | static GV *coro_current, *coro_idle; |
916 | static GV *coro_current, *coro_idle; |
853 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
917 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
854 | static int coro_nready; |
918 | static int coro_nready; |
855 | |
919 | |
856 | static void |
920 | static void |
857 | coro_enq (pTHX_ SV *sv) |
921 | coro_enq (SV *sv) |
858 | { |
922 | { |
859 | int prio; |
923 | int prio; |
860 | |
924 | |
861 | if (SvTYPE (sv) != SVt_PVHV) |
925 | if (SvTYPE (sv) != SVt_PVHV) |
862 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
926 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
… | |
… | |
866 | av_push (coro_ready [prio - PRIO_MIN], sv); |
930 | av_push (coro_ready [prio - PRIO_MIN], sv); |
867 | coro_nready++; |
931 | coro_nready++; |
868 | } |
932 | } |
869 | |
933 | |
870 | static SV * |
934 | static SV * |
871 | coro_deq (pTHX_ int min_prio) |
935 | coro_deq (int min_prio) |
872 | { |
936 | { |
873 | int prio = PRIO_MAX - PRIO_MIN; |
937 | int prio = PRIO_MAX - PRIO_MIN; |
874 | |
938 | |
875 | min_prio -= PRIO_MIN; |
939 | min_prio -= PRIO_MIN; |
876 | if (min_prio < 0) |
940 | if (min_prio < 0) |
… | |
… | |
893 | |
957 | |
894 | if (SvROK (coro)) |
958 | if (SvROK (coro)) |
895 | coro = SvRV (coro); |
959 | coro = SvRV (coro); |
896 | |
960 | |
897 | LOCK; |
961 | LOCK; |
898 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
962 | coro_enq (SvREFCNT_inc (coro)); |
899 | UNLOCK; |
963 | UNLOCK; |
900 | } |
964 | } |
901 | |
965 | |
902 | static void |
966 | static void |
903 | prepare_schedule (aTHX_ struct transfer_args *ta) |
967 | prepare_schedule (struct transfer_args *ta) |
904 | { |
968 | { |
905 | SV *current, *prev, *next; |
969 | SV *current, *prev, *next; |
906 | |
|
|
907 | LOCK; |
|
|
908 | |
970 | |
909 | current = GvSV (coro_current); |
971 | current = GvSV (coro_current); |
910 | |
972 | |
911 | for (;;) |
973 | for (;;) |
912 | { |
974 | { |
913 | LOCK; |
975 | LOCK; |
914 | |
|
|
915 | next = coro_deq (aTHX_ PRIO_MIN); |
976 | next = coro_deq (PRIO_MIN); |
|
|
977 | UNLOCK; |
916 | |
978 | |
917 | if (next) |
979 | if (next) |
918 | break; |
980 | break; |
919 | |
|
|
920 | UNLOCK; |
|
|
921 | |
981 | |
922 | { |
982 | { |
923 | dSP; |
983 | dSP; |
924 | |
984 | |
925 | ENTER; |
985 | ENTER; |
… | |
… | |
936 | |
996 | |
937 | prev = SvRV (current); |
997 | prev = SvRV (current); |
938 | SvRV (current) = next; |
998 | SvRV (current) = next; |
939 | |
999 | |
940 | /* free this only after the transfer */ |
1000 | /* free this only after the transfer */ |
|
|
1001 | LOCK; |
941 | free_coro_mortal (); |
1002 | free_coro_mortal (); |
|
|
1003 | UNLOCK; |
942 | coro_mortal = prev; |
1004 | coro_mortal = prev; |
943 | |
1005 | |
944 | ta->prev = SvSTATE (prev); |
1006 | ta->prev = SvSTATE (prev); |
945 | ta->next = SvSTATE (next); |
1007 | ta->next = SvSTATE (next); |
946 | ta->flags = TRANSFER_SAVE_ALL; |
1008 | ta->flags = TRANSFER_SAVE_ALL; |
947 | |
|
|
948 | UNLOCK; |
|
|
949 | } |
1009 | } |
950 | |
1010 | |
951 | static void |
1011 | static void |
952 | prepare_cede (aTHX_ struct transfer_args *ta) |
1012 | prepare_cede (struct transfer_args *ta) |
953 | { |
1013 | { |
954 | LOCK; |
1014 | LOCK; |
955 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
1015 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
956 | UNLOCK; |
1016 | UNLOCK; |
957 | |
1017 | |
958 | prepare_schedule (ta); |
1018 | prepare_schedule (ta); |
959 | } |
1019 | } |
960 | |
1020 | |
… | |
… | |
998 | main_mainstack = PL_mainstack; |
1058 | main_mainstack = PL_mainstack; |
999 | |
1059 | |
1000 | coroapi.ver = CORO_API_VERSION; |
1060 | coroapi.ver = CORO_API_VERSION; |
1001 | coroapi.transfer = api_transfer; |
1061 | coroapi.transfer = api_transfer; |
1002 | |
1062 | |
1003 | Newz (0, main_stack, 1, coro_stack); |
|
|
1004 | main_stack->idle_sp = (void *)-1; |
|
|
1005 | |
|
|
1006 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1063 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1007 | } |
1064 | } |
1008 | |
1065 | |
1009 | SV * |
1066 | SV * |
1010 | new (char *klass, ...) |
1067 | new (char *klass, ...) |
… | |
… | |
1021 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1078 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1022 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1079 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1023 | |
1080 | |
1024 | for (i = 1; i < items; i++) |
1081 | for (i = 1; i < items; i++) |
1025 | av_push (coro->args, newSVsv (ST (i))); |
1082 | av_push (coro->args, newSVsv (ST (i))); |
1026 | |
|
|
1027 | coro->stack = main_stack; |
|
|
1028 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
|
|
1029 | /*coro->stack = 0;*/ |
|
|
1030 | } |
1083 | } |
1031 | OUTPUT: |
1084 | OUTPUT: |
1032 | RETVAL |
1085 | RETVAL |
1033 | |
1086 | |
1034 | void |
1087 | void |
1035 | transfer (...) |
1088 | _set_stacklevel (...) |
1036 | ALIAS: |
1089 | ALIAS: |
1037 | Coro::schedule = 1 |
1090 | Coro::State::transfer = 1 |
1038 | Coro::cede = 2 |
1091 | Coro::schedule = 2 |
1039 | _set_stacklevel = 3 |
1092 | Coro::cede = 3 |
|
|
1093 | Coro::Cont::yield = 4 |
1040 | CODE: |
1094 | CODE: |
1041 | { |
1095 | { |
1042 | struct transfer_args ta; |
1096 | struct transfer_args ta; |
1043 | |
1097 | |
1044 | switch (ix) |
1098 | switch (ix) |
1045 | { |
1099 | { |
1046 | case 0: |
1100 | case 0: |
1047 | if (items != 3) |
|
|
1048 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
|
|
1049 | |
|
|
1050 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
|
|
1051 | break; |
|
|
1052 | |
|
|
1053 | case 1: |
|
|
1054 | prepare_schedule (&ta); |
|
|
1055 | break; |
|
|
1056 | |
|
|
1057 | case 2: |
|
|
1058 | prepare_cede (&ta); |
|
|
1059 | break; |
|
|
1060 | |
|
|
1061 | case 3: |
|
|
1062 | ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); |
1101 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1063 | ta.next = 0; |
1102 | ta.next = 0; |
1064 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1103 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1065 | break; |
1104 | break; |
|
|
1105 | |
|
|
1106 | case 1: |
|
|
1107 | if (items != 3) |
|
|
1108 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
|
|
1109 | |
|
|
1110 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
|
|
1111 | break; |
|
|
1112 | |
|
|
1113 | case 2: |
|
|
1114 | prepare_schedule (&ta); |
|
|
1115 | break; |
|
|
1116 | |
|
|
1117 | case 3: |
|
|
1118 | prepare_cede (&ta); |
|
|
1119 | break; |
|
|
1120 | |
|
|
1121 | case 4: |
|
|
1122 | { |
|
|
1123 | SV *yieldstack; |
|
|
1124 | SV *sv; |
|
|
1125 | AV *defav = GvAV (PL_defgv); |
|
|
1126 | |
|
|
1127 | yieldstack = *hv_fetch ( |
|
|
1128 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1129 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1130 | 0 |
|
|
1131 | ); |
|
|
1132 | |
|
|
1133 | /* set up @_ -- ugly */ |
|
|
1134 | av_clear (defav); |
|
|
1135 | av_fill (defav, items - 1); |
|
|
1136 | while (items--) |
|
|
1137 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1138 | |
|
|
1139 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1140 | ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1141 | ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1142 | ta.flags = 0; |
|
|
1143 | SvREFCNT_dec (sv); |
|
|
1144 | } |
|
|
1145 | break; |
|
|
1146 | |
1066 | } |
1147 | } |
1067 | |
1148 | |
1068 | TRANSFER (ta); |
1149 | TRANSFER (ta); |
1069 | } |
1150 | } |
1070 | |
1151 | |
… | |
… | |
1077 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1158 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1078 | |
1159 | |
1079 | ++coro_src->refcnt; |
1160 | ++coro_src->refcnt; |
1080 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1161 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1081 | } |
1162 | } |
1082 | |
|
|
1083 | void |
|
|
1084 | _nonlocal_goto (IV nextop) |
|
|
1085 | CODE: |
|
|
1086 | /* uuh, somebody will kill me again for this */ |
|
|
1087 | PL_op->op_next = INT2PTR (OP *, nextop); |
|
|
1088 | |
1163 | |
1089 | void |
1164 | void |
1090 | _exit (code) |
1165 | _exit (code) |
1091 | int code |
1166 | int code |
1092 | PROTOTYPE: $ |
1167 | PROTOTYPE: $ |
1093 | CODE: |
1168 | CODE: |
1094 | _exit (code); |
1169 | _exit (code); |
1095 | |
1170 | |
1096 | MODULE = Coro::State PACKAGE = Coro::Cont |
1171 | int |
|
|
1172 | cctx_count () |
|
|
1173 | CODE: |
|
|
1174 | RETVAL = cctx_count; |
|
|
1175 | OUTPUT: |
|
|
1176 | RETVAL |
1097 | |
1177 | |
1098 | void |
1178 | int |
1099 | yield (...) |
1179 | cctx_idle () |
1100 | PROTOTYPE: @ |
1180 | CODE: |
1101 | CODE: |
1181 | RETVAL = cctx_idle; |
1102 | { |
1182 | OUTPUT: |
1103 | SV *yieldstack; |
1183 | RETVAL |
1104 | SV *sv; |
|
|
1105 | AV *defav = GvAV (PL_defgv); |
|
|
1106 | struct coro *prev, *next; |
|
|
1107 | |
|
|
1108 | yieldstack = *hv_fetch ( |
|
|
1109 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1110 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1111 | 0 |
|
|
1112 | ); |
|
|
1113 | |
|
|
1114 | /* set up @_ -- ugly */ |
|
|
1115 | av_clear (defav); |
|
|
1116 | av_fill (defav, items - 1); |
|
|
1117 | while (items--) |
|
|
1118 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1119 | |
|
|
1120 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1121 | prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1122 | next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1123 | SvREFCNT_dec (sv); |
|
|
1124 | |
|
|
1125 | coro_state_transfer (aTHX_ prev, next, 0); |
|
|
1126 | } |
|
|
1127 | |
1184 | |
1128 | MODULE = Coro::State PACKAGE = Coro |
1185 | MODULE = Coro::State PACKAGE = Coro |
1129 | |
1186 | |
1130 | BOOT: |
1187 | BOOT: |
1131 | { |
1188 | { |