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.103 by root, Mon Nov 27 01:33:30 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,6,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) 496coro_destroy_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
537setup_coro (struct coro *coro) 543setup_coro (struct coro *coro)
538{ 544{
539 /* 545 /*
540 * emulate part of the perl startup here. 546 * emulate part of the perl startup here.
541 */ 547 */
542 dTHX;
543 dSP;
544 UNOP myop;
545 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE);
546 548
547 coro_init_stacks (aTHX); 549 coro_init_stacks ();
550
548 /*PL_curcop = 0;*/ 551 PL_curcop = 0;
549 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 552 PL_in_eval = 0;
553 PL_curpm = 0;
554
555 {
556 dSP;
557 LOGOP myop;
558
559 /* I have no idea why this is needed, but it is */
560 PUSHMARK (SP);
561
550 SvREFCNT_dec (GvAV (PL_defgv)); 562 SvREFCNT_dec (GvAV (PL_defgv));
551 GvAV (PL_defgv) = coro->args; coro->args = 0; 563 GvAV (PL_defgv) = coro->args; coro->args = 0;
552 564
553 SPAGAIN;
554
555 Zero (&myop, 1, UNOP); 565 Zero (&myop, 1, LOGOP);
556 myop.op_next = Nullop; 566 myop.op_next = Nullop;
557 myop.op_flags = OPf_WANT_VOID; 567 myop.op_flags = OPf_WANT_VOID;
558 568
559 PL_op = (OP *)&myop; 569 PL_op = (OP *)&myop;
560 570
561 PUSHMARK(SP); 571 PUSHMARK (SP);
562 XPUSHs (sub_init); 572 XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE));
563 PUTBACK; 573 PUTBACK;
564 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 574 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
565 SPAGAIN; 575 SPAGAIN;
566 576
567 ENTER; /* necessary e.g. for dounwind */ 577 ENTER; /* necessary e.g. for dounwind */
578 }
568} 579}
569 580
570static void 581static void
571transfer_tail () 582free_coro_mortal ()
572{ 583{
573 if (coro_mortal) 584 if (coro_mortal)
574 { 585 {
575 SvREFCNT_dec (coro_mortal); 586 SvREFCNT_dec (coro_mortal);
576 coro_mortal = 0; 587 coro_mortal = 0;
577 } 588 }
589}
578 590
591static void NOINLINE
592prepare_cctx (coro_stack *cctx)
593{
594 dSP;
595 LOGOP myop;
596
597 Zero (&myop, 1, LOGOP);
598 myop.op_next = PL_op;
599 myop.op_flags = OPf_WANT_VOID;
600
601 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx));
602
603 PUSHMARK (SP);
604 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE));
605 PUTBACK;
606 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
607 SPAGAIN;
608}
609
610static void
611coro_run (void *arg)
612{
613 /* coro_run is the alternative epilogue of transfer() */
579 UNLOCK; 614 UNLOCK;
580}
581 615
582static void
583coro_run (void *arg)
584{
585 /* 616 /*
586 * this is a _very_ stripped down perl interpreter ;) 617 * this is a _very_ stripped down perl interpreter ;)
587 */ 618 */
588 dTHX;
589
590 transfer_tail ();
591
592 PL_top_env = &PL_start_env; 619 PL_top_env = &PL_start_env;
593 PL_restartop = PL_op; 620 /* inject call to cctx_init */
621 prepare_cctx ((coro_stack *)arg);
622
594 /* somebody will hit me for both perl_run and PL_restart_top */ 623 /* somebody will hit me for both perl_run and PL_restartop */
595 perl_run (aTHX_ PERL_GET_CONTEXT); 624 perl_run (PL_curinterp);
596 625
626 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr);
597 abort (); 627 abort ();
598} 628}
599 629
600static coro_stack * 630static coro_stack *
601stack_new () 631stack_new ()
604 634
605 New (0, stack, 1, coro_stack); 635 New (0, stack, 1, coro_stack);
606 636
607#if HAVE_MMAP 637#if HAVE_MMAP
608 638
609 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ 639 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
640 /* 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); 641 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
611 642
612 if (stack->sptr == (void *)-1) 643 if (stack->sptr == (void *)-1)
613 { 644 {
614 fprintf (stderr, "FATAL: unable to mmap stack for coroutine\n"); 645 perror ("FATAL: unable to mmap stack for coroutine");
615 _exit (EXIT_FAILURE); 646 _exit (EXIT_FAILURE);
616 } 647 }
617 648
649# if STACKGUARD
618 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 650 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
651# endif
619 652
620#else 653#else
621 654
622 stack->ssize = STACKSIZE * (long)sizeof (long); 655 stack->ssize = STACKSIZE * (long)sizeof (long);
623 New (0, stack->sptr, STACKSIZE, long); 656 New (0, stack->sptr, STACKSIZE, long);
624 657
625 if (!stack->sptr) 658 if (!stack->sptr)
626 { 659 {
627 fprintf (stderr, "FATAL: unable to malloc stack for coroutine\n"); 660 perror (stderr, "FATAL: unable to malloc stack for coroutine");
628 _exit (EXIT_FAILURE); 661 _exit (EXIT_FAILURE);
629 } 662 }
630 663
631#endif 664#endif
632 665
633 coro_create (&stack->cctx, coro_run, 0, stack->sptr, stack->ssize); 666 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize);
634 667
635 return stack; 668 return stack;
636} 669}
637 670
638static void 671static void
639stack_free (coro_stack *stack) 672stack_free (coro_stack *stack)
640{ 673{
641 if (!stack || stack == main_stack) 674 if (!stack)
642 return; 675 return;
643 676
644#if HAVE_MMAP 677#if HAVE_MMAP
645 munmap (stack->sptr, stack->ssize); 678 munmap (stack->sptr, stack->ssize);
646#else 679#else
649 682
650 Safefree (stack); 683 Safefree (stack);
651} 684}
652 685
653static coro_stack *stack_first; 686static coro_stack *stack_first;
687static int cctx_count, cctx_idle;
654 688
655static coro_stack * 689static coro_stack *
656stack_get () 690stack_get ()
657{ 691{
658 coro_stack *stack; 692 coro_stack *stack;
659 693
660 if (stack_first) 694 if (stack_first)
661 { 695 {
696 --cctx_idle;
662 stack = stack_first; 697 stack = stack_first;
663 stack_first = stack->next; 698 stack_first = stack->next;
664 } 699 }
665 else 700 else
666 { 701 {
702 ++cctx_count;
667 stack = stack_new (); 703 stack = stack_new ();
668 PL_op = PL_op->op_next; 704 PL_op = PL_op->op_next;
669 } 705 }
670 706
671 return stack; 707 return stack;
672} 708}
673 709
674static void 710static void
675stack_put (coro_stack *stack) 711stack_put (coro_stack *stack)
676{ 712{
713 ++cctx_idle;
677 stack->next = stack_first; 714 stack->next = stack_first;
678 stack_first = stack; 715 stack_first = stack;
679} 716}
680 717
681/* never call directly, always through the coro_state_transfer global variable */ 718/* never call directly, always through the coro_state_transfer global variable */
682static void 719static void NOINLINE
683transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 720transfer (struct coro *prev, struct coro *next, int flags)
684{ 721{
685 dSTACKLEVEL; 722 dSTACKLEVEL;
686 723
687 /* sometimes transfer is only called to set idle_sp */ 724 /* sometimes transfer is only called to set idle_sp */
688 if (!prev->stack->idle_sp) 725 if (flags == TRANSFER_SET_STACKLEVEL)
689 prev->stack->idle_sp = stacklevel; 726 ((coro_stack *)prev)->idle_sp = STACKLEVEL;
690
691 LOCK;
692
693 if (prev != next) 727 else if (prev != next)
694 { 728 {
695 coro_stack *prev_stack = prev->stack; 729 coro_stack *prev__stack;
696 730
697 /* possibly "free" the stack */ 731 LOCK;
698 if (0 && prev_stack->idle_sp == stacklevel)
699 {
700 stack_put (prev_stack);
701 prev->stack = 0;
702 }
703 732
704 if (next->mainstack) 733 if (next->mainstack)
705 { 734 {
706 /* coroutine already started */ 735 /* coroutine already started */
707 SAVE (prev, flags); 736 SAVE (prev, flags);
712 /* need to start coroutine */ 741 /* need to start coroutine */
713 /* first get rid of the old state */ 742 /* first get rid of the old state */
714 SAVE (prev, -1); 743 SAVE (prev, -1);
715 /* setup coroutine call */ 744 /* setup coroutine call */
716 setup_coro (next); 745 setup_coro (next);
717 /* need to change stack from main_stack to real one */ 746 /* need a stack */
718 next->stack = 0; 747 next->stack = 0;
719 } 748 }
720 749
750 if (!prev->stack)
751 /* create a new empty context */
752 Newz (0, prev->stack, 1, coro_stack);
753
754 prev__stack = prev->stack;
755
756 /* possibly "free" the stack */
757 if (prev__stack->idle_sp == STACKLEVEL)
758 {
759 stack_put (prev__stack);
760 prev->stack = 0;
761 }
762
721 if (!next->stack) 763 if (!next->stack)
722 next->stack = stack_get (); 764 next->stack = stack_get ();
723 765
724 if (prev_stack != next->stack) 766 if (prev__stack != next->stack)
767 {
768 prev__stack->top_env = PL_top_env;
769 PL_top_env = next->stack->top_env;
725 coro_transfer (&prev_stack->cctx, &next->stack->cctx); 770 coro_transfer (&prev__stack->cctx, &next->stack->cctx);
771 }
772
773 free_coro_mortal ();
774
775 UNLOCK;
726 } 776 }
727
728 transfer_tail ();
729} 777}
730 778
731/* use this function pointer to call the above function */ 779struct transfer_args
732/* this is done to increase chances of the compiler not inlining the call */ 780{
733void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; 781 struct coro *prev, *next;
782 int flags;
783};
784
785#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
734 786
735static void 787static void
736coro_state_destroy (struct coro *coro) 788coro_state_destroy (struct coro *coro)
737{ 789{
738 if (coro->refcnt--) 790 if (coro->refcnt--)
740 792
741 if (coro->mainstack && coro->mainstack != main_mainstack) 793 if (coro->mainstack && coro->mainstack != main_mainstack)
742 { 794 {
743 struct coro temp; 795 struct coro temp;
744 796
745 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 797 SAVE ((&temp), TRANSFER_SAVE_ALL);
746 LOAD (aTHX_ coro); 798 LOAD (coro);
747 799
748 destroy_stacks (aTHX); 800 coro_destroy_stacks ();
749 801
750 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 802 LOAD ((&temp)); /* this will get rid of defsv etc.. */
751 803
752 coro->mainstack = 0; 804 coro->mainstack = 0;
753 } 805 }
756 SvREFCNT_dec (coro->args); 808 SvREFCNT_dec (coro->args);
757 Safefree (coro); 809 Safefree (coro);
758} 810}
759 811
760static int 812static int
761coro_state_clear (SV *sv, MAGIC *mg) 813coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
762{ 814{
763 struct coro *coro = (struct coro *)mg->mg_ptr; 815 struct coro *coro = (struct coro *)mg->mg_ptr;
764 mg->mg_ptr = 0; 816 mg->mg_ptr = 0;
765 817
766 coro_state_destroy (coro); 818 coro_state_destroy (coro);
767 819
768 return 0; 820 return 0;
769} 821}
770 822
771static int 823static int
772coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 824coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
773{ 825{
774 struct coro *coro = (struct coro *)mg->mg_ptr; 826 struct coro *coro = (struct coro *)mg->mg_ptr;
775 827
776 ++coro->refcnt; 828 ++coro->refcnt;
777 829
778 return 0; 830 return 0;
779} 831}
780 832
781static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 833static MGVTBL coro_state_vtbl = {
834 0, 0, 0, 0,
835 coro_state_clear,
836 0,
837#ifdef MGf_DUP
838 coro_state_dup,
839#else
840# define MGf_DUP 0
841#endif
842};
782 843
783static struct coro * 844static struct coro *
784SvSTATE (SV *coro) 845SvSTATE (SV *coro)
785{ 846{
786 HV *stash; 847 HV *stash;
801 assert (mg->mg_type == PERL_MAGIC_ext); 862 assert (mg->mg_type == PERL_MAGIC_ext);
802 return (struct coro *)mg->mg_ptr; 863 return (struct coro *)mg->mg_ptr;
803} 864}
804 865
805static void 866static void
867prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
868{
869 ta->prev = SvSTATE (prev);
870 ta->next = SvSTATE (next);
871 ta->flags = flags;
872}
873
874static void
806api_transfer (pTHX_ SV *prev, SV *next, int flags) 875api_transfer (SV *prev, SV *next, int flags)
807{ 876{
808 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 877 dTHX;
878 struct transfer_args ta;
879
880 prepare_transfer (&ta, prev, next, flags);
881 TRANSFER (ta);
809} 882}
810 883
811/** Coro ********************************************************************/ 884/** Coro ********************************************************************/
812 885
813#define PRIO_MAX 3 886#define PRIO_MAX 3
821static GV *coro_current, *coro_idle; 894static GV *coro_current, *coro_idle;
822static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 895static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
823static int coro_nready; 896static int coro_nready;
824 897
825static void 898static void
826coro_enq (pTHX_ SV *sv) 899coro_enq (SV *sv)
827{ 900{
828 int prio; 901 int prio;
829 902
830 if (SvTYPE (sv) != SVt_PVHV) 903 if (SvTYPE (sv) != SVt_PVHV)
831 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 904 croak ("Coro::ready tried to enqueue something that is not a coroutine");
835 av_push (coro_ready [prio - PRIO_MIN], sv); 908 av_push (coro_ready [prio - PRIO_MIN], sv);
836 coro_nready++; 909 coro_nready++;
837} 910}
838 911
839static SV * 912static SV *
840coro_deq (pTHX_ int min_prio) 913coro_deq (int min_prio)
841{ 914{
842 int prio = PRIO_MAX - PRIO_MIN; 915 int prio = PRIO_MAX - PRIO_MIN;
843 916
844 min_prio -= PRIO_MIN; 917 min_prio -= PRIO_MIN;
845 if (min_prio < 0) 918 if (min_prio < 0)
862 935
863 if (SvROK (coro)) 936 if (SvROK (coro))
864 coro = SvRV (coro); 937 coro = SvRV (coro);
865 938
866 LOCK; 939 LOCK;
867 coro_enq (aTHX_ SvREFCNT_inc (coro)); 940 coro_enq (SvREFCNT_inc (coro));
868 UNLOCK; 941 UNLOCK;
869} 942}
870 943
871static void 944static void
872api_schedule (void) 945prepare_schedule (struct transfer_args *ta)
873{ 946{
874 dTHX;
875
876 SV *prev, *next; 947 SV *current, *prev, *next;
948
877 SV *current = GvSV (coro_current); 949 current = GvSV (coro_current);
878 950
879 for (;;) 951 for (;;)
880 { 952 {
881 LOCK; 953 LOCK;
882
883 next = coro_deq (aTHX_ PRIO_MIN); 954 next = coro_deq (PRIO_MIN);
955 UNLOCK;
884 956
885 if (next) 957 if (next)
886 break; 958 break;
887
888 UNLOCK;
889 959
890 { 960 {
891 dSP; 961 dSP;
892 962
893 ENTER; 963 ENTER;
904 974
905 prev = SvRV (current); 975 prev = SvRV (current);
906 SvRV (current) = next; 976 SvRV (current) = next;
907 977
908 /* free this only after the transfer */ 978 /* free this only after the transfer */
979 LOCK;
980 free_coro_mortal ();
981 UNLOCK;
909 coro_mortal = prev; 982 coro_mortal = prev;
910 983
984 ta->prev = SvSTATE (prev);
985 ta->next = SvSTATE (next);
986 ta->flags = TRANSFER_SAVE_ALL;
987}
988
989static void
990prepare_cede (struct transfer_args *ta)
991{
992 LOCK;
993 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
911 UNLOCK; 994 UNLOCK;
912 995
913 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), TRANSFER_SAVE_ALL); 996 prepare_schedule (ta);
914} 997}
915 998
916static int coro_cede_self; 999static void
1000api_schedule (void)
1001{
1002 dTHX;
1003 struct transfer_args ta;
1004
1005 prepare_schedule (&ta);
1006 TRANSFER (ta);
1007}
917 1008
918static void 1009static void
919api_cede (void) 1010api_cede (void)
920{ 1011{
921 dTHX; 1012 dTHX;
922 SV *current = SvREFCNT_inc (SvRV (GvSV (coro_current))); 1013 struct transfer_args ta;
923 1014
924 LOCK; 1015 prepare_cede (&ta);
925 1016 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} 1017}
941 1018
942MODULE = Coro::State PACKAGE = Coro::State 1019MODULE = Coro::State PACKAGE = Coro::State
943 1020
944PROTOTYPES: DISABLE 1021PROTOTYPES: DISABLE
959 main_mainstack = PL_mainstack; 1036 main_mainstack = PL_mainstack;
960 1037
961 coroapi.ver = CORO_API_VERSION; 1038 coroapi.ver = CORO_API_VERSION;
962 coroapi.transfer = api_transfer; 1039 coroapi.transfer = api_transfer;
963 1040
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)); 1041 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
968} 1042}
969 1043
970SV * 1044SV *
971new (char *klass, ...) 1045new (char *klass, ...)
980 1054
981 hv = newHV (); 1055 hv = newHV ();
982 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1056 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)); 1057 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
984 1058
985 av_push (coro->args, newSVsv (RETVAL));
986 for (i = 1; i < items; i++) 1059 for (i = 1; i < items; i++)
987 av_push (coro->args, newSVsv (ST (i))); 1060 av_push (coro->args, newSVsv (ST (i)));
988 1061
989 coro->stack = main_stack;
990 /*coro->mainstack = 0; *//*actual work is done inside transfer */ 1062 /*coro->mainstack = 0; *//*actual work is done inside transfer */
991 /*coro->stack = 0;*/ 1063 /*coro->stack = 0;*/
992} 1064}
993 OUTPUT: 1065 OUTPUT:
994 RETVAL 1066 RETVAL
995 1067
996void 1068void
997transfer (prev, next, flags) 1069_set_stacklevel (...)
998 SV *prev 1070 ALIAS:
999 SV *next 1071 Coro::State::transfer = 1
1000 int flags 1072 Coro::schedule = 2
1073 Coro::cede = 3
1074 Coro::Cont::yield = 4
1001 CODE: 1075 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{ 1076{
1012 RETVAL = coro->prio; 1077 struct transfer_args ta;
1013 1078
1014 if (items > 1) 1079 switch (ix)
1015 { 1080 {
1081 case 0:
1082 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0)));
1083 ta.next = 0;
1084 ta.flags = TRANSFER_SET_STACKLEVEL;
1085 break;
1086
1087 case 1:
1016 if (ix) 1088 if (items != 3)
1017 newprio += coro->prio; 1089 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items);
1018 1090
1019 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1091 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1020 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1092 break;
1021 1093
1022 coro->prio = newprio; 1094 case 2:
1095 prepare_schedule (&ta);
1096 break;
1097
1098 case 3:
1099 prepare_cede (&ta);
1100 break;
1101
1102 case 4:
1103 {
1104 SV *yieldstack;
1105 SV *sv;
1106 AV *defav = GvAV (PL_defgv);
1107
1108 yieldstack = *hv_fetch (
1109 (HV *)SvRV (GvSV (coro_current)),
1110 "yieldstack", sizeof ("yieldstack") - 1,
1111 0
1112 );
1113
1114 /* set up @_ -- ugly */
1115 av_clear (defav);
1116 av_fill (defav, items - 1);
1117 while (items--)
1118 av_store (defav, items, SvREFCNT_inc (ST(items)));
1119
1120 sv = av_pop ((AV *)SvRV (yieldstack));
1121 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1122 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1123 ta.flags = 0;
1124 SvREFCNT_dec (sv);
1125 }
1126 break;
1127
1023 } 1128 }
1024}
1025 1129
1026void 1130 TRANSFER (ta);
1027_clear_idle_sp (Coro::State self) 1131}
1028 CODE:
1029 self->stack->idle_sp = 0;
1030 1132
1031void 1133void
1032_clone_state_from (SV *dst, SV *src) 1134_clone_state_from (SV *dst, SV *src)
1033 CODE: 1135 CODE:
1034{ 1136{
1045 int code 1147 int code
1046 PROTOTYPE: $ 1148 PROTOTYPE: $
1047 CODE: 1149 CODE:
1048 _exit (code); 1150 _exit (code);
1049 1151
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 1152MODULE = Coro::State PACKAGE = Coro
1083 1153
1084BOOT: 1154BOOT:
1085{ 1155{
1086 int i; 1156 int i;
1111 1181
1112 GCoroAPI = &coroapi; 1182 GCoroAPI = &coroapi;
1113 sv_setiv (sv, (IV)&coroapi); 1183 sv_setiv (sv, (IV)&coroapi);
1114 SvREADONLY_on (sv); 1184 SvREADONLY_on (sv);
1115 } 1185 }
1186}
1187
1188int
1189prio (Coro::State coro, int newprio = 0)
1190 ALIAS:
1191 nice = 1
1192 CODE:
1193{
1194 RETVAL = coro->prio;
1195
1196 if (items > 1)
1197 {
1198 if (ix)
1199 newprio += coro->prio;
1200
1201 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1202 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1203
1204 coro->prio = newprio;
1205 }
1116} 1206}
1117 1207
1118void 1208void
1119ready (SV *self) 1209ready (SV *self)
1120 PROTOTYPE: $ 1210 PROTOTYPE: $
1127 CODE: 1217 CODE:
1128 RETVAL = coro_nready; 1218 RETVAL = coro_nready;
1129 OUTPUT: 1219 OUTPUT:
1130 RETVAL 1220 RETVAL
1131 1221
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 1222MODULE = Coro::State PACKAGE = Coro::AIO
1150 1223
1151SV * 1224SV *
1152_get_state () 1225_get_state ()
1153 CODE: 1226 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines