… | |
… | |
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 | #define VAR(name,type) type name; |
170 | #define VAR(name,type) type name; |
144 | # include "state.h" |
171 | # include "state.h" |
145 | #undef VAR |
172 | #undef VAR |
146 | |
173 | |
… | |
… | |
214 | |
241 | |
215 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
242 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
216 | |
243 | |
217 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
244 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
218 | |
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 | |
219 | /* the next two functions merely cache the padlists */ |
253 | /* the next two functions merely cache the padlists */ |
220 | static void |
254 | static void |
221 | get_padlist (CV *cv) |
255 | get_padlist (CV *cv) |
222 | { |
256 | { |
223 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
257 | MAGIC *mg = CORO_MAGIC (cv); |
|
|
258 | AV *av; |
224 | |
259 | |
225 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
260 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
226 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
261 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
227 | else |
262 | else |
228 | { |
263 | { |
229 | #if 0 |
264 | #if 0 |
230 | /* this is probably cleaner, but also slower? */ |
265 | /* this is probably cleaner, but also slower? */ |
231 | CV *cp = Perl_cv_clone (cv); |
266 | CV *cp = Perl_cv_clone (cv); |
… | |
… | |
239 | } |
274 | } |
240 | |
275 | |
241 | static void |
276 | static void |
242 | put_padlist (CV *cv) |
277 | put_padlist (CV *cv) |
243 | { |
278 | { |
244 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
279 | MAGIC *mg = CORO_MAGIC (cv); |
|
|
280 | AV *av; |
245 | |
281 | |
246 | if (!mg) |
282 | if (!mg) |
247 | { |
283 | { |
248 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
284 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
249 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
285 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
250 | mg->mg_virtual = &vtbl_coro; |
286 | mg->mg_virtual = &vtbl_coro; |
251 | mg->mg_obj = (SV *)newAV (); |
287 | mg->mg_obj = (SV *)newAV (); |
252 | } |
288 | } |
253 | |
289 | |
254 | 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); |
255 | } |
296 | } |
256 | |
297 | |
257 | #define SB do { |
298 | #define SB do { |
258 | #define SE } while (0) |
299 | #define SE } while (0) |
259 | |
300 | |
260 | #define LOAD(state) load_state((state)); |
|
|
261 | #define SAVE(state,flags) save_state((state),(flags)); |
|
|
262 | |
|
|
263 | #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 |
264 | |
302 | |
265 | static void |
303 | static void |
266 | load_state(Coro__State c) |
304 | load_perl (Coro__State c) |
267 | { |
305 | { |
268 | #define VAR(name,type) PL_ ## name = c->name; |
306 | #define VAR(name,type) PL_ ## name = c->name; |
269 | # include "state.h" |
307 | # include "state.h" |
270 | #undef VAR |
308 | #undef VAR |
271 | |
309 | |
272 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
310 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
273 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
311 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
274 | 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 | } |
275 | |
324 | |
276 | { |
325 | { |
277 | dSP; |
326 | dSP; |
278 | CV *cv; |
327 | CV *cv; |
279 | |
328 | |
280 | /* now do the ugly restore mess */ |
329 | /* now do the ugly restore mess */ |
281 | while ((cv = (CV *)POPs)) |
330 | while ((cv = (CV *)POPs)) |
282 | { |
331 | { |
283 | AV *padlist = (AV *)POPs; |
|
|
284 | |
|
|
285 | if (padlist) |
|
|
286 | { |
|
|
287 | put_padlist (cv); /* mark this padlist as available */ |
332 | put_padlist (cv); /* mark this padlist as available */ |
288 | CvPADLIST(cv) = padlist; |
333 | CvDEPTH (cv) = PTR2IV (POPs); |
289 | } |
334 | CvPADLIST (cv) = (AV *)POPs; |
290 | |
|
|
291 | ++CvDEPTH(cv); |
|
|
292 | } |
335 | } |
293 | |
336 | |
294 | PUTBACK; |
337 | PUTBACK; |
295 | } |
338 | } |
296 | } |
339 | } |
297 | |
340 | |
298 | static void |
341 | static void |
299 | save_state(Coro__State c, int flags) |
342 | save_perl (Coro__State c) |
300 | { |
343 | { |
301 | { |
344 | { |
302 | dSP; |
345 | dSP; |
303 | I32 cxix = cxstack_ix; |
346 | I32 cxix = cxstack_ix; |
304 | PERL_CONTEXT *ccstk = cxstack; |
347 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
315 | { |
358 | { |
316 | while (cxix >= 0) |
359 | while (cxix >= 0) |
317 | { |
360 | { |
318 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
361 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
319 | |
362 | |
320 | if (CxTYPE(cx) == CXt_SUB) |
363 | if (CxTYPE (cx) == CXt_SUB) |
321 | { |
364 | { |
322 | CV *cv = cx->blk_sub.cv; |
365 | CV *cv = cx->blk_sub.cv; |
|
|
366 | |
323 | if (CvDEPTH(cv)) |
367 | if (CvDEPTH (cv)) |
324 | { |
368 | { |
325 | EXTEND (SP, CvDEPTH(cv)*2); |
369 | EXTEND (SP, 3); |
326 | |
370 | |
327 | while (--CvDEPTH(cv)) |
|
|
328 | { |
|
|
329 | /* this tells the restore code to increment CvDEPTH */ |
|
|
330 | PUSHs (Nullsv); |
|
|
331 | PUSHs ((SV *)cv); |
|
|
332 | } |
|
|
333 | |
|
|
334 | PUSHs ((SV *)CvPADLIST(cv)); |
371 | PUSHs ((SV *)CvPADLIST (cv)); |
|
|
372 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
335 | PUSHs ((SV *)cv); |
373 | PUSHs ((SV *)cv); |
336 | |
374 | |
|
|
375 | CvDEPTH (cv) = 0; |
337 | get_padlist (cv); |
376 | get_padlist (cv); |
338 | } |
377 | } |
339 | } |
378 | } |
340 | #ifdef CXt_FORMAT |
379 | #ifdef CXt_FORMAT |
341 | else if (CxTYPE(cx) == CXt_FORMAT) |
380 | else if (CxTYPE (cx) == CXt_FORMAT) |
342 | { |
381 | { |
343 | /* 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? */ |
344 | /* my bold guess is as a simple, plain sub... */ |
383 | /* my bold guess is as a simple, plain sub... */ |
345 | 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"); |
346 | } |
385 | } |
… | |
… | |
356 | } |
395 | } |
357 | |
396 | |
358 | PUTBACK; |
397 | PUTBACK; |
359 | } |
398 | } |
360 | |
399 | |
361 | 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; |
362 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
401 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
363 | 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; |
364 | |
404 | |
365 | #define VAR(name,type)c->name = PL_ ## name; |
405 | #define VAR(name,type)c->name = PL_ ## name; |
366 | # include "state.h" |
406 | # include "state.h" |
367 | #undef VAR |
407 | #undef VAR |
368 | } |
408 | } |
… | |
… | |
374 | * not usually need a lot of stackspace. |
414 | * not usually need a lot of stackspace. |
375 | */ |
415 | */ |
376 | static void |
416 | static void |
377 | coro_init_stacks () |
417 | coro_init_stacks () |
378 | { |
418 | { |
379 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
419 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
380 | PL_curstackinfo->si_type = PERLSI_MAIN; |
420 | PL_curstackinfo->si_type = PERLSI_MAIN; |
381 | PL_curstack = PL_curstackinfo->si_stack; |
421 | PL_curstack = PL_curstackinfo->si_stack; |
382 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
422 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
383 | |
423 | |
384 | PL_stack_base = AvARRAY(PL_curstack); |
424 | PL_stack_base = AvARRAY(PL_curstack); |
385 | PL_stack_sp = PL_stack_base; |
425 | PL_stack_sp = PL_stack_base; |
386 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
426 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
387 | |
427 | |
388 | New(50,PL_tmps_stack,96,SV*); |
428 | New(50,PL_tmps_stack,128,SV*); |
389 | PL_tmps_floor = -1; |
429 | PL_tmps_floor = -1; |
390 | PL_tmps_ix = -1; |
430 | PL_tmps_ix = -1; |
391 | PL_tmps_max = 96; |
431 | PL_tmps_max = 128; |
392 | |
432 | |
393 | New(54,PL_markstack,16,I32); |
433 | New(54,PL_markstack,32,I32); |
394 | PL_markstack_ptr = PL_markstack; |
434 | PL_markstack_ptr = PL_markstack; |
395 | PL_markstack_max = PL_markstack + 16; |
435 | PL_markstack_max = PL_markstack + 32; |
396 | |
436 | |
397 | #ifdef SET_MARK_OFFSET |
437 | #ifdef SET_MARK_OFFSET |
398 | SET_MARK_OFFSET; |
438 | SET_MARK_OFFSET; |
399 | #endif |
439 | #endif |
400 | |
440 | |
401 | New(54,PL_scopestack,16,I32); |
441 | New(54,PL_scopestack,32,I32); |
402 | PL_scopestack_ix = 0; |
442 | PL_scopestack_ix = 0; |
403 | PL_scopestack_max = 16; |
443 | PL_scopestack_max = 32; |
404 | |
444 | |
405 | New(54,PL_savestack,96,ANY); |
445 | New(54,PL_savestack,64,ANY); |
406 | PL_savestack_ix = 0; |
446 | PL_savestack_ix = 0; |
407 | PL_savestack_max = 96; |
447 | PL_savestack_max = 64; |
408 | |
448 | |
409 | #if !PERL_VERSION_ATLEAST (5,9,0) |
449 | #if !PERL_VERSION_ATLEAST (5,9,0) |
410 | New(54,PL_retstack,8,OP*); |
450 | New(54,PL_retstack,16,OP*); |
411 | PL_retstack_ix = 0; |
451 | PL_retstack_ix = 0; |
412 | PL_retstack_max = 8; |
452 | PL_retstack_max = 16; |
413 | #endif |
453 | #endif |
414 | } |
454 | } |
415 | |
455 | |
416 | /* |
456 | /* |
417 | * destroy the stacks, the callchain etc... |
457 | * destroy the stacks, the callchain etc... |
… | |
… | |
470 | * emulate part of the perl startup here. |
510 | * emulate part of the perl startup here. |
471 | */ |
511 | */ |
472 | |
512 | |
473 | coro_init_stacks (); |
513 | coro_init_stacks (); |
474 | |
514 | |
475 | PL_curcop = 0; |
515 | PL_curcop = &PL_compiling; |
476 | PL_in_eval = 0; |
516 | PL_in_eval = EVAL_NULL; |
477 | PL_curpm = 0; |
517 | PL_curpm = 0; |
|
|
518 | PL_localizing = 0; |
|
|
519 | PL_dirty = 0; |
|
|
520 | PL_restartop = 0; |
478 | |
521 | |
479 | { |
522 | { |
480 | dSP; |
523 | dSP; |
481 | LOGOP myop; |
524 | LOGOP myop; |
482 | |
525 | |
483 | /* I have no idea why this is needed, but it is */ |
|
|
484 | PUSHMARK (SP); |
|
|
485 | |
|
|
486 | SvREFCNT_dec (GvAV (PL_defgv)); |
526 | SvREFCNT_dec (GvAV (PL_defgv)); |
487 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
527 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
488 | |
528 | |
489 | Zero (&myop, 1, LOGOP); |
529 | Zero (&myop, 1, LOGOP); |
490 | myop.op_next = Nullop; |
530 | myop.op_next = Nullop; |
491 | myop.op_flags = OPf_WANT_VOID; |
531 | myop.op_flags = OPf_WANT_VOID; |
492 | |
532 | |
493 | PL_op = (OP *)&myop; |
|
|
494 | |
|
|
495 | PUSHMARK (SP); |
533 | PUSHMARK (SP); |
496 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
534 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
497 | PUTBACK; |
535 | PUTBACK; |
|
|
536 | PL_op = (OP *)&myop; |
498 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
537 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
499 | SPAGAIN; |
538 | SPAGAIN; |
500 | |
|
|
501 | ENTER; /* necessary e.g. for dounwind */ |
|
|
502 | } |
539 | } |
|
|
540 | |
|
|
541 | ENTER; /* necessary e.g. for dounwind */ |
503 | } |
542 | } |
504 | |
543 | |
505 | static void |
544 | static void |
506 | free_coro_mortal () |
545 | free_coro_mortal () |
507 | { |
546 | { |
… | |
… | |
510 | SvREFCNT_dec (coro_mortal); |
549 | SvREFCNT_dec (coro_mortal); |
511 | coro_mortal = 0; |
550 | coro_mortal = 0; |
512 | } |
551 | } |
513 | } |
552 | } |
514 | |
553 | |
|
|
554 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
515 | static void NOINLINE |
555 | static void NOINLINE |
516 | prepare_cctx (coro_cctx *cctx) |
556 | prepare_cctx (coro_cctx *cctx) |
517 | { |
557 | { |
518 | dSP; |
558 | dSP; |
519 | LOGOP myop; |
559 | LOGOP myop; |
520 | |
560 | |
521 | Zero (&myop, 1, LOGOP); |
561 | Zero (&myop, 1, LOGOP); |
522 | myop.op_next = PL_op; |
562 | myop.op_next = PL_op; |
523 | myop.op_flags = OPf_WANT_VOID; |
563 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
524 | |
|
|
525 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
|
|
526 | |
564 | |
527 | PUSHMARK (SP); |
565 | PUSHMARK (SP); |
|
|
566 | EXTEND (SP, 2); |
|
|
567 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
528 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
568 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
529 | PUTBACK; |
569 | PUTBACK; |
|
|
570 | PL_op = (OP *)&myop; |
530 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
571 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
531 | SPAGAIN; |
572 | SPAGAIN; |
532 | } |
573 | } |
533 | |
574 | |
534 | static void |
575 | static void |
535 | coro_run (void *arg) |
576 | coro_run (void *arg) |
536 | { |
577 | { |
537 | /* coro_run is the alternative epilogue of transfer() */ |
578 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
538 | UNLOCK; |
579 | UNLOCK; |
539 | |
580 | |
540 | /* |
581 | /* |
541 | * this is a _very_ stripped down perl interpreter ;) |
582 | * this is a _very_ stripped down perl interpreter ;) |
542 | */ |
583 | */ |
… | |
… | |
544 | |
585 | |
545 | /* inject call to cctx_init */ |
586 | /* inject call to cctx_init */ |
546 | prepare_cctx ((coro_cctx *)arg); |
587 | prepare_cctx ((coro_cctx *)arg); |
547 | |
588 | |
548 | /* 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; |
549 | perl_run (PL_curinterp); |
591 | perl_run (PL_curinterp); |
550 | |
592 | |
551 | 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); |
552 | abort (); |
594 | abort (); |
553 | } |
595 | } |
… | |
… | |
557 | { |
599 | { |
558 | coro_cctx *cctx; |
600 | coro_cctx *cctx; |
559 | |
601 | |
560 | ++cctx_count; |
602 | ++cctx_count; |
561 | |
603 | |
562 | New (0, cctx, 1, coro_cctx); |
604 | Newz (0, cctx, 1, coro_cctx); |
563 | |
605 | |
564 | #if HAVE_MMAP |
606 | #if HAVE_MMAP |
565 | |
607 | |
566 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
608 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
567 | /* mmap suppsedly does allocate-on-write for us */ |
609 | /* mmap supposedly does allocate-on-write for us */ |
568 | 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); |
569 | |
611 | |
570 | if (cctx->sptr == (void *)-1) |
612 | if (cctx->sptr == (void *)-1) |
571 | { |
613 | { |
572 | perror ("FATAL: unable to mmap stack for coroutine"); |
614 | perror ("FATAL: unable to mmap stack for coroutine"); |
… | |
… | |
601 | |
643 | |
602 | return cctx; |
644 | return cctx; |
603 | } |
645 | } |
604 | |
646 | |
605 | static void |
647 | static void |
606 | cctx_free (coro_cctx *cctx) |
648 | cctx_destroy (coro_cctx *cctx) |
607 | { |
649 | { |
608 | if (!cctx) |
650 | if (!cctx) |
609 | return; |
651 | return; |
610 | |
652 | |
611 | --cctx_count; |
653 | --cctx_count; |
… | |
… | |
628 | { |
670 | { |
629 | coro_cctx *cctx; |
671 | coro_cctx *cctx; |
630 | |
672 | |
631 | if (cctx_first) |
673 | if (cctx_first) |
632 | { |
674 | { |
633 | --cctx_idle; |
|
|
634 | cctx = cctx_first; |
675 | cctx = cctx_first; |
635 | cctx_first = cctx->next; |
676 | cctx_first = cctx->next; |
|
|
677 | --cctx_idle; |
636 | } |
678 | } |
637 | else |
679 | else |
638 | { |
680 | { |
639 | cctx = cctx_new (); |
681 | cctx = cctx_new (); |
640 | PL_op = PL_op->op_next; |
682 | PL_op = PL_op->op_next; |
… | |
… | |
644 | } |
686 | } |
645 | |
687 | |
646 | static void |
688 | static void |
647 | cctx_put (coro_cctx *cctx) |
689 | cctx_put (coro_cctx *cctx) |
648 | { |
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 | |
649 | ++cctx_idle; |
702 | ++cctx_idle; |
650 | cctx->next = cctx_first; |
703 | cctx->next = cctx_first; |
651 | cctx_first = cctx; |
704 | cctx_first = cctx; |
652 | } |
705 | } |
653 | |
706 | |
654 | /* never call directly, always through the coro_state_transfer global variable */ |
707 | /* never call directly, always through the coro_state_transfer global variable */ |
655 | static void NOINLINE |
708 | static void NOINLINE |
656 | transfer (struct coro *prev, struct coro *next, int flags) |
709 | transfer (struct coro *prev, struct coro *next) |
657 | { |
710 | { |
658 | dSTACKLEVEL; |
711 | dSTACKLEVEL; |
659 | |
712 | |
660 | /* sometimes transfer is only called to set idle_sp */ |
713 | /* sometimes transfer is only called to set idle_sp */ |
661 | if (flags == TRANSFER_SET_STACKLEVEL) |
714 | if (!next) |
|
|
715 | { |
662 | ((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 | } |
663 | else if (prev != next) |
719 | else if (prev != next) |
664 | { |
720 | { |
665 | coro_cctx *prev__cctx; |
721 | coro_cctx *prev__cctx; |
666 | |
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 | |
667 | LOCK; |
742 | LOCK; |
668 | |
743 | |
669 | if (next->mainstack) |
744 | if (next->flags & CF_NEW) |
670 | { |
745 | { |
671 | /* coroutine already started */ |
746 | /* need to start coroutine */ |
672 | SAVE (prev, flags); |
747 | next->flags &= ~CF_NEW; |
673 | 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); |
674 | } |
754 | } |
675 | else |
755 | else |
676 | { |
756 | { |
677 | /* need to start coroutine */ |
757 | /* coroutine already started */ |
678 | /* first get rid of the old state */ |
758 | save_perl (prev); |
679 | SAVE (prev, -1); |
759 | load_perl (next); |
680 | /* setup coroutine call */ |
|
|
681 | setup_coro (next); |
|
|
682 | /* need a stack */ |
|
|
683 | next->cctx = 0; |
|
|
684 | } |
760 | } |
685 | |
|
|
686 | if (!prev->cctx) |
|
|
687 | /* create a new empty context */ |
|
|
688 | Newz (0, prev->cctx, 1, coro_cctx); |
|
|
689 | |
761 | |
690 | prev__cctx = prev->cctx; |
762 | prev__cctx = prev->cctx; |
691 | |
763 | |
692 | /* possibly "free" the cctx */ |
764 | /* possibly "free" the cctx */ |
693 | if (prev__cctx->idle_sp == STACKLEVEL) |
765 | if (prev__cctx->idle_sp == STACKLEVEL) |
694 | { |
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 | |
695 | cctx_put (prev__cctx); |
772 | cctx_put (prev__cctx); |
696 | prev->cctx = 0; |
773 | prev__cctx->inuse = 0; |
697 | } |
774 | } |
698 | |
775 | |
699 | if (!next->cctx) |
776 | if (!next->cctx) |
|
|
777 | { |
700 | next->cctx = cctx_get (); |
778 | next->cctx = cctx_get (); |
|
|
779 | assert (!next->cctx->inuse); |
|
|
780 | next->cctx->inuse = 1; |
|
|
781 | } |
701 | |
782 | |
702 | if (prev__cctx != next->cctx) |
783 | if (prev__cctx != next->cctx) |
703 | { |
784 | { |
704 | prev__cctx->top_env = PL_top_env; |
785 | prev__cctx->top_env = PL_top_env; |
705 | PL_top_env = next->cctx->top_env; |
786 | PL_top_env = next->cctx->top_env; |
… | |
… | |
713 | } |
794 | } |
714 | |
795 | |
715 | struct transfer_args |
796 | struct transfer_args |
716 | { |
797 | { |
717 | struct coro *prev, *next; |
798 | struct coro *prev, *next; |
718 | int flags; |
|
|
719 | }; |
799 | }; |
720 | |
800 | |
721 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) |
801 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
722 | |
802 | |
723 | static void |
803 | static void |
724 | coro_state_destroy (struct coro *coro) |
804 | coro_state_destroy (struct coro *coro) |
725 | { |
805 | { |
726 | if (coro->refcnt--) |
806 | if (coro->refcnt--) |
727 | return; |
807 | return; |
728 | |
808 | |
729 | if (coro->mainstack && coro->mainstack != main_mainstack) |
809 | if (coro->mainstack && coro->mainstack != main_mainstack) |
730 | { |
810 | { |
731 | struct coro temp; |
811 | struct coro temp; |
|
|
812 | Zero (&temp, 1, struct coro); |
|
|
813 | temp.save = CORO_SAVE_ALL; |
732 | |
814 | |
733 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
815 | if (coro->flags & CF_RUNNING) |
734 | LOAD (coro); |
816 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
817 | |
|
|
818 | save_perl (&temp); |
|
|
819 | load_perl (coro); |
735 | |
820 | |
736 | coro_destroy_stacks (); |
821 | coro_destroy_stacks (); |
737 | |
822 | |
738 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
823 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
739 | |
824 | |
740 | coro->mainstack = 0; |
825 | coro->mainstack = 0; |
741 | } |
826 | } |
742 | |
827 | |
743 | cctx_free (coro->cctx); |
828 | cctx_destroy (coro->cctx); |
744 | SvREFCNT_dec (coro->args); |
829 | SvREFCNT_dec (coro->args); |
745 | Safefree (coro); |
830 | Safefree (coro); |
746 | } |
831 | } |
747 | |
832 | |
748 | static int |
833 | static int |
… | |
… | |
798 | assert (mg->mg_type == PERL_MAGIC_ext); |
883 | assert (mg->mg_type == PERL_MAGIC_ext); |
799 | return (struct coro *)mg->mg_ptr; |
884 | return (struct coro *)mg->mg_ptr; |
800 | } |
885 | } |
801 | |
886 | |
802 | static void |
887 | static void |
803 | 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) |
804 | { |
889 | { |
805 | ta->prev = SvSTATE (prev); |
890 | ta->prev = SvSTATE (prev_sv); |
806 | ta->next = SvSTATE (next); |
891 | ta->next = SvSTATE (next_sv); |
807 | ta->flags = flags; |
|
|
808 | } |
892 | } |
809 | |
893 | |
810 | static void |
894 | static void |
811 | api_transfer (SV *prev, SV *next, int flags) |
895 | api_transfer (SV *prev_sv, SV *next_sv) |
812 | { |
896 | { |
813 | dTHX; |
|
|
814 | struct transfer_args ta; |
897 | struct transfer_args ta; |
815 | |
898 | |
816 | prepare_transfer (&ta, prev, next, flags); |
899 | prepare_transfer (&ta, prev_sv, next_sv); |
817 | 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; |
818 | } |
913 | } |
819 | |
914 | |
820 | /** Coro ********************************************************************/ |
915 | /** Coro ********************************************************************/ |
821 | |
916 | |
822 | #define PRIO_MAX 3 |
917 | #define PRIO_MAX 3 |
… | |
… | |
825 | #define PRIO_LOW -1 |
920 | #define PRIO_LOW -1 |
826 | #define PRIO_IDLE -3 |
921 | #define PRIO_IDLE -3 |
827 | #define PRIO_MIN -4 |
922 | #define PRIO_MIN -4 |
828 | |
923 | |
829 | /* for Coro.pm */ |
924 | /* for Coro.pm */ |
830 | static GV *coro_current, *coro_idle; |
925 | static SV *coro_current; |
831 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
926 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
832 | static int coro_nready; |
927 | static int coro_nready; |
833 | |
928 | |
834 | static void |
929 | static void |
835 | coro_enq (SV *sv) |
930 | coro_enq (SV *coro_sv) |
836 | { |
931 | { |
837 | int prio; |
|
|
838 | |
|
|
839 | if (SvTYPE (sv) != SVt_PVHV) |
|
|
840 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
841 | |
|
|
842 | prio = SvSTATE (sv)->prio; |
|
|
843 | |
|
|
844 | av_push (coro_ready [prio - PRIO_MIN], sv); |
932 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
845 | coro_nready++; |
933 | coro_nready++; |
846 | } |
934 | } |
847 | |
935 | |
848 | static SV * |
936 | static SV * |
849 | coro_deq (int min_prio) |
937 | coro_deq (int min_prio) |
… | |
… | |
862 | } |
950 | } |
863 | |
951 | |
864 | return 0; |
952 | return 0; |
865 | } |
953 | } |
866 | |
954 | |
867 | static void |
955 | static int |
868 | api_ready (SV *coro) |
956 | api_ready (SV *coro_sv) |
869 | { |
957 | { |
870 | dTHX; |
958 | struct coro *coro; |
871 | |
959 | |
872 | if (SvROK (coro)) |
960 | if (SvROK (coro_sv)) |
873 | 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; |
874 | |
974 | |
875 | LOCK; |
975 | LOCK; |
876 | coro_enq (SvREFCNT_inc (coro)); |
976 | coro_enq (SvREFCNT_inc (coro_sv)); |
877 | 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; |
878 | } |
986 | } |
879 | |
987 | |
880 | static void |
988 | static void |
881 | prepare_schedule (struct transfer_args *ta) |
989 | prepare_schedule (struct transfer_args *ta) |
882 | { |
990 | { |
883 | SV *current, *prev, *next; |
991 | SV *prev, *next; |
884 | |
|
|
885 | current = GvSV (coro_current); |
|
|
886 | |
992 | |
887 | for (;;) |
993 | for (;;) |
888 | { |
994 | { |
889 | LOCK; |
995 | LOCK; |
890 | next = coro_deq (PRIO_MIN); |
996 | next = coro_deq (PRIO_MIN); |
… | |
… | |
899 | ENTER; |
1005 | ENTER; |
900 | SAVETMPS; |
1006 | SAVETMPS; |
901 | |
1007 | |
902 | PUSHMARK (SP); |
1008 | PUSHMARK (SP); |
903 | PUTBACK; |
1009 | PUTBACK; |
904 | call_sv (GvSV (coro_idle), G_DISCARD); |
1010 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
905 | |
1011 | |
906 | FREETMPS; |
1012 | FREETMPS; |
907 | LEAVE; |
1013 | LEAVE; |
908 | } |
1014 | } |
909 | } |
1015 | } |
910 | |
1016 | |
911 | prev = SvRV (current); |
1017 | prev = SvRV (coro_current); |
912 | SvRV (current) = next; |
1018 | SvRV_set (coro_current, next); |
913 | |
1019 | |
914 | /* free this only after the transfer */ |
1020 | /* free this only after the transfer */ |
915 | LOCK; |
1021 | LOCK; |
916 | free_coro_mortal (); |
1022 | free_coro_mortal (); |
917 | UNLOCK; |
1023 | UNLOCK; |
918 | coro_mortal = prev; |
1024 | coro_mortal = prev; |
919 | |
1025 | |
|
|
1026 | assert (!SvROK(prev));//D |
|
|
1027 | assert (!SvROK(next));//D |
|
|
1028 | |
920 | ta->prev = SvSTATE (prev); |
1029 | ta->prev = SvSTATE (prev); |
921 | ta->next = SvSTATE (next); |
1030 | ta->next = SvSTATE (next); |
922 | ta->flags = TRANSFER_SAVE_ALL; |
1031 | |
|
|
1032 | assert (ta->next->flags & CF_READY); |
|
|
1033 | ta->next->flags &= ~CF_READY; |
923 | } |
1034 | } |
924 | |
1035 | |
925 | static void |
1036 | static void |
926 | prepare_cede (struct transfer_args *ta) |
1037 | prepare_cede (struct transfer_args *ta) |
927 | { |
1038 | { |
928 | LOCK; |
1039 | api_ready (coro_current); |
929 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
930 | UNLOCK; |
|
|
931 | |
1040 | |
932 | prepare_schedule (ta); |
1041 | prepare_schedule (ta); |
933 | } |
1042 | } |
934 | |
1043 | |
935 | static void |
1044 | static void |
936 | api_schedule (void) |
1045 | api_schedule (void) |
937 | { |
1046 | { |
938 | dTHX; |
|
|
939 | struct transfer_args ta; |
1047 | struct transfer_args ta; |
940 | |
1048 | |
941 | prepare_schedule (&ta); |
1049 | prepare_schedule (&ta); |
942 | TRANSFER (ta); |
1050 | TRANSFER (ta); |
943 | } |
1051 | } |
944 | |
1052 | |
945 | static void |
1053 | static void |
946 | api_cede (void) |
1054 | api_cede (void) |
947 | { |
1055 | { |
948 | dTHX; |
|
|
949 | struct transfer_args ta; |
1056 | struct transfer_args ta; |
950 | |
1057 | |
951 | prepare_cede (&ta); |
1058 | prepare_cede (&ta); |
952 | TRANSFER (ta); |
1059 | TRANSFER (ta); |
953 | } |
1060 | } |
… | |
… | |
963 | #endif |
1070 | #endif |
964 | BOOT_PAGESIZE; |
1071 | BOOT_PAGESIZE; |
965 | |
1072 | |
966 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1073 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
967 | |
1074 | |
968 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
1075 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
969 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
1076 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
970 | 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)); |
971 | |
1080 | |
972 | main_mainstack = PL_mainstack; |
1081 | main_mainstack = PL_mainstack; |
973 | |
1082 | |
974 | coroapi.ver = CORO_API_VERSION; |
1083 | coroapi.ver = CORO_API_VERSION; |
975 | coroapi.transfer = api_transfer; |
1084 | coroapi.transfer = api_transfer; |
… | |
… | |
985 | HV *hv; |
1094 | HV *hv; |
986 | int i; |
1095 | int i; |
987 | |
1096 | |
988 | Newz (0, coro, 1, struct coro); |
1097 | Newz (0, coro, 1, struct coro); |
989 | coro->args = newAV (); |
1098 | coro->args = newAV (); |
|
|
1099 | coro->save = CORO_SAVE_ALL; |
|
|
1100 | coro->flags = CF_NEW; |
990 | |
1101 | |
991 | hv = newHV (); |
1102 | hv = newHV (); |
992 | 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; |
993 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1104 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
994 | |
1105 | |
995 | for (i = 1; i < items; i++) |
1106 | for (i = 1; i < items; i++) |
996 | av_push (coro->args, newSVsv (ST (i))); |
1107 | av_push (coro->args, newSVsv (ST (i))); |
997 | } |
1108 | } |
998 | 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: |
999 | RETVAL |
1117 | RETVAL |
1000 | |
1118 | |
1001 | void |
1119 | void |
1002 | _set_stacklevel (...) |
1120 | _set_stacklevel (...) |
1003 | ALIAS: |
1121 | ALIAS: |
1004 | Coro::State::transfer = 1 |
1122 | Coro::State::transfer = 1 |
1005 | Coro::schedule = 2 |
1123 | Coro::schedule = 2 |
1006 | Coro::cede = 3 |
1124 | Coro::cede = 3 |
1007 | Coro::Cont::yield = 4 |
|
|
1008 | CODE: |
1125 | CODE: |
1009 | { |
1126 | { |
1010 | struct transfer_args ta; |
1127 | struct transfer_args ta; |
1011 | |
1128 | |
1012 | switch (ix) |
1129 | switch (ix) |
1013 | { |
1130 | { |
1014 | case 0: |
1131 | case 0: |
1015 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1132 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1016 | ta.next = 0; |
1133 | ta.next = 0; |
1017 | ta.flags = TRANSFER_SET_STACKLEVEL; |
|
|
1018 | break; |
1134 | break; |
1019 | |
1135 | |
1020 | case 1: |
1136 | case 1: |
1021 | if (items != 3) |
1137 | if (items != 2) |
1022 | 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); |
1023 | |
1139 | |
1024 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1140 | prepare_transfer (&ta, ST (0), ST (1)); |
1025 | break; |
1141 | break; |
1026 | |
1142 | |
1027 | case 2: |
1143 | case 2: |
1028 | prepare_schedule (&ta); |
1144 | prepare_schedule (&ta); |
1029 | break; |
1145 | break; |
1030 | |
1146 | |
1031 | case 3: |
1147 | case 3: |
1032 | prepare_cede (&ta); |
1148 | prepare_cede (&ta); |
1033 | break; |
1149 | break; |
1034 | |
|
|
1035 | case 4: |
|
|
1036 | { |
|
|
1037 | SV *yieldstack; |
|
|
1038 | SV *sv; |
|
|
1039 | AV *defav = GvAV (PL_defgv); |
|
|
1040 | |
|
|
1041 | yieldstack = *hv_fetch ( |
|
|
1042 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1043 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1044 | 0 |
|
|
1045 | ); |
|
|
1046 | |
|
|
1047 | /* set up @_ -- ugly */ |
|
|
1048 | av_clear (defav); |
|
|
1049 | av_fill (defav, items - 1); |
|
|
1050 | while (items--) |
|
|
1051 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1052 | |
|
|
1053 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1054 | ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1055 | ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1056 | ta.flags = 0; |
|
|
1057 | SvREFCNT_dec (sv); |
|
|
1058 | } |
|
|
1059 | break; |
|
|
1060 | |
|
|
1061 | } |
1150 | } |
1062 | |
1151 | |
1063 | TRANSFER (ta); |
1152 | TRANSFER (ta); |
1064 | } |
1153 | } |
1065 | |
1154 | |
… | |
… | |
1109 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1198 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1110 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1199 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1111 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1200 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1112 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1201 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1113 | |
1202 | |
1114 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1203 | coro_current = get_sv ("Coro::current", FALSE); |
1115 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1204 | SvREADONLY_on (coro_current); |
1116 | |
1205 | |
1117 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1206 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1118 | coro_ready[i] = newAV (); |
1207 | coro_ready[i] = newAV (); |
1119 | |
1208 | |
1120 | { |
1209 | { |
1121 | SV *sv = perl_get_sv("Coro::API", 1); |
1210 | SV *sv = perl_get_sv("Coro::API", 1); |
1122 | |
1211 | |
1123 | coroapi.schedule = api_schedule; |
1212 | coroapi.schedule = api_schedule; |
|
|
1213 | coroapi.save = api_save; |
1124 | coroapi.cede = api_cede; |
1214 | coroapi.cede = api_cede; |
1125 | coroapi.ready = api_ready; |
1215 | coroapi.ready = api_ready; |
|
|
1216 | coroapi.is_ready = api_is_ready; |
1126 | coroapi.nready = &coro_nready; |
1217 | coroapi.nready = &coro_nready; |
1127 | coroapi.current = coro_current; |
1218 | coroapi.current = coro_current; |
1128 | |
1219 | |
1129 | GCoroAPI = &coroapi; |
1220 | GCoroAPI = &coroapi; |
1130 | sv_setiv (sv, (IV)&coroapi); |
1221 | sv_setiv (sv, (IV)&coroapi); |
1131 | SvREADONLY_on (sv); |
1222 | SvREADONLY_on (sv); |
1132 | } |
1223 | } |
1133 | } |
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))); |
1134 | |
1232 | |
1135 | int |
1233 | int |
1136 | prio (Coro::State coro, int newprio = 0) |
1234 | prio (Coro::State coro, int newprio = 0) |
1137 | ALIAS: |
1235 | ALIAS: |
1138 | nice = 1 |
1236 | nice = 1 |
… | |
… | |
1150 | |
1248 | |
1151 | coro->prio = newprio; |
1249 | coro->prio = newprio; |
1152 | } |
1250 | } |
1153 | } |
1251 | } |
1154 | |
1252 | |
1155 | void |
1253 | SV * |
1156 | ready (SV *self) |
1254 | ready (SV *self) |
1157 | PROTOTYPE: $ |
1255 | PROTOTYPE: $ |
1158 | CODE: |
1256 | CODE: |
1159 | 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 |
1160 | |
1268 | |
1161 | int |
1269 | int |
1162 | nready (...) |
1270 | nready (...) |
1163 | PROTOTYPE: |
1271 | PROTOTYPE: |
1164 | CODE: |
1272 | CODE: |
… | |
… | |
1170 | |
1278 | |
1171 | SV * |
1279 | SV * |
1172 | _get_state () |
1280 | _get_state () |
1173 | CODE: |
1281 | CODE: |
1174 | { |
1282 | { |
1175 | struct { |
1283 | struct io_state *data; |
1176 | int errorno; |
|
|
1177 | int laststype; |
|
|
1178 | int laststatval; |
|
|
1179 | Stat_t statcache; |
|
|
1180 | } data; |
|
|
1181 | |
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 | |
1182 | data.errorno = errno; |
1290 | data->errorno = errno; |
1183 | data.laststype = PL_laststype; |
1291 | data->laststype = PL_laststype; |
1184 | data.laststatval = PL_laststatval; |
1292 | data->laststatval = PL_laststatval; |
1185 | data.statcache = PL_statcache; |
1293 | data->statcache = PL_statcache; |
1186 | |
|
|
1187 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1188 | } |
1294 | } |
1189 | OUTPUT: |
1295 | OUTPUT: |
1190 | RETVAL |
1296 | RETVAL |
1191 | |
1297 | |
1192 | void |
1298 | void |
1193 | _set_state (char *data_) |
1299 | _set_state (char *data_) |
1194 | PROTOTYPE: $ |
1300 | PROTOTYPE: $ |
1195 | CODE: |
1301 | CODE: |
1196 | { |
1302 | { |
1197 | struct { |
1303 | struct io_state *data = (void *)data_; |
1198 | int errorno; |
|
|
1199 | int laststype; |
|
|
1200 | int laststatval; |
|
|
1201 | Stat_t statcache; |
|
|
1202 | } *data = (void *)data_; |
|
|
1203 | |
1304 | |
1204 | errno = data->errorno; |
1305 | errno = data->errorno; |
1205 | PL_laststype = data->laststype; |
1306 | PL_laststype = data->laststype; |
1206 | PL_laststatval = data->laststatval; |
1307 | PL_laststatval = data->laststatval; |
1207 | PL_statcache = data->statcache; |
1308 | PL_statcache = data->statcache; |
1208 | } |
1309 | } |
|
|
1310 | |