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.39 by root, Sun Nov 25 20:04:05 2001 UTC vs.
Revision 1.61 by root, Tue Aug 10 01:56:30 2004 UTC

1#define PERL_NO_GET_CONTEXT
2
1#include "EXTERN.h" 3#include "EXTERN.h"
2#include "perl.h" 4#include "perl.h"
3#include "XSUB.h" 5#include "XSUB.h"
6
7#include "patchlevel.h"
8
9#if PATCHLEVEL < 6
10# ifndef PL_ppaddr
11# define PL_ppaddr ppaddr
12# endif
13# ifndef call_sv
14# define call_sv perl_call_sv
15# endif
16# ifndef get_sv
17# define get_sv perl_get_sv
18# endif
19# ifndef get_cv
20# define get_cv perl_get_cv
21# endif
22# ifndef IS_PADGV
23# define IS_PADGV(v) 0
24# endif
25# ifndef IS_PADCONST
26# define IS_PADCONST(v) 0
27# endif
28#endif
4 29
5#include "libcoro/coro.c" 30#include "libcoro/coro.c"
6 31
7#include <signal.h> 32#include <signal.h>
8 33
16# undef HAVE_MMAP 41# undef HAVE_MMAP
17# endif 42# endif
18# endif 43# endif
19#endif 44#endif
20 45
21#define MAY_FLUSH /* increases codesize and is rarely used */
22
23#define SUB_INIT "Coro::State::initialize" 46#define SUB_INIT "Coro::State::initialize"
24#define UCORO_STATE "_coro_state" 47#define UCORO_STATE "_coro_state"
25 48
26/* 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
27 * 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
32 55
33#define labs(l) ((l) >= 0 ? (l) : -(l)) 56#define labs(l) ((l) >= 0 ? (l) : -(l))
34 57
35#include "CoroAPI.h" 58#include "CoroAPI.h"
36 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 0
66# define UNLOCK 0
67#endif
68
37static 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 */
38 75
39/* 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... */
40typedef struct { 77typedef struct {
41 int refcnt; /* pointer reference counter */ 78 int refcnt; /* pointer reference counter */
42 int usecnt; /* shared by how many coroutines */ 79 int usecnt; /* shared by how many coroutines */
47 void *sptr; 84 void *sptr;
48 long ssize; /* positive == mmap, otherwise malloc */ 85 long ssize; /* positive == mmap, otherwise malloc */
49} coro_stack; 86} coro_stack;
50 87
51struct coro { 88struct coro {
89 /* the top-level JMPENV for each coroutine, needed to catch dies. */
90 JMPENV start_env;
91
52 /* the optional C context */ 92 /* the optional C context */
53 coro_stack *stack; 93 coro_stack *stack;
54 void *cursp; 94 void *cursp;
55 int gencnt; 95 int gencnt;
56 96
57 /* optionally saved, might be zero */ 97 /* optionally saved, might be zero */
58 AV *defav; 98 AV *defav;
59 SV *defsv; 99 SV *defsv;
60 SV *errsv; 100 SV *errsv;
61 /* TODO: PL_pmop might be nice && effortless to save. */
62 101
63 /* saved global state not related to stacks */ 102 /* saved global state not related to stacks */
64 U8 dowarn; 103 U8 dowarn;
65 I32 in_eval; 104 I32 in_eval;
66 105
69 AV *curstack; 108 AV *curstack;
70 AV *mainstack; 109 AV *mainstack;
71 SV **stack_sp; 110 SV **stack_sp;
72 OP *op; 111 OP *op;
73 SV **curpad; 112 SV **curpad;
113 AV *comppad;
74 SV **stack_base; 114 SV **stack_base;
75 SV **stack_max; 115 SV **stack_max;
76 SV **tmps_stack; 116 SV **tmps_stack;
77 I32 tmps_floor; 117 I32 tmps_floor;
78 I32 tmps_ix; 118 I32 tmps_ix;
97}; 137};
98 138
99typedef struct coro *Coro__State; 139typedef struct coro *Coro__State;
100typedef struct coro *Coro__State_or_hashref; 140typedef struct coro *Coro__State_or_hashref;
101 141
102static AV *main_mainstack; /* used to differentiate between $main and others */
103static HV *coro_state_stash;
104static SV *ucoro_state_sv;
105static U32 ucoro_state_hash;
106static HV *padlist_cache;
107static SV *coro_mortal; /* will be freed after next transfer */
108
109/* mostly copied from op.c:cv_clone2 */ 142/* mostly copied from op.c:cv_clone2 */
110STATIC AV * 143STATIC AV *
111clone_padlist (AV *protopadlist) 144clone_padlist (pTHX_ AV *protopadlist)
112{ 145{
113 AV *av; 146 AV *av;
114 I32 ix; 147 I32 ix;
115 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE); 148 AV *protopad_name = (AV *) * av_fetch (protopadlist, 0, FALSE);
116 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE); 149 AV *protopad = (AV *) * av_fetch (protopadlist, 1, FALSE);
140 AvFLAGS (av) = AVf_REIFY; 173 AvFLAGS (av) = AVf_REIFY;
141 174
142 for (ix = fpad; ix > 0; ix--) 175 for (ix = fpad; ix > 0; ix--)
143 { 176 {
144 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv; 177 SV *namesv = (ix <= fname) ? pname[ix] : Nullsv;
178
145 if (namesv && namesv != &PL_sv_undef) 179 if (namesv && namesv != &PL_sv_undef)
146 { 180 {
147 char *name = SvPVX (namesv); /* XXX */ 181 char *name = SvPVX (namesv); /* XXX */
182
148 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&') 183 if (SvFLAGS (namesv) & SVf_FAKE || *name == '&')
149 { /* lexical from outside? */ 184 { /* lexical from outside? */
150 npad[ix] = SvREFCNT_inc (ppad[ix]); 185 npad[ix] = SvREFCNT_inc (ppad[ix]);
151 } 186 }
152 else 187 else
158 sv = (SV *) newAV (); 193 sv = (SV *) newAV ();
159 else if (*name == '%') 194 else if (*name == '%')
160 sv = (SV *) newHV (); 195 sv = (SV *) newHV ();
161 else 196 else
162 sv = NEWSV (0, 0); 197 sv = NEWSV (0, 0);
198
199#ifdef SvPADBUSY
163 if (!SvPADBUSY (sv)) 200 if (!SvPADBUSY (sv))
201#endif
164 SvPADMY_on (sv); 202 SvPADMY_on (sv);
203
165 npad[ix] = sv; 204 npad[ix] = sv;
166 } 205 }
167 } 206 }
168 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix])) 207 else if (IS_PADGV (ppad[ix]) || IS_PADCONST (ppad[ix]))
169 { 208 {
198#endif 237#endif
199 238
200 return newpadlist; 239 return newpadlist;
201} 240}
202 241
203#ifdef MAY_FLUSH
204STATIC void 242STATIC void
205free_padlist (AV *padlist) 243free_padlist (pTHX_ AV *padlist)
206{ 244{
207 /* may be during global destruction */ 245 /* may be during global destruction */
208 if (SvREFCNT(padlist)) 246 if (SvREFCNT (padlist))
209 { 247 {
210 I32 i = AvFILLp(padlist); 248 I32 i = AvFILLp (padlist);
211 while (i >= 0) 249 while (i >= 0)
212 { 250 {
213 SV **svp = av_fetch(padlist, i--, FALSE); 251 SV **svp = av_fetch (padlist, i--, FALSE);
214 SV *sv = svp ? *svp : Nullsv;
215 if (sv) 252 if (svp)
253 {
254 SV *sv;
255 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp)))
216 SvREFCNT_dec(sv); 256 SvREFCNT_dec (sv);
257
258 SvREFCNT_dec (*svp);
259 }
217 } 260 }
218 261
219 SvREFCNT_dec((SV*)padlist); 262 SvREFCNT_dec ((SV*)padlist);
220 } 263 }
221} 264}
222#endif 265
266STATIC int
267coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
268{
269 AV *padlist;
270 AV *av = (AV *)mg->mg_obj;
271
272 /* casting is fun. */
273 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
274 free_padlist (aTHX_ padlist);
275
276 SvREFCNT_dec (av);
277}
278
279#define PERL_MAGIC_coro PERL_MAGIC_ext
280
281static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
223 282
224/* the next two functions merely cache the padlists */ 283/* the next two functions merely cache the padlists */
225STATIC void 284STATIC void
226get_padlist (CV *cv) 285get_padlist (pTHX_ CV *cv)
227{ 286{
228 SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 0); 287 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
229 288
230 if (he && AvFILLp ((AV *)*he) >= 0) 289 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
231 CvPADLIST (cv) = (AV *)av_pop ((AV *)*he); 290 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
232 else 291 else
233 CvPADLIST (cv) = clone_padlist (CvPADLIST (cv)); 292 CvPADLIST (cv) = clone_padlist (aTHX_ CvPADLIST (cv));
234} 293}
235 294
236STATIC void 295STATIC void
237put_padlist (CV *cv) 296put_padlist (pTHX_ CV *cv)
238{ 297{
239 SV **he = hv_fetch (padlist_cache, (void *)&cv, sizeof (CV *), 1); 298 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
240 299
241 if (SvTYPE (*he) != SVt_PVAV) 300 if (!mg)
242 { 301 {
243 SvREFCNT_dec (*he); 302 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
303 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
304 mg->mg_virtual = &vtbl_coro;
244 *he = (SV *)newAV (); 305 mg->mg_obj = (SV *)newAV ();
245 } 306 }
246 307
247 av_push ((AV *)*he, (SV *)CvPADLIST (cv)); 308 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv));
248} 309}
249
250#ifdef MAY_FLUSH
251STATIC void
252flush_padlist_cache ()
253{
254 HV *hv = padlist_cache;
255 padlist_cache = newHV ();
256
257 if (hv_iterinit (hv))
258 {
259 HE *he;
260 AV *padlist;
261
262 while (!!(he = hv_iternext (hv)))
263 {
264 AV *av = (AV *)HeVAL(he);
265
266 /* casting is fun. */
267 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
268 free_padlist (padlist);
269 }
270 }
271
272 SvREFCNT_dec (hv);
273}
274#endif
275 310
276#define SB do { 311#define SB do {
277#define SE } while (0) 312#define SE } while (0)
278 313
279#define LOAD(state) load_state(aTHX_ (state)); 314#define LOAD(state) load_state(aTHX_ (state));
280#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); 315#define SAVE(state,flags) save_state(aTHX_ (state),(flags));
281 316
282#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE 317#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
283 318
284static void 319static void
285load_state(pTHX_ Coro__State c) 320load_state(pTHX_ Coro__State c)
286{ 321{
287 PL_dowarn = c->dowarn; 322 PL_dowarn = c->dowarn;
291 PL_curstack = c->curstack; 326 PL_curstack = c->curstack;
292 PL_mainstack = c->mainstack; 327 PL_mainstack = c->mainstack;
293 PL_stack_sp = c->stack_sp; 328 PL_stack_sp = c->stack_sp;
294 PL_op = c->op; 329 PL_op = c->op;
295 PL_curpad = c->curpad; 330 PL_curpad = c->curpad;
331 PL_comppad = c->comppad;
296 PL_stack_base = c->stack_base; 332 PL_stack_base = c->stack_base;
297 PL_stack_max = c->stack_max; 333 PL_stack_max = c->stack_max;
298 PL_tmps_stack = c->tmps_stack; 334 PL_tmps_stack = c->tmps_stack;
299 PL_tmps_floor = c->tmps_floor; 335 PL_tmps_floor = c->tmps_floor;
300 PL_tmps_ix = c->tmps_ix; 336 PL_tmps_ix = c->tmps_ix;
327 { 363 {
328 AV *padlist = (AV *)POPs; 364 AV *padlist = (AV *)POPs;
329 365
330 if (padlist) 366 if (padlist)
331 { 367 {
332 put_padlist (cv); /* mark this padlist as available */ 368 put_padlist (aTHX_ cv); /* mark this padlist as available */
333 CvPADLIST(cv) = padlist; 369 CvPADLIST(cv) = padlist;
334#ifdef USE_THREADS
335 /*CvOWNER(cv) = (struct perl_thread *)POPs;*/
336#endif
337 } 370 }
338 371
339 ++CvDEPTH(cv); 372 ++CvDEPTH(cv);
340 } 373 }
341 374
368 if (CxTYPE(cx) == CXt_SUB) 401 if (CxTYPE(cx) == CXt_SUB)
369 { 402 {
370 CV *cv = cx->blk_sub.cv; 403 CV *cv = cx->blk_sub.cv;
371 if (CvDEPTH(cv)) 404 if (CvDEPTH(cv))
372 { 405 {
373#ifdef USE_THREADS
374 /*XPUSHs ((SV *)CvOWNER(cv));*/
375 /*CvOWNER(cv) = 0;*/
376 /*error must unlock this cv etc.. etc...*/
377#endif
378 EXTEND (SP, CvDEPTH(cv)*2); 406 EXTEND (SP, CvDEPTH(cv)*2);
379 407
380 while (--CvDEPTH(cv)) 408 while (--CvDEPTH(cv))
381 { 409 {
382 /* this tells the restore code to increment CvDEPTH */ 410 /* this tells the restore code to increment CvDEPTH */
385 } 413 }
386 414
387 PUSHs ((SV *)CvPADLIST(cv)); 415 PUSHs ((SV *)CvPADLIST(cv));
388 PUSHs ((SV *)cv); 416 PUSHs ((SV *)cv);
389 417
390 get_padlist (cv); /* this is a monster */ 418 get_padlist (aTHX_ cv); /* this is a monster */
391 } 419 }
392 } 420 }
421#ifdef CXt_FORMAT
393 else if (CxTYPE(cx) == CXt_FORMAT) 422 else if (CxTYPE(cx) == CXt_FORMAT)
394 { 423 {
395 /* I never used formats, so how should I know how these are implemented? */ 424 /* I never used formats, so how should I know how these are implemented? */
396 /* my bold guess is as a simple, plain sub... */ 425 /* my bold guess is as a simple, plain sub... */
397 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 426 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
398 } 427 }
428#endif
399 } 429 }
400 430
401 if (top_si->si_type == PERLSI_MAIN) 431 if (top_si->si_type == PERLSI_MAIN)
402 break; 432 break;
403 433
420 c->curstack = PL_curstack; 450 c->curstack = PL_curstack;
421 c->mainstack = PL_mainstack; 451 c->mainstack = PL_mainstack;
422 c->stack_sp = PL_stack_sp; 452 c->stack_sp = PL_stack_sp;
423 c->op = PL_op; 453 c->op = PL_op;
424 c->curpad = PL_curpad; 454 c->curpad = PL_curpad;
455 c->comppad = PL_comppad;
425 c->stack_base = PL_stack_base; 456 c->stack_base = PL_stack_base;
426 c->stack_max = PL_stack_max; 457 c->stack_max = PL_stack_max;
427 c->tmps_stack = PL_tmps_stack; 458 c->tmps_stack = PL_tmps_stack;
428 c->tmps_floor = PL_tmps_floor; 459 c->tmps_floor = PL_tmps_floor;
429 c->tmps_ix = PL_tmps_ix; 460 c->tmps_ix = PL_tmps_ix;
445} 476}
446 477
447/* 478/*
448 * allocate various perl stacks. This is an exact copy 479 * allocate various perl stacks. This is an exact copy
449 * of perl.c:init_stacks, except that it uses less memory 480 * of perl.c:init_stacks, except that it uses less memory
450 * on the assumption that coroutines do not usually need 481 * on the (sometimes correct) assumption that coroutines do
451 * a lot of stackspace. 482 * not usually need a lot of stackspace.
452 */ 483 */
453STATIC void 484STATIC void
454coro_init_stacks (pTHX) 485coro_init_stacks (pTHX)
455{ 486{
487 LOCK;
488
456 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 489 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
457 PL_curstackinfo->si_type = PERLSI_MAIN; 490 PL_curstackinfo->si_type = PERLSI_MAIN;
458 PL_curstack = PL_curstackinfo->si_stack; 491 PL_curstack = PL_curstackinfo->si_stack;
459 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 492 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
460 493
469 502
470 New(54,PL_markstack,16,I32); 503 New(54,PL_markstack,16,I32);
471 PL_markstack_ptr = PL_markstack; 504 PL_markstack_ptr = PL_markstack;
472 PL_markstack_max = PL_markstack + 16; 505 PL_markstack_max = PL_markstack + 16;
473 506
507#ifdef SET_MARK_OFFSET
474 SET_MARK_OFFSET; 508 SET_MARK_OFFSET;
509#endif
475 510
476 New(54,PL_scopestack,16,I32); 511 New(54,PL_scopestack,16,I32);
477 PL_scopestack_ix = 0; 512 PL_scopestack_ix = 0;
478 PL_scopestack_max = 16; 513 PL_scopestack_max = 16;
479 514
482 PL_savestack_max = 96; 517 PL_savestack_max = 96;
483 518
484 New(54,PL_retstack,8,OP*); 519 New(54,PL_retstack,8,OP*);
485 PL_retstack_ix = 0; 520 PL_retstack_ix = 0;
486 PL_retstack_max = 8; 521 PL_retstack_max = 8;
522
523 UNLOCK;
487} 524}
488 525
489/* 526/*
490 * destroy the stacks, the callchain etc... 527 * destroy the stacks, the callchain etc...
491 * still there is a memleak of 128 bytes...
492 */ 528 */
493STATIC void 529STATIC void
494destroy_stacks(pTHX) 530destroy_stacks(pTHX)
495{ 531{
496 if (!IN_DESTRUCT) 532 if (!IN_DESTRUCT)
497 { 533 {
498 /* is this ugly, I ask? */ 534 /* is this ugly, I ask? */
499 while (PL_scopestack_ix) 535 LEAVE_SCOPE (0);
500 LEAVE;
501 536
502 /* sure it is, but more important: is it correct?? :/ */ 537 /* sure it is, but more important: is it correct?? :/ */
503 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
504 FREETMPS; 538 FREETMPS;
539
540 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/
505 } 541 }
506 542
507 while (PL_curstackinfo->si_next) 543 while (PL_curstackinfo->si_next)
508 PL_curstackinfo = PL_curstackinfo->si_next; 544 PL_curstackinfo = PL_curstackinfo->si_next;
509 545
510 while (PL_curstackinfo) 546 while (PL_curstackinfo)
511 { 547 {
512 PERL_SI *p = PL_curstackinfo->si_prev; 548 PERL_SI *p = PL_curstackinfo->si_prev;
513 549
514 { 550 { /*D*//*remove*/
515 dSP; 551 dSP;
516 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 552 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
517 PUTBACK; /* possibly superfluous */ 553 PUTBACK; /* possibly superfluous */
518 } 554 }
519 555
520 if (!IN_DESTRUCT) 556 if (!IN_DESTRUCT)
521 { 557 {
522 dounwind(-1); 558 dounwind (-1);/*D*//*remove*/
523 SvREFCNT_dec(PL_curstackinfo->si_stack); 559 SvREFCNT_dec (PL_curstackinfo->si_stack);
524 } 560 }
525 561
526 Safefree(PL_curstackinfo->si_cxstack); 562 Safefree (PL_curstackinfo->si_cxstack);
527 Safefree(PL_curstackinfo); 563 Safefree (PL_curstackinfo);
528 PL_curstackinfo = p; 564 PL_curstackinfo = p;
529 } 565 }
530 566
531 Safefree(PL_tmps_stack); 567 Safefree (PL_tmps_stack);
532 Safefree(PL_markstack); 568 Safefree (PL_markstack);
533 Safefree(PL_scopestack); 569 Safefree (PL_scopestack);
534 Safefree(PL_savestack); 570 Safefree (PL_savestack);
535 Safefree(PL_retstack); 571 Safefree (PL_retstack);
536} 572}
537 573
538static void 574static void
539allocate_stack (Coro__State ctx, int alloc) 575allocate_stack (Coro__State ctx, int alloc)
540{ 576{
543 New (0, stack, 1, coro_stack); 579 New (0, stack, 1, coro_stack);
544 580
545 stack->refcnt = 1; 581 stack->refcnt = 1;
546 stack->usecnt = 1; 582 stack->usecnt = 1;
547 stack->gencnt = ctx->gencnt = 0; 583 stack->gencnt = ctx->gencnt = 0;
584
548 if (alloc) 585 if (alloc)
549 { 586 {
550#if HAVE_MMAP 587#if HAVE_MMAP
551 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ 588 stack->ssize = 16384 * sizeof (long); /* mmap should do allocate-on-write for us */
552 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 589 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
553 if (stack->sptr == (void *)-1) 590 if (stack->sptr == (void *)-1)
554#endif 591#endif
555 { 592 {
556 /*FIXME*//*D*//* reasonable stack size! */ 593 /*FIXME*//*D*//* reasonable stack size! */
557 stack->ssize = -4096 * sizeof (long); 594 stack->ssize = - (8192 * sizeof (long));
558 New (0, stack->sptr, 4096, long); 595 New (0, stack->sptr, 8192, long);
559 } 596 }
560 } 597 }
561 else 598 else
562 stack->sptr = 0; 599 stack->sptr = 0;
563 600
577 { 614 {
578#ifdef HAVE_MMAP 615#ifdef HAVE_MMAP
579 if (stack->ssize > 0 && stack->sptr) 616 if (stack->ssize > 0 && stack->sptr)
580 munmap (stack->sptr, stack->ssize); 617 munmap (stack->sptr, stack->ssize);
581 else 618 else
582#else 619#endif
583 Safefree (stack->sptr); 620 Safefree (stack->sptr);
584#endif 621
585 Safefree (stack); 622 Safefree (stack);
586 } 623 }
587 else if (ctx->gencnt == stack->gencnt) 624 else if (ctx->gencnt == stack->gencnt)
588 --stack->usecnt; 625 --stack->usecnt;
589 } 626 }
593setup_coro (void *arg) 630setup_coro (void *arg)
594{ 631{
595 /* 632 /*
596 * emulate part of the perl startup here. 633 * emulate part of the perl startup here.
597 */ 634 */
635 dTHX;
598 dSP; 636 dSP;
599 Coro__State ctx = (Coro__State)arg; 637 Coro__State ctx = (Coro__State)arg;
600 SV *sub_init = (SV*)get_cv(SUB_INIT, FALSE); 638 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE);
601 639
602 coro_init_stacks (aTHX); 640 coro_init_stacks (aTHX);
603 /*PL_curcop = 0;*/ 641 /*PL_curcop = 0;*/
604 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 642 /*PL_in_eval = PL_in_eval;*/ /* inherit */
605 SvREFCNT_dec (GvAV (PL_defgv)); 643 SvREFCNT_dec (GvAV (PL_defgv));
606 GvAV (PL_defgv) = ctx->args; 644 GvAV (PL_defgv) = ctx->args; ctx->args = 0;
607 645
608 SPAGAIN; 646 SPAGAIN;
609 647
610 if (ctx->stack) 648 if (ctx->stack)
611 { 649 {
638 * that doesn't matter, though, since it is only 676 * that doesn't matter, though, since it is only
639 * pp_nextstate and we never return... 677 * pp_nextstate and we never return...
640 * ah yes, and I don't care anyways ;) 678 * ah yes, and I don't care anyways ;)
641 */ 679 */
642 PUTBACK; 680 PUTBACK;
643 PL_op = pp_entersub(); 681 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
644 SPAGAIN; 682 SPAGAIN;
645 683
646 ENTER; /* necessary e.g. for dounwind */ 684 ENTER; /* necessary e.g. for dounwind */
647 } 685 }
648} 686}
651continue_coro (void *arg) 689continue_coro (void *arg)
652{ 690{
653 /* 691 /*
654 * this is a _very_ stripped down perl interpreter ;) 692 * this is a _very_ stripped down perl interpreter ;)
655 */ 693 */
694 dTHX;
656 Coro__State ctx = (Coro__State)arg; 695 Coro__State ctx = (Coro__State)arg;
657 JMPENV coro_start_env; 696 JMPENV coro_start_env;
658 697
659 /* same as JMPENV_BOOTSTRAP */
660 Zero(&coro_start_env, 1, JMPENV);
661 coro_start_env.je_ret = -1;
662 coro_start_env.je_mustcatch = TRUE;
663 PL_top_env = &coro_start_env; 698 PL_top_env = &ctx->start_env;
664 699
665 ctx->cursp = 0; 700 ctx->cursp = 0;
666 PL_op = PL_op->op_next; 701 PL_op = PL_op->op_next;
667 CALLRUNOPS(aTHX); 702 CALLRUNOPS(aTHX);
668 703
669 abort (); 704 abort ();
670} 705}
671 706
672STATIC void 707STATIC void
673transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 708transfer (pTHX_ struct coro *prev, struct coro *next, int flags)
674{ 709{
675 dSTACKLEVEL; 710 dSTACKLEVEL;
676 static struct coro *xnext;
677 711
678 if (prev != next) 712 if (prev != next)
679 { 713 {
680 xnext = next;
681
682 if (next->mainstack) 714 if (next->mainstack)
683 { 715 {
716 LOCK;
684 SAVE (prev, flags); 717 SAVE (prev, flags);
685 LOAD (next); 718 LOAD (next);
719 UNLOCK;
686 720
687 /* mark this state as in-use */ 721 /* mark this state as in-use */
688 next->mainstack = 0; 722 next->mainstack = 0;
689 next->tmps_ix = -2; 723 next->tmps_ix = -2;
690 724
709 continue_coro, (void *)next, 743 continue_coro, (void *)next,
710 next->stack->sptr, labs (next->stack->ssize)); 744 next->stack->sptr, labs (next->stack->ssize));
711 } 745 }
712 746
713 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 747 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
748 prev->cursp = stacklevel;
714 /* don't add any code here */ 749 /* don't add any code here */
715 } 750 }
716 751 else
752 next->cursp = stacklevel;
717 } 753 }
718 else if (next->tmps_ix == -2) 754 else if (next->tmps_ix == -2)
719 croak ("tried to transfer to running coroutine"); 755 croak ("tried to transfer to running coroutine");
720 else 756 else
721 { 757 {
758 LOCK;
722 SAVE (prev, -1); /* first get rid of the old state */ 759 SAVE (prev, -1); /* first get rid of the old state */
760 UNLOCK;
723 761
724 if (flags & TRANSFER_SAVE_CCTXT) 762 if (flags & TRANSFER_SAVE_CCTXT)
725 { 763 {
726 if (!prev->stack) 764 if (!prev->stack)
727 allocate_stack (prev, 0); 765 allocate_stack (prev, 0);
728 766
729 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK) 767 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
730 { 768 {
769 PL_top_env = &next->start_env;
770
731 setup_coro (next); 771 setup_coro (next);
772 next->cursp = stacklevel;
732 773
733 prev->stack->refcnt++; 774 prev->stack->refcnt++;
734 prev->stack->usecnt++; 775 prev->stack->usecnt++;
735 next->stack = prev->stack; 776 next->stack = prev->stack;
736 next->gencnt = prev->gencnt; 777 next->gencnt = prev->gencnt;
737 } 778 }
738 else 779 else
739 { 780 {
781 assert (!next->stack);
740 allocate_stack (next, 1); 782 allocate_stack (next, 1);
741 coro_create (&(next->stack->cctx), 783 coro_create (&(next->stack->cctx),
742 setup_coro, (void *)next, 784 setup_coro, (void *)next,
743 next->stack->sptr, labs (next->stack->ssize)); 785 next->stack->sptr, labs (next->stack->ssize));
744 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 786 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
787 prev->cursp = stacklevel;
745 /* don't add any code here */ 788 /* don't add any code here */
746 } 789 }
747 } 790 }
748 else 791 else
792 {
749 setup_coro (next); 793 setup_coro (next);
794 next->cursp = stacklevel;
795 }
750 } 796 }
751
752 /*
753 * xnext is now either prev or next, depending on wether
754 * we switched the c stack or not. that's why i use a global
755 * variable, that should become thread-specific at one point.
756 */
757 xnext->cursp = stacklevel;
758 } 797 }
759 798
799 LOCK;
760 if (coro_mortal) 800 if (coro_mortal)
761 { 801 {
762 SvREFCNT_dec (coro_mortal); 802 SvREFCNT_dec (coro_mortal);
763 coro_mortal = 0; 803 coro_mortal = 0;
764 } 804 }
805 UNLOCK;
765} 806}
766 807
767#define SV_CORO(sv,func) \ 808#define SV_CORO(sv,func) \
768 do { \ 809 do { \
769 if (SvROK (sv)) \ 810 if (SvROK (sv)) \
770 sv = SvRV (sv); \ 811 sv = SvRV (sv); \
771 \ 812 \
772 if (SvTYPE(sv) == SVt_PVHV) \ 813 if (SvTYPE (sv) == SVt_PVHV) \
773 { \ 814 { \
774 HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ 815 HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
775 \ 816 \
776 if (!he) \ 817 if (!he) \
777 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ 818 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
778 \ 819 \
779 (sv) = SvRV (HeVAL(he)); \ 820 (sv) = SvRV (HeVAL(he)); \
780 } \ 821 } \
781 \ 822 \
782 /* must also be changed inside Coro::Cont::yield */ \ 823 /* must also be changed inside Coro::Cont::yield */ \
783 if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \ 824 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
784 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ 825 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
785 \ 826 \
786 } while(0) 827 } while(0)
787 828
788#define SvSTATE(sv) (struct coro *)SvIV (sv) 829#define SvSTATE(sv) (struct coro *)SvIV (sv)
791api_transfer(pTHX_ SV *prev, SV *next, int flags) 832api_transfer(pTHX_ SV *prev, SV *next, int flags)
792{ 833{
793 SV_CORO (prev, "Coro::transfer"); 834 SV_CORO (prev, "Coro::transfer");
794 SV_CORO (next, "Coro::transfer"); 835 SV_CORO (next, "Coro::transfer");
795 836
796 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags); 837 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
797} 838}
798 839
799/** Coro ********************************************************************/ 840/** Coro ********************************************************************/
800 841
801#define PRIO_MAX 3 842#define PRIO_MAX 3
809static GV *coro_current, *coro_idle; 850static GV *coro_current, *coro_idle;
810static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 851static AV *coro_ready[PRIO_MAX-PRIO_MIN+1];
811static int coro_nready; 852static int coro_nready;
812 853
813static void 854static void
814coro_enq (SV *sv) 855coro_enq (pTHX_ SV *sv)
815{ 856{
816 if (SvTYPE (sv) == SVt_PVHV) 857 if (SvTYPE (sv) == SVt_PVHV)
817 { 858 {
818 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0); 859 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
819 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 860 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
830 871
831 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 872 croak ("Coro::ready tried to enqueue something that is not a coroutine");
832} 873}
833 874
834static SV * 875static SV *
835coro_deq (int min_prio) 876coro_deq (pTHX_ int min_prio)
836{ 877{
837 int prio = PRIO_MAX - PRIO_MIN; 878 int prio = PRIO_MAX - PRIO_MIN;
838 879
839 min_prio -= PRIO_MIN; 880 min_prio -= PRIO_MIN;
840 if (min_prio < 0) 881 if (min_prio < 0)
851} 892}
852 893
853static void 894static void
854api_ready (SV *coro) 895api_ready (SV *coro)
855{ 896{
897 dTHX;
898
856 if (SvROK (coro)) 899 if (SvROK (coro))
857 coro = SvRV (coro); 900 coro = SvRV (coro);
858 901
902 LOCK;
859 coro_enq (SvREFCNT_inc (coro)); 903 coro_enq (aTHX_ SvREFCNT_inc (coro));
904 UNLOCK;
860} 905}
861 906
862static void 907static void
863api_schedule (void) 908api_schedule (void)
864{ 909{
910 dTHX;
911
865 SV *prev, *next; 912 SV *prev, *next;
866 913
914 LOCK;
915
867 prev = SvRV (GvSV (coro_current)); 916 prev = SvRV (GvSV (coro_current));
868 next = coro_deq (PRIO_MIN); 917 next = coro_deq (aTHX_ PRIO_MIN);
869 918
870 if (!next) 919 if (!next)
871 next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); 920 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
872 921
922 /* free this only after the transfer */
873 coro_mortal = prev; 923 coro_mortal = prev;
874 SV_CORO (prev, "Coro::schedule"); 924 SV_CORO (prev, "Coro::schedule");
875 925
876 SvRV (GvSV (coro_current)) = next; 926 SvRV (GvSV (coro_current)) = next;
877 927
878 SV_CORO (next, "Coro::schedule"); 928 SV_CORO (next, "Coro::schedule");
879 929
930 UNLOCK;
931
880 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), 932 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
881 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 933 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
882} 934}
883 935
884static void 936static void
885api_cede (void) 937api_cede (void)
886{ 938{
939 dTHX;
940
941 LOCK;
887 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); 942 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current))));
943 UNLOCK;
888 944
889 api_schedule (); 945 api_schedule ();
890} 946}
891 947
892MODULE = Coro::State PACKAGE = Coro::State 948MODULE = Coro::State PACKAGE = Coro::State
893 949
894PROTOTYPES: ENABLE 950PROTOTYPES: ENABLE
895 951
896BOOT: 952BOOT:
897{ /* {} necessary for stoopid perl-5.6.x */ 953{ /* {} necessary for stoopid perl-5.6.x */
954#ifdef USE_ITHREADS
955 MUTEX_INIT (&coro_mutex);
956#endif
957
898 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1); 958 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
899 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1); 959 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
900 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 960 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
901 961
902 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 962 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
903 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 963 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
904 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 964 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
905 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT)); 965 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
906
907 if (!padlist_cache)
908 padlist_cache = newHV ();
909 966
910 main_mainstack = PL_mainstack; 967 main_mainstack = PL_mainstack;
911 968
912 coroapi.ver = CORO_API_VERSION; 969 coroapi.ver = CORO_API_VERSION;
913 coroapi.transfer = api_transfer; 970 coroapi.transfer = api_transfer;
921 Coro__State coro; 978 Coro__State coro;
922 979
923 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV) 980 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV)
924 croak ("Coro::State::_newprocess expects an arrayref"); 981 croak ("Coro::State::_newprocess expects an arrayref");
925 982
926 New (0, coro, 1, struct coro); 983 Newz (0, coro, 1, struct coro);
927 984
928 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 985 coro->args = (AV *)SvREFCNT_inc (SvRV (args));
929 coro->mainstack = 0; /* actual work is done inside transfer */ 986 /*coro->mainstack = 0; *//*actual work is done inside transfer */
930 coro->stack = 0; 987 /*coro->stack = 0;*/
988
989 /* same as JMPENV_BOOTSTRAP */
990 /* we might be able to recycle start_env, but safe is safe */
991 /*Zero(&coro->start_env, 1, JMPENV);*/
992 coro->start_env.je_ret = -1;
993 coro->start_env.je_mustcatch = TRUE;
931 994
932 RETVAL = coro; 995 RETVAL = coro;
933 OUTPUT: 996 OUTPUT:
934 RETVAL 997 RETVAL
935 998
954 if (coro->mainstack && coro->mainstack != main_mainstack) 1017 if (coro->mainstack && coro->mainstack != main_mainstack)
955 { 1018 {
956 struct coro temp; 1019 struct coro temp;
957 1020
958 PUTBACK; 1021 PUTBACK;
959 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 1022 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
960 LOAD(aTHX_ coro); 1023 LOAD (aTHX_ coro);
961 SPAGAIN; 1024 SPAGAIN;
962 1025
963 destroy_stacks (aTHX); 1026 destroy_stacks (aTHX);
964 1027
965 LOAD((&temp)); /* this will get rid of defsv etc.. */ 1028 LOAD ((&temp)); /* this will get rid of defsv etc.. */
966 SPAGAIN; 1029 SPAGAIN;
967 1030
968 coro->mainstack = 0; 1031 coro->mainstack = 0;
969 } 1032 }
970 1033
971 deallocate_stack (coro); 1034 deallocate_stack (coro);
972 1035 SvREFCNT_dec (coro->args);
973 Safefree (coro); 1036 Safefree (coro);
974
975void
976flush()
977 CODE:
978#ifdef MAY_FLUSH
979 flush_padlist_cache ();
980#endif
981 1037
982void 1038void
983_exit(code) 1039_exit(code)
984 int code 1040 int code
985 PROTOTYPE: $ 1041 PROTOTYPE: $
986 CODE: 1042 CODE:
987#if defined(__GLIBC__) || _POSIX_C_SOURCE
988 _exit (code); 1043 _exit (code);
989#else
990 signal (SIGTERM, SIG_DFL);
991 raise (SIGTERM);
992 exit (code);
993#endif
994 1044
995MODULE = Coro::State PACKAGE = Coro::Cont 1045MODULE = Coro::State PACKAGE = Coro::Cont
996 1046
997# this is slightly dirty (should expose a c-level api) 1047# this is slightly dirty (should expose a c-level api)
998 1048
1004 SV *sv; 1054 SV *sv;
1005 AV *defav = GvAV (PL_defgv); 1055 AV *defav = GvAV (PL_defgv);
1006 struct coro *prev, *next; 1056 struct coro *prev, *next;
1007 1057
1008 if (!returnstk) 1058 if (!returnstk)
1009 returnstk = SvRV (get_sv ("Coro::Cont::return", FALSE)); 1059 returnstk = SvRV ((SV *)get_sv ("Coro::Cont::return", FALSE));
1010 1060
1011 /* set up @_ -- ugly */ 1061 /* set up @_ -- ugly */
1012 av_clear (defav); 1062 av_clear (defav);
1013 av_fill (defav, items - 1); 1063 av_fill (defav, items - 1);
1014 while (items--) 1064 while (items--)
1015 av_store (defav, items, SvREFCNT_inc (ST(items))); 1065 av_store (defav, items, SvREFCNT_inc (ST(items)));
1016 1066
1017 mg_get (returnstk); /* isn't documentation wrong for mg_get? */ 1067 SvGETMAGIC (returnstk); /* isn't documentation wrong for mg_get? */
1018 sv = av_pop ((AV *)SvRV (returnstk)); 1068 sv = av_pop ((AV *)SvRV (returnstk));
1019 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))); 1069 prev = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)));
1020 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))); 1070 next = (struct coro *)SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
1021 SvREFCNT_dec (sv); 1071 SvREFCNT_dec (sv);
1022 1072
1023 transfer(aTHX_ prev, next, 0); 1073 transfer (aTHX_ prev, next, 0);
1024 1074
1025MODULE = Coro::State PACKAGE = Coro 1075MODULE = Coro::State PACKAGE = Coro
1026 1076
1027# this is slightly dirty (should expose a c-level api) 1077# this is slightly dirty (should expose a c-level api)
1028 1078
1057 sv_setiv(sv, (IV)&coroapi); 1107 sv_setiv(sv, (IV)&coroapi);
1058 SvREADONLY_on(sv); 1108 SvREADONLY_on(sv);
1059 } 1109 }
1060} 1110}
1061 1111
1112#if !PERL_MICRO
1113
1062void 1114void
1063ready(self) 1115ready(self)
1064 SV * self 1116 SV * self
1065 PROTOTYPE: $ 1117 PROTOTYPE: $
1066 CODE: 1118 CODE:
1067 api_ready (self); 1119 api_ready (self);
1120
1121#endif
1068 1122
1069int 1123int
1070nready(...) 1124nready(...)
1071 PROTOTYPE: 1125 PROTOTYPE:
1072 CODE: 1126 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines