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.287 by root, Mon Nov 17 07:03:12 2008 UTC vs.
Revision 1.330 by root, Wed Nov 26 09:29:06 2008 UTC

16 16
17#ifdef WIN32 17#ifdef WIN32
18# undef setjmp 18# undef setjmp
19# undef longjmp 19# undef longjmp
20# undef _exit 20# undef _exit
21# define setjmp _setjmp // deep magic, don't ask 21# define setjmp _setjmp /* deep magic */
22#else 22#else
23# include <inttypes.h> /* most portable stdint.h */ 23# include <inttypes.h> /* most portable stdint.h */
24#endif 24#endif
25 25
26#ifdef HAVE_MMAP 26#ifdef HAVE_MMAP
55 55
56#define PERL_VERSION_ATLEAST(a,b,c) \ 56#define PERL_VERSION_ATLEAST(a,b,c) \
57 (PERL_REVISION > (a) \ 57 (PERL_REVISION > (a) \
58 || (PERL_REVISION == (a) \ 58 || (PERL_REVISION == (a) \
59 && (PERL_VERSION > (b) \ 59 && (PERL_VERSION > (b) \
60 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) 60 || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c)))))
61 61
62#if !PERL_VERSION_ATLEAST (5,6,0) 62#if !PERL_VERSION_ATLEAST (5,6,0)
63# ifndef PL_ppaddr 63# ifndef PL_ppaddr
64# define PL_ppaddr ppaddr 64# define PL_ppaddr ppaddr
65# endif 65# endif
95# define GV_NOTQUAL 0 95# define GV_NOTQUAL 0
96#endif 96#endif
97#ifndef newSV 97#ifndef newSV
98# define newSV(l) NEWSV(0,l) 98# define newSV(l) NEWSV(0,l)
99#endif 99#endif
100#ifndef CvISXSUB_on
101# define CvISXSUB_on(cv) (void)cv
102#endif
103#ifndef CvISXSUB
104# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE)
105#endif
106#ifndef Newx
107# define Newx(ptr,nitems,type) New (0,ptr,nitems,type)
108#endif
100 109
101/* 5.8.7 */ 110/* 5.8.7 */
102#ifndef SvRV_set 111#ifndef SvRV_set
103# define SvRV_set(s,v) SvRV(s) = (v) 112# define SvRV_set(s,v) SvRV(s) = (v)
104#endif 113#endif
117#endif 126#endif
118 127
119/* The next macros try to return the current stack pointer, in an as 128/* The next macros try to return the current stack pointer, in an as
120 * portable way as possible. */ 129 * portable way as possible. */
121#if __GNUC__ >= 4 130#if __GNUC__ >= 4
131# define dSTACKLEVEL int stacklevel_dummy
122# define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0) 132# define STACKLEVEL __builtin_frame_address (0)
123#else 133#else
124# define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel 134# define dSTACKLEVEL volatile void *stacklevel
135# define STACKLEVEL ((void *)&stacklevel)
125#endif 136#endif
126 137
127#define IN_DESTRUCT (PL_main_cv == Nullcv) 138#define IN_DESTRUCT (PL_main_cv == Nullcv)
128 139
129#if __GNUC__ >= 3 140#if __GNUC__ >= 3
140#define expect_true(expr) expect ((expr) != 0, 1) 151#define expect_true(expr) expect ((expr) != 0, 1)
141 152
142#define NOINLINE attribute ((noinline)) 153#define NOINLINE attribute ((noinline))
143 154
144#include "CoroAPI.h" 155#include "CoroAPI.h"
156#define GCoroAPI (&coroapi) /* very sneaky */
145 157
146#ifdef USE_ITHREADS 158#ifdef USE_ITHREADS
147# if CORO_PTHREAD 159# if CORO_PTHREAD
148static void *coro_thx; 160static void *coro_thx;
149# endif 161# endif
150#endif 162#endif
151 163
152static double (*nvtime)(); /* so why doesn't it take void? */ 164static double (*nvtime)(); /* so why doesn't it take void? */
165
166/* we hijack an hopefully unused CV flag for our purposes */
167#define CVf_SLF 0x4000
168static OP *pp_slf (pTHX);
153 169
154static U32 cctx_gen; 170static U32 cctx_gen;
155static size_t cctx_stacksize = CORO_STACKSIZE; 171static size_t cctx_stacksize = CORO_STACKSIZE;
156static struct CoroAPI coroapi; 172static struct CoroAPI coroapi;
157static AV *main_mainstack; /* used to differentiate between $main and others */ 173static AV *main_mainstack; /* used to differentiate between $main and others */
158static JMPENV *main_top_env; 174static JMPENV *main_top_env;
159static HV *coro_state_stash, *coro_stash; 175static HV *coro_state_stash, *coro_stash;
160static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
161 177
178static AV *av_destroy; /* destruction queue */
179static SV *sv_manager; /* the manager coro */
180static SV *sv_idle; /* $Coro::idle */
181
162static GV *irsgv; /* $/ */ 182static GV *irsgv; /* $/ */
163static GV *stdoutgv; /* *STDOUT */ 183static GV *stdoutgv; /* *STDOUT */
164static SV *rv_diehook; 184static SV *rv_diehook;
165static SV *rv_warnhook; 185static SV *rv_warnhook;
166static HV *hv_sig; /* %SIG */ 186static HV *hv_sig; /* %SIG */
167 187
168/* async_pool helper stuff */ 188/* async_pool helper stuff */
169static SV *sv_pool_rss; 189static SV *sv_pool_rss;
170static SV *sv_pool_size; 190static SV *sv_pool_size;
191static SV *sv_async_pool_idle; /* description string */
171static AV *av_async_pool; 192static AV *av_async_pool; /* idle pool */
193static SV *sv_Coro; /* class string */
194static CV *cv_pool_handler;
195static CV *cv_coro_state_new;
172 196
173/* Coro::AnyEvent */ 197/* Coro::AnyEvent */
174static SV *sv_activity; 198static SV *sv_activity;
175 199
176static struct coro_cctx *cctx_first; 200static struct coro_cctx *cctx_first;
205 int valgrind_id; 229 int valgrind_id;
206#endif 230#endif
207 unsigned char flags; 231 unsigned char flags;
208} coro_cctx; 232} coro_cctx;
209 233
234coro_cctx *cctx_current; /* the currently running cctx */
235
236/*****************************************************************************/
237
210enum { 238enum {
211 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
212 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
213 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
214 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 242 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
219{ 247{
220 SV *defsv; 248 SV *defsv;
221 AV *defav; 249 AV *defav;
222 SV *errsv; 250 SV *errsv;
223 SV *irsgv; 251 SV *irsgv;
252 HV *hinthv;
224#define VAR(name,type) type name; 253#define VAR(name,type) type name;
225# include "state.h" 254# include "state.h"
226#undef VAR 255#undef VAR
227} perl_slots; 256} perl_slots;
228 257
236 /* state data */ 265 /* state data */
237 struct CoroSLF slf_frame; /* saved slf frame */ 266 struct CoroSLF slf_frame; /* saved slf frame */
238 AV *mainstack; 267 AV *mainstack;
239 perl_slots *slot; /* basically the saved sp */ 268 perl_slots *slot; /* basically the saved sp */
240 269
270 CV *startcv; /* the CV to execute */
241 AV *args; /* data associated with this coroutine (initial args) */ 271 AV *args; /* data associated with this coroutine (initial args) */
242 int refcnt; /* coroutines are refcounted, yes */ 272 int refcnt; /* coroutines are refcounted, yes */
243 int flags; /* CF_ flags */ 273 int flags; /* CF_ flags */
244 HV *hv; /* the perl hash associated with this coro, if any */ 274 HV *hv; /* the perl hash associated with this coro, if any */
245 void (*on_destroy)(pTHX_ struct coro *coro); 275 void (*on_destroy)(pTHX_ struct coro *coro);
246 276
247 /* statistics */ 277 /* statistics */
248 int usecount; /* number of transfers to this coro */ 278 int usecount; /* number of transfers to this coro */
249 279
250 /* coro process data */ 280 /* coro process data */
251 int prio; 281 int prio;
252 SV *throw; /* exception to be thrown */ 282 SV *except; /* exception to be thrown */
283 SV *rouse_cb;
253 284
254 /* async_pool */ 285 /* async_pool */
255 SV *saved_deffh; 286 SV *saved_deffh;
287 SV *invoke_cb;
288 AV *invoke_av;
256 289
257 /* linked list */ 290 /* linked list */
258 struct coro *next, *prev; 291 struct coro *next, *prev;
259}; 292};
260 293
263 296
264/* the following variables are effectively part of the perl context */ 297/* the following variables are effectively part of the perl context */
265/* and get copied between struct coro and these variables */ 298/* and get copied between struct coro and these variables */
266/* the mainr easonw e don't support windows process emulation */ 299/* the mainr easonw e don't support windows process emulation */
267static struct CoroSLF slf_frame; /* the current slf frame */ 300static struct CoroSLF slf_frame; /* the current slf frame */
268static SV *coro_throw;
269 301
270/** Coro ********************************************************************/ 302/** Coro ********************************************************************/
271 303
272#define PRIO_MAX 3 304#define PRIO_MAX 3
273#define PRIO_HIGH 1 305#define PRIO_HIGH 1
278 310
279/* for Coro.pm */ 311/* for Coro.pm */
280static SV *coro_current; 312static SV *coro_current;
281static SV *coro_readyhook; 313static SV *coro_readyhook;
282static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 314static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
315static CV *cv_coro_run, *cv_coro_terminate;
283static struct coro *coro_first; 316static struct coro *coro_first;
284#define coro_nready coroapi.nready 317#define coro_nready coroapi.nready
285 318
286/** lowlevel stuff **********************************************************/ 319/** lowlevel stuff **********************************************************/
287 320
313 get_hv (name, create); 346 get_hv (name, create);
314#endif 347#endif
315 return get_hv (name, create); 348 return get_hv (name, create);
316} 349}
317 350
351/* may croak */
352INLINE CV *
353coro_sv_2cv (pTHX_ SV *sv)
354{
355 HV *st;
356 GV *gvp;
357 return sv_2cv (sv, &st, &gvp, 0);
358}
359
318static AV * 360static AV *
319coro_clone_padlist (pTHX_ CV *cv) 361coro_derive_padlist (pTHX_ CV *cv)
320{ 362{
321 AV *padlist = CvPADLIST (cv); 363 AV *padlist = CvPADLIST (cv);
322 AV *newpadlist, *newpad; 364 AV *newpadlist, *newpad;
323 365
324 newpadlist = newAV (); 366 newpadlist = newAV ();
344 if (SvREFCNT (padlist)) 386 if (SvREFCNT (padlist))
345 { 387 {
346 I32 i = AvFILLp (padlist); 388 I32 i = AvFILLp (padlist);
347 while (i >= 0) 389 while (i >= 0)
348 { 390 {
349 SV **svp = av_fetch (padlist, i--, FALSE); 391 SV **svp = AvARRAY (padlist)[i--];
350 if (svp) 392 if (svp)
351 { 393 {
352 SV *sv; 394 AvREAL_on (*svp);
353 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp)))
354 SvREFCNT_dec (sv); 395 av_undef (*svp);
355
356 SvREFCNT_dec (*svp); 396 SvREFCNT_dec (*svp);
357 } 397 }
358 } 398 }
359 399
360 SvREFCNT_dec ((SV*)padlist); 400 SvREFCNT_dec ((SV*)padlist);
439 else 479 else
440 { 480 {
441#if CORO_PREFER_PERL_FUNCTIONS 481#if CORO_PREFER_PERL_FUNCTIONS
442 /* this is probably cleaner? but also slower! */ 482 /* this is probably cleaner? but also slower! */
443 /* in practise, it seems to be less stable */ 483 /* in practise, it seems to be less stable */
444 CV *cp = Perl_cv_clone (cv); 484 CV *cp = Perl_cv_clone (aTHX_ cv);
445 CvPADLIST (cv) = CvPADLIST (cp); 485 CvPADLIST (cv) = CvPADLIST (cp);
446 CvPADLIST (cp) = 0; 486 CvPADLIST (cp) = 0;
447 SvREFCNT_dec (cp); 487 SvREFCNT_dec (cp);
448#else 488#else
449 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 489 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
450#endif 490#endif
451 } 491 }
452} 492}
453 493
454static void 494static void
461 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 501 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
462 502
463 av = (AV *)mg->mg_obj; 503 av = (AV *)mg->mg_obj;
464 504
465 if (expect_false (AvFILLp (av) >= AvMAX (av))) 505 if (expect_false (AvFILLp (av) >= AvMAX (av)))
466 av_extend (av, AvMAX (av) + 1); 506 av_extend (av, AvFILLp (av) + 1);
467 507
468 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 508 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
469} 509}
470 510
471/** load & save, init *******************************************************/ 511/** load & save, init *******************************************************/
476 perl_slots *slot = c->slot; 516 perl_slots *slot = c->slot;
477 c->slot = 0; 517 c->slot = 0;
478 518
479 PL_mainstack = c->mainstack; 519 PL_mainstack = c->mainstack;
480 520
481 GvSV (PL_defgv) = slot->defsv; 521 GvSV (PL_defgv) = slot->defsv;
482 GvAV (PL_defgv) = slot->defav; 522 GvAV (PL_defgv) = slot->defav;
483 GvSV (PL_errgv) = slot->errsv; 523 GvSV (PL_errgv) = slot->errsv;
484 GvSV (irsgv) = slot->irsgv; 524 GvSV (irsgv) = slot->irsgv;
525 GvHV (PL_hintgv) = slot->hinthv;
485 526
486 #define VAR(name,type) PL_ ## name = slot->name; 527 #define VAR(name,type) PL_ ## name = slot->name;
487 # include "state.h" 528 # include "state.h"
488 #undef VAR 529 #undef VAR
489 530
502 543
503 PUTBACK; 544 PUTBACK;
504 } 545 }
505 546
506 slf_frame = c->slf_frame; 547 slf_frame = c->slf_frame;
507 coro_throw = c->throw; 548 CORO_THROW = c->except;
508} 549}
509 550
510static void 551static void
511save_perl (pTHX_ Coro__State c) 552save_perl (pTHX_ Coro__State c)
512{ 553{
513 c->throw = coro_throw; 554 c->except = CORO_THROW;
514 c->slf_frame = slf_frame; 555 c->slf_frame = slf_frame;
515 556
516 { 557 {
517 dSP; 558 dSP;
518 I32 cxix = cxstack_ix; 559 I32 cxix = cxstack_ix;
575 c->mainstack = PL_mainstack; 616 c->mainstack = PL_mainstack;
576 617
577 { 618 {
578 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 619 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
579 620
580 slot->defav = GvAV (PL_defgv); 621 slot->defav = GvAV (PL_defgv);
581 slot->defsv = DEFSV; 622 slot->defsv = DEFSV;
582 slot->errsv = ERRSV; 623 slot->errsv = ERRSV;
583 slot->irsgv = GvSV (irsgv); 624 slot->irsgv = GvSV (irsgv);
625 slot->hinthv = GvHV (PL_hintgv);
584 626
585 #define VAR(name,type) slot->name = PL_ ## name; 627 #define VAR(name,type) slot->name = PL_ ## name;
586 # include "state.h" 628 # include "state.h"
587 #undef VAR 629 #undef VAR
588 } 630 }
593 * of perl.c:init_stacks, except that it uses less memory 635 * of perl.c:init_stacks, except that it uses less memory
594 * on the (sometimes correct) assumption that coroutines do 636 * on the (sometimes correct) assumption that coroutines do
595 * not usually need a lot of stackspace. 637 * not usually need a lot of stackspace.
596 */ 638 */
597#if CORO_PREFER_PERL_FUNCTIONS 639#if CORO_PREFER_PERL_FUNCTIONS
598# define coro_init_stacks init_stacks 640# define coro_init_stacks(thx) init_stacks ()
599#else 641#else
600static void 642static void
601coro_init_stacks (pTHX) 643coro_init_stacks (pTHX)
602{ 644{
603 PL_curstackinfo = new_stackinfo(32, 8); 645 PL_curstackinfo = new_stackinfo(32, 8);
666#if !PERL_VERSION_ATLEAST (5,10,0) 708#if !PERL_VERSION_ATLEAST (5,10,0)
667 Safefree (PL_retstack); 709 Safefree (PL_retstack);
668#endif 710#endif
669} 711}
670 712
713#define CORO_RSS \
714 rss += sizeof (SYM (curstackinfo)); \
715 rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \
716 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \
717 rss += SYM (tmps_max) * sizeof (SV *); \
718 rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \
719 rss += SYM (scopestack_max) * sizeof (I32); \
720 rss += SYM (savestack_max) * sizeof (ANY);
721
671static size_t 722static size_t
672coro_rss (pTHX_ struct coro *coro) 723coro_rss (pTHX_ struct coro *coro)
673{ 724{
674 size_t rss = sizeof (*coro); 725 size_t rss = sizeof (*coro);
675 726
676 if (coro->mainstack) 727 if (coro->mainstack)
677 { 728 {
678 perl_slots tmp_slot;
679 perl_slots *slot;
680
681 if (coro->flags & CF_RUNNING) 729 if (coro->flags & CF_RUNNING)
682 { 730 {
683 slot = &tmp_slot; 731 #define SYM(sym) PL_ ## sym
684 732 CORO_RSS;
685 #define VAR(name,type) slot->name = PL_ ## name;
686 # include "state.h"
687 #undef VAR 733 #undef SYM
688 } 734 }
689 else 735 else
690 slot = coro->slot;
691
692 if (slot)
693 { 736 {
694 rss += sizeof (slot->curstackinfo); 737 #define SYM(sym) coro->slot->sym
695 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 738 CORO_RSS;
696 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 739 #undef SYM
697 rss += slot->tmps_max * sizeof (SV *);
698 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
699 rss += slot->scopestack_max * sizeof (I32);
700 rss += slot->savestack_max * sizeof (ANY);
701
702#if !PERL_VERSION_ATLEAST (5,10,0)
703 rss += slot->retstack_max * sizeof (OP *);
704#endif
705 } 740 }
706 } 741 }
707 742
708 return rss; 743 return rss;
709} 744}
811slf_check_nop (pTHX_ struct CoroSLF *frame) 846slf_check_nop (pTHX_ struct CoroSLF *frame)
812{ 847{
813 return 0; 848 return 0;
814} 849}
815 850
851static int
852slf_check_repeat (pTHX_ struct CoroSLF *frame)
853{
854 return 1;
855}
856
857static UNOP coro_setup_op;
858
816static void NOINLINE /* noinline to keep it out of the transfer fast path */ 859static void NOINLINE /* noinline to keep it out of the transfer fast path */
817coro_setup (pTHX_ struct coro *coro) 860coro_setup (pTHX_ struct coro *coro)
818{ 861{
819 /* 862 /*
820 * emulate part of the perl startup here. 863 * emulate part of the perl startup here.
823 866
824 PL_runops = RUNOPS_DEFAULT; 867 PL_runops = RUNOPS_DEFAULT;
825 PL_curcop = &PL_compiling; 868 PL_curcop = &PL_compiling;
826 PL_in_eval = EVAL_NULL; 869 PL_in_eval = EVAL_NULL;
827 PL_comppad = 0; 870 PL_comppad = 0;
871 PL_comppad_name = 0;
872 PL_comppad_name_fill = 0;
873 PL_comppad_name_floor = 0;
828 PL_curpm = 0; 874 PL_curpm = 0;
829 PL_curpad = 0; 875 PL_curpad = 0;
830 PL_localizing = 0; 876 PL_localizing = 0;
831 PL_dirty = 0; 877 PL_dirty = 0;
832 PL_restartop = 0; 878 PL_restartop = 0;
833#if PERL_VERSION_ATLEAST (5,10,0) 879#if PERL_VERSION_ATLEAST (5,10,0)
834 PL_parser = 0; 880 PL_parser = 0;
835#endif 881#endif
882 PL_hints = 0;
836 883
837 /* recreate the die/warn hooks */ 884 /* recreate the die/warn hooks */
838 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 885 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook );
839 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); 886 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
840 887
841 GvSV (PL_defgv) = newSV (0); 888 GvSV (PL_defgv) = newSV (0);
842 GvAV (PL_defgv) = coro->args; coro->args = 0; 889 GvAV (PL_defgv) = coro->args; coro->args = 0;
843 GvSV (PL_errgv) = newSV (0); 890 GvSV (PL_errgv) = newSV (0);
844 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 891 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
892 GvHV (PL_hintgv) = 0;
845 PL_rs = newSVsv (GvSV (irsgv)); 893 PL_rs = newSVsv (GvSV (irsgv));
846 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 894 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
847 895
848 { 896 {
849 dSP; 897 dSP;
850 UNOP myop; 898 UNOP myop;
851 899
852 Zero (&myop, 1, UNOP); 900 Zero (&myop, 1, UNOP);
853 myop.op_next = Nullop; 901 myop.op_next = Nullop;
902 myop.op_type = OP_ENTERSUB;
854 myop.op_flags = OPf_WANT_VOID; 903 myop.op_flags = OPf_WANT_VOID;
855 904
856 PUSHMARK (SP); 905 PUSHMARK (SP);
857 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 906 PUSHs ((SV *)coro->startcv);
858 PUTBACK; 907 PUTBACK;
859 PL_op = (OP *)&myop; 908 PL_op = (OP *)&myop;
860 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 909 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
861 SPAGAIN;
862 } 910 }
863 911
864 /* this newly created coroutine might be run on an existing cctx which most 912 /* this newly created coroutine might be run on an existing cctx which most
865 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. 913 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here.
866 */ 914 */
867 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 915 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
868 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 916 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
869 917
870 coro_throw = coro->throw; 918 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
919 coro_setup_op.op_next = PL_op;
920 coro_setup_op.op_type = OP_ENTERSUB;
921 coro_setup_op.op_ppaddr = pp_slf;
922 /* no flags etc. required, as an init function won't be called */
923
924 PL_op = (OP *)&coro_setup_op;
925
926 /* copy throw, in case it was set before coro_setup */
927 CORO_THROW = coro->except;
871} 928}
872 929
873static void 930static void
874coro_destruct (pTHX_ struct coro *coro) 931coro_destruct (pTHX_ struct coro *coro)
875{ 932{
894 SvREFCNT_dec (GvAV (PL_defgv)); 951 SvREFCNT_dec (GvAV (PL_defgv));
895 SvREFCNT_dec (GvSV (PL_errgv)); 952 SvREFCNT_dec (GvSV (PL_errgv));
896 SvREFCNT_dec (PL_defoutgv); 953 SvREFCNT_dec (PL_defoutgv);
897 SvREFCNT_dec (PL_rs); 954 SvREFCNT_dec (PL_rs);
898 SvREFCNT_dec (GvSV (irsgv)); 955 SvREFCNT_dec (GvSV (irsgv));
956 SvREFCNT_dec (GvHV (PL_hintgv));
899 957
900 SvREFCNT_dec (PL_diehook); 958 SvREFCNT_dec (PL_diehook);
901 SvREFCNT_dec (PL_warnhook); 959 SvREFCNT_dec (PL_warnhook);
902 960
903 SvREFCNT_dec (coro->saved_deffh); 961 SvREFCNT_dec (coro->saved_deffh);
904 SvREFCNT_dec (coro_throw); 962 SvREFCNT_dec (coro->rouse_cb);
963 SvREFCNT_dec (coro->invoke_cb);
964 SvREFCNT_dec (coro->invoke_av);
905 965
906 coro_destruct_stacks (aTHX); 966 coro_destruct_stacks (aTHX);
907} 967}
908 968
909INLINE void 969INLINE void
919static int 979static int
920runops_trace (pTHX) 980runops_trace (pTHX)
921{ 981{
922 COP *oldcop = 0; 982 COP *oldcop = 0;
923 int oldcxix = -2; 983 int oldcxix = -2;
924 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
925 coro_cctx *cctx = coro->cctx;
926 984
927 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 985 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
928 { 986 {
929 PERL_ASYNC_CHECK (); 987 PERL_ASYNC_CHECK ();
930 988
931 if (cctx->flags & CC_TRACE_ALL) 989 if (cctx_current->flags & CC_TRACE_ALL)
932 { 990 {
933 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 991 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
934 { 992 {
935 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 993 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
936 SV **bot, **top; 994 SV **bot, **top;
937 AV *av = newAV (); /* return values */ 995 AV *av = newAV (); /* return values */
938 SV **cb; 996 SV **cb;
975 1033
976 if (PL_curcop != &PL_compiling) 1034 if (PL_curcop != &PL_compiling)
977 { 1035 {
978 SV **cb; 1036 SV **cb;
979 1037
980 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1038 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
981 { 1039 {
982 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1040 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
983 1041
984 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1042 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
985 { 1043 {
986 runops_proc_t old_runops = PL_runops;
987 dSP; 1044 dSP;
988 GV *gv = CvGV (cx->blk_sub.cv); 1045 GV *gv = CvGV (cx->blk_sub.cv);
989 SV *fullname = sv_2mortal (newSV (0)); 1046 SV *fullname = sv_2mortal (newSV (0));
990 1047
991 if (isGV (gv)) 1048 if (isGV (gv))
996 SAVETMPS; 1053 SAVETMPS;
997 EXTEND (SP, 3); 1054 EXTEND (SP, 3);
998 PUSHMARK (SP); 1055 PUSHMARK (SP);
999 PUSHs (&PL_sv_yes); 1056 PUSHs (&PL_sv_yes);
1000 PUSHs (fullname); 1057 PUSHs (fullname);
1001 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 1058 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
1002 PUTBACK; 1059 PUTBACK;
1003 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 1060 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
1004 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1061 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1005 SPAGAIN; 1062 SPAGAIN;
1006 FREETMPS; 1063 FREETMPS;
1009 } 1066 }
1010 1067
1011 oldcxix = cxstack_ix; 1068 oldcxix = cxstack_ix;
1012 } 1069 }
1013 1070
1014 if (cctx->flags & CC_TRACE_LINE) 1071 if (cctx_current->flags & CC_TRACE_LINE)
1015 { 1072 {
1016 dSP; 1073 dSP;
1017 1074
1018 PL_runops = RUNOPS_DEFAULT; 1075 PL_runops = RUNOPS_DEFAULT;
1019 ENTER; 1076 ENTER;
1038 1095
1039 TAINT_NOT; 1096 TAINT_NOT;
1040 return 0; 1097 return 0;
1041} 1098}
1042 1099
1100static struct CoroSLF cctx_ssl_frame;
1101
1043static void 1102static void
1044prepare_set_stacklevel (struct coro_transfer_args *ta, struct coro_cctx *cctx) 1103slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1045{ 1104{
1046 ta->prev = (struct coro *)cctx;
1047 ta->next = 0; 1105 ta->prev = 0;
1048} 1106}
1049 1107
1050/* inject a fake call to Coro::State::_cctx_init into the execution */ 1108static int
1051/* _cctx_init should be careful, as it could be called at almost any time */ 1109slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1052/* during execution of a perl program */ 1110{
1053/* also initialises PL_top_env */ 1111 *frame = cctx_ssl_frame;
1112
1113 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1114}
1115
1116/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1054static void NOINLINE 1117static void NOINLINE
1055cctx_prepare (pTHX_ coro_cctx *cctx) 1118cctx_prepare (pTHX)
1056{ 1119{
1057 dSP;
1058 UNOP myop;
1059
1060 PL_top_env = &PL_start_env; 1120 PL_top_env = &PL_start_env;
1061 1121
1062 if (cctx->flags & CC_TRACE) 1122 if (cctx_current->flags & CC_TRACE)
1063 PL_runops = runops_trace; 1123 PL_runops = runops_trace;
1064 1124
1065 Zero (&myop, 1, UNOP); 1125 /* we already must be executing an SLF op, there is no other valid way
1066 myop.op_next = PL_op; 1126 * that can lead to creation of a new cctx */
1067 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1127 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1128 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1068 1129
1069 PUSHMARK (SP); 1130 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1070 EXTEND (SP, 2); 1131 cctx_ssl_frame = slf_frame;
1071 PUSHs (sv_2mortal (newSViv ((IV)cctx))); 1132
1072 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1133 slf_frame.prepare = slf_prepare_set_stacklevel;
1073 PUTBACK; 1134 slf_frame.check = slf_check_set_stacklevel;
1074 PL_op = (OP *)&myop;
1075 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1076 SPAGAIN;
1077} 1135}
1078 1136
1079/* the tail of transfer: execute stuff we can only do after a transfer */ 1137/* the tail of transfer: execute stuff we can only do after a transfer */
1080INLINE void 1138INLINE void
1081transfer_tail (pTHX) 1139transfer_tail (pTHX)
1100 /* normally we would need to skip the entersub here */ 1158 /* normally we would need to skip the entersub here */
1101 /* not doing so will re-execute it, which is exactly what we want */ 1159 /* not doing so will re-execute it, which is exactly what we want */
1102 /* PL_nop = PL_nop->op_next */ 1160 /* PL_nop = PL_nop->op_next */
1103 1161
1104 /* inject a fake subroutine call to cctx_init */ 1162 /* inject a fake subroutine call to cctx_init */
1105 cctx_prepare (aTHX_ (coro_cctx *)arg); 1163 cctx_prepare (aTHX);
1106 1164
1107 /* cctx_run is the alternative tail of transfer() */ 1165 /* cctx_run is the alternative tail of transfer() */
1108 transfer_tail (aTHX); 1166 transfer_tail (aTHX);
1109 1167
1110 /* somebody or something will hit me for both perl_run and PL_restartop */ 1168 /* somebody or something will hit me for both perl_run and PL_restartop */
1111 PL_restartop = PL_op; 1169 PL_restartop = PL_op;
1112 perl_run (PL_curinterp); 1170 perl_run (PL_curinterp);
1171 /*
1172 * Unfortunately, there is no way to get at the return values of the
1173 * coro body here, as perl_run destroys these
1174 */
1113 1175
1114 /* 1176 /*
1115 * If perl-run returns we assume exit() was being called or the coro 1177 * If perl-run returns we assume exit() was being called or the coro
1116 * fell off the end, which seems to be the only valid (non-bug) 1178 * fell off the end, which seems to be the only valid (non-bug)
1117 * reason for perl_run to return. We try to exit by jumping to the 1179 * reason for perl_run to return. We try to exit by jumping to the
1118 * bootstrap-time "top" top_env, as we cannot restore the "main" 1180 * bootstrap-time "top" top_env, as we cannot restore the "main"
1119 * coroutine as Coro has no such concept 1181 * coroutine as Coro has no such concept.
1182 * This actually isn't valid with the pthread backend, but OSes requiring
1183 * that backend are too broken to do it in a standards-compliant way.
1120 */ 1184 */
1121 PL_top_env = main_top_env; 1185 PL_top_env = main_top_env;
1122 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1186 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1123 } 1187 }
1124} 1188}
1201cctx_destroy (coro_cctx *cctx) 1265cctx_destroy (coro_cctx *cctx)
1202{ 1266{
1203 if (!cctx) 1267 if (!cctx)
1204 return; 1268 return;
1205 1269
1270 assert (cctx != cctx_current);//D temporary
1271
1206 --cctx_count; 1272 --cctx_count;
1207 coro_destroy (&cctx->cctx); 1273 coro_destroy (&cctx->cctx);
1208 1274
1209 /* coro_transfer creates new, empty cctx's */ 1275 /* coro_transfer creates new, empty cctx's */
1210 if (cctx->sptr) 1276 if (cctx->sptr)
1273 /* TODO: throwing up here is considered harmful */ 1339 /* TODO: throwing up here is considered harmful */
1274 1340
1275 if (expect_true (prev != next)) 1341 if (expect_true (prev != next))
1276 { 1342 {
1277 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1343 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1278 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); 1344 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,");
1279 1345
1280 if (expect_false (next->flags & CF_RUNNING)) 1346 if (expect_false (next->flags & CF_RUNNING))
1281 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); 1347 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1282 1348
1283 if (expect_false (next->flags & CF_DESTROYED)) 1349 if (expect_false (next->flags & CF_DESTROYED))
1295transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1361transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1296{ 1362{
1297 dSTACKLEVEL; 1363 dSTACKLEVEL;
1298 1364
1299 /* sometimes transfer is only called to set idle_sp */ 1365 /* sometimes transfer is only called to set idle_sp */
1300 if (expect_false (!next)) 1366 if (expect_false (!prev))
1301 { 1367 {
1302 ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; 1368 cctx_current->idle_sp = STACKLEVEL;
1303 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1369 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1304 } 1370 }
1305 else if (expect_true (prev != next)) 1371 else if (expect_true (prev != next))
1306 { 1372 {
1307 coro_cctx *prev__cctx; 1373 coro_cctx *cctx_prev;
1308 1374
1309 if (expect_false (prev->flags & CF_NEW)) 1375 if (expect_false (prev->flags & CF_NEW))
1310 { 1376 {
1311 /* create a new empty/source context */ 1377 /* create a new empty/source context */
1312 prev->cctx = cctx_new_empty ();
1313 prev->flags &= ~CF_NEW; 1378 prev->flags &= ~CF_NEW;
1314 prev->flags |= CF_RUNNING; 1379 prev->flags |= CF_RUNNING;
1315 } 1380 }
1316 1381
1317 prev->flags &= ~CF_RUNNING; 1382 prev->flags &= ~CF_RUNNING;
1328 coro_setup (aTHX_ next); 1393 coro_setup (aTHX_ next);
1329 } 1394 }
1330 else 1395 else
1331 load_perl (aTHX_ next); 1396 load_perl (aTHX_ next);
1332 1397
1333 prev__cctx = prev->cctx; 1398 assert (!prev->cctx);//D temporary
1334 1399
1335 /* possibly untie and reuse the cctx */ 1400 /* possibly untie and reuse the cctx */
1336 if (expect_true ( 1401 if (expect_true (
1337 prev__cctx->idle_sp == (void *)stacklevel 1402 cctx_current->idle_sp == STACKLEVEL
1338 && !(prev__cctx->flags & CC_TRACE) 1403 && !(cctx_current->flags & CC_TRACE)
1339 && !force_cctx 1404 && !force_cctx
1340 )) 1405 ))
1341 { 1406 {
1342 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1407 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1343 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); 1408 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1344 1409
1345 prev->cctx = 0;
1346
1347 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1410 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1348 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1411 /* without this the next cctx_get might destroy the running cctx while still in use */
1349 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1412 if (expect_false (CCTX_EXPIRED (cctx_current)))
1350 if (!next->cctx) 1413 if (expect_true (!next->cctx))
1351 next->cctx = cctx_get (aTHX); 1414 next->cctx = cctx_get (aTHX);
1352 1415
1353 cctx_put (prev__cctx); 1416 cctx_put (cctx_current);
1354 } 1417 }
1418 else
1419 prev->cctx = cctx_current;
1355 1420
1356 ++next->usecount; 1421 ++next->usecount;
1357 1422
1358 if (expect_true (!next->cctx)) 1423 cctx_prev = cctx_current;
1359 next->cctx = cctx_get (aTHX); 1424 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1360 1425
1361 if (expect_false (prev__cctx != next->cctx)) 1426 next->cctx = 0;
1427
1428 if (expect_false (cctx_prev != cctx_current))
1362 { 1429 {
1363 prev__cctx->top_env = PL_top_env; 1430 cctx_prev->top_env = PL_top_env;
1364 PL_top_env = next->cctx->top_env; 1431 PL_top_env = cctx_current->top_env;
1365 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1432 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1366 } 1433 }
1367 1434
1368 transfer_tail (aTHX); 1435 transfer_tail (aTHX);
1369 } 1436 }
1370} 1437}
1409 1476
1410 coro->slot = 0; 1477 coro->slot = 0;
1411 } 1478 }
1412 1479
1413 cctx_destroy (coro->cctx); 1480 cctx_destroy (coro->cctx);
1481 SvREFCNT_dec (coro->startcv);
1414 SvREFCNT_dec (coro->args); 1482 SvREFCNT_dec (coro->args);
1483 SvREFCNT_dec (CORO_THROW);
1415 1484
1416 if (coro->next) coro->next->prev = coro->prev; 1485 if (coro->next) coro->next->prev = coro->prev;
1417 if (coro->prev) coro->prev->next = coro->next; 1486 if (coro->prev) coro->prev->next = coro->next;
1418 if (coro == coro_first) coro_first = coro->next; 1487 if (coro == coro_first) coro_first = coro->next;
1419 1488
1473 1542
1474 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1543 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1475 TRANSFER (ta, 1); 1544 TRANSFER (ta, 1);
1476} 1545}
1477 1546
1547/*****************************************************************************/
1548/* gensub: simple closure generation utility */
1549
1550#define GENSUB_ARG CvXSUBANY (cv).any_ptr
1551
1552/* create a closure from XS, returns a code reference */
1553/* the arg can be accessed via GENSUB_ARG from the callback */
1554/* the callback must use dXSARGS/XSRETURN */
1555static SV *
1556gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
1557{
1558 CV *cv = (CV *)newSV (0);
1559
1560 sv_upgrade ((SV *)cv, SVt_PVCV);
1561
1562 CvANON_on (cv);
1563 CvISXSUB_on (cv);
1564 CvXSUB (cv) = xsub;
1565 GENSUB_ARG = arg;
1566
1567 return newRV_noinc ((SV *)cv);
1568}
1569
1478/** Coro ********************************************************************/ 1570/** Coro ********************************************************************/
1479 1571
1480INLINE void 1572INLINE void
1481coro_enq (pTHX_ struct coro *coro) 1573coro_enq (pTHX_ struct coro *coro)
1482{ 1574{
1500{ 1592{
1501 struct coro *coro; 1593 struct coro *coro;
1502 SV *sv_hook; 1594 SV *sv_hook;
1503 void (*xs_hook)(void); 1595 void (*xs_hook)(void);
1504 1596
1505 if (SvROK (coro_sv))
1506 coro_sv = SvRV (coro_sv);
1507
1508 coro = SvSTATE (coro_sv); 1597 coro = SvSTATE (coro_sv);
1509 1598
1510 if (coro->flags & CF_READY) 1599 if (coro->flags & CF_READY)
1511 return 0; 1600 return 0;
1512 1601
1525 ENTER; 1614 ENTER;
1526 SAVETMPS; 1615 SAVETMPS;
1527 1616
1528 PUSHMARK (SP); 1617 PUSHMARK (SP);
1529 PUTBACK; 1618 PUTBACK;
1530 call_sv (sv_hook, G_DISCARD); 1619 call_sv (sv_hook, G_VOID | G_DISCARD);
1531 SPAGAIN;
1532 1620
1533 FREETMPS; 1621 FREETMPS;
1534 LEAVE; 1622 LEAVE;
1535 } 1623 }
1536 1624
1544api_is_ready (pTHX_ SV *coro_sv) 1632api_is_ready (pTHX_ SV *coro_sv)
1545{ 1633{
1546 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1634 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1547} 1635}
1548 1636
1637/* expects to own a reference to next->hv */
1549INLINE void 1638INLINE void
1550prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1639prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1551{ 1640{
1552 SV *prev_sv, *next_sv;
1553
1554 for (;;)
1555 {
1556 next_sv = coro_deq (aTHX);
1557
1558 /* nothing to schedule: call the idle handler */
1559 if (expect_false (!next_sv))
1560 {
1561 dSP;
1562
1563 ENTER;
1564 SAVETMPS;
1565
1566 PUSHMARK (SP);
1567 PUTBACK;
1568 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1569 SPAGAIN;
1570
1571 FREETMPS;
1572 LEAVE;
1573 continue;
1574 }
1575
1576 ta->next = SvSTATE_hv (next_sv);
1577
1578 /* cannot transfer to destroyed coros, skip and look for next */
1579 if (expect_false (ta->next->flags & CF_DESTROYED))
1580 {
1581 SvREFCNT_dec (next_sv);
1582 /* coro_nready has already been taken care of by destroy */
1583 continue;
1584 }
1585
1586 --coro_nready;
1587 break;
1588 }
1589
1590 /* free this only after the transfer */
1591 prev_sv = SvRV (coro_current); 1641 SV *prev_sv = SvRV (coro_current);
1642
1592 ta->prev = SvSTATE_hv (prev_sv); 1643 ta->prev = SvSTATE_hv (prev_sv);
1644 ta->next = next;
1645
1593 TRANSFER_CHECK (*ta); 1646 TRANSFER_CHECK (*ta);
1594 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); 1647
1595 ta->next->flags &= ~CF_READY;
1596 SvRV_set (coro_current, next_sv); 1648 SvRV_set (coro_current, (SV *)next->hv);
1597 1649
1598 free_coro_mortal (aTHX); 1650 free_coro_mortal (aTHX);
1599 coro_mortal = prev_sv; 1651 coro_mortal = prev_sv;
1600} 1652}
1601 1653
1654static void
1655prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1656{
1657 for (;;)
1658 {
1659 SV *next_sv = coro_deq (aTHX);
1660
1661 if (expect_true (next_sv))
1662 {
1663 struct coro *next = SvSTATE_hv (next_sv);
1664
1665 /* cannot transfer to destroyed coros, skip and look for next */
1666 if (expect_false (next->flags & CF_DESTROYED))
1667 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1668 else
1669 {
1670 next->flags &= ~CF_READY;
1671 --coro_nready;
1672
1673 prepare_schedule_to (aTHX_ ta, next);
1674 break;
1675 }
1676 }
1677 else
1678 {
1679 /* nothing to schedule: call the idle handler */
1680 if (SvROK (sv_idle)
1681 && SvOBJECT (SvRV (sv_idle)))
1682 {
1683 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1684 api_ready (aTHX_ SvRV (sv_idle));
1685 --coro_nready;
1686 }
1687 else
1688 {
1689 dSP;
1690
1691 ENTER;
1692 SAVETMPS;
1693
1694 PUSHMARK (SP);
1695 PUTBACK;
1696 call_sv (sv_idle, G_VOID | G_DISCARD);
1697
1698 FREETMPS;
1699 LEAVE;
1700 }
1701 }
1702 }
1703}
1704
1602INLINE void 1705INLINE void
1603prepare_cede (pTHX_ struct coro_transfer_args *ta) 1706prepare_cede (pTHX_ struct coro_transfer_args *ta)
1604{ 1707{
1605 api_ready (aTHX_ coro_current); 1708 api_ready (aTHX_ coro_current);
1606 prepare_schedule (aTHX_ ta); 1709 prepare_schedule (aTHX_ ta);
1625{ 1728{
1626 struct coro_transfer_args ta; 1729 struct coro_transfer_args ta;
1627 1730
1628 prepare_schedule (aTHX_ &ta); 1731 prepare_schedule (aTHX_ &ta);
1629 TRANSFER (ta, 1); 1732 TRANSFER (ta, 1);
1733}
1734
1735static void
1736api_schedule_to (pTHX_ SV *coro_sv)
1737{
1738 struct coro_transfer_args ta;
1739 struct coro *next = SvSTATE (coro_sv);
1740
1741 SvREFCNT_inc_NN (coro_sv);
1742 prepare_schedule_to (aTHX_ &ta, next);
1630} 1743}
1631 1744
1632static int 1745static int
1633api_cede (pTHX) 1746api_cede (pTHX)
1634{ 1747{
1663static void 1776static void
1664api_trace (pTHX_ SV *coro_sv, int flags) 1777api_trace (pTHX_ SV *coro_sv, int flags)
1665{ 1778{
1666 struct coro *coro = SvSTATE (coro_sv); 1779 struct coro *coro = SvSTATE (coro_sv);
1667 1780
1781 if (coro->flags & CF_RUNNING)
1782 croak ("cannot enable tracing on a running coroutine, caught");
1783
1668 if (flags & CC_TRACE) 1784 if (flags & CC_TRACE)
1669 { 1785 {
1670 if (!coro->cctx) 1786 if (!coro->cctx)
1671 coro->cctx = cctx_new_run (); 1787 coro->cctx = cctx_new_run ();
1672 else if (!(coro->cctx->flags & CC_TRACE)) 1788 else if (!(coro->cctx->flags & CC_TRACE))
1673 croak ("cannot enable tracing on coroutine with custom stack,"); 1789 croak ("cannot enable tracing on coroutine with custom stack, caught");
1674 1790
1675 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1791 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1676 } 1792 }
1677 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1793 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1678 { 1794 {
1681 if (coro->flags & CF_RUNNING) 1797 if (coro->flags & CF_RUNNING)
1682 PL_runops = RUNOPS_DEFAULT; 1798 PL_runops = RUNOPS_DEFAULT;
1683 else 1799 else
1684 coro->slot->runops = RUNOPS_DEFAULT; 1800 coro->slot->runops = RUNOPS_DEFAULT;
1685 } 1801 }
1802}
1803
1804static void
1805coro_call_on_destroy (pTHX_ struct coro *coro)
1806{
1807 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0);
1808 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1809
1810 if (on_destroyp)
1811 {
1812 AV *on_destroy = (AV *)SvRV (*on_destroyp);
1813
1814 while (AvFILLp (on_destroy) >= 0)
1815 {
1816 dSP; /* don't disturb outer sp */
1817 SV *cb = av_pop (on_destroy);
1818
1819 PUSHMARK (SP);
1820
1821 if (statusp)
1822 {
1823 int i;
1824 AV *status = (AV *)SvRV (*statusp);
1825 EXTEND (SP, AvFILLp (status) + 1);
1826
1827 for (i = 0; i <= AvFILLp (status); ++i)
1828 PUSHs (AvARRAY (status)[i]);
1829 }
1830
1831 PUTBACK;
1832 call_sv (sv_2mortal (cb), G_VOID | G_DISCARD);
1833 }
1834 }
1835}
1836
1837static void
1838slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1839{
1840 int i;
1841 HV *hv = (HV *)SvRV (coro_current);
1842 AV *av = newAV ();
1843
1844 av_extend (av, items - 1);
1845 for (i = 0; i < items; ++i)
1846 av_push (av, SvREFCNT_inc_NN (arg [i]));
1847
1848 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
1849
1850 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1851 api_ready (aTHX_ sv_manager);
1852
1853 frame->prepare = prepare_schedule;
1854 frame->check = slf_check_repeat;
1855}
1856
1857/*****************************************************************************/
1858/* async pool handler */
1859
1860static int
1861slf_check_pool_handler (pTHX_ struct CoroSLF *frame)
1862{
1863 HV *hv = (HV *)SvRV (coro_current);
1864 struct coro *coro = (struct coro *)frame->data;
1865
1866 if (!coro->invoke_cb)
1867 return 1; /* loop till we have invoke */
1868 else
1869 {
1870 hv_store (hv, "desc", sizeof ("desc") - 1,
1871 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1872
1873 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1874
1875 {
1876 dSP;
1877 XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0;
1878 PUTBACK;
1879 }
1880
1881 SvREFCNT_dec (GvAV (PL_defgv));
1882 GvAV (PL_defgv) = coro->invoke_av;
1883 coro->invoke_av = 0;
1884
1885 return 0;
1886 }
1887}
1888
1889static void
1890slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1891{
1892 HV *hv = (HV *)SvRV (coro_current);
1893 struct coro *coro = SvSTATE_hv ((SV *)hv);
1894
1895 if (expect_true (coro->saved_deffh))
1896 {
1897 /* subsequent iteration */
1898 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1899 coro->saved_deffh = 0;
1900
1901 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
1902 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1903 {
1904 coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate);
1905 coro->invoke_av = newAV ();
1906
1907 frame->prepare = prepare_nop;
1908 }
1909 else
1910 {
1911 av_clear (GvAV (PL_defgv));
1912 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0);
1913
1914 coro->prio = 0;
1915
1916 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1917 api_trace (aTHX_ coro_current, 0);
1918
1919 frame->prepare = prepare_schedule;
1920 av_push (av_async_pool, SvREFCNT_inc (hv));
1921 }
1922 }
1923 else
1924 {
1925 /* first iteration, simply fall through */
1926 frame->prepare = prepare_nop;
1927 }
1928
1929 frame->check = slf_check_pool_handler;
1930 frame->data = (void *)coro;
1931}
1932
1933/*****************************************************************************/
1934/* rouse callback */
1935
1936#define CORO_MAGIC_type_rouse PERL_MAGIC_ext
1937
1938static void
1939coro_rouse_callback (pTHX_ CV *cv)
1940{
1941 dXSARGS;
1942 SV *data = (SV *)GENSUB_ARG;
1943
1944 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1945 {
1946 /* first call, set args */
1947 AV *av = newAV ();
1948 SV *coro = SvRV (data);
1949
1950 SvRV_set (data, (SV *)av);
1951 api_ready (aTHX_ coro);
1952 SvREFCNT_dec (coro);
1953
1954 /* better take a full copy of the arguments */
1955 while (items--)
1956 av_store (av, items, newSVsv (ST (items)));
1957 }
1958
1959 XSRETURN_EMPTY;
1960}
1961
1962static int
1963slf_check_rouse_wait (pTHX_ struct CoroSLF *frame)
1964{
1965 SV *data = (SV *)frame->data;
1966
1967 if (CORO_THROW)
1968 return 0;
1969
1970 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1971 return 1;
1972
1973 /* now push all results on the stack */
1974 {
1975 dSP;
1976 AV *av = (AV *)SvRV (data);
1977 int i;
1978
1979 EXTEND (SP, AvFILLp (av) + 1);
1980 for (i = 0; i <= AvFILLp (av); ++i)
1981 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1982
1983 /* we have stolen the elements, so ste length to zero and free */
1984 AvFILLp (av) = -1;
1985 av_undef (av);
1986
1987 PUTBACK;
1988 }
1989
1990 return 0;
1991}
1992
1993static void
1994slf_init_rouse_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1995{
1996 SV *cb;
1997
1998 if (items)
1999 cb = arg [0];
2000 else
2001 {
2002 struct coro *coro = SvSTATE_current;
2003
2004 if (!coro->rouse_cb)
2005 croak ("Coro::rouse_wait called without rouse callback, and no default rouse callback found either,");
2006
2007 cb = sv_2mortal (coro->rouse_cb);
2008 coro->rouse_cb = 0;
2009 }
2010
2011 if (!SvROK (cb)
2012 || SvTYPE (SvRV (cb)) != SVt_PVCV
2013 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
2014 croak ("Coro::rouse_wait called with illegal callback argument,");
2015
2016 {
2017 CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */
2018 SV *data = (SV *)GENSUB_ARG;
2019
2020 frame->data = (void *)data;
2021 frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule;
2022 frame->check = slf_check_rouse_wait;
2023 }
2024}
2025
2026static SV *
2027coro_new_rouse_cb (pTHX)
2028{
2029 HV *hv = (HV *)SvRV (coro_current);
2030 struct coro *coro = SvSTATE_hv (hv);
2031 SV *data = newRV_inc ((SV *)hv);
2032 SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data);
2033
2034 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0);
2035 SvREFCNT_dec (data); /* magicext increases the refcount */
2036
2037 SvREFCNT_dec (coro->rouse_cb);
2038 coro->rouse_cb = SvREFCNT_inc_NN (cb);
2039
2040 return cb;
1686} 2041}
1687 2042
1688/*****************************************************************************/ 2043/*****************************************************************************/
1689/* schedule-like-function opcode (SLF) */ 2044/* schedule-like-function opcode (SLF) */
1690 2045
1714 2069
1715 RETURNOP (slf_restore.op_first); 2070 RETURNOP (slf_restore.op_first);
1716} 2071}
1717 2072
1718static void 2073static void
1719slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1720{
1721 prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data);
1722}
1723
1724static void
1725slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1726{
1727 assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1));
1728
1729 frame->prepare = slf_prepare_set_stacklevel;
1730 frame->check = slf_check_nop;
1731 frame->data = (void *)SvIV (arg [0]);
1732}
1733
1734static void
1735slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) 2074slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta)
1736{ 2075{
1737 SV **arg = (SV **)slf_frame.data; 2076 SV **arg = (SV **)slf_frame.data;
1738 2077
1739 prepare_transfer (aTHX_ ta, arg [0], arg [1]); 2078 prepare_transfer (aTHX_ ta, arg [0], arg [1]);
1756 frame->prepare = prepare_schedule; 2095 frame->prepare = prepare_schedule;
1757 frame->check = slf_check_nop; 2096 frame->check = slf_check_nop;
1758} 2097}
1759 2098
1760static void 2099static void
2100slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta)
2101{
2102 struct coro *next = (struct coro *)slf_frame.data;
2103
2104 SvREFCNT_inc_NN (next->hv);
2105 prepare_schedule_to (aTHX_ ta, next);
2106}
2107
2108static void
2109slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2110{
2111 if (!items)
2112 croak ("Coro::schedule_to expects a coroutine argument, caught");
2113
2114 frame->data = (void *)SvSTATE (arg [0]);
2115 frame->prepare = slf_prepare_schedule_to;
2116 frame->check = slf_check_nop;
2117}
2118
2119static void
2120slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2121{
2122 api_ready (aTHX_ SvRV (coro_current));
2123
2124 slf_init_schedule_to (aTHX_ frame, cv, arg, items);
2125}
2126
2127static void
1761slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2128slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1762{ 2129{
1763 frame->prepare = prepare_cede; 2130 frame->prepare = prepare_cede;
1764 frame->check = slf_check_nop; 2131 frame->check = slf_check_nop;
1765} 2132}
1768slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2135slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1769{ 2136{
1770 frame->prepare = prepare_cede_notself; 2137 frame->prepare = prepare_cede_notself;
1771 frame->check = slf_check_nop; 2138 frame->check = slf_check_nop;
1772} 2139}
1773
1774/* we hijack an hopefully unused CV flag for our purposes */
1775#define CVf_SLF 0x4000
1776 2140
1777/* 2141/*
1778 * these not obviously related functions are all rolled into one 2142 * these not obviously related functions are all rolled into one
1779 * function to increase chances that they all will call transfer with the same 2143 * function to increase chances that they all will call transfer with the same
1780 * stack offset 2144 * stack offset
1836 } 2200 }
1837 while (slf_frame.check (aTHX_ &slf_frame)); 2201 while (slf_frame.check (aTHX_ &slf_frame));
1838 2202
1839 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ 2203 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */
1840 2204
1841 /* return value handling - mostly like entersub */
1842 {
1843 dSP;
1844 SV **bot = PL_stack_base + checkmark;
1845 int gimme = GIMME_V;
1846
1847 /* make sure we put something on the stack in scalar context */
1848 if (gimme == G_SCALAR)
1849 {
1850 if (sp == bot)
1851 XPUSHs (&PL_sv_undef);
1852
1853 SP = bot + 1;
1854 }
1855
1856 PUTBACK;
1857 }
1858
1859 /* exception handling */ 2205 /* exception handling */
1860 if (expect_false (coro_throw)) 2206 if (expect_false (CORO_THROW))
1861 { 2207 {
1862 SV *exception = sv_2mortal (coro_throw); 2208 SV *exception = sv_2mortal (CORO_THROW);
1863 2209
1864 coro_throw = 0; 2210 CORO_THROW = 0;
1865 sv_setsv (ERRSV, exception); 2211 sv_setsv (ERRSV, exception);
1866 croak (0); 2212 croak (0);
2213 }
2214
2215 /* return value handling - mostly like entersub */
2216 /* make sure we put something on the stack in scalar context */
2217 if (GIMME_V == G_SCALAR)
2218 {
2219 dSP;
2220 SV **bot = PL_stack_base + checkmark;
2221
2222 if (sp == bot) /* too few, push undef */
2223 bot [1] = &PL_sv_undef;
2224 else if (sp != bot + 1) /* too many, take last one */
2225 bot [1] = *sp;
2226
2227 SP = bot + 1;
2228
2229 PUTBACK;
1867 } 2230 }
1868 2231
1869 return NORMAL; 2232 return NORMAL;
1870} 2233}
1871 2234
1912 } 2275 }
1913 else 2276 else
1914 slf_argc = 0; 2277 slf_argc = 0;
1915 2278
1916 PL_op->op_ppaddr = pp_slf; 2279 PL_op->op_ppaddr = pp_slf;
1917 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2280 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
1918 2281
1919 PL_op = (OP *)&slf_restore; 2282 PL_op = (OP *)&slf_restore;
1920} 2283}
1921 2284
1922/*****************************************************************************/ 2285/*****************************************************************************/
1993 PerlIOBuf_get_cnt, 2356 PerlIOBuf_get_cnt,
1994 PerlIOBuf_set_ptrcnt, 2357 PerlIOBuf_set_ptrcnt,
1995}; 2358};
1996 2359
1997/*****************************************************************************/ 2360/*****************************************************************************/
2361/* Coro::Semaphore & Coro::Signal */
2362
2363static SV *
2364coro_waitarray_new (pTHX_ int count)
2365{
2366 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */
2367 AV *av = newAV ();
2368 SV **ary;
2369
2370 /* unfortunately, building manually saves memory */
2371 Newx (ary, 2, SV *);
2372 AvALLOC (av) = ary;
2373#if PERL_VERSION_ATLEAST (5,10,0)
2374 AvARRAY (av) = ary;
2375#else
2376 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2377 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2378 SvPVX ((SV *)av) = (char *)ary;
2379#endif
2380 AvMAX (av) = 1;
2381 AvFILLp (av) = 0;
2382 ary [0] = newSViv (count);
2383
2384 return newRV_noinc ((SV *)av);
2385}
2386
1998/* Coro::Semaphore */ 2387/* semaphore */
1999 2388
2000static void 2389static void
2001coro_semaphore_adjust (pTHX_ AV *av, IV adjust) 2390coro_semaphore_adjust (pTHX_ AV *av, IV adjust)
2002{ 2391{
2003 SV *count_sv = AvARRAY (av)[0]; 2392 SV *count_sv = AvARRAY (av)[0];
2015 AvARRAY (av)[0] = AvARRAY (av)[1]; 2404 AvARRAY (av)[0] = AvARRAY (av)[1];
2016 AvARRAY (av)[1] = count_sv; 2405 AvARRAY (av)[1] = count_sv;
2017 cb = av_shift (av); 2406 cb = av_shift (av);
2018 2407
2019 if (SvOBJECT (cb)) 2408 if (SvOBJECT (cb))
2409 {
2020 api_ready (aTHX_ cb); 2410 api_ready (aTHX_ cb);
2021 else 2411 --count;
2022 croak ("callbacks not yet supported"); 2412 }
2413 else if (SvTYPE (cb) == SVt_PVCV)
2414 {
2415 dSP;
2416 PUSHMARK (SP);
2417 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2418 PUTBACK;
2419 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2420 }
2023 2421
2024 SvREFCNT_dec (cb); 2422 SvREFCNT_dec (cb);
2025
2026 --count;
2027 } 2423 }
2028} 2424}
2029 2425
2030static void 2426static void
2031coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2427coro_semaphore_on_destroy (pTHX_ struct coro *coro)
2033 /* call $sem->adjust (0) to possibly wake up some other waiters */ 2429 /* call $sem->adjust (0) to possibly wake up some other waiters */
2034 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); 2430 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2035} 2431}
2036 2432
2037static int 2433static int
2038slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) 2434slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire)
2039{ 2435{
2040 AV *av = (AV *)frame->data; 2436 AV *av = (AV *)frame->data;
2041 SV *count_sv = AvARRAY (av)[0]; 2437 SV *count_sv = AvARRAY (av)[0];
2042 2438
2439 /* if we are about to throw, don't actually acquire the lock, just throw */
2440 if (CORO_THROW)
2441 return 0;
2043 if (SvIVX (count_sv) > 0) 2442 else if (SvIVX (count_sv) > 0)
2044 { 2443 {
2045 SvSTATE_current->on_destroy = 0; 2444 SvSTATE_current->on_destroy = 0;
2445
2446 if (acquire)
2046 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2447 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2448 else
2449 coro_semaphore_adjust (aTHX_ av, 0);
2450
2047 return 0; 2451 return 0;
2048 } 2452 }
2049 else 2453 else
2050 { 2454 {
2051 int i; 2455 int i;
2060 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2464 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2061 return 1; 2465 return 1;
2062 } 2466 }
2063} 2467}
2064 2468
2065static void 2469static int
2470slf_check_semaphore_down (pTHX_ struct CoroSLF *frame)
2471{
2472 return slf_check_semaphore_down_or_wait (aTHX_ frame, 1);
2473}
2474
2475static int
2476slf_check_semaphore_wait (pTHX_ struct CoroSLF *frame)
2477{
2478 return slf_check_semaphore_down_or_wait (aTHX_ frame, 0);
2479}
2480
2481static void
2066slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2482slf_init_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2067{ 2483{
2068 AV *av = (AV *)SvRV (arg [0]); 2484 AV *av = (AV *)SvRV (arg [0]);
2069 2485
2070 if (SvIVX (AvARRAY (av)[0]) > 0) 2486 if (SvIVX (AvARRAY (av)[0]) > 0)
2071 { 2487 {
2072 frame->data = (void *)av; 2488 frame->data = (void *)av;
2073 frame->prepare = prepare_nop; 2489 frame->prepare = prepare_nop;
2074 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2075 } 2490 }
2076 else 2491 else
2077 { 2492 {
2078 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2493 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2079 2494
2080 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); 2495 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2081 frame->prepare = prepare_schedule; 2496 frame->prepare = prepare_schedule;
2082 2497
2083 /* to avoid race conditions when a woken-up coro gets terminated */ 2498 /* to avoid race conditions when a woken-up coro gets terminated */
2084 /* we arrange for a temporary on_destroy that calls adjust (0) */ 2499 /* we arrange for a temporary on_destroy that calls adjust (0) */
2085 assert (!SvSTATE_current->on_destroy);//D
2086 SvSTATE_current->on_destroy = coro_semaphore_on_destroy; 2500 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2087 } 2501 }
2502}
2088 2503
2504static void
2505slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2506{
2507 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2089 frame->check = slf_check_semaphore_down; 2508 frame->check = slf_check_semaphore_down;
2090
2091} 2509}
2092 2510
2093/*****************************************************************************/ 2511static void
2094/* gensub: simple closure generation utility */ 2512slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2095
2096#define GENSUB_ARG CvXSUBANY (cv).any_ptr
2097
2098/* create a closure from XS, returns a code reference */
2099/* the arg can be accessed via GENSUB_ARG from the callback */
2100/* the callback must use dXSARGS/XSRETURN */
2101static SV *
2102gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
2103{ 2513{
2104 CV *cv = (CV *)newSV (0); 2514 if (items >= 2)
2515 {
2516 /* callback form */
2517 AV *av = (AV *)SvRV (arg [0]);
2518 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2105 2519
2106 sv_upgrade ((SV *)cv, SVt_PVCV); 2520 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv));
2107 2521
2108 CvANON_on (cv); 2522 if (SvIVX (AvARRAY (av)[0]) > 0)
2109 CvISXSUB_on (cv); 2523 coro_semaphore_adjust (aTHX_ av, 0);
2110 CvXSUB (cv) = xsub;
2111 GENSUB_ARG = arg;
2112 2524
2113 return newRV_noinc ((SV *)cv); 2525 frame->prepare = prepare_nop;
2526 frame->check = slf_check_nop;
2527 }
2528 else
2529 {
2530 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2531 frame->check = slf_check_semaphore_wait;
2532 }
2533}
2534
2535/* signal */
2536
2537static void
2538coro_signal_wake (pTHX_ AV *av, int count)
2539{
2540 SvIVX (AvARRAY (av)[0]) = 0;
2541
2542 /* now signal count waiters */
2543 while (count > 0 && AvFILLp (av) > 0)
2544 {
2545 SV *cb;
2546
2547 /* swap first two elements so we can shift a waiter */
2548 cb = AvARRAY (av)[0];
2549 AvARRAY (av)[0] = AvARRAY (av)[1];
2550 AvARRAY (av)[1] = cb;
2551
2552 cb = av_shift (av);
2553
2554 api_ready (aTHX_ cb);
2555 sv_setiv (cb, 0); /* signal waiter */
2556 SvREFCNT_dec (cb);
2557
2558 --count;
2559 }
2560}
2561
2562static int
2563slf_check_signal_wait (pTHX_ struct CoroSLF *frame)
2564{
2565 /* if we are about to throw, also stop waiting */
2566 return SvROK ((SV *)frame->data) && !CORO_THROW;
2567}
2568
2569static void
2570slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2571{
2572 AV *av = (AV *)SvRV (arg [0]);
2573
2574 if (SvIVX (AvARRAY (av)[0]))
2575 {
2576 SvIVX (AvARRAY (av)[0]) = 0;
2577 frame->prepare = prepare_nop;
2578 frame->check = slf_check_nop;
2579 }
2580 else
2581 {
2582 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */
2583
2584 av_push (av, waiter);
2585
2586 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */
2587 frame->prepare = prepare_schedule;
2588 frame->check = slf_check_signal_wait;
2589 }
2114} 2590}
2115 2591
2116/*****************************************************************************/ 2592/*****************************************************************************/
2117/* Coro::AIO */ 2593/* Coro::AIO */
2118 2594
2133 dXSARGS; 2609 dXSARGS;
2134 AV *state = (AV *)GENSUB_ARG; 2610 AV *state = (AV *)GENSUB_ARG;
2135 SV *coro = av_pop (state); 2611 SV *coro = av_pop (state);
2136 SV *data_sv = newSV (sizeof (struct io_state)); 2612 SV *data_sv = newSV (sizeof (struct io_state));
2137 2613
2138 av_extend (state, items); 2614 av_extend (state, items - 1);
2139 2615
2140 sv_upgrade (data_sv, SVt_PV); 2616 sv_upgrade (data_sv, SVt_PV);
2141 SvCUR_set (data_sv, sizeof (struct io_state)); 2617 SvCUR_set (data_sv, sizeof (struct io_state));
2142 SvPOK_only (data_sv); 2618 SvPOK_only (data_sv);
2143 2619
2168static int 2644static int
2169slf_check_aio_req (pTHX_ struct CoroSLF *frame) 2645slf_check_aio_req (pTHX_ struct CoroSLF *frame)
2170{ 2646{
2171 AV *state = (AV *)frame->data; 2647 AV *state = (AV *)frame->data;
2172 2648
2649 /* if we are about to throw, return early */
2650 /* this does not cancel the aio request, but at least */
2651 /* it quickly returns */
2652 if (CORO_THROW)
2653 return 0;
2654
2173 /* one element that is an RV? repeat! */ 2655 /* one element that is an RV? repeat! */
2174 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) 2656 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0]))
2175 return 1; 2657 return 1;
2176 2658
2177 /* restore status */ 2659 /* restore status */
2247 2729
2248 for (i = 0; i < items; ++i) 2730 for (i = 0; i < items; ++i)
2249 PUSHs (arg [i]); 2731 PUSHs (arg [i]);
2250 2732
2251 /* now push the callback closure */ 2733 /* now push the callback closure */
2252 PUSHs (sv_2mortal (gensub (coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state)))); 2734 PUSHs (sv_2mortal (gensub (aTHX_ coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state))));
2253 2735
2254 /* now call the AIO function - we assume our request is uncancelable */ 2736 /* now call the AIO function - we assume our request is uncancelable */
2255 PUTBACK; 2737 PUTBACK;
2256 call_sv ((SV *)req, G_VOID | G_DISCARD); 2738 call_sv ((SV *)req, G_VOID | G_DISCARD);
2257 } 2739 }
2263} 2745}
2264 2746
2265static void 2747static void
2266coro_aio_req_xs (pTHX_ CV *cv) 2748coro_aio_req_xs (pTHX_ CV *cv)
2267{ 2749{
2268 dVAR;
2269 dXSARGS; 2750 dXSARGS;
2270 2751
2271 CORO_EXECUTE_SLF_XS (slf_init_aio_req); 2752 CORO_EXECUTE_SLF_XS (slf_init_aio_req);
2272 2753
2273 XSRETURN_EMPTY; 2754 XSRETURN_EMPTY;
2274} 2755}
2275 2756
2276/*****************************************************************************/ 2757/*****************************************************************************/
2758
2759#if CORO_CLONE
2760# include "clone.c"
2761#endif
2277 2762
2278MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2763MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2279 2764
2280PROTOTYPES: DISABLE 2765PROTOTYPES: DISABLE
2281 2766
2286 coro_thx = PERL_GET_CONTEXT; 2771 coro_thx = PERL_GET_CONTEXT;
2287# endif 2772# endif
2288#endif 2773#endif
2289 BOOT_PAGESIZE; 2774 BOOT_PAGESIZE;
2290 2775
2776 cctx_current = cctx_new_empty ();
2777
2291 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2778 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2292 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2779 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2293 2780
2294 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2781 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2295 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2782 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2314 2801
2315 { 2802 {
2316 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2803 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2317 2804
2318 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2805 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2319 hv_store_ent (PL_custom_op_names, slf, 2806 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2320 newSVpv ("coro_slf", 0), 0);
2321 2807
2322 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2808 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2323 hv_store_ent (PL_custom_op_descs, slf, 2809 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2324 newSVpv ("coro schedule like function", 0), 0);
2325 } 2810 }
2326 2811
2327 coroapi.ver = CORO_API_VERSION; 2812 coroapi.ver = CORO_API_VERSION;
2328 coroapi.rev = CORO_API_REVISION; 2813 coroapi.rev = CORO_API_REVISION;
2329 2814
2348 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 2833 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
2349} 2834}
2350 2835
2351SV * 2836SV *
2352new (char *klass, ...) 2837new (char *klass, ...)
2838 ALIAS:
2839 Coro::new = 1
2353 CODE: 2840 CODE:
2354{ 2841{
2355 struct coro *coro; 2842 struct coro *coro;
2356 MAGIC *mg; 2843 MAGIC *mg;
2357 HV *hv; 2844 HV *hv;
2845 CV *cb;
2358 int i; 2846 int i;
2847
2848 if (items > 1)
2849 {
2850 cb = coro_sv_2cv (aTHX_ ST (1));
2851
2852 if (!ix)
2853 {
2854 if (CvISXSUB (cb))
2855 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2856
2857 if (!CvROOT (cb))
2858 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
2859 }
2860 }
2359 2861
2360 Newz (0, coro, 1, struct coro); 2862 Newz (0, coro, 1, struct coro);
2361 coro->args = newAV (); 2863 coro->args = newAV ();
2362 coro->flags = CF_NEW; 2864 coro->flags = CF_NEW;
2363 2865
2368 coro->hv = hv = newHV (); 2870 coro->hv = hv = newHV ();
2369 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); 2871 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2370 mg->mg_flags |= MGf_DUP; 2872 mg->mg_flags |= MGf_DUP;
2371 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 2873 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2372 2874
2875 if (items > 1)
2876 {
2373 av_extend (coro->args, items - 1); 2877 av_extend (coro->args, items - 1 + ix - 1);
2878
2879 if (ix)
2880 {
2881 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
2882 cb = cv_coro_run;
2883 }
2884
2885 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
2886
2374 for (i = 1; i < items; i++) 2887 for (i = 2; i < items; i++)
2375 av_push (coro->args, newSVsv (ST (i))); 2888 av_push (coro->args, newSVsv (ST (i)));
2889 }
2376} 2890}
2377 OUTPUT: 2891 OUTPUT:
2378 RETVAL 2892 RETVAL
2379
2380void
2381_set_stacklevel (...)
2382 CODE:
2383 CORO_EXECUTE_SLF_XS (slf_init_set_stacklevel);
2384 2893
2385void 2894void
2386transfer (...) 2895transfer (...)
2387 PROTOTYPE: $$ 2896 PROTOTYPE: $$
2388 CODE: 2897 CODE:
2398void 2907void
2399_exit (int code) 2908_exit (int code)
2400 PROTOTYPE: $ 2909 PROTOTYPE: $
2401 CODE: 2910 CODE:
2402 _exit (code); 2911 _exit (code);
2912
2913SV *
2914clone (Coro::State coro)
2915 CODE:
2916{
2917#if CORO_CLONE
2918 struct coro *ncoro = coro_clone (coro);
2919 MAGIC *mg;
2920 /* TODO: too much duplication */
2921 ncoro->hv = newHV ();
2922 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2923 mg->mg_flags |= MGf_DUP;
2924 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
2925#else
2926 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
2927#endif
2928}
2929 OUTPUT:
2930 RETVAL
2403 2931
2404int 2932int
2405cctx_stacksize (int new_stacksize = 0) 2933cctx_stacksize (int new_stacksize = 0)
2406 PROTOTYPE: ;$ 2934 PROTOTYPE: ;$
2407 CODE: 2935 CODE:
2514throw (Coro::State self, SV *throw = &PL_sv_undef) 3042throw (Coro::State self, SV *throw = &PL_sv_undef)
2515 PROTOTYPE: $;$ 3043 PROTOTYPE: $;$
2516 CODE: 3044 CODE:
2517{ 3045{
2518 struct coro *current = SvSTATE_current; 3046 struct coro *current = SvSTATE_current;
2519 SV **throwp = self == current ? &coro_throw : &self->throw; 3047 SV **throwp = self == current ? &CORO_THROW : &self->except;
2520 SvREFCNT_dec (*throwp); 3048 SvREFCNT_dec (*throwp);
2521 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3049 *throwp = SvOK (throw) ? newSVsv (throw) : 0;
2522} 3050}
2523 3051
2524void 3052void
2528 3056
2529SV * 3057SV *
2530has_cctx (Coro::State coro) 3058has_cctx (Coro::State coro)
2531 PROTOTYPE: $ 3059 PROTOTYPE: $
2532 CODE: 3060 CODE:
2533 RETVAL = boolSV (!!coro->cctx); 3061 /* maybe manage the running flag differently */
3062 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
2534 OUTPUT: 3063 OUTPUT:
2535 RETVAL 3064 RETVAL
2536 3065
2537int 3066int
2538is_traced (Coro::State coro) 3067is_traced (Coro::State coro)
2558 3087
2559void 3088void
2560force_cctx () 3089force_cctx ()
2561 PROTOTYPE: 3090 PROTOTYPE:
2562 CODE: 3091 CODE:
2563 SvSTATE_current->cctx->idle_sp = 0; 3092 cctx_current->idle_sp = 0;
2564 3093
2565void 3094void
2566swap_defsv (Coro::State self) 3095swap_defsv (Coro::State self)
2567 PROTOTYPE: $ 3096 PROTOTYPE: $
2568 ALIAS: 3097 ALIAS:
2583 3112
2584BOOT: 3113BOOT:
2585{ 3114{
2586 int i; 3115 int i;
2587 3116
2588 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2589 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3117 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2590 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3118 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2591 3119 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
3120 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2592 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); 3121 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
2593 SvREADONLY_on (coro_current); 3122 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3123 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3124 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3125 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3126
3127 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3128 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3129 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3130 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
2594 3131
2595 coro_stash = gv_stashpv ("Coro", TRUE); 3132 coro_stash = gv_stashpv ("Coro", TRUE);
2596 3133
2597 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 3134 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2598 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3135 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2606 3143
2607 { 3144 {
2608 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3145 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2609 3146
2610 coroapi.schedule = api_schedule; 3147 coroapi.schedule = api_schedule;
3148 coroapi.schedule_to = api_schedule_to;
2611 coroapi.cede = api_cede; 3149 coroapi.cede = api_cede;
2612 coroapi.cede_notself = api_cede_notself; 3150 coroapi.cede_notself = api_cede_notself;
2613 coroapi.ready = api_ready; 3151 coroapi.ready = api_ready;
2614 coroapi.is_ready = api_is_ready; 3152 coroapi.is_ready = api_is_ready;
2615 coroapi.nready = coro_nready; 3153 coroapi.nready = coro_nready;
2616 coroapi.current = coro_current; 3154 coroapi.current = coro_current;
2617 3155
2618 GCoroAPI = &coroapi; 3156 /*GCoroAPI = &coroapi;*/
2619 sv_setiv (sv, (IV)&coroapi); 3157 sv_setiv (sv, (IV)&coroapi);
2620 SvREADONLY_on (sv); 3158 SvREADONLY_on (sv);
2621 } 3159 }
2622} 3160}
3161
3162void
3163terminate (...)
3164 CODE:
3165 CORO_EXECUTE_SLF_XS (slf_init_terminate);
2623 3166
2624void 3167void
2625schedule (...) 3168schedule (...)
2626 CODE: 3169 CODE:
2627 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3170 CORO_EXECUTE_SLF_XS (slf_init_schedule);
2628 3171
2629void 3172void
3173schedule_to (...)
3174 CODE:
3175 CORO_EXECUTE_SLF_XS (slf_init_schedule_to);
3176
3177void
3178cede_to (...)
3179 CODE:
3180 CORO_EXECUTE_SLF_XS (slf_init_cede_to);
3181
3182void
2630cede (...) 3183cede (...)
2631 CODE: 3184 CODE:
2632 CORO_EXECUTE_SLF_XS (slf_init_cede); 3185 CORO_EXECUTE_SLF_XS (slf_init_cede);
2633 3186
2634void 3187void
2635cede_notself (...) 3188cede_notself (...)
2636 CODE: 3189 CODE:
2637 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3190 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3191
3192void
3193_cancel (Coro::State self)
3194 CODE:
3195 coro_state_destroy (aTHX_ self);
3196 coro_call_on_destroy (aTHX_ self);
2638 3197
2639void 3198void
2640_set_current (SV *current) 3199_set_current (SV *current)
2641 PROTOTYPE: $ 3200 PROTOTYPE: $
2642 CODE: 3201 CODE:
2687 CODE: 3246 CODE:
2688 RETVAL = coro_nready; 3247 RETVAL = coro_nready;
2689 OUTPUT: 3248 OUTPUT:
2690 RETVAL 3249 RETVAL
2691 3250
2692# for async_pool speedup
2693void 3251void
2694_pool_1 (SV *cb) 3252_pool_handler (...)
2695 CODE: 3253 CODE:
2696{ 3254 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
2697 HV *hv = (HV *)SvRV (coro_current);
2698 struct coro *coro = SvSTATE_hv ((SV *)hv);
2699 AV *defav = GvAV (PL_defgv);
2700 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
2701 AV *invoke_av;
2702 int i, len;
2703 3255
2704 if (!invoke) 3256void
3257async_pool (SV *cv, ...)
3258 PROTOTYPE: &@
3259 PPCODE:
3260{
3261 HV *hv = (HV *)av_pop (av_async_pool);
3262 AV *av = newAV ();
3263 SV *cb = ST (0);
3264 int i;
3265
3266 av_extend (av, items - 2);
3267 for (i = 1; i < items; ++i)
3268 av_push (av, SvREFCNT_inc_NN (ST (i)));
3269
3270 if ((SV *)hv == &PL_sv_undef)
2705 { 3271 {
2706 SV *old = PL_diehook; 3272 PUSHMARK (SP);
2707 PL_diehook = 0; 3273 EXTEND (SP, 2);
2708 SvREFCNT_dec (old); 3274 PUSHs (sv_Coro);
2709 croak ("\3async_pool terminate\2\n"); 3275 PUSHs ((SV *)cv_pool_handler);
3276 PUTBACK;
3277 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3278 SPAGAIN;
3279
3280 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
2710 } 3281 }
2711 3282
2712 SvREFCNT_dec (coro->saved_deffh);
2713 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2714
2715 hv_store (hv, "desc", sizeof ("desc") - 1,
2716 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2717
2718 invoke_av = (AV *)SvRV (invoke);
2719 len = av_len (invoke_av);
2720
2721 sv_setsv (cb, AvARRAY (invoke_av)[0]);
2722
2723 if (len > 0)
2724 { 3283 {
2725 av_fill (defav, len - 1); 3284 struct coro *coro = SvSTATE_hv (hv);
2726 for (i = 0; i < len; ++i) 3285
2727 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); 3286 assert (!coro->invoke_cb);
3287 assert (!coro->invoke_av);
3288 coro->invoke_cb = SvREFCNT_inc (cb);
3289 coro->invoke_av = av;
2728 } 3290 }
3291
3292 api_ready (aTHX_ (SV *)hv);
3293
3294 if (GIMME_V != G_VOID)
3295 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
3296 else
3297 SvREFCNT_dec (hv);
2729} 3298}
3299
3300SV *
3301rouse_cb ()
3302 PROTOTYPE:
3303 CODE:
3304 RETVAL = coro_new_rouse_cb (aTHX);
3305 OUTPUT:
3306 RETVAL
2730 3307
2731void 3308void
2732_pool_2 (SV *cb) 3309rouse_wait (...)
3310 PROTOTYPE: ;$
2733 CODE: 3311 PPCODE:
2734{ 3312 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
2735 HV *hv = (HV *)SvRV (coro_current);
2736 struct coro *coro = SvSTATE_hv ((SV *)hv);
2737
2738 sv_setsv (cb, &PL_sv_undef);
2739
2740 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2741 coro->saved_deffh = 0;
2742
2743 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2744 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2745 {
2746 SV *old = PL_diehook;
2747 PL_diehook = 0;
2748 SvREFCNT_dec (old);
2749 croak ("\3async_pool terminate\2\n");
2750 }
2751
2752 av_clear (GvAV (PL_defgv));
2753 hv_store (hv, "desc", sizeof ("desc") - 1,
2754 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
2755
2756 coro->prio = 0;
2757
2758 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
2759 api_trace (aTHX_ coro_current, 0);
2760
2761 av_push (av_async_pool, newSVsv (coro_current));
2762}
2763 3313
2764 3314
2765MODULE = Coro::State PACKAGE = PerlIO::cede 3315MODULE = Coro::State PACKAGE = PerlIO::cede
2766 3316
2767BOOT: 3317BOOT:
2769 3319
2770 3320
2771MODULE = Coro::State PACKAGE = Coro::Semaphore 3321MODULE = Coro::State PACKAGE = Coro::Semaphore
2772 3322
2773SV * 3323SV *
2774new (SV *klass, SV *count_ = 0) 3324new (SV *klass, SV *count = 0)
2775 CODE: 3325 CODE:
2776{ 3326 RETVAL = sv_bless (
2777 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ 3327 coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1),
2778 AV *av = newAV (); 3328 GvSTASH (CvGV (cv))
2779 av_push (av, newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1)); 3329 );
2780 RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv))); 3330 OUTPUT:
2781} 3331 RETVAL
3332
3333# helper for Coro::Channel
3334SV *
3335_alloc (int count)
3336 CODE:
3337 RETVAL = coro_waitarray_new (aTHX_ count);
2782 OUTPUT: 3338 OUTPUT:
2783 RETVAL 3339 RETVAL
2784 3340
2785SV * 3341SV *
2786count (SV *self) 3342count (SV *self)
2795 adjust = 1 3351 adjust = 1
2796 CODE: 3352 CODE:
2797 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); 3353 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1);
2798 3354
2799void 3355void
2800down (SV *self) 3356down (...)
2801 CODE: 3357 CODE:
2802 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); 3358 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);
3359
3360void
3361wait (...)
3362 CODE:
3363 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait);
2803 3364
2804void 3365void
2805try (SV *self) 3366try (SV *self)
2806 PPCODE: 3367 PPCODE:
2807{ 3368{
2819 XSRETURN_NO; 3380 XSRETURN_NO;
2820} 3381}
2821 3382
2822void 3383void
2823waiters (SV *self) 3384waiters (SV *self)
2824 CODE: 3385 PPCODE:
2825{ 3386{
2826 AV *av = (AV *)SvRV (self); 3387 AV *av = (AV *)SvRV (self);
3388 int wcount = AvFILLp (av) + 1 - 1;
2827 3389
2828 if (GIMME_V == G_SCALAR) 3390 if (GIMME_V == G_SCALAR)
2829 XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); 3391 XPUSHs (sv_2mortal (newSViv (wcount)));
2830 else 3392 else
2831 { 3393 {
2832 int i; 3394 int i;
2833 EXTEND (SP, AvFILLp (av) + 1 - 1); 3395 EXTEND (SP, wcount);
2834 for (i = 1; i <= AvFILLp (av); ++i) 3396 for (i = 1; i <= wcount; ++i)
2835 PUSHs (newSVsv (AvARRAY (av)[i])); 3397 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
2836 } 3398 }
2837} 3399}
3400
3401MODULE = Coro::State PACKAGE = Coro::Signal
3402
3403SV *
3404new (SV *klass)
3405 CODE:
3406 RETVAL = sv_bless (
3407 coro_waitarray_new (aTHX_ 0),
3408 GvSTASH (CvGV (cv))
3409 );
3410 OUTPUT:
3411 RETVAL
3412
3413void
3414wait (...)
3415 CODE:
3416 CORO_EXECUTE_SLF_XS (slf_init_signal_wait);
3417
3418void
3419broadcast (SV *self)
3420 CODE:
3421{
3422 AV *av = (AV *)SvRV (self);
3423 coro_signal_wake (aTHX_ av, AvFILLp (av));
3424}
3425
3426void
3427send (SV *self)
3428 CODE:
3429{
3430 AV *av = (AV *)SvRV (self);
3431
3432 if (AvFILLp (av))
3433 coro_signal_wake (aTHX_ av, 1);
3434 else
3435 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */
3436}
3437
3438IV
3439awaited (SV *self)
3440 CODE:
3441 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1;
3442 OUTPUT:
3443 RETVAL
2838 3444
2839 3445
2840MODULE = Coro::State PACKAGE = Coro::AnyEvent 3446MODULE = Coro::State PACKAGE = Coro::AnyEvent
2841 3447
2842BOOT: 3448BOOT:
2857 sv_setsv (sv_activity, &PL_sv_undef); 3463 sv_setsv (sv_activity, &PL_sv_undef);
2858 if (coro_nready >= incede) 3464 if (coro_nready >= incede)
2859 { 3465 {
2860 PUSHMARK (SP); 3466 PUSHMARK (SP);
2861 PUTBACK; 3467 PUTBACK;
2862 call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL); 3468 call_pv ("Coro::AnyEvent::_activity", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2863 SPAGAIN;
2864 } 3469 }
2865 3470
2866 --incede; 3471 --incede;
2867} 3472}
2868 3473
2871 3476
2872void 3477void
2873_register (char *target, char *proto, SV *req) 3478_register (char *target, char *proto, SV *req)
2874 CODE: 3479 CODE:
2875{ 3480{
2876 HV *st; 3481 CV *req_cv = coro_sv_2cv (aTHX_ req);
2877 GV *gvp; 3482 /* newXSproto doesn't return the CV on 5.8 */
2878 CV *req_cv = sv_2cv (req, &st, &gvp, 0);
2879 CV *slf_cv = newXSproto (target, coro_aio_req_xs, __FILE__, proto); 3483 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3484 sv_setpv ((SV *)slf_cv, proto);
2880 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3485 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
2881} 3486}
2882 3487

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines