1 | #define PERL_NO_GET_CONTEXT |
|
|
2 | |
|
|
3 | #include "libcoro/coro.c" |
1 | #include "libcoro/coro.c" |
4 | |
2 | |
5 | #include "EXTERN.h" |
3 | #include "EXTERN.h" |
6 | #include "perl.h" |
4 | #include "perl.h" |
7 | #include "XSUB.h" |
5 | #include "XSUB.h" |
8 | |
6 | |
9 | #include "patchlevel.h" |
7 | #include "patchlevel.h" |
10 | |
8 | |
11 | #if PERL_VERSION < 6 |
9 | #if USE_VALGRIND |
|
|
10 | # include <valgrind/valgrind.h> |
|
|
11 | #endif |
|
|
12 | |
|
|
13 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
|
|
14 | (PERL_REVISION > (a) \ |
|
|
15 | || (PERL_REVISION == (a) \ |
|
|
16 | && (PERL_VERSION > (b) \ |
|
|
17 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
|
|
18 | |
|
|
19 | #if !PERL_VERSION_ATLEAST (5,6,0) |
12 | # ifndef PL_ppaddr |
20 | # ifndef PL_ppaddr |
13 | # define PL_ppaddr ppaddr |
21 | # define PL_ppaddr ppaddr |
14 | # endif |
22 | # endif |
15 | # ifndef call_sv |
23 | # ifndef call_sv |
16 | # define call_sv perl_call_sv |
24 | # define call_sv perl_call_sv |
… | |
… | |
27 | # ifndef IS_PADCONST |
35 | # ifndef IS_PADCONST |
28 | # define IS_PADCONST(v) 0 |
36 | # define IS_PADCONST(v) 0 |
29 | # endif |
37 | # endif |
30 | #endif |
38 | #endif |
31 | |
39 | |
|
|
40 | #include <stdio.h> |
32 | #include <errno.h> |
41 | #include <errno.h> |
33 | |
42 | |
34 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
43 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
35 | # undef STACKGUARD |
44 | # undef STACKGUARD |
36 | #endif |
45 | #endif |
… | |
… | |
57 | # else |
66 | # else |
58 | # define BOOT_PAGESIZE |
67 | # define BOOT_PAGESIZE |
59 | # endif |
68 | # endif |
60 | #endif |
69 | #endif |
61 | |
70 | |
62 | #define SUB_INIT "Coro::State::initialize" |
|
|
63 | |
|
|
64 | /* The next macro should declare a variable stacklevel that contains and approximation |
71 | /* The next macro should declare a variable stacklevel that contains and approximation |
65 | * to the current C stack pointer. Its property is that it changes with each call |
72 | * to the current C stack pointer. Its property is that it changes with each call |
66 | * and should be unique. */ |
73 | * and should be unique. */ |
|
|
74 | #define dSTACKLEVEL int stacklevel |
67 | #define dSTACKLEVEL void *stacklevel = &stacklevel |
75 | #define STACKLEVEL ((void *)&stacklevel) |
68 | |
76 | |
69 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
77 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
70 | |
78 | |
71 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
79 | #if __GNUC__ >= 3 |
|
|
80 | # define attribute(x) __attribute__(x) |
|
|
81 | #else |
|
|
82 | # define attribute(x) |
|
|
83 | #endif |
|
|
84 | |
|
|
85 | #define NOINLINE attribute ((noinline)) |
72 | |
86 | |
73 | #include "CoroAPI.h" |
87 | #include "CoroAPI.h" |
|
|
88 | |
|
|
89 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
74 | |
90 | |
75 | #ifdef USE_ITHREADS |
91 | #ifdef USE_ITHREADS |
76 | static perl_mutex coro_mutex; |
92 | static perl_mutex coro_mutex; |
77 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
93 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
78 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
94 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
… | |
… | |
84 | static struct CoroAPI coroapi; |
100 | static struct CoroAPI coroapi; |
85 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
101 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
86 | static HV *coro_state_stash, *coro_stash; |
102 | static HV *coro_state_stash, *coro_stash; |
87 | static SV *coro_mortal; /* will be freed after next transfer */ |
103 | static SV *coro_mortal; /* will be freed after next transfer */ |
88 | |
104 | |
89 | /* this is actually not just the c stack but also c registers etc... */ |
105 | /* this is a structure representing a c-level coroutine */ |
90 | typedef struct coro_stack { |
106 | typedef struct coro_stack { |
91 | struct coro_stack *next; |
107 | struct coro_stack *next; |
92 | |
108 | |
93 | void *idle_sp; |
109 | /* the stack */ |
94 | void *sptr; |
110 | void *sptr; |
95 | long ssize; /* positive == mmap, otherwise malloc */ |
111 | long ssize; /* positive == mmap, otherwise malloc */ |
96 | |
112 | |
97 | /* cpu state */ |
113 | /* cpu state */ |
|
|
114 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
115 | JMPENV *top_env; |
98 | coro_context cctx; |
116 | coro_context cctx; |
|
|
117 | |
|
|
118 | #if USE_VALGRIND |
|
|
119 | int valgrind_id; |
|
|
120 | #endif |
99 | } coro_stack; |
121 | } coro_stack; |
100 | |
122 | |
101 | static coro_stack *main_stack; |
123 | /* this is a structure representing a perl-level coroutine */ |
102 | |
|
|
103 | struct coro { |
124 | struct coro { |
|
|
125 | /* the c coroutine allocated to this perl coroutine, if any */ |
|
|
126 | coro_stack *stack; |
|
|
127 | |
|
|
128 | /* data associated with this coroutine (initial args) */ |
|
|
129 | AV *args; |
|
|
130 | int refcnt; |
|
|
131 | |
104 | /* optionally saved, might be zero */ |
132 | /* optionally saved, might be zero */ |
105 | AV *defav; |
133 | AV *defav; |
106 | SV *defsv; |
134 | SV *defsv; |
107 | SV *errsv; |
135 | SV *errsv; |
108 | |
136 | |
109 | /* saved global state not related to stacks */ |
137 | /* saved global state not related to stacks */ |
110 | U8 dowarn; |
138 | U8 dowarn; |
111 | I32 in_eval; |
139 | I32 in_eval; |
112 | |
|
|
113 | /* the c stack, if any */ |
|
|
114 | coro_stack *stack; |
|
|
115 | |
140 | |
116 | /* the stacks and related info (callchain etc..) */ |
141 | /* the stacks and related info (callchain etc..) */ |
117 | PERL_SI *curstackinfo; |
142 | PERL_SI *curstackinfo; |
118 | AV *curstack; |
143 | AV *curstack; |
119 | AV *mainstack; |
144 | AV *mainstack; |
… | |
… | |
140 | OP **retstack; |
165 | OP **retstack; |
141 | I32 retstack_ix; |
166 | I32 retstack_ix; |
142 | I32 retstack_max; |
167 | I32 retstack_max; |
143 | PMOP *curpm; |
168 | PMOP *curpm; |
144 | COP *curcop; |
169 | COP *curcop; |
145 | JMPENV *top_env; |
|
|
146 | |
170 | |
147 | /* coro process data */ |
171 | /* coro process data */ |
148 | int prio; |
172 | int prio; |
149 | |
|
|
150 | /* data associated with this coroutine (initial args) */ |
|
|
151 | AV *args; |
|
|
152 | int refcnt; |
|
|
153 | }; |
173 | }; |
154 | |
174 | |
155 | typedef struct coro *Coro__State; |
175 | typedef struct coro *Coro__State; |
156 | typedef struct coro *Coro__State_or_hashref; |
176 | typedef struct coro *Coro__State_or_hashref; |
157 | |
177 | |
158 | static AV * |
178 | static AV * |
159 | coro_clone_padlist (pTHX_ CV *cv) |
179 | coro_clone_padlist (CV *cv) |
160 | { |
180 | { |
161 | AV *padlist = CvPADLIST (cv); |
181 | AV *padlist = CvPADLIST (cv); |
162 | AV *newpadlist, *newpad; |
182 | AV *newpadlist, *newpad; |
163 | |
183 | |
164 | newpadlist = newAV (); |
184 | newpadlist = newAV (); |
165 | AvREAL_off (newpadlist); |
185 | AvREAL_off (newpadlist); |
166 | #if PERL_VERSION < 9 |
186 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
187 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
188 | #else |
167 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
189 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
168 | #else |
|
|
169 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
170 | #endif |
190 | #endif |
171 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
191 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
172 | --AvFILLp (padlist); |
192 | --AvFILLp (padlist); |
173 | |
193 | |
174 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
194 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
… | |
… | |
176 | |
196 | |
177 | return newpadlist; |
197 | return newpadlist; |
178 | } |
198 | } |
179 | |
199 | |
180 | static void |
200 | static void |
181 | free_padlist (pTHX_ AV *padlist) |
201 | free_padlist (AV *padlist) |
182 | { |
202 | { |
183 | /* may be during global destruction */ |
203 | /* may be during global destruction */ |
184 | if (SvREFCNT (padlist)) |
204 | if (SvREFCNT (padlist)) |
185 | { |
205 | { |
186 | I32 i = AvFILLp (padlist); |
206 | I32 i = AvFILLp (padlist); |
… | |
… | |
207 | AV *padlist; |
227 | AV *padlist; |
208 | AV *av = (AV *)mg->mg_obj; |
228 | AV *av = (AV *)mg->mg_obj; |
209 | |
229 | |
210 | /* casting is fun. */ |
230 | /* casting is fun. */ |
211 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
231 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
212 | free_padlist (aTHX_ padlist); |
232 | free_padlist (padlist); |
213 | |
233 | |
214 | SvREFCNT_dec (av); |
234 | SvREFCNT_dec (av); |
215 | |
235 | |
216 | return 0; |
236 | return 0; |
217 | } |
237 | } |
… | |
… | |
220 | |
240 | |
221 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
241 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
222 | |
242 | |
223 | /* the next two functions merely cache the padlists */ |
243 | /* the next two functions merely cache the padlists */ |
224 | static void |
244 | static void |
225 | get_padlist (pTHX_ CV *cv) |
245 | get_padlist (CV *cv) |
226 | { |
246 | { |
227 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
247 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
228 | |
248 | |
229 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
249 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
230 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
250 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
231 | else |
251 | else |
232 | { |
252 | { |
233 | #if 0 |
253 | #if 0 |
234 | /* this should work - but it doesn't :( */ |
254 | /* this is probably cleaner, but also slower? */ |
235 | CV *cp = Perl_cv_clone (aTHX_ cv); |
255 | CV *cp = Perl_cv_clone (cv); |
236 | CvPADLIST (cv) = CvPADLIST (cp); |
256 | CvPADLIST (cv) = CvPADLIST (cp); |
237 | CvPADLIST (cp) = 0; |
257 | CvPADLIST (cp) = 0; |
238 | SvREFCNT_dec (cp); |
258 | SvREFCNT_dec (cp); |
239 | #else |
259 | #else |
240 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
260 | CvPADLIST (cv) = coro_clone_padlist (cv); |
241 | #endif |
261 | #endif |
242 | } |
262 | } |
243 | } |
263 | } |
244 | |
264 | |
245 | static void |
265 | static void |
246 | put_padlist (pTHX_ CV *cv) |
266 | put_padlist (CV *cv) |
247 | { |
267 | { |
248 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
268 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
249 | |
269 | |
250 | if (!mg) |
270 | if (!mg) |
251 | { |
271 | { |
… | |
… | |
259 | } |
279 | } |
260 | |
280 | |
261 | #define SB do { |
281 | #define SB do { |
262 | #define SE } while (0) |
282 | #define SE } while (0) |
263 | |
283 | |
264 | #define LOAD(state) load_state(aTHX_ (state)); |
284 | #define LOAD(state) load_state((state)); |
265 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
285 | #define SAVE(state,flags) save_state((state),(flags)); |
266 | |
286 | |
267 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
287 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
268 | |
288 | |
269 | static void |
289 | static void |
270 | load_state(pTHX_ Coro__State c) |
290 | load_state(Coro__State c) |
271 | { |
291 | { |
272 | PL_dowarn = c->dowarn; |
292 | PL_dowarn = c->dowarn; |
273 | PL_in_eval = c->in_eval; |
293 | PL_in_eval = c->in_eval; |
274 | |
294 | |
275 | PL_curstackinfo = c->curstackinfo; |
295 | PL_curstackinfo = c->curstackinfo; |
… | |
… | |
293 | PL_scopestack_ix = c->scopestack_ix; |
313 | PL_scopestack_ix = c->scopestack_ix; |
294 | PL_scopestack_max = c->scopestack_max; |
314 | PL_scopestack_max = c->scopestack_max; |
295 | PL_savestack = c->savestack; |
315 | PL_savestack = c->savestack; |
296 | PL_savestack_ix = c->savestack_ix; |
316 | PL_savestack_ix = c->savestack_ix; |
297 | PL_savestack_max = c->savestack_max; |
317 | PL_savestack_max = c->savestack_max; |
298 | #if PERL_VERSION < 9 |
318 | #if !PERL_VERSION_ATLEAST (5,9,0) |
299 | PL_retstack = c->retstack; |
319 | PL_retstack = c->retstack; |
300 | PL_retstack_ix = c->retstack_ix; |
320 | PL_retstack_ix = c->retstack_ix; |
301 | PL_retstack_max = c->retstack_max; |
321 | PL_retstack_max = c->retstack_max; |
302 | #endif |
322 | #endif |
303 | PL_curpm = c->curpm; |
323 | PL_curpm = c->curpm; |
304 | PL_curcop = c->curcop; |
324 | PL_curcop = c->curcop; |
305 | PL_top_env = c->top_env; |
|
|
306 | |
325 | |
307 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
326 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
308 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
327 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
309 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
328 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
310 | |
329 | |
… | |
… | |
317 | { |
336 | { |
318 | AV *padlist = (AV *)POPs; |
337 | AV *padlist = (AV *)POPs; |
319 | |
338 | |
320 | if (padlist) |
339 | if (padlist) |
321 | { |
340 | { |
322 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
341 | put_padlist (cv); /* mark this padlist as available */ |
323 | CvPADLIST(cv) = padlist; |
342 | CvPADLIST(cv) = padlist; |
324 | } |
343 | } |
325 | |
344 | |
326 | ++CvDEPTH(cv); |
345 | ++CvDEPTH(cv); |
327 | } |
346 | } |
… | |
… | |
329 | PUTBACK; |
348 | PUTBACK; |
330 | } |
349 | } |
331 | } |
350 | } |
332 | |
351 | |
333 | static void |
352 | static void |
334 | save_state(pTHX_ Coro__State c, int flags) |
353 | save_state(Coro__State c, int flags) |
335 | { |
354 | { |
336 | { |
355 | { |
337 | dSP; |
356 | dSP; |
338 | I32 cxix = cxstack_ix; |
357 | I32 cxix = cxstack_ix; |
339 | PERL_CONTEXT *ccstk = cxstack; |
358 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
367 | } |
386 | } |
368 | |
387 | |
369 | PUSHs ((SV *)CvPADLIST(cv)); |
388 | PUSHs ((SV *)CvPADLIST(cv)); |
370 | PUSHs ((SV *)cv); |
389 | PUSHs ((SV *)cv); |
371 | |
390 | |
372 | get_padlist (aTHX_ cv); |
391 | get_padlist (cv); |
373 | } |
392 | } |
374 | } |
393 | } |
375 | #ifdef CXt_FORMAT |
394 | #ifdef CXt_FORMAT |
376 | else if (CxTYPE(cx) == CXt_FORMAT) |
395 | else if (CxTYPE(cx) == CXt_FORMAT) |
377 | { |
396 | { |
… | |
… | |
421 | c->scopestack_ix = PL_scopestack_ix; |
440 | c->scopestack_ix = PL_scopestack_ix; |
422 | c->scopestack_max = PL_scopestack_max; |
441 | c->scopestack_max = PL_scopestack_max; |
423 | c->savestack = PL_savestack; |
442 | c->savestack = PL_savestack; |
424 | c->savestack_ix = PL_savestack_ix; |
443 | c->savestack_ix = PL_savestack_ix; |
425 | c->savestack_max = PL_savestack_max; |
444 | c->savestack_max = PL_savestack_max; |
426 | #if PERL_VERSION < 9 |
445 | #if !PERL_VERSION_ATLEAST (5,9,0) |
427 | c->retstack = PL_retstack; |
446 | c->retstack = PL_retstack; |
428 | c->retstack_ix = PL_retstack_ix; |
447 | c->retstack_ix = PL_retstack_ix; |
429 | c->retstack_max = PL_retstack_max; |
448 | c->retstack_max = PL_retstack_max; |
430 | #endif |
449 | #endif |
431 | c->curpm = PL_curpm; |
450 | c->curpm = PL_curpm; |
432 | c->curcop = PL_curcop; |
451 | c->curcop = PL_curcop; |
433 | c->top_env = PL_top_env; |
|
|
434 | } |
452 | } |
435 | |
453 | |
436 | /* |
454 | /* |
437 | * allocate various perl stacks. This is an exact copy |
455 | * allocate various perl stacks. This is an exact copy |
438 | * of perl.c:init_stacks, except that it uses less memory |
456 | * of perl.c:init_stacks, except that it uses less memory |
439 | * on the (sometimes correct) assumption that coroutines do |
457 | * on the (sometimes correct) assumption that coroutines do |
440 | * not usually need a lot of stackspace. |
458 | * not usually need a lot of stackspace. |
441 | */ |
459 | */ |
442 | static void |
460 | static void |
443 | coro_init_stacks (pTHX) |
461 | coro_init_stacks () |
444 | { |
462 | { |
445 | LOCK; |
|
|
446 | |
|
|
447 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
463 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
448 | PL_curstackinfo->si_type = PERLSI_MAIN; |
464 | PL_curstackinfo->si_type = PERLSI_MAIN; |
449 | PL_curstack = PL_curstackinfo->si_stack; |
465 | PL_curstack = PL_curstackinfo->si_stack; |
450 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
466 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
451 | |
467 | |
… | |
… | |
472 | |
488 | |
473 | New(54,PL_savestack,96,ANY); |
489 | New(54,PL_savestack,96,ANY); |
474 | PL_savestack_ix = 0; |
490 | PL_savestack_ix = 0; |
475 | PL_savestack_max = 96; |
491 | PL_savestack_max = 96; |
476 | |
492 | |
477 | #if PERL_VERSION < 9 |
493 | #if !PERL_VERSION_ATLEAST (5,9,0) |
478 | New(54,PL_retstack,8,OP*); |
494 | New(54,PL_retstack,8,OP*); |
479 | PL_retstack_ix = 0; |
495 | PL_retstack_ix = 0; |
480 | PL_retstack_max = 8; |
496 | PL_retstack_max = 8; |
481 | #endif |
497 | #endif |
482 | |
|
|
483 | UNLOCK; |
|
|
484 | } |
498 | } |
485 | |
499 | |
486 | /* |
500 | /* |
487 | * destroy the stacks, the callchain etc... |
501 | * destroy the stacks, the callchain etc... |
488 | */ |
502 | */ |
489 | static void |
503 | static void |
490 | destroy_stacks(pTHX) |
504 | coro_destroy_stacks() |
491 | { |
505 | { |
492 | if (!IN_DESTRUCT) |
506 | if (!IN_DESTRUCT) |
493 | { |
507 | { |
494 | /* is this ugly, I ask? */ |
508 | /* is this ugly, I ask? */ |
495 | LEAVE_SCOPE (0); |
509 | LEAVE_SCOPE (0); |
… | |
… | |
526 | |
540 | |
527 | Safefree (PL_tmps_stack); |
541 | Safefree (PL_tmps_stack); |
528 | Safefree (PL_markstack); |
542 | Safefree (PL_markstack); |
529 | Safefree (PL_scopestack); |
543 | Safefree (PL_scopestack); |
530 | Safefree (PL_savestack); |
544 | Safefree (PL_savestack); |
531 | #if PERL_VERSION < 9 |
545 | #if !PERL_VERSION_ATLEAST (5,9,0) |
532 | Safefree (PL_retstack); |
546 | Safefree (PL_retstack); |
533 | #endif |
547 | #endif |
534 | } |
548 | } |
535 | |
549 | |
536 | static void |
550 | static void |
537 | setup_coro (struct coro *coro) |
551 | setup_coro (struct coro *coro) |
538 | { |
552 | { |
539 | /* |
553 | /* |
540 | * emulate part of the perl startup here. |
554 | * emulate part of the perl startup here. |
541 | */ |
555 | */ |
542 | dTHX; |
|
|
543 | dSP; |
|
|
544 | UNOP myop; |
|
|
545 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
|
|
546 | |
556 | |
547 | coro_init_stacks (aTHX); |
557 | coro_init_stacks (); |
|
|
558 | |
548 | /*PL_curcop = 0;*/ |
559 | PL_curcop = 0; |
549 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
560 | PL_in_eval = 0; |
|
|
561 | PL_curpm = 0; |
|
|
562 | |
|
|
563 | { |
|
|
564 | dSP; |
|
|
565 | LOGOP myop; |
|
|
566 | |
|
|
567 | /* I have no idea why this is needed, but it is */ |
|
|
568 | PUSHMARK (SP); |
|
|
569 | |
550 | SvREFCNT_dec (GvAV (PL_defgv)); |
570 | SvREFCNT_dec (GvAV (PL_defgv)); |
551 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
571 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
552 | |
572 | |
553 | SPAGAIN; |
|
|
554 | |
|
|
555 | Zero (&myop, 1, UNOP); |
573 | Zero (&myop, 1, LOGOP); |
556 | myop.op_next = Nullop; |
574 | myop.op_next = Nullop; |
557 | myop.op_flags = OPf_WANT_VOID; |
575 | myop.op_flags = OPf_WANT_VOID; |
558 | |
576 | |
559 | PL_op = (OP *)&myop; |
577 | PL_op = (OP *)&myop; |
560 | |
578 | |
561 | PUSHMARK(SP); |
579 | PUSHMARK (SP); |
562 | XPUSHs (sub_init); |
580 | XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE)); |
563 | PUTBACK; |
581 | PUTBACK; |
564 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
582 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
565 | SPAGAIN; |
583 | SPAGAIN; |
566 | |
584 | |
567 | ENTER; /* necessary e.g. for dounwind */ |
585 | ENTER; /* necessary e.g. for dounwind */ |
|
|
586 | } |
568 | } |
587 | } |
569 | |
588 | |
570 | static void |
589 | static void |
571 | transfer_tail () |
590 | free_coro_mortal () |
572 | { |
591 | { |
573 | if (coro_mortal) |
592 | if (coro_mortal) |
574 | { |
593 | { |
575 | SvREFCNT_dec (coro_mortal); |
594 | SvREFCNT_dec (coro_mortal); |
576 | coro_mortal = 0; |
595 | coro_mortal = 0; |
577 | } |
596 | } |
|
|
597 | } |
578 | |
598 | |
|
|
599 | static void NOINLINE |
|
|
600 | prepare_cctx (coro_stack *cctx) |
|
|
601 | { |
|
|
602 | dSP; |
|
|
603 | LOGOP myop; |
|
|
604 | |
|
|
605 | Zero (&myop, 1, LOGOP); |
|
|
606 | myop.op_next = PL_op; |
|
|
607 | myop.op_flags = OPf_WANT_VOID; |
|
|
608 | |
|
|
609 | sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx)); |
|
|
610 | |
|
|
611 | PUSHMARK (SP); |
|
|
612 | XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); |
|
|
613 | PUTBACK; |
|
|
614 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
615 | SPAGAIN; |
|
|
616 | } |
|
|
617 | |
|
|
618 | static void |
|
|
619 | coro_run (void *arg) |
|
|
620 | { |
|
|
621 | /* coro_run is the alternative epilogue of transfer() */ |
579 | UNLOCK; |
622 | UNLOCK; |
580 | } |
|
|
581 | |
623 | |
582 | static void |
|
|
583 | coro_run (void *arg) |
|
|
584 | { |
|
|
585 | /* |
624 | /* |
586 | * this is a _very_ stripped down perl interpreter ;) |
625 | * this is a _very_ stripped down perl interpreter ;) |
587 | */ |
626 | */ |
588 | dTHX; |
|
|
589 | |
|
|
590 | transfer_tail (); |
|
|
591 | |
|
|
592 | PL_top_env = &PL_start_env; |
627 | PL_top_env = &PL_start_env; |
593 | PL_restartop = PL_op->op_next; |
628 | /* inject call to cctx_init */ |
|
|
629 | prepare_cctx ((coro_stack *)arg); |
|
|
630 | |
594 | /* somebody will hit me for both perl_run and PL_restart_top */ |
631 | /* somebody will hit me for both perl_run and PL_restartop */ |
595 | perl_run (aTHX_ PERL_GET_CONTEXT); |
632 | perl_run (PL_curinterp); |
596 | |
633 | |
|
|
634 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); |
597 | abort (); |
635 | abort (); |
598 | } |
636 | } |
599 | |
637 | |
600 | static coro_stack * |
638 | static coro_stack * |
601 | stack_new () |
639 | stack_new () |
… | |
… | |
604 | |
642 | |
605 | New (0, stack, 1, coro_stack); |
643 | New (0, stack, 1, coro_stack); |
606 | |
644 | |
607 | #if HAVE_MMAP |
645 | #if HAVE_MMAP |
608 | |
646 | |
609 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ |
647 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
|
|
648 | /* mmap suppsedly does allocate-on-write for us */ |
610 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
649 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
611 | |
650 | |
612 | if (stack->sptr == (void *)-1) |
651 | if (stack->sptr == (void *)-1) |
613 | { |
652 | { |
614 | fprintf (stderr, "FATAL: unable to mmap stack for coroutine\n"); |
653 | perror ("FATAL: unable to mmap stack for coroutine"); |
615 | _exit (EXIT_FAILURE); |
654 | _exit (EXIT_FAILURE); |
616 | } |
655 | } |
617 | |
656 | |
|
|
657 | # if STACKGUARD |
618 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
658 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
|
|
659 | # endif |
619 | |
660 | |
620 | #else |
661 | #else |
621 | |
662 | |
622 | stack->ssize = STACKSIZE * (long)sizeof (long); |
663 | stack->ssize = STACKSIZE * (long)sizeof (long); |
623 | New (0, stack->sptr, STACKSIZE, long); |
664 | New (0, stack->sptr, STACKSIZE, long); |
624 | |
665 | |
625 | if (!stack->sptr) |
666 | if (!stack->sptr) |
626 | { |
667 | { |
627 | fprintf (stderr, "FATAL: unable to malloc stack for coroutine\n"); |
668 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
628 | _exit (EXIT_FAILURE); |
669 | _exit (EXIT_FAILURE); |
629 | } |
670 | } |
630 | |
671 | |
631 | #endif |
672 | #endif |
632 | |
673 | |
|
|
674 | #if USE_VALGRIND |
|
|
675 | stack->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
676 | STACKGUARD * PAGESIZE + (char *)stack->sptr, |
|
|
677 | stack->ssize + (char *)stack->sptr |
|
|
678 | ); |
|
|
679 | #endif |
|
|
680 | |
633 | coro_create (&stack->cctx, coro_run, 0, stack->sptr, stack->ssize); |
681 | coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); |
634 | |
682 | |
635 | return stack; |
683 | return stack; |
636 | } |
684 | } |
637 | |
685 | |
638 | static void |
686 | static void |
639 | stack_free (coro_stack *stack) |
687 | stack_free (coro_stack *stack) |
640 | { |
688 | { |
641 | if (!stack || stack == main_stack) |
689 | if (!stack) |
642 | return; |
690 | return; |
|
|
691 | |
|
|
692 | #if USE_VALGRIND |
|
|
693 | VALGRIND_STACK_DEREGISTER (stack->valgrind_id); |
|
|
694 | #endif |
643 | |
695 | |
644 | #if HAVE_MMAP |
696 | #if HAVE_MMAP |
645 | munmap (stack->sptr, stack->ssize); |
697 | munmap (stack->sptr, stack->ssize); |
646 | #else |
698 | #else |
647 | Safefree (stack->sptr); |
699 | Safefree (stack->sptr); |
… | |
… | |
649 | |
701 | |
650 | Safefree (stack); |
702 | Safefree (stack); |
651 | } |
703 | } |
652 | |
704 | |
653 | static coro_stack *stack_first; |
705 | static coro_stack *stack_first; |
|
|
706 | static int cctx_count, cctx_idle; |
654 | |
707 | |
655 | static void |
708 | static coro_stack * |
656 | stack_get (struct coro *coro) |
709 | stack_get () |
657 | { |
710 | { |
|
|
711 | coro_stack *stack; |
|
|
712 | |
658 | if (stack_first) |
713 | if (stack_first) |
659 | { |
714 | { |
|
|
715 | --cctx_idle; |
660 | coro->stack = stack_first; |
716 | stack = stack_first; |
661 | stack_first = stack_first->next; |
717 | stack_first = stack->next; |
662 | } |
718 | } |
663 | else |
719 | else |
|
|
720 | { |
|
|
721 | ++cctx_count; |
664 | coro->stack = stack_new (); |
722 | stack = stack_new (); |
|
|
723 | PL_op = PL_op->op_next; |
|
|
724 | } |
|
|
725 | |
|
|
726 | return stack; |
665 | } |
727 | } |
666 | |
728 | |
667 | static void |
729 | static void |
668 | stack_put (coro_stack *stack) |
730 | stack_put (coro_stack *stack) |
669 | { |
731 | { |
|
|
732 | ++cctx_idle; |
670 | stack->next = stack_first; |
733 | stack->next = stack_first; |
671 | stack_first = stack; |
734 | stack_first = stack; |
672 | } |
735 | } |
673 | |
736 | |
674 | /* never call directly, always through the coro_state_transfer global variable */ |
737 | /* never call directly, always through the coro_state_transfer global variable */ |
675 | static void |
738 | static void NOINLINE |
676 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
739 | transfer (struct coro *prev, struct coro *next, int flags) |
677 | { |
740 | { |
678 | dSTACKLEVEL; |
741 | dSTACKLEVEL; |
679 | |
742 | |
680 | /* sometimes transfer is only called to set idle_sp */ |
743 | /* sometimes transfer is only called to set idle_sp */ |
681 | if (!prev->stack->idle_sp) |
744 | if (flags == TRANSFER_SET_STACKLEVEL) |
682 | prev->stack->idle_sp = stacklevel; |
745 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
683 | |
|
|
684 | LOCK; |
|
|
685 | |
|
|
686 | if (prev != next) |
746 | else if (prev != next) |
687 | { |
747 | { |
688 | coro_stack *prev_stack = prev->stack; |
748 | coro_stack *prev__stack; |
689 | |
749 | |
690 | /* possibly "free" the stack */ |
750 | LOCK; |
691 | if (prev_stack->idle_sp == stacklevel) |
|
|
692 | { |
|
|
693 | stack_put (prev_stack); |
|
|
694 | prev->stack = 0; |
|
|
695 | } |
|
|
696 | |
751 | |
697 | if (next->mainstack) |
752 | if (next->mainstack) |
698 | { |
753 | { |
699 | /* coroutine already started */ |
754 | /* coroutine already started */ |
700 | SAVE (prev, flags); |
755 | SAVE (prev, flags); |
… | |
… | |
705 | /* need to start coroutine */ |
760 | /* need to start coroutine */ |
706 | /* first get rid of the old state */ |
761 | /* first get rid of the old state */ |
707 | SAVE (prev, -1); |
762 | SAVE (prev, -1); |
708 | /* setup coroutine call */ |
763 | /* setup coroutine call */ |
709 | setup_coro (next); |
764 | setup_coro (next); |
710 | /* need to change stack from main_stack to real one */ |
765 | /* need a stack */ |
711 | next->stack = 0; |
766 | next->stack = 0; |
712 | } |
767 | } |
713 | |
768 | |
|
|
769 | if (!prev->stack) |
|
|
770 | /* create a new empty context */ |
|
|
771 | Newz (0, prev->stack, 1, coro_stack); |
|
|
772 | |
|
|
773 | prev__stack = prev->stack; |
|
|
774 | |
|
|
775 | /* possibly "free" the stack */ |
|
|
776 | if (prev__stack->idle_sp == STACKLEVEL) |
|
|
777 | { |
|
|
778 | stack_put (prev__stack); |
|
|
779 | prev->stack = 0; |
|
|
780 | } |
|
|
781 | |
714 | if (!next->stack) |
782 | if (!next->stack) |
715 | stack_get (next); |
783 | next->stack = stack_get (); |
716 | |
784 | |
717 | if (prev_stack != next->stack) |
785 | if (prev__stack != next->stack) |
|
|
786 | { |
|
|
787 | prev__stack->top_env = PL_top_env; |
|
|
788 | PL_top_env = next->stack->top_env; |
718 | coro_transfer (&prev_stack->cctx, &next->stack->cctx); |
789 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
|
|
790 | } |
|
|
791 | |
|
|
792 | free_coro_mortal (); |
|
|
793 | |
|
|
794 | UNLOCK; |
719 | } |
795 | } |
720 | |
|
|
721 | transfer_tail (); |
|
|
722 | } |
796 | } |
723 | |
797 | |
724 | /* use this function pointer to call the above function */ |
798 | struct transfer_args |
725 | /* this is done to increase chances of the compiler not inlining the call */ |
799 | { |
726 | void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; |
800 | struct coro *prev, *next; |
|
|
801 | int flags; |
|
|
802 | }; |
|
|
803 | |
|
|
804 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) |
727 | |
805 | |
728 | static void |
806 | static void |
729 | coro_state_destroy (struct coro *coro) |
807 | coro_state_destroy (struct coro *coro) |
730 | { |
808 | { |
731 | if (coro->refcnt--) |
809 | if (coro->refcnt--) |
… | |
… | |
733 | |
811 | |
734 | if (coro->mainstack && coro->mainstack != main_mainstack) |
812 | if (coro->mainstack && coro->mainstack != main_mainstack) |
735 | { |
813 | { |
736 | struct coro temp; |
814 | struct coro temp; |
737 | |
815 | |
738 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
816 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
739 | LOAD (aTHX_ coro); |
817 | LOAD (coro); |
740 | |
818 | |
741 | destroy_stacks (aTHX); |
819 | coro_destroy_stacks (); |
742 | |
820 | |
743 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
821 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
744 | |
822 | |
745 | coro->mainstack = 0; |
823 | coro->mainstack = 0; |
746 | } |
824 | } |
… | |
… | |
749 | SvREFCNT_dec (coro->args); |
827 | SvREFCNT_dec (coro->args); |
750 | Safefree (coro); |
828 | Safefree (coro); |
751 | } |
829 | } |
752 | |
830 | |
753 | static int |
831 | static int |
754 | coro_state_clear (SV *sv, MAGIC *mg) |
832 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
755 | { |
833 | { |
756 | struct coro *coro = (struct coro *)mg->mg_ptr; |
834 | struct coro *coro = (struct coro *)mg->mg_ptr; |
757 | mg->mg_ptr = 0; |
835 | mg->mg_ptr = 0; |
758 | |
836 | |
759 | coro_state_destroy (coro); |
837 | coro_state_destroy (coro); |
760 | |
838 | |
761 | return 0; |
839 | return 0; |
762 | } |
840 | } |
763 | |
841 | |
764 | static int |
842 | static int |
765 | coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) |
843 | coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) |
766 | { |
844 | { |
767 | struct coro *coro = (struct coro *)mg->mg_ptr; |
845 | struct coro *coro = (struct coro *)mg->mg_ptr; |
768 | |
846 | |
769 | ++coro->refcnt; |
847 | ++coro->refcnt; |
770 | |
848 | |
771 | return 0; |
849 | return 0; |
772 | } |
850 | } |
773 | |
851 | |
774 | static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; |
852 | static MGVTBL coro_state_vtbl = { |
|
|
853 | 0, 0, 0, 0, |
|
|
854 | coro_state_clear, |
|
|
855 | 0, |
|
|
856 | #ifdef MGf_DUP |
|
|
857 | coro_state_dup, |
|
|
858 | #else |
|
|
859 | # define MGf_DUP 0 |
|
|
860 | #endif |
|
|
861 | }; |
775 | |
862 | |
776 | static struct coro * |
863 | static struct coro * |
777 | SvSTATE (SV *coro) |
864 | SvSTATE (SV *coro) |
778 | { |
865 | { |
779 | HV *stash; |
866 | HV *stash; |
… | |
… | |
794 | assert (mg->mg_type == PERL_MAGIC_ext); |
881 | assert (mg->mg_type == PERL_MAGIC_ext); |
795 | return (struct coro *)mg->mg_ptr; |
882 | return (struct coro *)mg->mg_ptr; |
796 | } |
883 | } |
797 | |
884 | |
798 | static void |
885 | static void |
|
|
886 | prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) |
|
|
887 | { |
|
|
888 | ta->prev = SvSTATE (prev); |
|
|
889 | ta->next = SvSTATE (next); |
|
|
890 | ta->flags = flags; |
|
|
891 | } |
|
|
892 | |
|
|
893 | static void |
799 | api_transfer (pTHX_ SV *prev, SV *next, int flags) |
894 | api_transfer (SV *prev, SV *next, int flags) |
800 | { |
895 | { |
801 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
896 | dTHX; |
|
|
897 | struct transfer_args ta; |
|
|
898 | |
|
|
899 | prepare_transfer (&ta, prev, next, flags); |
|
|
900 | TRANSFER (ta); |
802 | } |
901 | } |
803 | |
902 | |
804 | /** Coro ********************************************************************/ |
903 | /** Coro ********************************************************************/ |
805 | |
904 | |
806 | #define PRIO_MAX 3 |
905 | #define PRIO_MAX 3 |
… | |
… | |
814 | static GV *coro_current, *coro_idle; |
913 | static GV *coro_current, *coro_idle; |
815 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
914 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
816 | static int coro_nready; |
915 | static int coro_nready; |
817 | |
916 | |
818 | static void |
917 | static void |
819 | coro_enq (pTHX_ SV *sv) |
918 | coro_enq (SV *sv) |
820 | { |
919 | { |
821 | int prio; |
920 | int prio; |
822 | |
921 | |
823 | if (SvTYPE (sv) != SVt_PVHV) |
922 | if (SvTYPE (sv) != SVt_PVHV) |
824 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
923 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
… | |
… | |
828 | av_push (coro_ready [prio - PRIO_MIN], sv); |
927 | av_push (coro_ready [prio - PRIO_MIN], sv); |
829 | coro_nready++; |
928 | coro_nready++; |
830 | } |
929 | } |
831 | |
930 | |
832 | static SV * |
931 | static SV * |
833 | coro_deq (pTHX_ int min_prio) |
932 | coro_deq (int min_prio) |
834 | { |
933 | { |
835 | int prio = PRIO_MAX - PRIO_MIN; |
934 | int prio = PRIO_MAX - PRIO_MIN; |
836 | |
935 | |
837 | min_prio -= PRIO_MIN; |
936 | min_prio -= PRIO_MIN; |
838 | if (min_prio < 0) |
937 | if (min_prio < 0) |
… | |
… | |
855 | |
954 | |
856 | if (SvROK (coro)) |
955 | if (SvROK (coro)) |
857 | coro = SvRV (coro); |
956 | coro = SvRV (coro); |
858 | |
957 | |
859 | LOCK; |
958 | LOCK; |
860 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
959 | coro_enq (SvREFCNT_inc (coro)); |
861 | UNLOCK; |
960 | UNLOCK; |
862 | } |
961 | } |
863 | |
962 | |
864 | static void |
963 | static void |
865 | api_schedule (void) |
964 | prepare_schedule (struct transfer_args *ta) |
866 | { |
965 | { |
867 | dTHX; |
|
|
868 | |
|
|
869 | SV *prev, *next; |
966 | SV *current, *prev, *next; |
|
|
967 | |
870 | SV *current = GvSV (coro_current); |
968 | current = GvSV (coro_current); |
871 | |
969 | |
872 | for (;;) |
970 | for (;;) |
873 | { |
971 | { |
874 | LOCK; |
972 | LOCK; |
875 | |
|
|
876 | next = coro_deq (aTHX_ PRIO_MIN); |
973 | next = coro_deq (PRIO_MIN); |
|
|
974 | UNLOCK; |
877 | |
975 | |
878 | if (next) |
976 | if (next) |
879 | break; |
977 | break; |
880 | |
|
|
881 | UNLOCK; |
|
|
882 | |
978 | |
883 | { |
979 | { |
884 | dSP; |
980 | dSP; |
885 | |
981 | |
886 | ENTER; |
982 | ENTER; |
… | |
… | |
897 | |
993 | |
898 | prev = SvRV (current); |
994 | prev = SvRV (current); |
899 | SvRV (current) = next; |
995 | SvRV (current) = next; |
900 | |
996 | |
901 | /* free this only after the transfer */ |
997 | /* free this only after the transfer */ |
|
|
998 | LOCK; |
|
|
999 | free_coro_mortal (); |
|
|
1000 | UNLOCK; |
902 | coro_mortal = prev; |
1001 | coro_mortal = prev; |
903 | |
1002 | |
|
|
1003 | ta->prev = SvSTATE (prev); |
|
|
1004 | ta->next = SvSTATE (next); |
|
|
1005 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
1006 | } |
|
|
1007 | |
|
|
1008 | static void |
|
|
1009 | prepare_cede (struct transfer_args *ta) |
|
|
1010 | { |
|
|
1011 | LOCK; |
|
|
1012 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
904 | UNLOCK; |
1013 | UNLOCK; |
905 | |
1014 | |
906 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), TRANSFER_SAVE_ALL); |
1015 | prepare_schedule (ta); |
907 | } |
1016 | } |
908 | |
1017 | |
909 | static int coro_cede_self; |
1018 | static void |
|
|
1019 | api_schedule (void) |
|
|
1020 | { |
|
|
1021 | dTHX; |
|
|
1022 | struct transfer_args ta; |
|
|
1023 | |
|
|
1024 | prepare_schedule (&ta); |
|
|
1025 | TRANSFER (ta); |
|
|
1026 | } |
910 | |
1027 | |
911 | static void |
1028 | static void |
912 | api_cede (void) |
1029 | api_cede (void) |
913 | { |
1030 | { |
914 | dTHX; |
1031 | dTHX; |
915 | SV *current = SvREFCNT_inc (SvRV (GvSV (coro_current))); |
1032 | struct transfer_args ta; |
916 | |
1033 | |
917 | LOCK; |
1034 | prepare_cede (&ta); |
918 | |
1035 | TRANSFER (ta); |
919 | if (coro_cede_self) |
|
|
920 | { |
|
|
921 | AV *runqueue = coro_ready [PRIO_MAX - PRIO_MIN]; |
|
|
922 | av_unshift (runqueue, 1); |
|
|
923 | av_store (runqueue, 0, current); |
|
|
924 | coro_nready++; |
|
|
925 | coro_cede_self = 0; |
|
|
926 | } |
|
|
927 | else |
|
|
928 | coro_enq (aTHX_ current); |
|
|
929 | |
|
|
930 | UNLOCK; |
|
|
931 | |
|
|
932 | api_schedule (); |
|
|
933 | } |
1036 | } |
934 | |
1037 | |
935 | MODULE = Coro::State PACKAGE = Coro::State |
1038 | MODULE = Coro::State PACKAGE = Coro::State |
936 | |
1039 | |
937 | PROTOTYPES: DISABLE |
1040 | PROTOTYPES: DISABLE |
… | |
… | |
952 | main_mainstack = PL_mainstack; |
1055 | main_mainstack = PL_mainstack; |
953 | |
1056 | |
954 | coroapi.ver = CORO_API_VERSION; |
1057 | coroapi.ver = CORO_API_VERSION; |
955 | coroapi.transfer = api_transfer; |
1058 | coroapi.transfer = api_transfer; |
956 | |
1059 | |
957 | Newz (0, main_stack, 1, coro_stack); |
|
|
958 | main_stack->idle_sp = (void *)-1; |
|
|
959 | |
|
|
960 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1060 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
961 | } |
1061 | } |
962 | |
1062 | |
963 | SV * |
1063 | SV * |
964 | new (char *klass, ...) |
1064 | new (char *klass, ...) |
… | |
… | |
973 | |
1073 | |
974 | hv = newHV (); |
1074 | hv = newHV (); |
975 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1075 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
976 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1076 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
977 | |
1077 | |
978 | av_push (coro->args, newSVsv (RETVAL)); |
|
|
979 | for (i = 1; i < items; i++) |
1078 | for (i = 1; i < items; i++) |
980 | av_push (coro->args, newSVsv (ST (i))); |
1079 | av_push (coro->args, newSVsv (ST (i))); |
981 | |
1080 | |
982 | coro->stack = main_stack; |
|
|
983 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
1081 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
984 | /*coro->stack = 0;*/ |
1082 | /*coro->stack = 0;*/ |
985 | } |
1083 | } |
986 | OUTPUT: |
1084 | OUTPUT: |
987 | RETVAL |
1085 | RETVAL |
988 | |
1086 | |
989 | void |
1087 | void |
990 | transfer (prev, next, flags) |
1088 | _set_stacklevel (...) |
991 | SV *prev |
1089 | ALIAS: |
992 | SV *next |
1090 | Coro::State::transfer = 1 |
993 | int flags |
1091 | Coro::schedule = 2 |
|
|
1092 | Coro::cede = 3 |
|
|
1093 | Coro::Cont::yield = 4 |
994 | CODE: |
1094 | CODE: |
995 | PUTBACK; |
|
|
996 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
|
|
997 | SPAGAIN; |
|
|
998 | |
|
|
999 | int |
|
|
1000 | prio (Coro::State coro, int newprio = 0) |
|
|
1001 | ALIAS: |
|
|
1002 | nice = 1 |
|
|
1003 | CODE: |
|
|
1004 | { |
1095 | { |
1005 | RETVAL = coro->prio; |
1096 | struct transfer_args ta; |
1006 | |
1097 | |
1007 | if (items > 1) |
1098 | switch (ix) |
1008 | { |
1099 | { |
|
|
1100 | case 0: |
|
|
1101 | ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); |
|
|
1102 | ta.next = 0; |
|
|
1103 | ta.flags = TRANSFER_SET_STACKLEVEL; |
|
|
1104 | break; |
|
|
1105 | |
|
|
1106 | case 1: |
1009 | if (ix) |
1107 | if (items != 3) |
1010 | newprio += coro->prio; |
1108 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1011 | |
1109 | |
1012 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1110 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1013 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1111 | break; |
1014 | |
1112 | |
1015 | coro->prio = newprio; |
1113 | case 2: |
|
|
1114 | prepare_schedule (&ta); |
|
|
1115 | break; |
|
|
1116 | |
|
|
1117 | case 3: |
|
|
1118 | prepare_cede (&ta); |
|
|
1119 | break; |
|
|
1120 | |
|
|
1121 | case 4: |
|
|
1122 | { |
|
|
1123 | SV *yieldstack; |
|
|
1124 | SV *sv; |
|
|
1125 | AV *defav = GvAV (PL_defgv); |
|
|
1126 | |
|
|
1127 | yieldstack = *hv_fetch ( |
|
|
1128 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1129 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1130 | 0 |
|
|
1131 | ); |
|
|
1132 | |
|
|
1133 | /* set up @_ -- ugly */ |
|
|
1134 | av_clear (defav); |
|
|
1135 | av_fill (defav, items - 1); |
|
|
1136 | while (items--) |
|
|
1137 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1138 | |
|
|
1139 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1140 | ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1141 | ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1142 | ta.flags = 0; |
|
|
1143 | SvREFCNT_dec (sv); |
|
|
1144 | } |
|
|
1145 | break; |
|
|
1146 | |
1016 | } |
1147 | } |
1017 | } |
|
|
1018 | |
1148 | |
1019 | void |
1149 | TRANSFER (ta); |
1020 | _clear_idle_sp (Coro::State self) |
1150 | } |
1021 | CODE: |
|
|
1022 | self->stack->idle_sp = 0; |
|
|
1023 | |
1151 | |
1024 | void |
1152 | void |
1025 | _clone_state_from (SV *dst, SV *src) |
1153 | _clone_state_from (SV *dst, SV *src) |
1026 | CODE: |
1154 | CODE: |
1027 | { |
1155 | { |
… | |
… | |
1038 | int code |
1166 | int code |
1039 | PROTOTYPE: $ |
1167 | PROTOTYPE: $ |
1040 | CODE: |
1168 | CODE: |
1041 | _exit (code); |
1169 | _exit (code); |
1042 | |
1170 | |
1043 | MODULE = Coro::State PACKAGE = Coro::Cont |
|
|
1044 | |
|
|
1045 | void |
|
|
1046 | yield (...) |
|
|
1047 | PROTOTYPE: @ |
|
|
1048 | CODE: |
|
|
1049 | { |
|
|
1050 | SV *yieldstack; |
|
|
1051 | SV *sv; |
|
|
1052 | AV *defav = GvAV (PL_defgv); |
|
|
1053 | struct coro *prev, *next; |
|
|
1054 | |
|
|
1055 | yieldstack = *hv_fetch ( |
|
|
1056 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1057 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1058 | 0 |
|
|
1059 | ); |
|
|
1060 | |
|
|
1061 | /* set up @_ -- ugly */ |
|
|
1062 | av_clear (defav); |
|
|
1063 | av_fill (defav, items - 1); |
|
|
1064 | while (items--) |
|
|
1065 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1066 | |
|
|
1067 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1068 | prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1069 | next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1070 | SvREFCNT_dec (sv); |
|
|
1071 | |
|
|
1072 | coro_state_transfer (aTHX_ prev, next, 0); |
|
|
1073 | } |
|
|
1074 | |
|
|
1075 | MODULE = Coro::State PACKAGE = Coro |
1171 | MODULE = Coro::State PACKAGE = Coro |
1076 | |
1172 | |
1077 | BOOT: |
1173 | BOOT: |
1078 | { |
1174 | { |
1079 | int i; |
1175 | int i; |
… | |
… | |
1104 | |
1200 | |
1105 | GCoroAPI = &coroapi; |
1201 | GCoroAPI = &coroapi; |
1106 | sv_setiv (sv, (IV)&coroapi); |
1202 | sv_setiv (sv, (IV)&coroapi); |
1107 | SvREADONLY_on (sv); |
1203 | SvREADONLY_on (sv); |
1108 | } |
1204 | } |
|
|
1205 | } |
|
|
1206 | |
|
|
1207 | int |
|
|
1208 | prio (Coro::State coro, int newprio = 0) |
|
|
1209 | ALIAS: |
|
|
1210 | nice = 1 |
|
|
1211 | CODE: |
|
|
1212 | { |
|
|
1213 | RETVAL = coro->prio; |
|
|
1214 | |
|
|
1215 | if (items > 1) |
|
|
1216 | { |
|
|
1217 | if (ix) |
|
|
1218 | newprio += coro->prio; |
|
|
1219 | |
|
|
1220 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
|
|
1221 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
|
|
1222 | |
|
|
1223 | coro->prio = newprio; |
|
|
1224 | } |
1109 | } |
1225 | } |
1110 | |
1226 | |
1111 | void |
1227 | void |
1112 | ready (SV *self) |
1228 | ready (SV *self) |
1113 | PROTOTYPE: $ |
1229 | PROTOTYPE: $ |
… | |
… | |
1120 | CODE: |
1236 | CODE: |
1121 | RETVAL = coro_nready; |
1237 | RETVAL = coro_nready; |
1122 | OUTPUT: |
1238 | OUTPUT: |
1123 | RETVAL |
1239 | RETVAL |
1124 | |
1240 | |
1125 | void |
|
|
1126 | schedule (...) |
|
|
1127 | PROTOTYPE: |
|
|
1128 | CODE: |
|
|
1129 | api_schedule (); |
|
|
1130 | |
|
|
1131 | void |
|
|
1132 | _set_cede_self () |
|
|
1133 | CODE: |
|
|
1134 | coro_cede_self = 1; |
|
|
1135 | |
|
|
1136 | void |
|
|
1137 | cede (...) |
|
|
1138 | PROTOTYPE: |
|
|
1139 | CODE: |
|
|
1140 | api_cede (); |
|
|
1141 | |
|
|
1142 | MODULE = Coro::State PACKAGE = Coro::AIO |
1241 | MODULE = Coro::State PACKAGE = Coro::AIO |
1143 | |
1242 | |
1144 | SV * |
1243 | SV * |
1145 | _get_state () |
1244 | _get_state () |
1146 | CODE: |
1245 | CODE: |