… | |
… | |
4 | #define PERL_EXT |
4 | #define PERL_EXT |
5 | |
5 | |
6 | #include "EXTERN.h" |
6 | #include "EXTERN.h" |
7 | #include "perl.h" |
7 | #include "perl.h" |
8 | #include "XSUB.h" |
8 | #include "XSUB.h" |
|
|
9 | #include "perliol.h" |
9 | |
10 | |
10 | #include "patchlevel.h" |
11 | #include "patchlevel.h" |
11 | |
12 | |
12 | #include <stdio.h> |
13 | #include <stdio.h> |
13 | #include <errno.h> |
14 | #include <errno.h> |
14 | #include <assert.h> |
15 | #include <assert.h> |
|
|
16 | |
|
|
17 | #ifdef WIN32 |
|
|
18 | # undef setjmp |
|
|
19 | # undef longjmp |
|
|
20 | # undef _exit |
|
|
21 | # define setjmp _setjmp // deep magic, don't ask |
|
|
22 | #else |
|
|
23 | # include <inttypes.h> /* most portable stdint.h */ |
|
|
24 | #endif |
15 | |
25 | |
16 | #ifdef HAVE_MMAP |
26 | #ifdef HAVE_MMAP |
17 | # include <unistd.h> |
27 | # include <unistd.h> |
18 | # include <sys/mman.h> |
28 | # include <sys/mman.h> |
19 | # ifndef MAP_ANONYMOUS |
29 | # ifndef MAP_ANONYMOUS |
… | |
… | |
71 | # ifndef IS_PADCONST |
81 | # ifndef IS_PADCONST |
72 | # define IS_PADCONST(v) 0 |
82 | # define IS_PADCONST(v) 0 |
73 | # endif |
83 | # endif |
74 | #endif |
84 | #endif |
75 | |
85 | |
|
|
86 | /* 5.11 */ |
|
|
87 | #ifndef CxHASARGS |
|
|
88 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
89 | #endif |
|
|
90 | |
|
|
91 | /* 5.10.0 */ |
|
|
92 | #ifndef SvREFCNT_inc_NN |
|
|
93 | # define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
|
|
94 | #endif |
|
|
95 | |
|
|
96 | /* 5.8.8 */ |
|
|
97 | #ifndef GV_NOTQUAL |
|
|
98 | # define GV_NOTQUAL 0 |
|
|
99 | #endif |
|
|
100 | #ifndef newSV |
|
|
101 | # define newSV(l) NEWSV(0,l) |
|
|
102 | #endif |
|
|
103 | |
76 | /* 5.8.7 */ |
104 | /* 5.8.7 */ |
77 | #ifndef SvRV_set |
105 | #ifndef SvRV_set |
78 | # define SvRV_set(s,v) SvRV(s) = (v) |
106 | # define SvRV_set(s,v) SvRV(s) = (v) |
79 | #endif |
|
|
80 | |
|
|
81 | /* 5.8.8 */ |
|
|
82 | #ifndef GV_NOTQUAL |
|
|
83 | # define GV_NOTQUAL 0 |
|
|
84 | #endif |
|
|
85 | #ifndef newSV |
|
|
86 | # define newSV(l) NEWSV(0,l) |
|
|
87 | #endif |
107 | #endif |
88 | |
108 | |
89 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
109 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
90 | # undef CORO_STACKGUARD |
110 | # undef CORO_STACKGUARD |
91 | #endif |
111 | #endif |
… | |
… | |
133 | #endif |
153 | #endif |
134 | |
154 | |
135 | /* helper storage struct for Coro::AIO */ |
155 | /* helper storage struct for Coro::AIO */ |
136 | struct io_state |
156 | struct io_state |
137 | { |
157 | { |
|
|
158 | AV *res; |
138 | int errorno; |
159 | int errorno; |
139 | I32 laststype; |
160 | I32 laststype; |
140 | int laststatval; |
161 | int laststatval; |
141 | Stat_t statcache; |
162 | Stat_t statcache; |
142 | }; |
163 | }; |
143 | |
164 | |
|
|
165 | static double (*nvtime)(); /* so why doesn't it take void? */ |
|
|
166 | |
144 | static size_t coro_stacksize = CORO_STACKSIZE; |
167 | static size_t coro_stacksize = CORO_STACKSIZE; |
145 | static struct CoroAPI coroapi; |
168 | static struct CoroAPI coroapi; |
146 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
169 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
147 | static JMPENV *main_top_env; |
170 | static JMPENV *main_top_env; |
148 | static HV *coro_state_stash, *coro_stash; |
171 | static HV *coro_state_stash, *coro_stash; |
149 | static SV *coro_mortal; /* will be freed after next transfer */ |
172 | static volatile SV *coro_mortal; /* will be freed after next transfer */ |
150 | |
173 | |
151 | static GV *irsgv; /* $/ */ |
174 | static GV *irsgv; /* $/ */ |
152 | static GV *stdoutgv; /* *STDOUT */ |
175 | static GV *stdoutgv; /* *STDOUT */ |
|
|
176 | static SV *rv_diehook; |
|
|
177 | static SV *rv_warnhook; |
|
|
178 | static HV *hv_sig; /* %SIG */ |
153 | |
179 | |
154 | /* async_pool helper stuff */ |
180 | /* async_pool helper stuff */ |
155 | static SV *sv_pool_rss; |
181 | static SV *sv_pool_rss; |
156 | static SV *sv_pool_size; |
182 | static SV *sv_pool_size; |
157 | static AV *av_async_pool; |
183 | static AV *av_async_pool; |
|
|
184 | |
|
|
185 | /* Coro::AnyEvent */ |
|
|
186 | static SV *sv_activity; |
158 | |
187 | |
159 | static struct coro_cctx *cctx_first; |
188 | static struct coro_cctx *cctx_first; |
160 | static int cctx_count, cctx_idle; |
189 | static int cctx_count, cctx_idle; |
161 | |
190 | |
162 | enum { |
191 | enum { |
… | |
… | |
215 | |
244 | |
216 | /* process data */ |
245 | /* process data */ |
217 | AV *mainstack; |
246 | AV *mainstack; |
218 | perl_slots *slot; /* basically the saved sp */ |
247 | perl_slots *slot; /* basically the saved sp */ |
219 | |
248 | |
220 | /* data associated with this coroutine (initial args) */ |
249 | AV *args; /* data associated with this coroutine (initial args) */ |
221 | AV *args; |
250 | int refcnt; /* coroutines are refcounted, yes */ |
222 | int refcnt; |
|
|
223 | int flags; /* CF_ flags */ |
251 | int flags; /* CF_ flags */ |
|
|
252 | HV *hv; /* the perl hash associated with this coro, if any */ |
224 | |
253 | |
225 | /* statistics */ |
254 | /* statistics */ |
226 | int usecount; /* number of transfers to this coro */ |
255 | int usecount; /* number of transfers to this coro */ |
227 | |
256 | |
228 | /* coro process data */ |
257 | /* coro process data */ |
229 | int prio; |
258 | int prio; |
230 | SV *throw; |
259 | SV *throw; /* exception to be thrown */ |
231 | |
260 | |
232 | /* async_pool */ |
261 | /* async_pool */ |
233 | SV *saved_deffh; |
262 | SV *saved_deffh; |
234 | |
263 | |
235 | /* linked list */ |
264 | /* linked list */ |
236 | struct coro *next, *prev; |
265 | struct coro *next, *prev; |
237 | HV *hv; /* the perl hash associated with this coro, if any */ |
|
|
238 | }; |
266 | }; |
239 | |
267 | |
240 | typedef struct coro *Coro__State; |
268 | typedef struct coro *Coro__State; |
241 | typedef struct coro *Coro__State_or_hashref; |
269 | typedef struct coro *Coro__State_or_hashref; |
242 | |
270 | |
… | |
… | |
249 | #define PRIO_IDLE -3 |
277 | #define PRIO_IDLE -3 |
250 | #define PRIO_MIN -4 |
278 | #define PRIO_MIN -4 |
251 | |
279 | |
252 | /* for Coro.pm */ |
280 | /* for Coro.pm */ |
253 | static SV *coro_current; |
281 | static SV *coro_current; |
|
|
282 | static SV *coro_readyhook; |
254 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
283 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
255 | static int coro_nready; |
284 | static int coro_nready; |
256 | static struct coro *coro_first; |
285 | static struct coro *coro_first; |
257 | |
286 | |
258 | /** lowlevel stuff **********************************************************/ |
287 | /** lowlevel stuff **********************************************************/ |
259 | |
288 | |
|
|
289 | static SV * |
|
|
290 | coro_get_sv (pTHX_ const char *name, int create) |
|
|
291 | { |
|
|
292 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
293 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
|
|
294 | get_sv (name, create); |
|
|
295 | #endif |
|
|
296 | return get_sv (name, create); |
|
|
297 | } |
|
|
298 | |
|
|
299 | static AV * |
|
|
300 | coro_get_av (pTHX_ const char *name, int create) |
|
|
301 | { |
|
|
302 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
303 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
|
|
304 | get_av (name, create); |
|
|
305 | #endif |
|
|
306 | return get_av (name, create); |
|
|
307 | } |
|
|
308 | |
|
|
309 | static HV * |
|
|
310 | coro_get_hv (pTHX_ const char *name, int create) |
|
|
311 | { |
|
|
312 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
313 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
|
|
314 | get_hv (name, create); |
|
|
315 | #endif |
|
|
316 | return get_hv (name, create); |
|
|
317 | } |
|
|
318 | |
260 | static AV * |
319 | static AV * |
261 | coro_clone_padlist (pTHX_ CV *cv) |
320 | coro_clone_padlist (pTHX_ CV *cv) |
262 | { |
321 | { |
263 | AV *padlist = CvPADLIST (cv); |
322 | AV *padlist = CvPADLIST (cv); |
264 | AV *newpadlist, *newpad; |
323 | AV *newpadlist, *newpad; |
265 | |
324 | |
266 | newpadlist = newAV (); |
325 | newpadlist = newAV (); |
267 | AvREAL_off (newpadlist); |
326 | AvREAL_off (newpadlist); |
268 | #if PERL_VERSION_ATLEAST (5,9,0) |
327 | #if PERL_VERSION_ATLEAST (5,10,0) |
269 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
328 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
270 | #else |
329 | #else |
271 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
330 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
272 | #endif |
331 | #endif |
273 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
332 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
274 | --AvFILLp (padlist); |
333 | --AvFILLp (padlist); |
275 | |
334 | |
276 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
335 | av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); |
277 | av_store (newpadlist, 1, (SV *)newpad); |
336 | av_store (newpadlist, 1, (SV *)newpad); |
278 | |
337 | |
279 | return newpadlist; |
338 | return newpadlist; |
280 | } |
339 | } |
281 | |
340 | |
… | |
… | |
311 | |
370 | |
312 | /* casting is fun. */ |
371 | /* casting is fun. */ |
313 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
372 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
314 | free_padlist (aTHX_ padlist); |
373 | free_padlist (aTHX_ padlist); |
315 | |
374 | |
316 | SvREFCNT_dec (av); |
375 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
317 | |
376 | |
318 | return 0; |
377 | return 0; |
319 | } |
378 | } |
320 | |
379 | |
321 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
380 | #define CORO_MAGIC_type_cv PERL_MAGIC_ext |
|
|
381 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
322 | |
382 | |
323 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
383 | static MGVTBL coro_cv_vtbl = { |
|
|
384 | 0, 0, 0, 0, |
|
|
385 | coro_cv_free |
|
|
386 | }; |
324 | |
387 | |
325 | #define CORO_MAGIC(cv) \ |
388 | #define CORO_MAGIC(sv,type) \ |
326 | SvMAGIC (cv) \ |
389 | SvMAGIC (sv) \ |
327 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
390 | ? SvMAGIC (sv)->mg_type == type \ |
328 | ? SvMAGIC (cv) \ |
391 | ? SvMAGIC (sv) \ |
329 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
392 | : mg_find (sv, type) \ |
330 | : 0 |
393 | : 0 |
|
|
394 | |
|
|
395 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
396 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
331 | |
397 | |
332 | static struct coro * |
398 | static struct coro * |
333 | SvSTATE_ (pTHX_ SV *coro) |
399 | SvSTATE_ (pTHX_ SV *coro) |
334 | { |
400 | { |
335 | HV *stash; |
401 | HV *stash; |
… | |
… | |
347 | /* very slow, but rare, check */ |
413 | /* very slow, but rare, check */ |
348 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
414 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
349 | croak ("Coro::State object required"); |
415 | croak ("Coro::State object required"); |
350 | } |
416 | } |
351 | |
417 | |
352 | mg = CORO_MAGIC (coro); |
418 | mg = CORO_MAGIC_state (coro); |
353 | return (struct coro *)mg->mg_ptr; |
419 | return (struct coro *)mg->mg_ptr; |
354 | } |
420 | } |
355 | |
421 | |
356 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
422 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
357 | |
423 | |
358 | /* the next two functions merely cache the padlists */ |
424 | /* the next two functions merely cache the padlists */ |
359 | static void |
425 | static void |
360 | get_padlist (pTHX_ CV *cv) |
426 | get_padlist (pTHX_ CV *cv) |
361 | { |
427 | { |
362 | MAGIC *mg = CORO_MAGIC (cv); |
428 | MAGIC *mg = CORO_MAGIC_cv (cv); |
363 | AV *av; |
429 | AV *av; |
364 | |
430 | |
365 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
431 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
366 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
432 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
367 | else |
433 | else |
368 | { |
434 | { |
369 | #if CORO_PREFER_PERL_FUNCTIONS |
435 | #if CORO_PREFER_PERL_FUNCTIONS |
370 | /* this is probably cleaner, but also slower? */ |
436 | /* this is probably cleaner? but also slower! */ |
|
|
437 | /* in practise, it seems to be less stable */ |
371 | CV *cp = Perl_cv_clone (cv); |
438 | CV *cp = Perl_cv_clone (cv); |
372 | CvPADLIST (cv) = CvPADLIST (cp); |
439 | CvPADLIST (cv) = CvPADLIST (cp); |
373 | CvPADLIST (cp) = 0; |
440 | CvPADLIST (cp) = 0; |
374 | SvREFCNT_dec (cp); |
441 | SvREFCNT_dec (cp); |
375 | #else |
442 | #else |
… | |
… | |
379 | } |
446 | } |
380 | |
447 | |
381 | static void |
448 | static void |
382 | put_padlist (pTHX_ CV *cv) |
449 | put_padlist (pTHX_ CV *cv) |
383 | { |
450 | { |
384 | MAGIC *mg = CORO_MAGIC (cv); |
451 | MAGIC *mg = CORO_MAGIC_cv (cv); |
385 | AV *av; |
452 | AV *av; |
386 | |
453 | |
387 | if (expect_false (!mg)) |
454 | if (expect_false (!mg)) |
388 | { |
455 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
389 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
|
|
390 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
|
|
391 | mg->mg_virtual = &vtbl_coro; |
|
|
392 | mg->mg_obj = (SV *)newAV (); |
|
|
393 | } |
|
|
394 | |
456 | |
395 | av = (AV *)mg->mg_obj; |
457 | av = (AV *)mg->mg_obj; |
396 | |
458 | |
397 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
459 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
398 | av_extend (av, AvMAX (av) + 1); |
460 | av_extend (av, AvMAX (av) + 1); |
… | |
… | |
464 | |
526 | |
465 | if (expect_true (CvDEPTH (cv))) |
527 | if (expect_true (CvDEPTH (cv))) |
466 | { |
528 | { |
467 | EXTEND (SP, 3); |
529 | EXTEND (SP, 3); |
468 | PUSHs ((SV *)CvPADLIST (cv)); |
530 | PUSHs ((SV *)CvPADLIST (cv)); |
469 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
531 | PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); |
470 | PUSHs ((SV *)cv); |
532 | PUSHs ((SV *)cv); |
471 | |
533 | |
472 | CvDEPTH (cv) = 0; |
534 | CvDEPTH (cv) = 0; |
473 | get_padlist (aTHX_ cv); |
535 | get_padlist (aTHX_ cv); |
474 | } |
536 | } |
… | |
… | |
485 | |
547 | |
486 | PUTBACK; |
548 | PUTBACK; |
487 | } |
549 | } |
488 | |
550 | |
489 | /* allocate some space on the context stack for our purposes */ |
551 | /* allocate some space on the context stack for our purposes */ |
|
|
552 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
553 | if (SLOT_COUNT >= 1) CXINC; |
|
|
554 | if (SLOT_COUNT >= 2) CXINC; |
|
|
555 | if (SLOT_COUNT >= 3) CXINC; |
490 | { |
556 | { |
491 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
492 | int i; |
557 | int i; |
493 | if (SLOT_COUNT >= 1) CXINC; |
|
|
494 | if (SLOT_COUNT >= 2) CXINC; |
|
|
495 | if (SLOT_COUNT >= 3) CXINC; |
|
|
496 | for (i = 3; i < SLOT_COUNT; ++i) |
558 | for (i = 3; i < SLOT_COUNT; ++i) |
497 | CXINC; |
559 | CXINC; |
498 | |
|
|
499 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
|
|
500 | } |
560 | } |
|
|
561 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
501 | |
562 | |
502 | c->mainstack = PL_mainstack; |
563 | c->mainstack = PL_mainstack; |
503 | |
564 | |
504 | { |
565 | { |
505 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
566 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
… | |
… | |
555 | |
616 | |
556 | New(54,PL_savestack,24,ANY); |
617 | New(54,PL_savestack,24,ANY); |
557 | PL_savestack_ix = 0; |
618 | PL_savestack_ix = 0; |
558 | PL_savestack_max = 24; |
619 | PL_savestack_max = 24; |
559 | |
620 | |
560 | #if !PERL_VERSION_ATLEAST (5,9,0) |
621 | #if !PERL_VERSION_ATLEAST (5,10,0) |
561 | New(54,PL_retstack,4,OP*); |
622 | New(54,PL_retstack,4,OP*); |
562 | PL_retstack_ix = 0; |
623 | PL_retstack_ix = 0; |
563 | PL_retstack_max = 4; |
624 | PL_retstack_max = 4; |
564 | #endif |
625 | #endif |
565 | } |
626 | } |
… | |
… | |
588 | |
649 | |
589 | Safefree (PL_tmps_stack); |
650 | Safefree (PL_tmps_stack); |
590 | Safefree (PL_markstack); |
651 | Safefree (PL_markstack); |
591 | Safefree (PL_scopestack); |
652 | Safefree (PL_scopestack); |
592 | Safefree (PL_savestack); |
653 | Safefree (PL_savestack); |
593 | #if !PERL_VERSION_ATLEAST (5,9,0) |
654 | #if !PERL_VERSION_ATLEAST (5,10,0) |
594 | Safefree (PL_retstack); |
655 | Safefree (PL_retstack); |
595 | #endif |
656 | #endif |
596 | } |
657 | } |
597 | |
658 | |
598 | static size_t |
659 | static size_t |
… | |
… | |
614 | #undef VAR |
675 | #undef VAR |
615 | } |
676 | } |
616 | else |
677 | else |
617 | slot = coro->slot; |
678 | slot = coro->slot; |
618 | |
679 | |
|
|
680 | if (slot) |
|
|
681 | { |
619 | rss += sizeof (slot->curstackinfo); |
682 | rss += sizeof (slot->curstackinfo); |
620 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
683 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
621 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
684 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
622 | rss += slot->tmps_max * sizeof (SV *); |
685 | rss += slot->tmps_max * sizeof (SV *); |
623 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
686 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
624 | rss += slot->scopestack_max * sizeof (I32); |
687 | rss += slot->scopestack_max * sizeof (I32); |
625 | rss += slot->savestack_max * sizeof (ANY); |
688 | rss += slot->savestack_max * sizeof (ANY); |
626 | |
689 | |
627 | #if !PERL_VERSION_ATLEAST (5,9,0) |
690 | #if !PERL_VERSION_ATLEAST (5,10,0) |
628 | rss += slot->retstack_max * sizeof (OP *); |
691 | rss += slot->retstack_max * sizeof (OP *); |
629 | #endif |
692 | #endif |
|
|
693 | } |
630 | } |
694 | } |
631 | |
695 | |
632 | return rss; |
696 | return rss; |
633 | } |
697 | } |
634 | |
698 | |
635 | /** coroutine stack handling ************************************************/ |
699 | /** coroutine stack handling ************************************************/ |
|
|
700 | |
|
|
701 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
|
|
702 | static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); |
|
|
703 | static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); |
|
|
704 | |
|
|
705 | /* apparently < 5.8.8 */ |
|
|
706 | #ifndef MgPV_nolen_const |
|
|
707 | #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ |
|
|
708 | SvPV_nolen((SV*)((mg)->mg_ptr)) : \ |
|
|
709 | (const char*)(mg)->mg_ptr) |
|
|
710 | #endif |
|
|
711 | |
|
|
712 | /* |
|
|
713 | * This overrides the default magic get method of %SIG elements. |
|
|
714 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
|
|
715 | * and instead of tryign to save and restore the hash elements, we just provide |
|
|
716 | * readback here. |
|
|
717 | * We only do this when the hook is != 0, as they are often set to 0 temporarily, |
|
|
718 | * not expecting this to actually change the hook. This is a potential problem |
|
|
719 | * when a schedule happens then, but we ignore this. |
|
|
720 | */ |
|
|
721 | static int |
|
|
722 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
|
|
723 | { |
|
|
724 | const char *s = MgPV_nolen_const (mg); |
|
|
725 | |
|
|
726 | if (*s == '_') |
|
|
727 | { |
|
|
728 | SV **svp = 0; |
|
|
729 | |
|
|
730 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
731 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
732 | |
|
|
733 | if (svp) |
|
|
734 | { |
|
|
735 | sv_setsv (sv, *svp ? *svp : &PL_sv_undef); |
|
|
736 | return 0; |
|
|
737 | } |
|
|
738 | } |
|
|
739 | |
|
|
740 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
|
|
741 | } |
|
|
742 | |
|
|
743 | static int |
|
|
744 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
|
|
745 | { |
|
|
746 | const char *s = MgPV_nolen_const (mg); |
|
|
747 | |
|
|
748 | if (*s == '_') |
|
|
749 | { |
|
|
750 | SV **svp = 0; |
|
|
751 | |
|
|
752 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
753 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
754 | |
|
|
755 | if (svp) |
|
|
756 | { |
|
|
757 | SV *old = *svp; |
|
|
758 | *svp = 0; |
|
|
759 | SvREFCNT_dec (old); |
|
|
760 | return 0; |
|
|
761 | } |
|
|
762 | } |
|
|
763 | |
|
|
764 | return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; |
|
|
765 | } |
|
|
766 | |
|
|
767 | static int |
|
|
768 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
|
|
769 | { |
|
|
770 | const char *s = MgPV_nolen_const (mg); |
|
|
771 | |
|
|
772 | if (*s == '_') |
|
|
773 | { |
|
|
774 | SV **svp = 0; |
|
|
775 | |
|
|
776 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
777 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
778 | |
|
|
779 | if (svp) |
|
|
780 | { |
|
|
781 | SV *old = *svp; |
|
|
782 | *svp = newSVsv (sv); |
|
|
783 | SvREFCNT_dec (old); |
|
|
784 | return 0; |
|
|
785 | } |
|
|
786 | } |
|
|
787 | |
|
|
788 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
|
|
789 | } |
636 | |
790 | |
637 | static void |
791 | static void |
638 | coro_setup (pTHX_ struct coro *coro) |
792 | coro_setup (pTHX_ struct coro *coro) |
639 | { |
793 | { |
640 | /* |
794 | /* |
… | |
… | |
649 | PL_curpm = 0; |
803 | PL_curpm = 0; |
650 | PL_curpad = 0; |
804 | PL_curpad = 0; |
651 | PL_localizing = 0; |
805 | PL_localizing = 0; |
652 | PL_dirty = 0; |
806 | PL_dirty = 0; |
653 | PL_restartop = 0; |
807 | PL_restartop = 0; |
654 | PL_diehook = 0; |
808 | #if PERL_VERSION_ATLEAST (5,10,0) |
655 | PL_warnhook = 0; |
809 | PL_parser = 0; |
|
|
810 | #endif |
|
|
811 | |
|
|
812 | /* recreate the die/warn hooks */ |
|
|
813 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
|
|
814 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
656 | |
815 | |
657 | GvSV (PL_defgv) = newSV (0); |
816 | GvSV (PL_defgv) = newSV (0); |
658 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
817 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
659 | GvSV (PL_errgv) = newSV (0); |
818 | GvSV (PL_errgv) = newSV (0); |
660 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
819 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
661 | PL_rs = newSVsv (GvSV (irsgv)); |
820 | PL_rs = newSVsv (GvSV (irsgv)); |
662 | PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); |
821 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
663 | |
822 | |
664 | { |
823 | { |
665 | dSP; |
824 | dSP; |
666 | LOGOP myop; |
825 | LOGOP myop; |
667 | |
826 | |
… | |
… | |
675 | PL_op = (OP *)&myop; |
834 | PL_op = (OP *)&myop; |
676 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
835 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
677 | SPAGAIN; |
836 | SPAGAIN; |
678 | } |
837 | } |
679 | |
838 | |
680 | ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ |
839 | /* this newly created coroutine might be run on an existing cctx which most |
|
|
840 | * likely was suspended in set_stacklevel, called from entersub. |
|
|
841 | * set_stacklevl doesn't do anything on return, but entersub does LEAVE, |
|
|
842 | * so we ENTER here for symmetry |
|
|
843 | */ |
|
|
844 | ENTER; |
681 | } |
845 | } |
682 | |
846 | |
683 | static void |
847 | static void |
684 | coro_destroy (pTHX_ struct coro *coro) |
848 | coro_destroy (pTHX_ struct coro *coro) |
685 | { |
849 | { |
… | |
… | |
758 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
922 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
759 | : bot; |
923 | : bot; |
760 | |
924 | |
761 | av_extend (av, top - bot); |
925 | av_extend (av, top - bot); |
762 | while (bot < top) |
926 | while (bot < top) |
763 | av_push (av, SvREFCNT_inc (*bot++)); |
927 | av_push (av, SvREFCNT_inc_NN (*bot++)); |
764 | |
928 | |
765 | PL_runops = RUNOPS_DEFAULT; |
929 | PL_runops = RUNOPS_DEFAULT; |
766 | ENTER; |
930 | ENTER; |
767 | SAVETMPS; |
931 | SAVETMPS; |
768 | EXTEND (SP, 3); |
932 | EXTEND (SP, 3); |
… | |
… | |
806 | SAVETMPS; |
970 | SAVETMPS; |
807 | EXTEND (SP, 3); |
971 | EXTEND (SP, 3); |
808 | PUSHMARK (SP); |
972 | PUSHMARK (SP); |
809 | PUSHs (&PL_sv_yes); |
973 | PUSHs (&PL_sv_yes); |
810 | PUSHs (fullname); |
974 | PUSHs (fullname); |
811 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
975 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
812 | PUTBACK; |
976 | PUTBACK; |
813 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
977 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
814 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
978 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
815 | SPAGAIN; |
979 | SPAGAIN; |
816 | FREETMPS; |
980 | FREETMPS; |
… | |
… | |
852 | |
1016 | |
853 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
1017 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
854 | /* _cctx_init should be careful, as it could be called at almost any time */ |
1018 | /* _cctx_init should be careful, as it could be called at almost any time */ |
855 | /* during execution of a perl program */ |
1019 | /* during execution of a perl program */ |
856 | static void NOINLINE |
1020 | static void NOINLINE |
857 | prepare_cctx (pTHX_ coro_cctx *cctx) |
1021 | cctx_prepare (pTHX_ coro_cctx *cctx) |
858 | { |
1022 | { |
859 | dSP; |
1023 | dSP; |
860 | LOGOP myop; |
1024 | LOGOP myop; |
861 | |
1025 | |
862 | PL_top_env = &PL_start_env; |
1026 | PL_top_env = &PL_start_env; |
… | |
… | |
880 | |
1044 | |
881 | /* |
1045 | /* |
882 | * this is a _very_ stripped down perl interpreter ;) |
1046 | * this is a _very_ stripped down perl interpreter ;) |
883 | */ |
1047 | */ |
884 | static void |
1048 | static void |
885 | coro_run (void *arg) |
1049 | cctx_run (void *arg) |
886 | { |
1050 | { |
887 | dTHX; |
1051 | dTHX; |
888 | |
1052 | |
889 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
1053 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
890 | UNLOCK; |
1054 | UNLOCK; |
891 | |
1055 | |
892 | /* we now skip the entersub that lead to transfer() */ |
1056 | /* we now skip the entersub that lead to transfer() */ |
893 | PL_op = PL_op->op_next; |
1057 | PL_op = PL_op->op_next; |
894 | |
1058 | |
895 | /* inject a fake subroutine call to cctx_init */ |
1059 | /* inject a fake subroutine call to cctx_init */ |
896 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
1060 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
897 | |
1061 | |
898 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1062 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
899 | PL_restartop = PL_op; |
1063 | PL_restartop = PL_op; |
900 | perl_run (PL_curinterp); |
1064 | perl_run (PL_curinterp); |
901 | |
1065 | |
… | |
… | |
920 | ++cctx_count; |
1084 | ++cctx_count; |
921 | |
1085 | |
922 | Newz (0, cctx, 1, coro_cctx); |
1086 | Newz (0, cctx, 1, coro_cctx); |
923 | |
1087 | |
924 | #if HAVE_MMAP |
1088 | #if HAVE_MMAP |
925 | |
|
|
926 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1089 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
927 | /* mmap supposedly does allocate-on-write for us */ |
1090 | /* mmap supposedly does allocate-on-write for us */ |
928 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1091 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
929 | |
1092 | |
930 | if (cctx->sptr != (void *)-1) |
1093 | if (cctx->sptr != (void *)-1) |
… | |
… | |
951 | stack_start = cctx->sptr; |
1114 | stack_start = cctx->sptr; |
952 | stack_size = cctx->ssize; |
1115 | stack_size = cctx->ssize; |
953 | } |
1116 | } |
954 | |
1117 | |
955 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
1118 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
956 | coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); |
1119 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
957 | |
1120 | |
958 | return cctx; |
1121 | return cctx; |
959 | } |
1122 | } |
960 | |
1123 | |
961 | static void |
1124 | static void |
… | |
… | |
1033 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
1196 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
1034 | |
1197 | |
1035 | if (expect_false (next->flags & CF_DESTROYED)) |
1198 | if (expect_false (next->flags & CF_DESTROYED)) |
1036 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
1199 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
1037 | |
1200 | |
1038 | if ( |
|
|
1039 | #if PERL_VERSION_ATLEAST (5,9,0) |
1201 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1040 | expect_false (PL_parser) |
|
|
1041 | #else |
|
|
1042 | expect_false (PL_lex_state != LEX_NOTPARSING) |
1202 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1043 | #endif |
|
|
1044 | ) |
|
|
1045 | croak ("Coro::State::transfer called while parsing, but this is not supported"); |
1203 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version"); |
|
|
1204 | #endif |
1046 | } |
1205 | } |
1047 | } |
1206 | } |
1048 | |
1207 | |
1049 | /* always use the TRANSFER macro */ |
1208 | /* always use the TRANSFER macro */ |
1050 | static void NOINLINE |
1209 | static void NOINLINE |
1051 | transfer (pTHX_ struct coro *prev, struct coro *next) |
1210 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1052 | { |
1211 | { |
1053 | dSTACKLEVEL; |
1212 | dSTACKLEVEL; |
|
|
1213 | static volatile int has_throw; |
1054 | |
1214 | |
1055 | /* sometimes transfer is only called to set idle_sp */ |
1215 | /* sometimes transfer is only called to set idle_sp */ |
1056 | if (expect_false (!next)) |
1216 | if (expect_false (!next)) |
1057 | { |
1217 | { |
1058 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1218 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
… | |
… | |
1073 | prev->flags &= ~CF_RUNNING; |
1233 | prev->flags &= ~CF_RUNNING; |
1074 | next->flags |= CF_RUNNING; |
1234 | next->flags |= CF_RUNNING; |
1075 | |
1235 | |
1076 | LOCK; |
1236 | LOCK; |
1077 | |
1237 | |
|
|
1238 | /* first get rid of the old state */ |
|
|
1239 | save_perl (aTHX_ prev); |
|
|
1240 | |
1078 | if (expect_false (next->flags & CF_NEW)) |
1241 | if (expect_false (next->flags & CF_NEW)) |
1079 | { |
1242 | { |
1080 | /* need to start coroutine */ |
1243 | /* need to start coroutine */ |
1081 | next->flags &= ~CF_NEW; |
1244 | next->flags &= ~CF_NEW; |
1082 | /* first get rid of the old state */ |
|
|
1083 | save_perl (aTHX_ prev); |
|
|
1084 | /* setup coroutine call */ |
1245 | /* setup coroutine call */ |
1085 | coro_setup (aTHX_ next); |
1246 | coro_setup (aTHX_ next); |
1086 | } |
1247 | } |
1087 | else |
1248 | else |
1088 | { |
|
|
1089 | /* coroutine already started */ |
|
|
1090 | save_perl (aTHX_ prev); |
|
|
1091 | load_perl (aTHX_ next); |
1249 | load_perl (aTHX_ next); |
1092 | } |
|
|
1093 | |
1250 | |
1094 | prev__cctx = prev->cctx; |
1251 | prev__cctx = prev->cctx; |
1095 | |
1252 | |
1096 | /* possibly "free" the cctx */ |
1253 | /* possibly "free" the cctx */ |
1097 | if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) |
1254 | if (expect_true ( |
|
|
1255 | prev__cctx->idle_sp == STACKLEVEL |
|
|
1256 | && !(prev__cctx->flags & CC_TRACE) |
|
|
1257 | && !force_cctx |
|
|
1258 | )) |
1098 | { |
1259 | { |
1099 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1260 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1100 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1261 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1101 | |
1262 | |
1102 | prev->cctx = 0; |
1263 | prev->cctx = 0; |
1103 | |
1264 | |
1104 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1265 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1105 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1266 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1106 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1267 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
|
|
1268 | if (!next->cctx) |
1107 | next->cctx = cctx_get (aTHX); |
1269 | next->cctx = cctx_get (aTHX); |
1108 | |
1270 | |
1109 | cctx_put (prev__cctx); |
1271 | cctx_put (prev__cctx); |
1110 | } |
1272 | } |
1111 | |
1273 | |
1112 | ++next->usecount; |
1274 | ++next->usecount; |
1113 | |
1275 | |
1114 | if (expect_true (!next->cctx)) |
1276 | if (expect_true (!next->cctx)) |
1115 | next->cctx = cctx_get (aTHX); |
1277 | next->cctx = cctx_get (aTHX); |
|
|
1278 | |
|
|
1279 | has_throw = !!next->throw; |
1116 | |
1280 | |
1117 | if (expect_false (prev__cctx != next->cctx)) |
1281 | if (expect_false (prev__cctx != next->cctx)) |
1118 | { |
1282 | { |
1119 | prev__cctx->top_env = PL_top_env; |
1283 | prev__cctx->top_env = PL_top_env; |
1120 | PL_top_env = next->cctx->top_env; |
1284 | PL_top_env = next->cctx->top_env; |
… | |
… | |
1122 | } |
1286 | } |
1123 | |
1287 | |
1124 | free_coro_mortal (aTHX); |
1288 | free_coro_mortal (aTHX); |
1125 | UNLOCK; |
1289 | UNLOCK; |
1126 | |
1290 | |
1127 | if (expect_false (prev->throw || next->throw)) |
1291 | if (expect_false (has_throw)) |
1128 | { |
1292 | { |
1129 | struct coro *coro = SvSTATE (coro_current); |
1293 | struct coro *coro = SvSTATE (coro_current); |
1130 | |
1294 | |
1131 | if (coro->throw) |
1295 | if (coro->throw) |
1132 | { |
1296 | { |
… | |
… | |
1142 | struct transfer_args |
1306 | struct transfer_args |
1143 | { |
1307 | { |
1144 | struct coro *prev, *next; |
1308 | struct coro *prev, *next; |
1145 | }; |
1309 | }; |
1146 | |
1310 | |
1147 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
1311 | #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) |
1148 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1312 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1149 | |
1313 | |
1150 | /** high level stuff ********************************************************/ |
1314 | /** high level stuff ********************************************************/ |
1151 | |
1315 | |
1152 | static int |
1316 | static int |
… | |
… | |
1246 | { |
1410 | { |
1247 | dTHX; |
1411 | dTHX; |
1248 | struct transfer_args ta; |
1412 | struct transfer_args ta; |
1249 | |
1413 | |
1250 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1414 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1251 | TRANSFER (ta); |
1415 | TRANSFER (ta, 1); |
1252 | } |
1416 | } |
1253 | |
1417 | |
1254 | /** Coro ********************************************************************/ |
1418 | /** Coro ********************************************************************/ |
1255 | |
1419 | |
1256 | static void |
1420 | static void |
… | |
… | |
1258 | { |
1422 | { |
1259 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1423 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1260 | } |
1424 | } |
1261 | |
1425 | |
1262 | static SV * |
1426 | static SV * |
1263 | coro_deq (pTHX_ int min_prio) |
1427 | coro_deq (pTHX) |
1264 | { |
1428 | { |
1265 | int prio = PRIO_MAX - PRIO_MIN; |
1429 | int prio; |
1266 | |
1430 | |
1267 | min_prio -= PRIO_MIN; |
|
|
1268 | if (min_prio < 0) |
|
|
1269 | min_prio = 0; |
|
|
1270 | |
|
|
1271 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
1431 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1272 | if (AvFILLp (coro_ready [prio]) >= 0) |
1432 | if (AvFILLp (coro_ready [prio]) >= 0) |
1273 | return av_shift (coro_ready [prio]); |
1433 | return av_shift (coro_ready [prio]); |
1274 | |
1434 | |
1275 | return 0; |
1435 | return 0; |
1276 | } |
1436 | } |
… | |
… | |
1278 | static int |
1438 | static int |
1279 | api_ready (SV *coro_sv) |
1439 | api_ready (SV *coro_sv) |
1280 | { |
1440 | { |
1281 | dTHX; |
1441 | dTHX; |
1282 | struct coro *coro; |
1442 | struct coro *coro; |
|
|
1443 | SV *sv_hook; |
|
|
1444 | void (*xs_hook)(void); |
1283 | |
1445 | |
1284 | if (SvROK (coro_sv)) |
1446 | if (SvROK (coro_sv)) |
1285 | coro_sv = SvRV (coro_sv); |
1447 | coro_sv = SvRV (coro_sv); |
1286 | |
1448 | |
1287 | coro = SvSTATE (coro_sv); |
1449 | coro = SvSTATE (coro_sv); |
… | |
… | |
1290 | return 0; |
1452 | return 0; |
1291 | |
1453 | |
1292 | coro->flags |= CF_READY; |
1454 | coro->flags |= CF_READY; |
1293 | |
1455 | |
1294 | LOCK; |
1456 | LOCK; |
|
|
1457 | |
|
|
1458 | sv_hook = coro_nready ? 0 : coro_readyhook; |
|
|
1459 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
|
|
1460 | |
1295 | coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); |
1461 | coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv)); |
1296 | ++coro_nready; |
1462 | ++coro_nready; |
|
|
1463 | |
1297 | UNLOCK; |
1464 | UNLOCK; |
|
|
1465 | |
|
|
1466 | if (sv_hook) |
|
|
1467 | { |
|
|
1468 | dSP; |
|
|
1469 | |
|
|
1470 | ENTER; |
|
|
1471 | SAVETMPS; |
|
|
1472 | |
|
|
1473 | PUSHMARK (SP); |
|
|
1474 | PUTBACK; |
|
|
1475 | call_sv (sv_hook, G_DISCARD); |
|
|
1476 | SPAGAIN; |
|
|
1477 | |
|
|
1478 | FREETMPS; |
|
|
1479 | LEAVE; |
|
|
1480 | } |
|
|
1481 | |
|
|
1482 | if (xs_hook) |
|
|
1483 | xs_hook (); |
1298 | |
1484 | |
1299 | return 1; |
1485 | return 1; |
1300 | } |
1486 | } |
1301 | |
1487 | |
1302 | static int |
1488 | static int |
… | |
… | |
1312 | SV *prev_sv, *next_sv; |
1498 | SV *prev_sv, *next_sv; |
1313 | |
1499 | |
1314 | for (;;) |
1500 | for (;;) |
1315 | { |
1501 | { |
1316 | LOCK; |
1502 | LOCK; |
1317 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
1503 | next_sv = coro_deq (aTHX); |
1318 | |
1504 | |
1319 | /* nothing to schedule: call the idle handler */ |
1505 | /* nothing to schedule: call the idle handler */ |
1320 | if (expect_false (!next_sv)) |
1506 | if (expect_false (!next_sv)) |
1321 | { |
1507 | { |
1322 | dSP; |
1508 | dSP; |
… | |
… | |
1326 | SAVETMPS; |
1512 | SAVETMPS; |
1327 | |
1513 | |
1328 | PUSHMARK (SP); |
1514 | PUSHMARK (SP); |
1329 | PUTBACK; |
1515 | PUTBACK; |
1330 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
1516 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1517 | SPAGAIN; |
1331 | |
1518 | |
1332 | FREETMPS; |
1519 | FREETMPS; |
1333 | LEAVE; |
1520 | LEAVE; |
1334 | continue; |
1521 | continue; |
1335 | } |
1522 | } |
… | |
… | |
1390 | { |
1577 | { |
1391 | dTHX; |
1578 | dTHX; |
1392 | struct transfer_args ta; |
1579 | struct transfer_args ta; |
1393 | |
1580 | |
1394 | prepare_schedule (aTHX_ &ta); |
1581 | prepare_schedule (aTHX_ &ta); |
1395 | TRANSFER (ta); |
1582 | TRANSFER (ta, 1); |
1396 | } |
1583 | } |
1397 | |
1584 | |
1398 | static int |
1585 | static int |
1399 | api_cede (void) |
1586 | api_cede (void) |
1400 | { |
1587 | { |
… | |
… | |
1403 | |
1590 | |
1404 | prepare_cede (aTHX_ &ta); |
1591 | prepare_cede (aTHX_ &ta); |
1405 | |
1592 | |
1406 | if (expect_true (ta.prev != ta.next)) |
1593 | if (expect_true (ta.prev != ta.next)) |
1407 | { |
1594 | { |
1408 | TRANSFER (ta); |
1595 | TRANSFER (ta, 1); |
1409 | return 1; |
1596 | return 1; |
1410 | } |
1597 | } |
1411 | else |
1598 | else |
1412 | return 0; |
1599 | return 0; |
1413 | } |
1600 | } |
… | |
… | |
1418 | dTHX; |
1605 | dTHX; |
1419 | struct transfer_args ta; |
1606 | struct transfer_args ta; |
1420 | |
1607 | |
1421 | if (prepare_cede_notself (aTHX_ &ta)) |
1608 | if (prepare_cede_notself (aTHX_ &ta)) |
1422 | { |
1609 | { |
1423 | TRANSFER (ta); |
1610 | TRANSFER (ta, 1); |
1424 | return 1; |
1611 | return 1; |
1425 | } |
1612 | } |
1426 | else |
1613 | else |
1427 | return 0; |
1614 | return 0; |
1428 | } |
1615 | } |
… | |
… | |
1451 | else |
1638 | else |
1452 | coro->slot->runops = RUNOPS_DEFAULT; |
1639 | coro->slot->runops = RUNOPS_DEFAULT; |
1453 | } |
1640 | } |
1454 | } |
1641 | } |
1455 | |
1642 | |
|
|
1643 | static int |
|
|
1644 | coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
1645 | { |
|
|
1646 | AV *padlist; |
|
|
1647 | AV *av = (AV *)mg->mg_obj; |
|
|
1648 | |
|
|
1649 | abort (); |
|
|
1650 | |
|
|
1651 | return 0; |
|
|
1652 | } |
|
|
1653 | |
|
|
1654 | static MGVTBL coro_gensub_vtbl = { |
|
|
1655 | 0, 0, 0, 0, |
|
|
1656 | coro_gensub_free |
|
|
1657 | }; |
|
|
1658 | |
|
|
1659 | /*****************************************************************************/ |
|
|
1660 | /* PerlIO::cede */ |
|
|
1661 | |
|
|
1662 | typedef struct |
|
|
1663 | { |
|
|
1664 | PerlIOBuf base; |
|
|
1665 | NV next, every; |
|
|
1666 | } PerlIOCede; |
|
|
1667 | |
|
|
1668 | static IV |
|
|
1669 | PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
|
|
1670 | { |
|
|
1671 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1672 | |
|
|
1673 | self->every = SvCUR (arg) ? SvNV (arg) : 0.01; |
|
|
1674 | self->next = nvtime () + self->every; |
|
|
1675 | |
|
|
1676 | return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); |
|
|
1677 | } |
|
|
1678 | |
|
|
1679 | static SV * |
|
|
1680 | PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
|
|
1681 | { |
|
|
1682 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1683 | |
|
|
1684 | return newSVnv (self->every); |
|
|
1685 | } |
|
|
1686 | |
|
|
1687 | static IV |
|
|
1688 | PerlIOCede_flush (pTHX_ PerlIO *f) |
|
|
1689 | { |
|
|
1690 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
|
|
1691 | double now = nvtime (); |
|
|
1692 | |
|
|
1693 | if (now >= self->next) |
|
|
1694 | { |
|
|
1695 | api_cede (); |
|
|
1696 | self->next = now + self->every; |
|
|
1697 | } |
|
|
1698 | |
|
|
1699 | return PerlIOBuf_flush (aTHX_ f); |
|
|
1700 | } |
|
|
1701 | |
|
|
1702 | static PerlIO_funcs PerlIO_cede = |
|
|
1703 | { |
|
|
1704 | sizeof(PerlIO_funcs), |
|
|
1705 | "cede", |
|
|
1706 | sizeof(PerlIOCede), |
|
|
1707 | PERLIO_K_DESTRUCT | PERLIO_K_RAW, |
|
|
1708 | PerlIOCede_pushed, |
|
|
1709 | PerlIOBuf_popped, |
|
|
1710 | PerlIOBuf_open, |
|
|
1711 | PerlIOBase_binmode, |
|
|
1712 | PerlIOCede_getarg, |
|
|
1713 | PerlIOBase_fileno, |
|
|
1714 | PerlIOBuf_dup, |
|
|
1715 | PerlIOBuf_read, |
|
|
1716 | PerlIOBuf_unread, |
|
|
1717 | PerlIOBuf_write, |
|
|
1718 | PerlIOBuf_seek, |
|
|
1719 | PerlIOBuf_tell, |
|
|
1720 | PerlIOBuf_close, |
|
|
1721 | PerlIOCede_flush, |
|
|
1722 | PerlIOBuf_fill, |
|
|
1723 | PerlIOBase_eof, |
|
|
1724 | PerlIOBase_error, |
|
|
1725 | PerlIOBase_clearerr, |
|
|
1726 | PerlIOBase_setlinebuf, |
|
|
1727 | PerlIOBuf_get_base, |
|
|
1728 | PerlIOBuf_bufsiz, |
|
|
1729 | PerlIOBuf_get_ptr, |
|
|
1730 | PerlIOBuf_get_cnt, |
|
|
1731 | PerlIOBuf_set_ptrcnt, |
|
|
1732 | }; |
|
|
1733 | |
|
|
1734 | |
1456 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1735 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1457 | |
1736 | |
1458 | PROTOTYPES: DISABLE |
1737 | PROTOTYPES: DISABLE |
1459 | |
1738 | |
1460 | BOOT: |
1739 | BOOT: |
… | |
… | |
1464 | #endif |
1743 | #endif |
1465 | BOOT_PAGESIZE; |
1744 | BOOT_PAGESIZE; |
1466 | |
1745 | |
1467 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1746 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1468 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1747 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
|
|
1748 | |
|
|
1749 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
|
|
1750 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
|
|
1751 | orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr; |
|
|
1752 | |
|
|
1753 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
|
|
1754 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
|
|
1755 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
1469 | |
1756 | |
1470 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1757 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1471 | |
1758 | |
1472 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1759 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1473 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1760 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
… | |
… | |
1478 | main_top_env = PL_top_env; |
1765 | main_top_env = PL_top_env; |
1479 | |
1766 | |
1480 | while (main_top_env->je_prev) |
1767 | while (main_top_env->je_prev) |
1481 | main_top_env = main_top_env->je_prev; |
1768 | main_top_env = main_top_env->je_prev; |
1482 | |
1769 | |
1483 | coroapi.ver = CORO_API_VERSION; |
1770 | coroapi.ver = CORO_API_VERSION; |
|
|
1771 | coroapi.rev = CORO_API_REVISION; |
1484 | coroapi.transfer = api_transfer; |
1772 | coroapi.transfer = api_transfer; |
|
|
1773 | |
|
|
1774 | { |
|
|
1775 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
|
|
1776 | |
|
|
1777 | if (!svp) croak ("Time::HiRes is required"); |
|
|
1778 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
|
|
1779 | |
|
|
1780 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
1781 | } |
1485 | |
1782 | |
1486 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1783 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1487 | } |
1784 | } |
1488 | |
1785 | |
1489 | SV * |
1786 | SV * |
1490 | new (char *klass, ...) |
1787 | new (char *klass, ...) |
1491 | CODE: |
1788 | CODE: |
1492 | { |
1789 | { |
1493 | struct coro *coro; |
1790 | struct coro *coro; |
|
|
1791 | MAGIC *mg; |
1494 | HV *hv; |
1792 | HV *hv; |
1495 | int i; |
1793 | int i; |
1496 | |
1794 | |
1497 | Newz (0, coro, 1, struct coro); |
1795 | Newz (0, coro, 1, struct coro); |
1498 | coro->args = newAV (); |
1796 | coro->args = newAV (); |
… | |
… | |
1501 | if (coro_first) coro_first->prev = coro; |
1799 | if (coro_first) coro_first->prev = coro; |
1502 | coro->next = coro_first; |
1800 | coro->next = coro_first; |
1503 | coro_first = coro; |
1801 | coro_first = coro; |
1504 | |
1802 | |
1505 | coro->hv = hv = newHV (); |
1803 | coro->hv = hv = newHV (); |
1506 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1804 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
1805 | mg->mg_flags |= MGf_DUP; |
1507 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1806 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1508 | |
1807 | |
1509 | av_extend (coro->args, items - 1); |
1808 | av_extend (coro->args, items - 1); |
1510 | for (i = 1; i < items; i++) |
1809 | for (i = 1; i < items; i++) |
1511 | av_push (coro->args, newSVsv (ST (i))); |
1810 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1525 | Coro::cede_notself = 4 |
1824 | Coro::cede_notself = 4 |
1526 | CODE: |
1825 | CODE: |
1527 | { |
1826 | { |
1528 | struct transfer_args ta; |
1827 | struct transfer_args ta; |
1529 | |
1828 | |
|
|
1829 | PUTBACK; |
1530 | switch (ix) |
1830 | switch (ix) |
1531 | { |
1831 | { |
1532 | case 0: |
1832 | case 0: |
1533 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1833 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1534 | ta.next = 0; |
1834 | ta.next = 0; |
… | |
… | |
1553 | if (!prepare_cede_notself (aTHX_ &ta)) |
1853 | if (!prepare_cede_notself (aTHX_ &ta)) |
1554 | XSRETURN_EMPTY; |
1854 | XSRETURN_EMPTY; |
1555 | |
1855 | |
1556 | break; |
1856 | break; |
1557 | } |
1857 | } |
|
|
1858 | SPAGAIN; |
1558 | |
1859 | |
1559 | BARRIER; |
1860 | BARRIER; |
|
|
1861 | PUTBACK; |
1560 | TRANSFER (ta); |
1862 | TRANSFER (ta, 0); |
1561 | |
1863 | SPAGAIN; /* might be the sp of a different coroutine now */ |
1562 | if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) |
1864 | /* be extra careful not to ever do anything after TRANSFER */ |
1563 | XSRETURN_YES; |
|
|
1564 | } |
1865 | } |
1565 | |
1866 | |
1566 | bool |
1867 | bool |
1567 | _destroy (SV *coro_sv) |
1868 | _destroy (SV *coro_sv) |
1568 | CODE: |
1869 | CODE: |
1569 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
1870 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
1570 | OUTPUT: |
1871 | OUTPUT: |
1571 | RETVAL |
1872 | RETVAL |
1572 | |
1873 | |
1573 | void |
1874 | void |
1574 | _exit (code) |
1875 | _exit (int code) |
1575 | int code |
|
|
1576 | PROTOTYPE: $ |
1876 | PROTOTYPE: $ |
1577 | CODE: |
1877 | CODE: |
1578 | _exit (code); |
1878 | _exit (code); |
1579 | |
1879 | |
1580 | int |
1880 | int |
… | |
… | |
1614 | call (Coro::State coro, SV *coderef) |
1914 | call (Coro::State coro, SV *coderef) |
1615 | ALIAS: |
1915 | ALIAS: |
1616 | eval = 1 |
1916 | eval = 1 |
1617 | CODE: |
1917 | CODE: |
1618 | { |
1918 | { |
1619 | if (coro->mainstack) |
1919 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
1620 | { |
1920 | { |
1621 | struct coro temp; |
1921 | struct coro temp; |
1622 | |
1922 | |
1623 | if (!(coro->flags & CF_RUNNING)) |
1923 | if (!(coro->flags & CF_RUNNING)) |
1624 | { |
1924 | { |
|
|
1925 | PUTBACK; |
1625 | save_perl (aTHX_ &temp); |
1926 | save_perl (aTHX_ &temp); |
1626 | load_perl (aTHX_ coro); |
1927 | load_perl (aTHX_ coro); |
1627 | } |
1928 | } |
1628 | |
1929 | |
1629 | { |
1930 | { |
1630 | dSP; |
1931 | dSP; |
1631 | ENTER; |
1932 | ENTER; |
1632 | SAVETMPS; |
1933 | SAVETMPS; |
|
|
1934 | PUTBACK; |
|
|
1935 | PUSHSTACK; |
1633 | PUSHMARK (SP); |
1936 | PUSHMARK (SP); |
1634 | PUTBACK; |
|
|
1635 | |
1937 | |
1636 | if (ix) |
1938 | if (ix) |
1637 | eval_sv (coderef, 0); |
1939 | eval_sv (coderef, 0); |
1638 | else |
1940 | else |
1639 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1941 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1640 | |
1942 | |
|
|
1943 | POPSTACK; |
1641 | SPAGAIN; |
1944 | SPAGAIN; |
1642 | FREETMPS; |
1945 | FREETMPS; |
1643 | LEAVE; |
1946 | LEAVE; |
1644 | PUTBACK; |
1947 | PUTBACK; |
1645 | } |
1948 | } |
1646 | |
1949 | |
1647 | if (!(coro->flags & CF_RUNNING)) |
1950 | if (!(coro->flags & CF_RUNNING)) |
1648 | { |
1951 | { |
1649 | save_perl (aTHX_ coro); |
1952 | save_perl (aTHX_ coro); |
1650 | load_perl (aTHX_ &temp); |
1953 | load_perl (aTHX_ &temp); |
|
|
1954 | SPAGAIN; |
1651 | } |
1955 | } |
1652 | } |
1956 | } |
1653 | } |
1957 | } |
1654 | |
1958 | |
1655 | SV * |
1959 | SV * |
… | |
… | |
1667 | |
1971 | |
1668 | void |
1972 | void |
1669 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
1973 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
1670 | |
1974 | |
1671 | SV * |
1975 | SV * |
1672 | has_stack (Coro::State coro) |
1976 | has_cctx (Coro::State coro) |
1673 | PROTOTYPE: $ |
1977 | PROTOTYPE: $ |
1674 | CODE: |
1978 | CODE: |
1675 | RETVAL = boolSV (!!coro->cctx); |
1979 | RETVAL = boolSV (!!coro->cctx); |
1676 | OUTPUT: |
1980 | OUTPUT: |
1677 | RETVAL |
1981 | RETVAL |
… | |
… | |
1696 | case 1: RETVAL = coro->usecount; break; |
2000 | case 1: RETVAL = coro->usecount; break; |
1697 | } |
2001 | } |
1698 | OUTPUT: |
2002 | OUTPUT: |
1699 | RETVAL |
2003 | RETVAL |
1700 | |
2004 | |
|
|
2005 | void |
|
|
2006 | force_cctx () |
|
|
2007 | CODE: |
|
|
2008 | struct coro *coro = SvSTATE (coro_current); |
|
|
2009 | coro->cctx->idle_sp = 0; |
|
|
2010 | |
|
|
2011 | void |
|
|
2012 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
2013 | PROTOTYPE: $;$ |
|
|
2014 | CODE: |
|
|
2015 | SvREFCNT_dec (self->throw); |
|
|
2016 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
2017 | |
|
|
2018 | void |
|
|
2019 | swap_defsv (Coro::State self) |
|
|
2020 | PROTOTYPE: $ |
|
|
2021 | ALIAS: |
|
|
2022 | swap_defav = 1 |
|
|
2023 | CODE: |
|
|
2024 | if (!self->slot) |
|
|
2025 | croak ("cannot swap state with coroutine that has no saved state"); |
|
|
2026 | else |
|
|
2027 | { |
|
|
2028 | SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv); |
|
|
2029 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
|
|
2030 | |
|
|
2031 | SV *tmp = *src; *src = *dst; *dst = tmp; |
|
|
2032 | } |
1701 | |
2033 | |
1702 | MODULE = Coro::State PACKAGE = Coro |
2034 | MODULE = Coro::State PACKAGE = Coro |
1703 | |
2035 | |
1704 | BOOT: |
2036 | BOOT: |
1705 | { |
2037 | { |
1706 | int i; |
2038 | int i; |
1707 | |
2039 | |
1708 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
|
|
1709 | get_sv ("Coro::POOL_RSS" , TRUE); /* silence stupid 5.10 warning */ |
|
|
1710 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
|
|
1711 | get_sv ("Coro::POOL_SIZE" , TRUE); /* silence stupid 5.10 warning */ |
|
|
1712 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
2040 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
1713 | get_av ("Coro::async_pool", TRUE); /* silence stupid 5.10 warning */ |
2041 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
|
|
2042 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
1714 | |
2043 | |
1715 | coro_current = get_sv ("Coro::current", FALSE); |
2044 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
1716 | SvREADONLY_on (coro_current); |
2045 | SvREADONLY_on (coro_current); |
1717 | |
2046 | |
1718 | coro_stash = gv_stashpv ("Coro", TRUE); |
2047 | coro_stash = gv_stashpv ("Coro", TRUE); |
1719 | |
2048 | |
1720 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2049 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
… | |
… | |
1748 | void |
2077 | void |
1749 | _set_current (SV *current) |
2078 | _set_current (SV *current) |
1750 | PROTOTYPE: $ |
2079 | PROTOTYPE: $ |
1751 | CODE: |
2080 | CODE: |
1752 | SvREFCNT_dec (SvRV (coro_current)); |
2081 | SvREFCNT_dec (SvRV (coro_current)); |
1753 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
2082 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
|
|
2083 | |
|
|
2084 | void |
|
|
2085 | _set_readyhook (SV *hook) |
|
|
2086 | PROTOTYPE: $ |
|
|
2087 | CODE: |
|
|
2088 | LOCK; |
|
|
2089 | SvREFCNT_dec (coro_readyhook); |
|
|
2090 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
|
|
2091 | UNLOCK; |
1754 | |
2092 | |
1755 | int |
2093 | int |
1756 | prio (Coro::State coro, int newprio = 0) |
2094 | prio (Coro::State coro, int newprio = 0) |
1757 | ALIAS: |
2095 | ALIAS: |
1758 | nice = 1 |
2096 | nice = 1 |
… | |
… | |
1788 | CODE: |
2126 | CODE: |
1789 | RETVAL = coro_nready; |
2127 | RETVAL = coro_nready; |
1790 | OUTPUT: |
2128 | OUTPUT: |
1791 | RETVAL |
2129 | RETVAL |
1792 | |
2130 | |
1793 | void |
|
|
1794 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
|
|
1795 | PROTOTYPE: $;$ |
|
|
1796 | CODE: |
|
|
1797 | SvREFCNT_dec (self->throw); |
|
|
1798 | self->throw = SvOK (throw) ? newSVsv (throw) : 0; |
|
|
1799 | |
|
|
1800 | # for async_pool speedup |
2131 | # for async_pool speedup |
1801 | void |
2132 | void |
1802 | _pool_1 (SV *cb) |
2133 | _pool_1 (SV *cb) |
1803 | CODE: |
2134 | CODE: |
1804 | { |
2135 | { |
… | |
… | |
1808 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
2139 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1809 | AV *invoke_av; |
2140 | AV *invoke_av; |
1810 | int i, len; |
2141 | int i, len; |
1811 | |
2142 | |
1812 | if (!invoke) |
2143 | if (!invoke) |
|
|
2144 | { |
|
|
2145 | SV *old = PL_diehook; |
|
|
2146 | PL_diehook = 0; |
|
|
2147 | SvREFCNT_dec (old); |
1813 | croak ("\3async_pool terminate\2\n"); |
2148 | croak ("\3async_pool terminate\2\n"); |
|
|
2149 | } |
1814 | |
2150 | |
1815 | SvREFCNT_dec (coro->saved_deffh); |
2151 | SvREFCNT_dec (coro->saved_deffh); |
1816 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
2152 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
1817 | |
2153 | |
1818 | hv_store (hv, "desc", sizeof ("desc") - 1, |
2154 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1819 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
2155 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1820 | |
2156 | |
1821 | invoke_av = (AV *)SvRV (invoke); |
2157 | invoke_av = (AV *)SvRV (invoke); |
… | |
… | |
1825 | |
2161 | |
1826 | if (len > 0) |
2162 | if (len > 0) |
1827 | { |
2163 | { |
1828 | av_fill (defav, len - 1); |
2164 | av_fill (defav, len - 1); |
1829 | for (i = 0; i < len; ++i) |
2165 | for (i = 0; i < len; ++i) |
1830 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
2166 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
1831 | } |
2167 | } |
1832 | |
2168 | |
1833 | SvREFCNT_dec (invoke); |
2169 | SvREFCNT_dec (invoke); |
1834 | } |
2170 | } |
1835 | |
2171 | |
… | |
… | |
1844 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2180 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
1845 | coro->saved_deffh = 0; |
2181 | coro->saved_deffh = 0; |
1846 | |
2182 | |
1847 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
2183 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1848 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2184 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
2185 | { |
|
|
2186 | SV *old = PL_diehook; |
|
|
2187 | PL_diehook = 0; |
|
|
2188 | SvREFCNT_dec (old); |
1849 | croak ("\3async_pool terminate\2\n"); |
2189 | croak ("\3async_pool terminate\2\n"); |
|
|
2190 | } |
1850 | |
2191 | |
1851 | av_clear (GvAV (PL_defgv)); |
2192 | av_clear (GvAV (PL_defgv)); |
1852 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
2193 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1853 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
2194 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1854 | |
2195 | |
… | |
… | |
1858 | api_trace (coro_current, 0); |
2199 | api_trace (coro_current, 0); |
1859 | |
2200 | |
1860 | av_push (av_async_pool, newSVsv (coro_current)); |
2201 | av_push (av_async_pool, newSVsv (coro_current)); |
1861 | } |
2202 | } |
1862 | |
2203 | |
|
|
2204 | #if 0 |
|
|
2205 | |
|
|
2206 | void |
|
|
2207 | _generator_call (...) |
|
|
2208 | PROTOTYPE: @ |
|
|
2209 | PPCODE: |
|
|
2210 | fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr); |
|
|
2211 | xxxx |
|
|
2212 | abort (); |
|
|
2213 | |
|
|
2214 | SV * |
|
|
2215 | gensub (SV *sub, ...) |
|
|
2216 | PROTOTYPE: &;@ |
|
|
2217 | CODE: |
|
|
2218 | { |
|
|
2219 | struct coro *coro; |
|
|
2220 | MAGIC *mg; |
|
|
2221 | CV *xcv; |
|
|
2222 | CV *ncv = (CV *)newSV_type (SVt_PVCV); |
|
|
2223 | int i; |
|
|
2224 | |
|
|
2225 | CvGV (ncv) = CvGV (cv); |
|
|
2226 | CvFILE (ncv) = CvFILE (cv); |
|
|
2227 | |
|
|
2228 | Newz (0, coro, 1, struct coro); |
|
|
2229 | coro->args = newAV (); |
|
|
2230 | coro->flags = CF_NEW; |
|
|
2231 | |
|
|
2232 | av_extend (coro->args, items - 1); |
|
|
2233 | for (i = 1; i < items; i++) |
|
|
2234 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2235 | |
|
|
2236 | CvISXSUB_on (ncv); |
|
|
2237 | CvXSUBANY (ncv).any_ptr = (void *)coro; |
|
|
2238 | |
|
|
2239 | xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV)); |
|
|
2240 | |
|
|
2241 | CvXSUB (ncv) = CvXSUB (xcv); |
|
|
2242 | CvANON_on (ncv); |
|
|
2243 | |
|
|
2244 | mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0); |
|
|
2245 | RETVAL = newRV_noinc ((SV *)ncv); |
|
|
2246 | } |
|
|
2247 | OUTPUT: |
|
|
2248 | RETVAL |
|
|
2249 | |
|
|
2250 | #endif |
|
|
2251 | |
1863 | |
2252 | |
1864 | MODULE = Coro::State PACKAGE = Coro::AIO |
2253 | MODULE = Coro::State PACKAGE = Coro::AIO |
1865 | |
2254 | |
1866 | SV * |
2255 | void |
1867 | _get_state () |
2256 | _get_state (SV *self) |
1868 | CODE: |
2257 | PPCODE: |
1869 | { |
2258 | { |
1870 | struct io_state *data; |
2259 | AV *defav = GvAV (PL_defgv); |
1871 | |
2260 | AV *av = newAV (); |
|
|
2261 | int i; |
1872 | RETVAL = newSV (sizeof (struct io_state)); |
2262 | SV *data_sv = newSV (sizeof (struct io_state)); |
1873 | data = (struct io_state *)SvPVX (RETVAL); |
2263 | struct io_state *data = (struct io_state *)SvPVX (data_sv); |
1874 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
2264 | SvCUR_set (data_sv, sizeof (struct io_state)); |
1875 | SvPOK_only (RETVAL); |
2265 | SvPOK_only (data_sv); |
1876 | |
2266 | |
1877 | data->errorno = errno; |
2267 | data->errorno = errno; |
1878 | data->laststype = PL_laststype; |
2268 | data->laststype = PL_laststype; |
1879 | data->laststatval = PL_laststatval; |
2269 | data->laststatval = PL_laststatval; |
1880 | data->statcache = PL_statcache; |
2270 | data->statcache = PL_statcache; |
|
|
2271 | |
|
|
2272 | av_extend (av, AvFILLp (defav) + 1 + 1); |
|
|
2273 | |
|
|
2274 | for (i = 0; i <= AvFILLp (defav); ++i) |
|
|
2275 | av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i])); |
|
|
2276 | |
|
|
2277 | av_push (av, data_sv); |
|
|
2278 | |
|
|
2279 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
2280 | |
|
|
2281 | api_ready (self); |
1881 | } |
2282 | } |
1882 | OUTPUT: |
|
|
1883 | RETVAL |
|
|
1884 | |
2283 | |
1885 | void |
2284 | void |
1886 | _set_state (char *data_) |
2285 | _set_state (SV *state) |
1887 | PROTOTYPE: $ |
2286 | PROTOTYPE: $ |
1888 | CODE: |
2287 | PPCODE: |
1889 | { |
2288 | { |
1890 | struct io_state *data = (void *)data_; |
2289 | AV *av = (AV *)SvRV (state); |
|
|
2290 | struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]); |
|
|
2291 | int i; |
1891 | |
2292 | |
1892 | errno = data->errorno; |
2293 | errno = data->errorno; |
1893 | PL_laststype = data->laststype; |
2294 | PL_laststype = data->laststype; |
1894 | PL_laststatval = data->laststatval; |
2295 | PL_laststatval = data->laststatval; |
1895 | PL_statcache = data->statcache; |
2296 | PL_statcache = data->statcache; |
1896 | } |
|
|
1897 | |
2297 | |
|
|
2298 | EXTEND (SP, AvFILLp (av)); |
|
|
2299 | for (i = 0; i < AvFILLp (av); ++i) |
|
|
2300 | PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i]))); |
|
|
2301 | } |
|
|
2302 | |
|
|
2303 | |
|
|
2304 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
|
|
2305 | |
|
|
2306 | BOOT: |
|
|
2307 | sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE); |
|
|
2308 | |
|
|
2309 | SV * |
|
|
2310 | _schedule (...) |
|
|
2311 | PROTOTYPE: @ |
|
|
2312 | CODE: |
|
|
2313 | { |
|
|
2314 | static int incede; |
|
|
2315 | |
|
|
2316 | api_cede_notself (); |
|
|
2317 | |
|
|
2318 | ++incede; |
|
|
2319 | while (coro_nready >= incede && api_cede ()) |
|
|
2320 | ; |
|
|
2321 | |
|
|
2322 | sv_setsv (sv_activity, &PL_sv_undef); |
|
|
2323 | if (coro_nready >= incede) |
|
|
2324 | { |
|
|
2325 | PUSHMARK (SP); |
|
|
2326 | PUTBACK; |
|
|
2327 | call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL); |
|
|
2328 | SPAGAIN; |
|
|
2329 | } |
|
|
2330 | |
|
|
2331 | --incede; |
|
|
2332 | } |
|
|
2333 | |
|
|
2334 | |
|
|
2335 | MODULE = Coro::State PACKAGE = PerlIO::cede |
|
|
2336 | |
|
|
2337 | BOOT: |
|
|
2338 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |