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.91 by root, Sat Nov 25 01:14:11 2006 UTC vs.
Revision 1.101 by root, Mon Nov 27 00:08:18 2006 UTC

1#define PERL_NO_GET_CONTEXT
2
3#include "libcoro/coro.c" 1#include "libcoro/coro.c"
4 2
5#include "EXTERN.h" 3#include "EXTERN.h"
6#include "perl.h" 4#include "perl.h"
7#include "XSUB.h" 5#include "XSUB.h"
8 6
9#include "patchlevel.h" 7#include "patchlevel.h"
10 8
11#if PERL_VERSION < 6 9#define PERL_VERSION_ATLEAST(a,b,c) \
10 (PERL_REVISION > (a) \
11 || (PERL_REVISION == (a) \
12 && (PERL_VERSION > (b) \
13 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
14
15#if !PERL_VERSION_ATLEAST (5,8,0)
12# ifndef PL_ppaddr 16# ifndef PL_ppaddr
13# define PL_ppaddr ppaddr 17# define PL_ppaddr ppaddr
14# endif 18# endif
15# ifndef call_sv 19# ifndef call_sv
16# define call_sv perl_call_sv 20# define call_sv perl_call_sv
27# ifndef IS_PADCONST 31# ifndef IS_PADCONST
28# define IS_PADCONST(v) 0 32# define IS_PADCONST(v) 0
29# endif 33# endif
30#endif 34#endif
31 35
36#include <stdio.h>
32#include <errno.h> 37#include <errno.h>
33 38
34#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 39#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
35# undef STACKGUARD 40# undef STACKGUARD
36#endif 41#endif
57# else 62# else
58# define BOOT_PAGESIZE 63# define BOOT_PAGESIZE
59# endif 64# endif
60#endif 65#endif
61 66
62#define SUB_INIT "Coro::State::initialize"
63
64/* The next macro should declare a variable stacklevel that contains and approximation 67/* The next macro should declare a variable stacklevel that contains and approximation
65 * to the current C stack pointer. Its property is that it changes with each call 68 * to the current C stack pointer. Its property is that it changes with each call
66 * and should be unique. */ 69 * and should be unique. */
70#define dSTACKLEVEL int stacklevel
67#define dSTACKLEVEL void *stacklevel = &stacklevel 71#define STACKLEVEL ((void *)&stacklevel)
68 72
69#define IN_DESTRUCT (PL_main_cv == Nullcv) 73#define IN_DESTRUCT (PL_main_cv == Nullcv)
70 74
71#define labs(l) ((l) >= 0 ? (l) : -(l)) 75#if __GNUC__ >= 3
76# define attribute(x) __attribute__(x)
77#else
78# define attribute(x)
79#endif
80
81#define NOINLINE attribute ((noinline))
72 82
73#include "CoroAPI.h" 83#include "CoroAPI.h"
84
85#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
74 86
75#ifdef USE_ITHREADS 87#ifdef USE_ITHREADS
76static perl_mutex coro_mutex; 88static perl_mutex coro_mutex;
77# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 89# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
78# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 90# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
84static struct CoroAPI coroapi; 96static struct CoroAPI coroapi;
85static AV *main_mainstack; /* used to differentiate between $main and others */ 97static AV *main_mainstack; /* used to differentiate between $main and others */
86static HV *coro_state_stash, *coro_stash; 98static HV *coro_state_stash, *coro_stash;
87static SV *coro_mortal; /* will be freed after next transfer */ 99static SV *coro_mortal; /* will be freed after next transfer */
88 100
89/* this is actually not just the c stack but also c registers etc... */ 101/* this is a structure representing a c-level coroutine */
90typedef struct coro_stack { 102typedef struct coro_stack {
91 struct coro_stack *next; 103 struct coro_stack *next;
92 104
93 void *idle_sp; 105 /* the stack */
94 void *sptr; 106 void *sptr;
95 long ssize; /* positive == mmap, otherwise malloc */ 107 long ssize; /* positive == mmap, otherwise malloc */
96 108
97 /* cpu state */ 109 /* cpu state */
110 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
111 JMPENV *top_env;
98 coro_context cctx; 112 coro_context cctx;
99} coro_stack; 113} coro_stack;
100 114
101static coro_stack *main_stack; 115/* this is a structure representing a perl-level coroutine */
102
103struct coro { 116struct coro {
117 /* the c coroutine allocated to this perl coroutine, if any */
118 coro_stack *stack;
119
120 /* data associated with this coroutine (initial args) */
121 AV *args;
122 int refcnt;
123
104 /* optionally saved, might be zero */ 124 /* optionally saved, might be zero */
105 AV *defav; 125 AV *defav;
106 SV *defsv; 126 SV *defsv;
107 SV *errsv; 127 SV *errsv;
108 128
109 /* saved global state not related to stacks */ 129 /* saved global state not related to stacks */
110 U8 dowarn; 130 U8 dowarn;
111 I32 in_eval; 131 I32 in_eval;
112
113 /* the c stack, if any */
114 coro_stack *stack;
115 132
116 /* the stacks and related info (callchain etc..) */ 133 /* the stacks and related info (callchain etc..) */
117 PERL_SI *curstackinfo; 134 PERL_SI *curstackinfo;
118 AV *curstack; 135 AV *curstack;
119 AV *mainstack; 136 AV *mainstack;
140 OP **retstack; 157 OP **retstack;
141 I32 retstack_ix; 158 I32 retstack_ix;
142 I32 retstack_max; 159 I32 retstack_max;
143 PMOP *curpm; 160 PMOP *curpm;
144 COP *curcop; 161 COP *curcop;
145 JMPENV *top_env;
146 162
147 /* coro process data */ 163 /* coro process data */
148 int prio; 164 int prio;
149
150 /* data associated with this coroutine (initial args) */
151 AV *args;
152 int refcnt;
153}; 165};
154 166
155typedef struct coro *Coro__State; 167typedef struct coro *Coro__State;
156typedef struct coro *Coro__State_or_hashref; 168typedef struct coro *Coro__State_or_hashref;
157 169
158static AV * 170static AV *
159coro_clone_padlist (pTHX_ CV *cv) 171coro_clone_padlist (CV *cv)
160{ 172{
161 AV *padlist = CvPADLIST (cv); 173 AV *padlist = CvPADLIST (cv);
162 AV *newpadlist, *newpad; 174 AV *newpadlist, *newpad;
163 175
164 newpadlist = newAV (); 176 newpadlist = newAV ();
165 AvREAL_off (newpadlist); 177 AvREAL_off (newpadlist);
166#if PERL_VERSION < 9 178#if PERL_VERSION_ATLEAST (5,9,0)
179 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
180#else
167 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 181 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
168#else
169 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
170#endif 182#endif
171 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 183 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
172 --AvFILLp (padlist); 184 --AvFILLp (padlist);
173 185
174 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 186 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
176 188
177 return newpadlist; 189 return newpadlist;
178} 190}
179 191
180static void 192static void
181free_padlist (pTHX_ AV *padlist) 193free_padlist (AV *padlist)
182{ 194{
183 /* may be during global destruction */ 195 /* may be during global destruction */
184 if (SvREFCNT (padlist)) 196 if (SvREFCNT (padlist))
185 { 197 {
186 I32 i = AvFILLp (padlist); 198 I32 i = AvFILLp (padlist);
207 AV *padlist; 219 AV *padlist;
208 AV *av = (AV *)mg->mg_obj; 220 AV *av = (AV *)mg->mg_obj;
209 221
210 /* casting is fun. */ 222 /* casting is fun. */
211 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 223 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
212 free_padlist (aTHX_ padlist); 224 free_padlist (padlist);
213 225
214 SvREFCNT_dec (av); 226 SvREFCNT_dec (av);
215 227
216 return 0; 228 return 0;
217} 229}
220 232
221static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 233static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
222 234
223/* the next two functions merely cache the padlists */ 235/* the next two functions merely cache the padlists */
224static void 236static void
225get_padlist (pTHX_ CV *cv) 237get_padlist (CV *cv)
226{ 238{
227 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 239 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
228 240
229 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 241 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
230 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 242 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
231 else 243 else
232 { 244 {
233#if 0 245#if 0
234 /* this should work - but it doesn't :( */ 246 /* this is probably cleaner, but also slower? */
235 CV *cp = Perl_cv_clone (aTHX_ cv); 247 CV *cp = Perl_cv_clone (cv);
236 CvPADLIST (cv) = CvPADLIST (cp); 248 CvPADLIST (cv) = CvPADLIST (cp);
237 CvPADLIST (cp) = 0; 249 CvPADLIST (cp) = 0;
238 SvREFCNT_dec (cp); 250 SvREFCNT_dec (cp);
239#else 251#else
240 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 252 CvPADLIST (cv) = coro_clone_padlist (cv);
241#endif 253#endif
242 } 254 }
243} 255}
244 256
245static void 257static void
246put_padlist (pTHX_ CV *cv) 258put_padlist (CV *cv)
247{ 259{
248 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 260 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
249 261
250 if (!mg) 262 if (!mg)
251 { 263 {
259} 271}
260 272
261#define SB do { 273#define SB do {
262#define SE } while (0) 274#define SE } while (0)
263 275
264#define LOAD(state) load_state(aTHX_ (state)); 276#define LOAD(state) load_state((state));
265#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); 277#define SAVE(state,flags) save_state((state),(flags));
266 278
267#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 279#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
268 280
269static void 281static void
270load_state(pTHX_ Coro__State c) 282load_state(Coro__State c)
271{ 283{
272 PL_dowarn = c->dowarn; 284 PL_dowarn = c->dowarn;
273 PL_in_eval = c->in_eval; 285 PL_in_eval = c->in_eval;
274 286
275 PL_curstackinfo = c->curstackinfo; 287 PL_curstackinfo = c->curstackinfo;
293 PL_scopestack_ix = c->scopestack_ix; 305 PL_scopestack_ix = c->scopestack_ix;
294 PL_scopestack_max = c->scopestack_max; 306 PL_scopestack_max = c->scopestack_max;
295 PL_savestack = c->savestack; 307 PL_savestack = c->savestack;
296 PL_savestack_ix = c->savestack_ix; 308 PL_savestack_ix = c->savestack_ix;
297 PL_savestack_max = c->savestack_max; 309 PL_savestack_max = c->savestack_max;
298#if PERL_VERSION < 9 310#if !PERL_VERSION_ATLEAST (5,9,0)
299 PL_retstack = c->retstack; 311 PL_retstack = c->retstack;
300 PL_retstack_ix = c->retstack_ix; 312 PL_retstack_ix = c->retstack_ix;
301 PL_retstack_max = c->retstack_max; 313 PL_retstack_max = c->retstack_max;
302#endif 314#endif
303 PL_curpm = c->curpm; 315 PL_curpm = c->curpm;
304 PL_curcop = c->curcop; 316 PL_curcop = c->curcop;
305 PL_top_env = c->top_env;
306 317
307 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 318 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
308 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 319 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
309 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 320 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
310 321
317 { 328 {
318 AV *padlist = (AV *)POPs; 329 AV *padlist = (AV *)POPs;
319 330
320 if (padlist) 331 if (padlist)
321 { 332 {
322 put_padlist (aTHX_ cv); /* mark this padlist as available */ 333 put_padlist (cv); /* mark this padlist as available */
323 CvPADLIST(cv) = padlist; 334 CvPADLIST(cv) = padlist;
324 } 335 }
325 336
326 ++CvDEPTH(cv); 337 ++CvDEPTH(cv);
327 } 338 }
329 PUTBACK; 340 PUTBACK;
330 } 341 }
331} 342}
332 343
333static void 344static void
334save_state(pTHX_ Coro__State c, int flags) 345save_state(Coro__State c, int flags)
335{ 346{
336 { 347 {
337 dSP; 348 dSP;
338 I32 cxix = cxstack_ix; 349 I32 cxix = cxstack_ix;
339 PERL_CONTEXT *ccstk = cxstack; 350 PERL_CONTEXT *ccstk = cxstack;
367 } 378 }
368 379
369 PUSHs ((SV *)CvPADLIST(cv)); 380 PUSHs ((SV *)CvPADLIST(cv));
370 PUSHs ((SV *)cv); 381 PUSHs ((SV *)cv);
371 382
372 get_padlist (aTHX_ cv); 383 get_padlist (cv);
373 } 384 }
374 } 385 }
375#ifdef CXt_FORMAT 386#ifdef CXt_FORMAT
376 else if (CxTYPE(cx) == CXt_FORMAT) 387 else if (CxTYPE(cx) == CXt_FORMAT)
377 { 388 {
421 c->scopestack_ix = PL_scopestack_ix; 432 c->scopestack_ix = PL_scopestack_ix;
422 c->scopestack_max = PL_scopestack_max; 433 c->scopestack_max = PL_scopestack_max;
423 c->savestack = PL_savestack; 434 c->savestack = PL_savestack;
424 c->savestack_ix = PL_savestack_ix; 435 c->savestack_ix = PL_savestack_ix;
425 c->savestack_max = PL_savestack_max; 436 c->savestack_max = PL_savestack_max;
426#if PERL_VERSION < 9 437#if !PERL_VERSION_ATLEAST (5,9,0)
427 c->retstack = PL_retstack; 438 c->retstack = PL_retstack;
428 c->retstack_ix = PL_retstack_ix; 439 c->retstack_ix = PL_retstack_ix;
429 c->retstack_max = PL_retstack_max; 440 c->retstack_max = PL_retstack_max;
430#endif 441#endif
431 c->curpm = PL_curpm; 442 c->curpm = PL_curpm;
432 c->curcop = PL_curcop; 443 c->curcop = PL_curcop;
433 c->top_env = PL_top_env;
434} 444}
435 445
436/* 446/*
437 * allocate various perl stacks. This is an exact copy 447 * allocate various perl stacks. This is an exact copy
438 * of perl.c:init_stacks, except that it uses less memory 448 * of perl.c:init_stacks, except that it uses less memory
439 * on the (sometimes correct) assumption that coroutines do 449 * on the (sometimes correct) assumption that coroutines do
440 * not usually need a lot of stackspace. 450 * not usually need a lot of stackspace.
441 */ 451 */
442static void 452static void
443coro_init_stacks (pTHX) 453coro_init_stacks ()
444{ 454{
445 LOCK;
446
447 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 455 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1);
448 PL_curstackinfo->si_type = PERLSI_MAIN; 456 PL_curstackinfo->si_type = PERLSI_MAIN;
449 PL_curstack = PL_curstackinfo->si_stack; 457 PL_curstack = PL_curstackinfo->si_stack;
450 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 458 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
451 459
472 480
473 New(54,PL_savestack,96,ANY); 481 New(54,PL_savestack,96,ANY);
474 PL_savestack_ix = 0; 482 PL_savestack_ix = 0;
475 PL_savestack_max = 96; 483 PL_savestack_max = 96;
476 484
477#if PERL_VERSION < 9 485#if !PERL_VERSION_ATLEAST (5,9,0)
478 New(54,PL_retstack,8,OP*); 486 New(54,PL_retstack,8,OP*);
479 PL_retstack_ix = 0; 487 PL_retstack_ix = 0;
480 PL_retstack_max = 8; 488 PL_retstack_max = 8;
481#endif 489#endif
482
483 UNLOCK;
484} 490}
485 491
486/* 492/*
487 * destroy the stacks, the callchain etc... 493 * destroy the stacks, the callchain etc...
488 */ 494 */
489static void 495static void
490destroy_stacks(pTHX) 496destroy_stacks()
491{ 497{
492 if (!IN_DESTRUCT) 498 if (!IN_DESTRUCT)
493 { 499 {
494 /* is this ugly, I ask? */ 500 /* is this ugly, I ask? */
495 LEAVE_SCOPE (0); 501 LEAVE_SCOPE (0);
526 532
527 Safefree (PL_tmps_stack); 533 Safefree (PL_tmps_stack);
528 Safefree (PL_markstack); 534 Safefree (PL_markstack);
529 Safefree (PL_scopestack); 535 Safefree (PL_scopestack);
530 Safefree (PL_savestack); 536 Safefree (PL_savestack);
531#if PERL_VERSION < 9 537#if !PERL_VERSION_ATLEAST (5,9,0)
532 Safefree (PL_retstack); 538 Safefree (PL_retstack);
533#endif 539#endif
534} 540}
535 541
536static void 542static void
540 * emulate part of the perl startup here. 546 * emulate part of the perl startup here.
541 */ 547 */
542 dTHX; 548 dTHX;
543 dSP; 549 dSP;
544 UNOP myop; 550 UNOP myop;
545 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); 551 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
546 552
547 coro_init_stacks (aTHX); 553 coro_init_stacks ();
548 /*PL_curcop = 0;*/ 554 /*PL_curcop = 0;*/
549 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 555 /*PL_in_eval = PL_in_eval;*/ /* inherit */
550 SvREFCNT_dec (GvAV (PL_defgv)); 556 SvREFCNT_dec (GvAV (PL_defgv));
551 GvAV (PL_defgv) = coro->args; coro->args = 0; 557 GvAV (PL_defgv) = coro->args; coro->args = 0;
552 558
566 572
567 ENTER; /* necessary e.g. for dounwind */ 573 ENTER; /* necessary e.g. for dounwind */
568} 574}
569 575
570static void 576static void
571transfer_tail () 577free_coro_mortal ()
572{ 578{
573 if (coro_mortal) 579 if (coro_mortal)
574 { 580 {
575 SvREFCNT_dec (coro_mortal); 581 SvREFCNT_dec (coro_mortal);
576 coro_mortal = 0; 582 coro_mortal = 0;
577 } 583 }
584}
578 585
579 UNLOCK; 586static void NOINLINE
587prepare_cctx (coro_stack *cctx)
588{
589 dSP;
590 UNOP myop;
591
592 Zero (&myop, 1, UNOP);
593 myop.op_next = PL_op;
594 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
595
596 PUSHMARK(SP);
597 EXTEND (SP, 2);
598 PUSHs (newSViv (PTR2IV (cctx)));
599 PUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE));
600 PUTBACK;
601 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
602 SPAGAIN;
580} 603}
581 604
582static void 605static void
583coro_run (void *arg) 606coro_run (void *arg)
584{ 607{
585 /* 608 /*
586 * this is a _very_ stripped down perl interpreter ;) 609 * this is a _very_ stripped down perl interpreter ;)
587 */ 610 */
588 dTHX; 611 UNLOCK;
589
590 transfer_tail ();
591 612
592 PL_top_env = &PL_start_env; 613 PL_top_env = &PL_start_env;
593 PL_restartop = PL_op; 614 prepare_cctx ((coro_stack *)arg);
615
594 /* somebody will hit me for both perl_run and PL_restart_top */ 616 /* somebody will hit me for both perl_run and PL_restartop */
595 perl_run (aTHX_ PERL_GET_CONTEXT); 617 perl_run (PERL_GET_CONTEXT);
596 618
619 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr);
597 abort (); 620 abort ();
598} 621}
599 622
600static coro_stack * 623static coro_stack *
601stack_new () 624stack_new ()
604 627
605 New (0, stack, 1, coro_stack); 628 New (0, stack, 1, coro_stack);
606 629
607#if HAVE_MMAP 630#if HAVE_MMAP
608 631
609 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ 632 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
633 /* mmap suppsedly does allocate-on-write for us */
610 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 634 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
611 635
612 if (stack->sptr == (void *)-1) 636 if (stack->sptr == (void *)-1)
613 { 637 {
614 fprintf (stderr, "FATAL: unable to mmap stack for coroutine\n"); 638 perror ("FATAL: unable to mmap stack for coroutine");
615 _exit (EXIT_FAILURE); 639 _exit (EXIT_FAILURE);
616 } 640 }
617 641
642# if STACKGUARD
618 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 643 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
644# endif
619 645
620#else 646#else
621 647
622 stack->ssize = STACKSIZE * (long)sizeof (long); 648 stack->ssize = STACKSIZE * (long)sizeof (long);
623 New (0, stack->sptr, STACKSIZE, long); 649 New (0, stack->sptr, STACKSIZE, long);
624 650
625 if (!stack->sptr) 651 if (!stack->sptr)
626 { 652 {
627 fprintf (stderr, "FATAL: unable to malloc stack for coroutine\n"); 653 perror (stderr, "FATAL: unable to malloc stack for coroutine");
628 _exit (EXIT_FAILURE); 654 _exit (EXIT_FAILURE);
629 } 655 }
630 656
631#endif 657#endif
632 658
633 coro_create (&stack->cctx, coro_run, 0, stack->sptr, stack->ssize); 659 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize);
634 660
635 return stack; 661 return stack;
636} 662}
637 663
638static void 664static void
639stack_free (coro_stack *stack) 665stack_free (coro_stack *stack)
640{ 666{
641 if (!stack || stack == main_stack) 667 if (!stack)
642 return; 668 return;
643 669
644#if HAVE_MMAP 670#if HAVE_MMAP
645 munmap (stack->sptr, stack->ssize); 671 munmap (stack->sptr, stack->ssize);
646#else 672#else
677 stack->next = stack_first; 703 stack->next = stack_first;
678 stack_first = stack; 704 stack_first = stack;
679} 705}
680 706
681/* never call directly, always through the coro_state_transfer global variable */ 707/* never call directly, always through the coro_state_transfer global variable */
682static void 708static void NOINLINE
683transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 709transfer (struct coro *prev, struct coro *next, int flags)
684{ 710{
685 dSTACKLEVEL; 711 dSTACKLEVEL;
686 712
687 /* sometimes transfer is only called to set idle_sp */ 713 /* sometimes transfer is only called to set idle_sp */
688 if (!prev->stack->idle_sp) 714 if (flags == TRANSFER_SET_STACKLEVEL)
689 prev->stack->idle_sp = stacklevel; 715 ((coro_stack *)prev)->idle_sp = STACKLEVEL;
690
691 LOCK;
692
693 if (prev != next) 716 else if (prev != next)
694 { 717 {
695 coro_stack *prev_stack = prev->stack; 718 coro_stack *prev__stack;
696 719
697 /* possibly "free" the stack */ 720 LOCK;
698 if (0 && prev_stack->idle_sp == stacklevel)
699 {
700 stack_put (prev_stack);
701 prev->stack = 0;
702 }
703 721
704 if (next->mainstack) 722 if (next->mainstack)
705 { 723 {
706 /* coroutine already started */ 724 /* coroutine already started */
707 SAVE (prev, flags); 725 SAVE (prev, flags);
712 /* need to start coroutine */ 730 /* need to start coroutine */
713 /* first get rid of the old state */ 731 /* first get rid of the old state */
714 SAVE (prev, -1); 732 SAVE (prev, -1);
715 /* setup coroutine call */ 733 /* setup coroutine call */
716 setup_coro (next); 734 setup_coro (next);
717 /* need to change stack from main_stack to real one */ 735 /* need a stack */
718 next->stack = 0; 736 next->stack = 0;
719 } 737 }
720 738
739 if (!prev->stack)
740 /* create a new empty context */
741 Newz (0, prev->stack, 1, coro_stack);
742
743 prev__stack = prev->stack;
744
745 /* possibly "free" the stack */
746 if (prev__stack->idle_sp == STACKLEVEL)
747 {
748 stack_put (prev__stack);
749 prev->stack = 0;
750 }
751
721 if (!next->stack) 752 if (!next->stack)
722 next->stack = stack_get (); 753 next->stack = stack_get ();
723 754
724 if (prev_stack != next->stack) 755 if (prev__stack != next->stack)
756 {
757 prev__stack->top_env = PL_top_env;
758 PL_top_env = next->stack->top_env;
725 coro_transfer (&prev_stack->cctx, &next->stack->cctx); 759 coro_transfer (&prev__stack->cctx, &next->stack->cctx);
760 }
761
762 free_coro_mortal ();
763
764 UNLOCK;
726 } 765 }
727
728 transfer_tail ();
729} 766}
730 767
731/* use this function pointer to call the above function */ 768struct transfer_args
732/* this is done to increase chances of the compiler not inlining the call */ 769{
733void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; 770 struct coro *prev, *next;
771 int flags;
772};
773
774#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
734 775
735static void 776static void
736coro_state_destroy (struct coro *coro) 777coro_state_destroy (struct coro *coro)
737{ 778{
738 if (coro->refcnt--) 779 if (coro->refcnt--)
740 781
741 if (coro->mainstack && coro->mainstack != main_mainstack) 782 if (coro->mainstack && coro->mainstack != main_mainstack)
742 { 783 {
743 struct coro temp; 784 struct coro temp;
744 785
745 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 786 SAVE ((&temp), TRANSFER_SAVE_ALL);
746 LOAD (aTHX_ coro); 787 LOAD (coro);
747 788
748 destroy_stacks (aTHX); 789 destroy_stacks ();
749 790
750 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 791 LOAD ((&temp)); /* this will get rid of defsv etc.. */
751 792
752 coro->mainstack = 0; 793 coro->mainstack = 0;
753 } 794 }
756 SvREFCNT_dec (coro->args); 797 SvREFCNT_dec (coro->args);
757 Safefree (coro); 798 Safefree (coro);
758} 799}
759 800
760static int 801static int
761coro_state_clear (SV *sv, MAGIC *mg) 802coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
762{ 803{
763 struct coro *coro = (struct coro *)mg->mg_ptr; 804 struct coro *coro = (struct coro *)mg->mg_ptr;
764 mg->mg_ptr = 0; 805 mg->mg_ptr = 0;
765 806
766 coro_state_destroy (coro); 807 coro_state_destroy (coro);
767 808
768 return 0; 809 return 0;
769} 810}
770 811
771static int 812static int
772coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 813coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
773{ 814{
774 struct coro *coro = (struct coro *)mg->mg_ptr; 815 struct coro *coro = (struct coro *)mg->mg_ptr;
775 816
776 ++coro->refcnt; 817 ++coro->refcnt;
777 818
778 return 0; 819 return 0;
779} 820}
780 821
781static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 822static MGVTBL coro_state_vtbl = {
823 0, 0, 0, 0,
824 coro_state_clear,
825 0,
826#ifdef MGf_DUP
827 coro_state_dup,
828#endif
829};
782 830
783static struct coro * 831static struct coro *
784SvSTATE (SV *coro) 832SvSTATE (SV *coro)
785{ 833{
786 HV *stash; 834 HV *stash;
801 assert (mg->mg_type == PERL_MAGIC_ext); 849 assert (mg->mg_type == PERL_MAGIC_ext);
802 return (struct coro *)mg->mg_ptr; 850 return (struct coro *)mg->mg_ptr;
803} 851}
804 852
805static void 853static void
854prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
855{
856 ta->prev = SvSTATE (prev);
857 ta->next = SvSTATE (next);
858 ta->flags = flags;
859}
860
861static void
806api_transfer (pTHX_ SV *prev, SV *next, int flags) 862api_transfer (SV *prev, SV *next, int flags)
807{ 863{
808 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 864 dTHX;
865 struct transfer_args ta;
866
867 prepare_transfer (&ta, prev, next, flags);
868 TRANSFER (ta);
809} 869}
810 870
811/** Coro ********************************************************************/ 871/** Coro ********************************************************************/
812 872
813#define PRIO_MAX 3 873#define PRIO_MAX 3
821static GV *coro_current, *coro_idle; 881static GV *coro_current, *coro_idle;
822static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 882static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
823static int coro_nready; 883static int coro_nready;
824 884
825static void 885static void
826coro_enq (pTHX_ SV *sv) 886coro_enq (SV *sv)
827{ 887{
828 int prio; 888 int prio;
829 889
830 if (SvTYPE (sv) != SVt_PVHV) 890 if (SvTYPE (sv) != SVt_PVHV)
831 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 891 croak ("Coro::ready tried to enqueue something that is not a coroutine");
835 av_push (coro_ready [prio - PRIO_MIN], sv); 895 av_push (coro_ready [prio - PRIO_MIN], sv);
836 coro_nready++; 896 coro_nready++;
837} 897}
838 898
839static SV * 899static SV *
840coro_deq (pTHX_ int min_prio) 900coro_deq (int min_prio)
841{ 901{
842 int prio = PRIO_MAX - PRIO_MIN; 902 int prio = PRIO_MAX - PRIO_MIN;
843 903
844 min_prio -= PRIO_MIN; 904 min_prio -= PRIO_MIN;
845 if (min_prio < 0) 905 if (min_prio < 0)
862 922
863 if (SvROK (coro)) 923 if (SvROK (coro))
864 coro = SvRV (coro); 924 coro = SvRV (coro);
865 925
866 LOCK; 926 LOCK;
867 coro_enq (aTHX_ SvREFCNT_inc (coro)); 927 coro_enq (SvREFCNT_inc (coro));
868 UNLOCK; 928 UNLOCK;
869} 929}
870 930
871static void 931static void
872api_schedule (void) 932prepare_schedule (struct transfer_args *ta)
873{ 933{
874 dTHX;
875
876 SV *prev, *next; 934 SV *current, *prev, *next;
935
877 SV *current = GvSV (coro_current); 936 current = GvSV (coro_current);
878 937
879 for (;;) 938 for (;;)
880 { 939 {
881 LOCK; 940 LOCK;
882
883 next = coro_deq (aTHX_ PRIO_MIN); 941 next = coro_deq (PRIO_MIN);
942 UNLOCK;
884 943
885 if (next) 944 if (next)
886 break; 945 break;
887
888 UNLOCK;
889 946
890 { 947 {
891 dSP; 948 dSP;
892 949
893 ENTER; 950 ENTER;
904 961
905 prev = SvRV (current); 962 prev = SvRV (current);
906 SvRV (current) = next; 963 SvRV (current) = next;
907 964
908 /* free this only after the transfer */ 965 /* free this only after the transfer */
966 LOCK;
967 free_coro_mortal ();
968 UNLOCK;
909 coro_mortal = prev; 969 coro_mortal = prev;
910 970
971 ta->prev = SvSTATE (prev);
972 ta->next = SvSTATE (next);
973 ta->flags = TRANSFER_SAVE_ALL;
974}
975
976static void
977prepare_cede (struct transfer_args *ta)
978{
979 LOCK;
980 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
911 UNLOCK; 981 UNLOCK;
912 982
913 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), TRANSFER_SAVE_ALL); 983 prepare_schedule (ta);
914} 984}
915 985
916static int coro_cede_self; 986static void
987api_schedule (void)
988{
989 dTHX;
990 struct transfer_args ta;
991
992 prepare_schedule (&ta);
993 TRANSFER (ta);
994}
917 995
918static void 996static void
919api_cede (void) 997api_cede (void)
920{ 998{
921 dTHX; 999 dTHX;
922 SV *current = SvREFCNT_inc (SvRV (GvSV (coro_current))); 1000 struct transfer_args ta;
923 1001
924 LOCK; 1002 prepare_cede (&ta);
925 1003 TRANSFER (ta);
926 if (coro_cede_self)
927 {
928 AV *runqueue = coro_ready [PRIO_MAX - PRIO_MIN];
929 av_unshift (runqueue, 1);
930 av_store (runqueue, 0, current);
931 coro_nready++;
932 coro_cede_self = 0;
933 }
934 else
935 coro_enq (aTHX_ current);
936
937 UNLOCK;
938
939 api_schedule ();
940} 1004}
941 1005
942MODULE = Coro::State PACKAGE = Coro::State 1006MODULE = Coro::State PACKAGE = Coro::State
943 1007
944PROTOTYPES: DISABLE 1008PROTOTYPES: DISABLE
959 main_mainstack = PL_mainstack; 1023 main_mainstack = PL_mainstack;
960 1024
961 coroapi.ver = CORO_API_VERSION; 1025 coroapi.ver = CORO_API_VERSION;
962 coroapi.transfer = api_transfer; 1026 coroapi.transfer = api_transfer;
963 1027
964 Newz (0, main_stack, 1, coro_stack);
965 main_stack->idle_sp = (void *)-1;
966
967 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1028 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
968} 1029}
969 1030
970SV * 1031SV *
971new (char *klass, ...) 1032new (char *klass, ...)
980 1041
981 hv = newHV (); 1042 hv = newHV ();
982 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1043 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
983 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1044 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
984 1045
985 av_push (coro->args, newSVsv (RETVAL));
986 for (i = 1; i < items; i++) 1046 for (i = 1; i < items; i++)
987 av_push (coro->args, newSVsv (ST (i))); 1047 av_push (coro->args, newSVsv (ST (i)));
988 1048
989 coro->stack = main_stack;
990 /*coro->mainstack = 0; *//*actual work is done inside transfer */ 1049 /*coro->mainstack = 0; *//*actual work is done inside transfer */
991 /*coro->stack = 0;*/ 1050 /*coro->stack = 0;*/
992} 1051}
993 OUTPUT: 1052 OUTPUT:
994 RETVAL 1053 RETVAL
995 1054
996void 1055void
997transfer (prev, next, flags) 1056_set_stacklevel (...)
998 SV *prev 1057 ALIAS:
999 SV *next 1058 Coro::State::transfer = 1
1000 int flags 1059 Coro::schedule = 2
1060 Coro::cede = 3
1061 Coro::Cont::yield = 4
1001 CODE: 1062 CODE:
1002 PUTBACK;
1003 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
1004 SPAGAIN;
1005
1006int
1007prio (Coro::State coro, int newprio = 0)
1008 ALIAS:
1009 nice = 1
1010 CODE:
1011{ 1063{
1012 RETVAL = coro->prio; 1064 struct transfer_args ta;
1013 1065
1014 if (items > 1) 1066 switch (ix)
1015 { 1067 {
1068 case 0:
1069 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0)));
1070 ta.next = 0;
1071 ta.flags = TRANSFER_SET_STACKLEVEL;
1072 break;
1073
1074 case 1:
1016 if (ix) 1075 if (items != 3)
1017 newprio += coro->prio; 1076 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items);
1018 1077
1019 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1078 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1020 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1079 break;
1021 1080
1022 coro->prio = newprio; 1081 case 2:
1082 prepare_schedule (&ta);
1083 break;
1084
1085 case 3:
1086 prepare_cede (&ta);
1087 break;
1088
1089 case 4:
1090 {
1091 SV *yieldstack;
1092 SV *sv;
1093 AV *defav = GvAV (PL_defgv);
1094
1095 yieldstack = *hv_fetch (
1096 (HV *)SvRV (GvSV (coro_current)),
1097 "yieldstack", sizeof ("yieldstack") - 1,
1098 0
1099 );
1100
1101 /* set up @_ -- ugly */
1102 av_clear (defav);
1103 av_fill (defav, items - 1);
1104 while (items--)
1105 av_store (defav, items, SvREFCNT_inc (ST(items)));
1106
1107 sv = av_pop ((AV *)SvRV (yieldstack));
1108 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1109 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1110 ta.flags = 0;
1111 SvREFCNT_dec (sv);
1112 }
1113 break;
1114
1023 } 1115 }
1024}
1025 1116
1026void 1117 TRANSFER (ta);
1027_clear_idle_sp (Coro::State self) 1118}
1028 CODE:
1029 self->stack->idle_sp = 0;
1030 1119
1031void 1120void
1032_clone_state_from (SV *dst, SV *src) 1121_clone_state_from (SV *dst, SV *src)
1033 CODE: 1122 CODE:
1034{ 1123{
1045 int code 1134 int code
1046 PROTOTYPE: $ 1135 PROTOTYPE: $
1047 CODE: 1136 CODE:
1048 _exit (code); 1137 _exit (code);
1049 1138
1050MODULE = Coro::State PACKAGE = Coro::Cont
1051
1052void
1053yield (...)
1054 PROTOTYPE: @
1055 CODE:
1056{
1057 SV *yieldstack;
1058 SV *sv;
1059 AV *defav = GvAV (PL_defgv);
1060 struct coro *prev, *next;
1061
1062 yieldstack = *hv_fetch (
1063 (HV *)SvRV (GvSV (coro_current)),
1064 "yieldstack", sizeof ("yieldstack") - 1,
1065 0
1066 );
1067
1068 /* set up @_ -- ugly */
1069 av_clear (defav);
1070 av_fill (defav, items - 1);
1071 while (items--)
1072 av_store (defav, items, SvREFCNT_inc (ST(items)));
1073
1074 sv = av_pop ((AV *)SvRV (yieldstack));
1075 prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1076 next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1077 SvREFCNT_dec (sv);
1078
1079 coro_state_transfer (aTHX_ prev, next, 0);
1080}
1081
1082MODULE = Coro::State PACKAGE = Coro 1139MODULE = Coro::State PACKAGE = Coro
1083 1140
1084BOOT: 1141BOOT:
1085{ 1142{
1086 int i; 1143 int i;
1111 1168
1112 GCoroAPI = &coroapi; 1169 GCoroAPI = &coroapi;
1113 sv_setiv (sv, (IV)&coroapi); 1170 sv_setiv (sv, (IV)&coroapi);
1114 SvREADONLY_on (sv); 1171 SvREADONLY_on (sv);
1115 } 1172 }
1173}
1174
1175int
1176prio (Coro::State coro, int newprio = 0)
1177 ALIAS:
1178 nice = 1
1179 CODE:
1180{
1181 RETVAL = coro->prio;
1182
1183 if (items > 1)
1184 {
1185 if (ix)
1186 newprio += coro->prio;
1187
1188 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1189 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1190
1191 coro->prio = newprio;
1192 }
1116} 1193}
1117 1194
1118void 1195void
1119ready (SV *self) 1196ready (SV *self)
1120 PROTOTYPE: $ 1197 PROTOTYPE: $
1127 CODE: 1204 CODE:
1128 RETVAL = coro_nready; 1205 RETVAL = coro_nready;
1129 OUTPUT: 1206 OUTPUT:
1130 RETVAL 1207 RETVAL
1131 1208
1132void
1133schedule (...)
1134 PROTOTYPE:
1135 CODE:
1136 api_schedule ();
1137
1138void
1139_set_cede_self ()
1140 CODE:
1141 coro_cede_self = 1;
1142
1143void
1144cede (...)
1145 PROTOTYPE:
1146 CODE:
1147 api_cede ();
1148
1149MODULE = Coro::State PACKAGE = Coro::AIO 1209MODULE = Coro::State PACKAGE = Coro::AIO
1150 1210
1151SV * 1211SV *
1152_get_state () 1212_get_state ()
1153 CODE: 1213 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines