… | |
… | |
4 | #include "perl.h" |
4 | #include "perl.h" |
5 | #include "XSUB.h" |
5 | #include "XSUB.h" |
6 | |
6 | |
7 | #include "patchlevel.h" |
7 | #include "patchlevel.h" |
8 | |
8 | |
9 | #if USE_VALGRIND |
|
|
10 | # include <valgrind/valgrind.h> |
|
|
11 | #endif |
|
|
12 | |
|
|
13 | /* the maximum number of idle cctx that will be pooled */ |
|
|
14 | #define MAX_IDLE_CCTX 8 |
|
|
15 | |
|
|
16 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
|
|
17 | (PERL_REVISION > (a) \ |
|
|
18 | || (PERL_REVISION == (a) \ |
|
|
19 | && (PERL_VERSION > (b) \ |
|
|
20 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
|
|
21 | |
|
|
22 | #if !PERL_VERSION_ATLEAST (5,6,0) |
|
|
23 | # ifndef PL_ppaddr |
|
|
24 | # define PL_ppaddr ppaddr |
|
|
25 | # endif |
|
|
26 | # ifndef call_sv |
|
|
27 | # define call_sv perl_call_sv |
|
|
28 | # endif |
|
|
29 | # ifndef get_sv |
|
|
30 | # define get_sv perl_get_sv |
|
|
31 | # endif |
|
|
32 | # ifndef get_cv |
|
|
33 | # define get_cv perl_get_cv |
|
|
34 | # endif |
|
|
35 | # ifndef IS_PADGV |
|
|
36 | # define IS_PADGV(v) 0 |
|
|
37 | # endif |
|
|
38 | # ifndef IS_PADCONST |
|
|
39 | # define IS_PADCONST(v) 0 |
|
|
40 | # endif |
|
|
41 | #endif |
|
|
42 | |
|
|
43 | #include <stdio.h> |
9 | #include <stdio.h> |
44 | #include <errno.h> |
10 | #include <errno.h> |
45 | |
11 | #include <assert.h> |
46 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
|
|
47 | # undef STACKGUARD |
|
|
48 | #endif |
|
|
49 | |
|
|
50 | #ifndef STACKGUARD |
|
|
51 | # define STACKGUARD 0 |
|
|
52 | #endif |
|
|
53 | |
12 | |
54 | #ifdef HAVE_MMAP |
13 | #ifdef HAVE_MMAP |
55 | # include <unistd.h> |
14 | # include <unistd.h> |
56 | # include <sys/mman.h> |
15 | # include <sys/mman.h> |
57 | # ifndef MAP_ANONYMOUS |
16 | # ifndef MAP_ANONYMOUS |
… | |
… | |
72 | #else |
31 | #else |
73 | # define PAGESIZE 0 |
32 | # define PAGESIZE 0 |
74 | # define BOOT_PAGESIZE (void)0 |
33 | # define BOOT_PAGESIZE (void)0 |
75 | #endif |
34 | #endif |
76 | |
35 | |
|
|
36 | #if USE_VALGRIND |
|
|
37 | # include <valgrind/valgrind.h> |
|
|
38 | #endif |
|
|
39 | |
|
|
40 | /* the maximum number of idle cctx that will be pooled */ |
|
|
41 | #define MAX_IDLE_CCTX 8 |
|
|
42 | |
|
|
43 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
|
|
44 | (PERL_REVISION > (a) \ |
|
|
45 | || (PERL_REVISION == (a) \ |
|
|
46 | && (PERL_VERSION > (b) \ |
|
|
47 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
|
|
48 | |
|
|
49 | #if !PERL_VERSION_ATLEAST (5,6,0) |
|
|
50 | # ifndef PL_ppaddr |
|
|
51 | # define PL_ppaddr ppaddr |
|
|
52 | # endif |
|
|
53 | # ifndef call_sv |
|
|
54 | # define call_sv perl_call_sv |
|
|
55 | # endif |
|
|
56 | # ifndef get_sv |
|
|
57 | # define get_sv perl_get_sv |
|
|
58 | # endif |
|
|
59 | # ifndef get_cv |
|
|
60 | # define get_cv perl_get_cv |
|
|
61 | # endif |
|
|
62 | # ifndef IS_PADGV |
|
|
63 | # define IS_PADGV(v) 0 |
|
|
64 | # endif |
|
|
65 | # ifndef IS_PADCONST |
|
|
66 | # define IS_PADCONST(v) 0 |
|
|
67 | # endif |
|
|
68 | #endif |
|
|
69 | |
|
|
70 | /* 5.8.7 */ |
|
|
71 | #ifndef SvRV_set |
|
|
72 | # define SvRV_set(s,v) SvRV(s) = (v) |
|
|
73 | #endif |
|
|
74 | |
|
|
75 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
|
|
76 | # undef STACKGUARD |
|
|
77 | #endif |
|
|
78 | |
|
|
79 | #ifndef STACKGUARD |
|
|
80 | # define STACKGUARD 0 |
|
|
81 | #endif |
|
|
82 | |
|
|
83 | /* prefer perl internal functions over our own? */ |
|
|
84 | #ifndef PREFER_PERL_FUNCTIONS |
|
|
85 | # define PREFER_PERL_FUNCTIONS 0 |
|
|
86 | #endif |
|
|
87 | |
77 | /* The next macro should declare a variable stacklevel that contains and approximation |
88 | /* The next macro should declare a variable stacklevel that contains and approximation |
78 | * to the current C stack pointer. Its property is that it changes with each call |
89 | * to the current C stack pointer. Its property is that it changes with each call |
79 | * and should be unique. */ |
90 | * and should be unique. */ |
80 | #define dSTACKLEVEL int stacklevel |
91 | #define dSTACKLEVEL int stacklevel |
81 | #define STACKLEVEL ((void *)&stacklevel) |
92 | #define STACKLEVEL ((void *)&stacklevel) |
82 | |
93 | |
83 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
94 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
84 | |
95 | |
85 | #if __GNUC__ >= 3 |
96 | #if __GNUC__ >= 3 |
86 | # define attribute(x) __attribute__(x) |
97 | # define attribute(x) __attribute__(x) |
|
|
98 | # define BARRIER __asm__ __volatile__ ("" : : : "memory") |
87 | #else |
99 | #else |
88 | # define attribute(x) |
100 | # define attribute(x) |
|
|
101 | # define BARRIER |
89 | #endif |
102 | #endif |
90 | |
103 | |
91 | #define NOINLINE attribute ((noinline)) |
104 | #define NOINLINE attribute ((noinline)) |
92 | |
105 | |
93 | #include "CoroAPI.h" |
106 | #include "CoroAPI.h" |
94 | |
|
|
95 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
|
|
96 | |
107 | |
97 | #ifdef USE_ITHREADS |
108 | #ifdef USE_ITHREADS |
98 | static perl_mutex coro_mutex; |
109 | static perl_mutex coro_mutex; |
99 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
110 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
100 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
111 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
… | |
… | |
126 | /* the stack */ |
137 | /* the stack */ |
127 | void *sptr; |
138 | void *sptr; |
128 | long ssize; /* positive == mmap, otherwise malloc */ |
139 | long ssize; /* positive == mmap, otherwise malloc */ |
129 | |
140 | |
130 | /* cpu state */ |
141 | /* cpu state */ |
131 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
142 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
143 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
132 | JMPENV *top_env; |
144 | JMPENV *top_env; |
133 | coro_context cctx; |
145 | coro_context cctx; |
134 | |
146 | |
135 | int inuse; |
147 | int inuse; |
136 | |
148 | |
… | |
… | |
151 | coro_cctx *cctx; |
163 | coro_cctx *cctx; |
152 | |
164 | |
153 | /* data associated with this coroutine (initial args) */ |
165 | /* data associated with this coroutine (initial args) */ |
154 | AV *args; |
166 | AV *args; |
155 | int refcnt; |
167 | int refcnt; |
156 | int flags; |
168 | int save; /* CORO_SAVE flags */ |
|
|
169 | int flags; /* CF_ flags */ |
157 | |
170 | |
158 | /* optionally saved, might be zero */ |
171 | /* optionally saved, might be zero */ |
159 | AV *defav; |
172 | AV *defav; /* @_ */ |
160 | SV *defsv; |
173 | SV *defsv; /* $_ */ |
161 | SV *errsv; |
174 | SV *errsv; /* $@ */ |
|
|
175 | SV *irssv; /* $/ */ |
|
|
176 | SV *irssv_sv; /* real $/ cache */ |
162 | |
177 | |
163 | #define VAR(name,type) type name; |
178 | #define VAR(name,type) type name; |
164 | # include "state.h" |
179 | # include "state.h" |
165 | #undef VAR |
180 | #undef VAR |
166 | |
181 | |
… | |
… | |
252 | |
267 | |
253 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
268 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
254 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
269 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
255 | else |
270 | else |
256 | { |
271 | { |
257 | #if 0 |
272 | #if PREFER_PERL_FUNCTIONS |
258 | /* this is probably cleaner, but also slower? */ |
273 | /* this is probably cleaner, but also slower? */ |
259 | CV *cp = Perl_cv_clone (cv); |
274 | CV *cp = Perl_cv_clone (cv); |
260 | CvPADLIST (cv) = CvPADLIST (cp); |
275 | CvPADLIST (cv) = CvPADLIST (cp); |
261 | CvPADLIST (cp) = 0; |
276 | CvPADLIST (cp) = 0; |
262 | SvREFCNT_dec (cp); |
277 | SvREFCNT_dec (cp); |
… | |
… | |
289 | } |
304 | } |
290 | |
305 | |
291 | #define SB do { |
306 | #define SB do { |
292 | #define SE } while (0) |
307 | #define SE } while (0) |
293 | |
308 | |
294 | #define LOAD(state) load_state ((state)) |
|
|
295 | #define SAVE(state,flags) save_state ((state),(flags)) |
|
|
296 | |
|
|
297 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
309 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
298 | |
310 | |
299 | static void |
311 | static void |
300 | load_state (Coro__State c) |
312 | load_perl (Coro__State c) |
301 | { |
313 | { |
302 | #define VAR(name,type) PL_ ## name = c->name; |
314 | #define VAR(name,type) PL_ ## name = c->name; |
303 | # include "state.h" |
315 | # include "state.h" |
304 | #undef VAR |
316 | #undef VAR |
305 | |
317 | |
306 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
318 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
307 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
319 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
308 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
320 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
|
|
321 | if (c->irssv) |
|
|
322 | { |
|
|
323 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
|
|
324 | SvREFCNT_dec (c->irssv); |
|
|
325 | else |
|
|
326 | { |
|
|
327 | REPLACE_SV (PL_rs, c->irssv); |
|
|
328 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
|
|
329 | sv_setsv (c->irssv_sv, PL_rs); |
|
|
330 | } |
|
|
331 | } |
309 | |
332 | |
310 | { |
333 | { |
311 | dSP; |
334 | dSP; |
312 | CV *cv; |
335 | CV *cv; |
313 | |
336 | |
… | |
… | |
322 | PUTBACK; |
345 | PUTBACK; |
323 | } |
346 | } |
324 | } |
347 | } |
325 | |
348 | |
326 | static void |
349 | static void |
327 | save_state (Coro__State c, int flags) |
350 | save_perl (Coro__State c) |
328 | { |
351 | { |
329 | { |
352 | { |
330 | dSP; |
353 | dSP; |
331 | I32 cxix = cxstack_ix; |
354 | I32 cxix = cxstack_ix; |
332 | PERL_CONTEXT *ccstk = cxstack; |
355 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
335 | /* |
358 | /* |
336 | * the worst thing you can imagine happens first - we have to save |
359 | * the worst thing you can imagine happens first - we have to save |
337 | * (and reinitialize) all cv's in the whole callchain :( |
360 | * (and reinitialize) all cv's in the whole callchain :( |
338 | */ |
361 | */ |
339 | |
362 | |
|
|
363 | EXTEND (SP, 3 + 1); |
340 | PUSHs (Nullsv); |
364 | PUSHs (Nullsv); |
341 | /* this loop was inspired by pp_caller */ |
365 | /* this loop was inspired by pp_caller */ |
342 | for (;;) |
366 | for (;;) |
343 | { |
367 | { |
344 | while (cxix >= 0) |
368 | while (cxix >= 0) |
… | |
… | |
350 | CV *cv = cx->blk_sub.cv; |
374 | CV *cv = cx->blk_sub.cv; |
351 | |
375 | |
352 | if (CvDEPTH (cv)) |
376 | if (CvDEPTH (cv)) |
353 | { |
377 | { |
354 | EXTEND (SP, 3); |
378 | EXTEND (SP, 3); |
355 | |
|
|
356 | PUSHs ((SV *)CvPADLIST (cv)); |
379 | PUSHs ((SV *)CvPADLIST (cv)); |
357 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
380 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
358 | PUSHs ((SV *)cv); |
381 | PUSHs ((SV *)cv); |
359 | |
382 | |
360 | CvDEPTH (cv) = 0; |
383 | CvDEPTH (cv) = 0; |
361 | get_padlist (cv); |
384 | get_padlist (cv); |
362 | } |
385 | } |
363 | } |
386 | } |
364 | #ifdef CXt_FORMAT |
|
|
365 | else if (CxTYPE (cx) == CXt_FORMAT) |
|
|
366 | { |
|
|
367 | /* I never used formats, so how should I know how these are implemented? */ |
|
|
368 | /* my bold guess is as a simple, plain sub... */ |
|
|
369 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
|
|
370 | } |
|
|
371 | #endif |
|
|
372 | } |
387 | } |
373 | |
388 | |
374 | if (top_si->si_type == PERLSI_MAIN) |
389 | if (top_si->si_type == PERLSI_MAIN) |
375 | break; |
390 | break; |
376 | |
391 | |
… | |
… | |
380 | } |
395 | } |
381 | |
396 | |
382 | PUTBACK; |
397 | PUTBACK; |
383 | } |
398 | } |
384 | |
399 | |
385 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
400 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
386 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
401 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
387 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
402 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
403 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
388 | |
404 | |
389 | #define VAR(name,type)c->name = PL_ ## name; |
405 | #define VAR(name,type)c->name = PL_ ## name; |
390 | # include "state.h" |
406 | # include "state.h" |
391 | #undef VAR |
407 | #undef VAR |
392 | } |
408 | } |
… | |
… | |
395 | * allocate various perl stacks. This is an exact copy |
411 | * allocate various perl stacks. This is an exact copy |
396 | * of perl.c:init_stacks, except that it uses less memory |
412 | * of perl.c:init_stacks, except that it uses less memory |
397 | * on the (sometimes correct) assumption that coroutines do |
413 | * on the (sometimes correct) assumption that coroutines do |
398 | * not usually need a lot of stackspace. |
414 | * not usually need a lot of stackspace. |
399 | */ |
415 | */ |
|
|
416 | #if PREFER_PERL_FUNCTIONS |
|
|
417 | # define coro_init_stacks init_stacks |
|
|
418 | #else |
400 | static void |
419 | static void |
401 | coro_init_stacks () |
420 | coro_init_stacks () |
402 | { |
421 | { |
403 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
422 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
404 | PL_curstackinfo->si_type = PERLSI_MAIN; |
423 | PL_curstackinfo->si_type = PERLSI_MAIN; |
… | |
… | |
434 | New(54,PL_retstack,16,OP*); |
453 | New(54,PL_retstack,16,OP*); |
435 | PL_retstack_ix = 0; |
454 | PL_retstack_ix = 0; |
436 | PL_retstack_max = 16; |
455 | PL_retstack_max = 16; |
437 | #endif |
456 | #endif |
438 | } |
457 | } |
|
|
458 | #endif |
439 | |
459 | |
440 | /* |
460 | /* |
441 | * destroy the stacks, the callchain etc... |
461 | * destroy the stacks, the callchain etc... |
442 | */ |
462 | */ |
443 | static void |
463 | static void |
444 | coro_destroy_stacks () |
464 | coro_destroy_stacks () |
445 | { |
465 | { |
446 | if (!IN_DESTRUCT) |
466 | if (!IN_DESTRUCT) |
447 | { |
467 | { |
448 | /* is this ugly, I ask? */ |
468 | /* restore all saved variables and stuff */ |
449 | LEAVE_SCOPE (0); |
469 | LEAVE_SCOPE (0); |
|
|
470 | assert (PL_tmps_floor == -1); |
450 | |
471 | |
451 | /* sure it is, but more important: is it correct?? :/ */ |
472 | /* free all temporaries */ |
452 | FREETMPS; |
473 | FREETMPS; |
|
|
474 | assert (PL_tmps_ix == -1); |
453 | |
475 | |
454 | /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ |
476 | POPSTACK_TO (PL_mainstack); |
455 | } |
477 | } |
456 | |
478 | |
457 | while (PL_curstackinfo->si_next) |
479 | while (PL_curstackinfo->si_next) |
458 | PL_curstackinfo = PL_curstackinfo->si_next; |
480 | PL_curstackinfo = PL_curstackinfo->si_next; |
459 | |
481 | |
460 | while (PL_curstackinfo) |
482 | while (PL_curstackinfo) |
461 | { |
483 | { |
462 | PERL_SI *p = PL_curstackinfo->si_prev; |
484 | PERL_SI *p = PL_curstackinfo->si_prev; |
463 | |
485 | |
464 | { /*D*//*remove*/ |
|
|
465 | dSP; |
|
|
466 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
|
|
467 | PUTBACK; /* possibly superfluous */ |
|
|
468 | } |
|
|
469 | |
|
|
470 | if (!IN_DESTRUCT) |
486 | if (!IN_DESTRUCT) |
471 | { |
|
|
472 | dounwind (-1);/*D*//*remove*/ |
|
|
473 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
487 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
474 | } |
|
|
475 | |
488 | |
476 | Safefree (PL_curstackinfo->si_cxstack); |
489 | Safefree (PL_curstackinfo->si_cxstack); |
477 | Safefree (PL_curstackinfo); |
490 | Safefree (PL_curstackinfo); |
478 | PL_curstackinfo = p; |
491 | PL_curstackinfo = p; |
479 | } |
492 | } |
… | |
… | |
583 | { |
596 | { |
584 | coro_cctx *cctx; |
597 | coro_cctx *cctx; |
585 | |
598 | |
586 | ++cctx_count; |
599 | ++cctx_count; |
587 | |
600 | |
588 | New (0, cctx, 1, coro_cctx); |
601 | Newz (0, cctx, 1, coro_cctx); |
589 | |
602 | |
590 | #if HAVE_MMAP |
603 | #if HAVE_MMAP |
591 | |
604 | |
592 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
605 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
593 | /* mmap supposedly does allocate-on-write for us */ |
606 | /* mmap supposedly does allocate-on-write for us */ |
… | |
… | |
688 | cctx_first = cctx; |
701 | cctx_first = cctx; |
689 | } |
702 | } |
690 | |
703 | |
691 | /* never call directly, always through the coro_state_transfer global variable */ |
704 | /* never call directly, always through the coro_state_transfer global variable */ |
692 | static void NOINLINE |
705 | static void NOINLINE |
693 | transfer (struct coro *prev, struct coro *next, int flags) |
706 | transfer (struct coro *prev, struct coro *next) |
694 | { |
707 | { |
695 | dSTACKLEVEL; |
708 | dSTACKLEVEL; |
696 | |
709 | |
697 | /* sometimes transfer is only called to set idle_sp */ |
710 | /* sometimes transfer is only called to set idle_sp */ |
698 | if (flags == TRANSFER_SET_STACKLEVEL) |
711 | if (!next) |
|
|
712 | { |
699 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
713 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
|
|
714 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
|
|
715 | } |
700 | else if (prev != next) |
716 | else if (prev != next) |
701 | { |
717 | { |
702 | coro_cctx *prev__cctx; |
718 | coro_cctx *prev__cctx; |
703 | |
719 | |
704 | if (prev->flags & CF_NEW) |
720 | if (prev->flags & CF_NEW) |
… | |
… | |
725 | if (next->flags & CF_NEW) |
741 | if (next->flags & CF_NEW) |
726 | { |
742 | { |
727 | /* need to start coroutine */ |
743 | /* need to start coroutine */ |
728 | next->flags &= ~CF_NEW; |
744 | next->flags &= ~CF_NEW; |
729 | /* first get rid of the old state */ |
745 | /* first get rid of the old state */ |
730 | SAVE (prev, -1); |
746 | save_perl (prev); |
731 | /* setup coroutine call */ |
747 | /* setup coroutine call */ |
732 | setup_coro (next); |
748 | setup_coro (next); |
733 | /* need a new stack */ |
749 | /* need a new stack */ |
734 | assert (!next->stack); |
750 | assert (!next->cctx); |
735 | } |
751 | } |
736 | else |
752 | else |
737 | { |
753 | { |
738 | /* coroutine already started */ |
754 | /* coroutine already started */ |
739 | SAVE (prev, flags); |
755 | save_perl (prev); |
740 | LOAD (next); |
756 | load_perl (next); |
741 | } |
757 | } |
742 | |
758 | |
743 | prev__cctx = prev->cctx; |
759 | prev__cctx = prev->cctx; |
744 | |
760 | |
745 | /* possibly "free" the cctx */ |
761 | /* possibly "free" the cctx */ |
746 | if (prev__cctx->idle_sp == STACKLEVEL) |
762 | if (prev__cctx->idle_sp == STACKLEVEL) |
747 | { |
763 | { |
748 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
764 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
749 | assert (PL_top_env == prev__cctx->top_env); |
765 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
750 | |
766 | |
751 | prev->cctx = 0; |
767 | prev->cctx = 0; |
752 | |
768 | |
753 | cctx_put (prev__cctx); |
769 | cctx_put (prev__cctx); |
754 | prev__cctx->inuse = 0; |
770 | prev__cctx->inuse = 0; |
… | |
… | |
775 | } |
791 | } |
776 | |
792 | |
777 | struct transfer_args |
793 | struct transfer_args |
778 | { |
794 | { |
779 | struct coro *prev, *next; |
795 | struct coro *prev, *next; |
780 | int flags; |
|
|
781 | }; |
796 | }; |
782 | |
797 | |
783 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) |
798 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
784 | |
799 | |
785 | static void |
800 | static void |
786 | coro_state_destroy (struct coro *coro) |
801 | coro_state_destroy (struct coro *coro) |
787 | { |
802 | { |
788 | if (coro->refcnt--) |
803 | if (coro->refcnt--) |
789 | return; |
804 | return; |
790 | |
805 | |
791 | if (coro->mainstack && coro->mainstack != main_mainstack) |
806 | if (coro->mainstack && coro->mainstack != main_mainstack) |
792 | { |
807 | { |
793 | struct coro temp; |
808 | struct coro temp; |
|
|
809 | Zero (&temp, 1, struct coro); |
|
|
810 | temp.save = CORO_SAVE_ALL; |
794 | |
811 | |
795 | if (coro->flags & CF_RUNNING) |
812 | if (coro->flags & CF_RUNNING) |
796 | croak ("FATAL: tried to destroy currently running coroutine"); |
813 | croak ("FATAL: tried to destroy currently running coroutine"); |
797 | |
814 | |
798 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
815 | save_perl (&temp); |
799 | LOAD (coro); |
816 | load_perl (coro); |
800 | |
817 | |
801 | coro_destroy_stacks (); |
818 | coro_destroy_stacks (); |
802 | |
819 | |
803 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
820 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
804 | |
821 | |
805 | coro->mainstack = 0; |
822 | coro->mainstack = 0; |
806 | } |
823 | } |
807 | |
824 | |
808 | cctx_destroy (coro->cctx); |
825 | cctx_destroy (coro->cctx); |
… | |
… | |
863 | assert (mg->mg_type == PERL_MAGIC_ext); |
880 | assert (mg->mg_type == PERL_MAGIC_ext); |
864 | return (struct coro *)mg->mg_ptr; |
881 | return (struct coro *)mg->mg_ptr; |
865 | } |
882 | } |
866 | |
883 | |
867 | static void |
884 | static void |
868 | prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) |
885 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
869 | { |
886 | { |
870 | ta->prev = SvSTATE (prev); |
887 | ta->prev = SvSTATE (prev_sv); |
871 | ta->next = SvSTATE (next); |
888 | ta->next = SvSTATE (next_sv); |
872 | ta->flags = flags; |
|
|
873 | } |
889 | } |
874 | |
890 | |
875 | static void |
891 | static void |
876 | api_transfer (SV *prev, SV *next, int flags) |
892 | api_transfer (SV *prev_sv, SV *next_sv) |
877 | { |
893 | { |
878 | struct transfer_args ta; |
894 | struct transfer_args ta; |
879 | |
895 | |
880 | prepare_transfer (&ta, prev, next, flags); |
896 | prepare_transfer (&ta, prev_sv, next_sv); |
881 | TRANSFER (ta); |
897 | TRANSFER (ta); |
|
|
898 | } |
|
|
899 | |
|
|
900 | static int |
|
|
901 | api_save (SV *coro_sv, int new_save) |
|
|
902 | { |
|
|
903 | struct coro *coro = SvSTATE (coro_sv); |
|
|
904 | int old_save = coro->save; |
|
|
905 | |
|
|
906 | if (new_save >= 0) |
|
|
907 | coro->save = new_save; |
|
|
908 | |
|
|
909 | return old_save; |
882 | } |
910 | } |
883 | |
911 | |
884 | /** Coro ********************************************************************/ |
912 | /** Coro ********************************************************************/ |
885 | |
913 | |
886 | #define PRIO_MAX 3 |
914 | #define PRIO_MAX 3 |
… | |
… | |
932 | coro = SvSTATE (coro_sv); |
960 | coro = SvSTATE (coro_sv); |
933 | |
961 | |
934 | if (coro->flags & CF_READY) |
962 | if (coro->flags & CF_READY) |
935 | return 0; |
963 | return 0; |
936 | |
964 | |
937 | #if 0 /* this is actually harmless */ |
|
|
938 | if (coro->flags & CF_RUNNING) |
|
|
939 | croak ("Coro::ready called on currently running coroutine"); |
|
|
940 | #endif |
|
|
941 | |
|
|
942 | coro->flags |= CF_READY; |
965 | coro->flags |= CF_READY; |
943 | |
966 | |
944 | LOCK; |
967 | LOCK; |
945 | coro_enq (SvREFCNT_inc (coro_sv)); |
968 | coro_enq (SvREFCNT_inc (coro_sv)); |
946 | UNLOCK; |
969 | UNLOCK; |
… | |
… | |
949 | } |
972 | } |
950 | |
973 | |
951 | static int |
974 | static int |
952 | api_is_ready (SV *coro_sv) |
975 | api_is_ready (SV *coro_sv) |
953 | { |
976 | { |
954 | return !!SvSTATE (coro_sv)->flags & CF_READY; |
977 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
955 | } |
978 | } |
956 | |
979 | |
957 | static void |
980 | static void |
958 | prepare_schedule (struct transfer_args *ta) |
981 | prepare_schedule (struct transfer_args *ta) |
959 | { |
982 | { |
… | |
… | |
995 | assert (!SvROK(prev));//D |
1018 | assert (!SvROK(prev));//D |
996 | assert (!SvROK(next));//D |
1019 | assert (!SvROK(next));//D |
997 | |
1020 | |
998 | ta->prev = SvSTATE (prev); |
1021 | ta->prev = SvSTATE (prev); |
999 | ta->next = SvSTATE (next); |
1022 | ta->next = SvSTATE (next); |
1000 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
1001 | |
1023 | |
1002 | assert (ta->flags & CF_READY); |
1024 | assert (ta->next->flags & CF_READY); |
1003 | ta->next->flags &= ~CF_READY; |
1025 | ta->next->flags &= ~CF_READY; |
1004 | } |
1026 | } |
1005 | |
1027 | |
1006 | static void |
1028 | static void |
1007 | prepare_cede (struct transfer_args *ta) |
1029 | prepare_cede (struct transfer_args *ta) |
1008 | { |
1030 | { |
1009 | api_ready (coro_current); |
1031 | api_ready (coro_current); |
1010 | |
|
|
1011 | prepare_schedule (ta); |
1032 | prepare_schedule (ta); |
|
|
1033 | } |
|
|
1034 | |
|
|
1035 | static int |
|
|
1036 | prepare_cede_notself (struct transfer_args *ta) |
|
|
1037 | { |
|
|
1038 | if (coro_nready) |
|
|
1039 | { |
|
|
1040 | SV *prev = SvRV (coro_current); |
|
|
1041 | prepare_schedule (ta); |
|
|
1042 | api_ready (prev); |
|
|
1043 | return 1; |
|
|
1044 | } |
|
|
1045 | else |
|
|
1046 | return 0; |
1012 | } |
1047 | } |
1013 | |
1048 | |
1014 | static void |
1049 | static void |
1015 | api_schedule (void) |
1050 | api_schedule (void) |
1016 | { |
1051 | { |
… | |
… | |
1018 | |
1053 | |
1019 | prepare_schedule (&ta); |
1054 | prepare_schedule (&ta); |
1020 | TRANSFER (ta); |
1055 | TRANSFER (ta); |
1021 | } |
1056 | } |
1022 | |
1057 | |
1023 | static void |
1058 | static int |
1024 | api_cede (void) |
1059 | api_cede (void) |
1025 | { |
1060 | { |
1026 | struct transfer_args ta; |
1061 | struct transfer_args ta; |
1027 | |
1062 | |
1028 | prepare_cede (&ta); |
1063 | prepare_cede (&ta); |
|
|
1064 | |
|
|
1065 | if (ta.prev != ta.next) |
|
|
1066 | { |
1029 | TRANSFER (ta); |
1067 | TRANSFER (ta); |
|
|
1068 | return 1; |
|
|
1069 | } |
|
|
1070 | else |
|
|
1071 | return 0; |
|
|
1072 | } |
|
|
1073 | |
|
|
1074 | static int |
|
|
1075 | api_cede_notself (void) |
|
|
1076 | { |
|
|
1077 | struct transfer_args ta; |
|
|
1078 | |
|
|
1079 | if (prepare_cede_notself (&ta)) |
|
|
1080 | { |
|
|
1081 | TRANSFER (ta); |
|
|
1082 | return 1; |
|
|
1083 | } |
|
|
1084 | else |
|
|
1085 | return 0; |
1030 | } |
1086 | } |
1031 | |
1087 | |
1032 | MODULE = Coro::State PACKAGE = Coro::State |
1088 | MODULE = Coro::State PACKAGE = Coro::State |
1033 | |
1089 | |
1034 | PROTOTYPES: DISABLE |
1090 | PROTOTYPES: DISABLE |
… | |
… | |
1040 | #endif |
1096 | #endif |
1041 | BOOT_PAGESIZE; |
1097 | BOOT_PAGESIZE; |
1042 | |
1098 | |
1043 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1099 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1044 | |
1100 | |
1045 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
1101 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1046 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
1102 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1047 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
1103 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
|
|
1104 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1105 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1048 | |
1106 | |
1049 | main_mainstack = PL_mainstack; |
1107 | main_mainstack = PL_mainstack; |
1050 | |
1108 | |
1051 | coroapi.ver = CORO_API_VERSION; |
1109 | coroapi.ver = CORO_API_VERSION; |
1052 | coroapi.transfer = api_transfer; |
1110 | coroapi.transfer = api_transfer; |
… | |
… | |
1062 | HV *hv; |
1120 | HV *hv; |
1063 | int i; |
1121 | int i; |
1064 | |
1122 | |
1065 | Newz (0, coro, 1, struct coro); |
1123 | Newz (0, coro, 1, struct coro); |
1066 | coro->args = newAV (); |
1124 | coro->args = newAV (); |
|
|
1125 | coro->save = CORO_SAVE_ALL; |
1067 | coro->flags = CF_NEW; |
1126 | coro->flags = CF_NEW; |
1068 | |
1127 | |
1069 | hv = newHV (); |
1128 | hv = newHV (); |
1070 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1129 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1071 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1130 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1072 | |
1131 | |
1073 | for (i = 1; i < items; i++) |
1132 | for (i = 1; i < items; i++) |
1074 | av_push (coro->args, newSVsv (ST (i))); |
1133 | av_push (coro->args, newSVsv (ST (i))); |
1075 | } |
1134 | } |
1076 | OUTPUT: |
1135 | OUTPUT: |
|
|
1136 | RETVAL |
|
|
1137 | |
|
|
1138 | int |
|
|
1139 | save (SV *coro, int new_save = -1) |
|
|
1140 | CODE: |
|
|
1141 | RETVAL = api_save (coro, new_save); |
|
|
1142 | OUTPUT: |
1077 | RETVAL |
1143 | RETVAL |
1078 | |
1144 | |
1079 | void |
1145 | void |
1080 | _set_stacklevel (...) |
1146 | _set_stacklevel (...) |
1081 | ALIAS: |
1147 | ALIAS: |
1082 | Coro::State::transfer = 1 |
1148 | Coro::State::transfer = 1 |
1083 | Coro::schedule = 2 |
1149 | Coro::schedule = 2 |
1084 | Coro::cede = 3 |
1150 | Coro::cede = 3 |
|
|
1151 | Coro::cede_notself = 4 |
1085 | CODE: |
1152 | CODE: |
1086 | { |
1153 | { |
1087 | struct transfer_args ta; |
1154 | struct transfer_args ta; |
1088 | |
1155 | |
1089 | switch (ix) |
1156 | switch (ix) |
1090 | { |
1157 | { |
1091 | case 0: |
1158 | case 0: |
1092 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1159 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1093 | ta.next = 0; |
1160 | ta.next = 0; |
1094 | ta.flags = TRANSFER_SET_STACKLEVEL; |
|
|
1095 | break; |
1161 | break; |
1096 | |
1162 | |
1097 | case 1: |
1163 | case 1: |
1098 | if (items != 3) |
1164 | if (items != 2) |
1099 | croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items); |
1165 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1100 | |
1166 | |
1101 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1167 | prepare_transfer (&ta, ST (0), ST (1)); |
1102 | break; |
1168 | break; |
1103 | |
1169 | |
1104 | case 2: |
1170 | case 2: |
1105 | prepare_schedule (&ta); |
1171 | prepare_schedule (&ta); |
1106 | break; |
1172 | break; |
1107 | |
1173 | |
1108 | case 3: |
1174 | case 3: |
1109 | prepare_cede (&ta); |
1175 | prepare_cede (&ta); |
1110 | break; |
1176 | break; |
|
|
1177 | |
|
|
1178 | case 4: |
|
|
1179 | if (!prepare_cede_notself (&ta)) |
|
|
1180 | XSRETURN_EMPTY; |
|
|
1181 | |
|
|
1182 | break; |
1111 | } |
1183 | } |
1112 | |
1184 | |
|
|
1185 | BARRIER; |
1113 | TRANSFER (ta); |
1186 | TRANSFER (ta); |
1114 | } |
1187 | } |
1115 | |
1188 | |
1116 | void |
1189 | void |
1117 | _clone_state_from (SV *dst, SV *src) |
1190 | _clone_state_from (SV *dst, SV *src) |
… | |
… | |
1168 | coro_ready[i] = newAV (); |
1241 | coro_ready[i] = newAV (); |
1169 | |
1242 | |
1170 | { |
1243 | { |
1171 | SV *sv = perl_get_sv("Coro::API", 1); |
1244 | SV *sv = perl_get_sv("Coro::API", 1); |
1172 | |
1245 | |
1173 | coroapi.schedule = api_schedule; |
1246 | coroapi.schedule = api_schedule; |
|
|
1247 | coroapi.save = api_save; |
1174 | coroapi.cede = api_cede; |
1248 | coroapi.cede = api_cede; |
|
|
1249 | coroapi.cede_notself = api_cede_notself; |
1175 | coroapi.ready = api_ready; |
1250 | coroapi.ready = api_ready; |
1176 | coroapi.is_ready = api_is_ready; |
1251 | coroapi.is_ready = api_is_ready; |
1177 | coroapi.nready = &coro_nready; |
1252 | coroapi.nready = &coro_nready; |
1178 | coroapi.current = coro_current; |
1253 | coroapi.current = coro_current; |
1179 | |
1254 | |
1180 | GCoroAPI = &coroapi; |
1255 | GCoroAPI = &coroapi; |
1181 | sv_setiv (sv, (IV)&coroapi); |
1256 | sv_setiv (sv, (IV)&coroapi); |
1182 | SvREADONLY_on (sv); |
1257 | SvREADONLY_on (sv); |
1183 | } |
1258 | } |
1184 | } |
1259 | } |
|
|
1260 | |
|
|
1261 | void |
|
|
1262 | _set_current (SV *current) |
|
|
1263 | PROTOTYPE: $ |
|
|
1264 | CODE: |
|
|
1265 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1266 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
1185 | |
1267 | |
1186 | int |
1268 | int |
1187 | prio (Coro::State coro, int newprio = 0) |
1269 | prio (Coro::State coro, int newprio = 0) |
1188 | ALIAS: |
1270 | ALIAS: |
1189 | nice = 1 |
1271 | nice = 1 |
… | |
… | |
1192 | RETVAL = coro->prio; |
1274 | RETVAL = coro->prio; |
1193 | |
1275 | |
1194 | if (items > 1) |
1276 | if (items > 1) |
1195 | { |
1277 | { |
1196 | if (ix) |
1278 | if (ix) |
1197 | newprio += coro->prio; |
1279 | newprio = coro->prio - newprio; |
1198 | |
1280 | |
1199 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1281 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1200 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1282 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1201 | |
1283 | |
1202 | coro->prio = newprio; |
1284 | coro->prio = newprio; |
1203 | } |
1285 | } |
1204 | } |
1286 | } |
|
|
1287 | OUTPUT: |
|
|
1288 | RETVAL |
1205 | |
1289 | |
1206 | SV * |
1290 | SV * |
1207 | ready (SV *self) |
1291 | ready (SV *self) |
1208 | PROTOTYPE: $ |
1292 | PROTOTYPE: $ |
1209 | CODE: |
1293 | CODE: |
… | |
… | |
1225 | CODE: |
1309 | CODE: |
1226 | RETVAL = coro_nready; |
1310 | RETVAL = coro_nready; |
1227 | OUTPUT: |
1311 | OUTPUT: |
1228 | RETVAL |
1312 | RETVAL |
1229 | |
1313 | |
1230 | void |
|
|
1231 | _set_current (SV *current) |
|
|
1232 | PROTOTYPE: $ |
|
|
1233 | CODE: |
|
|
1234 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1235 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
|
|
1236 | |
|
|
1237 | MODULE = Coro::State PACKAGE = Coro::AIO |
1314 | MODULE = Coro::State PACKAGE = Coro::AIO |
1238 | |
1315 | |
1239 | SV * |
1316 | SV * |
1240 | _get_state () |
1317 | _get_state () |
1241 | CODE: |
1318 | CODE: |
1242 | { |
1319 | { |
|
|
1320 | struct io_state *data; |
|
|
1321 | |
1243 | RETVAL = newSV (sizeof (struct io_state)); |
1322 | RETVAL = newSV (sizeof (struct io_state)); |
1244 | struct io_state *data = (struct io_state *)SvPVX (RETVAL); |
1323 | data = (struct io_state *)SvPVX (RETVAL); |
1245 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
1324 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
1246 | SvPOK_only (RETVAL); |
1325 | SvPOK_only (RETVAL); |
1247 | |
1326 | |
1248 | data->errorno = errno; |
1327 | data->errorno = errno; |
1249 | data->laststype = PL_laststype; |
1328 | data->laststype = PL_laststype; |