… | |
… | |
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) \ |
… | |
… | |
87 | |
90 | |
88 | #define NOINLINE attribute ((noinline)) |
91 | #define NOINLINE attribute ((noinline)) |
89 | |
92 | |
90 | #include "CoroAPI.h" |
93 | #include "CoroAPI.h" |
91 | |
94 | |
92 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
|
|
93 | |
|
|
94 | #ifdef USE_ITHREADS |
95 | #ifdef USE_ITHREADS |
95 | static perl_mutex coro_mutex; |
96 | static perl_mutex coro_mutex; |
96 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
97 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
97 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
98 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
98 | #else |
99 | #else |
99 | # define LOCK (void)0 |
100 | # define LOCK (void)0 |
100 | # define UNLOCK (void)0 |
101 | # define UNLOCK (void)0 |
101 | #endif |
102 | #endif |
102 | |
103 | |
|
|
104 | struct io_state |
|
|
105 | { |
|
|
106 | int errorno; |
|
|
107 | I32 laststype; |
|
|
108 | int laststatval; |
|
|
109 | Stat_t statcache; |
|
|
110 | }; |
|
|
111 | |
103 | static struct CoroAPI coroapi; |
112 | static struct CoroAPI coroapi; |
104 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
113 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
105 | static HV *coro_state_stash, *coro_stash; |
114 | static HV *coro_state_stash, *coro_stash; |
106 | static SV *coro_mortal; /* will be freed after next transfer */ |
115 | static SV *coro_mortal; /* will be freed after next transfer */ |
107 | |
116 | |
… | |
… | |
127 | int valgrind_id; |
136 | int valgrind_id; |
128 | #endif |
137 | #endif |
129 | } coro_cctx; |
138 | } coro_cctx; |
130 | |
139 | |
131 | enum { |
140 | enum { |
132 | CF_RUNNING, /* coroutine is running */ |
141 | CF_RUNNING = 0x0001, /* coroutine is running */ |
133 | CF_READY, /* coroutine is ready */ |
142 | CF_READY = 0x0002, /* coroutine is ready */ |
|
|
143 | CF_NEW = 0x0004, /* ahs never been switched to */ |
134 | }; |
144 | }; |
135 | |
145 | |
136 | /* this is a structure representing a perl-level coroutine */ |
146 | /* this is a structure representing a perl-level coroutine */ |
137 | struct coro { |
147 | struct coro { |
138 | /* the c coroutine allocated to this perl coroutine, if any */ |
148 | /* the c coroutine allocated to this perl coroutine, if any */ |
139 | coro_cctx *cctx; |
149 | coro_cctx *cctx; |
140 | |
150 | |
141 | /* data associated with this coroutine (initial args) */ |
151 | /* data associated with this coroutine (initial args) */ |
142 | AV *args; |
152 | AV *args; |
143 | int refcnt; |
153 | int refcnt; |
144 | int flags; |
154 | int save; /* CORO_SAVE flags */ |
|
|
155 | int flags; /* CF_ flags */ |
145 | |
156 | |
146 | /* optionally saved, might be zero */ |
157 | /* optionally saved, might be zero */ |
147 | AV *defav; |
158 | AV *defav; /* @_ */ |
148 | SV *defsv; |
159 | SV *defsv; /* $_ */ |
149 | SV *errsv; |
160 | SV *errsv; /* $@ */ |
|
|
161 | SV *irssv; /* $/ */ |
150 | |
162 | |
151 | #define VAR(name,type) type name; |
163 | #define VAR(name,type) type name; |
152 | # include "state.h" |
164 | # include "state.h" |
153 | #undef VAR |
165 | #undef VAR |
154 | |
166 | |
… | |
… | |
277 | } |
289 | } |
278 | |
290 | |
279 | #define SB do { |
291 | #define SB do { |
280 | #define SE } while (0) |
292 | #define SE } while (0) |
281 | |
293 | |
282 | #define LOAD(state) load_state((state)); |
|
|
283 | #define SAVE(state,flags) save_state((state),(flags)); |
|
|
284 | |
|
|
285 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
294 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
286 | |
295 | |
287 | static void |
296 | static void |
288 | load_state(Coro__State c) |
297 | load_perl (Coro__State c) |
289 | { |
298 | { |
290 | #define VAR(name,type) PL_ ## name = c->name; |
299 | #define VAR(name,type) PL_ ## name = c->name; |
291 | # include "state.h" |
300 | # include "state.h" |
292 | #undef VAR |
301 | #undef VAR |
293 | |
302 | |
294 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
303 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
295 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
304 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
296 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
305 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
|
|
306 | if (c->irssv) REPLACE_SV (PL_rs , c->irssv); |
297 | |
307 | |
298 | { |
308 | { |
299 | dSP; |
309 | dSP; |
300 | CV *cv; |
310 | CV *cv; |
301 | |
311 | |
… | |
… | |
310 | PUTBACK; |
320 | PUTBACK; |
311 | } |
321 | } |
312 | } |
322 | } |
313 | |
323 | |
314 | static void |
324 | static void |
315 | save_state(Coro__State c, int flags) |
325 | save_perl (Coro__State c) |
316 | { |
326 | { |
317 | { |
327 | { |
318 | dSP; |
328 | dSP; |
319 | I32 cxix = cxstack_ix; |
329 | I32 cxix = cxstack_ix; |
320 | PERL_CONTEXT *ccstk = cxstack; |
330 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
331 | { |
341 | { |
332 | while (cxix >= 0) |
342 | while (cxix >= 0) |
333 | { |
343 | { |
334 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
344 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
335 | |
345 | |
336 | if (CxTYPE(cx) == CXt_SUB) |
346 | if (CxTYPE (cx) == CXt_SUB) |
337 | { |
347 | { |
338 | CV *cv = cx->blk_sub.cv; |
348 | CV *cv = cx->blk_sub.cv; |
339 | |
349 | |
340 | if (CvDEPTH (cv)) |
350 | if (CvDEPTH (cv)) |
341 | { |
351 | { |
342 | EXTEND (SP, 3); |
352 | EXTEND (SP, 3); |
343 | |
353 | |
344 | PUSHs ((SV *)CvPADLIST(cv)); |
354 | PUSHs ((SV *)CvPADLIST (cv)); |
345 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
355 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
346 | PUSHs ((SV *)cv); |
356 | PUSHs ((SV *)cv); |
347 | |
357 | |
348 | CvDEPTH (cv) = 0; |
358 | CvDEPTH (cv) = 0; |
349 | get_padlist (cv); |
359 | get_padlist (cv); |
350 | } |
360 | } |
351 | } |
361 | } |
352 | #ifdef CXt_FORMAT |
362 | #ifdef CXt_FORMAT |
353 | else if (CxTYPE(cx) == CXt_FORMAT) |
363 | else if (CxTYPE (cx) == CXt_FORMAT) |
354 | { |
364 | { |
355 | /* I never used formats, so how should I know how these are implemented? */ |
365 | /* I never used formats, so how should I know how these are implemented? */ |
356 | /* my bold guess is as a simple, plain sub... */ |
366 | /* my bold guess is as a simple, plain sub... */ |
357 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
367 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
358 | } |
368 | } |
… | |
… | |
368 | } |
378 | } |
369 | |
379 | |
370 | PUTBACK; |
380 | PUTBACK; |
371 | } |
381 | } |
372 | |
382 | |
373 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
383 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
374 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
384 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
375 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
385 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
386 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
376 | |
387 | |
377 | #define VAR(name,type)c->name = PL_ ## name; |
388 | #define VAR(name,type)c->name = PL_ ## name; |
378 | # include "state.h" |
389 | # include "state.h" |
379 | #undef VAR |
390 | #undef VAR |
380 | } |
391 | } |
… | |
… | |
386 | * not usually need a lot of stackspace. |
397 | * not usually need a lot of stackspace. |
387 | */ |
398 | */ |
388 | static void |
399 | static void |
389 | coro_init_stacks () |
400 | coro_init_stacks () |
390 | { |
401 | { |
391 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
402 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
392 | PL_curstackinfo->si_type = PERLSI_MAIN; |
403 | PL_curstackinfo->si_type = PERLSI_MAIN; |
393 | PL_curstack = PL_curstackinfo->si_stack; |
404 | PL_curstack = PL_curstackinfo->si_stack; |
394 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
405 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
395 | |
406 | |
396 | PL_stack_base = AvARRAY(PL_curstack); |
407 | PL_stack_base = AvARRAY(PL_curstack); |
397 | PL_stack_sp = PL_stack_base; |
408 | PL_stack_sp = PL_stack_base; |
398 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
409 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
399 | |
410 | |
400 | New(50,PL_tmps_stack,96,SV*); |
411 | New(50,PL_tmps_stack,128,SV*); |
401 | PL_tmps_floor = -1; |
412 | PL_tmps_floor = -1; |
402 | PL_tmps_ix = -1; |
413 | PL_tmps_ix = -1; |
403 | PL_tmps_max = 96; |
414 | PL_tmps_max = 128; |
404 | |
415 | |
405 | New(54,PL_markstack,16,I32); |
416 | New(54,PL_markstack,32,I32); |
406 | PL_markstack_ptr = PL_markstack; |
417 | PL_markstack_ptr = PL_markstack; |
407 | PL_markstack_max = PL_markstack + 16; |
418 | PL_markstack_max = PL_markstack + 32; |
408 | |
419 | |
409 | #ifdef SET_MARK_OFFSET |
420 | #ifdef SET_MARK_OFFSET |
410 | SET_MARK_OFFSET; |
421 | SET_MARK_OFFSET; |
411 | #endif |
422 | #endif |
412 | |
423 | |
413 | New(54,PL_scopestack,16,I32); |
424 | New(54,PL_scopestack,32,I32); |
414 | PL_scopestack_ix = 0; |
425 | PL_scopestack_ix = 0; |
415 | PL_scopestack_max = 16; |
426 | PL_scopestack_max = 32; |
416 | |
427 | |
417 | New(54,PL_savestack,96,ANY); |
428 | New(54,PL_savestack,64,ANY); |
418 | PL_savestack_ix = 0; |
429 | PL_savestack_ix = 0; |
419 | PL_savestack_max = 96; |
430 | PL_savestack_max = 64; |
420 | |
431 | |
421 | #if !PERL_VERSION_ATLEAST (5,9,0) |
432 | #if !PERL_VERSION_ATLEAST (5,9,0) |
422 | New(54,PL_retstack,8,OP*); |
433 | New(54,PL_retstack,16,OP*); |
423 | PL_retstack_ix = 0; |
434 | PL_retstack_ix = 0; |
424 | PL_retstack_max = 8; |
435 | PL_retstack_max = 16; |
425 | #endif |
436 | #endif |
426 | } |
437 | } |
427 | |
438 | |
428 | /* |
439 | /* |
429 | * destroy the stacks, the callchain etc... |
440 | * destroy the stacks, the callchain etc... |
… | |
… | |
482 | * emulate part of the perl startup here. |
493 | * emulate part of the perl startup here. |
483 | */ |
494 | */ |
484 | |
495 | |
485 | coro_init_stacks (); |
496 | coro_init_stacks (); |
486 | |
497 | |
487 | PL_curcop = 0; |
498 | PL_curcop = &PL_compiling; |
488 | PL_in_eval = 0; |
499 | PL_in_eval = EVAL_NULL; |
489 | PL_curpm = 0; |
500 | PL_curpm = 0; |
|
|
501 | PL_localizing = 0; |
|
|
502 | PL_dirty = 0; |
|
|
503 | PL_restartop = 0; |
490 | |
504 | |
491 | { |
505 | { |
492 | dSP; |
506 | dSP; |
493 | LOGOP myop; |
507 | LOGOP myop; |
494 | |
508 | |
495 | /* I have no idea why this is needed, but it is */ |
|
|
496 | PUSHMARK (SP); |
|
|
497 | |
|
|
498 | SvREFCNT_dec (GvAV (PL_defgv)); |
509 | SvREFCNT_dec (GvAV (PL_defgv)); |
499 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
510 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
500 | |
511 | |
501 | Zero (&myop, 1, LOGOP); |
512 | Zero (&myop, 1, LOGOP); |
502 | myop.op_next = Nullop; |
513 | myop.op_next = Nullop; |
503 | myop.op_flags = OPf_WANT_VOID; |
514 | myop.op_flags = OPf_WANT_VOID; |
504 | |
515 | |
505 | PL_op = (OP *)&myop; |
|
|
506 | |
|
|
507 | PUSHMARK (SP); |
516 | PUSHMARK (SP); |
508 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
517 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
509 | PUTBACK; |
518 | PUTBACK; |
|
|
519 | PL_op = (OP *)&myop; |
510 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
520 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
511 | SPAGAIN; |
521 | SPAGAIN; |
512 | |
|
|
513 | ENTER; /* necessary e.g. for dounwind */ |
|
|
514 | } |
522 | } |
|
|
523 | |
|
|
524 | ENTER; /* necessary e.g. for dounwind */ |
515 | } |
525 | } |
516 | |
526 | |
517 | static void |
527 | static void |
518 | free_coro_mortal () |
528 | free_coro_mortal () |
519 | { |
529 | { |
… | |
… | |
522 | SvREFCNT_dec (coro_mortal); |
532 | SvREFCNT_dec (coro_mortal); |
523 | coro_mortal = 0; |
533 | coro_mortal = 0; |
524 | } |
534 | } |
525 | } |
535 | } |
526 | |
536 | |
|
|
537 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
527 | static void NOINLINE |
538 | static void NOINLINE |
528 | prepare_cctx (coro_cctx *cctx) |
539 | prepare_cctx (coro_cctx *cctx) |
529 | { |
540 | { |
530 | dSP; |
541 | dSP; |
531 | LOGOP myop; |
542 | LOGOP myop; |
532 | |
543 | |
533 | Zero (&myop, 1, LOGOP); |
544 | Zero (&myop, 1, LOGOP); |
534 | myop.op_next = PL_op; |
545 | myop.op_next = PL_op; |
535 | myop.op_flags = OPf_WANT_VOID; |
546 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
536 | |
|
|
537 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
|
|
538 | |
547 | |
539 | PUSHMARK (SP); |
548 | PUSHMARK (SP); |
|
|
549 | EXTEND (SP, 2); |
|
|
550 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
540 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
551 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
541 | PUTBACK; |
552 | PUTBACK; |
|
|
553 | PL_op = (OP *)&myop; |
542 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
554 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
543 | SPAGAIN; |
555 | SPAGAIN; |
544 | } |
556 | } |
545 | |
557 | |
546 | static void |
558 | static void |
547 | coro_run (void *arg) |
559 | coro_run (void *arg) |
548 | { |
560 | { |
549 | /* coro_run is the alternative epilogue of transfer() */ |
561 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
550 | UNLOCK; |
562 | UNLOCK; |
551 | |
563 | |
552 | /* |
564 | /* |
553 | * this is a _very_ stripped down perl interpreter ;) |
565 | * this is a _very_ stripped down perl interpreter ;) |
554 | */ |
566 | */ |
… | |
… | |
556 | |
568 | |
557 | /* inject call to cctx_init */ |
569 | /* inject call to cctx_init */ |
558 | prepare_cctx ((coro_cctx *)arg); |
570 | prepare_cctx ((coro_cctx *)arg); |
559 | |
571 | |
560 | /* somebody will hit me for both perl_run and PL_restartop */ |
572 | /* somebody will hit me for both perl_run and PL_restartop */ |
|
|
573 | PL_restartop = PL_op; |
561 | perl_run (PL_curinterp); |
574 | perl_run (PL_curinterp); |
562 | |
575 | |
563 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
576 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
564 | abort (); |
577 | abort (); |
565 | } |
578 | } |
… | |
… | |
574 | New (0, cctx, 1, coro_cctx); |
587 | New (0, cctx, 1, coro_cctx); |
575 | |
588 | |
576 | #if HAVE_MMAP |
589 | #if HAVE_MMAP |
577 | |
590 | |
578 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
591 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
579 | /* mmap suppsedly does allocate-on-write for us */ |
592 | /* mmap supposedly does allocate-on-write for us */ |
580 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
593 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
581 | |
594 | |
582 | if (cctx->sptr == (void *)-1) |
595 | if (cctx->sptr == (void *)-1) |
583 | { |
596 | { |
584 | perror ("FATAL: unable to mmap stack for coroutine"); |
597 | perror ("FATAL: unable to mmap stack for coroutine"); |
… | |
… | |
613 | |
626 | |
614 | return cctx; |
627 | return cctx; |
615 | } |
628 | } |
616 | |
629 | |
617 | static void |
630 | static void |
618 | cctx_free (coro_cctx *cctx) |
631 | cctx_destroy (coro_cctx *cctx) |
619 | { |
632 | { |
620 | if (!cctx) |
633 | if (!cctx) |
621 | return; |
634 | return; |
622 | |
635 | |
623 | --cctx_count; |
636 | --cctx_count; |
… | |
… | |
640 | { |
653 | { |
641 | coro_cctx *cctx; |
654 | coro_cctx *cctx; |
642 | |
655 | |
643 | if (cctx_first) |
656 | if (cctx_first) |
644 | { |
657 | { |
645 | --cctx_idle; |
|
|
646 | cctx = cctx_first; |
658 | cctx = cctx_first; |
647 | cctx_first = cctx->next; |
659 | cctx_first = cctx->next; |
|
|
660 | --cctx_idle; |
648 | } |
661 | } |
649 | else |
662 | else |
650 | { |
663 | { |
651 | cctx = cctx_new (); |
664 | cctx = cctx_new (); |
652 | PL_op = PL_op->op_next; |
665 | PL_op = PL_op->op_next; |
… | |
… | |
656 | } |
669 | } |
657 | |
670 | |
658 | static void |
671 | static void |
659 | cctx_put (coro_cctx *cctx) |
672 | cctx_put (coro_cctx *cctx) |
660 | { |
673 | { |
|
|
674 | /* free another cctx if overlimit */ |
|
|
675 | if (cctx_idle >= MAX_IDLE_CCTX) |
|
|
676 | { |
|
|
677 | coro_cctx *first = cctx_first; |
|
|
678 | cctx_first = first->next; |
|
|
679 | --cctx_idle; |
|
|
680 | |
|
|
681 | assert (!first->inuse); |
|
|
682 | cctx_destroy (first); |
|
|
683 | } |
|
|
684 | |
661 | ++cctx_idle; |
685 | ++cctx_idle; |
662 | cctx->next = cctx_first; |
686 | cctx->next = cctx_first; |
663 | cctx_first = cctx; |
687 | cctx_first = cctx; |
664 | } |
688 | } |
665 | |
689 | |
666 | /* never call directly, always through the coro_state_transfer global variable */ |
690 | /* never call directly, always through the coro_state_transfer global variable */ |
667 | static void NOINLINE |
691 | static void NOINLINE |
668 | transfer (struct coro *prev, struct coro *next, int flags) |
692 | transfer (struct coro *prev, struct coro *next) |
669 | { |
693 | { |
670 | dSTACKLEVEL; |
694 | dSTACKLEVEL; |
671 | |
695 | |
672 | /* sometimes transfer is only called to set idle_sp */ |
696 | /* sometimes transfer is only called to set idle_sp */ |
673 | if (flags == TRANSFER_SET_STACKLEVEL) |
697 | if (!next) |
674 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
698 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
675 | else if (prev != next) |
699 | else if (prev != next) |
676 | { |
700 | { |
677 | coro_cctx *prev__cctx; |
701 | coro_cctx *prev__cctx; |
678 | |
702 | |
679 | if (!prev->cctx) |
703 | if (prev->flags & CF_NEW) |
680 | { |
704 | { |
681 | /* create a new empty context */ |
705 | /* create a new empty context */ |
682 | Newz (0, prev->cctx, 1, coro_cctx); |
706 | Newz (0, prev->cctx, 1, coro_cctx); |
683 | prev->cctx->inuse = 1; |
707 | prev->cctx->inuse = 1; |
|
|
708 | prev->flags &= ~CF_NEW; |
684 | prev->flags |= CF_RUNNING; |
709 | prev->flags |= CF_RUNNING; |
685 | } |
710 | } |
686 | |
711 | |
|
|
712 | /*TODO: must not croak here */ |
687 | if (!prev->flags & CF_RUNNING) |
713 | if (!prev->flags & CF_RUNNING) |
688 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
714 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
689 | |
715 | |
690 | if (next->flags & CF_RUNNING) |
716 | if (next->flags & CF_RUNNING) |
691 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
717 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
… | |
… | |
693 | prev->flags &= ~CF_RUNNING; |
719 | prev->flags &= ~CF_RUNNING; |
694 | next->flags |= CF_RUNNING; |
720 | next->flags |= CF_RUNNING; |
695 | |
721 | |
696 | LOCK; |
722 | LOCK; |
697 | |
723 | |
698 | if (next->mainstack) |
724 | if (next->flags & CF_NEW) |
699 | { |
|
|
700 | /* coroutine already started */ |
|
|
701 | SAVE (prev, flags); |
|
|
702 | LOAD (next); |
|
|
703 | } |
|
|
704 | else |
|
|
705 | { |
725 | { |
706 | /* need to start coroutine */ |
726 | /* need to start coroutine */ |
|
|
727 | next->flags &= ~CF_NEW; |
707 | /* first get rid of the old state */ |
728 | /* first get rid of the old state */ |
708 | SAVE (prev, -1); |
729 | save_perl (prev); |
709 | /* setup coroutine call */ |
730 | /* setup coroutine call */ |
710 | setup_coro (next); |
731 | setup_coro (next); |
711 | /* need a new stack */ |
732 | /* need a new stack */ |
712 | assert (!next->stack); |
733 | assert (!next->stack); |
713 | } |
734 | } |
|
|
735 | else |
|
|
736 | { |
|
|
737 | /* coroutine already started */ |
|
|
738 | save_perl (prev); |
|
|
739 | load_perl (next); |
|
|
740 | } |
714 | |
741 | |
715 | prev__cctx = prev->cctx; |
742 | prev__cctx = prev->cctx; |
716 | |
743 | |
717 | /* possibly "free" the cctx */ |
744 | /* possibly "free" the cctx */ |
718 | if (prev__cctx->idle_sp == STACKLEVEL) |
745 | if (prev__cctx->idle_sp == STACKLEVEL) |
719 | { |
746 | { |
720 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
747 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
721 | assert (PL_top_env == prev__cctx->top_env); |
748 | assert (PL_top_env == prev__cctx->top_env); |
722 | |
749 | |
|
|
750 | prev->cctx = 0; |
|
|
751 | |
723 | cctx_put (prev__cctx); |
752 | cctx_put (prev__cctx); |
724 | prev->cctx = 0; |
753 | prev__cctx->inuse = 0; |
725 | } |
754 | } |
726 | |
755 | |
727 | if (!next->cctx) |
756 | if (!next->cctx) |
|
|
757 | { |
728 | next->cctx = cctx_get (); |
758 | next->cctx = cctx_get (); |
|
|
759 | assert (!next->cctx->inuse); |
|
|
760 | next->cctx->inuse = 1; |
|
|
761 | } |
729 | |
762 | |
730 | if (prev__cctx != next->cctx) |
763 | if (prev__cctx != next->cctx) |
731 | { |
764 | { |
732 | assert ( prev__cctx->inuse); |
|
|
733 | assert (!next->cctx->inuse); |
|
|
734 | |
|
|
735 | prev__cctx->inuse = 0; |
|
|
736 | next->cctx->inuse = 1; |
|
|
737 | |
|
|
738 | prev__cctx->top_env = PL_top_env; |
765 | prev__cctx->top_env = PL_top_env; |
739 | PL_top_env = next->cctx->top_env; |
766 | PL_top_env = next->cctx->top_env; |
740 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
767 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
741 | } |
768 | } |
742 | |
769 | |
… | |
… | |
747 | } |
774 | } |
748 | |
775 | |
749 | struct transfer_args |
776 | struct transfer_args |
750 | { |
777 | { |
751 | struct coro *prev, *next; |
778 | struct coro *prev, *next; |
752 | int flags; |
|
|
753 | }; |
779 | }; |
754 | |
780 | |
755 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) |
781 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
756 | |
782 | |
757 | static void |
783 | static void |
758 | coro_state_destroy (struct coro *coro) |
784 | coro_state_destroy (struct coro *coro) |
759 | { |
785 | { |
760 | if (coro->refcnt--) |
786 | if (coro->refcnt--) |
761 | return; |
787 | return; |
762 | |
|
|
763 | if (coro->flags & CF_RUNNING) |
|
|
764 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
765 | |
788 | |
766 | if (coro->mainstack && coro->mainstack != main_mainstack) |
789 | if (coro->mainstack && coro->mainstack != main_mainstack) |
767 | { |
790 | { |
768 | struct coro temp; |
791 | struct coro temp; |
|
|
792 | Zero (&temp, 1, struct coro); |
|
|
793 | temp.save = CORO_SAVE_ALL; |
769 | |
794 | |
770 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
795 | if (coro->flags & CF_RUNNING) |
771 | LOAD (coro); |
796 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
797 | |
|
|
798 | save_perl (&temp); |
|
|
799 | load_perl (coro); |
772 | |
800 | |
773 | coro_destroy_stacks (); |
801 | coro_destroy_stacks (); |
774 | |
802 | |
775 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
803 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
776 | |
804 | |
777 | coro->mainstack = 0; |
805 | coro->mainstack = 0; |
778 | } |
806 | } |
779 | |
807 | |
780 | cctx_free (coro->cctx); |
808 | cctx_destroy (coro->cctx); |
781 | SvREFCNT_dec (coro->args); |
809 | SvREFCNT_dec (coro->args); |
782 | Safefree (coro); |
810 | Safefree (coro); |
783 | } |
811 | } |
784 | |
812 | |
785 | static int |
813 | static int |
… | |
… | |
835 | assert (mg->mg_type == PERL_MAGIC_ext); |
863 | assert (mg->mg_type == PERL_MAGIC_ext); |
836 | return (struct coro *)mg->mg_ptr; |
864 | return (struct coro *)mg->mg_ptr; |
837 | } |
865 | } |
838 | |
866 | |
839 | static void |
867 | static void |
840 | prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) |
868 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
841 | { |
869 | { |
842 | ta->prev = SvSTATE (prev); |
870 | ta->prev = SvSTATE (prev_sv); |
843 | ta->next = SvSTATE (next); |
871 | ta->next = SvSTATE (next_sv); |
844 | ta->flags = flags; |
|
|
845 | } |
872 | } |
846 | |
873 | |
847 | static void |
874 | static void |
848 | api_transfer (SV *prev, SV *next, int flags) |
875 | api_transfer (SV *prev_sv, SV *next_sv) |
849 | { |
876 | { |
850 | dTHX; |
|
|
851 | struct transfer_args ta; |
877 | struct transfer_args ta; |
852 | |
878 | |
853 | prepare_transfer (&ta, prev, next, flags); |
879 | prepare_transfer (&ta, prev_sv, next_sv); |
854 | TRANSFER (ta); |
880 | TRANSFER (ta); |
|
|
881 | } |
|
|
882 | |
|
|
883 | static int |
|
|
884 | api_save (SV *coro_sv, int new_save) |
|
|
885 | { |
|
|
886 | struct coro *coro = SvSTATE (coro_sv); |
|
|
887 | int old_save = coro->save; |
|
|
888 | |
|
|
889 | if (new_save >= 0) |
|
|
890 | coro->save = new_save; |
|
|
891 | |
|
|
892 | return old_save; |
855 | } |
893 | } |
856 | |
894 | |
857 | /** Coro ********************************************************************/ |
895 | /** Coro ********************************************************************/ |
858 | |
896 | |
859 | #define PRIO_MAX 3 |
897 | #define PRIO_MAX 3 |
… | |
… | |
862 | #define PRIO_LOW -1 |
900 | #define PRIO_LOW -1 |
863 | #define PRIO_IDLE -3 |
901 | #define PRIO_IDLE -3 |
864 | #define PRIO_MIN -4 |
902 | #define PRIO_MIN -4 |
865 | |
903 | |
866 | /* for Coro.pm */ |
904 | /* for Coro.pm */ |
867 | static GV *coro_current, *coro_idle; |
905 | static SV *coro_current; |
868 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
906 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
869 | static int coro_nready; |
907 | static int coro_nready; |
870 | |
908 | |
871 | static void |
909 | static void |
872 | coro_enq (SV *coro_sv) |
910 | coro_enq (SV *coro_sv) |
… | |
… | |
905 | coro = SvSTATE (coro_sv); |
943 | coro = SvSTATE (coro_sv); |
906 | |
944 | |
907 | if (coro->flags & CF_READY) |
945 | if (coro->flags & CF_READY) |
908 | return 0; |
946 | return 0; |
909 | |
947 | |
|
|
948 | #if 0 /* this is actually harmless */ |
910 | if (coro->flags & CF_RUNNING) |
949 | if (coro->flags & CF_RUNNING) |
911 | croak ("Coro::ready called on currently running coroutine"); |
950 | croak ("Coro::ready called on currently running coroutine"); |
|
|
951 | #endif |
912 | |
952 | |
913 | coro->flags |= CF_READY; |
953 | coro->flags |= CF_READY; |
914 | |
954 | |
915 | LOCK; |
955 | LOCK; |
916 | coro_enq (SvREFCNT_inc (coro_sv)); |
956 | coro_enq (SvREFCNT_inc (coro_sv)); |
… | |
… | |
926 | } |
966 | } |
927 | |
967 | |
928 | static void |
968 | static void |
929 | prepare_schedule (struct transfer_args *ta) |
969 | prepare_schedule (struct transfer_args *ta) |
930 | { |
970 | { |
931 | SV *current, *prev, *next; |
971 | SV *prev, *next; |
932 | |
|
|
933 | current = GvSV (coro_current); |
|
|
934 | |
972 | |
935 | for (;;) |
973 | for (;;) |
936 | { |
974 | { |
937 | LOCK; |
975 | LOCK; |
938 | next = coro_deq (PRIO_MIN); |
976 | next = coro_deq (PRIO_MIN); |
… | |
… | |
947 | ENTER; |
985 | ENTER; |
948 | SAVETMPS; |
986 | SAVETMPS; |
949 | |
987 | |
950 | PUSHMARK (SP); |
988 | PUSHMARK (SP); |
951 | PUTBACK; |
989 | PUTBACK; |
952 | call_sv (GvSV (coro_idle), G_DISCARD); |
990 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
953 | |
991 | |
954 | FREETMPS; |
992 | FREETMPS; |
955 | LEAVE; |
993 | LEAVE; |
956 | } |
994 | } |
957 | } |
995 | } |
958 | |
996 | |
959 | prev = SvRV (current); |
997 | prev = SvRV (coro_current); |
960 | SvRV (current) = next; |
998 | SvRV_set (coro_current, next); |
961 | |
999 | |
962 | /* free this only after the transfer */ |
1000 | /* free this only after the transfer */ |
963 | LOCK; |
1001 | LOCK; |
964 | free_coro_mortal (); |
1002 | free_coro_mortal (); |
965 | UNLOCK; |
1003 | UNLOCK; |
966 | coro_mortal = prev; |
1004 | coro_mortal = prev; |
967 | |
1005 | |
|
|
1006 | assert (!SvROK(prev));//D |
|
|
1007 | assert (!SvROK(next));//D |
|
|
1008 | |
968 | ta->prev = SvSTATE (prev); |
1009 | ta->prev = SvSTATE (prev); |
969 | ta->next = SvSTATE (next); |
1010 | ta->next = SvSTATE (next); |
970 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
971 | |
1011 | |
|
|
1012 | assert (ta->next->flags & CF_READY); |
972 | ta->next->flags &= ~CF_READY; |
1013 | ta->next->flags &= ~CF_READY; |
973 | } |
1014 | } |
974 | |
1015 | |
975 | static void |
1016 | static void |
976 | prepare_cede (struct transfer_args *ta) |
1017 | prepare_cede (struct transfer_args *ta) |
977 | { |
1018 | { |
978 | api_ready (GvSV (coro_current)); |
1019 | api_ready (coro_current); |
979 | |
1020 | |
980 | prepare_schedule (ta); |
1021 | prepare_schedule (ta); |
981 | } |
1022 | } |
982 | |
1023 | |
983 | static void |
1024 | static void |
984 | api_schedule (void) |
1025 | api_schedule (void) |
985 | { |
1026 | { |
986 | dTHX; |
|
|
987 | struct transfer_args ta; |
1027 | struct transfer_args ta; |
988 | |
1028 | |
989 | prepare_schedule (&ta); |
1029 | prepare_schedule (&ta); |
990 | TRANSFER (ta); |
1030 | TRANSFER (ta); |
991 | } |
1031 | } |
992 | |
1032 | |
993 | static void |
1033 | static void |
994 | api_cede (void) |
1034 | api_cede (void) |
995 | { |
1035 | { |
996 | dTHX; |
|
|
997 | struct transfer_args ta; |
1036 | struct transfer_args ta; |
998 | |
1037 | |
999 | prepare_cede (&ta); |
1038 | prepare_cede (&ta); |
1000 | TRANSFER (ta); |
1039 | TRANSFER (ta); |
1001 | } |
1040 | } |
… | |
… | |
1011 | #endif |
1050 | #endif |
1012 | BOOT_PAGESIZE; |
1051 | BOOT_PAGESIZE; |
1013 | |
1052 | |
1014 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1053 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1015 | |
1054 | |
1016 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
1055 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1017 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
1056 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1018 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
1057 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
|
|
1058 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1059 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1019 | |
1060 | |
1020 | main_mainstack = PL_mainstack; |
1061 | main_mainstack = PL_mainstack; |
1021 | |
1062 | |
1022 | coroapi.ver = CORO_API_VERSION; |
1063 | coroapi.ver = CORO_API_VERSION; |
1023 | coroapi.transfer = api_transfer; |
1064 | coroapi.transfer = api_transfer; |
… | |
… | |
1033 | HV *hv; |
1074 | HV *hv; |
1034 | int i; |
1075 | int i; |
1035 | |
1076 | |
1036 | Newz (0, coro, 1, struct coro); |
1077 | Newz (0, coro, 1, struct coro); |
1037 | coro->args = newAV (); |
1078 | coro->args = newAV (); |
|
|
1079 | coro->save = CORO_SAVE_ALL; |
|
|
1080 | coro->flags = CF_NEW; |
1038 | |
1081 | |
1039 | hv = newHV (); |
1082 | hv = newHV (); |
1040 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1083 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1041 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1084 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1042 | |
1085 | |
1043 | for (i = 1; i < items; i++) |
1086 | for (i = 1; i < items; i++) |
1044 | av_push (coro->args, newSVsv (ST (i))); |
1087 | av_push (coro->args, newSVsv (ST (i))); |
1045 | } |
1088 | } |
1046 | OUTPUT: |
1089 | OUTPUT: |
|
|
1090 | RETVAL |
|
|
1091 | |
|
|
1092 | int |
|
|
1093 | save (SV *coro, int new_save = -1) |
|
|
1094 | CODE: |
|
|
1095 | RETVAL = api_save (coro, new_save); |
|
|
1096 | OUTPUT: |
1047 | RETVAL |
1097 | RETVAL |
1048 | |
1098 | |
1049 | void |
1099 | void |
1050 | _set_stacklevel (...) |
1100 | _set_stacklevel (...) |
1051 | ALIAS: |
1101 | ALIAS: |
1052 | Coro::State::transfer = 1 |
1102 | Coro::State::transfer = 1 |
1053 | Coro::schedule = 2 |
1103 | Coro::schedule = 2 |
1054 | Coro::cede = 3 |
1104 | Coro::cede = 3 |
1055 | Coro::Cont::yield = 4 |
|
|
1056 | CODE: |
1105 | CODE: |
1057 | { |
1106 | { |
1058 | struct transfer_args ta; |
1107 | struct transfer_args ta; |
1059 | |
1108 | |
1060 | switch (ix) |
1109 | switch (ix) |
1061 | { |
1110 | { |
1062 | case 0: |
1111 | case 0: |
1063 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1112 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1064 | ta.next = 0; |
1113 | ta.next = 0; |
1065 | ta.flags = TRANSFER_SET_STACKLEVEL; |
|
|
1066 | break; |
1114 | break; |
1067 | |
1115 | |
1068 | case 1: |
1116 | case 1: |
1069 | if (items != 3) |
1117 | if (items != 2) |
1070 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1118 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1071 | |
1119 | |
1072 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1120 | prepare_transfer (&ta, ST (0), ST (1)); |
1073 | break; |
1121 | break; |
1074 | |
1122 | |
1075 | case 2: |
1123 | case 2: |
1076 | prepare_schedule (&ta); |
1124 | prepare_schedule (&ta); |
1077 | break; |
1125 | break; |
1078 | |
1126 | |
1079 | case 3: |
1127 | case 3: |
1080 | prepare_cede (&ta); |
1128 | prepare_cede (&ta); |
1081 | break; |
1129 | break; |
1082 | |
|
|
1083 | case 4: |
|
|
1084 | { |
|
|
1085 | SV *yieldstack; |
|
|
1086 | SV *sv; |
|
|
1087 | AV *defav = GvAV (PL_defgv); |
|
|
1088 | |
|
|
1089 | yieldstack = *hv_fetch ( |
|
|
1090 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1091 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1092 | 0 |
|
|
1093 | ); |
|
|
1094 | |
|
|
1095 | /* set up @_ -- ugly */ |
|
|
1096 | av_clear (defav); |
|
|
1097 | av_fill (defav, items - 1); |
|
|
1098 | while (items--) |
|
|
1099 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1100 | |
|
|
1101 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1102 | ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1103 | ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1104 | ta.flags = 0; |
|
|
1105 | SvREFCNT_dec (sv); |
|
|
1106 | } |
|
|
1107 | break; |
|
|
1108 | |
|
|
1109 | } |
1130 | } |
1110 | |
1131 | |
1111 | TRANSFER (ta); |
1132 | TRANSFER (ta); |
1112 | } |
1133 | } |
1113 | |
1134 | |
… | |
… | |
1157 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1178 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1158 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1179 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1159 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1180 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1160 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1181 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1161 | |
1182 | |
1162 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1183 | coro_current = get_sv ("Coro::current", FALSE); |
1163 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1184 | SvREADONLY_on (coro_current); |
1164 | |
1185 | |
1165 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1186 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1166 | coro_ready[i] = newAV (); |
1187 | coro_ready[i] = newAV (); |
1167 | |
1188 | |
1168 | { |
1189 | { |
1169 | SV *sv = perl_get_sv("Coro::API", 1); |
1190 | SV *sv = perl_get_sv("Coro::API", 1); |
1170 | |
1191 | |
1171 | coroapi.schedule = api_schedule; |
1192 | coroapi.schedule = api_schedule; |
|
|
1193 | coroapi.save = api_save; |
1172 | coroapi.cede = api_cede; |
1194 | coroapi.cede = api_cede; |
1173 | coroapi.ready = api_ready; |
1195 | coroapi.ready = api_ready; |
1174 | coroapi.is_ready = api_is_ready; |
1196 | coroapi.is_ready = api_is_ready; |
1175 | coroapi.nready = &coro_nready; |
1197 | coroapi.nready = &coro_nready; |
1176 | coroapi.current = coro_current; |
1198 | coroapi.current = coro_current; |
… | |
… | |
1178 | GCoroAPI = &coroapi; |
1200 | GCoroAPI = &coroapi; |
1179 | sv_setiv (sv, (IV)&coroapi); |
1201 | sv_setiv (sv, (IV)&coroapi); |
1180 | SvREADONLY_on (sv); |
1202 | SvREADONLY_on (sv); |
1181 | } |
1203 | } |
1182 | } |
1204 | } |
|
|
1205 | |
|
|
1206 | void |
|
|
1207 | _set_current (SV *current) |
|
|
1208 | PROTOTYPE: $ |
|
|
1209 | CODE: |
|
|
1210 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1211 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
1183 | |
1212 | |
1184 | int |
1213 | int |
1185 | prio (Coro::State coro, int newprio = 0) |
1214 | prio (Coro::State coro, int newprio = 0) |
1186 | ALIAS: |
1215 | ALIAS: |
1187 | nice = 1 |
1216 | nice = 1 |
… | |
… | |
1229 | |
1258 | |
1230 | SV * |
1259 | SV * |
1231 | _get_state () |
1260 | _get_state () |
1232 | CODE: |
1261 | CODE: |
1233 | { |
1262 | { |
1234 | struct { |
1263 | struct io_state *data; |
1235 | int errorno; |
|
|
1236 | int laststype; |
|
|
1237 | int laststatval; |
|
|
1238 | Stat_t statcache; |
|
|
1239 | } data; |
|
|
1240 | |
1264 | |
|
|
1265 | RETVAL = newSV (sizeof (struct io_state)); |
|
|
1266 | data = (struct io_state *)SvPVX (RETVAL); |
|
|
1267 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
|
|
1268 | SvPOK_only (RETVAL); |
|
|
1269 | |
1241 | data.errorno = errno; |
1270 | data->errorno = errno; |
1242 | data.laststype = PL_laststype; |
1271 | data->laststype = PL_laststype; |
1243 | data.laststatval = PL_laststatval; |
1272 | data->laststatval = PL_laststatval; |
1244 | data.statcache = PL_statcache; |
1273 | data->statcache = PL_statcache; |
1245 | |
|
|
1246 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1247 | } |
1274 | } |
1248 | OUTPUT: |
1275 | OUTPUT: |
1249 | RETVAL |
1276 | RETVAL |
1250 | |
1277 | |
1251 | void |
1278 | void |
1252 | _set_state (char *data_) |
1279 | _set_state (char *data_) |
1253 | PROTOTYPE: $ |
1280 | PROTOTYPE: $ |
1254 | CODE: |
1281 | CODE: |
1255 | { |
1282 | { |
1256 | struct { |
1283 | struct io_state *data = (void *)data_; |
1257 | int errorno; |
|
|
1258 | int laststype; |
|
|
1259 | int laststatval; |
|
|
1260 | Stat_t statcache; |
|
|
1261 | } *data = (void *)data_; |
|
|
1262 | |
1284 | |
1263 | errno = data->errorno; |
1285 | errno = data->errorno; |
1264 | PL_laststype = data->laststype; |
1286 | PL_laststype = data->laststype; |
1265 | PL_laststatval = data->laststatval; |
1287 | PL_laststatval = data->laststatval; |
1266 | PL_statcache = data->statcache; |
1288 | PL_statcache = data->statcache; |
1267 | } |
1289 | } |
|
|
1290 | |