… | |
… | |
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) |
101 | #else |
112 | #else |
102 | # define LOCK (void)0 |
113 | # define LOCK (void)0 |
103 | # define UNLOCK (void)0 |
114 | # define UNLOCK (void)0 |
104 | #endif |
115 | #endif |
105 | |
116 | |
|
|
117 | /* helper storage struct for Coro::AIO */ |
|
|
118 | struct io_state |
|
|
119 | { |
|
|
120 | int errorno; |
|
|
121 | I32 laststype; |
|
|
122 | int laststatval; |
|
|
123 | Stat_t statcache; |
|
|
124 | }; |
|
|
125 | |
106 | static struct CoroAPI coroapi; |
126 | static struct CoroAPI coroapi; |
107 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
127 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
108 | static HV *coro_state_stash, *coro_stash; |
128 | static HV *coro_state_stash, *coro_stash; |
109 | static SV *coro_mortal; /* will be freed after next transfer */ |
129 | static SV *coro_mortal; /* will be freed after next transfer */ |
110 | |
130 | |
… | |
… | |
118 | /* the stack */ |
138 | /* the stack */ |
119 | void *sptr; |
139 | void *sptr; |
120 | long ssize; /* positive == mmap, otherwise malloc */ |
140 | long ssize; /* positive == mmap, otherwise malloc */ |
121 | |
141 | |
122 | /* cpu state */ |
142 | /* cpu state */ |
123 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
143 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
144 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
124 | JMPENV *top_env; |
145 | JMPENV *top_env; |
125 | coro_context cctx; |
146 | coro_context cctx; |
126 | |
147 | |
127 | int inuse; |
148 | int inuse; |
128 | |
149 | |
… | |
… | |
130 | int valgrind_id; |
151 | int valgrind_id; |
131 | #endif |
152 | #endif |
132 | } coro_cctx; |
153 | } coro_cctx; |
133 | |
154 | |
134 | enum { |
155 | enum { |
135 | CF_RUNNING = 0x0001, /* coroutine is running */ |
156 | CF_RUNNING = 0x0001, /* coroutine is running */ |
136 | CF_READY = 0x0002, /* coroutine is ready */ |
157 | CF_READY = 0x0002, /* coroutine is ready */ |
137 | CF_NEW = 0x0004, /* ahs never been switched to */ |
158 | CF_NEW = 0x0004, /* has never been switched to */ |
|
|
159 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
138 | }; |
160 | }; |
139 | |
161 | |
140 | /* this is a structure representing a perl-level coroutine */ |
162 | /* this is a structure representing a perl-level coroutine */ |
141 | struct coro { |
163 | struct coro { |
142 | /* the c coroutine allocated to this perl coroutine, if any */ |
164 | /* the c coroutine allocated to this perl coroutine, if any */ |
143 | coro_cctx *cctx; |
165 | coro_cctx *cctx; |
144 | |
166 | |
145 | /* data associated with this coroutine (initial args) */ |
167 | /* data associated with this coroutine (initial args) */ |
146 | AV *args; |
168 | AV *args; |
147 | int refcnt; |
169 | int refcnt; |
148 | int flags; |
170 | int save; /* CORO_SAVE flags */ |
|
|
171 | int flags; /* CF_ flags */ |
149 | |
172 | |
150 | /* optionally saved, might be zero */ |
173 | /* optionally saved, might be zero */ |
151 | AV *defav; |
174 | AV *defav; /* @_ */ |
152 | SV *defsv; |
175 | SV *defsv; /* $_ */ |
153 | SV *errsv; |
176 | SV *errsv; /* $@ */ |
|
|
177 | SV *irssv; /* $/ */ |
|
|
178 | SV *irssv_sv; /* real $/ cache */ |
154 | |
179 | |
155 | #define VAR(name,type) type name; |
180 | #define VAR(name,type) type name; |
156 | # include "state.h" |
181 | # include "state.h" |
157 | #undef VAR |
182 | #undef VAR |
158 | |
183 | |
… | |
… | |
244 | |
269 | |
245 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
270 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
246 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
271 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
247 | else |
272 | else |
248 | { |
273 | { |
249 | #if 0 |
274 | #if PREFER_PERL_FUNCTIONS |
250 | /* this is probably cleaner, but also slower? */ |
275 | /* this is probably cleaner, but also slower? */ |
251 | CV *cp = Perl_cv_clone (cv); |
276 | CV *cp = Perl_cv_clone (cv); |
252 | CvPADLIST (cv) = CvPADLIST (cp); |
277 | CvPADLIST (cv) = CvPADLIST (cp); |
253 | CvPADLIST (cp) = 0; |
278 | CvPADLIST (cp) = 0; |
254 | SvREFCNT_dec (cp); |
279 | SvREFCNT_dec (cp); |
… | |
… | |
281 | } |
306 | } |
282 | |
307 | |
283 | #define SB do { |
308 | #define SB do { |
284 | #define SE } while (0) |
309 | #define SE } while (0) |
285 | |
310 | |
286 | #define LOAD(state) load_state ((state)) |
|
|
287 | #define SAVE(state,flags) save_state ((state),(flags)) |
|
|
288 | |
|
|
289 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
311 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
290 | |
312 | |
291 | static void |
313 | static void |
292 | load_state (Coro__State c) |
314 | load_perl (Coro__State c) |
293 | { |
315 | { |
294 | #define VAR(name,type) PL_ ## name = c->name; |
316 | #define VAR(name,type) PL_ ## name = c->name; |
295 | # include "state.h" |
317 | # include "state.h" |
296 | #undef VAR |
318 | #undef VAR |
297 | |
319 | |
298 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
320 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
299 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
321 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
300 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
322 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
|
|
323 | if (c->irssv) |
|
|
324 | { |
|
|
325 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
|
|
326 | SvREFCNT_dec (c->irssv); |
|
|
327 | else |
|
|
328 | { |
|
|
329 | REPLACE_SV (PL_rs, c->irssv); |
|
|
330 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
|
|
331 | sv_setsv (c->irssv_sv, PL_rs); |
|
|
332 | } |
|
|
333 | } |
301 | |
334 | |
302 | { |
335 | { |
303 | dSP; |
336 | dSP; |
304 | CV *cv; |
337 | CV *cv; |
305 | |
338 | |
… | |
… | |
314 | PUTBACK; |
347 | PUTBACK; |
315 | } |
348 | } |
316 | } |
349 | } |
317 | |
350 | |
318 | static void |
351 | static void |
319 | save_state (Coro__State c, int flags) |
352 | save_perl (Coro__State c) |
320 | { |
353 | { |
321 | { |
354 | { |
322 | dSP; |
355 | dSP; |
323 | I32 cxix = cxstack_ix; |
356 | I32 cxix = cxstack_ix; |
324 | PERL_CONTEXT *ccstk = cxstack; |
357 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
327 | /* |
360 | /* |
328 | * the worst thing you can imagine happens first - we have to save |
361 | * the worst thing you can imagine happens first - we have to save |
329 | * (and reinitialize) all cv's in the whole callchain :( |
362 | * (and reinitialize) all cv's in the whole callchain :( |
330 | */ |
363 | */ |
331 | |
364 | |
|
|
365 | EXTEND (SP, 3 + 1); |
332 | PUSHs (Nullsv); |
366 | PUSHs (Nullsv); |
333 | /* this loop was inspired by pp_caller */ |
367 | /* this loop was inspired by pp_caller */ |
334 | for (;;) |
368 | for (;;) |
335 | { |
369 | { |
336 | while (cxix >= 0) |
370 | while (cxix >= 0) |
… | |
… | |
342 | CV *cv = cx->blk_sub.cv; |
376 | CV *cv = cx->blk_sub.cv; |
343 | |
377 | |
344 | if (CvDEPTH (cv)) |
378 | if (CvDEPTH (cv)) |
345 | { |
379 | { |
346 | EXTEND (SP, 3); |
380 | EXTEND (SP, 3); |
347 | |
|
|
348 | PUSHs ((SV *)CvPADLIST (cv)); |
381 | PUSHs ((SV *)CvPADLIST (cv)); |
349 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
382 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
350 | PUSHs ((SV *)cv); |
383 | PUSHs ((SV *)cv); |
351 | |
384 | |
352 | CvDEPTH (cv) = 0; |
385 | CvDEPTH (cv) = 0; |
353 | get_padlist (cv); |
386 | get_padlist (cv); |
354 | } |
387 | } |
355 | } |
388 | } |
356 | #ifdef CXt_FORMAT |
|
|
357 | else if (CxTYPE (cx) == CXt_FORMAT) |
|
|
358 | { |
|
|
359 | /* I never used formats, so how should I know how these are implemented? */ |
|
|
360 | /* my bold guess is as a simple, plain sub... */ |
|
|
361 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
|
|
362 | } |
|
|
363 | #endif |
|
|
364 | } |
389 | } |
365 | |
390 | |
366 | if (top_si->si_type == PERLSI_MAIN) |
391 | if (top_si->si_type == PERLSI_MAIN) |
367 | break; |
392 | break; |
368 | |
393 | |
… | |
… | |
372 | } |
397 | } |
373 | |
398 | |
374 | PUTBACK; |
399 | PUTBACK; |
375 | } |
400 | } |
376 | |
401 | |
377 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
402 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
378 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
403 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
379 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
404 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
405 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
380 | |
406 | |
381 | #define VAR(name,type)c->name = PL_ ## name; |
407 | #define VAR(name,type)c->name = PL_ ## name; |
382 | # include "state.h" |
408 | # include "state.h" |
383 | #undef VAR |
409 | #undef VAR |
384 | } |
410 | } |
… | |
… | |
387 | * allocate various perl stacks. This is an exact copy |
413 | * allocate various perl stacks. This is an exact copy |
388 | * of perl.c:init_stacks, except that it uses less memory |
414 | * of perl.c:init_stacks, except that it uses less memory |
389 | * on the (sometimes correct) assumption that coroutines do |
415 | * on the (sometimes correct) assumption that coroutines do |
390 | * not usually need a lot of stackspace. |
416 | * not usually need a lot of stackspace. |
391 | */ |
417 | */ |
|
|
418 | #if PREFER_PERL_FUNCTIONS |
|
|
419 | # define coro_init_stacks init_stacks |
|
|
420 | #else |
392 | static void |
421 | static void |
393 | coro_init_stacks () |
422 | coro_init_stacks () |
394 | { |
423 | { |
395 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
424 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
396 | PL_curstackinfo->si_type = PERLSI_MAIN; |
425 | PL_curstackinfo->si_type = PERLSI_MAIN; |
… | |
… | |
426 | New(54,PL_retstack,16,OP*); |
455 | New(54,PL_retstack,16,OP*); |
427 | PL_retstack_ix = 0; |
456 | PL_retstack_ix = 0; |
428 | PL_retstack_max = 16; |
457 | PL_retstack_max = 16; |
429 | #endif |
458 | #endif |
430 | } |
459 | } |
|
|
460 | #endif |
431 | |
461 | |
432 | /* |
462 | /* |
433 | * destroy the stacks, the callchain etc... |
463 | * destroy the stacks, the callchain etc... |
434 | */ |
464 | */ |
435 | static void |
465 | static void |
436 | coro_destroy_stacks () |
466 | coro_destroy_stacks () |
437 | { |
467 | { |
438 | if (!IN_DESTRUCT) |
468 | if (!IN_DESTRUCT) |
439 | { |
469 | { |
440 | /* is this ugly, I ask? */ |
470 | /* restore all saved variables and stuff */ |
441 | LEAVE_SCOPE (0); |
471 | LEAVE_SCOPE (0); |
|
|
472 | assert (PL_tmps_floor == -1); |
442 | |
473 | |
443 | /* sure it is, but more important: is it correct?? :/ */ |
474 | /* free all temporaries */ |
444 | FREETMPS; |
475 | FREETMPS; |
|
|
476 | assert (PL_tmps_ix == -1); |
445 | |
477 | |
|
|
478 | /* unwind all extra stacks */ |
446 | /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ |
479 | POPSTACK_TO (PL_mainstack); |
|
|
480 | |
|
|
481 | /* unwind main stack */ |
|
|
482 | dounwind (-1); |
447 | } |
483 | } |
448 | |
484 | |
449 | while (PL_curstackinfo->si_next) |
485 | while (PL_curstackinfo->si_next) |
450 | PL_curstackinfo = PL_curstackinfo->si_next; |
486 | PL_curstackinfo = PL_curstackinfo->si_next; |
451 | |
487 | |
452 | while (PL_curstackinfo) |
488 | while (PL_curstackinfo) |
453 | { |
489 | { |
454 | PERL_SI *p = PL_curstackinfo->si_prev; |
490 | PERL_SI *p = PL_curstackinfo->si_prev; |
455 | |
491 | |
456 | { /*D*//*remove*/ |
|
|
457 | dSP; |
|
|
458 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
|
|
459 | PUTBACK; /* possibly superfluous */ |
|
|
460 | } |
|
|
461 | |
|
|
462 | if (!IN_DESTRUCT) |
492 | if (!IN_DESTRUCT) |
463 | { |
|
|
464 | dounwind (-1);/*D*//*remove*/ |
|
|
465 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
493 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
466 | } |
|
|
467 | |
494 | |
468 | Safefree (PL_curstackinfo->si_cxstack); |
495 | Safefree (PL_curstackinfo->si_cxstack); |
469 | Safefree (PL_curstackinfo); |
496 | Safefree (PL_curstackinfo); |
470 | PL_curstackinfo = p; |
497 | PL_curstackinfo = p; |
471 | } |
498 | } |
… | |
… | |
497 | |
524 | |
498 | { |
525 | { |
499 | dSP; |
526 | dSP; |
500 | LOGOP myop; |
527 | LOGOP myop; |
501 | |
528 | |
502 | /* I have no idea why this is needed, but it is */ |
|
|
503 | PUSHMARK (SP); |
|
|
504 | |
|
|
505 | SvREFCNT_dec (GvAV (PL_defgv)); |
529 | SvREFCNT_dec (GvAV (PL_defgv)); |
506 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
530 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
507 | |
531 | |
508 | Zero (&myop, 1, LOGOP); |
532 | Zero (&myop, 1, LOGOP); |
509 | myop.op_next = Nullop; |
533 | myop.op_next = Nullop; |
510 | myop.op_flags = OPf_WANT_VOID; |
534 | myop.op_flags = OPf_WANT_VOID; |
511 | |
535 | |
512 | PL_op = (OP *)&myop; |
|
|
513 | |
|
|
514 | PUSHMARK (SP); |
536 | PUSHMARK (SP); |
515 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
537 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
516 | PUTBACK; |
538 | PUTBACK; |
|
|
539 | PL_op = (OP *)&myop; |
517 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
540 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
518 | SPAGAIN; |
541 | SPAGAIN; |
519 | } |
542 | } |
520 | |
543 | |
521 | ENTER; /* necessary e.g. for dounwind */ |
544 | ENTER; /* necessary e.g. for dounwind */ |
… | |
… | |
529 | SvREFCNT_dec (coro_mortal); |
552 | SvREFCNT_dec (coro_mortal); |
530 | coro_mortal = 0; |
553 | coro_mortal = 0; |
531 | } |
554 | } |
532 | } |
555 | } |
533 | |
556 | |
|
|
557 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
534 | static void NOINLINE |
558 | static void NOINLINE |
535 | prepare_cctx (coro_cctx *cctx) |
559 | prepare_cctx (coro_cctx *cctx) |
536 | { |
560 | { |
537 | dSP; |
561 | dSP; |
538 | LOGOP myop; |
562 | LOGOP myop; |
539 | |
563 | |
540 | Zero (&myop, 1, LOGOP); |
564 | Zero (&myop, 1, LOGOP); |
541 | myop.op_next = PL_op; |
565 | myop.op_next = PL_op; |
542 | myop.op_flags = OPf_WANT_VOID; |
566 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
543 | |
|
|
544 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
|
|
545 | |
567 | |
546 | PUSHMARK (SP); |
568 | PUSHMARK (SP); |
|
|
569 | EXTEND (SP, 2); |
|
|
570 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
547 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
571 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
548 | PUTBACK; |
572 | PUTBACK; |
|
|
573 | PL_op = (OP *)&myop; |
549 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
574 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
550 | SPAGAIN; |
575 | SPAGAIN; |
551 | } |
576 | } |
552 | |
577 | |
553 | static void |
578 | static void |
554 | coro_run (void *arg) |
579 | coro_run (void *arg) |
555 | { |
580 | { |
556 | /* coro_run is the alternative epilogue of transfer() */ |
581 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
557 | UNLOCK; |
582 | UNLOCK; |
558 | |
583 | |
559 | /* |
584 | /* |
560 | * this is a _very_ stripped down perl interpreter ;) |
585 | * this is a _very_ stripped down perl interpreter ;) |
561 | */ |
586 | */ |
… | |
… | |
563 | |
588 | |
564 | /* inject call to cctx_init */ |
589 | /* inject call to cctx_init */ |
565 | prepare_cctx ((coro_cctx *)arg); |
590 | prepare_cctx ((coro_cctx *)arg); |
566 | |
591 | |
567 | /* somebody will hit me for both perl_run and PL_restartop */ |
592 | /* somebody will hit me for both perl_run and PL_restartop */ |
|
|
593 | PL_restartop = PL_op; |
568 | perl_run (PL_curinterp); |
594 | perl_run (PL_curinterp); |
569 | |
595 | |
570 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
596 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
571 | abort (); |
597 | abort (); |
572 | } |
598 | } |
… | |
… | |
576 | { |
602 | { |
577 | coro_cctx *cctx; |
603 | coro_cctx *cctx; |
578 | |
604 | |
579 | ++cctx_count; |
605 | ++cctx_count; |
580 | |
606 | |
581 | New (0, cctx, 1, coro_cctx); |
607 | Newz (0, cctx, 1, coro_cctx); |
582 | |
608 | |
583 | #if HAVE_MMAP |
609 | #if HAVE_MMAP |
584 | |
610 | |
585 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
611 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
586 | /* mmap suppsedly does allocate-on-write for us */ |
612 | /* mmap supposedly does allocate-on-write for us */ |
587 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
613 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
588 | |
614 | |
589 | if (cctx->sptr == (void *)-1) |
615 | if (cctx->sptr == (void *)-1) |
590 | { |
616 | { |
591 | perror ("FATAL: unable to mmap stack for coroutine"); |
617 | perror ("FATAL: unable to mmap stack for coroutine"); |
… | |
… | |
681 | cctx_first = cctx; |
707 | cctx_first = cctx; |
682 | } |
708 | } |
683 | |
709 | |
684 | /* never call directly, always through the coro_state_transfer global variable */ |
710 | /* never call directly, always through the coro_state_transfer global variable */ |
685 | static void NOINLINE |
711 | static void NOINLINE |
686 | transfer (struct coro *prev, struct coro *next, int flags) |
712 | transfer (struct coro *prev, struct coro *next) |
687 | { |
713 | { |
688 | dSTACKLEVEL; |
714 | dSTACKLEVEL; |
689 | |
715 | |
690 | /* sometimes transfer is only called to set idle_sp */ |
716 | /* sometimes transfer is only called to set idle_sp */ |
691 | if (flags == TRANSFER_SET_STACKLEVEL) |
717 | if (!next) |
|
|
718 | { |
692 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
719 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
|
|
720 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
|
|
721 | } |
693 | else if (prev != next) |
722 | else if (prev != next) |
694 | { |
723 | { |
695 | coro_cctx *prev__cctx; |
724 | coro_cctx *prev__cctx; |
696 | |
725 | |
697 | if (prev->flags & CF_NEW) |
726 | if (prev->flags & CF_NEW) |
… | |
… | |
708 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
737 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
709 | |
738 | |
710 | if (next->flags & CF_RUNNING) |
739 | if (next->flags & CF_RUNNING) |
711 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
740 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
712 | |
741 | |
|
|
742 | if (next->flags & CF_DESTROYED) |
|
|
743 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
744 | |
713 | prev->flags &= ~CF_RUNNING; |
745 | prev->flags &= ~CF_RUNNING; |
714 | next->flags |= CF_RUNNING; |
746 | next->flags |= CF_RUNNING; |
715 | |
747 | |
716 | LOCK; |
748 | LOCK; |
717 | |
749 | |
718 | if (next->flags & CF_NEW) |
750 | if (next->flags & CF_NEW) |
719 | { |
751 | { |
720 | /* need to start coroutine */ |
752 | /* need to start coroutine */ |
721 | next->flags &= ~CF_NEW; |
753 | next->flags &= ~CF_NEW; |
722 | /* first get rid of the old state */ |
754 | /* first get rid of the old state */ |
723 | SAVE (prev, -1); |
755 | save_perl (prev); |
724 | /* setup coroutine call */ |
756 | /* setup coroutine call */ |
725 | setup_coro (next); |
757 | setup_coro (next); |
726 | /* need a new stack */ |
758 | /* need a new stack */ |
727 | assert (!next->stack); |
759 | assert (!next->cctx); |
728 | } |
760 | } |
729 | else |
761 | else |
730 | { |
762 | { |
731 | /* coroutine already started */ |
763 | /* coroutine already started */ |
732 | SAVE (prev, flags); |
764 | save_perl (prev); |
733 | LOAD (next); |
765 | load_perl (next); |
734 | } |
766 | } |
735 | |
767 | |
736 | prev__cctx = prev->cctx; |
768 | prev__cctx = prev->cctx; |
737 | |
769 | |
738 | /* possibly "free" the cctx */ |
770 | /* possibly "free" the cctx */ |
739 | if (prev__cctx->idle_sp == STACKLEVEL) |
771 | if (prev__cctx->idle_sp == STACKLEVEL) |
740 | { |
772 | { |
741 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
773 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
742 | assert (PL_top_env == prev__cctx->top_env); |
774 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
743 | |
775 | |
744 | prev->cctx = 0; |
776 | prev->cctx = 0; |
745 | |
777 | |
746 | cctx_put (prev__cctx); |
778 | cctx_put (prev__cctx); |
747 | prev__cctx->inuse = 0; |
779 | prev__cctx->inuse = 0; |
… | |
… | |
760 | PL_top_env = next->cctx->top_env; |
792 | PL_top_env = next->cctx->top_env; |
761 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
793 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
762 | } |
794 | } |
763 | |
795 | |
764 | free_coro_mortal (); |
796 | free_coro_mortal (); |
765 | |
|
|
766 | UNLOCK; |
797 | UNLOCK; |
767 | } |
798 | } |
768 | } |
799 | } |
769 | |
800 | |
770 | struct transfer_args |
801 | struct transfer_args |
771 | { |
802 | { |
772 | struct coro *prev, *next; |
803 | struct coro *prev, *next; |
773 | int flags; |
|
|
774 | }; |
804 | }; |
775 | |
805 | |
776 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) |
806 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
777 | |
807 | |
778 | static void |
808 | static int |
779 | coro_state_destroy (struct coro *coro) |
809 | coro_state_destroy (struct coro *coro) |
780 | { |
810 | { |
781 | if (coro->refcnt--) |
811 | if (coro->flags & CF_DESTROYED) |
782 | return; |
812 | return 0; |
|
|
813 | |
|
|
814 | coro->flags |= CF_DESTROYED; |
783 | |
815 | |
784 | if (coro->mainstack && coro->mainstack != main_mainstack) |
816 | if (coro->mainstack && coro->mainstack != main_mainstack) |
785 | { |
817 | { |
|
|
818 | assert (!(coro->flags & CF_RUNNING)); |
|
|
819 | |
786 | struct coro temp; |
820 | struct coro temp; |
|
|
821 | Zero (&temp, 1, struct coro); |
|
|
822 | temp.save = CORO_SAVE_ALL; |
787 | |
823 | |
788 | if (coro->flags & CF_RUNNING) |
824 | if (coro->flags & CF_RUNNING) |
789 | croak ("FATAL: tried to destroy currently running coroutine"); |
825 | croak ("FATAL: tried to destroy currently running coroutine"); |
790 | |
826 | |
791 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
827 | save_perl (&temp); |
792 | LOAD (coro); |
828 | load_perl (coro); |
793 | |
829 | |
794 | coro_destroy_stacks (); |
830 | coro_destroy_stacks (); |
795 | |
831 | |
796 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
832 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
797 | |
833 | |
798 | coro->mainstack = 0; |
834 | coro->mainstack = 0; |
799 | } |
835 | } |
800 | |
836 | |
801 | cctx_destroy (coro->cctx); |
837 | cctx_destroy (coro->cctx); |
802 | SvREFCNT_dec (coro->args); |
838 | SvREFCNT_dec (coro->args); |
803 | Safefree (coro); |
839 | |
|
|
840 | return 1; |
804 | } |
841 | } |
805 | |
842 | |
806 | static int |
843 | static int |
807 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
844 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
808 | { |
845 | { |
809 | struct coro *coro = (struct coro *)mg->mg_ptr; |
846 | struct coro *coro = (struct coro *)mg->mg_ptr; |
810 | mg->mg_ptr = 0; |
847 | mg->mg_ptr = 0; |
811 | |
848 | |
|
|
849 | if (--coro->refcnt < 0) |
|
|
850 | { |
812 | coro_state_destroy (coro); |
851 | coro_state_destroy (coro); |
|
|
852 | Safefree (coro); |
|
|
853 | } |
813 | |
854 | |
814 | return 0; |
855 | return 0; |
815 | } |
856 | } |
816 | |
857 | |
817 | static int |
858 | static int |
… | |
… | |
824 | return 0; |
865 | return 0; |
825 | } |
866 | } |
826 | |
867 | |
827 | static MGVTBL coro_state_vtbl = { |
868 | static MGVTBL coro_state_vtbl = { |
828 | 0, 0, 0, 0, |
869 | 0, 0, 0, 0, |
829 | coro_state_clear, |
870 | coro_state_free, |
830 | 0, |
871 | 0, |
831 | #ifdef MGf_DUP |
872 | #ifdef MGf_DUP |
832 | coro_state_dup, |
873 | coro_state_dup, |
833 | #else |
874 | #else |
834 | # define MGf_DUP 0 |
875 | # define MGf_DUP 0 |
… | |
… | |
856 | assert (mg->mg_type == PERL_MAGIC_ext); |
897 | assert (mg->mg_type == PERL_MAGIC_ext); |
857 | return (struct coro *)mg->mg_ptr; |
898 | return (struct coro *)mg->mg_ptr; |
858 | } |
899 | } |
859 | |
900 | |
860 | static void |
901 | static void |
861 | prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) |
902 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
862 | { |
903 | { |
863 | ta->prev = SvSTATE (prev); |
904 | ta->prev = SvSTATE (prev_sv); |
864 | ta->next = SvSTATE (next); |
905 | ta->next = SvSTATE (next_sv); |
865 | ta->flags = flags; |
|
|
866 | } |
906 | } |
867 | |
907 | |
868 | static void |
908 | static void |
869 | api_transfer (SV *prev, SV *next, int flags) |
909 | api_transfer (SV *prev_sv, SV *next_sv) |
870 | { |
910 | { |
871 | struct transfer_args ta; |
911 | struct transfer_args ta; |
872 | |
912 | |
873 | prepare_transfer (&ta, prev, next, flags); |
913 | prepare_transfer (&ta, prev_sv, next_sv); |
874 | TRANSFER (ta); |
914 | TRANSFER (ta); |
|
|
915 | } |
|
|
916 | |
|
|
917 | static int |
|
|
918 | api_save (SV *coro_sv, int new_save) |
|
|
919 | { |
|
|
920 | struct coro *coro = SvSTATE (coro_sv); |
|
|
921 | int old_save = coro->save; |
|
|
922 | |
|
|
923 | if (new_save >= 0) |
|
|
924 | coro->save = new_save; |
|
|
925 | |
|
|
926 | return old_save; |
875 | } |
927 | } |
876 | |
928 | |
877 | /** Coro ********************************************************************/ |
929 | /** Coro ********************************************************************/ |
878 | |
930 | |
879 | #define PRIO_MAX 3 |
931 | #define PRIO_MAX 3 |
… | |
… | |
925 | coro = SvSTATE (coro_sv); |
977 | coro = SvSTATE (coro_sv); |
926 | |
978 | |
927 | if (coro->flags & CF_READY) |
979 | if (coro->flags & CF_READY) |
928 | return 0; |
980 | return 0; |
929 | |
981 | |
930 | #if 0 /* this is actually harmless */ |
|
|
931 | if (coro->flags & CF_RUNNING) |
|
|
932 | croak ("Coro::ready called on currently running coroutine"); |
|
|
933 | #endif |
|
|
934 | |
|
|
935 | coro->flags |= CF_READY; |
982 | coro->flags |= CF_READY; |
936 | |
983 | |
937 | LOCK; |
984 | LOCK; |
938 | coro_enq (SvREFCNT_inc (coro_sv)); |
985 | coro_enq (SvREFCNT_inc (coro_sv)); |
939 | UNLOCK; |
986 | UNLOCK; |
… | |
… | |
942 | } |
989 | } |
943 | |
990 | |
944 | static int |
991 | static int |
945 | api_is_ready (SV *coro_sv) |
992 | api_is_ready (SV *coro_sv) |
946 | { |
993 | { |
947 | return !!SvSTATE (coro_sv)->flags & CF_READY; |
994 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
948 | } |
995 | } |
949 | |
996 | |
950 | static void |
997 | static void |
951 | prepare_schedule (struct transfer_args *ta) |
998 | prepare_schedule (struct transfer_args *ta) |
952 | { |
999 | { |
953 | SV *prev, *next; |
1000 | SV *prev_sv, *next_sv; |
954 | |
1001 | |
955 | for (;;) |
1002 | for (;;) |
956 | { |
1003 | { |
957 | LOCK; |
1004 | LOCK; |
958 | next = coro_deq (PRIO_MIN); |
1005 | next_sv = coro_deq (PRIO_MIN); |
959 | UNLOCK; |
1006 | UNLOCK; |
960 | |
1007 | |
|
|
1008 | /* nothing to schedule: call the idle handler */ |
961 | if (next) |
1009 | if (!next_sv) |
962 | break; |
|
|
963 | |
|
|
964 | { |
1010 | { |
965 | dSP; |
1011 | dSP; |
966 | |
1012 | |
967 | ENTER; |
1013 | ENTER; |
968 | SAVETMPS; |
1014 | SAVETMPS; |
969 | |
1015 | |
970 | PUSHMARK (SP); |
1016 | PUSHMARK (SP); |
971 | PUTBACK; |
1017 | PUTBACK; |
972 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
1018 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
973 | |
1019 | |
974 | FREETMPS; |
1020 | FREETMPS; |
975 | LEAVE; |
1021 | LEAVE; |
|
|
1022 | continue; |
976 | } |
1023 | } |
977 | } |
|
|
978 | |
1024 | |
979 | prev = SvRV (coro_current); |
1025 | ta->next = SvSTATE (next_sv); |
980 | SvRV_set (coro_current, next); |
1026 | |
|
|
1027 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1028 | if (ta->next->flags & CF_DESTROYED) |
|
|
1029 | { |
|
|
1030 | SvREFCNT_dec (next_sv); |
|
|
1031 | continue; |
|
|
1032 | } |
|
|
1033 | |
|
|
1034 | break; |
|
|
1035 | } |
981 | |
1036 | |
982 | /* free this only after the transfer */ |
1037 | /* free this only after the transfer */ |
|
|
1038 | prev_sv = SvRV (coro_current); |
|
|
1039 | SvRV_set (coro_current, next_sv); |
|
|
1040 | ta->prev = SvSTATE (prev_sv); |
|
|
1041 | |
|
|
1042 | assert (ta->next->flags & CF_READY); |
|
|
1043 | ta->next->flags &= ~CF_READY; |
|
|
1044 | |
983 | LOCK; |
1045 | LOCK; |
984 | free_coro_mortal (); |
1046 | free_coro_mortal (); |
|
|
1047 | coro_mortal = prev_sv; |
985 | UNLOCK; |
1048 | UNLOCK; |
986 | coro_mortal = prev; |
|
|
987 | |
|
|
988 | assert (!SvROK(prev));//D |
|
|
989 | assert (!SvROK(next));//D |
|
|
990 | |
|
|
991 | ta->prev = SvSTATE (prev); |
|
|
992 | ta->next = SvSTATE (next); |
|
|
993 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
994 | |
|
|
995 | assert (ta->flags & CF_READY); |
|
|
996 | ta->next->flags &= ~CF_READY; |
|
|
997 | } |
1049 | } |
998 | |
1050 | |
999 | static void |
1051 | static void |
1000 | prepare_cede (struct transfer_args *ta) |
1052 | prepare_cede (struct transfer_args *ta) |
1001 | { |
1053 | { |
1002 | api_ready (coro_current); |
1054 | api_ready (coro_current); |
1003 | |
|
|
1004 | prepare_schedule (ta); |
1055 | prepare_schedule (ta); |
|
|
1056 | } |
|
|
1057 | |
|
|
1058 | static int |
|
|
1059 | prepare_cede_notself (struct transfer_args *ta) |
|
|
1060 | { |
|
|
1061 | if (coro_nready) |
|
|
1062 | { |
|
|
1063 | SV *prev = SvRV (coro_current); |
|
|
1064 | prepare_schedule (ta); |
|
|
1065 | api_ready (prev); |
|
|
1066 | return 1; |
|
|
1067 | } |
|
|
1068 | else |
|
|
1069 | return 0; |
1005 | } |
1070 | } |
1006 | |
1071 | |
1007 | static void |
1072 | static void |
1008 | api_schedule (void) |
1073 | api_schedule (void) |
1009 | { |
1074 | { |
… | |
… | |
1011 | |
1076 | |
1012 | prepare_schedule (&ta); |
1077 | prepare_schedule (&ta); |
1013 | TRANSFER (ta); |
1078 | TRANSFER (ta); |
1014 | } |
1079 | } |
1015 | |
1080 | |
1016 | static void |
1081 | static int |
1017 | api_cede (void) |
1082 | api_cede (void) |
1018 | { |
1083 | { |
1019 | struct transfer_args ta; |
1084 | struct transfer_args ta; |
1020 | |
1085 | |
1021 | prepare_cede (&ta); |
1086 | prepare_cede (&ta); |
|
|
1087 | |
|
|
1088 | if (ta.prev != ta.next) |
|
|
1089 | { |
1022 | TRANSFER (ta); |
1090 | TRANSFER (ta); |
|
|
1091 | return 1; |
|
|
1092 | } |
|
|
1093 | else |
|
|
1094 | return 0; |
|
|
1095 | } |
|
|
1096 | |
|
|
1097 | static int |
|
|
1098 | api_cede_notself (void) |
|
|
1099 | { |
|
|
1100 | struct transfer_args ta; |
|
|
1101 | |
|
|
1102 | if (prepare_cede_notself (&ta)) |
|
|
1103 | { |
|
|
1104 | TRANSFER (ta); |
|
|
1105 | return 1; |
|
|
1106 | } |
|
|
1107 | else |
|
|
1108 | return 0; |
1023 | } |
1109 | } |
1024 | |
1110 | |
1025 | MODULE = Coro::State PACKAGE = Coro::State |
1111 | MODULE = Coro::State PACKAGE = Coro::State |
1026 | |
1112 | |
1027 | PROTOTYPES: DISABLE |
1113 | PROTOTYPES: DISABLE |
… | |
… | |
1033 | #endif |
1119 | #endif |
1034 | BOOT_PAGESIZE; |
1120 | BOOT_PAGESIZE; |
1035 | |
1121 | |
1036 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1122 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1037 | |
1123 | |
1038 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
1124 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1039 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
1125 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1040 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
1126 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
|
|
1127 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1128 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1041 | |
1129 | |
1042 | main_mainstack = PL_mainstack; |
1130 | main_mainstack = PL_mainstack; |
1043 | |
1131 | |
1044 | coroapi.ver = CORO_API_VERSION; |
1132 | coroapi.ver = CORO_API_VERSION; |
1045 | coroapi.transfer = api_transfer; |
1133 | coroapi.transfer = api_transfer; |
… | |
… | |
1055 | HV *hv; |
1143 | HV *hv; |
1056 | int i; |
1144 | int i; |
1057 | |
1145 | |
1058 | Newz (0, coro, 1, struct coro); |
1146 | Newz (0, coro, 1, struct coro); |
1059 | coro->args = newAV (); |
1147 | coro->args = newAV (); |
|
|
1148 | coro->save = CORO_SAVE_ALL; |
1060 | coro->flags = CF_NEW; |
1149 | coro->flags = CF_NEW; |
1061 | |
1150 | |
1062 | hv = newHV (); |
1151 | hv = newHV (); |
1063 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1152 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1064 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1153 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1065 | |
1154 | |
1066 | for (i = 1; i < items; i++) |
1155 | for (i = 1; i < items; i++) |
1067 | av_push (coro->args, newSVsv (ST (i))); |
1156 | av_push (coro->args, newSVsv (ST (i))); |
1068 | } |
1157 | } |
1069 | OUTPUT: |
1158 | OUTPUT: |
|
|
1159 | RETVAL |
|
|
1160 | |
|
|
1161 | int |
|
|
1162 | save (SV *coro, int new_save = -1) |
|
|
1163 | CODE: |
|
|
1164 | RETVAL = api_save (coro, new_save); |
|
|
1165 | OUTPUT: |
1070 | RETVAL |
1166 | RETVAL |
1071 | |
1167 | |
1072 | void |
1168 | void |
1073 | _set_stacklevel (...) |
1169 | _set_stacklevel (...) |
1074 | ALIAS: |
1170 | ALIAS: |
1075 | Coro::State::transfer = 1 |
1171 | Coro::State::transfer = 1 |
1076 | Coro::schedule = 2 |
1172 | Coro::schedule = 2 |
1077 | Coro::cede = 3 |
1173 | Coro::cede = 3 |
|
|
1174 | Coro::cede_notself = 4 |
1078 | CODE: |
1175 | CODE: |
1079 | { |
1176 | { |
1080 | struct transfer_args ta; |
1177 | struct transfer_args ta; |
1081 | |
1178 | |
1082 | switch (ix) |
1179 | switch (ix) |
1083 | { |
1180 | { |
1084 | case 0: |
1181 | case 0: |
1085 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1182 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1086 | ta.next = 0; |
1183 | ta.next = 0; |
1087 | ta.flags = TRANSFER_SET_STACKLEVEL; |
|
|
1088 | break; |
1184 | break; |
1089 | |
1185 | |
1090 | case 1: |
1186 | case 1: |
1091 | if (items != 3) |
1187 | if (items != 2) |
1092 | croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items); |
1188 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1093 | |
1189 | |
1094 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1190 | prepare_transfer (&ta, ST (0), ST (1)); |
1095 | break; |
1191 | break; |
1096 | |
1192 | |
1097 | case 2: |
1193 | case 2: |
1098 | prepare_schedule (&ta); |
1194 | prepare_schedule (&ta); |
1099 | break; |
1195 | break; |
1100 | |
1196 | |
1101 | case 3: |
1197 | case 3: |
1102 | prepare_cede (&ta); |
1198 | prepare_cede (&ta); |
1103 | break; |
1199 | break; |
|
|
1200 | |
|
|
1201 | case 4: |
|
|
1202 | if (!prepare_cede_notself (&ta)) |
|
|
1203 | XSRETURN_EMPTY; |
|
|
1204 | |
|
|
1205 | break; |
1104 | } |
1206 | } |
1105 | |
1207 | |
|
|
1208 | BARRIER; |
1106 | TRANSFER (ta); |
1209 | TRANSFER (ta); |
1107 | } |
|
|
1108 | |
1210 | |
1109 | void |
1211 | if (GIMME_V != G_VOID && ta.next != ta.prev) |
1110 | _clone_state_from (SV *dst, SV *src) |
1212 | XSRETURN_YES; |
|
|
1213 | } |
|
|
1214 | |
|
|
1215 | bool |
|
|
1216 | _destroy (SV *coro_sv) |
1111 | CODE: |
1217 | CODE: |
1112 | { |
1218 | RETVAL = coro_state_destroy (SvSTATE (coro_sv)); |
1113 | struct coro *coro_src = SvSTATE (src); |
1219 | OUTPUT: |
1114 | |
1220 | RETVAL |
1115 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
|
|
1116 | |
|
|
1117 | ++coro_src->refcnt; |
|
|
1118 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
|
|
1119 | } |
|
|
1120 | |
1221 | |
1121 | void |
1222 | void |
1122 | _exit (code) |
1223 | _exit (code) |
1123 | int code |
1224 | int code |
1124 | PROTOTYPE: $ |
1225 | PROTOTYPE: $ |
… | |
… | |
1161 | coro_ready[i] = newAV (); |
1262 | coro_ready[i] = newAV (); |
1162 | |
1263 | |
1163 | { |
1264 | { |
1164 | SV *sv = perl_get_sv("Coro::API", 1); |
1265 | SV *sv = perl_get_sv("Coro::API", 1); |
1165 | |
1266 | |
1166 | coroapi.schedule = api_schedule; |
1267 | coroapi.schedule = api_schedule; |
|
|
1268 | coroapi.save = api_save; |
1167 | coroapi.cede = api_cede; |
1269 | coroapi.cede = api_cede; |
|
|
1270 | coroapi.cede_notself = api_cede_notself; |
1168 | coroapi.ready = api_ready; |
1271 | coroapi.ready = api_ready; |
1169 | coroapi.is_ready = api_is_ready; |
1272 | coroapi.is_ready = api_is_ready; |
1170 | coroapi.nready = &coro_nready; |
1273 | coroapi.nready = &coro_nready; |
1171 | coroapi.current = coro_current; |
1274 | coroapi.current = coro_current; |
1172 | |
1275 | |
1173 | GCoroAPI = &coroapi; |
1276 | GCoroAPI = &coroapi; |
1174 | sv_setiv (sv, (IV)&coroapi); |
1277 | sv_setiv (sv, (IV)&coroapi); |
1175 | SvREADONLY_on (sv); |
1278 | SvREADONLY_on (sv); |
1176 | } |
1279 | } |
1177 | } |
1280 | } |
|
|
1281 | |
|
|
1282 | void |
|
|
1283 | _set_current (SV *current) |
|
|
1284 | PROTOTYPE: $ |
|
|
1285 | CODE: |
|
|
1286 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1287 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
1178 | |
1288 | |
1179 | int |
1289 | int |
1180 | prio (Coro::State coro, int newprio = 0) |
1290 | prio (Coro::State coro, int newprio = 0) |
1181 | ALIAS: |
1291 | ALIAS: |
1182 | nice = 1 |
1292 | nice = 1 |
… | |
… | |
1185 | RETVAL = coro->prio; |
1295 | RETVAL = coro->prio; |
1186 | |
1296 | |
1187 | if (items > 1) |
1297 | if (items > 1) |
1188 | { |
1298 | { |
1189 | if (ix) |
1299 | if (ix) |
1190 | newprio += coro->prio; |
1300 | newprio = coro->prio - newprio; |
1191 | |
1301 | |
1192 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1302 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1193 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1303 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1194 | |
1304 | |
1195 | coro->prio = newprio; |
1305 | coro->prio = newprio; |
1196 | } |
1306 | } |
1197 | } |
1307 | } |
|
|
1308 | OUTPUT: |
|
|
1309 | RETVAL |
1198 | |
1310 | |
1199 | SV * |
1311 | SV * |
1200 | ready (SV *self) |
1312 | ready (SV *self) |
1201 | PROTOTYPE: $ |
1313 | PROTOTYPE: $ |
1202 | CODE: |
1314 | CODE: |
… | |
… | |
1218 | CODE: |
1330 | CODE: |
1219 | RETVAL = coro_nready; |
1331 | RETVAL = coro_nready; |
1220 | OUTPUT: |
1332 | OUTPUT: |
1221 | RETVAL |
1333 | RETVAL |
1222 | |
1334 | |
1223 | void |
|
|
1224 | _set_current (SV *current) |
|
|
1225 | PROTOTYPE: $ |
|
|
1226 | CODE: |
|
|
1227 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1228 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
|
|
1229 | |
|
|
1230 | MODULE = Coro::State PACKAGE = Coro::AIO |
1335 | MODULE = Coro::State PACKAGE = Coro::AIO |
1231 | |
1336 | |
1232 | SV * |
1337 | SV * |
1233 | _get_state () |
1338 | _get_state () |
1234 | CODE: |
1339 | CODE: |
1235 | { |
1340 | { |
1236 | struct { |
1341 | struct io_state *data; |
1237 | int errorno; |
|
|
1238 | int laststype; |
|
|
1239 | int laststatval; |
|
|
1240 | Stat_t statcache; |
|
|
1241 | } data; |
|
|
1242 | |
1342 | |
|
|
1343 | RETVAL = newSV (sizeof (struct io_state)); |
|
|
1344 | data = (struct io_state *)SvPVX (RETVAL); |
|
|
1345 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
|
|
1346 | SvPOK_only (RETVAL); |
|
|
1347 | |
1243 | data.errorno = errno; |
1348 | data->errorno = errno; |
1244 | data.laststype = PL_laststype; |
1349 | data->laststype = PL_laststype; |
1245 | data.laststatval = PL_laststatval; |
1350 | data->laststatval = PL_laststatval; |
1246 | data.statcache = PL_statcache; |
1351 | data->statcache = PL_statcache; |
1247 | |
|
|
1248 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1249 | } |
1352 | } |
1250 | OUTPUT: |
1353 | OUTPUT: |
1251 | RETVAL |
1354 | RETVAL |
1252 | |
1355 | |
1253 | void |
1356 | void |
1254 | _set_state (char *data_) |
1357 | _set_state (char *data_) |
1255 | PROTOTYPE: $ |
1358 | PROTOTYPE: $ |
1256 | CODE: |
1359 | CODE: |
1257 | { |
1360 | { |
1258 | struct { |
1361 | struct io_state *data = (void *)data_; |
1259 | int errorno; |
|
|
1260 | int laststype; |
|
|
1261 | int laststatval; |
|
|
1262 | Stat_t statcache; |
|
|
1263 | } *data = (void *)data_; |
|
|
1264 | |
1362 | |
1265 | errno = data->errorno; |
1363 | errno = data->errorno; |
1266 | PL_laststype = data->laststype; |
1364 | PL_laststype = data->laststype; |
1267 | PL_laststatval = data->laststatval; |
1365 | PL_laststatval = data->laststatval; |
1268 | PL_statcache = data->statcache; |
1366 | PL_statcache = data->statcache; |
1269 | } |
1367 | } |
|
|
1368 | |