ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
Revision: 1.264
Committed: Fri Nov 14 02:29:09 2008 UTC (15 years, 6 months ago) by root
Branch: MAIN
Changes since 1.263: +129 -60 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.137 /** coroutine stack handling ************************************************/
717    
718 root 1.214 static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
719 root 1.220 static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
720 root 1.239 static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
721 root 1.214
722 root 1.238 /* apparently < 5.8.8 */
723     #ifndef MgPV_nolen_const
724     #define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
725 root 1.241 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
726 root 1.238 (const char*)(mg)->mg_ptr)
727     #endif
728    
729 root 1.263 /* we sometimes need to create the effect of entersub calling us */
730 root 1.264 #define SSL_HEAD (void)0
731 root 1.263 /* we somtimes need to create the effect of leaving via entersub */
732 root 1.264 #define SSL_TAIL (void)0
733 root 1.263
734 root 1.214 /*
735     * This overrides the default magic get method of %SIG elements.
736     * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
737     * and instead of tryign to save and restore the hash elements, we just provide
738     * readback here.
739     * We only do this when the hook is != 0, as they are often set to 0 temporarily,
740     * not expecting this to actually change the hook. This is a potential problem
741     * when a schedule happens then, but we ignore this.
742     */
743     static int
744     coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
745     {
746     const char *s = MgPV_nolen_const (mg);
747    
748     if (*s == '_')
749     {
750 root 1.239 SV **svp = 0;
751    
752     if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
753     if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
754    
755     if (svp)
756     {
757     sv_setsv (sv, *svp ? *svp : &PL_sv_undef);
758     return 0;
759     }
760 root 1.214 }
761    
762 root 1.220 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
763     }
764    
765     static int
766 root 1.239 coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
767     {
768     const char *s = MgPV_nolen_const (mg);
769    
770     if (*s == '_')
771     {
772     SV **svp = 0;
773    
774     if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
775     if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
776    
777     if (svp)
778     {
779     SV *old = *svp;
780     *svp = 0;
781     SvREFCNT_dec (old);
782     return 0;
783     }
784     }
785    
786     return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0;
787     }
788    
789     static int
790 root 1.220 coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
791     {
792     const char *s = MgPV_nolen_const (mg);
793    
794     if (*s == '_')
795     {
796     SV **svp = 0;
797    
798     if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
799     if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
800    
801     if (svp)
802     {
803     SV *old = *svp;
804     *svp = newSVsv (sv);
805     SvREFCNT_dec (old);
806 root 1.231 return 0;
807 root 1.220 }
808     }
809    
810     return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0;
811 root 1.214 }
812    
813 root 1.13 static void
814 root 1.179 coro_setup (pTHX_ struct coro *coro)
815 root 1.13 {
816 root 1.89 /*
817     * emulate part of the perl startup here.
818     */
819 root 1.146 coro_init_stacks (aTHX);
820 root 1.15
821 root 1.169 PL_runops = RUNOPS_DEFAULT;
822 root 1.117 PL_curcop = &PL_compiling;
823     PL_in_eval = EVAL_NULL;
824 root 1.142 PL_comppad = 0;
825 root 1.117 PL_curpm = 0;
826 root 1.196 PL_curpad = 0;
827 root 1.117 PL_localizing = 0;
828     PL_dirty = 0;
829     PL_restartop = 0;
830 root 1.229 #if PERL_VERSION_ATLEAST (5,10,0)
831 root 1.228 PL_parser = 0;
832     #endif
833 root 1.214
834     /* recreate the die/warn hooks */
835 root 1.220 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook );
836     PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
837 root 1.178
838 root 1.191 GvSV (PL_defgv) = newSV (0);
839 root 1.178 GvAV (PL_defgv) = coro->args; coro->args = 0;
840 root 1.191 GvSV (PL_errgv) = newSV (0);
841 root 1.180 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
842 root 1.179 PL_rs = newSVsv (GvSV (irsgv));
843 root 1.249 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
844 root 1.103
845 root 1.102 {
846     dSP;
847 root 1.260 UNOP myop;
848 root 1.102
849 root 1.260 Zero (&myop, 1, UNOP);
850 root 1.102 myop.op_next = Nullop;
851     myop.op_flags = OPf_WANT_VOID;
852 root 1.89
853 root 1.102 PUSHMARK (SP);
854 root 1.192 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
855 root 1.102 PUTBACK;
856 root 1.120 PL_op = (OP *)&myop;
857 root 1.102 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
858     SPAGAIN;
859 root 1.117 }
860 root 1.210
861     /* this newly created coroutine might be run on an existing cctx which most
862     * likely was suspended in set_stacklevel, called from entersub.
863 root 1.261 * set_stacklevel doesn't do anything on return, but entersub does LEAVE,
864 root 1.260 * so we ENTER here for symmetry.
865 root 1.210 */
866 root 1.264 SSL_HEAD;
867 root 1.13 }
868    
869     static void
870 root 1.253 coro_destruct (pTHX_ struct coro *coro)
871 root 1.178 {
872     if (!IN_DESTRUCT)
873     {
874     /* restore all saved variables and stuff */
875     LEAVE_SCOPE (0);
876     assert (PL_tmps_floor == -1);
877    
878     /* free all temporaries */
879     FREETMPS;
880     assert (PL_tmps_ix == -1);
881    
882     /* unwind all extra stacks */
883     POPSTACK_TO (PL_mainstack);
884    
885     /* unwind main stack */
886     dounwind (-1);
887     }
888    
889     SvREFCNT_dec (GvSV (PL_defgv));
890     SvREFCNT_dec (GvAV (PL_defgv));
891     SvREFCNT_dec (GvSV (PL_errgv));
892 root 1.180 SvREFCNT_dec (PL_defoutgv);
893 root 1.178 SvREFCNT_dec (PL_rs);
894     SvREFCNT_dec (GvSV (irsgv));
895    
896 root 1.198 SvREFCNT_dec (PL_diehook);
897     SvREFCNT_dec (PL_warnhook);
898    
899 root 1.184 SvREFCNT_dec (coro->saved_deffh);
900 root 1.196 SvREFCNT_dec (coro->throw);
901 root 1.181
902 root 1.253 coro_destruct_stacks (aTHX);
903 root 1.178 }
904    
905 root 1.263 INLINE void
906 root 1.146 free_coro_mortal (pTHX)
907 root 1.13 {
908 root 1.194 if (expect_true (coro_mortal))
909 root 1.15 {
910 root 1.89 SvREFCNT_dec (coro_mortal);
911     coro_mortal = 0;
912     }
913 root 1.13 }
914    
915 root 1.165 static int
916 root 1.169 runops_trace (pTHX)
917 root 1.165 {
918     COP *oldcop = 0;
919 root 1.167 int oldcxix = -2;
920 root 1.169 struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */
921     coro_cctx *cctx = coro->cctx;
922 root 1.165
923     while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
924     {
925     PERL_ASYNC_CHECK ();
926    
927 root 1.173 if (cctx->flags & CC_TRACE_ALL)
928 root 1.167 {
929 root 1.173 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB)
930     {
931     PERL_CONTEXT *cx = &cxstack[cxstack_ix];
932     SV **bot, **top;
933     AV *av = newAV (); /* return values */
934     SV **cb;
935     dSP;
936 root 1.167
937 root 1.173 GV *gv = CvGV (cx->blk_sub.cv);
938     SV *fullname = sv_2mortal (newSV (0));
939     if (isGV (gv))
940     gv_efullname3 (fullname, gv, 0);
941    
942     bot = PL_stack_base + cx->blk_oldsp + 1;
943     top = cx->blk_gimme == G_ARRAY ? SP + 1
944     : cx->blk_gimme == G_SCALAR ? bot + 1
945     : bot;
946 root 1.167
947 root 1.193 av_extend (av, top - bot);
948 root 1.173 while (bot < top)
949 root 1.249 av_push (av, SvREFCNT_inc_NN (*bot++));
950 root 1.167
951 root 1.173 PL_runops = RUNOPS_DEFAULT;
952 root 1.167 ENTER;
953     SAVETMPS;
954     EXTEND (SP, 3);
955 root 1.173 PUSHMARK (SP);
956     PUSHs (&PL_sv_no);
957     PUSHs (fullname);
958     PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
959     PUTBACK;
960 root 1.212 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
961 root 1.173 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
962     SPAGAIN;
963     FREETMPS;
964     LEAVE;
965     PL_runops = runops_trace;
966     }
967    
968     if (oldcop != PL_curcop)
969     {
970     oldcop = PL_curcop;
971 root 1.167
972 root 1.173 if (PL_curcop != &PL_compiling)
973 root 1.167 {
974 root 1.173 SV **cb;
975    
976     if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB)
977     {
978     PERL_CONTEXT *cx = &cxstack[cxstack_ix];
979    
980     if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
981     {
982     runops_proc_t old_runops = PL_runops;
983     dSP;
984     GV *gv = CvGV (cx->blk_sub.cv);
985     SV *fullname = sv_2mortal (newSV (0));
986    
987     if (isGV (gv))
988     gv_efullname3 (fullname, gv, 0);
989    
990     PL_runops = RUNOPS_DEFAULT;
991     ENTER;
992     SAVETMPS;
993     EXTEND (SP, 3);
994     PUSHMARK (SP);
995     PUSHs (&PL_sv_yes);
996     PUSHs (fullname);
997 root 1.223 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
998 root 1.173 PUTBACK;
999 root 1.212 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
1000 root 1.173 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1001     SPAGAIN;
1002     FREETMPS;
1003     LEAVE;
1004     PL_runops = runops_trace;
1005     }
1006    
1007     oldcxix = cxstack_ix;
1008     }
1009 root 1.167
1010 root 1.173 if (cctx->flags & CC_TRACE_LINE)
1011 root 1.167 {
1012 root 1.173 dSP;
1013 root 1.167
1014 root 1.173 PL_runops = RUNOPS_DEFAULT;
1015     ENTER;
1016     SAVETMPS;
1017     EXTEND (SP, 3);
1018     PL_runops = RUNOPS_DEFAULT;
1019 root 1.167 PUSHMARK (SP);
1020 root 1.173 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
1021     PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
1022 root 1.167 PUTBACK;
1023 root 1.212 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
1024 root 1.169 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1025 root 1.167 SPAGAIN;
1026 root 1.173 FREETMPS;
1027     LEAVE;
1028     PL_runops = runops_trace;
1029 root 1.167 }
1030 root 1.169 }
1031 root 1.167 }
1032 root 1.165 }
1033     }
1034    
1035     TAINT_NOT;
1036     return 0;
1037     }
1038    
1039 root 1.260 static void
1040     prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx)
1041     {
1042     ta->prev = (struct coro *)cctx;
1043     ta->next = 0;
1044     }
1045    
1046 root 1.120 /* inject a fake call to Coro::State::_cctx_init into the execution */
1047 root 1.150 /* _cctx_init should be careful, as it could be called at almost any time */
1048     /* during execution of a perl program */
1049 root 1.259 /* also initialises PL_top_env */
1050 root 1.100 static void NOINLINE
1051 root 1.201 cctx_prepare (pTHX_ coro_cctx *cctx)
1052 root 1.99 {
1053     dSP;
1054 root 1.260 UNOP myop;
1055 root 1.99
1056 root 1.165 PL_top_env = &PL_start_env;
1057    
1058     if (cctx->flags & CC_TRACE)
1059 root 1.169 PL_runops = runops_trace;
1060 root 1.165
1061 root 1.260 Zero (&myop, 1, UNOP);
1062     myop.op_next = PL_op;
1063 root 1.120 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
1064 root 1.102
1065     PUSHMARK (SP);
1066 root 1.120 EXTEND (SP, 2);
1067 root 1.260 PUSHs (sv_2mortal (newSViv ((IV)cctx)));
1068 root 1.120 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
1069 root 1.99 PUTBACK;
1070 root 1.120 PL_op = (OP *)&myop;
1071     PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1072 root 1.99 SPAGAIN;
1073     }
1074    
1075 root 1.262 /* the tail of transfer: execute stuff we can only do after a transfer */
1076 root 1.263 INLINE void
1077 root 1.262 transfer_tail (pTHX)
1078 root 1.259 {
1079 root 1.260 struct coro *next = (struct coro *)transfer_next;
1080 root 1.263 assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */
1081 root 1.264 assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next));
1082 root 1.260
1083     free_coro_mortal (aTHX);
1084 root 1.259 UNLOCK;
1085    
1086 root 1.260 if (expect_false (next->throw))
1087 root 1.259 {
1088 root 1.260 SV *exception = sv_2mortal (next->throw);
1089 root 1.259
1090 root 1.260 next->throw = 0;
1091     sv_setsv (ERRSV, exception);
1092     croak (0);
1093 root 1.259 }
1094     }
1095    
1096 root 1.148 /*
1097     * this is a _very_ stripped down perl interpreter ;)
1098     */
1099 root 1.99 static void
1100 root 1.201 cctx_run (void *arg)
1101 root 1.13 {
1102 root 1.256 #ifdef USE_ITHREADS
1103     # if CORO_PTHREAD
1104     PERL_SET_CONTEXT (coro_thx);
1105     # endif
1106     #endif
1107     {
1108     dTHX;
1109 root 1.146
1110 root 1.260 /* entersub called ENTER, but we never 'returned', undo that here */
1111 root 1.264 SSL_TAIL;
1112 root 1.260
1113 root 1.264 /* we now skip the op that did lead to transfer() */
1114 root 1.256 PL_op = PL_op->op_next;
1115 root 1.107
1116 root 1.256 /* inject a fake subroutine call to cctx_init */
1117     cctx_prepare (aTHX_ (coro_cctx *)arg);
1118 root 1.89
1119 root 1.260 /* cctx_run is the alternative tail of transfer() */
1120 root 1.262 transfer_tail (aTHX);
1121 root 1.259
1122 root 1.256 /* somebody or something will hit me for both perl_run and PL_restartop */
1123     PL_restartop = PL_op;
1124     perl_run (PL_curinterp);
1125 root 1.89
1126 root 1.256 /*
1127     * If perl-run returns we assume exit() was being called or the coro
1128     * fell off the end, which seems to be the only valid (non-bug)
1129     * reason for perl_run to return. We try to exit by jumping to the
1130     * bootstrap-time "top" top_env, as we cannot restore the "main"
1131     * coroutine as Coro has no such concept
1132     */
1133     PL_top_env = main_top_env;
1134     JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1135     }
1136 root 1.89 }
1137    
1138 root 1.106 static coro_cctx *
1139     cctx_new ()
1140 root 1.89 {
1141 root 1.106 coro_cctx *cctx;
1142 root 1.89
1143 root 1.107 ++cctx_count;
1144 root 1.258 New (0, cctx, 1, coro_cctx);
1145    
1146 root 1.259 cctx->gen = cctx_gen;
1147     cctx->flags = 0;
1148 root 1.260 cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */
1149 root 1.258
1150     return cctx;
1151     }
1152    
1153     /* create a new cctx only suitable as source */
1154     static coro_cctx *
1155     cctx_new_empty ()
1156     {
1157     coro_cctx *cctx = cctx_new ();
1158 root 1.89
1159 root 1.259 cctx->sptr = 0;
1160 root 1.258 coro_create (&cctx->cctx, 0, 0, 0, 0);
1161    
1162     return cctx;
1163     }
1164    
1165     /* create a new cctx suitable as destination/running a perl interpreter */
1166     static coro_cctx *
1167     cctx_new_run ()
1168     {
1169     coro_cctx *cctx = cctx_new ();
1170     void *stack_start;
1171     size_t stack_size;
1172 root 1.254
1173 root 1.89 #if HAVE_MMAP
1174 root 1.254 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1175 root 1.120 /* mmap supposedly does allocate-on-write for us */
1176 root 1.106 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1177 root 1.19
1178 root 1.141 if (cctx->sptr != (void *)-1)
1179 root 1.13 {
1180 root 1.263 #if CORO_STACKGUARD
1181     mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1182     #endif
1183     stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1184     stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1185 root 1.165 cctx->flags |= CC_MAPPED;
1186 root 1.141 }
1187     else
1188     #endif
1189 root 1.89 {
1190 root 1.254 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1191     New (0, cctx->sptr, cctx_stacksize, long);
1192 root 1.13
1193 root 1.141 if (!cctx->sptr)
1194     {
1195 root 1.264 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
1196 root 1.141 _exit (EXIT_FAILURE);
1197     }
1198 root 1.89
1199 root 1.145 stack_start = cctx->sptr;
1200     stack_size = cctx->ssize;
1201     }
1202 root 1.104
1203 root 1.263 #if CORO_USE_VALGRIND
1204     cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1205     #endif
1206    
1207 root 1.201 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size);
1208 root 1.13
1209 root 1.106 return cctx;
1210 root 1.13 }
1211    
1212 root 1.15 static void
1213 root 1.115 cctx_destroy (coro_cctx *cctx)
1214 root 1.15 {
1215 root 1.106 if (!cctx)
1216 root 1.89 return;
1217    
1218 root 1.107 --cctx_count;
1219 root 1.253 coro_destroy (&cctx->cctx);
1220 root 1.107
1221 root 1.253 /* coro_transfer creates new, empty cctx's */
1222     if (cctx->sptr)
1223     {
1224 root 1.263 #if CORO_USE_VALGRIND
1225     VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1226     #endif
1227 root 1.104
1228 root 1.89 #if HAVE_MMAP
1229 root 1.253 if (cctx->flags & CC_MAPPED)
1230     munmap (cctx->sptr, cctx->ssize);
1231     else
1232 root 1.89 #endif
1233 root 1.253 Safefree (cctx->sptr);
1234     }
1235 root 1.89
1236 root 1.106 Safefree (cctx);
1237 root 1.89 }
1238 root 1.32
1239 root 1.193 /* wether this cctx should be destructed */
1240 root 1.254 #define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1241 root 1.193
1242 root 1.106 static coro_cctx *
1243 root 1.211 cctx_get (pTHX)
1244 root 1.89 {
1245 root 1.211 while (expect_true (cctx_first))
1246 root 1.89 {
1247 root 1.211 coro_cctx *cctx = cctx_first;
1248     cctx_first = cctx->next;
1249     --cctx_idle;
1250 root 1.145
1251 root 1.194 if (expect_true (!CCTX_EXPIRED (cctx)))
1252 root 1.145 return cctx;
1253    
1254     cctx_destroy (cctx);
1255 root 1.89 }
1256 root 1.91
1257 root 1.258 return cctx_new_run ();
1258 root 1.89 }
1259 root 1.19
1260 root 1.89 static void
1261 root 1.211 cctx_put (coro_cctx *cctx)
1262 root 1.89 {
1263 root 1.263 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
1264 root 1.253
1265 root 1.115 /* free another cctx if overlimit */
1266 root 1.254 if (expect_false (cctx_idle >= cctx_max_idle))
1267 root 1.115 {
1268 root 1.211 coro_cctx *first = cctx_first;
1269     cctx_first = first->next;
1270     --cctx_idle;
1271 root 1.115
1272     cctx_destroy (first);
1273     }
1274    
1275 root 1.211 ++cctx_idle;
1276     cctx->next = cctx_first;
1277     cctx_first = cctx;
1278 root 1.15 }
1279    
1280 root 1.137 /** coroutine switching *****************************************************/
1281    
1282 root 1.194 static void
1283 root 1.170 transfer_check (pTHX_ struct coro *prev, struct coro *next)
1284     {
1285 root 1.194 if (expect_true (prev != next))
1286 root 1.170 {
1287 root 1.194 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1288 root 1.170 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states");
1289    
1290 root 1.194 if (expect_false (next->flags & CF_RUNNING))
1291 root 1.170 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
1292    
1293 root 1.194 if (expect_false (next->flags & CF_DESTROYED))
1294 root 1.170 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
1295    
1296 root 1.228 #if !PERL_VERSION_ATLEAST (5,10,0)
1297 root 1.230 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1298 root 1.229 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version");
1299 root 1.198 #endif
1300 root 1.170 }
1301     }
1302    
1303     /* always use the TRANSFER macro */
1304 root 1.100 static void NOINLINE
1305 root 1.227 transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1306 root 1.8 {
1307 root 1.15 dSTACKLEVEL;
1308 root 1.8
1309 root 1.89 /* sometimes transfer is only called to set idle_sp */
1310 root 1.194 if (expect_false (!next))
1311 root 1.124 {
1312     ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1313 root 1.132 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1314 root 1.124 }
1315 root 1.194 else if (expect_true (prev != next))
1316 root 1.8 {
1317 root 1.106 coro_cctx *prev__cctx;
1318 root 1.89
1319 root 1.194 if (expect_false (prev->flags & CF_NEW))
1320 root 1.111 {
1321 root 1.253 /* create a new empty/source context */
1322 root 1.258 prev->cctx = cctx_new_empty ();
1323 root 1.117 prev->flags &= ~CF_NEW;
1324     prev->flags |= CF_RUNNING;
1325 root 1.111 }
1326    
1327     prev->flags &= ~CF_RUNNING;
1328     next->flags |= CF_RUNNING;
1329    
1330 root 1.92 LOCK;
1331 root 1.13
1332 root 1.214 /* first get rid of the old state */
1333     save_perl (aTHX_ prev);
1334    
1335 root 1.194 if (expect_false (next->flags & CF_NEW))
1336 root 1.90 {
1337     /* need to start coroutine */
1338 root 1.117 next->flags &= ~CF_NEW;
1339 root 1.89 /* setup coroutine call */
1340 root 1.179 coro_setup (aTHX_ next);
1341 root 1.8 }
1342 root 1.118 else
1343 root 1.214 load_perl (aTHX_ next);
1344 root 1.15
1345 root 1.106 prev__cctx = prev->cctx;
1346 root 1.92
1347 root 1.261 /* possibly untie and reuse the cctx */
1348 root 1.227 if (expect_true (
1349     prev__cctx->idle_sp == STACKLEVEL
1350     && !(prev__cctx->flags & CC_TRACE)
1351     && !force_cctx
1352     ))
1353 root 1.92 {
1354 root 1.114 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
1355 root 1.263 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te));
1356 root 1.112
1357 root 1.117 prev->cctx = 0;
1358    
1359 root 1.193 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1360 root 1.194 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1361     if (expect_false (CCTX_EXPIRED (prev__cctx)))
1362 root 1.201 if (!next->cctx)
1363 root 1.211 next->cctx = cctx_get (aTHX);
1364 root 1.193
1365 root 1.211 cctx_put (prev__cctx);
1366 root 1.92 }
1367    
1368 root 1.172 ++next->usecount;
1369    
1370 root 1.194 if (expect_true (!next->cctx))
1371 root 1.211 next->cctx = cctx_get (aTHX);
1372 root 1.111
1373 root 1.263 assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next));
1374 root 1.260 transfer_next = next;
1375 root 1.216
1376 root 1.194 if (expect_false (prev__cctx != next->cctx))
1377 root 1.117 {
1378 root 1.106 prev__cctx->top_env = PL_top_env;
1379     PL_top_env = next->cctx->top_env;
1380     coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1381 root 1.92 }
1382    
1383 root 1.262 transfer_tail (aTHX);
1384 root 1.8 }
1385 root 1.39 }
1386 root 1.23
1387 root 1.227 #define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx))
1388 root 1.170 #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1389 root 1.92
1390 root 1.137 /** high level stuff ********************************************************/
1391    
1392 root 1.133 static int
1393 root 1.146 coro_state_destroy (pTHX_ struct coro *coro)
1394 root 1.87 {
1395 root 1.133 if (coro->flags & CF_DESTROYED)
1396     return 0;
1397    
1398     coro->flags |= CF_DESTROYED;
1399 root 1.137
1400     if (coro->flags & CF_READY)
1401     {
1402     /* reduce nready, as destroying a ready coro effectively unreadies it */
1403 root 1.139 /* alternative: look through all ready queues and remove the coro */
1404 root 1.137 LOCK;
1405     --coro_nready;
1406     UNLOCK;
1407     }
1408     else
1409     coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1410 root 1.87
1411     if (coro->mainstack && coro->mainstack != main_mainstack)
1412     {
1413 root 1.140 struct coro temp;
1414    
1415 root 1.177 if (coro->flags & CF_RUNNING)
1416     croak ("FATAL: tried to destroy currently running coroutine");
1417 root 1.133
1418 root 1.146 save_perl (aTHX_ &temp);
1419     load_perl (aTHX_ coro);
1420 root 1.87
1421 root 1.253 coro_destruct (aTHX_ coro);
1422 root 1.87
1423 root 1.198 load_perl (aTHX_ &temp);
1424 root 1.87
1425 root 1.198 coro->slot = 0;
1426 root 1.87 }
1427    
1428 root 1.115 cctx_destroy (coro->cctx);
1429 root 1.87 SvREFCNT_dec (coro->args);
1430 root 1.133
1431 root 1.151 if (coro->next) coro->next->prev = coro->prev;
1432     if (coro->prev) coro->prev->next = coro->next;
1433 root 1.153 if (coro == coro_first) coro_first = coro->next;
1434 root 1.151
1435 root 1.133 return 1;
1436 root 1.87 }
1437    
1438     static int
1439 root 1.134 coro_state_free (pTHX_ SV *sv, MAGIC *mg)
1440 root 1.87 {
1441     struct coro *coro = (struct coro *)mg->mg_ptr;
1442     mg->mg_ptr = 0;
1443    
1444 root 1.151 coro->hv = 0;
1445    
1446 root 1.134 if (--coro->refcnt < 0)
1447     {
1448 root 1.146 coro_state_destroy (aTHX_ coro);
1449 root 1.134 Safefree (coro);
1450     }
1451 root 1.133
1452 root 1.87 return 0;
1453     }
1454    
1455     static int
1456 root 1.98 coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
1457 root 1.87 {
1458     struct coro *coro = (struct coro *)mg->mg_ptr;
1459    
1460     ++coro->refcnt;
1461    
1462     return 0;
1463     }
1464    
1465 root 1.100 static MGVTBL coro_state_vtbl = {
1466     0, 0, 0, 0,
1467 root 1.134 coro_state_free,
1468 root 1.100 0,
1469     #ifdef MGf_DUP
1470     coro_state_dup,
1471 root 1.102 #else
1472     # define MGf_DUP 0
1473 root 1.100 #endif
1474     };
1475 root 1.87
1476 root 1.23 static void
1477 root 1.146 prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
1478 root 1.23 {
1479 root 1.122 ta->prev = SvSTATE (prev_sv);
1480     ta->next = SvSTATE (next_sv);
1481 root 1.170 TRANSFER_CHECK (*ta);
1482 root 1.92 }
1483    
1484     static void
1485 root 1.122 api_transfer (SV *prev_sv, SV *next_sv)
1486 root 1.92 {
1487 root 1.146 dTHX;
1488 root 1.92 struct transfer_args ta;
1489    
1490 root 1.146 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1491 root 1.227 TRANSFER (ta, 1);
1492 root 1.21 }
1493    
1494 root 1.22 /** Coro ********************************************************************/
1495    
1496     static void
1497 root 1.146 coro_enq (pTHX_ SV *coro_sv)
1498 root 1.22 {
1499 root 1.112 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
1500 root 1.22 }
1501    
1502     static SV *
1503 root 1.218 coro_deq (pTHX)
1504 root 1.22 {
1505 root 1.218 int prio;
1506 root 1.22
1507 root 1.218 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1508 root 1.83 if (AvFILLp (coro_ready [prio]) >= 0)
1509 root 1.137 return av_shift (coro_ready [prio]);
1510 root 1.22
1511     return 0;
1512     }
1513    
1514 root 1.111 static int
1515     api_ready (SV *coro_sv)
1516 root 1.23 {
1517 root 1.146 dTHX;
1518 root 1.111 struct coro *coro;
1519 root 1.237 SV *sv_hook;
1520     void (*xs_hook)(void);
1521 root 1.111
1522     if (SvROK (coro_sv))
1523     coro_sv = SvRV (coro_sv);
1524    
1525     coro = SvSTATE (coro_sv);
1526 root 1.112
1527 root 1.111 if (coro->flags & CF_READY)
1528     return 0;
1529 pcg 1.56
1530 root 1.111 coro->flags |= CF_READY;
1531 root 1.39
1532 pcg 1.55 LOCK;
1533 root 1.233
1534 root 1.237 sv_hook = coro_nready ? 0 : coro_readyhook;
1535     xs_hook = coro_nready ? 0 : coroapi.readyhook;
1536 root 1.233
1537 root 1.249 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1538 root 1.137 ++coro_nready;
1539 root 1.233
1540 pcg 1.55 UNLOCK;
1541 root 1.233
1542 root 1.237 if (sv_hook)
1543 root 1.233 {
1544     dSP;
1545    
1546     ENTER;
1547     SAVETMPS;
1548    
1549     PUSHMARK (SP);
1550     PUTBACK;
1551 root 1.237 call_sv (sv_hook, G_DISCARD);
1552 root 1.233 SPAGAIN;
1553    
1554     FREETMPS;
1555     LEAVE;
1556     }
1557 root 1.111
1558 root 1.237 if (xs_hook)
1559     xs_hook ();
1560    
1561 root 1.111 return 1;
1562     }
1563    
1564     static int
1565     api_is_ready (SV *coro_sv)
1566     {
1567 root 1.146 dTHX;
1568 root 1.263
1569 root 1.130 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1570 root 1.23 }
1571    
1572 root 1.263 INLINE void
1573 root 1.146 prepare_schedule (pTHX_ struct transfer_args *ta)
1574 root 1.23 {
1575 root 1.133 SV *prev_sv, *next_sv;
1576 root 1.88
1577     for (;;)
1578     {
1579     LOCK;
1580 root 1.218 next_sv = coro_deq (aTHX);
1581 root 1.88
1582 root 1.133 /* nothing to schedule: call the idle handler */
1583 root 1.194 if (expect_false (!next_sv))
1584 root 1.133 {
1585 root 1.140 dSP;
1586 root 1.138 UNLOCK;
1587 root 1.133
1588     ENTER;
1589     SAVETMPS;
1590 root 1.23
1591 root 1.133 PUSHMARK (SP);
1592     PUTBACK;
1593     call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1594 root 1.219 SPAGAIN;
1595 root 1.133
1596     FREETMPS;
1597     LEAVE;
1598     continue;
1599     }
1600 root 1.88
1601 root 1.133 ta->next = SvSTATE (next_sv);
1602 pcg 1.55
1603 root 1.133 /* cannot transfer to destroyed coros, skip and look for next */
1604 root 1.194 if (expect_false (ta->next->flags & CF_DESTROYED))
1605 root 1.133 {
1606 root 1.138 UNLOCK;
1607 root 1.133 SvREFCNT_dec (next_sv);
1608 root 1.260 /* coro_nready has already been taken care of by destroy */
1609 root 1.133 continue;
1610     }
1611 root 1.23
1612 root 1.137 --coro_nready;
1613     UNLOCK;
1614 root 1.133 break;
1615 root 1.88 }
1616    
1617 root 1.133 /* free this only after the transfer */
1618     prev_sv = SvRV (coro_current);
1619     ta->prev = SvSTATE (prev_sv);
1620 root 1.170 TRANSFER_CHECK (*ta);
1621 root 1.263 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1622 root 1.133 ta->next->flags &= ~CF_READY;
1623 root 1.170 SvRV_set (coro_current, next_sv);
1624 root 1.23
1625 root 1.99 LOCK;
1626 root 1.146 free_coro_mortal (aTHX);
1627 root 1.133 coro_mortal = prev_sv;
1628 root 1.99 UNLOCK;
1629 root 1.92 }
1630    
1631 root 1.263 INLINE void
1632 root 1.146 prepare_cede (pTHX_ struct transfer_args *ta)
1633 root 1.92 {
1634 root 1.117 api_ready (coro_current);
1635 root 1.146 prepare_schedule (aTHX_ ta);
1636 root 1.131 }
1637 pcg 1.55
1638 root 1.131 static int
1639 root 1.146 prepare_cede_notself (pTHX_ struct transfer_args *ta)
1640 root 1.131 {
1641     if (coro_nready)
1642     {
1643     SV *prev = SvRV (coro_current);
1644 root 1.146 prepare_schedule (aTHX_ ta);
1645 root 1.131 api_ready (prev);
1646     return 1;
1647     }
1648     else
1649     return 0;
1650 root 1.39 }
1651    
1652 root 1.92 static void
1653     api_schedule (void)
1654     {
1655 root 1.146 dTHX;
1656 root 1.92 struct transfer_args ta;
1657    
1658 root 1.146 prepare_schedule (aTHX_ &ta);
1659 root 1.227 TRANSFER (ta, 1);
1660 root 1.92 }
1661 root 1.89
1662 root 1.131 static int
1663 root 1.39 api_cede (void)
1664     {
1665 root 1.146 dTHX;
1666 root 1.92 struct transfer_args ta;
1667 root 1.89
1668 root 1.146 prepare_cede (aTHX_ &ta);
1669 root 1.131
1670 root 1.194 if (expect_true (ta.prev != ta.next))
1671 root 1.131 {
1672 root 1.227 TRANSFER (ta, 1);
1673 root 1.131 return 1;
1674     }
1675     else
1676     return 0;
1677     }
1678    
1679     static int
1680     api_cede_notself (void)
1681     {
1682 root 1.146 dTHX;
1683 root 1.131 struct transfer_args ta;
1684    
1685 root 1.146 if (prepare_cede_notself (aTHX_ &ta))
1686 root 1.131 {
1687 root 1.227 TRANSFER (ta, 1);
1688 root 1.131 return 1;
1689     }
1690     else
1691     return 0;
1692 root 1.23 }
1693    
1694 root 1.173 static void
1695 root 1.174 api_trace (SV *coro_sv, int flags)
1696 root 1.173 {
1697     dTHX;
1698 root 1.174 struct coro *coro = SvSTATE (coro_sv);
1699 root 1.173
1700     if (flags & CC_TRACE)
1701     {
1702     if (!coro->cctx)
1703 root 1.258 coro->cctx = cctx_new_run ();
1704 root 1.173 else if (!(coro->cctx->flags & CC_TRACE))
1705     croak ("cannot enable tracing on coroutine with custom stack");
1706    
1707     coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1708     }
1709     else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1710     {
1711     coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1712    
1713     if (coro->flags & CF_RUNNING)
1714     PL_runops = RUNOPS_DEFAULT;
1715     else
1716 root 1.198 coro->slot->runops = RUNOPS_DEFAULT;
1717 root 1.173 }
1718     }
1719    
1720 root 1.255 #if 0
1721 root 1.243 static int
1722     coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1723     {
1724     AV *padlist;
1725     AV *av = (AV *)mg->mg_obj;
1726    
1727     abort ();
1728    
1729     return 0;
1730     }
1731    
1732     static MGVTBL coro_gensub_vtbl = {
1733     0, 0, 0, 0,
1734     coro_gensub_free
1735     };
1736 root 1.255 #endif
1737 root 1.243
1738 root 1.246 /*****************************************************************************/
1739     /* PerlIO::cede */
1740    
1741     typedef struct
1742     {
1743     PerlIOBuf base;
1744     NV next, every;
1745     } PerlIOCede;
1746    
1747     static IV
1748     PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1749     {
1750     PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1751    
1752 root 1.247 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1753 root 1.246 self->next = nvtime () + self->every;
1754    
1755     return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1756     }
1757    
1758     static SV *
1759     PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
1760     {
1761     PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1762    
1763     return newSVnv (self->every);
1764     }
1765    
1766     static IV
1767     PerlIOCede_flush (pTHX_ PerlIO *f)
1768     {
1769     PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1770     double now = nvtime ();
1771    
1772     if (now >= self->next)
1773     {
1774     api_cede ();
1775     self->next = now + self->every;
1776     }
1777    
1778 root 1.248 return PerlIOBuf_flush (aTHX_ f);
1779 root 1.246 }
1780    
1781     static PerlIO_funcs PerlIO_cede =
1782     {
1783     sizeof(PerlIO_funcs),
1784     "cede",
1785     sizeof(PerlIOCede),
1786     PERLIO_K_DESTRUCT | PERLIO_K_RAW,
1787     PerlIOCede_pushed,
1788     PerlIOBuf_popped,
1789     PerlIOBuf_open,
1790     PerlIOBase_binmode,
1791     PerlIOCede_getarg,
1792     PerlIOBase_fileno,
1793     PerlIOBuf_dup,
1794     PerlIOBuf_read,
1795     PerlIOBuf_unread,
1796     PerlIOBuf_write,
1797     PerlIOBuf_seek,
1798     PerlIOBuf_tell,
1799     PerlIOBuf_close,
1800     PerlIOCede_flush,
1801     PerlIOBuf_fill,
1802     PerlIOBase_eof,
1803     PerlIOBase_error,
1804     PerlIOBase_clearerr,
1805     PerlIOBase_setlinebuf,
1806     PerlIOBuf_get_base,
1807     PerlIOBuf_bufsiz,
1808     PerlIOBuf_get_ptr,
1809     PerlIOBuf_get_cnt,
1810     PerlIOBuf_set_ptrcnt,
1811     };
1812    
1813 root 1.264 /*****************************************************************************/
1814    
1815     static const CV *ssl_cv; /* for quick consistency check */
1816    
1817     static UNOP ssl_restore; /* restore stack as entersub did, for first-re-run */
1818     static SV *ssl_arg0;
1819     static SV *ssl_arg1;
1820    
1821     /* this restores the stack in the case we patched the entersub, to */
1822     /* recreate the stack frame as perl will on following calls */
1823     /* since entersub cleared the stack */
1824     static OP *
1825     pp_restore (pTHX)
1826     {
1827     dSP;
1828    
1829     PUSHMARK (SP);
1830    
1831     EXTEND (SP, 3);
1832     if (ssl_arg0) PUSHs (sv_2mortal (ssl_arg0)), ssl_arg0 = 0;
1833     if (ssl_arg1) PUSHs (sv_2mortal (ssl_arg1)), ssl_arg1 = 0;
1834     PUSHs ((SV *)CvGV (ssl_cv));
1835    
1836     RETURNOP (ssl_restore.op_first);
1837     }
1838    
1839     /* declare prototype */
1840     XS(XS_Coro__State__set_stacklevel);
1841    
1842     static OP *
1843     pp_set_stacklevel (pTHX)
1844     {
1845     dSP;
1846     struct transfer_args ta;
1847     SV **arg = PL_stack_base + TOPMARK + 1;
1848     int items = SP - arg; /* args without function object */
1849    
1850     /* do a quick consistency check on the "function" object, and if it isn't */
1851     /* for us, divert to the real entersub */
1852     if (SvTYPE (*sp) != SVt_PVGV || CvXSUB (GvCV (*sp)) != XS_Coro__State__set_stacklevel)
1853     return PL_ppaddr[OP_ENTERSUB](aTHX);
1854    
1855     /* pop args */
1856     SP = PL_stack_base + POPMARK;
1857    
1858     if (!(PL_op->op_flags & OPf_STACKED))
1859     {
1860     /* ampersand-form of call, use @_ instead of stack */
1861     AV *av = GvAV (PL_defgv);
1862     arg = AvARRAY (av);
1863     items = AvFILLp (av) + 1;
1864     }
1865    
1866     PUTBACK;
1867     switch (PL_op->op_private & 7)
1868     {
1869     case 0:
1870     prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0]));
1871     break;
1872    
1873     case 1:
1874     if (items != 2)
1875     croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items);
1876    
1877     prepare_transfer (aTHX_ &ta, arg [0], arg [1]);
1878     break;
1879    
1880     case 2:
1881     prepare_schedule (aTHX_ &ta);
1882     break;
1883    
1884     case 3:
1885     prepare_cede (aTHX_ &ta);
1886     break;
1887    
1888     case 4:
1889     if (!prepare_cede_notself (aTHX_ &ta))
1890     RETURN;
1891    
1892     break;
1893     }
1894    
1895     TRANSFER (ta, 0);
1896     SPAGAIN;
1897    
1898     skip:
1899    
1900     RETURN;
1901     }
1902 root 1.246
1903 root 1.173 MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1904 root 1.1
1905 root 1.87 PROTOTYPES: DISABLE
1906 root 1.1
1907 root 1.264 # these not obviously related functions are all rolled into the same xs
1908     # function to increase chances that they all will call transfer with the same
1909     # stack offset
1910     void
1911     _set_stacklevel (...)
1912     ALIAS:
1913     Coro::State::transfer = 1
1914     Coro::schedule = 2
1915     Coro::cede = 3
1916     Coro::cede_notself = 4
1917     CODE:
1918     {
1919     assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel));
1920    
1921     /* we patch the op, and then re-run the whole call */
1922     /* we have to put some dummy argument on the stack for this to work */
1923     /* TODO: walk back the opcode chain (but how?), nuke the pp_gv etc. */
1924     ssl_restore.op_next = (OP *)&ssl_restore;
1925     ssl_restore.op_type = OP_NULL;
1926     ssl_restore.op_ppaddr = pp_restore;
1927     ssl_restore.op_first = PL_op;
1928    
1929     ssl_arg0 = items > 0 ? SvREFCNT_inc (ST (0)) : 0;
1930     ssl_arg1 = items > 1 ? SvREFCNT_inc (ST (1)) : 0;
1931    
1932     PL_op->op_ppaddr = pp_set_stacklevel;
1933     PL_op->op_private = PL_op->op_private & ~7 | ix; /* we potentially share our private flags with entersub */
1934    
1935     PL_op = (OP *)&ssl_restore;
1936     }
1937    
1938 root 1.3 BOOT:
1939 root 1.88 {
1940 pcg 1.55 #ifdef USE_ITHREADS
1941 root 1.253 MUTEX_INIT (&coro_lock);
1942 root 1.256 # if CORO_PTHREAD
1943     coro_thx = PERL_GET_CONTEXT;
1944     # endif
1945 pcg 1.55 #endif
1946 root 1.78 BOOT_PAGESIZE;
1947 pcg 1.55
1948 root 1.264 ssl_cv = get_cv ("Coro::State::_set_stacklevel", 0);
1949    
1950 root 1.190 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1951     stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1952 root 1.220
1953 root 1.239 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1954     orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1955     orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1956 root 1.220
1957 root 1.213 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1958 root 1.220 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1959     rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1960 root 1.199
1961 root 1.14 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1962 root 1.7
1963 root 1.167 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1964     newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1965     newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1966     newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1967    
1968 root 1.12 main_mainstack = PL_mainstack;
1969 root 1.148 main_top_env = PL_top_env;
1970    
1971     while (main_top_env->je_prev)
1972     main_top_env = main_top_env->je_prev;
1973 root 1.23
1974 root 1.237 coroapi.ver = CORO_API_VERSION;
1975     coroapi.rev = CORO_API_REVISION;
1976     coroapi.transfer = api_transfer;
1977 root 1.87
1978 root 1.246 {
1979     SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1980    
1981     if (!svp) croak ("Time::HiRes is required");
1982     if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1983    
1984     nvtime = INT2PTR (double (*)(), SvIV (*svp));
1985     }
1986    
1987 root 1.87 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1988 root 1.9 }
1989 root 1.3
1990 root 1.87 SV *
1991 root 1.86 new (char *klass, ...)
1992 root 1.1 CODE:
1993 root 1.86 {
1994 root 1.87 struct coro *coro;
1995 root 1.214 MAGIC *mg;
1996 root 1.87 HV *hv;
1997 root 1.86 int i;
1998    
1999 pcg 1.47 Newz (0, coro, 1, struct coro);
2000 root 1.178 coro->args = newAV ();
2001 root 1.117 coro->flags = CF_NEW;
2002 root 1.86
2003 root 1.153 if (coro_first) coro_first->prev = coro;
2004     coro->next = coro_first;
2005     coro_first = coro;
2006 root 1.151
2007     coro->hv = hv = newHV ();
2008 root 1.214 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2009     mg->mg_flags |= MGf_DUP;
2010 root 1.89 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2011    
2012 root 1.192 av_extend (coro->args, items - 1);
2013 root 1.86 for (i = 1; i < items; i++)
2014     av_push (coro->args, newSVsv (ST (i)));
2015     }
2016 root 1.1 OUTPUT:
2017     RETVAL
2018    
2019 root 1.133 bool
2020     _destroy (SV *coro_sv)
2021 root 1.87 CODE:
2022 root 1.146 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
2023 root 1.133 OUTPUT:
2024     RETVAL
2025 root 1.12
2026 root 1.7 void
2027 root 1.233 _exit (int code)
2028 root 1.20 PROTOTYPE: $
2029     CODE:
2030     _exit (code);
2031 root 1.1
2032 root 1.106 int
2033 root 1.145 cctx_stacksize (int new_stacksize = 0)
2034     CODE:
2035 root 1.254 RETVAL = cctx_stacksize;
2036 root 1.145 if (new_stacksize)
2037 root 1.254 {
2038     cctx_stacksize = new_stacksize;
2039     ++cctx_gen;
2040     }
2041     OUTPUT:
2042     RETVAL
2043    
2044     int
2045     cctx_max_idle (int max_idle = 0)
2046     CODE:
2047     RETVAL = cctx_max_idle;
2048     if (max_idle > 1)
2049     cctx_max_idle = max_idle;
2050 root 1.145 OUTPUT:
2051     RETVAL
2052    
2053     int
2054 root 1.106 cctx_count ()
2055     CODE:
2056     RETVAL = cctx_count;
2057     OUTPUT:
2058     RETVAL
2059    
2060     int
2061     cctx_idle ()
2062     CODE:
2063 root 1.211 RETVAL = cctx_idle;
2064 root 1.106 OUTPUT:
2065     RETVAL
2066    
2067 root 1.151 void
2068     list ()
2069     PPCODE:
2070     {
2071     struct coro *coro;
2072 root 1.153 for (coro = coro_first; coro; coro = coro->next)
2073 root 1.151 if (coro->hv)
2074     XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
2075     }
2076    
2077     void
2078 root 1.164 call (Coro::State coro, SV *coderef)
2079     ALIAS:
2080     eval = 1
2081 root 1.151 CODE:
2082     {
2083 root 1.245 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2084 root 1.151 {
2085     struct coro temp;
2086    
2087     if (!(coro->flags & CF_RUNNING))
2088     {
2089 root 1.219 PUTBACK;
2090 root 1.151 save_perl (aTHX_ &temp);
2091     load_perl (aTHX_ coro);
2092     }
2093    
2094     {
2095     dSP;
2096     ENTER;
2097     SAVETMPS;
2098 root 1.208 PUTBACK;
2099     PUSHSTACK;
2100 root 1.151 PUSHMARK (SP);
2101 root 1.198
2102 root 1.164 if (ix)
2103     eval_sv (coderef, 0);
2104     else
2105     call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2106 root 1.198
2107 root 1.208 POPSTACK;
2108 root 1.211 SPAGAIN;
2109 root 1.151 FREETMPS;
2110     LEAVE;
2111 root 1.210 PUTBACK;
2112 root 1.151 }
2113    
2114     if (!(coro->flags & CF_RUNNING))
2115     {
2116     save_perl (aTHX_ coro);
2117     load_perl (aTHX_ &temp);
2118 root 1.219 SPAGAIN;
2119 root 1.151 }
2120     }
2121     }
2122 root 1.172
2123 root 1.151 SV *
2124 root 1.152 is_ready (Coro::State coro)
2125 root 1.151 PROTOTYPE: $
2126     ALIAS:
2127     is_ready = CF_READY
2128     is_running = CF_RUNNING
2129     is_new = CF_NEW
2130     is_destroyed = CF_DESTROYED
2131     CODE:
2132     RETVAL = boolSV (coro->flags & ix);
2133     OUTPUT:
2134     RETVAL
2135    
2136 root 1.165 void
2137 root 1.260 throw (Coro::State self, SV *throw = &PL_sv_undef)
2138     PROTOTYPE: $;$
2139     CODE:
2140     SvREFCNT_dec (self->throw);
2141     self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2142    
2143     void
2144 root 1.174 api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
2145 root 1.165
2146 root 1.162 SV *
2147 root 1.234 has_cctx (Coro::State coro)
2148 root 1.162 PROTOTYPE: $
2149     CODE:
2150     RETVAL = boolSV (!!coro->cctx);
2151     OUTPUT:
2152     RETVAL
2153    
2154 root 1.167 int
2155     is_traced (Coro::State coro)
2156     PROTOTYPE: $
2157     CODE:
2158     RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
2159     OUTPUT:
2160     RETVAL
2161    
2162 root 1.255 UV
2163 root 1.152 rss (Coro::State coro)
2164     PROTOTYPE: $
2165 root 1.172 ALIAS:
2166     usecount = 1
2167 root 1.152 CODE:
2168 root 1.172 switch (ix)
2169     {
2170     case 0: RETVAL = coro_rss (aTHX_ coro); break;
2171     case 1: RETVAL = coro->usecount; break;
2172     }
2173 root 1.152 OUTPUT:
2174     RETVAL
2175    
2176 root 1.226 void
2177     force_cctx ()
2178     CODE:
2179     struct coro *coro = SvSTATE (coro_current);
2180     coro->cctx->idle_sp = 0;
2181 root 1.151
2182 root 1.242 void
2183     swap_defsv (Coro::State self)
2184     PROTOTYPE: $
2185     ALIAS:
2186     swap_defav = 1
2187     CODE:
2188     if (!self->slot)
2189     croak ("cannot swap state with coroutine that has no saved state");
2190     else
2191     {
2192     SV **src = ix ? (SV **)&GvAV (PL_defgv) : &GvSV (PL_defgv);
2193     SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
2194    
2195     SV *tmp = *src; *src = *dst; *dst = tmp;
2196     }
2197    
2198 root 1.21 MODULE = Coro::State PACKAGE = Coro
2199    
2200     BOOT:
2201     {
2202 root 1.22 int i;
2203    
2204 root 1.233 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2205 root 1.213 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2206     sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2207 root 1.159
2208 root 1.213 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
2209 root 1.159 SvREADONLY_on (coro_current);
2210    
2211 root 1.198 coro_stash = gv_stashpv ("Coro", TRUE);
2212 root 1.88
2213     newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2214     newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2215     newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
2216     newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
2217     newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
2218     newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
2219 root 1.22
2220     for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2221     coro_ready[i] = newAV ();
2222 root 1.26
2223     {
2224 root 1.263 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2225 root 1.26
2226 root 1.131 coroapi.schedule = api_schedule;
2227     coroapi.cede = api_cede;
2228     coroapi.cede_notself = api_cede_notself;
2229     coroapi.ready = api_ready;
2230     coroapi.is_ready = api_is_ready;
2231     coroapi.nready = &coro_nready;
2232     coroapi.current = coro_current;
2233 root 1.26
2234     GCoroAPI = &coroapi;
2235 root 1.81 sv_setiv (sv, (IV)&coroapi);
2236     SvREADONLY_on (sv);
2237 root 1.26 }
2238 root 1.21 }
2239    
2240 root 1.122 void
2241     _set_current (SV *current)
2242     PROTOTYPE: $
2243     CODE:
2244     SvREFCNT_dec (SvRV (coro_current));
2245 root 1.249 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2246 root 1.122
2247 root 1.233 void
2248     _set_readyhook (SV *hook)
2249     PROTOTYPE: $
2250     CODE:
2251     LOCK;
2252 root 1.236 SvREFCNT_dec (coro_readyhook);
2253 root 1.233 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2254     UNLOCK;
2255    
2256 root 1.92 int
2257     prio (Coro::State coro, int newprio = 0)
2258     ALIAS:
2259     nice = 1
2260     CODE:
2261     {
2262     RETVAL = coro->prio;
2263    
2264     if (items > 1)
2265     {
2266     if (ix)
2267 root 1.129 newprio = coro->prio - newprio;
2268 root 1.92
2269     if (newprio < PRIO_MIN) newprio = PRIO_MIN;
2270     if (newprio > PRIO_MAX) newprio = PRIO_MAX;
2271    
2272     coro->prio = newprio;
2273     }
2274     }
2275 root 1.130 OUTPUT:
2276     RETVAL
2277 root 1.92
2278 root 1.111 SV *
2279 root 1.89 ready (SV *self)
2280 root 1.35 PROTOTYPE: $
2281 root 1.21 CODE:
2282 root 1.111 RETVAL = boolSV (api_ready (self));
2283     OUTPUT:
2284     RETVAL
2285    
2286 root 1.25 int
2287 root 1.87 nready (...)
2288 root 1.25 PROTOTYPE:
2289     CODE:
2290     RETVAL = coro_nready;
2291     OUTPUT:
2292     RETVAL
2293    
2294 root 1.159 # for async_pool speedup
2295 root 1.161 void
2296     _pool_1 (SV *cb)
2297 root 1.159 CODE:
2298     {
2299 root 1.181 struct coro *coro = SvSTATE (coro_current);
2300 root 1.159 HV *hv = (HV *)SvRV (coro_current);
2301     AV *defav = GvAV (PL_defgv);
2302 root 1.212 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
2303 root 1.159 AV *invoke_av;
2304 root 1.181 int i, len;
2305    
2306 root 1.159 if (!invoke)
2307 root 1.221 {
2308 root 1.240 SV *old = PL_diehook;
2309     PL_diehook = 0;
2310     SvREFCNT_dec (old);
2311 root 1.221 croak ("\3async_pool terminate\2\n");
2312     }
2313 root 1.159
2314 root 1.184 SvREFCNT_dec (coro->saved_deffh);
2315 root 1.249 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2316 root 1.182
2317 root 1.159 hv_store (hv, "desc", sizeof ("desc") - 1,
2318 root 1.212 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2319 root 1.159
2320     invoke_av = (AV *)SvRV (invoke);
2321     len = av_len (invoke_av);
2322    
2323 root 1.161 sv_setsv (cb, AvARRAY (invoke_av)[0]);
2324 root 1.159
2325     if (len > 0)
2326     {
2327 root 1.161 av_fill (defav, len - 1);
2328 root 1.159 for (i = 0; i < len; ++i)
2329 root 1.249 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2330 root 1.159 }
2331    
2332     SvREFCNT_dec (invoke);
2333     }
2334    
2335     void
2336 root 1.161 _pool_2 (SV *cb)
2337 root 1.159 CODE:
2338     {
2339     struct coro *coro = SvSTATE (coro_current);
2340    
2341 root 1.161 sv_setsv (cb, &PL_sv_undef);
2342    
2343 root 1.184 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2344     coro->saved_deffh = 0;
2345 root 1.181
2346 root 1.255 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2347 root 1.159 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2348 root 1.221 {
2349 root 1.240 SV *old = PL_diehook;
2350     PL_diehook = 0;
2351     SvREFCNT_dec (old);
2352 root 1.221 croak ("\3async_pool terminate\2\n");
2353     }
2354 root 1.159
2355     av_clear (GvAV (PL_defgv));
2356 root 1.212 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
2357     newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
2358 root 1.175
2359 root 1.159 coro->prio = 0;
2360 root 1.173
2361     if (coro->cctx && (coro->cctx->flags & CC_TRACE))
2362 root 1.174 api_trace (coro_current, 0);
2363 root 1.173
2364 root 1.159 av_push (av_async_pool, newSVsv (coro_current));
2365     }
2366    
2367 root 1.243 #if 0
2368    
2369     void
2370     _generator_call (...)
2371     PROTOTYPE: @
2372     PPCODE:
2373     fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr);
2374     xxxx
2375     abort ();
2376    
2377     SV *
2378     gensub (SV *sub, ...)
2379     PROTOTYPE: &;@
2380     CODE:
2381     {
2382     struct coro *coro;
2383     MAGIC *mg;
2384     CV *xcv;
2385     CV *ncv = (CV *)newSV_type (SVt_PVCV);
2386     int i;
2387    
2388     CvGV (ncv) = CvGV (cv);
2389     CvFILE (ncv) = CvFILE (cv);
2390    
2391     Newz (0, coro, 1, struct coro);
2392     coro->args = newAV ();
2393     coro->flags = CF_NEW;
2394    
2395     av_extend (coro->args, items - 1);
2396     for (i = 1; i < items; i++)
2397     av_push (coro->args, newSVsv (ST (i)));
2398    
2399     CvISXSUB_on (ncv);
2400     CvXSUBANY (ncv).any_ptr = (void *)coro;
2401    
2402     xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV));
2403    
2404     CvXSUB (ncv) = CvXSUB (xcv);
2405     CvANON_on (ncv);
2406    
2407     mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0);
2408     RETVAL = newRV_noinc ((SV *)ncv);
2409     }
2410     OUTPUT:
2411     RETVAL
2412    
2413     #endif
2414    
2415 root 1.159
2416 root 1.89 MODULE = Coro::State PACKAGE = Coro::AIO
2417    
2418 root 1.249 void
2419     _get_state (SV *self)
2420     PPCODE:
2421 root 1.70 {
2422 root 1.249 AV *defav = GvAV (PL_defgv);
2423     AV *av = newAV ();
2424     int i;
2425     SV *data_sv = newSV (sizeof (struct io_state));
2426     struct io_state *data = (struct io_state *)SvPVX (data_sv);
2427     SvCUR_set (data_sv, sizeof (struct io_state));
2428     SvPOK_only (data_sv);
2429 root 1.120
2430     data->errorno = errno;
2431     data->laststype = PL_laststype;
2432     data->laststatval = PL_laststatval;
2433     data->statcache = PL_statcache;
2434 root 1.249
2435     av_extend (av, AvFILLp (defav) + 1 + 1);
2436    
2437     for (i = 0; i <= AvFILLp (defav); ++i)
2438     av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2439    
2440     av_push (av, data_sv);
2441    
2442     XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2443    
2444     api_ready (self);
2445 root 1.70 }
2446    
2447     void
2448 root 1.249 _set_state (SV *state)
2449 root 1.70 PROTOTYPE: $
2450 root 1.249 PPCODE:
2451 root 1.70 {
2452 root 1.249 AV *av = (AV *)SvRV (state);
2453     struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2454     int i;
2455 root 1.70
2456 root 1.120 errno = data->errorno;
2457     PL_laststype = data->laststype;
2458 root 1.70 PL_laststatval = data->laststatval;
2459 root 1.120 PL_statcache = data->statcache;
2460 root 1.249
2461     EXTEND (SP, AvFILLp (av));
2462     for (i = 0; i < AvFILLp (av); ++i)
2463     PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2464 root 1.70 }
2465 root 1.120
2466 root 1.233
2467     MODULE = Coro::State PACKAGE = Coro::AnyEvent
2468    
2469     BOOT:
2470     sv_activity = coro_get_sv (aTHX_ "Coro::AnyEvent::ACTIVITY", TRUE);
2471    
2472     SV *
2473 root 1.237 _schedule (...)
2474 root 1.233 PROTOTYPE: @
2475     CODE:
2476     {
2477     static int incede;
2478    
2479     api_cede_notself ();
2480    
2481     ++incede;
2482     while (coro_nready >= incede && api_cede ())
2483     ;
2484    
2485     sv_setsv (sv_activity, &PL_sv_undef);
2486     if (coro_nready >= incede)
2487     {
2488     PUSHMARK (SP);
2489     PUTBACK;
2490     call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL);
2491     SPAGAIN;
2492     }
2493    
2494     --incede;
2495     }
2496    
2497 root 1.246
2498     MODULE = Coro::State PACKAGE = PerlIO::cede
2499    
2500     BOOT:
2501     PerlIO_define_layer (aTHX_ &PerlIO_cede);
2502 root 1.263