… | |
… | |
55 | |
55 | |
56 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
56 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
57 | (PERL_REVISION > (a) \ |
57 | (PERL_REVISION > (a) \ |
58 | || (PERL_REVISION == (a) \ |
58 | || (PERL_REVISION == (a) \ |
59 | && (PERL_VERSION > (b) \ |
59 | && (PERL_VERSION > (b) \ |
60 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
60 | || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
61 | |
61 | |
62 | #if !PERL_VERSION_ATLEAST (5,6,0) |
62 | #if !PERL_VERSION_ATLEAST (5,6,0) |
63 | # ifndef PL_ppaddr |
63 | # ifndef PL_ppaddr |
64 | # define PL_ppaddr ppaddr |
64 | # define PL_ppaddr ppaddr |
65 | # endif |
65 | # endif |
… | |
… | |
98 | # define newSV(l) NEWSV(0,l) |
98 | # define newSV(l) NEWSV(0,l) |
99 | #endif |
99 | #endif |
100 | #ifndef CvISXSUB_on |
100 | #ifndef CvISXSUB_on |
101 | # define CvISXSUB_on(cv) (void)cv |
101 | # define CvISXSUB_on(cv) (void)cv |
102 | #endif |
102 | #endif |
|
|
103 | #ifndef CvISXSUB |
|
|
104 | # define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) |
|
|
105 | #endif |
|
|
106 | #ifndef Newx |
|
|
107 | # define Newx(ptr,nitems,type) New (0,ptr,nitems,type) |
|
|
108 | #endif |
103 | |
109 | |
104 | /* 5.8.7 */ |
110 | /* 5.8.7 */ |
105 | #ifndef SvRV_set |
111 | #ifndef SvRV_set |
106 | # define SvRV_set(s,v) SvRV(s) = (v) |
112 | # define SvRV_set(s,v) SvRV(s) = (v) |
107 | #endif |
113 | #endif |
… | |
… | |
127 | #else |
133 | #else |
128 | # define dSTACKLEVEL volatile void *stacklevel |
134 | # define dSTACKLEVEL volatile void *stacklevel |
129 | # define STACKLEVEL ((void *)&stacklevel) |
135 | # define STACKLEVEL ((void *)&stacklevel) |
130 | #endif |
136 | #endif |
131 | |
137 | |
132 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
138 | #define IN_DESTRUCT PL_dirty |
133 | |
139 | |
134 | #if __GNUC__ >= 3 |
140 | #if __GNUC__ >= 3 |
135 | # define attribute(x) __attribute__(x) |
141 | # define attribute(x) __attribute__(x) |
136 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
142 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
137 | # define INLINE static inline |
143 | # define INLINE static inline |
… | |
… | |
167 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
173 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
168 | static JMPENV *main_top_env; |
174 | static JMPENV *main_top_env; |
169 | static HV *coro_state_stash, *coro_stash; |
175 | static HV *coro_state_stash, *coro_stash; |
170 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
176 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
171 | |
177 | |
|
|
178 | static AV *av_destroy; /* destruction queue */ |
|
|
179 | static SV *sv_manager; /* the manager coro */ |
|
|
180 | static SV *sv_idle; /* $Coro::idle */ |
|
|
181 | |
172 | static GV *irsgv; /* $/ */ |
182 | static GV *irsgv; /* $/ */ |
173 | static GV *stdoutgv; /* *STDOUT */ |
183 | static GV *stdoutgv; /* *STDOUT */ |
174 | static SV *rv_diehook; |
184 | static SV *rv_diehook; |
175 | static SV *rv_warnhook; |
185 | static SV *rv_warnhook; |
176 | static HV *hv_sig; /* %SIG */ |
186 | static HV *hv_sig; /* %SIG */ |
177 | |
187 | |
178 | /* async_pool helper stuff */ |
188 | /* async_pool helper stuff */ |
179 | static SV *sv_pool_rss; |
189 | static SV *sv_pool_rss; |
180 | static SV *sv_pool_size; |
190 | static SV *sv_pool_size; |
|
|
191 | static SV *sv_async_pool_idle; /* description string */ |
181 | static AV *av_async_pool; |
192 | static AV *av_async_pool; /* idle pool */ |
|
|
193 | static SV *sv_Coro; /* class string */ |
|
|
194 | static CV *cv_pool_handler; |
|
|
195 | static CV *cv_coro_state_new; |
182 | |
196 | |
183 | /* Coro::AnyEvent */ |
197 | /* Coro::AnyEvent */ |
184 | static SV *sv_activity; |
198 | static SV *sv_activity; |
185 | |
199 | |
186 | static struct coro_cctx *cctx_first; |
200 | static struct coro_cctx *cctx_first; |
… | |
… | |
215 | int valgrind_id; |
229 | int valgrind_id; |
216 | #endif |
230 | #endif |
217 | unsigned char flags; |
231 | unsigned char flags; |
218 | } coro_cctx; |
232 | } coro_cctx; |
219 | |
233 | |
|
|
234 | coro_cctx *cctx_current; /* the currently running cctx */ |
|
|
235 | |
|
|
236 | /*****************************************************************************/ |
|
|
237 | |
220 | enum { |
238 | enum { |
221 | CF_RUNNING = 0x0001, /* coroutine is running */ |
239 | CF_RUNNING = 0x0001, /* coroutine is running */ |
222 | CF_READY = 0x0002, /* coroutine is ready */ |
240 | CF_READY = 0x0002, /* coroutine is ready */ |
223 | CF_NEW = 0x0004, /* has never been switched to */ |
241 | CF_NEW = 0x0004, /* has never been switched to */ |
224 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
242 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
… | |
… | |
229 | { |
247 | { |
230 | SV *defsv; |
248 | SV *defsv; |
231 | AV *defav; |
249 | AV *defav; |
232 | SV *errsv; |
250 | SV *errsv; |
233 | SV *irsgv; |
251 | SV *irsgv; |
|
|
252 | HV *hinthv; |
234 | #define VAR(name,type) type name; |
253 | #define VAR(name,type) type name; |
235 | # include "state.h" |
254 | # include "state.h" |
236 | #undef VAR |
255 | #undef VAR |
237 | } perl_slots; |
256 | } perl_slots; |
238 | |
257 | |
… | |
… | |
246 | /* state data */ |
265 | /* state data */ |
247 | struct CoroSLF slf_frame; /* saved slf frame */ |
266 | struct CoroSLF slf_frame; /* saved slf frame */ |
248 | AV *mainstack; |
267 | AV *mainstack; |
249 | perl_slots *slot; /* basically the saved sp */ |
268 | perl_slots *slot; /* basically the saved sp */ |
250 | |
269 | |
|
|
270 | CV *startcv; /* the CV to execute */ |
251 | AV *args; /* data associated with this coroutine (initial args) */ |
271 | AV *args; /* data associated with this coroutine (initial args) */ |
252 | int refcnt; /* coroutines are refcounted, yes */ |
272 | int refcnt; /* coroutines are refcounted, yes */ |
253 | int flags; /* CF_ flags */ |
273 | int flags; /* CF_ flags */ |
254 | HV *hv; /* the perl hash associated with this coro, if any */ |
274 | HV *hv; /* the perl hash associated with this coro, if any */ |
255 | void (*on_destroy)(pTHX_ struct coro *coro); |
275 | void (*on_destroy)(pTHX_ struct coro *coro); |
256 | |
276 | |
257 | /* statistics */ |
277 | /* statistics */ |
258 | int usecount; /* number of transfers to this coro */ |
278 | int usecount; /* number of transfers to this coro */ |
259 | |
279 | |
… | |
… | |
262 | SV *except; /* exception to be thrown */ |
282 | SV *except; /* exception to be thrown */ |
263 | SV *rouse_cb; |
283 | SV *rouse_cb; |
264 | |
284 | |
265 | /* async_pool */ |
285 | /* async_pool */ |
266 | SV *saved_deffh; |
286 | SV *saved_deffh; |
|
|
287 | SV *invoke_cb; |
|
|
288 | AV *invoke_av; |
267 | |
289 | |
268 | /* linked list */ |
290 | /* linked list */ |
269 | struct coro *next, *prev; |
291 | struct coro *next, *prev; |
270 | }; |
292 | }; |
271 | |
293 | |
… | |
… | |
288 | |
310 | |
289 | /* for Coro.pm */ |
311 | /* for Coro.pm */ |
290 | static SV *coro_current; |
312 | static SV *coro_current; |
291 | static SV *coro_readyhook; |
313 | static SV *coro_readyhook; |
292 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
314 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
|
|
315 | static CV *cv_coro_run, *cv_coro_terminate; |
293 | static struct coro *coro_first; |
316 | static struct coro *coro_first; |
294 | #define coro_nready coroapi.nready |
317 | #define coro_nready coroapi.nready |
295 | |
318 | |
296 | /** lowlevel stuff **********************************************************/ |
319 | /** lowlevel stuff **********************************************************/ |
297 | |
320 | |
… | |
… | |
323 | get_hv (name, create); |
346 | get_hv (name, create); |
324 | #endif |
347 | #endif |
325 | return get_hv (name, create); |
348 | return get_hv (name, create); |
326 | } |
349 | } |
327 | |
350 | |
|
|
351 | /* may croak */ |
|
|
352 | INLINE CV * |
|
|
353 | coro_sv_2cv (pTHX_ SV *sv) |
|
|
354 | { |
|
|
355 | HV *st; |
|
|
356 | GV *gvp; |
|
|
357 | return sv_2cv (sv, &st, &gvp, 0); |
|
|
358 | } |
|
|
359 | |
|
|
360 | /*****************************************************************************/ |
|
|
361 | /* magic glue */ |
|
|
362 | |
|
|
363 | #define CORO_MAGIC_type_cv 26 |
|
|
364 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
365 | |
|
|
366 | #define CORO_MAGIC_NN(sv, type) \ |
|
|
367 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
|
|
368 | ? SvMAGIC (sv) \ |
|
|
369 | : mg_find (sv, type)) |
|
|
370 | |
|
|
371 | #define CORO_MAGIC(sv, type) \ |
|
|
372 | (expect_true (SvMAGIC (sv)) \ |
|
|
373 | ? CORO_MAGIC_NN (sv, type) \ |
|
|
374 | : 0) |
|
|
375 | |
|
|
376 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
377 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
|
|
378 | |
|
|
379 | INLINE struct coro * |
|
|
380 | SvSTATE_ (pTHX_ SV *coro) |
|
|
381 | { |
|
|
382 | HV *stash; |
|
|
383 | MAGIC *mg; |
|
|
384 | |
|
|
385 | if (SvROK (coro)) |
|
|
386 | coro = SvRV (coro); |
|
|
387 | |
|
|
388 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
389 | croak ("Coro::State object required"); |
|
|
390 | |
|
|
391 | stash = SvSTASH (coro); |
|
|
392 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
393 | { |
|
|
394 | /* very slow, but rare, check */ |
|
|
395 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
396 | croak ("Coro::State object required"); |
|
|
397 | } |
|
|
398 | |
|
|
399 | mg = CORO_MAGIC_state (coro); |
|
|
400 | return (struct coro *)mg->mg_ptr; |
|
|
401 | } |
|
|
402 | |
|
|
403 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
404 | |
|
|
405 | /* faster than SvSTATE, but expects a coroutine hv */ |
|
|
406 | #define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) |
|
|
407 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
|
|
408 | |
|
|
409 | /*****************************************************************************/ |
|
|
410 | /* padlist management and caching */ |
|
|
411 | |
328 | static AV * |
412 | static AV * |
329 | coro_clone_padlist (pTHX_ CV *cv) |
413 | coro_derive_padlist (pTHX_ CV *cv) |
330 | { |
414 | { |
331 | AV *padlist = CvPADLIST (cv); |
415 | AV *padlist = CvPADLIST (cv); |
332 | AV *newpadlist, *newpad; |
416 | AV *newpadlist, *newpad; |
333 | |
417 | |
334 | newpadlist = newAV (); |
418 | newpadlist = newAV (); |
… | |
… | |
339 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
423 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
340 | #endif |
424 | #endif |
341 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
425 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
342 | --AvFILLp (padlist); |
426 | --AvFILLp (padlist); |
343 | |
427 | |
344 | av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); |
428 | av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); |
345 | av_store (newpadlist, 1, (SV *)newpad); |
429 | av_store (newpadlist, 1, (SV *)newpad); |
346 | |
430 | |
347 | return newpadlist; |
431 | return newpadlist; |
348 | } |
432 | } |
349 | |
433 | |
350 | static void |
434 | static void |
351 | free_padlist (pTHX_ AV *padlist) |
435 | free_padlist (pTHX_ AV *padlist) |
352 | { |
436 | { |
353 | /* may be during global destruction */ |
437 | /* may be during global destruction */ |
354 | if (SvREFCNT (padlist)) |
438 | if (!IN_DESTRUCT) |
355 | { |
439 | { |
356 | I32 i = AvFILLp (padlist); |
440 | I32 i = AvFILLp (padlist); |
357 | while (i >= 0) |
441 | |
|
|
442 | while (i > 0) /* special-case index 0 */ |
358 | { |
443 | { |
359 | SV **svp = av_fetch (padlist, i--, FALSE); |
444 | /* we try to be extra-careful here */ |
360 | if (svp) |
445 | AV *av = (AV *)AvARRAY (padlist)[i--]; |
361 | { |
446 | I32 j = AvFILLp (av); |
362 | SV *sv; |
447 | |
363 | while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) |
448 | while (j >= 0) |
|
|
449 | SvREFCNT_dec (AvARRAY (av)[j--]); |
|
|
450 | |
|
|
451 | AvFILLp (av) = -1; |
364 | SvREFCNT_dec (sv); |
452 | SvREFCNT_dec (av); |
365 | |
|
|
366 | SvREFCNT_dec (*svp); |
|
|
367 | } |
|
|
368 | } |
453 | } |
369 | |
454 | |
|
|
455 | SvREFCNT_dec (AvARRAY (padlist)[0]); |
|
|
456 | |
|
|
457 | AvFILLp (padlist) = -1; |
370 | SvREFCNT_dec ((SV*)padlist); |
458 | SvREFCNT_dec ((SV*)padlist); |
371 | } |
459 | } |
372 | } |
460 | } |
373 | |
461 | |
374 | static int |
462 | static int |
… | |
… | |
383 | |
471 | |
384 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
472 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
385 | |
473 | |
386 | return 0; |
474 | return 0; |
387 | } |
475 | } |
388 | |
|
|
389 | #define CORO_MAGIC_type_cv 26 |
|
|
390 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
391 | |
476 | |
392 | static MGVTBL coro_cv_vtbl = { |
477 | static MGVTBL coro_cv_vtbl = { |
393 | 0, 0, 0, 0, |
478 | 0, 0, 0, 0, |
394 | coro_cv_free |
479 | coro_cv_free |
395 | }; |
480 | }; |
396 | |
|
|
397 | #define CORO_MAGIC_NN(sv, type) \ |
|
|
398 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
|
|
399 | ? SvMAGIC (sv) \ |
|
|
400 | : mg_find (sv, type)) |
|
|
401 | |
|
|
402 | #define CORO_MAGIC(sv, type) \ |
|
|
403 | (expect_true (SvMAGIC (sv)) \ |
|
|
404 | ? CORO_MAGIC_NN (sv, type) \ |
|
|
405 | : 0) |
|
|
406 | |
|
|
407 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
408 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
|
|
409 | |
|
|
410 | INLINE struct coro * |
|
|
411 | SvSTATE_ (pTHX_ SV *coro) |
|
|
412 | { |
|
|
413 | HV *stash; |
|
|
414 | MAGIC *mg; |
|
|
415 | |
|
|
416 | if (SvROK (coro)) |
|
|
417 | coro = SvRV (coro); |
|
|
418 | |
|
|
419 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
420 | croak ("Coro::State object required"); |
|
|
421 | |
|
|
422 | stash = SvSTASH (coro); |
|
|
423 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
424 | { |
|
|
425 | /* very slow, but rare, check */ |
|
|
426 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
427 | croak ("Coro::State object required"); |
|
|
428 | } |
|
|
429 | |
|
|
430 | mg = CORO_MAGIC_state (coro); |
|
|
431 | return (struct coro *)mg->mg_ptr; |
|
|
432 | } |
|
|
433 | |
|
|
434 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
435 | |
|
|
436 | /* faster than SvSTATE, but expects a coroutine hv */ |
|
|
437 | #define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) |
|
|
438 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
|
|
439 | |
481 | |
440 | /* the next two functions merely cache the padlists */ |
482 | /* the next two functions merely cache the padlists */ |
441 | static void |
483 | static void |
442 | get_padlist (pTHX_ CV *cv) |
484 | get_padlist (pTHX_ CV *cv) |
443 | { |
485 | { |
… | |
… | |
454 | CV *cp = Perl_cv_clone (aTHX_ cv); |
496 | CV *cp = Perl_cv_clone (aTHX_ cv); |
455 | CvPADLIST (cv) = CvPADLIST (cp); |
497 | CvPADLIST (cv) = CvPADLIST (cp); |
456 | CvPADLIST (cp) = 0; |
498 | CvPADLIST (cp) = 0; |
457 | SvREFCNT_dec (cp); |
499 | SvREFCNT_dec (cp); |
458 | #else |
500 | #else |
459 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
501 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
460 | #endif |
502 | #endif |
461 | } |
503 | } |
462 | } |
504 | } |
463 | |
505 | |
464 | static void |
506 | static void |
… | |
… | |
471 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
513 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
472 | |
514 | |
473 | av = (AV *)mg->mg_obj; |
515 | av = (AV *)mg->mg_obj; |
474 | |
516 | |
475 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
517 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
476 | av_extend (av, AvMAX (av) + 1); |
518 | av_extend (av, AvFILLp (av) + 1); |
477 | |
519 | |
478 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
520 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
479 | } |
521 | } |
480 | |
522 | |
481 | /** load & save, init *******************************************************/ |
523 | /** load & save, init *******************************************************/ |
… | |
… | |
486 | perl_slots *slot = c->slot; |
528 | perl_slots *slot = c->slot; |
487 | c->slot = 0; |
529 | c->slot = 0; |
488 | |
530 | |
489 | PL_mainstack = c->mainstack; |
531 | PL_mainstack = c->mainstack; |
490 | |
532 | |
491 | GvSV (PL_defgv) = slot->defsv; |
533 | GvSV (PL_defgv) = slot->defsv; |
492 | GvAV (PL_defgv) = slot->defav; |
534 | GvAV (PL_defgv) = slot->defav; |
493 | GvSV (PL_errgv) = slot->errsv; |
535 | GvSV (PL_errgv) = slot->errsv; |
494 | GvSV (irsgv) = slot->irsgv; |
536 | GvSV (irsgv) = slot->irsgv; |
|
|
537 | GvHV (PL_hintgv) = slot->hinthv; |
495 | |
538 | |
496 | #define VAR(name,type) PL_ ## name = slot->name; |
539 | #define VAR(name,type) PL_ ## name = slot->name; |
497 | # include "state.h" |
540 | # include "state.h" |
498 | #undef VAR |
541 | #undef VAR |
499 | |
542 | |
… | |
… | |
585 | c->mainstack = PL_mainstack; |
628 | c->mainstack = PL_mainstack; |
586 | |
629 | |
587 | { |
630 | { |
588 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
631 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
589 | |
632 | |
590 | slot->defav = GvAV (PL_defgv); |
633 | slot->defav = GvAV (PL_defgv); |
591 | slot->defsv = DEFSV; |
634 | slot->defsv = DEFSV; |
592 | slot->errsv = ERRSV; |
635 | slot->errsv = ERRSV; |
593 | slot->irsgv = GvSV (irsgv); |
636 | slot->irsgv = GvSV (irsgv); |
|
|
637 | slot->hinthv = GvHV (PL_hintgv); |
594 | |
638 | |
595 | #define VAR(name,type) slot->name = PL_ ## name; |
639 | #define VAR(name,type) slot->name = PL_ ## name; |
596 | # include "state.h" |
640 | # include "state.h" |
597 | #undef VAR |
641 | #undef VAR |
598 | } |
642 | } |
… | |
… | |
676 | #if !PERL_VERSION_ATLEAST (5,10,0) |
720 | #if !PERL_VERSION_ATLEAST (5,10,0) |
677 | Safefree (PL_retstack); |
721 | Safefree (PL_retstack); |
678 | #endif |
722 | #endif |
679 | } |
723 | } |
680 | |
724 | |
|
|
725 | #define CORO_RSS \ |
|
|
726 | rss += sizeof (SYM (curstackinfo)); \ |
|
|
727 | rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \ |
|
|
728 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \ |
|
|
729 | rss += SYM (tmps_max) * sizeof (SV *); \ |
|
|
730 | rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \ |
|
|
731 | rss += SYM (scopestack_max) * sizeof (I32); \ |
|
|
732 | rss += SYM (savestack_max) * sizeof (ANY); |
|
|
733 | |
681 | static size_t |
734 | static size_t |
682 | coro_rss (pTHX_ struct coro *coro) |
735 | coro_rss (pTHX_ struct coro *coro) |
683 | { |
736 | { |
684 | size_t rss = sizeof (*coro); |
737 | size_t rss = sizeof (*coro); |
685 | |
738 | |
686 | if (coro->mainstack) |
739 | if (coro->mainstack) |
687 | { |
740 | { |
688 | perl_slots tmp_slot; |
|
|
689 | perl_slots *slot; |
|
|
690 | |
|
|
691 | if (coro->flags & CF_RUNNING) |
741 | if (coro->flags & CF_RUNNING) |
692 | { |
742 | { |
693 | slot = &tmp_slot; |
743 | #define SYM(sym) PL_ ## sym |
694 | |
744 | CORO_RSS; |
695 | #define VAR(name,type) slot->name = PL_ ## name; |
|
|
696 | # include "state.h" |
|
|
697 | #undef VAR |
745 | #undef SYM |
698 | } |
746 | } |
699 | else |
747 | else |
700 | slot = coro->slot; |
|
|
701 | |
|
|
702 | if (slot) |
|
|
703 | { |
748 | { |
704 | rss += sizeof (slot->curstackinfo); |
749 | #define SYM(sym) coro->slot->sym |
705 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
750 | CORO_RSS; |
706 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
751 | #undef SYM |
707 | rss += slot->tmps_max * sizeof (SV *); |
|
|
708 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
|
|
709 | rss += slot->scopestack_max * sizeof (I32); |
|
|
710 | rss += slot->savestack_max * sizeof (ANY); |
|
|
711 | |
|
|
712 | #if !PERL_VERSION_ATLEAST (5,10,0) |
|
|
713 | rss += slot->retstack_max * sizeof (OP *); |
|
|
714 | #endif |
|
|
715 | } |
752 | } |
716 | } |
753 | } |
717 | |
754 | |
718 | return rss; |
755 | return rss; |
719 | } |
756 | } |
… | |
… | |
732 | #endif |
769 | #endif |
733 | |
770 | |
734 | /* |
771 | /* |
735 | * This overrides the default magic get method of %SIG elements. |
772 | * This overrides the default magic get method of %SIG elements. |
736 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
773 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
737 | * and instead of tryign to save and restore the hash elements, we just provide |
774 | * and instead of trying to save and restore the hash elements, we just provide |
738 | * readback here. |
775 | * readback here. |
739 | * We only do this when the hook is != 0, as they are often set to 0 temporarily, |
|
|
740 | * not expecting this to actually change the hook. This is a potential problem |
|
|
741 | * when a schedule happens then, but we ignore this. |
|
|
742 | */ |
776 | */ |
743 | static int |
777 | static int |
744 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
778 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
745 | { |
779 | { |
746 | const char *s = MgPV_nolen_const (mg); |
780 | const char *s = MgPV_nolen_const (mg); |
… | |
… | |
799 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
833 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
800 | |
834 | |
801 | if (svp) |
835 | if (svp) |
802 | { |
836 | { |
803 | SV *old = *svp; |
837 | SV *old = *svp; |
804 | *svp = newSVsv (sv); |
838 | *svp = SvOK (sv) ? newSVsv (sv) : 0; |
805 | SvREFCNT_dec (old); |
839 | SvREFCNT_dec (old); |
806 | return 0; |
840 | return 0; |
807 | } |
841 | } |
808 | } |
842 | } |
809 | |
843 | |
… | |
… | |
819 | |
853 | |
820 | static int |
854 | static int |
821 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
855 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
822 | { |
856 | { |
823 | return 0; |
857 | return 0; |
|
|
858 | } |
|
|
859 | |
|
|
860 | static int |
|
|
861 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
|
|
862 | { |
|
|
863 | return 1; |
824 | } |
864 | } |
825 | |
865 | |
826 | static UNOP coro_setup_op; |
866 | static UNOP coro_setup_op; |
827 | |
867 | |
828 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
868 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
… | |
… | |
835 | |
875 | |
836 | PL_runops = RUNOPS_DEFAULT; |
876 | PL_runops = RUNOPS_DEFAULT; |
837 | PL_curcop = &PL_compiling; |
877 | PL_curcop = &PL_compiling; |
838 | PL_in_eval = EVAL_NULL; |
878 | PL_in_eval = EVAL_NULL; |
839 | PL_comppad = 0; |
879 | PL_comppad = 0; |
|
|
880 | PL_comppad_name = 0; |
|
|
881 | PL_comppad_name_fill = 0; |
|
|
882 | PL_comppad_name_floor = 0; |
840 | PL_curpm = 0; |
883 | PL_curpm = 0; |
841 | PL_curpad = 0; |
884 | PL_curpad = 0; |
842 | PL_localizing = 0; |
885 | PL_localizing = 0; |
843 | PL_dirty = 0; |
886 | PL_dirty = 0; |
844 | PL_restartop = 0; |
887 | PL_restartop = 0; |
845 | #if PERL_VERSION_ATLEAST (5,10,0) |
888 | #if PERL_VERSION_ATLEAST (5,10,0) |
846 | PL_parser = 0; |
889 | PL_parser = 0; |
847 | #endif |
890 | #endif |
|
|
891 | PL_hints = 0; |
848 | |
892 | |
849 | /* recreate the die/warn hooks */ |
893 | /* recreate the die/warn hooks */ |
850 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
894 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
851 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
895 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
852 | |
896 | |
853 | GvSV (PL_defgv) = newSV (0); |
897 | GvSV (PL_defgv) = newSV (0); |
854 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
898 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
855 | GvSV (PL_errgv) = newSV (0); |
899 | GvSV (PL_errgv) = newSV (0); |
856 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
900 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
|
|
901 | GvHV (PL_hintgv) = 0; |
857 | PL_rs = newSVsv (GvSV (irsgv)); |
902 | PL_rs = newSVsv (GvSV (irsgv)); |
858 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
903 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
859 | |
904 | |
860 | { |
905 | { |
861 | dSP; |
906 | dSP; |
862 | UNOP myop; |
907 | UNOP myop; |
863 | |
908 | |
864 | Zero (&myop, 1, UNOP); |
909 | Zero (&myop, 1, UNOP); |
865 | myop.op_next = Nullop; |
910 | myop.op_next = Nullop; |
|
|
911 | myop.op_type = OP_ENTERSUB; |
866 | myop.op_flags = OPf_WANT_VOID; |
912 | myop.op_flags = OPf_WANT_VOID; |
867 | |
913 | |
868 | PUSHMARK (SP); |
914 | PUSHMARK (SP); |
869 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
915 | PUSHs ((SV *)coro->startcv); |
870 | PUTBACK; |
916 | PUTBACK; |
871 | PL_op = (OP *)&myop; |
917 | PL_op = (OP *)&myop; |
872 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
918 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
873 | SPAGAIN; |
|
|
874 | } |
919 | } |
875 | |
920 | |
876 | /* this newly created coroutine might be run on an existing cctx which most |
921 | /* this newly created coroutine might be run on an existing cctx which most |
877 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
922 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
878 | */ |
923 | */ |
879 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
924 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
880 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
925 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
881 | |
926 | |
882 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
927 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
883 | coro_setup_op.op_next = PL_op; |
928 | coro_setup_op.op_next = PL_op; |
884 | coro_setup_op.op_type = OP_CUSTOM; |
929 | coro_setup_op.op_type = OP_ENTERSUB; |
885 | coro_setup_op.op_ppaddr = pp_slf; |
930 | coro_setup_op.op_ppaddr = pp_slf; |
886 | /* no flags required, as an init function won't be called */ |
931 | /* no flags etc. required, as an init function won't be called */ |
887 | |
932 | |
888 | PL_op = (OP *)&coro_setup_op; |
933 | PL_op = (OP *)&coro_setup_op; |
889 | |
934 | |
890 | /* copy throw, in case it was set before coro_setup */ |
935 | /* copy throw, in case it was set before coro_setup */ |
891 | CORO_THROW = coro->except; |
936 | CORO_THROW = coro->except; |
892 | } |
937 | } |
893 | |
938 | |
894 | static void |
939 | static void |
895 | coro_destruct (pTHX_ struct coro *coro) |
940 | coro_unwind_stacks (pTHX) |
896 | { |
941 | { |
897 | if (!IN_DESTRUCT) |
942 | if (!IN_DESTRUCT) |
898 | { |
943 | { |
899 | /* restore all saved variables and stuff */ |
944 | /* restore all saved variables and stuff */ |
900 | LEAVE_SCOPE (0); |
945 | LEAVE_SCOPE (0); |
… | |
… | |
908 | POPSTACK_TO (PL_mainstack); |
953 | POPSTACK_TO (PL_mainstack); |
909 | |
954 | |
910 | /* unwind main stack */ |
955 | /* unwind main stack */ |
911 | dounwind (-1); |
956 | dounwind (-1); |
912 | } |
957 | } |
|
|
958 | } |
|
|
959 | |
|
|
960 | static void |
|
|
961 | coro_destruct_perl (pTHX_ struct coro *coro) |
|
|
962 | { |
|
|
963 | coro_unwind_stacks (aTHX); |
913 | |
964 | |
914 | SvREFCNT_dec (GvSV (PL_defgv)); |
965 | SvREFCNT_dec (GvSV (PL_defgv)); |
915 | SvREFCNT_dec (GvAV (PL_defgv)); |
966 | SvREFCNT_dec (GvAV (PL_defgv)); |
916 | SvREFCNT_dec (GvSV (PL_errgv)); |
967 | SvREFCNT_dec (GvSV (PL_errgv)); |
917 | SvREFCNT_dec (PL_defoutgv); |
968 | SvREFCNT_dec (PL_defoutgv); |
918 | SvREFCNT_dec (PL_rs); |
969 | SvREFCNT_dec (PL_rs); |
919 | SvREFCNT_dec (GvSV (irsgv)); |
970 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
971 | SvREFCNT_dec (GvHV (PL_hintgv)); |
920 | |
972 | |
921 | SvREFCNT_dec (PL_diehook); |
973 | SvREFCNT_dec (PL_diehook); |
922 | SvREFCNT_dec (PL_warnhook); |
974 | SvREFCNT_dec (PL_warnhook); |
923 | |
975 | |
924 | SvREFCNT_dec (CORO_THROW); |
|
|
925 | SvREFCNT_dec (coro->saved_deffh); |
976 | SvREFCNT_dec (coro->saved_deffh); |
926 | SvREFCNT_dec (coro->rouse_cb); |
977 | SvREFCNT_dec (coro->rouse_cb); |
|
|
978 | SvREFCNT_dec (coro->invoke_cb); |
|
|
979 | SvREFCNT_dec (coro->invoke_av); |
927 | |
980 | |
928 | coro_destruct_stacks (aTHX); |
981 | coro_destruct_stacks (aTHX); |
929 | } |
982 | } |
930 | |
983 | |
931 | INLINE void |
984 | INLINE void |
… | |
… | |
941 | static int |
994 | static int |
942 | runops_trace (pTHX) |
995 | runops_trace (pTHX) |
943 | { |
996 | { |
944 | COP *oldcop = 0; |
997 | COP *oldcop = 0; |
945 | int oldcxix = -2; |
998 | int oldcxix = -2; |
946 | struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */ |
|
|
947 | coro_cctx *cctx = coro->cctx; |
|
|
948 | |
999 | |
949 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
1000 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
950 | { |
1001 | { |
951 | PERL_ASYNC_CHECK (); |
1002 | PERL_ASYNC_CHECK (); |
952 | |
1003 | |
953 | if (cctx->flags & CC_TRACE_ALL) |
1004 | if (cctx_current->flags & CC_TRACE_ALL) |
954 | { |
1005 | { |
955 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
1006 | if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB) |
956 | { |
1007 | { |
957 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1008 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
958 | SV **bot, **top; |
1009 | SV **bot, **top; |
959 | AV *av = newAV (); /* return values */ |
1010 | AV *av = newAV (); /* return values */ |
960 | SV **cb; |
1011 | SV **cb; |
… | |
… | |
997 | |
1048 | |
998 | if (PL_curcop != &PL_compiling) |
1049 | if (PL_curcop != &PL_compiling) |
999 | { |
1050 | { |
1000 | SV **cb; |
1051 | SV **cb; |
1001 | |
1052 | |
1002 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
1053 | if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB) |
1003 | { |
1054 | { |
1004 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1055 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1005 | |
1056 | |
1006 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1057 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1007 | { |
1058 | { |
1008 | runops_proc_t old_runops = PL_runops; |
|
|
1009 | dSP; |
1059 | dSP; |
1010 | GV *gv = CvGV (cx->blk_sub.cv); |
1060 | GV *gv = CvGV (cx->blk_sub.cv); |
1011 | SV *fullname = sv_2mortal (newSV (0)); |
1061 | SV *fullname = sv_2mortal (newSV (0)); |
1012 | |
1062 | |
1013 | if (isGV (gv)) |
1063 | if (isGV (gv)) |
… | |
… | |
1018 | SAVETMPS; |
1068 | SAVETMPS; |
1019 | EXTEND (SP, 3); |
1069 | EXTEND (SP, 3); |
1020 | PUSHMARK (SP); |
1070 | PUSHMARK (SP); |
1021 | PUSHs (&PL_sv_yes); |
1071 | PUSHs (&PL_sv_yes); |
1022 | PUSHs (fullname); |
1072 | PUSHs (fullname); |
1023 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1073 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1024 | PUTBACK; |
1074 | PUTBACK; |
1025 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1075 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1026 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1076 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1027 | SPAGAIN; |
1077 | SPAGAIN; |
1028 | FREETMPS; |
1078 | FREETMPS; |
… | |
… | |
1031 | } |
1081 | } |
1032 | |
1082 | |
1033 | oldcxix = cxstack_ix; |
1083 | oldcxix = cxstack_ix; |
1034 | } |
1084 | } |
1035 | |
1085 | |
1036 | if (cctx->flags & CC_TRACE_LINE) |
1086 | if (cctx_current->flags & CC_TRACE_LINE) |
1037 | { |
1087 | { |
1038 | dSP; |
1088 | dSP; |
1039 | |
1089 | |
1040 | PL_runops = RUNOPS_DEFAULT; |
1090 | PL_runops = RUNOPS_DEFAULT; |
1041 | ENTER; |
1091 | ENTER; |
… | |
… | |
1060 | |
1110 | |
1061 | TAINT_NOT; |
1111 | TAINT_NOT; |
1062 | return 0; |
1112 | return 0; |
1063 | } |
1113 | } |
1064 | |
1114 | |
1065 | static struct coro_cctx *cctx_ssl_cctx; |
|
|
1066 | static struct CoroSLF cctx_ssl_frame; |
1115 | static struct CoroSLF cctx_ssl_frame; |
1067 | |
1116 | |
1068 | static void |
1117 | static void |
1069 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1118 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1070 | { |
1119 | { |
1071 | ta->prev = (struct coro *)cctx_ssl_cctx; |
|
|
1072 | ta->next = 0; |
1120 | ta->prev = 0; |
1073 | } |
1121 | } |
1074 | |
1122 | |
1075 | static int |
1123 | static int |
1076 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1124 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1077 | { |
1125 | { |
… | |
… | |
1080 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1128 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1081 | } |
1129 | } |
1082 | |
1130 | |
1083 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1131 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1084 | static void NOINLINE |
1132 | static void NOINLINE |
1085 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1133 | cctx_prepare (pTHX) |
1086 | { |
1134 | { |
1087 | PL_top_env = &PL_start_env; |
1135 | PL_top_env = &PL_start_env; |
1088 | |
1136 | |
1089 | if (cctx->flags & CC_TRACE) |
1137 | if (cctx_current->flags & CC_TRACE) |
1090 | PL_runops = runops_trace; |
1138 | PL_runops = runops_trace; |
1091 | |
1139 | |
1092 | /* we already must be executing an SLF op, there is no other valid way |
1140 | /* we already must be executing an SLF op, there is no other valid way |
1093 | * that can lead to creation of a new cctx */ |
1141 | * that can lead to creation of a new cctx */ |
1094 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1142 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1095 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1143 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1096 | |
1144 | |
1097 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1145 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1098 | cctx_ssl_cctx = cctx; |
|
|
1099 | cctx_ssl_frame = slf_frame; |
1146 | cctx_ssl_frame = slf_frame; |
1100 | |
1147 | |
1101 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1148 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1102 | slf_frame.check = slf_check_set_stacklevel; |
1149 | slf_frame.check = slf_check_set_stacklevel; |
1103 | } |
1150 | } |
… | |
… | |
1126 | /* normally we would need to skip the entersub here */ |
1173 | /* normally we would need to skip the entersub here */ |
1127 | /* not doing so will re-execute it, which is exactly what we want */ |
1174 | /* not doing so will re-execute it, which is exactly what we want */ |
1128 | /* PL_nop = PL_nop->op_next */ |
1175 | /* PL_nop = PL_nop->op_next */ |
1129 | |
1176 | |
1130 | /* inject a fake subroutine call to cctx_init */ |
1177 | /* inject a fake subroutine call to cctx_init */ |
1131 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1178 | cctx_prepare (aTHX); |
1132 | |
1179 | |
1133 | /* cctx_run is the alternative tail of transfer() */ |
1180 | /* cctx_run is the alternative tail of transfer() */ |
1134 | transfer_tail (aTHX); |
1181 | transfer_tail (aTHX); |
1135 | |
1182 | |
1136 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1183 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1137 | PL_restartop = PL_op; |
1184 | PL_restartop = PL_op; |
1138 | perl_run (PL_curinterp); |
1185 | perl_run (PL_curinterp); |
|
|
1186 | /* |
|
|
1187 | * Unfortunately, there is no way to get at the return values of the |
|
|
1188 | * coro body here, as perl_run destroys these |
|
|
1189 | */ |
1139 | |
1190 | |
1140 | /* |
1191 | /* |
1141 | * If perl-run returns we assume exit() was being called or the coro |
1192 | * If perl-run returns we assume exit() was being called or the coro |
1142 | * fell off the end, which seems to be the only valid (non-bug) |
1193 | * fell off the end, which seems to be the only valid (non-bug) |
1143 | * reason for perl_run to return. We try to exit by jumping to the |
1194 | * reason for perl_run to return. We try to exit by jumping to the |
1144 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1195 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1145 | * coroutine as Coro has no such concept |
1196 | * coroutine as Coro has no such concept. |
|
|
1197 | * This actually isn't valid with the pthread backend, but OSes requiring |
|
|
1198 | * that backend are too broken to do it in a standards-compliant way. |
1146 | */ |
1199 | */ |
1147 | PL_top_env = main_top_env; |
1200 | PL_top_env = main_top_env; |
1148 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1201 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1149 | } |
1202 | } |
1150 | } |
1203 | } |
… | |
… | |
1227 | cctx_destroy (coro_cctx *cctx) |
1280 | cctx_destroy (coro_cctx *cctx) |
1228 | { |
1281 | { |
1229 | if (!cctx) |
1282 | if (!cctx) |
1230 | return; |
1283 | return; |
1231 | |
1284 | |
|
|
1285 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary? |
|
|
1286 | |
1232 | --cctx_count; |
1287 | --cctx_count; |
1233 | coro_destroy (&cctx->cctx); |
1288 | coro_destroy (&cctx->cctx); |
1234 | |
1289 | |
1235 | /* coro_transfer creates new, empty cctx's */ |
1290 | /* coro_transfer creates new, empty cctx's */ |
1236 | if (cctx->sptr) |
1291 | if (cctx->sptr) |
… | |
… | |
1299 | /* TODO: throwing up here is considered harmful */ |
1354 | /* TODO: throwing up here is considered harmful */ |
1300 | |
1355 | |
1301 | if (expect_true (prev != next)) |
1356 | if (expect_true (prev != next)) |
1302 | { |
1357 | { |
1303 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1358 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1304 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); |
1359 | croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); |
1305 | |
1360 | |
1306 | if (expect_false (next->flags & CF_RUNNING)) |
1361 | if (expect_false (next->flags & CF_RUNNING)) |
1307 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1362 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1308 | |
1363 | |
1309 | if (expect_false (next->flags & CF_DESTROYED)) |
1364 | if (expect_false (next->flags & CF_DESTROYED)) |
… | |
… | |
1321 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1376 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1322 | { |
1377 | { |
1323 | dSTACKLEVEL; |
1378 | dSTACKLEVEL; |
1324 | |
1379 | |
1325 | /* sometimes transfer is only called to set idle_sp */ |
1380 | /* sometimes transfer is only called to set idle_sp */ |
1326 | if (expect_false (!next)) |
1381 | if (expect_false (!prev)) |
1327 | { |
1382 | { |
1328 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1383 | cctx_current->idle_sp = STACKLEVEL; |
1329 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1384 | assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1330 | } |
1385 | } |
1331 | else if (expect_true (prev != next)) |
1386 | else if (expect_true (prev != next)) |
1332 | { |
1387 | { |
1333 | coro_cctx *prev__cctx; |
1388 | coro_cctx *cctx_prev; |
1334 | |
1389 | |
1335 | if (expect_false (prev->flags & CF_NEW)) |
1390 | if (expect_false (prev->flags & CF_NEW)) |
1336 | { |
1391 | { |
1337 | /* create a new empty/source context */ |
1392 | /* create a new empty/source context */ |
1338 | prev->cctx = cctx_new_empty (); |
|
|
1339 | prev->flags &= ~CF_NEW; |
1393 | prev->flags &= ~CF_NEW; |
1340 | prev->flags |= CF_RUNNING; |
1394 | prev->flags |= CF_RUNNING; |
1341 | } |
1395 | } |
1342 | |
1396 | |
1343 | prev->flags &= ~CF_RUNNING; |
1397 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1354 | coro_setup (aTHX_ next); |
1408 | coro_setup (aTHX_ next); |
1355 | } |
1409 | } |
1356 | else |
1410 | else |
1357 | load_perl (aTHX_ next); |
1411 | load_perl (aTHX_ next); |
1358 | |
1412 | |
1359 | prev__cctx = prev->cctx; |
|
|
1360 | |
|
|
1361 | /* possibly untie and reuse the cctx */ |
1413 | /* possibly untie and reuse the cctx */ |
1362 | if (expect_true ( |
1414 | if (expect_true ( |
1363 | prev__cctx->idle_sp == STACKLEVEL |
1415 | cctx_current->idle_sp == STACKLEVEL |
1364 | && !(prev__cctx->flags & CC_TRACE) |
1416 | && !(cctx_current->flags & CC_TRACE) |
1365 | && !force_cctx |
1417 | && !force_cctx |
1366 | )) |
1418 | )) |
1367 | { |
1419 | { |
1368 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1420 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1369 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); |
1421 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1370 | |
1422 | |
1371 | prev->cctx = 0; |
|
|
1372 | |
|
|
1373 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1423 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ |
1374 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1424 | /* without this the next cctx_get might destroy the running cctx while still in use */ |
1375 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1425 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1376 | if (!next->cctx) |
1426 | if (expect_true (!next->cctx)) |
1377 | next->cctx = cctx_get (aTHX); |
1427 | next->cctx = cctx_get (aTHX); |
1378 | |
1428 | |
1379 | cctx_put (prev__cctx); |
1429 | cctx_put (cctx_current); |
1380 | } |
1430 | } |
|
|
1431 | else |
|
|
1432 | prev->cctx = cctx_current; |
1381 | |
1433 | |
1382 | ++next->usecount; |
1434 | ++next->usecount; |
1383 | |
1435 | |
1384 | if (expect_true (!next->cctx)) |
1436 | cctx_prev = cctx_current; |
1385 | next->cctx = cctx_get (aTHX); |
1437 | cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1386 | |
1438 | |
1387 | if (expect_false (prev__cctx != next->cctx)) |
1439 | next->cctx = 0; |
|
|
1440 | |
|
|
1441 | if (expect_false (cctx_prev != cctx_current)) |
1388 | { |
1442 | { |
1389 | prev__cctx->top_env = PL_top_env; |
1443 | cctx_prev->top_env = PL_top_env; |
1390 | PL_top_env = next->cctx->top_env; |
1444 | PL_top_env = cctx_current->top_env; |
1391 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1445 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1392 | } |
1446 | } |
1393 | |
1447 | |
1394 | transfer_tail (aTHX); |
1448 | transfer_tail (aTHX); |
1395 | } |
1449 | } |
1396 | } |
1450 | } |
… | |
… | |
1418 | --coro_nready; |
1472 | --coro_nready; |
1419 | } |
1473 | } |
1420 | else |
1474 | else |
1421 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1475 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1422 | |
1476 | |
1423 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1477 | if (coro->mainstack |
|
|
1478 | && coro->mainstack != main_mainstack |
|
|
1479 | && coro->slot |
|
|
1480 | && !PL_dirty) |
1424 | { |
1481 | { |
1425 | struct coro temp; |
1482 | struct coro temp; |
1426 | |
1483 | |
1427 | assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); |
1484 | assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); |
1428 | |
1485 | |
1429 | save_perl (aTHX_ &temp); |
1486 | save_perl (aTHX_ &temp); |
1430 | load_perl (aTHX_ coro); |
1487 | load_perl (aTHX_ coro); |
1431 | |
1488 | |
1432 | coro_destruct (aTHX_ coro); |
1489 | coro_destruct_perl (aTHX_ coro); |
1433 | |
1490 | |
1434 | load_perl (aTHX_ &temp); |
1491 | load_perl (aTHX_ &temp); |
1435 | |
1492 | |
1436 | coro->slot = 0; |
1493 | coro->slot = 0; |
1437 | } |
1494 | } |
1438 | |
1495 | |
1439 | cctx_destroy (coro->cctx); |
1496 | cctx_destroy (coro->cctx); |
|
|
1497 | SvREFCNT_dec (coro->startcv); |
1440 | SvREFCNT_dec (coro->args); |
1498 | SvREFCNT_dec (coro->args); |
|
|
1499 | SvREFCNT_dec (CORO_THROW); |
1441 | |
1500 | |
1442 | if (coro->next) coro->next->prev = coro->prev; |
1501 | if (coro->next) coro->next->prev = coro->prev; |
1443 | if (coro->prev) coro->prev->next = coro->next; |
1502 | if (coro->prev) coro->prev->next = coro->next; |
1444 | if (coro == coro_first) coro_first = coro->next; |
1503 | if (coro == coro_first) coro_first = coro->next; |
1445 | |
1504 | |
… | |
… | |
1549 | { |
1608 | { |
1550 | struct coro *coro; |
1609 | struct coro *coro; |
1551 | SV *sv_hook; |
1610 | SV *sv_hook; |
1552 | void (*xs_hook)(void); |
1611 | void (*xs_hook)(void); |
1553 | |
1612 | |
1554 | if (SvROK (coro_sv)) |
|
|
1555 | coro_sv = SvRV (coro_sv); |
|
|
1556 | |
|
|
1557 | coro = SvSTATE (coro_sv); |
1613 | coro = SvSTATE (coro_sv); |
1558 | |
1614 | |
1559 | if (coro->flags & CF_READY) |
1615 | if (coro->flags & CF_READY) |
1560 | return 0; |
1616 | return 0; |
1561 | |
1617 | |
… | |
… | |
1592 | api_is_ready (pTHX_ SV *coro_sv) |
1648 | api_is_ready (pTHX_ SV *coro_sv) |
1593 | { |
1649 | { |
1594 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1650 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1595 | } |
1651 | } |
1596 | |
1652 | |
|
|
1653 | /* expects to own a reference to next->hv */ |
1597 | INLINE void |
1654 | INLINE void |
1598 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1655 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1599 | { |
1656 | { |
1600 | SV *prev_sv, *next_sv; |
|
|
1601 | |
|
|
1602 | for (;;) |
|
|
1603 | { |
|
|
1604 | next_sv = coro_deq (aTHX); |
|
|
1605 | |
|
|
1606 | /* nothing to schedule: call the idle handler */ |
|
|
1607 | if (expect_false (!next_sv)) |
|
|
1608 | { |
|
|
1609 | dSP; |
|
|
1610 | |
|
|
1611 | ENTER; |
|
|
1612 | SAVETMPS; |
|
|
1613 | |
|
|
1614 | PUSHMARK (SP); |
|
|
1615 | PUTBACK; |
|
|
1616 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
|
|
1617 | |
|
|
1618 | FREETMPS; |
|
|
1619 | LEAVE; |
|
|
1620 | continue; |
|
|
1621 | } |
|
|
1622 | |
|
|
1623 | ta->next = SvSTATE_hv (next_sv); |
|
|
1624 | |
|
|
1625 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1626 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
|
|
1627 | { |
|
|
1628 | SvREFCNT_dec (next_sv); |
|
|
1629 | /* coro_nready has already been taken care of by destroy */ |
|
|
1630 | continue; |
|
|
1631 | } |
|
|
1632 | |
|
|
1633 | --coro_nready; |
|
|
1634 | break; |
|
|
1635 | } |
|
|
1636 | |
|
|
1637 | /* free this only after the transfer */ |
|
|
1638 | prev_sv = SvRV (coro_current); |
1657 | SV *prev_sv = SvRV (coro_current); |
|
|
1658 | |
1639 | ta->prev = SvSTATE_hv (prev_sv); |
1659 | ta->prev = SvSTATE_hv (prev_sv); |
|
|
1660 | ta->next = next; |
|
|
1661 | |
1640 | TRANSFER_CHECK (*ta); |
1662 | TRANSFER_CHECK (*ta); |
1641 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1663 | |
1642 | ta->next->flags &= ~CF_READY; |
|
|
1643 | SvRV_set (coro_current, next_sv); |
1664 | SvRV_set (coro_current, (SV *)next->hv); |
1644 | |
1665 | |
1645 | free_coro_mortal (aTHX); |
1666 | free_coro_mortal (aTHX); |
1646 | coro_mortal = prev_sv; |
1667 | coro_mortal = prev_sv; |
1647 | } |
1668 | } |
1648 | |
1669 | |
|
|
1670 | static void |
|
|
1671 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
|
|
1672 | { |
|
|
1673 | for (;;) |
|
|
1674 | { |
|
|
1675 | SV *next_sv = coro_deq (aTHX); |
|
|
1676 | |
|
|
1677 | if (expect_true (next_sv)) |
|
|
1678 | { |
|
|
1679 | struct coro *next = SvSTATE_hv (next_sv); |
|
|
1680 | |
|
|
1681 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1682 | if (expect_false (next->flags & CF_DESTROYED)) |
|
|
1683 | SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ |
|
|
1684 | else |
|
|
1685 | { |
|
|
1686 | next->flags &= ~CF_READY; |
|
|
1687 | --coro_nready; |
|
|
1688 | |
|
|
1689 | prepare_schedule_to (aTHX_ ta, next); |
|
|
1690 | break; |
|
|
1691 | } |
|
|
1692 | } |
|
|
1693 | else |
|
|
1694 | { |
|
|
1695 | /* nothing to schedule: call the idle handler */ |
|
|
1696 | if (SvROK (sv_idle) |
|
|
1697 | && SvOBJECT (SvRV (sv_idle))) |
|
|
1698 | { |
|
|
1699 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
|
|
1700 | api_ready (aTHX_ SvRV (sv_idle)); |
|
|
1701 | --coro_nready; |
|
|
1702 | } |
|
|
1703 | else |
|
|
1704 | { |
|
|
1705 | dSP; |
|
|
1706 | |
|
|
1707 | ENTER; |
|
|
1708 | SAVETMPS; |
|
|
1709 | |
|
|
1710 | PUSHMARK (SP); |
|
|
1711 | PUTBACK; |
|
|
1712 | call_sv (sv_idle, G_VOID | G_DISCARD); |
|
|
1713 | |
|
|
1714 | FREETMPS; |
|
|
1715 | LEAVE; |
|
|
1716 | } |
|
|
1717 | } |
|
|
1718 | } |
|
|
1719 | } |
|
|
1720 | |
1649 | INLINE void |
1721 | INLINE void |
1650 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1722 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1651 | { |
1723 | { |
1652 | api_ready (aTHX_ coro_current); |
1724 | api_ready (aTHX_ coro_current); |
1653 | prepare_schedule (aTHX_ ta); |
1725 | prepare_schedule (aTHX_ ta); |
… | |
… | |
1672 | { |
1744 | { |
1673 | struct coro_transfer_args ta; |
1745 | struct coro_transfer_args ta; |
1674 | |
1746 | |
1675 | prepare_schedule (aTHX_ &ta); |
1747 | prepare_schedule (aTHX_ &ta); |
1676 | TRANSFER (ta, 1); |
1748 | TRANSFER (ta, 1); |
|
|
1749 | } |
|
|
1750 | |
|
|
1751 | static void |
|
|
1752 | api_schedule_to (pTHX_ SV *coro_sv) |
|
|
1753 | { |
|
|
1754 | struct coro_transfer_args ta; |
|
|
1755 | struct coro *next = SvSTATE (coro_sv); |
|
|
1756 | |
|
|
1757 | SvREFCNT_inc_NN (coro_sv); |
|
|
1758 | prepare_schedule_to (aTHX_ &ta, next); |
1677 | } |
1759 | } |
1678 | |
1760 | |
1679 | static int |
1761 | static int |
1680 | api_cede (pTHX) |
1762 | api_cede (pTHX) |
1681 | { |
1763 | { |
… | |
… | |
1710 | static void |
1792 | static void |
1711 | api_trace (pTHX_ SV *coro_sv, int flags) |
1793 | api_trace (pTHX_ SV *coro_sv, int flags) |
1712 | { |
1794 | { |
1713 | struct coro *coro = SvSTATE (coro_sv); |
1795 | struct coro *coro = SvSTATE (coro_sv); |
1714 | |
1796 | |
|
|
1797 | if (coro->flags & CF_RUNNING) |
|
|
1798 | croak ("cannot enable tracing on a running coroutine, caught"); |
|
|
1799 | |
1715 | if (flags & CC_TRACE) |
1800 | if (flags & CC_TRACE) |
1716 | { |
1801 | { |
1717 | if (!coro->cctx) |
1802 | if (!coro->cctx) |
1718 | coro->cctx = cctx_new_run (); |
1803 | coro->cctx = cctx_new_run (); |
1719 | else if (!(coro->cctx->flags & CC_TRACE)) |
1804 | else if (!(coro->cctx->flags & CC_TRACE)) |
1720 | croak ("cannot enable tracing on coroutine with custom stack,"); |
1805 | croak ("cannot enable tracing on coroutine with custom stack, caught"); |
1721 | |
1806 | |
1722 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1807 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1723 | } |
1808 | } |
1724 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1809 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1725 | { |
1810 | { |
… | |
… | |
1730 | else |
1815 | else |
1731 | coro->slot->runops = RUNOPS_DEFAULT; |
1816 | coro->slot->runops = RUNOPS_DEFAULT; |
1732 | } |
1817 | } |
1733 | } |
1818 | } |
1734 | |
1819 | |
|
|
1820 | static void |
|
|
1821 | coro_call_on_destroy (pTHX_ struct coro *coro) |
|
|
1822 | { |
|
|
1823 | SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); |
|
|
1824 | SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0); |
|
|
1825 | |
|
|
1826 | if (on_destroyp) |
|
|
1827 | { |
|
|
1828 | AV *on_destroy = (AV *)SvRV (*on_destroyp); |
|
|
1829 | |
|
|
1830 | while (AvFILLp (on_destroy) >= 0) |
|
|
1831 | { |
|
|
1832 | dSP; /* don't disturb outer sp */ |
|
|
1833 | SV *cb = av_pop (on_destroy); |
|
|
1834 | |
|
|
1835 | PUSHMARK (SP); |
|
|
1836 | |
|
|
1837 | if (statusp) |
|
|
1838 | { |
|
|
1839 | int i; |
|
|
1840 | AV *status = (AV *)SvRV (*statusp); |
|
|
1841 | EXTEND (SP, AvFILLp (status) + 1); |
|
|
1842 | |
|
|
1843 | for (i = 0; i <= AvFILLp (status); ++i) |
|
|
1844 | PUSHs (AvARRAY (status)[i]); |
|
|
1845 | } |
|
|
1846 | |
|
|
1847 | PUTBACK; |
|
|
1848 | call_sv (sv_2mortal (cb), G_VOID | G_DISCARD); |
|
|
1849 | } |
|
|
1850 | } |
|
|
1851 | } |
|
|
1852 | |
|
|
1853 | static void |
|
|
1854 | slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1855 | { |
|
|
1856 | int i; |
|
|
1857 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1858 | AV *av = newAV (); |
|
|
1859 | |
|
|
1860 | av_extend (av, items - 1); |
|
|
1861 | for (i = 0; i < items; ++i) |
|
|
1862 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
|
|
1863 | |
|
|
1864 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
|
|
1865 | |
|
|
1866 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
|
|
1867 | api_ready (aTHX_ sv_manager); |
|
|
1868 | |
|
|
1869 | frame->prepare = prepare_schedule; |
|
|
1870 | frame->check = slf_check_repeat; |
|
|
1871 | |
|
|
1872 | /* as a minor optimisation, we could unwind all stacks here */ |
|
|
1873 | /* but that puts extra pressure on pp_slf, and is not worth much */ |
|
|
1874 | /*coro_unwind_stacks (aTHX);*/ |
|
|
1875 | } |
|
|
1876 | |
|
|
1877 | /*****************************************************************************/ |
|
|
1878 | /* async pool handler */ |
|
|
1879 | |
|
|
1880 | static int |
|
|
1881 | slf_check_pool_handler (pTHX_ struct CoroSLF *frame) |
|
|
1882 | { |
|
|
1883 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1884 | struct coro *coro = (struct coro *)frame->data; |
|
|
1885 | |
|
|
1886 | if (!coro->invoke_cb) |
|
|
1887 | return 1; /* loop till we have invoke */ |
|
|
1888 | else |
|
|
1889 | { |
|
|
1890 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1891 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1892 | |
|
|
1893 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
|
|
1894 | |
|
|
1895 | { |
|
|
1896 | dSP; |
|
|
1897 | XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0; |
|
|
1898 | PUTBACK; |
|
|
1899 | } |
|
|
1900 | |
|
|
1901 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
1902 | GvAV (PL_defgv) = coro->invoke_av; |
|
|
1903 | coro->invoke_av = 0; |
|
|
1904 | |
|
|
1905 | return 0; |
|
|
1906 | } |
|
|
1907 | } |
|
|
1908 | |
|
|
1909 | static void |
|
|
1910 | slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1911 | { |
|
|
1912 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1913 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
1914 | |
|
|
1915 | if (expect_true (coro->saved_deffh)) |
|
|
1916 | { |
|
|
1917 | /* subsequent iteration */ |
|
|
1918 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
|
|
1919 | coro->saved_deffh = 0; |
|
|
1920 | |
|
|
1921 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
|
|
1922 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1923 | { |
|
|
1924 | coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); |
|
|
1925 | coro->invoke_av = newAV (); |
|
|
1926 | |
|
|
1927 | frame->prepare = prepare_nop; |
|
|
1928 | } |
|
|
1929 | else |
|
|
1930 | { |
|
|
1931 | av_clear (GvAV (PL_defgv)); |
|
|
1932 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
|
|
1933 | |
|
|
1934 | coro->prio = 0; |
|
|
1935 | |
|
|
1936 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
1937 | api_trace (aTHX_ coro_current, 0); |
|
|
1938 | |
|
|
1939 | frame->prepare = prepare_schedule; |
|
|
1940 | av_push (av_async_pool, SvREFCNT_inc (hv)); |
|
|
1941 | } |
|
|
1942 | } |
|
|
1943 | else |
|
|
1944 | { |
|
|
1945 | /* first iteration, simply fall through */ |
|
|
1946 | frame->prepare = prepare_nop; |
|
|
1947 | } |
|
|
1948 | |
|
|
1949 | frame->check = slf_check_pool_handler; |
|
|
1950 | frame->data = (void *)coro; |
|
|
1951 | } |
|
|
1952 | |
1735 | /*****************************************************************************/ |
1953 | /*****************************************************************************/ |
1736 | /* rouse callback */ |
1954 | /* rouse callback */ |
1737 | |
1955 | |
1738 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
1956 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
1739 | |
1957 | |
… | |
… | |
1744 | SV *data = (SV *)GENSUB_ARG; |
1962 | SV *data = (SV *)GENSUB_ARG; |
1745 | |
1963 | |
1746 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
1964 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
1747 | { |
1965 | { |
1748 | /* first call, set args */ |
1966 | /* first call, set args */ |
1749 | int i; |
|
|
1750 | AV *av = newAV (); |
1967 | AV *av = newAV (); |
1751 | SV *coro = SvRV (data); |
1968 | SV *coro = SvRV (data); |
1752 | |
1969 | |
1753 | SvRV_set (data, (SV *)av); |
1970 | SvRV_set (data, (SV *)av); |
1754 | api_ready (aTHX_ coro); |
1971 | api_ready (aTHX_ coro); |
… | |
… | |
1895 | static void |
2112 | static void |
1896 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2113 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1897 | { |
2114 | { |
1898 | frame->prepare = prepare_schedule; |
2115 | frame->prepare = prepare_schedule; |
1899 | frame->check = slf_check_nop; |
2116 | frame->check = slf_check_nop; |
|
|
2117 | } |
|
|
2118 | |
|
|
2119 | static void |
|
|
2120 | slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta) |
|
|
2121 | { |
|
|
2122 | struct coro *next = (struct coro *)slf_frame.data; |
|
|
2123 | |
|
|
2124 | SvREFCNT_inc_NN (next->hv); |
|
|
2125 | prepare_schedule_to (aTHX_ ta, next); |
|
|
2126 | } |
|
|
2127 | |
|
|
2128 | static void |
|
|
2129 | slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2130 | { |
|
|
2131 | if (!items) |
|
|
2132 | croak ("Coro::schedule_to expects a coroutine argument, caught"); |
|
|
2133 | |
|
|
2134 | frame->data = (void *)SvSTATE (arg [0]); |
|
|
2135 | frame->prepare = slf_prepare_schedule_to; |
|
|
2136 | frame->check = slf_check_nop; |
|
|
2137 | } |
|
|
2138 | |
|
|
2139 | static void |
|
|
2140 | slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2141 | { |
|
|
2142 | api_ready (aTHX_ SvRV (coro_current)); |
|
|
2143 | |
|
|
2144 | slf_init_schedule_to (aTHX_ frame, cv, arg, items); |
1900 | } |
2145 | } |
1901 | |
2146 | |
1902 | static void |
2147 | static void |
1903 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2148 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1904 | { |
2149 | { |
… | |
… | |
2050 | } |
2295 | } |
2051 | else |
2296 | else |
2052 | slf_argc = 0; |
2297 | slf_argc = 0; |
2053 | |
2298 | |
2054 | PL_op->op_ppaddr = pp_slf; |
2299 | PL_op->op_ppaddr = pp_slf; |
2055 | PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ |
2300 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2056 | |
2301 | |
2057 | PL_op = (OP *)&slf_restore; |
2302 | PL_op = (OP *)&slf_restore; |
2058 | } |
2303 | } |
2059 | |
2304 | |
2060 | /*****************************************************************************/ |
2305 | /*****************************************************************************/ |
… | |
… | |
2143 | SV **ary; |
2388 | SV **ary; |
2144 | |
2389 | |
2145 | /* unfortunately, building manually saves memory */ |
2390 | /* unfortunately, building manually saves memory */ |
2146 | Newx (ary, 2, SV *); |
2391 | Newx (ary, 2, SV *); |
2147 | AvALLOC (av) = ary; |
2392 | AvALLOC (av) = ary; |
|
|
2393 | #if PERL_VERSION_ATLEAST (5,10,0) |
2148 | /*AvARRAY (av) = ary;*/ |
2394 | AvARRAY (av) = ary; |
|
|
2395 | #else |
2149 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
2396 | /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */ |
|
|
2397 | /* -DDEBUGGING flags this as a bug, despite it perfectly working */ |
|
|
2398 | SvPVX ((SV *)av) = (char *)ary; |
|
|
2399 | #endif |
2150 | AvMAX (av) = 1; |
2400 | AvMAX (av) = 1; |
2151 | AvFILLp (av) = 0; |
2401 | AvFILLp (av) = 0; |
2152 | ary [0] = newSViv (count); |
2402 | ary [0] = newSViv (count); |
2153 | |
2403 | |
2154 | return newRV_noinc ((SV *)av); |
2404 | return newRV_noinc ((SV *)av); |
… | |
… | |
2174 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2424 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2175 | AvARRAY (av)[1] = count_sv; |
2425 | AvARRAY (av)[1] = count_sv; |
2176 | cb = av_shift (av); |
2426 | cb = av_shift (av); |
2177 | |
2427 | |
2178 | if (SvOBJECT (cb)) |
2428 | if (SvOBJECT (cb)) |
|
|
2429 | { |
2179 | api_ready (aTHX_ cb); |
2430 | api_ready (aTHX_ cb); |
2180 | else |
2431 | --count; |
2181 | croak ("callbacks not yet supported"); |
2432 | } |
|
|
2433 | else if (SvTYPE (cb) == SVt_PVCV) |
|
|
2434 | { |
|
|
2435 | dSP; |
|
|
2436 | PUSHMARK (SP); |
|
|
2437 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
2438 | PUTBACK; |
|
|
2439 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
|
|
2440 | } |
2182 | |
2441 | |
2183 | SvREFCNT_dec (cb); |
2442 | SvREFCNT_dec (cb); |
2184 | |
|
|
2185 | --count; |
|
|
2186 | } |
2443 | } |
2187 | } |
2444 | } |
2188 | |
2445 | |
2189 | static void |
2446 | static void |
2190 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
2447 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
… | |
… | |
2272 | } |
2529 | } |
2273 | |
2530 | |
2274 | static void |
2531 | static void |
2275 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2532 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2276 | { |
2533 | { |
|
|
2534 | if (items >= 2) |
|
|
2535 | { |
|
|
2536 | /* callback form */ |
|
|
2537 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2538 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
|
|
2539 | |
|
|
2540 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
|
|
2541 | |
|
|
2542 | if (SvIVX (AvARRAY (av)[0]) > 0) |
|
|
2543 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2544 | |
|
|
2545 | frame->prepare = prepare_nop; |
|
|
2546 | frame->check = slf_check_nop; |
|
|
2547 | } |
|
|
2548 | else |
|
|
2549 | { |
2277 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2550 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2278 | frame->check = slf_check_semaphore_wait; |
2551 | frame->check = slf_check_semaphore_wait; |
|
|
2552 | } |
2279 | } |
2553 | } |
2280 | |
2554 | |
2281 | /* signal */ |
2555 | /* signal */ |
2282 | |
2556 | |
2283 | static void |
2557 | static void |
… | |
… | |
2355 | dXSARGS; |
2629 | dXSARGS; |
2356 | AV *state = (AV *)GENSUB_ARG; |
2630 | AV *state = (AV *)GENSUB_ARG; |
2357 | SV *coro = av_pop (state); |
2631 | SV *coro = av_pop (state); |
2358 | SV *data_sv = newSV (sizeof (struct io_state)); |
2632 | SV *data_sv = newSV (sizeof (struct io_state)); |
2359 | |
2633 | |
2360 | av_extend (state, items); |
2634 | av_extend (state, items - 1); |
2361 | |
2635 | |
2362 | sv_upgrade (data_sv, SVt_PV); |
2636 | sv_upgrade (data_sv, SVt_PV); |
2363 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2637 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2364 | SvPOK_only (data_sv); |
2638 | SvPOK_only (data_sv); |
2365 | |
2639 | |
… | |
… | |
2500 | XSRETURN_EMPTY; |
2774 | XSRETURN_EMPTY; |
2501 | } |
2775 | } |
2502 | |
2776 | |
2503 | /*****************************************************************************/ |
2777 | /*****************************************************************************/ |
2504 | |
2778 | |
|
|
2779 | #if CORO_CLONE |
|
|
2780 | # include "clone.c" |
|
|
2781 | #endif |
|
|
2782 | |
2505 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2783 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2506 | |
2784 | |
2507 | PROTOTYPES: DISABLE |
2785 | PROTOTYPES: DISABLE |
2508 | |
2786 | |
2509 | BOOT: |
2787 | BOOT: |
… | |
… | |
2513 | coro_thx = PERL_GET_CONTEXT; |
2791 | coro_thx = PERL_GET_CONTEXT; |
2514 | # endif |
2792 | # endif |
2515 | #endif |
2793 | #endif |
2516 | BOOT_PAGESIZE; |
2794 | BOOT_PAGESIZE; |
2517 | |
2795 | |
|
|
2796 | cctx_current = cctx_new_empty (); |
|
|
2797 | |
2518 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2798 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2519 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2799 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2520 | |
2800 | |
2521 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
2801 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
2522 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
2802 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
… | |
… | |
2541 | |
2821 | |
2542 | { |
2822 | { |
2543 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2823 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2544 | |
2824 | |
2545 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2825 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2546 | hv_store_ent (PL_custom_op_names, slf, |
2826 | hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0); |
2547 | newSVpv ("coro_slf", 0), 0); |
|
|
2548 | |
2827 | |
2549 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2828 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2550 | hv_store_ent (PL_custom_op_descs, slf, |
2829 | hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0); |
2551 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2552 | } |
2830 | } |
2553 | |
2831 | |
2554 | coroapi.ver = CORO_API_VERSION; |
2832 | coroapi.ver = CORO_API_VERSION; |
2555 | coroapi.rev = CORO_API_REVISION; |
2833 | coroapi.rev = CORO_API_REVISION; |
2556 | |
2834 | |
… | |
… | |
2575 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2853 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2576 | } |
2854 | } |
2577 | |
2855 | |
2578 | SV * |
2856 | SV * |
2579 | new (char *klass, ...) |
2857 | new (char *klass, ...) |
|
|
2858 | ALIAS: |
|
|
2859 | Coro::new = 1 |
2580 | CODE: |
2860 | CODE: |
2581 | { |
2861 | { |
2582 | struct coro *coro; |
2862 | struct coro *coro; |
2583 | MAGIC *mg; |
2863 | MAGIC *mg; |
2584 | HV *hv; |
2864 | HV *hv; |
|
|
2865 | CV *cb; |
2585 | int i; |
2866 | int i; |
|
|
2867 | |
|
|
2868 | if (items > 1) |
|
|
2869 | { |
|
|
2870 | cb = coro_sv_2cv (aTHX_ ST (1)); |
|
|
2871 | |
|
|
2872 | if (!ix) |
|
|
2873 | { |
|
|
2874 | if (CvISXSUB (cb)) |
|
|
2875 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
2876 | |
|
|
2877 | if (!CvROOT (cb)) |
|
|
2878 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
2879 | } |
|
|
2880 | } |
2586 | |
2881 | |
2587 | Newz (0, coro, 1, struct coro); |
2882 | Newz (0, coro, 1, struct coro); |
2588 | coro->args = newAV (); |
2883 | coro->args = newAV (); |
2589 | coro->flags = CF_NEW; |
2884 | coro->flags = CF_NEW; |
2590 | |
2885 | |
… | |
… | |
2595 | coro->hv = hv = newHV (); |
2890 | coro->hv = hv = newHV (); |
2596 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2891 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2597 | mg->mg_flags |= MGf_DUP; |
2892 | mg->mg_flags |= MGf_DUP; |
2598 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2893 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2599 | |
2894 | |
|
|
2895 | if (items > 1) |
|
|
2896 | { |
2600 | av_extend (coro->args, items - 1); |
2897 | av_extend (coro->args, items - 1 + ix - 1); |
|
|
2898 | |
|
|
2899 | if (ix) |
|
|
2900 | { |
|
|
2901 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
2902 | cb = cv_coro_run; |
|
|
2903 | } |
|
|
2904 | |
|
|
2905 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
2906 | |
2601 | for (i = 1; i < items; i++) |
2907 | for (i = 2; i < items; i++) |
2602 | av_push (coro->args, newSVsv (ST (i))); |
2908 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2909 | } |
2603 | } |
2910 | } |
2604 | OUTPUT: |
2911 | OUTPUT: |
2605 | RETVAL |
2912 | RETVAL |
2606 | |
2913 | |
2607 | void |
2914 | void |
… | |
… | |
2620 | void |
2927 | void |
2621 | _exit (int code) |
2928 | _exit (int code) |
2622 | PROTOTYPE: $ |
2929 | PROTOTYPE: $ |
2623 | CODE: |
2930 | CODE: |
2624 | _exit (code); |
2931 | _exit (code); |
|
|
2932 | |
|
|
2933 | SV * |
|
|
2934 | clone (Coro::State coro) |
|
|
2935 | CODE: |
|
|
2936 | { |
|
|
2937 | #if CORO_CLONE |
|
|
2938 | struct coro *ncoro = coro_clone (aTHX_ coro); |
|
|
2939 | MAGIC *mg; |
|
|
2940 | /* TODO: too much duplication */ |
|
|
2941 | ncoro->hv = newHV (); |
|
|
2942 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
|
|
2943 | mg->mg_flags |= MGf_DUP; |
|
|
2944 | RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv)); |
|
|
2945 | #else |
|
|
2946 | croak ("Coro::State->clone has not been configured into this installation of Coro, realised"); |
|
|
2947 | #endif |
|
|
2948 | } |
|
|
2949 | OUTPUT: |
|
|
2950 | RETVAL |
2625 | |
2951 | |
2626 | int |
2952 | int |
2627 | cctx_stacksize (int new_stacksize = 0) |
2953 | cctx_stacksize (int new_stacksize = 0) |
2628 | PROTOTYPE: ;$ |
2954 | PROTOTYPE: ;$ |
2629 | CODE: |
2955 | CODE: |
… | |
… | |
2750 | |
3076 | |
2751 | SV * |
3077 | SV * |
2752 | has_cctx (Coro::State coro) |
3078 | has_cctx (Coro::State coro) |
2753 | PROTOTYPE: $ |
3079 | PROTOTYPE: $ |
2754 | CODE: |
3080 | CODE: |
2755 | RETVAL = boolSV (!!coro->cctx); |
3081 | /* maybe manage the running flag differently */ |
|
|
3082 | RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING)); |
2756 | OUTPUT: |
3083 | OUTPUT: |
2757 | RETVAL |
3084 | RETVAL |
2758 | |
3085 | |
2759 | int |
3086 | int |
2760 | is_traced (Coro::State coro) |
3087 | is_traced (Coro::State coro) |
… | |
… | |
2780 | |
3107 | |
2781 | void |
3108 | void |
2782 | force_cctx () |
3109 | force_cctx () |
2783 | PROTOTYPE: |
3110 | PROTOTYPE: |
2784 | CODE: |
3111 | CODE: |
2785 | SvSTATE_current->cctx->idle_sp = 0; |
3112 | cctx_current->idle_sp = 0; |
2786 | |
3113 | |
2787 | void |
3114 | void |
2788 | swap_defsv (Coro::State self) |
3115 | swap_defsv (Coro::State self) |
2789 | PROTOTYPE: $ |
3116 | PROTOTYPE: $ |
2790 | ALIAS: |
3117 | ALIAS: |
… | |
… | |
2805 | |
3132 | |
2806 | BOOT: |
3133 | BOOT: |
2807 | { |
3134 | { |
2808 | int i; |
3135 | int i; |
2809 | |
3136 | |
2810 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
2811 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3137 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2812 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3138 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2813 | |
3139 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
|
|
3140 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
2814 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
3141 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
2815 | SvREADONLY_on (coro_current); |
3142 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
3143 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
|
|
3144 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
|
|
3145 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
|
|
3146 | |
|
|
3147 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
|
|
3148 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
|
|
3149 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
|
|
3150 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
2816 | |
3151 | |
2817 | coro_stash = gv_stashpv ("Coro", TRUE); |
3152 | coro_stash = gv_stashpv ("Coro", TRUE); |
2818 | |
3153 | |
2819 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
3154 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2820 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
3155 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
… | |
… | |
2828 | |
3163 | |
2829 | { |
3164 | { |
2830 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3165 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
2831 | |
3166 | |
2832 | coroapi.schedule = api_schedule; |
3167 | coroapi.schedule = api_schedule; |
|
|
3168 | coroapi.schedule_to = api_schedule_to; |
2833 | coroapi.cede = api_cede; |
3169 | coroapi.cede = api_cede; |
2834 | coroapi.cede_notself = api_cede_notself; |
3170 | coroapi.cede_notself = api_cede_notself; |
2835 | coroapi.ready = api_ready; |
3171 | coroapi.ready = api_ready; |
2836 | coroapi.is_ready = api_is_ready; |
3172 | coroapi.is_ready = api_is_ready; |
2837 | coroapi.nready = coro_nready; |
3173 | coroapi.nready = coro_nready; |
… | |
… | |
2842 | SvREADONLY_on (sv); |
3178 | SvREADONLY_on (sv); |
2843 | } |
3179 | } |
2844 | } |
3180 | } |
2845 | |
3181 | |
2846 | void |
3182 | void |
|
|
3183 | terminate (...) |
|
|
3184 | CODE: |
|
|
3185 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
|
|
3186 | |
|
|
3187 | void |
2847 | schedule (...) |
3188 | schedule (...) |
2848 | CODE: |
3189 | CODE: |
2849 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
3190 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
|
|
3191 | |
|
|
3192 | void |
|
|
3193 | schedule_to (...) |
|
|
3194 | CODE: |
|
|
3195 | CORO_EXECUTE_SLF_XS (slf_init_schedule_to); |
|
|
3196 | |
|
|
3197 | void |
|
|
3198 | cede_to (...) |
|
|
3199 | CODE: |
|
|
3200 | CORO_EXECUTE_SLF_XS (slf_init_cede_to); |
2850 | |
3201 | |
2851 | void |
3202 | void |
2852 | cede (...) |
3203 | cede (...) |
2853 | CODE: |
3204 | CODE: |
2854 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3205 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
2855 | |
3206 | |
2856 | void |
3207 | void |
2857 | cede_notself (...) |
3208 | cede_notself (...) |
2858 | CODE: |
3209 | CODE: |
2859 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3210 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
|
|
3211 | |
|
|
3212 | void |
|
|
3213 | _cancel (Coro::State self) |
|
|
3214 | CODE: |
|
|
3215 | coro_state_destroy (aTHX_ self); |
|
|
3216 | coro_call_on_destroy (aTHX_ self); |
2860 | |
3217 | |
2861 | void |
3218 | void |
2862 | _set_current (SV *current) |
3219 | _set_current (SV *current) |
2863 | PROTOTYPE: $ |
3220 | PROTOTYPE: $ |
2864 | CODE: |
3221 | CODE: |
… | |
… | |
2909 | CODE: |
3266 | CODE: |
2910 | RETVAL = coro_nready; |
3267 | RETVAL = coro_nready; |
2911 | OUTPUT: |
3268 | OUTPUT: |
2912 | RETVAL |
3269 | RETVAL |
2913 | |
3270 | |
2914 | # for async_pool speedup |
|
|
2915 | void |
3271 | void |
2916 | _pool_1 (SV *cb) |
3272 | _pool_handler (...) |
2917 | CODE: |
3273 | CODE: |
2918 | { |
3274 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
2919 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2920 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
2921 | AV *defav = GvAV (PL_defgv); |
|
|
2922 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
2923 | AV *invoke_av; |
|
|
2924 | int i, len; |
|
|
2925 | |
3275 | |
2926 | if (!invoke) |
3276 | void |
|
|
3277 | async_pool (SV *cv, ...) |
|
|
3278 | PROTOTYPE: &@ |
|
|
3279 | PPCODE: |
|
|
3280 | { |
|
|
3281 | HV *hv = (HV *)av_pop (av_async_pool); |
|
|
3282 | AV *av = newAV (); |
|
|
3283 | SV *cb = ST (0); |
|
|
3284 | int i; |
|
|
3285 | |
|
|
3286 | av_extend (av, items - 2); |
|
|
3287 | for (i = 1; i < items; ++i) |
|
|
3288 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
|
|
3289 | |
|
|
3290 | if ((SV *)hv == &PL_sv_undef) |
2927 | { |
3291 | { |
2928 | SV *old = PL_diehook; |
3292 | PUSHMARK (SP); |
2929 | PL_diehook = 0; |
3293 | EXTEND (SP, 2); |
2930 | SvREFCNT_dec (old); |
3294 | PUSHs (sv_Coro); |
2931 | croak ("\3async_pool terminate\2\n"); |
3295 | PUSHs ((SV *)cv_pool_handler); |
|
|
3296 | PUTBACK; |
|
|
3297 | call_sv ((SV *)cv_coro_state_new, G_SCALAR); |
|
|
3298 | SPAGAIN; |
|
|
3299 | |
|
|
3300 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
2932 | } |
3301 | } |
2933 | |
3302 | |
2934 | SvREFCNT_dec (coro->saved_deffh); |
|
|
2935 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
|
|
2936 | |
|
|
2937 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
2938 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
2939 | |
|
|
2940 | invoke_av = (AV *)SvRV (invoke); |
|
|
2941 | len = av_len (invoke_av); |
|
|
2942 | |
|
|
2943 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
2944 | |
|
|
2945 | if (len > 0) |
|
|
2946 | { |
3303 | { |
2947 | av_fill (defav, len - 1); |
3304 | struct coro *coro = SvSTATE_hv (hv); |
2948 | for (i = 0; i < len; ++i) |
3305 | |
2949 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
3306 | assert (!coro->invoke_cb); |
|
|
3307 | assert (!coro->invoke_av); |
|
|
3308 | coro->invoke_cb = SvREFCNT_inc (cb); |
|
|
3309 | coro->invoke_av = av; |
2950 | } |
3310 | } |
2951 | } |
|
|
2952 | |
3311 | |
2953 | void |
3312 | api_ready (aTHX_ (SV *)hv); |
2954 | _pool_2 (SV *cb) |
|
|
2955 | CODE: |
|
|
2956 | { |
|
|
2957 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2958 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
2959 | |
3313 | |
2960 | sv_setsv (cb, &PL_sv_undef); |
3314 | if (GIMME_V != G_VOID) |
2961 | |
3315 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); |
2962 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
3316 | else |
2963 | coro->saved_deffh = 0; |
|
|
2964 | |
|
|
2965 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
|
|
2966 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
2967 | { |
|
|
2968 | SV *old = PL_diehook; |
|
|
2969 | PL_diehook = 0; |
|
|
2970 | SvREFCNT_dec (old); |
3317 | SvREFCNT_dec (hv); |
2971 | croak ("\3async_pool terminate\2\n"); |
|
|
2972 | } |
|
|
2973 | |
|
|
2974 | av_clear (GvAV (PL_defgv)); |
|
|
2975 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
2976 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
2977 | |
|
|
2978 | coro->prio = 0; |
|
|
2979 | |
|
|
2980 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
2981 | api_trace (aTHX_ coro_current, 0); |
|
|
2982 | |
|
|
2983 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
2984 | } |
3318 | } |
2985 | |
3319 | |
2986 | SV * |
3320 | SV * |
2987 | rouse_cb () |
3321 | rouse_cb () |
2988 | PROTOTYPE: |
3322 | PROTOTYPE: |
… | |
… | |
2990 | RETVAL = coro_new_rouse_cb (aTHX); |
3324 | RETVAL = coro_new_rouse_cb (aTHX); |
2991 | OUTPUT: |
3325 | OUTPUT: |
2992 | RETVAL |
3326 | RETVAL |
2993 | |
3327 | |
2994 | void |
3328 | void |
2995 | rouse_wait (SV *cb = 0) |
3329 | rouse_wait (...) |
2996 | PROTOTYPE: ;$ |
3330 | PROTOTYPE: ;$ |
2997 | PPCODE: |
3331 | PPCODE: |
2998 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3332 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
2999 | |
3333 | |
3000 | |
3334 | |
… | |
… | |
3037 | adjust = 1 |
3371 | adjust = 1 |
3038 | CODE: |
3372 | CODE: |
3039 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3373 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3040 | |
3374 | |
3041 | void |
3375 | void |
3042 | down (SV *self) |
3376 | down (...) |
3043 | CODE: |
3377 | CODE: |
3044 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3378 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3045 | |
3379 | |
3046 | void |
3380 | void |
3047 | wait (SV *self) |
3381 | wait (...) |
3048 | CODE: |
3382 | CODE: |
3049 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
3383 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
3050 | |
3384 | |
3051 | void |
3385 | void |
3052 | try (SV *self) |
3386 | try (SV *self) |
… | |
… | |
3095 | ); |
3429 | ); |
3096 | OUTPUT: |
3430 | OUTPUT: |
3097 | RETVAL |
3431 | RETVAL |
3098 | |
3432 | |
3099 | void |
3433 | void |
3100 | wait (SV *self) |
3434 | wait (...) |
3101 | CODE: |
3435 | CODE: |
3102 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
3436 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
3103 | |
3437 | |
3104 | void |
3438 | void |
3105 | broadcast (SV *self) |
3439 | broadcast (SV *self) |
… | |
… | |
3162 | |
3496 | |
3163 | void |
3497 | void |
3164 | _register (char *target, char *proto, SV *req) |
3498 | _register (char *target, char *proto, SV *req) |
3165 | CODE: |
3499 | CODE: |
3166 | { |
3500 | { |
3167 | HV *st; |
3501 | CV *req_cv = coro_sv_2cv (aTHX_ req); |
3168 | GV *gvp; |
|
|
3169 | CV *req_cv = sv_2cv (req, &st, &gvp, 0); |
|
|
3170 | /* newXSproto doesn't return the CV on 5.8 */ |
3502 | /* newXSproto doesn't return the CV on 5.8 */ |
3171 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3503 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3172 | sv_setpv ((SV *)slf_cv, proto); |
3504 | sv_setpv ((SV *)slf_cv, proto); |
3173 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3505 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3174 | } |
3506 | } |