|
|
1 | #define PERL_NO_GET_CONTEXT |
|
|
2 | |
|
|
3 | #include "libcoro/coro.c" |
|
|
4 | |
1 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
2 | #include "perl.h" |
6 | #include "perl.h" |
3 | #include "XSUB.h" |
7 | #include "XSUB.h" |
4 | |
8 | |
5 | #include "patchlevel.h" |
9 | #include "patchlevel.h" |
6 | |
10 | |
7 | #if PATCHLEVEL < 6 |
11 | #if PERL_VERSION < 6 |
8 | # ifndef PL_ppaddr |
12 | # ifndef PL_ppaddr |
9 | # define PL_ppaddr ppaddr |
13 | # define PL_ppaddr ppaddr |
10 | # endif |
14 | # endif |
11 | # ifndef call_sv |
15 | # ifndef call_sv |
12 | # define call_sv perl_call_sv |
16 | # define call_sv perl_call_sv |
… | |
… | |
23 | # ifndef IS_PADCONST |
27 | # ifndef IS_PADCONST |
24 | # define IS_PADCONST(v) 0 |
28 | # define IS_PADCONST(v) 0 |
25 | # endif |
29 | # endif |
26 | #endif |
30 | #endif |
27 | |
31 | |
28 | #include "libcoro/coro.c" |
|
|
29 | |
|
|
30 | #include <signal.h> |
32 | #include <errno.h> |
31 | |
33 | |
32 | #ifdef HAVE_MMAP |
34 | #ifdef HAVE_MMAP |
33 | # include <unistd.h> |
35 | # include <unistd.h> |
34 | # include <sys/mman.h> |
36 | # include <sys/mman.h> |
35 | # ifndef MAP_ANONYMOUS |
37 | # ifndef MAP_ANONYMOUS |
… | |
… | |
55 | |
57 | |
56 | #include "CoroAPI.h" |
58 | #include "CoroAPI.h" |
57 | |
59 | |
58 | #ifdef USE_ITHREADS |
60 | #ifdef USE_ITHREADS |
59 | static perl_mutex coro_mutex; |
61 | static perl_mutex coro_mutex; |
60 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
62 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
61 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
63 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
62 | #else |
64 | #else |
63 | # define LOCK 0 |
65 | # define LOCK (void)0 |
64 | # define UNLOCK 0 |
66 | # define UNLOCK (void)0 |
65 | #endif |
67 | #endif |
66 | |
68 | |
67 | static struct CoroAPI coroapi; |
69 | static struct CoroAPI coroapi; |
|
|
70 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
71 | static HV *coro_state_stash; |
|
|
72 | static SV *ucoro_state_sv; |
|
|
73 | static U32 ucoro_state_hash; |
|
|
74 | static SV *coro_mortal; /* will be freed after next transfer */ |
68 | |
75 | |
69 | /* this is actually not only the c stack but also c registers etc... */ |
76 | /* this is actually not only the c stack but also c registers etc... */ |
70 | typedef struct { |
77 | typedef struct { |
71 | int refcnt; /* pointer reference counter */ |
78 | int refcnt; /* pointer reference counter */ |
72 | int usecnt; /* shared by how many coroutines */ |
79 | int usecnt; /* shared by how many coroutines */ |
… | |
… | |
102 | AV *mainstack; |
109 | AV *mainstack; |
103 | SV **stack_sp; |
110 | SV **stack_sp; |
104 | OP *op; |
111 | OP *op; |
105 | SV **curpad; |
112 | SV **curpad; |
106 | AV *comppad; |
113 | AV *comppad; |
|
|
114 | CV *compcv; |
107 | SV **stack_base; |
115 | SV **stack_base; |
108 | SV **stack_max; |
116 | SV **stack_max; |
109 | SV **tmps_stack; |
117 | SV **tmps_stack; |
110 | I32 tmps_floor; |
118 | I32 tmps_floor; |
111 | I32 tmps_ix; |
119 | I32 tmps_ix; |
… | |
… | |
120 | I32 savestack_ix; |
128 | I32 savestack_ix; |
121 | I32 savestack_max; |
129 | I32 savestack_max; |
122 | OP **retstack; |
130 | OP **retstack; |
123 | I32 retstack_ix; |
131 | I32 retstack_ix; |
124 | I32 retstack_max; |
132 | I32 retstack_max; |
|
|
133 | PMOP *curpm; |
125 | COP *curcop; |
134 | COP *curcop; |
126 | JMPENV *top_env; |
135 | JMPENV *top_env; |
127 | |
136 | |
128 | /* data associated with this coroutine (initial args) */ |
137 | /* data associated with this coroutine (initial args) */ |
129 | AV *args; |
138 | AV *args; |
130 | }; |
139 | }; |
131 | |
140 | |
132 | typedef struct coro *Coro__State; |
141 | typedef struct coro *Coro__State; |
133 | typedef struct coro *Coro__State_or_hashref; |
142 | typedef struct coro *Coro__State_or_hashref; |
134 | |
143 | |
135 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
136 | static HV *coro_state_stash; |
|
|
137 | static SV *ucoro_state_sv; |
|
|
138 | static U32 ucoro_state_hash; |
|
|
139 | static SV *coro_mortal; /* will be freed after next transfer */ |
|
|
140 | |
|
|
141 | /* mostly copied from op.c:cv_clone2 */ |
144 | /* mostly copied from op.c:cv_clone2 */ |
142 | STATIC AV * |
145 | STATIC AV * |
143 | clone_padlist (AV *protopadlist) |
146 | clone_padlist (pTHX_ AV *protopadlist) |
144 | { |
147 | { |
145 | AV *av; |
148 | AV *av; |
146 | I32 ix; |
149 | I32 ix; |
147 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
150 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
148 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
151 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
… | |
… | |
167 | av_store (newpadlist, 1, (SV *) newpad); |
170 | av_store (newpadlist, 1, (SV *) newpad); |
168 | |
171 | |
169 | av = newAV (); /* will be @_ */ |
172 | av = newAV (); /* will be @_ */ |
170 | av_extend (av, 0); |
173 | av_extend (av, 0); |
171 | av_store (newpad, 0, (SV *) av); |
174 | av_store (newpad, 0, (SV *) av); |
172 | AvFLAGS (av) = AVf_REIFY; |
175 | AvREIFY_on (av); |
173 | |
176 | |
174 | for (ix = fpad; ix > 0; ix--) |
177 | for (ix = fpad; ix > 0; ix--) |
175 | { |
178 | { |
176 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
179 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
177 | |
180 | |
… | |
… | |
237 | |
240 | |
238 | return newpadlist; |
241 | return newpadlist; |
239 | } |
242 | } |
240 | |
243 | |
241 | STATIC void |
244 | STATIC void |
242 | free_padlist (AV *padlist) |
245 | free_padlist (pTHX_ AV *padlist) |
243 | { |
246 | { |
244 | /* may be during global destruction */ |
247 | /* may be during global destruction */ |
245 | if (SvREFCNT (padlist)) |
248 | if (SvREFCNT (padlist)) |
246 | { |
249 | { |
247 | I32 i = AvFILLp (padlist); |
250 | I32 i = AvFILLp (padlist); |
… | |
… | |
268 | AV *padlist; |
271 | AV *padlist; |
269 | AV *av = (AV *)mg->mg_obj; |
272 | AV *av = (AV *)mg->mg_obj; |
270 | |
273 | |
271 | /* casting is fun. */ |
274 | /* casting is fun. */ |
272 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
275 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
273 | free_padlist (padlist); |
276 | free_padlist (aTHX_ padlist); |
|
|
277 | |
|
|
278 | SvREFCNT_dec (av); |
|
|
279 | |
|
|
280 | return 0; |
274 | } |
281 | } |
275 | |
282 | |
276 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
283 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
277 | |
284 | |
278 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
285 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
279 | |
286 | |
280 | /* the next two functions merely cache the padlists */ |
287 | /* the next two functions merely cache the padlists */ |
281 | STATIC void |
288 | STATIC void |
282 | get_padlist (CV *cv) |
289 | get_padlist (pTHX_ CV *cv) |
283 | { |
290 | { |
284 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
291 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
285 | |
292 | |
286 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
293 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
287 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
294 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
288 | else |
295 | else |
289 | CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); |
296 | CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv)); |
290 | } |
297 | } |
291 | |
298 | |
292 | STATIC void |
299 | STATIC void |
293 | put_padlist (CV *cv) |
300 | put_padlist (pTHX_ CV *cv) |
294 | { |
301 | { |
295 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
302 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
296 | |
303 | |
297 | if (!mg) |
304 | if (!mg) |
298 | { |
305 | { |
… | |
… | |
324 | PL_mainstack = c->mainstack; |
331 | PL_mainstack = c->mainstack; |
325 | PL_stack_sp = c->stack_sp; |
332 | PL_stack_sp = c->stack_sp; |
326 | PL_op = c->op; |
333 | PL_op = c->op; |
327 | PL_curpad = c->curpad; |
334 | PL_curpad = c->curpad; |
328 | PL_comppad = c->comppad; |
335 | PL_comppad = c->comppad; |
|
|
336 | PL_compcv = c->compcv; |
329 | PL_stack_base = c->stack_base; |
337 | PL_stack_base = c->stack_base; |
330 | PL_stack_max = c->stack_max; |
338 | PL_stack_max = c->stack_max; |
331 | PL_tmps_stack = c->tmps_stack; |
339 | PL_tmps_stack = c->tmps_stack; |
332 | PL_tmps_floor = c->tmps_floor; |
340 | PL_tmps_floor = c->tmps_floor; |
333 | PL_tmps_ix = c->tmps_ix; |
341 | PL_tmps_ix = c->tmps_ix; |
… | |
… | |
339 | PL_scopestack_ix = c->scopestack_ix; |
347 | PL_scopestack_ix = c->scopestack_ix; |
340 | PL_scopestack_max = c->scopestack_max; |
348 | PL_scopestack_max = c->scopestack_max; |
341 | PL_savestack = c->savestack; |
349 | PL_savestack = c->savestack; |
342 | PL_savestack_ix = c->savestack_ix; |
350 | PL_savestack_ix = c->savestack_ix; |
343 | PL_savestack_max = c->savestack_max; |
351 | PL_savestack_max = c->savestack_max; |
|
|
352 | #if PERL_VERSION < 9 |
344 | PL_retstack = c->retstack; |
353 | PL_retstack = c->retstack; |
345 | PL_retstack_ix = c->retstack_ix; |
354 | PL_retstack_ix = c->retstack_ix; |
346 | PL_retstack_max = c->retstack_max; |
355 | PL_retstack_max = c->retstack_max; |
|
|
356 | #endif |
|
|
357 | PL_curpm = c->curpm; |
347 | PL_curcop = c->curcop; |
358 | PL_curcop = c->curcop; |
348 | PL_top_env = c->top_env; |
359 | PL_top_env = c->top_env; |
349 | |
360 | |
350 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
361 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
351 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
362 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
… | |
… | |
360 | { |
371 | { |
361 | AV *padlist = (AV *)POPs; |
372 | AV *padlist = (AV *)POPs; |
362 | |
373 | |
363 | if (padlist) |
374 | if (padlist) |
364 | { |
375 | { |
365 | put_padlist (cv); /* mark this padlist as available */ |
376 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
366 | CvPADLIST(cv) = padlist; |
377 | CvPADLIST(cv) = padlist; |
367 | } |
378 | } |
368 | |
379 | |
369 | ++CvDEPTH(cv); |
380 | ++CvDEPTH(cv); |
370 | } |
381 | } |
… | |
… | |
410 | } |
421 | } |
411 | |
422 | |
412 | PUSHs ((SV *)CvPADLIST(cv)); |
423 | PUSHs ((SV *)CvPADLIST(cv)); |
413 | PUSHs ((SV *)cv); |
424 | PUSHs ((SV *)cv); |
414 | |
425 | |
415 | get_padlist (cv); /* this is a monster */ |
426 | get_padlist (aTHX_ cv); /* this is a monster */ |
416 | } |
427 | } |
417 | } |
428 | } |
418 | #ifdef CXt_FORMAT |
429 | #ifdef CXt_FORMAT |
419 | else if (CxTYPE(cx) == CXt_FORMAT) |
430 | else if (CxTYPE(cx) == CXt_FORMAT) |
420 | { |
431 | { |
… | |
… | |
448 | c->mainstack = PL_mainstack; |
459 | c->mainstack = PL_mainstack; |
449 | c->stack_sp = PL_stack_sp; |
460 | c->stack_sp = PL_stack_sp; |
450 | c->op = PL_op; |
461 | c->op = PL_op; |
451 | c->curpad = PL_curpad; |
462 | c->curpad = PL_curpad; |
452 | c->comppad = PL_comppad; |
463 | c->comppad = PL_comppad; |
|
|
464 | c->compcv = PL_compcv; |
453 | c->stack_base = PL_stack_base; |
465 | c->stack_base = PL_stack_base; |
454 | c->stack_max = PL_stack_max; |
466 | c->stack_max = PL_stack_max; |
455 | c->tmps_stack = PL_tmps_stack; |
467 | c->tmps_stack = PL_tmps_stack; |
456 | c->tmps_floor = PL_tmps_floor; |
468 | c->tmps_floor = PL_tmps_floor; |
457 | c->tmps_ix = PL_tmps_ix; |
469 | c->tmps_ix = PL_tmps_ix; |
… | |
… | |
463 | c->scopestack_ix = PL_scopestack_ix; |
475 | c->scopestack_ix = PL_scopestack_ix; |
464 | c->scopestack_max = PL_scopestack_max; |
476 | c->scopestack_max = PL_scopestack_max; |
465 | c->savestack = PL_savestack; |
477 | c->savestack = PL_savestack; |
466 | c->savestack_ix = PL_savestack_ix; |
478 | c->savestack_ix = PL_savestack_ix; |
467 | c->savestack_max = PL_savestack_max; |
479 | c->savestack_max = PL_savestack_max; |
|
|
480 | #if PERL_VERSION < 9 |
468 | c->retstack = PL_retstack; |
481 | c->retstack = PL_retstack; |
469 | c->retstack_ix = PL_retstack_ix; |
482 | c->retstack_ix = PL_retstack_ix; |
470 | c->retstack_max = PL_retstack_max; |
483 | c->retstack_max = PL_retstack_max; |
|
|
484 | #endif |
|
|
485 | c->curpm = PL_curpm; |
471 | c->curcop = PL_curcop; |
486 | c->curcop = PL_curcop; |
472 | c->top_env = PL_top_env; |
487 | c->top_env = PL_top_env; |
473 | } |
488 | } |
474 | |
489 | |
475 | /* |
490 | /* |
… | |
… | |
511 | |
526 | |
512 | New(54,PL_savestack,96,ANY); |
527 | New(54,PL_savestack,96,ANY); |
513 | PL_savestack_ix = 0; |
528 | PL_savestack_ix = 0; |
514 | PL_savestack_max = 96; |
529 | PL_savestack_max = 96; |
515 | |
530 | |
|
|
531 | #if PERL_VERSION < 9 |
516 | New(54,PL_retstack,8,OP*); |
532 | New(54,PL_retstack,8,OP*); |
517 | PL_retstack_ix = 0; |
533 | PL_retstack_ix = 0; |
518 | PL_retstack_max = 8; |
534 | PL_retstack_max = 8; |
|
|
535 | #endif |
519 | |
536 | |
520 | UNLOCK; |
537 | UNLOCK; |
521 | } |
538 | } |
522 | |
539 | |
523 | /* |
540 | /* |
… | |
… | |
531 | /* is this ugly, I ask? */ |
548 | /* is this ugly, I ask? */ |
532 | LEAVE_SCOPE (0); |
549 | LEAVE_SCOPE (0); |
533 | |
550 | |
534 | /* sure it is, but more important: is it correct?? :/ */ |
551 | /* sure it is, but more important: is it correct?? :/ */ |
535 | FREETMPS; |
552 | FREETMPS; |
|
|
553 | |
|
|
554 | /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ |
536 | } |
555 | } |
537 | |
556 | |
538 | while (PL_curstackinfo->si_next) |
557 | while (PL_curstackinfo->si_next) |
539 | PL_curstackinfo = PL_curstackinfo->si_next; |
558 | PL_curstackinfo = PL_curstackinfo->si_next; |
540 | |
559 | |
541 | while (PL_curstackinfo) |
560 | while (PL_curstackinfo) |
542 | { |
561 | { |
543 | PERL_SI *p = PL_curstackinfo->si_prev; |
562 | PERL_SI *p = PL_curstackinfo->si_prev; |
544 | |
563 | |
545 | { |
564 | { /*D*//*remove*/ |
546 | dSP; |
565 | dSP; |
547 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
566 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
548 | PUTBACK; /* possibly superfluous */ |
567 | PUTBACK; /* possibly superfluous */ |
549 | } |
568 | } |
550 | |
569 | |
551 | if (!IN_DESTRUCT) |
570 | if (!IN_DESTRUCT) |
552 | { |
571 | { |
553 | dounwind(-1); |
572 | dounwind (-1);/*D*//*remove*/ |
554 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
573 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
555 | } |
574 | } |
556 | |
575 | |
557 | Safefree(PL_curstackinfo->si_cxstack); |
576 | Safefree (PL_curstackinfo->si_cxstack); |
558 | Safefree(PL_curstackinfo); |
577 | Safefree (PL_curstackinfo); |
559 | PL_curstackinfo = p; |
578 | PL_curstackinfo = p; |
560 | } |
579 | } |
561 | |
580 | |
562 | Safefree(PL_tmps_stack); |
581 | Safefree (PL_tmps_stack); |
563 | Safefree(PL_markstack); |
582 | Safefree (PL_markstack); |
564 | Safefree(PL_scopestack); |
583 | Safefree (PL_scopestack); |
565 | Safefree(PL_savestack); |
584 | Safefree (PL_savestack); |
|
|
585 | #if PERL_VERSION < 9 |
566 | Safefree(PL_retstack); |
586 | Safefree (PL_retstack); |
|
|
587 | #endif |
567 | } |
588 | } |
568 | |
589 | |
569 | static void |
590 | static void |
570 | allocate_stack (Coro__State ctx, int alloc) |
591 | allocate_stack (Coro__State ctx, int alloc) |
571 | { |
592 | { |
… | |
… | |
578 | stack->gencnt = ctx->gencnt = 0; |
599 | stack->gencnt = ctx->gencnt = 0; |
579 | |
600 | |
580 | if (alloc) |
601 | if (alloc) |
581 | { |
602 | { |
582 | #if HAVE_MMAP |
603 | #if HAVE_MMAP |
583 | stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ |
604 | stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */ |
584 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
605 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
585 | if (stack->sptr == (void *)-1) |
606 | if (stack->sptr == (void *)-1) |
586 | #endif |
607 | #endif |
587 | { |
608 | { |
588 | /*FIXME*//*D*//* reasonable stack size! */ |
|
|
589 | stack->ssize = - (8192 * sizeof (long)); |
609 | stack->ssize = - (STACKSIZE * (long)sizeof (long)); |
590 | New (0, stack->sptr, 8192, long); |
610 | New (0, stack->sptr, STACKSIZE, long); |
591 | } |
611 | } |
592 | } |
612 | } |
593 | else |
613 | else |
594 | stack->sptr = 0; |
614 | stack->sptr = 0; |
595 | |
615 | |
… | |
… | |
625 | setup_coro (void *arg) |
645 | setup_coro (void *arg) |
626 | { |
646 | { |
627 | /* |
647 | /* |
628 | * emulate part of the perl startup here. |
648 | * emulate part of the perl startup here. |
629 | */ |
649 | */ |
|
|
650 | dTHX; |
630 | dSP; |
651 | dSP; |
631 | Coro__State ctx = (Coro__State)arg; |
652 | Coro__State ctx = (Coro__State)arg; |
632 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
653 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
633 | |
654 | |
634 | coro_init_stacks (aTHX); |
655 | coro_init_stacks (aTHX); |
… | |
… | |
683 | continue_coro (void *arg) |
704 | continue_coro (void *arg) |
684 | { |
705 | { |
685 | /* |
706 | /* |
686 | * this is a _very_ stripped down perl interpreter ;) |
707 | * this is a _very_ stripped down perl interpreter ;) |
687 | */ |
708 | */ |
|
|
709 | dTHX; |
688 | Coro__State ctx = (Coro__State)arg; |
710 | Coro__State ctx = (Coro__State)arg; |
689 | JMPENV coro_start_env; |
|
|
690 | |
711 | |
691 | PL_top_env = &ctx->start_env; |
712 | PL_top_env = &ctx->start_env; |
692 | |
713 | |
693 | ctx->cursp = 0; |
714 | ctx->cursp = 0; |
694 | PL_op = PL_op->op_next; |
715 | PL_op = PL_op->op_next; |
… | |
… | |
817 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
838 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
818 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
839 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
819 | \ |
840 | \ |
820 | } while(0) |
841 | } while(0) |
821 | |
842 | |
822 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
843 | #define SvSTATE(sv) INT2PTR (struct coro *, SvIV (sv)) |
823 | |
844 | |
824 | static void |
845 | static void |
825 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
846 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
826 | { |
847 | { |
827 | SV_CORO (prev, "Coro::transfer"); |
848 | SV_CORO (prev, "Coro::transfer"); |
828 | SV_CORO (next, "Coro::transfer"); |
849 | SV_CORO (next, "Coro::transfer"); |
829 | |
850 | |
830 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
851 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
831 | } |
852 | } |
832 | |
853 | |
833 | /** Coro ********************************************************************/ |
854 | /** Coro ********************************************************************/ |
834 | |
855 | |
835 | #define PRIO_MAX 3 |
856 | #define PRIO_MAX 3 |
… | |
… | |
843 | static GV *coro_current, *coro_idle; |
864 | static GV *coro_current, *coro_idle; |
844 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
865 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
845 | static int coro_nready; |
866 | static int coro_nready; |
846 | |
867 | |
847 | static void |
868 | static void |
848 | coro_enq (SV *sv) |
869 | coro_enq (pTHX_ SV *sv) |
849 | { |
870 | { |
|
|
871 | SV **xprio; |
|
|
872 | int prio; |
|
|
873 | |
850 | if (SvTYPE (sv) == SVt_PVHV) |
874 | if (SvTYPE (sv) != SVt_PVHV) |
851 | { |
|
|
852 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
|
|
853 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
854 | |
|
|
855 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
856 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
857 | : prio; |
|
|
858 | |
|
|
859 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
860 | coro_nready++; |
|
|
861 | |
|
|
862 | return; |
|
|
863 | } |
|
|
864 | |
|
|
865 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
875 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
876 | |
|
|
877 | xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
|
|
878 | prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
879 | |
|
|
880 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
881 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
882 | : prio; |
|
|
883 | |
|
|
884 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
885 | coro_nready++; |
866 | } |
886 | } |
867 | |
887 | |
868 | static SV * |
888 | static SV * |
869 | coro_deq (int min_prio) |
889 | coro_deq (pTHX_ int min_prio) |
870 | { |
890 | { |
871 | int prio = PRIO_MAX - PRIO_MIN; |
891 | int prio = PRIO_MAX - PRIO_MIN; |
872 | |
892 | |
873 | min_prio -= PRIO_MIN; |
893 | min_prio -= PRIO_MIN; |
874 | if (min_prio < 0) |
894 | if (min_prio < 0) |
… | |
… | |
885 | } |
905 | } |
886 | |
906 | |
887 | static void |
907 | static void |
888 | api_ready (SV *coro) |
908 | api_ready (SV *coro) |
889 | { |
909 | { |
|
|
910 | dTHX; |
|
|
911 | |
890 | if (SvROK (coro)) |
912 | if (SvROK (coro)) |
891 | coro = SvRV (coro); |
913 | coro = SvRV (coro); |
892 | |
914 | |
893 | LOCK; |
915 | LOCK; |
894 | coro_enq (SvREFCNT_inc (coro)); |
916 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
895 | UNLOCK; |
917 | UNLOCK; |
896 | } |
918 | } |
897 | |
919 | |
898 | static void |
920 | static void |
899 | api_schedule (void) |
921 | api_schedule (void) |
900 | { |
922 | { |
|
|
923 | dTHX; |
|
|
924 | |
901 | SV *prev, *next; |
925 | SV *prev, *next; |
902 | |
926 | |
903 | LOCK; |
927 | LOCK; |
904 | |
928 | |
905 | prev = SvRV (GvSV (coro_current)); |
929 | prev = SvRV (GvSV (coro_current)); |
906 | next = coro_deq (PRIO_MIN); |
930 | next = coro_deq (aTHX_ PRIO_MIN); |
907 | |
931 | |
908 | if (!next) |
932 | if (!next) |
909 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
933 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
910 | |
934 | |
911 | /* free this only after the transfer */ |
935 | /* free this only after the transfer */ |
… | |
… | |
923 | } |
947 | } |
924 | |
948 | |
925 | static void |
949 | static void |
926 | api_cede (void) |
950 | api_cede (void) |
927 | { |
951 | { |
|
|
952 | dTHX; |
|
|
953 | |
928 | LOCK; |
954 | LOCK; |
929 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
955 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
930 | UNLOCK; |
956 | UNLOCK; |
931 | |
957 | |
932 | api_schedule (); |
958 | api_schedule (); |
933 | } |
959 | } |
934 | |
960 | |
… | |
… | |
968 | croak ("Coro::State::_newprocess expects an arrayref"); |
994 | croak ("Coro::State::_newprocess expects an arrayref"); |
969 | |
995 | |
970 | Newz (0, coro, 1, struct coro); |
996 | Newz (0, coro, 1, struct coro); |
971 | |
997 | |
972 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
998 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
973 | coro->mainstack = 0; /* actual work is done inside transfer */ |
999 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
974 | coro->stack = 0; |
1000 | /*coro->stack = 0;*/ |
975 | |
1001 | |
976 | /* same as JMPENV_BOOTSTRAP */ |
1002 | /* same as JMPENV_BOOTSTRAP */ |
977 | /* we might be able to recycle start_env, but safe is safe */ |
1003 | /* we might be able to recycle start_env, but safe is safe */ |
978 | //Zero(&coro->start_env, 1, JMPENV); |
1004 | /*Zero(&coro->start_env, 1, JMPENV);*/ |
979 | coro->start_env.je_ret = -1; |
1005 | coro->start_env.je_ret = -1; |
980 | coro->start_env.je_mustcatch = TRUE; |
1006 | coro->start_env.je_mustcatch = TRUE; |
981 | |
1007 | |
982 | RETVAL = coro; |
1008 | RETVAL = coro; |
983 | OUTPUT: |
1009 | OUTPUT: |
… | |
… | |
1004 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1030 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1005 | { |
1031 | { |
1006 | struct coro temp; |
1032 | struct coro temp; |
1007 | |
1033 | |
1008 | PUTBACK; |
1034 | PUTBACK; |
1009 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
1035 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
1010 | LOAD(aTHX_ coro); |
1036 | LOAD (aTHX_ coro); |
1011 | SPAGAIN; |
1037 | SPAGAIN; |
1012 | |
1038 | |
1013 | destroy_stacks (aTHX); |
1039 | destroy_stacks (aTHX); |
1014 | |
1040 | |
1015 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
1041 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
1016 | SPAGAIN; |
1042 | SPAGAIN; |
1017 | |
1043 | |
1018 | coro->mainstack = 0; |
1044 | coro->mainstack = 0; |
1019 | } |
1045 | } |
1020 | |
1046 | |
… | |
… | |
1035 | |
1061 | |
1036 | void |
1062 | void |
1037 | yield(...) |
1063 | yield(...) |
1038 | PROTOTYPE: @ |
1064 | PROTOTYPE: @ |
1039 | CODE: |
1065 | CODE: |
1040 | static SV *returnstk; |
1066 | SV *yieldstack; |
1041 | SV *sv; |
1067 | SV *sv; |
1042 | AV *defav = GvAV (PL_defgv); |
1068 | AV *defav = GvAV (PL_defgv); |
1043 | struct coro *prev, *next; |
1069 | struct coro *prev, *next; |
1044 | |
1070 | |
1045 | if (!returnstk) |
1071 | yieldstack = *hv_fetch ( |
1046 | returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); |
1072 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1073 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1074 | 0 |
|
|
1075 | ); |
1047 | |
1076 | |
1048 | /* set up @_ -- ugly */ |
1077 | /* set up @_ -- ugly */ |
1049 | av_clear (defav); |
1078 | av_clear (defav); |
1050 | av_fill (defav, items - 1); |
1079 | av_fill (defav, items - 1); |
1051 | while (items--) |
1080 | while (items--) |
1052 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1081 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1053 | |
1082 | |
1054 | mg_get (returnstk); /* isn't documentation wrong for mg_get? */ |
|
|
1055 | sv = av_pop ((AV *)SvRV (returnstk)); |
1083 | sv = av_pop ((AV *)SvRV (yieldstack)); |
1056 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1084 | prev = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)))); |
1057 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1085 | next = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)))); |
1058 | SvREFCNT_dec (sv); |
1086 | SvREFCNT_dec (sv); |
1059 | |
1087 | |
1060 | transfer(aTHX_ prev, next, 0); |
1088 | transfer (aTHX_ prev, next, 0); |
1061 | |
1089 | |
1062 | MODULE = Coro::State PACKAGE = Coro |
1090 | MODULE = Coro::State PACKAGE = Coro |
1063 | |
1091 | |
1064 | # this is slightly dirty (should expose a c-level api) |
1092 | # this is slightly dirty (should expose a c-level api) |
1065 | |
1093 | |
… | |
… | |
1125 | cede(...) |
1153 | cede(...) |
1126 | PROTOTYPE: |
1154 | PROTOTYPE: |
1127 | CODE: |
1155 | CODE: |
1128 | api_cede (); |
1156 | api_cede (); |
1129 | |
1157 | |
|
|
1158 | # and these are hacks |
|
|
1159 | SV * |
|
|
1160 | _aio_get_state () |
|
|
1161 | CODE: |
|
|
1162 | { |
|
|
1163 | struct { |
|
|
1164 | int errorno; |
|
|
1165 | int laststype; |
|
|
1166 | int laststatval; |
|
|
1167 | Stat_t statcache; |
|
|
1168 | } data; |
|
|
1169 | |
|
|
1170 | data.errorno = errno; |
|
|
1171 | data.laststype = PL_laststype; |
|
|
1172 | data.laststatval = PL_laststatval; |
|
|
1173 | data.statcache = PL_statcache; |
|
|
1174 | |
|
|
1175 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1176 | } |
|
|
1177 | OUTPUT: |
|
|
1178 | RETVAL |
|
|
1179 | |
|
|
1180 | void |
|
|
1181 | _aio_set_state (char *data_) |
|
|
1182 | PROTOTYPE: $ |
|
|
1183 | CODE: |
|
|
1184 | { |
|
|
1185 | struct { |
|
|
1186 | int errorno; |
|
|
1187 | int laststype; |
|
|
1188 | int laststatval; |
|
|
1189 | Stat_t statcache; |
|
|
1190 | } *data = (void *)data_; |
|
|
1191 | |
|
|
1192 | errno = data->errorno; |
|
|
1193 | PL_laststype = data->laststype; |
|
|
1194 | PL_laststatval = data->laststatval; |
|
|
1195 | PL_statcache = data->statcache; |
|
|
1196 | } |