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.105 by root, Mon Nov 27 02:01:33 2006 UTC vs.
Revision 1.141 by root, Mon Jan 22 18:45:17 2007 UTC

4#include "perl.h" 4#include "perl.h"
5#include "XSUB.h" 5#include "XSUB.h"
6 6
7#include "patchlevel.h" 7#include "patchlevel.h"
8 8
9#include <stdio.h>
10#include <errno.h>
11#include <assert.h>
12
13#ifdef HAVE_MMAP
14# include <unistd.h>
15# include <sys/mman.h>
16# ifndef MAP_ANONYMOUS
17# ifdef MAP_ANON
18# define MAP_ANONYMOUS MAP_ANON
19# else
20# undef HAVE_MMAP
21# endif
22# endif
23# include <limits.h>
24# ifndef PAGESIZE
25# define PAGESIZE pagesize
26# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
27static long pagesize;
28# else
29# define BOOT_PAGESIZE (void)0
30# endif
31#else
32# define PAGESIZE 0
33# define BOOT_PAGESIZE (void)0
34#endif
35
9#if USE_VALGRIND 36#if USE_VALGRIND
10# include <valgrind/valgrind.h> 37# include <valgrind/valgrind.h>
38# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end))
39#else
40# define REGISTER_STACK(cctx,start,end)
11#endif 41#endif
42
43/* the maximum number of idle cctx that will be pooled */
44#define MAX_IDLE_CCTX 8
12 45
13#define PERL_VERSION_ATLEAST(a,b,c) \ 46#define PERL_VERSION_ATLEAST(a,b,c) \
14 (PERL_REVISION > (a) \ 47 (PERL_REVISION > (a) \
15 || (PERL_REVISION == (a) \ 48 || (PERL_REVISION == (a) \
16 && (PERL_VERSION > (b) \ 49 && (PERL_VERSION > (b) \
35# ifndef IS_PADCONST 68# ifndef IS_PADCONST
36# define IS_PADCONST(v) 0 69# define IS_PADCONST(v) 0
37# endif 70# endif
38#endif 71#endif
39 72
40#include <stdio.h> 73/* 5.8.7 */
41#include <errno.h> 74#ifndef SvRV_set
75# define SvRV_set(s,v) SvRV(s) = (v)
76#endif
42 77
43#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 78#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
44# undef STACKGUARD 79# undef STACKGUARD
45#endif 80#endif
46 81
47#ifndef STACKGUARD 82#ifndef STACKGUARD
48# define STACKGUARD 0 83# define STACKGUARD 0
49#endif 84#endif
50 85
51#ifdef HAVE_MMAP 86/* prefer perl internal functions over our own? */
52# include <unistd.h> 87#ifndef PREFER_PERL_FUNCTIONS
53# include <sys/mman.h> 88# define PREFER_PERL_FUNCTIONS 0
54# ifndef MAP_ANONYMOUS
55# ifdef MAP_ANON
56# define MAP_ANONYMOUS MAP_ANON
57# else
58# undef HAVE_MMAP
59# endif
60# endif
61# include <limits.h>
62# ifndef PAGESIZE
63# define PAGESIZE pagesize
64# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
65static long pagesize;
66# else
67# define BOOT_PAGESIZE
68# endif
69#endif 89#endif
70 90
71/* The next macro should declare a variable stacklevel that contains and approximation 91/* The next macro should declare a variable stacklevel that contains and approximation
72 * to the current C stack pointer. Its property is that it changes with each call 92 * to the current C stack pointer. Its property is that it changes with each call
73 * and should be unique. */ 93 * and should be unique. */
76 96
77#define IN_DESTRUCT (PL_main_cv == Nullcv) 97#define IN_DESTRUCT (PL_main_cv == Nullcv)
78 98
79#if __GNUC__ >= 3 99#if __GNUC__ >= 3
80# define attribute(x) __attribute__(x) 100# define attribute(x) __attribute__(x)
101# define BARRIER __asm__ __volatile__ ("" : : : "memory")
81#else 102#else
82# define attribute(x) 103# define attribute(x)
104# define BARRIER
83#endif 105#endif
84 106
85#define NOINLINE attribute ((noinline)) 107#define NOINLINE attribute ((noinline))
86 108
87#include "CoroAPI.h" 109#include "CoroAPI.h"
88
89#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
90 110
91#ifdef USE_ITHREADS 111#ifdef USE_ITHREADS
92static perl_mutex coro_mutex; 112static perl_mutex coro_mutex;
93# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 113# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
94# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 114# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
95#else 115#else
96# define LOCK (void)0 116# define LOCK (void)0
97# define UNLOCK (void)0 117# define UNLOCK (void)0
98#endif 118#endif
99 119
120/* helper storage struct for Coro::AIO */
121struct io_state
122{
123 int errorno;
124 I32 laststype;
125 int laststatval;
126 Stat_t statcache;
127};
128
100static struct CoroAPI coroapi; 129static struct CoroAPI coroapi;
101static AV *main_mainstack; /* used to differentiate between $main and others */ 130static AV *main_mainstack; /* used to differentiate between $main and others */
102static HV *coro_state_stash, *coro_stash; 131static HV *coro_state_stash, *coro_stash;
103static SV *coro_mortal; /* will be freed after next transfer */ 132static SV *coro_mortal; /* will be freed after next transfer */
104 133
134static struct coro_cctx *cctx_first;
135static int cctx_count, cctx_idle;
136
105/* this is a structure representing a c-level coroutine */ 137/* this is a structure representing a c-level coroutine */
106typedef struct coro_stack { 138typedef struct coro_cctx {
107 struct coro_stack *next; 139 struct coro_cctx *next;
108 140
109 /* the stack */ 141 /* the stack */
110 void *sptr; 142 void *sptr;
111 long ssize; /* positive == mmap, otherwise malloc */ 143 ssize_t ssize; /* positive == mmap, otherwise malloc */
112 144
113 /* cpu state */ 145 /* cpu state */
114 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 146 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
147 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
115 JMPENV *top_env; 148 JMPENV *top_env;
116 coro_context cctx; 149 coro_context cctx;
117 150
151 int inuse;
152
118#if USE_VALGRIND 153#if USE_VALGRIND
119 int valgrind_id; 154 int valgrind_id;
120#endif 155#endif
121} coro_stack; 156} coro_cctx;
157
158enum {
159 CF_RUNNING = 0x0001, /* coroutine is running */
160 CF_READY = 0x0002, /* coroutine is ready */
161 CF_NEW = 0x0004, /* has never been switched to */
162 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
163};
122 164
123/* this is a structure representing a perl-level coroutine */ 165/* this is a structure representing a perl-level coroutine */
124struct coro { 166struct coro {
125 /* the c coroutine allocated to this perl coroutine, if any */ 167 /* the c coroutine allocated to this perl coroutine, if any */
126 coro_stack *stack; 168 coro_cctx *cctx;
127 169
128 /* data associated with this coroutine (initial args) */ 170 /* data associated with this coroutine (initial args) */
129 AV *args; 171 AV *args;
130 int refcnt; 172 int refcnt;
173 int save; /* CORO_SAVE flags */
174 int flags; /* CF_ flags */
131 175
132 /* optionally saved, might be zero */ 176 /* optionally saved, might be zero */
133 AV *defav; 177 AV *defav; /* @_ */
134 SV *defsv; 178 SV *defsv; /* $_ */
135 SV *errsv; 179 SV *errsv; /* $@ */
180 SV *irssv; /* $/ */
181 SV *irssv_sv; /* real $/ cache */
136 182
137 /* saved global state not related to stacks */ 183#define VAR(name,type) type name;
138 U8 dowarn; 184# include "state.h"
139 I32 in_eval; 185#undef VAR
140
141 /* the stacks and related info (callchain etc..) */
142 PERL_SI *curstackinfo;
143 AV *curstack;
144 AV *mainstack;
145 SV **stack_sp;
146 OP *op;
147 SV **curpad;
148 AV *comppad;
149 CV *compcv;
150 SV **stack_base;
151 SV **stack_max;
152 SV **tmps_stack;
153 I32 tmps_floor;
154 I32 tmps_ix;
155 I32 tmps_max;
156 I32 *markstack;
157 I32 *markstack_ptr;
158 I32 *markstack_max;
159 I32 *scopestack;
160 I32 scopestack_ix;
161 I32 scopestack_max;
162 ANY *savestack;
163 I32 savestack_ix;
164 I32 savestack_max;
165 OP **retstack;
166 I32 retstack_ix;
167 I32 retstack_max;
168 PMOP *curpm;
169 COP *curcop;
170 186
171 /* coro process data */ 187 /* coro process data */
172 int prio; 188 int prio;
173}; 189};
174 190
175typedef struct coro *Coro__State; 191typedef struct coro *Coro__State;
176typedef struct coro *Coro__State_or_hashref; 192typedef struct coro *Coro__State_or_hashref;
193
194/** Coro ********************************************************************/
195
196#define PRIO_MAX 3
197#define PRIO_HIGH 1
198#define PRIO_NORMAL 0
199#define PRIO_LOW -1
200#define PRIO_IDLE -3
201#define PRIO_MIN -4
202
203/* for Coro.pm */
204static SV *coro_current;
205static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
206static int coro_nready;
207
208/** lowlevel stuff **********************************************************/
177 209
178static AV * 210static AV *
179coro_clone_padlist (CV *cv) 211coro_clone_padlist (CV *cv)
180{ 212{
181 AV *padlist = CvPADLIST (cv); 213 AV *padlist = CvPADLIST (cv);
238 270
239#define PERL_MAGIC_coro PERL_MAGIC_ext 271#define PERL_MAGIC_coro PERL_MAGIC_ext
240 272
241static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 273static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
242 274
275#define CORO_MAGIC(cv) \
276 SvMAGIC (cv) \
277 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
278 ? SvMAGIC (cv) \
279 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
280 : 0
281
243/* the next two functions merely cache the padlists */ 282/* the next two functions merely cache the padlists */
244static void 283static void
245get_padlist (CV *cv) 284get_padlist (CV *cv)
246{ 285{
247 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 286 MAGIC *mg = CORO_MAGIC (cv);
287 AV *av;
248 288
249 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 289 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
250 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 290 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
251 else 291 else
252 { 292 {
253#if 0 293#if PREFER_PERL_FUNCTIONS
254 /* this is probably cleaner, but also slower? */ 294 /* this is probably cleaner, but also slower? */
255 CV *cp = Perl_cv_clone (cv); 295 CV *cp = Perl_cv_clone (cv);
256 CvPADLIST (cv) = CvPADLIST (cp); 296 CvPADLIST (cv) = CvPADLIST (cp);
257 CvPADLIST (cp) = 0; 297 CvPADLIST (cp) = 0;
258 SvREFCNT_dec (cp); 298 SvREFCNT_dec (cp);
263} 303}
264 304
265static void 305static void
266put_padlist (CV *cv) 306put_padlist (CV *cv)
267{ 307{
268 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 308 MAGIC *mg = CORO_MAGIC (cv);
309 AV *av;
269 310
270 if (!mg) 311 if (!mg)
271 { 312 {
272 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 313 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
273 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 314 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
274 mg->mg_virtual = &vtbl_coro; 315 mg->mg_virtual = &vtbl_coro;
275 mg->mg_obj = (SV *)newAV (); 316 mg->mg_obj = (SV *)newAV ();
276 } 317 }
277 318
278 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); 319 av = (AV *)mg->mg_obj;
320
321 if (AvFILLp (av) >= AvMAX (av))
322 av_extend (av, AvMAX (av) + 1);
323
324 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
279} 325}
326
327/** load & save, init *******************************************************/
280 328
281#define SB do { 329#define SB do {
282#define SE } while (0) 330#define SE } while (0)
283 331
284#define LOAD(state) load_state((state));
285#define SAVE(state,flags) save_state((state),(flags));
286
287#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 332#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
288 333
289static void 334static void
290load_state(Coro__State c) 335load_perl (Coro__State c)
291{ 336{
292 PL_dowarn = c->dowarn; 337#define VAR(name,type) PL_ ## name = c->name;
293 PL_in_eval = c->in_eval; 338# include "state.h"
294 339#undef VAR
295 PL_curstackinfo = c->curstackinfo;
296 PL_curstack = c->curstack;
297 PL_mainstack = c->mainstack;
298 PL_stack_sp = c->stack_sp;
299 PL_op = c->op;
300 PL_curpad = c->curpad;
301 PL_comppad = c->comppad;
302 PL_compcv = c->compcv;
303 PL_stack_base = c->stack_base;
304 PL_stack_max = c->stack_max;
305 PL_tmps_stack = c->tmps_stack;
306 PL_tmps_floor = c->tmps_floor;
307 PL_tmps_ix = c->tmps_ix;
308 PL_tmps_max = c->tmps_max;
309 PL_markstack = c->markstack;
310 PL_markstack_ptr = c->markstack_ptr;
311 PL_markstack_max = c->markstack_max;
312 PL_scopestack = c->scopestack;
313 PL_scopestack_ix = c->scopestack_ix;
314 PL_scopestack_max = c->scopestack_max;
315 PL_savestack = c->savestack;
316 PL_savestack_ix = c->savestack_ix;
317 PL_savestack_max = c->savestack_max;
318#if !PERL_VERSION_ATLEAST (5,9,0)
319 PL_retstack = c->retstack;
320 PL_retstack_ix = c->retstack_ix;
321 PL_retstack_max = c->retstack_max;
322#endif
323 PL_curpm = c->curpm;
324 PL_curcop = c->curcop;
325 340
326 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 341 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
327 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 342 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
328 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 343 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
344 if (c->irssv)
345 {
346 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
347 SvREFCNT_dec (c->irssv);
348 else
349 {
350 REPLACE_SV (PL_rs, c->irssv);
351 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
352 sv_setsv (c->irssv_sv, PL_rs);
353 }
354 }
329 355
330 { 356 {
331 dSP; 357 dSP;
332 CV *cv; 358 CV *cv;
333 359
334 /* now do the ugly restore mess */ 360 /* now do the ugly restore mess */
335 while ((cv = (CV *)POPs)) 361 while ((cv = (CV *)POPs))
336 { 362 {
337 AV *padlist = (AV *)POPs;
338
339 if (padlist)
340 {
341 put_padlist (cv); /* mark this padlist as available */ 363 put_padlist (cv); /* mark this padlist as available */
342 CvPADLIST(cv) = padlist; 364 CvDEPTH (cv) = PTR2IV (POPs);
343 } 365 CvPADLIST (cv) = (AV *)POPs;
344
345 ++CvDEPTH(cv);
346 } 366 }
347 367
348 PUTBACK; 368 PUTBACK;
349 } 369 }
350} 370}
351 371
352static void 372static void
353save_state(Coro__State c, int flags) 373save_perl (Coro__State c)
354{ 374{
355 { 375 {
356 dSP; 376 dSP;
357 I32 cxix = cxstack_ix; 377 I32 cxix = cxstack_ix;
358 PERL_CONTEXT *ccstk = cxstack; 378 PERL_CONTEXT *ccstk = cxstack;
361 /* 381 /*
362 * the worst thing you can imagine happens first - we have to save 382 * the worst thing you can imagine happens first - we have to save
363 * (and reinitialize) all cv's in the whole callchain :( 383 * (and reinitialize) all cv's in the whole callchain :(
364 */ 384 */
365 385
386 EXTEND (SP, 3 + 1);
366 PUSHs (Nullsv); 387 PUSHs (Nullsv);
367 /* this loop was inspired by pp_caller */ 388 /* this loop was inspired by pp_caller */
368 for (;;) 389 for (;;)
369 { 390 {
370 while (cxix >= 0) 391 while (cxix >= 0)
371 { 392 {
372 PERL_CONTEXT *cx = &ccstk[cxix--]; 393 PERL_CONTEXT *cx = &ccstk[cxix--];
373 394
374 if (CxTYPE(cx) == CXt_SUB) 395 if (CxTYPE (cx) == CXt_SUB)
375 { 396 {
376 CV *cv = cx->blk_sub.cv; 397 CV *cv = cx->blk_sub.cv;
398
377 if (CvDEPTH(cv)) 399 if (CvDEPTH (cv))
378 { 400 {
379 EXTEND (SP, CvDEPTH(cv)*2); 401 EXTEND (SP, 3);
380
381 while (--CvDEPTH(cv))
382 {
383 /* this tells the restore code to increment CvDEPTH */
384 PUSHs (Nullsv);
385 PUSHs ((SV *)cv);
386 }
387
388 PUSHs ((SV *)CvPADLIST(cv)); 402 PUSHs ((SV *)CvPADLIST (cv));
403 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
389 PUSHs ((SV *)cv); 404 PUSHs ((SV *)cv);
390 405
406 CvDEPTH (cv) = 0;
391 get_padlist (cv); 407 get_padlist (cv);
392 } 408 }
393 } 409 }
394#ifdef CXt_FORMAT
395 else if (CxTYPE(cx) == CXt_FORMAT)
396 {
397 /* I never used formats, so how should I know how these are implemented? */
398 /* my bold guess is as a simple, plain sub... */
399 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
400 }
401#endif
402 } 410 }
403 411
404 if (top_si->si_type == PERLSI_MAIN) 412 if (top_si->si_type == PERLSI_MAIN)
405 break; 413 break;
406 414
410 } 418 }
411 419
412 PUTBACK; 420 PUTBACK;
413 } 421 }
414 422
415 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 423 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
416 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 424 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
417 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 425 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
426 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
418 427
419 c->dowarn = PL_dowarn; 428#define VAR(name,type)c->name = PL_ ## name;
420 c->in_eval = PL_in_eval; 429# include "state.h"
421 430#undef VAR
422 c->curstackinfo = PL_curstackinfo;
423 c->curstack = PL_curstack;
424 c->mainstack = PL_mainstack;
425 c->stack_sp = PL_stack_sp;
426 c->op = PL_op;
427 c->curpad = PL_curpad;
428 c->comppad = PL_comppad;
429 c->compcv = PL_compcv;
430 c->stack_base = PL_stack_base;
431 c->stack_max = PL_stack_max;
432 c->tmps_stack = PL_tmps_stack;
433 c->tmps_floor = PL_tmps_floor;
434 c->tmps_ix = PL_tmps_ix;
435 c->tmps_max = PL_tmps_max;
436 c->markstack = PL_markstack;
437 c->markstack_ptr = PL_markstack_ptr;
438 c->markstack_max = PL_markstack_max;
439 c->scopestack = PL_scopestack;
440 c->scopestack_ix = PL_scopestack_ix;
441 c->scopestack_max = PL_scopestack_max;
442 c->savestack = PL_savestack;
443 c->savestack_ix = PL_savestack_ix;
444 c->savestack_max = PL_savestack_max;
445#if !PERL_VERSION_ATLEAST (5,9,0)
446 c->retstack = PL_retstack;
447 c->retstack_ix = PL_retstack_ix;
448 c->retstack_max = PL_retstack_max;
449#endif
450 c->curpm = PL_curpm;
451 c->curcop = PL_curcop;
452} 431}
453 432
454/* 433/*
455 * allocate various perl stacks. This is an exact copy 434 * allocate various perl stacks. This is an exact copy
456 * of perl.c:init_stacks, except that it uses less memory 435 * of perl.c:init_stacks, except that it uses less memory
457 * on the (sometimes correct) assumption that coroutines do 436 * on the (sometimes correct) assumption that coroutines do
458 * not usually need a lot of stackspace. 437 * not usually need a lot of stackspace.
459 */ 438 */
439#if PREFER_PERL_FUNCTIONS
440# define coro_init_stacks init_stacks
441#else
460static void 442static void
461coro_init_stacks () 443coro_init_stacks ()
462{ 444{
463 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 445 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
464 PL_curstackinfo->si_type = PERLSI_MAIN; 446 PL_curstackinfo->si_type = PERLSI_MAIN;
465 PL_curstack = PL_curstackinfo->si_stack; 447 PL_curstack = PL_curstackinfo->si_stack;
466 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 448 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
467 449
468 PL_stack_base = AvARRAY(PL_curstack); 450 PL_stack_base = AvARRAY(PL_curstack);
469 PL_stack_sp = PL_stack_base; 451 PL_stack_sp = PL_stack_base;
470 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 452 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
471 453
472 New(50,PL_tmps_stack,96,SV*); 454 New(50,PL_tmps_stack,128,SV*);
473 PL_tmps_floor = -1; 455 PL_tmps_floor = -1;
474 PL_tmps_ix = -1; 456 PL_tmps_ix = -1;
475 PL_tmps_max = 96; 457 PL_tmps_max = 128;
476 458
477 New(54,PL_markstack,16,I32); 459 New(54,PL_markstack,32,I32);
478 PL_markstack_ptr = PL_markstack; 460 PL_markstack_ptr = PL_markstack;
479 PL_markstack_max = PL_markstack + 16; 461 PL_markstack_max = PL_markstack + 32;
480 462
481#ifdef SET_MARK_OFFSET 463#ifdef SET_MARK_OFFSET
482 SET_MARK_OFFSET; 464 SET_MARK_OFFSET;
483#endif 465#endif
484 466
485 New(54,PL_scopestack,16,I32); 467 New(54,PL_scopestack,32,I32);
486 PL_scopestack_ix = 0; 468 PL_scopestack_ix = 0;
487 PL_scopestack_max = 16; 469 PL_scopestack_max = 32;
488 470
489 New(54,PL_savestack,96,ANY); 471 New(54,PL_savestack,64,ANY);
490 PL_savestack_ix = 0; 472 PL_savestack_ix = 0;
491 PL_savestack_max = 96; 473 PL_savestack_max = 64;
492 474
493#if !PERL_VERSION_ATLEAST (5,9,0) 475#if !PERL_VERSION_ATLEAST (5,9,0)
494 New(54,PL_retstack,8,OP*); 476 New(54,PL_retstack,16,OP*);
495 PL_retstack_ix = 0; 477 PL_retstack_ix = 0;
496 PL_retstack_max = 8; 478 PL_retstack_max = 16;
497#endif 479#endif
498} 480}
481#endif
499 482
500/* 483/*
501 * destroy the stacks, the callchain etc... 484 * destroy the stacks, the callchain etc...
502 */ 485 */
503static void 486static void
504coro_destroy_stacks() 487coro_destroy_stacks ()
505{ 488{
506 if (!IN_DESTRUCT) 489 if (!IN_DESTRUCT)
507 { 490 {
508 /* is this ugly, I ask? */ 491 /* restore all saved variables and stuff */
509 LEAVE_SCOPE (0); 492 LEAVE_SCOPE (0);
493 assert (PL_tmps_floor == -1);
510 494
511 /* sure it is, but more important: is it correct?? :/ */ 495 /* free all temporaries */
512 FREETMPS; 496 FREETMPS;
497 assert (PL_tmps_ix == -1);
513 498
499 /* unwind all extra stacks */
514 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ 500 POPSTACK_TO (PL_mainstack);
501
502 /* unwind main stack */
503 dounwind (-1);
515 } 504 }
516 505
517 while (PL_curstackinfo->si_next) 506 while (PL_curstackinfo->si_next)
518 PL_curstackinfo = PL_curstackinfo->si_next; 507 PL_curstackinfo = PL_curstackinfo->si_next;
519 508
520 while (PL_curstackinfo) 509 while (PL_curstackinfo)
521 { 510 {
522 PERL_SI *p = PL_curstackinfo->si_prev; 511 PERL_SI *p = PL_curstackinfo->si_prev;
523 512
524 { /*D*//*remove*/
525 dSP;
526 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
527 PUTBACK; /* possibly superfluous */
528 }
529
530 if (!IN_DESTRUCT) 513 if (!IN_DESTRUCT)
531 {
532 dounwind (-1);/*D*//*remove*/
533 SvREFCNT_dec (PL_curstackinfo->si_stack); 514 SvREFCNT_dec (PL_curstackinfo->si_stack);
534 }
535 515
536 Safefree (PL_curstackinfo->si_cxstack); 516 Safefree (PL_curstackinfo->si_cxstack);
537 Safefree (PL_curstackinfo); 517 Safefree (PL_curstackinfo);
538 PL_curstackinfo = p; 518 PL_curstackinfo = p;
539 } 519 }
545#if !PERL_VERSION_ATLEAST (5,9,0) 525#if !PERL_VERSION_ATLEAST (5,9,0)
546 Safefree (PL_retstack); 526 Safefree (PL_retstack);
547#endif 527#endif
548} 528}
549 529
530/** coroutine stack handling ************************************************/
531
550static void 532static void
551setup_coro (struct coro *coro) 533setup_coro (struct coro *coro)
552{ 534{
553 /* 535 /*
554 * emulate part of the perl startup here. 536 * emulate part of the perl startup here.
555 */ 537 */
556 538
557 coro_init_stacks (); 539 coro_init_stacks ();
558 540
559 PL_curcop = 0; 541 PL_curcop = &PL_compiling;
560 PL_in_eval = 0; 542 PL_in_eval = EVAL_NULL;
561 PL_curpm = 0; 543 PL_curpm = 0;
544 PL_localizing = 0;
545 PL_dirty = 0;
546 PL_restartop = 0;
562 547
563 { 548 {
564 dSP; 549 dSP;
565 LOGOP myop; 550 LOGOP myop;
566 551
567 /* I have no idea why this is needed, but it is */
568 PUSHMARK (SP);
569
570 SvREFCNT_dec (GvAV (PL_defgv)); 552 SvREFCNT_dec (GvAV (PL_defgv));
571 GvAV (PL_defgv) = coro->args; coro->args = 0; 553 GvAV (PL_defgv) = coro->args; coro->args = 0;
572 554
573 Zero (&myop, 1, LOGOP); 555 Zero (&myop, 1, LOGOP);
574 myop.op_next = Nullop; 556 myop.op_next = Nullop;
575 myop.op_flags = OPf_WANT_VOID; 557 myop.op_flags = OPf_WANT_VOID;
576 558
559 PUSHMARK (SP);
560 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
561 PUTBACK;
577 PL_op = (OP *)&myop; 562 PL_op = (OP *)&myop;
578
579 PUSHMARK (SP);
580 XPUSHs ((SV *)get_cv ("Coro::State::coro_init", FALSE));
581 PUTBACK;
582 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 563 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
583 SPAGAIN; 564 SPAGAIN;
584
585 ENTER; /* necessary e.g. for dounwind */
586 } 565 }
566
567 ENTER; /* necessary e.g. for dounwind */
587} 568}
588 569
589static void 570static void
590free_coro_mortal () 571free_coro_mortal ()
591{ 572{
594 SvREFCNT_dec (coro_mortal); 575 SvREFCNT_dec (coro_mortal);
595 coro_mortal = 0; 576 coro_mortal = 0;
596 } 577 }
597} 578}
598 579
580/* inject a fake call to Coro::State::_cctx_init into the execution */
599static void NOINLINE 581static void NOINLINE
600prepare_cctx (coro_stack *cctx) 582prepare_cctx (coro_cctx *cctx)
601{ 583{
602 dSP; 584 dSP;
603 LOGOP myop; 585 LOGOP myop;
604 586
605 Zero (&myop, 1, LOGOP); 587 Zero (&myop, 1, LOGOP);
606 myop.op_next = PL_op; 588 myop.op_next = PL_op;
607 myop.op_flags = OPf_WANT_VOID; 589 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
608
609 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV (cctx));
610 590
611 PUSHMARK (SP); 591 PUSHMARK (SP);
592 EXTEND (SP, 2);
593 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx))));
612 XPUSHs ((SV *)get_cv ("Coro::State::cctx_init", FALSE)); 594 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
613 PUTBACK; 595 PUTBACK;
596 PL_op = (OP *)&myop;
614 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 597 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
615 SPAGAIN; 598 SPAGAIN;
616} 599}
617 600
618static void 601static void
619coro_run (void *arg) 602coro_run (void *arg)
620{ 603{
621 /* coro_run is the alternative epilogue of transfer() */ 604 /* coro_run is the alternative tail of transfer(), so unlock here. */
622 UNLOCK; 605 UNLOCK;
623 606
624 /* 607 /*
625 * this is a _very_ stripped down perl interpreter ;) 608 * this is a _very_ stripped down perl interpreter ;)
626 */ 609 */
627 PL_top_env = &PL_start_env; 610 PL_top_env = &PL_start_env;
611
628 /* inject call to cctx_init */ 612 /* inject call to cctx_init */
629 prepare_cctx ((coro_stack *)arg); 613 prepare_cctx ((coro_cctx *)arg);
630 614
631 /* somebody will hit me for both perl_run and PL_restartop */ 615 /* somebody will hit me for both perl_run and PL_restartop */
616 PL_restartop = PL_op;
632 perl_run (PL_curinterp); 617 perl_run (PL_curinterp);
633 618
634 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 619 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
635 abort (); 620 abort ();
636} 621}
637 622
638static coro_stack * 623static coro_cctx *
639stack_new () 624cctx_new ()
640{ 625{
641 coro_stack *stack; 626 coro_cctx *cctx;
642 627
628 ++cctx_count;
629
643 New (0, stack, 1, coro_stack); 630 Newz (0, cctx, 1, coro_cctx);
644 631
645#if HAVE_MMAP 632#if HAVE_MMAP
646 633
647 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 634 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
648 /* mmap suppsedly does allocate-on-write for us */ 635 /* mmap supposedly does allocate-on-write for us */
649 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 636 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
650 637
651 if (stack->sptr == (void *)-1) 638 if (cctx->sptr != (void *)-1)
652 {
653 perror ("FATAL: unable to mmap stack for coroutine");
654 _exit (EXIT_FAILURE);
655 } 639 {
656
657# if STACKGUARD 640# if STACKGUARD
658 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 641 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
659# endif 642# endif
643 REGISTER_STACK (
644 cctx,
645 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
646 cctx->ssize + (char *)cctx->sptr
647 );
660 648
661#else 649 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
662 650 }
651 else
652#endif
653 {
663 stack->ssize = STACKSIZE * (long)sizeof (long); 654 cctx->ssize = -STACKSIZE * (long)sizeof (long);
664 New (0, stack->sptr, STACKSIZE, long); 655 New (0, cctx->sptr, STACKSIZE, long);
665 656
666 if (!stack->sptr) 657 if (!cctx->sptr)
667 { 658 {
668 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 659 perror ("FATAL: unable to allocate stack for coroutine");
669 _exit (EXIT_FAILURE); 660 _exit (EXIT_FAILURE);
670 } 661 }
671 662
672#endif 663 REGISTER_STACK (
664 cctx,
665 (char *)cctx->sptr,
666 (char *)cctx->sptr - cctx->ssize
667 );
668
669 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize);
670 }
671
672 return cctx;
673}
674
675static void
676cctx_destroy (coro_cctx *cctx)
677{
678 if (!cctx)
679 return;
680
681 --cctx_count;
673 682
674#if USE_VALGRIND 683#if USE_VALGRIND
675 stack->valgrind_id = VALGRIND_STACK_REGISTER (
676 STACKGUARD * PAGESIZE + (char *)stack->sptr,
677 stack->ssize + (char *)stack->sptr
678 );
679#endif
680
681 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize);
682
683 return stack;
684}
685
686static void
687stack_free (coro_stack *stack)
688{
689 if (!stack)
690 return;
691
692#if USE_VALGRIND
693 VALGRIND_STACK_DEREGISTER (stack->valgrind_id); 684 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
694#endif 685#endif
695 686
696#if HAVE_MMAP 687#if HAVE_MMAP
688 if (cctx->ssize > 0)
697 munmap (stack->sptr, stack->ssize); 689 munmap (cctx->sptr, cctx->ssize);
698#else 690 else
691#endif
699 Safefree (stack->sptr); 692 Safefree (cctx->sptr);
700#endif
701 693
702 Safefree (stack); 694 Safefree (cctx);
703} 695}
704 696
705static coro_stack *stack_first;
706static int cctx_count, cctx_idle;
707
708static coro_stack * 697static coro_cctx *
709stack_get () 698cctx_get ()
710{ 699{
711 coro_stack *stack; 700 coro_cctx *cctx;
712 701
713 if (stack_first) 702 if (cctx_first)
714 { 703 {
704 cctx = cctx_first;
705 cctx_first = cctx->next;
715 --cctx_idle; 706 --cctx_idle;
716 stack = stack_first;
717 stack_first = stack->next;
718 } 707 }
719 else 708 else
720 { 709 {
721 ++cctx_count; 710 cctx = cctx_new ();
722 stack = stack_new ();
723 PL_op = PL_op->op_next; 711 PL_op = PL_op->op_next;
724 } 712 }
725 713
726 return stack; 714 return cctx;
727} 715}
728 716
729static void 717static void
730stack_put (coro_stack *stack) 718cctx_put (coro_cctx *cctx)
731{ 719{
720 /* free another cctx if overlimit */
721 if (cctx_idle >= MAX_IDLE_CCTX)
722 {
723 coro_cctx *first = cctx_first;
724 cctx_first = first->next;
725 --cctx_idle;
726
727 assert (!first->inuse);
728 cctx_destroy (first);
729 }
730
732 ++cctx_idle; 731 ++cctx_idle;
733 stack->next = stack_first; 732 cctx->next = cctx_first;
734 stack_first = stack; 733 cctx_first = cctx;
735} 734}
735
736/** coroutine switching *****************************************************/
736 737
737/* never call directly, always through the coro_state_transfer global variable */ 738/* never call directly, always through the coro_state_transfer global variable */
738static void NOINLINE 739static void NOINLINE
739transfer (struct coro *prev, struct coro *next, int flags) 740transfer (struct coro *prev, struct coro *next)
740{ 741{
741 dSTACKLEVEL; 742 dSTACKLEVEL;
742 743
743 /* sometimes transfer is only called to set idle_sp */ 744 /* sometimes transfer is only called to set idle_sp */
744 if (flags == TRANSFER_SET_STACKLEVEL) 745 if (!next)
746 {
745 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 747 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
748 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
749 }
746 else if (prev != next) 750 else if (prev != next)
747 { 751 {
748 coro_stack *prev__stack; 752 coro_cctx *prev__cctx;
753
754 if (prev->flags & CF_NEW)
755 {
756 /* create a new empty context */
757 Newz (0, prev->cctx, 1, coro_cctx);
758 prev->cctx->inuse = 1;
759 prev->flags &= ~CF_NEW;
760 prev->flags |= CF_RUNNING;
761 }
762
763 /*TODO: must not croak here */
764 if (!prev->flags & CF_RUNNING)
765 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
766
767 if (next->flags & CF_RUNNING)
768 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
769
770 if (next->flags & CF_DESTROYED)
771 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
772
773 prev->flags &= ~CF_RUNNING;
774 next->flags |= CF_RUNNING;
749 775
750 LOCK; 776 LOCK;
751 777
752 if (next->mainstack) 778 if (next->flags & CF_NEW)
753 { 779 {
754 /* coroutine already started */ 780 /* need to start coroutine */
755 SAVE (prev, flags); 781 next->flags &= ~CF_NEW;
756 LOAD (next); 782 /* first get rid of the old state */
783 save_perl (prev);
784 /* setup coroutine call */
785 setup_coro (next);
786 /* need a new stack */
787 assert (!next->cctx);
757 } 788 }
758 else 789 else
759 { 790 {
760 /* need to start coroutine */ 791 /* coroutine already started */
761 /* first get rid of the old state */ 792 save_perl (prev);
762 SAVE (prev, -1); 793 load_perl (next);
763 /* setup coroutine call */
764 setup_coro (next);
765 /* need a stack */
766 next->stack = 0;
767 } 794 }
768 795
769 if (!prev->stack)
770 /* create a new empty context */
771 Newz (0, prev->stack, 1, coro_stack);
772
773 prev__stack = prev->stack; 796 prev__cctx = prev->cctx;
774 797
775 /* possibly "free" the stack */ 798 /* possibly "free" the cctx */
776 if (prev__stack->idle_sp == STACKLEVEL) 799 if (prev__cctx->idle_sp == STACKLEVEL)
777 { 800 {
778 stack_put (prev__stack); 801 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
802 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
803
779 prev->stack = 0; 804 prev->cctx = 0;
805
806 cctx_put (prev__cctx);
807 prev__cctx->inuse = 0;
780 } 808 }
781 809
782 if (!next->stack) 810 if (!next->cctx)
783 next->stack = stack_get ();
784
785 if (prev__stack != next->stack)
786 { 811 {
787 prev__stack->top_env = PL_top_env; 812 next->cctx = cctx_get ();
788 PL_top_env = next->stack->top_env; 813 assert (!next->cctx->inuse);
789 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 814 next->cctx->inuse = 1;
790 } 815 }
791 816
817 if (prev__cctx != next->cctx)
818 {
819 prev__cctx->top_env = PL_top_env;
820 PL_top_env = next->cctx->top_env;
821 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
822 }
823
792 free_coro_mortal (); 824 free_coro_mortal ();
793
794 UNLOCK; 825 UNLOCK;
795 } 826 }
796} 827}
797 828
798struct transfer_args 829struct transfer_args
799{ 830{
800 struct coro *prev, *next; 831 struct coro *prev, *next;
801 int flags;
802}; 832};
803 833
804#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) 834#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
805 835
836/** high level stuff ********************************************************/
837
806static void 838static int
807coro_state_destroy (struct coro *coro) 839coro_state_destroy (struct coro *coro)
808{ 840{
809 if (coro->refcnt--) 841 if (coro->flags & CF_DESTROYED)
810 return; 842 return 0;
843
844 coro->flags |= CF_DESTROYED;
845
846 if (coro->flags & CF_READY)
847 {
848 /* reduce nready, as destroying a ready coro effectively unreadies it */
849 /* alternative: look through all ready queues and remove the coro */
850 LOCK;
851 --coro_nready;
852 UNLOCK;
853 }
854 else
855 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
811 856
812 if (coro->mainstack && coro->mainstack != main_mainstack) 857 if (coro->mainstack && coro->mainstack != main_mainstack)
813 { 858 {
814 struct coro temp; 859 struct coro temp;
815 860
816 SAVE ((&temp), TRANSFER_SAVE_ALL); 861 assert (!(coro->flags & CF_RUNNING));
817 LOAD (coro); 862
863 Zero (&temp, 1, struct coro);
864 temp.save = CORO_SAVE_ALL;
865
866 if (coro->flags & CF_RUNNING)
867 croak ("FATAL: tried to destroy currently running coroutine");
868
869 save_perl (&temp);
870 load_perl (coro);
818 871
819 coro_destroy_stacks (); 872 coro_destroy_stacks ();
820 873
821 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 874 load_perl (&temp); /* this will get rid of defsv etc.. */
822 875
823 coro->mainstack = 0; 876 coro->mainstack = 0;
824 } 877 }
825 878
826 stack_free (coro->stack); 879 cctx_destroy (coro->cctx);
827 SvREFCNT_dec (coro->args); 880 SvREFCNT_dec (coro->args);
828 Safefree (coro); 881
882 return 1;
829} 883}
830 884
831static int 885static int
832coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 886coro_state_free (pTHX_ SV *sv, MAGIC *mg)
833{ 887{
834 struct coro *coro = (struct coro *)mg->mg_ptr; 888 struct coro *coro = (struct coro *)mg->mg_ptr;
835 mg->mg_ptr = 0; 889 mg->mg_ptr = 0;
836 890
891 if (--coro->refcnt < 0)
892 {
837 coro_state_destroy (coro); 893 coro_state_destroy (coro);
894 Safefree (coro);
895 }
838 896
839 return 0; 897 return 0;
840} 898}
841 899
842static int 900static int
849 return 0; 907 return 0;
850} 908}
851 909
852static MGVTBL coro_state_vtbl = { 910static MGVTBL coro_state_vtbl = {
853 0, 0, 0, 0, 911 0, 0, 0, 0,
854 coro_state_clear, 912 coro_state_free,
855 0, 913 0,
856#ifdef MGf_DUP 914#ifdef MGf_DUP
857 coro_state_dup, 915 coro_state_dup,
858#else 916#else
859# define MGf_DUP 0 917# define MGf_DUP 0
881 assert (mg->mg_type == PERL_MAGIC_ext); 939 assert (mg->mg_type == PERL_MAGIC_ext);
882 return (struct coro *)mg->mg_ptr; 940 return (struct coro *)mg->mg_ptr;
883} 941}
884 942
885static void 943static void
886prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) 944prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
887{ 945{
888 ta->prev = SvSTATE (prev); 946 ta->prev = SvSTATE (prev_sv);
889 ta->next = SvSTATE (next); 947 ta->next = SvSTATE (next_sv);
890 ta->flags = flags;
891} 948}
892 949
893static void 950static void
894api_transfer (SV *prev, SV *next, int flags) 951api_transfer (SV *prev_sv, SV *next_sv)
895{ 952{
896 dTHX;
897 struct transfer_args ta; 953 struct transfer_args ta;
898 954
899 prepare_transfer (&ta, prev, next, flags); 955 prepare_transfer (&ta, prev_sv, next_sv);
900 TRANSFER (ta); 956 TRANSFER (ta);
901} 957}
902 958
959static int
960api_save (SV *coro_sv, int new_save)
961{
962 struct coro *coro = SvSTATE (coro_sv);
963 int old_save = coro->save;
964
965 if (new_save >= 0)
966 coro->save = new_save;
967
968 return old_save;
969}
970
903/** Coro ********************************************************************/ 971/** Coro ********************************************************************/
904 972
905#define PRIO_MAX 3
906#define PRIO_HIGH 1
907#define PRIO_NORMAL 0
908#define PRIO_LOW -1
909#define PRIO_IDLE -3
910#define PRIO_MIN -4
911
912/* for Coro.pm */
913static GV *coro_current, *coro_idle;
914static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
915static int coro_nready;
916
917static void 973static void
918coro_enq (SV *sv) 974coro_enq (SV *coro_sv)
919{ 975{
920 int prio;
921
922 if (SvTYPE (sv) != SVt_PVHV)
923 croak ("Coro::ready tried to enqueue something that is not a coroutine");
924
925 prio = SvSTATE (sv)->prio;
926
927 av_push (coro_ready [prio - PRIO_MIN], sv); 976 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
928 coro_nready++;
929} 977}
930 978
931static SV * 979static SV *
932coro_deq (int min_prio) 980coro_deq (int min_prio)
933{ 981{
937 if (min_prio < 0) 985 if (min_prio < 0)
938 min_prio = 0; 986 min_prio = 0;
939 987
940 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 988 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
941 if (AvFILLp (coro_ready [prio]) >= 0) 989 if (AvFILLp (coro_ready [prio]) >= 0)
942 {
943 coro_nready--;
944 return av_shift (coro_ready [prio]); 990 return av_shift (coro_ready [prio]);
945 }
946 991
947 return 0; 992 return 0;
948} 993}
949 994
950static void 995static int
951api_ready (SV *coro) 996api_ready (SV *coro_sv)
952{ 997{
953 dTHX; 998 struct coro *coro;
954 999
955 if (SvROK (coro)) 1000 if (SvROK (coro_sv))
956 coro = SvRV (coro); 1001 coro_sv = SvRV (coro_sv);
1002
1003 coro = SvSTATE (coro_sv);
1004
1005 if (coro->flags & CF_READY)
1006 return 0;
1007
1008 coro->flags |= CF_READY;
957 1009
958 LOCK; 1010 LOCK;
959 coro_enq (SvREFCNT_inc (coro)); 1011 coro_enq (SvREFCNT_inc (coro_sv));
1012 ++coro_nready;
960 UNLOCK; 1013 UNLOCK;
1014
1015 return 1;
1016}
1017
1018static int
1019api_is_ready (SV *coro_sv)
1020{
1021 return !!(SvSTATE (coro_sv)->flags & CF_READY);
961} 1022}
962 1023
963static void 1024static void
964prepare_schedule (struct transfer_args *ta) 1025prepare_schedule (struct transfer_args *ta)
965{ 1026{
966 SV *current, *prev, *next; 1027 SV *prev_sv, *next_sv;
967
968 current = GvSV (coro_current);
969 1028
970 for (;;) 1029 for (;;)
971 { 1030 {
972 LOCK; 1031 LOCK;
973 next = coro_deq (PRIO_MIN); 1032 next_sv = coro_deq (PRIO_MIN);
1033
1034 /* nothing to schedule: call the idle handler */
1035 if (!next_sv)
1036 {
1037 dSP;
1038 UNLOCK;
1039
1040 ENTER;
1041 SAVETMPS;
1042
1043 PUSHMARK (SP);
1044 PUTBACK;
1045 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1046
1047 FREETMPS;
1048 LEAVE;
1049 continue;
1050 }
1051
1052 ta->next = SvSTATE (next_sv);
1053
1054 /* cannot transfer to destroyed coros, skip and look for next */
1055 if (ta->next->flags & CF_DESTROYED)
1056 {
1057 UNLOCK;
1058 SvREFCNT_dec (next_sv);
1059 /* coro_nready is already taken care of by destroy */
1060 continue;
1061 }
1062
1063 --coro_nready;
974 UNLOCK; 1064 UNLOCK;
975
976 if (next)
977 break; 1065 break;
978
979 {
980 dSP;
981
982 ENTER;
983 SAVETMPS;
984
985 PUSHMARK (SP);
986 PUTBACK;
987 call_sv (GvSV (coro_idle), G_DISCARD);
988
989 FREETMPS;
990 LEAVE;
991 } 1066 }
992 }
993
994 prev = SvRV (current);
995 SvRV (current) = next;
996 1067
997 /* free this only after the transfer */ 1068 /* free this only after the transfer */
1069 prev_sv = SvRV (coro_current);
1070 SvRV_set (coro_current, next_sv);
1071 ta->prev = SvSTATE (prev_sv);
1072
1073 assert (ta->next->flags & CF_READY);
1074 ta->next->flags &= ~CF_READY;
1075
998 LOCK; 1076 LOCK;
999 free_coro_mortal (); 1077 free_coro_mortal ();
1078 coro_mortal = prev_sv;
1000 UNLOCK; 1079 UNLOCK;
1001 coro_mortal = prev;
1002
1003 ta->prev = SvSTATE (prev);
1004 ta->next = SvSTATE (next);
1005 ta->flags = TRANSFER_SAVE_ALL;
1006} 1080}
1007 1081
1008static void 1082static void
1009prepare_cede (struct transfer_args *ta) 1083prepare_cede (struct transfer_args *ta)
1010{ 1084{
1011 LOCK; 1085 api_ready (coro_current);
1012 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
1013 UNLOCK;
1014
1015 prepare_schedule (ta); 1086 prepare_schedule (ta);
1016} 1087}
1017 1088
1089static int
1090prepare_cede_notself (struct transfer_args *ta)
1091{
1092 if (coro_nready)
1093 {
1094 SV *prev = SvRV (coro_current);
1095 prepare_schedule (ta);
1096 api_ready (prev);
1097 return 1;
1098 }
1099 else
1100 return 0;
1101}
1102
1018static void 1103static void
1019api_schedule (void) 1104api_schedule (void)
1020{ 1105{
1021 dTHX;
1022 struct transfer_args ta; 1106 struct transfer_args ta;
1023 1107
1024 prepare_schedule (&ta); 1108 prepare_schedule (&ta);
1025 TRANSFER (ta); 1109 TRANSFER (ta);
1026} 1110}
1027 1111
1028static void 1112static int
1029api_cede (void) 1113api_cede (void)
1030{ 1114{
1031 dTHX;
1032 struct transfer_args ta; 1115 struct transfer_args ta;
1033 1116
1034 prepare_cede (&ta); 1117 prepare_cede (&ta);
1118
1119 if (ta.prev != ta.next)
1120 {
1035 TRANSFER (ta); 1121 TRANSFER (ta);
1122 return 1;
1123 }
1124 else
1125 return 0;
1126}
1127
1128static int
1129api_cede_notself (void)
1130{
1131 struct transfer_args ta;
1132
1133 if (prepare_cede_notself (&ta))
1134 {
1135 TRANSFER (ta);
1136 return 1;
1137 }
1138 else
1139 return 0;
1036} 1140}
1037 1141
1038MODULE = Coro::State PACKAGE = Coro::State 1142MODULE = Coro::State PACKAGE = Coro::State
1039 1143
1040PROTOTYPES: DISABLE 1144PROTOTYPES: DISABLE
1046#endif 1150#endif
1047 BOOT_PAGESIZE; 1151 BOOT_PAGESIZE;
1048 1152
1049 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1153 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1050 1154
1051 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1155 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1052 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1156 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1053 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 1157 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1158 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1159 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1054 1160
1055 main_mainstack = PL_mainstack; 1161 main_mainstack = PL_mainstack;
1056 1162
1057 coroapi.ver = CORO_API_VERSION; 1163 coroapi.ver = CORO_API_VERSION;
1058 coroapi.transfer = api_transfer; 1164 coroapi.transfer = api_transfer;
1068 HV *hv; 1174 HV *hv;
1069 int i; 1175 int i;
1070 1176
1071 Newz (0, coro, 1, struct coro); 1177 Newz (0, coro, 1, struct coro);
1072 coro->args = newAV (); 1178 coro->args = newAV ();
1179 coro->save = CORO_SAVE_ALL;
1180 coro->flags = CF_NEW;
1073 1181
1074 hv = newHV (); 1182 hv = newHV ();
1075 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1183 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1076 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1184 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1077 1185
1078 for (i = 1; i < items; i++) 1186 for (i = 1; i < items; i++)
1079 av_push (coro->args, newSVsv (ST (i))); 1187 av_push (coro->args, newSVsv (ST (i)));
1080
1081 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1082 /*coro->stack = 0;*/
1083} 1188}
1084 OUTPUT: 1189 OUTPUT:
1190 RETVAL
1191
1192int
1193save (SV *coro, int new_save = -1)
1194 CODE:
1195 RETVAL = api_save (coro, new_save);
1196 OUTPUT:
1085 RETVAL 1197 RETVAL
1086 1198
1087void 1199void
1088_set_stacklevel (...) 1200_set_stacklevel (...)
1089 ALIAS: 1201 ALIAS:
1090 Coro::State::transfer = 1 1202 Coro::State::transfer = 1
1091 Coro::schedule = 2 1203 Coro::schedule = 2
1092 Coro::cede = 3 1204 Coro::cede = 3
1093 Coro::Cont::yield = 4 1205 Coro::cede_notself = 4
1094 CODE: 1206 CODE:
1095{ 1207{
1096 struct transfer_args ta; 1208 struct transfer_args ta;
1097 1209
1098 switch (ix) 1210 switch (ix)
1099 { 1211 {
1100 case 0: 1212 case 0:
1101 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1213 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1102 ta.next = 0; 1214 ta.next = 0;
1103 ta.flags = TRANSFER_SET_STACKLEVEL;
1104 break; 1215 break;
1105 1216
1106 case 1: 1217 case 1:
1107 if (items != 3) 1218 if (items != 2)
1108 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); 1219 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1109 1220
1110 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1221 prepare_transfer (&ta, ST (0), ST (1));
1111 break; 1222 break;
1112 1223
1113 case 2: 1224 case 2:
1114 prepare_schedule (&ta); 1225 prepare_schedule (&ta);
1115 break; 1226 break;
1117 case 3: 1228 case 3:
1118 prepare_cede (&ta); 1229 prepare_cede (&ta);
1119 break; 1230 break;
1120 1231
1121 case 4: 1232 case 4:
1122 { 1233 if (!prepare_cede_notself (&ta))
1123 SV *yieldstack; 1234 XSRETURN_EMPTY;
1124 SV *sv;
1125 AV *defav = GvAV (PL_defgv);
1126 1235
1127 yieldstack = *hv_fetch (
1128 (HV *)SvRV (GvSV (coro_current)),
1129 "yieldstack", sizeof ("yieldstack") - 1,
1130 0
1131 );
1132
1133 /* set up @_ -- ugly */
1134 av_clear (defav);
1135 av_fill (defav, items - 1);
1136 while (items--)
1137 av_store (defav, items, SvREFCNT_inc (ST(items)));
1138
1139 sv = av_pop ((AV *)SvRV (yieldstack));
1140 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1141 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1142 ta.flags = 0;
1143 SvREFCNT_dec (sv);
1144 }
1145 break; 1236 break;
1146
1147 } 1237 }
1148 1238
1239 BARRIER;
1149 TRANSFER (ta); 1240 TRANSFER (ta);
1150}
1151 1241
1152void 1242 if (GIMME_V != G_VOID && ta.next != ta.prev)
1153_clone_state_from (SV *dst, SV *src) 1243 XSRETURN_YES;
1244}
1245
1246bool
1247_destroy (SV *coro_sv)
1154 CODE: 1248 CODE:
1155{ 1249 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1156 struct coro *coro_src = SvSTATE (src); 1250 OUTPUT:
1157 1251 RETVAL
1158 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1159
1160 ++coro_src->refcnt;
1161 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1162}
1163 1252
1164void 1253void
1165_exit (code) 1254_exit (code)
1166 int code 1255 int code
1167 PROTOTYPE: $ 1256 PROTOTYPE: $
1168 CODE: 1257 CODE:
1169 _exit (code); 1258 _exit (code);
1170 1259
1260int
1261cctx_count ()
1262 CODE:
1263 RETVAL = cctx_count;
1264 OUTPUT:
1265 RETVAL
1266
1267int
1268cctx_idle ()
1269 CODE:
1270 RETVAL = cctx_idle;
1271 OUTPUT:
1272 RETVAL
1273
1171MODULE = Coro::State PACKAGE = Coro 1274MODULE = Coro::State PACKAGE = Coro
1172 1275
1173BOOT: 1276BOOT:
1174{ 1277{
1175 int i; 1278 int i;
1181 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1284 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1182 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1285 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1183 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1286 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1184 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1287 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1185 1288
1186 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1289 coro_current = get_sv ("Coro::current", FALSE);
1187 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1290 SvREADONLY_on (coro_current);
1188 1291
1189 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1292 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1190 coro_ready[i] = newAV (); 1293 coro_ready[i] = newAV ();
1191 1294
1192 { 1295 {
1193 SV *sv = perl_get_sv("Coro::API", 1); 1296 SV *sv = perl_get_sv("Coro::API", 1);
1194 1297
1195 coroapi.schedule = api_schedule; 1298 coroapi.schedule = api_schedule;
1299 coroapi.save = api_save;
1196 coroapi.cede = api_cede; 1300 coroapi.cede = api_cede;
1301 coroapi.cede_notself = api_cede_notself;
1197 coroapi.ready = api_ready; 1302 coroapi.ready = api_ready;
1303 coroapi.is_ready = api_is_ready;
1198 coroapi.nready = &coro_nready; 1304 coroapi.nready = &coro_nready;
1199 coroapi.current = coro_current; 1305 coroapi.current = coro_current;
1200 1306
1201 GCoroAPI = &coroapi; 1307 GCoroAPI = &coroapi;
1202 sv_setiv (sv, (IV)&coroapi); 1308 sv_setiv (sv, (IV)&coroapi);
1203 SvREADONLY_on (sv); 1309 SvREADONLY_on (sv);
1204 } 1310 }
1205} 1311}
1312
1313void
1314_set_current (SV *current)
1315 PROTOTYPE: $
1316 CODE:
1317 SvREFCNT_dec (SvRV (coro_current));
1318 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1206 1319
1207int 1320int
1208prio (Coro::State coro, int newprio = 0) 1321prio (Coro::State coro, int newprio = 0)
1209 ALIAS: 1322 ALIAS:
1210 nice = 1 1323 nice = 1
1213 RETVAL = coro->prio; 1326 RETVAL = coro->prio;
1214 1327
1215 if (items > 1) 1328 if (items > 1)
1216 { 1329 {
1217 if (ix) 1330 if (ix)
1218 newprio += coro->prio; 1331 newprio = coro->prio - newprio;
1219 1332
1220 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1333 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1221 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1334 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1222 1335
1223 coro->prio = newprio; 1336 coro->prio = newprio;
1224 } 1337 }
1225} 1338}
1339 OUTPUT:
1340 RETVAL
1226 1341
1227void 1342SV *
1228ready (SV *self) 1343ready (SV *self)
1229 PROTOTYPE: $ 1344 PROTOTYPE: $
1230 CODE: 1345 CODE:
1231 api_ready (self); 1346 RETVAL = boolSV (api_ready (self));
1347 OUTPUT:
1348 RETVAL
1349
1350SV *
1351is_ready (SV *self)
1352 PROTOTYPE: $
1353 CODE:
1354 RETVAL = boolSV (api_is_ready (self));
1355 OUTPUT:
1356 RETVAL
1232 1357
1233int 1358int
1234nready (...) 1359nready (...)
1235 PROTOTYPE: 1360 PROTOTYPE:
1236 CODE: 1361 CODE:
1242 1367
1243SV * 1368SV *
1244_get_state () 1369_get_state ()
1245 CODE: 1370 CODE:
1246{ 1371{
1247 struct { 1372 struct io_state *data;
1248 int errorno;
1249 int laststype;
1250 int laststatval;
1251 Stat_t statcache;
1252 } data;
1253 1373
1374 RETVAL = newSV (sizeof (struct io_state));
1375 data = (struct io_state *)SvPVX (RETVAL);
1376 SvCUR_set (RETVAL, sizeof (struct io_state));
1377 SvPOK_only (RETVAL);
1378
1254 data.errorno = errno; 1379 data->errorno = errno;
1255 data.laststype = PL_laststype; 1380 data->laststype = PL_laststype;
1256 data.laststatval = PL_laststatval; 1381 data->laststatval = PL_laststatval;
1257 data.statcache = PL_statcache; 1382 data->statcache = PL_statcache;
1258
1259 RETVAL = newSVpvn ((char *)&data, sizeof data);
1260} 1383}
1261 OUTPUT: 1384 OUTPUT:
1262 RETVAL 1385 RETVAL
1263 1386
1264void 1387void
1265_set_state (char *data_) 1388_set_state (char *data_)
1266 PROTOTYPE: $ 1389 PROTOTYPE: $
1267 CODE: 1390 CODE:
1268{ 1391{
1269 struct { 1392 struct io_state *data = (void *)data_;
1270 int errorno;
1271 int laststype;
1272 int laststatval;
1273 Stat_t statcache;
1274 } *data = (void *)data_;
1275 1393
1276 errno = data->errorno; 1394 errno = data->errorno;
1277 PL_laststype = data->laststype; 1395 PL_laststype = data->laststype;
1278 PL_laststatval = data->laststatval; 1396 PL_laststatval = data->laststatval;
1279 PL_statcache = data->statcache; 1397 PL_statcache = data->statcache;
1280} 1398}
1399

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines