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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines