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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines