1 | #include "libcoro/coro.c" |
1 | #include "libcoro/coro.c" |
|
|
2 | |
|
|
3 | #define PERL_NO_GET_CONTEXT |
2 | |
4 | |
3 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
4 | #include "perl.h" |
6 | #include "perl.h" |
5 | #include "XSUB.h" |
7 | #include "XSUB.h" |
6 | |
8 | |
… | |
… | |
31 | #else |
33 | #else |
32 | # define PAGESIZE 0 |
34 | # define PAGESIZE 0 |
33 | # define BOOT_PAGESIZE (void)0 |
35 | # define BOOT_PAGESIZE (void)0 |
34 | #endif |
36 | #endif |
35 | |
37 | |
36 | #if USE_VALGRIND |
38 | #if CORO_USE_VALGRIND |
37 | # include <valgrind/valgrind.h> |
39 | # include <valgrind/valgrind.h> |
|
|
40 | # define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end)) |
|
|
41 | #else |
|
|
42 | # define REGISTER_STACK(cctx,start,end) |
38 | #endif |
43 | #endif |
39 | |
44 | |
40 | /* the maximum number of idle cctx that will be pooled */ |
45 | /* the maximum number of idle cctx that will be pooled */ |
41 | #define MAX_IDLE_CCTX 8 |
46 | #define MAX_IDLE_CCTX 8 |
42 | |
47 | |
… | |
… | |
71 | #ifndef SvRV_set |
76 | #ifndef SvRV_set |
72 | # define SvRV_set(s,v) SvRV(s) = (v) |
77 | # define SvRV_set(s,v) SvRV(s) = (v) |
73 | #endif |
78 | #endif |
74 | |
79 | |
75 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
80 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
76 | # undef STACKGUARD |
81 | # undef CORO_STACKGUARD |
77 | #endif |
82 | #endif |
78 | |
83 | |
79 | #ifndef STACKGUARD |
84 | #ifndef CORO_STACKGUARD |
80 | # define STACKGUARD 0 |
85 | # define CORO_STACKGUARD 0 |
81 | #endif |
86 | #endif |
82 | |
87 | |
83 | /* prefer perl internal functions over our own? */ |
88 | /* prefer perl internal functions over our own? */ |
84 | #ifndef PREFER_PERL_FUNCTIONS |
89 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
85 | # define PREFER_PERL_FUNCTIONS 0 |
90 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
86 | #endif |
91 | #endif |
87 | |
92 | |
88 | /* The next macro should declare a variable stacklevel that contains and approximation |
93 | /* The next macro should declare a variable stacklevel that contains and approximation |
89 | * to the current C stack pointer. Its property is that it changes with each call |
94 | * to the current C stack pointer. Its property is that it changes with each call |
90 | * and should be unique. */ |
95 | * and should be unique. */ |
… | |
… | |
93 | |
98 | |
94 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
99 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
95 | |
100 | |
96 | #if __GNUC__ >= 3 |
101 | #if __GNUC__ >= 3 |
97 | # define attribute(x) __attribute__(x) |
102 | # define attribute(x) __attribute__(x) |
|
|
103 | # define BARRIER __asm__ __volatile__ ("" : : : "memory") |
98 | #else |
104 | #else |
99 | # define attribute(x) |
105 | # define attribute(x) |
|
|
106 | # define BARRIER |
100 | #endif |
107 | #endif |
101 | |
108 | |
102 | #define NOINLINE attribute ((noinline)) |
109 | #define NOINLINE attribute ((noinline)) |
103 | |
110 | |
104 | #include "CoroAPI.h" |
111 | #include "CoroAPI.h" |
… | |
… | |
110 | #else |
117 | #else |
111 | # define LOCK (void)0 |
118 | # define LOCK (void)0 |
112 | # define UNLOCK (void)0 |
119 | # define UNLOCK (void)0 |
113 | #endif |
120 | #endif |
114 | |
121 | |
|
|
122 | /* helper storage struct for Coro::AIO */ |
115 | struct io_state |
123 | struct io_state |
116 | { |
124 | { |
117 | int errorno; |
125 | int errorno; |
118 | I32 laststype; |
126 | I32 laststype; |
119 | int laststatval; |
127 | int laststatval; |
120 | Stat_t statcache; |
128 | Stat_t statcache; |
121 | }; |
129 | }; |
122 | |
130 | |
|
|
131 | static size_t coro_stacksize = CORO_STACKSIZE; |
123 | static struct CoroAPI coroapi; |
132 | static struct CoroAPI coroapi; |
124 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
133 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
125 | static HV *coro_state_stash, *coro_stash; |
134 | static HV *coro_state_stash, *coro_stash; |
126 | static SV *coro_mortal; /* will be freed after next transfer */ |
135 | static SV *coro_mortal; /* will be freed after next transfer */ |
127 | |
136 | |
… | |
… | |
132 | typedef struct coro_cctx { |
141 | typedef struct coro_cctx { |
133 | struct coro_cctx *next; |
142 | struct coro_cctx *next; |
134 | |
143 | |
135 | /* the stack */ |
144 | /* the stack */ |
136 | void *sptr; |
145 | void *sptr; |
137 | long ssize; /* positive == mmap, otherwise malloc */ |
146 | size_t ssize; |
138 | |
147 | |
139 | /* cpu state */ |
148 | /* cpu state */ |
140 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
149 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
150 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
141 | JMPENV *top_env; |
151 | JMPENV *top_env; |
142 | coro_context cctx; |
152 | coro_context cctx; |
143 | |
153 | |
144 | int inuse; |
|
|
145 | |
|
|
146 | #if USE_VALGRIND |
154 | #if CORO_USE_VALGRIND |
147 | int valgrind_id; |
155 | int valgrind_id; |
148 | #endif |
156 | #endif |
|
|
157 | char inuse, mapped; |
149 | } coro_cctx; |
158 | } coro_cctx; |
150 | |
159 | |
151 | enum { |
160 | enum { |
152 | CF_RUNNING = 0x0001, /* coroutine is running */ |
161 | CF_RUNNING = 0x0001, /* coroutine is running */ |
153 | CF_READY = 0x0002, /* coroutine is ready */ |
162 | CF_READY = 0x0002, /* coroutine is ready */ |
154 | CF_NEW = 0x0004, /* ahs never been switched to */ |
163 | CF_NEW = 0x0004, /* has never been switched to */ |
|
|
164 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
155 | }; |
165 | }; |
156 | |
166 | |
157 | /* this is a structure representing a perl-level coroutine */ |
167 | /* this is a structure representing a perl-level coroutine */ |
158 | struct coro { |
168 | struct coro { |
159 | /* the c coroutine allocated to this perl coroutine, if any */ |
169 | /* the c coroutine allocated to this perl coroutine, if any */ |
… | |
… | |
167 | |
177 | |
168 | /* optionally saved, might be zero */ |
178 | /* optionally saved, might be zero */ |
169 | AV *defav; /* @_ */ |
179 | AV *defav; /* @_ */ |
170 | SV *defsv; /* $_ */ |
180 | SV *defsv; /* $_ */ |
171 | SV *errsv; /* $@ */ |
181 | SV *errsv; /* $@ */ |
|
|
182 | GV *deffh; /* default filehandle */ |
172 | SV *irssv; /* $/ */ |
183 | SV *irssv; /* $/ */ |
173 | SV *irssv_sv; /* real $/ cache */ |
184 | SV *irssv_sv; /* real $/ cache */ |
174 | |
185 | |
175 | #define VAR(name,type) type name; |
186 | #define VAR(name,type) type name; |
176 | # include "state.h" |
187 | # include "state.h" |
… | |
… | |
181 | }; |
192 | }; |
182 | |
193 | |
183 | typedef struct coro *Coro__State; |
194 | typedef struct coro *Coro__State; |
184 | typedef struct coro *Coro__State_or_hashref; |
195 | typedef struct coro *Coro__State_or_hashref; |
185 | |
196 | |
|
|
197 | /** Coro ********************************************************************/ |
|
|
198 | |
|
|
199 | #define PRIO_MAX 3 |
|
|
200 | #define PRIO_HIGH 1 |
|
|
201 | #define PRIO_NORMAL 0 |
|
|
202 | #define PRIO_LOW -1 |
|
|
203 | #define PRIO_IDLE -3 |
|
|
204 | #define PRIO_MIN -4 |
|
|
205 | |
|
|
206 | /* for Coro.pm */ |
|
|
207 | static SV *coro_current; |
|
|
208 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
|
|
209 | static int coro_nready; |
|
|
210 | |
|
|
211 | /** lowlevel stuff **********************************************************/ |
|
|
212 | |
186 | static AV * |
213 | static AV * |
187 | coro_clone_padlist (CV *cv) |
214 | coro_clone_padlist (pTHX_ CV *cv) |
188 | { |
215 | { |
189 | AV *padlist = CvPADLIST (cv); |
216 | AV *padlist = CvPADLIST (cv); |
190 | AV *newpadlist, *newpad; |
217 | AV *newpadlist, *newpad; |
191 | |
218 | |
192 | newpadlist = newAV (); |
219 | newpadlist = newAV (); |
… | |
… | |
204 | |
231 | |
205 | return newpadlist; |
232 | return newpadlist; |
206 | } |
233 | } |
207 | |
234 | |
208 | static void |
235 | static void |
209 | free_padlist (AV *padlist) |
236 | free_padlist (pTHX_ AV *padlist) |
210 | { |
237 | { |
211 | /* may be during global destruction */ |
238 | /* may be during global destruction */ |
212 | if (SvREFCNT (padlist)) |
239 | if (SvREFCNT (padlist)) |
213 | { |
240 | { |
214 | I32 i = AvFILLp (padlist); |
241 | I32 i = AvFILLp (padlist); |
… | |
… | |
235 | AV *padlist; |
262 | AV *padlist; |
236 | AV *av = (AV *)mg->mg_obj; |
263 | AV *av = (AV *)mg->mg_obj; |
237 | |
264 | |
238 | /* casting is fun. */ |
265 | /* casting is fun. */ |
239 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
266 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
240 | free_padlist (padlist); |
267 | free_padlist (aTHX_ padlist); |
241 | |
268 | |
242 | SvREFCNT_dec (av); |
269 | SvREFCNT_dec (av); |
243 | |
270 | |
244 | return 0; |
271 | return 0; |
245 | } |
272 | } |
… | |
… | |
255 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
282 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
256 | : 0 |
283 | : 0 |
257 | |
284 | |
258 | /* the next two functions merely cache the padlists */ |
285 | /* the next two functions merely cache the padlists */ |
259 | static void |
286 | static void |
260 | get_padlist (CV *cv) |
287 | get_padlist (pTHX_ CV *cv) |
261 | { |
288 | { |
262 | MAGIC *mg = CORO_MAGIC (cv); |
289 | MAGIC *mg = CORO_MAGIC (cv); |
263 | AV *av; |
290 | AV *av; |
264 | |
291 | |
265 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
292 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
266 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
293 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
267 | else |
294 | else |
268 | { |
295 | { |
269 | #if PREFER_PERL_FUNCTIONS |
296 | #if CORO_PREFER_PERL_FUNCTIONS |
270 | /* this is probably cleaner, but also slower? */ |
297 | /* this is probably cleaner, but also slower? */ |
271 | CV *cp = Perl_cv_clone (cv); |
298 | CV *cp = Perl_cv_clone (cv); |
272 | CvPADLIST (cv) = CvPADLIST (cp); |
299 | CvPADLIST (cv) = CvPADLIST (cp); |
273 | CvPADLIST (cp) = 0; |
300 | CvPADLIST (cp) = 0; |
274 | SvREFCNT_dec (cp); |
301 | SvREFCNT_dec (cp); |
275 | #else |
302 | #else |
276 | CvPADLIST (cv) = coro_clone_padlist (cv); |
303 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
277 | #endif |
304 | #endif |
278 | } |
305 | } |
279 | } |
306 | } |
280 | |
307 | |
281 | static void |
308 | static void |
282 | put_padlist (CV *cv) |
309 | put_padlist (pTHX_ CV *cv) |
283 | { |
310 | { |
284 | MAGIC *mg = CORO_MAGIC (cv); |
311 | MAGIC *mg = CORO_MAGIC (cv); |
285 | AV *av; |
312 | AV *av; |
286 | |
313 | |
287 | if (!mg) |
314 | if (!mg) |
… | |
… | |
298 | av_extend (av, AvMAX (av) + 1); |
325 | av_extend (av, AvMAX (av) + 1); |
299 | |
326 | |
300 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
327 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
301 | } |
328 | } |
302 | |
329 | |
|
|
330 | /** load & save, init *******************************************************/ |
|
|
331 | |
303 | #define SB do { |
332 | #define SB do { |
304 | #define SE } while (0) |
333 | #define SE } while (0) |
305 | |
334 | |
306 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
335 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
307 | |
336 | |
308 | static void |
337 | static void |
309 | load_perl (Coro__State c) |
338 | load_perl (pTHX_ Coro__State c) |
310 | { |
339 | { |
311 | #define VAR(name,type) PL_ ## name = c->name; |
340 | #define VAR(name,type) PL_ ## name = c->name; |
312 | # include "state.h" |
341 | # include "state.h" |
313 | #undef VAR |
342 | #undef VAR |
314 | |
343 | |
315 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
344 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
316 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
345 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
317 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
346 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
|
|
347 | if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh); |
|
|
348 | |
318 | if (c->irssv) |
349 | if (c->irssv) |
319 | { |
350 | { |
320 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
351 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
|
|
352 | { |
321 | SvREFCNT_dec (c->irssv); |
353 | SvREFCNT_dec (c->irssv); |
|
|
354 | c->irssv = 0; |
|
|
355 | } |
322 | else |
356 | else |
323 | { |
357 | { |
324 | REPLACE_SV (PL_rs, c->irssv); |
358 | REPLACE_SV (PL_rs, c->irssv); |
325 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
359 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
326 | sv_setsv (c->irssv_sv, PL_rs); |
360 | sv_setsv (c->irssv_sv, PL_rs); |
… | |
… | |
332 | CV *cv; |
366 | CV *cv; |
333 | |
367 | |
334 | /* now do the ugly restore mess */ |
368 | /* now do the ugly restore mess */ |
335 | while ((cv = (CV *)POPs)) |
369 | while ((cv = (CV *)POPs)) |
336 | { |
370 | { |
337 | put_padlist (cv); /* mark this padlist as available */ |
371 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
338 | CvDEPTH (cv) = PTR2IV (POPs); |
372 | CvDEPTH (cv) = PTR2IV (POPs); |
339 | CvPADLIST (cv) = (AV *)POPs; |
373 | CvPADLIST (cv) = (AV *)POPs; |
340 | } |
374 | } |
341 | |
375 | |
342 | PUTBACK; |
376 | PUTBACK; |
343 | } |
377 | } |
|
|
378 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
344 | } |
379 | } |
345 | |
380 | |
346 | static void |
381 | static void |
347 | save_perl (Coro__State c) |
382 | save_perl (pTHX_ Coro__State c) |
348 | { |
383 | { |
|
|
384 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
349 | { |
385 | { |
350 | dSP; |
386 | dSP; |
351 | I32 cxix = cxstack_ix; |
387 | I32 cxix = cxstack_ix; |
352 | PERL_CONTEXT *ccstk = cxstack; |
388 | PERL_CONTEXT *ccstk = cxstack; |
353 | PERL_SI *top_si = PL_curstackinfo; |
389 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
376 | PUSHs ((SV *)CvPADLIST (cv)); |
412 | PUSHs ((SV *)CvPADLIST (cv)); |
377 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
413 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
378 | PUSHs ((SV *)cv); |
414 | PUSHs ((SV *)cv); |
379 | |
415 | |
380 | CvDEPTH (cv) = 0; |
416 | CvDEPTH (cv) = 0; |
381 | get_padlist (cv); |
417 | get_padlist (aTHX_ cv); |
382 | } |
418 | } |
383 | } |
419 | } |
384 | } |
420 | } |
385 | |
421 | |
386 | if (top_si->si_type == PERLSI_MAIN) |
422 | if (top_si->si_type == PERLSI_MAIN) |
… | |
… | |
395 | } |
431 | } |
396 | |
432 | |
397 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
433 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
398 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
434 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
399 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
435 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
436 | c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0; |
400 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
437 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
401 | |
438 | |
402 | #define VAR(name,type)c->name = PL_ ## name; |
439 | #define VAR(name,type)c->name = PL_ ## name; |
403 | # include "state.h" |
440 | # include "state.h" |
404 | #undef VAR |
441 | #undef VAR |
… | |
… | |
408 | * allocate various perl stacks. This is an exact copy |
445 | * allocate various perl stacks. This is an exact copy |
409 | * of perl.c:init_stacks, except that it uses less memory |
446 | * of perl.c:init_stacks, except that it uses less memory |
410 | * on the (sometimes correct) assumption that coroutines do |
447 | * on the (sometimes correct) assumption that coroutines do |
411 | * not usually need a lot of stackspace. |
448 | * not usually need a lot of stackspace. |
412 | */ |
449 | */ |
413 | #if PREFER_PERL_FUNCTIONS |
450 | #if CORO_PREFER_PERL_FUNCTIONS |
414 | # define coro_init_stacks init_stacks |
451 | # define coro_init_stacks init_stacks |
415 | #else |
452 | #else |
416 | static void |
453 | static void |
417 | coro_init_stacks () |
454 | coro_init_stacks (pTHX) |
418 | { |
455 | { |
419 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
456 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
420 | PL_curstackinfo->si_type = PERLSI_MAIN; |
457 | PL_curstackinfo->si_type = PERLSI_MAIN; |
421 | PL_curstack = PL_curstackinfo->si_stack; |
458 | PL_curstack = PL_curstackinfo->si_stack; |
422 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
459 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
… | |
… | |
456 | |
493 | |
457 | /* |
494 | /* |
458 | * destroy the stacks, the callchain etc... |
495 | * destroy the stacks, the callchain etc... |
459 | */ |
496 | */ |
460 | static void |
497 | static void |
461 | coro_destroy_stacks () |
498 | coro_destroy_stacks (pTHX) |
462 | { |
499 | { |
463 | if (!IN_DESTRUCT) |
500 | if (!IN_DESTRUCT) |
464 | { |
501 | { |
465 | /* restore all saved variables and stuff */ |
502 | /* restore all saved variables and stuff */ |
466 | LEAVE_SCOPE (0); |
503 | LEAVE_SCOPE (0); |
… | |
… | |
468 | |
505 | |
469 | /* free all temporaries */ |
506 | /* free all temporaries */ |
470 | FREETMPS; |
507 | FREETMPS; |
471 | assert (PL_tmps_ix == -1); |
508 | assert (PL_tmps_ix == -1); |
472 | |
509 | |
|
|
510 | /* unwind all extra stacks */ |
473 | POPSTACK_TO (PL_mainstack); |
511 | POPSTACK_TO (PL_mainstack); |
|
|
512 | |
|
|
513 | /* unwind main stack */ |
|
|
514 | dounwind (-1); |
474 | } |
515 | } |
475 | |
516 | |
476 | while (PL_curstackinfo->si_next) |
517 | while (PL_curstackinfo->si_next) |
477 | PL_curstackinfo = PL_curstackinfo->si_next; |
518 | PL_curstackinfo = PL_curstackinfo->si_next; |
478 | |
519 | |
… | |
… | |
495 | #if !PERL_VERSION_ATLEAST (5,9,0) |
536 | #if !PERL_VERSION_ATLEAST (5,9,0) |
496 | Safefree (PL_retstack); |
537 | Safefree (PL_retstack); |
497 | #endif |
538 | #endif |
498 | } |
539 | } |
499 | |
540 | |
|
|
541 | /** coroutine stack handling ************************************************/ |
|
|
542 | |
500 | static void |
543 | static void |
501 | setup_coro (struct coro *coro) |
544 | setup_coro (pTHX_ struct coro *coro) |
502 | { |
545 | { |
503 | /* |
546 | /* |
504 | * emulate part of the perl startup here. |
547 | * emulate part of the perl startup here. |
505 | */ |
548 | */ |
506 | |
|
|
507 | coro_init_stacks (); |
549 | coro_init_stacks (aTHX); |
508 | |
550 | |
509 | PL_curcop = &PL_compiling; |
551 | PL_curcop = &PL_compiling; |
510 | PL_in_eval = EVAL_NULL; |
552 | PL_in_eval = EVAL_NULL; |
|
|
553 | PL_comppad = 0; |
511 | PL_curpm = 0; |
554 | PL_curpm = 0; |
512 | PL_localizing = 0; |
555 | PL_localizing = 0; |
513 | PL_dirty = 0; |
556 | PL_dirty = 0; |
514 | PL_restartop = 0; |
557 | PL_restartop = 0; |
515 | |
558 | |
… | |
… | |
534 | |
577 | |
535 | ENTER; /* necessary e.g. for dounwind */ |
578 | ENTER; /* necessary e.g. for dounwind */ |
536 | } |
579 | } |
537 | |
580 | |
538 | static void |
581 | static void |
539 | free_coro_mortal () |
582 | free_coro_mortal (pTHX) |
540 | { |
583 | { |
541 | if (coro_mortal) |
584 | if (coro_mortal) |
542 | { |
585 | { |
543 | SvREFCNT_dec (coro_mortal); |
586 | SvREFCNT_dec (coro_mortal); |
544 | coro_mortal = 0; |
587 | coro_mortal = 0; |
545 | } |
588 | } |
546 | } |
589 | } |
547 | |
590 | |
548 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
591 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
549 | static void NOINLINE |
592 | static void NOINLINE |
550 | prepare_cctx (coro_cctx *cctx) |
593 | prepare_cctx (pTHX_ coro_cctx *cctx) |
551 | { |
594 | { |
552 | dSP; |
595 | dSP; |
553 | LOGOP myop; |
596 | LOGOP myop; |
554 | |
597 | |
555 | Zero (&myop, 1, LOGOP); |
598 | Zero (&myop, 1, LOGOP); |
… | |
… | |
567 | } |
610 | } |
568 | |
611 | |
569 | static void |
612 | static void |
570 | coro_run (void *arg) |
613 | coro_run (void *arg) |
571 | { |
614 | { |
|
|
615 | dTHX; |
|
|
616 | |
572 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
617 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
573 | UNLOCK; |
618 | UNLOCK; |
574 | |
619 | |
575 | /* |
620 | /* |
576 | * this is a _very_ stripped down perl interpreter ;) |
621 | * this is a _very_ stripped down perl interpreter ;) |
577 | */ |
622 | */ |
578 | PL_top_env = &PL_start_env; |
623 | PL_top_env = &PL_start_env; |
579 | |
624 | |
580 | /* inject call to cctx_init */ |
625 | /* inject call to cctx_init */ |
581 | prepare_cctx ((coro_cctx *)arg); |
626 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
582 | |
627 | |
583 | /* somebody will hit me for both perl_run and PL_restartop */ |
628 | /* somebody will hit me for both perl_run and PL_restartop */ |
584 | PL_restartop = PL_op; |
629 | PL_restartop = PL_op; |
585 | perl_run (PL_curinterp); |
630 | perl_run (PL_curinterp); |
586 | |
631 | |
… | |
… | |
590 | |
635 | |
591 | static coro_cctx * |
636 | static coro_cctx * |
592 | cctx_new () |
637 | cctx_new () |
593 | { |
638 | { |
594 | coro_cctx *cctx; |
639 | coro_cctx *cctx; |
|
|
640 | void *stack_start; |
|
|
641 | size_t stack_size; |
595 | |
642 | |
596 | ++cctx_count; |
643 | ++cctx_count; |
597 | |
644 | |
598 | Newz (0, cctx, 1, coro_cctx); |
645 | Newz (0, cctx, 1, coro_cctx); |
599 | |
646 | |
600 | #if HAVE_MMAP |
647 | #if HAVE_MMAP |
601 | |
648 | |
602 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
649 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
603 | /* mmap supposedly does allocate-on-write for us */ |
650 | /* mmap supposedly does allocate-on-write for us */ |
604 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
651 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
605 | |
652 | |
606 | if (cctx->sptr == (void *)-1) |
653 | if (cctx->sptr != (void *)-1) |
607 | { |
|
|
608 | perror ("FATAL: unable to mmap stack for coroutine"); |
|
|
609 | _exit (EXIT_FAILURE); |
|
|
610 | } |
654 | { |
611 | |
|
|
612 | # if STACKGUARD |
655 | # if CORO_STACKGUARD |
613 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
656 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
614 | # endif |
657 | # endif |
|
|
658 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
|
|
659 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
|
|
660 | cctx->mapped = 1; |
|
|
661 | } |
|
|
662 | else |
|
|
663 | #endif |
|
|
664 | { |
|
|
665 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
|
|
666 | New (0, cctx->sptr, coro_stacksize, long); |
615 | |
667 | |
616 | #else |
|
|
617 | |
|
|
618 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
|
|
619 | New (0, cctx->sptr, STACKSIZE, long); |
|
|
620 | |
|
|
621 | if (!cctx->sptr) |
668 | if (!cctx->sptr) |
622 | { |
669 | { |
623 | perror ("FATAL: unable to malloc stack for coroutine"); |
670 | perror ("FATAL: unable to allocate stack for coroutine"); |
624 | _exit (EXIT_FAILURE); |
671 | _exit (EXIT_FAILURE); |
625 | } |
672 | } |
626 | |
673 | |
627 | #endif |
674 | stack_start = cctx->sptr; |
|
|
675 | stack_size = cctx->ssize; |
|
|
676 | } |
628 | |
677 | |
629 | #if USE_VALGRIND |
678 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
630 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
631 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
|
|
632 | cctx->ssize + (char *)cctx->sptr |
|
|
633 | ); |
|
|
634 | #endif |
|
|
635 | |
|
|
636 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
679 | coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); |
637 | |
680 | |
638 | return cctx; |
681 | return cctx; |
639 | } |
682 | } |
640 | |
683 | |
641 | static void |
684 | static void |
… | |
… | |
644 | if (!cctx) |
687 | if (!cctx) |
645 | return; |
688 | return; |
646 | |
689 | |
647 | --cctx_count; |
690 | --cctx_count; |
648 | |
691 | |
649 | #if USE_VALGRIND |
692 | #if CORO_USE_VALGRIND |
650 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
693 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
651 | #endif |
694 | #endif |
652 | |
695 | |
653 | #if HAVE_MMAP |
696 | #if HAVE_MMAP |
|
|
697 | if (cctx->mapped) |
654 | munmap (cctx->sptr, cctx->ssize); |
698 | munmap (cctx->sptr, cctx->ssize); |
655 | #else |
699 | else |
|
|
700 | #endif |
656 | Safefree (cctx->sptr); |
701 | Safefree (cctx->sptr); |
657 | #endif |
|
|
658 | |
702 | |
659 | Safefree (cctx); |
703 | Safefree (cctx); |
660 | } |
704 | } |
661 | |
705 | |
662 | static coro_cctx * |
706 | static coro_cctx * |
663 | cctx_get () |
707 | cctx_get (pTHX) |
664 | { |
708 | { |
665 | coro_cctx *cctx; |
|
|
666 | |
|
|
667 | if (cctx_first) |
709 | while (cctx_first) |
668 | { |
710 | { |
669 | cctx = cctx_first; |
711 | coro_cctx *cctx = cctx_first; |
670 | cctx_first = cctx->next; |
712 | cctx_first = cctx->next; |
671 | --cctx_idle; |
713 | --cctx_idle; |
|
|
714 | |
|
|
715 | if (cctx->ssize >= coro_stacksize) |
|
|
716 | return cctx; |
|
|
717 | |
|
|
718 | cctx_destroy (cctx); |
672 | } |
719 | } |
673 | else |
720 | |
674 | { |
|
|
675 | cctx = cctx_new (); |
|
|
676 | PL_op = PL_op->op_next; |
721 | PL_op = PL_op->op_next; |
677 | } |
|
|
678 | |
|
|
679 | return cctx; |
722 | return cctx_new (); |
680 | } |
723 | } |
681 | |
724 | |
682 | static void |
725 | static void |
683 | cctx_put (coro_cctx *cctx) |
726 | cctx_put (coro_cctx *cctx) |
684 | { |
727 | { |
… | |
… | |
696 | ++cctx_idle; |
739 | ++cctx_idle; |
697 | cctx->next = cctx_first; |
740 | cctx->next = cctx_first; |
698 | cctx_first = cctx; |
741 | cctx_first = cctx; |
699 | } |
742 | } |
700 | |
743 | |
|
|
744 | /** coroutine switching *****************************************************/ |
|
|
745 | |
701 | /* never call directly, always through the coro_state_transfer global variable */ |
746 | /* never call directly, always through the coro_state_transfer global variable */ |
702 | static void NOINLINE |
747 | static void NOINLINE |
703 | transfer (struct coro *prev, struct coro *next) |
748 | transfer (pTHX_ struct coro *prev, struct coro *next) |
704 | { |
749 | { |
705 | dSTACKLEVEL; |
750 | dSTACKLEVEL; |
706 | |
751 | |
707 | /* sometimes transfer is only called to set idle_sp */ |
752 | /* sometimes transfer is only called to set idle_sp */ |
708 | if (!next) |
753 | if (!next) |
709 | { |
754 | { |
710 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
755 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
711 | assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */ |
756 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
712 | } |
757 | } |
713 | else if (prev != next) |
758 | else if (prev != next) |
714 | { |
759 | { |
715 | coro_cctx *prev__cctx; |
760 | coro_cctx *prev__cctx; |
716 | |
761 | |
… | |
… | |
728 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
773 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
729 | |
774 | |
730 | if (next->flags & CF_RUNNING) |
775 | if (next->flags & CF_RUNNING) |
731 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
776 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
732 | |
777 | |
|
|
778 | if (next->flags & CF_DESTROYED) |
|
|
779 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
780 | |
733 | prev->flags &= ~CF_RUNNING; |
781 | prev->flags &= ~CF_RUNNING; |
734 | next->flags |= CF_RUNNING; |
782 | next->flags |= CF_RUNNING; |
735 | |
783 | |
736 | LOCK; |
784 | LOCK; |
737 | |
785 | |
738 | if (next->flags & CF_NEW) |
786 | if (next->flags & CF_NEW) |
739 | { |
787 | { |
740 | /* need to start coroutine */ |
788 | /* need to start coroutine */ |
741 | next->flags &= ~CF_NEW; |
789 | next->flags &= ~CF_NEW; |
742 | /* first get rid of the old state */ |
790 | /* first get rid of the old state */ |
743 | save_perl (prev); |
791 | save_perl (aTHX_ prev); |
744 | /* setup coroutine call */ |
792 | /* setup coroutine call */ |
745 | setup_coro (next); |
793 | setup_coro (aTHX_ next); |
746 | /* need a new stack */ |
794 | /* need a new stack */ |
747 | assert (!next->cctx); |
795 | assert (!next->cctx); |
748 | } |
796 | } |
749 | else |
797 | else |
750 | { |
798 | { |
751 | /* coroutine already started */ |
799 | /* coroutine already started */ |
752 | save_perl (prev); |
800 | save_perl (aTHX_ prev); |
753 | load_perl (next); |
801 | load_perl (aTHX_ next); |
754 | } |
802 | } |
755 | |
803 | |
756 | prev__cctx = prev->cctx; |
804 | prev__cctx = prev->cctx; |
757 | |
805 | |
758 | /* possibly "free" the cctx */ |
806 | /* possibly "free" the cctx */ |
759 | if (prev__cctx->idle_sp == STACKLEVEL) |
807 | if (prev__cctx->idle_sp == STACKLEVEL) |
760 | { |
808 | { |
761 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
809 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
762 | assert (PL_top_env == prev__cctx->top_env); |
810 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
763 | |
811 | |
764 | prev->cctx = 0; |
812 | prev->cctx = 0; |
765 | |
813 | |
766 | cctx_put (prev__cctx); |
814 | cctx_put (prev__cctx); |
767 | prev__cctx->inuse = 0; |
815 | prev__cctx->inuse = 0; |
768 | } |
816 | } |
769 | |
817 | |
770 | if (!next->cctx) |
818 | if (!next->cctx) |
771 | { |
819 | { |
772 | next->cctx = cctx_get (); |
820 | next->cctx = cctx_get (aTHX); |
773 | assert (!next->cctx->inuse); |
821 | assert (!next->cctx->inuse); |
774 | next->cctx->inuse = 1; |
822 | next->cctx->inuse = 1; |
775 | } |
823 | } |
776 | |
824 | |
777 | if (prev__cctx != next->cctx) |
825 | if (prev__cctx != next->cctx) |
… | |
… | |
779 | prev__cctx->top_env = PL_top_env; |
827 | prev__cctx->top_env = PL_top_env; |
780 | PL_top_env = next->cctx->top_env; |
828 | PL_top_env = next->cctx->top_env; |
781 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
829 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
782 | } |
830 | } |
783 | |
831 | |
784 | free_coro_mortal (); |
832 | free_coro_mortal (aTHX); |
785 | |
|
|
786 | UNLOCK; |
833 | UNLOCK; |
787 | } |
834 | } |
788 | } |
835 | } |
789 | |
836 | |
790 | struct transfer_args |
837 | struct transfer_args |
791 | { |
838 | { |
792 | struct coro *prev, *next; |
839 | struct coro *prev, *next; |
793 | }; |
840 | }; |
794 | |
841 | |
795 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
842 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
796 | |
843 | |
|
|
844 | /** high level stuff ********************************************************/ |
|
|
845 | |
797 | static void |
846 | static int |
798 | coro_state_destroy (struct coro *coro) |
847 | coro_state_destroy (pTHX_ struct coro *coro) |
799 | { |
848 | { |
800 | if (coro->refcnt--) |
849 | if (coro->flags & CF_DESTROYED) |
801 | return; |
850 | return 0; |
|
|
851 | |
|
|
852 | coro->flags |= CF_DESTROYED; |
|
|
853 | |
|
|
854 | if (coro->flags & CF_READY) |
|
|
855 | { |
|
|
856 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
|
|
857 | /* alternative: look through all ready queues and remove the coro */ |
|
|
858 | LOCK; |
|
|
859 | --coro_nready; |
|
|
860 | UNLOCK; |
|
|
861 | } |
|
|
862 | else |
|
|
863 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
802 | |
864 | |
803 | if (coro->mainstack && coro->mainstack != main_mainstack) |
865 | if (coro->mainstack && coro->mainstack != main_mainstack) |
804 | { |
866 | { |
805 | struct coro temp; |
867 | struct coro temp; |
|
|
868 | |
|
|
869 | assert (!(coro->flags & CF_RUNNING)); |
|
|
870 | |
806 | Zero (&temp, 1, struct coro); |
871 | Zero (&temp, 1, struct coro); |
807 | temp.save = CORO_SAVE_ALL; |
872 | temp.save = CORO_SAVE_DEF; |
808 | |
873 | |
809 | if (coro->flags & CF_RUNNING) |
874 | if (coro->flags & CF_RUNNING) |
810 | croak ("FATAL: tried to destroy currently running coroutine"); |
875 | croak ("FATAL: tried to destroy currently running coroutine"); |
811 | |
876 | |
812 | save_perl (&temp); |
877 | save_perl (aTHX_ &temp); |
813 | load_perl (coro); |
878 | load_perl (aTHX_ coro); |
814 | |
879 | |
815 | coro_destroy_stacks (); |
880 | coro_destroy_stacks (aTHX); |
816 | |
881 | |
817 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
882 | load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ |
818 | |
883 | |
819 | coro->mainstack = 0; |
884 | coro->mainstack = 0; |
820 | } |
885 | } |
821 | |
886 | |
822 | cctx_destroy (coro->cctx); |
887 | cctx_destroy (coro->cctx); |
823 | SvREFCNT_dec (coro->args); |
888 | SvREFCNT_dec (coro->args); |
824 | Safefree (coro); |
889 | |
|
|
890 | return 1; |
825 | } |
891 | } |
826 | |
892 | |
827 | static int |
893 | static int |
828 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
894 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
829 | { |
895 | { |
830 | struct coro *coro = (struct coro *)mg->mg_ptr; |
896 | struct coro *coro = (struct coro *)mg->mg_ptr; |
831 | mg->mg_ptr = 0; |
897 | mg->mg_ptr = 0; |
832 | |
898 | |
|
|
899 | if (--coro->refcnt < 0) |
|
|
900 | { |
833 | coro_state_destroy (coro); |
901 | coro_state_destroy (aTHX_ coro); |
|
|
902 | Safefree (coro); |
|
|
903 | } |
834 | |
904 | |
835 | return 0; |
905 | return 0; |
836 | } |
906 | } |
837 | |
907 | |
838 | static int |
908 | static int |
… | |
… | |
845 | return 0; |
915 | return 0; |
846 | } |
916 | } |
847 | |
917 | |
848 | static MGVTBL coro_state_vtbl = { |
918 | static MGVTBL coro_state_vtbl = { |
849 | 0, 0, 0, 0, |
919 | 0, 0, 0, 0, |
850 | coro_state_clear, |
920 | coro_state_free, |
851 | 0, |
921 | 0, |
852 | #ifdef MGf_DUP |
922 | #ifdef MGf_DUP |
853 | coro_state_dup, |
923 | coro_state_dup, |
854 | #else |
924 | #else |
855 | # define MGf_DUP 0 |
925 | # define MGf_DUP 0 |
856 | #endif |
926 | #endif |
857 | }; |
927 | }; |
858 | |
928 | |
859 | static struct coro * |
929 | static struct coro * |
860 | SvSTATE (SV *coro) |
930 | SvSTATE_ (pTHX_ SV *coro) |
861 | { |
931 | { |
862 | HV *stash; |
932 | HV *stash; |
863 | MAGIC *mg; |
933 | MAGIC *mg; |
864 | |
934 | |
865 | if (SvROK (coro)) |
935 | if (SvROK (coro)) |
… | |
… | |
871 | /* very slow, but rare, check */ |
941 | /* very slow, but rare, check */ |
872 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
942 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
873 | croak ("Coro::State object required"); |
943 | croak ("Coro::State object required"); |
874 | } |
944 | } |
875 | |
945 | |
876 | mg = SvMAGIC (coro); |
946 | mg = CORO_MAGIC (coro); |
877 | assert (mg->mg_type == PERL_MAGIC_ext); |
|
|
878 | return (struct coro *)mg->mg_ptr; |
947 | return (struct coro *)mg->mg_ptr; |
879 | } |
948 | } |
880 | |
949 | |
|
|
950 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
951 | |
881 | static void |
952 | static void |
882 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
953 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
883 | { |
954 | { |
884 | ta->prev = SvSTATE (prev_sv); |
955 | ta->prev = SvSTATE (prev_sv); |
885 | ta->next = SvSTATE (next_sv); |
956 | ta->next = SvSTATE (next_sv); |
886 | } |
957 | } |
887 | |
958 | |
888 | static void |
959 | static void |
889 | api_transfer (SV *prev_sv, SV *next_sv) |
960 | api_transfer (SV *prev_sv, SV *next_sv) |
890 | { |
961 | { |
|
|
962 | dTHX; |
891 | struct transfer_args ta; |
963 | struct transfer_args ta; |
892 | |
964 | |
893 | prepare_transfer (&ta, prev_sv, next_sv); |
965 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
894 | TRANSFER (ta); |
966 | TRANSFER (ta); |
895 | } |
967 | } |
896 | |
968 | |
897 | static int |
969 | static int |
898 | api_save (SV *coro_sv, int new_save) |
970 | api_save (SV *coro_sv, int new_save) |
899 | { |
971 | { |
|
|
972 | dTHX; |
900 | struct coro *coro = SvSTATE (coro_sv); |
973 | struct coro *coro = SvSTATE (coro_sv); |
901 | int old_save = coro->save; |
974 | int old_save = coro->save; |
902 | |
975 | |
903 | if (new_save >= 0) |
976 | if (new_save >= 0) |
904 | coro->save = new_save; |
977 | coro->save = new_save; |
… | |
… | |
906 | return old_save; |
979 | return old_save; |
907 | } |
980 | } |
908 | |
981 | |
909 | /** Coro ********************************************************************/ |
982 | /** Coro ********************************************************************/ |
910 | |
983 | |
911 | #define PRIO_MAX 3 |
|
|
912 | #define PRIO_HIGH 1 |
|
|
913 | #define PRIO_NORMAL 0 |
|
|
914 | #define PRIO_LOW -1 |
|
|
915 | #define PRIO_IDLE -3 |
|
|
916 | #define PRIO_MIN -4 |
|
|
917 | |
|
|
918 | /* for Coro.pm */ |
|
|
919 | static SV *coro_current; |
|
|
920 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
|
|
921 | static int coro_nready; |
|
|
922 | |
|
|
923 | static void |
984 | static void |
924 | coro_enq (SV *coro_sv) |
985 | coro_enq (pTHX_ SV *coro_sv) |
925 | { |
986 | { |
926 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
987 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
927 | coro_nready++; |
|
|
928 | } |
988 | } |
929 | |
989 | |
930 | static SV * |
990 | static SV * |
931 | coro_deq (int min_prio) |
991 | coro_deq (pTHX_ int min_prio) |
932 | { |
992 | { |
933 | int prio = PRIO_MAX - PRIO_MIN; |
993 | int prio = PRIO_MAX - PRIO_MIN; |
934 | |
994 | |
935 | min_prio -= PRIO_MIN; |
995 | min_prio -= PRIO_MIN; |
936 | if (min_prio < 0) |
996 | if (min_prio < 0) |
937 | min_prio = 0; |
997 | min_prio = 0; |
938 | |
998 | |
939 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
999 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
940 | if (AvFILLp (coro_ready [prio]) >= 0) |
1000 | if (AvFILLp (coro_ready [prio]) >= 0) |
941 | { |
|
|
942 | coro_nready--; |
|
|
943 | return av_shift (coro_ready [prio]); |
1001 | return av_shift (coro_ready [prio]); |
944 | } |
|
|
945 | |
1002 | |
946 | return 0; |
1003 | return 0; |
947 | } |
1004 | } |
948 | |
1005 | |
949 | static int |
1006 | static int |
950 | api_ready (SV *coro_sv) |
1007 | api_ready (SV *coro_sv) |
951 | { |
1008 | { |
|
|
1009 | dTHX; |
952 | struct coro *coro; |
1010 | struct coro *coro; |
953 | |
1011 | |
954 | if (SvROK (coro_sv)) |
1012 | if (SvROK (coro_sv)) |
955 | coro_sv = SvRV (coro_sv); |
1013 | coro_sv = SvRV (coro_sv); |
956 | |
1014 | |
… | |
… | |
960 | return 0; |
1018 | return 0; |
961 | |
1019 | |
962 | coro->flags |= CF_READY; |
1020 | coro->flags |= CF_READY; |
963 | |
1021 | |
964 | LOCK; |
1022 | LOCK; |
965 | coro_enq (SvREFCNT_inc (coro_sv)); |
1023 | coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); |
|
|
1024 | ++coro_nready; |
966 | UNLOCK; |
1025 | UNLOCK; |
967 | |
1026 | |
968 | return 1; |
1027 | return 1; |
969 | } |
1028 | } |
970 | |
1029 | |
971 | static int |
1030 | static int |
972 | api_is_ready (SV *coro_sv) |
1031 | api_is_ready (SV *coro_sv) |
973 | { |
1032 | { |
|
|
1033 | dTHX; |
974 | return !!SvSTATE (coro_sv)->flags & CF_READY; |
1034 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
975 | } |
1035 | } |
976 | |
1036 | |
977 | static void |
1037 | static void |
978 | prepare_schedule (struct transfer_args *ta) |
1038 | prepare_schedule (pTHX_ struct transfer_args *ta) |
979 | { |
1039 | { |
980 | SV *prev, *next; |
1040 | SV *prev_sv, *next_sv; |
981 | |
1041 | |
982 | for (;;) |
1042 | for (;;) |
983 | { |
1043 | { |
984 | LOCK; |
1044 | LOCK; |
985 | next = coro_deq (PRIO_MIN); |
1045 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
|
|
1046 | |
|
|
1047 | /* nothing to schedule: call the idle handler */ |
|
|
1048 | if (!next_sv) |
|
|
1049 | { |
|
|
1050 | dSP; |
|
|
1051 | UNLOCK; |
|
|
1052 | |
|
|
1053 | ENTER; |
|
|
1054 | SAVETMPS; |
|
|
1055 | |
|
|
1056 | PUSHMARK (SP); |
|
|
1057 | PUTBACK; |
|
|
1058 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1059 | |
|
|
1060 | FREETMPS; |
|
|
1061 | LEAVE; |
|
|
1062 | continue; |
|
|
1063 | } |
|
|
1064 | |
|
|
1065 | ta->next = SvSTATE (next_sv); |
|
|
1066 | |
|
|
1067 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1068 | if (ta->next->flags & CF_DESTROYED) |
|
|
1069 | { |
|
|
1070 | UNLOCK; |
|
|
1071 | SvREFCNT_dec (next_sv); |
|
|
1072 | /* coro_nready is already taken care of by destroy */ |
|
|
1073 | continue; |
|
|
1074 | } |
|
|
1075 | |
|
|
1076 | --coro_nready; |
986 | UNLOCK; |
1077 | UNLOCK; |
987 | |
|
|
988 | if (next) |
|
|
989 | break; |
1078 | break; |
990 | |
|
|
991 | { |
|
|
992 | dSP; |
|
|
993 | |
|
|
994 | ENTER; |
|
|
995 | SAVETMPS; |
|
|
996 | |
|
|
997 | PUSHMARK (SP); |
|
|
998 | PUTBACK; |
|
|
999 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1000 | |
|
|
1001 | FREETMPS; |
|
|
1002 | LEAVE; |
|
|
1003 | } |
1079 | } |
1004 | } |
|
|
1005 | |
|
|
1006 | prev = SvRV (coro_current); |
|
|
1007 | SvRV_set (coro_current, next); |
|
|
1008 | |
1080 | |
1009 | /* free this only after the transfer */ |
1081 | /* free this only after the transfer */ |
1010 | LOCK; |
1082 | prev_sv = SvRV (coro_current); |
1011 | free_coro_mortal (); |
1083 | SvRV_set (coro_current, next_sv); |
1012 | UNLOCK; |
|
|
1013 | coro_mortal = prev; |
|
|
1014 | |
|
|
1015 | assert (!SvROK(prev));//D |
|
|
1016 | assert (!SvROK(next));//D |
|
|
1017 | |
|
|
1018 | ta->prev = SvSTATE (prev); |
1084 | ta->prev = SvSTATE (prev_sv); |
1019 | ta->next = SvSTATE (next); |
|
|
1020 | |
1085 | |
1021 | assert (ta->next->flags & CF_READY); |
1086 | assert (ta->next->flags & CF_READY); |
1022 | ta->next->flags &= ~CF_READY; |
1087 | ta->next->flags &= ~CF_READY; |
1023 | } |
|
|
1024 | |
1088 | |
|
|
1089 | LOCK; |
|
|
1090 | free_coro_mortal (aTHX); |
|
|
1091 | coro_mortal = prev_sv; |
|
|
1092 | UNLOCK; |
|
|
1093 | } |
|
|
1094 | |
1025 | static void |
1095 | static void |
1026 | prepare_cede (struct transfer_args *ta) |
1096 | prepare_cede (pTHX_ struct transfer_args *ta) |
1027 | { |
1097 | { |
1028 | api_ready (coro_current); |
1098 | api_ready (coro_current); |
1029 | |
|
|
1030 | prepare_schedule (ta); |
1099 | prepare_schedule (aTHX_ ta); |
|
|
1100 | } |
|
|
1101 | |
|
|
1102 | static int |
|
|
1103 | prepare_cede_notself (pTHX_ struct transfer_args *ta) |
|
|
1104 | { |
|
|
1105 | if (coro_nready) |
|
|
1106 | { |
|
|
1107 | SV *prev = SvRV (coro_current); |
|
|
1108 | prepare_schedule (aTHX_ ta); |
|
|
1109 | api_ready (prev); |
|
|
1110 | return 1; |
|
|
1111 | } |
|
|
1112 | else |
|
|
1113 | return 0; |
1031 | } |
1114 | } |
1032 | |
1115 | |
1033 | static void |
1116 | static void |
1034 | api_schedule (void) |
1117 | api_schedule (void) |
1035 | { |
1118 | { |
|
|
1119 | dTHX; |
1036 | struct transfer_args ta; |
1120 | struct transfer_args ta; |
1037 | |
1121 | |
1038 | prepare_schedule (&ta); |
1122 | prepare_schedule (aTHX_ &ta); |
1039 | TRANSFER (ta); |
1123 | TRANSFER (ta); |
1040 | } |
1124 | } |
1041 | |
1125 | |
1042 | static void |
1126 | static int |
1043 | api_cede (void) |
1127 | api_cede (void) |
1044 | { |
1128 | { |
|
|
1129 | dTHX; |
1045 | struct transfer_args ta; |
1130 | struct transfer_args ta; |
1046 | |
1131 | |
1047 | prepare_cede (&ta); |
1132 | prepare_cede (aTHX_ &ta); |
|
|
1133 | |
|
|
1134 | if (ta.prev != ta.next) |
|
|
1135 | { |
1048 | TRANSFER (ta); |
1136 | TRANSFER (ta); |
|
|
1137 | return 1; |
|
|
1138 | } |
|
|
1139 | else |
|
|
1140 | return 0; |
|
|
1141 | } |
|
|
1142 | |
|
|
1143 | static int |
|
|
1144 | api_cede_notself (void) |
|
|
1145 | { |
|
|
1146 | dTHX; |
|
|
1147 | struct transfer_args ta; |
|
|
1148 | |
|
|
1149 | if (prepare_cede_notself (aTHX_ &ta)) |
|
|
1150 | { |
|
|
1151 | TRANSFER (ta); |
|
|
1152 | return 1; |
|
|
1153 | } |
|
|
1154 | else |
|
|
1155 | return 0; |
1049 | } |
1156 | } |
1050 | |
1157 | |
1051 | MODULE = Coro::State PACKAGE = Coro::State |
1158 | MODULE = Coro::State PACKAGE = Coro::State |
1052 | |
1159 | |
1053 | PROTOTYPES: DISABLE |
1160 | PROTOTYPES: DISABLE |
… | |
… | |
1063 | |
1170 | |
1064 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1171 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1065 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1172 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1066 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1173 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1067 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
1174 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1175 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
|
|
1176 | newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); |
1068 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1177 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1069 | |
1178 | |
1070 | main_mainstack = PL_mainstack; |
1179 | main_mainstack = PL_mainstack; |
1071 | |
1180 | |
1072 | coroapi.ver = CORO_API_VERSION; |
1181 | coroapi.ver = CORO_API_VERSION; |
… | |
… | |
1083 | HV *hv; |
1192 | HV *hv; |
1084 | int i; |
1193 | int i; |
1085 | |
1194 | |
1086 | Newz (0, coro, 1, struct coro); |
1195 | Newz (0, coro, 1, struct coro); |
1087 | coro->args = newAV (); |
1196 | coro->args = newAV (); |
1088 | coro->save = CORO_SAVE_ALL; |
1197 | coro->save = CORO_SAVE_DEF; |
1089 | coro->flags = CF_NEW; |
1198 | coro->flags = CF_NEW; |
1090 | |
1199 | |
1091 | hv = newHV (); |
1200 | hv = newHV (); |
1092 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1201 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1093 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1202 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
… | |
… | |
1103 | CODE: |
1212 | CODE: |
1104 | RETVAL = api_save (coro, new_save); |
1213 | RETVAL = api_save (coro, new_save); |
1105 | OUTPUT: |
1214 | OUTPUT: |
1106 | RETVAL |
1215 | RETVAL |
1107 | |
1216 | |
|
|
1217 | int |
|
|
1218 | save_also (SV *coro_sv, int save_also) |
|
|
1219 | CODE: |
|
|
1220 | { |
|
|
1221 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1222 | RETVAL = coro->save; |
|
|
1223 | coro->save |= save_also; |
|
|
1224 | } |
|
|
1225 | OUTPUT: |
|
|
1226 | RETVAL |
|
|
1227 | |
1108 | void |
1228 | void |
1109 | _set_stacklevel (...) |
1229 | _set_stacklevel (...) |
1110 | ALIAS: |
1230 | ALIAS: |
1111 | Coro::State::transfer = 1 |
1231 | Coro::State::transfer = 1 |
1112 | Coro::schedule = 2 |
1232 | Coro::schedule = 2 |
1113 | Coro::cede = 3 |
1233 | Coro::cede = 3 |
|
|
1234 | Coro::cede_notself = 4 |
1114 | CODE: |
1235 | CODE: |
1115 | { |
1236 | { |
1116 | struct transfer_args ta; |
1237 | struct transfer_args ta; |
1117 | |
1238 | |
1118 | switch (ix) |
1239 | switch (ix) |
… | |
… | |
1124 | |
1245 | |
1125 | case 1: |
1246 | case 1: |
1126 | if (items != 2) |
1247 | if (items != 2) |
1127 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1248 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1128 | |
1249 | |
1129 | prepare_transfer (&ta, ST (0), ST (1)); |
1250 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1130 | break; |
1251 | break; |
1131 | |
1252 | |
1132 | case 2: |
1253 | case 2: |
1133 | prepare_schedule (&ta); |
1254 | prepare_schedule (aTHX_ &ta); |
1134 | break; |
1255 | break; |
1135 | |
1256 | |
1136 | case 3: |
1257 | case 3: |
1137 | prepare_cede (&ta); |
1258 | prepare_cede (aTHX_ &ta); |
|
|
1259 | break; |
|
|
1260 | |
|
|
1261 | case 4: |
|
|
1262 | if (!prepare_cede_notself (aTHX_ &ta)) |
|
|
1263 | XSRETURN_EMPTY; |
|
|
1264 | |
1138 | break; |
1265 | break; |
1139 | } |
1266 | } |
1140 | |
1267 | |
|
|
1268 | BARRIER; |
1141 | TRANSFER (ta); |
1269 | TRANSFER (ta); |
1142 | } |
|
|
1143 | |
1270 | |
1144 | void |
1271 | if (GIMME_V != G_VOID && ta.next != ta.prev) |
1145 | _clone_state_from (SV *dst, SV *src) |
1272 | XSRETURN_YES; |
|
|
1273 | } |
|
|
1274 | |
|
|
1275 | bool |
|
|
1276 | _destroy (SV *coro_sv) |
1146 | CODE: |
1277 | CODE: |
1147 | { |
1278 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
1148 | struct coro *coro_src = SvSTATE (src); |
1279 | OUTPUT: |
1149 | |
1280 | RETVAL |
1150 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
|
|
1151 | |
|
|
1152 | ++coro_src->refcnt; |
|
|
1153 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
|
|
1154 | } |
|
|
1155 | |
1281 | |
1156 | void |
1282 | void |
1157 | _exit (code) |
1283 | _exit (code) |
1158 | int code |
1284 | int code |
1159 | PROTOTYPE: $ |
1285 | PROTOTYPE: $ |
1160 | CODE: |
1286 | CODE: |
1161 | _exit (code); |
1287 | _exit (code); |
1162 | |
1288 | |
1163 | int |
1289 | int |
|
|
1290 | cctx_stacksize (int new_stacksize = 0) |
|
|
1291 | CODE: |
|
|
1292 | RETVAL = coro_stacksize; |
|
|
1293 | if (new_stacksize) |
|
|
1294 | coro_stacksize = new_stacksize; |
|
|
1295 | OUTPUT: |
|
|
1296 | RETVAL |
|
|
1297 | |
|
|
1298 | int |
1164 | cctx_count () |
1299 | cctx_count () |
1165 | CODE: |
1300 | CODE: |
1166 | RETVAL = cctx_count; |
1301 | RETVAL = cctx_count; |
1167 | OUTPUT: |
1302 | OUTPUT: |
1168 | RETVAL |
1303 | RETVAL |
… | |
… | |
1196 | coro_ready[i] = newAV (); |
1331 | coro_ready[i] = newAV (); |
1197 | |
1332 | |
1198 | { |
1333 | { |
1199 | SV *sv = perl_get_sv("Coro::API", 1); |
1334 | SV *sv = perl_get_sv("Coro::API", 1); |
1200 | |
1335 | |
1201 | coroapi.schedule = api_schedule; |
1336 | coroapi.schedule = api_schedule; |
1202 | coroapi.save = api_save; |
1337 | coroapi.save = api_save; |
1203 | coroapi.cede = api_cede; |
1338 | coroapi.cede = api_cede; |
|
|
1339 | coroapi.cede_notself = api_cede_notself; |
1204 | coroapi.ready = api_ready; |
1340 | coroapi.ready = api_ready; |
1205 | coroapi.is_ready = api_is_ready; |
1341 | coroapi.is_ready = api_is_ready; |
1206 | coroapi.nready = &coro_nready; |
1342 | coroapi.nready = &coro_nready; |
1207 | coroapi.current = coro_current; |
1343 | coroapi.current = coro_current; |
1208 | |
1344 | |
1209 | GCoroAPI = &coroapi; |
1345 | GCoroAPI = &coroapi; |
1210 | sv_setiv (sv, (IV)&coroapi); |
1346 | sv_setiv (sv, (IV)&coroapi); |
1211 | SvREADONLY_on (sv); |
1347 | SvREADONLY_on (sv); |
1212 | } |
1348 | } |
… | |
… | |
1236 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1372 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1237 | |
1373 | |
1238 | coro->prio = newprio; |
1374 | coro->prio = newprio; |
1239 | } |
1375 | } |
1240 | } |
1376 | } |
|
|
1377 | OUTPUT: |
|
|
1378 | RETVAL |
1241 | |
1379 | |
1242 | SV * |
1380 | SV * |
1243 | ready (SV *self) |
1381 | ready (SV *self) |
1244 | PROTOTYPE: $ |
1382 | PROTOTYPE: $ |
1245 | CODE: |
1383 | CODE: |