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.205 by root, Tue Oct 9 14:07:02 2007 UTC vs.
Revision 1.268 by root, Fri Nov 14 06:41:41 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
36# define BOOT_PAGESIZE (void)0 46# define BOOT_PAGESIZE (void)0
37#endif 47#endif
38 48
39#if CORO_USE_VALGRIND 49#if CORO_USE_VALGRIND
40# include <valgrind/valgrind.h> 50# include <valgrind/valgrind.h>
41# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end))
42#else
43# define REGISTER_STACK(cctx,start,end)
44#endif 51#endif
45 52
46/* the maximum number of idle cctx that will be pooled */ 53/* the maximum number of idle cctx that will be pooled */
47#define MAX_IDLE_CCTX 8 54static int cctx_max_idle = 4;
48 55
49#define PERL_VERSION_ATLEAST(a,b,c) \ 56#define PERL_VERSION_ATLEAST(a,b,c) \
50 (PERL_REVISION > (a) \ 57 (PERL_REVISION > (a) \
51 || (PERL_REVISION == (a) \ 58 || (PERL_REVISION == (a) \
52 && (PERL_VERSION > (b) \ 59 && (PERL_VERSION > (b) \
71# ifndef IS_PADCONST 78# ifndef IS_PADCONST
72# define IS_PADCONST(v) 0 79# define IS_PADCONST(v) 0
73# endif 80# endif
74#endif 81#endif
75 82
83/* 5.11 */
84#ifndef CxHASARGS
85# define CxHASARGS(cx) (cx)->blk_sub.hasargs
86#endif
87
88/* 5.10.0 */
89#ifndef SvREFCNT_inc_NN
90# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
91#endif
92
93/* 5.8.8 */
94#ifndef GV_NOTQUAL
95# define GV_NOTQUAL 0
96#endif
97#ifndef newSV
98# define newSV(l) NEWSV(0,l)
99#endif
100
76/* 5.8.7 */ 101/* 5.8.7 */
77#ifndef SvRV_set 102#ifndef SvRV_set
78# define SvRV_set(s,v) SvRV(s) = (v) 103# define SvRV_set(s,v) SvRV(s) = (v)
79#endif 104#endif
80 105
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
88
89#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 106#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
90# undef CORO_STACKGUARD 107# undef CORO_STACKGUARD
91#endif 108#endif
92 109
93#ifndef CORO_STACKGUARD 110#ifndef CORO_STACKGUARD
99# define CORO_PREFER_PERL_FUNCTIONS 0 116# define CORO_PREFER_PERL_FUNCTIONS 0
100#endif 117#endif
101 118
102/* The next macros try to return the current stack pointer, in an as 119/* The next macros try to return the current stack pointer, in an as
103 * portable way as possible. */ 120 * portable way as possible. */
104#define dSTACKLEVEL volatile char stacklevel 121#if __GNUC__ >= 4
105#define STACKLEVEL ((void *)&stacklevel) 122# define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0)
123#else
124# define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel
125#endif
106 126
107#define IN_DESTRUCT (PL_main_cv == Nullcv) 127#define IN_DESTRUCT (PL_main_cv == Nullcv)
108 128
109#if __GNUC__ >= 3 129#if __GNUC__ >= 3
110# define attribute(x) __attribute__(x) 130# define attribute(x) __attribute__(x)
111# define BARRIER __asm__ __volatile__ ("" : : : "memory")
112# define expect(expr,value) __builtin_expect ((expr),(value)) 131# define expect(expr,value) __builtin_expect ((expr),(value))
132# define INLINE static inline
113#else 133#else
114# define attribute(x) 134# define attribute(x)
115# define BARRIER
116# define expect(expr,value) (expr) 135# define expect(expr,value) (expr)
136# define INLINE static
117#endif 137#endif
118 138
119#define expect_false(expr) expect ((expr) != 0, 0) 139#define expect_false(expr) expect ((expr) != 0, 0)
120#define expect_true(expr) expect ((expr) != 0, 1) 140#define expect_true(expr) expect ((expr) != 0, 1)
121 141
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
147
127static perl_mutex coro_mutex; 148static perl_mutex coro_lock;
128# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 149# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
129# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 150# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
151# if CORO_PTHREAD
152static void *coro_thx;
153# endif
154
130#else 155#else
156
131# define LOCK (void)0 157# define LOCK (void)0
132# define UNLOCK (void)0 158# define UNLOCK (void)0
133#endif
134 159
135#define strpair(const) const, sizeof (const) - 1 160#endif
161
162# undef LOCK
163# define LOCK (void)0
164# undef UNLOCK
165# define UNLOCK (void)0
136 166
137/* helper storage struct for Coro::AIO */ 167/* helper storage struct for Coro::AIO */
138struct io_state 168struct io_state
139{ 169{
170 AV *res;
140 int errorno; 171 int errorno;
141 I32 laststype; 172 I32 laststype; /* U16 in 5.10.0 */
142 int laststatval; 173 int laststatval;
143 Stat_t statcache; 174 Stat_t statcache;
144}; 175};
145 176
177static double (*nvtime)(); /* so why doesn't it take void? */
178
179static U32 cctx_gen;
146static size_t coro_stacksize = CORO_STACKSIZE; 180static size_t cctx_stacksize = CORO_STACKSIZE;
147static struct CoroAPI coroapi; 181static struct CoroAPI coroapi;
148static AV *main_mainstack; /* used to differentiate between $main and others */ 182static AV *main_mainstack; /* used to differentiate between $main and others */
149static JMPENV *main_top_env; 183static JMPENV *main_top_env;
150static HV *coro_state_stash, *coro_stash; 184static HV *coro_state_stash, *coro_stash;
151static SV *coro_mortal; /* will be freed after next transfer */ 185static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
186static volatile struct coro *transfer_next;
187
188struct transfer_args
189{
190 struct coro *prev, *next;
191};
152 192
153static GV *irsgv; /* $/ */ 193static GV *irsgv; /* $/ */
154static GV *stdoutgv; /* *STDOUT */ 194static GV *stdoutgv; /* *STDOUT */
155 195static SV *rv_diehook;
196static SV *rv_warnhook;
156static HV *hv_sig; /* %SIG */ 197static HV *hv_sig; /* %SIG */
157static SV *sv_diehook;
158static SV *sv_warnhook;
159 198
160/* async_pool helper stuff */ 199/* async_pool helper stuff */
161static SV *sv_pool_rss; 200static SV *sv_pool_rss;
162static SV *sv_pool_size; 201static SV *sv_pool_size;
163static AV *av_async_pool; 202static AV *av_async_pool;
203
204/* Coro::AnyEvent */
205static SV *sv_activity;
164 206
165static struct coro_cctx *cctx_first; 207static struct coro_cctx *cctx_first;
166static int cctx_count, cctx_idle; 208static int cctx_count, cctx_idle;
167 209
168enum { 210enum {
186 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 228 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
187 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 229 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
188 JMPENV *top_env; 230 JMPENV *top_env;
189 coro_context cctx; 231 coro_context cctx;
190 232
233 U32 gen;
191#if CORO_USE_VALGRIND 234#if CORO_USE_VALGRIND
192 int valgrind_id; 235 int valgrind_id;
193#endif 236#endif
194 unsigned char flags; 237 unsigned char flags;
195} coro_cctx; 238} coro_cctx;
254#define PRIO_IDLE -3 297#define PRIO_IDLE -3
255#define PRIO_MIN -4 298#define PRIO_MIN -4
256 299
257/* for Coro.pm */ 300/* for Coro.pm */
258static SV *coro_current; 301static SV *coro_current;
302static SV *coro_readyhook;
259static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 303static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
260static int coro_nready; 304static int coro_nready;
261static struct coro *coro_first; 305static struct coro *coro_first;
262 306
263/** lowlevel stuff **********************************************************/ 307/** lowlevel stuff **********************************************************/
264 308
265static SV * 309static SV *
266coro_get_sv (const char *name, int create) 310coro_get_sv (pTHX_ const char *name, int create)
267{ 311{
268#if PERL_VERSION_ATLEAST (5,9,0) 312#if PERL_VERSION_ATLEAST (5,10,0)
269 /* 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 */
270 get_sv (name, create); 314 get_sv (name, create);
271#endif 315#endif
272 return get_sv (name, create); 316 return get_sv (name, create);
273} 317}
274 318
275static AV * 319static AV *
276coro_get_av (const char *name, int create) 320coro_get_av (pTHX_ const char *name, int create)
277{ 321{
278#if PERL_VERSION_ATLEAST (5,9,0) 322#if PERL_VERSION_ATLEAST (5,10,0)
279 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 323 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
280 get_av (name, create); 324 get_av (name, create);
281#endif 325#endif
282 return get_av (name, create); 326 return get_av (name, create);
283} 327}
284 328
285static HV * 329static HV *
286coro_get_hv (const char *name, int create) 330coro_get_hv (pTHX_ const char *name, int create)
287{ 331{
288#if PERL_VERSION_ATLEAST (5,9,0) 332#if PERL_VERSION_ATLEAST (5,10,0)
289 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 333 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
290 get_hv (name, create); 334 get_hv (name, create);
291#endif 335#endif
292 return get_hv (name, create); 336 return get_hv (name, create);
293} 337}
298 AV *padlist = CvPADLIST (cv); 342 AV *padlist = CvPADLIST (cv);
299 AV *newpadlist, *newpad; 343 AV *newpadlist, *newpad;
300 344
301 newpadlist = newAV (); 345 newpadlist = newAV ();
302 AvREAL_off (newpadlist); 346 AvREAL_off (newpadlist);
303#if PERL_VERSION_ATLEAST (5,9,0) 347#if PERL_VERSION_ATLEAST (5,10,0)
304 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); 348 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
305#else 349#else
306 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 350 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
307#endif 351#endif
308 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 352 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
309 --AvFILLp (padlist); 353 --AvFILLp (padlist);
310 354
311 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 355 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
312 av_store (newpadlist, 1, (SV *)newpad); 356 av_store (newpadlist, 1, (SV *)newpad);
313 357
314 return newpadlist; 358 return newpadlist;
315} 359}
316 360
346 390
347 /* casting is fun. */ 391 /* casting is fun. */
348 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 392 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
349 free_padlist (aTHX_ padlist); 393 free_padlist (aTHX_ padlist);
350 394
351 SvREFCNT_dec (av); 395 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
352 396
353 return 0; 397 return 0;
354} 398}
355 399
356#define PERL_MAGIC_coro PERL_MAGIC_ext 400#define CORO_MAGIC_type_cv PERL_MAGIC_ext
401#define CORO_MAGIC_type_state PERL_MAGIC_ext
357 402
358static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 403static MGVTBL coro_cv_vtbl = {
404 0, 0, 0, 0,
405 coro_cv_free
406};
359 407
360#define CORO_MAGIC(cv) \ 408#define CORO_MAGIC(sv, type) \
361 SvMAGIC (cv) \ 409 SvMAGIC (sv) \
362 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 410 ? SvMAGIC (sv)->mg_type == type \
363 ? SvMAGIC (cv) \ 411 ? SvMAGIC (sv) \
364 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 412 : mg_find (sv, type) \
365 : 0 413 : 0
366 414
367static struct coro * 415#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
416#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
417
418INLINE struct coro *
368SvSTATE_ (pTHX_ SV *coro) 419SvSTATE_ (pTHX_ SV *coro)
369{ 420{
370 HV *stash; 421 HV *stash;
371 MAGIC *mg; 422 MAGIC *mg;
372 423
382 /* very slow, but rare, check */ 433 /* very slow, but rare, check */
383 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 434 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
384 croak ("Coro::State object required"); 435 croak ("Coro::State object required");
385 } 436 }
386 437
387 mg = CORO_MAGIC (coro); 438 mg = CORO_MAGIC_state (coro);
388 return (struct coro *)mg->mg_ptr; 439 return (struct coro *)mg->mg_ptr;
389} 440}
390 441
391#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) 442#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
392 443
393/* the next two functions merely cache the padlists */ 444/* the next two functions merely cache the padlists */
394static void 445static void
395get_padlist (pTHX_ CV *cv) 446get_padlist (pTHX_ CV *cv)
396{ 447{
397 MAGIC *mg = CORO_MAGIC (cv); 448 MAGIC *mg = CORO_MAGIC_cv (cv);
398 AV *av; 449 AV *av;
399 450
400 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 451 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
401 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 452 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
402 else 453 else
403 { 454 {
404#if CORO_PREFER_PERL_FUNCTIONS 455#if CORO_PREFER_PERL_FUNCTIONS
405 /* this is probably cleaner, but also slower? */ 456 /* this is probably cleaner? but also slower! */
457 /* in practise, it seems to be less stable */
406 CV *cp = Perl_cv_clone (cv); 458 CV *cp = Perl_cv_clone (cv);
407 CvPADLIST (cv) = CvPADLIST (cp); 459 CvPADLIST (cv) = CvPADLIST (cp);
408 CvPADLIST (cp) = 0; 460 CvPADLIST (cp) = 0;
409 SvREFCNT_dec (cp); 461 SvREFCNT_dec (cp);
410#else 462#else
414} 466}
415 467
416static void 468static void
417put_padlist (pTHX_ CV *cv) 469put_padlist (pTHX_ CV *cv)
418{ 470{
419 MAGIC *mg = CORO_MAGIC (cv); 471 MAGIC *mg = CORO_MAGIC_cv (cv);
420 AV *av; 472 AV *av;
421 473
422 if (expect_false (!mg)) 474 if (expect_false (!mg))
423 { 475 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
424 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
425 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
426 mg->mg_virtual = &vtbl_coro;
427 mg->mg_obj = (SV *)newAV ();
428 }
429 476
430 av = (AV *)mg->mg_obj; 477 av = (AV *)mg->mg_obj;
431 478
432 if (expect_false (AvFILLp (av) >= AvMAX (av))) 479 if (expect_false (AvFILLp (av) >= AvMAX (av)))
433 av_extend (av, AvMAX (av) + 1); 480 av_extend (av, AvMAX (av) + 1);
451 GvSV (irsgv) = slot->irsgv; 498 GvSV (irsgv) = slot->irsgv;
452 499
453 #define VAR(name,type) PL_ ## name = slot->name; 500 #define VAR(name,type) PL_ ## name = slot->name;
454 # include "state.h" 501 # include "state.h"
455 #undef VAR 502 #undef VAR
456
457 /*hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);*/
458 /*hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);*/
459 503
460 { 504 {
461 dSP; 505 dSP;
462 506
463 CV *cv; 507 CV *cv;
502 546
503 if (expect_true (CvDEPTH (cv))) 547 if (expect_true (CvDEPTH (cv)))
504 { 548 {
505 EXTEND (SP, 3); 549 EXTEND (SP, 3);
506 PUSHs ((SV *)CvPADLIST (cv)); 550 PUSHs ((SV *)CvPADLIST (cv));
507 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 551 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
508 PUSHs ((SV *)cv); 552 PUSHs ((SV *)cv);
509 553
510 CvDEPTH (cv) = 0; 554 CvDEPTH (cv) = 0;
511 get_padlist (aTHX_ cv); 555 get_padlist (aTHX_ cv);
512 } 556 }
592 636
593 New(54,PL_savestack,24,ANY); 637 New(54,PL_savestack,24,ANY);
594 PL_savestack_ix = 0; 638 PL_savestack_ix = 0;
595 PL_savestack_max = 24; 639 PL_savestack_max = 24;
596 640
597#if !PERL_VERSION_ATLEAST (5,9,0) 641#if !PERL_VERSION_ATLEAST (5,10,0)
598 New(54,PL_retstack,4,OP*); 642 New(54,PL_retstack,4,OP*);
599 PL_retstack_ix = 0; 643 PL_retstack_ix = 0;
600 PL_retstack_max = 4; 644 PL_retstack_max = 4;
601#endif 645#endif
602} 646}
604 648
605/* 649/*
606 * destroy the stacks, the callchain etc... 650 * destroy the stacks, the callchain etc...
607 */ 651 */
608static void 652static void
609coro_destroy_stacks (pTHX) 653coro_destruct_stacks (pTHX)
610{ 654{
611 while (PL_curstackinfo->si_next) 655 while (PL_curstackinfo->si_next)
612 PL_curstackinfo = PL_curstackinfo->si_next; 656 PL_curstackinfo = PL_curstackinfo->si_next;
613 657
614 while (PL_curstackinfo) 658 while (PL_curstackinfo)
625 669
626 Safefree (PL_tmps_stack); 670 Safefree (PL_tmps_stack);
627 Safefree (PL_markstack); 671 Safefree (PL_markstack);
628 Safefree (PL_scopestack); 672 Safefree (PL_scopestack);
629 Safefree (PL_savestack); 673 Safefree (PL_savestack);
630#if !PERL_VERSION_ATLEAST (5,9,0) 674#if !PERL_VERSION_ATLEAST (5,10,0)
631 Safefree (PL_retstack); 675 Safefree (PL_retstack);
632#endif 676#endif
633} 677}
634 678
635static size_t 679static size_t
651 #undef VAR 695 #undef VAR
652 } 696 }
653 else 697 else
654 slot = coro->slot; 698 slot = coro->slot;
655 699
700 if (slot)
701 {
656 rss += sizeof (slot->curstackinfo); 702 rss += sizeof (slot->curstackinfo);
657 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 703 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
658 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 704 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *);
659 rss += slot->tmps_max * sizeof (SV *); 705 rss += slot->tmps_max * sizeof (SV *);
660 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); 706 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
661 rss += slot->scopestack_max * sizeof (I32); 707 rss += slot->scopestack_max * sizeof (I32);
662 rss += slot->savestack_max * sizeof (ANY); 708 rss += slot->savestack_max * sizeof (ANY);
663 709
664#if !PERL_VERSION_ATLEAST (5,9,0) 710#if !PERL_VERSION_ATLEAST (5,10,0)
665 rss += slot->retstack_max * sizeof (OP *); 711 rss += slot->retstack_max * sizeof (OP *);
666#endif 712#endif
713 }
667 } 714 }
668 715
669 return rss; 716 return rss;
670} 717}
671 718
719/** set stacklevel support **************************************************/
720
721/* we sometimes need to create the effect of pp_set_stacklevel calling us */
722#define SSL_HEAD (void)0
723/* we sometimes need to create the effect of leaving via pp_set_stacklevel */
724#define SSL_TAIL set_stacklevel_tail (aTHX)
725
726INLINE void
727set_stacklevel_tail (pTHX)
728{
729 dSP;
730 SV **bot = SP;
731
732 int gimme = GIMME_V;
733
734 /* make sure we put something on the stack in scalar context */
735 if (gimme == G_SCALAR)
736 {
737 if (sp == bot)
738 XPUSHs (&PL_sv_undef);
739
740 SP = bot + 1;
741 }
742
743 PUTBACK;
744}
745
672/** coroutine stack handling ************************************************/ 746/** coroutine stack handling ************************************************/
747
748static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
749static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
750static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
751
752/* apparently < 5.8.8 */
753#ifndef MgPV_nolen_const
754#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
755 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
756 (const char*)(mg)->mg_ptr)
757#endif
758
759/*
760 * This overrides the default magic get method of %SIG elements.
761 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
762 * and instead of tryign to save and restore the hash elements, we just provide
763 * readback here.
764 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
765 * not expecting this to actually change the hook. This is a potential problem
766 * when a schedule happens then, but we ignore this.
767 */
768static int
769coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
770{
771 const char *s = MgPV_nolen_const (mg);
772
773 if (*s == '_')
774 {
775 SV **svp = 0;
776
777 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
778 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
779
780 if (svp)
781 {
782 sv_setsv (sv, *svp ? *svp : &PL_sv_undef);
783 return 0;
784 }
785 }
786
787 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
788}
789
790static int
791coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
792{
793 const char *s = MgPV_nolen_const (mg);
794
795 if (*s == '_')
796 {
797 SV **svp = 0;
798
799 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
800 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
801
802 if (svp)
803 {
804 SV *old = *svp;
805 *svp = 0;
806 SvREFCNT_dec (old);
807 return 0;
808 }
809 }
810
811 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0;
812}
813
814static int
815coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
816{
817 const char *s = MgPV_nolen_const (mg);
818
819 if (*s == '_')
820 {
821 SV **svp = 0;
822
823 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
824 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
825
826 if (svp)
827 {
828 SV *old = *svp;
829 *svp = newSVsv (sv);
830 SvREFCNT_dec (old);
831 return 0;
832 }
833 }
834
835 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0;
836}
673 837
674static void 838static void
675coro_setup (pTHX_ struct coro *coro) 839coro_setup (pTHX_ struct coro *coro)
676{ 840{
677 /* 841 /*
686 PL_curpm = 0; 850 PL_curpm = 0;
687 PL_curpad = 0; 851 PL_curpad = 0;
688 PL_localizing = 0; 852 PL_localizing = 0;
689 PL_dirty = 0; 853 PL_dirty = 0;
690 PL_restartop = 0; 854 PL_restartop = 0;
691 PL_diehook = 0; hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); 855#if PERL_VERSION_ATLEAST (5,10,0)
692 PL_warnhook = 0; hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); 856 PL_parser = 0;
857#endif
858
859 /* recreate the die/warn hooks */
860 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook );
861 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
693 862
694 GvSV (PL_defgv) = newSV (0); 863 GvSV (PL_defgv) = newSV (0);
695 GvAV (PL_defgv) = coro->args; coro->args = 0; 864 GvAV (PL_defgv) = coro->args; coro->args = 0;
696 GvSV (PL_errgv) = newSV (0); 865 GvSV (PL_errgv) = newSV (0);
697 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 866 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
698 PL_rs = newSVsv (GvSV (irsgv)); 867 PL_rs = newSVsv (GvSV (irsgv));
699 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 868 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
700 869
701 { 870 {
702 dSP; 871 dSP;
703 LOGOP myop; 872 UNOP myop;
704 873
705 Zero (&myop, 1, LOGOP); 874 Zero (&myop, 1, UNOP);
706 myop.op_next = Nullop; 875 myop.op_next = Nullop;
707 myop.op_flags = OPf_WANT_VOID; 876 myop.op_flags = OPf_WANT_VOID;
708 877
709 PUSHMARK (SP); 878 PUSHMARK (SP);
710 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 879 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
712 PL_op = (OP *)&myop; 881 PL_op = (OP *)&myop;
713 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 882 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
714 SPAGAIN; 883 SPAGAIN;
715 } 884 }
716 885
717 ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ 886 /* this newly created coroutine might be run on an existing cctx which most
887 * likely was suspended in set_stacklevel, called from pp_set_stacklevel,
888 * so we have to emulate entering pp_set_stacklevel here.
889 */
890 SSL_HEAD;
718} 891}
719 892
720static void 893static void
721coro_destroy (pTHX_ struct coro *coro) 894coro_destruct (pTHX_ struct coro *coro)
722{ 895{
723 if (!IN_DESTRUCT) 896 if (!IN_DESTRUCT)
724 { 897 {
725 /* restore all saved variables and stuff */ 898 /* restore all saved variables and stuff */
726 LEAVE_SCOPE (0); 899 LEAVE_SCOPE (0);
748 SvREFCNT_dec (PL_warnhook); 921 SvREFCNT_dec (PL_warnhook);
749 922
750 SvREFCNT_dec (coro->saved_deffh); 923 SvREFCNT_dec (coro->saved_deffh);
751 SvREFCNT_dec (coro->throw); 924 SvREFCNT_dec (coro->throw);
752 925
753 coro_destroy_stacks (aTHX); 926 coro_destruct_stacks (aTHX);
754} 927}
755 928
756static void 929INLINE void
757free_coro_mortal (pTHX) 930free_coro_mortal (pTHX)
758{ 931{
759 if (expect_true (coro_mortal)) 932 if (expect_true (coro_mortal))
760 { 933 {
761 SvREFCNT_dec (coro_mortal); 934 SvREFCNT_dec (coro_mortal);
795 : cx->blk_gimme == G_SCALAR ? bot + 1 968 : cx->blk_gimme == G_SCALAR ? bot + 1
796 : bot; 969 : bot;
797 970
798 av_extend (av, top - bot); 971 av_extend (av, top - bot);
799 while (bot < top) 972 while (bot < top)
800 av_push (av, SvREFCNT_inc (*bot++)); 973 av_push (av, SvREFCNT_inc_NN (*bot++));
801 974
802 PL_runops = RUNOPS_DEFAULT; 975 PL_runops = RUNOPS_DEFAULT;
803 ENTER; 976 ENTER;
804 SAVETMPS; 977 SAVETMPS;
805 EXTEND (SP, 3); 978 EXTEND (SP, 3);
806 PUSHMARK (SP); 979 PUSHMARK (SP);
807 PUSHs (&PL_sv_no); 980 PUSHs (&PL_sv_no);
808 PUSHs (fullname); 981 PUSHs (fullname);
809 PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); 982 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
810 PUTBACK; 983 PUTBACK;
811 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); 984 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
812 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 985 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
813 SPAGAIN; 986 SPAGAIN;
814 FREETMPS; 987 FREETMPS;
815 LEAVE; 988 LEAVE;
816 PL_runops = runops_trace; 989 PL_runops = runops_trace;
843 SAVETMPS; 1016 SAVETMPS;
844 EXTEND (SP, 3); 1017 EXTEND (SP, 3);
845 PUSHMARK (SP); 1018 PUSHMARK (SP);
846 PUSHs (&PL_sv_yes); 1019 PUSHs (&PL_sv_yes);
847 PUSHs (fullname); 1020 PUSHs (fullname);
848 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 1021 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
849 PUTBACK; 1022 PUTBACK;
850 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); 1023 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
851 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1024 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
852 SPAGAIN; 1025 SPAGAIN;
853 FREETMPS; 1026 FREETMPS;
854 LEAVE; 1027 LEAVE;
855 PL_runops = runops_trace; 1028 PL_runops = runops_trace;
869 PL_runops = RUNOPS_DEFAULT; 1042 PL_runops = RUNOPS_DEFAULT;
870 PUSHMARK (SP); 1043 PUSHMARK (SP);
871 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); 1044 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
872 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); 1045 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
873 PUTBACK; 1046 PUTBACK;
874 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_line_cb"), 0); 1047 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
875 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1048 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
876 SPAGAIN; 1049 SPAGAIN;
877 FREETMPS; 1050 FREETMPS;
878 LEAVE; 1051 LEAVE;
879 PL_runops = runops_trace; 1052 PL_runops = runops_trace;
885 1058
886 TAINT_NOT; 1059 TAINT_NOT;
887 return 0; 1060 return 0;
888} 1061}
889 1062
1063static void
1064prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx)
1065{
1066 ta->prev = (struct coro *)cctx;
1067 ta->next = 0;
1068}
1069
890/* inject a fake call to Coro::State::_cctx_init into the execution */ 1070/* inject a fake call to Coro::State::_cctx_init into the execution */
891/* _cctx_init should be careful, as it could be called at almost any time */ 1071/* _cctx_init should be careful, as it could be called at almost any time */
892/* during execution of a perl program */ 1072/* during execution of a perl program */
1073/* also initialises PL_top_env */
893static void NOINLINE 1074static void NOINLINE
894cctx_prepare (pTHX_ coro_cctx *cctx) 1075cctx_prepare (pTHX_ coro_cctx *cctx)
895{ 1076{
896 dSP; 1077 dSP;
897 LOGOP myop; 1078 UNOP myop;
898 1079
899 PL_top_env = &PL_start_env; 1080 PL_top_env = &PL_start_env;
900 1081
901 if (cctx->flags & CC_TRACE) 1082 if (cctx->flags & CC_TRACE)
902 PL_runops = runops_trace; 1083 PL_runops = runops_trace;
903 1084
904 Zero (&myop, 1, LOGOP); 1085 Zero (&myop, 1, UNOP);
905 myop.op_next = PL_op; 1086 myop.op_next = PL_op;
906 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1087 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
907 1088
908 PUSHMARK (SP); 1089 PUSHMARK (SP);
909 EXTEND (SP, 2); 1090 EXTEND (SP, 2);
910 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); 1091 PUSHs (sv_2mortal (newSViv ((IV)cctx)));
911 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1092 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
912 PUTBACK; 1093 PUTBACK;
913 PL_op = (OP *)&myop; 1094 PL_op = (OP *)&myop;
914 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1095 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
915 SPAGAIN; 1096 SPAGAIN;
916} 1097}
917 1098
1099/* the tail of transfer: execute stuff we can only do after a transfer */
1100INLINE void
1101transfer_tail (pTHX)
1102{
1103 struct coro *next = (struct coro *)transfer_next;
1104 assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */
1105 assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next));
1106
1107 free_coro_mortal (aTHX);
1108 UNLOCK;
1109
1110 if (expect_false (next->throw))
1111 {
1112 SV *exception = sv_2mortal (next->throw);
1113
1114 next->throw = 0;
1115 sv_setsv (ERRSV, exception);
1116 croak (0);
1117 }
1118}
1119
918/* 1120/*
919 * this is a _very_ stripped down perl interpreter ;) 1121 * this is a _very_ stripped down perl interpreter ;)
920 */ 1122 */
921static void 1123static void
922cctx_run (void *arg) 1124cctx_run (void *arg)
923{ 1125{
1126#ifdef USE_ITHREADS
1127# if CORO_PTHREAD
1128 PERL_SET_CONTEXT (coro_thx);
1129# endif
1130#endif
1131 {
924 dTHX; 1132 dTHX;
925 1133
926 /* cctx_run is the alternative tail of transfer(), so unlock here. */ 1134 /* we are the alternative tail to pp_set_stacklevel */
927 UNLOCK; 1135 /* so do the same things here */
1136 SSL_TAIL;
928 1137
929 /* we now skip the entersub that lead to transfer() */ 1138 /* we now skip the op that did lead to transfer() */
930 PL_op = PL_op->op_next; 1139 PL_op = PL_op->op_next;
931 1140
932 /* inject a fake subroutine call to cctx_init */ 1141 /* inject a fake subroutine call to cctx_init */
933 cctx_prepare (aTHX_ (coro_cctx *)arg); 1142 cctx_prepare (aTHX_ (coro_cctx *)arg);
934 1143
1144 /* cctx_run is the alternative tail of transfer() */
1145 transfer_tail (aTHX);
1146
935 /* somebody or something will hit me for both perl_run and PL_restartop */ 1147 /* somebody or something will hit me for both perl_run and PL_restartop */
936 PL_restartop = PL_op; 1148 PL_restartop = PL_op;
937 perl_run (PL_curinterp); 1149 perl_run (PL_curinterp);
938 1150
939 /* 1151 /*
940 * If perl-run returns we assume exit() was being called or the coro 1152 * If perl-run returns we assume exit() was being called or the coro
941 * fell off the end, which seems to be the only valid (non-bug) 1153 * fell off the end, which seems to be the only valid (non-bug)
942 * reason for perl_run to return. We try to exit by jumping to the 1154 * reason for perl_run to return. We try to exit by jumping to the
943 * bootstrap-time "top" top_env, as we cannot restore the "main" 1155 * bootstrap-time "top" top_env, as we cannot restore the "main"
944 * coroutine as Coro has no such concept 1156 * coroutine as Coro has no such concept
945 */ 1157 */
946 PL_top_env = main_top_env; 1158 PL_top_env = main_top_env;
947 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1159 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1160 }
948} 1161}
949 1162
950static coro_cctx * 1163static coro_cctx *
951cctx_new () 1164cctx_new ()
952{ 1165{
953 coro_cctx *cctx; 1166 coro_cctx *cctx;
1167
1168 ++cctx_count;
1169 New (0, cctx, 1, coro_cctx);
1170
1171 cctx->gen = cctx_gen;
1172 cctx->flags = 0;
1173 cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */
1174
1175 return cctx;
1176}
1177
1178/* create a new cctx only suitable as source */
1179static coro_cctx *
1180cctx_new_empty ()
1181{
1182 coro_cctx *cctx = cctx_new ();
1183
1184 cctx->sptr = 0;
1185 coro_create (&cctx->cctx, 0, 0, 0, 0);
1186
1187 return cctx;
1188}
1189
1190/* create a new cctx suitable as destination/running a perl interpreter */
1191static coro_cctx *
1192cctx_new_run ()
1193{
1194 coro_cctx *cctx = cctx_new ();
954 void *stack_start; 1195 void *stack_start;
955 size_t stack_size; 1196 size_t stack_size;
956 1197
957 ++cctx_count;
958 fprintf (stderr, "bp1\n");//D
959
960 Newz (0, cctx, 1, coro_cctx);
961
962#if HAVE_MMAP 1198#if HAVE_MMAP
963 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1199 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
964 /* mmap supposedly does allocate-on-write for us */ 1200 /* mmap supposedly does allocate-on-write for us */
965 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1201 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
966 1202
967 if (cctx->sptr != (void *)-1) 1203 if (cctx->sptr != (void *)-1)
968 { 1204 {
969# if CORO_STACKGUARD 1205 #if CORO_STACKGUARD
970 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1206 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
971# endif 1207 #endif
972 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 1208 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
973 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 1209 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
974 cctx->flags |= CC_MAPPED; 1210 cctx->flags |= CC_MAPPED;
975 } 1211 }
976 else 1212 else
977#endif 1213#endif
978 { 1214 {
979 cctx->ssize = coro_stacksize * (long)sizeof (long); 1215 cctx->ssize = cctx_stacksize * (long)sizeof (long);
980 New (0, cctx->sptr, coro_stacksize, long); 1216 New (0, cctx->sptr, cctx_stacksize, long);
981 1217
982 if (!cctx->sptr) 1218 if (!cctx->sptr)
983 { 1219 {
984 perror ("FATAL: unable to allocate stack for coroutine"); 1220 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
985 _exit (EXIT_FAILURE); 1221 _exit (EXIT_FAILURE);
986 } 1222 }
987 1223
988 stack_start = cctx->sptr; 1224 stack_start = cctx->sptr;
989 stack_size = cctx->ssize; 1225 stack_size = cctx->ssize;
990 } 1226 }
991 1227
992 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); 1228 #if CORO_USE_VALGRIND
1229 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1230 #endif
1231
993 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); 1232 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size);
994 1233
995 return cctx; 1234 return cctx;
996} 1235}
997 1236
1000{ 1239{
1001 if (!cctx) 1240 if (!cctx)
1002 return; 1241 return;
1003 1242
1004 --cctx_count; 1243 --cctx_count;
1244 coro_destroy (&cctx->cctx);
1005 1245
1246 /* coro_transfer creates new, empty cctx's */
1247 if (cctx->sptr)
1248 {
1006#if CORO_USE_VALGRIND 1249 #if CORO_USE_VALGRIND
1007 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1250 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1008#endif 1251 #endif
1009 1252
1010#if HAVE_MMAP 1253#if HAVE_MMAP
1011 if (cctx->flags & CC_MAPPED) 1254 if (cctx->flags & CC_MAPPED)
1012 munmap (cctx->sptr, cctx->ssize); 1255 munmap (cctx->sptr, cctx->ssize);
1013 else 1256 else
1014#endif 1257#endif
1015 Safefree (cctx->sptr); 1258 Safefree (cctx->sptr);
1259 }
1016 1260
1017 Safefree (cctx); 1261 Safefree (cctx);
1018} 1262}
1019 1263
1020/* wether this cctx should be destructed */ 1264/* wether this cctx should be destructed */
1021#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1265#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1022 1266
1023static coro_cctx * 1267static coro_cctx *
1024cctx_get (pTHX) 1268cctx_get (pTHX)
1025{ 1269{
1026 while (expect_true (cctx_first)) 1270 while (expect_true (cctx_first))
1033 return cctx; 1277 return cctx;
1034 1278
1035 cctx_destroy (cctx); 1279 cctx_destroy (cctx);
1036 } 1280 }
1037 1281
1038 return cctx_new (); 1282 return cctx_new_run ();
1039} 1283}
1040 1284
1041static void 1285static void
1042cctx_put (coro_cctx *cctx) 1286cctx_put (coro_cctx *cctx)
1043{ 1287{
1288 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
1289
1044 /* free another cctx if overlimit */ 1290 /* free another cctx if overlimit */
1045 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1291 if (expect_false (cctx_idle >= cctx_max_idle))
1046 { 1292 {
1047 coro_cctx *first = cctx_first; 1293 coro_cctx *first = cctx_first;
1048 cctx_first = first->next; 1294 cctx_first = first->next;
1049 --cctx_idle; 1295 --cctx_idle;
1050 1296
1070 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 1316 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
1071 1317
1072 if (expect_false (next->flags & CF_DESTROYED)) 1318 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"); 1319 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
1074 1320
1075 if (
1076#if PERL_VERSION_ATLEAST (5,9,0) 1321#if !PERL_VERSION_ATLEAST (5,10,0)
1077 expect_false (PL_parser)
1078#else
1079 expect_false (PL_lex_state != LEX_NOTPARSING) 1322 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1080#endif
1081 )
1082 croak ("Coro::State::transfer called while parsing, but this is not supported"); 1323 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version");
1324#endif
1083 } 1325 }
1084} 1326}
1085 1327
1086/* always use the TRANSFER macro */ 1328/* always use the TRANSFER macro */
1087static void NOINLINE 1329static void NOINLINE
1088transfer (pTHX_ struct coro *prev, struct coro *next) 1330transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1089{ 1331{
1090 dSTACKLEVEL; 1332 dSTACKLEVEL;
1091 1333
1092 /* sometimes transfer is only called to set idle_sp */ 1334 /* sometimes transfer is only called to set idle_sp */
1093 if (expect_false (!next)) 1335 if (expect_false (!next))
1094 { 1336 {
1095 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1337 ((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 */ 1338 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1097 } 1339 }
1098 else if (expect_true (prev != next)) 1340 else if (expect_true (prev != next))
1099 { 1341 {
1100 coro_cctx *prev__cctx; 1342 coro_cctx *prev__cctx;
1101 1343
1102 if (expect_false (prev->flags & CF_NEW)) 1344 if (expect_false (prev->flags & CF_NEW))
1103 { 1345 {
1104 /* create a new empty context */ 1346 /* create a new empty/source context */
1105 Newz (0, prev->cctx, 1, coro_cctx); 1347 prev->cctx = cctx_new_empty ();
1106 prev->flags &= ~CF_NEW; 1348 prev->flags &= ~CF_NEW;
1107 prev->flags |= CF_RUNNING; 1349 prev->flags |= CF_RUNNING;
1108 } 1350 }
1109 1351
1110 prev->flags &= ~CF_RUNNING; 1352 prev->flags &= ~CF_RUNNING;
1111 next->flags |= CF_RUNNING; 1353 next->flags |= CF_RUNNING;
1112 1354
1113 LOCK; 1355 LOCK;
1356
1357 /* first get rid of the old state */
1358 save_perl (aTHX_ prev);
1114 1359
1115 if (expect_false (next->flags & CF_NEW)) 1360 if (expect_false (next->flags & CF_NEW))
1116 { 1361 {
1117 /* need to start coroutine */ 1362 /* need to start coroutine */
1118 next->flags &= ~CF_NEW; 1363 next->flags &= ~CF_NEW;
1119 /* first get rid of the old state */
1120 save_perl (aTHX_ prev);
1121 /* setup coroutine call */ 1364 /* setup coroutine call */
1122 coro_setup (aTHX_ next); 1365 coro_setup (aTHX_ next);
1123 } 1366 }
1124 else 1367 else
1368 load_perl (aTHX_ next);
1369
1370 prev__cctx = prev->cctx;
1371
1372 /* possibly untie and reuse the cctx */
1373 if (expect_true (
1374 prev__cctx->idle_sp == stacklevel
1375 && !(prev__cctx->flags & CC_TRACE)
1376 && !force_cctx
1377 ))
1125 { 1378 {
1126 /* coroutine already started */
1127 save_perl (aTHX_ prev);
1128 load_perl (aTHX_ next);
1129 }
1130
1131 prev__cctx = prev->cctx;
1132
1133 /* possibly "free" the cctx */
1134 if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)))
1135 {
1136 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1379 /* 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)); 1380 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te));
1138 1381
1139 prev->cctx = 0; 1382 prev->cctx = 0;
1140 1383
1141 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1384 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1142 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1385 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1149 1392
1150 ++next->usecount; 1393 ++next->usecount;
1151 1394
1152 if (expect_true (!next->cctx)) 1395 if (expect_true (!next->cctx))
1153 next->cctx = cctx_get (aTHX); 1396 next->cctx = cctx_get (aTHX);
1397
1398 assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next));
1399 transfer_next = next;
1154 1400
1155 if (expect_false (prev__cctx != next->cctx)) 1401 if (expect_false (prev__cctx != next->cctx))
1156 { 1402 {
1157 prev__cctx->top_env = PL_top_env; 1403 prev__cctx->top_env = PL_top_env;
1158 PL_top_env = next->cctx->top_env; 1404 PL_top_env = next->cctx->top_env;
1159 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1405 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1160 } 1406 }
1161 1407
1162 free_coro_mortal (aTHX); 1408 transfer_tail (aTHX);
1163 UNLOCK;
1164
1165 if (expect_false (prev->throw || next->throw))
1166 {
1167 struct coro *coro = SvSTATE (coro_current);
1168
1169 if (coro->throw)
1170 {
1171 SV *exception = coro->throw;
1172 coro->throw = 0;
1173 sv_setsv (ERRSV, exception);
1174 croak (0);
1175 }
1176 }
1177 } 1409 }
1178} 1410}
1179 1411
1180struct transfer_args
1181{
1182 struct coro *prev, *next;
1183};
1184
1185#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1412#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) 1413#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1187 1414
1188/** high level stuff ********************************************************/ 1415/** high level stuff ********************************************************/
1189 1416
1190static int 1417static int
1214 croak ("FATAL: tried to destroy currently running coroutine"); 1441 croak ("FATAL: tried to destroy currently running coroutine");
1215 1442
1216 save_perl (aTHX_ &temp); 1443 save_perl (aTHX_ &temp);
1217 load_perl (aTHX_ coro); 1444 load_perl (aTHX_ coro);
1218 1445
1219 coro_destroy (aTHX_ coro); 1446 coro_destruct (aTHX_ coro);
1220 1447
1221 load_perl (aTHX_ &temp); 1448 load_perl (aTHX_ &temp);
1222 1449
1223 coro->slot = 0; 1450 coro->slot = 0;
1224 } 1451 }
1284{ 1511{
1285 dTHX; 1512 dTHX;
1286 struct transfer_args ta; 1513 struct transfer_args ta;
1287 1514
1288 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1515 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1289 TRANSFER (ta); 1516 TRANSFER (ta, 1);
1290} 1517}
1291 1518
1292/** Coro ********************************************************************/ 1519/** Coro ********************************************************************/
1293 1520
1294static void 1521static void
1296{ 1523{
1297 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 1524 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
1298} 1525}
1299 1526
1300static SV * 1527static SV *
1301coro_deq (pTHX_ int min_prio) 1528coro_deq (pTHX)
1302{ 1529{
1303 int prio = PRIO_MAX - PRIO_MIN; 1530 int prio;
1304 1531
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; ) 1532 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1310 if (AvFILLp (coro_ready [prio]) >= 0) 1533 if (AvFILLp (coro_ready [prio]) >= 0)
1311 return av_shift (coro_ready [prio]); 1534 return av_shift (coro_ready [prio]);
1312 1535
1313 return 0; 1536 return 0;
1314} 1537}
1316static int 1539static int
1317api_ready (SV *coro_sv) 1540api_ready (SV *coro_sv)
1318{ 1541{
1319 dTHX; 1542 dTHX;
1320 struct coro *coro; 1543 struct coro *coro;
1544 SV *sv_hook;
1545 void (*xs_hook)(void);
1321 1546
1322 if (SvROK (coro_sv)) 1547 if (SvROK (coro_sv))
1323 coro_sv = SvRV (coro_sv); 1548 coro_sv = SvRV (coro_sv);
1324 1549
1325 coro = SvSTATE (coro_sv); 1550 coro = SvSTATE (coro_sv);
1328 return 0; 1553 return 0;
1329 1554
1330 coro->flags |= CF_READY; 1555 coro->flags |= CF_READY;
1331 1556
1332 LOCK; 1557 LOCK;
1558
1559 sv_hook = coro_nready ? 0 : coro_readyhook;
1560 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1561
1333 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1562 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1334 ++coro_nready; 1563 ++coro_nready;
1564
1335 UNLOCK; 1565 UNLOCK;
1566
1567 if (sv_hook)
1568 {
1569 dSP;
1570
1571 ENTER;
1572 SAVETMPS;
1573
1574 PUSHMARK (SP);
1575 PUTBACK;
1576 call_sv (sv_hook, G_DISCARD);
1577 SPAGAIN;
1578
1579 FREETMPS;
1580 LEAVE;
1581 }
1582
1583 if (xs_hook)
1584 xs_hook ();
1336 1585
1337 return 1; 1586 return 1;
1338} 1587}
1339 1588
1340static int 1589static int
1341api_is_ready (SV *coro_sv) 1590api_is_ready (SV *coro_sv)
1342{ 1591{
1343 dTHX; 1592 dTHX;
1593
1344 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1594 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1345} 1595}
1346 1596
1347static void 1597INLINE void
1348prepare_schedule (pTHX_ struct transfer_args *ta) 1598prepare_schedule (pTHX_ struct transfer_args *ta)
1349{ 1599{
1350 SV *prev_sv, *next_sv; 1600 SV *prev_sv, *next_sv;
1351 1601
1352 for (;;) 1602 for (;;)
1353 { 1603 {
1354 LOCK; 1604 LOCK;
1355 next_sv = coro_deq (aTHX_ PRIO_MIN); 1605 next_sv = coro_deq (aTHX);
1356 1606
1357 /* nothing to schedule: call the idle handler */ 1607 /* nothing to schedule: call the idle handler */
1358 if (expect_false (!next_sv)) 1608 if (expect_false (!next_sv))
1359 { 1609 {
1360 dSP; 1610 dSP;
1364 SAVETMPS; 1614 SAVETMPS;
1365 1615
1366 PUSHMARK (SP); 1616 PUSHMARK (SP);
1367 PUTBACK; 1617 PUTBACK;
1368 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1618 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1619 SPAGAIN;
1369 1620
1370 FREETMPS; 1621 FREETMPS;
1371 LEAVE; 1622 LEAVE;
1372 continue; 1623 continue;
1373 } 1624 }
1377 /* cannot transfer to destroyed coros, skip and look for next */ 1628 /* cannot transfer to destroyed coros, skip and look for next */
1378 if (expect_false (ta->next->flags & CF_DESTROYED)) 1629 if (expect_false (ta->next->flags & CF_DESTROYED))
1379 { 1630 {
1380 UNLOCK; 1631 UNLOCK;
1381 SvREFCNT_dec (next_sv); 1632 SvREFCNT_dec (next_sv);
1382 /* coro_nready is already taken care of by destroy */ 1633 /* coro_nready has already been taken care of by destroy */
1383 continue; 1634 continue;
1384 } 1635 }
1385 1636
1386 --coro_nready; 1637 --coro_nready;
1387 UNLOCK; 1638 UNLOCK;
1390 1641
1391 /* free this only after the transfer */ 1642 /* free this only after the transfer */
1392 prev_sv = SvRV (coro_current); 1643 prev_sv = SvRV (coro_current);
1393 ta->prev = SvSTATE (prev_sv); 1644 ta->prev = SvSTATE (prev_sv);
1394 TRANSFER_CHECK (*ta); 1645 TRANSFER_CHECK (*ta);
1395 assert (ta->next->flags & CF_READY); 1646 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1396 ta->next->flags &= ~CF_READY; 1647 ta->next->flags &= ~CF_READY;
1397 SvRV_set (coro_current, next_sv); 1648 SvRV_set (coro_current, next_sv);
1398 1649
1399 LOCK; 1650 LOCK;
1400 free_coro_mortal (aTHX); 1651 free_coro_mortal (aTHX);
1401 coro_mortal = prev_sv; 1652 coro_mortal = prev_sv;
1402 UNLOCK; 1653 UNLOCK;
1403} 1654}
1404 1655
1405static void 1656INLINE void
1406prepare_cede (pTHX_ struct transfer_args *ta) 1657prepare_cede (pTHX_ struct transfer_args *ta)
1407{ 1658{
1408 api_ready (coro_current); 1659 api_ready (coro_current);
1409 prepare_schedule (aTHX_ ta); 1660 prepare_schedule (aTHX_ ta);
1410} 1661}
1411 1662
1412static int 1663static void
1413prepare_cede_notself (pTHX_ struct transfer_args *ta) 1664prepare_cede_notself (pTHX_ struct transfer_args *ta)
1414{ 1665{
1666 SV *prev = SvRV (coro_current);
1667
1415 if (coro_nready) 1668 if (coro_nready)
1416 { 1669 {
1417 SV *prev = SvRV (coro_current);
1418 prepare_schedule (aTHX_ ta); 1670 prepare_schedule (aTHX_ ta);
1419 api_ready (prev); 1671 api_ready (prev);
1672 }
1673 else
1674 ta->prev = ta->next = SvSTATE (prev);
1675}
1676
1677static void
1678api_schedule (void)
1679{
1680 dTHX;
1681 struct transfer_args ta;
1682
1683 prepare_schedule (aTHX_ &ta);
1684 TRANSFER (ta, 1);
1685}
1686
1687static int
1688api_cede (void)
1689{
1690 dTHX;
1691 struct transfer_args ta;
1692
1693 prepare_cede (aTHX_ &ta);
1694
1695 if (expect_true (ta.prev != ta.next))
1696 {
1697 TRANSFER (ta, 1);
1420 return 1; 1698 return 1;
1421 } 1699 }
1422 else 1700 else
1423 return 0; 1701 return 0;
1424} 1702}
1425 1703
1426static void
1427api_schedule (void)
1428{
1429 dTHX;
1430 struct transfer_args ta;
1431
1432 prepare_schedule (aTHX_ &ta);
1433 TRANSFER (ta);
1434}
1435
1436static int 1704static int
1437api_cede (void) 1705api_cede_notself (void)
1438{ 1706{
1707 if (coro_nready)
1708 {
1439 dTHX; 1709 dTHX;
1440 struct transfer_args ta; 1710 struct transfer_args ta;
1441 1711
1442 prepare_cede (aTHX_ &ta); 1712 prepare_cede_notself (aTHX_ &ta);
1443
1444 if (expect_true (ta.prev != ta.next))
1445 {
1446 TRANSFER (ta); 1713 TRANSFER (ta, 1);
1447 return 1; 1714 return 1;
1448 } 1715 }
1449 else 1716 else
1450 return 0; 1717 return 0;
1451} 1718}
1452 1719
1453static int
1454api_cede_notself (void)
1455{
1456 dTHX;
1457 struct transfer_args ta;
1458
1459 if (prepare_cede_notself (aTHX_ &ta))
1460 {
1461 TRANSFER (ta);
1462 return 1;
1463 }
1464 else
1465 return 0;
1466}
1467
1468static void 1720static void
1469api_trace (SV *coro_sv, int flags) 1721api_trace (SV *coro_sv, int flags)
1470{ 1722{
1471 dTHX; 1723 dTHX;
1472 struct coro *coro = SvSTATE (coro_sv); 1724 struct coro *coro = SvSTATE (coro_sv);
1473 1725
1474 if (flags & CC_TRACE) 1726 if (flags & CC_TRACE)
1475 { 1727 {
1476 if (!coro->cctx) 1728 if (!coro->cctx)
1477 coro->cctx = cctx_new (); 1729 coro->cctx = cctx_new_run ();
1478 else if (!(coro->cctx->flags & CC_TRACE)) 1730 else if (!(coro->cctx->flags & CC_TRACE))
1479 croak ("cannot enable tracing on coroutine with custom stack"); 1731 croak ("cannot enable tracing on coroutine with custom stack");
1480 1732
1481 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1733 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1482 } 1734 }
1489 else 1741 else
1490 coro->slot->runops = RUNOPS_DEFAULT; 1742 coro->slot->runops = RUNOPS_DEFAULT;
1491 } 1743 }
1492} 1744}
1493 1745
1746#if 0
1747static int
1748coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1749{
1750 AV *padlist;
1751 AV *av = (AV *)mg->mg_obj;
1752
1753 abort ();
1754
1755 return 0;
1756}
1757
1758static MGVTBL coro_gensub_vtbl = {
1759 0, 0, 0, 0,
1760 coro_gensub_free
1761};
1762#endif
1763
1764/*****************************************************************************/
1765/* PerlIO::cede */
1766
1767typedef struct
1768{
1769 PerlIOBuf base;
1770 NV next, every;
1771} PerlIOCede;
1772
1773static IV
1774PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1775{
1776 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1777
1778 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1779 self->next = nvtime () + self->every;
1780
1781 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1782}
1783
1784static SV *
1785PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
1786{
1787 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1788
1789 return newSVnv (self->every);
1790}
1791
1792static IV
1793PerlIOCede_flush (pTHX_ PerlIO *f)
1794{
1795 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1796 double now = nvtime ();
1797
1798 if (now >= self->next)
1799 {
1800 api_cede ();
1801 self->next = now + self->every;
1802 }
1803
1804 return PerlIOBuf_flush (aTHX_ f);
1805}
1806
1807static PerlIO_funcs PerlIO_cede =
1808{
1809 sizeof(PerlIO_funcs),
1810 "cede",
1811 sizeof(PerlIOCede),
1812 PERLIO_K_DESTRUCT | PERLIO_K_RAW,
1813 PerlIOCede_pushed,
1814 PerlIOBuf_popped,
1815 PerlIOBuf_open,
1816 PerlIOBase_binmode,
1817 PerlIOCede_getarg,
1818 PerlIOBase_fileno,
1819 PerlIOBuf_dup,
1820 PerlIOBuf_read,
1821 PerlIOBuf_unread,
1822 PerlIOBuf_write,
1823 PerlIOBuf_seek,
1824 PerlIOBuf_tell,
1825 PerlIOBuf_close,
1826 PerlIOCede_flush,
1827 PerlIOBuf_fill,
1828 PerlIOBase_eof,
1829 PerlIOBase_error,
1830 PerlIOBase_clearerr,
1831 PerlIOBase_setlinebuf,
1832 PerlIOBuf_get_base,
1833 PerlIOBuf_bufsiz,
1834 PerlIOBuf_get_ptr,
1835 PerlIOBuf_get_cnt,
1836 PerlIOBuf_set_ptrcnt,
1837};
1838
1839/*****************************************************************************/
1840
1841static const CV *ssl_cv; /* for quick consistency check */
1842
1843static UNOP ssl_restore; /* restore stack as entersub did, for first-re-run */
1844static SV *ssl_arg0;
1845static SV *ssl_arg1;
1846
1847/* this restores the stack in the case we patched the entersub, to */
1848/* recreate the stack frame as perl will on following calls */
1849/* since entersub cleared the stack */
1850static OP *
1851pp_restore (pTHX)
1852{
1853 dSP;
1854
1855 PUSHMARK (SP);
1856
1857 EXTEND (SP, 3);
1858 if (ssl_arg0) PUSHs (sv_2mortal (ssl_arg0)), ssl_arg0 = 0;
1859 if (ssl_arg1) PUSHs (sv_2mortal (ssl_arg1)), ssl_arg1 = 0;
1860 PUSHs ((SV *)CvGV (ssl_cv));
1861
1862 RETURNOP (ssl_restore.op_first);
1863}
1864
1865#define OPpENTERSUB_SSL 15 /* the part of op_private entersub hopefully doesn't use */
1866
1867/* declare prototype */
1868XS(XS_Coro__State__set_stacklevel);
1869
1870/*
1871 * these not obviously related functions are all rolled into one
1872 * function to increase chances that they all will call transfer with the same
1873 * stack offset
1874 */
1875static OP *
1876pp_set_stacklevel (pTHX)
1877{
1878 dSP;
1879 struct transfer_args ta;
1880 SV **arg = PL_stack_base + TOPMARK + 1;
1881 int items = SP - arg; /* args without function object */
1882
1883 /* do a quick consistency check on the "function" object, and if it isn't */
1884 /* for us, divert to the real entersub */
1885 if (SvTYPE (*sp) != SVt_PVGV || CvXSUB (GvCV (*sp)) != XS_Coro__State__set_stacklevel)
1886 return PL_ppaddr[OP_ENTERSUB](aTHX);
1887
1888 /* pop args */
1889 SP = PL_stack_base + POPMARK;
1890
1891 if (!(PL_op->op_flags & OPf_STACKED))
1892 {
1893 /* ampersand-form of call, use @_ instead of stack */
1894 AV *av = GvAV (PL_defgv);
1895 arg = AvARRAY (av);
1896 items = AvFILLp (av) + 1;
1897 }
1898
1899 PUTBACK;
1900 switch (PL_op->op_private & OPpENTERSUB_SSL)
1901 {
1902 case 0:
1903 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0]));
1904 break;
1905
1906 case 1:
1907 if (items != 2)
1908 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items);
1909
1910 prepare_transfer (aTHX_ &ta, arg [0], arg [1]);
1911 break;
1912
1913 case 2:
1914 prepare_schedule (aTHX_ &ta);
1915 break;
1916
1917 case 3:
1918 prepare_cede (aTHX_ &ta);
1919 break;
1920
1921 case 4:
1922 prepare_cede_notself (aTHX_ &ta);
1923 break;
1924 }
1925
1926 TRANSFER (ta, 0);
1927 SPAGAIN;
1928
1929skip:
1930 PUTBACK;
1931 SSL_TAIL;
1932 SPAGAIN;
1933 RETURN;
1934}
1935
1936static void
1937coro_ssl_patch (pTHX_ CV *cv, int ix, SV **args, int items)
1938{
1939 assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel));
1940
1941 assert (("FATAL: ssl call with illegal CV value", CvGV (cv)));
1942 ssl_cv = cv;
1943
1944 /* we patch the op, and then re-run the whole call */
1945 /* we have to put some dummy argument on the stack for this to work */
1946 ssl_restore.op_next = (OP *)&ssl_restore;
1947 ssl_restore.op_type = OP_NULL;
1948 ssl_restore.op_ppaddr = pp_restore;
1949 ssl_restore.op_first = PL_op;
1950
1951 ssl_arg0 = items > 0 ? SvREFCNT_inc (args [0]) : 0;
1952 ssl_arg1 = items > 1 ? SvREFCNT_inc (args [1]) : 0;
1953
1954 PL_op->op_ppaddr = pp_set_stacklevel;
1955 PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SSL | ix; /* we potentially share our private flags with entersub */
1956
1957 PL_op = (OP *)&ssl_restore;
1958}
1959
1494MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1960MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1495 1961
1496PROTOTYPES: DISABLE 1962PROTOTYPES: DISABLE
1497 1963
1498BOOT: 1964BOOT:
1499{ 1965{
1500#ifdef USE_ITHREADS 1966#ifdef USE_ITHREADS
1501 MUTEX_INIT (&coro_mutex); 1967 MUTEX_INIT (&coro_lock);
1968# if CORO_PTHREAD
1969 coro_thx = PERL_GET_CONTEXT;
1970# endif
1502#endif 1971#endif
1503 BOOT_PAGESIZE; 1972 BOOT_PAGESIZE;
1504 1973
1505 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1974 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1506 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1975 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1507 1976
1977 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1978 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1979 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1980
1508 hv_sig = coro_get_hv ("SIG", TRUE); 1981 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1509 sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); 1982 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1510 sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); 1983 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 1984
1515 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1985 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1516 1986
1517 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1987 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1518 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1988 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1523 main_top_env = PL_top_env; 1993 main_top_env = PL_top_env;
1524 1994
1525 while (main_top_env->je_prev) 1995 while (main_top_env->je_prev)
1526 main_top_env = main_top_env->je_prev; 1996 main_top_env = main_top_env->je_prev;
1527 1997
1528 coroapi.ver = CORO_API_VERSION; 1998 coroapi.ver = CORO_API_VERSION;
1999 coroapi.rev = CORO_API_REVISION;
1529 coroapi.transfer = api_transfer; 2000 coroapi.transfer = api_transfer;
2001
2002 {
2003 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
2004
2005 if (!svp) croak ("Time::HiRes is required");
2006 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
2007
2008 nvtime = INT2PTR (double (*)(), SvIV (*svp));
2009 }
1530 2010
1531 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 2011 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1532} 2012}
1533 2013
1534SV * 2014SV *
1535new (char *klass, ...) 2015new (char *klass, ...)
1536 CODE: 2016 CODE:
1537{ 2017{
1538 struct coro *coro; 2018 struct coro *coro;
2019 MAGIC *mg;
1539 HV *hv; 2020 HV *hv;
1540 int i; 2021 int i;
1541 2022
1542 Newz (0, coro, 1, struct coro); 2023 Newz (0, coro, 1, struct coro);
1543 coro->args = newAV (); 2024 coro->args = newAV ();
1546 if (coro_first) coro_first->prev = coro; 2027 if (coro_first) coro_first->prev = coro;
1547 coro->next = coro_first; 2028 coro->next = coro_first;
1548 coro_first = coro; 2029 coro_first = coro;
1549 2030
1550 coro->hv = hv = newHV (); 2031 coro->hv = hv = newHV ();
1551 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 2032 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2033 mg->mg_flags |= MGf_DUP;
1552 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 2034 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1553 2035
1554 av_extend (coro->args, items - 1); 2036 av_extend (coro->args, items - 1);
1555 for (i = 1; i < items; i++) 2037 for (i = 1; i < items; i++)
1556 av_push (coro->args, newSVsv (ST (i))); 2038 av_push (coro->args, newSVsv (ST (i)));
1557} 2039}
1558 OUTPUT: 2040 OUTPUT:
1559 RETVAL 2041 RETVAL
1560 2042
1561# these not obviously related functions are all rolled into the same xs
1562# function to increase chances that they all will call transfer with the same
1563# stack offset
1564void 2043void
1565_set_stacklevel (...) 2044_set_stacklevel (...)
1566 ALIAS: 2045 ALIAS:
1567 Coro::State::transfer = 1 2046 Coro::State::transfer = 1
1568 Coro::schedule = 2 2047 Coro::schedule = 2
1569 Coro::cede = 3 2048 Coro::cede = 3
1570 Coro::cede_notself = 4 2049 Coro::cede_notself = 4
1571 CODE: 2050 CODE:
1572{ 2051 coro_ssl_patch (aTHX_ cv, ix, &ST (0), items);
1573 struct transfer_args ta;
1574
1575 switch (ix)
1576 {
1577 case 0:
1578 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1579 ta.next = 0;
1580 break;
1581
1582 case 1:
1583 if (items != 2)
1584 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1585
1586 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1587 break;
1588
1589 case 2:
1590 prepare_schedule (aTHX_ &ta);
1591 break;
1592
1593 case 3:
1594 prepare_cede (aTHX_ &ta);
1595 break;
1596
1597 case 4:
1598 if (!prepare_cede_notself (aTHX_ &ta))
1599 XSRETURN_EMPTY;
1600
1601 break;
1602 }
1603
1604 BARRIER;
1605 TRANSFER (ta);
1606
1607 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev))
1608 XSRETURN_YES;
1609}
1610 2052
1611bool 2053bool
1612_destroy (SV *coro_sv) 2054_destroy (SV *coro_sv)
1613 CODE: 2055 CODE:
1614 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 2056 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1615 OUTPUT: 2057 OUTPUT:
1616 RETVAL 2058 RETVAL
1617 2059
1618void 2060void
1619_exit (code) 2061_exit (int code)
1620 int code
1621 PROTOTYPE: $ 2062 PROTOTYPE: $
1622 CODE: 2063 CODE:
1623 _exit (code); 2064 _exit (code);
1624 2065
1625int 2066int
1626cctx_stacksize (int new_stacksize = 0) 2067cctx_stacksize (int new_stacksize = 0)
1627 CODE: 2068 CODE:
1628 RETVAL = coro_stacksize; 2069 RETVAL = cctx_stacksize;
1629 if (new_stacksize) 2070 if (new_stacksize)
2071 {
1630 coro_stacksize = new_stacksize; 2072 cctx_stacksize = new_stacksize;
2073 ++cctx_gen;
2074 }
2075 OUTPUT:
2076 RETVAL
2077
2078int
2079cctx_max_idle (int max_idle = 0)
2080 CODE:
2081 RETVAL = cctx_max_idle;
2082 if (max_idle > 1)
2083 cctx_max_idle = max_idle;
1631 OUTPUT: 2084 OUTPUT:
1632 RETVAL 2085 RETVAL
1633 2086
1634int 2087int
1635cctx_count () 2088cctx_count ()
1659call (Coro::State coro, SV *coderef) 2112call (Coro::State coro, SV *coderef)
1660 ALIAS: 2113 ALIAS:
1661 eval = 1 2114 eval = 1
1662 CODE: 2115 CODE:
1663{ 2116{
1664 if (coro->mainstack) 2117 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1665 { 2118 {
1666 struct coro temp; 2119 struct coro temp;
1667 2120
1668 if (!(coro->flags & CF_RUNNING)) 2121 if (!(coro->flags & CF_RUNNING))
1669 { 2122 {
2123 PUTBACK;
1670 save_perl (aTHX_ &temp); 2124 save_perl (aTHX_ &temp);
1671 load_perl (aTHX_ coro); 2125 load_perl (aTHX_ coro);
1672 } 2126 }
1673 2127
1674 { 2128 {
1675 dSP; 2129 dSP;
1676 ENTER; 2130 ENTER;
1677 SAVETMPS; 2131 SAVETMPS;
2132 PUTBACK;
2133 PUSHSTACK;
1678 PUSHMARK (SP); 2134 PUSHMARK (SP);
1679 PUTBACK;
1680 2135
1681 if (ix) 2136 if (ix)
1682 eval_sv (coderef, 0); 2137 eval_sv (coderef, 0);
1683 else 2138 else
1684 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 2139 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1685 2140
2141 POPSTACK;
1686 SPAGAIN; 2142 SPAGAIN;
1687 FREETMPS; 2143 FREETMPS;
1688 LEAVE; 2144 LEAVE;
1689 PUTBACK; 2145 PUTBACK;
1690 } 2146 }
1691 2147
1692 if (!(coro->flags & CF_RUNNING)) 2148 if (!(coro->flags & CF_RUNNING))
1693 { 2149 {
1694 save_perl (aTHX_ coro); 2150 save_perl (aTHX_ coro);
1695 load_perl (aTHX_ &temp); 2151 load_perl (aTHX_ &temp);
2152 SPAGAIN;
1696 } 2153 }
1697 } 2154 }
1698} 2155}
1699 2156
1700SV * 2157SV *
1709 RETVAL = boolSV (coro->flags & ix); 2166 RETVAL = boolSV (coro->flags & ix);
1710 OUTPUT: 2167 OUTPUT:
1711 RETVAL 2168 RETVAL
1712 2169
1713void 2170void
2171throw (Coro::State self, SV *throw = &PL_sv_undef)
2172 PROTOTYPE: $;$
2173 CODE:
2174 SvREFCNT_dec (self->throw);
2175 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2176
2177void
1714api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2178api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1715 2179
1716SV * 2180SV *
1717has_stack (Coro::State coro) 2181has_cctx (Coro::State coro)
1718 PROTOTYPE: $ 2182 PROTOTYPE: $
1719 CODE: 2183 CODE:
1720 RETVAL = boolSV (!!coro->cctx); 2184 RETVAL = boolSV (!!coro->cctx);
1721 OUTPUT: 2185 OUTPUT:
1722 RETVAL 2186 RETVAL
1727 CODE: 2191 CODE:
1728 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2192 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1729 OUTPUT: 2193 OUTPUT:
1730 RETVAL 2194 RETVAL
1731 2195
1732IV 2196UV
1733rss (Coro::State coro) 2197rss (Coro::State coro)
1734 PROTOTYPE: $ 2198 PROTOTYPE: $
1735 ALIAS: 2199 ALIAS:
1736 usecount = 1 2200 usecount = 1
1737 CODE: 2201 CODE:
1741 case 1: RETVAL = coro->usecount; break; 2205 case 1: RETVAL = coro->usecount; break;
1742 } 2206 }
1743 OUTPUT: 2207 OUTPUT:
1744 RETVAL 2208 RETVAL
1745 2209
2210void
2211force_cctx ()
2212 CODE:
2213 struct coro *coro = SvSTATE (coro_current);
2214 coro->cctx->idle_sp = 0;
2215
2216void
2217swap_defsv (Coro::State self)
2218 PROTOTYPE: $
2219 ALIAS:
2220 swap_defav = 1
2221 CODE:
2222 if (!self->slot)
2223 croak ("cannot swap state with coroutine that has no saved state");
2224 else
2225 {
2226 SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv);
2227 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
2228
2229 SV *tmp = *src; *src = *dst; *dst = tmp;
2230 }
1746 2231
1747MODULE = Coro::State PACKAGE = Coro 2232MODULE = Coro::State PACKAGE = Coro
1748 2233
1749BOOT: 2234BOOT:
1750{ 2235{
1751 int i; 2236 int i;
1752 2237
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); 2238 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2239 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2240 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1756 2241
1757 coro_current = coro_get_sv ("Coro::current", FALSE); 2242 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1758 SvREADONLY_on (coro_current); 2243 SvREADONLY_on (coro_current);
1759 2244
1760 coro_stash = gv_stashpv ("Coro", TRUE); 2245 coro_stash = gv_stashpv ("Coro", TRUE);
1761 2246
1762 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 2247 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1768 2253
1769 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 2254 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1770 coro_ready[i] = newAV (); 2255 coro_ready[i] = newAV ();
1771 2256
1772 { 2257 {
1773 SV *sv = perl_get_sv ("Coro::API", TRUE); 2258 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
1774 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
1775 2259
1776 coroapi.schedule = api_schedule; 2260 coroapi.schedule = api_schedule;
1777 coroapi.cede = api_cede; 2261 coroapi.cede = api_cede;
1778 coroapi.cede_notself = api_cede_notself; 2262 coroapi.cede_notself = api_cede_notself;
1779 coroapi.ready = api_ready; 2263 coroapi.ready = api_ready;
1790void 2274void
1791_set_current (SV *current) 2275_set_current (SV *current)
1792 PROTOTYPE: $ 2276 PROTOTYPE: $
1793 CODE: 2277 CODE:
1794 SvREFCNT_dec (SvRV (coro_current)); 2278 SvREFCNT_dec (SvRV (coro_current));
1795 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 2279 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2280
2281void
2282_set_readyhook (SV *hook)
2283 PROTOTYPE: $
2284 CODE:
2285 LOCK;
2286 SvREFCNT_dec (coro_readyhook);
2287 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2288 UNLOCK;
1796 2289
1797int 2290int
1798prio (Coro::State coro, int newprio = 0) 2291prio (Coro::State coro, int newprio = 0)
1799 ALIAS: 2292 ALIAS:
1800 nice = 1 2293 nice = 1
1830 CODE: 2323 CODE:
1831 RETVAL = coro_nready; 2324 RETVAL = coro_nready;
1832 OUTPUT: 2325 OUTPUT:
1833 RETVAL 2326 RETVAL
1834 2327
1835void
1836throw (Coro::State self, SV *throw = &PL_sv_undef)
1837 PROTOTYPE: $;$
1838 CODE:
1839 SvREFCNT_dec (self->throw);
1840 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
1841
1842# for async_pool speedup 2328# for async_pool speedup
1843void 2329void
1844_pool_1 (SV *cb) 2330_pool_1 (SV *cb)
1845 CODE: 2331 CODE:
1846{ 2332{
1847 struct coro *coro = SvSTATE (coro_current); 2333 struct coro *coro = SvSTATE (coro_current);
1848 HV *hv = (HV *)SvRV (coro_current); 2334 HV *hv = (HV *)SvRV (coro_current);
1849 AV *defav = GvAV (PL_defgv); 2335 AV *defav = GvAV (PL_defgv);
1850 SV *invoke = hv_delete (hv, strpair ("_invoke"), 0); 2336 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1851 AV *invoke_av; 2337 AV *invoke_av;
1852 int i, len; 2338 int i, len;
1853 2339
1854 if (!invoke) 2340 if (!invoke)
2341 {
2342 SV *old = PL_diehook;
2343 PL_diehook = 0;
2344 SvREFCNT_dec (old);
1855 croak ("\3async_pool terminate\2\n"); 2345 croak ("\3async_pool terminate\2\n");
2346 }
1856 2347
1857 SvREFCNT_dec (coro->saved_deffh); 2348 SvREFCNT_dec (coro->saved_deffh);
1858 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2349 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1859 2350
1860 hv_store (hv, "desc", sizeof ("desc") - 1, 2351 hv_store (hv, "desc", sizeof ("desc") - 1,
1861 newSVpvn (strpair ("[async_pool]")), 0); 2352 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1862 2353
1863 invoke_av = (AV *)SvRV (invoke); 2354 invoke_av = (AV *)SvRV (invoke);
1864 len = av_len (invoke_av); 2355 len = av_len (invoke_av);
1865 2356
1866 sv_setsv (cb, AvARRAY (invoke_av)[0]); 2357 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1867 2358
1868 if (len > 0) 2359 if (len > 0)
1869 { 2360 {
1870 av_fill (defav, len - 1); 2361 av_fill (defav, len - 1);
1871 for (i = 0; i < len; ++i) 2362 for (i = 0; i < len; ++i)
1872 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2363 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
1873 } 2364 }
1874 2365
1875 SvREFCNT_dec (invoke); 2366 SvREFCNT_dec (invoke);
1876} 2367}
1877 2368
1884 sv_setsv (cb, &PL_sv_undef); 2375 sv_setsv (cb, &PL_sv_undef);
1885 2376
1886 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2377 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1887 coro->saved_deffh = 0; 2378 coro->saved_deffh = 0;
1888 2379
1889 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2380 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
1890 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2381 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2382 {
2383 SV *old = PL_diehook;
2384 PL_diehook = 0;
2385 SvREFCNT_dec (old);
1891 croak ("\3async_pool terminate\2\n"); 2386 croak ("\3async_pool terminate\2\n");
2387 }
1892 2388
1893 av_clear (GvAV (PL_defgv)); 2389 av_clear (GvAV (PL_defgv));
1894 hv_store ((HV *)SvRV (coro_current), strpair ("desc"), 2390 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1895 newSVpvn (strpair ("[async_pool idle]")), 0); 2391 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1896 2392
1897 coro->prio = 0; 2393 coro->prio = 0;
1898 2394
1899 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 2395 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1900 api_trace (coro_current, 0); 2396 api_trace (coro_current, 0);
1901 2397
1902 av_push (av_async_pool, newSVsv (coro_current)); 2398 av_push (av_async_pool, newSVsv (coro_current));
1903} 2399}
1904 2400
2401#if 0
2402
2403void
2404_generator_call (...)
2405 PROTOTYPE: @
2406 PPCODE:
2407 fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr);
2408 xxxx
2409 abort ();
2410
2411SV *
2412gensub (SV *sub, ...)
2413 PROTOTYPE: &;@
2414 CODE:
2415{
2416 struct coro *coro;
2417 MAGIC *mg;
2418 CV *xcv;
2419 CV *ncv = (CV *)newSV_type (SVt_PVCV);
2420 int i;
2421
2422 CvGV (ncv) = CvGV (cv);
2423 CvFILE (ncv) = CvFILE (cv);
2424
2425 Newz (0, coro, 1, struct coro);
2426 coro->args = newAV ();
2427 coro->flags = CF_NEW;
2428
2429 av_extend (coro->args, items - 1);
2430 for (i = 1; i < items; i++)
2431 av_push (coro->args, newSVsv (ST (i)));
2432
2433 CvISXSUB_on (ncv);
2434 CvXSUBANY (ncv).any_ptr = (void *)coro;
2435
2436 xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV));
2437
2438 CvXSUB (ncv) = CvXSUB (xcv);
2439 CvANON_on (ncv);
2440
2441 mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0);
2442 RETVAL = newRV_noinc ((SV *)ncv);
2443}
2444 OUTPUT:
2445 RETVAL
2446
2447#endif
2448
1905 2449
1906MODULE = Coro::State PACKAGE = Coro::AIO 2450MODULE = Coro::State PACKAGE = Coro::AIO
1907 2451
1908SV * 2452void
1909_get_state () 2453_get_state (SV *self)
1910 CODE: 2454 PPCODE:
1911{ 2455{
1912 struct io_state *data; 2456 AV *defav = GvAV (PL_defgv);
1913 2457 AV *av = newAV ();
2458 int i;
1914 RETVAL = newSV (sizeof (struct io_state)); 2459 SV *data_sv = newSV (sizeof (struct io_state));
1915 data = (struct io_state *)SvPVX (RETVAL); 2460 struct io_state *data = (struct io_state *)SvPVX (data_sv);
1916 SvCUR_set (RETVAL, sizeof (struct io_state)); 2461 SvCUR_set (data_sv, sizeof (struct io_state));
1917 SvPOK_only (RETVAL); 2462 SvPOK_only (data_sv);
1918 2463
1919 data->errorno = errno; 2464 data->errorno = errno;
1920 data->laststype = PL_laststype; 2465 data->laststype = PL_laststype;
1921 data->laststatval = PL_laststatval; 2466 data->laststatval = PL_laststatval;
1922 data->statcache = PL_statcache; 2467 data->statcache = PL_statcache;
2468
2469 av_extend (av, AvFILLp (defav) + 1 + 1);
2470
2471 for (i = 0; i <= AvFILLp (defav); ++i)
2472 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2473
2474 av_push (av, data_sv);
2475
2476 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2477
2478 api_ready (self);
1923} 2479}
1924 OUTPUT:
1925 RETVAL
1926 2480
1927void 2481void
1928_set_state (char *data_) 2482_set_state (SV *state)
1929 PROTOTYPE: $ 2483 PROTOTYPE: $
1930 CODE: 2484 PPCODE:
1931{ 2485{
1932 struct io_state *data = (void *)data_; 2486 AV *av = (AV *)SvRV (state);
2487 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2488 int i;
1933 2489
1934 errno = data->errorno; 2490 errno = data->errorno;
1935 PL_laststype = data->laststype; 2491 PL_laststype = data->laststype;
1936 PL_laststatval = data->laststatval; 2492 PL_laststatval = data->laststatval;
1937 PL_statcache = data->statcache; 2493 PL_statcache = data->statcache;
1938}
1939 2494
2495 EXTEND (SP, AvFILLp (av));
2496 for (i = 0; i < AvFILLp (av); ++i)
2497 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2498}
2499
2500
2501MODULE = Coro::State PACKAGE = Coro::AnyEvent
2502
2503BOOT:
2504 sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE);
2505
2506SV *
2507_schedule (...)
2508 PROTOTYPE: @
2509 CODE:
2510{
2511 static int incede;
2512
2513 api_cede_notself ();
2514
2515 ++incede;
2516 while (coro_nready >= incede && api_cede ())
2517 ;
2518
2519 sv_setsv (sv_activity, &PL_sv_undef);
2520 if (coro_nready >= incede)
2521 {
2522 PUSHMARK (SP);
2523 PUTBACK;
2524 call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL);
2525 SPAGAIN;
2526 }
2527
2528 --incede;
2529}
2530
2531
2532MODULE = Coro::State PACKAGE = PerlIO::cede
2533
2534BOOT:
2535 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2536

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines