… | |
… | |
7 | #include "patchlevel.h" |
7 | #include "patchlevel.h" |
8 | |
8 | |
9 | #if USE_VALGRIND |
9 | #if USE_VALGRIND |
10 | # include <valgrind/valgrind.h> |
10 | # include <valgrind/valgrind.h> |
11 | #endif |
11 | #endif |
|
|
12 | |
|
|
13 | /* the maximum number of idle cctx that will be pooled */ |
|
|
14 | #define MAX_IDLE_CCTX 8 |
12 | |
15 | |
13 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
16 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
14 | (PERL_REVISION > (a) \ |
17 | (PERL_REVISION > (a) \ |
15 | || (PERL_REVISION == (a) \ |
18 | || (PERL_REVISION == (a) \ |
16 | && (PERL_VERSION > (b) \ |
19 | && (PERL_VERSION > (b) \ |
… | |
… | |
35 | # ifndef IS_PADCONST |
38 | # ifndef IS_PADCONST |
36 | # define IS_PADCONST(v) 0 |
39 | # define IS_PADCONST(v) 0 |
37 | # endif |
40 | # endif |
38 | #endif |
41 | #endif |
39 | |
42 | |
|
|
43 | /* 5.8.7 */ |
|
|
44 | #ifndef SvRV_set |
|
|
45 | # define SvRV_set(s,v) SvRV(s) = (v) |
|
|
46 | #endif |
|
|
47 | |
40 | #include <stdio.h> |
48 | #include <stdio.h> |
41 | #include <errno.h> |
49 | #include <errno.h> |
|
|
50 | #include <assert.h> |
42 | |
51 | |
43 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
52 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
44 | # undef STACKGUARD |
53 | # undef STACKGUARD |
45 | #endif |
54 | #endif |
46 | |
55 | |
… | |
… | |
87 | |
96 | |
88 | #define NOINLINE attribute ((noinline)) |
97 | #define NOINLINE attribute ((noinline)) |
89 | |
98 | |
90 | #include "CoroAPI.h" |
99 | #include "CoroAPI.h" |
91 | |
100 | |
92 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
|
|
93 | |
|
|
94 | #ifdef USE_ITHREADS |
101 | #ifdef USE_ITHREADS |
95 | static perl_mutex coro_mutex; |
102 | static perl_mutex coro_mutex; |
96 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
103 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
97 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
104 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
98 | #else |
105 | #else |
99 | # define LOCK (void)0 |
106 | # define LOCK (void)0 |
100 | # define UNLOCK (void)0 |
107 | # define UNLOCK (void)0 |
101 | #endif |
108 | #endif |
102 | |
109 | |
|
|
110 | struct io_state |
|
|
111 | { |
|
|
112 | int errorno; |
|
|
113 | I32 laststype; |
|
|
114 | int laststatval; |
|
|
115 | Stat_t statcache; |
|
|
116 | }; |
|
|
117 | |
103 | static struct CoroAPI coroapi; |
118 | static struct CoroAPI coroapi; |
104 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
119 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
105 | static HV *coro_state_stash, *coro_stash; |
120 | static HV *coro_state_stash, *coro_stash; |
106 | static SV *coro_mortal; /* will be freed after next transfer */ |
121 | static SV *coro_mortal; /* will be freed after next transfer */ |
107 | |
122 | |
… | |
… | |
119 | /* cpu state */ |
134 | /* cpu state */ |
120 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
135 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
121 | JMPENV *top_env; |
136 | JMPENV *top_env; |
122 | coro_context cctx; |
137 | coro_context cctx; |
123 | |
138 | |
|
|
139 | int inuse; |
|
|
140 | |
124 | #if USE_VALGRIND |
141 | #if USE_VALGRIND |
125 | int valgrind_id; |
142 | int valgrind_id; |
126 | #endif |
143 | #endif |
127 | } coro_cctx; |
144 | } coro_cctx; |
|
|
145 | |
|
|
146 | enum { |
|
|
147 | CF_RUNNING = 0x0001, /* coroutine is running */ |
|
|
148 | CF_READY = 0x0002, /* coroutine is ready */ |
|
|
149 | CF_NEW = 0x0004, /* ahs never been switched to */ |
|
|
150 | }; |
128 | |
151 | |
129 | /* this is a structure representing a perl-level coroutine */ |
152 | /* this is a structure representing a perl-level coroutine */ |
130 | struct coro { |
153 | struct coro { |
131 | /* the c coroutine allocated to this perl coroutine, if any */ |
154 | /* the c coroutine allocated to this perl coroutine, if any */ |
132 | coro_cctx *cctx; |
155 | coro_cctx *cctx; |
133 | |
156 | |
134 | /* data associated with this coroutine (initial args) */ |
157 | /* data associated with this coroutine (initial args) */ |
135 | AV *args; |
158 | AV *args; |
136 | int refcnt; |
159 | int refcnt; |
|
|
160 | int save; /* CORO_SAVE flags */ |
|
|
161 | int flags; /* CF_ flags */ |
137 | |
162 | |
138 | /* optionally saved, might be zero */ |
163 | /* optionally saved, might be zero */ |
139 | AV *defav; |
164 | AV *defav; /* @_ */ |
140 | SV *defsv; |
165 | SV *defsv; /* $_ */ |
141 | SV *errsv; |
166 | SV *errsv; /* $@ */ |
|
|
167 | SV *irssv; /* $/ */ |
|
|
168 | SV *irssv_sv; /* real $/ cache */ |
142 | |
169 | |
143 | /* saved global state not related to stacks */ |
170 | #define VAR(name,type) type name; |
144 | U8 dowarn; |
171 | # include "state.h" |
145 | I32 in_eval; |
172 | #undef VAR |
146 | |
|
|
147 | /* the stacks and related info (callchain etc..) */ |
|
|
148 | PERL_SI *curstackinfo; |
|
|
149 | AV *curstack; |
|
|
150 | AV *mainstack; |
|
|
151 | SV **stack_sp; |
|
|
152 | OP *op; |
|
|
153 | SV **curpad; |
|
|
154 | AV *comppad; |
|
|
155 | CV *compcv; |
|
|
156 | SV **stack_base; |
|
|
157 | SV **stack_max; |
|
|
158 | SV **tmps_stack; |
|
|
159 | I32 tmps_floor; |
|
|
160 | I32 tmps_ix; |
|
|
161 | I32 tmps_max; |
|
|
162 | I32 *markstack; |
|
|
163 | I32 *markstack_ptr; |
|
|
164 | I32 *markstack_max; |
|
|
165 | I32 *scopestack; |
|
|
166 | I32 scopestack_ix; |
|
|
167 | I32 scopestack_max; |
|
|
168 | ANY *savestack; |
|
|
169 | I32 savestack_ix; |
|
|
170 | I32 savestack_max; |
|
|
171 | OP **retstack; |
|
|
172 | I32 retstack_ix; |
|
|
173 | I32 retstack_max; |
|
|
174 | PMOP *curpm; |
|
|
175 | COP *curcop; |
|
|
176 | |
173 | |
177 | /* coro process data */ |
174 | /* coro process data */ |
178 | int prio; |
175 | int prio; |
179 | }; |
176 | }; |
180 | |
177 | |
… | |
… | |
244 | |
241 | |
245 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
242 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
246 | |
243 | |
247 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
244 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
248 | |
245 | |
|
|
246 | #define CORO_MAGIC(cv) \ |
|
|
247 | SvMAGIC (cv) \ |
|
|
248 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
|
|
249 | ? SvMAGIC (cv) \ |
|
|
250 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
|
|
251 | : 0 |
|
|
252 | |
249 | /* the next two functions merely cache the padlists */ |
253 | /* the next two functions merely cache the padlists */ |
250 | static void |
254 | static void |
251 | get_padlist (CV *cv) |
255 | get_padlist (CV *cv) |
252 | { |
256 | { |
253 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
257 | MAGIC *mg = CORO_MAGIC (cv); |
|
|
258 | AV *av; |
254 | |
259 | |
255 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
260 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
256 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
261 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
257 | else |
262 | else |
258 | { |
263 | { |
259 | #if 0 |
264 | #if 0 |
260 | /* this is probably cleaner, but also slower? */ |
265 | /* this is probably cleaner, but also slower? */ |
261 | CV *cp = Perl_cv_clone (cv); |
266 | CV *cp = Perl_cv_clone (cv); |
… | |
… | |
269 | } |
274 | } |
270 | |
275 | |
271 | static void |
276 | static void |
272 | put_padlist (CV *cv) |
277 | put_padlist (CV *cv) |
273 | { |
278 | { |
274 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
279 | MAGIC *mg = CORO_MAGIC (cv); |
|
|
280 | AV *av; |
275 | |
281 | |
276 | if (!mg) |
282 | if (!mg) |
277 | { |
283 | { |
278 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
284 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
279 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
285 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
280 | mg->mg_virtual = &vtbl_coro; |
286 | mg->mg_virtual = &vtbl_coro; |
281 | mg->mg_obj = (SV *)newAV (); |
287 | mg->mg_obj = (SV *)newAV (); |
282 | } |
288 | } |
283 | |
289 | |
284 | av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); |
290 | av = (AV *)mg->mg_obj; |
|
|
291 | |
|
|
292 | if (AvFILLp (av) >= AvMAX (av)) |
|
|
293 | av_extend (av, AvMAX (av) + 1); |
|
|
294 | |
|
|
295 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
285 | } |
296 | } |
286 | |
297 | |
287 | #define SB do { |
298 | #define SB do { |
288 | #define SE } while (0) |
299 | #define SE } while (0) |
289 | |
300 | |
290 | #define LOAD(state) load_state((state)); |
|
|
291 | #define SAVE(state,flags) save_state((state),(flags)); |
|
|
292 | |
|
|
293 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
301 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
294 | |
302 | |
295 | static void |
303 | static void |
296 | load_state(Coro__State c) |
304 | load_perl (Coro__State c) |
297 | { |
305 | { |
298 | PL_dowarn = c->dowarn; |
306 | #define VAR(name,type) PL_ ## name = c->name; |
299 | PL_in_eval = c->in_eval; |
307 | # include "state.h" |
300 | |
308 | #undef VAR |
301 | PL_curstackinfo = c->curstackinfo; |
|
|
302 | PL_curstack = c->curstack; |
|
|
303 | PL_mainstack = c->mainstack; |
|
|
304 | PL_stack_sp = c->stack_sp; |
|
|
305 | PL_op = c->op; |
|
|
306 | PL_curpad = c->curpad; |
|
|
307 | PL_comppad = c->comppad; |
|
|
308 | PL_compcv = c->compcv; |
|
|
309 | PL_stack_base = c->stack_base; |
|
|
310 | PL_stack_max = c->stack_max; |
|
|
311 | PL_tmps_stack = c->tmps_stack; |
|
|
312 | PL_tmps_floor = c->tmps_floor; |
|
|
313 | PL_tmps_ix = c->tmps_ix; |
|
|
314 | PL_tmps_max = c->tmps_max; |
|
|
315 | PL_markstack = c->markstack; |
|
|
316 | PL_markstack_ptr = c->markstack_ptr; |
|
|
317 | PL_markstack_max = c->markstack_max; |
|
|
318 | PL_scopestack = c->scopestack; |
|
|
319 | PL_scopestack_ix = c->scopestack_ix; |
|
|
320 | PL_scopestack_max = c->scopestack_max; |
|
|
321 | PL_savestack = c->savestack; |
|
|
322 | PL_savestack_ix = c->savestack_ix; |
|
|
323 | PL_savestack_max = c->savestack_max; |
|
|
324 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
325 | PL_retstack = c->retstack; |
|
|
326 | PL_retstack_ix = c->retstack_ix; |
|
|
327 | PL_retstack_max = c->retstack_max; |
|
|
328 | #endif |
|
|
329 | PL_curpm = c->curpm; |
|
|
330 | PL_curcop = c->curcop; |
|
|
331 | |
309 | |
332 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
310 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
333 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
311 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
334 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
312 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
|
|
313 | if (c->irssv) |
|
|
314 | { |
|
|
315 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
|
|
316 | SvREFCNT_dec (c->irssv); |
|
|
317 | else |
|
|
318 | { |
|
|
319 | REPLACE_SV (PL_rs, c->irssv); |
|
|
320 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
|
|
321 | sv_setsv (c->irssv_sv, PL_rs); |
|
|
322 | } |
|
|
323 | } |
335 | |
324 | |
336 | { |
325 | { |
337 | dSP; |
326 | dSP; |
338 | CV *cv; |
327 | CV *cv; |
339 | |
328 | |
340 | /* now do the ugly restore mess */ |
329 | /* now do the ugly restore mess */ |
341 | while ((cv = (CV *)POPs)) |
330 | while ((cv = (CV *)POPs)) |
342 | { |
331 | { |
343 | AV *padlist = (AV *)POPs; |
|
|
344 | |
|
|
345 | if (padlist) |
|
|
346 | { |
|
|
347 | put_padlist (cv); /* mark this padlist as available */ |
332 | put_padlist (cv); /* mark this padlist as available */ |
348 | CvPADLIST(cv) = padlist; |
333 | CvDEPTH (cv) = PTR2IV (POPs); |
349 | } |
334 | CvPADLIST (cv) = (AV *)POPs; |
350 | |
|
|
351 | ++CvDEPTH(cv); |
|
|
352 | } |
335 | } |
353 | |
336 | |
354 | PUTBACK; |
337 | PUTBACK; |
355 | } |
338 | } |
356 | } |
339 | } |
357 | |
340 | |
358 | static void |
341 | static void |
359 | save_state(Coro__State c, int flags) |
342 | save_perl (Coro__State c) |
360 | { |
343 | { |
361 | { |
344 | { |
362 | dSP; |
345 | dSP; |
363 | I32 cxix = cxstack_ix; |
346 | I32 cxix = cxstack_ix; |
364 | PERL_CONTEXT *ccstk = cxstack; |
347 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
375 | { |
358 | { |
376 | while (cxix >= 0) |
359 | while (cxix >= 0) |
377 | { |
360 | { |
378 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
361 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
379 | |
362 | |
380 | if (CxTYPE(cx) == CXt_SUB) |
363 | if (CxTYPE (cx) == CXt_SUB) |
381 | { |
364 | { |
382 | CV *cv = cx->blk_sub.cv; |
365 | CV *cv = cx->blk_sub.cv; |
|
|
366 | |
383 | if (CvDEPTH(cv)) |
367 | if (CvDEPTH (cv)) |
384 | { |
368 | { |
385 | EXTEND (SP, CvDEPTH(cv)*2); |
369 | EXTEND (SP, 3); |
386 | |
370 | |
387 | while (--CvDEPTH(cv)) |
|
|
388 | { |
|
|
389 | /* this tells the restore code to increment CvDEPTH */ |
|
|
390 | PUSHs (Nullsv); |
|
|
391 | PUSHs ((SV *)cv); |
|
|
392 | } |
|
|
393 | |
|
|
394 | PUSHs ((SV *)CvPADLIST(cv)); |
371 | PUSHs ((SV *)CvPADLIST (cv)); |
|
|
372 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
395 | PUSHs ((SV *)cv); |
373 | PUSHs ((SV *)cv); |
396 | |
374 | |
|
|
375 | CvDEPTH (cv) = 0; |
397 | get_padlist (cv); |
376 | get_padlist (cv); |
398 | } |
377 | } |
399 | } |
378 | } |
400 | #ifdef CXt_FORMAT |
379 | #ifdef CXt_FORMAT |
401 | else if (CxTYPE(cx) == CXt_FORMAT) |
380 | else if (CxTYPE (cx) == CXt_FORMAT) |
402 | { |
381 | { |
403 | /* I never used formats, so how should I know how these are implemented? */ |
382 | /* I never used formats, so how should I know how these are implemented? */ |
404 | /* my bold guess is as a simple, plain sub... */ |
383 | /* my bold guess is as a simple, plain sub... */ |
405 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
384 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
406 | } |
385 | } |
… | |
… | |
416 | } |
395 | } |
417 | |
396 | |
418 | PUTBACK; |
397 | PUTBACK; |
419 | } |
398 | } |
420 | |
399 | |
421 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
400 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
422 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
401 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
423 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
402 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
403 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
424 | |
404 | |
425 | c->dowarn = PL_dowarn; |
405 | #define VAR(name,type)c->name = PL_ ## name; |
426 | c->in_eval = PL_in_eval; |
406 | # include "state.h" |
427 | |
407 | #undef VAR |
428 | c->curstackinfo = PL_curstackinfo; |
|
|
429 | c->curstack = PL_curstack; |
|
|
430 | c->mainstack = PL_mainstack; |
|
|
431 | c->stack_sp = PL_stack_sp; |
|
|
432 | c->op = PL_op; |
|
|
433 | c->curpad = PL_curpad; |
|
|
434 | c->comppad = PL_comppad; |
|
|
435 | c->compcv = PL_compcv; |
|
|
436 | c->stack_base = PL_stack_base; |
|
|
437 | c->stack_max = PL_stack_max; |
|
|
438 | c->tmps_stack = PL_tmps_stack; |
|
|
439 | c->tmps_floor = PL_tmps_floor; |
|
|
440 | c->tmps_ix = PL_tmps_ix; |
|
|
441 | c->tmps_max = PL_tmps_max; |
|
|
442 | c->markstack = PL_markstack; |
|
|
443 | c->markstack_ptr = PL_markstack_ptr; |
|
|
444 | c->markstack_max = PL_markstack_max; |
|
|
445 | c->scopestack = PL_scopestack; |
|
|
446 | c->scopestack_ix = PL_scopestack_ix; |
|
|
447 | c->scopestack_max = PL_scopestack_max; |
|
|
448 | c->savestack = PL_savestack; |
|
|
449 | c->savestack_ix = PL_savestack_ix; |
|
|
450 | c->savestack_max = PL_savestack_max; |
|
|
451 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
452 | c->retstack = PL_retstack; |
|
|
453 | c->retstack_ix = PL_retstack_ix; |
|
|
454 | c->retstack_max = PL_retstack_max; |
|
|
455 | #endif |
|
|
456 | c->curpm = PL_curpm; |
|
|
457 | c->curcop = PL_curcop; |
|
|
458 | } |
408 | } |
459 | |
409 | |
460 | /* |
410 | /* |
461 | * allocate various perl stacks. This is an exact copy |
411 | * allocate various perl stacks. This is an exact copy |
462 | * of perl.c:init_stacks, except that it uses less memory |
412 | * of perl.c:init_stacks, except that it uses less memory |
… | |
… | |
464 | * not usually need a lot of stackspace. |
414 | * not usually need a lot of stackspace. |
465 | */ |
415 | */ |
466 | static void |
416 | static void |
467 | coro_init_stacks () |
417 | coro_init_stacks () |
468 | { |
418 | { |
469 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
419 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
470 | PL_curstackinfo->si_type = PERLSI_MAIN; |
420 | PL_curstackinfo->si_type = PERLSI_MAIN; |
471 | PL_curstack = PL_curstackinfo->si_stack; |
421 | PL_curstack = PL_curstackinfo->si_stack; |
472 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
422 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
473 | |
423 | |
474 | PL_stack_base = AvARRAY(PL_curstack); |
424 | PL_stack_base = AvARRAY(PL_curstack); |
475 | PL_stack_sp = PL_stack_base; |
425 | PL_stack_sp = PL_stack_base; |
476 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
426 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
477 | |
427 | |
478 | New(50,PL_tmps_stack,96,SV*); |
428 | New(50,PL_tmps_stack,128,SV*); |
479 | PL_tmps_floor = -1; |
429 | PL_tmps_floor = -1; |
480 | PL_tmps_ix = -1; |
430 | PL_tmps_ix = -1; |
481 | PL_tmps_max = 96; |
431 | PL_tmps_max = 128; |
482 | |
432 | |
483 | New(54,PL_markstack,16,I32); |
433 | New(54,PL_markstack,32,I32); |
484 | PL_markstack_ptr = PL_markstack; |
434 | PL_markstack_ptr = PL_markstack; |
485 | PL_markstack_max = PL_markstack + 16; |
435 | PL_markstack_max = PL_markstack + 32; |
486 | |
436 | |
487 | #ifdef SET_MARK_OFFSET |
437 | #ifdef SET_MARK_OFFSET |
488 | SET_MARK_OFFSET; |
438 | SET_MARK_OFFSET; |
489 | #endif |
439 | #endif |
490 | |
440 | |
491 | New(54,PL_scopestack,16,I32); |
441 | New(54,PL_scopestack,32,I32); |
492 | PL_scopestack_ix = 0; |
442 | PL_scopestack_ix = 0; |
493 | PL_scopestack_max = 16; |
443 | PL_scopestack_max = 32; |
494 | |
444 | |
495 | New(54,PL_savestack,96,ANY); |
445 | New(54,PL_savestack,64,ANY); |
496 | PL_savestack_ix = 0; |
446 | PL_savestack_ix = 0; |
497 | PL_savestack_max = 96; |
447 | PL_savestack_max = 64; |
498 | |
448 | |
499 | #if !PERL_VERSION_ATLEAST (5,9,0) |
449 | #if !PERL_VERSION_ATLEAST (5,9,0) |
500 | New(54,PL_retstack,8,OP*); |
450 | New(54,PL_retstack,16,OP*); |
501 | PL_retstack_ix = 0; |
451 | PL_retstack_ix = 0; |
502 | PL_retstack_max = 8; |
452 | PL_retstack_max = 16; |
503 | #endif |
453 | #endif |
504 | } |
454 | } |
505 | |
455 | |
506 | /* |
456 | /* |
507 | * destroy the stacks, the callchain etc... |
457 | * destroy the stacks, the callchain etc... |
… | |
… | |
560 | * emulate part of the perl startup here. |
510 | * emulate part of the perl startup here. |
561 | */ |
511 | */ |
562 | |
512 | |
563 | coro_init_stacks (); |
513 | coro_init_stacks (); |
564 | |
514 | |
565 | PL_curcop = 0; |
515 | PL_curcop = &PL_compiling; |
566 | PL_in_eval = 0; |
516 | PL_in_eval = EVAL_NULL; |
567 | PL_curpm = 0; |
517 | PL_curpm = 0; |
|
|
518 | PL_localizing = 0; |
|
|
519 | PL_dirty = 0; |
|
|
520 | PL_restartop = 0; |
568 | |
521 | |
569 | { |
522 | { |
570 | dSP; |
523 | dSP; |
571 | LOGOP myop; |
524 | LOGOP myop; |
572 | |
525 | |
573 | /* I have no idea why this is needed, but it is */ |
|
|
574 | PUSHMARK (SP); |
|
|
575 | |
|
|
576 | SvREFCNT_dec (GvAV (PL_defgv)); |
526 | SvREFCNT_dec (GvAV (PL_defgv)); |
577 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
527 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
578 | |
528 | |
579 | Zero (&myop, 1, LOGOP); |
529 | Zero (&myop, 1, LOGOP); |
580 | myop.op_next = Nullop; |
530 | myop.op_next = Nullop; |
581 | myop.op_flags = OPf_WANT_VOID; |
531 | myop.op_flags = OPf_WANT_VOID; |
582 | |
532 | |
583 | PL_op = (OP *)&myop; |
|
|
584 | |
|
|
585 | PUSHMARK (SP); |
533 | PUSHMARK (SP); |
586 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
534 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
587 | PUTBACK; |
535 | PUTBACK; |
|
|
536 | PL_op = (OP *)&myop; |
588 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
537 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
589 | SPAGAIN; |
538 | SPAGAIN; |
590 | |
|
|
591 | ENTER; /* necessary e.g. for dounwind */ |
|
|
592 | } |
539 | } |
|
|
540 | |
|
|
541 | ENTER; /* necessary e.g. for dounwind */ |
593 | } |
542 | } |
594 | |
543 | |
595 | static void |
544 | static void |
596 | free_coro_mortal () |
545 | free_coro_mortal () |
597 | { |
546 | { |
… | |
… | |
600 | SvREFCNT_dec (coro_mortal); |
549 | SvREFCNT_dec (coro_mortal); |
601 | coro_mortal = 0; |
550 | coro_mortal = 0; |
602 | } |
551 | } |
603 | } |
552 | } |
604 | |
553 | |
|
|
554 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
605 | static void NOINLINE |
555 | static void NOINLINE |
606 | prepare_cctx (coro_cctx *cctx) |
556 | prepare_cctx (coro_cctx *cctx) |
607 | { |
557 | { |
608 | dSP; |
558 | dSP; |
609 | LOGOP myop; |
559 | LOGOP myop; |
610 | |
560 | |
611 | Zero (&myop, 1, LOGOP); |
561 | Zero (&myop, 1, LOGOP); |
612 | myop.op_next = PL_op; |
562 | myop.op_next = PL_op; |
613 | myop.op_flags = OPf_WANT_VOID; |
563 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
614 | |
|
|
615 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
|
|
616 | |
564 | |
617 | PUSHMARK (SP); |
565 | PUSHMARK (SP); |
|
|
566 | EXTEND (SP, 2); |
|
|
567 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
618 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
568 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
619 | PUTBACK; |
569 | PUTBACK; |
|
|
570 | PL_op = (OP *)&myop; |
620 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
571 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
621 | SPAGAIN; |
572 | SPAGAIN; |
622 | } |
573 | } |
623 | |
574 | |
624 | static void |
575 | static void |
625 | coro_run (void *arg) |
576 | coro_run (void *arg) |
626 | { |
577 | { |
627 | /* coro_run is the alternative epilogue of transfer() */ |
578 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
628 | UNLOCK; |
579 | UNLOCK; |
629 | |
580 | |
630 | /* |
581 | /* |
631 | * this is a _very_ stripped down perl interpreter ;) |
582 | * this is a _very_ stripped down perl interpreter ;) |
632 | */ |
583 | */ |
… | |
… | |
634 | |
585 | |
635 | /* inject call to cctx_init */ |
586 | /* inject call to cctx_init */ |
636 | prepare_cctx ((coro_cctx *)arg); |
587 | prepare_cctx ((coro_cctx *)arg); |
637 | |
588 | |
638 | /* somebody will hit me for both perl_run and PL_restartop */ |
589 | /* somebody will hit me for both perl_run and PL_restartop */ |
|
|
590 | PL_restartop = PL_op; |
639 | perl_run (PL_curinterp); |
591 | perl_run (PL_curinterp); |
640 | |
592 | |
641 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
593 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
642 | abort (); |
594 | abort (); |
643 | } |
595 | } |
… | |
… | |
647 | { |
599 | { |
648 | coro_cctx *cctx; |
600 | coro_cctx *cctx; |
649 | |
601 | |
650 | ++cctx_count; |
602 | ++cctx_count; |
651 | |
603 | |
652 | New (0, cctx, 1, coro_cctx); |
604 | Newz (0, cctx, 1, coro_cctx); |
653 | |
605 | |
654 | #if HAVE_MMAP |
606 | #if HAVE_MMAP |
655 | |
607 | |
656 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
608 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
657 | /* mmap suppsedly does allocate-on-write for us */ |
609 | /* mmap supposedly does allocate-on-write for us */ |
658 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
610 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
659 | |
611 | |
660 | if (cctx->sptr == (void *)-1) |
612 | if (cctx->sptr == (void *)-1) |
661 | { |
613 | { |
662 | perror ("FATAL: unable to mmap stack for coroutine"); |
614 | perror ("FATAL: unable to mmap stack for coroutine"); |
… | |
… | |
691 | |
643 | |
692 | return cctx; |
644 | return cctx; |
693 | } |
645 | } |
694 | |
646 | |
695 | static void |
647 | static void |
696 | cctx_free (coro_cctx *cctx) |
648 | cctx_destroy (coro_cctx *cctx) |
697 | { |
649 | { |
698 | if (!cctx) |
650 | if (!cctx) |
699 | return; |
651 | return; |
700 | |
652 | |
701 | --cctx_count; |
653 | --cctx_count; |
… | |
… | |
718 | { |
670 | { |
719 | coro_cctx *cctx; |
671 | coro_cctx *cctx; |
720 | |
672 | |
721 | if (cctx_first) |
673 | if (cctx_first) |
722 | { |
674 | { |
723 | --cctx_idle; |
|
|
724 | cctx = cctx_first; |
675 | cctx = cctx_first; |
725 | cctx_first = cctx->next; |
676 | cctx_first = cctx->next; |
|
|
677 | --cctx_idle; |
726 | } |
678 | } |
727 | else |
679 | else |
728 | { |
680 | { |
729 | cctx = cctx_new (); |
681 | cctx = cctx_new (); |
730 | PL_op = PL_op->op_next; |
682 | PL_op = PL_op->op_next; |
… | |
… | |
734 | } |
686 | } |
735 | |
687 | |
736 | static void |
688 | static void |
737 | cctx_put (coro_cctx *cctx) |
689 | cctx_put (coro_cctx *cctx) |
738 | { |
690 | { |
|
|
691 | /* free another cctx if overlimit */ |
|
|
692 | if (cctx_idle >= MAX_IDLE_CCTX) |
|
|
693 | { |
|
|
694 | coro_cctx *first = cctx_first; |
|
|
695 | cctx_first = first->next; |
|
|
696 | --cctx_idle; |
|
|
697 | |
|
|
698 | assert (!first->inuse); |
|
|
699 | cctx_destroy (first); |
|
|
700 | } |
|
|
701 | |
739 | ++cctx_idle; |
702 | ++cctx_idle; |
740 | cctx->next = cctx_first; |
703 | cctx->next = cctx_first; |
741 | cctx_first = cctx; |
704 | cctx_first = cctx; |
742 | } |
705 | } |
743 | |
706 | |
744 | /* never call directly, always through the coro_state_transfer global variable */ |
707 | /* never call directly, always through the coro_state_transfer global variable */ |
745 | static void NOINLINE |
708 | static void NOINLINE |
746 | transfer (struct coro *prev, struct coro *next, int flags) |
709 | transfer (struct coro *prev, struct coro *next) |
747 | { |
710 | { |
748 | dSTACKLEVEL; |
711 | dSTACKLEVEL; |
749 | |
712 | |
750 | /* sometimes transfer is only called to set idle_sp */ |
713 | /* sometimes transfer is only called to set idle_sp */ |
751 | if (flags == TRANSFER_SET_STACKLEVEL) |
714 | if (!next) |
|
|
715 | { |
752 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
716 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
|
|
717 | assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */ |
|
|
718 | } |
753 | else if (prev != next) |
719 | else if (prev != next) |
754 | { |
720 | { |
755 | coro_cctx *prev__cctx; |
721 | coro_cctx *prev__cctx; |
756 | |
722 | |
|
|
723 | if (prev->flags & CF_NEW) |
|
|
724 | { |
|
|
725 | /* create a new empty context */ |
|
|
726 | Newz (0, prev->cctx, 1, coro_cctx); |
|
|
727 | prev->cctx->inuse = 1; |
|
|
728 | prev->flags &= ~CF_NEW; |
|
|
729 | prev->flags |= CF_RUNNING; |
|
|
730 | } |
|
|
731 | |
|
|
732 | /*TODO: must not croak here */ |
|
|
733 | if (!prev->flags & CF_RUNNING) |
|
|
734 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
|
|
735 | |
|
|
736 | if (next->flags & CF_RUNNING) |
|
|
737 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
738 | |
|
|
739 | prev->flags &= ~CF_RUNNING; |
|
|
740 | next->flags |= CF_RUNNING; |
|
|
741 | |
757 | LOCK; |
742 | LOCK; |
758 | |
743 | |
759 | if (next->mainstack) |
744 | if (next->flags & CF_NEW) |
760 | { |
745 | { |
761 | /* coroutine already started */ |
746 | /* need to start coroutine */ |
762 | SAVE (prev, flags); |
747 | next->flags &= ~CF_NEW; |
763 | LOAD (next); |
748 | /* first get rid of the old state */ |
|
|
749 | save_perl (prev); |
|
|
750 | /* setup coroutine call */ |
|
|
751 | setup_coro (next); |
|
|
752 | /* need a new stack */ |
|
|
753 | assert (!next->cctx); |
764 | } |
754 | } |
765 | else |
755 | else |
766 | { |
756 | { |
767 | /* need to start coroutine */ |
757 | /* coroutine already started */ |
768 | /* first get rid of the old state */ |
758 | save_perl (prev); |
769 | SAVE (prev, -1); |
759 | load_perl (next); |
770 | /* setup coroutine call */ |
|
|
771 | setup_coro (next); |
|
|
772 | /* need a stack */ |
|
|
773 | next->cctx = 0; |
|
|
774 | } |
760 | } |
775 | |
|
|
776 | if (!prev->cctx) |
|
|
777 | /* create a new empty context */ |
|
|
778 | Newz (0, prev->cctx, 1, coro_cctx); |
|
|
779 | |
761 | |
780 | prev__cctx = prev->cctx; |
762 | prev__cctx = prev->cctx; |
781 | |
763 | |
782 | /* possibly "free" the cctx */ |
764 | /* possibly "free" the cctx */ |
783 | if (prev__cctx->idle_sp == STACKLEVEL) |
765 | if (prev__cctx->idle_sp == STACKLEVEL) |
784 | { |
766 | { |
|
|
767 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
|
|
768 | assert (PL_top_env == prev__cctx->top_env); |
|
|
769 | |
|
|
770 | prev->cctx = 0; |
|
|
771 | |
785 | cctx_put (prev__cctx); |
772 | cctx_put (prev__cctx); |
786 | prev->cctx = 0; |
773 | prev__cctx->inuse = 0; |
787 | } |
774 | } |
788 | |
775 | |
789 | if (!next->cctx) |
776 | if (!next->cctx) |
|
|
777 | { |
790 | next->cctx = cctx_get (); |
778 | next->cctx = cctx_get (); |
|
|
779 | assert (!next->cctx->inuse); |
|
|
780 | next->cctx->inuse = 1; |
|
|
781 | } |
791 | |
782 | |
792 | if (prev__cctx != next->cctx) |
783 | if (prev__cctx != next->cctx) |
793 | { |
784 | { |
794 | prev__cctx->top_env = PL_top_env; |
785 | prev__cctx->top_env = PL_top_env; |
795 | PL_top_env = next->cctx->top_env; |
786 | PL_top_env = next->cctx->top_env; |
… | |
… | |
803 | } |
794 | } |
804 | |
795 | |
805 | struct transfer_args |
796 | struct transfer_args |
806 | { |
797 | { |
807 | struct coro *prev, *next; |
798 | struct coro *prev, *next; |
808 | int flags; |
|
|
809 | }; |
799 | }; |
810 | |
800 | |
811 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) |
801 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
812 | |
802 | |
813 | static void |
803 | static void |
814 | coro_state_destroy (struct coro *coro) |
804 | coro_state_destroy (struct coro *coro) |
815 | { |
805 | { |
816 | if (coro->refcnt--) |
806 | if (coro->refcnt--) |
817 | return; |
807 | return; |
818 | |
808 | |
819 | if (coro->mainstack && coro->mainstack != main_mainstack) |
809 | if (coro->mainstack && coro->mainstack != main_mainstack) |
820 | { |
810 | { |
821 | struct coro temp; |
811 | struct coro temp; |
|
|
812 | Zero (&temp, 1, struct coro); |
|
|
813 | temp.save = CORO_SAVE_ALL; |
822 | |
814 | |
823 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
815 | if (coro->flags & CF_RUNNING) |
824 | LOAD (coro); |
816 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
817 | |
|
|
818 | save_perl (&temp); |
|
|
819 | load_perl (coro); |
825 | |
820 | |
826 | coro_destroy_stacks (); |
821 | coro_destroy_stacks (); |
827 | |
822 | |
828 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
823 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
829 | |
824 | |
830 | coro->mainstack = 0; |
825 | coro->mainstack = 0; |
831 | } |
826 | } |
832 | |
827 | |
833 | cctx_free (coro->cctx); |
828 | cctx_destroy (coro->cctx); |
834 | SvREFCNT_dec (coro->args); |
829 | SvREFCNT_dec (coro->args); |
835 | Safefree (coro); |
830 | Safefree (coro); |
836 | } |
831 | } |
837 | |
832 | |
838 | static int |
833 | static int |
… | |
… | |
888 | assert (mg->mg_type == PERL_MAGIC_ext); |
883 | assert (mg->mg_type == PERL_MAGIC_ext); |
889 | return (struct coro *)mg->mg_ptr; |
884 | return (struct coro *)mg->mg_ptr; |
890 | } |
885 | } |
891 | |
886 | |
892 | static void |
887 | static void |
893 | prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) |
888 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
894 | { |
889 | { |
895 | ta->prev = SvSTATE (prev); |
890 | ta->prev = SvSTATE (prev_sv); |
896 | ta->next = SvSTATE (next); |
891 | ta->next = SvSTATE (next_sv); |
897 | ta->flags = flags; |
|
|
898 | } |
892 | } |
899 | |
893 | |
900 | static void |
894 | static void |
901 | api_transfer (SV *prev, SV *next, int flags) |
895 | api_transfer (SV *prev_sv, SV *next_sv) |
902 | { |
896 | { |
903 | dTHX; |
|
|
904 | struct transfer_args ta; |
897 | struct transfer_args ta; |
905 | |
898 | |
906 | prepare_transfer (&ta, prev, next, flags); |
899 | prepare_transfer (&ta, prev_sv, next_sv); |
907 | TRANSFER (ta); |
900 | TRANSFER (ta); |
|
|
901 | } |
|
|
902 | |
|
|
903 | static int |
|
|
904 | api_save (SV *coro_sv, int new_save) |
|
|
905 | { |
|
|
906 | struct coro *coro = SvSTATE (coro_sv); |
|
|
907 | int old_save = coro->save; |
|
|
908 | |
|
|
909 | if (new_save >= 0) |
|
|
910 | coro->save = new_save; |
|
|
911 | |
|
|
912 | return old_save; |
908 | } |
913 | } |
909 | |
914 | |
910 | /** Coro ********************************************************************/ |
915 | /** Coro ********************************************************************/ |
911 | |
916 | |
912 | #define PRIO_MAX 3 |
917 | #define PRIO_MAX 3 |
… | |
… | |
915 | #define PRIO_LOW -1 |
920 | #define PRIO_LOW -1 |
916 | #define PRIO_IDLE -3 |
921 | #define PRIO_IDLE -3 |
917 | #define PRIO_MIN -4 |
922 | #define PRIO_MIN -4 |
918 | |
923 | |
919 | /* for Coro.pm */ |
924 | /* for Coro.pm */ |
920 | static GV *coro_current, *coro_idle; |
925 | static SV *coro_current; |
921 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
926 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
922 | static int coro_nready; |
927 | static int coro_nready; |
923 | |
928 | |
924 | static void |
929 | static void |
925 | coro_enq (SV *sv) |
930 | coro_enq (SV *coro_sv) |
926 | { |
931 | { |
927 | int prio; |
|
|
928 | |
|
|
929 | if (SvTYPE (sv) != SVt_PVHV) |
|
|
930 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
931 | |
|
|
932 | prio = SvSTATE (sv)->prio; |
|
|
933 | |
|
|
934 | av_push (coro_ready [prio - PRIO_MIN], sv); |
932 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
935 | coro_nready++; |
933 | coro_nready++; |
936 | } |
934 | } |
937 | |
935 | |
938 | static SV * |
936 | static SV * |
939 | coro_deq (int min_prio) |
937 | coro_deq (int min_prio) |
… | |
… | |
952 | } |
950 | } |
953 | |
951 | |
954 | return 0; |
952 | return 0; |
955 | } |
953 | } |
956 | |
954 | |
957 | static void |
955 | static int |
958 | api_ready (SV *coro) |
956 | api_ready (SV *coro_sv) |
959 | { |
957 | { |
960 | dTHX; |
958 | struct coro *coro; |
961 | |
959 | |
962 | if (SvROK (coro)) |
960 | if (SvROK (coro_sv)) |
963 | coro = SvRV (coro); |
961 | coro_sv = SvRV (coro_sv); |
|
|
962 | |
|
|
963 | coro = SvSTATE (coro_sv); |
|
|
964 | |
|
|
965 | if (coro->flags & CF_READY) |
|
|
966 | return 0; |
|
|
967 | |
|
|
968 | #if 0 /* this is actually harmless */ |
|
|
969 | if (coro->flags & CF_RUNNING) |
|
|
970 | croak ("Coro::ready called on currently running coroutine"); |
|
|
971 | #endif |
|
|
972 | |
|
|
973 | coro->flags |= CF_READY; |
964 | |
974 | |
965 | LOCK; |
975 | LOCK; |
966 | coro_enq (SvREFCNT_inc (coro)); |
976 | coro_enq (SvREFCNT_inc (coro_sv)); |
967 | UNLOCK; |
977 | UNLOCK; |
|
|
978 | |
|
|
979 | return 1; |
|
|
980 | } |
|
|
981 | |
|
|
982 | static int |
|
|
983 | api_is_ready (SV *coro_sv) |
|
|
984 | { |
|
|
985 | return !!SvSTATE (coro_sv)->flags & CF_READY; |
968 | } |
986 | } |
969 | |
987 | |
970 | static void |
988 | static void |
971 | prepare_schedule (struct transfer_args *ta) |
989 | prepare_schedule (struct transfer_args *ta) |
972 | { |
990 | { |
973 | SV *current, *prev, *next; |
991 | SV *prev, *next; |
974 | |
|
|
975 | current = GvSV (coro_current); |
|
|
976 | |
992 | |
977 | for (;;) |
993 | for (;;) |
978 | { |
994 | { |
979 | LOCK; |
995 | LOCK; |
980 | next = coro_deq (PRIO_MIN); |
996 | next = coro_deq (PRIO_MIN); |
… | |
… | |
989 | ENTER; |
1005 | ENTER; |
990 | SAVETMPS; |
1006 | SAVETMPS; |
991 | |
1007 | |
992 | PUSHMARK (SP); |
1008 | PUSHMARK (SP); |
993 | PUTBACK; |
1009 | PUTBACK; |
994 | call_sv (GvSV (coro_idle), G_DISCARD); |
1010 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
995 | |
1011 | |
996 | FREETMPS; |
1012 | FREETMPS; |
997 | LEAVE; |
1013 | LEAVE; |
998 | } |
1014 | } |
999 | } |
1015 | } |
1000 | |
1016 | |
1001 | prev = SvRV (current); |
1017 | prev = SvRV (coro_current); |
1002 | SvRV (current) = next; |
1018 | SvRV_set (coro_current, next); |
1003 | |
1019 | |
1004 | /* free this only after the transfer */ |
1020 | /* free this only after the transfer */ |
1005 | LOCK; |
1021 | LOCK; |
1006 | free_coro_mortal (); |
1022 | free_coro_mortal (); |
1007 | UNLOCK; |
1023 | UNLOCK; |
1008 | coro_mortal = prev; |
1024 | coro_mortal = prev; |
1009 | |
1025 | |
|
|
1026 | assert (!SvROK(prev));//D |
|
|
1027 | assert (!SvROK(next));//D |
|
|
1028 | |
1010 | ta->prev = SvSTATE (prev); |
1029 | ta->prev = SvSTATE (prev); |
1011 | ta->next = SvSTATE (next); |
1030 | ta->next = SvSTATE (next); |
1012 | ta->flags = TRANSFER_SAVE_ALL; |
1031 | |
|
|
1032 | assert (ta->next->flags & CF_READY); |
|
|
1033 | ta->next->flags &= ~CF_READY; |
1013 | } |
1034 | } |
1014 | |
1035 | |
1015 | static void |
1036 | static void |
1016 | prepare_cede (struct transfer_args *ta) |
1037 | prepare_cede (struct transfer_args *ta) |
1017 | { |
1038 | { |
1018 | LOCK; |
1039 | api_ready (coro_current); |
1019 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
1020 | UNLOCK; |
|
|
1021 | |
1040 | |
1022 | prepare_schedule (ta); |
1041 | prepare_schedule (ta); |
1023 | } |
1042 | } |
1024 | |
1043 | |
1025 | static void |
1044 | static void |
1026 | api_schedule (void) |
1045 | api_schedule (void) |
1027 | { |
1046 | { |
1028 | dTHX; |
|
|
1029 | struct transfer_args ta; |
1047 | struct transfer_args ta; |
1030 | |
1048 | |
1031 | prepare_schedule (&ta); |
1049 | prepare_schedule (&ta); |
1032 | TRANSFER (ta); |
1050 | TRANSFER (ta); |
1033 | } |
1051 | } |
1034 | |
1052 | |
1035 | static void |
1053 | static void |
1036 | api_cede (void) |
1054 | api_cede (void) |
1037 | { |
1055 | { |
1038 | dTHX; |
|
|
1039 | struct transfer_args ta; |
1056 | struct transfer_args ta; |
1040 | |
1057 | |
1041 | prepare_cede (&ta); |
1058 | prepare_cede (&ta); |
1042 | TRANSFER (ta); |
1059 | TRANSFER (ta); |
1043 | } |
1060 | } |
… | |
… | |
1053 | #endif |
1070 | #endif |
1054 | BOOT_PAGESIZE; |
1071 | BOOT_PAGESIZE; |
1055 | |
1072 | |
1056 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1073 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1057 | |
1074 | |
1058 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
1075 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1059 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
1076 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1060 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
1077 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
|
|
1078 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1079 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1061 | |
1080 | |
1062 | main_mainstack = PL_mainstack; |
1081 | main_mainstack = PL_mainstack; |
1063 | |
1082 | |
1064 | coroapi.ver = CORO_API_VERSION; |
1083 | coroapi.ver = CORO_API_VERSION; |
1065 | coroapi.transfer = api_transfer; |
1084 | coroapi.transfer = api_transfer; |
… | |
… | |
1075 | HV *hv; |
1094 | HV *hv; |
1076 | int i; |
1095 | int i; |
1077 | |
1096 | |
1078 | Newz (0, coro, 1, struct coro); |
1097 | Newz (0, coro, 1, struct coro); |
1079 | coro->args = newAV (); |
1098 | coro->args = newAV (); |
|
|
1099 | coro->save = CORO_SAVE_ALL; |
|
|
1100 | coro->flags = CF_NEW; |
1080 | |
1101 | |
1081 | hv = newHV (); |
1102 | hv = newHV (); |
1082 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1103 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1083 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1104 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1084 | |
1105 | |
1085 | for (i = 1; i < items; i++) |
1106 | for (i = 1; i < items; i++) |
1086 | av_push (coro->args, newSVsv (ST (i))); |
1107 | av_push (coro->args, newSVsv (ST (i))); |
1087 | } |
1108 | } |
1088 | OUTPUT: |
1109 | OUTPUT: |
|
|
1110 | RETVAL |
|
|
1111 | |
|
|
1112 | int |
|
|
1113 | save (SV *coro, int new_save = -1) |
|
|
1114 | CODE: |
|
|
1115 | RETVAL = api_save (coro, new_save); |
|
|
1116 | OUTPUT: |
1089 | RETVAL |
1117 | RETVAL |
1090 | |
1118 | |
1091 | void |
1119 | void |
1092 | _set_stacklevel (...) |
1120 | _set_stacklevel (...) |
1093 | ALIAS: |
1121 | ALIAS: |
1094 | Coro::State::transfer = 1 |
1122 | Coro::State::transfer = 1 |
1095 | Coro::schedule = 2 |
1123 | Coro::schedule = 2 |
1096 | Coro::cede = 3 |
1124 | Coro::cede = 3 |
1097 | Coro::Cont::yield = 4 |
|
|
1098 | CODE: |
1125 | CODE: |
1099 | { |
1126 | { |
1100 | struct transfer_args ta; |
1127 | struct transfer_args ta; |
1101 | |
1128 | |
1102 | switch (ix) |
1129 | switch (ix) |
1103 | { |
1130 | { |
1104 | case 0: |
1131 | case 0: |
1105 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1132 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1106 | ta.next = 0; |
1133 | ta.next = 0; |
1107 | ta.flags = TRANSFER_SET_STACKLEVEL; |
|
|
1108 | break; |
1134 | break; |
1109 | |
1135 | |
1110 | case 1: |
1136 | case 1: |
1111 | if (items != 3) |
1137 | if (items != 2) |
1112 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1138 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1113 | |
1139 | |
1114 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1140 | prepare_transfer (&ta, ST (0), ST (1)); |
1115 | break; |
1141 | break; |
1116 | |
1142 | |
1117 | case 2: |
1143 | case 2: |
1118 | prepare_schedule (&ta); |
1144 | prepare_schedule (&ta); |
1119 | break; |
1145 | break; |
1120 | |
1146 | |
1121 | case 3: |
1147 | case 3: |
1122 | prepare_cede (&ta); |
1148 | prepare_cede (&ta); |
1123 | break; |
1149 | break; |
1124 | |
|
|
1125 | case 4: |
|
|
1126 | { |
|
|
1127 | SV *yieldstack; |
|
|
1128 | SV *sv; |
|
|
1129 | AV *defav = GvAV (PL_defgv); |
|
|
1130 | |
|
|
1131 | yieldstack = *hv_fetch ( |
|
|
1132 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1133 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1134 | 0 |
|
|
1135 | ); |
|
|
1136 | |
|
|
1137 | /* set up @_ -- ugly */ |
|
|
1138 | av_clear (defav); |
|
|
1139 | av_fill (defav, items - 1); |
|
|
1140 | while (items--) |
|
|
1141 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1142 | |
|
|
1143 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1144 | ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1145 | ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1146 | ta.flags = 0; |
|
|
1147 | SvREFCNT_dec (sv); |
|
|
1148 | } |
|
|
1149 | break; |
|
|
1150 | |
|
|
1151 | } |
1150 | } |
1152 | |
1151 | |
1153 | TRANSFER (ta); |
1152 | TRANSFER (ta); |
1154 | } |
1153 | } |
1155 | |
1154 | |
… | |
… | |
1199 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1198 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1200 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1199 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1201 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1200 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1202 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1201 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1203 | |
1202 | |
1204 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1203 | coro_current = get_sv ("Coro::current", FALSE); |
1205 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1204 | SvREADONLY_on (coro_current); |
1206 | |
1205 | |
1207 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1206 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1208 | coro_ready[i] = newAV (); |
1207 | coro_ready[i] = newAV (); |
1209 | |
1208 | |
1210 | { |
1209 | { |
1211 | SV *sv = perl_get_sv("Coro::API", 1); |
1210 | SV *sv = perl_get_sv("Coro::API", 1); |
1212 | |
1211 | |
1213 | coroapi.schedule = api_schedule; |
1212 | coroapi.schedule = api_schedule; |
|
|
1213 | coroapi.save = api_save; |
1214 | coroapi.cede = api_cede; |
1214 | coroapi.cede = api_cede; |
1215 | coroapi.ready = api_ready; |
1215 | coroapi.ready = api_ready; |
|
|
1216 | coroapi.is_ready = api_is_ready; |
1216 | coroapi.nready = &coro_nready; |
1217 | coroapi.nready = &coro_nready; |
1217 | coroapi.current = coro_current; |
1218 | coroapi.current = coro_current; |
1218 | |
1219 | |
1219 | GCoroAPI = &coroapi; |
1220 | GCoroAPI = &coroapi; |
1220 | sv_setiv (sv, (IV)&coroapi); |
1221 | sv_setiv (sv, (IV)&coroapi); |
1221 | SvREADONLY_on (sv); |
1222 | SvREADONLY_on (sv); |
1222 | } |
1223 | } |
1223 | } |
1224 | } |
|
|
1225 | |
|
|
1226 | void |
|
|
1227 | _set_current (SV *current) |
|
|
1228 | PROTOTYPE: $ |
|
|
1229 | CODE: |
|
|
1230 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1231 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
1224 | |
1232 | |
1225 | int |
1233 | int |
1226 | prio (Coro::State coro, int newprio = 0) |
1234 | prio (Coro::State coro, int newprio = 0) |
1227 | ALIAS: |
1235 | ALIAS: |
1228 | nice = 1 |
1236 | nice = 1 |
… | |
… | |
1240 | |
1248 | |
1241 | coro->prio = newprio; |
1249 | coro->prio = newprio; |
1242 | } |
1250 | } |
1243 | } |
1251 | } |
1244 | |
1252 | |
1245 | void |
1253 | SV * |
1246 | ready (SV *self) |
1254 | ready (SV *self) |
1247 | PROTOTYPE: $ |
1255 | PROTOTYPE: $ |
1248 | CODE: |
1256 | CODE: |
1249 | api_ready (self); |
1257 | RETVAL = boolSV (api_ready (self)); |
|
|
1258 | OUTPUT: |
|
|
1259 | RETVAL |
|
|
1260 | |
|
|
1261 | SV * |
|
|
1262 | is_ready (SV *self) |
|
|
1263 | PROTOTYPE: $ |
|
|
1264 | CODE: |
|
|
1265 | RETVAL = boolSV (api_is_ready (self)); |
|
|
1266 | OUTPUT: |
|
|
1267 | RETVAL |
1250 | |
1268 | |
1251 | int |
1269 | int |
1252 | nready (...) |
1270 | nready (...) |
1253 | PROTOTYPE: |
1271 | PROTOTYPE: |
1254 | CODE: |
1272 | CODE: |
… | |
… | |
1260 | |
1278 | |
1261 | SV * |
1279 | SV * |
1262 | _get_state () |
1280 | _get_state () |
1263 | CODE: |
1281 | CODE: |
1264 | { |
1282 | { |
1265 | struct { |
1283 | struct io_state *data; |
1266 | int errorno; |
|
|
1267 | int laststype; |
|
|
1268 | int laststatval; |
|
|
1269 | Stat_t statcache; |
|
|
1270 | } data; |
|
|
1271 | |
1284 | |
|
|
1285 | RETVAL = newSV (sizeof (struct io_state)); |
|
|
1286 | data = (struct io_state *)SvPVX (RETVAL); |
|
|
1287 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
|
|
1288 | SvPOK_only (RETVAL); |
|
|
1289 | |
1272 | data.errorno = errno; |
1290 | data->errorno = errno; |
1273 | data.laststype = PL_laststype; |
1291 | data->laststype = PL_laststype; |
1274 | data.laststatval = PL_laststatval; |
1292 | data->laststatval = PL_laststatval; |
1275 | data.statcache = PL_statcache; |
1293 | data->statcache = PL_statcache; |
1276 | |
|
|
1277 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1278 | } |
1294 | } |
1279 | OUTPUT: |
1295 | OUTPUT: |
1280 | RETVAL |
1296 | RETVAL |
1281 | |
1297 | |
1282 | void |
1298 | void |
1283 | _set_state (char *data_) |
1299 | _set_state (char *data_) |
1284 | PROTOTYPE: $ |
1300 | PROTOTYPE: $ |
1285 | CODE: |
1301 | CODE: |
1286 | { |
1302 | { |
1287 | struct { |
1303 | struct io_state *data = (void *)data_; |
1288 | int errorno; |
|
|
1289 | int laststype; |
|
|
1290 | int laststatval; |
|
|
1291 | Stat_t statcache; |
|
|
1292 | } *data = (void *)data_; |
|
|
1293 | |
1304 | |
1294 | errno = data->errorno; |
1305 | errno = data->errorno; |
1295 | PL_laststype = data->laststype; |
1306 | PL_laststype = data->laststype; |
1296 | PL_laststatval = data->laststatval; |
1307 | PL_laststatval = data->laststatval; |
1297 | PL_statcache = data->statcache; |
1308 | PL_statcache = data->statcache; |
1298 | } |
1309 | } |
|
|
1310 | |