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