ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.29 by root, Sat Aug 25 21:40:34 2001 UTC vs.
Revision 1.104 by root, Mon Nov 27 01:51:02 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines