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 |
… | |
… | |
54 | # ifndef PAGESIZE |
62 | # ifndef PAGESIZE |
55 | # define PAGESIZE pagesize |
63 | # define PAGESIZE pagesize |
56 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
64 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
57 | static long pagesize; |
65 | static long pagesize; |
58 | # else |
66 | # else |
59 | # define BOOT_PAGESIZE |
67 | # define BOOT_PAGESIZE (void)0 |
60 | # endif |
68 | # endif |
|
|
69 | #else |
|
|
70 | # define PAGESIZE 0 |
|
|
71 | # define BOOT_PAGESIZE (void)0 |
61 | #endif |
72 | #endif |
62 | |
73 | |
63 | /* The next macro should declare a variable stacklevel that contains and approximation |
74 | /* The next macro should declare a variable stacklevel that contains and approximation |
64 | * to the current C stack pointer. Its property is that it changes with each call |
75 | * to the current C stack pointer. Its property is that it changes with each call |
65 | * and should be unique. */ |
76 | * and should be unique. */ |
66 | #define dSTACKLEVEL int stacklevel |
77 | #define dSTACKLEVEL int stacklevel |
67 | #define STACKLEVEL ((void *)&stacklevel) |
78 | #define STACKLEVEL ((void *)&stacklevel) |
68 | |
79 | |
69 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
80 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
|
|
81 | |
|
|
82 | #if __GNUC__ >= 3 |
|
|
83 | # define attribute(x) __attribute__(x) |
|
|
84 | #else |
|
|
85 | # define attribute(x) |
|
|
86 | #endif |
|
|
87 | |
|
|
88 | #define NOINLINE attribute ((noinline)) |
70 | |
89 | |
71 | #include "CoroAPI.h" |
90 | #include "CoroAPI.h" |
72 | |
91 | |
73 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
92 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
74 | |
93 | |
… | |
… | |
84 | static struct CoroAPI coroapi; |
103 | static struct CoroAPI coroapi; |
85 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
104 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
86 | static HV *coro_state_stash, *coro_stash; |
105 | static HV *coro_state_stash, *coro_stash; |
87 | static SV *coro_mortal; /* will be freed after next transfer */ |
106 | static SV *coro_mortal; /* will be freed after next transfer */ |
88 | |
107 | |
|
|
108 | static struct coro_cctx *cctx_first; |
|
|
109 | static int cctx_count, cctx_idle; |
|
|
110 | |
89 | /* this is a structure representing a c-level coroutine */ |
111 | /* this is a structure representing a c-level coroutine */ |
90 | typedef struct coro_stack { |
112 | typedef struct coro_cctx { |
91 | struct coro_stack *next; |
113 | struct coro_cctx *next; |
92 | |
114 | |
93 | /* the stack */ |
115 | /* the stack */ |
94 | void *sptr; |
116 | void *sptr; |
95 | long ssize; /* positive == mmap, otherwise malloc */ |
117 | long ssize; /* positive == mmap, otherwise malloc */ |
96 | |
118 | |
97 | /* cpu state */ |
119 | /* cpu state */ |
98 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
120 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
99 | JMPENV *top_env; |
121 | JMPENV *top_env; |
100 | coro_context cctx; |
122 | coro_context cctx; |
101 | } coro_stack; |
123 | |
|
|
124 | #if USE_VALGRIND |
|
|
125 | int valgrind_id; |
|
|
126 | #endif |
|
|
127 | } coro_cctx; |
102 | |
128 | |
103 | /* this is a structure representing a perl-level coroutine */ |
129 | /* this is a structure representing a perl-level coroutine */ |
104 | struct coro { |
130 | struct coro { |
105 | /* the c coroutine allocated to this perl coroutine, if any */ |
131 | /* the c coroutine allocated to this perl coroutine, if any */ |
106 | coro_stack *stack; |
132 | coro_cctx *cctx; |
107 | |
133 | |
108 | /* data associated with this coroutine (initial args) */ |
134 | /* data associated with this coroutine (initial args) */ |
109 | AV *args; |
135 | AV *args; |
110 | int refcnt; |
136 | int refcnt; |
111 | |
137 | |
112 | /* optionally saved, might be zero */ |
138 | /* optionally saved, might be zero */ |
113 | AV *defav; |
139 | AV *defav; |
114 | SV *defsv; |
140 | SV *defsv; |
115 | SV *errsv; |
141 | SV *errsv; |
116 | |
142 | |
117 | /* saved global state not related to stacks */ |
143 | #define VAR(name,type) type name; |
118 | U8 dowarn; |
144 | # include "state.h" |
119 | I32 in_eval; |
145 | #undef VAR |
120 | |
|
|
121 | /* the stacks and related info (callchain etc..) */ |
|
|
122 | PERL_SI *curstackinfo; |
|
|
123 | AV *curstack; |
|
|
124 | AV *mainstack; |
|
|
125 | SV **stack_sp; |
|
|
126 | OP *op; |
|
|
127 | SV **curpad; |
|
|
128 | AV *comppad; |
|
|
129 | CV *compcv; |
|
|
130 | SV **stack_base; |
|
|
131 | SV **stack_max; |
|
|
132 | SV **tmps_stack; |
|
|
133 | I32 tmps_floor; |
|
|
134 | I32 tmps_ix; |
|
|
135 | I32 tmps_max; |
|
|
136 | I32 *markstack; |
|
|
137 | I32 *markstack_ptr; |
|
|
138 | I32 *markstack_max; |
|
|
139 | I32 *scopestack; |
|
|
140 | I32 scopestack_ix; |
|
|
141 | I32 scopestack_max; |
|
|
142 | ANY *savestack; |
|
|
143 | I32 savestack_ix; |
|
|
144 | I32 savestack_max; |
|
|
145 | OP **retstack; |
|
|
146 | I32 retstack_ix; |
|
|
147 | I32 retstack_max; |
|
|
148 | PMOP *curpm; |
|
|
149 | COP *curcop; |
|
|
150 | |
146 | |
151 | /* coro process data */ |
147 | /* coro process data */ |
152 | int prio; |
148 | int prio; |
153 | }; |
149 | }; |
154 | |
150 | |
155 | typedef struct coro *Coro__State; |
151 | typedef struct coro *Coro__State; |
156 | typedef struct coro *Coro__State_or_hashref; |
152 | typedef struct coro *Coro__State_or_hashref; |
157 | |
153 | |
158 | static AV * |
154 | static AV * |
159 | coro_clone_padlist (pTHX_ CV *cv) |
155 | coro_clone_padlist (CV *cv) |
160 | { |
156 | { |
161 | AV *padlist = CvPADLIST (cv); |
157 | AV *padlist = CvPADLIST (cv); |
162 | AV *newpadlist, *newpad; |
158 | AV *newpadlist, *newpad; |
163 | |
159 | |
164 | newpadlist = newAV (); |
160 | newpadlist = newAV (); |
165 | AvREAL_off (newpadlist); |
161 | AvREAL_off (newpadlist); |
166 | #if PERL_VERSION < 9 |
162 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
163 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
164 | #else |
167 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
165 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
168 | #else |
|
|
169 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
170 | #endif |
166 | #endif |
171 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
167 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
172 | --AvFILLp (padlist); |
168 | --AvFILLp (padlist); |
173 | |
169 | |
174 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
170 | av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); |
… | |
… | |
176 | |
172 | |
177 | return newpadlist; |
173 | return newpadlist; |
178 | } |
174 | } |
179 | |
175 | |
180 | static void |
176 | static void |
181 | free_padlist (pTHX_ AV *padlist) |
177 | free_padlist (AV *padlist) |
182 | { |
178 | { |
183 | /* may be during global destruction */ |
179 | /* may be during global destruction */ |
184 | if (SvREFCNT (padlist)) |
180 | if (SvREFCNT (padlist)) |
185 | { |
181 | { |
186 | I32 i = AvFILLp (padlist); |
182 | I32 i = AvFILLp (padlist); |
… | |
… | |
207 | AV *padlist; |
203 | AV *padlist; |
208 | AV *av = (AV *)mg->mg_obj; |
204 | AV *av = (AV *)mg->mg_obj; |
209 | |
205 | |
210 | /* casting is fun. */ |
206 | /* casting is fun. */ |
211 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
207 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
212 | free_padlist (aTHX_ padlist); |
208 | free_padlist (padlist); |
213 | |
209 | |
214 | SvREFCNT_dec (av); |
210 | SvREFCNT_dec (av); |
215 | |
211 | |
216 | return 0; |
212 | return 0; |
217 | } |
213 | } |
… | |
… | |
220 | |
216 | |
221 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
217 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
222 | |
218 | |
223 | /* the next two functions merely cache the padlists */ |
219 | /* the next two functions merely cache the padlists */ |
224 | static void |
220 | static void |
225 | get_padlist (pTHX_ CV *cv) |
221 | get_padlist (CV *cv) |
226 | { |
222 | { |
227 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
223 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
228 | |
224 | |
229 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
225 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
230 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
226 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
231 | else |
227 | else |
232 | { |
228 | { |
233 | #if 0 |
229 | #if 0 |
234 | /* this is probably cleaner, but also slower? */ |
230 | /* this is probably cleaner, but also slower? */ |
235 | CV *cp = Perl_cv_clone (aTHX_ cv); |
231 | CV *cp = Perl_cv_clone (cv); |
236 | CvPADLIST (cv) = CvPADLIST (cp); |
232 | CvPADLIST (cv) = CvPADLIST (cp); |
237 | CvPADLIST (cp) = 0; |
233 | CvPADLIST (cp) = 0; |
238 | SvREFCNT_dec (cp); |
234 | SvREFCNT_dec (cp); |
239 | #else |
235 | #else |
240 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
236 | CvPADLIST (cv) = coro_clone_padlist (cv); |
241 | #endif |
237 | #endif |
242 | } |
238 | } |
243 | } |
239 | } |
244 | |
240 | |
245 | static void |
241 | static void |
246 | put_padlist (pTHX_ CV *cv) |
242 | put_padlist (CV *cv) |
247 | { |
243 | { |
248 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
244 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
249 | |
245 | |
250 | if (!mg) |
246 | if (!mg) |
251 | { |
247 | { |
… | |
… | |
259 | } |
255 | } |
260 | |
256 | |
261 | #define SB do { |
257 | #define SB do { |
262 | #define SE } while (0) |
258 | #define SE } while (0) |
263 | |
259 | |
264 | #define LOAD(state) load_state(aTHX_ (state)); |
260 | #define LOAD(state) load_state((state)); |
265 | #define SAVE(state,flags) save_state(aTHX_ (state),(flags)); |
261 | #define SAVE(state,flags) save_state((state),(flags)); |
266 | |
262 | |
267 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
263 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
268 | |
264 | |
269 | static void |
265 | static void |
270 | load_state(pTHX_ Coro__State c) |
266 | load_state(Coro__State c) |
271 | { |
267 | { |
272 | PL_dowarn = c->dowarn; |
268 | #define VAR(name,type) PL_ ## name = c->name; |
273 | PL_in_eval = c->in_eval; |
269 | # include "state.h" |
274 | |
270 | #undef VAR |
275 | PL_curstackinfo = c->curstackinfo; |
|
|
276 | PL_curstack = c->curstack; |
|
|
277 | PL_mainstack = c->mainstack; |
|
|
278 | PL_stack_sp = c->stack_sp; |
|
|
279 | PL_op = c->op; |
|
|
280 | PL_curpad = c->curpad; |
|
|
281 | PL_comppad = c->comppad; |
|
|
282 | PL_compcv = c->compcv; |
|
|
283 | PL_stack_base = c->stack_base; |
|
|
284 | PL_stack_max = c->stack_max; |
|
|
285 | PL_tmps_stack = c->tmps_stack; |
|
|
286 | PL_tmps_floor = c->tmps_floor; |
|
|
287 | PL_tmps_ix = c->tmps_ix; |
|
|
288 | PL_tmps_max = c->tmps_max; |
|
|
289 | PL_markstack = c->markstack; |
|
|
290 | PL_markstack_ptr = c->markstack_ptr; |
|
|
291 | PL_markstack_max = c->markstack_max; |
|
|
292 | PL_scopestack = c->scopestack; |
|
|
293 | PL_scopestack_ix = c->scopestack_ix; |
|
|
294 | PL_scopestack_max = c->scopestack_max; |
|
|
295 | PL_savestack = c->savestack; |
|
|
296 | PL_savestack_ix = c->savestack_ix; |
|
|
297 | PL_savestack_max = c->savestack_max; |
|
|
298 | #if PERL_VERSION < 9 |
|
|
299 | PL_retstack = c->retstack; |
|
|
300 | PL_retstack_ix = c->retstack_ix; |
|
|
301 | PL_retstack_max = c->retstack_max; |
|
|
302 | #endif |
|
|
303 | PL_curpm = c->curpm; |
|
|
304 | PL_curcop = c->curcop; |
|
|
305 | |
271 | |
306 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
272 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
307 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
273 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
308 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
274 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
309 | |
275 | |
… | |
… | |
316 | { |
282 | { |
317 | AV *padlist = (AV *)POPs; |
283 | AV *padlist = (AV *)POPs; |
318 | |
284 | |
319 | if (padlist) |
285 | if (padlist) |
320 | { |
286 | { |
321 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
287 | put_padlist (cv); /* mark this padlist as available */ |
322 | CvPADLIST(cv) = padlist; |
288 | CvPADLIST(cv) = padlist; |
323 | } |
289 | } |
324 | |
290 | |
325 | ++CvDEPTH(cv); |
291 | ++CvDEPTH(cv); |
326 | } |
292 | } |
… | |
… | |
328 | PUTBACK; |
294 | PUTBACK; |
329 | } |
295 | } |
330 | } |
296 | } |
331 | |
297 | |
332 | static void |
298 | static void |
333 | save_state(pTHX_ Coro__State c, int flags) |
299 | save_state(Coro__State c, int flags) |
334 | { |
300 | { |
335 | { |
301 | { |
336 | dSP; |
302 | dSP; |
337 | I32 cxix = cxstack_ix; |
303 | I32 cxix = cxstack_ix; |
338 | PERL_CONTEXT *ccstk = cxstack; |
304 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
366 | } |
332 | } |
367 | |
333 | |
368 | PUSHs ((SV *)CvPADLIST(cv)); |
334 | PUSHs ((SV *)CvPADLIST(cv)); |
369 | PUSHs ((SV *)cv); |
335 | PUSHs ((SV *)cv); |
370 | |
336 | |
371 | get_padlist (aTHX_ cv); |
337 | get_padlist (cv); |
372 | } |
338 | } |
373 | } |
339 | } |
374 | #ifdef CXt_FORMAT |
340 | #ifdef CXt_FORMAT |
375 | else if (CxTYPE(cx) == CXt_FORMAT) |
341 | else if (CxTYPE(cx) == CXt_FORMAT) |
376 | { |
342 | { |
… | |
… | |
394 | |
360 | |
395 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
361 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
396 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
362 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
397 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
363 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
398 | |
364 | |
399 | c->dowarn = PL_dowarn; |
365 | #define VAR(name,type)c->name = PL_ ## name; |
400 | c->in_eval = PL_in_eval; |
366 | # include "state.h" |
401 | |
367 | #undef VAR |
402 | c->curstackinfo = PL_curstackinfo; |
|
|
403 | c->curstack = PL_curstack; |
|
|
404 | c->mainstack = PL_mainstack; |
|
|
405 | c->stack_sp = PL_stack_sp; |
|
|
406 | c->op = PL_op; |
|
|
407 | c->curpad = PL_curpad; |
|
|
408 | c->comppad = PL_comppad; |
|
|
409 | c->compcv = PL_compcv; |
|
|
410 | c->stack_base = PL_stack_base; |
|
|
411 | c->stack_max = PL_stack_max; |
|
|
412 | c->tmps_stack = PL_tmps_stack; |
|
|
413 | c->tmps_floor = PL_tmps_floor; |
|
|
414 | c->tmps_ix = PL_tmps_ix; |
|
|
415 | c->tmps_max = PL_tmps_max; |
|
|
416 | c->markstack = PL_markstack; |
|
|
417 | c->markstack_ptr = PL_markstack_ptr; |
|
|
418 | c->markstack_max = PL_markstack_max; |
|
|
419 | c->scopestack = PL_scopestack; |
|
|
420 | c->scopestack_ix = PL_scopestack_ix; |
|
|
421 | c->scopestack_max = PL_scopestack_max; |
|
|
422 | c->savestack = PL_savestack; |
|
|
423 | c->savestack_ix = PL_savestack_ix; |
|
|
424 | c->savestack_max = PL_savestack_max; |
|
|
425 | #if PERL_VERSION < 9 |
|
|
426 | c->retstack = PL_retstack; |
|
|
427 | c->retstack_ix = PL_retstack_ix; |
|
|
428 | c->retstack_max = PL_retstack_max; |
|
|
429 | #endif |
|
|
430 | c->curpm = PL_curpm; |
|
|
431 | c->curcop = PL_curcop; |
|
|
432 | } |
368 | } |
433 | |
369 | |
434 | /* |
370 | /* |
435 | * allocate various perl stacks. This is an exact copy |
371 | * allocate various perl stacks. This is an exact copy |
436 | * of perl.c:init_stacks, except that it uses less memory |
372 | * of perl.c:init_stacks, except that it uses less memory |
437 | * on the (sometimes correct) assumption that coroutines do |
373 | * on the (sometimes correct) assumption that coroutines do |
438 | * not usually need a lot of stackspace. |
374 | * not usually need a lot of stackspace. |
439 | */ |
375 | */ |
440 | static void |
376 | static void |
441 | coro_init_stacks (pTHX) |
377 | coro_init_stacks () |
442 | { |
378 | { |
443 | LOCK; |
|
|
444 | |
|
|
445 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
379 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
446 | PL_curstackinfo->si_type = PERLSI_MAIN; |
380 | PL_curstackinfo->si_type = PERLSI_MAIN; |
447 | PL_curstack = PL_curstackinfo->si_stack; |
381 | PL_curstack = PL_curstackinfo->si_stack; |
448 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
382 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
449 | |
383 | |
… | |
… | |
470 | |
404 | |
471 | New(54,PL_savestack,96,ANY); |
405 | New(54,PL_savestack,96,ANY); |
472 | PL_savestack_ix = 0; |
406 | PL_savestack_ix = 0; |
473 | PL_savestack_max = 96; |
407 | PL_savestack_max = 96; |
474 | |
408 | |
475 | #if PERL_VERSION < 9 |
409 | #if !PERL_VERSION_ATLEAST (5,9,0) |
476 | New(54,PL_retstack,8,OP*); |
410 | New(54,PL_retstack,8,OP*); |
477 | PL_retstack_ix = 0; |
411 | PL_retstack_ix = 0; |
478 | PL_retstack_max = 8; |
412 | PL_retstack_max = 8; |
479 | #endif |
413 | #endif |
480 | |
|
|
481 | UNLOCK; |
|
|
482 | } |
414 | } |
483 | |
415 | |
484 | /* |
416 | /* |
485 | * destroy the stacks, the callchain etc... |
417 | * destroy the stacks, the callchain etc... |
486 | */ |
418 | */ |
487 | static void |
419 | static void |
488 | destroy_stacks(pTHX) |
420 | coro_destroy_stacks () |
489 | { |
421 | { |
490 | if (!IN_DESTRUCT) |
422 | if (!IN_DESTRUCT) |
491 | { |
423 | { |
492 | /* is this ugly, I ask? */ |
424 | /* is this ugly, I ask? */ |
493 | LEAVE_SCOPE (0); |
425 | LEAVE_SCOPE (0); |
… | |
… | |
524 | |
456 | |
525 | Safefree (PL_tmps_stack); |
457 | Safefree (PL_tmps_stack); |
526 | Safefree (PL_markstack); |
458 | Safefree (PL_markstack); |
527 | Safefree (PL_scopestack); |
459 | Safefree (PL_scopestack); |
528 | Safefree (PL_savestack); |
460 | Safefree (PL_savestack); |
529 | #if PERL_VERSION < 9 |
461 | #if !PERL_VERSION_ATLEAST (5,9,0) |
530 | Safefree (PL_retstack); |
462 | Safefree (PL_retstack); |
531 | #endif |
463 | #endif |
532 | } |
464 | } |
533 | |
465 | |
534 | static void |
466 | static void |
535 | setup_coro (struct coro *coro) |
467 | setup_coro (struct coro *coro) |
536 | { |
468 | { |
537 | /* |
469 | /* |
538 | * emulate part of the perl startup here. |
470 | * emulate part of the perl startup here. |
539 | */ |
471 | */ |
540 | dTHX; |
|
|
541 | dSP; |
|
|
542 | UNOP myop; |
|
|
543 | SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); |
|
|
544 | |
472 | |
545 | coro_init_stacks (aTHX); |
473 | coro_init_stacks (); |
|
|
474 | |
546 | /*PL_curcop = 0;*/ |
475 | PL_curcop = 0; |
547 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
476 | PL_in_eval = 0; |
|
|
477 | PL_curpm = 0; |
|
|
478 | |
|
|
479 | { |
|
|
480 | dSP; |
|
|
481 | LOGOP myop; |
|
|
482 | |
|
|
483 | /* I have no idea why this is needed, but it is */ |
|
|
484 | PUSHMARK (SP); |
|
|
485 | |
548 | SvREFCNT_dec (GvAV (PL_defgv)); |
486 | SvREFCNT_dec (GvAV (PL_defgv)); |
549 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
487 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
550 | |
488 | |
551 | SPAGAIN; |
|
|
552 | |
|
|
553 | Zero (&myop, 1, UNOP); |
489 | Zero (&myop, 1, LOGOP); |
554 | myop.op_next = Nullop; |
490 | myop.op_next = Nullop; |
555 | myop.op_flags = OPf_WANT_VOID; |
491 | myop.op_flags = OPf_WANT_VOID; |
556 | |
492 | |
557 | PL_op = (OP *)&myop; |
493 | PL_op = (OP *)&myop; |
558 | |
494 | |
559 | PUSHMARK(SP); |
495 | PUSHMARK (SP); |
560 | XPUSHs (sub_init); |
496 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
561 | PUTBACK; |
497 | PUTBACK; |
562 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
498 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
563 | SPAGAIN; |
499 | SPAGAIN; |
564 | |
500 | |
565 | ENTER; /* necessary e.g. for dounwind */ |
501 | ENTER; /* necessary e.g. for dounwind */ |
|
|
502 | } |
566 | } |
503 | } |
567 | |
504 | |
568 | static void |
505 | static void |
569 | free_coro_mortal () |
506 | free_coro_mortal () |
570 | { |
507 | { |
571 | if (coro_mortal) |
508 | if (coro_mortal) |
572 | { |
509 | { |
573 | dTHX; |
|
|
574 | |
|
|
575 | SvREFCNT_dec (coro_mortal); |
510 | SvREFCNT_dec (coro_mortal); |
576 | coro_mortal = 0; |
511 | coro_mortal = 0; |
577 | } |
512 | } |
578 | } |
513 | } |
579 | |
514 | |
|
|
515 | static void NOINLINE |
|
|
516 | prepare_cctx (coro_cctx *cctx) |
|
|
517 | { |
|
|
518 | dSP; |
|
|
519 | LOGOP myop; |
|
|
520 | |
|
|
521 | Zero (&myop, 1, LOGOP); |
|
|
522 | myop.op_next = PL_op; |
|
|
523 | myop.op_flags = OPf_WANT_VOID; |
|
|
524 | |
|
|
525 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
|
|
526 | |
|
|
527 | PUSHMARK (SP); |
|
|
528 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
|
|
529 | PUTBACK; |
|
|
530 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
531 | SPAGAIN; |
|
|
532 | } |
|
|
533 | |
580 | static void |
534 | static void |
581 | coro_run (void *arg) |
535 | coro_run (void *arg) |
582 | { |
536 | { |
|
|
537 | /* coro_run is the alternative epilogue of transfer() */ |
|
|
538 | UNLOCK; |
|
|
539 | |
583 | /* |
540 | /* |
584 | * this is a _very_ stripped down perl interpreter ;) |
541 | * this is a _very_ stripped down perl interpreter ;) |
585 | */ |
542 | */ |
586 | dTHX; |
|
|
587 | int ret; |
|
|
588 | |
|
|
589 | UNLOCK; |
|
|
590 | |
|
|
591 | PL_top_env = &PL_start_env; |
543 | PL_top_env = &PL_start_env; |
592 | |
544 | |
593 | sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); |
545 | /* inject call to cctx_init */ |
594 | sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op)); |
546 | prepare_cctx ((coro_cctx *)arg); |
595 | |
|
|
596 | /* continue at cctx_init, without entersub */ |
|
|
597 | PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE)); |
|
|
598 | |
547 | |
599 | /* somebody will hit me for both perl_run and PL_restartop */ |
548 | /* somebody will hit me for both perl_run and PL_restartop */ |
600 | ret = perl_run (aTHX_ PERL_GET_CONTEXT); |
549 | perl_run (PL_curinterp); |
601 | printf ("ret %d\n", ret);//D |
|
|
602 | |
550 | |
603 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); |
551 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
604 | abort (); |
552 | abort (); |
605 | } |
553 | } |
606 | |
554 | |
607 | static coro_stack * |
555 | static coro_cctx * |
608 | stack_new () |
556 | cctx_new () |
609 | { |
557 | { |
610 | coro_stack *stack; |
558 | coro_cctx *cctx; |
611 | |
559 | |
|
|
560 | ++cctx_count; |
|
|
561 | |
612 | New (0, stack, 1, coro_stack); |
562 | New (0, cctx, 1, coro_cctx); |
613 | |
563 | |
614 | #if HAVE_MMAP |
564 | #if HAVE_MMAP |
615 | |
565 | |
616 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
566 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
617 | /* mmap suppsedly does allocate-on-write for us */ |
567 | /* mmap suppsedly does allocate-on-write for us */ |
618 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
568 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
619 | |
569 | |
620 | if (stack->sptr == (void *)-1) |
570 | if (cctx->sptr == (void *)-1) |
621 | { |
571 | { |
622 | perror ("FATAL: unable to mmap stack for coroutine"); |
572 | perror ("FATAL: unable to mmap stack for coroutine"); |
623 | _exit (EXIT_FAILURE); |
573 | _exit (EXIT_FAILURE); |
624 | } |
574 | } |
625 | |
575 | |
626 | # if STACKGUARD |
576 | # if STACKGUARD |
627 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
577 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
628 | # endif |
578 | # endif |
629 | |
579 | |
630 | #else |
580 | #else |
631 | |
581 | |
632 | stack->ssize = STACKSIZE * (long)sizeof (long); |
582 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
633 | New (0, stack->sptr, STACKSIZE, long); |
583 | New (0, cctx->sptr, STACKSIZE, long); |
634 | |
584 | |
635 | if (!stack->sptr) |
585 | if (!cctx->sptr) |
636 | { |
586 | { |
637 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
587 | perror ("FATAL: unable to malloc stack for coroutine"); |
638 | _exit (EXIT_FAILURE); |
588 | _exit (EXIT_FAILURE); |
639 | } |
589 | } |
640 | |
590 | |
641 | #endif |
591 | #endif |
642 | |
592 | |
|
|
593 | #if USE_VALGRIND |
|
|
594 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
595 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
|
|
596 | cctx->ssize + (char *)cctx->sptr |
|
|
597 | ); |
|
|
598 | #endif |
|
|
599 | |
643 | coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); |
600 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
644 | |
601 | |
645 | return stack; |
602 | return cctx; |
646 | } |
603 | } |
647 | |
604 | |
648 | static void |
605 | static void |
649 | stack_free (coro_stack *stack) |
606 | cctx_free (coro_cctx *cctx) |
650 | { |
607 | { |
651 | if (!stack) |
608 | if (!cctx) |
652 | return; |
609 | return; |
653 | |
610 | |
|
|
611 | --cctx_count; |
|
|
612 | |
|
|
613 | #if USE_VALGRIND |
|
|
614 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
|
|
615 | #endif |
|
|
616 | |
654 | #if HAVE_MMAP |
617 | #if HAVE_MMAP |
655 | munmap (stack->sptr, stack->ssize); |
618 | munmap (cctx->sptr, cctx->ssize); |
656 | #else |
619 | #else |
657 | Safefree (stack->sptr); |
620 | Safefree (cctx->sptr); |
658 | #endif |
621 | #endif |
659 | |
622 | |
660 | Safefree (stack); |
623 | Safefree (cctx); |
661 | } |
624 | } |
662 | |
625 | |
663 | static coro_stack *stack_first; |
|
|
664 | |
|
|
665 | static coro_stack * |
626 | static coro_cctx * |
666 | stack_get () |
627 | cctx_get () |
667 | { |
628 | { |
668 | coro_stack *stack; |
629 | coro_cctx *cctx; |
669 | |
630 | |
670 | if (stack_first) |
631 | if (cctx_first) |
671 | { |
632 | { |
672 | stack = stack_first; |
633 | --cctx_idle; |
|
|
634 | cctx = cctx_first; |
673 | stack_first = stack->next; |
635 | cctx_first = cctx->next; |
674 | } |
636 | } |
675 | else |
637 | else |
676 | { |
638 | { |
677 | stack = stack_new (); |
639 | cctx = cctx_new (); |
678 | PL_op = PL_op->op_next; |
640 | PL_op = PL_op->op_next; |
679 | } |
641 | } |
680 | |
642 | |
681 | return stack; |
643 | return cctx; |
682 | } |
644 | } |
683 | |
645 | |
684 | static void |
646 | static void |
685 | stack_put (coro_stack *stack) |
647 | cctx_put (coro_cctx *cctx) |
686 | { |
648 | { |
687 | stack->next = stack_first; |
649 | ++cctx_idle; |
688 | stack_first = stack; |
650 | cctx->next = cctx_first; |
|
|
651 | cctx_first = cctx; |
689 | } |
652 | } |
690 | |
653 | |
691 | /* never call directly, always through the coro_state_transfer global variable */ |
654 | /* never call directly, always through the coro_state_transfer global variable */ |
692 | static void |
655 | static void NOINLINE |
693 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
656 | transfer (struct coro *prev, struct coro *next, int flags) |
694 | { |
657 | { |
695 | dSTACKLEVEL; |
658 | dSTACKLEVEL; |
696 | |
659 | |
697 | /* sometimes transfer is only called to set idle_sp */ |
660 | /* sometimes transfer is only called to set idle_sp */ |
698 | if (flags == TRANSFER_SET_STACKLEVEL) |
661 | if (flags == TRANSFER_SET_STACKLEVEL) |
699 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
662 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
700 | else if (prev != next) |
663 | else if (prev != next) |
701 | { |
664 | { |
702 | coro_stack *prev__stack; |
665 | coro_cctx *prev__cctx; |
703 | |
666 | |
704 | LOCK; |
667 | LOCK; |
705 | |
668 | |
706 | if (next->mainstack) |
669 | if (next->mainstack) |
707 | { |
670 | { |
… | |
… | |
715 | /* first get rid of the old state */ |
678 | /* first get rid of the old state */ |
716 | SAVE (prev, -1); |
679 | SAVE (prev, -1); |
717 | /* setup coroutine call */ |
680 | /* setup coroutine call */ |
718 | setup_coro (next); |
681 | setup_coro (next); |
719 | /* need a stack */ |
682 | /* need a stack */ |
720 | next->stack = 0; |
683 | next->cctx = 0; |
721 | } |
684 | } |
722 | |
685 | |
723 | if (!prev->stack) |
686 | if (!prev->cctx) |
724 | /* create a new empty context */ |
687 | /* create a new empty context */ |
725 | Newz (0, prev->stack, 1, coro_stack); |
688 | Newz (0, prev->cctx, 1, coro_cctx); |
726 | |
689 | |
727 | prev__stack = prev->stack; |
690 | prev__cctx = prev->cctx; |
728 | |
691 | |
729 | /* possibly "free" the stack */ |
692 | /* possibly "free" the cctx */ |
730 | if (prev__stack->idle_sp == STACKLEVEL) |
693 | if (prev__cctx->idle_sp == STACKLEVEL) |
731 | { |
694 | { |
732 | stack_put (prev__stack); |
695 | cctx_put (prev__cctx); |
733 | prev->stack = 0; |
696 | prev->cctx = 0; |
734 | } |
697 | } |
735 | |
698 | |
736 | if (!next->stack) |
699 | if (!next->cctx) |
737 | next->stack = stack_get (); |
700 | next->cctx = cctx_get (); |
738 | |
701 | |
739 | if (prev__stack != next->stack) |
702 | if (prev__cctx != next->cctx) |
740 | { |
703 | { |
741 | prev__stack->top_env = PL_top_env; |
704 | prev__cctx->top_env = PL_top_env; |
742 | PL_top_env = next->stack->top_env; |
705 | PL_top_env = next->cctx->top_env; |
743 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
706 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
744 | } |
707 | } |
745 | |
708 | |
746 | free_coro_mortal (); |
709 | free_coro_mortal (); |
747 | |
710 | |
748 | UNLOCK; |
711 | UNLOCK; |
749 | } |
712 | } |
750 | } |
713 | } |
751 | |
|
|
752 | /* use this function pointer to call the above function */ |
|
|
753 | /* this is done to increase chances of the compiler not inlining the call */ |
|
|
754 | /* not static to make it even harder for the compiler (and theoretically impossible in most cases */ |
|
|
755 | void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; |
|
|
756 | |
714 | |
757 | struct transfer_args |
715 | struct transfer_args |
758 | { |
716 | { |
759 | struct coro *prev, *next; |
717 | struct coro *prev, *next; |
760 | int flags; |
718 | int flags; |
761 | }; |
719 | }; |
762 | |
720 | |
763 | #define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) |
721 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) |
764 | |
722 | |
765 | static void |
723 | static void |
766 | coro_state_destroy (struct coro *coro) |
724 | coro_state_destroy (struct coro *coro) |
767 | { |
725 | { |
768 | if (coro->refcnt--) |
726 | if (coro->refcnt--) |
… | |
… | |
770 | |
728 | |
771 | if (coro->mainstack && coro->mainstack != main_mainstack) |
729 | if (coro->mainstack && coro->mainstack != main_mainstack) |
772 | { |
730 | { |
773 | struct coro temp; |
731 | struct coro temp; |
774 | |
732 | |
775 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
733 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
776 | LOAD (aTHX_ coro); |
734 | LOAD (coro); |
777 | |
735 | |
778 | destroy_stacks (aTHX); |
736 | coro_destroy_stacks (); |
779 | |
737 | |
780 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
738 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
781 | |
739 | |
782 | coro->mainstack = 0; |
740 | coro->mainstack = 0; |
783 | } |
741 | } |
784 | |
742 | |
785 | stack_free (coro->stack); |
743 | cctx_free (coro->cctx); |
786 | SvREFCNT_dec (coro->args); |
744 | SvREFCNT_dec (coro->args); |
787 | Safefree (coro); |
745 | Safefree (coro); |
788 | } |
746 | } |
789 | |
747 | |
790 | static int |
748 | static int |
791 | coro_state_clear (SV *sv, MAGIC *mg) |
749 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
792 | { |
750 | { |
793 | struct coro *coro = (struct coro *)mg->mg_ptr; |
751 | struct coro *coro = (struct coro *)mg->mg_ptr; |
794 | mg->mg_ptr = 0; |
752 | mg->mg_ptr = 0; |
795 | |
753 | |
796 | coro_state_destroy (coro); |
754 | coro_state_destroy (coro); |
797 | |
755 | |
798 | return 0; |
756 | return 0; |
799 | } |
757 | } |
800 | |
758 | |
801 | static int |
759 | static int |
802 | coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) |
760 | coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) |
803 | { |
761 | { |
804 | struct coro *coro = (struct coro *)mg->mg_ptr; |
762 | struct coro *coro = (struct coro *)mg->mg_ptr; |
805 | |
763 | |
806 | ++coro->refcnt; |
764 | ++coro->refcnt; |
807 | |
765 | |
808 | return 0; |
766 | return 0; |
809 | } |
767 | } |
810 | |
768 | |
811 | static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; |
769 | static MGVTBL coro_state_vtbl = { |
|
|
770 | 0, 0, 0, 0, |
|
|
771 | coro_state_clear, |
|
|
772 | 0, |
|
|
773 | #ifdef MGf_DUP |
|
|
774 | coro_state_dup, |
|
|
775 | #else |
|
|
776 | # define MGf_DUP 0 |
|
|
777 | #endif |
|
|
778 | }; |
812 | |
779 | |
813 | static struct coro * |
780 | static struct coro * |
814 | SvSTATE (SV *coro) |
781 | SvSTATE (SV *coro) |
815 | { |
782 | { |
816 | HV *stash; |
783 | HV *stash; |
… | |
… | |
831 | assert (mg->mg_type == PERL_MAGIC_ext); |
798 | assert (mg->mg_type == PERL_MAGIC_ext); |
832 | return (struct coro *)mg->mg_ptr; |
799 | return (struct coro *)mg->mg_ptr; |
833 | } |
800 | } |
834 | |
801 | |
835 | static void |
802 | static void |
836 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) |
803 | prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) |
837 | { |
804 | { |
838 | ta->prev = SvSTATE (prev); |
805 | ta->prev = SvSTATE (prev); |
839 | ta->next = SvSTATE (next); |
806 | ta->next = SvSTATE (next); |
840 | ta->flags = flags; |
807 | ta->flags = flags; |
841 | } |
808 | } |
… | |
… | |
844 | api_transfer (SV *prev, SV *next, int flags) |
811 | api_transfer (SV *prev, SV *next, int flags) |
845 | { |
812 | { |
846 | dTHX; |
813 | dTHX; |
847 | struct transfer_args ta; |
814 | struct transfer_args ta; |
848 | |
815 | |
849 | prepare_transfer (aTHX_ &ta, prev, next, flags); |
816 | prepare_transfer (&ta, prev, next, flags); |
850 | TRANSFER (ta); |
817 | TRANSFER (ta); |
851 | } |
818 | } |
852 | |
819 | |
853 | /** Coro ********************************************************************/ |
820 | /** Coro ********************************************************************/ |
854 | |
821 | |
… | |
… | |
863 | static GV *coro_current, *coro_idle; |
830 | static GV *coro_current, *coro_idle; |
864 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
831 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
865 | static int coro_nready; |
832 | static int coro_nready; |
866 | |
833 | |
867 | static void |
834 | static void |
868 | coro_enq (pTHX_ SV *sv) |
835 | coro_enq (SV *sv) |
869 | { |
836 | { |
870 | int prio; |
837 | int prio; |
871 | |
838 | |
872 | if (SvTYPE (sv) != SVt_PVHV) |
839 | if (SvTYPE (sv) != SVt_PVHV) |
873 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
840 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
… | |
… | |
877 | av_push (coro_ready [prio - PRIO_MIN], sv); |
844 | av_push (coro_ready [prio - PRIO_MIN], sv); |
878 | coro_nready++; |
845 | coro_nready++; |
879 | } |
846 | } |
880 | |
847 | |
881 | static SV * |
848 | static SV * |
882 | coro_deq (pTHX_ int min_prio) |
849 | coro_deq (int min_prio) |
883 | { |
850 | { |
884 | int prio = PRIO_MAX - PRIO_MIN; |
851 | int prio = PRIO_MAX - PRIO_MIN; |
885 | |
852 | |
886 | min_prio -= PRIO_MIN; |
853 | min_prio -= PRIO_MIN; |
887 | if (min_prio < 0) |
854 | if (min_prio < 0) |
… | |
… | |
904 | |
871 | |
905 | if (SvROK (coro)) |
872 | if (SvROK (coro)) |
906 | coro = SvRV (coro); |
873 | coro = SvRV (coro); |
907 | |
874 | |
908 | LOCK; |
875 | LOCK; |
909 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
876 | coro_enq (SvREFCNT_inc (coro)); |
910 | UNLOCK; |
877 | UNLOCK; |
911 | } |
878 | } |
912 | |
879 | |
913 | static void |
880 | static void |
914 | prepare_schedule (aTHX_ struct transfer_args *ta) |
881 | prepare_schedule (struct transfer_args *ta) |
915 | { |
882 | { |
916 | SV *current, *prev, *next; |
883 | SV *current, *prev, *next; |
917 | |
|
|
918 | LOCK; |
|
|
919 | |
884 | |
920 | current = GvSV (coro_current); |
885 | current = GvSV (coro_current); |
921 | |
886 | |
922 | for (;;) |
887 | for (;;) |
923 | { |
888 | { |
924 | LOCK; |
889 | LOCK; |
925 | |
|
|
926 | next = coro_deq (aTHX_ PRIO_MIN); |
890 | next = coro_deq (PRIO_MIN); |
|
|
891 | UNLOCK; |
927 | |
892 | |
928 | if (next) |
893 | if (next) |
929 | break; |
894 | break; |
930 | |
|
|
931 | UNLOCK; |
|
|
932 | |
895 | |
933 | { |
896 | { |
934 | dSP; |
897 | dSP; |
935 | |
898 | |
936 | ENTER; |
899 | ENTER; |
… | |
… | |
947 | |
910 | |
948 | prev = SvRV (current); |
911 | prev = SvRV (current); |
949 | SvRV (current) = next; |
912 | SvRV (current) = next; |
950 | |
913 | |
951 | /* free this only after the transfer */ |
914 | /* free this only after the transfer */ |
|
|
915 | LOCK; |
952 | free_coro_mortal (); |
916 | free_coro_mortal (); |
|
|
917 | UNLOCK; |
953 | coro_mortal = prev; |
918 | coro_mortal = prev; |
954 | |
919 | |
955 | ta->prev = SvSTATE (prev); |
920 | ta->prev = SvSTATE (prev); |
956 | ta->next = SvSTATE (next); |
921 | ta->next = SvSTATE (next); |
957 | ta->flags = TRANSFER_SAVE_ALL; |
922 | ta->flags = TRANSFER_SAVE_ALL; |
958 | |
|
|
959 | UNLOCK; |
|
|
960 | } |
923 | } |
961 | |
924 | |
962 | static void |
925 | static void |
963 | prepare_cede (aTHX_ struct transfer_args *ta) |
926 | prepare_cede (struct transfer_args *ta) |
964 | { |
927 | { |
965 | LOCK; |
928 | LOCK; |
966 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
929 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
967 | UNLOCK; |
930 | UNLOCK; |
968 | |
931 | |
969 | prepare_schedule (ta); |
932 | prepare_schedule (ta); |
970 | } |
933 | } |
971 | |
934 | |
… | |
… | |
1029 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
992 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1030 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
993 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1031 | |
994 | |
1032 | for (i = 1; i < items; i++) |
995 | for (i = 1; i < items; i++) |
1033 | av_push (coro->args, newSVsv (ST (i))); |
996 | av_push (coro->args, newSVsv (ST (i))); |
1034 | |
|
|
1035 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
|
|
1036 | /*coro->stack = 0;*/ |
|
|
1037 | } |
997 | } |
1038 | OUTPUT: |
998 | OUTPUT: |
1039 | RETVAL |
999 | RETVAL |
1040 | |
1000 | |
1041 | void |
1001 | void |
… | |
… | |
1050 | struct transfer_args ta; |
1010 | struct transfer_args ta; |
1051 | |
1011 | |
1052 | switch (ix) |
1012 | switch (ix) |
1053 | { |
1013 | { |
1054 | case 0: |
1014 | case 0: |
1055 | ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); |
1015 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1056 | ta.next = 0; |
1016 | ta.next = 0; |
1057 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1017 | ta.flags = TRANSFER_SET_STACKLEVEL; |
1058 | break; |
1018 | break; |
1059 | |
1019 | |
1060 | case 1: |
1020 | case 1: |
… | |
… | |
1114 | ++coro_src->refcnt; |
1074 | ++coro_src->refcnt; |
1115 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1075 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1116 | } |
1076 | } |
1117 | |
1077 | |
1118 | void |
1078 | void |
1119 | _nonlocal_goto (IV nextop) |
|
|
1120 | CODE: |
|
|
1121 | /* uuh, somebody will kill me again for this */ |
|
|
1122 | PL_op->op_next = INT2PTR (OP *, nextop); |
|
|
1123 | |
|
|
1124 | void |
|
|
1125 | _exit (code) |
1079 | _exit (code) |
1126 | int code |
1080 | int code |
1127 | PROTOTYPE: $ |
1081 | PROTOTYPE: $ |
1128 | CODE: |
1082 | CODE: |
1129 | _exit (code); |
1083 | _exit (code); |
|
|
1084 | |
|
|
1085 | int |
|
|
1086 | cctx_count () |
|
|
1087 | CODE: |
|
|
1088 | RETVAL = cctx_count; |
|
|
1089 | OUTPUT: |
|
|
1090 | RETVAL |
|
|
1091 | |
|
|
1092 | int |
|
|
1093 | cctx_idle () |
|
|
1094 | CODE: |
|
|
1095 | RETVAL = cctx_idle; |
|
|
1096 | OUTPUT: |
|
|
1097 | RETVAL |
1130 | |
1098 | |
1131 | MODULE = Coro::State PACKAGE = Coro |
1099 | MODULE = Coro::State PACKAGE = Coro |
1132 | |
1100 | |
1133 | BOOT: |
1101 | BOOT: |
1134 | { |
1102 | { |