… | |
… | |
133 | #else |
133 | #else |
134 | # define dSTACKLEVEL volatile void *stacklevel |
134 | # define dSTACKLEVEL volatile void *stacklevel |
135 | # define STACKLEVEL ((void *)&stacklevel) |
135 | # define STACKLEVEL ((void *)&stacklevel) |
136 | #endif |
136 | #endif |
137 | |
137 | |
138 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
138 | #define IN_DESTRUCT PL_dirty |
139 | |
139 | |
140 | #if __GNUC__ >= 3 |
140 | #if __GNUC__ >= 3 |
141 | # define attribute(x) __attribute__(x) |
141 | # define attribute(x) __attribute__(x) |
142 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
142 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
143 | # define INLINE static inline |
143 | # define INLINE static inline |
144 | #else |
144 | #else |
145 | # define attribute(x) |
145 | # define attribute(x) |
146 | # define expect(expr,value) (expr) |
146 | # define expect(expr,value) (expr) |
147 | # define INLINE static |
147 | # define INLINE static |
… | |
… | |
156 | #define GCoroAPI (&coroapi) /* very sneaky */ |
156 | #define GCoroAPI (&coroapi) /* very sneaky */ |
157 | |
157 | |
158 | #ifdef USE_ITHREADS |
158 | #ifdef USE_ITHREADS |
159 | # if CORO_PTHREAD |
159 | # if CORO_PTHREAD |
160 | static void *coro_thx; |
160 | static void *coro_thx; |
|
|
161 | # endif |
|
|
162 | #endif |
|
|
163 | |
|
|
164 | #ifdef __linux |
|
|
165 | # include <time.h> /* for timespec */ |
|
|
166 | # include <syscall.h> /* for SYS_* */ |
|
|
167 | # ifdef SYS_clock_gettime |
|
|
168 | # define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
|
|
169 | # define CORO_CLOCK_MONOTONIC 1 |
|
|
170 | # define CORO_CLOCK_THREAD_CPUTIME_ID 3 |
161 | # endif |
171 | # endif |
162 | #endif |
172 | #endif |
163 | |
173 | |
164 | static double (*nvtime)(); /* so why doesn't it take void? */ |
174 | static double (*nvtime)(); /* so why doesn't it take void? */ |
165 | |
175 | |
… | |
… | |
175 | static HV *coro_state_stash, *coro_stash; |
185 | static HV *coro_state_stash, *coro_stash; |
176 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
186 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
177 | |
187 | |
178 | static AV *av_destroy; /* destruction queue */ |
188 | static AV *av_destroy; /* destruction queue */ |
179 | static SV *sv_manager; /* the manager coro */ |
189 | static SV *sv_manager; /* the manager coro */ |
|
|
190 | static SV *sv_idle; /* $Coro::idle */ |
180 | |
191 | |
181 | static GV *irsgv; /* $/ */ |
192 | static GV *irsgv; /* $/ */ |
182 | static GV *stdoutgv; /* *STDOUT */ |
193 | static GV *stdoutgv; /* *STDOUT */ |
183 | static SV *rv_diehook; |
194 | static SV *rv_diehook; |
184 | static SV *rv_warnhook; |
195 | static SV *rv_warnhook; |
… | |
… | |
194 | static CV *cv_coro_state_new; |
205 | static CV *cv_coro_state_new; |
195 | |
206 | |
196 | /* Coro::AnyEvent */ |
207 | /* Coro::AnyEvent */ |
197 | static SV *sv_activity; |
208 | static SV *sv_activity; |
198 | |
209 | |
|
|
210 | /* enable processtime/realtime profiling */ |
|
|
211 | static char profile_times; |
|
|
212 | typedef U32 coro_ts[2]; |
|
|
213 | |
199 | static struct coro_cctx *cctx_first; |
214 | static struct coro_cctx *cctx_first; |
200 | static int cctx_count, cctx_idle; |
215 | static int cctx_count, cctx_idle; |
201 | |
216 | |
202 | enum { |
217 | enum { |
203 | CC_MAPPED = 0x01, |
218 | CC_MAPPED = 0x01, |
… | |
… | |
237 | enum { |
252 | enum { |
238 | CF_RUNNING = 0x0001, /* coroutine is running */ |
253 | CF_RUNNING = 0x0001, /* coroutine is running */ |
239 | CF_READY = 0x0002, /* coroutine is ready */ |
254 | CF_READY = 0x0002, /* coroutine is ready */ |
240 | CF_NEW = 0x0004, /* has never been switched to */ |
255 | CF_NEW = 0x0004, /* has never been switched to */ |
241 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
256 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
|
|
257 | CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */ |
242 | }; |
258 | }; |
243 | |
259 | |
244 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
260 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
245 | typedef struct |
261 | typedef struct |
246 | { |
262 | { |
247 | SV *defsv; |
263 | SV *defsv; |
248 | AV *defav; |
264 | AV *defav; |
249 | SV *errsv; |
265 | SV *errsv; |
250 | SV *irsgv; |
266 | SV *irsgv; |
|
|
267 | HV *hinthv; |
251 | #define VAR(name,type) type name; |
268 | #define VAR(name,type) type name; |
252 | # include "state.h" |
269 | # include "state.h" |
253 | #undef VAR |
270 | #undef VAR |
254 | } perl_slots; |
271 | } perl_slots; |
255 | |
272 | |
… | |
… | |
257 | |
274 | |
258 | /* this is a structure representing a perl-level coroutine */ |
275 | /* this is a structure representing a perl-level coroutine */ |
259 | struct coro { |
276 | struct coro { |
260 | /* the C coroutine allocated to this perl coroutine, if any */ |
277 | /* the C coroutine allocated to this perl coroutine, if any */ |
261 | coro_cctx *cctx; |
278 | coro_cctx *cctx; |
|
|
279 | |
|
|
280 | /* ready queue */ |
|
|
281 | struct coro *next_ready; |
262 | |
282 | |
263 | /* state data */ |
283 | /* state data */ |
264 | struct CoroSLF slf_frame; /* saved slf frame */ |
284 | struct CoroSLF slf_frame; /* saved slf frame */ |
265 | AV *mainstack; |
285 | AV *mainstack; |
266 | perl_slots *slot; /* basically the saved sp */ |
286 | perl_slots *slot; /* basically the saved sp */ |
… | |
… | |
283 | /* async_pool */ |
303 | /* async_pool */ |
284 | SV *saved_deffh; |
304 | SV *saved_deffh; |
285 | SV *invoke_cb; |
305 | SV *invoke_cb; |
286 | AV *invoke_av; |
306 | AV *invoke_av; |
287 | |
307 | |
|
|
308 | /* on_enter/on_leave */ |
|
|
309 | AV *on_enter; |
|
|
310 | AV *on_leave; |
|
|
311 | |
288 | /* linked list */ |
312 | /* linked list */ |
289 | struct coro *next, *prev; |
313 | struct coro *next, *prev; |
290 | }; |
314 | }; |
291 | |
315 | |
292 | typedef struct coro *Coro__State; |
316 | typedef struct coro *Coro__State; |
… | |
… | |
297 | /* the mainr easonw e don't support windows process emulation */ |
321 | /* the mainr easonw e don't support windows process emulation */ |
298 | static struct CoroSLF slf_frame; /* the current slf frame */ |
322 | static struct CoroSLF slf_frame; /* the current slf frame */ |
299 | |
323 | |
300 | /** Coro ********************************************************************/ |
324 | /** Coro ********************************************************************/ |
301 | |
325 | |
302 | #define PRIO_MAX 3 |
326 | #define CORO_PRIO_MAX 3 |
303 | #define PRIO_HIGH 1 |
327 | #define CORO_PRIO_HIGH 1 |
304 | #define PRIO_NORMAL 0 |
328 | #define CORO_PRIO_NORMAL 0 |
305 | #define PRIO_LOW -1 |
329 | #define CORO_PRIO_LOW -1 |
306 | #define PRIO_IDLE -3 |
330 | #define CORO_PRIO_IDLE -3 |
307 | #define PRIO_MIN -4 |
331 | #define CORO_PRIO_MIN -4 |
308 | |
332 | |
309 | /* for Coro.pm */ |
333 | /* for Coro.pm */ |
310 | static SV *coro_current; |
334 | static SV *coro_current; |
311 | static SV *coro_readyhook; |
335 | static SV *coro_readyhook; |
312 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
336 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
313 | static CV *cv_coro_run, *cv_coro_terminate; |
337 | static CV *cv_coro_run, *cv_coro_terminate; |
314 | static struct coro *coro_first; |
338 | static struct coro *coro_first; |
315 | #define coro_nready coroapi.nready |
339 | #define coro_nready coroapi.nready |
316 | |
340 | |
317 | /** lowlevel stuff **********************************************************/ |
341 | /** lowlevel stuff **********************************************************/ |
… | |
… | |
350 | INLINE CV * |
374 | INLINE CV * |
351 | coro_sv_2cv (pTHX_ SV *sv) |
375 | coro_sv_2cv (pTHX_ SV *sv) |
352 | { |
376 | { |
353 | HV *st; |
377 | HV *st; |
354 | GV *gvp; |
378 | GV *gvp; |
355 | return sv_2cv (sv, &st, &gvp, 0); |
379 | CV *cv = sv_2cv (sv, &st, &gvp, 0); |
|
|
380 | |
|
|
381 | if (!cv) |
|
|
382 | croak ("code reference expected"); |
|
|
383 | |
|
|
384 | return cv; |
356 | } |
385 | } |
|
|
386 | |
|
|
387 | /*****************************************************************************/ |
|
|
388 | /* magic glue */ |
|
|
389 | |
|
|
390 | #define CORO_MAGIC_type_cv 26 |
|
|
391 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
392 | |
|
|
393 | #define CORO_MAGIC_NN(sv, type) \ |
|
|
394 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
|
|
395 | ? SvMAGIC (sv) \ |
|
|
396 | : mg_find (sv, type)) |
|
|
397 | |
|
|
398 | #define CORO_MAGIC(sv, type) \ |
|
|
399 | (expect_true (SvMAGIC (sv)) \ |
|
|
400 | ? CORO_MAGIC_NN (sv, type) \ |
|
|
401 | : 0) |
|
|
402 | |
|
|
403 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
404 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
|
|
405 | |
|
|
406 | INLINE struct coro * |
|
|
407 | SvSTATE_ (pTHX_ SV *coro) |
|
|
408 | { |
|
|
409 | HV *stash; |
|
|
410 | MAGIC *mg; |
|
|
411 | |
|
|
412 | if (SvROK (coro)) |
|
|
413 | coro = SvRV (coro); |
|
|
414 | |
|
|
415 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
416 | croak ("Coro::State object required"); |
|
|
417 | |
|
|
418 | stash = SvSTASH (coro); |
|
|
419 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
420 | { |
|
|
421 | /* very slow, but rare, check */ |
|
|
422 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
423 | croak ("Coro::State object required"); |
|
|
424 | } |
|
|
425 | |
|
|
426 | mg = CORO_MAGIC_state (coro); |
|
|
427 | return (struct coro *)mg->mg_ptr; |
|
|
428 | } |
|
|
429 | |
|
|
430 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
431 | |
|
|
432 | /* faster than SvSTATE, but expects a coroutine hv */ |
|
|
433 | #define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) |
|
|
434 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
|
|
435 | |
|
|
436 | /*****************************************************************************/ |
|
|
437 | /* padlist management and caching */ |
357 | |
438 | |
358 | static AV * |
439 | static AV * |
359 | coro_derive_padlist (pTHX_ CV *cv) |
440 | coro_derive_padlist (pTHX_ CV *cv) |
360 | { |
441 | { |
361 | AV *padlist = CvPADLIST (cv); |
442 | AV *padlist = CvPADLIST (cv); |
… | |
… | |
369 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
450 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
370 | #endif |
451 | #endif |
371 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
452 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
372 | --AvFILLp (padlist); |
453 | --AvFILLp (padlist); |
373 | |
454 | |
374 | av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); |
455 | av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); |
375 | av_store (newpadlist, 1, (SV *)newpad); |
456 | av_store (newpadlist, 1, (SV *)newpad); |
376 | |
457 | |
377 | return newpadlist; |
458 | return newpadlist; |
378 | } |
459 | } |
379 | |
460 | |
380 | static void |
461 | static void |
381 | free_padlist (pTHX_ AV *padlist) |
462 | free_padlist (pTHX_ AV *padlist) |
382 | { |
463 | { |
383 | /* may be during global destruction */ |
464 | /* may be during global destruction */ |
384 | if (SvREFCNT (padlist)) |
465 | if (!IN_DESTRUCT) |
385 | { |
466 | { |
386 | I32 i = AvFILLp (padlist); |
467 | I32 i = AvFILLp (padlist); |
387 | while (i >= 0) |
468 | |
|
|
469 | while (i > 0) /* special-case index 0 */ |
388 | { |
470 | { |
389 | SV **svp = av_fetch (padlist, i--, FALSE); |
471 | /* we try to be extra-careful here */ |
390 | if (svp) |
472 | AV *av = (AV *)AvARRAY (padlist)[i--]; |
391 | { |
473 | I32 j = AvFILLp (av); |
392 | SV *sv; |
474 | |
393 | while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) |
475 | while (j >= 0) |
|
|
476 | SvREFCNT_dec (AvARRAY (av)[j--]); |
|
|
477 | |
|
|
478 | AvFILLp (av) = -1; |
394 | SvREFCNT_dec (sv); |
479 | SvREFCNT_dec (av); |
395 | |
|
|
396 | SvREFCNT_dec (*svp); |
|
|
397 | } |
|
|
398 | } |
480 | } |
399 | |
481 | |
|
|
482 | SvREFCNT_dec (AvARRAY (padlist)[0]); |
|
|
483 | |
|
|
484 | AvFILLp (padlist) = -1; |
400 | SvREFCNT_dec ((SV*)padlist); |
485 | SvREFCNT_dec ((SV*)padlist); |
401 | } |
486 | } |
402 | } |
487 | } |
403 | |
488 | |
404 | static int |
489 | static int |
405 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
490 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
406 | { |
491 | { |
407 | AV *padlist; |
492 | AV *padlist; |
408 | AV *av = (AV *)mg->mg_obj; |
493 | AV *av = (AV *)mg->mg_obj; |
|
|
494 | |
|
|
495 | /* perl manages to free our internal AV and _then_ call us */ |
|
|
496 | if (IN_DESTRUCT) |
|
|
497 | return; |
409 | |
498 | |
410 | /* casting is fun. */ |
499 | /* casting is fun. */ |
411 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
500 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
412 | free_padlist (aTHX_ padlist); |
501 | free_padlist (aTHX_ padlist); |
413 | |
502 | |
414 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
503 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
415 | |
504 | |
416 | return 0; |
505 | return 0; |
417 | } |
506 | } |
418 | |
|
|
419 | #define CORO_MAGIC_type_cv 26 |
|
|
420 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
421 | |
507 | |
422 | static MGVTBL coro_cv_vtbl = { |
508 | static MGVTBL coro_cv_vtbl = { |
423 | 0, 0, 0, 0, |
509 | 0, 0, 0, 0, |
424 | coro_cv_free |
510 | coro_cv_free |
425 | }; |
511 | }; |
426 | |
|
|
427 | #define CORO_MAGIC_NN(sv, type) \ |
|
|
428 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
|
|
429 | ? SvMAGIC (sv) \ |
|
|
430 | : mg_find (sv, type)) |
|
|
431 | |
|
|
432 | #define CORO_MAGIC(sv, type) \ |
|
|
433 | (expect_true (SvMAGIC (sv)) \ |
|
|
434 | ? CORO_MAGIC_NN (sv, type) \ |
|
|
435 | : 0) |
|
|
436 | |
|
|
437 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
438 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
|
|
439 | |
|
|
440 | INLINE struct coro * |
|
|
441 | SvSTATE_ (pTHX_ SV *coro) |
|
|
442 | { |
|
|
443 | HV *stash; |
|
|
444 | MAGIC *mg; |
|
|
445 | |
|
|
446 | if (SvROK (coro)) |
|
|
447 | coro = SvRV (coro); |
|
|
448 | |
|
|
449 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
450 | croak ("Coro::State object required"); |
|
|
451 | |
|
|
452 | stash = SvSTASH (coro); |
|
|
453 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
454 | { |
|
|
455 | /* very slow, but rare, check */ |
|
|
456 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
457 | croak ("Coro::State object required"); |
|
|
458 | } |
|
|
459 | |
|
|
460 | mg = CORO_MAGIC_state (coro); |
|
|
461 | return (struct coro *)mg->mg_ptr; |
|
|
462 | } |
|
|
463 | |
|
|
464 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
465 | |
|
|
466 | /* faster than SvSTATE, but expects a coroutine hv */ |
|
|
467 | #define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) |
|
|
468 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
|
|
469 | |
512 | |
470 | /* the next two functions merely cache the padlists */ |
513 | /* the next two functions merely cache the padlists */ |
471 | static void |
514 | static void |
472 | get_padlist (pTHX_ CV *cv) |
515 | get_padlist (pTHX_ CV *cv) |
473 | { |
516 | { |
… | |
… | |
501 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
544 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
502 | |
545 | |
503 | av = (AV *)mg->mg_obj; |
546 | av = (AV *)mg->mg_obj; |
504 | |
547 | |
505 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
548 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
506 | av_extend (av, AvMAX (av) + 1); |
549 | av_extend (av, AvFILLp (av) + 1); |
507 | |
550 | |
508 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
551 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
509 | } |
552 | } |
510 | |
553 | |
511 | /** load & save, init *******************************************************/ |
554 | /** load & save, init *******************************************************/ |
|
|
555 | |
|
|
556 | static void |
|
|
557 | on_enterleave_call (pTHX_ SV *cb); |
512 | |
558 | |
513 | static void |
559 | static void |
514 | load_perl (pTHX_ Coro__State c) |
560 | load_perl (pTHX_ Coro__State c) |
515 | { |
561 | { |
516 | perl_slots *slot = c->slot; |
562 | perl_slots *slot = c->slot; |
517 | c->slot = 0; |
563 | c->slot = 0; |
518 | |
564 | |
519 | PL_mainstack = c->mainstack; |
565 | PL_mainstack = c->mainstack; |
520 | |
566 | |
521 | GvSV (PL_defgv) = slot->defsv; |
567 | GvSV (PL_defgv) = slot->defsv; |
522 | GvAV (PL_defgv) = slot->defav; |
568 | GvAV (PL_defgv) = slot->defav; |
523 | GvSV (PL_errgv) = slot->errsv; |
569 | GvSV (PL_errgv) = slot->errsv; |
524 | GvSV (irsgv) = slot->irsgv; |
570 | GvSV (irsgv) = slot->irsgv; |
|
|
571 | GvHV (PL_hintgv) = slot->hinthv; |
525 | |
572 | |
526 | #define VAR(name,type) PL_ ## name = slot->name; |
573 | #define VAR(name,type) PL_ ## name = slot->name; |
527 | # include "state.h" |
574 | # include "state.h" |
528 | #undef VAR |
575 | #undef VAR |
529 | |
576 | |
… | |
… | |
543 | PUTBACK; |
590 | PUTBACK; |
544 | } |
591 | } |
545 | |
592 | |
546 | slf_frame = c->slf_frame; |
593 | slf_frame = c->slf_frame; |
547 | CORO_THROW = c->except; |
594 | CORO_THROW = c->except; |
|
|
595 | |
|
|
596 | if (expect_false (c->on_enter)) |
|
|
597 | { |
|
|
598 | int i; |
|
|
599 | |
|
|
600 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
|
|
601 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
|
|
602 | } |
548 | } |
603 | } |
549 | |
604 | |
550 | static void |
605 | static void |
551 | save_perl (pTHX_ Coro__State c) |
606 | save_perl (pTHX_ Coro__State c) |
552 | { |
607 | { |
|
|
608 | if (expect_false (c->on_leave)) |
|
|
609 | { |
|
|
610 | int i; |
|
|
611 | |
|
|
612 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
|
|
613 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
|
|
614 | } |
|
|
615 | |
553 | c->except = CORO_THROW; |
616 | c->except = CORO_THROW; |
554 | c->slf_frame = slf_frame; |
617 | c->slf_frame = slf_frame; |
555 | |
618 | |
556 | { |
619 | { |
557 | dSP; |
620 | dSP; |
… | |
… | |
570 | { |
633 | { |
571 | while (expect_true (cxix >= 0)) |
634 | while (expect_true (cxix >= 0)) |
572 | { |
635 | { |
573 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
636 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
574 | |
637 | |
575 | if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)) |
638 | if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT)) |
576 | { |
639 | { |
577 | CV *cv = cx->blk_sub.cv; |
640 | CV *cv = cx->blk_sub.cv; |
578 | |
641 | |
579 | if (expect_true (CvDEPTH (cv))) |
642 | if (expect_true (CvDEPTH (cv))) |
580 | { |
643 | { |
… | |
… | |
615 | c->mainstack = PL_mainstack; |
678 | c->mainstack = PL_mainstack; |
616 | |
679 | |
617 | { |
680 | { |
618 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
681 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
619 | |
682 | |
620 | slot->defav = GvAV (PL_defgv); |
683 | slot->defav = GvAV (PL_defgv); |
621 | slot->defsv = DEFSV; |
684 | slot->defsv = DEFSV; |
622 | slot->errsv = ERRSV; |
685 | slot->errsv = ERRSV; |
623 | slot->irsgv = GvSV (irsgv); |
686 | slot->irsgv = GvSV (irsgv); |
|
|
687 | slot->hinthv = GvHV (PL_hintgv); |
624 | |
688 | |
625 | #define VAR(name,type) slot->name = PL_ ## name; |
689 | #define VAR(name,type) slot->name = PL_ ## name; |
626 | # include "state.h" |
690 | # include "state.h" |
627 | #undef VAR |
691 | #undef VAR |
628 | } |
692 | } |
… | |
… | |
755 | #endif |
819 | #endif |
756 | |
820 | |
757 | /* |
821 | /* |
758 | * This overrides the default magic get method of %SIG elements. |
822 | * This overrides the default magic get method of %SIG elements. |
759 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
823 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
760 | * and instead of tryign to save and restore the hash elements, we just provide |
824 | * and instead of trying to save and restore the hash elements, we just provide |
761 | * readback here. |
825 | * readback here. |
762 | * We only do this when the hook is != 0, as they are often set to 0 temporarily, |
|
|
763 | * not expecting this to actually change the hook. This is a potential problem |
|
|
764 | * when a schedule happens then, but we ignore this. |
|
|
765 | */ |
826 | */ |
766 | static int |
827 | static int |
767 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
828 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
768 | { |
829 | { |
769 | const char *s = MgPV_nolen_const (mg); |
830 | const char *s = MgPV_nolen_const (mg); |
… | |
… | |
822 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
883 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
823 | |
884 | |
824 | if (svp) |
885 | if (svp) |
825 | { |
886 | { |
826 | SV *old = *svp; |
887 | SV *old = *svp; |
827 | *svp = newSVsv (sv); |
888 | *svp = SvOK (sv) ? newSVsv (sv) : 0; |
828 | SvREFCNT_dec (old); |
889 | SvREFCNT_dec (old); |
829 | return 0; |
890 | return 0; |
830 | } |
891 | } |
831 | } |
892 | } |
832 | |
893 | |
… | |
… | |
864 | |
925 | |
865 | PL_runops = RUNOPS_DEFAULT; |
926 | PL_runops = RUNOPS_DEFAULT; |
866 | PL_curcop = &PL_compiling; |
927 | PL_curcop = &PL_compiling; |
867 | PL_in_eval = EVAL_NULL; |
928 | PL_in_eval = EVAL_NULL; |
868 | PL_comppad = 0; |
929 | PL_comppad = 0; |
|
|
930 | PL_comppad_name = 0; |
|
|
931 | PL_comppad_name_fill = 0; |
|
|
932 | PL_comppad_name_floor = 0; |
869 | PL_curpm = 0; |
933 | PL_curpm = 0; |
870 | PL_curpad = 0; |
934 | PL_curpad = 0; |
871 | PL_localizing = 0; |
935 | PL_localizing = 0; |
872 | PL_dirty = 0; |
936 | PL_dirty = 0; |
873 | PL_restartop = 0; |
937 | PL_restartop = 0; |
874 | #if PERL_VERSION_ATLEAST (5,10,0) |
938 | #if PERL_VERSION_ATLEAST (5,10,0) |
875 | PL_parser = 0; |
939 | PL_parser = 0; |
876 | #endif |
940 | #endif |
|
|
941 | PL_hints = 0; |
877 | |
942 | |
878 | /* recreate the die/warn hooks */ |
943 | /* recreate the die/warn hooks */ |
879 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
944 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
880 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
945 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
881 | |
946 | |
882 | GvSV (PL_defgv) = newSV (0); |
947 | GvSV (PL_defgv) = newSV (0); |
883 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
948 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
884 | GvSV (PL_errgv) = newSV (0); |
949 | GvSV (PL_errgv) = newSV (0); |
885 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
950 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
|
|
951 | GvHV (PL_hintgv) = 0; |
886 | PL_rs = newSVsv (GvSV (irsgv)); |
952 | PL_rs = newSVsv (GvSV (irsgv)); |
887 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
953 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
888 | |
954 | |
889 | { |
955 | { |
890 | dSP; |
956 | dSP; |
… | |
… | |
919 | /* copy throw, in case it was set before coro_setup */ |
985 | /* copy throw, in case it was set before coro_setup */ |
920 | CORO_THROW = coro->except; |
986 | CORO_THROW = coro->except; |
921 | } |
987 | } |
922 | |
988 | |
923 | static void |
989 | static void |
924 | coro_destruct (pTHX_ struct coro *coro) |
990 | coro_unwind_stacks (pTHX) |
925 | { |
991 | { |
926 | if (!IN_DESTRUCT) |
992 | if (!IN_DESTRUCT) |
927 | { |
993 | { |
928 | /* restore all saved variables and stuff */ |
994 | /* restore all saved variables and stuff */ |
929 | LEAVE_SCOPE (0); |
995 | LEAVE_SCOPE (0); |
… | |
… | |
937 | POPSTACK_TO (PL_mainstack); |
1003 | POPSTACK_TO (PL_mainstack); |
938 | |
1004 | |
939 | /* unwind main stack */ |
1005 | /* unwind main stack */ |
940 | dounwind (-1); |
1006 | dounwind (-1); |
941 | } |
1007 | } |
|
|
1008 | } |
942 | |
1009 | |
943 | SvREFCNT_dec (GvSV (PL_defgv)); |
1010 | static void |
944 | SvREFCNT_dec (GvAV (PL_defgv)); |
1011 | coro_destruct_perl (pTHX_ struct coro *coro) |
945 | SvREFCNT_dec (GvSV (PL_errgv)); |
1012 | { |
946 | SvREFCNT_dec (PL_defoutgv); |
1013 | SV *svf [9]; |
947 | SvREFCNT_dec (PL_rs); |
|
|
948 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
949 | |
1014 | |
950 | SvREFCNT_dec (PL_diehook); |
|
|
951 | SvREFCNT_dec (PL_warnhook); |
|
|
952 | |
1015 | { |
|
|
1016 | struct coro *current = SvSTATE_current; |
|
|
1017 | |
|
|
1018 | assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); |
|
|
1019 | |
|
|
1020 | save_perl (aTHX_ current); |
|
|
1021 | load_perl (aTHX_ coro); |
|
|
1022 | |
|
|
1023 | coro_unwind_stacks (aTHX); |
|
|
1024 | coro_destruct_stacks (aTHX); |
|
|
1025 | |
|
|
1026 | // now save some sv's to be free'd later |
|
|
1027 | svf [0] = GvSV (PL_defgv); |
|
|
1028 | svf [1] = (SV *)GvAV (PL_defgv); |
|
|
1029 | svf [2] = GvSV (PL_errgv); |
|
|
1030 | svf [3] = (SV *)PL_defoutgv; |
|
|
1031 | svf [4] = PL_rs; |
|
|
1032 | svf [5] = GvSV (irsgv); |
|
|
1033 | svf [6] = (SV *)GvHV (PL_hintgv); |
|
|
1034 | svf [7] = PL_diehook; |
|
|
1035 | svf [8] = PL_warnhook; |
|
|
1036 | assert (9 == sizeof (svf) / sizeof (*svf)); |
|
|
1037 | |
|
|
1038 | load_perl (aTHX_ current); |
|
|
1039 | } |
|
|
1040 | |
|
|
1041 | { |
|
|
1042 | int i; |
|
|
1043 | |
|
|
1044 | for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i) |
|
|
1045 | SvREFCNT_dec (svf [i]); |
|
|
1046 | |
953 | SvREFCNT_dec (coro->saved_deffh); |
1047 | SvREFCNT_dec (coro->saved_deffh); |
954 | SvREFCNT_dec (coro->rouse_cb); |
1048 | SvREFCNT_dec (coro->rouse_cb); |
955 | SvREFCNT_dec (coro->invoke_cb); |
1049 | SvREFCNT_dec (coro->invoke_cb); |
956 | SvREFCNT_dec (coro->invoke_av); |
1050 | SvREFCNT_dec (coro->invoke_av); |
957 | |
1051 | } |
958 | coro_destruct_stacks (aTHX); |
|
|
959 | } |
1052 | } |
960 | |
1053 | |
961 | INLINE void |
1054 | INLINE void |
962 | free_coro_mortal (pTHX) |
1055 | free_coro_mortal (pTHX) |
963 | { |
1056 | { |
… | |
… | |
971 | static int |
1064 | static int |
972 | runops_trace (pTHX) |
1065 | runops_trace (pTHX) |
973 | { |
1066 | { |
974 | COP *oldcop = 0; |
1067 | COP *oldcop = 0; |
975 | int oldcxix = -2; |
1068 | int oldcxix = -2; |
976 | struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */ |
|
|
977 | coro_cctx *cctx = coro->cctx; |
|
|
978 | |
1069 | |
979 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
1070 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
980 | { |
1071 | { |
981 | PERL_ASYNC_CHECK (); |
1072 | PERL_ASYNC_CHECK (); |
982 | |
1073 | |
983 | if (cctx->flags & CC_TRACE_ALL) |
1074 | if (cctx_current->flags & CC_TRACE_ALL) |
984 | { |
1075 | { |
985 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
1076 | if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB) |
986 | { |
1077 | { |
987 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1078 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
988 | SV **bot, **top; |
1079 | SV **bot, **top; |
989 | AV *av = newAV (); /* return values */ |
1080 | AV *av = newAV (); /* return values */ |
990 | SV **cb; |
1081 | SV **cb; |
… | |
… | |
1027 | |
1118 | |
1028 | if (PL_curcop != &PL_compiling) |
1119 | if (PL_curcop != &PL_compiling) |
1029 | { |
1120 | { |
1030 | SV **cb; |
1121 | SV **cb; |
1031 | |
1122 | |
1032 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
1123 | if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB) |
1033 | { |
1124 | { |
1034 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1125 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1035 | |
1126 | |
1036 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1127 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1037 | { |
1128 | { |
… | |
… | |
1060 | } |
1151 | } |
1061 | |
1152 | |
1062 | oldcxix = cxstack_ix; |
1153 | oldcxix = cxstack_ix; |
1063 | } |
1154 | } |
1064 | |
1155 | |
1065 | if (cctx->flags & CC_TRACE_LINE) |
1156 | if (cctx_current->flags & CC_TRACE_LINE) |
1066 | { |
1157 | { |
1067 | dSP; |
1158 | dSP; |
1068 | |
1159 | |
1069 | PL_runops = RUNOPS_DEFAULT; |
1160 | PL_runops = RUNOPS_DEFAULT; |
1070 | ENTER; |
1161 | ENTER; |
… | |
… | |
1170 | /* |
1261 | /* |
1171 | * If perl-run returns we assume exit() was being called or the coro |
1262 | * If perl-run returns we assume exit() was being called or the coro |
1172 | * fell off the end, which seems to be the only valid (non-bug) |
1263 | * fell off the end, which seems to be the only valid (non-bug) |
1173 | * reason for perl_run to return. We try to exit by jumping to the |
1264 | * reason for perl_run to return. We try to exit by jumping to the |
1174 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1265 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1175 | * coroutine as Coro has no such concept |
1266 | * coroutine as Coro has no such concept. |
|
|
1267 | * This actually isn't valid with the pthread backend, but OSes requiring |
|
|
1268 | * that backend are too broken to do it in a standards-compliant way. |
1176 | */ |
1269 | */ |
1177 | PL_top_env = main_top_env; |
1270 | PL_top_env = main_top_env; |
1178 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1271 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1179 | } |
1272 | } |
1180 | } |
1273 | } |
… | |
… | |
1257 | cctx_destroy (coro_cctx *cctx) |
1350 | cctx_destroy (coro_cctx *cctx) |
1258 | { |
1351 | { |
1259 | if (!cctx) |
1352 | if (!cctx) |
1260 | return; |
1353 | return; |
1261 | |
1354 | |
1262 | assert (cctx != cctx_current);//D temporary |
1355 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary? |
1263 | |
1356 | |
1264 | --cctx_count; |
1357 | --cctx_count; |
1265 | coro_destroy (&cctx->cctx); |
1358 | coro_destroy (&cctx->cctx); |
1266 | |
1359 | |
1267 | /* coro_transfer creates new, empty cctx's */ |
1360 | /* coro_transfer creates new, empty cctx's */ |
… | |
… | |
1331 | /* TODO: throwing up here is considered harmful */ |
1424 | /* TODO: throwing up here is considered harmful */ |
1332 | |
1425 | |
1333 | if (expect_true (prev != next)) |
1426 | if (expect_true (prev != next)) |
1334 | { |
1427 | { |
1335 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1428 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1336 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); |
1429 | croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); |
1337 | |
1430 | |
1338 | if (expect_false (next->flags & CF_RUNNING)) |
|
|
1339 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
|
|
1340 | |
|
|
1341 | if (expect_false (next->flags & CF_DESTROYED)) |
1431 | if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED))) |
1342 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); |
1432 | croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); |
1343 | |
1433 | |
1344 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1434 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1345 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1435 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1346 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1436 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1347 | #endif |
1437 | #endif |
… | |
… | |
1395 | )) |
1485 | )) |
1396 | { |
1486 | { |
1397 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1487 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1398 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1488 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1399 | |
1489 | |
1400 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1490 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ |
1401 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1491 | /* without this the next cctx_get might destroy the running cctx while still in use */ |
1402 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1492 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1403 | if (!next->cctx) |
1493 | if (expect_true (!next->cctx)) |
1404 | next->cctx = cctx_get (aTHX); |
1494 | next->cctx = cctx_get (aTHX); |
1405 | |
1495 | |
1406 | cctx_put (cctx_current); |
1496 | cctx_put (cctx_current); |
1407 | assert (!prev->cctx);//D temporary |
|
|
1408 | } |
1497 | } |
1409 | else |
1498 | else |
1410 | prev->cctx = cctx_current; |
1499 | prev->cctx = cctx_current; |
1411 | |
1500 | |
1412 | ++next->usecount; |
1501 | ++next->usecount; |
… | |
… | |
1436 | coro_state_destroy (pTHX_ struct coro *coro) |
1525 | coro_state_destroy (pTHX_ struct coro *coro) |
1437 | { |
1526 | { |
1438 | if (coro->flags & CF_DESTROYED) |
1527 | if (coro->flags & CF_DESTROYED) |
1439 | return 0; |
1528 | return 0; |
1440 | |
1529 | |
1441 | if (coro->on_destroy) |
1530 | if (coro->on_destroy && !PL_dirty) |
1442 | coro->on_destroy (aTHX_ coro); |
1531 | coro->on_destroy (aTHX_ coro); |
1443 | |
1532 | |
1444 | coro->flags |= CF_DESTROYED; |
1533 | coro->flags |= CF_DESTROYED; |
1445 | |
1534 | |
1446 | if (coro->flags & CF_READY) |
1535 | if (coro->flags & CF_READY) |
… | |
… | |
1450 | --coro_nready; |
1539 | --coro_nready; |
1451 | } |
1540 | } |
1452 | else |
1541 | else |
1453 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1542 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1454 | |
1543 | |
1455 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1544 | if (coro->mainstack |
1456 | { |
1545 | && coro->mainstack != main_mainstack |
1457 | struct coro temp; |
1546 | && coro->slot |
1458 | |
1547 | && !PL_dirty) |
1459 | assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); |
|
|
1460 | |
|
|
1461 | save_perl (aTHX_ &temp); |
|
|
1462 | load_perl (aTHX_ coro); |
|
|
1463 | |
|
|
1464 | coro_destruct (aTHX_ coro); |
1548 | coro_destruct_perl (aTHX_ coro); |
1465 | |
|
|
1466 | load_perl (aTHX_ &temp); |
|
|
1467 | |
|
|
1468 | coro->slot = 0; |
|
|
1469 | } |
|
|
1470 | |
1549 | |
1471 | cctx_destroy (coro->cctx); |
1550 | cctx_destroy (coro->cctx); |
1472 | SvREFCNT_dec (coro->startcv); |
1551 | SvREFCNT_dec (coro->startcv); |
1473 | SvREFCNT_dec (coro->args); |
1552 | SvREFCNT_dec (coro->args); |
1474 | SvREFCNT_dec (CORO_THROW); |
1553 | SvREFCNT_dec (CORO_THROW); |
… | |
… | |
1561 | /** Coro ********************************************************************/ |
1640 | /** Coro ********************************************************************/ |
1562 | |
1641 | |
1563 | INLINE void |
1642 | INLINE void |
1564 | coro_enq (pTHX_ struct coro *coro) |
1643 | coro_enq (pTHX_ struct coro *coro) |
1565 | { |
1644 | { |
1566 | av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); |
1645 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1567 | } |
|
|
1568 | |
1646 | |
1569 | INLINE SV * |
1647 | SvREFCNT_inc_NN (coro->hv); |
|
|
1648 | |
|
|
1649 | coro->next_ready = 0; |
|
|
1650 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
|
|
1651 | ready [1] = coro; |
|
|
1652 | } |
|
|
1653 | |
|
|
1654 | INLINE struct coro * |
1570 | coro_deq (pTHX) |
1655 | coro_deq (pTHX) |
1571 | { |
1656 | { |
1572 | int prio; |
1657 | int prio; |
1573 | |
1658 | |
1574 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1659 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
1575 | if (AvFILLp (coro_ready [prio]) >= 0) |
1660 | { |
1576 | return av_shift (coro_ready [prio]); |
1661 | struct coro **ready = coro_ready [prio]; |
|
|
1662 | |
|
|
1663 | if (ready [0]) |
|
|
1664 | { |
|
|
1665 | struct coro *coro = ready [0]; |
|
|
1666 | ready [0] = coro->next_ready; |
|
|
1667 | return coro; |
|
|
1668 | } |
|
|
1669 | } |
1577 | |
1670 | |
1578 | return 0; |
1671 | return 0; |
1579 | } |
1672 | } |
1580 | |
1673 | |
1581 | static int |
1674 | static int |
… | |
… | |
1583 | { |
1676 | { |
1584 | struct coro *coro; |
1677 | struct coro *coro; |
1585 | SV *sv_hook; |
1678 | SV *sv_hook; |
1586 | void (*xs_hook)(void); |
1679 | void (*xs_hook)(void); |
1587 | |
1680 | |
1588 | if (SvROK (coro_sv)) |
|
|
1589 | coro_sv = SvRV (coro_sv); |
|
|
1590 | |
|
|
1591 | coro = SvSTATE (coro_sv); |
1681 | coro = SvSTATE (coro_sv); |
1592 | |
1682 | |
1593 | if (coro->flags & CF_READY) |
1683 | if (coro->flags & CF_READY) |
1594 | return 0; |
1684 | return 0; |
1595 | |
1685 | |
… | |
… | |
1648 | static void |
1738 | static void |
1649 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1739 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1650 | { |
1740 | { |
1651 | for (;;) |
1741 | for (;;) |
1652 | { |
1742 | { |
1653 | SV *next_sv = coro_deq (aTHX); |
1743 | struct coro *next = coro_deq (aTHX); |
1654 | |
1744 | |
1655 | if (expect_true (next_sv)) |
1745 | if (expect_true (next)) |
1656 | { |
1746 | { |
1657 | struct coro *next = SvSTATE_hv (next_sv); |
|
|
1658 | |
|
|
1659 | /* cannot transfer to destroyed coros, skip and look for next */ |
1747 | /* cannot transfer to destroyed coros, skip and look for next */ |
1660 | if (expect_false (next->flags & CF_DESTROYED)) |
1748 | if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED))) |
1661 | SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ |
1749 | SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ |
1662 | else |
1750 | else |
1663 | { |
1751 | { |
1664 | next->flags &= ~CF_READY; |
1752 | next->flags &= ~CF_READY; |
1665 | --coro_nready; |
1753 | --coro_nready; |
1666 | |
1754 | |
… | |
… | |
1669 | } |
1757 | } |
1670 | } |
1758 | } |
1671 | else |
1759 | else |
1672 | { |
1760 | { |
1673 | /* nothing to schedule: call the idle handler */ |
1761 | /* nothing to schedule: call the idle handler */ |
|
|
1762 | if (SvROK (sv_idle) |
|
|
1763 | && SvOBJECT (SvRV (sv_idle))) |
|
|
1764 | { |
|
|
1765 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
|
|
1766 | api_ready (aTHX_ SvRV (sv_idle)); |
|
|
1767 | --coro_nready; |
|
|
1768 | } |
|
|
1769 | else |
|
|
1770 | { |
1674 | dSP; |
1771 | dSP; |
1675 | |
1772 | |
1676 | ENTER; |
1773 | ENTER; |
1677 | SAVETMPS; |
1774 | SAVETMPS; |
1678 | |
1775 | |
1679 | PUSHMARK (SP); |
1776 | PUSHMARK (SP); |
1680 | PUTBACK; |
1777 | PUTBACK; |
1681 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1778 | call_sv (sv_idle, G_VOID | G_DISCARD); |
1682 | |
1779 | |
1683 | FREETMPS; |
1780 | FREETMPS; |
1684 | LEAVE; |
1781 | LEAVE; |
|
|
1782 | } |
1685 | } |
1783 | } |
1686 | } |
1784 | } |
1687 | } |
1785 | } |
1688 | |
1786 | |
1689 | INLINE void |
1787 | INLINE void |
… | |
… | |
1760 | static void |
1858 | static void |
1761 | api_trace (pTHX_ SV *coro_sv, int flags) |
1859 | api_trace (pTHX_ SV *coro_sv, int flags) |
1762 | { |
1860 | { |
1763 | struct coro *coro = SvSTATE (coro_sv); |
1861 | struct coro *coro = SvSTATE (coro_sv); |
1764 | |
1862 | |
|
|
1863 | if (coro->flags & CF_RUNNING) |
|
|
1864 | croak ("cannot enable tracing on a running coroutine, caught"); |
|
|
1865 | |
1765 | if (flags & CC_TRACE) |
1866 | if (flags & CC_TRACE) |
1766 | { |
1867 | { |
1767 | if (!coro->cctx) |
1868 | if (!coro->cctx) |
1768 | coro->cctx = cctx_new_run (); |
1869 | coro->cctx = cctx_new_run (); |
1769 | else if (!(coro->cctx->flags & CC_TRACE)) |
1870 | else if (!(coro->cctx->flags & CC_TRACE)) |
1770 | croak ("cannot enable tracing on coroutine with custom stack,"); |
1871 | croak ("cannot enable tracing on coroutine with custom stack, caught"); |
1771 | |
1872 | |
1772 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1873 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1773 | } |
1874 | } |
1774 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1875 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1775 | { |
1876 | { |
… | |
… | |
1831 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1932 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1832 | api_ready (aTHX_ sv_manager); |
1933 | api_ready (aTHX_ sv_manager); |
1833 | |
1934 | |
1834 | frame->prepare = prepare_schedule; |
1935 | frame->prepare = prepare_schedule; |
1835 | frame->check = slf_check_repeat; |
1936 | frame->check = slf_check_repeat; |
|
|
1937 | |
|
|
1938 | /* as a minor optimisation, we could unwind all stacks here */ |
|
|
1939 | /* but that puts extra pressure on pp_slf, and is not worth much */ |
|
|
1940 | /*coro_unwind_stacks (aTHX);*/ |
1836 | } |
1941 | } |
1837 | |
1942 | |
1838 | /*****************************************************************************/ |
1943 | /*****************************************************************************/ |
1839 | /* async pool handler */ |
1944 | /* async pool handler */ |
1840 | |
1945 | |
… | |
… | |
1923 | SV *data = (SV *)GENSUB_ARG; |
2028 | SV *data = (SV *)GENSUB_ARG; |
1924 | |
2029 | |
1925 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2030 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
1926 | { |
2031 | { |
1927 | /* first call, set args */ |
2032 | /* first call, set args */ |
|
|
2033 | SV *coro = SvRV (data); |
1928 | AV *av = newAV (); |
2034 | AV *av = newAV (); |
1929 | SV *coro = SvRV (data); |
|
|
1930 | |
2035 | |
1931 | SvRV_set (data, (SV *)av); |
2036 | SvRV_set (data, (SV *)av); |
1932 | api_ready (aTHX_ coro); |
|
|
1933 | SvREFCNT_dec (coro); |
|
|
1934 | |
2037 | |
1935 | /* better take a full copy of the arguments */ |
2038 | /* better take a full copy of the arguments */ |
1936 | while (items--) |
2039 | while (items--) |
1937 | av_store (av, items, newSVsv (ST (items))); |
2040 | av_store (av, items, newSVsv (ST (items))); |
|
|
2041 | |
|
|
2042 | api_ready (aTHX_ coro); |
|
|
2043 | SvREFCNT_dec (coro); |
1938 | } |
2044 | } |
1939 | |
2045 | |
1940 | XSRETURN_EMPTY; |
2046 | XSRETURN_EMPTY; |
1941 | } |
2047 | } |
1942 | |
2048 | |
… | |
… | |
1959 | |
2065 | |
1960 | EXTEND (SP, AvFILLp (av) + 1); |
2066 | EXTEND (SP, AvFILLp (av) + 1); |
1961 | for (i = 0; i <= AvFILLp (av); ++i) |
2067 | for (i = 0; i <= AvFILLp (av); ++i) |
1962 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
2068 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
1963 | |
2069 | |
1964 | /* we have stolen the elements, so ste length to zero and free */ |
2070 | /* we have stolen the elements, so set length to zero and free */ |
1965 | AvFILLp (av) = -1; |
2071 | AvFILLp (av) = -1; |
1966 | av_undef (av); |
2072 | av_undef (av); |
1967 | |
2073 | |
1968 | PUTBACK; |
2074 | PUTBACK; |
1969 | } |
2075 | } |
… | |
… | |
2259 | |
2365 | |
2260 | PL_op->op_ppaddr = pp_slf; |
2366 | PL_op->op_ppaddr = pp_slf; |
2261 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2367 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2262 | |
2368 | |
2263 | PL_op = (OP *)&slf_restore; |
2369 | PL_op = (OP *)&slf_restore; |
|
|
2370 | } |
|
|
2371 | |
|
|
2372 | /*****************************************************************************/ |
|
|
2373 | /* dynamic wind */ |
|
|
2374 | |
|
|
2375 | static void |
|
|
2376 | on_enterleave_call (pTHX_ SV *cb) |
|
|
2377 | { |
|
|
2378 | dSP; |
|
|
2379 | |
|
|
2380 | PUSHSTACK; |
|
|
2381 | |
|
|
2382 | PUSHMARK (SP); |
|
|
2383 | PUTBACK; |
|
|
2384 | call_sv (cb, G_VOID | G_DISCARD); |
|
|
2385 | SPAGAIN; |
|
|
2386 | |
|
|
2387 | POPSTACK; |
|
|
2388 | } |
|
|
2389 | |
|
|
2390 | static SV * |
|
|
2391 | coro_avp_pop_and_free (pTHX_ AV **avp) |
|
|
2392 | { |
|
|
2393 | AV *av = *avp; |
|
|
2394 | SV *res = av_pop (av); |
|
|
2395 | |
|
|
2396 | if (AvFILLp (av) < 0) |
|
|
2397 | { |
|
|
2398 | *avp = 0; |
|
|
2399 | SvREFCNT_dec (av); |
|
|
2400 | } |
|
|
2401 | |
|
|
2402 | return res; |
|
|
2403 | } |
|
|
2404 | |
|
|
2405 | static void |
|
|
2406 | coro_pop_on_enter (pTHX_ void *coro) |
|
|
2407 | { |
|
|
2408 | SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter); |
|
|
2409 | SvREFCNT_dec (cb); |
|
|
2410 | } |
|
|
2411 | |
|
|
2412 | static void |
|
|
2413 | coro_pop_on_leave (pTHX_ void *coro) |
|
|
2414 | { |
|
|
2415 | SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave); |
|
|
2416 | on_enterleave_call (aTHX_ sv_2mortal (cb)); |
2264 | } |
2417 | } |
2265 | |
2418 | |
2266 | /*****************************************************************************/ |
2419 | /*****************************************************************************/ |
2267 | /* PerlIO::cede */ |
2420 | /* PerlIO::cede */ |
2268 | |
2421 | |
… | |
… | |
2342 | /* Coro::Semaphore & Coro::Signal */ |
2495 | /* Coro::Semaphore & Coro::Signal */ |
2343 | |
2496 | |
2344 | static SV * |
2497 | static SV * |
2345 | coro_waitarray_new (pTHX_ int count) |
2498 | coro_waitarray_new (pTHX_ int count) |
2346 | { |
2499 | { |
2347 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
2500 | /* a waitarray=semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
2348 | AV *av = newAV (); |
2501 | AV *av = newAV (); |
2349 | SV **ary; |
2502 | SV **ary; |
2350 | |
2503 | |
2351 | /* unfortunately, building manually saves memory */ |
2504 | /* unfortunately, building manually saves memory */ |
2352 | Newx (ary, 2, SV *); |
2505 | Newx (ary, 2, SV *); |
2353 | AvALLOC (av) = ary; |
2506 | AvALLOC (av) = ary; |
|
|
2507 | #if PERL_VERSION_ATLEAST (5,10,0) |
2354 | /*AvARRAY (av) = ary;*/ |
2508 | AvARRAY (av) = ary; |
|
|
2509 | #else |
2355 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
2510 | /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */ |
|
|
2511 | /* -DDEBUGGING flags this as a bug, despite it perfectly working */ |
|
|
2512 | SvPVX ((SV *)av) = (char *)ary; |
|
|
2513 | #endif |
2356 | AvMAX (av) = 1; |
2514 | AvMAX (av) = 1; |
2357 | AvFILLp (av) = 0; |
2515 | AvFILLp (av) = 0; |
2358 | ary [0] = newSViv (count); |
2516 | ary [0] = newSViv (count); |
2359 | |
2517 | |
2360 | return newRV_noinc ((SV *)av); |
2518 | return newRV_noinc ((SV *)av); |
… | |
… | |
2491 | { |
2649 | { |
2492 | /* callback form */ |
2650 | /* callback form */ |
2493 | AV *av = (AV *)SvRV (arg [0]); |
2651 | AV *av = (AV *)SvRV (arg [0]); |
2494 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
2652 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
2495 | |
2653 | |
2496 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
2654 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2497 | |
2655 | |
2498 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2656 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2499 | coro_semaphore_adjust (aTHX_ av, 0); |
2657 | coro_semaphore_adjust (aTHX_ av, 0); |
2500 | |
2658 | |
2501 | frame->prepare = prepare_nop; |
2659 | frame->prepare = prepare_nop; |
… | |
… | |
2525 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2683 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2526 | AvARRAY (av)[1] = cb; |
2684 | AvARRAY (av)[1] = cb; |
2527 | |
2685 | |
2528 | cb = av_shift (av); |
2686 | cb = av_shift (av); |
2529 | |
2687 | |
|
|
2688 | if (SvTYPE (cb) == SVt_PVCV) |
|
|
2689 | { |
|
|
2690 | dSP; |
|
|
2691 | PUSHMARK (SP); |
|
|
2692 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
2693 | PUTBACK; |
|
|
2694 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
|
|
2695 | } |
|
|
2696 | else |
|
|
2697 | { |
2530 | api_ready (aTHX_ cb); |
2698 | api_ready (aTHX_ cb); |
2531 | sv_setiv (cb, 0); /* signal waiter */ |
2699 | sv_setiv (cb, 0); /* signal waiter */ |
|
|
2700 | } |
|
|
2701 | |
2532 | SvREFCNT_dec (cb); |
2702 | SvREFCNT_dec (cb); |
2533 | |
2703 | |
2534 | --count; |
2704 | --count; |
2535 | } |
2705 | } |
2536 | } |
2706 | } |
… | |
… | |
2545 | static void |
2715 | static void |
2546 | slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2716 | slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2547 | { |
2717 | { |
2548 | AV *av = (AV *)SvRV (arg [0]); |
2718 | AV *av = (AV *)SvRV (arg [0]); |
2549 | |
2719 | |
|
|
2720 | if (items >= 2) |
|
|
2721 | { |
|
|
2722 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
|
|
2723 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
|
|
2724 | |
2550 | if (SvIVX (AvARRAY (av)[0])) |
2725 | if (SvIVX (AvARRAY (av)[0])) |
|
|
2726 | coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ |
|
|
2727 | |
|
|
2728 | frame->prepare = prepare_nop; |
|
|
2729 | frame->check = slf_check_nop; |
|
|
2730 | } |
|
|
2731 | else if (SvIVX (AvARRAY (av)[0])) |
2551 | { |
2732 | { |
2552 | SvIVX (AvARRAY (av)[0]) = 0; |
2733 | SvIVX (AvARRAY (av)[0]) = 0; |
2553 | frame->prepare = prepare_nop; |
2734 | frame->prepare = prepare_nop; |
2554 | frame->check = slf_check_nop; |
2735 | frame->check = slf_check_nop; |
2555 | } |
2736 | } |
2556 | else |
2737 | else |
2557 | { |
2738 | { |
2558 | SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ |
2739 | SV *waiter = newSVsv (coro_current); /* owned by signal av */ |
2559 | |
2740 | |
2560 | av_push (av, waiter); |
2741 | av_push (av, waiter); |
2561 | |
2742 | |
2562 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
2743 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
2563 | frame->prepare = prepare_schedule; |
2744 | frame->prepare = prepare_schedule; |
… | |
… | |
2804 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
2985 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
2805 | |
2986 | |
2806 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
2987 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
2807 | } |
2988 | } |
2808 | |
2989 | |
2809 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2990 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
2810 | } |
2991 | } |
2811 | |
2992 | |
2812 | SV * |
2993 | SV * |
2813 | new (char *klass, ...) |
2994 | new (char *klass, ...) |
2814 | ALIAS: |
2995 | ALIAS: |
… | |
… | |
2889 | SV * |
3070 | SV * |
2890 | clone (Coro::State coro) |
3071 | clone (Coro::State coro) |
2891 | CODE: |
3072 | CODE: |
2892 | { |
3073 | { |
2893 | #if CORO_CLONE |
3074 | #if CORO_CLONE |
2894 | struct coro *ncoro = coro_clone (coro); |
3075 | struct coro *ncoro = coro_clone (aTHX_ coro); |
2895 | MAGIC *mg; |
3076 | MAGIC *mg; |
2896 | /* TODO: too much duplication */ |
3077 | /* TODO: too much duplication */ |
2897 | ncoro->hv = newHV (); |
3078 | ncoro->hv = newHV (); |
2898 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
3079 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
2899 | mg->mg_flags |= MGf_DUP; |
3080 | mg->mg_flags |= MGf_DUP; |
… | |
… | |
2961 | eval = 1 |
3142 | eval = 1 |
2962 | CODE: |
3143 | CODE: |
2963 | { |
3144 | { |
2964 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
3145 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
2965 | { |
3146 | { |
2966 | struct coro temp; |
3147 | struct coro *current = SvSTATE_current; |
2967 | |
3148 | |
2968 | if (!(coro->flags & CF_RUNNING)) |
3149 | if (current != coro) |
2969 | { |
3150 | { |
2970 | PUTBACK; |
3151 | PUTBACK; |
2971 | save_perl (aTHX_ &temp); |
3152 | save_perl (aTHX_ current); |
2972 | load_perl (aTHX_ coro); |
3153 | load_perl (aTHX_ coro); |
|
|
3154 | SPAGAIN; |
2973 | } |
3155 | } |
2974 | |
3156 | |
2975 | { |
|
|
2976 | dSP; |
|
|
2977 | ENTER; |
|
|
2978 | SAVETMPS; |
|
|
2979 | PUTBACK; |
|
|
2980 | PUSHSTACK; |
3157 | PUSHSTACK; |
|
|
3158 | |
2981 | PUSHMARK (SP); |
3159 | PUSHMARK (SP); |
|
|
3160 | PUTBACK; |
2982 | |
3161 | |
2983 | if (ix) |
3162 | if (ix) |
2984 | eval_sv (coderef, 0); |
3163 | eval_sv (coderef, 0); |
2985 | else |
3164 | else |
2986 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
3165 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
2987 | |
3166 | |
2988 | POPSTACK; |
3167 | POPSTACK; |
2989 | SPAGAIN; |
3168 | SPAGAIN; |
2990 | FREETMPS; |
|
|
2991 | LEAVE; |
|
|
2992 | PUTBACK; |
|
|
2993 | } |
|
|
2994 | |
3169 | |
2995 | if (!(coro->flags & CF_RUNNING)) |
3170 | if (current != coro) |
2996 | { |
3171 | { |
|
|
3172 | PUTBACK; |
2997 | save_perl (aTHX_ coro); |
3173 | save_perl (aTHX_ coro); |
2998 | load_perl (aTHX_ &temp); |
3174 | load_perl (aTHX_ current); |
2999 | SPAGAIN; |
3175 | SPAGAIN; |
3000 | } |
3176 | } |
3001 | } |
3177 | } |
3002 | } |
3178 | } |
3003 | |
3179 | |
… | |
… | |
3007 | ALIAS: |
3183 | ALIAS: |
3008 | is_ready = CF_READY |
3184 | is_ready = CF_READY |
3009 | is_running = CF_RUNNING |
3185 | is_running = CF_RUNNING |
3010 | is_new = CF_NEW |
3186 | is_new = CF_NEW |
3011 | is_destroyed = CF_DESTROYED |
3187 | is_destroyed = CF_DESTROYED |
|
|
3188 | is_suspended = CF_SUSPENDED |
3012 | CODE: |
3189 | CODE: |
3013 | RETVAL = boolSV (coro->flags & ix); |
3190 | RETVAL = boolSV (coro->flags & ix); |
3014 | OUTPUT: |
3191 | OUTPUT: |
3015 | RETVAL |
3192 | RETVAL |
3016 | |
3193 | |
… | |
… | |
3020 | CODE: |
3197 | CODE: |
3021 | { |
3198 | { |
3022 | struct coro *current = SvSTATE_current; |
3199 | struct coro *current = SvSTATE_current; |
3023 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3200 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3024 | SvREFCNT_dec (*throwp); |
3201 | SvREFCNT_dec (*throwp); |
|
|
3202 | SvGETMAGIC (throw); |
3025 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3203 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3026 | } |
3204 | } |
3027 | |
3205 | |
3028 | void |
3206 | void |
3029 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3207 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
… | |
… | |
3032 | |
3210 | |
3033 | SV * |
3211 | SV * |
3034 | has_cctx (Coro::State coro) |
3212 | has_cctx (Coro::State coro) |
3035 | PROTOTYPE: $ |
3213 | PROTOTYPE: $ |
3036 | CODE: |
3214 | CODE: |
3037 | RETVAL = boolSV (!!coro->cctx); |
3215 | /* maybe manage the running flag differently */ |
|
|
3216 | RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING)); |
3038 | OUTPUT: |
3217 | OUTPUT: |
3039 | RETVAL |
3218 | RETVAL |
3040 | |
3219 | |
3041 | int |
3220 | int |
3042 | is_traced (Coro::State coro) |
3221 | is_traced (Coro::State coro) |
… | |
… | |
3062 | |
3241 | |
3063 | void |
3242 | void |
3064 | force_cctx () |
3243 | force_cctx () |
3065 | PROTOTYPE: |
3244 | PROTOTYPE: |
3066 | CODE: |
3245 | CODE: |
3067 | SvSTATE_current->cctx->idle_sp = 0; |
3246 | cctx_current->idle_sp = 0; |
3068 | |
3247 | |
3069 | void |
3248 | void |
3070 | swap_defsv (Coro::State self) |
3249 | swap_defsv (Coro::State self) |
3071 | PROTOTYPE: $ |
3250 | PROTOTYPE: $ |
3072 | ALIAS: |
3251 | ALIAS: |
… | |
… | |
3080 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
3259 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
3081 | |
3260 | |
3082 | SV *tmp = *src; *src = *dst; *dst = tmp; |
3261 | SV *tmp = *src; *src = *dst; *dst = tmp; |
3083 | } |
3262 | } |
3084 | |
3263 | |
|
|
3264 | void |
|
|
3265 | cancel (Coro::State self) |
|
|
3266 | CODE: |
|
|
3267 | coro_state_destroy (aTHX_ self); |
|
|
3268 | coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ |
|
|
3269 | |
3085 | |
3270 | |
3086 | MODULE = Coro::State PACKAGE = Coro |
3271 | MODULE = Coro::State PACKAGE = Coro |
3087 | |
3272 | |
3088 | BOOT: |
3273 | BOOT: |
3089 | { |
3274 | { |
3090 | int i; |
|
|
3091 | |
|
|
3092 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3275 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3093 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3276 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3094 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
3277 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3095 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3278 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3096 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3279 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3097 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3280 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3098 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3281 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3099 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
3282 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
|
|
3283 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3100 | |
3284 | |
3101 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3285 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3102 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3286 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3103 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3287 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3104 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3288 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3105 | |
3289 | |
3106 | coro_stash = gv_stashpv ("Coro", TRUE); |
3290 | coro_stash = gv_stashpv ("Coro", TRUE); |
3107 | |
3291 | |
3108 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
3292 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3109 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
3293 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3110 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
3294 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
3111 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
3295 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (CORO_PRIO_LOW)); |
3112 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
3296 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (CORO_PRIO_IDLE)); |
3113 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
3297 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (CORO_PRIO_MIN)); |
3114 | |
|
|
3115 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
|
|
3116 | coro_ready[i] = newAV (); |
|
|
3117 | |
3298 | |
3118 | { |
3299 | { |
3119 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3300 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3120 | |
3301 | |
3121 | coroapi.schedule = api_schedule; |
3302 | coroapi.schedule = api_schedule; |
… | |
… | |
3162 | cede_notself (...) |
3343 | cede_notself (...) |
3163 | CODE: |
3344 | CODE: |
3164 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3345 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3165 | |
3346 | |
3166 | void |
3347 | void |
3167 | _cancel (Coro::State self) |
|
|
3168 | CODE: |
|
|
3169 | coro_state_destroy (aTHX_ self); |
|
|
3170 | coro_call_on_destroy (aTHX_ self); |
|
|
3171 | |
|
|
3172 | void |
|
|
3173 | _set_current (SV *current) |
3348 | _set_current (SV *current) |
3174 | PROTOTYPE: $ |
3349 | PROTOTYPE: $ |
3175 | CODE: |
3350 | CODE: |
3176 | SvREFCNT_dec (SvRV (coro_current)); |
3351 | SvREFCNT_dec (SvRV (coro_current)); |
3177 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
3352 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
… | |
… | |
3179 | void |
3354 | void |
3180 | _set_readyhook (SV *hook) |
3355 | _set_readyhook (SV *hook) |
3181 | PROTOTYPE: $ |
3356 | PROTOTYPE: $ |
3182 | CODE: |
3357 | CODE: |
3183 | SvREFCNT_dec (coro_readyhook); |
3358 | SvREFCNT_dec (coro_readyhook); |
|
|
3359 | SvGETMAGIC (hook); |
3184 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
3360 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
3185 | |
3361 | |
3186 | int |
3362 | int |
3187 | prio (Coro::State coro, int newprio = 0) |
3363 | prio (Coro::State coro, int newprio = 0) |
3188 | PROTOTYPE: $;$ |
3364 | PROTOTYPE: $;$ |
… | |
… | |
3195 | if (items > 1) |
3371 | if (items > 1) |
3196 | { |
3372 | { |
3197 | if (ix) |
3373 | if (ix) |
3198 | newprio = coro->prio - newprio; |
3374 | newprio = coro->prio - newprio; |
3199 | |
3375 | |
3200 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
3376 | if (newprio < CORO_PRIO_MIN) newprio = CORO_PRIO_MIN; |
3201 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
3377 | if (newprio > CORO_PRIO_MAX) newprio = CORO_PRIO_MAX; |
3202 | |
3378 | |
3203 | coro->prio = newprio; |
3379 | coro->prio = newprio; |
3204 | } |
3380 | } |
3205 | } |
3381 | } |
3206 | OUTPUT: |
3382 | OUTPUT: |
… | |
… | |
3219 | PROTOTYPE: |
3395 | PROTOTYPE: |
3220 | CODE: |
3396 | CODE: |
3221 | RETVAL = coro_nready; |
3397 | RETVAL = coro_nready; |
3222 | OUTPUT: |
3398 | OUTPUT: |
3223 | RETVAL |
3399 | RETVAL |
|
|
3400 | |
|
|
3401 | void |
|
|
3402 | suspend (Coro::State self) |
|
|
3403 | PROTOTYPE: $ |
|
|
3404 | CODE: |
|
|
3405 | self->flags |= CF_SUSPENDED; |
|
|
3406 | |
|
|
3407 | void |
|
|
3408 | resume (Coro::State self) |
|
|
3409 | PROTOTYPE: $ |
|
|
3410 | CODE: |
|
|
3411 | self->flags &= ~CF_SUSPENDED; |
3224 | |
3412 | |
3225 | void |
3413 | void |
3226 | _pool_handler (...) |
3414 | _pool_handler (...) |
3227 | CODE: |
3415 | CODE: |
3228 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
3416 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
… | |
… | |
3283 | rouse_wait (...) |
3471 | rouse_wait (...) |
3284 | PROTOTYPE: ;$ |
3472 | PROTOTYPE: ;$ |
3285 | PPCODE: |
3473 | PPCODE: |
3286 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3474 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3287 | |
3475 | |
|
|
3476 | void |
|
|
3477 | on_enter (SV *block) |
|
|
3478 | ALIAS: |
|
|
3479 | on_leave = 1 |
|
|
3480 | PROTOTYPE: & |
|
|
3481 | CODE: |
|
|
3482 | { |
|
|
3483 | struct coro *coro = SvSTATE_current; |
|
|
3484 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
|
|
3485 | |
|
|
3486 | block = (SV *)coro_sv_2cv (aTHX_ block); |
|
|
3487 | |
|
|
3488 | if (!*avp) |
|
|
3489 | *avp = newAV (); |
|
|
3490 | |
|
|
3491 | av_push (*avp, SvREFCNT_inc (block)); |
|
|
3492 | |
|
|
3493 | if (!ix) |
|
|
3494 | on_enterleave_call (aTHX_ block); |
|
|
3495 | |
|
|
3496 | LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */ |
|
|
3497 | SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro); |
|
|
3498 | ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */ |
|
|
3499 | } |
|
|
3500 | |
3288 | |
3501 | |
3289 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3502 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3290 | |
3503 | |
3291 | BOOT: |
3504 | BOOT: |
3292 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
3505 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
… | |
… | |
3295 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
3508 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
3296 | |
3509 | |
3297 | SV * |
3510 | SV * |
3298 | new (SV *klass, SV *count = 0) |
3511 | new (SV *klass, SV *count = 0) |
3299 | CODE: |
3512 | CODE: |
|
|
3513 | { |
|
|
3514 | int semcnt = 1; |
|
|
3515 | |
|
|
3516 | if (count) |
|
|
3517 | { |
|
|
3518 | SvGETMAGIC (count); |
|
|
3519 | |
|
|
3520 | if (SvOK (count)) |
|
|
3521 | semcnt = SvIV (count); |
|
|
3522 | } |
|
|
3523 | |
3300 | RETVAL = sv_bless ( |
3524 | RETVAL = sv_bless ( |
3301 | coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), |
3525 | coro_waitarray_new (aTHX_ semcnt), |
3302 | GvSTASH (CvGV (cv)) |
3526 | GvSTASH (CvGV (cv)) |
3303 | ); |
3527 | ); |
|
|
3528 | } |
3304 | OUTPUT: |
3529 | OUTPUT: |
3305 | RETVAL |
3530 | RETVAL |
3306 | |
3531 | |
3307 | # helper for Coro::Channel |
3532 | # helper for Coro::Channel and others |
3308 | SV * |
3533 | SV * |
3309 | _alloc (int count) |
3534 | _alloc (int count) |
3310 | CODE: |
3535 | CODE: |
3311 | RETVAL = coro_waitarray_new (aTHX_ count); |
3536 | RETVAL = coro_waitarray_new (aTHX_ count); |
3312 | OUTPUT: |
3537 | OUTPUT: |
… | |
… | |
3370 | for (i = 1; i <= wcount; ++i) |
3595 | for (i = 1; i <= wcount; ++i) |
3371 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
3596 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
3372 | } |
3597 | } |
3373 | } |
3598 | } |
3374 | |
3599 | |
|
|
3600 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
|
|
3601 | |
|
|
3602 | void |
|
|
3603 | _may_delete (SV *sem, int count, int extra_refs) |
|
|
3604 | PPCODE: |
|
|
3605 | { |
|
|
3606 | AV *av = (AV *)SvRV (sem); |
|
|
3607 | |
|
|
3608 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
|
|
3609 | && AvFILLp (av) == 0 /* no waiters, just count */ |
|
|
3610 | && SvIV (AvARRAY (av)[0]) == count) |
|
|
3611 | XSRETURN_YES; |
|
|
3612 | |
|
|
3613 | XSRETURN_NO; |
|
|
3614 | } |
|
|
3615 | |
3375 | MODULE = Coro::State PACKAGE = Coro::Signal |
3616 | MODULE = Coro::State PACKAGE = Coro::Signal |
3376 | |
3617 | |
3377 | SV * |
3618 | SV * |
3378 | new (SV *klass) |
3619 | new (SV *klass) |
3379 | CODE: |
3620 | CODE: |