… | |
… | |
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 |
… | |
… | |
159 | # if CORO_PTHREAD |
159 | # if CORO_PTHREAD |
160 | static void *coro_thx; |
160 | static void *coro_thx; |
161 | # endif |
161 | # endif |
162 | #endif |
162 | #endif |
163 | |
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 |
|
|
171 | # endif |
|
|
172 | #endif |
|
|
173 | |
164 | static double (*nvtime)(); /* so why doesn't it take void? */ |
174 | static double (*nvtime)(); /* so why doesn't it take void? */ |
|
|
175 | static void (*u2time)(pTHX_ UV ret[2]); |
165 | |
176 | |
166 | /* we hijack an hopefully unused CV flag for our purposes */ |
177 | /* we hijack an hopefully unused CV flag for our purposes */ |
167 | #define CVf_SLF 0x4000 |
178 | #define CVf_SLF 0x4000 |
168 | static OP *pp_slf (pTHX); |
179 | static OP *pp_slf (pTHX); |
169 | |
180 | |
… | |
… | |
195 | static CV *cv_coro_state_new; |
206 | static CV *cv_coro_state_new; |
196 | |
207 | |
197 | /* Coro::AnyEvent */ |
208 | /* Coro::AnyEvent */ |
198 | static SV *sv_activity; |
209 | static SV *sv_activity; |
199 | |
210 | |
|
|
211 | /* enable processtime/realtime profiling */ |
|
|
212 | static char enable_times; |
|
|
213 | typedef U32 coro_ts[2]; |
|
|
214 | static coro_ts time_real, time_cpu; |
|
|
215 | static char times_valid; |
|
|
216 | |
200 | static struct coro_cctx *cctx_first; |
217 | static struct coro_cctx *cctx_first; |
201 | static int cctx_count, cctx_idle; |
218 | static int cctx_count, cctx_idle; |
202 | |
219 | |
203 | enum { |
220 | enum { |
204 | CC_MAPPED = 0x01, |
221 | CC_MAPPED = 0x01, |
… | |
… | |
238 | enum { |
255 | enum { |
239 | CF_RUNNING = 0x0001, /* coroutine is running */ |
256 | CF_RUNNING = 0x0001, /* coroutine is running */ |
240 | CF_READY = 0x0002, /* coroutine is ready */ |
257 | CF_READY = 0x0002, /* coroutine is ready */ |
241 | CF_NEW = 0x0004, /* has never been switched to */ |
258 | CF_NEW = 0x0004, /* has never been switched to */ |
242 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
259 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
|
|
260 | CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */ |
243 | }; |
261 | }; |
244 | |
262 | |
245 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
263 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
246 | typedef struct |
264 | typedef struct |
247 | { |
265 | { |
… | |
… | |
260 | /* this is a structure representing a perl-level coroutine */ |
278 | /* this is a structure representing a perl-level coroutine */ |
261 | struct coro { |
279 | struct coro { |
262 | /* the C coroutine allocated to this perl coroutine, if any */ |
280 | /* the C coroutine allocated to this perl coroutine, if any */ |
263 | coro_cctx *cctx; |
281 | coro_cctx *cctx; |
264 | |
282 | |
|
|
283 | /* ready queue */ |
|
|
284 | struct coro *next_ready; |
|
|
285 | |
265 | /* state data */ |
286 | /* state data */ |
266 | struct CoroSLF slf_frame; /* saved slf frame */ |
287 | struct CoroSLF slf_frame; /* saved slf frame */ |
267 | AV *mainstack; |
288 | AV *mainstack; |
268 | perl_slots *slot; /* basically the saved sp */ |
289 | perl_slots *slot; /* basically the saved sp */ |
269 | |
290 | |
… | |
… | |
285 | /* async_pool */ |
306 | /* async_pool */ |
286 | SV *saved_deffh; |
307 | SV *saved_deffh; |
287 | SV *invoke_cb; |
308 | SV *invoke_cb; |
288 | AV *invoke_av; |
309 | AV *invoke_av; |
289 | |
310 | |
|
|
311 | /* on_enter/on_leave */ |
|
|
312 | AV *on_enter; |
|
|
313 | AV *on_leave; |
|
|
314 | |
|
|
315 | /* times */ |
|
|
316 | coro_ts t_cpu, t_real; |
|
|
317 | |
290 | /* linked list */ |
318 | /* linked list */ |
291 | struct coro *next, *prev; |
319 | struct coro *next, *prev; |
292 | }; |
320 | }; |
293 | |
321 | |
294 | typedef struct coro *Coro__State; |
322 | typedef struct coro *Coro__State; |
… | |
… | |
299 | /* the mainr easonw e don't support windows process emulation */ |
327 | /* the mainr easonw e don't support windows process emulation */ |
300 | static struct CoroSLF slf_frame; /* the current slf frame */ |
328 | static struct CoroSLF slf_frame; /* the current slf frame */ |
301 | |
329 | |
302 | /** Coro ********************************************************************/ |
330 | /** Coro ********************************************************************/ |
303 | |
331 | |
304 | #define PRIO_MAX 3 |
332 | #define CORO_PRIO_MAX 3 |
305 | #define PRIO_HIGH 1 |
333 | #define CORO_PRIO_HIGH 1 |
306 | #define PRIO_NORMAL 0 |
334 | #define CORO_PRIO_NORMAL 0 |
307 | #define PRIO_LOW -1 |
335 | #define CORO_PRIO_LOW -1 |
308 | #define PRIO_IDLE -3 |
336 | #define CORO_PRIO_IDLE -3 |
309 | #define PRIO_MIN -4 |
337 | #define CORO_PRIO_MIN -4 |
310 | |
338 | |
311 | /* for Coro.pm */ |
339 | /* for Coro.pm */ |
312 | static SV *coro_current; |
340 | static SV *coro_current; |
313 | static SV *coro_readyhook; |
341 | static SV *coro_readyhook; |
314 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
342 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
315 | static CV *cv_coro_run, *cv_coro_terminate; |
343 | static CV *cv_coro_run, *cv_coro_terminate; |
316 | static struct coro *coro_first; |
344 | static struct coro *coro_first; |
317 | #define coro_nready coroapi.nready |
345 | #define coro_nready coroapi.nready |
318 | |
346 | |
319 | /** lowlevel stuff **********************************************************/ |
347 | /** lowlevel stuff **********************************************************/ |
… | |
… | |
352 | INLINE CV * |
380 | INLINE CV * |
353 | coro_sv_2cv (pTHX_ SV *sv) |
381 | coro_sv_2cv (pTHX_ SV *sv) |
354 | { |
382 | { |
355 | HV *st; |
383 | HV *st; |
356 | GV *gvp; |
384 | GV *gvp; |
357 | return sv_2cv (sv, &st, &gvp, 0); |
385 | CV *cv = sv_2cv (sv, &st, &gvp, 0); |
|
|
386 | |
|
|
387 | if (!cv) |
|
|
388 | croak ("code reference expected"); |
|
|
389 | |
|
|
390 | return cv; |
358 | } |
391 | } |
|
|
392 | |
|
|
393 | INLINE void |
|
|
394 | coro_times_update () |
|
|
395 | { |
|
|
396 | #ifdef coro_clock_gettime |
|
|
397 | struct timespec ts; |
|
|
398 | |
|
|
399 | ts.tv_sec = ts.tv_nsec = 0; |
|
|
400 | coro_clock_gettime (CORO_CLOCK_THREAD_CPUTIME_ID, &ts); |
|
|
401 | time_cpu [0] = ts.tv_sec; time_cpu [1] = ts.tv_nsec; |
|
|
402 | |
|
|
403 | ts.tv_sec = ts.tv_nsec = 0; |
|
|
404 | coro_clock_gettime (CORO_CLOCK_MONOTONIC, &ts); |
|
|
405 | time_real [0] = ts.tv_sec; time_real [1] = ts.tv_nsec; |
|
|
406 | #else |
|
|
407 | dTHX; |
|
|
408 | UV tv[2]; |
|
|
409 | |
|
|
410 | u2time (aTHX_ tv); |
|
|
411 | time_real [0] = tv [0]; |
|
|
412 | time_real [1] = tv [1] * 1000; |
|
|
413 | #endif |
|
|
414 | } |
|
|
415 | |
|
|
416 | INLINE void |
|
|
417 | coro_times_add (struct coro *c) |
|
|
418 | { |
|
|
419 | c->t_real [1] += time_real [1]; |
|
|
420 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
|
|
421 | c->t_real [0] += time_real [0]; |
|
|
422 | |
|
|
423 | c->t_cpu [1] += time_cpu [1]; |
|
|
424 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
|
|
425 | c->t_cpu [0] += time_cpu [0]; |
|
|
426 | } |
|
|
427 | |
|
|
428 | INLINE void |
|
|
429 | coro_times_sub (struct coro *c) |
|
|
430 | { |
|
|
431 | if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } |
|
|
432 | c->t_real [1] -= time_real [1]; |
|
|
433 | c->t_real [0] -= time_real [0]; |
|
|
434 | |
|
|
435 | if (c->t_cpu [1] < time_cpu [1]) { c->t_cpu [1] += 1000000000; --c->t_cpu [0]; } |
|
|
436 | c->t_cpu [1] -= time_cpu [1]; |
|
|
437 | c->t_cpu [0] -= time_cpu [0]; |
|
|
438 | } |
|
|
439 | |
|
|
440 | /*****************************************************************************/ |
|
|
441 | /* magic glue */ |
|
|
442 | |
|
|
443 | #define CORO_MAGIC_type_cv 26 |
|
|
444 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
445 | |
|
|
446 | #define CORO_MAGIC_NN(sv, type) \ |
|
|
447 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
|
|
448 | ? SvMAGIC (sv) \ |
|
|
449 | : mg_find (sv, type)) |
|
|
450 | |
|
|
451 | #define CORO_MAGIC(sv, type) \ |
|
|
452 | (expect_true (SvMAGIC (sv)) \ |
|
|
453 | ? CORO_MAGIC_NN (sv, type) \ |
|
|
454 | : 0) |
|
|
455 | |
|
|
456 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
457 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
|
|
458 | |
|
|
459 | INLINE struct coro * |
|
|
460 | SvSTATE_ (pTHX_ SV *coro) |
|
|
461 | { |
|
|
462 | HV *stash; |
|
|
463 | MAGIC *mg; |
|
|
464 | |
|
|
465 | if (SvROK (coro)) |
|
|
466 | coro = SvRV (coro); |
|
|
467 | |
|
|
468 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
469 | croak ("Coro::State object required"); |
|
|
470 | |
|
|
471 | stash = SvSTASH (coro); |
|
|
472 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
473 | { |
|
|
474 | /* very slow, but rare, check */ |
|
|
475 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
476 | croak ("Coro::State object required"); |
|
|
477 | } |
|
|
478 | |
|
|
479 | mg = CORO_MAGIC_state (coro); |
|
|
480 | return (struct coro *)mg->mg_ptr; |
|
|
481 | } |
|
|
482 | |
|
|
483 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
484 | |
|
|
485 | /* faster than SvSTATE, but expects a coroutine hv */ |
|
|
486 | #define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) |
|
|
487 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
|
|
488 | |
|
|
489 | /*****************************************************************************/ |
|
|
490 | /* padlist management and caching */ |
359 | |
491 | |
360 | static AV * |
492 | static AV * |
361 | coro_derive_padlist (pTHX_ CV *cv) |
493 | coro_derive_padlist (pTHX_ CV *cv) |
362 | { |
494 | { |
363 | AV *padlist = CvPADLIST (cv); |
495 | AV *padlist = CvPADLIST (cv); |
… | |
… | |
371 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
503 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
372 | #endif |
504 | #endif |
373 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
505 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
374 | --AvFILLp (padlist); |
506 | --AvFILLp (padlist); |
375 | |
507 | |
376 | av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); |
508 | av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); |
377 | av_store (newpadlist, 1, (SV *)newpad); |
509 | av_store (newpadlist, 1, (SV *)newpad); |
378 | |
510 | |
379 | return newpadlist; |
511 | return newpadlist; |
380 | } |
512 | } |
381 | |
513 | |
382 | static void |
514 | static void |
383 | free_padlist (pTHX_ AV *padlist) |
515 | free_padlist (pTHX_ AV *padlist) |
384 | { |
516 | { |
385 | /* may be during global destruction */ |
517 | /* may be during global destruction */ |
386 | if (SvREFCNT (padlist)) |
518 | if (!IN_DESTRUCT) |
387 | { |
519 | { |
388 | I32 i = AvFILLp (padlist); |
520 | I32 i = AvFILLp (padlist); |
389 | while (i >= 0) |
521 | |
|
|
522 | while (i > 0) /* special-case index 0 */ |
390 | { |
523 | { |
|
|
524 | /* we try to be extra-careful here */ |
391 | SV **svp = AvARRAY (padlist)[i--]; |
525 | AV *av = (AV *)AvARRAY (padlist)[i--]; |
392 | if (svp) |
526 | I32 j = AvFILLp (av); |
393 | { |
527 | |
394 | AvREAL_on (*svp); |
528 | while (j >= 0) |
395 | av_undef (*svp); |
529 | SvREFCNT_dec (AvARRAY (av)[j--]); |
|
|
530 | |
|
|
531 | AvFILLp (av) = -1; |
396 | SvREFCNT_dec (*svp); |
532 | SvREFCNT_dec (av); |
397 | } |
|
|
398 | } |
533 | } |
399 | |
534 | |
|
|
535 | SvREFCNT_dec (AvARRAY (padlist)[0]); |
|
|
536 | |
|
|
537 | AvFILLp (padlist) = -1; |
400 | SvREFCNT_dec ((SV*)padlist); |
538 | SvREFCNT_dec ((SV*)padlist); |
401 | } |
539 | } |
402 | } |
540 | } |
403 | |
541 | |
404 | static int |
542 | static int |
405 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
543 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
406 | { |
544 | { |
407 | AV *padlist; |
545 | AV *padlist; |
408 | AV *av = (AV *)mg->mg_obj; |
546 | AV *av = (AV *)mg->mg_obj; |
|
|
547 | |
|
|
548 | /* perl manages to free our internal AV and _then_ call us */ |
|
|
549 | if (IN_DESTRUCT) |
|
|
550 | return 0; |
409 | |
551 | |
410 | /* casting is fun. */ |
552 | /* casting is fun. */ |
411 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
553 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
412 | free_padlist (aTHX_ padlist); |
554 | free_padlist (aTHX_ padlist); |
413 | |
555 | |
414 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
556 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
415 | |
557 | |
416 | return 0; |
558 | return 0; |
417 | } |
559 | } |
418 | |
|
|
419 | #define CORO_MAGIC_type_cv 26 |
|
|
420 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
421 | |
560 | |
422 | static MGVTBL coro_cv_vtbl = { |
561 | static MGVTBL coro_cv_vtbl = { |
423 | 0, 0, 0, 0, |
562 | 0, 0, 0, 0, |
424 | coro_cv_free |
563 | coro_cv_free |
425 | }; |
564 | }; |
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 | |
565 | |
470 | /* the next two functions merely cache the padlists */ |
566 | /* the next two functions merely cache the padlists */ |
471 | static void |
567 | static void |
472 | get_padlist (pTHX_ CV *cv) |
568 | get_padlist (pTHX_ CV *cv) |
473 | { |
569 | { |
… | |
… | |
509 | } |
605 | } |
510 | |
606 | |
511 | /** load & save, init *******************************************************/ |
607 | /** load & save, init *******************************************************/ |
512 | |
608 | |
513 | static void |
609 | static void |
|
|
610 | on_enterleave_call (pTHX_ SV *cb); |
|
|
611 | |
|
|
612 | static void |
514 | load_perl (pTHX_ Coro__State c) |
613 | load_perl (pTHX_ Coro__State c) |
515 | { |
614 | { |
516 | perl_slots *slot = c->slot; |
615 | perl_slots *slot = c->slot; |
517 | c->slot = 0; |
616 | c->slot = 0; |
518 | |
617 | |
… | |
… | |
544 | PUTBACK; |
643 | PUTBACK; |
545 | } |
644 | } |
546 | |
645 | |
547 | slf_frame = c->slf_frame; |
646 | slf_frame = c->slf_frame; |
548 | CORO_THROW = c->except; |
647 | CORO_THROW = c->except; |
|
|
648 | |
|
|
649 | if (expect_false (enable_times)) |
|
|
650 | { |
|
|
651 | if (expect_false (!times_valid)) |
|
|
652 | coro_times_update (); |
|
|
653 | |
|
|
654 | coro_times_sub (c); |
|
|
655 | } |
|
|
656 | |
|
|
657 | if (expect_false (c->on_enter)) |
|
|
658 | { |
|
|
659 | int i; |
|
|
660 | |
|
|
661 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
|
|
662 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
|
|
663 | } |
549 | } |
664 | } |
550 | |
665 | |
551 | static void |
666 | static void |
552 | save_perl (pTHX_ Coro__State c) |
667 | save_perl (pTHX_ Coro__State c) |
553 | { |
668 | { |
|
|
669 | if (expect_false (c->on_leave)) |
|
|
670 | { |
|
|
671 | int i; |
|
|
672 | |
|
|
673 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
|
|
674 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
|
|
675 | } |
|
|
676 | |
|
|
677 | times_valid = 0; |
|
|
678 | |
|
|
679 | if (expect_false (enable_times)) |
|
|
680 | { |
|
|
681 | coro_times_update (); times_valid = 1; |
|
|
682 | coro_times_add (c); |
|
|
683 | } |
|
|
684 | |
554 | c->except = CORO_THROW; |
685 | c->except = CORO_THROW; |
555 | c->slf_frame = slf_frame; |
686 | c->slf_frame = slf_frame; |
556 | |
687 | |
557 | { |
688 | { |
558 | dSP; |
689 | dSP; |
… | |
… | |
571 | { |
702 | { |
572 | while (expect_true (cxix >= 0)) |
703 | while (expect_true (cxix >= 0)) |
573 | { |
704 | { |
574 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
705 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
575 | |
706 | |
576 | if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)) |
707 | if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT)) |
577 | { |
708 | { |
578 | CV *cv = cx->blk_sub.cv; |
709 | CV *cv = cx->blk_sub.cv; |
579 | |
710 | |
580 | if (expect_true (CvDEPTH (cv))) |
711 | if (expect_true (CvDEPTH (cv))) |
581 | { |
712 | { |
… | |
… | |
605 | /* we manually unroll here, as usually 2 slots is enough */ |
736 | /* we manually unroll here, as usually 2 slots is enough */ |
606 | if (SLOT_COUNT >= 1) CXINC; |
737 | if (SLOT_COUNT >= 1) CXINC; |
607 | if (SLOT_COUNT >= 2) CXINC; |
738 | if (SLOT_COUNT >= 2) CXINC; |
608 | if (SLOT_COUNT >= 3) CXINC; |
739 | if (SLOT_COUNT >= 3) CXINC; |
609 | { |
740 | { |
610 | int i; |
741 | unsigned int i; |
611 | for (i = 3; i < SLOT_COUNT; ++i) |
742 | for (i = 3; i < SLOT_COUNT; ++i) |
612 | CXINC; |
743 | CXINC; |
613 | } |
744 | } |
614 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
745 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
615 | |
746 | |
… | |
… | |
757 | #endif |
888 | #endif |
758 | |
889 | |
759 | /* |
890 | /* |
760 | * This overrides the default magic get method of %SIG elements. |
891 | * This overrides the default magic get method of %SIG elements. |
761 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
892 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
762 | * and instead of tryign to save and restore the hash elements, we just provide |
893 | * and instead of trying to save and restore the hash elements, we just provide |
763 | * readback here. |
894 | * readback here. |
764 | * We only do this when the hook is != 0, as they are often set to 0 temporarily, |
|
|
765 | * not expecting this to actually change the hook. This is a potential problem |
|
|
766 | * when a schedule happens then, but we ignore this. |
|
|
767 | */ |
895 | */ |
768 | static int |
896 | static int |
769 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
897 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
770 | { |
898 | { |
771 | const char *s = MgPV_nolen_const (mg); |
899 | const char *s = MgPV_nolen_const (mg); |
… | |
… | |
824 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
952 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
825 | |
953 | |
826 | if (svp) |
954 | if (svp) |
827 | { |
955 | { |
828 | SV *old = *svp; |
956 | SV *old = *svp; |
829 | *svp = newSVsv (sv); |
957 | *svp = SvOK (sv) ? newSVsv (sv) : 0; |
830 | SvREFCNT_dec (old); |
958 | SvREFCNT_dec (old); |
831 | return 0; |
959 | return 0; |
832 | } |
960 | } |
833 | } |
961 | } |
834 | |
962 | |
… | |
… | |
923 | |
1051 | |
924 | PL_op = (OP *)&coro_setup_op; |
1052 | PL_op = (OP *)&coro_setup_op; |
925 | |
1053 | |
926 | /* copy throw, in case it was set before coro_setup */ |
1054 | /* copy throw, in case it was set before coro_setup */ |
927 | CORO_THROW = coro->except; |
1055 | CORO_THROW = coro->except; |
928 | } |
|
|
929 | |
1056 | |
|
|
1057 | if (expect_false (enable_times)) |
|
|
1058 | { |
|
|
1059 | coro_times_update (); |
|
|
1060 | coro_times_sub (coro); |
|
|
1061 | } |
|
|
1062 | } |
|
|
1063 | |
930 | static void |
1064 | static void |
931 | coro_destruct (pTHX_ struct coro *coro) |
1065 | coro_unwind_stacks (pTHX) |
932 | { |
1066 | { |
933 | if (!IN_DESTRUCT) |
1067 | if (!IN_DESTRUCT) |
934 | { |
1068 | { |
935 | /* restore all saved variables and stuff */ |
1069 | /* restore all saved variables and stuff */ |
936 | LEAVE_SCOPE (0); |
1070 | LEAVE_SCOPE (0); |
… | |
… | |
944 | POPSTACK_TO (PL_mainstack); |
1078 | POPSTACK_TO (PL_mainstack); |
945 | |
1079 | |
946 | /* unwind main stack */ |
1080 | /* unwind main stack */ |
947 | dounwind (-1); |
1081 | dounwind (-1); |
948 | } |
1082 | } |
|
|
1083 | } |
949 | |
1084 | |
950 | SvREFCNT_dec (GvSV (PL_defgv)); |
1085 | static void |
951 | SvREFCNT_dec (GvAV (PL_defgv)); |
1086 | coro_destruct_perl (pTHX_ struct coro *coro) |
952 | SvREFCNT_dec (GvSV (PL_errgv)); |
1087 | { |
953 | SvREFCNT_dec (PL_defoutgv); |
1088 | SV *svf [9]; |
954 | SvREFCNT_dec (PL_rs); |
|
|
955 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
956 | SvREFCNT_dec (GvHV (PL_hintgv)); |
|
|
957 | |
1089 | |
958 | SvREFCNT_dec (PL_diehook); |
|
|
959 | SvREFCNT_dec (PL_warnhook); |
|
|
960 | |
1090 | { |
|
|
1091 | struct coro *current = SvSTATE_current; |
|
|
1092 | |
|
|
1093 | assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); |
|
|
1094 | |
|
|
1095 | save_perl (aTHX_ current); |
|
|
1096 | load_perl (aTHX_ coro); |
|
|
1097 | |
|
|
1098 | coro_unwind_stacks (aTHX); |
|
|
1099 | coro_destruct_stacks (aTHX); |
|
|
1100 | |
|
|
1101 | // now save some sv's to be free'd later |
|
|
1102 | svf [0] = GvSV (PL_defgv); |
|
|
1103 | svf [1] = (SV *)GvAV (PL_defgv); |
|
|
1104 | svf [2] = GvSV (PL_errgv); |
|
|
1105 | svf [3] = (SV *)PL_defoutgv; |
|
|
1106 | svf [4] = PL_rs; |
|
|
1107 | svf [5] = GvSV (irsgv); |
|
|
1108 | svf [6] = (SV *)GvHV (PL_hintgv); |
|
|
1109 | svf [7] = PL_diehook; |
|
|
1110 | svf [8] = PL_warnhook; |
|
|
1111 | assert (9 == sizeof (svf) / sizeof (*svf)); |
|
|
1112 | |
|
|
1113 | load_perl (aTHX_ current); |
|
|
1114 | } |
|
|
1115 | |
|
|
1116 | { |
|
|
1117 | unsigned int i; |
|
|
1118 | |
|
|
1119 | for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i) |
|
|
1120 | SvREFCNT_dec (svf [i]); |
|
|
1121 | |
961 | SvREFCNT_dec (coro->saved_deffh); |
1122 | SvREFCNT_dec (coro->saved_deffh); |
962 | SvREFCNT_dec (coro->rouse_cb); |
1123 | SvREFCNT_dec (coro->rouse_cb); |
963 | SvREFCNT_dec (coro->invoke_cb); |
1124 | SvREFCNT_dec (coro->invoke_cb); |
964 | SvREFCNT_dec (coro->invoke_av); |
1125 | SvREFCNT_dec (coro->invoke_av); |
965 | |
1126 | } |
966 | coro_destruct_stacks (aTHX); |
|
|
967 | } |
1127 | } |
968 | |
1128 | |
969 | INLINE void |
1129 | INLINE void |
970 | free_coro_mortal (pTHX) |
1130 | free_coro_mortal (pTHX) |
971 | { |
1131 | { |
… | |
… | |
1265 | cctx_destroy (coro_cctx *cctx) |
1425 | cctx_destroy (coro_cctx *cctx) |
1266 | { |
1426 | { |
1267 | if (!cctx) |
1427 | if (!cctx) |
1268 | return; |
1428 | return; |
1269 | |
1429 | |
1270 | assert (cctx != cctx_current);//D temporary |
1430 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary? |
1271 | |
1431 | |
1272 | --cctx_count; |
1432 | --cctx_count; |
1273 | coro_destroy (&cctx->cctx); |
1433 | coro_destroy (&cctx->cctx); |
1274 | |
1434 | |
1275 | /* coro_transfer creates new, empty cctx's */ |
1435 | /* coro_transfer creates new, empty cctx's */ |
… | |
… | |
1339 | /* TODO: throwing up here is considered harmful */ |
1499 | /* TODO: throwing up here is considered harmful */ |
1340 | |
1500 | |
1341 | if (expect_true (prev != next)) |
1501 | if (expect_true (prev != next)) |
1342 | { |
1502 | { |
1343 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1503 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1344 | croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); |
1504 | croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); |
1345 | |
1505 | |
1346 | if (expect_false (next->flags & CF_RUNNING)) |
|
|
1347 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
|
|
1348 | |
|
|
1349 | if (expect_false (next->flags & CF_DESTROYED)) |
1506 | if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED))) |
1350 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); |
1507 | croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); |
1351 | |
1508 | |
1352 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1509 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1353 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1510 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1354 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1511 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1355 | #endif |
1512 | #endif |
… | |
… | |
1393 | coro_setup (aTHX_ next); |
1550 | coro_setup (aTHX_ next); |
1394 | } |
1551 | } |
1395 | else |
1552 | else |
1396 | load_perl (aTHX_ next); |
1553 | load_perl (aTHX_ next); |
1397 | |
1554 | |
1398 | assert (!prev->cctx);//D temporary |
|
|
1399 | |
|
|
1400 | /* possibly untie and reuse the cctx */ |
1555 | /* possibly untie and reuse the cctx */ |
1401 | if (expect_true ( |
1556 | if (expect_true ( |
1402 | cctx_current->idle_sp == STACKLEVEL |
1557 | cctx_current->idle_sp == STACKLEVEL |
1403 | && !(cctx_current->flags & CC_TRACE) |
1558 | && !(cctx_current->flags & CC_TRACE) |
1404 | && !force_cctx |
1559 | && !force_cctx |
… | |
… | |
1445 | coro_state_destroy (pTHX_ struct coro *coro) |
1600 | coro_state_destroy (pTHX_ struct coro *coro) |
1446 | { |
1601 | { |
1447 | if (coro->flags & CF_DESTROYED) |
1602 | if (coro->flags & CF_DESTROYED) |
1448 | return 0; |
1603 | return 0; |
1449 | |
1604 | |
1450 | if (coro->on_destroy) |
1605 | if (coro->on_destroy && !PL_dirty) |
1451 | coro->on_destroy (aTHX_ coro); |
1606 | coro->on_destroy (aTHX_ coro); |
1452 | |
1607 | |
1453 | coro->flags |= CF_DESTROYED; |
1608 | coro->flags |= CF_DESTROYED; |
1454 | |
1609 | |
1455 | if (coro->flags & CF_READY) |
1610 | if (coro->flags & CF_READY) |
… | |
… | |
1459 | --coro_nready; |
1614 | --coro_nready; |
1460 | } |
1615 | } |
1461 | else |
1616 | else |
1462 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1617 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1463 | |
1618 | |
1464 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1619 | if (coro->mainstack |
1465 | { |
1620 | && coro->mainstack != main_mainstack |
1466 | struct coro temp; |
1621 | && coro->slot |
1467 | |
1622 | && !PL_dirty) |
1468 | assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); |
|
|
1469 | |
|
|
1470 | save_perl (aTHX_ &temp); |
|
|
1471 | load_perl (aTHX_ coro); |
|
|
1472 | |
|
|
1473 | coro_destruct (aTHX_ coro); |
1623 | coro_destruct_perl (aTHX_ coro); |
1474 | |
|
|
1475 | load_perl (aTHX_ &temp); |
|
|
1476 | |
|
|
1477 | coro->slot = 0; |
|
|
1478 | } |
|
|
1479 | |
1624 | |
1480 | cctx_destroy (coro->cctx); |
1625 | cctx_destroy (coro->cctx); |
1481 | SvREFCNT_dec (coro->startcv); |
1626 | SvREFCNT_dec (coro->startcv); |
1482 | SvREFCNT_dec (coro->args); |
1627 | SvREFCNT_dec (coro->args); |
1483 | SvREFCNT_dec (CORO_THROW); |
1628 | SvREFCNT_dec (CORO_THROW); |
… | |
… | |
1570 | /** Coro ********************************************************************/ |
1715 | /** Coro ********************************************************************/ |
1571 | |
1716 | |
1572 | INLINE void |
1717 | INLINE void |
1573 | coro_enq (pTHX_ struct coro *coro) |
1718 | coro_enq (pTHX_ struct coro *coro) |
1574 | { |
1719 | { |
1575 | av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); |
1720 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1576 | } |
|
|
1577 | |
1721 | |
1578 | INLINE SV * |
1722 | SvREFCNT_inc_NN (coro->hv); |
|
|
1723 | |
|
|
1724 | coro->next_ready = 0; |
|
|
1725 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
|
|
1726 | ready [1] = coro; |
|
|
1727 | } |
|
|
1728 | |
|
|
1729 | INLINE struct coro * |
1579 | coro_deq (pTHX) |
1730 | coro_deq (pTHX) |
1580 | { |
1731 | { |
1581 | int prio; |
1732 | int prio; |
1582 | |
1733 | |
1583 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1734 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
1584 | if (AvFILLp (coro_ready [prio]) >= 0) |
1735 | { |
1585 | return av_shift (coro_ready [prio]); |
1736 | struct coro **ready = coro_ready [prio]; |
|
|
1737 | |
|
|
1738 | if (ready [0]) |
|
|
1739 | { |
|
|
1740 | struct coro *coro = ready [0]; |
|
|
1741 | ready [0] = coro->next_ready; |
|
|
1742 | return coro; |
|
|
1743 | } |
|
|
1744 | } |
1586 | |
1745 | |
1587 | return 0; |
1746 | return 0; |
1588 | } |
1747 | } |
1589 | |
1748 | |
1590 | static int |
1749 | static int |
… | |
… | |
1654 | static void |
1813 | static void |
1655 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1814 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1656 | { |
1815 | { |
1657 | for (;;) |
1816 | for (;;) |
1658 | { |
1817 | { |
1659 | SV *next_sv = coro_deq (aTHX); |
1818 | struct coro *next = coro_deq (aTHX); |
1660 | |
1819 | |
1661 | if (expect_true (next_sv)) |
1820 | if (expect_true (next)) |
1662 | { |
1821 | { |
1663 | struct coro *next = SvSTATE_hv (next_sv); |
|
|
1664 | |
|
|
1665 | /* cannot transfer to destroyed coros, skip and look for next */ |
1822 | /* cannot transfer to destroyed coros, skip and look for next */ |
1666 | if (expect_false (next->flags & CF_DESTROYED)) |
1823 | if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED))) |
1667 | SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ |
1824 | SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ |
1668 | else |
1825 | else |
1669 | { |
1826 | { |
1670 | next->flags &= ~CF_READY; |
1827 | next->flags &= ~CF_READY; |
1671 | --coro_nready; |
1828 | --coro_nready; |
1672 | |
1829 | |
… | |
… | |
1850 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
2007 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1851 | api_ready (aTHX_ sv_manager); |
2008 | api_ready (aTHX_ sv_manager); |
1852 | |
2009 | |
1853 | frame->prepare = prepare_schedule; |
2010 | frame->prepare = prepare_schedule; |
1854 | frame->check = slf_check_repeat; |
2011 | frame->check = slf_check_repeat; |
|
|
2012 | |
|
|
2013 | /* as a minor optimisation, we could unwind all stacks here */ |
|
|
2014 | /* but that puts extra pressure on pp_slf, and is not worth much */ |
|
|
2015 | /*coro_unwind_stacks (aTHX);*/ |
1855 | } |
2016 | } |
1856 | |
2017 | |
1857 | /*****************************************************************************/ |
2018 | /*****************************************************************************/ |
1858 | /* async pool handler */ |
2019 | /* async pool handler */ |
1859 | |
2020 | |
… | |
… | |
1942 | SV *data = (SV *)GENSUB_ARG; |
2103 | SV *data = (SV *)GENSUB_ARG; |
1943 | |
2104 | |
1944 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2105 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
1945 | { |
2106 | { |
1946 | /* first call, set args */ |
2107 | /* first call, set args */ |
|
|
2108 | SV *coro = SvRV (data); |
1947 | AV *av = newAV (); |
2109 | AV *av = newAV (); |
1948 | SV *coro = SvRV (data); |
|
|
1949 | |
2110 | |
1950 | SvRV_set (data, (SV *)av); |
2111 | SvRV_set (data, (SV *)av); |
1951 | api_ready (aTHX_ coro); |
|
|
1952 | SvREFCNT_dec (coro); |
|
|
1953 | |
2112 | |
1954 | /* better take a full copy of the arguments */ |
2113 | /* better take a full copy of the arguments */ |
1955 | while (items--) |
2114 | while (items--) |
1956 | av_store (av, items, newSVsv (ST (items))); |
2115 | av_store (av, items, newSVsv (ST (items))); |
|
|
2116 | |
|
|
2117 | api_ready (aTHX_ coro); |
|
|
2118 | SvREFCNT_dec (coro); |
1957 | } |
2119 | } |
1958 | |
2120 | |
1959 | XSRETURN_EMPTY; |
2121 | XSRETURN_EMPTY; |
1960 | } |
2122 | } |
1961 | |
2123 | |
… | |
… | |
1978 | |
2140 | |
1979 | EXTEND (SP, AvFILLp (av) + 1); |
2141 | EXTEND (SP, AvFILLp (av) + 1); |
1980 | for (i = 0; i <= AvFILLp (av); ++i) |
2142 | for (i = 0; i <= AvFILLp (av); ++i) |
1981 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
2143 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
1982 | |
2144 | |
1983 | /* we have stolen the elements, so ste length to zero and free */ |
2145 | /* we have stolen the elements, so set length to zero and free */ |
1984 | AvFILLp (av) = -1; |
2146 | AvFILLp (av) = -1; |
1985 | av_undef (av); |
2147 | av_undef (av); |
1986 | |
2148 | |
1987 | PUTBACK; |
2149 | PUTBACK; |
1988 | } |
2150 | } |
… | |
… | |
2278 | |
2440 | |
2279 | PL_op->op_ppaddr = pp_slf; |
2441 | PL_op->op_ppaddr = pp_slf; |
2280 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2442 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2281 | |
2443 | |
2282 | PL_op = (OP *)&slf_restore; |
2444 | PL_op = (OP *)&slf_restore; |
|
|
2445 | } |
|
|
2446 | |
|
|
2447 | /*****************************************************************************/ |
|
|
2448 | /* dynamic wind */ |
|
|
2449 | |
|
|
2450 | static void |
|
|
2451 | on_enterleave_call (pTHX_ SV *cb) |
|
|
2452 | { |
|
|
2453 | dSP; |
|
|
2454 | |
|
|
2455 | PUSHSTACK; |
|
|
2456 | |
|
|
2457 | PUSHMARK (SP); |
|
|
2458 | PUTBACK; |
|
|
2459 | call_sv (cb, G_VOID | G_DISCARD); |
|
|
2460 | SPAGAIN; |
|
|
2461 | |
|
|
2462 | POPSTACK; |
|
|
2463 | } |
|
|
2464 | |
|
|
2465 | static SV * |
|
|
2466 | coro_avp_pop_and_free (pTHX_ AV **avp) |
|
|
2467 | { |
|
|
2468 | AV *av = *avp; |
|
|
2469 | SV *res = av_pop (av); |
|
|
2470 | |
|
|
2471 | if (AvFILLp (av) < 0) |
|
|
2472 | { |
|
|
2473 | *avp = 0; |
|
|
2474 | SvREFCNT_dec (av); |
|
|
2475 | } |
|
|
2476 | |
|
|
2477 | return res; |
|
|
2478 | } |
|
|
2479 | |
|
|
2480 | static void |
|
|
2481 | coro_pop_on_enter (pTHX_ void *coro) |
|
|
2482 | { |
|
|
2483 | SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter); |
|
|
2484 | SvREFCNT_dec (cb); |
|
|
2485 | } |
|
|
2486 | |
|
|
2487 | static void |
|
|
2488 | coro_pop_on_leave (pTHX_ void *coro) |
|
|
2489 | { |
|
|
2490 | SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave); |
|
|
2491 | on_enterleave_call (aTHX_ sv_2mortal (cb)); |
2283 | } |
2492 | } |
2284 | |
2493 | |
2285 | /*****************************************************************************/ |
2494 | /*****************************************************************************/ |
2286 | /* PerlIO::cede */ |
2495 | /* PerlIO::cede */ |
2287 | |
2496 | |
… | |
… | |
2361 | /* Coro::Semaphore & Coro::Signal */ |
2570 | /* Coro::Semaphore & Coro::Signal */ |
2362 | |
2571 | |
2363 | static SV * |
2572 | static SV * |
2364 | coro_waitarray_new (pTHX_ int count) |
2573 | coro_waitarray_new (pTHX_ int count) |
2365 | { |
2574 | { |
2366 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
2575 | /* a waitarray=semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
2367 | AV *av = newAV (); |
2576 | AV *av = newAV (); |
2368 | SV **ary; |
2577 | SV **ary; |
2369 | |
2578 | |
2370 | /* unfortunately, building manually saves memory */ |
2579 | /* unfortunately, building manually saves memory */ |
2371 | Newx (ary, 2, SV *); |
2580 | Newx (ary, 2, SV *); |
… | |
… | |
2515 | { |
2724 | { |
2516 | /* callback form */ |
2725 | /* callback form */ |
2517 | AV *av = (AV *)SvRV (arg [0]); |
2726 | AV *av = (AV *)SvRV (arg [0]); |
2518 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
2727 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
2519 | |
2728 | |
2520 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
2729 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2521 | |
2730 | |
2522 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2731 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2523 | coro_semaphore_adjust (aTHX_ av, 0); |
2732 | coro_semaphore_adjust (aTHX_ av, 0); |
2524 | |
2733 | |
2525 | frame->prepare = prepare_nop; |
2734 | frame->prepare = prepare_nop; |
… | |
… | |
2549 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2758 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2550 | AvARRAY (av)[1] = cb; |
2759 | AvARRAY (av)[1] = cb; |
2551 | |
2760 | |
2552 | cb = av_shift (av); |
2761 | cb = av_shift (av); |
2553 | |
2762 | |
|
|
2763 | if (SvTYPE (cb) == SVt_PVCV) |
|
|
2764 | { |
|
|
2765 | dSP; |
|
|
2766 | PUSHMARK (SP); |
|
|
2767 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
2768 | PUTBACK; |
|
|
2769 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
|
|
2770 | } |
|
|
2771 | else |
|
|
2772 | { |
2554 | api_ready (aTHX_ cb); |
2773 | api_ready (aTHX_ cb); |
2555 | sv_setiv (cb, 0); /* signal waiter */ |
2774 | sv_setiv (cb, 0); /* signal waiter */ |
|
|
2775 | } |
|
|
2776 | |
2556 | SvREFCNT_dec (cb); |
2777 | SvREFCNT_dec (cb); |
2557 | |
2778 | |
2558 | --count; |
2779 | --count; |
2559 | } |
2780 | } |
2560 | } |
2781 | } |
… | |
… | |
2569 | static void |
2790 | static void |
2570 | slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2791 | slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2571 | { |
2792 | { |
2572 | AV *av = (AV *)SvRV (arg [0]); |
2793 | AV *av = (AV *)SvRV (arg [0]); |
2573 | |
2794 | |
|
|
2795 | if (items >= 2) |
|
|
2796 | { |
|
|
2797 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
|
|
2798 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
|
|
2799 | |
2574 | if (SvIVX (AvARRAY (av)[0])) |
2800 | if (SvIVX (AvARRAY (av)[0])) |
|
|
2801 | coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ |
|
|
2802 | |
|
|
2803 | frame->prepare = prepare_nop; |
|
|
2804 | frame->check = slf_check_nop; |
|
|
2805 | } |
|
|
2806 | else if (SvIVX (AvARRAY (av)[0])) |
2575 | { |
2807 | { |
2576 | SvIVX (AvARRAY (av)[0]) = 0; |
2808 | SvIVX (AvARRAY (av)[0]) = 0; |
2577 | frame->prepare = prepare_nop; |
2809 | frame->prepare = prepare_nop; |
2578 | frame->check = slf_check_nop; |
2810 | frame->check = slf_check_nop; |
2579 | } |
2811 | } |
2580 | else |
2812 | else |
2581 | { |
2813 | { |
2582 | SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ |
2814 | SV *waiter = newSVsv (coro_current); /* owned by signal av */ |
2583 | |
2815 | |
2584 | av_push (av, waiter); |
2816 | av_push (av, waiter); |
2585 | |
2817 | |
2586 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
2818 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
2587 | frame->prepare = prepare_schedule; |
2819 | frame->prepare = prepare_schedule; |
… | |
… | |
2826 | |
3058 | |
2827 | if (!svp) croak ("Time::HiRes is required"); |
3059 | if (!svp) croak ("Time::HiRes is required"); |
2828 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
3060 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
2829 | |
3061 | |
2830 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
3062 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
3063 | |
|
|
3064 | svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); |
|
|
3065 | u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp)); |
2831 | } |
3066 | } |
2832 | |
3067 | |
2833 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
3068 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
2834 | } |
3069 | } |
2835 | |
3070 | |
2836 | SV * |
3071 | SV * |
2837 | new (char *klass, ...) |
3072 | new (char *klass, ...) |
2838 | ALIAS: |
3073 | ALIAS: |
… | |
… | |
2913 | SV * |
3148 | SV * |
2914 | clone (Coro::State coro) |
3149 | clone (Coro::State coro) |
2915 | CODE: |
3150 | CODE: |
2916 | { |
3151 | { |
2917 | #if CORO_CLONE |
3152 | #if CORO_CLONE |
2918 | struct coro *ncoro = coro_clone (coro); |
3153 | struct coro *ncoro = coro_clone (aTHX_ coro); |
2919 | MAGIC *mg; |
3154 | MAGIC *mg; |
2920 | /* TODO: too much duplication */ |
3155 | /* TODO: too much duplication */ |
2921 | ncoro->hv = newHV (); |
3156 | ncoro->hv = newHV (); |
2922 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
3157 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
2923 | mg->mg_flags |= MGf_DUP; |
3158 | mg->mg_flags |= MGf_DUP; |
… | |
… | |
2985 | eval = 1 |
3220 | eval = 1 |
2986 | CODE: |
3221 | CODE: |
2987 | { |
3222 | { |
2988 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
3223 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
2989 | { |
3224 | { |
2990 | struct coro temp; |
3225 | struct coro *current = SvSTATE_current; |
2991 | |
3226 | |
2992 | if (!(coro->flags & CF_RUNNING)) |
3227 | if (current != coro) |
2993 | { |
3228 | { |
2994 | PUTBACK; |
3229 | PUTBACK; |
2995 | save_perl (aTHX_ &temp); |
3230 | save_perl (aTHX_ current); |
2996 | load_perl (aTHX_ coro); |
3231 | load_perl (aTHX_ coro); |
|
|
3232 | SPAGAIN; |
2997 | } |
3233 | } |
2998 | |
3234 | |
2999 | { |
|
|
3000 | dSP; |
|
|
3001 | ENTER; |
|
|
3002 | SAVETMPS; |
|
|
3003 | PUTBACK; |
|
|
3004 | PUSHSTACK; |
3235 | PUSHSTACK; |
|
|
3236 | |
3005 | PUSHMARK (SP); |
3237 | PUSHMARK (SP); |
|
|
3238 | PUTBACK; |
3006 | |
3239 | |
3007 | if (ix) |
3240 | if (ix) |
3008 | eval_sv (coderef, 0); |
3241 | eval_sv (coderef, 0); |
3009 | else |
3242 | else |
3010 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
3243 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
3011 | |
3244 | |
3012 | POPSTACK; |
3245 | POPSTACK; |
3013 | SPAGAIN; |
3246 | SPAGAIN; |
3014 | FREETMPS; |
|
|
3015 | LEAVE; |
|
|
3016 | PUTBACK; |
|
|
3017 | } |
|
|
3018 | |
3247 | |
3019 | if (!(coro->flags & CF_RUNNING)) |
3248 | if (current != coro) |
3020 | { |
3249 | { |
|
|
3250 | PUTBACK; |
3021 | save_perl (aTHX_ coro); |
3251 | save_perl (aTHX_ coro); |
3022 | load_perl (aTHX_ &temp); |
3252 | load_perl (aTHX_ current); |
3023 | SPAGAIN; |
3253 | SPAGAIN; |
3024 | } |
3254 | } |
3025 | } |
3255 | } |
3026 | } |
3256 | } |
3027 | |
3257 | |
… | |
… | |
3031 | ALIAS: |
3261 | ALIAS: |
3032 | is_ready = CF_READY |
3262 | is_ready = CF_READY |
3033 | is_running = CF_RUNNING |
3263 | is_running = CF_RUNNING |
3034 | is_new = CF_NEW |
3264 | is_new = CF_NEW |
3035 | is_destroyed = CF_DESTROYED |
3265 | is_destroyed = CF_DESTROYED |
|
|
3266 | is_suspended = CF_SUSPENDED |
3036 | CODE: |
3267 | CODE: |
3037 | RETVAL = boolSV (coro->flags & ix); |
3268 | RETVAL = boolSV (coro->flags & ix); |
3038 | OUTPUT: |
3269 | OUTPUT: |
3039 | RETVAL |
3270 | RETVAL |
3040 | |
3271 | |
… | |
… | |
3044 | CODE: |
3275 | CODE: |
3045 | { |
3276 | { |
3046 | struct coro *current = SvSTATE_current; |
3277 | struct coro *current = SvSTATE_current; |
3047 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3278 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3048 | SvREFCNT_dec (*throwp); |
3279 | SvREFCNT_dec (*throwp); |
|
|
3280 | SvGETMAGIC (throw); |
3049 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3281 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3050 | } |
3282 | } |
3051 | |
3283 | |
3052 | void |
3284 | void |
3053 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3285 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
… | |
… | |
3105 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
3337 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
3106 | |
3338 | |
3107 | SV *tmp = *src; *src = *dst; *dst = tmp; |
3339 | SV *tmp = *src; *src = *dst; *dst = tmp; |
3108 | } |
3340 | } |
3109 | |
3341 | |
|
|
3342 | void |
|
|
3343 | cancel (Coro::State self) |
|
|
3344 | CODE: |
|
|
3345 | coro_state_destroy (aTHX_ self); |
|
|
3346 | coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ |
|
|
3347 | |
|
|
3348 | |
|
|
3349 | SV * |
|
|
3350 | enable_times (int enabled = enable_times) |
|
|
3351 | CODE: |
|
|
3352 | { |
|
|
3353 | RETVAL = boolSV (enable_times); |
|
|
3354 | |
|
|
3355 | if (enabled != enable_times) |
|
|
3356 | { |
|
|
3357 | enable_times = enabled; |
|
|
3358 | |
|
|
3359 | coro_times_update (); |
|
|
3360 | (enabled ? coro_times_sub : coro_times_add)(SvSTATE (coro_current)); |
|
|
3361 | } |
|
|
3362 | } |
|
|
3363 | OUTPUT: |
|
|
3364 | RETVAL |
|
|
3365 | |
|
|
3366 | void |
|
|
3367 | times (Coro::State self) |
|
|
3368 | PPCODE: |
|
|
3369 | { |
|
|
3370 | struct coro *current = SvSTATE (coro_current); |
|
|
3371 | |
|
|
3372 | if (expect_false (current == self)) |
|
|
3373 | { |
|
|
3374 | coro_times_update (); |
|
|
3375 | coro_times_add (SvSTATE (coro_current)); |
|
|
3376 | } |
|
|
3377 | |
|
|
3378 | EXTEND (SP, 2); |
|
|
3379 | PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); |
|
|
3380 | PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); |
|
|
3381 | |
|
|
3382 | if (expect_false (current == self)) |
|
|
3383 | coro_times_sub (SvSTATE (coro_current)); |
|
|
3384 | } |
3110 | |
3385 | |
3111 | MODULE = Coro::State PACKAGE = Coro |
3386 | MODULE = Coro::State PACKAGE = Coro |
3112 | |
3387 | |
3113 | BOOT: |
3388 | BOOT: |
3114 | { |
3389 | { |
3115 | int i; |
|
|
3116 | |
|
|
3117 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3390 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3118 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3391 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3119 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
3392 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3120 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3393 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3121 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3394 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3122 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3395 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3123 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3396 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3124 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
3397 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
… | |
… | |
3129 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3402 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3130 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3403 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3131 | |
3404 | |
3132 | coro_stash = gv_stashpv ("Coro", TRUE); |
3405 | coro_stash = gv_stashpv ("Coro", TRUE); |
3133 | |
3406 | |
3134 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
3407 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3135 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
3408 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3136 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
3409 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
3137 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
3410 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (CORO_PRIO_LOW)); |
3138 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
3411 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (CORO_PRIO_IDLE)); |
3139 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
3412 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (CORO_PRIO_MIN)); |
3140 | |
|
|
3141 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
|
|
3142 | coro_ready[i] = newAV (); |
|
|
3143 | |
3413 | |
3144 | { |
3414 | { |
3145 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3415 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3146 | |
3416 | |
3147 | coroapi.schedule = api_schedule; |
3417 | coroapi.schedule = api_schedule; |
… | |
… | |
3188 | cede_notself (...) |
3458 | cede_notself (...) |
3189 | CODE: |
3459 | CODE: |
3190 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3460 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3191 | |
3461 | |
3192 | void |
3462 | void |
3193 | _cancel (Coro::State self) |
|
|
3194 | CODE: |
|
|
3195 | coro_state_destroy (aTHX_ self); |
|
|
3196 | coro_call_on_destroy (aTHX_ self); |
|
|
3197 | |
|
|
3198 | void |
|
|
3199 | _set_current (SV *current) |
3463 | _set_current (SV *current) |
3200 | PROTOTYPE: $ |
3464 | PROTOTYPE: $ |
3201 | CODE: |
3465 | CODE: |
3202 | SvREFCNT_dec (SvRV (coro_current)); |
3466 | SvREFCNT_dec (SvRV (coro_current)); |
3203 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
3467 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
… | |
… | |
3205 | void |
3469 | void |
3206 | _set_readyhook (SV *hook) |
3470 | _set_readyhook (SV *hook) |
3207 | PROTOTYPE: $ |
3471 | PROTOTYPE: $ |
3208 | CODE: |
3472 | CODE: |
3209 | SvREFCNT_dec (coro_readyhook); |
3473 | SvREFCNT_dec (coro_readyhook); |
|
|
3474 | SvGETMAGIC (hook); |
3210 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
3475 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
3211 | |
3476 | |
3212 | int |
3477 | int |
3213 | prio (Coro::State coro, int newprio = 0) |
3478 | prio (Coro::State coro, int newprio = 0) |
3214 | PROTOTYPE: $;$ |
3479 | PROTOTYPE: $;$ |
… | |
… | |
3221 | if (items > 1) |
3486 | if (items > 1) |
3222 | { |
3487 | { |
3223 | if (ix) |
3488 | if (ix) |
3224 | newprio = coro->prio - newprio; |
3489 | newprio = coro->prio - newprio; |
3225 | |
3490 | |
3226 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
3491 | if (newprio < CORO_PRIO_MIN) newprio = CORO_PRIO_MIN; |
3227 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
3492 | if (newprio > CORO_PRIO_MAX) newprio = CORO_PRIO_MAX; |
3228 | |
3493 | |
3229 | coro->prio = newprio; |
3494 | coro->prio = newprio; |
3230 | } |
3495 | } |
3231 | } |
3496 | } |
3232 | OUTPUT: |
3497 | OUTPUT: |
… | |
… | |
3245 | PROTOTYPE: |
3510 | PROTOTYPE: |
3246 | CODE: |
3511 | CODE: |
3247 | RETVAL = coro_nready; |
3512 | RETVAL = coro_nready; |
3248 | OUTPUT: |
3513 | OUTPUT: |
3249 | RETVAL |
3514 | RETVAL |
|
|
3515 | |
|
|
3516 | void |
|
|
3517 | suspend (Coro::State self) |
|
|
3518 | PROTOTYPE: $ |
|
|
3519 | CODE: |
|
|
3520 | self->flags |= CF_SUSPENDED; |
|
|
3521 | |
|
|
3522 | void |
|
|
3523 | resume (Coro::State self) |
|
|
3524 | PROTOTYPE: $ |
|
|
3525 | CODE: |
|
|
3526 | self->flags &= ~CF_SUSPENDED; |
3250 | |
3527 | |
3251 | void |
3528 | void |
3252 | _pool_handler (...) |
3529 | _pool_handler (...) |
3253 | CODE: |
3530 | CODE: |
3254 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
3531 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
… | |
… | |
3309 | rouse_wait (...) |
3586 | rouse_wait (...) |
3310 | PROTOTYPE: ;$ |
3587 | PROTOTYPE: ;$ |
3311 | PPCODE: |
3588 | PPCODE: |
3312 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3589 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3313 | |
3590 | |
|
|
3591 | void |
|
|
3592 | on_enter (SV *block) |
|
|
3593 | ALIAS: |
|
|
3594 | on_leave = 1 |
|
|
3595 | PROTOTYPE: & |
|
|
3596 | CODE: |
|
|
3597 | { |
|
|
3598 | struct coro *coro = SvSTATE_current; |
|
|
3599 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
|
|
3600 | |
|
|
3601 | block = (SV *)coro_sv_2cv (aTHX_ block); |
|
|
3602 | |
|
|
3603 | if (!*avp) |
|
|
3604 | *avp = newAV (); |
|
|
3605 | |
|
|
3606 | av_push (*avp, SvREFCNT_inc (block)); |
|
|
3607 | |
|
|
3608 | if (!ix) |
|
|
3609 | on_enterleave_call (aTHX_ block); |
|
|
3610 | |
|
|
3611 | LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */ |
|
|
3612 | SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro); |
|
|
3613 | ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */ |
|
|
3614 | } |
|
|
3615 | |
3314 | |
3616 | |
3315 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3617 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3316 | |
3618 | |
3317 | BOOT: |
3619 | BOOT: |
3318 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
3620 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
… | |
… | |
3321 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
3623 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
3322 | |
3624 | |
3323 | SV * |
3625 | SV * |
3324 | new (SV *klass, SV *count = 0) |
3626 | new (SV *klass, SV *count = 0) |
3325 | CODE: |
3627 | CODE: |
|
|
3628 | { |
|
|
3629 | int semcnt = 1; |
|
|
3630 | |
|
|
3631 | if (count) |
|
|
3632 | { |
|
|
3633 | SvGETMAGIC (count); |
|
|
3634 | |
|
|
3635 | if (SvOK (count)) |
|
|
3636 | semcnt = SvIV (count); |
|
|
3637 | } |
|
|
3638 | |
3326 | RETVAL = sv_bless ( |
3639 | RETVAL = sv_bless ( |
3327 | coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), |
3640 | coro_waitarray_new (aTHX_ semcnt), |
3328 | GvSTASH (CvGV (cv)) |
3641 | GvSTASH (CvGV (cv)) |
3329 | ); |
3642 | ); |
|
|
3643 | } |
3330 | OUTPUT: |
3644 | OUTPUT: |
3331 | RETVAL |
3645 | RETVAL |
3332 | |
3646 | |
3333 | # helper for Coro::Channel |
3647 | # helper for Coro::Channel and others |
3334 | SV * |
3648 | SV * |
3335 | _alloc (int count) |
3649 | _alloc (int count) |
3336 | CODE: |
3650 | CODE: |
3337 | RETVAL = coro_waitarray_new (aTHX_ count); |
3651 | RETVAL = coro_waitarray_new (aTHX_ count); |
3338 | OUTPUT: |
3652 | OUTPUT: |
… | |
… | |
3396 | for (i = 1; i <= wcount; ++i) |
3710 | for (i = 1; i <= wcount; ++i) |
3397 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
3711 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
3398 | } |
3712 | } |
3399 | } |
3713 | } |
3400 | |
3714 | |
|
|
3715 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
|
|
3716 | |
|
|
3717 | void |
|
|
3718 | _may_delete (SV *sem, int count, int extra_refs) |
|
|
3719 | PPCODE: |
|
|
3720 | { |
|
|
3721 | AV *av = (AV *)SvRV (sem); |
|
|
3722 | |
|
|
3723 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
|
|
3724 | && AvFILLp (av) == 0 /* no waiters, just count */ |
|
|
3725 | && SvIV (AvARRAY (av)[0]) == count) |
|
|
3726 | XSRETURN_YES; |
|
|
3727 | |
|
|
3728 | XSRETURN_NO; |
|
|
3729 | } |
|
|
3730 | |
3401 | MODULE = Coro::State PACKAGE = Coro::Signal |
3731 | MODULE = Coro::State PACKAGE = Coro::Signal |
3402 | |
3732 | |
3403 | SV * |
3733 | SV * |
3404 | new (SV *klass) |
3734 | new (SV *klass) |
3405 | CODE: |
3735 | CODE: |