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