ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.51 by pcg, Sun Nov 30 17:03:48 2003 UTC vs.
Revision 1.77 by root, Tue Oct 31 23:55:33 2006 UTC

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 <errno.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>
35# ifndef MAP_ANONYMOUS 37# ifndef MAP_ANONYMOUS
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
61static 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 (void)0
66# define UNLOCK (void)0
67#endif
68
58static struct CoroAPI coroapi; 69static struct CoroAPI coroapi;
70static AV *main_mainstack; /* used to differentiate between $main and others */
71static HV *coro_state_stash;
72static SV *ucoro_state_sv;
73static U32 ucoro_state_hash;
74static 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... */
61typedef struct { 77typedef 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
123typedef struct coro *Coro__State; 141typedef struct coro *Coro__State;
124typedef struct coro *Coro__State_or_hashref; 142typedef struct coro *Coro__State_or_hashref;
125 143
126static AV *main_mainstack; /* used to differentiate between $main and others */ 144static AV *
127static HV *coro_state_stash; 145coro_clone_padlist (pTHX_ CV *cv)
128static SV *ucoro_state_sv;
129static U32 ucoro_state_hash;
130static HV *padlist_cache;
131static SV *coro_mortal; /* will be freed after next transfer */
132
133/* mostly copied from op.c:cv_clone2 */
134STATIC AV *
135clone_padlist (AV *protopadlist)
136{ 146{
137 AV *av; 147 AV *padlist = CvPADLIST (cv);
138 I32 ix;
139 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE);
140 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE);
141 SV **pname = AvARRAY (protopad_name);
142 SV **ppad = AvARRAY (protopad);
143 I32 fname = AvFILLp (protopad_name);
144 I32 fpad = AvFILLp (protopad);
145 AV *newpadlist, *newpad_name, *newpad; 148 AV *newpadlist, *newpad;
146 SV **npad;
147
148 newpad_name = newAV ();
149 for (ix = fname; ix >= 0; ix--)
150 av_store (newpad_name, ix, SvREFCNT_inc (pname[ix]));
151
152 newpad = newAV ();
153 av_fill (newpad, AvFILLp (protopad));
154 npad = AvARRAY (newpad);
155 149
156 newpadlist = newAV (); 150 newpadlist = newAV ();
157 AvREAL_off (newpadlist); 151 AvREAL_off (newpadlist);
158 av_store (newpadlist, 0, (SV *) newpad_name); 152 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
153 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
154 --AvFILLp (padlist);
155
156 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
159 av_store (newpadlist, 1, (SV *) newpad); 157 av_store (newpadlist, 1, (SV *)newpad);
160
161 av = newAV (); /* will be @_ */
162 av_extend (av, 0);
163 av_store (newpad, 0, (SV *) av);
164 AvFLAGS (av) = AVf_REIFY;
165
166 for (ix = fpad; ix > 0; ix--)
167 {
168 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv;
169 if (namesv && namesv != &PL_sv_undef)
170 {
171 char *name = SvPVX (namesv); /* XXX */
172 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&')
173 { /* lexical from outside? */
174 npad[ix] = SvREFCNT_inc (ppad[ix]);
175 }
176 else
177 { /* our own lexical */
178 SV *sv;
179 if (*name == '&')
180 sv = SvREFCNT_inc (ppad[ix]);
181 else if (*name == '@')
182 sv = (SV *) newAV ();
183 else if (*name == '%')
184 sv = (SV *) newHV ();
185 else
186 sv = NEWSV (0, 0);
187#ifdef SvPADBUSY
188 if (!SvPADBUSY (sv))
189#endif
190 SvPADMY_on (sv);
191 npad[ix] = sv;
192 }
193 }
194 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix]))
195 {
196 npad[ix] = SvREFCNT_inc (ppad[ix]);
197 }
198 else
199 {
200 SV *sv = NEWSV (0, 0);
201 SvPADTMP_on (sv);
202 npad[ix] = sv;
203 }
204 }
205
206#if 0 /* return -ENOTUNDERSTOOD */
207 /* Now that vars are all in place, clone nested closures. */
208
209 for (ix = fpad; ix > 0; ix--) {
210 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
211 if (namesv
212 && namesv != &PL_sv_undef
213 && !(SvFLAGS(namesv) & SVf_FAKE)
214 && *SvPVX(namesv) == '&'
215 && CvCLONE(ppad[ix]))
216 {
217 CV *kid = cv_clone((CV*)ppad[ix]);
218 SvREFCNT_dec(ppad[ix]);
219 CvCLONE_on(kid);
220 SvPADMY_on(kid);
221 npad[ix] = (SV*)kid;
222 }
223 }
224#endif
225 158
226 return newpadlist; 159 return newpadlist;
227} 160}
228 161
229STATIC void 162static void
230free_padlist (AV *padlist) 163free_padlist (pTHX_ AV *padlist)
231{ 164{
232 /* may be during global destruction */ 165 /* may be during global destruction */
233 if (SvREFCNT (padlist)) 166 if (SvREFCNT (padlist))
234 { 167 {
235 I32 i = AvFILLp (padlist); 168 I32 i = AvFILLp (padlist);
248 181
249 SvREFCNT_dec ((SV*)padlist); 182 SvREFCNT_dec ((SV*)padlist);
250 } 183 }
251} 184}
252 185
253STATIC int 186static int
254coro_cv_free (SV *sv, MAGIC *mg) 187coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
255{ 188{
256 AV *padlist; 189 AV *padlist;
257 AV *av = (AV *)mg->mg_obj; 190 AV *av = (AV *)mg->mg_obj;
258 191
259 /* casting is fun. */ 192 /* casting is fun. */
260 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 193 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
261 free_padlist (padlist); 194 free_padlist (aTHX_ padlist);
195
196 SvREFCNT_dec (av);
197
198 return 0;
262} 199}
263 200
264#define PERL_MAGIC_coro PERL_MAGIC_ext 201#define PERL_MAGIC_coro PERL_MAGIC_ext
265 202
266static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 203static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
267 204
268/* the next two functions merely cache the padlists */ 205/* the next two functions merely cache the padlists */
269STATIC void 206static void
270get_padlist (CV *cv) 207get_padlist (pTHX_ CV *cv)
271{ 208{
272 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 209 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
273 210
274 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 211 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
275 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 212 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
276 else 213 else
214 {
215#if 0
216 /* this should work - but it doesn't :( */
217 CV *cp = Perl_cv_clone (aTHX_ cv);
218 CvPADLIST (cv) = CvPADLIST (cp);
219 CvPADLIST (cp) = 0;
220 SvREFCNT_dec (cp);
221#else
277 CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); 222 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
223#endif
224 }
278} 225}
279 226
280STATIC void 227static void
281put_padlist (CV *cv) 228put_padlist (pTHX_ CV *cv)
282{ 229{
283 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 230 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
284 231
285 if (!mg) 232 if (!mg)
286 { 233 {
312 PL_mainstack = c->mainstack; 259 PL_mainstack = c->mainstack;
313 PL_stack_sp = c->stack_sp; 260 PL_stack_sp = c->stack_sp;
314 PL_op = c->op; 261 PL_op = c->op;
315 PL_curpad = c->curpad; 262 PL_curpad = c->curpad;
316 PL_comppad = c->comppad; 263 PL_comppad = c->comppad;
264 PL_compcv = c->compcv;
317 PL_stack_base = c->stack_base; 265 PL_stack_base = c->stack_base;
318 PL_stack_max = c->stack_max; 266 PL_stack_max = c->stack_max;
319 PL_tmps_stack = c->tmps_stack; 267 PL_tmps_stack = c->tmps_stack;
320 PL_tmps_floor = c->tmps_floor; 268 PL_tmps_floor = c->tmps_floor;
321 PL_tmps_ix = c->tmps_ix; 269 PL_tmps_ix = c->tmps_ix;
327 PL_scopestack_ix = c->scopestack_ix; 275 PL_scopestack_ix = c->scopestack_ix;
328 PL_scopestack_max = c->scopestack_max; 276 PL_scopestack_max = c->scopestack_max;
329 PL_savestack = c->savestack; 277 PL_savestack = c->savestack;
330 PL_savestack_ix = c->savestack_ix; 278 PL_savestack_ix = c->savestack_ix;
331 PL_savestack_max = c->savestack_max; 279 PL_savestack_max = c->savestack_max;
280#if PERL_VERSION < 9
332 PL_retstack = c->retstack; 281 PL_retstack = c->retstack;
333 PL_retstack_ix = c->retstack_ix; 282 PL_retstack_ix = c->retstack_ix;
334 PL_retstack_max = c->retstack_max; 283 PL_retstack_max = c->retstack_max;
284#endif
285 PL_curpm = c->curpm;
335 PL_curcop = c->curcop; 286 PL_curcop = c->curcop;
336 PL_top_env = c->top_env; 287 PL_top_env = c->top_env;
337 288
338 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 289 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
339 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 290 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
348 { 299 {
349 AV *padlist = (AV *)POPs; 300 AV *padlist = (AV *)POPs;
350 301
351 if (padlist) 302 if (padlist)
352 { 303 {
353 put_padlist (cv); /* mark this padlist as available */ 304 put_padlist (aTHX_ cv); /* mark this padlist as available */
354 CvPADLIST(cv) = padlist; 305 CvPADLIST(cv) = padlist;
355#ifdef USE_THREADS
356 /*CvOWNER(cv) = (struct perl_thread *)POPs;*/
357#endif
358 } 306 }
359 307
360 ++CvDEPTH(cv); 308 ++CvDEPTH(cv);
361 } 309 }
362 310
389 if (CxTYPE(cx) == CXt_SUB) 337 if (CxTYPE(cx) == CXt_SUB)
390 { 338 {
391 CV *cv = cx->blk_sub.cv; 339 CV *cv = cx->blk_sub.cv;
392 if (CvDEPTH(cv)) 340 if (CvDEPTH(cv))
393 { 341 {
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); 342 EXTEND (SP, CvDEPTH(cv)*2);
400 343
401 while (--CvDEPTH(cv)) 344 while (--CvDEPTH(cv))
402 { 345 {
403 /* this tells the restore code to increment CvDEPTH */ 346 /* this tells the restore code to increment CvDEPTH */
406 } 349 }
407 350
408 PUSHs ((SV *)CvPADLIST(cv)); 351 PUSHs ((SV *)CvPADLIST(cv));
409 PUSHs ((SV *)cv); 352 PUSHs ((SV *)cv);
410 353
411 get_padlist (cv); /* this is a monster */ 354 get_padlist (aTHX_ cv);
412 } 355 }
413 } 356 }
414#ifdef CXt_FORMAT 357#ifdef CXt_FORMAT
415 else if (CxTYPE(cx) == CXt_FORMAT) 358 else if (CxTYPE(cx) == CXt_FORMAT)
416 { 359 {
444 c->mainstack = PL_mainstack; 387 c->mainstack = PL_mainstack;
445 c->stack_sp = PL_stack_sp; 388 c->stack_sp = PL_stack_sp;
446 c->op = PL_op; 389 c->op = PL_op;
447 c->curpad = PL_curpad; 390 c->curpad = PL_curpad;
448 c->comppad = PL_comppad; 391 c->comppad = PL_comppad;
392 c->compcv = PL_compcv;
449 c->stack_base = PL_stack_base; 393 c->stack_base = PL_stack_base;
450 c->stack_max = PL_stack_max; 394 c->stack_max = PL_stack_max;
451 c->tmps_stack = PL_tmps_stack; 395 c->tmps_stack = PL_tmps_stack;
452 c->tmps_floor = PL_tmps_floor; 396 c->tmps_floor = PL_tmps_floor;
453 c->tmps_ix = PL_tmps_ix; 397 c->tmps_ix = PL_tmps_ix;
459 c->scopestack_ix = PL_scopestack_ix; 403 c->scopestack_ix = PL_scopestack_ix;
460 c->scopestack_max = PL_scopestack_max; 404 c->scopestack_max = PL_scopestack_max;
461 c->savestack = PL_savestack; 405 c->savestack = PL_savestack;
462 c->savestack_ix = PL_savestack_ix; 406 c->savestack_ix = PL_savestack_ix;
463 c->savestack_max = PL_savestack_max; 407 c->savestack_max = PL_savestack_max;
408#if PERL_VERSION < 9
464 c->retstack = PL_retstack; 409 c->retstack = PL_retstack;
465 c->retstack_ix = PL_retstack_ix; 410 c->retstack_ix = PL_retstack_ix;
466 c->retstack_max = PL_retstack_max; 411 c->retstack_max = PL_retstack_max;
412#endif
413 c->curpm = PL_curpm;
467 c->curcop = PL_curcop; 414 c->curcop = PL_curcop;
468 c->top_env = PL_top_env; 415 c->top_env = PL_top_env;
469} 416}
470 417
471/* 418/*
472 * allocate various perl stacks. This is an exact copy 419 * allocate various perl stacks. This is an exact copy
473 * of perl.c:init_stacks, except that it uses less memory 420 * of perl.c:init_stacks, except that it uses less memory
474 * on the assumption that coroutines do not usually need 421 * on the (sometimes correct) assumption that coroutines do
475 * a lot of stackspace. 422 * not usually need a lot of stackspace.
476 */ 423 */
477STATIC void 424static void
478coro_init_stacks (pTHX) 425coro_init_stacks (pTHX)
479{ 426{
427 LOCK;
428
480 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 429 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
481 PL_curstackinfo->si_type = PERLSI_MAIN; 430 PL_curstackinfo->si_type = PERLSI_MAIN;
482 PL_curstack = PL_curstackinfo->si_stack; 431 PL_curstack = PL_curstackinfo->si_stack;
483 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 432 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
484 433
505 454
506 New(54,PL_savestack,96,ANY); 455 New(54,PL_savestack,96,ANY);
507 PL_savestack_ix = 0; 456 PL_savestack_ix = 0;
508 PL_savestack_max = 96; 457 PL_savestack_max = 96;
509 458
459#if PERL_VERSION < 9
510 New(54,PL_retstack,8,OP*); 460 New(54,PL_retstack,8,OP*);
511 PL_retstack_ix = 0; 461 PL_retstack_ix = 0;
512 PL_retstack_max = 8; 462 PL_retstack_max = 8;
463#endif
464
465 UNLOCK;
513} 466}
514 467
515/* 468/*
516 * destroy the stacks, the callchain etc... 469 * destroy the stacks, the callchain etc...
517 */ 470 */
518STATIC void 471static void
519destroy_stacks(pTHX) 472destroy_stacks(pTHX)
520{ 473{
521 if (!IN_DESTRUCT) 474 if (!IN_DESTRUCT)
522 { 475 {
523 /* is this ugly, I ask? */ 476 /* is this ugly, I ask? */
524 while (PL_scopestack_ix) 477 LEAVE_SCOPE (0);
525 LEAVE;
526 478
527 /* sure it is, but more important: is it correct?? :/ */ 479 /* sure it is, but more important: is it correct?? :/ */
528 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
529 FREETMPS; 480 FREETMPS;
481
482 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/
530 } 483 }
531 484
532 while (PL_curstackinfo->si_next) 485 while (PL_curstackinfo->si_next)
533 PL_curstackinfo = PL_curstackinfo->si_next; 486 PL_curstackinfo = PL_curstackinfo->si_next;
534 487
535 while (PL_curstackinfo) 488 while (PL_curstackinfo)
536 { 489 {
537 PERL_SI *p = PL_curstackinfo->si_prev; 490 PERL_SI *p = PL_curstackinfo->si_prev;
538 491
539 { 492 { /*D*//*remove*/
540 dSP; 493 dSP;
541 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 494 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
542 PUTBACK; /* possibly superfluous */ 495 PUTBACK; /* possibly superfluous */
543 } 496 }
544 497
545 if (!IN_DESTRUCT) 498 if (!IN_DESTRUCT)
546 { 499 {
547 dounwind(-1); 500 dounwind (-1);/*D*//*remove*/
548 SvREFCNT_dec(PL_curstackinfo->si_stack); 501 SvREFCNT_dec (PL_curstackinfo->si_stack);
549 } 502 }
550 503
551 Safefree(PL_curstackinfo->si_cxstack); 504 Safefree (PL_curstackinfo->si_cxstack);
552 Safefree(PL_curstackinfo); 505 Safefree (PL_curstackinfo);
553 PL_curstackinfo = p; 506 PL_curstackinfo = p;
554 } 507 }
555 508
556 Safefree(PL_tmps_stack); 509 Safefree (PL_tmps_stack);
557 Safefree(PL_markstack); 510 Safefree (PL_markstack);
558 Safefree(PL_scopestack); 511 Safefree (PL_scopestack);
559 Safefree(PL_savestack); 512 Safefree (PL_savestack);
513#if PERL_VERSION < 9
560 Safefree(PL_retstack); 514 Safefree (PL_retstack);
515#endif
561} 516}
562 517
563static void 518static void
564allocate_stack (Coro__State ctx, int alloc) 519allocate_stack (Coro__State ctx, int alloc)
565{ 520{
568 New (0, stack, 1, coro_stack); 523 New (0, stack, 1, coro_stack);
569 524
570 stack->refcnt = 1; 525 stack->refcnt = 1;
571 stack->usecnt = 1; 526 stack->usecnt = 1;
572 stack->gencnt = ctx->gencnt = 0; 527 stack->gencnt = ctx->gencnt = 0;
528
573 if (alloc) 529 if (alloc)
574 { 530 {
575#if HAVE_MMAP 531#if HAVE_MMAP
576 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ 532 stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */
577 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 533 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
578 if (stack->sptr == (void *)-1) 534 if (stack->sptr == (void *)-1)
579#endif 535#endif
580 { 536 {
581 /*FIXME*//*D*//* reasonable stack size! */
582 stack->ssize = - (16384 * sizeof (long)); 537 stack->ssize = - (STACKSIZE * (long)sizeof (long));
583 New (0, stack->sptr, 16384, long); 538 New (0, stack->sptr, STACKSIZE, long);
584 } 539 }
585 } 540 }
586 else 541 else
587 stack->sptr = 0; 542 stack->sptr = 0;
588 543
618setup_coro (void *arg) 573setup_coro (void *arg)
619{ 574{
620 /* 575 /*
621 * emulate part of the perl startup here. 576 * emulate part of the perl startup here.
622 */ 577 */
578 dTHX;
623 dSP; 579 dSP;
624 Coro__State ctx = (Coro__State)arg; 580 Coro__State ctx = (Coro__State)arg;
625 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 581 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE);
626 582
627 coro_init_stacks (aTHX); 583 coro_init_stacks (aTHX);
628 /*PL_curcop = 0;*/ 584 /*PL_curcop = 0;*/
629 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 585 /*PL_in_eval = PL_in_eval;*/ /* inherit */
630 SvREFCNT_dec (GvAV (PL_defgv)); 586 SvREFCNT_dec (GvAV (PL_defgv));
676continue_coro (void *arg) 632continue_coro (void *arg)
677{ 633{
678 /* 634 /*
679 * this is a _very_ stripped down perl interpreter ;) 635 * this is a _very_ stripped down perl interpreter ;)
680 */ 636 */
637 dTHX;
681 Coro__State ctx = (Coro__State)arg; 638 Coro__State ctx = (Coro__State)arg;
682 JMPENV coro_start_env;
683 639
684 PL_top_env = &ctx->start_env; 640 PL_top_env = &ctx->start_env;
685 641
686 ctx->cursp = 0; 642 ctx->cursp = 0;
687 PL_op = PL_op->op_next; 643 PL_op = PL_op->op_next;
688 CALLRUNOPS(aTHX); 644 CALLRUNOPS(aTHX);
689 645
690 abort (); 646 abort ();
691} 647}
692 648
693STATIC void 649static void
694transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 650transfer (pTHX_ struct coro *prev, struct coro *next, int flags)
695{ 651{
696 dSTACKLEVEL; 652 dSTACKLEVEL;
697 static struct coro *xnext;
698 653
699 if (prev != next) 654 if (prev != next)
700 { 655 {
701 xnext = next;
702
703 if (next->mainstack) 656 if (next->mainstack)
704 { 657 {
658 LOCK;
705 SAVE (prev, flags); 659 SAVE (prev, flags);
706 LOAD (next); 660 LOAD (next);
661 UNLOCK;
707 662
708 /* mark this state as in-use */ 663 /* mark this state as in-use */
709 next->mainstack = 0; 664 next->mainstack = 0;
710 next->tmps_ix = -2; 665 next->tmps_ix = -2;
711 666
730 continue_coro, (void *)next, 685 continue_coro, (void *)next,
731 next->stack->sptr, labs (next->stack->ssize)); 686 next->stack->sptr, labs (next->stack->ssize));
732 } 687 }
733 688
734 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 689 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
690 prev->cursp = stacklevel;
735 /* don't add any code here */ 691 /* don't add any code here */
736 } 692 }
737 693 else
694 next->cursp = stacklevel;
738 } 695 }
739 else if (next->tmps_ix == -2) 696 else if (next->tmps_ix == -2)
740 croak ("tried to transfer to running coroutine"); 697 croak ("tried to transfer to running coroutine");
741 else 698 else
742 { 699 {
700 LOCK;
743 SAVE (prev, -1); /* first get rid of the old state */ 701 SAVE (prev, -1); /* first get rid of the old state */
702 UNLOCK;
744 703
745 if (flags & TRANSFER_SAVE_CCTXT) 704 if (flags & TRANSFER_SAVE_CCTXT)
746 { 705 {
747 if (!prev->stack) 706 if (!prev->stack)
748 allocate_stack (prev, 0); 707 allocate_stack (prev, 0);
750 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 709 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
751 { 710 {
752 PL_top_env = &next->start_env; 711 PL_top_env = &next->start_env;
753 712
754 setup_coro (next); 713 setup_coro (next);
714 next->cursp = stacklevel;
755 715
756 prev->stack->refcnt++; 716 prev->stack->refcnt++;
757 prev->stack->usecnt++; 717 prev->stack->usecnt++;
758 next->stack = prev->stack; 718 next->stack = prev->stack;
759 next->gencnt = prev->gencnt; 719 next->gencnt = prev->gencnt;
764 allocate_stack (next, 1); 724 allocate_stack (next, 1);
765 coro_create (&(next->stack->cctx), 725 coro_create (&(next->stack->cctx),
766 setup_coro, (void *)next, 726 setup_coro, (void *)next,
767 next->stack->sptr, labs (next->stack->ssize)); 727 next->stack->sptr, labs (next->stack->ssize));
768 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 728 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
729 prev->cursp = stacklevel;
769 /* don't add any code here */ 730 /* don't add any code here */
770 } 731 }
771 } 732 }
772 else 733 else
734 {
773 setup_coro (next); 735 setup_coro (next);
736 next->cursp = stacklevel;
737 }
774 } 738 }
775
776 /*
777 * xnext is now either prev or next, depending on wether
778 * we switched the c stack or not. that's why I use a global
779 * variable, that should become thread-specific at one point.
780 */
781 xnext->cursp = stacklevel;
782 } 739 }
783 740
741 LOCK;
784 if (coro_mortal) 742 if (coro_mortal)
785 { 743 {
786 SvREFCNT_dec (coro_mortal); 744 SvREFCNT_dec (coro_mortal);
787 coro_mortal = 0; 745 coro_mortal = 0;
788 } 746 }
747 UNLOCK;
789} 748}
790 749
791#define SV_CORO(sv,func) \ 750#define SV_CORO(sv,func) \
792 do { \ 751 do { \
793 if (SvROK (sv)) \ 752 if (SvROK (sv)) \
794 sv = SvRV (sv); \ 753 sv = SvRV (sv); \
795 \ 754 \
796 if (SvTYPE(sv) == SVt_PVHV) \ 755 if (SvTYPE (sv) == SVt_PVHV) \
797 { \ 756 { \
798 HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ 757 HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
799 \ 758 \
800 if (!he) \ 759 if (!he) \
801 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ 760 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
802 \ 761 \
803 (sv) = SvRV (HeVAL(he)); \ 762 (sv) = SvRV (HeVAL(he)); \
804 } \ 763 } \
805 \ 764 \
806 /* must also be changed inside Coro::Cont::yield */ \ 765 /* must also be changed inside Coro::Cont::yield */ \
807 if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ 766 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
808 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ 767 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
809 \ 768 \
810 } while(0) 769 } while(0)
811 770
812#define SvSTATE(sv) (struct coro *)SvIV (sv) 771#define SvSTATE(sv) INT2PTR (struct coro *, SvIV (sv))
813 772
814static void 773static void
815api_transfer(pTHX_ SV *prev, SV *next, int flags) 774api_transfer(pTHX_ SV *prev, SV *next, int flags)
816{ 775{
817 SV_CORO (prev, "Coro::transfer"); 776 SV_CORO (prev, "Coro::transfer");
818 SV_CORO (next, "Coro::transfer"); 777 SV_CORO (next, "Coro::transfer");
819 778
820 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); 779 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
821} 780}
822 781
823/** Coro ********************************************************************/ 782/** Coro ********************************************************************/
824 783
825#define PRIO_MAX 3 784#define PRIO_MAX 3
833static GV *coro_current, *coro_idle; 792static GV *coro_current, *coro_idle;
834static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 793static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
835static int coro_nready; 794static int coro_nready;
836 795
837static void 796static void
838coro_enq (SV *sv) 797coro_enq (pTHX_ SV *sv)
839{ 798{
799 SV **xprio;
800 int prio;
801
840 if (SvTYPE (sv) == SVt_PVHV) 802 if (SvTYPE (sv) != SVt_PVHV)
841 {
842 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
843 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
844
845 prio = prio > PRIO_MAX ? PRIO_MAX
846 : prio < PRIO_MIN ? PRIO_MIN
847 : prio;
848
849 av_push (coro_ready [prio - PRIO_MIN], sv);
850 coro_nready++;
851
852 return;
853 }
854
855 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 803 croak ("Coro::ready tried to enqueue something that is not a coroutine");
804
805 xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
806 prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
807
808 prio = prio > PRIO_MAX ? PRIO_MAX
809 : prio < PRIO_MIN ? PRIO_MIN
810 : prio;
811
812 av_push (coro_ready [prio - PRIO_MIN], sv);
813 coro_nready++;
856} 814}
857 815
858static SV * 816static SV *
859coro_deq (int min_prio) 817coro_deq (pTHX_ int min_prio)
860{ 818{
861 int prio = PRIO_MAX - PRIO_MIN; 819 int prio = PRIO_MAX - PRIO_MIN;
862 820
863 min_prio -= PRIO_MIN; 821 min_prio -= PRIO_MIN;
864 if (min_prio < 0) 822 if (min_prio < 0)
875} 833}
876 834
877static void 835static void
878api_ready (SV *coro) 836api_ready (SV *coro)
879{ 837{
838 dTHX;
839
880 if (SvROK (coro)) 840 if (SvROK (coro))
881 coro = SvRV (coro); 841 coro = SvRV (coro);
882 842
843 LOCK;
883 coro_enq (SvREFCNT_inc (coro)); 844 coro_enq (aTHX_ SvREFCNT_inc (coro));
845 UNLOCK;
884} 846}
885 847
886static void 848static void
887api_schedule (void) 849api_schedule (void)
888{ 850{
851 dTHX;
852
889 SV *prev, *next; 853 SV *prev, *next;
890 854
855 LOCK;
856
891 prev = SvRV (GvSV (coro_current)); 857 prev = SvRV (GvSV (coro_current));
892 next = coro_deq (PRIO_MIN); 858 next = coro_deq (aTHX_ PRIO_MIN);
893 859
894 if (!next) 860 if (!next)
895 next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); 861 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
896 862
897 /* free this only after the transfer */ 863 /* free this only after the transfer */
900 866
901 SvRV (GvSV (coro_current)) = next; 867 SvRV (GvSV (coro_current)) = next;
902 868
903 SV_CORO (next, "Coro::schedule"); 869 SV_CORO (next, "Coro::schedule");
904 870
871 UNLOCK;
872
905 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), 873 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
906 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 874 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
907} 875}
908 876
909static void 877static void
910api_cede (void) 878api_cede (void)
911{ 879{
880 dTHX;
881
882 LOCK;
912 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); 883 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current))));
884 UNLOCK;
913 885
914 api_schedule (); 886 api_schedule ();
915} 887}
916 888
917MODULE = Coro::State PACKAGE = Coro::State 889MODULE = Coro::State PACKAGE = Coro::State
918 890
919PROTOTYPES: ENABLE 891PROTOTYPES: ENABLE
920 892
921BOOT: 893BOOT:
922{ /* {} necessary for stoopid perl-5.6.x */ 894{ /* {} necessary for stoopid perl-5.6.x */
895#ifdef USE_ITHREADS
896 MUTEX_INIT (&coro_mutex);
897#endif
898
923 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 899 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
924 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 900 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
925 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 901 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
926 902
927 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 903 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
928 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 904 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
929 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 905 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
930 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); 906 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
931
932 if (!padlist_cache)
933 padlist_cache = newHV ();
934 907
935 main_mainstack = PL_mainstack; 908 main_mainstack = PL_mainstack;
936 909
937 coroapi.ver = CORO_API_VERSION; 910 coroapi.ver = CORO_API_VERSION;
938 coroapi.transfer = api_transfer; 911 coroapi.transfer = api_transfer;
949 croak ("Coro::State::_newprocess expects an arrayref"); 922 croak ("Coro::State::_newprocess expects an arrayref");
950 923
951 Newz (0, coro, 1, struct coro); 924 Newz (0, coro, 1, struct coro);
952 925
953 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 926 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
954 coro->mainstack = 0; /* actual work is done inside transfer */ 927 /*coro->mainstack = 0; *//*actual work is done inside transfer */
955 coro->stack = 0; 928 /*coro->stack = 0;*/
956 929
957 /* same as JMPENV_BOOTSTRAP */ 930 /* same as JMPENV_BOOTSTRAP */
958 /* we might be able to recycle start_env, but safe is safe */ 931 /* we might be able to recycle start_env, but safe is safe */
959 //Zero(&coro->start_env, 1, JMPENV); 932 /*Zero(&coro->start_env, 1, JMPENV);*/
960 coro->start_env.je_ret = -1; 933 coro->start_env.je_ret = -1;
961 coro->start_env.je_mustcatch = TRUE; 934 coro->start_env.je_mustcatch = TRUE;
962 935
963 RETVAL = coro; 936 RETVAL = coro;
964 OUTPUT: 937 OUTPUT:
985 if (coro->mainstack && coro->mainstack != main_mainstack) 958 if (coro->mainstack && coro->mainstack != main_mainstack)
986 { 959 {
987 struct coro temp; 960 struct coro temp;
988 961
989 PUTBACK; 962 PUTBACK;
990 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 963 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
991 LOAD(aTHX_ coro); 964 LOAD (aTHX_ coro);
992 SPAGAIN; 965 SPAGAIN;
993 966
994 destroy_stacks (aTHX); 967 destroy_stacks (aTHX);
995 968
996 LOAD((&temp)); /* this will get rid of defsv etc.. */ 969 LOAD ((&temp)); /* this will get rid of defsv etc.. */
997 SPAGAIN; 970 SPAGAIN;
998 971
999 coro->mainstack = 0; 972 coro->mainstack = 0;
1000 } 973 }
1001 974
1006void 979void
1007_exit(code) 980_exit(code)
1008 int code 981 int code
1009 PROTOTYPE: $ 982 PROTOTYPE: $
1010 CODE: 983 CODE:
1011#if defined(__GLIBC__) || _POSIX_C_SOURCE
1012 _exit (code); 984 _exit (code);
1013#else
1014 signal (SIGTERM, SIG_DFL);
1015 raise (SIGTERM);
1016 exit (code);
1017#endif
1018 985
1019MODULE = Coro::State PACKAGE = Coro::Cont 986MODULE = Coro::State PACKAGE = Coro::Cont
1020 987
1021# this is slightly dirty (should expose a c-level api) 988# this is slightly dirty (should expose a c-level api)
1022 989
1023void 990void
1024yield(...) 991yield(...)
1025 PROTOTYPE: @ 992 PROTOTYPE: @
1026 CODE: 993 CODE:
1027 static SV *returnstk; 994 SV *yieldstack;
1028 SV *sv; 995 SV *sv;
1029 AV *defav = GvAV (PL_defgv); 996 AV *defav = GvAV (PL_defgv);
1030 struct coro *prev, *next; 997 struct coro *prev, *next;
1031 998
1032 if (!returnstk) 999 yieldstack = *hv_fetch (
1033 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); 1000 (HV *)SvRV (GvSV (coro_current)),
1001 "yieldstack", sizeof ("yieldstack") - 1,
1002 0
1003 );
1034 1004
1035 /* set up @_ -- ugly */ 1005 /* set up @_ -- ugly */
1036 av_clear (defav); 1006 av_clear (defav);
1037 av_fill (defav, items - 1); 1007 av_fill (defav, items - 1);
1038 while (items--) 1008 while (items--)
1039 av_store (defav, items, SvREFCNT_inc (ST(items))); 1009 av_store (defav, items, SvREFCNT_inc (ST(items)));
1040 1010
1041 mg_get (returnstk); /* isn't documentation wrong for mg_get? */
1042 sv = av_pop ((AV *)SvRV (returnstk)); 1011 sv = av_pop ((AV *)SvRV (yieldstack));
1043 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 1012 prev = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))));
1044 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1013 next = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))));
1045 SvREFCNT_dec (sv); 1014 SvREFCNT_dec (sv);
1046 1015
1047 transfer(aTHX_ prev, next, 0); 1016 transfer (aTHX_ prev, next, 0);
1048 1017
1049MODULE = Coro::State PACKAGE = Coro 1018MODULE = Coro::State PACKAGE = Coro
1050 1019
1051# this is slightly dirty (should expose a c-level api) 1020# this is slightly dirty (should expose a c-level api)
1052 1021
1112cede(...) 1081cede(...)
1113 PROTOTYPE: 1082 PROTOTYPE:
1114 CODE: 1083 CODE:
1115 api_cede (); 1084 api_cede ();
1116 1085
1086# and these are hacks
1087SV *
1088_aio_get_state ()
1089 CODE:
1090{
1091 struct {
1092 int errorno;
1093 int laststype;
1094 int laststatval;
1095 Stat_t statcache;
1096 } data;
1097
1098 data.errorno = errno;
1099 data.laststype = PL_laststype;
1100 data.laststatval = PL_laststatval;
1101 data.statcache = PL_statcache;
1102
1103 RETVAL = newSVpvn ((char *)&data, sizeof data);
1104}
1105 OUTPUT:
1106 RETVAL
1107
1108void
1109_aio_set_state (char *data_)
1110 PROTOTYPE: $
1111 CODE:
1112{
1113 struct {
1114 int errorno;
1115 int laststype;
1116 int laststatval;
1117 Stat_t statcache;
1118 } *data = (void *)data_;
1119
1120 errno = data->errorno;
1121 PL_laststype = data->laststype;
1122 PL_laststatval = data->laststatval;
1123 PL_statcache = data->statcache;
1124}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines