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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines