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