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.49 by pcg, Sun Nov 23 03:34:13 2003 UTC vs.
Revision 1.69 by root, Fri Dec 16 01:33:20 2005 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 <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>
39# undef HAVE_MMAP 41# undef HAVE_MMAP
40# endif 42# endif
41# endif 43# endif
42#endif 44#endif
43 45
44#define MAY_FLUSH /* increases codesize and is rarely used */
45
46#define SUB_INIT "Coro::State::initialize" 46#define SUB_INIT "Coro::State::initialize"
47#define UCORO_STATE "_coro_state" 47#define UCORO_STATE "_coro_state"
48 48
49/* The next macro should declare a variable stacklevel that contains and approximation 49/* The next macro should declare a variable stacklevel that contains and approximation
50 * to the current C stack pointer. Its property is that it changes with each call 50 * to the current C stack pointer. Its property is that it changes with each call
55 55
56#define labs(l) ((l) >= 0 ? (l) : -(l)) 56#define labs(l) ((l) >= 0 ? (l) : -(l))
57 57
58#include "CoroAPI.h" 58#include "CoroAPI.h"
59 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
60static 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 */
61 75
62/* 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... */
63typedef struct { 77typedef struct {
64 int refcnt; /* pointer reference counter */ 78 int refcnt; /* pointer reference counter */
65 int usecnt; /* shared by how many coroutines */ 79 int usecnt; /* shared by how many coroutines */
95 AV *mainstack; 109 AV *mainstack;
96 SV **stack_sp; 110 SV **stack_sp;
97 OP *op; 111 OP *op;
98 SV **curpad; 112 SV **curpad;
99 AV *comppad; 113 AV *comppad;
114 CV *compcv;
100 SV **stack_base; 115 SV **stack_base;
101 SV **stack_max; 116 SV **stack_max;
102 SV **tmps_stack; 117 SV **tmps_stack;
103 I32 tmps_floor; 118 I32 tmps_floor;
104 I32 tmps_ix; 119 I32 tmps_ix;
113 I32 savestack_ix; 128 I32 savestack_ix;
114 I32 savestack_max; 129 I32 savestack_max;
115 OP **retstack; 130 OP **retstack;
116 I32 retstack_ix; 131 I32 retstack_ix;
117 I32 retstack_max; 132 I32 retstack_max;
133 PMOP *curpm;
118 COP *curcop; 134 COP *curcop;
119 JMPENV *top_env; 135 JMPENV *top_env;
120 136
121 /* data associated with this coroutine (initial args) */ 137 /* data associated with this coroutine (initial args) */
122 AV *args; 138 AV *args;
123}; 139};
124 140
125typedef struct coro *Coro__State; 141typedef struct coro *Coro__State;
126typedef struct coro *Coro__State_or_hashref; 142typedef struct coro *Coro__State_or_hashref;
127 143
128static AV *main_mainstack; /* used to differentiate between $main and others */
129static HV *coro_state_stash;
130static SV *ucoro_state_sv;
131static U32 ucoro_state_hash;
132static HV *padlist_cache;
133static SV *coro_mortal; /* will be freed after next transfer */
134
135/* mostly copied from op.c:cv_clone2 */ 144/* mostly copied from op.c:cv_clone2 */
136STATIC AV * 145STATIC AV *
137clone_padlist (AV *protopadlist) 146clone_padlist (pTHX_ AV *protopadlist)
138{ 147{
139 AV *av; 148 AV *av;
140 I32 ix; 149 I32 ix;
141 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); 150 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE);
142 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); 151 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE);
166 AvFLAGS (av) = AVf_REIFY; 175 AvFLAGS (av) = AVf_REIFY;
167 176
168 for (ix = fpad; ix > 0; ix--) 177 for (ix = fpad; ix > 0; ix--)
169 { 178 {
170 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; 179 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv;
180
171 if (namesv && namesv != &PL_sv_undef) 181 if (namesv && namesv != &PL_sv_undef)
172 { 182 {
173 char *name = SvPVX (namesv); /* XXX */ 183 char *name = SvPVX (namesv); /* XXX */
184
174 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') 185 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&')
175 { /* lexical from outside? */ 186 { /* lexical from outside? */
176 npad[ix] = SvREFCNT_inc (ppad[ix]); 187 npad[ix] = SvREFCNT_inc (ppad[ix]);
177 } 188 }
178 else 189 else
184 sv = (SV *) newAV (); 195 sv = (SV *) newAV ();
185 else if (*name == '%') 196 else if (*name == '%')
186 sv = (SV *) newHV (); 197 sv = (SV *) newHV ();
187 else 198 else
188 sv = NEWSV (0, 0); 199 sv = NEWSV (0, 0);
200
189#ifdef SvPADBUSY 201#ifdef SvPADBUSY
190 if (!SvPADBUSY (sv)) 202 if (!SvPADBUSY (sv))
191#endif 203#endif
192 SvPADMY_on (sv); 204 SvPADMY_on (sv);
205
193 npad[ix] = sv; 206 npad[ix] = sv;
194 } 207 }
195 } 208 }
196 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) 209 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix]))
197 { 210 {
226#endif 239#endif
227 240
228 return newpadlist; 241 return newpadlist;
229} 242}
230 243
231#ifdef MAY_FLUSH
232STATIC void 244STATIC void
233free_padlist (AV *padlist) 245free_padlist (pTHX_ AV *padlist)
234{ 246{
235 /* may be during global destruction */ 247 /* may be during global destruction */
236 if (SvREFCNT(padlist)) 248 if (SvREFCNT (padlist))
237 { 249 {
238 I32 i = AvFILLp(padlist); 250 I32 i = AvFILLp (padlist);
239 while (i >= 0) 251 while (i >= 0)
240 { 252 {
241 SV **svp = av_fetch(padlist, i--, FALSE); 253 SV **svp = av_fetch (padlist, i--, FALSE);
242 SV *sv = svp ? *svp : Nullsv;
243 if (sv) 254 if (svp)
255 {
256 SV *sv;
257 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp)))
244 SvREFCNT_dec(sv); 258 SvREFCNT_dec (sv);
259
260 SvREFCNT_dec (*svp);
261 }
245 } 262 }
246 263
247 SvREFCNT_dec((SV*)padlist); 264 SvREFCNT_dec ((SV*)padlist);
248 } 265 }
249} 266}
250#endif 267
268STATIC int
269coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
270{
271 AV *padlist;
272 AV *av = (AV *)mg->mg_obj;
273
274 /* casting is fun. */
275 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
276 free_padlist (aTHX_ padlist);
277
278 SvREFCNT_dec (av);
279}
280
281#define PERL_MAGIC_coro PERL_MAGIC_ext
282
283static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
251 284
252/* the next two functions merely cache the padlists */ 285/* the next two functions merely cache the padlists */
253STATIC void 286STATIC void
254get_padlist (CV *cv) 287get_padlist (pTHX_ CV *cv)
255{ 288{
256 SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 0); 289 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
257 290
258 if (he && AvFILLp ((AV *)*he) >= 0) 291 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
259 CvPADLIST (cv) = (AV *)av_pop ((AV *)*he); 292 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
260 else 293 else
261 CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); 294 CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv));
262} 295}
263 296
264STATIC void 297STATIC void
265put_padlist (CV *cv) 298put_padlist (pTHX_ CV *cv)
266{ 299{
267 SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 1); 300 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
268 301
269 if (SvTYPE (*he) != SVt_PVAV) 302 if (!mg)
270 { 303 {
271 SvREFCNT_dec (*he); 304 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
305 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
306 mg->mg_virtual = &vtbl_coro;
272 *he = (SV *)newAV (); 307 mg->mg_obj = (SV *)newAV ();
273 } 308 }
274 309
275 av_push ((AV *)*he, (SV *)CvPADLIST (cv)); 310 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv));
276} 311}
277
278#ifdef MAY_FLUSH
279STATIC void
280flush_padlist_cache ()
281{
282 HV *hv = padlist_cache;
283 padlist_cache = newHV ();
284
285 if (hv_iterinit (hv))
286 {
287 HE *he;
288 AV *padlist;
289
290 while (!!(he = hv_iternext (hv)))
291 {
292 AV *av = (AV *)HeVAL(he);
293
294 /* casting is fun. */
295 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
296 free_padlist (padlist);
297 }
298 }
299
300 SvREFCNT_dec (hv);
301}
302#endif
303 312
304#define SB do { 313#define SB do {
305#define SE } while (0) 314#define SE } while (0)
306 315
307#define LOAD(state) load_state(aTHX_ (state)); 316#define LOAD(state) load_state(aTHX_ (state));
320 PL_mainstack = c->mainstack; 329 PL_mainstack = c->mainstack;
321 PL_stack_sp = c->stack_sp; 330 PL_stack_sp = c->stack_sp;
322 PL_op = c->op; 331 PL_op = c->op;
323 PL_curpad = c->curpad; 332 PL_curpad = c->curpad;
324 PL_comppad = c->comppad; 333 PL_comppad = c->comppad;
334 PL_compcv = c->compcv;
325 PL_stack_base = c->stack_base; 335 PL_stack_base = c->stack_base;
326 PL_stack_max = c->stack_max; 336 PL_stack_max = c->stack_max;
327 PL_tmps_stack = c->tmps_stack; 337 PL_tmps_stack = c->tmps_stack;
328 PL_tmps_floor = c->tmps_floor; 338 PL_tmps_floor = c->tmps_floor;
329 PL_tmps_ix = c->tmps_ix; 339 PL_tmps_ix = c->tmps_ix;
338 PL_savestack_ix = c->savestack_ix; 348 PL_savestack_ix = c->savestack_ix;
339 PL_savestack_max = c->savestack_max; 349 PL_savestack_max = c->savestack_max;
340 PL_retstack = c->retstack; 350 PL_retstack = c->retstack;
341 PL_retstack_ix = c->retstack_ix; 351 PL_retstack_ix = c->retstack_ix;
342 PL_retstack_max = c->retstack_max; 352 PL_retstack_max = c->retstack_max;
353 PL_curpm = c->curpm;
343 PL_curcop = c->curcop; 354 PL_curcop = c->curcop;
344 PL_top_env = c->top_env; 355 PL_top_env = c->top_env;
345 356
346 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 357 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
347 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 358 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
356 { 367 {
357 AV *padlist = (AV *)POPs; 368 AV *padlist = (AV *)POPs;
358 369
359 if (padlist) 370 if (padlist)
360 { 371 {
361 put_padlist (cv); /* mark this padlist as available */ 372 put_padlist (aTHX_ cv); /* mark this padlist as available */
362 CvPADLIST(cv) = padlist; 373 CvPADLIST(cv) = padlist;
363#ifdef USE_THREADS
364 /*CvOWNER(cv) = (struct perl_thread *)POPs;*/
365#endif
366 } 374 }
367 375
368 ++CvDEPTH(cv); 376 ++CvDEPTH(cv);
369 } 377 }
370 378
397 if (CxTYPE(cx) == CXt_SUB) 405 if (CxTYPE(cx) == CXt_SUB)
398 { 406 {
399 CV *cv = cx->blk_sub.cv; 407 CV *cv = cx->blk_sub.cv;
400 if (CvDEPTH(cv)) 408 if (CvDEPTH(cv))
401 { 409 {
402#ifdef USE_THREADS
403 /*XPUSHs ((SV *)CvOWNER(cv));*/
404 /*CvOWNER(cv) = 0;*/
405 /*error must unlock this cv etc.. etc...*/
406#endif
407 EXTEND (SP, CvDEPTH(cv)*2); 410 EXTEND (SP, CvDEPTH(cv)*2);
408 411
409 while (--CvDEPTH(cv)) 412 while (--CvDEPTH(cv))
410 { 413 {
411 /* this tells the restore code to increment CvDEPTH */ 414 /* this tells the restore code to increment CvDEPTH */
414 } 417 }
415 418
416 PUSHs ((SV *)CvPADLIST(cv)); 419 PUSHs ((SV *)CvPADLIST(cv));
417 PUSHs ((SV *)cv); 420 PUSHs ((SV *)cv);
418 421
419 get_padlist (cv); /* this is a monster */ 422 get_padlist (aTHX_ cv); /* this is a monster */
420 } 423 }
421 } 424 }
422#ifdef CXt_FORMAT 425#ifdef CXt_FORMAT
423 else if (CxTYPE(cx) == CXt_FORMAT) 426 else if (CxTYPE(cx) == CXt_FORMAT)
424 { 427 {
452 c->mainstack = PL_mainstack; 455 c->mainstack = PL_mainstack;
453 c->stack_sp = PL_stack_sp; 456 c->stack_sp = PL_stack_sp;
454 c->op = PL_op; 457 c->op = PL_op;
455 c->curpad = PL_curpad; 458 c->curpad = PL_curpad;
456 c->comppad = PL_comppad; 459 c->comppad = PL_comppad;
460 c->compcv = PL_compcv;
457 c->stack_base = PL_stack_base; 461 c->stack_base = PL_stack_base;
458 c->stack_max = PL_stack_max; 462 c->stack_max = PL_stack_max;
459 c->tmps_stack = PL_tmps_stack; 463 c->tmps_stack = PL_tmps_stack;
460 c->tmps_floor = PL_tmps_floor; 464 c->tmps_floor = PL_tmps_floor;
461 c->tmps_ix = PL_tmps_ix; 465 c->tmps_ix = PL_tmps_ix;
470 c->savestack_ix = PL_savestack_ix; 474 c->savestack_ix = PL_savestack_ix;
471 c->savestack_max = PL_savestack_max; 475 c->savestack_max = PL_savestack_max;
472 c->retstack = PL_retstack; 476 c->retstack = PL_retstack;
473 c->retstack_ix = PL_retstack_ix; 477 c->retstack_ix = PL_retstack_ix;
474 c->retstack_max = PL_retstack_max; 478 c->retstack_max = PL_retstack_max;
479 c->curpm = PL_curpm;
475 c->curcop = PL_curcop; 480 c->curcop = PL_curcop;
476 c->top_env = PL_top_env; 481 c->top_env = PL_top_env;
477} 482}
478 483
479/* 484/*
480 * allocate various perl stacks. This is an exact copy 485 * allocate various perl stacks. This is an exact copy
481 * of perl.c:init_stacks, except that it uses less memory 486 * of perl.c:init_stacks, except that it uses less memory
482 * on the assumption that coroutines do not usually need 487 * on the (sometimes correct) assumption that coroutines do
483 * a lot of stackspace. 488 * not usually need a lot of stackspace.
484 */ 489 */
485STATIC void 490STATIC void
486coro_init_stacks (pTHX) 491coro_init_stacks (pTHX)
487{ 492{
493 LOCK;
494
488 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 495 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
489 PL_curstackinfo->si_type = PERLSI_MAIN; 496 PL_curstackinfo->si_type = PERLSI_MAIN;
490 PL_curstack = PL_curstackinfo->si_stack; 497 PL_curstack = PL_curstackinfo->si_stack;
491 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 498 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
492 499
516 PL_savestack_max = 96; 523 PL_savestack_max = 96;
517 524
518 New(54,PL_retstack,8,OP*); 525 New(54,PL_retstack,8,OP*);
519 PL_retstack_ix = 0; 526 PL_retstack_ix = 0;
520 PL_retstack_max = 8; 527 PL_retstack_max = 8;
528
529 UNLOCK;
521} 530}
522 531
523/* 532/*
524 * destroy the stacks, the callchain etc... 533 * destroy the stacks, the callchain etc...
525 */ 534 */
527destroy_stacks(pTHX) 536destroy_stacks(pTHX)
528{ 537{
529 if (!IN_DESTRUCT) 538 if (!IN_DESTRUCT)
530 { 539 {
531 /* is this ugly, I ask? */ 540 /* is this ugly, I ask? */
532 while (PL_scopestack_ix) 541 LEAVE_SCOPE (0);
533 LEAVE;
534 542
535 /* sure it is, but more important: is it correct?? :/ */ 543 /* sure it is, but more important: is it correct?? :/ */
536 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
537 FREETMPS; 544 FREETMPS;
545
546 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/
538 } 547 }
539 548
540 while (PL_curstackinfo->si_next) 549 while (PL_curstackinfo->si_next)
541 PL_curstackinfo = PL_curstackinfo->si_next; 550 PL_curstackinfo = PL_curstackinfo->si_next;
542 551
543 while (PL_curstackinfo) 552 while (PL_curstackinfo)
544 { 553 {
545 PERL_SI *p = PL_curstackinfo->si_prev; 554 PERL_SI *p = PL_curstackinfo->si_prev;
546 555
547 { 556 { /*D*//*remove*/
548 dSP; 557 dSP;
549 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 558 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
550 PUTBACK; /* possibly superfluous */ 559 PUTBACK; /* possibly superfluous */
551 } 560 }
552 561
553 if (!IN_DESTRUCT) 562 if (!IN_DESTRUCT)
554 { 563 {
555 dounwind(-1); 564 dounwind (-1);/*D*//*remove*/
556 SvREFCNT_dec(PL_curstackinfo->si_stack); 565 SvREFCNT_dec (PL_curstackinfo->si_stack);
557 } 566 }
558 567
559 Safefree(PL_curstackinfo->si_cxstack); 568 Safefree (PL_curstackinfo->si_cxstack);
560 Safefree(PL_curstackinfo); 569 Safefree (PL_curstackinfo);
561 PL_curstackinfo = p; 570 PL_curstackinfo = p;
562 } 571 }
563 572
564 Safefree(PL_tmps_stack); 573 Safefree (PL_tmps_stack);
565 Safefree(PL_markstack); 574 Safefree (PL_markstack);
566 Safefree(PL_scopestack); 575 Safefree (PL_scopestack);
567 Safefree(PL_savestack); 576 Safefree (PL_savestack);
568 Safefree(PL_retstack); 577 Safefree (PL_retstack);
569} 578}
570 579
571static void 580static void
572allocate_stack (Coro__State ctx, int alloc) 581allocate_stack (Coro__State ctx, int alloc)
573{ 582{
576 New (0, stack, 1, coro_stack); 585 New (0, stack, 1, coro_stack);
577 586
578 stack->refcnt = 1; 587 stack->refcnt = 1;
579 stack->usecnt = 1; 588 stack->usecnt = 1;
580 stack->gencnt = ctx->gencnt = 0; 589 stack->gencnt = ctx->gencnt = 0;
590
581 if (alloc) 591 if (alloc)
582 { 592 {
583#if HAVE_MMAP 593#if HAVE_MMAP
584 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ 594 stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */
585 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);
586 if (stack->sptr == (void *)-1) 596 if (stack->sptr == (void *)-1)
587#endif 597#endif
588 { 598 {
589 /*FIXME*//*D*//* reasonable stack size! */ 599 /*FIXME*//*D*//* reasonable stack size! */
590 stack->ssize = -4096 * sizeof (long); 600 stack->ssize = - (STACKSIZE * sizeof (long));
591 New (0, stack->sptr, 4096, long); 601 New (0, stack->sptr, STACKSIZE, long);
592 } 602 }
593 } 603 }
594 else 604 else
595 stack->sptr = 0; 605 stack->sptr = 0;
596 606
626setup_coro (void *arg) 636setup_coro (void *arg)
627{ 637{
628 /* 638 /*
629 * emulate part of the perl startup here. 639 * emulate part of the perl startup here.
630 */ 640 */
641 dTHX;
631 dSP; 642 dSP;
632 Coro__State ctx = (Coro__State)arg; 643 Coro__State ctx = (Coro__State)arg;
633 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 644 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE);
634 645
635 coro_init_stacks (aTHX); 646 coro_init_stacks (aTHX);
636 /*PL_curcop = 0;*/ 647 /*PL_curcop = 0;*/
637 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 648 /*PL_in_eval = PL_in_eval;*/ /* inherit */
638 SvREFCNT_dec (GvAV (PL_defgv)); 649 SvREFCNT_dec (GvAV (PL_defgv));
684continue_coro (void *arg) 695continue_coro (void *arg)
685{ 696{
686 /* 697 /*
687 * this is a _very_ stripped down perl interpreter ;) 698 * this is a _very_ stripped down perl interpreter ;)
688 */ 699 */
700 dTHX;
689 Coro__State ctx = (Coro__State)arg; 701 Coro__State ctx = (Coro__State)arg;
690 JMPENV coro_start_env; 702 JMPENV coro_start_env;
691 703
692 PL_top_env = &ctx->start_env; 704 PL_top_env = &ctx->start_env;
693 705
697 709
698 abort (); 710 abort ();
699} 711}
700 712
701STATIC void 713STATIC void
702transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 714transfer (pTHX_ struct coro *prev, struct coro *next, int flags)
703{ 715{
704 dSTACKLEVEL; 716 dSTACKLEVEL;
705 static struct coro *xnext;
706 717
707 if (prev != next) 718 if (prev != next)
708 { 719 {
709 xnext = next;
710
711 if (next->mainstack) 720 if (next->mainstack)
712 { 721 {
722 LOCK;
713 SAVE (prev, flags); 723 SAVE (prev, flags);
714 LOAD (next); 724 LOAD (next);
725 UNLOCK;
715 726
716 /* mark this state as in-use */ 727 /* mark this state as in-use */
717 next->mainstack = 0; 728 next->mainstack = 0;
718 next->tmps_ix = -2; 729 next->tmps_ix = -2;
719 730
738 continue_coro, (void *)next, 749 continue_coro, (void *)next,
739 next->stack->sptr, labs (next->stack->ssize)); 750 next->stack->sptr, labs (next->stack->ssize));
740 } 751 }
741 752
742 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 753 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
754 prev->cursp = stacklevel;
743 /* don't add any code here */ 755 /* don't add any code here */
744 } 756 }
745 757 else
758 next->cursp = stacklevel;
746 } 759 }
747 else if (next->tmps_ix == -2) 760 else if (next->tmps_ix == -2)
748 croak ("tried to transfer to running coroutine"); 761 croak ("tried to transfer to running coroutine");
749 else 762 else
750 { 763 {
764 LOCK;
751 SAVE (prev, -1); /* first get rid of the old state */ 765 SAVE (prev, -1); /* first get rid of the old state */
766 UNLOCK;
752 767
753 if (flags & TRANSFER_SAVE_CCTXT) 768 if (flags & TRANSFER_SAVE_CCTXT)
754 { 769 {
755 if (!prev->stack) 770 if (!prev->stack)
756 allocate_stack (prev, 0); 771 allocate_stack (prev, 0);
758 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 773 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
759 { 774 {
760 PL_top_env = &next->start_env; 775 PL_top_env = &next->start_env;
761 776
762 setup_coro (next); 777 setup_coro (next);
778 next->cursp = stacklevel;
763 779
764 prev->stack->refcnt++; 780 prev->stack->refcnt++;
765 prev->stack->usecnt++; 781 prev->stack->usecnt++;
766 next->stack = prev->stack; 782 next->stack = prev->stack;
767 next->gencnt = prev->gencnt; 783 next->gencnt = prev->gencnt;
772 allocate_stack (next, 1); 788 allocate_stack (next, 1);
773 coro_create (&(next->stack->cctx), 789 coro_create (&(next->stack->cctx),
774 setup_coro, (void *)next, 790 setup_coro, (void *)next,
775 next->stack->sptr, labs (next->stack->ssize)); 791 next->stack->sptr, labs (next->stack->ssize));
776 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 792 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
793 prev->cursp = stacklevel;
777 /* don't add any code here */ 794 /* don't add any code here */
778 } 795 }
779 } 796 }
780 else 797 else
798 {
781 setup_coro (next); 799 setup_coro (next);
800 next->cursp = stacklevel;
801 }
782 } 802 }
783
784 /*
785 * xnext is now either prev or next, depending on wether
786 * we switched the c stack or not. that's why I use a global
787 * variable, that should become thread-specific at one point.
788 */
789 xnext->cursp = stacklevel;
790 } 803 }
791 804
805 LOCK;
792 if (coro_mortal) 806 if (coro_mortal)
793 { 807 {
794 SvREFCNT_dec (coro_mortal); 808 SvREFCNT_dec (coro_mortal);
795 coro_mortal = 0; 809 coro_mortal = 0;
796 } 810 }
811 UNLOCK;
797} 812}
798 813
799#define SV_CORO(sv,func) \ 814#define SV_CORO(sv,func) \
800 do { \ 815 do { \
801 if (SvROK (sv)) \ 816 if (SvROK (sv)) \
802 sv = SvRV (sv); \ 817 sv = SvRV (sv); \
803 \ 818 \
804 if (SvTYPE(sv) == SVt_PVHV) \ 819 if (SvTYPE (sv) == SVt_PVHV) \
805 { \ 820 { \
806 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); \
807 \ 822 \
808 if (!he) \ 823 if (!he) \
809 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); \
810 \ 825 \
811 (sv) = SvRV (HeVAL(he)); \ 826 (sv) = SvRV (HeVAL(he)); \
812 } \ 827 } \
813 \ 828 \
814 /* must also be changed inside Coro::Cont::yield */ \ 829 /* must also be changed inside Coro::Cont::yield */ \
815 if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ 830 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
816 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); \
817 \ 832 \
818 } while(0) 833 } while(0)
819 834
820#define SvSTATE(sv) (struct coro *)SvIV (sv) 835#define SvSTATE(sv) (struct coro *)SvIV (sv)
823api_transfer(pTHX_ SV *prev, SV *next, int flags) 838api_transfer(pTHX_ SV *prev, SV *next, int flags)
824{ 839{
825 SV_CORO (prev, "Coro::transfer"); 840 SV_CORO (prev, "Coro::transfer");
826 SV_CORO (next, "Coro::transfer"); 841 SV_CORO (next, "Coro::transfer");
827 842
828 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); 843 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
829} 844}
830 845
831/** Coro ********************************************************************/ 846/** Coro ********************************************************************/
832 847
833#define PRIO_MAX 3 848#define PRIO_MAX 3
841static GV *coro_current, *coro_idle; 856static GV *coro_current, *coro_idle;
842static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 857static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
843static int coro_nready; 858static int coro_nready;
844 859
845static void 860static void
846coro_enq (SV *sv) 861coro_enq (pTHX_ SV *sv)
847{ 862{
848 if (SvTYPE (sv) == SVt_PVHV) 863 if (SvTYPE (sv) == SVt_PVHV)
849 { 864 {
850 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); 865 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
851 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 866 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
862 877
863 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");
864} 879}
865 880
866static SV * 881static SV *
867coro_deq (int min_prio) 882coro_deq (pTHX_ int min_prio)
868{ 883{
869 int prio = PRIO_MAX - PRIO_MIN; 884 int prio = PRIO_MAX - PRIO_MIN;
870 885
871 min_prio -= PRIO_MIN; 886 min_prio -= PRIO_MIN;
872 if (min_prio < 0) 887 if (min_prio < 0)
883} 898}
884 899
885static void 900static void
886api_ready (SV *coro) 901api_ready (SV *coro)
887{ 902{
903 dTHX;
904
888 if (SvROK (coro)) 905 if (SvROK (coro))
889 coro = SvRV (coro); 906 coro = SvRV (coro);
890 907
908 LOCK;
891 coro_enq (SvREFCNT_inc (coro)); 909 coro_enq (aTHX_ SvREFCNT_inc (coro));
910 UNLOCK;
892} 911}
893 912
894static void 913static void
895api_schedule (void) 914api_schedule (void)
896{ 915{
916 dTHX;
917
897 SV *prev, *next; 918 SV *prev, *next;
898 919
920 LOCK;
921
899 prev = SvRV (GvSV (coro_current)); 922 prev = SvRV (GvSV (coro_current));
900 next = coro_deq (PRIO_MIN); 923 next = coro_deq (aTHX_ PRIO_MIN);
901 924
902 if (!next) 925 if (!next)
903 next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); 926 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
904 927
905 /* free this only after the transfer */ 928 /* free this only after the transfer */
908 931
909 SvRV (GvSV (coro_current)) = next; 932 SvRV (GvSV (coro_current)) = next;
910 933
911 SV_CORO (next, "Coro::schedule"); 934 SV_CORO (next, "Coro::schedule");
912 935
936 UNLOCK;
937
913 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), 938 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
914 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 939 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
915} 940}
916 941
917static void 942static void
918api_cede (void) 943api_cede (void)
919{ 944{
945 dTHX;
946
947 LOCK;
920 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); 948 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current))));
949 UNLOCK;
921 950
922 api_schedule (); 951 api_schedule ();
923} 952}
924 953
925MODULE = Coro::State PACKAGE = Coro::State 954MODULE = Coro::State PACKAGE = Coro::State
926 955
927PROTOTYPES: ENABLE 956PROTOTYPES: ENABLE
928 957
929BOOT: 958BOOT:
930{ /* {} 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
931 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 964 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
932 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 965 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
933 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 966 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
934 967
935 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 968 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
936 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 969 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
937 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 970 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
938 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); 971 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
939
940 if (!padlist_cache)
941 padlist_cache = newHV ();
942 972
943 main_mainstack = PL_mainstack; 973 main_mainstack = PL_mainstack;
944 974
945 coroapi.ver = CORO_API_VERSION; 975 coroapi.ver = CORO_API_VERSION;
946 coroapi.transfer = api_transfer; 976 coroapi.transfer = api_transfer;
957 croak ("Coro::State::_newprocess expects an arrayref"); 987 croak ("Coro::State::_newprocess expects an arrayref");
958 988
959 Newz (0, coro, 1, struct coro); 989 Newz (0, coro, 1, struct coro);
960 990
961 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 991 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
962 coro->mainstack = 0; /* actual work is done inside transfer */ 992 /*coro->mainstack = 0; *//*actual work is done inside transfer */
963 coro->stack = 0; 993 /*coro->stack = 0;*/
964 994
965 /* same as JMPENV_BOOTSTRAP */ 995 /* same as JMPENV_BOOTSTRAP */
966 /* 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 */
967 //Zero(&coro->start_env, 1, JMPENV); 997 /*Zero(&coro->start_env, 1, JMPENV);*/
968 coro->start_env.je_ret = -1; 998 coro->start_env.je_ret = -1;
969 coro->start_env.je_mustcatch = TRUE; 999 coro->start_env.je_mustcatch = TRUE;
970 1000
971 RETVAL = coro; 1001 RETVAL = coro;
972 OUTPUT: 1002 OUTPUT:
993 if (coro->mainstack && coro->mainstack != main_mainstack) 1023 if (coro->mainstack && coro->mainstack != main_mainstack)
994 { 1024 {
995 struct coro temp; 1025 struct coro temp;
996 1026
997 PUTBACK; 1027 PUTBACK;
998 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 1028 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
999 LOAD(aTHX_ coro); 1029 LOAD (aTHX_ coro);
1000 SPAGAIN; 1030 SPAGAIN;
1001 1031
1002 destroy_stacks (aTHX); 1032 destroy_stacks (aTHX);
1003 1033
1004 LOAD((&temp)); /* this will get rid of defsv etc.. */ 1034 LOAD ((&temp)); /* this will get rid of defsv etc.. */
1005 SPAGAIN; 1035 SPAGAIN;
1006 1036
1007 coro->mainstack = 0; 1037 coro->mainstack = 0;
1008 } 1038 }
1009 1039
1010 deallocate_stack (coro); 1040 deallocate_stack (coro);
1011 SvREFCNT_dec (coro->args); 1041 SvREFCNT_dec (coro->args);
1012 Safefree (coro); 1042 Safefree (coro);
1013
1014void
1015flush()
1016 CODE:
1017#ifdef MAY_FLUSH
1018 flush_padlist_cache ();
1019#endif
1020 1043
1021void 1044void
1022_exit(code) 1045_exit(code)
1023 int code 1046 int code
1024 PROTOTYPE: $ 1047 PROTOTYPE: $
1025 CODE: 1048 CODE:
1026#if defined(__GLIBC__) || _POSIX_C_SOURCE
1027 _exit (code); 1049 _exit (code);
1028#else
1029 signal (SIGTERM, SIG_DFL);
1030 raise (SIGTERM);
1031 exit (code);
1032#endif
1033 1050
1034MODULE = Coro::State PACKAGE = Coro::Cont 1051MODULE = Coro::State PACKAGE = Coro::Cont
1035 1052
1036# this is slightly dirty (should expose a c-level api) 1053# this is slightly dirty (should expose a c-level api)
1037 1054
1038void 1055void
1039yield(...) 1056yield(...)
1040 PROTOTYPE: @ 1057 PROTOTYPE: @
1041 CODE: 1058 CODE:
1042 static SV *returnstk; 1059 SV *yieldstack;
1043 SV *sv; 1060 SV *sv;
1044 AV *defav = GvAV (PL_defgv); 1061 AV *defav = GvAV (PL_defgv);
1045 struct coro *prev, *next; 1062 struct coro *prev, *next;
1046 1063
1047 if (!returnstk) 1064 yieldstack = *hv_fetch (
1048 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE)); 1065 (HV *)SvRV (GvSV (coro_current)),
1066 "yieldstack", sizeof ("yieldstack") - 1,
1067 0
1068 );
1049 1069
1050 /* set up @_ -- ugly */ 1070 /* set up @_ -- ugly */
1051 av_clear (defav); 1071 av_clear (defav);
1052 av_fill (defav, items - 1); 1072 av_fill (defav, items - 1);
1053 while (items--) 1073 while (items--)
1054 av_store (defav, items, SvREFCNT_inc (ST(items))); 1074 av_store (defav, items, SvREFCNT_inc (ST(items)));
1055 1075
1056 mg_get (returnstk); /* isn't documentation wrong for mg_get? */
1057 sv = av_pop ((AV *)SvRV (returnstk)); 1076 sv = av_pop ((AV *)SvRV (yieldstack));
1058 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)));
1059 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)));
1060 SvREFCNT_dec (sv); 1079 SvREFCNT_dec (sv);
1061 1080
1062 transfer(aTHX_ prev, next, 0); 1081 transfer (aTHX_ prev, next, 0);
1063 1082
1064MODULE = Coro::State PACKAGE = Coro 1083MODULE = Coro::State PACKAGE = Coro
1065 1084
1066# this is slightly dirty (should expose a c-level api) 1085# this is slightly dirty (should expose a c-level api)
1067 1086
1096 sv_setiv(sv, (IV)&coroapi); 1115 sv_setiv(sv, (IV)&coroapi);
1097 SvREADONLY_on(sv); 1116 SvREADONLY_on(sv);
1098 } 1117 }
1099} 1118}
1100 1119
1120#if !PERL_MICRO
1121
1101void 1122void
1102ready(self) 1123ready(self)
1103 SV * self 1124 SV * self
1104 PROTOTYPE: $ 1125 PROTOTYPE: $
1105 CODE: 1126 CODE:
1106 api_ready (self); 1127 api_ready (self);
1128
1129#endif
1107 1130
1108int 1131int
1109nready(...) 1132nready(...)
1110 PROTOTYPE: 1133 PROTOTYPE:
1111 CODE: 1134 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines