… | |
… | |
4 | #include "perl.h" |
4 | #include "perl.h" |
5 | #include "XSUB.h" |
5 | #include "XSUB.h" |
6 | |
6 | |
7 | #include "patchlevel.h" |
7 | #include "patchlevel.h" |
8 | |
8 | |
9 | #if USE_VALGRIND |
|
|
10 | # include <valgrind/valgrind.h> |
|
|
11 | #endif |
|
|
12 | |
|
|
13 | #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) |
|
|
20 | # ifndef PL_ppaddr |
|
|
21 | # define PL_ppaddr ppaddr |
|
|
22 | # endif |
|
|
23 | # ifndef call_sv |
|
|
24 | # define call_sv perl_call_sv |
|
|
25 | # endif |
|
|
26 | # ifndef get_sv |
|
|
27 | # define get_sv perl_get_sv |
|
|
28 | # endif |
|
|
29 | # ifndef get_cv |
|
|
30 | # define get_cv perl_get_cv |
|
|
31 | # endif |
|
|
32 | # ifndef IS_PADGV |
|
|
33 | # define IS_PADGV(v) 0 |
|
|
34 | # endif |
|
|
35 | # ifndef IS_PADCONST |
|
|
36 | # define IS_PADCONST(v) 0 |
|
|
37 | # endif |
|
|
38 | #endif |
|
|
39 | |
|
|
40 | #include <stdio.h> |
9 | #include <stdio.h> |
41 | #include <errno.h> |
10 | #include <errno.h> |
42 | |
11 | #include <assert.h> |
43 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
|
|
44 | # undef STACKGUARD |
|
|
45 | #endif |
|
|
46 | |
|
|
47 | #ifndef STACKGUARD |
|
|
48 | # define STACKGUARD 0 |
|
|
49 | #endif |
|
|
50 | |
12 | |
51 | #ifdef HAVE_MMAP |
13 | #ifdef HAVE_MMAP |
52 | # include <unistd.h> |
14 | # include <unistd.h> |
53 | # include <sys/mman.h> |
15 | # include <sys/mman.h> |
54 | # ifndef MAP_ANONYMOUS |
16 | # ifndef MAP_ANONYMOUS |
… | |
… | |
69 | #else |
31 | #else |
70 | # define PAGESIZE 0 |
32 | # define PAGESIZE 0 |
71 | # define BOOT_PAGESIZE (void)0 |
33 | # define BOOT_PAGESIZE (void)0 |
72 | #endif |
34 | #endif |
73 | |
35 | |
|
|
36 | #if USE_VALGRIND |
|
|
37 | # include <valgrind/valgrind.h> |
|
|
38 | # define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end)) |
|
|
39 | #else |
|
|
40 | # define REGISTER_STACK(cctx,start,end) |
|
|
41 | #endif |
|
|
42 | |
|
|
43 | /* the maximum number of idle cctx that will be pooled */ |
|
|
44 | #define MAX_IDLE_CCTX 8 |
|
|
45 | |
|
|
46 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
|
|
47 | (PERL_REVISION > (a) \ |
|
|
48 | || (PERL_REVISION == (a) \ |
|
|
49 | && (PERL_VERSION > (b) \ |
|
|
50 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
|
|
51 | |
|
|
52 | #if !PERL_VERSION_ATLEAST (5,6,0) |
|
|
53 | # ifndef PL_ppaddr |
|
|
54 | # define PL_ppaddr ppaddr |
|
|
55 | # endif |
|
|
56 | # ifndef call_sv |
|
|
57 | # define call_sv perl_call_sv |
|
|
58 | # endif |
|
|
59 | # ifndef get_sv |
|
|
60 | # define get_sv perl_get_sv |
|
|
61 | # endif |
|
|
62 | # ifndef get_cv |
|
|
63 | # define get_cv perl_get_cv |
|
|
64 | # endif |
|
|
65 | # ifndef IS_PADGV |
|
|
66 | # define IS_PADGV(v) 0 |
|
|
67 | # endif |
|
|
68 | # ifndef IS_PADCONST |
|
|
69 | # define IS_PADCONST(v) 0 |
|
|
70 | # endif |
|
|
71 | #endif |
|
|
72 | |
|
|
73 | /* 5.8.7 */ |
|
|
74 | #ifndef SvRV_set |
|
|
75 | # define SvRV_set(s,v) SvRV(s) = (v) |
|
|
76 | #endif |
|
|
77 | |
|
|
78 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
|
|
79 | # undef STACKGUARD |
|
|
80 | #endif |
|
|
81 | |
|
|
82 | #ifndef STACKGUARD |
|
|
83 | # define STACKGUARD 0 |
|
|
84 | #endif |
|
|
85 | |
|
|
86 | /* prefer perl internal functions over our own? */ |
|
|
87 | #ifndef PREFER_PERL_FUNCTIONS |
|
|
88 | # define PREFER_PERL_FUNCTIONS 0 |
|
|
89 | #endif |
|
|
90 | |
74 | /* The next macro should declare a variable stacklevel that contains and approximation |
91 | /* The next macro should declare a variable stacklevel that contains and approximation |
75 | * to the current C stack pointer. Its property is that it changes with each call |
92 | * to the current C stack pointer. Its property is that it changes with each call |
76 | * and should be unique. */ |
93 | * and should be unique. */ |
77 | #define dSTACKLEVEL int stacklevel |
94 | #define dSTACKLEVEL int stacklevel |
78 | #define STACKLEVEL ((void *)&stacklevel) |
95 | #define STACKLEVEL ((void *)&stacklevel) |
79 | |
96 | |
80 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
97 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
81 | |
98 | |
82 | #if __GNUC__ >= 3 |
99 | #if __GNUC__ >= 3 |
83 | # define attribute(x) __attribute__(x) |
100 | # define attribute(x) __attribute__(x) |
|
|
101 | # define BARRIER __asm__ __volatile__ ("" : : : "memory") |
84 | #else |
102 | #else |
85 | # define attribute(x) |
103 | # define attribute(x) |
|
|
104 | # define BARRIER |
86 | #endif |
105 | #endif |
87 | |
106 | |
88 | #define NOINLINE attribute ((noinline)) |
107 | #define NOINLINE attribute ((noinline)) |
89 | |
108 | |
90 | #include "CoroAPI.h" |
109 | #include "CoroAPI.h" |
91 | |
|
|
92 | #define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */ |
|
|
93 | |
110 | |
94 | #ifdef USE_ITHREADS |
111 | #ifdef USE_ITHREADS |
95 | static perl_mutex coro_mutex; |
112 | static perl_mutex coro_mutex; |
96 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
113 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
97 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
114 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
98 | #else |
115 | #else |
99 | # define LOCK (void)0 |
116 | # define LOCK (void)0 |
100 | # define UNLOCK (void)0 |
117 | # define UNLOCK (void)0 |
101 | #endif |
118 | #endif |
102 | |
119 | |
|
|
120 | /* helper storage struct for Coro::AIO */ |
|
|
121 | struct io_state |
|
|
122 | { |
|
|
123 | int errorno; |
|
|
124 | I32 laststype; |
|
|
125 | int laststatval; |
|
|
126 | Stat_t statcache; |
|
|
127 | }; |
|
|
128 | |
103 | static struct CoroAPI coroapi; |
129 | static struct CoroAPI coroapi; |
104 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
130 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
105 | static HV *coro_state_stash, *coro_stash; |
131 | static HV *coro_state_stash, *coro_stash; |
106 | static SV *coro_mortal; /* will be freed after next transfer */ |
132 | static SV *coro_mortal; /* will be freed after next transfer */ |
107 | |
133 | |
… | |
… | |
112 | typedef struct coro_cctx { |
138 | typedef struct coro_cctx { |
113 | struct coro_cctx *next; |
139 | struct coro_cctx *next; |
114 | |
140 | |
115 | /* the stack */ |
141 | /* the stack */ |
116 | void *sptr; |
142 | void *sptr; |
117 | long ssize; /* positive == mmap, otherwise malloc */ |
143 | ssize_t ssize; /* positive == mmap, otherwise malloc */ |
118 | |
144 | |
119 | /* cpu state */ |
145 | /* cpu state */ |
120 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
146 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
147 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
121 | JMPENV *top_env; |
148 | JMPENV *top_env; |
122 | coro_context cctx; |
149 | coro_context cctx; |
123 | |
150 | |
|
|
151 | int inuse; |
|
|
152 | |
124 | #if USE_VALGRIND |
153 | #if USE_VALGRIND |
125 | int valgrind_id; |
154 | int valgrind_id; |
126 | #endif |
155 | #endif |
127 | } coro_cctx; |
156 | } coro_cctx; |
|
|
157 | |
|
|
158 | enum { |
|
|
159 | CF_RUNNING = 0x0001, /* coroutine is running */ |
|
|
160 | CF_READY = 0x0002, /* coroutine is ready */ |
|
|
161 | CF_NEW = 0x0004, /* has never been switched to */ |
|
|
162 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
|
|
163 | }; |
128 | |
164 | |
129 | /* this is a structure representing a perl-level coroutine */ |
165 | /* this is a structure representing a perl-level coroutine */ |
130 | struct coro { |
166 | struct coro { |
131 | /* the c coroutine allocated to this perl coroutine, if any */ |
167 | /* the c coroutine allocated to this perl coroutine, if any */ |
132 | coro_cctx *cctx; |
168 | coro_cctx *cctx; |
133 | |
169 | |
134 | /* data associated with this coroutine (initial args) */ |
170 | /* data associated with this coroutine (initial args) */ |
135 | AV *args; |
171 | AV *args; |
136 | int refcnt; |
172 | int refcnt; |
|
|
173 | int save; /* CORO_SAVE flags */ |
|
|
174 | int flags; /* CF_ flags */ |
137 | |
175 | |
138 | /* optionally saved, might be zero */ |
176 | /* optionally saved, might be zero */ |
139 | AV *defav; |
177 | AV *defav; /* @_ */ |
140 | SV *defsv; |
178 | SV *defsv; /* $_ */ |
141 | SV *errsv; |
179 | SV *errsv; /* $@ */ |
|
|
180 | SV *irssv; /* $/ */ |
|
|
181 | SV *irssv_sv; /* real $/ cache */ |
142 | |
182 | |
143 | #define VAR(name,type) type name; |
183 | #define VAR(name,type) type name; |
144 | # include "state.h" |
184 | # include "state.h" |
145 | #undef VAR |
185 | #undef VAR |
146 | |
186 | |
… | |
… | |
148 | int prio; |
188 | int prio; |
149 | }; |
189 | }; |
150 | |
190 | |
151 | typedef struct coro *Coro__State; |
191 | typedef struct coro *Coro__State; |
152 | typedef struct coro *Coro__State_or_hashref; |
192 | typedef struct coro *Coro__State_or_hashref; |
|
|
193 | |
|
|
194 | /** Coro ********************************************************************/ |
|
|
195 | |
|
|
196 | #define PRIO_MAX 3 |
|
|
197 | #define PRIO_HIGH 1 |
|
|
198 | #define PRIO_NORMAL 0 |
|
|
199 | #define PRIO_LOW -1 |
|
|
200 | #define PRIO_IDLE -3 |
|
|
201 | #define PRIO_MIN -4 |
|
|
202 | |
|
|
203 | /* for Coro.pm */ |
|
|
204 | static SV *coro_current; |
|
|
205 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
|
|
206 | static int coro_nready; |
|
|
207 | |
|
|
208 | /** lowlevel stuff **********************************************************/ |
153 | |
209 | |
154 | static AV * |
210 | static AV * |
155 | coro_clone_padlist (CV *cv) |
211 | coro_clone_padlist (CV *cv) |
156 | { |
212 | { |
157 | AV *padlist = CvPADLIST (cv); |
213 | AV *padlist = CvPADLIST (cv); |
… | |
… | |
232 | |
288 | |
233 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
289 | if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) |
234 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
290 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
235 | else |
291 | else |
236 | { |
292 | { |
237 | #if 0 |
293 | #if PREFER_PERL_FUNCTIONS |
238 | /* this is probably cleaner, but also slower? */ |
294 | /* this is probably cleaner, but also slower? */ |
239 | CV *cp = Perl_cv_clone (cv); |
295 | CV *cp = Perl_cv_clone (cv); |
240 | CvPADLIST (cv) = CvPADLIST (cp); |
296 | CvPADLIST (cv) = CvPADLIST (cp); |
241 | CvPADLIST (cp) = 0; |
297 | CvPADLIST (cp) = 0; |
242 | SvREFCNT_dec (cp); |
298 | SvREFCNT_dec (cp); |
… | |
… | |
266 | av_extend (av, AvMAX (av) + 1); |
322 | av_extend (av, AvMAX (av) + 1); |
267 | |
323 | |
268 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
324 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
269 | } |
325 | } |
270 | |
326 | |
|
|
327 | /** load & save, init *******************************************************/ |
|
|
328 | |
271 | #define SB do { |
329 | #define SB do { |
272 | #define SE } while (0) |
330 | #define SE } while (0) |
273 | |
331 | |
274 | #define LOAD(state) load_state((state)); |
|
|
275 | #define SAVE(state,flags) save_state((state),(flags)); |
|
|
276 | |
|
|
277 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE |
332 | #define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE |
278 | |
333 | |
279 | static void |
334 | static void |
280 | load_state(Coro__State c) |
335 | load_perl (Coro__State c) |
281 | { |
336 | { |
282 | #define VAR(name,type) PL_ ## name = c->name; |
337 | #define VAR(name,type) PL_ ## name = c->name; |
283 | # include "state.h" |
338 | # include "state.h" |
284 | #undef VAR |
339 | #undef VAR |
285 | |
340 | |
286 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
341 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
287 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
342 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
288 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
343 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
|
|
344 | if (c->irssv) |
|
|
345 | { |
|
|
346 | if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) |
|
|
347 | SvREFCNT_dec (c->irssv); |
|
|
348 | else |
|
|
349 | { |
|
|
350 | REPLACE_SV (PL_rs, c->irssv); |
|
|
351 | if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); |
|
|
352 | sv_setsv (c->irssv_sv, PL_rs); |
|
|
353 | } |
|
|
354 | } |
289 | |
355 | |
290 | { |
356 | { |
291 | dSP; |
357 | dSP; |
292 | CV *cv; |
358 | CV *cv; |
293 | |
359 | |
… | |
… | |
302 | PUTBACK; |
368 | PUTBACK; |
303 | } |
369 | } |
304 | } |
370 | } |
305 | |
371 | |
306 | static void |
372 | static void |
307 | save_state(Coro__State c, int flags) |
373 | save_perl (Coro__State c) |
308 | { |
374 | { |
309 | { |
375 | { |
310 | dSP; |
376 | dSP; |
311 | I32 cxix = cxstack_ix; |
377 | I32 cxix = cxstack_ix; |
312 | PERL_CONTEXT *ccstk = cxstack; |
378 | PERL_CONTEXT *ccstk = cxstack; |
… | |
… | |
315 | /* |
381 | /* |
316 | * the worst thing you can imagine happens first - we have to save |
382 | * the worst thing you can imagine happens first - we have to save |
317 | * (and reinitialize) all cv's in the whole callchain :( |
383 | * (and reinitialize) all cv's in the whole callchain :( |
318 | */ |
384 | */ |
319 | |
385 | |
|
|
386 | EXTEND (SP, 3 + 1); |
320 | PUSHs (Nullsv); |
387 | PUSHs (Nullsv); |
321 | /* this loop was inspired by pp_caller */ |
388 | /* this loop was inspired by pp_caller */ |
322 | for (;;) |
389 | for (;;) |
323 | { |
390 | { |
324 | while (cxix >= 0) |
391 | while (cxix >= 0) |
325 | { |
392 | { |
326 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
393 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
327 | |
394 | |
328 | if (CxTYPE(cx) == CXt_SUB) |
395 | if (CxTYPE (cx) == CXt_SUB) |
329 | { |
396 | { |
330 | CV *cv = cx->blk_sub.cv; |
397 | CV *cv = cx->blk_sub.cv; |
331 | |
398 | |
332 | if (CvDEPTH (cv)) |
399 | if (CvDEPTH (cv)) |
333 | { |
400 | { |
334 | EXTEND (SP, 3); |
401 | EXTEND (SP, 3); |
335 | |
|
|
336 | PUSHs ((SV *)CvPADLIST(cv)); |
402 | PUSHs ((SV *)CvPADLIST (cv)); |
337 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
403 | PUSHs (INT2PTR (SV *, CvDEPTH (cv))); |
338 | PUSHs ((SV *)cv); |
404 | PUSHs ((SV *)cv); |
339 | |
405 | |
340 | CvDEPTH (cv) = 0; |
406 | CvDEPTH (cv) = 0; |
341 | get_padlist (cv); |
407 | get_padlist (cv); |
342 | } |
408 | } |
343 | } |
409 | } |
344 | #ifdef CXt_FORMAT |
|
|
345 | else if (CxTYPE(cx) == CXt_FORMAT) |
|
|
346 | { |
|
|
347 | /* I never used formats, so how should I know how these are implemented? */ |
|
|
348 | /* my bold guess is as a simple, plain sub... */ |
|
|
349 | croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); |
|
|
350 | } |
|
|
351 | #endif |
|
|
352 | } |
410 | } |
353 | |
411 | |
354 | if (top_si->si_type == PERLSI_MAIN) |
412 | if (top_si->si_type == PERLSI_MAIN) |
355 | break; |
413 | break; |
356 | |
414 | |
… | |
… | |
360 | } |
418 | } |
361 | |
419 | |
362 | PUTBACK; |
420 | PUTBACK; |
363 | } |
421 | } |
364 | |
422 | |
365 | c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
423 | c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; |
366 | c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
424 | c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; |
367 | c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
425 | c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; |
|
|
426 | c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; |
368 | |
427 | |
369 | #define VAR(name,type)c->name = PL_ ## name; |
428 | #define VAR(name,type)c->name = PL_ ## name; |
370 | # include "state.h" |
429 | # include "state.h" |
371 | #undef VAR |
430 | #undef VAR |
372 | } |
431 | } |
… | |
… | |
375 | * allocate various perl stacks. This is an exact copy |
434 | * allocate various perl stacks. This is an exact copy |
376 | * of perl.c:init_stacks, except that it uses less memory |
435 | * of perl.c:init_stacks, except that it uses less memory |
377 | * on the (sometimes correct) assumption that coroutines do |
436 | * on the (sometimes correct) assumption that coroutines do |
378 | * not usually need a lot of stackspace. |
437 | * not usually need a lot of stackspace. |
379 | */ |
438 | */ |
|
|
439 | #if PREFER_PERL_FUNCTIONS |
|
|
440 | # define coro_init_stacks init_stacks |
|
|
441 | #else |
380 | static void |
442 | static void |
381 | coro_init_stacks () |
443 | coro_init_stacks () |
382 | { |
444 | { |
383 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
445 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
384 | PL_curstackinfo->si_type = PERLSI_MAIN; |
446 | PL_curstackinfo->si_type = PERLSI_MAIN; |
385 | PL_curstack = PL_curstackinfo->si_stack; |
447 | PL_curstack = PL_curstackinfo->si_stack; |
386 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
448 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
387 | |
449 | |
388 | PL_stack_base = AvARRAY(PL_curstack); |
450 | PL_stack_base = AvARRAY(PL_curstack); |
389 | PL_stack_sp = PL_stack_base; |
451 | PL_stack_sp = PL_stack_base; |
390 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
452 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
391 | |
453 | |
392 | New(50,PL_tmps_stack,96,SV*); |
454 | New(50,PL_tmps_stack,128,SV*); |
393 | PL_tmps_floor = -1; |
455 | PL_tmps_floor = -1; |
394 | PL_tmps_ix = -1; |
456 | PL_tmps_ix = -1; |
395 | PL_tmps_max = 96; |
457 | PL_tmps_max = 128; |
396 | |
458 | |
397 | New(54,PL_markstack,16,I32); |
459 | New(54,PL_markstack,32,I32); |
398 | PL_markstack_ptr = PL_markstack; |
460 | PL_markstack_ptr = PL_markstack; |
399 | PL_markstack_max = PL_markstack + 16; |
461 | PL_markstack_max = PL_markstack + 32; |
400 | |
462 | |
401 | #ifdef SET_MARK_OFFSET |
463 | #ifdef SET_MARK_OFFSET |
402 | SET_MARK_OFFSET; |
464 | SET_MARK_OFFSET; |
403 | #endif |
465 | #endif |
404 | |
466 | |
405 | New(54,PL_scopestack,16,I32); |
467 | New(54,PL_scopestack,32,I32); |
406 | PL_scopestack_ix = 0; |
468 | PL_scopestack_ix = 0; |
407 | PL_scopestack_max = 16; |
469 | PL_scopestack_max = 32; |
408 | |
470 | |
409 | New(54,PL_savestack,96,ANY); |
471 | New(54,PL_savestack,64,ANY); |
410 | PL_savestack_ix = 0; |
472 | PL_savestack_ix = 0; |
411 | PL_savestack_max = 96; |
473 | PL_savestack_max = 64; |
412 | |
474 | |
413 | #if !PERL_VERSION_ATLEAST (5,9,0) |
475 | #if !PERL_VERSION_ATLEAST (5,9,0) |
414 | New(54,PL_retstack,8,OP*); |
476 | New(54,PL_retstack,16,OP*); |
415 | PL_retstack_ix = 0; |
477 | PL_retstack_ix = 0; |
416 | PL_retstack_max = 8; |
478 | PL_retstack_max = 16; |
417 | #endif |
479 | #endif |
418 | } |
480 | } |
|
|
481 | #endif |
419 | |
482 | |
420 | /* |
483 | /* |
421 | * destroy the stacks, the callchain etc... |
484 | * destroy the stacks, the callchain etc... |
422 | */ |
485 | */ |
423 | static void |
486 | static void |
424 | coro_destroy_stacks () |
487 | coro_destroy_stacks () |
425 | { |
488 | { |
426 | if (!IN_DESTRUCT) |
489 | if (!IN_DESTRUCT) |
427 | { |
490 | { |
428 | /* is this ugly, I ask? */ |
491 | /* restore all saved variables and stuff */ |
429 | LEAVE_SCOPE (0); |
492 | LEAVE_SCOPE (0); |
|
|
493 | assert (PL_tmps_floor == -1); |
430 | |
494 | |
431 | /* sure it is, but more important: is it correct?? :/ */ |
495 | /* free all temporaries */ |
432 | FREETMPS; |
496 | FREETMPS; |
|
|
497 | assert (PL_tmps_ix == -1); |
433 | |
498 | |
|
|
499 | /* unwind all extra stacks */ |
434 | /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ |
500 | POPSTACK_TO (PL_mainstack); |
|
|
501 | |
|
|
502 | /* unwind main stack */ |
|
|
503 | dounwind (-1); |
435 | } |
504 | } |
436 | |
505 | |
437 | while (PL_curstackinfo->si_next) |
506 | while (PL_curstackinfo->si_next) |
438 | PL_curstackinfo = PL_curstackinfo->si_next; |
507 | PL_curstackinfo = PL_curstackinfo->si_next; |
439 | |
508 | |
440 | while (PL_curstackinfo) |
509 | while (PL_curstackinfo) |
441 | { |
510 | { |
442 | PERL_SI *p = PL_curstackinfo->si_prev; |
511 | PERL_SI *p = PL_curstackinfo->si_prev; |
443 | |
512 | |
444 | { /*D*//*remove*/ |
|
|
445 | dSP; |
|
|
446 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
|
|
447 | PUTBACK; /* possibly superfluous */ |
|
|
448 | } |
|
|
449 | |
|
|
450 | if (!IN_DESTRUCT) |
513 | if (!IN_DESTRUCT) |
451 | { |
|
|
452 | dounwind (-1);/*D*//*remove*/ |
|
|
453 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
514 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
454 | } |
|
|
455 | |
515 | |
456 | Safefree (PL_curstackinfo->si_cxstack); |
516 | Safefree (PL_curstackinfo->si_cxstack); |
457 | Safefree (PL_curstackinfo); |
517 | Safefree (PL_curstackinfo); |
458 | PL_curstackinfo = p; |
518 | PL_curstackinfo = p; |
459 | } |
519 | } |
… | |
… | |
465 | #if !PERL_VERSION_ATLEAST (5,9,0) |
525 | #if !PERL_VERSION_ATLEAST (5,9,0) |
466 | Safefree (PL_retstack); |
526 | Safefree (PL_retstack); |
467 | #endif |
527 | #endif |
468 | } |
528 | } |
469 | |
529 | |
|
|
530 | /** coroutine stack handling ************************************************/ |
|
|
531 | |
470 | static void |
532 | static void |
471 | setup_coro (struct coro *coro) |
533 | setup_coro (struct coro *coro) |
472 | { |
534 | { |
473 | /* |
535 | /* |
474 | * emulate part of the perl startup here. |
536 | * emulate part of the perl startup here. |
475 | */ |
537 | */ |
476 | |
538 | |
477 | coro_init_stacks (); |
539 | coro_init_stacks (); |
478 | |
540 | |
479 | PL_curcop = 0; |
541 | PL_curcop = &PL_compiling; |
480 | PL_in_eval = 0; |
542 | PL_in_eval = EVAL_NULL; |
481 | PL_curpm = 0; |
543 | PL_curpm = 0; |
|
|
544 | PL_localizing = 0; |
|
|
545 | PL_dirty = 0; |
|
|
546 | PL_restartop = 0; |
482 | |
547 | |
483 | { |
548 | { |
484 | dSP; |
549 | dSP; |
485 | LOGOP myop; |
550 | LOGOP myop; |
486 | |
551 | |
487 | /* I have no idea why this is needed, but it is */ |
|
|
488 | PUSHMARK (SP); |
|
|
489 | |
|
|
490 | SvREFCNT_dec (GvAV (PL_defgv)); |
552 | SvREFCNT_dec (GvAV (PL_defgv)); |
491 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
553 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
492 | |
554 | |
493 | Zero (&myop, 1, LOGOP); |
555 | Zero (&myop, 1, LOGOP); |
494 | myop.op_next = Nullop; |
556 | myop.op_next = Nullop; |
495 | myop.op_flags = OPf_WANT_VOID; |
557 | myop.op_flags = OPf_WANT_VOID; |
496 | |
558 | |
497 | PL_op = (OP *)&myop; |
|
|
498 | |
|
|
499 | PUSHMARK (SP); |
559 | PUSHMARK (SP); |
500 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
560 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
501 | PUTBACK; |
561 | PUTBACK; |
|
|
562 | PL_op = (OP *)&myop; |
502 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
563 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
503 | SPAGAIN; |
564 | SPAGAIN; |
504 | |
|
|
505 | ENTER; /* necessary e.g. for dounwind */ |
|
|
506 | } |
565 | } |
|
|
566 | |
|
|
567 | ENTER; /* necessary e.g. for dounwind */ |
507 | } |
568 | } |
508 | |
569 | |
509 | static void |
570 | static void |
510 | free_coro_mortal () |
571 | free_coro_mortal () |
511 | { |
572 | { |
… | |
… | |
514 | SvREFCNT_dec (coro_mortal); |
575 | SvREFCNT_dec (coro_mortal); |
515 | coro_mortal = 0; |
576 | coro_mortal = 0; |
516 | } |
577 | } |
517 | } |
578 | } |
518 | |
579 | |
|
|
580 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
519 | static void NOINLINE |
581 | static void NOINLINE |
520 | prepare_cctx (coro_cctx *cctx) |
582 | prepare_cctx (coro_cctx *cctx) |
521 | { |
583 | { |
522 | dSP; |
584 | dSP; |
523 | LOGOP myop; |
585 | LOGOP myop; |
524 | |
586 | |
525 | Zero (&myop, 1, LOGOP); |
587 | Zero (&myop, 1, LOGOP); |
526 | myop.op_next = PL_op; |
588 | myop.op_next = PL_op; |
527 | myop.op_flags = OPf_WANT_VOID; |
589 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
528 | |
|
|
529 | sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx)); |
|
|
530 | |
590 | |
531 | PUSHMARK (SP); |
591 | PUSHMARK (SP); |
|
|
592 | EXTEND (SP, 2); |
|
|
593 | PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); |
532 | XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
594 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
533 | PUTBACK; |
595 | PUTBACK; |
|
|
596 | PL_op = (OP *)&myop; |
534 | PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); |
597 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
535 | SPAGAIN; |
598 | SPAGAIN; |
536 | } |
599 | } |
537 | |
600 | |
538 | static void |
601 | static void |
539 | coro_run (void *arg) |
602 | coro_run (void *arg) |
540 | { |
603 | { |
541 | /* coro_run is the alternative epilogue of transfer() */ |
604 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
542 | UNLOCK; |
605 | UNLOCK; |
543 | |
606 | |
544 | /* |
607 | /* |
545 | * this is a _very_ stripped down perl interpreter ;) |
608 | * this is a _very_ stripped down perl interpreter ;) |
546 | */ |
609 | */ |
… | |
… | |
548 | |
611 | |
549 | /* inject call to cctx_init */ |
612 | /* inject call to cctx_init */ |
550 | prepare_cctx ((coro_cctx *)arg); |
613 | prepare_cctx ((coro_cctx *)arg); |
551 | |
614 | |
552 | /* somebody will hit me for both perl_run and PL_restartop */ |
615 | /* somebody will hit me for both perl_run and PL_restartop */ |
|
|
616 | PL_restartop = PL_op; |
553 | perl_run (PL_curinterp); |
617 | perl_run (PL_curinterp); |
554 | |
618 | |
555 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
619 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
556 | abort (); |
620 | abort (); |
557 | } |
621 | } |
… | |
… | |
561 | { |
625 | { |
562 | coro_cctx *cctx; |
626 | coro_cctx *cctx; |
563 | |
627 | |
564 | ++cctx_count; |
628 | ++cctx_count; |
565 | |
629 | |
566 | New (0, cctx, 1, coro_cctx); |
630 | Newz (0, cctx, 1, coro_cctx); |
567 | |
631 | |
568 | #if HAVE_MMAP |
632 | #if HAVE_MMAP |
569 | |
633 | |
570 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
634 | cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; |
571 | /* mmap suppsedly does allocate-on-write for us */ |
635 | /* mmap supposedly does allocate-on-write for us */ |
572 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
636 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
573 | |
637 | |
574 | if (cctx->sptr == (void *)-1) |
638 | if (cctx->sptr != (void *)-1) |
575 | { |
|
|
576 | perror ("FATAL: unable to mmap stack for coroutine"); |
|
|
577 | _exit (EXIT_FAILURE); |
|
|
578 | } |
639 | { |
579 | |
|
|
580 | # if STACKGUARD |
640 | # if STACKGUARD |
581 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
641 | mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
582 | # endif |
642 | # endif |
583 | |
643 | REGISTER_STACK ( |
584 | #else |
644 | cctx, |
585 | |
|
|
586 | cctx->ssize = STACKSIZE * (long)sizeof (long); |
|
|
587 | New (0, cctx->sptr, STACKSIZE, long); |
|
|
588 | |
|
|
589 | if (!cctx->sptr) |
|
|
590 | { |
|
|
591 | perror ("FATAL: unable to malloc stack for coroutine"); |
|
|
592 | _exit (EXIT_FAILURE); |
|
|
593 | } |
|
|
594 | |
|
|
595 | #endif |
|
|
596 | |
|
|
597 | #if USE_VALGRIND |
|
|
598 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ( |
|
|
599 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
645 | STACKGUARD * PAGESIZE + (char *)cctx->sptr, |
600 | cctx->ssize + (char *)cctx->sptr |
646 | cctx->ssize + (char *)cctx->sptr |
601 | ); |
647 | ); |
602 | #endif |
|
|
603 | |
648 | |
604 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
649 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); |
|
|
650 | } |
|
|
651 | else |
|
|
652 | #endif |
|
|
653 | { |
|
|
654 | cctx->ssize = -STACKSIZE * (long)sizeof (long); |
|
|
655 | New (0, cctx->sptr, STACKSIZE, long); |
|
|
656 | |
|
|
657 | if (!cctx->sptr) |
|
|
658 | { |
|
|
659 | perror ("FATAL: unable to allocate stack for coroutine"); |
|
|
660 | _exit (EXIT_FAILURE); |
|
|
661 | } |
|
|
662 | |
|
|
663 | REGISTER_STACK ( |
|
|
664 | cctx, |
|
|
665 | (char *)cctx->sptr, |
|
|
666 | (char *)cctx->sptr - cctx->ssize |
|
|
667 | ); |
|
|
668 | |
|
|
669 | coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize); |
|
|
670 | } |
605 | |
671 | |
606 | return cctx; |
672 | return cctx; |
607 | } |
673 | } |
608 | |
674 | |
609 | static void |
675 | static void |
610 | cctx_free (coro_cctx *cctx) |
676 | cctx_destroy (coro_cctx *cctx) |
611 | { |
677 | { |
612 | if (!cctx) |
678 | if (!cctx) |
613 | return; |
679 | return; |
614 | |
680 | |
615 | --cctx_count; |
681 | --cctx_count; |
… | |
… | |
617 | #if USE_VALGRIND |
683 | #if USE_VALGRIND |
618 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
684 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
619 | #endif |
685 | #endif |
620 | |
686 | |
621 | #if HAVE_MMAP |
687 | #if HAVE_MMAP |
|
|
688 | if (cctx->ssize > 0) |
622 | munmap (cctx->sptr, cctx->ssize); |
689 | munmap (cctx->sptr, cctx->ssize); |
623 | #else |
690 | else |
|
|
691 | #endif |
624 | Safefree (cctx->sptr); |
692 | Safefree (cctx->sptr); |
625 | #endif |
|
|
626 | |
693 | |
627 | Safefree (cctx); |
694 | Safefree (cctx); |
628 | } |
695 | } |
629 | |
696 | |
630 | static coro_cctx * |
697 | static coro_cctx * |
… | |
… | |
632 | { |
699 | { |
633 | coro_cctx *cctx; |
700 | coro_cctx *cctx; |
634 | |
701 | |
635 | if (cctx_first) |
702 | if (cctx_first) |
636 | { |
703 | { |
637 | --cctx_idle; |
|
|
638 | cctx = cctx_first; |
704 | cctx = cctx_first; |
639 | cctx_first = cctx->next; |
705 | cctx_first = cctx->next; |
|
|
706 | --cctx_idle; |
640 | } |
707 | } |
641 | else |
708 | else |
642 | { |
709 | { |
643 | cctx = cctx_new (); |
710 | cctx = cctx_new (); |
644 | PL_op = PL_op->op_next; |
711 | PL_op = PL_op->op_next; |
… | |
… | |
648 | } |
715 | } |
649 | |
716 | |
650 | static void |
717 | static void |
651 | cctx_put (coro_cctx *cctx) |
718 | cctx_put (coro_cctx *cctx) |
652 | { |
719 | { |
|
|
720 | /* free another cctx if overlimit */ |
|
|
721 | if (cctx_idle >= MAX_IDLE_CCTX) |
|
|
722 | { |
|
|
723 | coro_cctx *first = cctx_first; |
|
|
724 | cctx_first = first->next; |
|
|
725 | --cctx_idle; |
|
|
726 | |
|
|
727 | assert (!first->inuse); |
|
|
728 | cctx_destroy (first); |
|
|
729 | } |
|
|
730 | |
653 | ++cctx_idle; |
731 | ++cctx_idle; |
654 | cctx->next = cctx_first; |
732 | cctx->next = cctx_first; |
655 | cctx_first = cctx; |
733 | cctx_first = cctx; |
656 | } |
734 | } |
657 | |
735 | |
|
|
736 | /** coroutine switching *****************************************************/ |
|
|
737 | |
658 | /* never call directly, always through the coro_state_transfer global variable */ |
738 | /* never call directly, always through the coro_state_transfer global variable */ |
659 | static void NOINLINE |
739 | static void NOINLINE |
660 | transfer (struct coro *prev, struct coro *next, int flags) |
740 | transfer (struct coro *prev, struct coro *next) |
661 | { |
741 | { |
662 | dSTACKLEVEL; |
742 | dSTACKLEVEL; |
663 | |
743 | |
664 | /* sometimes transfer is only called to set idle_sp */ |
744 | /* sometimes transfer is only called to set idle_sp */ |
665 | if (flags == TRANSFER_SET_STACKLEVEL) |
745 | if (!next) |
|
|
746 | { |
666 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
747 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
|
|
748 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
|
|
749 | } |
667 | else if (prev != next) |
750 | else if (prev != next) |
668 | { |
751 | { |
669 | coro_cctx *prev__cctx; |
752 | coro_cctx *prev__cctx; |
670 | |
753 | |
|
|
754 | if (prev->flags & CF_NEW) |
|
|
755 | { |
|
|
756 | /* create a new empty context */ |
|
|
757 | Newz (0, prev->cctx, 1, coro_cctx); |
|
|
758 | prev->cctx->inuse = 1; |
|
|
759 | prev->flags &= ~CF_NEW; |
|
|
760 | prev->flags |= CF_RUNNING; |
|
|
761 | } |
|
|
762 | |
|
|
763 | /*TODO: must not croak here */ |
|
|
764 | if (!prev->flags & CF_RUNNING) |
|
|
765 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
|
|
766 | |
|
|
767 | if (next->flags & CF_RUNNING) |
|
|
768 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
769 | |
|
|
770 | if (next->flags & CF_DESTROYED) |
|
|
771 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
772 | |
|
|
773 | prev->flags &= ~CF_RUNNING; |
|
|
774 | next->flags |= CF_RUNNING; |
|
|
775 | |
671 | LOCK; |
776 | LOCK; |
672 | |
777 | |
673 | if (next->mainstack) |
778 | if (next->flags & CF_NEW) |
674 | { |
779 | { |
675 | /* coroutine already started */ |
780 | /* need to start coroutine */ |
676 | SAVE (prev, flags); |
781 | next->flags &= ~CF_NEW; |
677 | LOAD (next); |
782 | /* first get rid of the old state */ |
|
|
783 | save_perl (prev); |
|
|
784 | /* setup coroutine call */ |
|
|
785 | setup_coro (next); |
|
|
786 | /* need a new stack */ |
|
|
787 | assert (!next->cctx); |
678 | } |
788 | } |
679 | else |
789 | else |
680 | { |
790 | { |
681 | /* need to start coroutine */ |
791 | /* coroutine already started */ |
682 | /* first get rid of the old state */ |
792 | save_perl (prev); |
683 | SAVE (prev, -1); |
793 | load_perl (next); |
684 | /* setup coroutine call */ |
|
|
685 | setup_coro (next); |
|
|
686 | /* need a stack */ |
|
|
687 | next->cctx = 0; |
|
|
688 | } |
794 | } |
689 | |
|
|
690 | if (!prev->cctx) |
|
|
691 | /* create a new empty context */ |
|
|
692 | Newz (0, prev->cctx, 1, coro_cctx); |
|
|
693 | |
795 | |
694 | prev__cctx = prev->cctx; |
796 | prev__cctx = prev->cctx; |
695 | |
797 | |
696 | /* possibly "free" the cctx */ |
798 | /* possibly "free" the cctx */ |
697 | if (prev__cctx->idle_sp == STACKLEVEL) |
799 | if (prev__cctx->idle_sp == STACKLEVEL) |
698 | { |
800 | { |
699 | assert (PL_top_env == prev__cctx->top_env);//D |
801 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
|
|
802 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
|
|
803 | |
|
|
804 | prev->cctx = 0; |
|
|
805 | |
700 | cctx_put (prev__cctx); |
806 | cctx_put (prev__cctx); |
701 | prev->cctx = 0; |
807 | prev__cctx->inuse = 0; |
702 | } |
808 | } |
703 | |
809 | |
704 | if (!next->cctx) |
810 | if (!next->cctx) |
|
|
811 | { |
705 | next->cctx = cctx_get (); |
812 | next->cctx = cctx_get (); |
|
|
813 | assert (!next->cctx->inuse); |
|
|
814 | next->cctx->inuse = 1; |
|
|
815 | } |
706 | |
816 | |
707 | if (prev__cctx != next->cctx) |
817 | if (prev__cctx != next->cctx) |
708 | { |
818 | { |
709 | prev__cctx->top_env = PL_top_env; |
819 | prev__cctx->top_env = PL_top_env; |
710 | PL_top_env = next->cctx->top_env; |
820 | PL_top_env = next->cctx->top_env; |
711 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
821 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
712 | } |
822 | } |
713 | |
823 | |
714 | free_coro_mortal (); |
824 | free_coro_mortal (); |
715 | |
|
|
716 | UNLOCK; |
825 | UNLOCK; |
717 | } |
826 | } |
718 | } |
827 | } |
719 | |
828 | |
720 | struct transfer_args |
829 | struct transfer_args |
721 | { |
830 | { |
722 | struct coro *prev, *next; |
831 | struct coro *prev, *next; |
723 | int flags; |
|
|
724 | }; |
832 | }; |
725 | |
833 | |
726 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) |
834 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
727 | |
835 | |
|
|
836 | /** high level stuff ********************************************************/ |
|
|
837 | |
728 | static void |
838 | static int |
729 | coro_state_destroy (struct coro *coro) |
839 | coro_state_destroy (struct coro *coro) |
730 | { |
840 | { |
731 | if (coro->refcnt--) |
841 | if (coro->flags & CF_DESTROYED) |
732 | return; |
842 | return 0; |
|
|
843 | |
|
|
844 | coro->flags |= CF_DESTROYED; |
|
|
845 | |
|
|
846 | if (coro->flags & CF_READY) |
|
|
847 | { |
|
|
848 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
|
|
849 | /* alternative: look through all ready queues and remove the coro */ |
|
|
850 | LOCK; |
|
|
851 | --coro_nready; |
|
|
852 | UNLOCK; |
|
|
853 | } |
|
|
854 | else |
|
|
855 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
733 | |
856 | |
734 | if (coro->mainstack && coro->mainstack != main_mainstack) |
857 | if (coro->mainstack && coro->mainstack != main_mainstack) |
735 | { |
858 | { |
736 | struct coro temp; |
859 | struct coro temp; |
737 | |
860 | |
738 | SAVE ((&temp), TRANSFER_SAVE_ALL); |
861 | assert (!(coro->flags & CF_RUNNING)); |
739 | LOAD (coro); |
862 | |
|
|
863 | Zero (&temp, 1, struct coro); |
|
|
864 | temp.save = CORO_SAVE_ALL; |
|
|
865 | |
|
|
866 | if (coro->flags & CF_RUNNING) |
|
|
867 | croak ("FATAL: tried to destroy currently running coroutine"); |
|
|
868 | |
|
|
869 | save_perl (&temp); |
|
|
870 | load_perl (coro); |
740 | |
871 | |
741 | coro_destroy_stacks (); |
872 | coro_destroy_stacks (); |
742 | |
873 | |
743 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
874 | load_perl (&temp); /* this will get rid of defsv etc.. */ |
744 | |
875 | |
745 | coro->mainstack = 0; |
876 | coro->mainstack = 0; |
746 | } |
877 | } |
747 | |
878 | |
748 | cctx_free (coro->cctx); |
879 | cctx_destroy (coro->cctx); |
749 | SvREFCNT_dec (coro->args); |
880 | SvREFCNT_dec (coro->args); |
750 | Safefree (coro); |
881 | |
|
|
882 | return 1; |
751 | } |
883 | } |
752 | |
884 | |
753 | static int |
885 | static int |
754 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
886 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
755 | { |
887 | { |
756 | struct coro *coro = (struct coro *)mg->mg_ptr; |
888 | struct coro *coro = (struct coro *)mg->mg_ptr; |
757 | mg->mg_ptr = 0; |
889 | mg->mg_ptr = 0; |
758 | |
890 | |
|
|
891 | if (--coro->refcnt < 0) |
|
|
892 | { |
759 | coro_state_destroy (coro); |
893 | coro_state_destroy (coro); |
|
|
894 | Safefree (coro); |
|
|
895 | } |
760 | |
896 | |
761 | return 0; |
897 | return 0; |
762 | } |
898 | } |
763 | |
899 | |
764 | static int |
900 | static int |
… | |
… | |
771 | return 0; |
907 | return 0; |
772 | } |
908 | } |
773 | |
909 | |
774 | static MGVTBL coro_state_vtbl = { |
910 | static MGVTBL coro_state_vtbl = { |
775 | 0, 0, 0, 0, |
911 | 0, 0, 0, 0, |
776 | coro_state_clear, |
912 | coro_state_free, |
777 | 0, |
913 | 0, |
778 | #ifdef MGf_DUP |
914 | #ifdef MGf_DUP |
779 | coro_state_dup, |
915 | coro_state_dup, |
780 | #else |
916 | #else |
781 | # define MGf_DUP 0 |
917 | # define MGf_DUP 0 |
… | |
… | |
803 | assert (mg->mg_type == PERL_MAGIC_ext); |
939 | assert (mg->mg_type == PERL_MAGIC_ext); |
804 | return (struct coro *)mg->mg_ptr; |
940 | return (struct coro *)mg->mg_ptr; |
805 | } |
941 | } |
806 | |
942 | |
807 | static void |
943 | static void |
808 | prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) |
944 | prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
809 | { |
945 | { |
810 | ta->prev = SvSTATE (prev); |
946 | ta->prev = SvSTATE (prev_sv); |
811 | ta->next = SvSTATE (next); |
947 | ta->next = SvSTATE (next_sv); |
812 | ta->flags = flags; |
|
|
813 | } |
948 | } |
814 | |
949 | |
815 | static void |
950 | static void |
816 | api_transfer (SV *prev, SV *next, int flags) |
951 | api_transfer (SV *prev_sv, SV *next_sv) |
817 | { |
952 | { |
818 | dTHX; |
|
|
819 | struct transfer_args ta; |
953 | struct transfer_args ta; |
820 | |
954 | |
821 | prepare_transfer (&ta, prev, next, flags); |
955 | prepare_transfer (&ta, prev_sv, next_sv); |
822 | TRANSFER (ta); |
956 | TRANSFER (ta); |
823 | } |
957 | } |
824 | |
958 | |
|
|
959 | static int |
|
|
960 | api_save (SV *coro_sv, int new_save) |
|
|
961 | { |
|
|
962 | struct coro *coro = SvSTATE (coro_sv); |
|
|
963 | int old_save = coro->save; |
|
|
964 | |
|
|
965 | if (new_save >= 0) |
|
|
966 | coro->save = new_save; |
|
|
967 | |
|
|
968 | return old_save; |
|
|
969 | } |
|
|
970 | |
825 | /** Coro ********************************************************************/ |
971 | /** Coro ********************************************************************/ |
826 | |
972 | |
827 | #define PRIO_MAX 3 |
|
|
828 | #define PRIO_HIGH 1 |
|
|
829 | #define PRIO_NORMAL 0 |
|
|
830 | #define PRIO_LOW -1 |
|
|
831 | #define PRIO_IDLE -3 |
|
|
832 | #define PRIO_MIN -4 |
|
|
833 | |
|
|
834 | /* for Coro.pm */ |
|
|
835 | static GV *coro_current, *coro_idle; |
|
|
836 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
|
|
837 | static int coro_nready; |
|
|
838 | |
|
|
839 | static void |
973 | static void |
840 | coro_enq (SV *sv) |
974 | coro_enq (SV *coro_sv) |
841 | { |
975 | { |
842 | int prio; |
|
|
843 | |
|
|
844 | if (SvTYPE (sv) != SVt_PVHV) |
|
|
845 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
846 | |
|
|
847 | prio = SvSTATE (sv)->prio; |
|
|
848 | |
|
|
849 | av_push (coro_ready [prio - PRIO_MIN], sv); |
976 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
850 | coro_nready++; |
|
|
851 | } |
977 | } |
852 | |
978 | |
853 | static SV * |
979 | static SV * |
854 | coro_deq (int min_prio) |
980 | coro_deq (int min_prio) |
855 | { |
981 | { |
… | |
… | |
859 | if (min_prio < 0) |
985 | if (min_prio < 0) |
860 | min_prio = 0; |
986 | min_prio = 0; |
861 | |
987 | |
862 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
988 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
863 | if (AvFILLp (coro_ready [prio]) >= 0) |
989 | if (AvFILLp (coro_ready [prio]) >= 0) |
864 | { |
|
|
865 | coro_nready--; |
|
|
866 | return av_shift (coro_ready [prio]); |
990 | return av_shift (coro_ready [prio]); |
867 | } |
|
|
868 | |
991 | |
869 | return 0; |
992 | return 0; |
870 | } |
993 | } |
871 | |
994 | |
872 | static void |
995 | static int |
873 | api_ready (SV *coro) |
996 | api_ready (SV *coro_sv) |
874 | { |
997 | { |
875 | dTHX; |
998 | struct coro *coro; |
876 | |
999 | |
877 | if (SvROK (coro)) |
1000 | if (SvROK (coro_sv)) |
878 | coro = SvRV (coro); |
1001 | coro_sv = SvRV (coro_sv); |
|
|
1002 | |
|
|
1003 | coro = SvSTATE (coro_sv); |
|
|
1004 | |
|
|
1005 | if (coro->flags & CF_READY) |
|
|
1006 | return 0; |
|
|
1007 | |
|
|
1008 | coro->flags |= CF_READY; |
879 | |
1009 | |
880 | LOCK; |
1010 | LOCK; |
881 | coro_enq (SvREFCNT_inc (coro)); |
1011 | coro_enq (SvREFCNT_inc (coro_sv)); |
|
|
1012 | ++coro_nready; |
882 | UNLOCK; |
1013 | UNLOCK; |
|
|
1014 | |
|
|
1015 | return 1; |
|
|
1016 | } |
|
|
1017 | |
|
|
1018 | static int |
|
|
1019 | api_is_ready (SV *coro_sv) |
|
|
1020 | { |
|
|
1021 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
883 | } |
1022 | } |
884 | |
1023 | |
885 | static void |
1024 | static void |
886 | prepare_schedule (struct transfer_args *ta) |
1025 | prepare_schedule (struct transfer_args *ta) |
887 | { |
1026 | { |
888 | SV *current, *prev, *next; |
1027 | SV *prev_sv, *next_sv; |
889 | |
|
|
890 | current = GvSV (coro_current); |
|
|
891 | |
1028 | |
892 | for (;;) |
1029 | for (;;) |
893 | { |
1030 | { |
894 | LOCK; |
1031 | LOCK; |
895 | next = coro_deq (PRIO_MIN); |
1032 | next_sv = coro_deq (PRIO_MIN); |
|
|
1033 | |
|
|
1034 | /* nothing to schedule: call the idle handler */ |
|
|
1035 | if (!next_sv) |
|
|
1036 | { |
|
|
1037 | dSP; |
|
|
1038 | UNLOCK; |
|
|
1039 | |
|
|
1040 | ENTER; |
|
|
1041 | SAVETMPS; |
|
|
1042 | |
|
|
1043 | PUSHMARK (SP); |
|
|
1044 | PUTBACK; |
|
|
1045 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1046 | |
|
|
1047 | FREETMPS; |
|
|
1048 | LEAVE; |
|
|
1049 | continue; |
|
|
1050 | } |
|
|
1051 | |
|
|
1052 | ta->next = SvSTATE (next_sv); |
|
|
1053 | |
|
|
1054 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1055 | if (ta->next->flags & CF_DESTROYED) |
|
|
1056 | { |
|
|
1057 | UNLOCK; |
|
|
1058 | SvREFCNT_dec (next_sv); |
|
|
1059 | /* coro_nready is already taken care of by destroy */ |
|
|
1060 | continue; |
|
|
1061 | } |
|
|
1062 | |
|
|
1063 | --coro_nready; |
896 | UNLOCK; |
1064 | UNLOCK; |
897 | |
|
|
898 | if (next) |
|
|
899 | break; |
1065 | break; |
900 | |
|
|
901 | { |
|
|
902 | dSP; |
|
|
903 | |
|
|
904 | ENTER; |
|
|
905 | SAVETMPS; |
|
|
906 | |
|
|
907 | PUSHMARK (SP); |
|
|
908 | PUTBACK; |
|
|
909 | call_sv (GvSV (coro_idle), G_DISCARD); |
|
|
910 | |
|
|
911 | FREETMPS; |
|
|
912 | LEAVE; |
|
|
913 | } |
1066 | } |
914 | } |
|
|
915 | |
|
|
916 | prev = SvRV (current); |
|
|
917 | SvRV (current) = next; |
|
|
918 | |
1067 | |
919 | /* free this only after the transfer */ |
1068 | /* free this only after the transfer */ |
|
|
1069 | prev_sv = SvRV (coro_current); |
|
|
1070 | SvRV_set (coro_current, next_sv); |
|
|
1071 | ta->prev = SvSTATE (prev_sv); |
|
|
1072 | |
|
|
1073 | assert (ta->next->flags & CF_READY); |
|
|
1074 | ta->next->flags &= ~CF_READY; |
|
|
1075 | |
920 | LOCK; |
1076 | LOCK; |
921 | free_coro_mortal (); |
1077 | free_coro_mortal (); |
|
|
1078 | coro_mortal = prev_sv; |
922 | UNLOCK; |
1079 | UNLOCK; |
923 | coro_mortal = prev; |
|
|
924 | |
|
|
925 | ta->prev = SvSTATE (prev); |
|
|
926 | ta->next = SvSTATE (next); |
|
|
927 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
928 | } |
1080 | } |
929 | |
1081 | |
930 | static void |
1082 | static void |
931 | prepare_cede (struct transfer_args *ta) |
1083 | prepare_cede (struct transfer_args *ta) |
932 | { |
1084 | { |
933 | LOCK; |
1085 | api_ready (coro_current); |
934 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
935 | UNLOCK; |
|
|
936 | |
|
|
937 | prepare_schedule (ta); |
1086 | prepare_schedule (ta); |
938 | } |
1087 | } |
939 | |
1088 | |
|
|
1089 | static int |
|
|
1090 | prepare_cede_notself (struct transfer_args *ta) |
|
|
1091 | { |
|
|
1092 | if (coro_nready) |
|
|
1093 | { |
|
|
1094 | SV *prev = SvRV (coro_current); |
|
|
1095 | prepare_schedule (ta); |
|
|
1096 | api_ready (prev); |
|
|
1097 | return 1; |
|
|
1098 | } |
|
|
1099 | else |
|
|
1100 | return 0; |
|
|
1101 | } |
|
|
1102 | |
940 | static void |
1103 | static void |
941 | api_schedule (void) |
1104 | api_schedule (void) |
942 | { |
1105 | { |
943 | dTHX; |
|
|
944 | struct transfer_args ta; |
1106 | struct transfer_args ta; |
945 | |
1107 | |
946 | prepare_schedule (&ta); |
1108 | prepare_schedule (&ta); |
947 | TRANSFER (ta); |
1109 | TRANSFER (ta); |
948 | } |
1110 | } |
949 | |
1111 | |
950 | static void |
1112 | static int |
951 | api_cede (void) |
1113 | api_cede (void) |
952 | { |
1114 | { |
953 | dTHX; |
|
|
954 | struct transfer_args ta; |
1115 | struct transfer_args ta; |
955 | |
1116 | |
956 | prepare_cede (&ta); |
1117 | prepare_cede (&ta); |
|
|
1118 | |
|
|
1119 | if (ta.prev != ta.next) |
|
|
1120 | { |
957 | TRANSFER (ta); |
1121 | TRANSFER (ta); |
|
|
1122 | return 1; |
|
|
1123 | } |
|
|
1124 | else |
|
|
1125 | return 0; |
|
|
1126 | } |
|
|
1127 | |
|
|
1128 | static int |
|
|
1129 | api_cede_notself (void) |
|
|
1130 | { |
|
|
1131 | struct transfer_args ta; |
|
|
1132 | |
|
|
1133 | if (prepare_cede_notself (&ta)) |
|
|
1134 | { |
|
|
1135 | TRANSFER (ta); |
|
|
1136 | return 1; |
|
|
1137 | } |
|
|
1138 | else |
|
|
1139 | return 0; |
958 | } |
1140 | } |
959 | |
1141 | |
960 | MODULE = Coro::State PACKAGE = Coro::State |
1142 | MODULE = Coro::State PACKAGE = Coro::State |
961 | |
1143 | |
962 | PROTOTYPES: DISABLE |
1144 | PROTOTYPES: DISABLE |
… | |
… | |
968 | #endif |
1150 | #endif |
969 | BOOT_PAGESIZE; |
1151 | BOOT_PAGESIZE; |
970 | |
1152 | |
971 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1153 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
972 | |
1154 | |
973 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
1155 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
974 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
1156 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
975 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
1157 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
|
|
1158 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
|
|
1159 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
976 | |
1160 | |
977 | main_mainstack = PL_mainstack; |
1161 | main_mainstack = PL_mainstack; |
978 | |
1162 | |
979 | coroapi.ver = CORO_API_VERSION; |
1163 | coroapi.ver = CORO_API_VERSION; |
980 | coroapi.transfer = api_transfer; |
1164 | coroapi.transfer = api_transfer; |
… | |
… | |
990 | HV *hv; |
1174 | HV *hv; |
991 | int i; |
1175 | int i; |
992 | |
1176 | |
993 | Newz (0, coro, 1, struct coro); |
1177 | Newz (0, coro, 1, struct coro); |
994 | coro->args = newAV (); |
1178 | coro->args = newAV (); |
|
|
1179 | coro->save = CORO_SAVE_ALL; |
|
|
1180 | coro->flags = CF_NEW; |
995 | |
1181 | |
996 | hv = newHV (); |
1182 | hv = newHV (); |
997 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1183 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
998 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1184 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
999 | |
1185 | |
1000 | for (i = 1; i < items; i++) |
1186 | for (i = 1; i < items; i++) |
1001 | av_push (coro->args, newSVsv (ST (i))); |
1187 | av_push (coro->args, newSVsv (ST (i))); |
1002 | } |
1188 | } |
1003 | OUTPUT: |
1189 | OUTPUT: |
|
|
1190 | RETVAL |
|
|
1191 | |
|
|
1192 | int |
|
|
1193 | save (SV *coro, int new_save = -1) |
|
|
1194 | CODE: |
|
|
1195 | RETVAL = api_save (coro, new_save); |
|
|
1196 | OUTPUT: |
1004 | RETVAL |
1197 | RETVAL |
1005 | |
1198 | |
1006 | void |
1199 | void |
1007 | _set_stacklevel (...) |
1200 | _set_stacklevel (...) |
1008 | ALIAS: |
1201 | ALIAS: |
1009 | Coro::State::transfer = 1 |
1202 | Coro::State::transfer = 1 |
1010 | Coro::schedule = 2 |
1203 | Coro::schedule = 2 |
1011 | Coro::cede = 3 |
1204 | Coro::cede = 3 |
1012 | Coro::Cont::yield = 4 |
1205 | Coro::cede_notself = 4 |
1013 | CODE: |
1206 | CODE: |
1014 | { |
1207 | { |
1015 | struct transfer_args ta; |
1208 | struct transfer_args ta; |
1016 | |
1209 | |
1017 | switch (ix) |
1210 | switch (ix) |
1018 | { |
1211 | { |
1019 | case 0: |
1212 | case 0: |
1020 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1213 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1021 | ta.next = 0; |
1214 | ta.next = 0; |
1022 | ta.flags = TRANSFER_SET_STACKLEVEL; |
|
|
1023 | break; |
1215 | break; |
1024 | |
1216 | |
1025 | case 1: |
1217 | case 1: |
1026 | if (items != 3) |
1218 | if (items != 2) |
1027 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1219 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1028 | |
1220 | |
1029 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1221 | prepare_transfer (&ta, ST (0), ST (1)); |
1030 | break; |
1222 | break; |
1031 | |
1223 | |
1032 | case 2: |
1224 | case 2: |
1033 | prepare_schedule (&ta); |
1225 | prepare_schedule (&ta); |
1034 | break; |
1226 | break; |
… | |
… | |
1036 | case 3: |
1228 | case 3: |
1037 | prepare_cede (&ta); |
1229 | prepare_cede (&ta); |
1038 | break; |
1230 | break; |
1039 | |
1231 | |
1040 | case 4: |
1232 | case 4: |
1041 | { |
1233 | if (!prepare_cede_notself (&ta)) |
1042 | SV *yieldstack; |
1234 | XSRETURN_EMPTY; |
1043 | SV *sv; |
|
|
1044 | AV *defav = GvAV (PL_defgv); |
|
|
1045 | |
1235 | |
1046 | yieldstack = *hv_fetch ( |
|
|
1047 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1048 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1049 | 0 |
|
|
1050 | ); |
|
|
1051 | |
|
|
1052 | /* set up @_ -- ugly */ |
|
|
1053 | av_clear (defav); |
|
|
1054 | av_fill (defav, items - 1); |
|
|
1055 | while (items--) |
|
|
1056 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
|
|
1057 | |
|
|
1058 | sv = av_pop ((AV *)SvRV (yieldstack)); |
|
|
1059 | ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0)); |
|
|
1060 | ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0)); |
|
|
1061 | ta.flags = 0; |
|
|
1062 | SvREFCNT_dec (sv); |
|
|
1063 | } |
|
|
1064 | break; |
1236 | break; |
1065 | |
|
|
1066 | } |
1237 | } |
1067 | |
1238 | |
|
|
1239 | BARRIER; |
1068 | TRANSFER (ta); |
1240 | TRANSFER (ta); |
1069 | } |
|
|
1070 | |
1241 | |
1071 | void |
1242 | if (GIMME_V != G_VOID && ta.next != ta.prev) |
1072 | _clone_state_from (SV *dst, SV *src) |
1243 | XSRETURN_YES; |
|
|
1244 | } |
|
|
1245 | |
|
|
1246 | bool |
|
|
1247 | _destroy (SV *coro_sv) |
1073 | CODE: |
1248 | CODE: |
1074 | { |
1249 | RETVAL = coro_state_destroy (SvSTATE (coro_sv)); |
1075 | struct coro *coro_src = SvSTATE (src); |
1250 | OUTPUT: |
1076 | |
1251 | RETVAL |
1077 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
|
|
1078 | |
|
|
1079 | ++coro_src->refcnt; |
|
|
1080 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
|
|
1081 | } |
|
|
1082 | |
1252 | |
1083 | void |
1253 | void |
1084 | _exit (code) |
1254 | _exit (code) |
1085 | int code |
1255 | int code |
1086 | PROTOTYPE: $ |
1256 | PROTOTYPE: $ |
… | |
… | |
1114 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1284 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1115 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1285 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1116 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1286 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1117 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1287 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1118 | |
1288 | |
1119 | coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); |
1289 | coro_current = get_sv ("Coro::current", FALSE); |
1120 | coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); |
1290 | SvREADONLY_on (coro_current); |
1121 | |
1291 | |
1122 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1292 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1123 | coro_ready[i] = newAV (); |
1293 | coro_ready[i] = newAV (); |
1124 | |
1294 | |
1125 | { |
1295 | { |
1126 | SV *sv = perl_get_sv("Coro::API", 1); |
1296 | SV *sv = perl_get_sv("Coro::API", 1); |
1127 | |
1297 | |
1128 | coroapi.schedule = api_schedule; |
1298 | coroapi.schedule = api_schedule; |
|
|
1299 | coroapi.save = api_save; |
1129 | coroapi.cede = api_cede; |
1300 | coroapi.cede = api_cede; |
|
|
1301 | coroapi.cede_notself = api_cede_notself; |
1130 | coroapi.ready = api_ready; |
1302 | coroapi.ready = api_ready; |
|
|
1303 | coroapi.is_ready = api_is_ready; |
1131 | coroapi.nready = &coro_nready; |
1304 | coroapi.nready = &coro_nready; |
1132 | coroapi.current = coro_current; |
1305 | coroapi.current = coro_current; |
1133 | |
1306 | |
1134 | GCoroAPI = &coroapi; |
1307 | GCoroAPI = &coroapi; |
1135 | sv_setiv (sv, (IV)&coroapi); |
1308 | sv_setiv (sv, (IV)&coroapi); |
1136 | SvREADONLY_on (sv); |
1309 | SvREADONLY_on (sv); |
1137 | } |
1310 | } |
1138 | } |
1311 | } |
|
|
1312 | |
|
|
1313 | void |
|
|
1314 | _set_current (SV *current) |
|
|
1315 | PROTOTYPE: $ |
|
|
1316 | CODE: |
|
|
1317 | SvREFCNT_dec (SvRV (coro_current)); |
|
|
1318 | SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); |
1139 | |
1319 | |
1140 | int |
1320 | int |
1141 | prio (Coro::State coro, int newprio = 0) |
1321 | prio (Coro::State coro, int newprio = 0) |
1142 | ALIAS: |
1322 | ALIAS: |
1143 | nice = 1 |
1323 | nice = 1 |
… | |
… | |
1146 | RETVAL = coro->prio; |
1326 | RETVAL = coro->prio; |
1147 | |
1327 | |
1148 | if (items > 1) |
1328 | if (items > 1) |
1149 | { |
1329 | { |
1150 | if (ix) |
1330 | if (ix) |
1151 | newprio += coro->prio; |
1331 | newprio = coro->prio - newprio; |
1152 | |
1332 | |
1153 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1333 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1154 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1334 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1155 | |
1335 | |
1156 | coro->prio = newprio; |
1336 | coro->prio = newprio; |
1157 | } |
1337 | } |
1158 | } |
1338 | } |
|
|
1339 | OUTPUT: |
|
|
1340 | RETVAL |
1159 | |
1341 | |
1160 | void |
1342 | SV * |
1161 | ready (SV *self) |
1343 | ready (SV *self) |
1162 | PROTOTYPE: $ |
1344 | PROTOTYPE: $ |
1163 | CODE: |
1345 | CODE: |
1164 | api_ready (self); |
1346 | RETVAL = boolSV (api_ready (self)); |
|
|
1347 | OUTPUT: |
|
|
1348 | RETVAL |
|
|
1349 | |
|
|
1350 | SV * |
|
|
1351 | is_ready (SV *self) |
|
|
1352 | PROTOTYPE: $ |
|
|
1353 | CODE: |
|
|
1354 | RETVAL = boolSV (api_is_ready (self)); |
|
|
1355 | OUTPUT: |
|
|
1356 | RETVAL |
1165 | |
1357 | |
1166 | int |
1358 | int |
1167 | nready (...) |
1359 | nready (...) |
1168 | PROTOTYPE: |
1360 | PROTOTYPE: |
1169 | CODE: |
1361 | CODE: |
… | |
… | |
1175 | |
1367 | |
1176 | SV * |
1368 | SV * |
1177 | _get_state () |
1369 | _get_state () |
1178 | CODE: |
1370 | CODE: |
1179 | { |
1371 | { |
1180 | struct { |
1372 | struct io_state *data; |
1181 | int errorno; |
|
|
1182 | int laststype; |
|
|
1183 | int laststatval; |
|
|
1184 | Stat_t statcache; |
|
|
1185 | } data; |
|
|
1186 | |
1373 | |
|
|
1374 | RETVAL = newSV (sizeof (struct io_state)); |
|
|
1375 | data = (struct io_state *)SvPVX (RETVAL); |
|
|
1376 | SvCUR_set (RETVAL, sizeof (struct io_state)); |
|
|
1377 | SvPOK_only (RETVAL); |
|
|
1378 | |
1187 | data.errorno = errno; |
1379 | data->errorno = errno; |
1188 | data.laststype = PL_laststype; |
1380 | data->laststype = PL_laststype; |
1189 | data.laststatval = PL_laststatval; |
1381 | data->laststatval = PL_laststatval; |
1190 | data.statcache = PL_statcache; |
1382 | data->statcache = PL_statcache; |
1191 | |
|
|
1192 | RETVAL = newSVpvn ((char *)&data, sizeof data); |
|
|
1193 | } |
1383 | } |
1194 | OUTPUT: |
1384 | OUTPUT: |
1195 | RETVAL |
1385 | RETVAL |
1196 | |
1386 | |
1197 | void |
1387 | void |
1198 | _set_state (char *data_) |
1388 | _set_state (char *data_) |
1199 | PROTOTYPE: $ |
1389 | PROTOTYPE: $ |
1200 | CODE: |
1390 | CODE: |
1201 | { |
1391 | { |
1202 | struct { |
1392 | struct io_state *data = (void *)data_; |
1203 | int errorno; |
|
|
1204 | int laststype; |
|
|
1205 | int laststatval; |
|
|
1206 | Stat_t statcache; |
|
|
1207 | } *data = (void *)data_; |
|
|
1208 | |
1393 | |
1209 | errno = data->errorno; |
1394 | errno = data->errorno; |
1210 | PL_laststype = data->laststype; |
1395 | PL_laststype = data->laststype; |
1211 | PL_laststatval = data->laststatval; |
1396 | PL_laststatval = data->laststatval; |
1212 | PL_statcache = data->statcache; |
1397 | PL_statcache = data->statcache; |
1213 | } |
1398 | } |
|
|
1399 | |