… | |
… | |
6 | #include "perl.h" |
6 | #include "perl.h" |
7 | #include "XSUB.h" |
7 | #include "XSUB.h" |
8 | |
8 | |
9 | #include "patchlevel.h" |
9 | #include "patchlevel.h" |
10 | |
10 | |
11 | #if PATCHLEVEL < 6 |
11 | #if PERL_VERSION < 6 |
12 | # ifndef PL_ppaddr |
12 | # ifndef PL_ppaddr |
13 | # define PL_ppaddr ppaddr |
13 | # define PL_ppaddr ppaddr |
14 | # endif |
14 | # endif |
15 | # ifndef call_sv |
15 | # ifndef call_sv |
16 | # define call_sv perl_call_sv |
16 | # define call_sv perl_call_sv |
… | |
… | |
27 | # ifndef IS_PADCONST |
27 | # ifndef IS_PADCONST |
28 | # define IS_PADCONST(v) 0 |
28 | # define IS_PADCONST(v) 0 |
29 | # endif |
29 | # endif |
30 | #endif |
30 | #endif |
31 | |
31 | |
32 | #include <signal.h> |
32 | #include <errno.h> |
|
|
33 | |
|
|
34 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
|
|
35 | # undef STACKGUARD |
|
|
36 | #endif |
|
|
37 | |
|
|
38 | #ifndef STACKGUARD |
|
|
39 | # define STACKGUARD 0 |
|
|
40 | #endif |
33 | |
41 | |
34 | #ifdef HAVE_MMAP |
42 | #ifdef HAVE_MMAP |
35 | # include <unistd.h> |
43 | # include <unistd.h> |
36 | # include <sys/mman.h> |
44 | # include <sys/mman.h> |
37 | # ifndef MAP_ANONYMOUS |
45 | # ifndef MAP_ANONYMOUS |
… | |
… | |
39 | # define MAP_ANONYMOUS MAP_ANON |
47 | # define MAP_ANONYMOUS MAP_ANON |
40 | # else |
48 | # else |
41 | # undef HAVE_MMAP |
49 | # undef HAVE_MMAP |
42 | # endif |
50 | # endif |
43 | # endif |
51 | # endif |
|
|
52 | # include <limits.h> |
|
|
53 | # ifndef PAGESIZE |
|
|
54 | # define PAGESIZE pagesize |
|
|
55 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
|
|
56 | static long pagesize; |
|
|
57 | # else |
|
|
58 | # define BOOT_PAGESIZE |
|
|
59 | # endif |
44 | #endif |
60 | #endif |
45 | |
61 | |
46 | #define SUB_INIT "Coro::State::initialize" |
62 | #define SUB_INIT "Coro::State::initialize" |
47 | #define UCORO_STATE "_coro_state" |
63 | #define UCORO_STATE "_coro_state" |
48 | |
64 | |
… | |
… | |
57 | |
73 | |
58 | #include "CoroAPI.h" |
74 | #include "CoroAPI.h" |
59 | |
75 | |
60 | #ifdef USE_ITHREADS |
76 | #ifdef USE_ITHREADS |
61 | static perl_mutex coro_mutex; |
77 | static perl_mutex coro_mutex; |
62 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
78 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
63 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
79 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
64 | #else |
80 | #else |
65 | # define LOCK 0 |
81 | # define LOCK (void)0 |
66 | # define UNLOCK 0 |
82 | # define UNLOCK (void)0 |
67 | #endif |
83 | #endif |
68 | |
84 | |
69 | static struct CoroAPI coroapi; |
85 | static struct CoroAPI coroapi; |
70 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
86 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
71 | static HV *coro_state_stash; |
87 | static HV *coro_state_stash; |
… | |
… | |
84 | void *sptr; |
100 | void *sptr; |
85 | long ssize; /* positive == mmap, otherwise malloc */ |
101 | long ssize; /* positive == mmap, otherwise malloc */ |
86 | } coro_stack; |
102 | } coro_stack; |
87 | |
103 | |
88 | struct coro { |
104 | struct coro { |
89 | /* the top-level JMPENV for each coroutine, needed to catch dies. */ |
|
|
90 | JMPENV start_env; |
|
|
91 | |
|
|
92 | /* the optional C context */ |
105 | /* the optional C context */ |
93 | coro_stack *stack; |
106 | coro_stack *stack; |
94 | void *cursp; |
107 | void *cursp; |
95 | int gencnt; |
108 | int gencnt; |
96 | |
109 | |
… | |
… | |
128 | I32 savestack_ix; |
141 | I32 savestack_ix; |
129 | I32 savestack_max; |
142 | I32 savestack_max; |
130 | OP **retstack; |
143 | OP **retstack; |
131 | I32 retstack_ix; |
144 | I32 retstack_ix; |
132 | I32 retstack_max; |
145 | I32 retstack_max; |
|
|
146 | PMOP *curpm; |
133 | COP *curcop; |
147 | COP *curcop; |
134 | JMPENV *top_env; |
148 | JMPENV *top_env; |
135 | |
149 | |
136 | /* data associated with this coroutine (initial args) */ |
150 | /* data associated with this coroutine (initial args) */ |
137 | AV *args; |
151 | AV *args; |
138 | }; |
152 | }; |
139 | |
153 | |
140 | typedef struct coro *Coro__State; |
154 | typedef struct coro *Coro__State; |
141 | typedef struct coro *Coro__State_or_hashref; |
155 | typedef struct coro *Coro__State_or_hashref; |
142 | |
156 | |
143 | /* mostly copied from op.c:cv_clone2 */ |
157 | static AV * |
144 | STATIC AV * |
158 | coro_clone_padlist (pTHX_ CV *cv) |
145 | clone_padlist (pTHX_ AV *protopadlist) |
|
|
146 | { |
159 | { |
147 | AV *av; |
160 | AV *padlist = CvPADLIST (cv); |
148 | I32 ix; |
|
|
149 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
|
|
150 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
|
|
151 | SV **pname = AvARRAY (protopad_name); |
|
|
152 | SV **ppad = AvARRAY (protopad); |
|
|
153 | I32 fname = AvFILLp (protopad_name); |
|
|
154 | I32 fpad = AvFILLp (protopad); |
|
|
155 | AV *newpadlist, *newpad_name, *newpad; |
161 | AV *newpadlist, *newpad; |
156 | SV **npad; |
|
|
157 | |
|
|
158 | newpad_name = newAV (); |
|
|
159 | for (ix = fname; ix >= 0; ix--) |
|
|
160 | av_store (newpad_name, ix, SvREFCNT_inc (pname[ix])); |
|
|
161 | |
|
|
162 | newpad = newAV (); |
|
|
163 | av_fill (newpad, AvFILLp (protopad)); |
|
|
164 | npad = AvARRAY (newpad); |
|
|
165 | |
162 | |
166 | newpadlist = newAV (); |
163 | newpadlist = newAV (); |
167 | AvREAL_off (newpadlist); |
164 | AvREAL_off (newpadlist); |
168 | av_store (newpadlist, 0, (SV *) newpad_name); |
165 | #if PERL_VERSION < 9 |
|
|
166 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
|
|
167 | #else |
|
|
168 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
169 | #endif |
|
|
170 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
|
|
171 | --AvFILLp (padlist); |
|
|
172 | |
|
|
173 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
169 | av_store (newpadlist, 1, (SV *) newpad); |
174 | av_store (newpadlist, 1, (SV *)newpad); |
170 | |
|
|
171 | av = newAV (); /* will be @_ */ |
|
|
172 | av_extend (av, 0); |
|
|
173 | av_store (newpad, 0, (SV *) av); |
|
|
174 | AvFLAGS (av) = AVf_REIFY; |
|
|
175 | |
|
|
176 | for (ix = fpad; ix > 0; ix--) |
|
|
177 | { |
|
|
178 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
|
|
179 | |
|
|
180 | if (namesv && namesv != &PL_sv_undef) |
|
|
181 | { |
|
|
182 | char *name = SvPVX (namesv); /* XXX */ |
|
|
183 | |
|
|
184 | if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') |
|
|
185 | { /* lexical from outside? */ |
|
|
186 | npad[ix] = SvREFCNT_inc (ppad[ix]); |
|
|
187 | } |
|
|
188 | else |
|
|
189 | { /* our own lexical */ |
|
|
190 | SV *sv; |
|
|
191 | if (*name == '&') |
|
|
192 | sv = SvREFCNT_inc (ppad[ix]); |
|
|
193 | else if (*name == '@') |
|
|
194 | sv = (SV *) newAV (); |
|
|
195 | else if (*name == '%') |
|
|
196 | sv = (SV *) newHV (); |
|
|
197 | else |
|
|
198 | sv = NEWSV (0, 0); |
|
|
199 | |
|
|
200 | #ifdef SvPADBUSY |
|
|
201 | if (!SvPADBUSY (sv)) |
|
|
202 | #endif |
|
|
203 | SvPADMY_on (sv); |
|
|
204 | |
|
|
205 | npad[ix] = sv; |
|
|
206 | } |
|
|
207 | } |
|
|
208 | else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
|
|
209 | { |
|
|
210 | npad[ix] = SvREFCNT_inc (ppad[ix]); |
|
|
211 | } |
|
|
212 | else |
|
|
213 | { |
|
|
214 | SV *sv = NEWSV (0, 0); |
|
|
215 | SvPADTMP_on (sv); |
|
|
216 | npad[ix] = sv; |
|
|
217 | } |
|
|
218 | } |
|
|
219 | |
|
|
220 | #if 0 /* return -ENOTUNDERSTOOD */ |
|
|
221 | /* Now that vars are all in place, clone nested closures. */ |
|
|
222 | |
|
|
223 | for (ix = fpad; ix > 0; ix--) { |
|
|
224 | SV* namesv = (ix <= fname) ? pname[ix] : Nullsv; |
|
|
225 | if (namesv |
|
|
226 | && namesv != &PL_sv_undef |
|
|
227 | && !(SvFLAGS(namesv) & SVf_FAKE) |
|
|
228 | && *SvPVX(namesv) == '&' |
|
|
229 | && CvCLONE(ppad[ix])) |
|
|
230 | { |
|
|
231 | CV *kid = cv_clone((CV*)ppad[ix]); |
|
|
232 | SvREFCNT_dec(ppad[ix]); |
|
|
233 | CvCLONE_on(kid); |
|
|
234 | SvPADMY_on(kid); |
|
|
235 | npad[ix] = (SV*)kid; |
|
|
236 | } |
|
|
237 | } |
|
|
238 | #endif |
|
|
239 | |
175 | |
240 | return newpadlist; |
176 | return newpadlist; |
241 | } |
177 | } |
242 | |
178 | |
243 | STATIC void |
179 | static void |
244 | free_padlist (pTHX_ AV *padlist) |
180 | free_padlist (pTHX_ AV *padlist) |
245 | { |
181 | { |
246 | /* may be during global destruction */ |
182 | /* may be during global destruction */ |
247 | if (SvREFCNT (padlist)) |
183 | if (SvREFCNT (padlist)) |
248 | { |
184 | { |
… | |
… | |
262 | |
198 | |
263 | SvREFCNT_dec ((SV*)padlist); |
199 | SvREFCNT_dec ((SV*)padlist); |
264 | } |
200 | } |
265 | } |
201 | } |
266 | |
202 | |
267 | STATIC int |
203 | static int |
268 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
204 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
269 | { |
205 | { |
270 | AV *padlist; |
206 | AV *padlist; |
271 | AV *av = (AV *)mg->mg_obj; |
207 | AV *av = (AV *)mg->mg_obj; |
272 | |
208 | |
273 | /* casting is fun. */ |
209 | /* casting is fun. */ |
274 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
210 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
275 | free_padlist (aTHX_ padlist); |
211 | free_padlist (aTHX_ padlist); |
276 | |
212 | |
277 | SvREFCNT_dec (av); |
213 | SvREFCNT_dec (av); |
|
|
214 | |
|
|
215 | return 0; |
278 | } |
216 | } |
279 | |
217 | |
280 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
218 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
281 | |
219 | |
282 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
220 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
283 | |
221 | |
284 | /* the next two functions merely cache the padlists */ |
222 | /* the next two functions merely cache the padlists */ |
285 | STATIC void |
223 | static void |
286 | get_padlist (pTHX_ CV *cv) |
224 | get_padlist (pTHX_ CV *cv) |
287 | { |
225 | { |
288 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
226 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
289 | |
227 | |
290 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
228 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
291 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
229 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
292 | else |
230 | else |
|
|
231 | { |
|
|
232 | #if 0 |
|
|
233 | /* this should work - but it doesn't :( */ |
|
|
234 | CV *cp = Perl_cv_clone (aTHX_ cv); |
|
|
235 | CvPADLIST (cv) = CvPADLIST (cp); |
|
|
236 | CvPADLIST (cp) = 0; |
|
|
237 | SvREFCNT_dec (cp); |
|
|
238 | #else |
293 | CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv)); |
239 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
|
|
240 | #endif |
|
|
241 | } |
294 | } |
242 | } |
295 | |
243 | |
296 | STATIC void |
244 | static void |
297 | put_padlist (pTHX_ CV *cv) |
245 | put_padlist (pTHX_ CV *cv) |
298 | { |
246 | { |
299 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
247 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
300 | |
248 | |
301 | if (!mg) |
249 | if (!mg) |
… | |
… | |
344 | PL_scopestack_ix = c->scopestack_ix; |
292 | PL_scopestack_ix = c->scopestack_ix; |
345 | PL_scopestack_max = c->scopestack_max; |
293 | PL_scopestack_max = c->scopestack_max; |
346 | PL_savestack = c->savestack; |
294 | PL_savestack = c->savestack; |
347 | PL_savestack_ix = c->savestack_ix; |
295 | PL_savestack_ix = c->savestack_ix; |
348 | PL_savestack_max = c->savestack_max; |
296 | PL_savestack_max = c->savestack_max; |
|
|
297 | #if PERL_VERSION < 9 |
349 | PL_retstack = c->retstack; |
298 | PL_retstack = c->retstack; |
350 | PL_retstack_ix = c->retstack_ix; |
299 | PL_retstack_ix = c->retstack_ix; |
351 | PL_retstack_max = c->retstack_max; |
300 | PL_retstack_max = c->retstack_max; |
|
|
301 | #endif |
|
|
302 | PL_curpm = c->curpm; |
352 | PL_curcop = c->curcop; |
303 | PL_curcop = c->curcop; |
353 | PL_top_env = c->top_env; |
304 | PL_top_env = c->top_env; |
354 | |
305 | |
355 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
306 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
356 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
307 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
… | |
… | |
415 | } |
366 | } |
416 | |
367 | |
417 | PUSHs ((SV *)CvPADLIST(cv)); |
368 | PUSHs ((SV *)CvPADLIST(cv)); |
418 | PUSHs ((SV *)cv); |
369 | PUSHs ((SV *)cv); |
419 | |
370 | |
420 | get_padlist (aTHX_ cv); /* this is a monster */ |
371 | get_padlist (aTHX_ cv); |
421 | } |
372 | } |
422 | } |
373 | } |
423 | #ifdef CXt_FORMAT |
374 | #ifdef CXt_FORMAT |
424 | else if (CxTYPE(cx) == CXt_FORMAT) |
375 | else if (CxTYPE(cx) == CXt_FORMAT) |
425 | { |
376 | { |
… | |
… | |
469 | c->scopestack_ix = PL_scopestack_ix; |
420 | c->scopestack_ix = PL_scopestack_ix; |
470 | c->scopestack_max = PL_scopestack_max; |
421 | c->scopestack_max = PL_scopestack_max; |
471 | c->savestack = PL_savestack; |
422 | c->savestack = PL_savestack; |
472 | c->savestack_ix = PL_savestack_ix; |
423 | c->savestack_ix = PL_savestack_ix; |
473 | c->savestack_max = PL_savestack_max; |
424 | c->savestack_max = PL_savestack_max; |
|
|
425 | #if PERL_VERSION < 9 |
474 | c->retstack = PL_retstack; |
426 | c->retstack = PL_retstack; |
475 | c->retstack_ix = PL_retstack_ix; |
427 | c->retstack_ix = PL_retstack_ix; |
476 | c->retstack_max = PL_retstack_max; |
428 | c->retstack_max = PL_retstack_max; |
|
|
429 | #endif |
|
|
430 | c->curpm = PL_curpm; |
477 | c->curcop = PL_curcop; |
431 | c->curcop = PL_curcop; |
478 | c->top_env = PL_top_env; |
432 | c->top_env = PL_top_env; |
479 | } |
433 | } |
480 | |
434 | |
481 | /* |
435 | /* |
482 | * allocate various perl stacks. This is an exact copy |
436 | * allocate various perl stacks. This is an exact copy |
483 | * of perl.c:init_stacks, except that it uses less memory |
437 | * of perl.c:init_stacks, except that it uses less memory |
484 | * on the (sometimes correct) assumption that coroutines do |
438 | * on the (sometimes correct) assumption that coroutines do |
485 | * not usually need a lot of stackspace. |
439 | * not usually need a lot of stackspace. |
486 | */ |
440 | */ |
487 | STATIC void |
441 | static void |
488 | coro_init_stacks (pTHX) |
442 | coro_init_stacks (pTHX) |
489 | { |
443 | { |
490 | LOCK; |
444 | LOCK; |
491 | |
445 | |
492 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
446 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
… | |
… | |
517 | |
471 | |
518 | New(54,PL_savestack,96,ANY); |
472 | New(54,PL_savestack,96,ANY); |
519 | PL_savestack_ix = 0; |
473 | PL_savestack_ix = 0; |
520 | PL_savestack_max = 96; |
474 | PL_savestack_max = 96; |
521 | |
475 | |
|
|
476 | #if PERL_VERSION < 9 |
522 | New(54,PL_retstack,8,OP*); |
477 | New(54,PL_retstack,8,OP*); |
523 | PL_retstack_ix = 0; |
478 | PL_retstack_ix = 0; |
524 | PL_retstack_max = 8; |
479 | PL_retstack_max = 8; |
|
|
480 | #endif |
525 | |
481 | |
526 | UNLOCK; |
482 | UNLOCK; |
527 | } |
483 | } |
528 | |
484 | |
529 | /* |
485 | /* |
530 | * destroy the stacks, the callchain etc... |
486 | * destroy the stacks, the callchain etc... |
531 | */ |
487 | */ |
532 | STATIC void |
488 | static void |
533 | destroy_stacks(pTHX) |
489 | destroy_stacks(pTHX) |
534 | { |
490 | { |
535 | if (!IN_DESTRUCT) |
491 | if (!IN_DESTRUCT) |
536 | { |
492 | { |
537 | /* is this ugly, I ask? */ |
493 | /* is this ugly, I ask? */ |
… | |
… | |
569 | |
525 | |
570 | Safefree (PL_tmps_stack); |
526 | Safefree (PL_tmps_stack); |
571 | Safefree (PL_markstack); |
527 | Safefree (PL_markstack); |
572 | Safefree (PL_scopestack); |
528 | Safefree (PL_scopestack); |
573 | Safefree (PL_savestack); |
529 | Safefree (PL_savestack); |
|
|
530 | #if PERL_VERSION < 9 |
574 | Safefree (PL_retstack); |
531 | Safefree (PL_retstack); |
|
|
532 | #endif |
575 | } |
533 | } |
576 | |
534 | |
577 | static void |
535 | static void |
578 | allocate_stack (Coro__State ctx, int alloc) |
536 | allocate_stack (Coro__State ctx, int alloc) |
579 | { |
537 | { |
… | |
… | |
586 | stack->gencnt = ctx->gencnt = 0; |
544 | stack->gencnt = ctx->gencnt = 0; |
587 | |
545 | |
588 | if (alloc) |
546 | if (alloc) |
589 | { |
547 | { |
590 | #if HAVE_MMAP |
548 | #if HAVE_MMAP |
591 | stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ |
549 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ |
592 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
550 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
593 | if (stack->sptr == (void *)-1) |
551 | if (stack->sptr != (void *)-1) |
594 | #endif |
|
|
595 | { |
552 | { |
596 | /*FIXME*//*D*//* reasonable stack size! */ |
553 | # if STACKGUARD |
|
|
554 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
|
|
555 | # endif |
|
|
556 | } |
|
|
557 | else |
|
|
558 | #endif |
|
|
559 | { |
597 | stack->ssize = - (8192 * sizeof (long)); |
560 | stack->ssize = - (STACKSIZE * (long)sizeof (long)); |
598 | New (0, stack->sptr, 8192, long); |
561 | New (0, stack->sptr, STACKSIZE, long); |
599 | } |
562 | } |
600 | } |
563 | } |
601 | else |
564 | else |
602 | stack->sptr = 0; |
565 | stack->sptr = 0; |
603 | |
566 | |
… | |
… | |
663 | } |
626 | } |
664 | else |
627 | else |
665 | { |
628 | { |
666 | UNOP myop; |
629 | UNOP myop; |
667 | |
630 | |
668 | PL_op = (OP *)&myop; |
|
|
669 | |
|
|
670 | Zero(&myop, 1, UNOP); |
631 | Zero(&myop, 1, UNOP); |
671 | myop.op_next = Nullop; |
632 | myop.op_next = Nullop; |
672 | myop.op_flags = OPf_WANT_VOID; |
633 | myop.op_flags = OPf_WANT_VOID; |
|
|
634 | |
|
|
635 | PL_op = (OP *)&myop; |
673 | |
636 | |
674 | PUSHMARK(SP); |
637 | PUSHMARK(SP); |
675 | XPUSHs (sub_init); |
638 | XPUSHs (sub_init); |
676 | /* |
639 | /* |
677 | * the next line is slightly wrong, as PL_op->op_next |
640 | * the next line is slightly wrong, as PL_op->op_next |
… | |
… | |
694 | /* |
657 | /* |
695 | * this is a _very_ stripped down perl interpreter ;) |
658 | * this is a _very_ stripped down perl interpreter ;) |
696 | */ |
659 | */ |
697 | dTHX; |
660 | dTHX; |
698 | Coro__State ctx = (Coro__State)arg; |
661 | Coro__State ctx = (Coro__State)arg; |
699 | JMPENV coro_start_env; |
|
|
700 | |
662 | |
701 | PL_top_env = &ctx->start_env; |
663 | PL_top_env = &PL_start_env; |
702 | |
664 | |
703 | ctx->cursp = 0; |
665 | ctx->cursp = 0; |
704 | PL_op = PL_op->op_next; |
666 | PL_op = PL_op->op_next; |
705 | CALLRUNOPS(aTHX); |
667 | CALLRUNOPS(aTHX); |
706 | |
668 | |
707 | abort (); |
669 | abort (); |
708 | } |
670 | } |
709 | |
671 | |
710 | STATIC void |
672 | static void |
711 | transfer (pTHX_ struct coro *prev, struct coro *next, int flags) |
673 | transfer (pTHX_ struct coro *prev, struct coro *next, int flags) |
712 | { |
674 | { |
713 | dSTACKLEVEL; |
675 | dSTACKLEVEL; |
714 | |
676 | |
715 | if (prev != next) |
677 | if (prev != next) |
716 | { |
678 | { |
|
|
679 | /* has this coro been created yet? */ |
717 | if (next->mainstack) |
680 | if (next->mainstack) |
718 | { |
681 | { |
719 | LOCK; |
682 | LOCK; |
720 | SAVE (prev, flags); |
683 | SAVE (prev, flags); |
721 | LOAD (next); |
684 | LOAD (next); |
… | |
… | |
760 | { |
723 | { |
761 | LOCK; |
724 | LOCK; |
762 | SAVE (prev, -1); /* first get rid of the old state */ |
725 | SAVE (prev, -1); /* first get rid of the old state */ |
763 | UNLOCK; |
726 | UNLOCK; |
764 | |
727 | |
|
|
728 | /* create the coroutine for the first time */ |
765 | if (flags & TRANSFER_SAVE_CCTXT) |
729 | if (flags & TRANSFER_SAVE_CCTXT) |
766 | { |
730 | { |
767 | if (!prev->stack) |
731 | if (!prev->stack) |
768 | allocate_stack (prev, 0); |
732 | allocate_stack (prev, 0); |
769 | |
733 | |
|
|
734 | /* the new coroutine starts with start_env again */ |
|
|
735 | PL_top_env = &PL_start_env; |
|
|
736 | |
770 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
737 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
771 | { |
738 | { |
772 | PL_top_env = &next->start_env; |
|
|
773 | |
|
|
774 | setup_coro (next); |
739 | setup_coro (next); |
775 | next->cursp = stacklevel; |
740 | next->cursp = stacklevel; |
776 | |
741 | |
777 | prev->stack->refcnt++; |
742 | prev->stack->refcnt++; |
778 | prev->stack->usecnt++; |
743 | prev->stack->usecnt++; |
… | |
… | |
827 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
792 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
828 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
793 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
829 | \ |
794 | \ |
830 | } while(0) |
795 | } while(0) |
831 | |
796 | |
832 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
797 | #define SvSTATE(sv) INT2PTR (struct coro *, SvIV (sv)) |
833 | |
798 | |
834 | static void |
799 | static void |
835 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
800 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
836 | { |
801 | { |
837 | SV_CORO (prev, "Coro::transfer"); |
802 | SV_CORO (prev, "Coro::transfer"); |
… | |
… | |
855 | static int coro_nready; |
820 | static int coro_nready; |
856 | |
821 | |
857 | static void |
822 | static void |
858 | coro_enq (pTHX_ SV *sv) |
823 | coro_enq (pTHX_ SV *sv) |
859 | { |
824 | { |
|
|
825 | SV **xprio; |
|
|
826 | int prio; |
|
|
827 | |
860 | if (SvTYPE (sv) == SVt_PVHV) |
828 | if (SvTYPE (sv) != SVt_PVHV) |
861 | { |
|
|
862 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
|
|
863 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
864 | |
|
|
865 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
866 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
867 | : prio; |
|
|
868 | |
|
|
869 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
870 | coro_nready++; |
|
|
871 | |
|
|
872 | return; |
|
|
873 | } |
|
|
874 | |
|
|
875 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
829 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
830 | |
|
|
831 | xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
|
|
832 | prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
|
|
833 | |
|
|
834 | prio = prio > PRIO_MAX ? PRIO_MAX |
|
|
835 | : prio < PRIO_MIN ? PRIO_MIN |
|
|
836 | : prio; |
|
|
837 | |
|
|
838 | av_push (coro_ready [prio - PRIO_MIN], sv); |
|
|
839 | coro_nready++; |
876 | } |
840 | } |
877 | |
841 | |
878 | static SV * |
842 | static SV * |
879 | coro_deq (pTHX_ int min_prio) |
843 | coro_deq (pTHX_ int min_prio) |
880 | { |
844 | { |
… | |
… | |
955 | BOOT: |
919 | BOOT: |
956 | { /* {} necessary for stoopid perl-5.6.x */ |
920 | { /* {} necessary for stoopid perl-5.6.x */ |
957 | #ifdef USE_ITHREADS |
921 | #ifdef USE_ITHREADS |
958 | MUTEX_INIT (&coro_mutex); |
922 | MUTEX_INIT (&coro_mutex); |
959 | #endif |
923 | #endif |
|
|
924 | BOOT_PAGESIZE; |
960 | |
925 | |
961 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
926 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
962 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
927 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
963 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
928 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
964 | |
929 | |
… | |
… | |
986 | Newz (0, coro, 1, struct coro); |
951 | Newz (0, coro, 1, struct coro); |
987 | |
952 | |
988 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
953 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
989 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
954 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
990 | /*coro->stack = 0;*/ |
955 | /*coro->stack = 0;*/ |
991 | |
|
|
992 | /* same as JMPENV_BOOTSTRAP */ |
|
|
993 | /* we might be able to recycle start_env, but safe is safe */ |
|
|
994 | /*Zero(&coro->start_env, 1, JMPENV);*/ |
|
|
995 | coro->start_env.je_ret = -1; |
|
|
996 | coro->start_env.je_mustcatch = TRUE; |
|
|
997 | |
956 | |
998 | RETVAL = coro; |
957 | RETVAL = coro; |
999 | OUTPUT: |
958 | OUTPUT: |
1000 | RETVAL |
959 | RETVAL |
1001 | |
960 | |
… | |
… | |
1045 | CODE: |
1004 | CODE: |
1046 | _exit (code); |
1005 | _exit (code); |
1047 | |
1006 | |
1048 | MODULE = Coro::State PACKAGE = Coro::Cont |
1007 | MODULE = Coro::State PACKAGE = Coro::Cont |
1049 | |
1008 | |
1050 | # this is slightly dirty (should expose a c-level api) |
|
|
1051 | |
|
|
1052 | void |
1009 | void |
1053 | yield(...) |
1010 | yield(...) |
1054 | PROTOTYPE: @ |
1011 | PROTOTYPE: @ |
1055 | CODE: |
1012 | CODE: |
1056 | SV *yieldstack; |
1013 | SV *yieldstack; |
… | |
… | |
1069 | av_fill (defav, items - 1); |
1026 | av_fill (defav, items - 1); |
1070 | while (items--) |
1027 | while (items--) |
1071 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1028 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1072 | |
1029 | |
1073 | sv = av_pop ((AV *)SvRV (yieldstack)); |
1030 | sv = av_pop ((AV *)SvRV (yieldstack)); |
1074 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1031 | prev = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)))); |
1075 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1032 | next = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)))); |
1076 | SvREFCNT_dec (sv); |
1033 | SvREFCNT_dec (sv); |
1077 | |
1034 | |
1078 | transfer (aTHX_ prev, next, 0); |
1035 | transfer (aTHX_ prev, next, 0); |
1079 | |
1036 | |
1080 | MODULE = Coro::State PACKAGE = Coro |
1037 | MODULE = Coro::State PACKAGE = Coro |
1081 | |
|
|
1082 | # this is slightly dirty (should expose a c-level api) |
|
|
1083 | |
1038 | |
1084 | BOOT: |
1039 | BOOT: |
1085 | { |
1040 | { |
1086 | int i; |
1041 | int i; |
1087 | HV *stash = gv_stashpv ("Coro", TRUE); |
1042 | HV *stash = gv_stashpv ("Coro", TRUE); |
… | |
… | |
1107 | coroapi.ready = api_ready; |
1062 | coroapi.ready = api_ready; |
1108 | coroapi.nready = &coro_nready; |
1063 | coroapi.nready = &coro_nready; |
1109 | coroapi.current = coro_current; |
1064 | coroapi.current = coro_current; |
1110 | |
1065 | |
1111 | GCoroAPI = &coroapi; |
1066 | GCoroAPI = &coroapi; |
1112 | sv_setiv(sv, (IV)&coroapi); |
1067 | sv_setiv (sv, (IV)&coroapi); |
1113 | SvREADONLY_on(sv); |
1068 | SvREADONLY_on (sv); |
1114 | } |
1069 | } |
1115 | } |
1070 | } |
1116 | |
1071 | |
1117 | #if !PERL_MICRO |
1072 | #if !PERL_MICRO |
1118 | |
1073 | |
… | |
… | |
1143 | cede(...) |
1098 | cede(...) |
1144 | PROTOTYPE: |
1099 | PROTOTYPE: |
1145 | CODE: |
1100 | CODE: |
1146 | api_cede (); |
1101 | api_cede (); |
1147 | |
1102 | |
|
|
1103 | # and these are hacks |
|
|
1104 | SV * |
|
|
1105 | _aio_get_state () |
|
|
1106 | CODE: |
|
|
1107 | { |
|
|
1108 | struct { |
|
|
1109 | int errorno; |
|
|
1110 | int laststype; |
|
|
1111 | int laststatval; |
|
|
1112 | Stat_t statcache; |
|
|
1113 | } data; |
|
|
1114 | |
|
|
1115 | data.errorno = errno; |
|
|
1116 | data.laststype = PL_laststype; |
|
|
1117 | data.laststatval = PL_laststatval; |
|
|
1118 | data.statcache = PL_statcache; |
|
|
1119 | |
|
|
1120 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1121 | } |
|
|
1122 | OUTPUT: |
|
|
1123 | RETVAL |
|
|
1124 | |
|
|
1125 | void |
|
|
1126 | _aio_set_state (char *data_) |
|
|
1127 | PROTOTYPE: $ |
|
|
1128 | CODE: |
|
|
1129 | { |
|
|
1130 | struct { |
|
|
1131 | int errorno; |
|
|
1132 | int laststype; |
|
|
1133 | int laststatval; |
|
|
1134 | Stat_t statcache; |
|
|
1135 | } *data = (void *)data_; |
|
|
1136 | |
|
|
1137 | errno = data->errorno; |
|
|
1138 | PL_laststype = data->laststype; |
|
|
1139 | PL_laststatval = data->laststatval; |
|
|
1140 | PL_statcache = data->statcache; |
|
|
1141 | } |