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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines