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