|
|
1 | #define PERL_NO_GET_CONTEXT |
|
|
2 | |
|
|
3 | #include "libcoro/coro.c" |
|
|
4 | |
1 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
2 | #include "perl.h" |
6 | #include "perl.h" |
3 | #include "XSUB.h" |
7 | #include "XSUB.h" |
4 | |
8 | |
5 | #include "patchlevel.h" |
9 | #include "patchlevel.h" |
6 | |
10 | |
7 | #if PATCHLEVEL < 6 |
11 | #if PERL_VERSION < 6 |
8 | # ifndef PL_ppaddr |
12 | # ifndef PL_ppaddr |
9 | # define PL_ppaddr ppaddr |
13 | # define PL_ppaddr ppaddr |
10 | # endif |
14 | # endif |
11 | # ifndef call_sv |
15 | # ifndef call_sv |
12 | # define call_sv perl_call_sv |
16 | # define call_sv perl_call_sv |
… | |
… | |
23 | # ifndef IS_PADCONST |
27 | # ifndef IS_PADCONST |
24 | # define IS_PADCONST(v) 0 |
28 | # define IS_PADCONST(v) 0 |
25 | # endif |
29 | # endif |
26 | #endif |
30 | #endif |
27 | |
31 | |
28 | #include "libcoro/coro.c" |
|
|
29 | |
|
|
30 | #include <signal.h> |
32 | #include <signal.h> |
31 | |
33 | |
32 | #ifdef HAVE_MMAP |
34 | #ifdef HAVE_MMAP |
33 | # include <unistd.h> |
35 | # include <unistd.h> |
34 | # include <sys/mman.h> |
36 | # include <sys/mman.h> |
… | |
… | |
53 | |
55 | |
54 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
56 | #define labs(l) ((l) >= 0 ? (l) : -(l)) |
55 | |
57 | |
56 | #include "CoroAPI.h" |
58 | #include "CoroAPI.h" |
57 | |
59 | |
|
|
60 | #ifdef USE_ITHREADS |
|
|
61 | static perl_mutex coro_mutex; |
|
|
62 | # define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) |
|
|
63 | # define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) |
|
|
64 | #else |
|
|
65 | # define LOCK 0 |
|
|
66 | # define UNLOCK 0 |
|
|
67 | #endif |
|
|
68 | |
58 | static struct CoroAPI coroapi; |
69 | static struct CoroAPI coroapi; |
|
|
70 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
71 | static HV *coro_state_stash; |
|
|
72 | static SV *ucoro_state_sv; |
|
|
73 | static U32 ucoro_state_hash; |
|
|
74 | static SV *coro_mortal; /* will be freed after next transfer */ |
59 | |
75 | |
60 | /* this is actually not only the c stack but also c registers etc... */ |
76 | /* this is actually not only the c stack but also c registers etc... */ |
61 | typedef struct { |
77 | typedef struct { |
62 | int refcnt; /* pointer reference counter */ |
78 | int refcnt; /* pointer reference counter */ |
63 | int usecnt; /* shared by how many coroutines */ |
79 | int usecnt; /* shared by how many coroutines */ |
… | |
… | |
93 | AV *mainstack; |
109 | AV *mainstack; |
94 | SV **stack_sp; |
110 | SV **stack_sp; |
95 | OP *op; |
111 | OP *op; |
96 | SV **curpad; |
112 | SV **curpad; |
97 | AV *comppad; |
113 | AV *comppad; |
|
|
114 | CV *compcv; |
98 | SV **stack_base; |
115 | SV **stack_base; |
99 | SV **stack_max; |
116 | SV **stack_max; |
100 | SV **tmps_stack; |
117 | SV **tmps_stack; |
101 | I32 tmps_floor; |
118 | I32 tmps_floor; |
102 | I32 tmps_ix; |
119 | I32 tmps_ix; |
… | |
… | |
111 | I32 savestack_ix; |
128 | I32 savestack_ix; |
112 | I32 savestack_max; |
129 | I32 savestack_max; |
113 | OP **retstack; |
130 | OP **retstack; |
114 | I32 retstack_ix; |
131 | I32 retstack_ix; |
115 | I32 retstack_max; |
132 | I32 retstack_max; |
|
|
133 | PMOP *curpm; |
116 | COP *curcop; |
134 | COP *curcop; |
117 | JMPENV *top_env; |
135 | JMPENV *top_env; |
118 | |
136 | |
119 | /* data associated with this coroutine (initial args) */ |
137 | /* data associated with this coroutine (initial args) */ |
120 | AV *args; |
138 | AV *args; |
121 | }; |
139 | }; |
122 | |
140 | |
123 | typedef struct coro *Coro__State; |
141 | typedef struct coro *Coro__State; |
124 | typedef struct coro *Coro__State_or_hashref; |
142 | typedef struct coro *Coro__State_or_hashref; |
125 | |
143 | |
126 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
127 | static HV *coro_state_stash; |
|
|
128 | static SV *ucoro_state_sv; |
|
|
129 | static U32 ucoro_state_hash; |
|
|
130 | static HV *padlist_cache; |
|
|
131 | static SV *coro_mortal; /* will be freed after next transfer */ |
|
|
132 | |
|
|
133 | /* mostly copied from op.c:cv_clone2 */ |
144 | /* mostly copied from op.c:cv_clone2 */ |
134 | STATIC AV * |
145 | STATIC AV * |
135 | clone_padlist (AV *protopadlist) |
146 | clone_padlist (pTHX_ AV *protopadlist) |
136 | { |
147 | { |
137 | AV *av; |
148 | AV *av; |
138 | I32 ix; |
149 | I32 ix; |
139 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
150 | AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); |
140 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
151 | AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); |
… | |
… | |
164 | AvFLAGS (av) = AVf_REIFY; |
175 | AvFLAGS (av) = AVf_REIFY; |
165 | |
176 | |
166 | for (ix = fpad; ix > 0; ix--) |
177 | for (ix = fpad; ix > 0; ix--) |
167 | { |
178 | { |
168 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
179 | SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; |
|
|
180 | |
169 | if (namesv && namesv != &PL_sv_undef) |
181 | if (namesv && namesv != &PL_sv_undef) |
170 | { |
182 | { |
171 | char *name = SvPVX (namesv); /* XXX */ |
183 | char *name = SvPVX (namesv); /* XXX */ |
|
|
184 | |
172 | if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') |
185 | if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') |
173 | { /* lexical from outside? */ |
186 | { /* lexical from outside? */ |
174 | npad[ix] = SvREFCNT_inc (ppad[ix]); |
187 | npad[ix] = SvREFCNT_inc (ppad[ix]); |
175 | } |
188 | } |
176 | else |
189 | else |
… | |
… | |
182 | sv = (SV *) newAV (); |
195 | sv = (SV *) newAV (); |
183 | else if (*name == '%') |
196 | else if (*name == '%') |
184 | sv = (SV *) newHV (); |
197 | sv = (SV *) newHV (); |
185 | else |
198 | else |
186 | sv = NEWSV (0, 0); |
199 | sv = NEWSV (0, 0); |
|
|
200 | |
187 | #ifdef SvPADBUSY |
201 | #ifdef SvPADBUSY |
188 | if (!SvPADBUSY (sv)) |
202 | if (!SvPADBUSY (sv)) |
189 | #endif |
203 | #endif |
190 | SvPADMY_on (sv); |
204 | SvPADMY_on (sv); |
|
|
205 | |
191 | npad[ix] = sv; |
206 | npad[ix] = sv; |
192 | } |
207 | } |
193 | } |
208 | } |
194 | else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
209 | else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) |
195 | { |
210 | { |
… | |
… | |
225 | |
240 | |
226 | return newpadlist; |
241 | return newpadlist; |
227 | } |
242 | } |
228 | |
243 | |
229 | STATIC void |
244 | STATIC void |
230 | free_padlist (AV *padlist) |
245 | free_padlist (pTHX_ AV *padlist) |
231 | { |
246 | { |
232 | /* may be during global destruction */ |
247 | /* may be during global destruction */ |
233 | if (SvREFCNT (padlist)) |
248 | if (SvREFCNT (padlist)) |
234 | { |
249 | { |
235 | I32 i = AvFILLp (padlist); |
250 | I32 i = AvFILLp (padlist); |
… | |
… | |
249 | SvREFCNT_dec ((SV*)padlist); |
264 | SvREFCNT_dec ((SV*)padlist); |
250 | } |
265 | } |
251 | } |
266 | } |
252 | |
267 | |
253 | STATIC int |
268 | STATIC int |
254 | coro_cv_free (SV *sv, MAGIC *mg) |
269 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
255 | { |
270 | { |
256 | AV *padlist; |
271 | AV *padlist; |
257 | AV *av = (AV *)mg->mg_obj; |
272 | AV *av = (AV *)mg->mg_obj; |
258 | |
273 | |
259 | /* casting is fun. */ |
274 | /* casting is fun. */ |
260 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
275 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
261 | free_padlist (padlist); |
276 | free_padlist (aTHX_ padlist); |
|
|
277 | |
|
|
278 | SvREFCNT_dec (av); |
262 | } |
279 | } |
263 | |
280 | |
264 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
281 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
265 | |
282 | |
266 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
283 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
267 | |
284 | |
268 | /* the next two functions merely cache the padlists */ |
285 | /* the next two functions merely cache the padlists */ |
269 | STATIC void |
286 | STATIC void |
270 | get_padlist (CV *cv) |
287 | get_padlist (pTHX_ CV *cv) |
271 | { |
288 | { |
272 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
289 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
273 | |
290 | |
274 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
291 | if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) |
275 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
292 | CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); |
276 | else |
293 | else |
277 | CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); |
294 | CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv)); |
278 | } |
295 | } |
279 | |
296 | |
280 | STATIC void |
297 | STATIC void |
281 | put_padlist (CV *cv) |
298 | put_padlist (pTHX_ CV *cv) |
282 | { |
299 | { |
283 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
300 | MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
284 | |
301 | |
285 | if (!mg) |
302 | if (!mg) |
286 | { |
303 | { |
… | |
… | |
312 | PL_mainstack = c->mainstack; |
329 | PL_mainstack = c->mainstack; |
313 | PL_stack_sp = c->stack_sp; |
330 | PL_stack_sp = c->stack_sp; |
314 | PL_op = c->op; |
331 | PL_op = c->op; |
315 | PL_curpad = c->curpad; |
332 | PL_curpad = c->curpad; |
316 | PL_comppad = c->comppad; |
333 | PL_comppad = c->comppad; |
|
|
334 | PL_compcv = c->compcv; |
317 | PL_stack_base = c->stack_base; |
335 | PL_stack_base = c->stack_base; |
318 | PL_stack_max = c->stack_max; |
336 | PL_stack_max = c->stack_max; |
319 | PL_tmps_stack = c->tmps_stack; |
337 | PL_tmps_stack = c->tmps_stack; |
320 | PL_tmps_floor = c->tmps_floor; |
338 | PL_tmps_floor = c->tmps_floor; |
321 | PL_tmps_ix = c->tmps_ix; |
339 | PL_tmps_ix = c->tmps_ix; |
… | |
… | |
330 | PL_savestack_ix = c->savestack_ix; |
348 | PL_savestack_ix = c->savestack_ix; |
331 | PL_savestack_max = c->savestack_max; |
349 | PL_savestack_max = c->savestack_max; |
332 | PL_retstack = c->retstack; |
350 | PL_retstack = c->retstack; |
333 | PL_retstack_ix = c->retstack_ix; |
351 | PL_retstack_ix = c->retstack_ix; |
334 | PL_retstack_max = c->retstack_max; |
352 | PL_retstack_max = c->retstack_max; |
|
|
353 | PL_curpm = c->curpm; |
335 | PL_curcop = c->curcop; |
354 | PL_curcop = c->curcop; |
336 | PL_top_env = c->top_env; |
355 | PL_top_env = c->top_env; |
337 | |
356 | |
338 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
357 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
339 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
358 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
… | |
… | |
348 | { |
367 | { |
349 | AV *padlist = (AV *)POPs; |
368 | AV *padlist = (AV *)POPs; |
350 | |
369 | |
351 | if (padlist) |
370 | if (padlist) |
352 | { |
371 | { |
353 | put_padlist (cv); /* mark this padlist as available */ |
372 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
354 | CvPADLIST(cv) = padlist; |
373 | CvPADLIST(cv) = padlist; |
355 | #ifdef USE_THREADS |
|
|
356 | /*CvOWNER(cv) = (struct perl_thread *)POPs;*/ |
|
|
357 | #endif |
|
|
358 | } |
374 | } |
359 | |
375 | |
360 | ++CvDEPTH(cv); |
376 | ++CvDEPTH(cv); |
361 | } |
377 | } |
362 | |
378 | |
… | |
… | |
389 | if (CxTYPE(cx) == CXt_SUB) |
405 | if (CxTYPE(cx) == CXt_SUB) |
390 | { |
406 | { |
391 | CV *cv = cx->blk_sub.cv; |
407 | CV *cv = cx->blk_sub.cv; |
392 | if (CvDEPTH(cv)) |
408 | if (CvDEPTH(cv)) |
393 | { |
409 | { |
394 | #ifdef USE_THREADS |
|
|
395 | /*XPUSHs ((SV *)CvOWNER(cv));*/ |
|
|
396 | /*CvOWNER(cv) = 0;*/ |
|
|
397 | /*error must unlock this cv etc.. etc...*/ |
|
|
398 | #endif |
|
|
399 | EXTEND (SP, CvDEPTH(cv)*2); |
410 | EXTEND (SP, CvDEPTH(cv)*2); |
400 | |
411 | |
401 | while (--CvDEPTH(cv)) |
412 | while (--CvDEPTH(cv)) |
402 | { |
413 | { |
403 | /* this tells the restore code to increment CvDEPTH */ |
414 | /* this tells the restore code to increment CvDEPTH */ |
… | |
… | |
406 | } |
417 | } |
407 | |
418 | |
408 | PUSHs ((SV *)CvPADLIST(cv)); |
419 | PUSHs ((SV *)CvPADLIST(cv)); |
409 | PUSHs ((SV *)cv); |
420 | PUSHs ((SV *)cv); |
410 | |
421 | |
411 | get_padlist (cv); /* this is a monster */ |
422 | get_padlist (aTHX_ cv); /* this is a monster */ |
412 | } |
423 | } |
413 | } |
424 | } |
414 | #ifdef CXt_FORMAT |
425 | #ifdef CXt_FORMAT |
415 | else if (CxTYPE(cx) == CXt_FORMAT) |
426 | else if (CxTYPE(cx) == CXt_FORMAT) |
416 | { |
427 | { |
… | |
… | |
444 | c->mainstack = PL_mainstack; |
455 | c->mainstack = PL_mainstack; |
445 | c->stack_sp = PL_stack_sp; |
456 | c->stack_sp = PL_stack_sp; |
446 | c->op = PL_op; |
457 | c->op = PL_op; |
447 | c->curpad = PL_curpad; |
458 | c->curpad = PL_curpad; |
448 | c->comppad = PL_comppad; |
459 | c->comppad = PL_comppad; |
|
|
460 | c->compcv = PL_compcv; |
449 | c->stack_base = PL_stack_base; |
461 | c->stack_base = PL_stack_base; |
450 | c->stack_max = PL_stack_max; |
462 | c->stack_max = PL_stack_max; |
451 | c->tmps_stack = PL_tmps_stack; |
463 | c->tmps_stack = PL_tmps_stack; |
452 | c->tmps_floor = PL_tmps_floor; |
464 | c->tmps_floor = PL_tmps_floor; |
453 | c->tmps_ix = PL_tmps_ix; |
465 | c->tmps_ix = PL_tmps_ix; |
… | |
… | |
462 | c->savestack_ix = PL_savestack_ix; |
474 | c->savestack_ix = PL_savestack_ix; |
463 | c->savestack_max = PL_savestack_max; |
475 | c->savestack_max = PL_savestack_max; |
464 | c->retstack = PL_retstack; |
476 | c->retstack = PL_retstack; |
465 | c->retstack_ix = PL_retstack_ix; |
477 | c->retstack_ix = PL_retstack_ix; |
466 | c->retstack_max = PL_retstack_max; |
478 | c->retstack_max = PL_retstack_max; |
|
|
479 | c->curpm = PL_curpm; |
467 | c->curcop = PL_curcop; |
480 | c->curcop = PL_curcop; |
468 | c->top_env = PL_top_env; |
481 | c->top_env = PL_top_env; |
469 | } |
482 | } |
470 | |
483 | |
471 | /* |
484 | /* |
… | |
… | |
475 | * not usually need a lot of stackspace. |
488 | * not usually need a lot of stackspace. |
476 | */ |
489 | */ |
477 | STATIC void |
490 | STATIC void |
478 | coro_init_stacks (pTHX) |
491 | coro_init_stacks (pTHX) |
479 | { |
492 | { |
|
|
493 | LOCK; |
|
|
494 | |
480 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
495 | PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); |
481 | PL_curstackinfo->si_type = PERLSI_MAIN; |
496 | PL_curstackinfo->si_type = PERLSI_MAIN; |
482 | PL_curstack = PL_curstackinfo->si_stack; |
497 | PL_curstack = PL_curstackinfo->si_stack; |
483 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
498 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
484 | |
499 | |
… | |
… | |
508 | PL_savestack_max = 96; |
523 | PL_savestack_max = 96; |
509 | |
524 | |
510 | New(54,PL_retstack,8,OP*); |
525 | New(54,PL_retstack,8,OP*); |
511 | PL_retstack_ix = 0; |
526 | PL_retstack_ix = 0; |
512 | PL_retstack_max = 8; |
527 | PL_retstack_max = 8; |
|
|
528 | |
|
|
529 | UNLOCK; |
513 | } |
530 | } |
514 | |
531 | |
515 | /* |
532 | /* |
516 | * destroy the stacks, the callchain etc... |
533 | * destroy the stacks, the callchain etc... |
517 | */ |
534 | */ |
… | |
… | |
523 | /* is this ugly, I ask? */ |
540 | /* is this ugly, I ask? */ |
524 | LEAVE_SCOPE (0); |
541 | LEAVE_SCOPE (0); |
525 | |
542 | |
526 | /* sure it is, but more important: is it correct?? :/ */ |
543 | /* sure it is, but more important: is it correct?? :/ */ |
527 | FREETMPS; |
544 | FREETMPS; |
|
|
545 | |
|
|
546 | /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ |
528 | } |
547 | } |
529 | |
548 | |
530 | while (PL_curstackinfo->si_next) |
549 | while (PL_curstackinfo->si_next) |
531 | PL_curstackinfo = PL_curstackinfo->si_next; |
550 | PL_curstackinfo = PL_curstackinfo->si_next; |
532 | |
551 | |
533 | while (PL_curstackinfo) |
552 | while (PL_curstackinfo) |
534 | { |
553 | { |
535 | PERL_SI *p = PL_curstackinfo->si_prev; |
554 | PERL_SI *p = PL_curstackinfo->si_prev; |
536 | |
555 | |
537 | { |
556 | { /*D*//*remove*/ |
538 | dSP; |
557 | dSP; |
539 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
558 | SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); |
540 | PUTBACK; /* possibly superfluous */ |
559 | PUTBACK; /* possibly superfluous */ |
541 | } |
560 | } |
542 | |
561 | |
543 | if (!IN_DESTRUCT) |
562 | if (!IN_DESTRUCT) |
544 | { |
563 | { |
545 | dounwind(-1); |
564 | dounwind (-1);/*D*//*remove*/ |
546 | SvREFCNT_dec(PL_curstackinfo->si_stack); |
565 | SvREFCNT_dec (PL_curstackinfo->si_stack); |
547 | } |
566 | } |
548 | |
567 | |
549 | Safefree(PL_curstackinfo->si_cxstack); |
568 | Safefree (PL_curstackinfo->si_cxstack); |
550 | Safefree(PL_curstackinfo); |
569 | Safefree (PL_curstackinfo); |
551 | PL_curstackinfo = p; |
570 | PL_curstackinfo = p; |
552 | } |
571 | } |
553 | |
572 | |
554 | Safefree(PL_tmps_stack); |
573 | Safefree (PL_tmps_stack); |
555 | Safefree(PL_markstack); |
574 | Safefree (PL_markstack); |
556 | Safefree(PL_scopestack); |
575 | Safefree (PL_scopestack); |
557 | Safefree(PL_savestack); |
576 | Safefree (PL_savestack); |
558 | Safefree(PL_retstack); |
577 | Safefree (PL_retstack); |
559 | } |
578 | } |
560 | |
579 | |
561 | static void |
580 | static void |
562 | allocate_stack (Coro__State ctx, int alloc) |
581 | allocate_stack (Coro__State ctx, int alloc) |
563 | { |
582 | { |
… | |
… | |
566 | New (0, stack, 1, coro_stack); |
585 | New (0, stack, 1, coro_stack); |
567 | |
586 | |
568 | stack->refcnt = 1; |
587 | stack->refcnt = 1; |
569 | stack->usecnt = 1; |
588 | stack->usecnt = 1; |
570 | stack->gencnt = ctx->gencnt = 0; |
589 | stack->gencnt = ctx->gencnt = 0; |
|
|
590 | |
571 | if (alloc) |
591 | if (alloc) |
572 | { |
592 | { |
573 | #if HAVE_MMAP |
593 | #if HAVE_MMAP |
574 | stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ |
594 | stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */ |
575 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
595 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
… | |
… | |
616 | setup_coro (void *arg) |
636 | setup_coro (void *arg) |
617 | { |
637 | { |
618 | /* |
638 | /* |
619 | * emulate part of the perl startup here. |
639 | * emulate part of the perl startup here. |
620 | */ |
640 | */ |
|
|
641 | dTHX; |
621 | dSP; |
642 | dSP; |
622 | Coro__State ctx = (Coro__State)arg; |
643 | Coro__State ctx = (Coro__State)arg; |
623 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
644 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
624 | |
645 | |
625 | coro_init_stacks (aTHX); |
646 | coro_init_stacks (aTHX); |
… | |
… | |
674 | continue_coro (void *arg) |
695 | continue_coro (void *arg) |
675 | { |
696 | { |
676 | /* |
697 | /* |
677 | * this is a _very_ stripped down perl interpreter ;) |
698 | * this is a _very_ stripped down perl interpreter ;) |
678 | */ |
699 | */ |
|
|
700 | dTHX; |
679 | Coro__State ctx = (Coro__State)arg; |
701 | Coro__State ctx = (Coro__State)arg; |
680 | JMPENV coro_start_env; |
702 | JMPENV coro_start_env; |
681 | |
703 | |
682 | PL_top_env = &ctx->start_env; |
704 | PL_top_env = &ctx->start_env; |
683 | |
705 | |
… | |
… | |
687 | |
709 | |
688 | abort (); |
710 | abort (); |
689 | } |
711 | } |
690 | |
712 | |
691 | STATIC void |
713 | STATIC void |
692 | transfer(pTHX_ struct coro *prev, struct coro *next, int flags) |
714 | transfer (pTHX_ struct coro *prev, struct coro *next, int flags) |
693 | { |
715 | { |
694 | dSTACKLEVEL; |
716 | dSTACKLEVEL; |
695 | static struct coro *xnext; |
|
|
696 | |
717 | |
697 | if (prev != next) |
718 | if (prev != next) |
698 | { |
719 | { |
699 | xnext = next; |
|
|
700 | |
|
|
701 | if (next->mainstack) |
720 | if (next->mainstack) |
702 | { |
721 | { |
|
|
722 | LOCK; |
703 | SAVE (prev, flags); |
723 | SAVE (prev, flags); |
704 | LOAD (next); |
724 | LOAD (next); |
|
|
725 | UNLOCK; |
705 | |
726 | |
706 | /* mark this state as in-use */ |
727 | /* mark this state as in-use */ |
707 | next->mainstack = 0; |
728 | next->mainstack = 0; |
708 | next->tmps_ix = -2; |
729 | next->tmps_ix = -2; |
709 | |
730 | |
… | |
… | |
728 | continue_coro, (void *)next, |
749 | continue_coro, (void *)next, |
729 | next->stack->sptr, labs (next->stack->ssize)); |
750 | next->stack->sptr, labs (next->stack->ssize)); |
730 | } |
751 | } |
731 | |
752 | |
732 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
753 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
754 | prev->cursp = stacklevel; |
733 | /* don't add any code here */ |
755 | /* don't add any code here */ |
734 | } |
756 | } |
735 | |
757 | else |
|
|
758 | next->cursp = stacklevel; |
736 | } |
759 | } |
737 | else if (next->tmps_ix == -2) |
760 | else if (next->tmps_ix == -2) |
738 | croak ("tried to transfer to running coroutine"); |
761 | croak ("tried to transfer to running coroutine"); |
739 | else |
762 | else |
740 | { |
763 | { |
|
|
764 | LOCK; |
741 | SAVE (prev, -1); /* first get rid of the old state */ |
765 | SAVE (prev, -1); /* first get rid of the old state */ |
|
|
766 | UNLOCK; |
742 | |
767 | |
743 | if (flags & TRANSFER_SAVE_CCTXT) |
768 | if (flags & TRANSFER_SAVE_CCTXT) |
744 | { |
769 | { |
745 | if (!prev->stack) |
770 | if (!prev->stack) |
746 | allocate_stack (prev, 0); |
771 | allocate_stack (prev, 0); |
… | |
… | |
748 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
773 | if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) |
749 | { |
774 | { |
750 | PL_top_env = &next->start_env; |
775 | PL_top_env = &next->start_env; |
751 | |
776 | |
752 | setup_coro (next); |
777 | setup_coro (next); |
|
|
778 | next->cursp = stacklevel; |
753 | |
779 | |
754 | prev->stack->refcnt++; |
780 | prev->stack->refcnt++; |
755 | prev->stack->usecnt++; |
781 | prev->stack->usecnt++; |
756 | next->stack = prev->stack; |
782 | next->stack = prev->stack; |
757 | next->gencnt = prev->gencnt; |
783 | next->gencnt = prev->gencnt; |
… | |
… | |
762 | allocate_stack (next, 1); |
788 | allocate_stack (next, 1); |
763 | coro_create (&(next->stack->cctx), |
789 | coro_create (&(next->stack->cctx), |
764 | setup_coro, (void *)next, |
790 | setup_coro, (void *)next, |
765 | next->stack->sptr, labs (next->stack->ssize)); |
791 | next->stack->sptr, labs (next->stack->ssize)); |
766 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
792 | coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); |
|
|
793 | prev->cursp = stacklevel; |
767 | /* don't add any code here */ |
794 | /* don't add any code here */ |
768 | } |
795 | } |
769 | } |
796 | } |
770 | else |
797 | else |
|
|
798 | { |
771 | setup_coro (next); |
799 | setup_coro (next); |
|
|
800 | next->cursp = stacklevel; |
|
|
801 | } |
772 | } |
802 | } |
773 | |
|
|
774 | /* |
|
|
775 | * xnext is now either prev or next, depending on wether |
|
|
776 | * we switched the c stack or not. that's why I use a global |
|
|
777 | * variable, that should become thread-specific at one point. |
|
|
778 | */ |
|
|
779 | xnext->cursp = stacklevel; |
|
|
780 | } |
803 | } |
781 | |
804 | |
|
|
805 | LOCK; |
782 | if (coro_mortal) |
806 | if (coro_mortal) |
783 | { |
807 | { |
784 | SvREFCNT_dec (coro_mortal); |
808 | SvREFCNT_dec (coro_mortal); |
785 | coro_mortal = 0; |
809 | coro_mortal = 0; |
786 | } |
810 | } |
|
|
811 | UNLOCK; |
787 | } |
812 | } |
788 | |
813 | |
789 | #define SV_CORO(sv,func) \ |
814 | #define SV_CORO(sv,func) \ |
790 | do { \ |
815 | do { \ |
791 | if (SvROK (sv)) \ |
816 | if (SvROK (sv)) \ |
792 | sv = SvRV (sv); \ |
817 | sv = SvRV (sv); \ |
793 | \ |
818 | \ |
794 | if (SvTYPE(sv) == SVt_PVHV) \ |
819 | if (SvTYPE (sv) == SVt_PVHV) \ |
795 | { \ |
820 | { \ |
796 | HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
821 | HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ |
797 | \ |
822 | \ |
798 | if (!he) \ |
823 | if (!he) \ |
799 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
824 | croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ |
800 | \ |
825 | \ |
801 | (sv) = SvRV (HeVAL(he)); \ |
826 | (sv) = SvRV (HeVAL(he)); \ |
802 | } \ |
827 | } \ |
803 | \ |
828 | \ |
804 | /* must also be changed inside Coro::Cont::yield */ \ |
829 | /* must also be changed inside Coro::Cont::yield */ \ |
805 | if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ |
830 | if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ |
806 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
831 | croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ |
807 | \ |
832 | \ |
808 | } while(0) |
833 | } while(0) |
809 | |
834 | |
810 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
835 | #define SvSTATE(sv) (struct coro *)SvIV (sv) |
… | |
… | |
813 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
838 | api_transfer(pTHX_ SV *prev, SV *next, int flags) |
814 | { |
839 | { |
815 | SV_CORO (prev, "Coro::transfer"); |
840 | SV_CORO (prev, "Coro::transfer"); |
816 | SV_CORO (next, "Coro::transfer"); |
841 | SV_CORO (next, "Coro::transfer"); |
817 | |
842 | |
818 | transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); |
843 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
819 | } |
844 | } |
820 | |
845 | |
821 | /** Coro ********************************************************************/ |
846 | /** Coro ********************************************************************/ |
822 | |
847 | |
823 | #define PRIO_MAX 3 |
848 | #define PRIO_MAX 3 |
… | |
… | |
831 | static GV *coro_current, *coro_idle; |
856 | static GV *coro_current, *coro_idle; |
832 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
857 | static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; |
833 | static int coro_nready; |
858 | static int coro_nready; |
834 | |
859 | |
835 | static void |
860 | static void |
836 | coro_enq (SV *sv) |
861 | coro_enq (pTHX_ SV *sv) |
837 | { |
862 | { |
838 | if (SvTYPE (sv) == SVt_PVHV) |
863 | if (SvTYPE (sv) == SVt_PVHV) |
839 | { |
864 | { |
840 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
865 | SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); |
841 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
866 | int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; |
… | |
… | |
852 | |
877 | |
853 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
878 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
854 | } |
879 | } |
855 | |
880 | |
856 | static SV * |
881 | static SV * |
857 | coro_deq (int min_prio) |
882 | coro_deq (pTHX_ int min_prio) |
858 | { |
883 | { |
859 | int prio = PRIO_MAX - PRIO_MIN; |
884 | int prio = PRIO_MAX - PRIO_MIN; |
860 | |
885 | |
861 | min_prio -= PRIO_MIN; |
886 | min_prio -= PRIO_MIN; |
862 | if (min_prio < 0) |
887 | if (min_prio < 0) |
… | |
… | |
873 | } |
898 | } |
874 | |
899 | |
875 | static void |
900 | static void |
876 | api_ready (SV *coro) |
901 | api_ready (SV *coro) |
877 | { |
902 | { |
|
|
903 | dTHX; |
|
|
904 | |
878 | if (SvROK (coro)) |
905 | if (SvROK (coro)) |
879 | coro = SvRV (coro); |
906 | coro = SvRV (coro); |
880 | |
907 | |
|
|
908 | LOCK; |
881 | coro_enq (SvREFCNT_inc (coro)); |
909 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
|
|
910 | UNLOCK; |
882 | } |
911 | } |
883 | |
912 | |
884 | static void |
913 | static void |
885 | api_schedule (void) |
914 | api_schedule (void) |
886 | { |
915 | { |
|
|
916 | dTHX; |
|
|
917 | |
887 | SV *prev, *next; |
918 | SV *prev, *next; |
888 | |
919 | |
|
|
920 | LOCK; |
|
|
921 | |
889 | prev = SvRV (GvSV (coro_current)); |
922 | prev = SvRV (GvSV (coro_current)); |
890 | next = coro_deq (PRIO_MIN); |
923 | next = coro_deq (aTHX_ PRIO_MIN); |
891 | |
924 | |
892 | if (!next) |
925 | if (!next) |
893 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
926 | next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); |
894 | |
927 | |
895 | /* free this only after the transfer */ |
928 | /* free this only after the transfer */ |
… | |
… | |
898 | |
931 | |
899 | SvRV (GvSV (coro_current)) = next; |
932 | SvRV (GvSV (coro_current)) = next; |
900 | |
933 | |
901 | SV_CORO (next, "Coro::schedule"); |
934 | SV_CORO (next, "Coro::schedule"); |
902 | |
935 | |
|
|
936 | UNLOCK; |
|
|
937 | |
903 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
938 | transfer (aTHX_ SvSTATE (prev), SvSTATE (next), |
904 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
939 | TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); |
905 | } |
940 | } |
906 | |
941 | |
907 | static void |
942 | static void |
908 | api_cede (void) |
943 | api_cede (void) |
909 | { |
944 | { |
|
|
945 | dTHX; |
|
|
946 | |
|
|
947 | LOCK; |
910 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
948 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
949 | UNLOCK; |
911 | |
950 | |
912 | api_schedule (); |
951 | api_schedule (); |
913 | } |
952 | } |
914 | |
953 | |
915 | MODULE = Coro::State PACKAGE = Coro::State |
954 | MODULE = Coro::State PACKAGE = Coro::State |
916 | |
955 | |
917 | PROTOTYPES: ENABLE |
956 | PROTOTYPES: ENABLE |
918 | |
957 | |
919 | BOOT: |
958 | BOOT: |
920 | { /* {} necessary for stoopid perl-5.6.x */ |
959 | { /* {} necessary for stoopid perl-5.6.x */ |
|
|
960 | #ifdef USE_ITHREADS |
|
|
961 | MUTEX_INIT (&coro_mutex); |
|
|
962 | #endif |
|
|
963 | |
921 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
964 | ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); |
922 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
965 | PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); |
923 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
966 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
924 | |
967 | |
925 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
968 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); |
926 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
969 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); |
927 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
970 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); |
928 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
971 | newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); |
929 | |
|
|
930 | if (!padlist_cache) |
|
|
931 | padlist_cache = newHV (); |
|
|
932 | |
972 | |
933 | main_mainstack = PL_mainstack; |
973 | main_mainstack = PL_mainstack; |
934 | |
974 | |
935 | coroapi.ver = CORO_API_VERSION; |
975 | coroapi.ver = CORO_API_VERSION; |
936 | coroapi.transfer = api_transfer; |
976 | coroapi.transfer = api_transfer; |
… | |
… | |
947 | croak ("Coro::State::_newprocess expects an arrayref"); |
987 | croak ("Coro::State::_newprocess expects an arrayref"); |
948 | |
988 | |
949 | Newz (0, coro, 1, struct coro); |
989 | Newz (0, coro, 1, struct coro); |
950 | |
990 | |
951 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
991 | coro->args = (AV *)SvREFCNT_inc (SvRV (args)); |
952 | coro->mainstack = 0; /* actual work is done inside transfer */ |
992 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
953 | coro->stack = 0; |
993 | /*coro->stack = 0;*/ |
954 | |
994 | |
955 | /* same as JMPENV_BOOTSTRAP */ |
995 | /* same as JMPENV_BOOTSTRAP */ |
956 | /* we might be able to recycle start_env, but safe is safe */ |
996 | /* we might be able to recycle start_env, but safe is safe */ |
957 | //Zero(&coro->start_env, 1, JMPENV); |
997 | /*Zero(&coro->start_env, 1, JMPENV);*/ |
958 | coro->start_env.je_ret = -1; |
998 | coro->start_env.je_ret = -1; |
959 | coro->start_env.je_mustcatch = TRUE; |
999 | coro->start_env.je_mustcatch = TRUE; |
960 | |
1000 | |
961 | RETVAL = coro; |
1001 | RETVAL = coro; |
962 | OUTPUT: |
1002 | OUTPUT: |
… | |
… | |
983 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1023 | if (coro->mainstack && coro->mainstack != main_mainstack) |
984 | { |
1024 | { |
985 | struct coro temp; |
1025 | struct coro temp; |
986 | |
1026 | |
987 | PUTBACK; |
1027 | PUTBACK; |
988 | SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); |
1028 | SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); |
989 | LOAD(aTHX_ coro); |
1029 | LOAD (aTHX_ coro); |
990 | SPAGAIN; |
1030 | SPAGAIN; |
991 | |
1031 | |
992 | destroy_stacks (aTHX); |
1032 | destroy_stacks (aTHX); |
993 | |
1033 | |
994 | LOAD((&temp)); /* this will get rid of defsv etc.. */ |
1034 | LOAD ((&temp)); /* this will get rid of defsv etc.. */ |
995 | SPAGAIN; |
1035 | SPAGAIN; |
996 | |
1036 | |
997 | coro->mainstack = 0; |
1037 | coro->mainstack = 0; |
998 | } |
1038 | } |
999 | |
1039 | |
… | |
… | |
1014 | |
1054 | |
1015 | void |
1055 | void |
1016 | yield(...) |
1056 | yield(...) |
1017 | PROTOTYPE: @ |
1057 | PROTOTYPE: @ |
1018 | CODE: |
1058 | CODE: |
1019 | static SV *returnstk; |
1059 | SV *yieldstack; |
1020 | SV *sv; |
1060 | SV *sv; |
1021 | AV *defav = GvAV (PL_defgv); |
1061 | AV *defav = GvAV (PL_defgv); |
1022 | struct coro *prev, *next; |
1062 | struct coro *prev, *next; |
1023 | |
1063 | |
1024 | if (!returnstk) |
1064 | yieldstack = *hv_fetch ( |
1025 | returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); |
1065 | (HV *)SvRV (GvSV (coro_current)), |
|
|
1066 | "yieldstack", sizeof ("yieldstack") - 1, |
|
|
1067 | 0 |
|
|
1068 | ); |
1026 | |
1069 | |
1027 | /* set up @_ -- ugly */ |
1070 | /* set up @_ -- ugly */ |
1028 | av_clear (defav); |
1071 | av_clear (defav); |
1029 | av_fill (defav, items - 1); |
1072 | av_fill (defav, items - 1); |
1030 | while (items--) |
1073 | while (items--) |
1031 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1074 | av_store (defav, items, SvREFCNT_inc (ST(items))); |
1032 | |
1075 | |
1033 | mg_get (returnstk); /* isn't documentation wrong for mg_get? */ |
|
|
1034 | sv = av_pop ((AV *)SvRV (returnstk)); |
1076 | sv = av_pop ((AV *)SvRV (yieldstack)); |
1035 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1077 | prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); |
1036 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1078 | next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); |
1037 | SvREFCNT_dec (sv); |
1079 | SvREFCNT_dec (sv); |
1038 | |
1080 | |
1039 | transfer(aTHX_ prev, next, 0); |
1081 | transfer (aTHX_ prev, next, 0); |
1040 | |
1082 | |
1041 | MODULE = Coro::State PACKAGE = Coro |
1083 | MODULE = Coro::State PACKAGE = Coro |
1042 | |
1084 | |
1043 | # this is slightly dirty (should expose a c-level api) |
1085 | # this is slightly dirty (should expose a c-level api) |
1044 | |
1086 | |