ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
Revision: 1.265
Committed: Fri Nov 14 02:42:26 2008 UTC (15 years, 6 months ago) by root
Branch: MAIN
Changes since 1.264: +27 -11 lines
Log Message:
*** empty log message ***

File Contents

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