… | |
… | |
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 | |
… | |
… | |
269 | } |
296 | } |
270 | |
297 | |
271 | #define SB do { |
298 | #define SB do { |
272 | #define SE } while (0) |
299 | #define SE } while (0) |
273 | |
300 | |
274 | #define LOAD(state) load_state((state)); |
|
|
275 | #define SAVE(state,flags) save_state((state),(flags)); |
|
|
276 | |
|
|
277 | #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 |
278 | |
302 | |
279 | static void |
303 | static void |
280 | load_state(Coro__State c) |
304 | load_perl (Coro__State c) |
281 | { |
305 | { |
282 | #define VAR(name,type) PL_ ## name = c->name; |
306 | #define VAR(name,type) PL_ ## name = c->name; |
283 | # include "state.h" |
307 | # include "state.h" |
284 | #undef VAR |
308 | #undef VAR |
285 | |
309 | |
286 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
310 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
287 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
311 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
288 | 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 | } |
289 | |
324 | |
290 | { |
325 | { |
291 | dSP; |
326 | dSP; |
292 | CV *cv; |
327 | CV *cv; |
293 | |
328 | |
… | |
… | |
302 | PUTBACK; |
337 | PUTBACK; |
303 | } |
338 | } |
304 | } |
339 | } |
305 | |
340 | |
306 | static void |
341 | static void |
307 | save_state(Coro__State c, int flags) |
342 | save_perl (Coro__State c) |
308 | { |
343 | { |
309 | { |
344 | { |
310 | dSP; |
345 | dSP; |
311 | I32 cxix = cxstack_ix; |
346 | I32 cxix = cxstack_ix; |
312 | PERL_CONTEXT *ccstk = cxstack; |
347 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
315 | /* |
350 | /* |
316 | * the worst thing you can imagine happens first - we have to save |
351 | * the worst thing you can imagine happens first - we have to save |
317 | * (and reinitialize) all cv's in the whole callchain :( |
352 | * (and reinitialize) all cv's in the whole callchain :( |
318 | */ |
353 | */ |
319 | |
354 | |
|
|
355 | EXTEND (SP, 3 + 1); |
320 | PUSHs (Nullsv); |
356 | PUSHs (Nullsv); |
321 | /* this loop was inspired by pp_caller */ |
357 | /* this loop was inspired by pp_caller */ |
322 | for (;;) |
358 | for (;;) |
323 | { |
359 | { |
324 | while (cxix >= 0) |
360 | while (cxix >= 0) |
325 | { |
361 | { |
326 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
362 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
327 | |
363 | |
328 | if (CxTYPE(cx) == CXt_SUB) |
364 | if (CxTYPE (cx) == CXt_SUB) |
329 | { |
365 | { |
330 | CV *cv = cx->blk_sub.cv; |
366 | CV *cv = cx->blk_sub.cv; |
331 | |
367 | |
332 | if (CvDEPTH (cv)) |
368 | if (CvDEPTH (cv)) |
333 | { |
369 | { |
334 | EXTEND (SP, 3); |
370 | EXTEND (SP, 3); |
335 | |
|
|
336 | PUSHs ((SV *)CvPADLIST(cv)); |
371 | PUSHs ((SV *)CvPADLIST (cv)); |
337 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
372 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
338 | PUSHs ((SV *)cv); |
373 | PUSHs ((SV *)cv); |
339 | |
374 | |
340 | CvDEPTH (cv) = 0; |
375 | CvDEPTH (cv) = 0; |
341 | get_padlist (cv); |
376 | get_padlist (cv); |
342 | } |
377 | } |
343 | } |
378 | } |
344 | #ifdef CXt_FORMAT |
|
|
345 | else if (CxTYPE(cx) == CXt_FORMAT) |
|
|
346 | { |
|
|
347 | /* I never used formats, so how should I know how these are implemented? */ |
|
|
348 | /* my bold guess is as a simple, plain sub... */ |
|
|
349 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
|
|
350 | } |
|
|
351 | #endif |
|
|
352 | } |
379 | } |
353 | |
380 | |
354 | if (top_si->si_type == PERLSI_MAIN) |
381 | if (top_si->si_type == PERLSI_MAIN) |
355 | break; |
382 | break; |
356 | |
383 | |
… | |
… | |
360 | } |
387 | } |
361 | |
388 | |
362 | PUTBACK; |
389 | PUTBACK; |
363 | } |
390 | } |
364 | |
391 | |
365 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
392 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
366 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
393 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
367 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
394 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
395 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
368 | |
396 | |
369 | #define VAR(name,type)c->name = PL_ ## name; |
397 | #define VAR(name,type)c->name = PL_ ## name; |
370 | # include "state.h" |
398 | # include "state.h" |
371 | #undef VAR |
399 | #undef VAR |
372 | } |
400 | } |
… | |
… | |
375 | * allocate various perl stacks. This is an exact copy |
403 | * allocate various perl stacks. This is an exact copy |
376 | * of perl.c:init_stacks, except that it uses less memory |
404 | * of perl.c:init_stacks, except that it uses less memory |
377 | * on the (sometimes correct) assumption that coroutines do |
405 | * on the (sometimes correct) assumption that coroutines do |
378 | * not usually need a lot of stackspace. |
406 | * not usually need a lot of stackspace. |
379 | */ |
407 | */ |
|
|
408 | #if USE_PERL_INIT_STACKS |
|
|
409 | # define coro_init_stacks init_stacks |
|
|
410 | #else |
|
|
411 | |
380 | static void |
412 | static void |
381 | coro_init_stacks () |
413 | coro_init_stacks () |
382 | { |
414 | { |
383 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
415 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
384 | PL_curstackinfo->si_type = PERLSI_MAIN; |
416 | PL_curstackinfo->si_type = PERLSI_MAIN; |
385 | PL_curstack = PL_curstackinfo->si_stack; |
417 | PL_curstack = PL_curstackinfo->si_stack; |
386 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
418 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
387 | |
419 | |
388 | PL_stack_base = AvARRAY(PL_curstack); |
420 | PL_stack_base = AvARRAY(PL_curstack); |
389 | PL_stack_sp = PL_stack_base; |
421 | PL_stack_sp = PL_stack_base; |
390 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
422 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
391 | |
423 | |
392 | New(50,PL_tmps_stack,96,SV*); |
424 | New(50,PL_tmps_stack,128,SV*); |
393 | PL_tmps_floor = -1; |
425 | PL_tmps_floor = -1; |
394 | PL_tmps_ix = -1; |
426 | PL_tmps_ix = -1; |
395 | PL_tmps_max = 96; |
427 | PL_tmps_max = 128; |
396 | |
428 | |
397 | New(54,PL_markstack,16,I32); |
429 | New(54,PL_markstack,32,I32); |
398 | PL_markstack_ptr = PL_markstack; |
430 | PL_markstack_ptr = PL_markstack; |
399 | PL_markstack_max = PL_markstack + 16; |
431 | PL_markstack_max = PL_markstack + 32; |
400 | |
432 | |
401 | #ifdef SET_MARK_OFFSET |
433 | #ifdef SET_MARK_OFFSET |
402 | SET_MARK_OFFSET; |
434 | SET_MARK_OFFSET; |
403 | #endif |
435 | #endif |
404 | |
436 | |
405 | New(54,PL_scopestack,16,I32); |
437 | New(54,PL_scopestack,32,I32); |
406 | PL_scopestack_ix = 0; |
438 | PL_scopestack_ix = 0; |
407 | PL_scopestack_max = 16; |
439 | PL_scopestack_max = 32; |
408 | |
440 | |
409 | New(54,PL_savestack,96,ANY); |
441 | New(54,PL_savestack,64,ANY); |
410 | PL_savestack_ix = 0; |
442 | PL_savestack_ix = 0; |
411 | PL_savestack_max = 96; |
443 | PL_savestack_max = 64; |
412 | |
444 | |
413 | #if !PERL_VERSION_ATLEAST (5,9,0) |
445 | #if !PERL_VERSION_ATLEAST (5,9,0) |
414 | New(54,PL_retstack,8,OP*); |
446 | New(54,PL_retstack,16,OP*); |
415 | PL_retstack_ix = 0; |
447 | PL_retstack_ix = 0; |
416 | PL_retstack_max = 8; |
448 | PL_retstack_max = 16; |
417 | #endif |
449 | #endif |
418 | } |
450 | } |
419 | |
451 | |
420 | /* |
452 | /* |
421 | * destroy the stacks, the callchain etc... |
453 | * destroy the stacks, the callchain etc... |
… | |
… | |
423 | static void |
455 | static void |
424 | coro_destroy_stacks () |
456 | coro_destroy_stacks () |
425 | { |
457 | { |
426 | if (!IN_DESTRUCT) |
458 | if (!IN_DESTRUCT) |
427 | { |
459 | { |
428 | /* is this ugly, I ask? */ |
460 | /* restore all saved variables and stuff */ |
429 | LEAVE_SCOPE (0); |
461 | LEAVE_SCOPE (0); |
|
|
462 | assert (PL_tmps_floor == -1); |
430 | |
463 | |
431 | /* sure it is, but more important: is it correct?? :/ */ |
464 | /* free all temporaries */ |
432 | FREETMPS; |
465 | FREETMPS; |
|
|
466 | assert (PL_tmps_ix == -1); |
433 | |
467 | |
434 | /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ |
468 | POPSTACK_TO (PL_mainstack); |
435 | } |
469 | } |
436 | |
470 | |
437 | while (PL_curstackinfo->si_next) |
471 | while (PL_curstackinfo->si_next) |
438 | PL_curstackinfo = PL_curstackinfo->si_next; |
472 | PL_curstackinfo = PL_curstackinfo->si_next; |
439 | |
473 | |
440 | while (PL_curstackinfo) |
474 | while (PL_curstackinfo) |
441 | { |
475 | { |
442 | PERL_SI *p = PL_curstackinfo->si_prev; |
476 | PERL_SI *p = PL_curstackinfo->si_prev; |
443 | |
477 | |
444 | { /*D*//*remove*/ |
|
|
445 | dSP; |
|
|
446 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
|
|
447 | PUTBACK; /* possibly superfluous */ |
|
|
448 | } |
|
|
449 | |
|
|
450 | if (!IN_DESTRUCT) |
478 | if (!IN_DESTRUCT) |
451 | { |
|
|
452 | dounwind (-1);/*D*//*remove*/ |
|
|
453 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
479 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
454 | } |
|
|
455 | |
480 | |
456 | Safefree (PL_curstackinfo->si_cxstack); |
481 | Safefree (PL_curstackinfo->si_cxstack); |
457 | Safefree (PL_curstackinfo); |
482 | Safefree (PL_curstackinfo); |
458 | PL_curstackinfo = p; |
483 | PL_curstackinfo = p; |
459 | } |
484 | } |
… | |
… | |
464 | Safefree (PL_savestack); |
489 | Safefree (PL_savestack); |
465 | #if !PERL_VERSION_ATLEAST (5,9,0) |
490 | #if !PERL_VERSION_ATLEAST (5,9,0) |
466 | Safefree (PL_retstack); |
491 | Safefree (PL_retstack); |
467 | #endif |
492 | #endif |
468 | } |
493 | } |
|
|
494 | #endif |
469 | |
495 | |
470 | static void |
496 | static void |
471 | setup_coro (struct coro *coro) |
497 | setup_coro (struct coro *coro) |
472 | { |
498 | { |
473 | /* |
499 | /* |
474 | * emulate part of the perl startup here. |
500 | * emulate part of the perl startup here. |
475 | */ |
501 | */ |
476 | |
502 | |
477 | coro_init_stacks (); |
503 | coro_init_stacks (); |
478 | |
504 | |
479 | PL_curcop = 0; |
505 | PL_curcop = &PL_compiling; |
480 | PL_in_eval = 0; |
506 | PL_in_eval = EVAL_NULL; |
481 | PL_curpm = 0; |
507 | PL_curpm = 0; |
|
|
508 | PL_localizing = 0; |
|
|
509 | PL_dirty = 0; |
|
|
510 | PL_restartop = 0; |
482 | |
511 | |
483 | { |
512 | { |
484 | dSP; |
513 | dSP; |
485 | LOGOP myop; |
514 | LOGOP myop; |
486 | |
515 | |
487 | /* I have no idea why this is needed, but it is */ |
|
|
488 | PUSHMARK (SP); |
|
|
489 | |
|
|
490 | SvREFCNT_dec (GvAV (PL_defgv)); |
516 | SvREFCNT_dec (GvAV (PL_defgv)); |
491 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
517 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
492 | |
518 | |
493 | Zero (&myop, 1, LOGOP); |
519 | Zero (&myop, 1, LOGOP); |
494 | myop.op_next = Nullop; |
520 | myop.op_next = Nullop; |
495 | myop.op_flags = OPf_WANT_VOID; |
521 | myop.op_flags = OPf_WANT_VOID; |
496 | |
522 | |
497 | PL_op = (OP *)&myop; |
|
|
498 | |
|
|
499 | PUSHMARK (SP); |
523 | PUSHMARK (SP); |
500 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
524 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
501 | PUTBACK; |
525 | PUTBACK; |
|
|
526 | PL_op = (OP *)&myop; |
502 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
527 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
503 | SPAGAIN; |
528 | SPAGAIN; |
504 | |
|
|
505 | ENTER; /* necessary e.g. for dounwind */ |
|
|
506 | } |
529 | } |
|
|
530 | |
|
|
531 | ENTER; /* necessary e.g. for dounwind */ |
507 | } |
532 | } |
508 | |
533 | |
509 | static void |
534 | static void |
510 | free_coro_mortal () |
535 | free_coro_mortal () |
511 | { |
536 | { |
… | |
… | |
514 | SvREFCNT_dec (coro_mortal); |
539 | SvREFCNT_dec (coro_mortal); |
515 | coro_mortal = 0; |
540 | coro_mortal = 0; |
516 | } |
541 | } |
517 | } |
542 | } |
518 | |
543 | |
|
|
544 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
519 | static void NOINLINE |
545 | static void NOINLINE |
520 | prepare_cctx (coro_cctx *cctx) |
546 | prepare_cctx (coro_cctx *cctx) |
521 | { |
547 | { |
522 | dSP; |
548 | dSP; |
523 | LOGOP myop; |
549 | LOGOP myop; |
524 | |
550 | |
525 | Zero (&myop, 1, LOGOP); |
551 | Zero (&myop, 1, LOGOP); |
526 | myop.op_next = PL_op; |
552 | myop.op_next = PL_op; |
527 | myop.op_flags = OPf_WANT_VOID; |
553 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
528 | |
|
|
529 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
|
|
530 | |
554 | |
531 | PUSHMARK (SP); |
555 | PUSHMARK (SP); |
|
|
556 | EXTEND (SP, 2); |
|
|
557 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
532 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
558 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
533 | PUTBACK; |
559 | PUTBACK; |
|
|
560 | PL_op = (OP *)&myop; |
534 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
561 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
535 | SPAGAIN; |
562 | SPAGAIN; |
536 | } |
563 | } |
537 | |
564 | |
538 | static void |
565 | static void |
539 | coro_run (void *arg) |
566 | coro_run (void *arg) |
540 | { |
567 | { |
541 | /* coro_run is the alternative epilogue of transfer() */ |
568 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
542 | UNLOCK; |
569 | UNLOCK; |
543 | |
570 | |
544 | /* |
571 | /* |
545 | * this is a _very_ stripped down perl interpreter ;) |
572 | * this is a _very_ stripped down perl interpreter ;) |
546 | */ |
573 | */ |
… | |
… | |
548 | |
575 | |
549 | /* inject call to cctx_init */ |
576 | /* inject call to cctx_init */ |
550 | prepare_cctx ((coro_cctx *)arg); |
577 | prepare_cctx ((coro_cctx *)arg); |
551 | |
578 | |
552 | /* somebody will hit me for both perl_run and PL_restartop */ |
579 | /* somebody will hit me for both perl_run and PL_restartop */ |
|
|
580 | PL_restartop = PL_op; |
553 | perl_run (PL_curinterp); |
581 | perl_run (PL_curinterp); |
554 | |
582 | |
555 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
583 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
556 | abort (); |
584 | abort (); |
557 | } |
585 | } |
… | |
… | |
561 | { |
589 | { |
562 | coro_cctx *cctx; |
590 | coro_cctx *cctx; |
563 | |
591 | |
564 | ++cctx_count; |
592 | ++cctx_count; |
565 | |
593 | |
566 | New (0, cctx, 1, coro_cctx); |
594 | Newz (0, cctx, 1, coro_cctx); |
567 | |
595 | |
568 | #if HAVE_MMAP |
596 | #if HAVE_MMAP |
569 | |
597 | |
570 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
598 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
571 | /* mmap suppsedly does allocate-on-write for us */ |
599 | /* mmap supposedly does allocate-on-write for us */ |
572 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
600 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
573 | |
601 | |
574 | if (cctx->sptr == (void *)-1) |
602 | if (cctx->sptr == (void *)-1) |
575 | { |
603 | { |
576 | perror ("FATAL: unable to mmap stack for coroutine"); |
604 | perror ("FATAL: unable to mmap stack for coroutine"); |
… | |
… | |
605 | |
633 | |
606 | return cctx; |
634 | return cctx; |
607 | } |
635 | } |
608 | |
636 | |
609 | static void |
637 | static void |
610 | cctx_free (coro_cctx *cctx) |
638 | cctx_destroy (coro_cctx *cctx) |
611 | { |
639 | { |
612 | if (!cctx) |
640 | if (!cctx) |
613 | return; |
641 | return; |
614 | |
642 | |
615 | --cctx_count; |
643 | --cctx_count; |
… | |
… | |
632 | { |
660 | { |
633 | coro_cctx *cctx; |
661 | coro_cctx *cctx; |
634 | |
662 | |
635 | if (cctx_first) |
663 | if (cctx_first) |
636 | { |
664 | { |
637 | --cctx_idle; |
|
|
638 | cctx = cctx_first; |
665 | cctx = cctx_first; |
639 | cctx_first = cctx->next; |
666 | cctx_first = cctx->next; |
|
|
667 | --cctx_idle; |
640 | } |
668 | } |
641 | else |
669 | else |
642 | { |
670 | { |
643 | cctx = cctx_new (); |
671 | cctx = cctx_new (); |
644 | PL_op = PL_op->op_next; |
672 | PL_op = PL_op->op_next; |
… | |
… | |
648 | } |
676 | } |
649 | |
677 | |
650 | static void |
678 | static void |
651 | cctx_put (coro_cctx *cctx) |
679 | cctx_put (coro_cctx *cctx) |
652 | { |
680 | { |
|
|
681 | /* free another cctx if overlimit */ |
|
|
682 | if (cctx_idle >= MAX_IDLE_CCTX) |
|
|
683 | { |
|
|
684 | coro_cctx *first = cctx_first; |
|
|
685 | cctx_first = first->next; |
|
|
686 | --cctx_idle; |
|
|
687 | |
|
|
688 | assert (!first->inuse); |
|
|
689 | cctx_destroy (first); |
|
|
690 | } |
|
|
691 | |
653 | ++cctx_idle; |
692 | ++cctx_idle; |
654 | cctx->next = cctx_first; |
693 | cctx->next = cctx_first; |
655 | cctx_first = cctx; |
694 | cctx_first = cctx; |
656 | } |
695 | } |
657 | |
696 | |
658 | /* never call directly, always through the coro_state_transfer global variable */ |
697 | /* never call directly, always through the coro_state_transfer global variable */ |
659 | static void NOINLINE |
698 | static void NOINLINE |
660 | transfer (struct coro *prev, struct coro *next, int flags) |
699 | transfer (struct coro *prev, struct coro *next) |
661 | { |
700 | { |
662 | dSTACKLEVEL; |
701 | dSTACKLEVEL; |
663 | |
702 | |
664 | /* sometimes transfer is only called to set idle_sp */ |
703 | /* sometimes transfer is only called to set idle_sp */ |
665 | if (flags == TRANSFER_SET_STACKLEVEL) |
704 | if (!next) |
|
|
705 | { |
666 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
706 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
|
|
707 | assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */ |
|
|
708 | } |
667 | else if (prev != next) |
709 | else if (prev != next) |
668 | { |
710 | { |
669 | coro_cctx *prev__cctx; |
711 | coro_cctx *prev__cctx; |
670 | |
712 | |
|
|
713 | if (prev->flags & CF_NEW) |
|
|
714 | { |
|
|
715 | /* create a new empty context */ |
|
|
716 | Newz (0, prev->cctx, 1, coro_cctx); |
|
|
717 | prev->cctx->inuse = 1; |
|
|
718 | prev->flags &= ~CF_NEW; |
|
|
719 | prev->flags |= CF_RUNNING; |
|
|
720 | } |
|
|
721 | |
|
|
722 | /*TODO: must not croak here */ |
|
|
723 | if (!prev->flags & CF_RUNNING) |
|
|
724 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
|
|
725 | |
|
|
726 | if (next->flags & CF_RUNNING) |
|
|
727 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
728 | |
|
|
729 | prev->flags &= ~CF_RUNNING; |
|
|
730 | next->flags |= CF_RUNNING; |
|
|
731 | |
671 | LOCK; |
732 | LOCK; |
672 | |
733 | |
673 | if (next->mainstack) |
734 | if (next->flags & CF_NEW) |
674 | { |
735 | { |
675 | /* coroutine already started */ |
736 | /* need to start coroutine */ |
676 | SAVE (prev, flags); |
737 | next->flags &= ~CF_NEW; |
677 | LOAD (next); |
738 | /* first get rid of the old state */ |
|
|
739 | save_perl (prev); |
|
|
740 | /* setup coroutine call */ |
|
|
741 | setup_coro (next); |
|
|
742 | /* need a new stack */ |
|
|
743 | assert (!next->cctx); |
678 | } |
744 | } |
679 | else |
745 | else |
680 | { |
746 | { |
681 | /* need to start coroutine */ |
747 | /* coroutine already started */ |
682 | /* first get rid of the old state */ |
748 | save_perl (prev); |
683 | SAVE (prev, -1); |
749 | load_perl (next); |
684 | /* setup coroutine call */ |
|
|
685 | setup_coro (next); |
|
|
686 | /* need a stack */ |
|
|
687 | next->cctx = 0; |
|
|
688 | } |
750 | } |
689 | |
|
|
690 | if (!prev->cctx) |
|
|
691 | /* create a new empty context */ |
|
|
692 | Newz (0, prev->cctx, 1, coro_cctx); |
|
|
693 | |
751 | |
694 | prev__cctx = prev->cctx; |
752 | prev__cctx = prev->cctx; |
695 | |
753 | |
696 | /* possibly "free" the cctx */ |
754 | /* possibly "free" the cctx */ |
697 | if (prev__cctx->idle_sp == STACKLEVEL) |
755 | if (prev__cctx->idle_sp == STACKLEVEL) |
698 | { |
756 | { |
|
|
757 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
699 | assert (PL_top_env == prev__cctx->top_env);//D |
758 | assert (PL_top_env == prev__cctx->top_env); |
|
|
759 | |
|
|
760 | prev->cctx = 0; |
|
|
761 | |
700 | cctx_put (prev__cctx); |
762 | cctx_put (prev__cctx); |
701 | prev->cctx = 0; |
763 | prev__cctx->inuse = 0; |
702 | } |
764 | } |
703 | |
765 | |
704 | if (!next->cctx) |
766 | if (!next->cctx) |
|
|
767 | { |
705 | next->cctx = cctx_get (); |
768 | next->cctx = cctx_get (); |
|
|
769 | assert (!next->cctx->inuse); |
|
|
770 | next->cctx->inuse = 1; |
|
|
771 | } |
706 | |
772 | |
707 | if (prev__cctx != next->cctx) |
773 | if (prev__cctx != next->cctx) |
708 | { |
774 | { |
709 | prev__cctx->top_env = PL_top_env; |
775 | prev__cctx->top_env = PL_top_env; |
710 | PL_top_env = next->cctx->top_env; |
776 | PL_top_env = next->cctx->top_env; |
… | |
… | |
718 | } |
784 | } |
719 | |
785 | |
720 | struct transfer_args |
786 | struct transfer_args |
721 | { |
787 | { |
722 | struct coro *prev, *next; |
788 | struct coro *prev, *next; |
723 | int flags; |
|
|
724 | }; |
789 | }; |
725 | |
790 | |
726 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) |
791 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
727 | |
792 | |
728 | static void |
793 | static void |
729 | coro_state_destroy (struct coro *coro) |
794 | coro_state_destroy (struct coro *coro) |
730 | { |
795 | { |
731 | if (coro->refcnt--) |
796 | if (coro->refcnt--) |
732 | return; |
797 | return; |
733 | |
798 | |
734 | if (coro->mainstack && coro->mainstack != main_mainstack) |
799 | if (coro->mainstack && coro->mainstack != main_mainstack) |
735 | { |
800 | { |
736 | struct coro temp; |
801 | struct coro temp; |
|
|
802 | Zero (&temp, 1, struct coro); |
|
|
803 | temp.save = CORO_SAVE_ALL; |
737 | |
804 | |
738 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
805 | if (coro->flags & CF_RUNNING) |
739 | LOAD (coro); |
806 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
807 | |
|
|
808 | save_perl (&temp); |
|
|
809 | load_perl (coro); |
740 | |
810 | |
741 | coro_destroy_stacks (); |
811 | coro_destroy_stacks (); |
742 | |
812 | |
743 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
813 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
744 | |
814 | |
745 | coro->mainstack = 0; |
815 | coro->mainstack = 0; |
746 | } |
816 | } |
747 | |
817 | |
748 | cctx_free (coro->cctx); |
818 | cctx_destroy (coro->cctx); |
749 | SvREFCNT_dec (coro->args); |
819 | SvREFCNT_dec (coro->args); |
750 | Safefree (coro); |
820 | Safefree (coro); |
751 | } |
821 | } |
752 | |
822 | |
753 | static int |
823 | static int |
… | |
… | |
803 | assert (mg->mg_type == PERL_MAGIC_ext); |
873 | assert (mg->mg_type == PERL_MAGIC_ext); |
804 | return (struct coro *)mg->mg_ptr; |
874 | return (struct coro *)mg->mg_ptr; |
805 | } |
875 | } |
806 | |
876 | |
807 | static void |
877 | static void |
808 | prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) |
878 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
809 | { |
879 | { |
810 | ta->prev = SvSTATE (prev); |
880 | ta->prev = SvSTATE (prev_sv); |
811 | ta->next = SvSTATE (next); |
881 | ta->next = SvSTATE (next_sv); |
812 | ta->flags = flags; |
|
|
813 | } |
882 | } |
814 | |
883 | |
815 | static void |
884 | static void |
816 | api_transfer (SV *prev, SV *next, int flags) |
885 | api_transfer (SV *prev_sv, SV *next_sv) |
817 | { |
886 | { |
818 | dTHX; |
|
|
819 | struct transfer_args ta; |
887 | struct transfer_args ta; |
820 | |
888 | |
821 | prepare_transfer (&ta, prev, next, flags); |
889 | prepare_transfer (&ta, prev_sv, next_sv); |
822 | TRANSFER (ta); |
890 | TRANSFER (ta); |
|
|
891 | } |
|
|
892 | |
|
|
893 | static int |
|
|
894 | api_save (SV *coro_sv, int new_save) |
|
|
895 | { |
|
|
896 | struct coro *coro = SvSTATE (coro_sv); |
|
|
897 | int old_save = coro->save; |
|
|
898 | |
|
|
899 | if (new_save >= 0) |
|
|
900 | coro->save = new_save; |
|
|
901 | |
|
|
902 | return old_save; |
823 | } |
903 | } |
824 | |
904 | |
825 | /** Coro ********************************************************************/ |
905 | /** Coro ********************************************************************/ |
826 | |
906 | |
827 | #define PRIO_MAX 3 |
907 | #define PRIO_MAX 3 |
… | |
… | |
830 | #define PRIO_LOW -1 |
910 | #define PRIO_LOW -1 |
831 | #define PRIO_IDLE -3 |
911 | #define PRIO_IDLE -3 |
832 | #define PRIO_MIN -4 |
912 | #define PRIO_MIN -4 |
833 | |
913 | |
834 | /* for Coro.pm */ |
914 | /* for Coro.pm */ |
835 | static GV *coro_current, *coro_idle; |
915 | static SV *coro_current; |
836 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
916 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
837 | static int coro_nready; |
917 | static int coro_nready; |
838 | |
918 | |
839 | static void |
919 | static void |
840 | coro_enq (SV *sv) |
920 | coro_enq (SV *coro_sv) |
841 | { |
921 | { |
842 | int prio; |
|
|
843 | |
|
|
844 | if (SvTYPE (sv) != SVt_PVHV) |
|
|
845 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
846 | |
|
|
847 | prio = SvSTATE (sv)->prio; |
|
|
848 | |
|
|
849 | av_push (coro_ready [prio - PRIO_MIN], sv); |
922 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
850 | coro_nready++; |
923 | coro_nready++; |
851 | } |
924 | } |
852 | |
925 | |
853 | static SV * |
926 | static SV * |
854 | coro_deq (int min_prio) |
927 | coro_deq (int min_prio) |
… | |
… | |
867 | } |
940 | } |
868 | |
941 | |
869 | return 0; |
942 | return 0; |
870 | } |
943 | } |
871 | |
944 | |
872 | static void |
945 | static int |
873 | api_ready (SV *coro) |
946 | api_ready (SV *coro_sv) |
874 | { |
947 | { |
875 | dTHX; |
948 | struct coro *coro; |
876 | |
949 | |
877 | if (SvROK (coro)) |
950 | if (SvROK (coro_sv)) |
878 | coro = SvRV (coro); |
951 | coro_sv = SvRV (coro_sv); |
|
|
952 | |
|
|
953 | coro = SvSTATE (coro_sv); |
|
|
954 | |
|
|
955 | if (coro->flags & CF_READY) |
|
|
956 | return 0; |
|
|
957 | |
|
|
958 | #if 0 /* this is actually harmless */ |
|
|
959 | if (coro->flags & CF_RUNNING) |
|
|
960 | croak ("Coro::ready called on currently running coroutine"); |
|
|
961 | #endif |
|
|
962 | |
|
|
963 | coro->flags |= CF_READY; |
879 | |
964 | |
880 | LOCK; |
965 | LOCK; |
881 | coro_enq (SvREFCNT_inc (coro)); |
966 | coro_enq (SvREFCNT_inc (coro_sv)); |
882 | UNLOCK; |
967 | UNLOCK; |
|
|
968 | |
|
|
969 | return 1; |
|
|
970 | } |
|
|
971 | |
|
|
972 | static int |
|
|
973 | api_is_ready (SV *coro_sv) |
|
|
974 | { |
|
|
975 | return !!SvSTATE (coro_sv)->flags & CF_READY; |
883 | } |
976 | } |
884 | |
977 | |
885 | static void |
978 | static void |
886 | prepare_schedule (struct transfer_args *ta) |
979 | prepare_schedule (struct transfer_args *ta) |
887 | { |
980 | { |
888 | SV *current, *prev, *next; |
981 | SV *prev, *next; |
889 | |
|
|
890 | current = GvSV (coro_current); |
|
|
891 | |
982 | |
892 | for (;;) |
983 | for (;;) |
893 | { |
984 | { |
894 | LOCK; |
985 | LOCK; |
895 | next = coro_deq (PRIO_MIN); |
986 | next = coro_deq (PRIO_MIN); |
… | |
… | |
904 | ENTER; |
995 | ENTER; |
905 | SAVETMPS; |
996 | SAVETMPS; |
906 | |
997 | |
907 | PUSHMARK (SP); |
998 | PUSHMARK (SP); |
908 | PUTBACK; |
999 | PUTBACK; |
909 | call_sv (GvSV (coro_idle), G_DISCARD); |
1000 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
910 | |
1001 | |
911 | FREETMPS; |
1002 | FREETMPS; |
912 | LEAVE; |
1003 | LEAVE; |
913 | } |
1004 | } |
914 | } |
1005 | } |
915 | |
1006 | |
916 | prev = SvRV (current); |
1007 | prev = SvRV (coro_current); |
917 | SvRV (current) = next; |
1008 | SvRV_set (coro_current, next); |
918 | |
1009 | |
919 | /* free this only after the transfer */ |
1010 | /* free this only after the transfer */ |
920 | LOCK; |
1011 | LOCK; |
921 | free_coro_mortal (); |
1012 | free_coro_mortal (); |
922 | UNLOCK; |
1013 | UNLOCK; |
923 | coro_mortal = prev; |
1014 | coro_mortal = prev; |
924 | |
1015 | |
|
|
1016 | assert (!SvROK(prev));//D |
|
|
1017 | assert (!SvROK(next));//D |
|
|
1018 | |
925 | ta->prev = SvSTATE (prev); |
1019 | ta->prev = SvSTATE (prev); |
926 | ta->next = SvSTATE (next); |
1020 | ta->next = SvSTATE (next); |
927 | ta->flags = TRANSFER_SAVE_ALL; |
1021 | |
|
|
1022 | assert (ta->next->flags & CF_READY); |
|
|
1023 | ta->next->flags &= ~CF_READY; |
928 | } |
1024 | } |
929 | |
1025 | |
930 | static void |
1026 | static void |
931 | prepare_cede (struct transfer_args *ta) |
1027 | prepare_cede (struct transfer_args *ta) |
932 | { |
1028 | { |
933 | LOCK; |
1029 | api_ready (coro_current); |
934 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
935 | UNLOCK; |
|
|
936 | |
1030 | |
937 | prepare_schedule (ta); |
1031 | prepare_schedule (ta); |
938 | } |
1032 | } |
939 | |
1033 | |
940 | static void |
1034 | static void |
941 | api_schedule (void) |
1035 | api_schedule (void) |
942 | { |
1036 | { |
943 | dTHX; |
|
|
944 | struct transfer_args ta; |
1037 | struct transfer_args ta; |
945 | |
1038 | |
946 | prepare_schedule (&ta); |
1039 | prepare_schedule (&ta); |
947 | TRANSFER (ta); |
1040 | TRANSFER (ta); |
948 | } |
1041 | } |
949 | |
1042 | |
950 | static void |
1043 | static void |
951 | api_cede (void) |
1044 | api_cede (void) |
952 | { |
1045 | { |
953 | dTHX; |
|
|
954 | struct transfer_args ta; |
1046 | struct transfer_args ta; |
955 | |
1047 | |
956 | prepare_cede (&ta); |
1048 | prepare_cede (&ta); |
957 | TRANSFER (ta); |
1049 | TRANSFER (ta); |
958 | } |
1050 | } |
… | |
… | |
968 | #endif |
1060 | #endif |
969 | BOOT_PAGESIZE; |
1061 | BOOT_PAGESIZE; |
970 | |
1062 | |
971 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1063 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
972 | |
1064 | |
973 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
1065 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
974 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
1066 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
975 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
1067 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
|
|
1068 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1069 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
976 | |
1070 | |
977 | main_mainstack = PL_mainstack; |
1071 | main_mainstack = PL_mainstack; |
978 | |
1072 | |
979 | coroapi.ver = CORO_API_VERSION; |
1073 | coroapi.ver = CORO_API_VERSION; |
980 | coroapi.transfer = api_transfer; |
1074 | coroapi.transfer = api_transfer; |
… | |
… | |
990 | HV *hv; |
1084 | HV *hv; |
991 | int i; |
1085 | int i; |
992 | |
1086 | |
993 | Newz (0, coro, 1, struct coro); |
1087 | Newz (0, coro, 1, struct coro); |
994 | coro->args = newAV (); |
1088 | coro->args = newAV (); |
|
|
1089 | coro->save = CORO_SAVE_ALL; |
|
|
1090 | coro->flags = CF_NEW; |
995 | |
1091 | |
996 | hv = newHV (); |
1092 | hv = newHV (); |
997 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1093 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
998 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1094 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
999 | |
1095 | |
1000 | for (i = 1; i < items; i++) |
1096 | for (i = 1; i < items; i++) |
1001 | av_push (coro->args, newSVsv (ST (i))); |
1097 | av_push (coro->args, newSVsv (ST (i))); |
1002 | } |
1098 | } |
1003 | OUTPUT: |
1099 | OUTPUT: |
|
|
1100 | RETVAL |
|
|
1101 | |
|
|
1102 | int |
|
|
1103 | save (SV *coro, int new_save = -1) |
|
|
1104 | CODE: |
|
|
1105 | RETVAL = api_save (coro, new_save); |
|
|
1106 | OUTPUT: |
1004 | RETVAL |
1107 | RETVAL |
1005 | |
1108 | |
1006 | void |
1109 | void |
1007 | _set_stacklevel (...) |
1110 | _set_stacklevel (...) |
1008 | ALIAS: |
1111 | ALIAS: |
1009 | Coro::State::transfer = 1 |
1112 | Coro::State::transfer = 1 |
1010 | Coro::schedule = 2 |
1113 | Coro::schedule = 2 |
1011 | Coro::cede = 3 |
1114 | Coro::cede = 3 |
1012 | Coro::Cont::yield = 4 |
|
|
1013 | CODE: |
1115 | CODE: |
1014 | { |
1116 | { |
1015 | struct transfer_args ta; |
1117 | struct transfer_args ta; |
1016 | |
1118 | |
1017 | switch (ix) |
1119 | switch (ix) |
1018 | { |
1120 | { |
1019 | case 0: |
1121 | case 0: |
1020 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1122 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1021 | ta.next = 0; |
1123 | ta.next = 0; |
1022 | ta.flags = TRANSFER_SET_STACKLEVEL; |
|
|
1023 | break; |
1124 | break; |
1024 | |
1125 | |
1025 | case 1: |
1126 | case 1: |
1026 | if (items != 3) |
1127 | if (items != 2) |
1027 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1128 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1028 | |
1129 | |
1029 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1130 | prepare_transfer (&ta, ST (0), ST (1)); |
1030 | break; |
1131 | break; |
1031 | |
1132 | |
1032 | case 2: |
1133 | case 2: |
1033 | prepare_schedule (&ta); |
1134 | prepare_schedule (&ta); |
1034 | break; |
1135 | break; |
1035 | |
1136 | |
1036 | case 3: |
1137 | case 3: |
1037 | prepare_cede (&ta); |
1138 | prepare_cede (&ta); |
1038 | break; |
1139 | break; |
1039 | |
|
|
1040 | case 4: |
|
|
1041 | { |
|
|
1042 | SV *yieldstack; |
|
|
1043 | SV *sv; |
|
|
1044 | AV *defav = GvAV (PL_defgv); |
|
|
1045 | |
|
|
1046 | yieldstack = *hv_fetch ( |
|
|
1047 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1048 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1049 | 0 |
|
|
1050 | ); |
|
|
1051 | |
|
|
1052 | /* set up @_ -- ugly */ |
|
|
1053 | av_clear (defav); |
|
|
1054 | av_fill (defav, items - 1); |
|
|
1055 | while (items--) |
|
|
1056 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1057 | |
|
|
1058 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1059 | ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1060 | ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1061 | ta.flags = 0; |
|
|
1062 | SvREFCNT_dec (sv); |
|
|
1063 | } |
|
|
1064 | break; |
|
|
1065 | |
|
|
1066 | } |
1140 | } |
1067 | |
1141 | |
1068 | TRANSFER (ta); |
1142 | TRANSFER (ta); |
1069 | } |
1143 | } |
1070 | |
1144 | |
… | |
… | |
1114 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1188 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1115 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1189 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1116 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1190 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1117 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1191 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1118 | |
1192 | |
1119 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1193 | coro_current = get_sv ("Coro::current", FALSE); |
1120 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1194 | SvREADONLY_on (coro_current); |
1121 | |
1195 | |
1122 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1196 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1123 | coro_ready[i] = newAV (); |
1197 | coro_ready[i] = newAV (); |
1124 | |
1198 | |
1125 | { |
1199 | { |
1126 | SV *sv = perl_get_sv("Coro::API", 1); |
1200 | SV *sv = perl_get_sv("Coro::API", 1); |
1127 | |
1201 | |
1128 | coroapi.schedule = api_schedule; |
1202 | coroapi.schedule = api_schedule; |
|
|
1203 | coroapi.save = api_save; |
1129 | coroapi.cede = api_cede; |
1204 | coroapi.cede = api_cede; |
1130 | coroapi.ready = api_ready; |
1205 | coroapi.ready = api_ready; |
|
|
1206 | coroapi.is_ready = api_is_ready; |
1131 | coroapi.nready = &coro_nready; |
1207 | coroapi.nready = &coro_nready; |
1132 | coroapi.current = coro_current; |
1208 | coroapi.current = coro_current; |
1133 | |
1209 | |
1134 | GCoroAPI = &coroapi; |
1210 | GCoroAPI = &coroapi; |
1135 | sv_setiv (sv, (IV)&coroapi); |
1211 | sv_setiv (sv, (IV)&coroapi); |
1136 | SvREADONLY_on (sv); |
1212 | SvREADONLY_on (sv); |
1137 | } |
1213 | } |
1138 | } |
1214 | } |
|
|
1215 | |
|
|
1216 | void |
|
|
1217 | _set_current (SV *current) |
|
|
1218 | PROTOTYPE: $ |
|
|
1219 | CODE: |
|
|
1220 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1221 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
1139 | |
1222 | |
1140 | int |
1223 | int |
1141 | prio (Coro::State coro, int newprio = 0) |
1224 | prio (Coro::State coro, int newprio = 0) |
1142 | ALIAS: |
1225 | ALIAS: |
1143 | nice = 1 |
1226 | nice = 1 |
… | |
… | |
1155 | |
1238 | |
1156 | coro->prio = newprio; |
1239 | coro->prio = newprio; |
1157 | } |
1240 | } |
1158 | } |
1241 | } |
1159 | |
1242 | |
1160 | void |
1243 | SV * |
1161 | ready (SV *self) |
1244 | ready (SV *self) |
1162 | PROTOTYPE: $ |
1245 | PROTOTYPE: $ |
1163 | CODE: |
1246 | CODE: |
1164 | api_ready (self); |
1247 | RETVAL = boolSV (api_ready (self)); |
|
|
1248 | OUTPUT: |
|
|
1249 | RETVAL |
|
|
1250 | |
|
|
1251 | SV * |
|
|
1252 | is_ready (SV *self) |
|
|
1253 | PROTOTYPE: $ |
|
|
1254 | CODE: |
|
|
1255 | RETVAL = boolSV (api_is_ready (self)); |
|
|
1256 | OUTPUT: |
|
|
1257 | RETVAL |
1165 | |
1258 | |
1166 | int |
1259 | int |
1167 | nready (...) |
1260 | nready (...) |
1168 | PROTOTYPE: |
1261 | PROTOTYPE: |
1169 | CODE: |
1262 | CODE: |
… | |
… | |
1175 | |
1268 | |
1176 | SV * |
1269 | SV * |
1177 | _get_state () |
1270 | _get_state () |
1178 | CODE: |
1271 | CODE: |
1179 | { |
1272 | { |
1180 | struct { |
1273 | struct io_state *data; |
1181 | int errorno; |
|
|
1182 | int laststype; |
|
|
1183 | int laststatval; |
|
|
1184 | Stat_t statcache; |
|
|
1185 | } data; |
|
|
1186 | |
1274 | |
|
|
1275 | RETVAL = newSV (sizeof (struct io_state)); |
|
|
1276 | data = (struct io_state *)SvPVX (RETVAL); |
|
|
1277 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
|
|
1278 | SvPOK_only (RETVAL); |
|
|
1279 | |
1187 | data.errorno = errno; |
1280 | data->errorno = errno; |
1188 | data.laststype = PL_laststype; |
1281 | data->laststype = PL_laststype; |
1189 | data.laststatval = PL_laststatval; |
1282 | data->laststatval = PL_laststatval; |
1190 | data.statcache = PL_statcache; |
1283 | data->statcache = PL_statcache; |
1191 | |
|
|
1192 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1193 | } |
1284 | } |
1194 | OUTPUT: |
1285 | OUTPUT: |
1195 | RETVAL |
1286 | RETVAL |
1196 | |
1287 | |
1197 | void |
1288 | void |
1198 | _set_state (char *data_) |
1289 | _set_state (char *data_) |
1199 | PROTOTYPE: $ |
1290 | PROTOTYPE: $ |
1200 | CODE: |
1291 | CODE: |
1201 | { |
1292 | { |
1202 | struct { |
1293 | struct io_state *data = (void *)data_; |
1203 | int errorno; |
|
|
1204 | int laststype; |
|
|
1205 | int laststatval; |
|
|
1206 | Stat_t statcache; |
|
|
1207 | } *data = (void *)data_; |
|
|
1208 | |
1294 | |
1209 | errno = data->errorno; |
1295 | errno = data->errorno; |
1210 | PL_laststype = data->laststype; |
1296 | PL_laststype = data->laststype; |
1211 | PL_laststatval = data->laststatval; |
1297 | PL_laststatval = data->laststatval; |
1212 | PL_statcache = data->statcache; |
1298 | PL_statcache = data->statcache; |
1213 | } |
1299 | } |
|
|
1300 | |