ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
Revision: 1.111
Committed: Thu Nov 30 18:21:14 2006 UTC (17 years, 5 months ago) by root
Branch: MAIN
Changes since 1.110: +67 -15 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.63 #include "libcoro/coro.c"
2    
3 root 1.1 #include "EXTERN.h"
4     #include "perl.h"
5     #include "XSUB.h"
6    
7 pcg 1.46 #include "patchlevel.h"
8    
9 root 1.104 #if USE_VALGRIND
10     # include <valgrind/valgrind.h>
11     #endif
12    
13 root 1.100 #define PERL_VERSION_ATLEAST(a,b,c) \
14     (PERL_REVISION > (a) \
15     || (PERL_REVISION == (a) \
16     && (PERL_VERSION > (b) \
17     || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
18    
19 root 1.102 #if !PERL_VERSION_ATLEAST (5,6,0)
20 pcg 1.46 # ifndef PL_ppaddr
21     # define PL_ppaddr ppaddr
22     # endif
23     # ifndef call_sv
24     # define call_sv perl_call_sv
25     # endif
26     # ifndef get_sv
27     # define get_sv perl_get_sv
28     # endif
29     # ifndef get_cv
30     # define get_cv perl_get_cv
31     # endif
32     # ifndef IS_PADGV
33     # define IS_PADGV(v) 0
34     # endif
35     # ifndef IS_PADCONST
36     # define IS_PADCONST(v) 0
37     # endif
38     #endif
39    
40 root 1.92 #include <stdio.h>
41 root 1.70 #include <errno.h>
42 root 1.20
43 root 1.78 #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
44     # undef STACKGUARD
45     #endif
46    
47     #ifndef STACKGUARD
48     # define STACKGUARD 0
49     #endif
50    
51 root 1.13 #ifdef HAVE_MMAP
52     # include <unistd.h>
53     # include <sys/mman.h>
54 root 1.36 # ifndef MAP_ANONYMOUS
55     # ifdef MAP_ANON
56     # define MAP_ANONYMOUS MAP_ANON
57 root 1.16 # else
58     # undef HAVE_MMAP
59     # endif
60     # endif
61 root 1.78 # include <limits.h>
62     # ifndef PAGESIZE
63     # define PAGESIZE pagesize
64     # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
65     static long pagesize;
66     # else
67 root 1.106 # define BOOT_PAGESIZE (void)0
68 root 1.78 # endif
69 root 1.106 #else
70     # define PAGESIZE 0
71     # define BOOT_PAGESIZE (void)0
72 root 1.3 #endif
73    
74 root 1.28 /* The next macro should declare a variable stacklevel that contains and approximation
75 root 1.23 * to the current C stack pointer. Its property is that it changes with each call
76 root 1.15 * and should be unique. */
77 root 1.92 #define dSTACKLEVEL int stacklevel
78     #define STACKLEVEL ((void *)&stacklevel)
79 root 1.15
80 root 1.34 #define IN_DESTRUCT (PL_main_cv == Nullcv)
81    
82 root 1.100 #if __GNUC__ >= 3
83     # define attribute(x) __attribute__(x)
84     #else
85     # define attribute(x)
86     #endif
87    
88     #define NOINLINE attribute ((noinline))
89    
90 root 1.23 #include "CoroAPI.h"
91    
92 root 1.92 #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
93    
94 pcg 1.55 #ifdef USE_ITHREADS
95     static perl_mutex coro_mutex;
96 root 1.69 # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
97 pcg 1.55 # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
98     #else
99 root 1.69 # define LOCK (void)0
100     # define UNLOCK (void)0
101 pcg 1.55 #endif
102    
103 root 1.23 static struct CoroAPI coroapi;
104 pcg 1.56 static AV *main_mainstack; /* used to differentiate between $main and others */
105 root 1.88 static HV *coro_state_stash, *coro_stash;
106 pcg 1.58 static SV *coro_mortal; /* will be freed after next transfer */
107 root 1.23
108 root 1.107 static struct coro_cctx *cctx_first;
109     static int cctx_count, cctx_idle;
110    
111 root 1.92 /* this is a structure representing a c-level coroutine */
112 root 1.106 typedef struct coro_cctx {
113     struct coro_cctx *next;
114 root 1.15
115 root 1.93 /* the stack */
116 root 1.15 void *sptr;
117     long ssize; /* positive == mmap, otherwise malloc */
118 root 1.89
119     /* cpu state */
120 root 1.96 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
121 root 1.93 JMPENV *top_env;
122 root 1.89 coro_context cctx;
123 root 1.104
124 root 1.111 int inuse;
125    
126 root 1.104 #if USE_VALGRIND
127     int valgrind_id;
128     #endif
129 root 1.106 } coro_cctx;
130 root 1.15
131 root 1.111 enum {
132     CF_RUNNING, /* coroutine is running */
133     CF_READY, /* coroutine is ready */
134     };
135    
136 root 1.92 /* this is a structure representing a perl-level coroutine */
137 root 1.1 struct coro {
138 root 1.92 /* the c coroutine allocated to this perl coroutine, if any */
139 root 1.106 coro_cctx *cctx;
140 root 1.92
141     /* data associated with this coroutine (initial args) */
142     AV *args;
143     int refcnt;
144 root 1.111 int flags;
145 root 1.92
146 root 1.7 /* optionally saved, might be zero */
147 root 1.3 AV *defav;
148 root 1.7 SV *defsv;
149     SV *errsv;
150 root 1.1
151 root 1.108 #define VAR(name,type) type name;
152     # include "state.h"
153     #undef VAR
154 root 1.1
155 root 1.87 /* coro process data */
156     int prio;
157 root 1.1 };
158    
159     typedef struct coro *Coro__State;
160     typedef struct coro *Coro__State_or_hashref;
161    
162 root 1.77 static AV *
163 root 1.98 coro_clone_padlist (CV *cv)
164 root 1.77 {
165     AV *padlist = CvPADLIST (cv);
166     AV *newpadlist, *newpad;
167 root 1.3
168     newpadlist = newAV ();
169     AvREAL_off (newpadlist);
170 root 1.100 #if PERL_VERSION_ATLEAST (5,9,0)
171     Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
172     #else
173 root 1.77 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
174 root 1.79 #endif
175 root 1.77 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
176     --AvFILLp (padlist);
177 root 1.3
178 root 1.77 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
179     av_store (newpadlist, 1, (SV *)newpad);
180 root 1.3
181     return newpadlist;
182     }
183    
184 root 1.77 static void
185 root 1.98 free_padlist (AV *padlist)
186 root 1.3 {
187     /* may be during global destruction */
188 pcg 1.50 if (SvREFCNT (padlist))
189 root 1.3 {
190 pcg 1.50 I32 i = AvFILLp (padlist);
191 root 1.3 while (i >= 0)
192     {
193 pcg 1.50 SV **svp = av_fetch (padlist, i--, FALSE);
194     if (svp)
195     {
196     SV *sv;
197     while (&PL_sv_undef != (sv = av_pop ((AV *)*svp)))
198     SvREFCNT_dec (sv);
199    
200     SvREFCNT_dec (*svp);
201     }
202 root 1.3 }
203    
204 pcg 1.50 SvREFCNT_dec ((SV*)padlist);
205     }
206     }
207    
208 root 1.77 static int
209 pcg 1.54 coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
210 pcg 1.50 {
211     AV *padlist;
212     AV *av = (AV *)mg->mg_obj;
213    
214     /* casting is fun. */
215     while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
216 root 1.98 free_padlist (padlist);
217 root 1.61
218     SvREFCNT_dec (av);
219 root 1.76
220     return 0;
221 root 1.3 }
222 pcg 1.50
223     #define PERL_MAGIC_coro PERL_MAGIC_ext
224    
225     static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
226 root 1.3
227 root 1.109 #define CORO_MAGIC(cv) \
228     SvMAGIC (cv) \
229     ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
230     ? SvMAGIC (cv) \
231     : mg_find ((SV *)cv, PERL_MAGIC_coro) \
232     : 0
233    
234 root 1.7 /* the next two functions merely cache the padlists */
235 root 1.77 static void
236 root 1.98 get_padlist (CV *cv)
237 root 1.3 {
238 root 1.109 MAGIC *mg = CORO_MAGIC (cv);
239     AV *av;
240 root 1.4
241 root 1.109 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
242     CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
243 root 1.4 else
244 root 1.77 {
245     #if 0
246 root 1.96 /* this is probably cleaner, but also slower? */
247 root 1.98 CV *cp = Perl_cv_clone (cv);
248 root 1.77 CvPADLIST (cv) = CvPADLIST (cp);
249     CvPADLIST (cp) = 0;
250     SvREFCNT_dec (cp);
251     #else
252 root 1.98 CvPADLIST (cv) = coro_clone_padlist (cv);
253 root 1.77 #endif
254     }
255 root 1.4 }
256    
257 root 1.77 static void
258 root 1.98 put_padlist (CV *cv)
259 root 1.4 {
260 root 1.109 MAGIC *mg = CORO_MAGIC (cv);
261     AV *av;
262 root 1.7
263 pcg 1.50 if (!mg)
264 root 1.7 {
265 pcg 1.50 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
266     mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
267     mg->mg_virtual = &vtbl_coro;
268     mg->mg_obj = (SV *)newAV ();
269 root 1.7 }
270    
271 root 1.109 av = (AV *)mg->mg_obj;
272    
273     if (AvFILLp (av) >= AvMAX (av))
274     av_extend (av, AvMAX (av) + 1);
275    
276     AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
277 root 1.7 }
278    
279     #define SB do {
280     #define SE } while (0)
281    
282 root 1.98 #define LOAD(state) load_state((state));
283     #define SAVE(state,flags) save_state((state),(flags));
284 root 1.7
285 pcg 1.47 #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
286 root 1.7
287     static void
288 root 1.98 load_state(Coro__State c)
289 root 1.7 {
290 root 1.108 #define VAR(name,type) PL_ ## name = c->name;
291     # include "state.h"
292     #undef VAR
293 root 1.7
294     if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
295     if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
296     if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
297    
298     {
299     dSP;
300     CV *cv;
301    
302     /* now do the ugly restore mess */
303     while ((cv = (CV *)POPs))
304     {
305 root 1.109 put_padlist (cv); /* mark this padlist as available */
306     CvDEPTH (cv) = PTR2IV (POPs);
307     CvPADLIST (cv) = (AV *)POPs;
308 root 1.7 }
309    
310     PUTBACK;
311     }
312     }
313    
314 root 1.3 static void
315 root 1.98 save_state(Coro__State c, int flags)
316 root 1.3 {
317     {
318     dSP;
319     I32 cxix = cxstack_ix;
320 root 1.11 PERL_CONTEXT *ccstk = cxstack;
321 root 1.3 PERL_SI *top_si = PL_curstackinfo;
322    
323     /*
324     * the worst thing you can imagine happens first - we have to save
325     * (and reinitialize) all cv's in the whole callchain :(
326     */
327    
328     PUSHs (Nullsv);
329     /* this loop was inspired by pp_caller */
330     for (;;)
331     {
332 root 1.30 while (cxix >= 0)
333 root 1.3 {
334 root 1.4 PERL_CONTEXT *cx = &ccstk[cxix--];
335 root 1.3
336     if (CxTYPE(cx) == CXt_SUB)
337     {
338     CV *cv = cx->blk_sub.cv;
339 root 1.109
340     if (CvDEPTH (cv))
341 root 1.3 {
342 root 1.109 EXTEND (SP, 3);
343 root 1.7
344 root 1.3 PUSHs ((SV *)CvPADLIST(cv));
345 root 1.109 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
346 root 1.3 PUSHs ((SV *)cv);
347    
348 root 1.109 CvDEPTH (cv) = 0;
349 root 1.98 get_padlist (cv);
350 root 1.3 }
351     }
352 pcg 1.46 #ifdef CXt_FORMAT
353 root 1.3 else if (CxTYPE(cx) == CXt_FORMAT)
354     {
355     /* I never used formats, so how should I know how these are implemented? */
356     /* my bold guess is as a simple, plain sub... */
357     croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
358     }
359 pcg 1.46 #endif
360 root 1.3 }
361    
362     if (top_si->si_type == PERLSI_MAIN)
363     break;
364    
365     top_si = top_si->si_prev;
366     ccstk = top_si->si_cxstack;
367     cxix = top_si->si_cxix;
368     }
369    
370     PUTBACK;
371     }
372    
373 root 1.12 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
374     c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
375     c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
376 root 1.7
377 root 1.108 #define VAR(name,type)c->name = PL_ ## name;
378     # include "state.h"
379     #undef VAR
380 root 1.13 }
381    
382     /*
383     * allocate various perl stacks. This is an exact copy
384     * of perl.c:init_stacks, except that it uses less memory
385 pcg 1.52 * on the (sometimes correct) assumption that coroutines do
386     * not usually need a lot of stackspace.
387 root 1.13 */
388 root 1.77 static void
389 root 1.98 coro_init_stacks ()
390 root 1.13 {
391     PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
392     PL_curstackinfo->si_type = PERLSI_MAIN;
393     PL_curstack = PL_curstackinfo->si_stack;
394     PL_mainstack = PL_curstack; /* remember in case we switch stacks */
395    
396     PL_stack_base = AvARRAY(PL_curstack);
397     PL_stack_sp = PL_stack_base;
398     PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
399    
400 root 1.15 New(50,PL_tmps_stack,96,SV*);
401 root 1.13 PL_tmps_floor = -1;
402     PL_tmps_ix = -1;
403 root 1.15 PL_tmps_max = 96;
404 root 1.13
405 root 1.15 New(54,PL_markstack,16,I32);
406 root 1.13 PL_markstack_ptr = PL_markstack;
407 root 1.15 PL_markstack_max = PL_markstack + 16;
408 root 1.13
409 pcg 1.46 #ifdef SET_MARK_OFFSET
410 root 1.13 SET_MARK_OFFSET;
411 pcg 1.46 #endif
412 root 1.13
413 root 1.15 New(54,PL_scopestack,16,I32);
414 root 1.13 PL_scopestack_ix = 0;
415 root 1.15 PL_scopestack_max = 16;
416 root 1.13
417 root 1.15 New(54,PL_savestack,96,ANY);
418 root 1.13 PL_savestack_ix = 0;
419 root 1.15 PL_savestack_max = 96;
420 root 1.13
421 root 1.100 #if !PERL_VERSION_ATLEAST (5,9,0)
422 root 1.13 New(54,PL_retstack,8,OP*);
423     PL_retstack_ix = 0;
424     PL_retstack_max = 8;
425 root 1.71 #endif
426 root 1.3 }
427 root 1.1
428 root 1.7 /*
429     * destroy the stacks, the callchain etc...
430     */
431 root 1.77 static void
432 root 1.106 coro_destroy_stacks ()
433 root 1.1 {
434 root 1.34 if (!IN_DESTRUCT)
435 root 1.31 {
436     /* is this ugly, I ask? */
437 pcg 1.52 LEAVE_SCOPE (0);
438 root 1.31
439     /* sure it is, but more important: is it correct?? :/ */
440 pcg 1.52 FREETMPS;
441 pcg 1.57
442     /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/
443 root 1.31 }
444 root 1.7
445 root 1.4 while (PL_curstackinfo->si_next)
446     PL_curstackinfo = PL_curstackinfo->si_next;
447    
448     while (PL_curstackinfo)
449     {
450     PERL_SI *p = PL_curstackinfo->si_prev;
451    
452 pcg 1.57 { /*D*//*remove*/
453 root 1.7 dSP;
454     SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
455     PUTBACK; /* possibly superfluous */
456     }
457    
458 root 1.34 if (!IN_DESTRUCT)
459 root 1.31 {
460 pcg 1.57 dounwind (-1);/*D*//*remove*/
461     SvREFCNT_dec (PL_curstackinfo->si_stack);
462 root 1.31 }
463 root 1.7
464 pcg 1.57 Safefree (PL_curstackinfo->si_cxstack);
465     Safefree (PL_curstackinfo);
466 root 1.4 PL_curstackinfo = p;
467     }
468    
469 pcg 1.57 Safefree (PL_tmps_stack);
470     Safefree (PL_markstack);
471     Safefree (PL_scopestack);
472     Safefree (PL_savestack);
473 root 1.100 #if !PERL_VERSION_ATLEAST (5,9,0)
474 pcg 1.57 Safefree (PL_retstack);
475 root 1.71 #endif
476 root 1.1 }
477    
478 root 1.13 static void
479 root 1.89 setup_coro (struct coro *coro)
480 root 1.13 {
481 root 1.89 /*
482     * emulate part of the perl startup here.
483     */
484 root 1.15
485 root 1.98 coro_init_stacks ();
486 root 1.15
487 root 1.103 PL_curcop = 0;
488     PL_in_eval = 0;
489     PL_curpm = 0;
490    
491 root 1.102 {
492     dSP;
493     LOGOP myop;
494    
495 root 1.103 /* I have no idea why this is needed, but it is */
496     PUSHMARK (SP);
497    
498 root 1.102 SvREFCNT_dec (GvAV (PL_defgv));
499     GvAV (PL_defgv) = coro->args; coro->args = 0;
500    
501     Zero (&myop, 1, LOGOP);
502     myop.op_next = Nullop;
503     myop.op_flags = OPf_WANT_VOID;
504 root 1.89
505 root 1.102 PL_op = (OP *)&myop;
506 root 1.89
507 root 1.102 PUSHMARK (SP);
508 root 1.107 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
509 root 1.102 PUTBACK;
510     PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
511     SPAGAIN;
512 root 1.15
513 root 1.102 ENTER; /* necessary e.g. for dounwind */
514     }
515 root 1.13 }
516    
517     static void
518 root 1.92 free_coro_mortal ()
519 root 1.13 {
520 root 1.89 if (coro_mortal)
521 root 1.15 {
522 root 1.89 SvREFCNT_dec (coro_mortal);
523     coro_mortal = 0;
524     }
525 root 1.13 }
526    
527 root 1.100 static void NOINLINE
528 root 1.106 prepare_cctx (coro_cctx *cctx)
529 root 1.99 {
530     dSP;
531 root 1.102 LOGOP myop;
532 root 1.99
533 root 1.102 Zero (&myop, 1, LOGOP);
534 root 1.99 myop.op_next = PL_op;
535 root 1.102 myop.op_flags = OPf_WANT_VOID;
536 root 1.99
537 root 1.107 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
538 root 1.102
539     PUSHMARK (SP);
540 root 1.107 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
541 root 1.99 PUTBACK;
542     PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
543     SPAGAIN;
544     }
545    
546     static void
547 root 1.89 coro_run (void *arg)
548 root 1.13 {
549 root 1.102 /* coro_run is the alternative epilogue of transfer() */
550     UNLOCK;
551    
552 root 1.13 /*
553 root 1.89 * this is a _very_ stripped down perl interpreter ;)
554 root 1.13 */
555 root 1.93 PL_top_env = &PL_start_env;
556 root 1.107
557 root 1.102 /* inject call to cctx_init */
558 root 1.106 prepare_cctx ((coro_cctx *)arg);
559 root 1.89
560 root 1.94 /* somebody will hit me for both perl_run and PL_restartop */
561 root 1.102 perl_run (PL_curinterp);
562 root 1.89
563 root 1.105 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
564 root 1.89 abort ();
565     }
566    
567 root 1.106 static coro_cctx *
568     cctx_new ()
569 root 1.89 {
570 root 1.106 coro_cctx *cctx;
571 root 1.89
572 root 1.107 ++cctx_count;
573    
574 root 1.106 New (0, cctx, 1, coro_cctx);
575 root 1.89
576     #if HAVE_MMAP
577 root 1.13
578 root 1.106 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
579 root 1.94 /* mmap suppsedly does allocate-on-write for us */
580 root 1.106 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
581 root 1.19
582 root 1.106 if (cctx->sptr == (void *)-1)
583 root 1.13 {
584 root 1.92 perror ("FATAL: unable to mmap stack for coroutine");
585 root 1.89 _exit (EXIT_FAILURE);
586     }
587 root 1.15
588 root 1.94 # if STACKGUARD
589 root 1.106 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
590 root 1.94 # endif
591 root 1.19
592 root 1.89 #else
593    
594 root 1.106 cctx->ssize = STACKSIZE * (long)sizeof (long);
595     New (0, cctx->sptr, STACKSIZE, long);
596 root 1.89
597 root 1.106 if (!cctx->sptr)
598 root 1.89 {
599 root 1.106 perror ("FATAL: unable to malloc stack for coroutine");
600 root 1.89 _exit (EXIT_FAILURE);
601 root 1.13 }
602    
603 root 1.89 #endif
604    
605 root 1.104 #if USE_VALGRIND
606 root 1.106 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
607     STACKGUARD * PAGESIZE + (char *)cctx->sptr,
608     cctx->ssize + (char *)cctx->sptr
609 root 1.104 );
610     #endif
611    
612 root 1.106 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
613 root 1.13
614 root 1.106 return cctx;
615 root 1.13 }
616    
617 root 1.15 static void
618 root 1.106 cctx_free (coro_cctx *cctx)
619 root 1.15 {
620 root 1.106 if (!cctx)
621 root 1.89 return;
622    
623 root 1.107 --cctx_count;
624    
625 root 1.104 #if USE_VALGRIND
626 root 1.106 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
627 root 1.104 #endif
628    
629 root 1.89 #if HAVE_MMAP
630 root 1.106 munmap (cctx->sptr, cctx->ssize);
631 root 1.89 #else
632 root 1.106 Safefree (cctx->sptr);
633 root 1.89 #endif
634    
635 root 1.106 Safefree (cctx);
636 root 1.89 }
637 root 1.32
638 root 1.106 static coro_cctx *
639     cctx_get ()
640 root 1.89 {
641 root 1.106 coro_cctx *cctx;
642 root 1.91
643 root 1.106 if (cctx_first)
644 root 1.89 {
645 root 1.102 --cctx_idle;
646 root 1.106 cctx = cctx_first;
647     cctx_first = cctx->next;
648 root 1.89 }
649     else
650 root 1.91 {
651 root 1.106 cctx = cctx_new ();
652 root 1.91 PL_op = PL_op->op_next;
653     }
654    
655 root 1.106 return cctx;
656 root 1.89 }
657 root 1.19
658 root 1.89 static void
659 root 1.106 cctx_put (coro_cctx *cctx)
660 root 1.89 {
661 root 1.102 ++cctx_idle;
662 root 1.106 cctx->next = cctx_first;
663     cctx_first = cctx;
664 root 1.15 }
665    
666 root 1.84 /* never call directly, always through the coro_state_transfer global variable */
667 root 1.100 static void NOINLINE
668     transfer (struct coro *prev, struct coro *next, int flags)
669 root 1.8 {
670 root 1.15 dSTACKLEVEL;
671 root 1.8
672 root 1.89 /* sometimes transfer is only called to set idle_sp */
673 root 1.92 if (flags == TRANSFER_SET_STACKLEVEL)
674 root 1.106 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
675 root 1.92 else if (prev != next)
676 root 1.8 {
677 root 1.106 coro_cctx *prev__cctx;
678 root 1.89
679 root 1.111 if (!prev->cctx)
680     {
681     /* create a new empty context */
682     Newz (0, prev->cctx, 1, coro_cctx);
683     prev->cctx->inuse = 1;
684     prev->flags |= CF_RUNNING;
685     }
686    
687     assert ( prev->flags & CF_RUNNING);
688     assert (!next->flags & CF_RUNNING);
689    
690     prev->flags &= ~CF_RUNNING;
691     next->flags |= CF_RUNNING;
692    
693 root 1.92 LOCK;
694 root 1.13
695 root 1.90 if (next->mainstack)
696 root 1.89 {
697 root 1.90 /* coroutine already started */
698     SAVE (prev, flags);
699     LOAD (next);
700     }
701     else
702     {
703     /* need to start coroutine */
704 root 1.89 /* first get rid of the old state */
705     SAVE (prev, -1);
706     /* setup coroutine call */
707     setup_coro (next);
708 root 1.92 /* need a stack */
709 root 1.111 assert (!next->stack);
710 root 1.106 next->cctx = 0;
711 root 1.8 }
712 root 1.15
713 root 1.106 prev__cctx = prev->cctx;
714 root 1.92
715 root 1.106 /* possibly "free" the cctx */
716     if (prev__cctx->idle_sp == STACKLEVEL)
717 root 1.92 {
718 root 1.110 assert (PL_top_env == prev__cctx->top_env);//D
719 root 1.106 cctx_put (prev__cctx);
720     prev->cctx = 0;
721 root 1.92 }
722    
723 root 1.106 if (!next->cctx)
724     next->cctx = cctx_get ();
725 root 1.81
726 root 1.106 if (prev__cctx != next->cctx)
727 root 1.92 {
728 root 1.111 assert ( prev__cctx->inuse);
729     assert (!next->cctx->inuse);
730    
731     prev__cctx->inuse = 0;
732     next->cctx->inuse = 1;
733    
734 root 1.106 prev__cctx->top_env = PL_top_env;
735     PL_top_env = next->cctx->top_env;
736     coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
737 root 1.92 }
738    
739     free_coro_mortal ();
740    
741     UNLOCK;
742 root 1.8 }
743 root 1.39 }
744 root 1.23
745 root 1.92 struct transfer_args
746     {
747     struct coro *prev, *next;
748     int flags;
749     };
750    
751 root 1.100 #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
752 root 1.92
753 root 1.87 static void
754     coro_state_destroy (struct coro *coro)
755     {
756     if (coro->refcnt--)
757     return;
758    
759     if (coro->mainstack && coro->mainstack != main_mainstack)
760     {
761     struct coro temp;
762    
763 root 1.98 SAVE ((&temp), TRANSFER_SAVE_ALL);
764     LOAD (coro);
765 root 1.87
766 root 1.102 coro_destroy_stacks ();
767 root 1.87
768     LOAD ((&temp)); /* this will get rid of defsv etc.. */
769    
770     coro->mainstack = 0;
771     }
772    
773 root 1.106 cctx_free (coro->cctx);
774 root 1.87 SvREFCNT_dec (coro->args);
775     Safefree (coro);
776     }
777    
778     static int
779 root 1.98 coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
780 root 1.87 {
781     struct coro *coro = (struct coro *)mg->mg_ptr;
782     mg->mg_ptr = 0;
783    
784     coro_state_destroy (coro);
785    
786     return 0;
787     }
788    
789     static int
790 root 1.98 coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
791 root 1.87 {
792     struct coro *coro = (struct coro *)mg->mg_ptr;
793    
794     ++coro->refcnt;
795    
796     return 0;
797     }
798    
799 root 1.100 static MGVTBL coro_state_vtbl = {
800     0, 0, 0, 0,
801     coro_state_clear,
802     0,
803     #ifdef MGf_DUP
804     coro_state_dup,
805 root 1.102 #else
806     # define MGf_DUP 0
807 root 1.100 #endif
808     };
809 root 1.87
810     static struct coro *
811     SvSTATE (SV *coro)
812     {
813 root 1.88 HV *stash;
814     MAGIC *mg;
815    
816     if (SvROK (coro))
817     coro = SvRV (coro);
818    
819     stash = SvSTASH (coro);
820     if (stash != coro_stash && stash != coro_state_stash)
821     {
822     /* very slow, but rare, check */
823     if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
824     croak ("Coro::State object required");
825     }
826    
827     mg = SvMAGIC (coro);
828 root 1.87 assert (mg->mg_type == PERL_MAGIC_ext);
829     return (struct coro *)mg->mg_ptr;
830     }
831 root 1.23
832     static void
833 root 1.98 prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
834 root 1.23 {
835 root 1.92 ta->prev = SvSTATE (prev);
836     ta->next = SvSTATE (next);
837     ta->flags = flags;
838     }
839    
840     static void
841     api_transfer (SV *prev, SV *next, int flags)
842     {
843     dTHX;
844     struct transfer_args ta;
845    
846 root 1.98 prepare_transfer (&ta, prev, next, flags);
847 root 1.92 TRANSFER (ta);
848 root 1.21 }
849    
850 root 1.22 /** Coro ********************************************************************/
851    
852     #define PRIO_MAX 3
853     #define PRIO_HIGH 1
854     #define PRIO_NORMAL 0
855     #define PRIO_LOW -1
856     #define PRIO_IDLE -3
857     #define PRIO_MIN -4
858    
859     /* for Coro.pm */
860     static GV *coro_current, *coro_idle;
861 root 1.83 static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
862 root 1.25 static int coro_nready;
863 root 1.22
864     static void
865 root 1.98 coro_enq (SV *sv)
866 root 1.22 {
867 root 1.74 int prio;
868    
869 root 1.73 if (SvTYPE (sv) != SVt_PVHV)
870     croak ("Coro::ready tried to enqueue something that is not a coroutine");
871 root 1.22
872 root 1.88 prio = SvSTATE (sv)->prio;
873 root 1.22
874 root 1.73 av_push (coro_ready [prio - PRIO_MIN], sv);
875     coro_nready++;
876 root 1.22 }
877    
878     static SV *
879 root 1.98 coro_deq (int min_prio)
880 root 1.22 {
881     int prio = PRIO_MAX - PRIO_MIN;
882    
883     min_prio -= PRIO_MIN;
884     if (min_prio < 0)
885     min_prio = 0;
886    
887     for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
888 root 1.83 if (AvFILLp (coro_ready [prio]) >= 0)
889 root 1.25 {
890     coro_nready--;
891 root 1.83 return av_shift (coro_ready [prio]);
892 root 1.25 }
893 root 1.22
894     return 0;
895     }
896    
897 root 1.111 static int
898     api_ready (SV *coro_sv)
899 root 1.23 {
900 root 1.111 struct coro *coro;
901    
902     if (SvROK (coro_sv))
903     coro_sv = SvRV (coro_sv);
904    
905     coro = SvSTATE (coro_sv);
906     if (coro->flags & CF_READY)
907     return 0;
908 pcg 1.56
909 root 1.111 coro->flags |= CF_READY;
910 root 1.39
911 pcg 1.55 LOCK;
912 root 1.111 coro_enq (SvREFCNT_inc (coro_sv));
913 pcg 1.55 UNLOCK;
914 root 1.111
915     return 1;
916     }
917    
918     static int
919     api_is_ready (SV *coro_sv)
920     {
921     struct coro *coro;
922    
923     return !!(SvSTATE (coro_sv)->flags & CF_READY);
924 root 1.23 }
925    
926     static void
927 root 1.98 prepare_schedule (struct transfer_args *ta)
928 root 1.23 {
929 root 1.92 SV *current, *prev, *next;
930    
931     current = GvSV (coro_current);
932 root 1.88
933     for (;;)
934     {
935     LOCK;
936 root 1.98 next = coro_deq (PRIO_MIN);
937 root 1.99 UNLOCK;
938 root 1.88
939     if (next)
940     break;
941 root 1.23
942 root 1.88 {
943     dSP;
944    
945     ENTER;
946     SAVETMPS;
947 pcg 1.55
948 root 1.88 PUSHMARK (SP);
949     PUTBACK;
950     call_sv (GvSV (coro_idle), G_DISCARD);
951 root 1.23
952 root 1.88 FREETMPS;
953     LEAVE;
954     }
955     }
956    
957     prev = SvRV (current);
958     SvRV (current) = next;
959 root 1.23
960 root 1.40 /* free this only after the transfer */
961 root 1.99 LOCK;
962 root 1.92 free_coro_mortal ();
963 root 1.99 UNLOCK;
964 root 1.39 coro_mortal = prev;
965 root 1.23
966 root 1.92 ta->prev = SvSTATE (prev);
967     ta->next = SvSTATE (next);
968     ta->flags = TRANSFER_SAVE_ALL;
969 root 1.111
970     ta->next->flags &= ~CF_READY;
971 root 1.92 }
972    
973     static void
974 root 1.98 prepare_cede (struct transfer_args *ta)
975 root 1.92 {
976 root 1.111 api_ready (GvSV (coro_current));
977 pcg 1.55
978 root 1.92 prepare_schedule (ta);
979 root 1.39 }
980    
981 root 1.92 static void
982     api_schedule (void)
983     {
984     dTHX;
985     struct transfer_args ta;
986    
987     prepare_schedule (&ta);
988     TRANSFER (ta);
989     }
990 root 1.89
991 root 1.39 static void
992     api_cede (void)
993     {
994 pcg 1.56 dTHX;
995 root 1.92 struct transfer_args ta;
996 root 1.89
997 root 1.92 prepare_cede (&ta);
998     TRANSFER (ta);
999 root 1.23 }
1000    
1001 root 1.3 MODULE = Coro::State PACKAGE = Coro::State
1002 root 1.1
1003 root 1.87 PROTOTYPES: DISABLE
1004 root 1.1
1005 root 1.3 BOOT:
1006 root 1.88 {
1007 pcg 1.55 #ifdef USE_ITHREADS
1008     MUTEX_INIT (&coro_mutex);
1009     #endif
1010 root 1.78 BOOT_PAGESIZE;
1011 pcg 1.55
1012 root 1.14 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1013 root 1.7
1014 root 1.13 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
1015     newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
1016     newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
1017 root 1.7
1018 root 1.12 main_mainstack = PL_mainstack;
1019 root 1.23
1020 root 1.26 coroapi.ver = CORO_API_VERSION;
1021     coroapi.transfer = api_transfer;
1022 root 1.87
1023     assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1024 root 1.9 }
1025 root 1.3
1026 root 1.87 SV *
1027 root 1.86 new (char *klass, ...)
1028 root 1.1 CODE:
1029 root 1.86 {
1030 root 1.87 struct coro *coro;
1031     HV *hv;
1032 root 1.86 int i;
1033    
1034 pcg 1.47 Newz (0, coro, 1, struct coro);
1035 root 1.86 coro->args = newAV ();
1036    
1037 root 1.89 hv = newHV ();
1038     sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1039     RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1040    
1041 root 1.86 for (i = 1; i < items; i++)
1042     av_push (coro->args, newSVsv (ST (i)));
1043     }
1044 root 1.1 OUTPUT:
1045     RETVAL
1046    
1047     void
1048 root 1.94 _set_stacklevel (...)
1049 root 1.92 ALIAS:
1050 root 1.94 Coro::State::transfer = 1
1051     Coro::schedule = 2
1052     Coro::cede = 3
1053     Coro::Cont::yield = 4
1054 root 1.1 CODE:
1055 root 1.87 {
1056 root 1.92 struct transfer_args ta;
1057 root 1.1
1058 root 1.92 switch (ix)
1059 root 1.1 {
1060 root 1.92 case 0:
1061 root 1.106 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1062 root 1.94 ta.next = 0;
1063     ta.flags = TRANSFER_SET_STACKLEVEL;
1064     break;
1065    
1066     case 1:
1067 root 1.92 if (items != 3)
1068     croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items);
1069    
1070     prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1071     break;
1072    
1073 root 1.94 case 2:
1074 root 1.92 prepare_schedule (&ta);
1075     break;
1076    
1077 root 1.94 case 3:
1078 root 1.92 prepare_cede (&ta);
1079     break;
1080    
1081 root 1.94 case 4:
1082     {
1083     SV *yieldstack;
1084     SV *sv;
1085     AV *defav = GvAV (PL_defgv);
1086    
1087     yieldstack = *hv_fetch (
1088     (HV *)SvRV (GvSV (coro_current)),
1089     "yieldstack", sizeof ("yieldstack") - 1,
1090     0
1091     );
1092    
1093     /* set up @_ -- ugly */
1094     av_clear (defav);
1095     av_fill (defav, items - 1);
1096     while (items--)
1097     av_store (defav, items, SvREFCNT_inc (ST(items)));
1098    
1099     sv = av_pop ((AV *)SvRV (yieldstack));
1100     ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1101     ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1102     ta.flags = 0;
1103     SvREFCNT_dec (sv);
1104     }
1105     break;
1106    
1107 root 1.92 }
1108 root 1.1
1109 root 1.92 TRANSFER (ta);
1110 root 1.87 }
1111 root 1.1
1112 root 1.87 void
1113     _clone_state_from (SV *dst, SV *src)
1114     CODE:
1115     {
1116     struct coro *coro_src = SvSTATE (src);
1117 root 1.13
1118 root 1.87 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1119 root 1.13
1120 root 1.87 ++coro_src->refcnt;
1121     sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1122     }
1123 root 1.12
1124 root 1.7 void
1125 root 1.87 _exit (code)
1126 root 1.20 int code
1127     PROTOTYPE: $
1128     CODE:
1129     _exit (code);
1130 root 1.1
1131 root 1.106 int
1132     cctx_count ()
1133     CODE:
1134     RETVAL = cctx_count;
1135     OUTPUT:
1136     RETVAL
1137    
1138     int
1139     cctx_idle ()
1140     CODE:
1141     RETVAL = cctx_idle;
1142     OUTPUT:
1143     RETVAL
1144    
1145 root 1.21 MODULE = Coro::State PACKAGE = Coro
1146    
1147     BOOT:
1148     {
1149 root 1.22 int i;
1150    
1151 root 1.88 coro_stash = gv_stashpv ("Coro", TRUE);
1152    
1153     newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1154     newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1155     newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1156     newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1157     newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1158     newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1159 root 1.22
1160 root 1.21 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
1161     coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1162 root 1.22
1163     for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1164     coro_ready[i] = newAV ();
1165 root 1.26
1166     {
1167     SV *sv = perl_get_sv("Coro::API", 1);
1168    
1169     coroapi.schedule = api_schedule;
1170 root 1.39 coroapi.cede = api_cede;
1171 root 1.26 coroapi.ready = api_ready;
1172 root 1.111 coroapi.is_ready = api_is_ready;
1173 root 1.26 coroapi.nready = &coro_nready;
1174     coroapi.current = coro_current;
1175    
1176     GCoroAPI = &coroapi;
1177 root 1.81 sv_setiv (sv, (IV)&coroapi);
1178     SvREADONLY_on (sv);
1179 root 1.26 }
1180 root 1.21 }
1181    
1182 root 1.92 int
1183     prio (Coro::State coro, int newprio = 0)
1184     ALIAS:
1185     nice = 1
1186     CODE:
1187     {
1188     RETVAL = coro->prio;
1189    
1190     if (items > 1)
1191     {
1192     if (ix)
1193     newprio += coro->prio;
1194    
1195     if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1196     if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1197    
1198     coro->prio = newprio;
1199     }
1200     }
1201    
1202 root 1.111 SV *
1203 root 1.89 ready (SV *self)
1204 root 1.35 PROTOTYPE: $
1205 root 1.21 CODE:
1206 root 1.111 RETVAL = boolSV (api_ready (self));
1207     OUTPUT:
1208     RETVAL
1209    
1210     SV *
1211     is_ready (SV *self)
1212     PROTOTYPE: $
1213     CODE:
1214     RETVAL = boolSV (api_is_ready (self));
1215     OUTPUT:
1216     RETVAL
1217 pcg 1.50
1218 root 1.25 int
1219 root 1.87 nready (...)
1220 root 1.25 PROTOTYPE:
1221     CODE:
1222     RETVAL = coro_nready;
1223     OUTPUT:
1224     RETVAL
1225    
1226 root 1.89 MODULE = Coro::State PACKAGE = Coro::AIO
1227    
1228 root 1.70 SV *
1229 root 1.89 _get_state ()
1230 root 1.70 CODE:
1231     {
1232     struct {
1233     int errorno;
1234     int laststype;
1235     int laststatval;
1236     Stat_t statcache;
1237     } data;
1238    
1239     data.errorno = errno;
1240     data.laststype = PL_laststype;
1241     data.laststatval = PL_laststatval;
1242     data.statcache = PL_statcache;
1243    
1244     RETVAL = newSVpvn ((char *)&data, sizeof data);
1245     }
1246     OUTPUT:
1247     RETVAL
1248    
1249     void
1250 root 1.89 _set_state (char *data_)
1251 root 1.70 PROTOTYPE: $
1252     CODE:
1253     {
1254     struct {
1255     int errorno;
1256     int laststype;
1257     int laststatval;
1258     Stat_t statcache;
1259     } *data = (void *)data_;
1260    
1261     errno = data->errorno;
1262     PL_laststype = data->laststype;
1263     PL_laststatval = data->laststatval;
1264     PL_statcache = data->statcache;
1265     }