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.202 by root, Mon Oct 8 02:50:23 2007 UTC vs.
Revision 1.253 by root, Fri Nov 7 20:12:26 2008 UTC

4#define PERL_EXT 4#define PERL_EXT
5 5
6#include "EXTERN.h" 6#include "EXTERN.h"
7#include "perl.h" 7#include "perl.h"
8#include "XSUB.h" 8#include "XSUB.h"
9#include "perliol.h"
9 10
10#include "patchlevel.h" 11#include "patchlevel.h"
11 12
12#include <stdio.h> 13#include <stdio.h>
13#include <errno.h> 14#include <errno.h>
14#include <assert.h> 15#include <assert.h>
16
17#ifdef WIN32
18# undef setjmp
19# undef longjmp
20# undef _exit
21# define setjmp _setjmp // deep magic, don't ask
22#else
23# include <inttypes.h> /* most portable stdint.h */
24#endif
15 25
16#ifdef HAVE_MMAP 26#ifdef HAVE_MMAP
17# include <unistd.h> 27# include <unistd.h>
18# include <sys/mman.h> 28# include <sys/mman.h>
19# ifndef MAP_ANONYMOUS 29# ifndef MAP_ANONYMOUS
71# ifndef IS_PADCONST 81# ifndef IS_PADCONST
72# define IS_PADCONST(v) 0 82# define IS_PADCONST(v) 0
73# endif 83# endif
74#endif 84#endif
75 85
86/* 5.11 */
87#ifndef CxHASARGS
88# define CxHASARGS(cx) (cx)->blk_sub.hasargs
89#endif
90
91/* 5.10.0 */
92#ifndef SvREFCNT_inc_NN
93# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
94#endif
95
96/* 5.8.8 */
97#ifndef GV_NOTQUAL
98# define GV_NOTQUAL 0
99#endif
100#ifndef newSV
101# define newSV(l) NEWSV(0,l)
102#endif
103
76/* 5.8.7 */ 104/* 5.8.7 */
77#ifndef SvRV_set 105#ifndef SvRV_set
78# define SvRV_set(s,v) SvRV(s) = (v) 106# define SvRV_set(s,v) SvRV(s) = (v)
79#endif
80
81/* 5.8.8 */
82#ifndef GV_NOTQUAL
83# define GV_NOTQUAL 0
84#endif
85#ifndef newSV
86# define newSV(l) NEWSV(0,l)
87#endif 107#endif
88 108
89#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 109#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
90# undef CORO_STACKGUARD 110# undef CORO_STACKGUARD
91#endif 111#endif
122#define NOINLINE attribute ((noinline)) 142#define NOINLINE attribute ((noinline))
123 143
124#include "CoroAPI.h" 144#include "CoroAPI.h"
125 145
126#ifdef USE_ITHREADS 146#ifdef USE_ITHREADS
127static perl_mutex coro_mutex; 147static perl_mutex coro_lock;
128# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 148# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
129# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 149# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
130#else 150#else
131# define LOCK (void)0 151# define LOCK (void)0
132# define UNLOCK (void)0 152# define UNLOCK (void)0
133#endif 153#endif
134 154
135#define strpair(const) const, sizeof (const) - 1
136
137/* helper storage struct for Coro::AIO */ 155/* helper storage struct for Coro::AIO */
138struct io_state 156struct io_state
139{ 157{
158 AV *res;
140 int errorno; 159 int errorno;
141 I32 laststype; 160 I32 laststype;
142 int laststatval; 161 int laststatval;
143 Stat_t statcache; 162 Stat_t statcache;
144}; 163};
145 164
165static double (*nvtime)(); /* so why doesn't it take void? */
166
146static size_t coro_stacksize = CORO_STACKSIZE; 167static size_t coro_stacksize = CORO_STACKSIZE;
147static struct CoroAPI coroapi; 168static struct CoroAPI coroapi;
148static AV *main_mainstack; /* used to differentiate between $main and others */ 169static AV *main_mainstack; /* used to differentiate between $main and others */
149static JMPENV *main_top_env; 170static JMPENV *main_top_env;
150static HV *coro_state_stash, *coro_stash; 171static HV *coro_state_stash, *coro_stash;
151static SV *coro_mortal; /* will be freed after next transfer */ 172static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
152 173
153static GV *irsgv; /* $/ */ 174static GV *irsgv; /* $/ */
154static GV *stdoutgv; /* *STDOUT */ 175static GV *stdoutgv; /* *STDOUT */
155 176static SV *rv_diehook;
177static SV *rv_warnhook;
156static HV *hv_sig; /* %SIG */ 178static HV *hv_sig; /* %SIG */
157static SV *sv_diehook;
158static SV *sv_warnhook;
159 179
160/* async_pool helper stuff */ 180/* async_pool helper stuff */
161static SV *sv_pool_rss; 181static SV *sv_pool_rss;
162static SV *sv_pool_size; 182static SV *sv_pool_size;
163static AV *av_async_pool; 183static AV *av_async_pool;
184
185/* Coro::AnyEvent */
186static SV *sv_activity;
164 187
165static struct coro_cctx *cctx_first; 188static struct coro_cctx *cctx_first;
166static int cctx_count, cctx_idle; 189static int cctx_count, cctx_idle;
167 190
168enum { 191enum {
221 244
222 /* process data */ 245 /* process data */
223 AV *mainstack; 246 AV *mainstack;
224 perl_slots *slot; /* basically the saved sp */ 247 perl_slots *slot; /* basically the saved sp */
225 248
226 /* data associated with this coroutine (initial args) */ 249 AV *args; /* data associated with this coroutine (initial args) */
227 AV *args; 250 int refcnt; /* coroutines are refcounted, yes */
228 int refcnt;
229 int flags; /* CF_ flags */ 251 int flags; /* CF_ flags */
252 HV *hv; /* the perl hash associated with this coro, if any */
230 253
231 /* statistics */ 254 /* statistics */
232 int usecount; /* number of transfers to this coro */ 255 int usecount; /* number of transfers to this coro */
233 256
234 /* coro process data */ 257 /* coro process data */
235 int prio; 258 int prio;
236 SV *throw; 259 SV *throw; /* exception to be thrown */
237 260
238 /* async_pool */ 261 /* async_pool */
239 SV *saved_deffh; 262 SV *saved_deffh;
240 263
241 /* linked list */ 264 /* linked list */
242 struct coro *next, *prev; 265 struct coro *next, *prev;
243 HV *hv; /* the perl hash associated with this coro, if any */
244}; 266};
245 267
246typedef struct coro *Coro__State; 268typedef struct coro *Coro__State;
247typedef struct coro *Coro__State_or_hashref; 269typedef struct coro *Coro__State_or_hashref;
248 270
255#define PRIO_IDLE -3 277#define PRIO_IDLE -3
256#define PRIO_MIN -4 278#define PRIO_MIN -4
257 279
258/* for Coro.pm */ 280/* for Coro.pm */
259static SV *coro_current; 281static SV *coro_current;
282static SV *coro_readyhook;
260static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 283static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
261static int coro_nready; 284static int coro_nready;
262static struct coro *coro_first; 285static struct coro *coro_first;
263 286
264/** lowlevel stuff **********************************************************/ 287/** lowlevel stuff **********************************************************/
265 288
266static SV * 289static SV *
267coro_get_sv (const char *name, int create) 290coro_get_sv (pTHX_ const char *name, int create)
268{ 291{
269#if PERL_VERSION_ATLEAST (5,9,0) 292#if PERL_VERSION_ATLEAST (5,10,0)
270 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 293 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
271 get_sv (name, create); 294 get_sv (name, create);
272#endif 295#endif
273 return get_sv (name, create); 296 return get_sv (name, create);
274} 297}
275 298
276static AV * 299static AV *
277coro_get_av (const char *name, int create) 300coro_get_av (pTHX_ const char *name, int create)
278{ 301{
279#if PERL_VERSION_ATLEAST (5,9,0) 302#if PERL_VERSION_ATLEAST (5,10,0)
280 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 303 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
281 get_av (name, create); 304 get_av (name, create);
282#endif 305#endif
283 return get_av (name, create); 306 return get_av (name, create);
284} 307}
285 308
286static HV * 309static HV *
287coro_get_hv (const char *name, int create) 310coro_get_hv (pTHX_ const char *name, int create)
288{ 311{
289#if PERL_VERSION_ATLEAST (5,9,0) 312#if PERL_VERSION_ATLEAST (5,10,0)
290 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 313 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
291 get_hv (name, create); 314 get_hv (name, create);
292#endif 315#endif
293 return get_hv (name, create); 316 return get_hv (name, create);
294} 317}
299 AV *padlist = CvPADLIST (cv); 322 AV *padlist = CvPADLIST (cv);
300 AV *newpadlist, *newpad; 323 AV *newpadlist, *newpad;
301 324
302 newpadlist = newAV (); 325 newpadlist = newAV ();
303 AvREAL_off (newpadlist); 326 AvREAL_off (newpadlist);
304#if PERL_VERSION_ATLEAST (5,9,0) 327#if PERL_VERSION_ATLEAST (5,10,0)
305 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); 328 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
306#else 329#else
307 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 330 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
308#endif 331#endif
309 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 332 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
310 --AvFILLp (padlist); 333 --AvFILLp (padlist);
311 334
312 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 335 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
313 av_store (newpadlist, 1, (SV *)newpad); 336 av_store (newpadlist, 1, (SV *)newpad);
314 337
315 return newpadlist; 338 return newpadlist;
316} 339}
317 340
347 370
348 /* casting is fun. */ 371 /* casting is fun. */
349 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 372 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
350 free_padlist (aTHX_ padlist); 373 free_padlist (aTHX_ padlist);
351 374
352 SvREFCNT_dec (av); 375 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
353 376
354 return 0; 377 return 0;
355} 378}
356 379
357#define PERL_MAGIC_coro PERL_MAGIC_ext 380#define CORO_MAGIC_type_cv PERL_MAGIC_ext
381#define CORO_MAGIC_type_state PERL_MAGIC_ext
358 382
359static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 383static MGVTBL coro_cv_vtbl = {
384 0, 0, 0, 0,
385 coro_cv_free
386};
360 387
361#define CORO_MAGIC(cv) \ 388#define CORO_MAGIC(sv, type) \
362 SvMAGIC (cv) \ 389 SvMAGIC (sv) \
363 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 390 ? SvMAGIC (sv)->mg_type == type \
364 ? SvMAGIC (cv) \ 391 ? SvMAGIC (sv) \
365 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 392 : mg_find (sv, type) \
366 : 0 393 : 0
394
395#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
396#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
367 397
368static struct coro * 398static struct coro *
369SvSTATE_ (pTHX_ SV *coro) 399SvSTATE_ (pTHX_ SV *coro)
370{ 400{
371 HV *stash; 401 HV *stash;
383 /* very slow, but rare, check */ 413 /* very slow, but rare, check */
384 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 414 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
385 croak ("Coro::State object required"); 415 croak ("Coro::State object required");
386 } 416 }
387 417
388 mg = CORO_MAGIC (coro); 418 mg = CORO_MAGIC_state (coro);
389 return (struct coro *)mg->mg_ptr; 419 return (struct coro *)mg->mg_ptr;
390} 420}
391 421
392#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) 422#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
393 423
394/* the next two functions merely cache the padlists */ 424/* the next two functions merely cache the padlists */
395static void 425static void
396get_padlist (pTHX_ CV *cv) 426get_padlist (pTHX_ CV *cv)
397{ 427{
398 MAGIC *mg = CORO_MAGIC (cv); 428 MAGIC *mg = CORO_MAGIC_cv (cv);
399 AV *av; 429 AV *av;
400 430
401 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 431 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
402 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 432 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
403 else 433 else
404 { 434 {
405#if CORO_PREFER_PERL_FUNCTIONS 435#if CORO_PREFER_PERL_FUNCTIONS
406 /* this is probably cleaner, but also slower? */ 436 /* this is probably cleaner? but also slower! */
437 /* in practise, it seems to be less stable */
407 CV *cp = Perl_cv_clone (cv); 438 CV *cp = Perl_cv_clone (cv);
408 CvPADLIST (cv) = CvPADLIST (cp); 439 CvPADLIST (cv) = CvPADLIST (cp);
409 CvPADLIST (cp) = 0; 440 CvPADLIST (cp) = 0;
410 SvREFCNT_dec (cp); 441 SvREFCNT_dec (cp);
411#else 442#else
415} 446}
416 447
417static void 448static void
418put_padlist (pTHX_ CV *cv) 449put_padlist (pTHX_ CV *cv)
419{ 450{
420 MAGIC *mg = CORO_MAGIC (cv); 451 MAGIC *mg = CORO_MAGIC_cv (cv);
421 AV *av; 452 AV *av;
422 453
423 if (expect_false (!mg)) 454 if (expect_false (!mg))
424 { 455 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
425 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
426 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
427 mg->mg_virtual = &vtbl_coro;
428 mg->mg_obj = (SV *)newAV ();
429 }
430 456
431 av = (AV *)mg->mg_obj; 457 av = (AV *)mg->mg_obj;
432 458
433 if (expect_false (AvFILLp (av) >= AvMAX (av))) 459 if (expect_false (AvFILLp (av) >= AvMAX (av)))
434 av_extend (av, AvMAX (av) + 1); 460 av_extend (av, AvMAX (av) + 1);
452 GvSV (irsgv) = slot->irsgv; 478 GvSV (irsgv) = slot->irsgv;
453 479
454 #define VAR(name,type) PL_ ## name = slot->name; 480 #define VAR(name,type) PL_ ## name = slot->name;
455 # include "state.h" 481 # include "state.h"
456 #undef VAR 482 #undef VAR
457
458 /*hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);*/
459 /*hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);*/
460 483
461 { 484 {
462 dSP; 485 dSP;
463 486
464 CV *cv; 487 CV *cv;
503 526
504 if (expect_true (CvDEPTH (cv))) 527 if (expect_true (CvDEPTH (cv)))
505 { 528 {
506 EXTEND (SP, 3); 529 EXTEND (SP, 3);
507 PUSHs ((SV *)CvPADLIST (cv)); 530 PUSHs ((SV *)CvPADLIST (cv));
508 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 531 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
509 PUSHs ((SV *)cv); 532 PUSHs ((SV *)cv);
510 533
511 CvDEPTH (cv) = 0; 534 CvDEPTH (cv) = 0;
512 get_padlist (aTHX_ cv); 535 get_padlist (aTHX_ cv);
513 } 536 }
557 * allocate various perl stacks. This is an exact copy 580 * allocate various perl stacks. This is an exact copy
558 * of perl.c:init_stacks, except that it uses less memory 581 * of perl.c:init_stacks, except that it uses less memory
559 * on the (sometimes correct) assumption that coroutines do 582 * on the (sometimes correct) assumption that coroutines do
560 * not usually need a lot of stackspace. 583 * not usually need a lot of stackspace.
561 */ 584 */
562#if 1 585#if CORO_PREFER_PERL_FUNCTIONS
563# define coro_init_stacks init_stacks 586# define coro_init_stacks init_stacks
564#else 587#else
565static void 588static void
566coro_init_stacks (pTHX) 589coro_init_stacks (pTHX)
567{ 590{
593 616
594 New(54,PL_savestack,24,ANY); 617 New(54,PL_savestack,24,ANY);
595 PL_savestack_ix = 0; 618 PL_savestack_ix = 0;
596 PL_savestack_max = 24; 619 PL_savestack_max = 24;
597 620
598#if !PERL_VERSION_ATLEAST (5,9,0) 621#if !PERL_VERSION_ATLEAST (5,10,0)
599 New(54,PL_retstack,4,OP*); 622 New(54,PL_retstack,4,OP*);
600 PL_retstack_ix = 0; 623 PL_retstack_ix = 0;
601 PL_retstack_max = 4; 624 PL_retstack_max = 4;
602#endif 625#endif
603} 626}
605 628
606/* 629/*
607 * destroy the stacks, the callchain etc... 630 * destroy the stacks, the callchain etc...
608 */ 631 */
609static void 632static void
610coro_destroy_stacks (pTHX) 633coro_destruct_stacks (pTHX)
611{ 634{
612 while (PL_curstackinfo->si_next) 635 while (PL_curstackinfo->si_next)
613 PL_curstackinfo = PL_curstackinfo->si_next; 636 PL_curstackinfo = PL_curstackinfo->si_next;
614 637
615 while (PL_curstackinfo) 638 while (PL_curstackinfo)
626 649
627 Safefree (PL_tmps_stack); 650 Safefree (PL_tmps_stack);
628 Safefree (PL_markstack); 651 Safefree (PL_markstack);
629 Safefree (PL_scopestack); 652 Safefree (PL_scopestack);
630 Safefree (PL_savestack); 653 Safefree (PL_savestack);
631#if !PERL_VERSION_ATLEAST (5,9,0) 654#if !PERL_VERSION_ATLEAST (5,10,0)
632 Safefree (PL_retstack); 655 Safefree (PL_retstack);
633#endif 656#endif
634} 657}
635 658
636static size_t 659static size_t
652 #undef VAR 675 #undef VAR
653 } 676 }
654 else 677 else
655 slot = coro->slot; 678 slot = coro->slot;
656 679
680 if (slot)
681 {
657 rss += sizeof (slot->curstackinfo); 682 rss += sizeof (slot->curstackinfo);
658 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 683 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
659 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 684 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *);
660 rss += slot->tmps_max * sizeof (SV *); 685 rss += slot->tmps_max * sizeof (SV *);
661 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); 686 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
662 rss += slot->scopestack_max * sizeof (I32); 687 rss += slot->scopestack_max * sizeof (I32);
663 rss += slot->savestack_max * sizeof (ANY); 688 rss += slot->savestack_max * sizeof (ANY);
664 689
665#if !PERL_VERSION_ATLEAST (5,9,0) 690#if !PERL_VERSION_ATLEAST (5,10,0)
666 rss += slot->retstack_max * sizeof (OP *); 691 rss += slot->retstack_max * sizeof (OP *);
667#endif 692#endif
693 }
668 } 694 }
669 695
670 return rss; 696 return rss;
671} 697}
672 698
673/** coroutine stack handling ************************************************/ 699/** coroutine stack handling ************************************************/
700
701static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
702static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
703static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
704
705/* apparently < 5.8.8 */
706#ifndef MgPV_nolen_const
707#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
708 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
709 (const char*)(mg)->mg_ptr)
710#endif
711
712/*
713 * This overrides the default magic get method of %SIG elements.
714 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
715 * and instead of tryign to save and restore the hash elements, we just provide
716 * readback here.
717 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
718 * not expecting this to actually change the hook. This is a potential problem
719 * when a schedule happens then, but we ignore this.
720 */
721static int
722coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
723{
724 const char *s = MgPV_nolen_const (mg);
725
726 if (*s == '_')
727 {
728 SV **svp = 0;
729
730 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
731 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
732
733 if (svp)
734 {
735 sv_setsv (sv, *svp ? *svp : &PL_sv_undef);
736 return 0;
737 }
738 }
739
740 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
741}
742
743static int
744coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
745{
746 const char *s = MgPV_nolen_const (mg);
747
748 if (*s == '_')
749 {
750 SV **svp = 0;
751
752 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
753 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
754
755 if (svp)
756 {
757 SV *old = *svp;
758 *svp = 0;
759 SvREFCNT_dec (old);
760 return 0;
761 }
762 }
763
764 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0;
765}
766
767static int
768coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
769{
770 const char *s = MgPV_nolen_const (mg);
771
772 if (*s == '_')
773 {
774 SV **svp = 0;
775
776 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
777 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
778
779 if (svp)
780 {
781 SV *old = *svp;
782 *svp = newSVsv (sv);
783 SvREFCNT_dec (old);
784 return 0;
785 }
786 }
787
788 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0;
789}
674 790
675static void 791static void
676coro_setup (pTHX_ struct coro *coro) 792coro_setup (pTHX_ struct coro *coro)
677{ 793{
678 /* 794 /*
687 PL_curpm = 0; 803 PL_curpm = 0;
688 PL_curpad = 0; 804 PL_curpad = 0;
689 PL_localizing = 0; 805 PL_localizing = 0;
690 PL_dirty = 0; 806 PL_dirty = 0;
691 PL_restartop = 0; 807 PL_restartop = 0;
692 PL_diehook = 0; hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); 808#if PERL_VERSION_ATLEAST (5,10,0)
693 PL_warnhook = 0; hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); 809 PL_parser = 0;
810#endif
811
812 /* recreate the die/warn hooks */
813 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook );
814 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
694 815
695 GvSV (PL_defgv) = newSV (0); 816 GvSV (PL_defgv) = newSV (0);
696 GvAV (PL_defgv) = coro->args; coro->args = 0; 817 GvAV (PL_defgv) = coro->args; coro->args = 0;
697 GvSV (PL_errgv) = newSV (0); 818 GvSV (PL_errgv) = newSV (0);
698 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 819 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
699 PL_rs = newSVsv (GvSV (irsgv)); 820 PL_rs = newSVsv (GvSV (irsgv));
700 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 821 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
701 822
702 { 823 {
703 dSP; 824 dSP;
704 LOGOP myop; 825 LOGOP myop;
705 826
713 PL_op = (OP *)&myop; 834 PL_op = (OP *)&myop;
714 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 835 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
715 SPAGAIN; 836 SPAGAIN;
716 } 837 }
717 838
718 ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ 839 /* this newly created coroutine might be run on an existing cctx which most
840 * likely was suspended in set_stacklevel, called from entersub.
841 * set_stacklevl doesn't do anything on return, but entersub does LEAVE,
842 * so we ENTER here for symmetry
843 */
844 ENTER;
719} 845}
720 846
721static void 847static void
722coro_destroy (pTHX_ struct coro *coro) 848coro_destruct (pTHX_ struct coro *coro)
723{ 849{
724 if (!IN_DESTRUCT) 850 if (!IN_DESTRUCT)
725 { 851 {
726 /* restore all saved variables and stuff */ 852 /* restore all saved variables and stuff */
727 LEAVE_SCOPE (0); 853 LEAVE_SCOPE (0);
749 SvREFCNT_dec (PL_warnhook); 875 SvREFCNT_dec (PL_warnhook);
750 876
751 SvREFCNT_dec (coro->saved_deffh); 877 SvREFCNT_dec (coro->saved_deffh);
752 SvREFCNT_dec (coro->throw); 878 SvREFCNT_dec (coro->throw);
753 879
754 coro_destroy_stacks (aTHX); 880 coro_destruct_stacks (aTHX);
755} 881}
756 882
757static void 883static void
758free_coro_mortal (pTHX) 884free_coro_mortal (pTHX)
759{ 885{
796 : cx->blk_gimme == G_SCALAR ? bot + 1 922 : cx->blk_gimme == G_SCALAR ? bot + 1
797 : bot; 923 : bot;
798 924
799 av_extend (av, top - bot); 925 av_extend (av, top - bot);
800 while (bot < top) 926 while (bot < top)
801 av_push (av, SvREFCNT_inc (*bot++)); 927 av_push (av, SvREFCNT_inc_NN (*bot++));
802 928
803 PL_runops = RUNOPS_DEFAULT; 929 PL_runops = RUNOPS_DEFAULT;
804 ENTER; 930 ENTER;
805 SAVETMPS; 931 SAVETMPS;
806 EXTEND (SP, 3); 932 EXTEND (SP, 3);
807 PUSHMARK (SP); 933 PUSHMARK (SP);
808 PUSHs (&PL_sv_no); 934 PUSHs (&PL_sv_no);
809 PUSHs (fullname); 935 PUSHs (fullname);
810 PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); 936 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
811 PUTBACK; 937 PUTBACK;
812 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); 938 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
813 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 939 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
814 SPAGAIN; 940 SPAGAIN;
815 FREETMPS; 941 FREETMPS;
816 LEAVE; 942 LEAVE;
817 PL_runops = runops_trace; 943 PL_runops = runops_trace;
844 SAVETMPS; 970 SAVETMPS;
845 EXTEND (SP, 3); 971 EXTEND (SP, 3);
846 PUSHMARK (SP); 972 PUSHMARK (SP);
847 PUSHs (&PL_sv_yes); 973 PUSHs (&PL_sv_yes);
848 PUSHs (fullname); 974 PUSHs (fullname);
849 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 975 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
850 PUTBACK; 976 PUTBACK;
851 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); 977 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
852 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 978 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
853 SPAGAIN; 979 SPAGAIN;
854 FREETMPS; 980 FREETMPS;
855 LEAVE; 981 LEAVE;
856 PL_runops = runops_trace; 982 PL_runops = runops_trace;
870 PL_runops = RUNOPS_DEFAULT; 996 PL_runops = RUNOPS_DEFAULT;
871 PUSHMARK (SP); 997 PUSHMARK (SP);
872 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); 998 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
873 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); 999 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
874 PUTBACK; 1000 PUTBACK;
875 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_line_cb"), 0); 1001 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
876 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1002 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
877 SPAGAIN; 1003 SPAGAIN;
878 FREETMPS; 1004 FREETMPS;
879 LEAVE; 1005 LEAVE;
880 PL_runops = runops_trace; 1006 PL_runops = runops_trace;
954 coro_cctx *cctx; 1080 coro_cctx *cctx;
955 void *stack_start; 1081 void *stack_start;
956 size_t stack_size; 1082 size_t stack_size;
957 1083
958 ++cctx_count; 1084 ++cctx_count;
959
960 Newz (0, cctx, 1, coro_cctx); 1085 Newz (0, cctx, 1, coro_cctx);
961 1086
962#if HAVE_MMAP 1087#if HAVE_MMAP
963 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1088 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
964 /* mmap supposedly does allocate-on-write for us */ 1089 /* mmap supposedly does allocate-on-write for us */
1000{ 1125{
1001 if (!cctx) 1126 if (!cctx)
1002 return; 1127 return;
1003 1128
1004 --cctx_count; 1129 --cctx_count;
1130 coro_destroy (&cctx->cctx);
1005 1131
1132 /* coro_transfer creates new, empty cctx's */
1133 if (cctx->sptr)
1134 {
1006#if CORO_USE_VALGRIND 1135#if CORO_USE_VALGRIND
1007 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1136 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1008#endif 1137#endif
1009 1138
1010#if HAVE_MMAP 1139#if HAVE_MMAP
1011 if (cctx->flags & CC_MAPPED) 1140 if (cctx->flags & CC_MAPPED)
1012 munmap (cctx->sptr, cctx->ssize); 1141 munmap (cctx->sptr, cctx->ssize);
1013 else 1142 else
1014#endif 1143#endif
1015 Safefree (cctx->sptr); 1144 Safefree (cctx->sptr);
1145 }
1016 1146
1017 Safefree (cctx); 1147 Safefree (cctx);
1018} 1148}
1019 1149
1020/* wether this cctx should be destructed */ 1150/* wether this cctx should be destructed */
1039} 1169}
1040 1170
1041static void 1171static void
1042cctx_put (coro_cctx *cctx) 1172cctx_put (coro_cctx *cctx)
1043{ 1173{
1174 assert (("cctx_put called on non-initialised cctx", cctx->sptr));
1175
1044 /* free another cctx if overlimit */ 1176 /* free another cctx if overlimit */
1045 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1177 if (expect_false (cctx_idle >= MAX_IDLE_CCTX))
1046 { 1178 {
1047 coro_cctx *first = cctx_first; 1179 coro_cctx *first = cctx_first;
1048 cctx_first = first->next; 1180 cctx_first = first->next;
1070 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 1202 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
1071 1203
1072 if (expect_false (next->flags & CF_DESTROYED)) 1204 if (expect_false (next->flags & CF_DESTROYED))
1073 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); 1205 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
1074 1206
1075 if (
1076#if PERL_VERSION_ATLEAST (5,9,0) 1207#if !PERL_VERSION_ATLEAST (5,10,0)
1077 expect_false (PL_parser)
1078#else
1079 expect_false (PL_lex_state != LEX_NOTPARSING) 1208 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1080#endif
1081 )
1082 croak ("Coro::State::transfer called while parsing, but this is not supported"); 1209 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version");
1210#endif
1083 } 1211 }
1084} 1212}
1085 1213
1086/* always use the TRANSFER macro */ 1214/* always use the TRANSFER macro */
1087static void NOINLINE 1215static void NOINLINE
1088transfer (pTHX_ struct coro *prev, struct coro *next) 1216transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1089{ 1217{
1090 dSTACKLEVEL; 1218 dSTACKLEVEL;
1091 1219
1092 /* sometimes transfer is only called to set idle_sp */ 1220 /* sometimes transfer is only called to set idle_sp */
1093 if (expect_false (!next)) 1221 if (expect_false (!next))
1095 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1223 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1096 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1224 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1097 } 1225 }
1098 else if (expect_true (prev != next)) 1226 else if (expect_true (prev != next))
1099 { 1227 {
1228 static volatile int has_throw;
1100 coro_cctx *prev__cctx; 1229 coro_cctx *prev__cctx;
1101 1230
1102 if (expect_false (prev->flags & CF_NEW)) 1231 if (expect_false (prev->flags & CF_NEW))
1103 { 1232 {
1104 /* create a new empty context */ 1233 /* create a new empty/source context */
1234 ++cctx_count;
1105 Newz (0, prev->cctx, 1, coro_cctx); 1235 New (0, prev->cctx, 1, coro_cctx);
1236 prev->cctx->sptr = 0;
1237 coro_create (&prev->cctx->cctx, 0, 0, 0, 0);
1238
1106 prev->flags &= ~CF_NEW; 1239 prev->flags &= ~CF_NEW;
1107 prev->flags |= CF_RUNNING; 1240 prev->flags |= CF_RUNNING;
1108 } 1241 }
1109 1242
1110 prev->flags &= ~CF_RUNNING; 1243 prev->flags &= ~CF_RUNNING;
1111 next->flags |= CF_RUNNING; 1244 next->flags |= CF_RUNNING;
1112 1245
1113 LOCK; 1246 LOCK;
1247
1248 /* first get rid of the old state */
1249 save_perl (aTHX_ prev);
1114 1250
1115 if (expect_false (next->flags & CF_NEW)) 1251 if (expect_false (next->flags & CF_NEW))
1116 { 1252 {
1117 /* need to start coroutine */ 1253 /* need to start coroutine */
1118 next->flags &= ~CF_NEW; 1254 next->flags &= ~CF_NEW;
1119 /* first get rid of the old state */
1120 save_perl (aTHX_ prev);
1121 /* setup coroutine call */ 1255 /* setup coroutine call */
1122 coro_setup (aTHX_ next); 1256 coro_setup (aTHX_ next);
1123 } 1257 }
1124 else 1258 else
1125 {
1126 /* coroutine already started */
1127 save_perl (aTHX_ prev);
1128 load_perl (aTHX_ next); 1259 load_perl (aTHX_ next);
1129 }
1130 1260
1131 prev__cctx = prev->cctx; 1261 prev__cctx = prev->cctx;
1132 1262
1133 /* possibly "free" the cctx */ 1263 /* possibly "free" the cctx */
1134 if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) 1264 if (expect_true (
1265 prev__cctx->idle_sp == STACKLEVEL
1266 && !(prev__cctx->flags & CC_TRACE)
1267 && !force_cctx
1268 ))
1135 { 1269 {
1136 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1270 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
1137 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1271 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
1138 1272
1139 prev->cctx = 0; 1273 prev->cctx = 0;
1149 1283
1150 ++next->usecount; 1284 ++next->usecount;
1151 1285
1152 if (expect_true (!next->cctx)) 1286 if (expect_true (!next->cctx))
1153 next->cctx = cctx_get (aTHX); 1287 next->cctx = cctx_get (aTHX);
1288
1289 has_throw = !!next->throw;
1154 1290
1155 if (expect_false (prev__cctx != next->cctx)) 1291 if (expect_false (prev__cctx != next->cctx))
1156 { 1292 {
1157 prev__cctx->top_env = PL_top_env; 1293 prev__cctx->top_env = PL_top_env;
1158 PL_top_env = next->cctx->top_env; 1294 PL_top_env = next->cctx->top_env;
1160 } 1296 }
1161 1297
1162 free_coro_mortal (aTHX); 1298 free_coro_mortal (aTHX);
1163 UNLOCK; 1299 UNLOCK;
1164 1300
1165 if (expect_false (prev->throw || next->throw)) 1301 if (expect_false (has_throw))
1166 { 1302 {
1167 struct coro *coro = SvSTATE (coro_current); 1303 struct coro *coro = SvSTATE (coro_current);
1168 1304
1169 if (coro->throw) 1305 if (coro->throw)
1170 { 1306 {
1180struct transfer_args 1316struct transfer_args
1181{ 1317{
1182 struct coro *prev, *next; 1318 struct coro *prev, *next;
1183}; 1319};
1184 1320
1185#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1321#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx))
1186#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1322#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1187 1323
1188/** high level stuff ********************************************************/ 1324/** high level stuff ********************************************************/
1189 1325
1190static int 1326static int
1214 croak ("FATAL: tried to destroy currently running coroutine"); 1350 croak ("FATAL: tried to destroy currently running coroutine");
1215 1351
1216 save_perl (aTHX_ &temp); 1352 save_perl (aTHX_ &temp);
1217 load_perl (aTHX_ coro); 1353 load_perl (aTHX_ coro);
1218 1354
1219 coro_destroy (aTHX_ coro); 1355 coro_destruct (aTHX_ coro);
1220 1356
1221 load_perl (aTHX_ &temp); 1357 load_perl (aTHX_ &temp);
1222 1358
1223 coro->slot = 0; 1359 coro->slot = 0;
1224 } 1360 }
1284{ 1420{
1285 dTHX; 1421 dTHX;
1286 struct transfer_args ta; 1422 struct transfer_args ta;
1287 1423
1288 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1424 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1289 TRANSFER (ta); 1425 TRANSFER (ta, 1);
1290} 1426}
1291 1427
1292/** Coro ********************************************************************/ 1428/** Coro ********************************************************************/
1293 1429
1294static void 1430static void
1296{ 1432{
1297 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 1433 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
1298} 1434}
1299 1435
1300static SV * 1436static SV *
1301coro_deq (pTHX_ int min_prio) 1437coro_deq (pTHX)
1302{ 1438{
1303 int prio = PRIO_MAX - PRIO_MIN; 1439 int prio;
1304 1440
1305 min_prio -= PRIO_MIN;
1306 if (min_prio < 0)
1307 min_prio = 0;
1308
1309 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 1441 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1310 if (AvFILLp (coro_ready [prio]) >= 0) 1442 if (AvFILLp (coro_ready [prio]) >= 0)
1311 return av_shift (coro_ready [prio]); 1443 return av_shift (coro_ready [prio]);
1312 1444
1313 return 0; 1445 return 0;
1314} 1446}
1316static int 1448static int
1317api_ready (SV *coro_sv) 1449api_ready (SV *coro_sv)
1318{ 1450{
1319 dTHX; 1451 dTHX;
1320 struct coro *coro; 1452 struct coro *coro;
1453 SV *sv_hook;
1454 void (*xs_hook)(void);
1321 1455
1322 if (SvROK (coro_sv)) 1456 if (SvROK (coro_sv))
1323 coro_sv = SvRV (coro_sv); 1457 coro_sv = SvRV (coro_sv);
1324 1458
1325 coro = SvSTATE (coro_sv); 1459 coro = SvSTATE (coro_sv);
1328 return 0; 1462 return 0;
1329 1463
1330 coro->flags |= CF_READY; 1464 coro->flags |= CF_READY;
1331 1465
1332 LOCK; 1466 LOCK;
1467
1468 sv_hook = coro_nready ? 0 : coro_readyhook;
1469 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1470
1333 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1471 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1334 ++coro_nready; 1472 ++coro_nready;
1473
1335 UNLOCK; 1474 UNLOCK;
1475
1476 if (sv_hook)
1477 {
1478 dSP;
1479
1480 ENTER;
1481 SAVETMPS;
1482
1483 PUSHMARK (SP);
1484 PUTBACK;
1485 call_sv (sv_hook, G_DISCARD);
1486 SPAGAIN;
1487
1488 FREETMPS;
1489 LEAVE;
1490 }
1491
1492 if (xs_hook)
1493 xs_hook ();
1336 1494
1337 return 1; 1495 return 1;
1338} 1496}
1339 1497
1340static int 1498static int
1350 SV *prev_sv, *next_sv; 1508 SV *prev_sv, *next_sv;
1351 1509
1352 for (;;) 1510 for (;;)
1353 { 1511 {
1354 LOCK; 1512 LOCK;
1355 next_sv = coro_deq (aTHX_ PRIO_MIN); 1513 next_sv = coro_deq (aTHX);
1356 1514
1357 /* nothing to schedule: call the idle handler */ 1515 /* nothing to schedule: call the idle handler */
1358 if (expect_false (!next_sv)) 1516 if (expect_false (!next_sv))
1359 { 1517 {
1360 dSP; 1518 dSP;
1364 SAVETMPS; 1522 SAVETMPS;
1365 1523
1366 PUSHMARK (SP); 1524 PUSHMARK (SP);
1367 PUTBACK; 1525 PUTBACK;
1368 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1526 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1527 SPAGAIN;
1369 1528
1370 FREETMPS; 1529 FREETMPS;
1371 LEAVE; 1530 LEAVE;
1372 continue; 1531 continue;
1373 } 1532 }
1428{ 1587{
1429 dTHX; 1588 dTHX;
1430 struct transfer_args ta; 1589 struct transfer_args ta;
1431 1590
1432 prepare_schedule (aTHX_ &ta); 1591 prepare_schedule (aTHX_ &ta);
1433 TRANSFER (ta); 1592 TRANSFER (ta, 1);
1434} 1593}
1435 1594
1436static int 1595static int
1437api_cede (void) 1596api_cede (void)
1438{ 1597{
1441 1600
1442 prepare_cede (aTHX_ &ta); 1601 prepare_cede (aTHX_ &ta);
1443 1602
1444 if (expect_true (ta.prev != ta.next)) 1603 if (expect_true (ta.prev != ta.next))
1445 { 1604 {
1446 TRANSFER (ta); 1605 TRANSFER (ta, 1);
1447 return 1; 1606 return 1;
1448 } 1607 }
1449 else 1608 else
1450 return 0; 1609 return 0;
1451} 1610}
1456 dTHX; 1615 dTHX;
1457 struct transfer_args ta; 1616 struct transfer_args ta;
1458 1617
1459 if (prepare_cede_notself (aTHX_ &ta)) 1618 if (prepare_cede_notself (aTHX_ &ta))
1460 { 1619 {
1461 TRANSFER (ta); 1620 TRANSFER (ta, 1);
1462 return 1; 1621 return 1;
1463 } 1622 }
1464 else 1623 else
1465 return 0; 1624 return 0;
1466} 1625}
1489 else 1648 else
1490 coro->slot->runops = RUNOPS_DEFAULT; 1649 coro->slot->runops = RUNOPS_DEFAULT;
1491 } 1650 }
1492} 1651}
1493 1652
1653static int
1654coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1655{
1656 AV *padlist;
1657 AV *av = (AV *)mg->mg_obj;
1658
1659 abort ();
1660
1661 return 0;
1662}
1663
1664static MGVTBL coro_gensub_vtbl = {
1665 0, 0, 0, 0,
1666 coro_gensub_free
1667};
1668
1669/*****************************************************************************/
1670/* PerlIO::cede */
1671
1672typedef struct
1673{
1674 PerlIOBuf base;
1675 NV next, every;
1676} PerlIOCede;
1677
1678static IV
1679PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1680{
1681 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1682
1683 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1684 self->next = nvtime () + self->every;
1685
1686 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1687}
1688
1689static SV *
1690PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
1691{
1692 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1693
1694 return newSVnv (self->every);
1695}
1696
1697static IV
1698PerlIOCede_flush (pTHX_ PerlIO *f)
1699{
1700 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1701 double now = nvtime ();
1702
1703 if (now >= self->next)
1704 {
1705 api_cede ();
1706 self->next = now + self->every;
1707 }
1708
1709 return PerlIOBuf_flush (aTHX_ f);
1710}
1711
1712static PerlIO_funcs PerlIO_cede =
1713{
1714 sizeof(PerlIO_funcs),
1715 "cede",
1716 sizeof(PerlIOCede),
1717 PERLIO_K_DESTRUCT | PERLIO_K_RAW,
1718 PerlIOCede_pushed,
1719 PerlIOBuf_popped,
1720 PerlIOBuf_open,
1721 PerlIOBase_binmode,
1722 PerlIOCede_getarg,
1723 PerlIOBase_fileno,
1724 PerlIOBuf_dup,
1725 PerlIOBuf_read,
1726 PerlIOBuf_unread,
1727 PerlIOBuf_write,
1728 PerlIOBuf_seek,
1729 PerlIOBuf_tell,
1730 PerlIOBuf_close,
1731 PerlIOCede_flush,
1732 PerlIOBuf_fill,
1733 PerlIOBase_eof,
1734 PerlIOBase_error,
1735 PerlIOBase_clearerr,
1736 PerlIOBase_setlinebuf,
1737 PerlIOBuf_get_base,
1738 PerlIOBuf_bufsiz,
1739 PerlIOBuf_get_ptr,
1740 PerlIOBuf_get_cnt,
1741 PerlIOBuf_set_ptrcnt,
1742};
1743
1744
1494MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1745MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1495 1746
1496PROTOTYPES: DISABLE 1747PROTOTYPES: DISABLE
1497 1748
1498BOOT: 1749BOOT:
1499{ 1750{
1500#ifdef USE_ITHREADS 1751#ifdef USE_ITHREADS
1501 MUTEX_INIT (&coro_mutex); 1752 MUTEX_INIT (&coro_lock);
1502#endif 1753#endif
1503 BOOT_PAGESIZE; 1754 BOOT_PAGESIZE;
1504 1755
1505 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1756 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1506 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1757 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1507 1758
1759 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1760 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1761 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1762
1508 hv_sig = coro_get_hv ("SIG", TRUE); 1763 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1509 sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); 1764 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1510 sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); 1765 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1511
1512 if (!PL_diehook ) hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);
1513 if (!PL_warnhook) hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);
1514 1766
1515 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1767 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1516 1768
1517 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1769 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1518 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1770 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1523 main_top_env = PL_top_env; 1775 main_top_env = PL_top_env;
1524 1776
1525 while (main_top_env->je_prev) 1777 while (main_top_env->je_prev)
1526 main_top_env = main_top_env->je_prev; 1778 main_top_env = main_top_env->je_prev;
1527 1779
1528 coroapi.ver = CORO_API_VERSION; 1780 coroapi.ver = CORO_API_VERSION;
1781 coroapi.rev = CORO_API_REVISION;
1529 coroapi.transfer = api_transfer; 1782 coroapi.transfer = api_transfer;
1783
1784 {
1785 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1786
1787 if (!svp) croak ("Time::HiRes is required");
1788 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1789
1790 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1791 }
1530 1792
1531 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1793 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1532} 1794}
1533 1795
1534SV * 1796SV *
1535new (char *klass, ...) 1797new (char *klass, ...)
1536 CODE: 1798 CODE:
1537{ 1799{
1538 struct coro *coro; 1800 struct coro *coro;
1801 MAGIC *mg;
1539 HV *hv; 1802 HV *hv;
1540 int i; 1803 int i;
1541 1804
1542 Newz (0, coro, 1, struct coro); 1805 Newz (0, coro, 1, struct coro);
1543 coro->args = newAV (); 1806 coro->args = newAV ();
1546 if (coro_first) coro_first->prev = coro; 1809 if (coro_first) coro_first->prev = coro;
1547 coro->next = coro_first; 1810 coro->next = coro_first;
1548 coro_first = coro; 1811 coro_first = coro;
1549 1812
1550 coro->hv = hv = newHV (); 1813 coro->hv = hv = newHV ();
1551 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1814 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
1815 mg->mg_flags |= MGf_DUP;
1552 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1816 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1553 1817
1554 av_extend (coro->args, items - 1); 1818 av_extend (coro->args, items - 1);
1555 for (i = 1; i < items; i++) 1819 for (i = 1; i < items; i++)
1556 av_push (coro->args, newSVsv (ST (i))); 1820 av_push (coro->args, newSVsv (ST (i)));
1570 Coro::cede_notself = 4 1834 Coro::cede_notself = 4
1571 CODE: 1835 CODE:
1572{ 1836{
1573 struct transfer_args ta; 1837 struct transfer_args ta;
1574 1838
1839 PUTBACK;
1575 switch (ix) 1840 switch (ix)
1576 { 1841 {
1577 case 0: 1842 case 0:
1578 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1843 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1579 ta.next = 0; 1844 ta.next = 0;
1580 break; 1845 break;
1581 1846
1582 case 1: 1847 case 1:
1583 if (items != 2) 1848 if (items != 2)
1584 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1849 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1585 1850
1586 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1851 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1587 break; 1852 break;
1588 1853
1589 case 2: 1854 case 2:
1598 if (!prepare_cede_notself (aTHX_ &ta)) 1863 if (!prepare_cede_notself (aTHX_ &ta))
1599 XSRETURN_EMPTY; 1864 XSRETURN_EMPTY;
1600 1865
1601 break; 1866 break;
1602 } 1867 }
1868 SPAGAIN;
1603 1869
1604 BARRIER; 1870 BARRIER;
1871 PUTBACK;
1605 TRANSFER (ta); 1872 TRANSFER (ta, 0);
1606 1873 SPAGAIN; /* might be the sp of a different coroutine now */
1607 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) 1874 /* be extra careful not to ever do anything after TRANSFER */
1608 XSRETURN_YES;
1609} 1875}
1610 1876
1611bool 1877bool
1612_destroy (SV *coro_sv) 1878_destroy (SV *coro_sv)
1613 CODE: 1879 CODE:
1614 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 1880 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1615 OUTPUT: 1881 OUTPUT:
1616 RETVAL 1882 RETVAL
1617 1883
1618void 1884void
1619_exit (code) 1885_exit (int code)
1620 int code
1621 PROTOTYPE: $ 1886 PROTOTYPE: $
1622 CODE: 1887 CODE:
1623 _exit (code); 1888 _exit (code);
1624 1889
1625int 1890int
1659call (Coro::State coro, SV *coderef) 1924call (Coro::State coro, SV *coderef)
1660 ALIAS: 1925 ALIAS:
1661 eval = 1 1926 eval = 1
1662 CODE: 1927 CODE:
1663{ 1928{
1664 if (coro->mainstack) 1929 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1665 { 1930 {
1666 struct coro temp; 1931 struct coro temp;
1667 1932
1668 if (!(coro->flags & CF_RUNNING)) 1933 if (!(coro->flags & CF_RUNNING))
1669 { 1934 {
1935 PUTBACK;
1670 save_perl (aTHX_ &temp); 1936 save_perl (aTHX_ &temp);
1671 load_perl (aTHX_ coro); 1937 load_perl (aTHX_ coro);
1672 } 1938 }
1673 1939
1674 { 1940 {
1675 dSP; 1941 dSP;
1676 ENTER; 1942 ENTER;
1677 SAVETMPS; 1943 SAVETMPS;
1944 PUTBACK;
1945 PUSHSTACK;
1678 PUSHMARK (SP); 1946 PUSHMARK (SP);
1679 PUTBACK;
1680 1947
1681 if (ix) 1948 if (ix)
1682 eval_sv (coderef, 0); 1949 eval_sv (coderef, 0);
1683 else 1950 else
1684 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1951 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1685 1952
1953 POPSTACK;
1686 SPAGAIN; 1954 SPAGAIN;
1687 FREETMPS; 1955 FREETMPS;
1688 LEAVE; 1956 LEAVE;
1689 PUTBACK; 1957 PUTBACK;
1690 } 1958 }
1691 1959
1692 if (!(coro->flags & CF_RUNNING)) 1960 if (!(coro->flags & CF_RUNNING))
1693 { 1961 {
1694 save_perl (aTHX_ coro); 1962 save_perl (aTHX_ coro);
1695 load_perl (aTHX_ &temp); 1963 load_perl (aTHX_ &temp);
1964 SPAGAIN;
1696 } 1965 }
1697 } 1966 }
1698} 1967}
1699 1968
1700SV * 1969SV *
1712 1981
1713void 1982void
1714api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 1983api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1715 1984
1716SV * 1985SV *
1717has_stack (Coro::State coro) 1986has_cctx (Coro::State coro)
1718 PROTOTYPE: $ 1987 PROTOTYPE: $
1719 CODE: 1988 CODE:
1720 RETVAL = boolSV (!!coro->cctx); 1989 RETVAL = boolSV (!!coro->cctx);
1721 OUTPUT: 1990 OUTPUT:
1722 RETVAL 1991 RETVAL
1741 case 1: RETVAL = coro->usecount; break; 2010 case 1: RETVAL = coro->usecount; break;
1742 } 2011 }
1743 OUTPUT: 2012 OUTPUT:
1744 RETVAL 2013 RETVAL
1745 2014
2015void
2016force_cctx ()
2017 CODE:
2018 struct coro *coro = SvSTATE (coro_current);
2019 coro->cctx->idle_sp = 0;
2020
2021void
2022swap_defsv (Coro::State self)
2023 PROTOTYPE: $
2024 ALIAS:
2025 swap_defav = 1
2026 CODE:
2027 if (!self->slot)
2028 croak ("cannot swap state with coroutine that has no saved state");
2029 else
2030 {
2031 SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv);
2032 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
2033
2034 SV *tmp = *src; *src = *dst; *dst = tmp;
2035 }
1746 2036
1747MODULE = Coro::State PACKAGE = Coro 2037MODULE = Coro::State PACKAGE = Coro
1748 2038
1749BOOT: 2039BOOT:
1750{ 2040{
1751 int i; 2041 int i;
1752 2042
1753 sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE);
1754 sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE);
1755 av_async_pool = coro_get_av ("Coro::async_pool", TRUE); 2043 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2044 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2045 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1756 2046
1757 coro_current = coro_get_sv ("Coro::current", FALSE); 2047 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1758 SvREADONLY_on (coro_current); 2048 SvREADONLY_on (coro_current);
1759 2049
1760 coro_stash = gv_stashpv ("Coro", TRUE); 2050 coro_stash = gv_stashpv ("Coro", TRUE);
1761 2051
1762 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 2052 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1790void 2080void
1791_set_current (SV *current) 2081_set_current (SV *current)
1792 PROTOTYPE: $ 2082 PROTOTYPE: $
1793 CODE: 2083 CODE:
1794 SvREFCNT_dec (SvRV (coro_current)); 2084 SvREFCNT_dec (SvRV (coro_current));
1795 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 2085 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2086
2087void
2088_set_readyhook (SV *hook)
2089 PROTOTYPE: $
2090 CODE:
2091 LOCK;
2092 SvREFCNT_dec (coro_readyhook);
2093 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2094 UNLOCK;
1796 2095
1797int 2096int
1798prio (Coro::State coro, int newprio = 0) 2097prio (Coro::State coro, int newprio = 0)
1799 ALIAS: 2098 ALIAS:
1800 nice = 1 2099 nice = 1
1845 CODE: 2144 CODE:
1846{ 2145{
1847 struct coro *coro = SvSTATE (coro_current); 2146 struct coro *coro = SvSTATE (coro_current);
1848 HV *hv = (HV *)SvRV (coro_current); 2147 HV *hv = (HV *)SvRV (coro_current);
1849 AV *defav = GvAV (PL_defgv); 2148 AV *defav = GvAV (PL_defgv);
1850 SV *invoke = hv_delete (hv, strpair ("_invoke"), 0); 2149 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1851 AV *invoke_av; 2150 AV *invoke_av;
1852 int i, len; 2151 int i, len;
1853 2152
1854 if (!invoke) 2153 if (!invoke)
2154 {
2155 SV *old = PL_diehook;
2156 PL_diehook = 0;
2157 SvREFCNT_dec (old);
1855 croak ("\3async_pool terminate\2\n"); 2158 croak ("\3async_pool terminate\2\n");
2159 }
1856 2160
1857 SvREFCNT_dec (coro->saved_deffh); 2161 SvREFCNT_dec (coro->saved_deffh);
1858 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2162 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1859 2163
1860 hv_store (hv, "desc", sizeof ("desc") - 1, 2164 hv_store (hv, "desc", sizeof ("desc") - 1,
1861 newSVpvn (strpair ("[async_pool]")), 0); 2165 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1862 2166
1863 invoke_av = (AV *)SvRV (invoke); 2167 invoke_av = (AV *)SvRV (invoke);
1864 len = av_len (invoke_av); 2168 len = av_len (invoke_av);
1865 2169
1866 sv_setsv (cb, AvARRAY (invoke_av)[0]); 2170 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1867 2171
1868 if (len > 0) 2172 if (len > 0)
1869 { 2173 {
1870 av_fill (defav, len - 1); 2174 av_fill (defav, len - 1);
1871 for (i = 0; i < len; ++i) 2175 for (i = 0; i < len; ++i)
1872 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2176 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
1873 } 2177 }
1874 2178
1875 SvREFCNT_dec (invoke); 2179 SvREFCNT_dec (invoke);
1876} 2180}
1877 2181
1886 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2190 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1887 coro->saved_deffh = 0; 2191 coro->saved_deffh = 0;
1888 2192
1889 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2193 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1890 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2194 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2195 {
2196 SV *old = PL_diehook;
2197 PL_diehook = 0;
2198 SvREFCNT_dec (old);
1891 croak ("\3async_pool terminate\2\n"); 2199 croak ("\3async_pool terminate\2\n");
2200 }
1892 2201
1893 av_clear (GvAV (PL_defgv)); 2202 av_clear (GvAV (PL_defgv));
1894 hv_store ((HV *)SvRV (coro_current), strpair ("desc"), 2203 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1895 newSVpvn (strpair ("[async_pool idle]")), 0); 2204 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1896 2205
1897 coro->prio = 0; 2206 coro->prio = 0;
1898 2207
1899 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 2208 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1900 api_trace (coro_current, 0); 2209 api_trace (coro_current, 0);
1901 2210
1902 av_push (av_async_pool, newSVsv (coro_current)); 2211 av_push (av_async_pool, newSVsv (coro_current));
1903} 2212}
1904 2213
2214#if 0
2215
2216void
2217_generator_call (...)
2218 PROTOTYPE: @
2219 PPCODE:
2220 fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr);
2221 xxxx
2222 abort ();
2223
2224SV *
2225gensub (SV *sub, ...)
2226 PROTOTYPE: &;@
2227 CODE:
2228{
2229 struct coro *coro;
2230 MAGIC *mg;
2231 CV *xcv;
2232 CV *ncv = (CV *)newSV_type (SVt_PVCV);
2233 int i;
2234
2235 CvGV (ncv) = CvGV (cv);
2236 CvFILE (ncv) = CvFILE (cv);
2237
2238 Newz (0, coro, 1, struct coro);
2239 coro->args = newAV ();
2240 coro->flags = CF_NEW;
2241
2242 av_extend (coro->args, items - 1);
2243 for (i = 1; i < items; i++)
2244 av_push (coro->args, newSVsv (ST (i)));
2245
2246 CvISXSUB_on (ncv);
2247 CvXSUBANY (ncv).any_ptr = (void *)coro;
2248
2249 xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV));
2250
2251 CvXSUB (ncv) = CvXSUB (xcv);
2252 CvANON_on (ncv);
2253
2254 mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0);
2255 RETVAL = newRV_noinc ((SV *)ncv);
2256}
2257 OUTPUT:
2258 RETVAL
2259
2260#endif
2261
1905 2262
1906MODULE = Coro::State PACKAGE = Coro::AIO 2263MODULE = Coro::State PACKAGE = Coro::AIO
1907 2264
1908SV * 2265void
1909_get_state () 2266_get_state (SV *self)
1910 CODE: 2267 PPCODE:
1911{ 2268{
1912 struct io_state *data; 2269 AV *defav = GvAV (PL_defgv);
1913 2270 AV *av = newAV ();
2271 int i;
1914 RETVAL = newSV (sizeof (struct io_state)); 2272 SV *data_sv = newSV (sizeof (struct io_state));
1915 data = (struct io_state *)SvPVX (RETVAL); 2273 struct io_state *data = (struct io_state *)SvPVX (data_sv);
1916 SvCUR_set (RETVAL, sizeof (struct io_state)); 2274 SvCUR_set (data_sv, sizeof (struct io_state));
1917 SvPOK_only (RETVAL); 2275 SvPOK_only (data_sv);
1918 2276
1919 data->errorno = errno; 2277 data->errorno = errno;
1920 data->laststype = PL_laststype; 2278 data->laststype = PL_laststype;
1921 data->laststatval = PL_laststatval; 2279 data->laststatval = PL_laststatval;
1922 data->statcache = PL_statcache; 2280 data->statcache = PL_statcache;
2281
2282 av_extend (av, AvFILLp (defav) + 1 + 1);
2283
2284 for (i = 0; i <= AvFILLp (defav); ++i)
2285 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2286
2287 av_push (av, data_sv);
2288
2289 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2290
2291 api_ready (self);
1923} 2292}
1924 OUTPUT:
1925 RETVAL
1926 2293
1927void 2294void
1928_set_state (char *data_) 2295_set_state (SV *state)
1929 PROTOTYPE: $ 2296 PROTOTYPE: $
1930 CODE: 2297 PPCODE:
1931{ 2298{
1932 struct io_state *data = (void *)data_; 2299 AV *av = (AV *)SvRV (state);
2300 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2301 int i;
1933 2302
1934 errno = data->errorno; 2303 errno = data->errorno;
1935 PL_laststype = data->laststype; 2304 PL_laststype = data->laststype;
1936 PL_laststatval = data->laststatval; 2305 PL_laststatval = data->laststatval;
1937 PL_statcache = data->statcache; 2306 PL_statcache = data->statcache;
1938}
1939 2307
2308 EXTEND (SP, AvFILLp (av));
2309 for (i = 0; i < AvFILLp (av); ++i)
2310 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2311}
2312
2313
2314MODULE = Coro::State PACKAGE = Coro::AnyEvent
2315
2316BOOT:
2317 sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE);
2318
2319SV *
2320_schedule (...)
2321 PROTOTYPE: @
2322 CODE:
2323{
2324 static int incede;
2325
2326 api_cede_notself ();
2327
2328 ++incede;
2329 while (coro_nready >= incede && api_cede ())
2330 ;
2331
2332 sv_setsv (sv_activity, &PL_sv_undef);
2333 if (coro_nready >= incede)
2334 {
2335 PUSHMARK (SP);
2336 PUTBACK;
2337 call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL);
2338 SPAGAIN;
2339 }
2340
2341 --incede;
2342}
2343
2344
2345MODULE = Coro::State PACKAGE = PerlIO::cede
2346
2347BOOT:
2348 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines