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.122 by root, Mon Dec 4 13:47:56 2006 UTC vs.
Revision 1.151 by root, Wed Sep 19 21:39:15 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines