ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.323 by root, Sat Nov 22 06:03:10 2008 UTC vs.
Revision 1.360 by root, Mon Jun 29 06:14:23 2009 UTC

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
160static void *coro_thx; 160static 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
164static double (*nvtime)(); /* so why doesn't it take void? */ 174static double (*nvtime)(); /* so why doesn't it take void? */
175static 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
168static OP *pp_slf (pTHX); 179static OP *pp_slf (pTHX);
169 180
175static HV *coro_state_stash, *coro_stash; 186static HV *coro_state_stash, *coro_stash;
176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 187static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
177 188
178static AV *av_destroy; /* destruction queue */ 189static AV *av_destroy; /* destruction queue */
179static SV *sv_manager; /* the manager coro */ 190static SV *sv_manager; /* the manager coro */
191static SV *sv_idle; /* $Coro::idle */
180 192
181static GV *irsgv; /* $/ */ 193static GV *irsgv; /* $/ */
182static GV *stdoutgv; /* *STDOUT */ 194static GV *stdoutgv; /* *STDOUT */
183static SV *rv_diehook; 195static SV *rv_diehook;
184static SV *rv_warnhook; 196static SV *rv_warnhook;
194static CV *cv_coro_state_new; 206static CV *cv_coro_state_new;
195 207
196/* Coro::AnyEvent */ 208/* Coro::AnyEvent */
197static SV *sv_activity; 209static SV *sv_activity;
198 210
211/* enable processtime/realtime profiling */
212static char enable_times;
213typedef U32 coro_ts[2];
214static coro_ts time_real, time_cpu;
215static char times_valid;
216
199static struct coro_cctx *cctx_first; 217static struct coro_cctx *cctx_first;
200static int cctx_count, cctx_idle; 218static int cctx_count, cctx_idle;
201 219
202enum { 220enum {
203 CC_MAPPED = 0x01, 221 CC_MAPPED = 0x01,
237enum { 255enum {
238 CF_RUNNING = 0x0001, /* coroutine is running */ 256 CF_RUNNING = 0x0001, /* coroutine is running */
239 CF_READY = 0x0002, /* coroutine is ready */ 257 CF_READY = 0x0002, /* coroutine is ready */
240 CF_NEW = 0x0004, /* has never been switched to */ 258 CF_NEW = 0x0004, /* has never been switched to */
241 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 */
242}; 261};
243 262
244/* 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 */
245typedef struct 264typedef struct
246{ 265{
247 SV *defsv; 266 SV *defsv;
248 AV *defav; 267 AV *defav;
249 SV *errsv; 268 SV *errsv;
250 SV *irsgv; 269 SV *irsgv;
270 HV *hinthv;
251#define VAR(name,type) type name; 271#define VAR(name,type) type name;
252# include "state.h" 272# include "state.h"
253#undef VAR 273#undef VAR
254} perl_slots; 274} perl_slots;
255 275
257 277
258/* this is a structure representing a perl-level coroutine */ 278/* this is a structure representing a perl-level coroutine */
259struct coro { 279struct coro {
260 /* the C coroutine allocated to this perl coroutine, if any */ 280 /* the C coroutine allocated to this perl coroutine, if any */
261 coro_cctx *cctx; 281 coro_cctx *cctx;
282
283 /* ready queue */
284 struct coro *next_ready;
262 285
263 /* state data */ 286 /* state data */
264 struct CoroSLF slf_frame; /* saved slf frame */ 287 struct CoroSLF slf_frame; /* saved slf frame */
265 AV *mainstack; 288 AV *mainstack;
266 perl_slots *slot; /* basically the saved sp */ 289 perl_slots *slot; /* basically the saved sp */
283 /* async_pool */ 306 /* async_pool */
284 SV *saved_deffh; 307 SV *saved_deffh;
285 SV *invoke_cb; 308 SV *invoke_cb;
286 AV *invoke_av; 309 AV *invoke_av;
287 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
288 /* linked list */ 318 /* linked list */
289 struct coro *next, *prev; 319 struct coro *next, *prev;
290}; 320};
291 321
292typedef struct coro *Coro__State; 322typedef struct coro *Coro__State;
297/* the mainr easonw e don't support windows process emulation */ 327/* the mainr easonw e don't support windows process emulation */
298static struct CoroSLF slf_frame; /* the current slf frame */ 328static struct CoroSLF slf_frame; /* the current slf frame */
299 329
300/** Coro ********************************************************************/ 330/** Coro ********************************************************************/
301 331
302#define PRIO_MAX 3 332#define CORO_PRIO_MAX 3
303#define PRIO_HIGH 1 333#define CORO_PRIO_HIGH 1
304#define PRIO_NORMAL 0 334#define CORO_PRIO_NORMAL 0
305#define PRIO_LOW -1 335#define CORO_PRIO_LOW -1
306#define PRIO_IDLE -3 336#define CORO_PRIO_IDLE -3
307#define PRIO_MIN -4 337#define CORO_PRIO_MIN -4
308 338
309/* for Coro.pm */ 339/* for Coro.pm */
310static SV *coro_current; 340static SV *coro_current;
311static SV *coro_readyhook; 341static SV *coro_readyhook;
312static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 342static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
313static CV *cv_coro_run, *cv_coro_terminate; 343static CV *cv_coro_run, *cv_coro_terminate;
314static struct coro *coro_first; 344static struct coro *coro_first;
315#define coro_nready coroapi.nready 345#define coro_nready coroapi.nready
316 346
317/** lowlevel stuff **********************************************************/ 347/** lowlevel stuff **********************************************************/
350INLINE CV * 380INLINE CV *
351coro_sv_2cv (pTHX_ SV *sv) 381coro_sv_2cv (pTHX_ SV *sv)
352{ 382{
353 HV *st; 383 HV *st;
354 GV *gvp; 384 GV *gvp;
355 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;
356} 391}
392
393INLINE void
394coro_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 UV tv[2];
408
409 u2time (aTHX_ &tv);
410 time_real [0] = tv [0];
411 time_real [1] = tv [1] * 1000;
412#endif
413}
414
415INLINE void
416coro_times_add (struct coro *c)
417{
418 c->t_real [1] += time_real [1];
419 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; }
420 c->t_real [0] += time_real [0];
421
422 c->t_cpu [1] += time_cpu [1];
423 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; }
424 c->t_cpu [0] += time_cpu [0];
425}
426
427INLINE void
428coro_times_sub (struct coro *c)
429{
430 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; }
431 c->t_real [1] -= time_real [1];
432 c->t_real [0] -= time_real [0];
433
434 if (c->t_cpu [1] < time_cpu [1]) { c->t_cpu [1] += 1000000000; --c->t_cpu [0]; }
435 c->t_cpu [1] -= time_cpu [1];
436 c->t_cpu [0] -= time_cpu [0];
437}
438
439/*****************************************************************************/
440/* magic glue */
441
442#define CORO_MAGIC_type_cv 26
443#define CORO_MAGIC_type_state PERL_MAGIC_ext
444
445#define CORO_MAGIC_NN(sv, type) \
446 (expect_true (SvMAGIC (sv)->mg_type == type) \
447 ? SvMAGIC (sv) \
448 : mg_find (sv, type))
449
450#define CORO_MAGIC(sv, type) \
451 (expect_true (SvMAGIC (sv)) \
452 ? CORO_MAGIC_NN (sv, type) \
453 : 0)
454
455#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
456#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
457
458INLINE struct coro *
459SvSTATE_ (pTHX_ SV *coro)
460{
461 HV *stash;
462 MAGIC *mg;
463
464 if (SvROK (coro))
465 coro = SvRV (coro);
466
467 if (expect_false (SvTYPE (coro) != SVt_PVHV))
468 croak ("Coro::State object required");
469
470 stash = SvSTASH (coro);
471 if (expect_false (stash != coro_stash && stash != coro_state_stash))
472 {
473 /* very slow, but rare, check */
474 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
475 croak ("Coro::State object required");
476 }
477
478 mg = CORO_MAGIC_state (coro);
479 return (struct coro *)mg->mg_ptr;
480}
481
482#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
483
484/* faster than SvSTATE, but expects a coroutine hv */
485#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
486#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
487
488/*****************************************************************************/
489/* padlist management and caching */
357 490
358static AV * 491static AV *
359coro_derive_padlist (pTHX_ CV *cv) 492coro_derive_padlist (pTHX_ CV *cv)
360{ 493{
361 AV *padlist = CvPADLIST (cv); 494 AV *padlist = CvPADLIST (cv);
369 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 502 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
370#endif 503#endif
371 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 504 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
372 --AvFILLp (padlist); 505 --AvFILLp (padlist);
373 506
374 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 507 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
375 av_store (newpadlist, 1, (SV *)newpad); 508 av_store (newpadlist, 1, (SV *)newpad);
376 509
377 return newpadlist; 510 return newpadlist;
378} 511}
379 512
380static void 513static void
381free_padlist (pTHX_ AV *padlist) 514free_padlist (pTHX_ AV *padlist)
382{ 515{
383 /* may be during global destruction */ 516 /* may be during global destruction */
384 if (SvREFCNT (padlist)) 517 if (!IN_DESTRUCT)
385 { 518 {
386 I32 i = AvFILLp (padlist); 519 I32 i = AvFILLp (padlist);
387 while (i >= 0) 520
521 while (i > 0) /* special-case index 0 */
388 { 522 {
389 SV **svp = av_fetch (padlist, i--, FALSE); 523 /* we try to be extra-careful here */
390 if (svp) 524 AV *av = (AV *)AvARRAY (padlist)[i--];
391 { 525 I32 j = AvFILLp (av);
392 SV *sv; 526
393 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 527 while (j >= 0)
528 SvREFCNT_dec (AvARRAY (av)[j--]);
529
530 AvFILLp (av) = -1;
394 SvREFCNT_dec (sv); 531 SvREFCNT_dec (av);
395
396 SvREFCNT_dec (*svp);
397 }
398 } 532 }
399 533
534 SvREFCNT_dec (AvARRAY (padlist)[0]);
535
536 AvFILLp (padlist) = -1;
400 SvREFCNT_dec ((SV*)padlist); 537 SvREFCNT_dec ((SV*)padlist);
401 } 538 }
402} 539}
403 540
404static int 541static int
405coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 542coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
406{ 543{
407 AV *padlist; 544 AV *padlist;
408 AV *av = (AV *)mg->mg_obj; 545 AV *av = (AV *)mg->mg_obj;
546
547 /* perl manages to free our internal AV and _then_ call us */
548 if (IN_DESTRUCT)
549 return;
409 550
410 /* casting is fun. */ 551 /* casting is fun. */
411 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 552 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
412 free_padlist (aTHX_ padlist); 553 free_padlist (aTHX_ padlist);
413 554
414 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 555 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
415 556
416 return 0; 557 return 0;
417} 558}
418
419#define CORO_MAGIC_type_cv 26
420#define CORO_MAGIC_type_state PERL_MAGIC_ext
421 559
422static MGVTBL coro_cv_vtbl = { 560static MGVTBL coro_cv_vtbl = {
423 0, 0, 0, 0, 561 0, 0, 0, 0,
424 coro_cv_free 562 coro_cv_free
425}; 563};
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
440INLINE struct coro *
441SvSTATE_ (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 564
470/* the next two functions merely cache the padlists */ 565/* the next two functions merely cache the padlists */
471static void 566static void
472get_padlist (pTHX_ CV *cv) 567get_padlist (pTHX_ CV *cv)
473{ 568{
501 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 596 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
502 597
503 av = (AV *)mg->mg_obj; 598 av = (AV *)mg->mg_obj;
504 599
505 if (expect_false (AvFILLp (av) >= AvMAX (av))) 600 if (expect_false (AvFILLp (av) >= AvMAX (av)))
506 av_extend (av, AvMAX (av) + 1); 601 av_extend (av, AvFILLp (av) + 1);
507 602
508 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 603 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
509} 604}
510 605
511/** load & save, init *******************************************************/ 606/** load & save, init *******************************************************/
607
608static void
609on_enterleave_call (pTHX_ SV *cb);
512 610
513static void 611static void
514load_perl (pTHX_ Coro__State c) 612load_perl (pTHX_ Coro__State c)
515{ 613{
516 perl_slots *slot = c->slot; 614 perl_slots *slot = c->slot;
517 c->slot = 0; 615 c->slot = 0;
518 616
519 PL_mainstack = c->mainstack; 617 PL_mainstack = c->mainstack;
520 618
521 GvSV (PL_defgv) = slot->defsv; 619 GvSV (PL_defgv) = slot->defsv;
522 GvAV (PL_defgv) = slot->defav; 620 GvAV (PL_defgv) = slot->defav;
523 GvSV (PL_errgv) = slot->errsv; 621 GvSV (PL_errgv) = slot->errsv;
524 GvSV (irsgv) = slot->irsgv; 622 GvSV (irsgv) = slot->irsgv;
623 GvHV (PL_hintgv) = slot->hinthv;
525 624
526 #define VAR(name,type) PL_ ## name = slot->name; 625 #define VAR(name,type) PL_ ## name = slot->name;
527 # include "state.h" 626 # include "state.h"
528 #undef VAR 627 #undef VAR
529 628
543 PUTBACK; 642 PUTBACK;
544 } 643 }
545 644
546 slf_frame = c->slf_frame; 645 slf_frame = c->slf_frame;
547 CORO_THROW = c->except; 646 CORO_THROW = c->except;
647
648 if (expect_false (enable_times))
649 {
650 if (expect_false (!times_valid))
651 coro_times_update ();
652
653 coro_times_sub (c);
654 }
655
656 if (expect_false (c->on_enter))
657 {
658 int i;
659
660 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
661 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
662 }
548} 663}
549 664
550static void 665static void
551save_perl (pTHX_ Coro__State c) 666save_perl (pTHX_ Coro__State c)
552{ 667{
668 if (expect_false (c->on_leave))
669 {
670 int i;
671
672 for (i = AvFILLp (c->on_leave); i >= 0; --i)
673 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
674 }
675
676 times_valid = 0;
677
678 if (expect_false (enable_times))
679 {
680 coro_times_update (); times_valid = 1;
681 coro_times_add (c);
682 }
683
553 c->except = CORO_THROW; 684 c->except = CORO_THROW;
554 c->slf_frame = slf_frame; 685 c->slf_frame = slf_frame;
555 686
556 { 687 {
557 dSP; 688 dSP;
570 { 701 {
571 while (expect_true (cxix >= 0)) 702 while (expect_true (cxix >= 0))
572 { 703 {
573 PERL_CONTEXT *cx = &ccstk[cxix--]; 704 PERL_CONTEXT *cx = &ccstk[cxix--];
574 705
575 if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)) 706 if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT))
576 { 707 {
577 CV *cv = cx->blk_sub.cv; 708 CV *cv = cx->blk_sub.cv;
578 709
579 if (expect_true (CvDEPTH (cv))) 710 if (expect_true (CvDEPTH (cv)))
580 { 711 {
615 c->mainstack = PL_mainstack; 746 c->mainstack = PL_mainstack;
616 747
617 { 748 {
618 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 749 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
619 750
620 slot->defav = GvAV (PL_defgv); 751 slot->defav = GvAV (PL_defgv);
621 slot->defsv = DEFSV; 752 slot->defsv = DEFSV;
622 slot->errsv = ERRSV; 753 slot->errsv = ERRSV;
623 slot->irsgv = GvSV (irsgv); 754 slot->irsgv = GvSV (irsgv);
755 slot->hinthv = GvHV (PL_hintgv);
624 756
625 #define VAR(name,type) slot->name = PL_ ## name; 757 #define VAR(name,type) slot->name = PL_ ## name;
626 # include "state.h" 758 # include "state.h"
627 #undef VAR 759 #undef VAR
628 } 760 }
755#endif 887#endif
756 888
757/* 889/*
758 * This overrides the default magic get method of %SIG elements. 890 * 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 891 * 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 892 * and instead of trying to save and restore the hash elements, we just provide
761 * readback here. 893 * 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 */ 894 */
766static int 895static int
767coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 896coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
768{ 897{
769 const char *s = MgPV_nolen_const (mg); 898 const char *s = MgPV_nolen_const (mg);
822 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 951 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
823 952
824 if (svp) 953 if (svp)
825 { 954 {
826 SV *old = *svp; 955 SV *old = *svp;
827 *svp = newSVsv (sv); 956 *svp = SvOK (sv) ? newSVsv (sv) : 0;
828 SvREFCNT_dec (old); 957 SvREFCNT_dec (old);
829 return 0; 958 return 0;
830 } 959 }
831 } 960 }
832 961
864 993
865 PL_runops = RUNOPS_DEFAULT; 994 PL_runops = RUNOPS_DEFAULT;
866 PL_curcop = &PL_compiling; 995 PL_curcop = &PL_compiling;
867 PL_in_eval = EVAL_NULL; 996 PL_in_eval = EVAL_NULL;
868 PL_comppad = 0; 997 PL_comppad = 0;
998 PL_comppad_name = 0;
999 PL_comppad_name_fill = 0;
1000 PL_comppad_name_floor = 0;
869 PL_curpm = 0; 1001 PL_curpm = 0;
870 PL_curpad = 0; 1002 PL_curpad = 0;
871 PL_localizing = 0; 1003 PL_localizing = 0;
872 PL_dirty = 0; 1004 PL_dirty = 0;
873 PL_restartop = 0; 1005 PL_restartop = 0;
874#if PERL_VERSION_ATLEAST (5,10,0) 1006#if PERL_VERSION_ATLEAST (5,10,0)
875 PL_parser = 0; 1007 PL_parser = 0;
876#endif 1008#endif
1009 PL_hints = 0;
877 1010
878 /* recreate the die/warn hooks */ 1011 /* recreate the die/warn hooks */
879 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 1012 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); 1013 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
881 1014
882 GvSV (PL_defgv) = newSV (0); 1015 GvSV (PL_defgv) = newSV (0);
883 GvAV (PL_defgv) = coro->args; coro->args = 0; 1016 GvAV (PL_defgv) = coro->args; coro->args = 0;
884 GvSV (PL_errgv) = newSV (0); 1017 GvSV (PL_errgv) = newSV (0);
885 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 1018 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
1019 GvHV (PL_hintgv) = 0;
886 PL_rs = newSVsv (GvSV (irsgv)); 1020 PL_rs = newSVsv (GvSV (irsgv));
887 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 1021 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
888 1022
889 { 1023 {
890 dSP; 1024 dSP;
916 1050
917 PL_op = (OP *)&coro_setup_op; 1051 PL_op = (OP *)&coro_setup_op;
918 1052
919 /* copy throw, in case it was set before coro_setup */ 1053 /* copy throw, in case it was set before coro_setup */
920 CORO_THROW = coro->except; 1054 CORO_THROW = coro->except;
921}
922 1055
1056 if (expect_false (enable_times))
1057 {
1058 coro_times_update ();
1059 coro_times_sub (coro);
1060 }
1061}
1062
923static void 1063static void
924coro_destruct (pTHX_ struct coro *coro) 1064coro_unwind_stacks (pTHX)
925{ 1065{
926 if (!IN_DESTRUCT) 1066 if (!IN_DESTRUCT)
927 { 1067 {
928 /* restore all saved variables and stuff */ 1068 /* restore all saved variables and stuff */
929 LEAVE_SCOPE (0); 1069 LEAVE_SCOPE (0);
937 POPSTACK_TO (PL_mainstack); 1077 POPSTACK_TO (PL_mainstack);
938 1078
939 /* unwind main stack */ 1079 /* unwind main stack */
940 dounwind (-1); 1080 dounwind (-1);
941 } 1081 }
1082}
942 1083
943 SvREFCNT_dec (GvSV (PL_defgv)); 1084static void
944 SvREFCNT_dec (GvAV (PL_defgv)); 1085coro_destruct_perl (pTHX_ struct coro *coro)
945 SvREFCNT_dec (GvSV (PL_errgv)); 1086{
946 SvREFCNT_dec (PL_defoutgv); 1087 SV *svf [9];
947 SvREFCNT_dec (PL_rs);
948 SvREFCNT_dec (GvSV (irsgv));
949 1088
950 SvREFCNT_dec (PL_diehook);
951 SvREFCNT_dec (PL_warnhook);
952 1089 {
1090 struct coro *current = SvSTATE_current;
1091
1092 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1093
1094 save_perl (aTHX_ current);
1095 load_perl (aTHX_ coro);
1096
1097 coro_unwind_stacks (aTHX);
1098 coro_destruct_stacks (aTHX);
1099
1100 // now save some sv's to be free'd later
1101 svf [0] = GvSV (PL_defgv);
1102 svf [1] = (SV *)GvAV (PL_defgv);
1103 svf [2] = GvSV (PL_errgv);
1104 svf [3] = (SV *)PL_defoutgv;
1105 svf [4] = PL_rs;
1106 svf [5] = GvSV (irsgv);
1107 svf [6] = (SV *)GvHV (PL_hintgv);
1108 svf [7] = PL_diehook;
1109 svf [8] = PL_warnhook;
1110 assert (9 == sizeof (svf) / sizeof (*svf));
1111
1112 load_perl (aTHX_ current);
1113 }
1114
1115 {
1116 int i;
1117
1118 for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i)
1119 SvREFCNT_dec (svf [i]);
1120
953 SvREFCNT_dec (coro->saved_deffh); 1121 SvREFCNT_dec (coro->saved_deffh);
954 SvREFCNT_dec (coro->rouse_cb); 1122 SvREFCNT_dec (coro->rouse_cb);
955 SvREFCNT_dec (coro->invoke_cb); 1123 SvREFCNT_dec (coro->invoke_cb);
956 SvREFCNT_dec (coro->invoke_av); 1124 SvREFCNT_dec (coro->invoke_av);
957 1125 }
958 coro_destruct_stacks (aTHX);
959} 1126}
960 1127
961INLINE void 1128INLINE void
962free_coro_mortal (pTHX) 1129free_coro_mortal (pTHX)
963{ 1130{
1168 /* 1335 /*
1169 * If perl-run returns we assume exit() was being called or the coro 1336 * If perl-run returns we assume exit() was being called or the coro
1170 * fell off the end, which seems to be the only valid (non-bug) 1337 * fell off the end, which seems to be the only valid (non-bug)
1171 * reason for perl_run to return. We try to exit by jumping to the 1338 * reason for perl_run to return. We try to exit by jumping to the
1172 * bootstrap-time "top" top_env, as we cannot restore the "main" 1339 * bootstrap-time "top" top_env, as we cannot restore the "main"
1173 * coroutine as Coro has no such concept 1340 * coroutine as Coro has no such concept.
1341 * This actually isn't valid with the pthread backend, but OSes requiring
1342 * that backend are too broken to do it in a standards-compliant way.
1174 */ 1343 */
1175 PL_top_env = main_top_env; 1344 PL_top_env = main_top_env;
1176 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1345 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1177 } 1346 }
1178} 1347}
1255cctx_destroy (coro_cctx *cctx) 1424cctx_destroy (coro_cctx *cctx)
1256{ 1425{
1257 if (!cctx) 1426 if (!cctx)
1258 return; 1427 return;
1259 1428
1260 assert (cctx != cctx_current);//D temporary 1429 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1261 1430
1262 --cctx_count; 1431 --cctx_count;
1263 coro_destroy (&cctx->cctx); 1432 coro_destroy (&cctx->cctx);
1264 1433
1265 /* coro_transfer creates new, empty cctx's */ 1434 /* coro_transfer creates new, empty cctx's */
1329 /* TODO: throwing up here is considered harmful */ 1498 /* TODO: throwing up here is considered harmful */
1330 1499
1331 if (expect_true (prev != next)) 1500 if (expect_true (prev != next))
1332 { 1501 {
1333 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1502 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1334 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); 1503 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1335 1504
1336 if (expect_false (next->flags & CF_RUNNING))
1337 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1338
1339 if (expect_false (next->flags & CF_DESTROYED)) 1505 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1340 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); 1506 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1341 1507
1342#if !PERL_VERSION_ATLEAST (5,10,0) 1508#if !PERL_VERSION_ATLEAST (5,10,0)
1343 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1509 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1344 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); 1510 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1345#endif 1511#endif
1393 )) 1559 ))
1394 { 1560 {
1395 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1561 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1396 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); 1562 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1397 1563
1398 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1564 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1399 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1565 /* without this the next cctx_get might destroy the running cctx while still in use */
1400 if (expect_false (CCTX_EXPIRED (cctx_current))) 1566 if (expect_false (CCTX_EXPIRED (cctx_current)))
1401 if (!next->cctx) 1567 if (expect_true (!next->cctx))
1402 next->cctx = cctx_get (aTHX); 1568 next->cctx = cctx_get (aTHX);
1403 1569
1404 cctx_put (cctx_current); 1570 cctx_put (cctx_current);
1405 assert (!prev->cctx);//D temporary
1406 } 1571 }
1407 else 1572 else
1408 prev->cctx = cctx_current; 1573 prev->cctx = cctx_current;
1409 1574
1410 ++next->usecount; 1575 ++next->usecount;
1434coro_state_destroy (pTHX_ struct coro *coro) 1599coro_state_destroy (pTHX_ struct coro *coro)
1435{ 1600{
1436 if (coro->flags & CF_DESTROYED) 1601 if (coro->flags & CF_DESTROYED)
1437 return 0; 1602 return 0;
1438 1603
1439 if (coro->on_destroy) 1604 if (coro->on_destroy && !PL_dirty)
1440 coro->on_destroy (aTHX_ coro); 1605 coro->on_destroy (aTHX_ coro);
1441 1606
1442 coro->flags |= CF_DESTROYED; 1607 coro->flags |= CF_DESTROYED;
1443 1608
1444 if (coro->flags & CF_READY) 1609 if (coro->flags & CF_READY)
1448 --coro_nready; 1613 --coro_nready;
1449 } 1614 }
1450 else 1615 else
1451 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1616 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1452 1617
1453 if (coro->mainstack && coro->mainstack != main_mainstack) 1618 if (coro->mainstack
1454 { 1619 && coro->mainstack != main_mainstack
1455 struct coro temp; 1620 && coro->slot
1456 1621 && !PL_dirty)
1457 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING)));
1458
1459 save_perl (aTHX_ &temp);
1460 load_perl (aTHX_ coro);
1461
1462 coro_destruct (aTHX_ coro); 1622 coro_destruct_perl (aTHX_ coro);
1463
1464 load_perl (aTHX_ &temp);
1465
1466 coro->slot = 0;
1467 }
1468 1623
1469 cctx_destroy (coro->cctx); 1624 cctx_destroy (coro->cctx);
1470 SvREFCNT_dec (coro->startcv); 1625 SvREFCNT_dec (coro->startcv);
1471 SvREFCNT_dec (coro->args); 1626 SvREFCNT_dec (coro->args);
1472 SvREFCNT_dec (CORO_THROW); 1627 SvREFCNT_dec (CORO_THROW);
1559/** Coro ********************************************************************/ 1714/** Coro ********************************************************************/
1560 1715
1561INLINE void 1716INLINE void
1562coro_enq (pTHX_ struct coro *coro) 1717coro_enq (pTHX_ struct coro *coro)
1563{ 1718{
1564 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); 1719 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN];
1565}
1566 1720
1567INLINE SV * 1721 SvREFCNT_inc_NN (coro->hv);
1722
1723 coro->next_ready = 0;
1724 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1725 ready [1] = coro;
1726}
1727
1728INLINE struct coro *
1568coro_deq (pTHX) 1729coro_deq (pTHX)
1569{ 1730{
1570 int prio; 1731 int prio;
1571 1732
1572 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1733 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; )
1573 if (AvFILLp (coro_ready [prio]) >= 0) 1734 {
1574 return av_shift (coro_ready [prio]); 1735 struct coro **ready = coro_ready [prio];
1736
1737 if (ready [0])
1738 {
1739 struct coro *coro = ready [0];
1740 ready [0] = coro->next_ready;
1741 return coro;
1742 }
1743 }
1575 1744
1576 return 0; 1745 return 0;
1577} 1746}
1578 1747
1579static int 1748static int
1581{ 1750{
1582 struct coro *coro; 1751 struct coro *coro;
1583 SV *sv_hook; 1752 SV *sv_hook;
1584 void (*xs_hook)(void); 1753 void (*xs_hook)(void);
1585 1754
1586 if (SvROK (coro_sv))
1587 coro_sv = SvRV (coro_sv);
1588
1589 coro = SvSTATE (coro_sv); 1755 coro = SvSTATE (coro_sv);
1590 1756
1591 if (coro->flags & CF_READY) 1757 if (coro->flags & CF_READY)
1592 return 0; 1758 return 0;
1593 1759
1646static void 1812static void
1647prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1813prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1648{ 1814{
1649 for (;;) 1815 for (;;)
1650 { 1816 {
1651 SV *next_sv = coro_deq (aTHX); 1817 struct coro *next = coro_deq (aTHX);
1652 1818
1653 if (expect_true (next_sv)) 1819 if (expect_true (next))
1654 { 1820 {
1655 struct coro *next = SvSTATE_hv (next_sv);
1656
1657 /* cannot transfer to destroyed coros, skip and look for next */ 1821 /* cannot transfer to destroyed coros, skip and look for next */
1658 if (expect_false (next->flags & CF_DESTROYED)) 1822 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1659 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1823 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1660 else 1824 else
1661 { 1825 {
1662 next->flags &= ~CF_READY; 1826 next->flags &= ~CF_READY;
1663 --coro_nready; 1827 --coro_nready;
1664 1828
1667 } 1831 }
1668 } 1832 }
1669 else 1833 else
1670 { 1834 {
1671 /* nothing to schedule: call the idle handler */ 1835 /* nothing to schedule: call the idle handler */
1836 if (SvROK (sv_idle)
1837 && SvOBJECT (SvRV (sv_idle)))
1838 {
1839 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1840 api_ready (aTHX_ SvRV (sv_idle));
1841 --coro_nready;
1842 }
1843 else
1844 {
1672 dSP; 1845 dSP;
1673 1846
1674 ENTER; 1847 ENTER;
1675 SAVETMPS; 1848 SAVETMPS;
1676 1849
1677 PUSHMARK (SP); 1850 PUSHMARK (SP);
1678 PUTBACK; 1851 PUTBACK;
1679 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1852 call_sv (sv_idle, G_VOID | G_DISCARD);
1680 1853
1681 FREETMPS; 1854 FREETMPS;
1682 LEAVE; 1855 LEAVE;
1856 }
1683 } 1857 }
1684 } 1858 }
1685} 1859}
1686 1860
1687INLINE void 1861INLINE void
1758static void 1932static void
1759api_trace (pTHX_ SV *coro_sv, int flags) 1933api_trace (pTHX_ SV *coro_sv, int flags)
1760{ 1934{
1761 struct coro *coro = SvSTATE (coro_sv); 1935 struct coro *coro = SvSTATE (coro_sv);
1762 1936
1937 if (coro->flags & CF_RUNNING)
1938 croak ("cannot enable tracing on a running coroutine, caught");
1939
1763 if (flags & CC_TRACE) 1940 if (flags & CC_TRACE)
1764 { 1941 {
1765 if (!coro->cctx) 1942 if (!coro->cctx)
1766 coro->cctx = cctx_new_run (); 1943 coro->cctx = cctx_new_run ();
1767 else if (!(coro->cctx->flags & CC_TRACE)) 1944 else if (!(coro->cctx->flags & CC_TRACE))
1768 croak ("cannot enable tracing on coroutine with custom stack,"); 1945 croak ("cannot enable tracing on coroutine with custom stack, caught");
1769 1946
1770 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1947 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1771 } 1948 }
1772 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1949 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1773 { 1950 {
1829 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 2006 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1830 api_ready (aTHX_ sv_manager); 2007 api_ready (aTHX_ sv_manager);
1831 2008
1832 frame->prepare = prepare_schedule; 2009 frame->prepare = prepare_schedule;
1833 frame->check = slf_check_repeat; 2010 frame->check = slf_check_repeat;
2011
2012 /* as a minor optimisation, we could unwind all stacks here */
2013 /* but that puts extra pressure on pp_slf, and is not worth much */
2014 /*coro_unwind_stacks (aTHX);*/
1834} 2015}
1835 2016
1836/*****************************************************************************/ 2017/*****************************************************************************/
1837/* async pool handler */ 2018/* async pool handler */
1838 2019
1921 SV *data = (SV *)GENSUB_ARG; 2102 SV *data = (SV *)GENSUB_ARG;
1922 2103
1923 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2104 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1924 { 2105 {
1925 /* first call, set args */ 2106 /* first call, set args */
2107 SV *coro = SvRV (data);
1926 AV *av = newAV (); 2108 AV *av = newAV ();
1927 SV *coro = SvRV (data);
1928 2109
1929 SvRV_set (data, (SV *)av); 2110 SvRV_set (data, (SV *)av);
1930 api_ready (aTHX_ coro);
1931 SvREFCNT_dec (coro);
1932 2111
1933 /* better take a full copy of the arguments */ 2112 /* better take a full copy of the arguments */
1934 while (items--) 2113 while (items--)
1935 av_store (av, items, newSVsv (ST (items))); 2114 av_store (av, items, newSVsv (ST (items)));
2115
2116 api_ready (aTHX_ coro);
2117 SvREFCNT_dec (coro);
1936 } 2118 }
1937 2119
1938 XSRETURN_EMPTY; 2120 XSRETURN_EMPTY;
1939} 2121}
1940 2122
1957 2139
1958 EXTEND (SP, AvFILLp (av) + 1); 2140 EXTEND (SP, AvFILLp (av) + 1);
1959 for (i = 0; i <= AvFILLp (av); ++i) 2141 for (i = 0; i <= AvFILLp (av); ++i)
1960 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2142 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1961 2143
1962 /* we have stolen the elements, so ste length to zero and free */ 2144 /* we have stolen the elements, so set length to zero and free */
1963 AvFILLp (av) = -1; 2145 AvFILLp (av) = -1;
1964 av_undef (av); 2146 av_undef (av);
1965 2147
1966 PUTBACK; 2148 PUTBACK;
1967 } 2149 }
2257 2439
2258 PL_op->op_ppaddr = pp_slf; 2440 PL_op->op_ppaddr = pp_slf;
2259 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2441 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2260 2442
2261 PL_op = (OP *)&slf_restore; 2443 PL_op = (OP *)&slf_restore;
2444}
2445
2446/*****************************************************************************/
2447/* dynamic wind */
2448
2449static void
2450on_enterleave_call (pTHX_ SV *cb)
2451{
2452 dSP;
2453
2454 PUSHSTACK;
2455
2456 PUSHMARK (SP);
2457 PUTBACK;
2458 call_sv (cb, G_VOID | G_DISCARD);
2459 SPAGAIN;
2460
2461 POPSTACK;
2462}
2463
2464static SV *
2465coro_avp_pop_and_free (pTHX_ AV **avp)
2466{
2467 AV *av = *avp;
2468 SV *res = av_pop (av);
2469
2470 if (AvFILLp (av) < 0)
2471 {
2472 *avp = 0;
2473 SvREFCNT_dec (av);
2474 }
2475
2476 return res;
2477}
2478
2479static void
2480coro_pop_on_enter (pTHX_ void *coro)
2481{
2482 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2483 SvREFCNT_dec (cb);
2484}
2485
2486static void
2487coro_pop_on_leave (pTHX_ void *coro)
2488{
2489 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2490 on_enterleave_call (aTHX_ sv_2mortal (cb));
2262} 2491}
2263 2492
2264/*****************************************************************************/ 2493/*****************************************************************************/
2265/* PerlIO::cede */ 2494/* PerlIO::cede */
2266 2495
2340/* Coro::Semaphore & Coro::Signal */ 2569/* Coro::Semaphore & Coro::Signal */
2341 2570
2342static SV * 2571static SV *
2343coro_waitarray_new (pTHX_ int count) 2572coro_waitarray_new (pTHX_ int count)
2344{ 2573{
2345 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ 2574 /* a waitarray=semaphore contains a counter IV in $sem->[0] and any waiters after that */
2346 AV *av = newAV (); 2575 AV *av = newAV ();
2347 SV **ary; 2576 SV **ary;
2348 2577
2349 /* unfortunately, building manually saves memory */ 2578 /* unfortunately, building manually saves memory */
2350 Newx (ary, 2, SV *); 2579 Newx (ary, 2, SV *);
2351 AvALLOC (av) = ary; 2580 AvALLOC (av) = ary;
2581#if PERL_VERSION_ATLEAST (5,10,0)
2352 /*AvARRAY (av) = ary;*/ 2582 AvARRAY (av) = ary;
2583#else
2353 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2584 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2585 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2586 SvPVX ((SV *)av) = (char *)ary;
2587#endif
2354 AvMAX (av) = 1; 2588 AvMAX (av) = 1;
2355 AvFILLp (av) = 0; 2589 AvFILLp (av) = 0;
2356 ary [0] = newSViv (count); 2590 ary [0] = newSViv (count);
2357 2591
2358 return newRV_noinc ((SV *)av); 2592 return newRV_noinc ((SV *)av);
2489 { 2723 {
2490 /* callback form */ 2724 /* callback form */
2491 AV *av = (AV *)SvRV (arg [0]); 2725 AV *av = (AV *)SvRV (arg [0]);
2492 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); 2726 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2493 2727
2494 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); 2728 av_push (av, SvREFCNT_inc_NN (cb_cv));
2495 2729
2496 if (SvIVX (AvARRAY (av)[0]) > 0) 2730 if (SvIVX (AvARRAY (av)[0]) > 0)
2497 coro_semaphore_adjust (aTHX_ av, 0); 2731 coro_semaphore_adjust (aTHX_ av, 0);
2498 2732
2499 frame->prepare = prepare_nop; 2733 frame->prepare = prepare_nop;
2523 AvARRAY (av)[0] = AvARRAY (av)[1]; 2757 AvARRAY (av)[0] = AvARRAY (av)[1];
2524 AvARRAY (av)[1] = cb; 2758 AvARRAY (av)[1] = cb;
2525 2759
2526 cb = av_shift (av); 2760 cb = av_shift (av);
2527 2761
2762 if (SvTYPE (cb) == SVt_PVCV)
2763 {
2764 dSP;
2765 PUSHMARK (SP);
2766 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2767 PUTBACK;
2768 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2769 }
2770 else
2771 {
2528 api_ready (aTHX_ cb); 2772 api_ready (aTHX_ cb);
2529 sv_setiv (cb, 0); /* signal waiter */ 2773 sv_setiv (cb, 0); /* signal waiter */
2774 }
2775
2530 SvREFCNT_dec (cb); 2776 SvREFCNT_dec (cb);
2531 2777
2532 --count; 2778 --count;
2533 } 2779 }
2534} 2780}
2543static void 2789static void
2544slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2790slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2545{ 2791{
2546 AV *av = (AV *)SvRV (arg [0]); 2792 AV *av = (AV *)SvRV (arg [0]);
2547 2793
2794 if (items >= 2)
2795 {
2796 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2797 av_push (av, SvREFCNT_inc_NN (cb_cv));
2798
2548 if (SvIVX (AvARRAY (av)[0])) 2799 if (SvIVX (AvARRAY (av)[0]))
2800 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */
2801
2802 frame->prepare = prepare_nop;
2803 frame->check = slf_check_nop;
2804 }
2805 else if (SvIVX (AvARRAY (av)[0]))
2549 { 2806 {
2550 SvIVX (AvARRAY (av)[0]) = 0; 2807 SvIVX (AvARRAY (av)[0]) = 0;
2551 frame->prepare = prepare_nop; 2808 frame->prepare = prepare_nop;
2552 frame->check = slf_check_nop; 2809 frame->check = slf_check_nop;
2553 } 2810 }
2554 else 2811 else
2555 { 2812 {
2556 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ 2813 SV *waiter = newSVsv (coro_current); /* owned by signal av */
2557 2814
2558 av_push (av, waiter); 2815 av_push (av, waiter);
2559 2816
2560 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ 2817 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */
2561 frame->prepare = prepare_schedule; 2818 frame->prepare = prepare_schedule;
2800 3057
2801 if (!svp) croak ("Time::HiRes is required"); 3058 if (!svp) croak ("Time::HiRes is required");
2802 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); 3059 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
2803 3060
2804 nvtime = INT2PTR (double (*)(), SvIV (*svp)); 3061 nvtime = INT2PTR (double (*)(), SvIV (*svp));
3062
3063 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
3064 u2time = INT2PTR (double (*)(), SvIV (*svp));
2805 } 3065 }
2806 3066
2807 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 3067 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
2808} 3068}
2809 3069
2810SV * 3070SV *
2811new (char *klass, ...) 3071new (char *klass, ...)
2812 ALIAS: 3072 ALIAS:
2887SV * 3147SV *
2888clone (Coro::State coro) 3148clone (Coro::State coro)
2889 CODE: 3149 CODE:
2890{ 3150{
2891#if CORO_CLONE 3151#if CORO_CLONE
2892 struct coro *ncoro = coro_clone (coro); 3152 struct coro *ncoro = coro_clone (aTHX_ coro);
2893 MAGIC *mg; 3153 MAGIC *mg;
2894 /* TODO: too much duplication */ 3154 /* TODO: too much duplication */
2895 ncoro->hv = newHV (); 3155 ncoro->hv = newHV ();
2896 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); 3156 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2897 mg->mg_flags |= MGf_DUP; 3157 mg->mg_flags |= MGf_DUP;
2959 eval = 1 3219 eval = 1
2960 CODE: 3220 CODE:
2961{ 3221{
2962 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3222 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2963 { 3223 {
2964 struct coro temp; 3224 struct coro *current = SvSTATE_current;
2965 3225
2966 if (!(coro->flags & CF_RUNNING)) 3226 if (current != coro)
2967 { 3227 {
2968 PUTBACK; 3228 PUTBACK;
2969 save_perl (aTHX_ &temp); 3229 save_perl (aTHX_ current);
2970 load_perl (aTHX_ coro); 3230 load_perl (aTHX_ coro);
3231 SPAGAIN;
2971 } 3232 }
2972 3233
2973 {
2974 dSP;
2975 ENTER;
2976 SAVETMPS;
2977 PUTBACK;
2978 PUSHSTACK; 3234 PUSHSTACK;
3235
2979 PUSHMARK (SP); 3236 PUSHMARK (SP);
3237 PUTBACK;
2980 3238
2981 if (ix) 3239 if (ix)
2982 eval_sv (coderef, 0); 3240 eval_sv (coderef, 0);
2983 else 3241 else
2984 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3242 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2985 3243
2986 POPSTACK; 3244 POPSTACK;
2987 SPAGAIN; 3245 SPAGAIN;
2988 FREETMPS;
2989 LEAVE;
2990 PUTBACK;
2991 }
2992 3246
2993 if (!(coro->flags & CF_RUNNING)) 3247 if (current != coro)
2994 { 3248 {
3249 PUTBACK;
2995 save_perl (aTHX_ coro); 3250 save_perl (aTHX_ coro);
2996 load_perl (aTHX_ &temp); 3251 load_perl (aTHX_ current);
2997 SPAGAIN; 3252 SPAGAIN;
2998 } 3253 }
2999 } 3254 }
3000} 3255}
3001 3256
3005 ALIAS: 3260 ALIAS:
3006 is_ready = CF_READY 3261 is_ready = CF_READY
3007 is_running = CF_RUNNING 3262 is_running = CF_RUNNING
3008 is_new = CF_NEW 3263 is_new = CF_NEW
3009 is_destroyed = CF_DESTROYED 3264 is_destroyed = CF_DESTROYED
3265 is_suspended = CF_SUSPENDED
3010 CODE: 3266 CODE:
3011 RETVAL = boolSV (coro->flags & ix); 3267 RETVAL = boolSV (coro->flags & ix);
3012 OUTPUT: 3268 OUTPUT:
3013 RETVAL 3269 RETVAL
3014 3270
3018 CODE: 3274 CODE:
3019{ 3275{
3020 struct coro *current = SvSTATE_current; 3276 struct coro *current = SvSTATE_current;
3021 SV **throwp = self == current ? &CORO_THROW : &self->except; 3277 SV **throwp = self == current ? &CORO_THROW : &self->except;
3022 SvREFCNT_dec (*throwp); 3278 SvREFCNT_dec (*throwp);
3279 SvGETMAGIC (throw);
3023 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3280 *throwp = SvOK (throw) ? newSVsv (throw) : 0;
3024} 3281}
3025 3282
3026void 3283void
3027api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3284api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3079 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3336 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3080 3337
3081 SV *tmp = *src; *src = *dst; *dst = tmp; 3338 SV *tmp = *src; *src = *dst; *dst = tmp;
3082 } 3339 }
3083 3340
3341void
3342cancel (Coro::State self)
3343 CODE:
3344 coro_state_destroy (aTHX_ self);
3345 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3346
3347
3348SV *
3349enable_times (int enabled = enable_times)
3350 CODE:
3351{
3352 RETVAL = boolSV (enable_times);
3353
3354 if (enabled != enable_times)
3355 {
3356 enable_times = enabled;
3357
3358 coro_times_update ();
3359 (enabled ? coro_times_sub : coro_times_add)(SvSTATE (coro_current));
3360 }
3361}
3362 OUTPUT:
3363 RETVAL
3364
3365void
3366times (Coro::State self)
3367 PPCODE:
3368{
3369 struct coro *current = SvSTATE (coro_current);
3370
3371 if (expect_false (current == self))
3372 {
3373 coro_times_update ();
3374 coro_times_add (SvSTATE (coro_current));
3375 }
3376
3377 EXTEND (SP, 2);
3378 PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9)));
3379 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9)));
3380
3381 if (expect_false (current == self))
3382 coro_times_sub (SvSTATE (coro_current));
3383}
3084 3384
3085MODULE = Coro::State PACKAGE = Coro 3385MODULE = Coro::State PACKAGE = Coro
3086 3386
3087BOOT: 3387BOOT:
3088{ 3388{
3089 int i;
3090
3091 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3389 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3092 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3390 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3093 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3391 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3094 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3392 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3095 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3393 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3096 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3394 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3097 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3395 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3098 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3396 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3397 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3099 3398
3100 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3399 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3101 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3400 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3102 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3401 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3103 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3402 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
3104 3403
3105 coro_stash = gv_stashpv ("Coro", TRUE); 3404 coro_stash = gv_stashpv ("Coro", TRUE);
3106 3405
3107 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 3406 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3108 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3407 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3109 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3408 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));
3110 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3409 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (CORO_PRIO_LOW));
3111 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3410 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (CORO_PRIO_IDLE));
3112 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3411 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (CORO_PRIO_MIN));
3113
3114 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3115 coro_ready[i] = newAV ();
3116 3412
3117 { 3413 {
3118 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3414 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3119 3415
3120 coroapi.schedule = api_schedule; 3416 coroapi.schedule = api_schedule;
3161cede_notself (...) 3457cede_notself (...)
3162 CODE: 3458 CODE:
3163 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3459 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3164 3460
3165void 3461void
3166_cancel (Coro::State self)
3167 CODE:
3168 coro_state_destroy (aTHX_ self);
3169 coro_call_on_destroy (aTHX_ self);
3170
3171void
3172_set_current (SV *current) 3462_set_current (SV *current)
3173 PROTOTYPE: $ 3463 PROTOTYPE: $
3174 CODE: 3464 CODE:
3175 SvREFCNT_dec (SvRV (coro_current)); 3465 SvREFCNT_dec (SvRV (coro_current));
3176 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3466 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3178void 3468void
3179_set_readyhook (SV *hook) 3469_set_readyhook (SV *hook)
3180 PROTOTYPE: $ 3470 PROTOTYPE: $
3181 CODE: 3471 CODE:
3182 SvREFCNT_dec (coro_readyhook); 3472 SvREFCNT_dec (coro_readyhook);
3473 SvGETMAGIC (hook);
3183 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; 3474 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
3184 3475
3185int 3476int
3186prio (Coro::State coro, int newprio = 0) 3477prio (Coro::State coro, int newprio = 0)
3187 PROTOTYPE: $;$ 3478 PROTOTYPE: $;$
3194 if (items > 1) 3485 if (items > 1)
3195 { 3486 {
3196 if (ix) 3487 if (ix)
3197 newprio = coro->prio - newprio; 3488 newprio = coro->prio - newprio;
3198 3489
3199 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 3490 if (newprio < CORO_PRIO_MIN) newprio = CORO_PRIO_MIN;
3200 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 3491 if (newprio > CORO_PRIO_MAX) newprio = CORO_PRIO_MAX;
3201 3492
3202 coro->prio = newprio; 3493 coro->prio = newprio;
3203 } 3494 }
3204} 3495}
3205 OUTPUT: 3496 OUTPUT:
3218 PROTOTYPE: 3509 PROTOTYPE:
3219 CODE: 3510 CODE:
3220 RETVAL = coro_nready; 3511 RETVAL = coro_nready;
3221 OUTPUT: 3512 OUTPUT:
3222 RETVAL 3513 RETVAL
3514
3515void
3516suspend (Coro::State self)
3517 PROTOTYPE: $
3518 CODE:
3519 self->flags |= CF_SUSPENDED;
3520
3521void
3522resume (Coro::State self)
3523 PROTOTYPE: $
3524 CODE:
3525 self->flags &= ~CF_SUSPENDED;
3223 3526
3224void 3527void
3225_pool_handler (...) 3528_pool_handler (...)
3226 CODE: 3529 CODE:
3227 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3530 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3282rouse_wait (...) 3585rouse_wait (...)
3283 PROTOTYPE: ;$ 3586 PROTOTYPE: ;$
3284 PPCODE: 3587 PPCODE:
3285 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3588 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3286 3589
3590void
3591on_enter (SV *block)
3592 ALIAS:
3593 on_leave = 1
3594 PROTOTYPE: &
3595 CODE:
3596{
3597 struct coro *coro = SvSTATE_current;
3598 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3599
3600 block = (SV *)coro_sv_2cv (aTHX_ block);
3601
3602 if (!*avp)
3603 *avp = newAV ();
3604
3605 av_push (*avp, SvREFCNT_inc (block));
3606
3607 if (!ix)
3608 on_enterleave_call (aTHX_ block);
3609
3610 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3611 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3612 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3613}
3614
3287 3615
3288MODULE = Coro::State PACKAGE = PerlIO::cede 3616MODULE = Coro::State PACKAGE = PerlIO::cede
3289 3617
3290BOOT: 3618BOOT:
3291 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3619 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3294MODULE = Coro::State PACKAGE = Coro::Semaphore 3622MODULE = Coro::State PACKAGE = Coro::Semaphore
3295 3623
3296SV * 3624SV *
3297new (SV *klass, SV *count = 0) 3625new (SV *klass, SV *count = 0)
3298 CODE: 3626 CODE:
3627{
3628 int semcnt = 1;
3629
3630 if (count)
3631 {
3632 SvGETMAGIC (count);
3633
3634 if (SvOK (count))
3635 semcnt = SvIV (count);
3636 }
3637
3299 RETVAL = sv_bless ( 3638 RETVAL = sv_bless (
3300 coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), 3639 coro_waitarray_new (aTHX_ semcnt),
3301 GvSTASH (CvGV (cv)) 3640 GvSTASH (CvGV (cv))
3302 ); 3641 );
3642}
3303 OUTPUT: 3643 OUTPUT:
3304 RETVAL 3644 RETVAL
3305 3645
3306# helper for Coro::Channel 3646# helper for Coro::Channel and others
3307SV * 3647SV *
3308_alloc (int count) 3648_alloc (int count)
3309 CODE: 3649 CODE:
3310 RETVAL = coro_waitarray_new (aTHX_ count); 3650 RETVAL = coro_waitarray_new (aTHX_ count);
3311 OUTPUT: 3651 OUTPUT:
3369 for (i = 1; i <= wcount; ++i) 3709 for (i = 1; i <= wcount; ++i)
3370 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3710 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3371 } 3711 }
3372} 3712}
3373 3713
3714MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3715
3716void
3717_may_delete (SV *sem, int count, int extra_refs)
3718 PPCODE:
3719{
3720 AV *av = (AV *)SvRV (sem);
3721
3722 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3723 && AvFILLp (av) == 0 /* no waiters, just count */
3724 && SvIV (AvARRAY (av)[0]) == count)
3725 XSRETURN_YES;
3726
3727 XSRETURN_NO;
3728}
3729
3374MODULE = Coro::State PACKAGE = Coro::Signal 3730MODULE = Coro::State PACKAGE = Coro::Signal
3375 3731
3376SV * 3732SV *
3377new (SV *klass) 3733new (SV *klass)
3378 CODE: 3734 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines