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.90 by root, Sat Nov 25 00:56:35 2006 UTC vs.
Revision 1.116 by root, Fri Dec 1 14:01:43 2006 UTC

1#define PERL_NO_GET_CONTEXT
2
3#include "libcoro/coro.c" 1#include "libcoro/coro.c"
4 2
5#include "EXTERN.h" 3#include "EXTERN.h"
6#include "perl.h" 4#include "perl.h"
7#include "XSUB.h" 5#include "XSUB.h"
8 6
9#include "patchlevel.h" 7#include "patchlevel.h"
10 8
11#if PERL_VERSION < 6 9#if USE_VALGRIND
10# include <valgrind/valgrind.h>
11#endif
12
13/* the maximum number of idle cctx that will be pooled */
14#define MAX_IDLE_CCTX 8
15
16#define PERL_VERSION_ATLEAST(a,b,c) \
17 (PERL_REVISION > (a) \
18 || (PERL_REVISION == (a) \
19 && (PERL_VERSION > (b) \
20 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
21
22#if !PERL_VERSION_ATLEAST (5,6,0)
12# ifndef PL_ppaddr 23# ifndef PL_ppaddr
13# define PL_ppaddr ppaddr 24# define PL_ppaddr ppaddr
14# endif 25# endif
15# ifndef call_sv 26# ifndef call_sv
16# define call_sv perl_call_sv 27# define call_sv perl_call_sv
27# ifndef IS_PADCONST 38# ifndef IS_PADCONST
28# define IS_PADCONST(v) 0 39# define IS_PADCONST(v) 0
29# endif 40# endif
30#endif 41#endif
31 42
43#include <stdio.h>
32#include <errno.h> 44#include <errno.h>
33 45
34#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 46#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
35# undef STACKGUARD 47# undef STACKGUARD
36#endif 48#endif
53# ifndef PAGESIZE 65# ifndef PAGESIZE
54# define PAGESIZE pagesize 66# define PAGESIZE pagesize
55# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) 67# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
56static long pagesize; 68static long pagesize;
57# else 69# else
58# define BOOT_PAGESIZE 70# define BOOT_PAGESIZE (void)0
59# endif 71# endif
72#else
73# define PAGESIZE 0
74# define BOOT_PAGESIZE (void)0
60#endif 75#endif
61
62#define SUB_INIT "Coro::State::initialize"
63 76
64/* The next macro should declare a variable stacklevel that contains and approximation 77/* The next macro should declare a variable stacklevel that contains and approximation
65 * to the current C stack pointer. Its property is that it changes with each call 78 * to the current C stack pointer. Its property is that it changes with each call
66 * and should be unique. */ 79 * and should be unique. */
80#define dSTACKLEVEL int stacklevel
67#define dSTACKLEVEL void *stacklevel = &stacklevel 81#define STACKLEVEL ((void *)&stacklevel)
68 82
69#define IN_DESTRUCT (PL_main_cv == Nullcv) 83#define IN_DESTRUCT (PL_main_cv == Nullcv)
70 84
71#define labs(l) ((l) >= 0 ? (l) : -(l)) 85#if __GNUC__ >= 3
86# define attribute(x) __attribute__(x)
87#else
88# define attribute(x)
89#endif
90
91#define NOINLINE attribute ((noinline))
72 92
73#include "CoroAPI.h" 93#include "CoroAPI.h"
94
95#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
74 96
75#ifdef USE_ITHREADS 97#ifdef USE_ITHREADS
76static perl_mutex coro_mutex; 98static perl_mutex coro_mutex;
77# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 99# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
78# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 100# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
84static struct CoroAPI coroapi; 106static struct CoroAPI coroapi;
85static AV *main_mainstack; /* used to differentiate between $main and others */ 107static AV *main_mainstack; /* used to differentiate between $main and others */
86static HV *coro_state_stash, *coro_stash; 108static HV *coro_state_stash, *coro_stash;
87static SV *coro_mortal; /* will be freed after next transfer */ 109static SV *coro_mortal; /* will be freed after next transfer */
88 110
89/* this is actually not just the c stack but also c registers etc... */ 111static struct coro_cctx *cctx_first;
112static int cctx_count, cctx_idle;
113
114/* this is a structure representing a c-level coroutine */
90typedef struct coro_stack { 115typedef struct coro_cctx {
91 struct coro_stack *next; 116 struct coro_cctx *next;
92 117
93 void *idle_sp; 118 /* the stack */
94 void *sptr; 119 void *sptr;
95 long ssize; /* positive == mmap, otherwise malloc */ 120 long ssize; /* positive == mmap, otherwise malloc */
96 121
97 /* cpu state */ 122 /* cpu state */
123 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
124 JMPENV *top_env;
98 coro_context cctx; 125 coro_context cctx;
99} coro_stack;
100 126
101static coro_stack *main_stack; 127 int inuse;
102 128
129#if USE_VALGRIND
130 int valgrind_id;
131#endif
132} coro_cctx;
133
134enum {
135 CF_RUNNING, /* coroutine is running */
136 CF_READY, /* coroutine is ready */
137};
138
139/* this is a structure representing a perl-level coroutine */
103struct coro { 140struct coro {
141 /* the c coroutine allocated to this perl coroutine, if any */
142 coro_cctx *cctx;
143
144 /* data associated with this coroutine (initial args) */
145 AV *args;
146 int refcnt;
147 int flags;
148
104 /* optionally saved, might be zero */ 149 /* optionally saved, might be zero */
105 AV *defav; 150 AV *defav;
106 SV *defsv; 151 SV *defsv;
107 SV *errsv; 152 SV *errsv;
108 153
109 /* saved global state not related to stacks */ 154#define VAR(name,type) type name;
110 U8 dowarn; 155# include "state.h"
111 I32 in_eval; 156#undef VAR
112
113 /* the c stack, if any */
114 coro_stack *stack;
115
116 /* the stacks and related info (callchain etc..) */
117 PERL_SI *curstackinfo;
118 AV *curstack;
119 AV *mainstack;
120 SV **stack_sp;
121 OP *op;
122 SV **curpad;
123 AV *comppad;
124 CV *compcv;
125 SV **stack_base;
126 SV **stack_max;
127 SV **tmps_stack;
128 I32 tmps_floor;
129 I32 tmps_ix;
130 I32 tmps_max;
131 I32 *markstack;
132 I32 *markstack_ptr;
133 I32 *markstack_max;
134 I32 *scopestack;
135 I32 scopestack_ix;
136 I32 scopestack_max;
137 ANY *savestack;
138 I32 savestack_ix;
139 I32 savestack_max;
140 OP **retstack;
141 I32 retstack_ix;
142 I32 retstack_max;
143 PMOP *curpm;
144 COP *curcop;
145 JMPENV *top_env;
146 157
147 /* coro process data */ 158 /* coro process data */
148 int prio; 159 int prio;
149
150 /* data associated with this coroutine (initial args) */
151 AV *args;
152 int refcnt;
153}; 160};
154 161
155typedef struct coro *Coro__State; 162typedef struct coro *Coro__State;
156typedef struct coro *Coro__State_or_hashref; 163typedef struct coro *Coro__State_or_hashref;
157 164
158static AV * 165static AV *
159coro_clone_padlist (pTHX_ CV *cv) 166coro_clone_padlist (CV *cv)
160{ 167{
161 AV *padlist = CvPADLIST (cv); 168 AV *padlist = CvPADLIST (cv);
162 AV *newpadlist, *newpad; 169 AV *newpadlist, *newpad;
163 170
164 newpadlist = newAV (); 171 newpadlist = newAV ();
165 AvREAL_off (newpadlist); 172 AvREAL_off (newpadlist);
166#if PERL_VERSION < 9 173#if PERL_VERSION_ATLEAST (5,9,0)
174 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
175#else
167 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 176 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
168#else
169 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
170#endif 177#endif
171 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 178 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
172 --AvFILLp (padlist); 179 --AvFILLp (padlist);
173 180
174 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 181 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
176 183
177 return newpadlist; 184 return newpadlist;
178} 185}
179 186
180static void 187static void
181free_padlist (pTHX_ AV *padlist) 188free_padlist (AV *padlist)
182{ 189{
183 /* may be during global destruction */ 190 /* may be during global destruction */
184 if (SvREFCNT (padlist)) 191 if (SvREFCNT (padlist))
185 { 192 {
186 I32 i = AvFILLp (padlist); 193 I32 i = AvFILLp (padlist);
207 AV *padlist; 214 AV *padlist;
208 AV *av = (AV *)mg->mg_obj; 215 AV *av = (AV *)mg->mg_obj;
209 216
210 /* casting is fun. */ 217 /* casting is fun. */
211 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 218 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
212 free_padlist (aTHX_ padlist); 219 free_padlist (padlist);
213 220
214 SvREFCNT_dec (av); 221 SvREFCNT_dec (av);
215 222
216 return 0; 223 return 0;
217} 224}
218 225
219#define PERL_MAGIC_coro PERL_MAGIC_ext 226#define PERL_MAGIC_coro PERL_MAGIC_ext
220 227
221static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 228static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
222 229
230#define CORO_MAGIC(cv) \
231 SvMAGIC (cv) \
232 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
233 ? SvMAGIC (cv) \
234 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
235 : 0
236
223/* the next two functions merely cache the padlists */ 237/* the next two functions merely cache the padlists */
224static void 238static void
225get_padlist (pTHX_ CV *cv) 239get_padlist (CV *cv)
226{ 240{
227 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 241 MAGIC *mg = CORO_MAGIC (cv);
242 AV *av;
228 243
229 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 244 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
230 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 245 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
231 else 246 else
232 { 247 {
233#if 0 248#if 0
234 /* this should work - but it doesn't :( */ 249 /* this is probably cleaner, but also slower? */
235 CV *cp = Perl_cv_clone (aTHX_ cv); 250 CV *cp = Perl_cv_clone (cv);
236 CvPADLIST (cv) = CvPADLIST (cp); 251 CvPADLIST (cv) = CvPADLIST (cp);
237 CvPADLIST (cp) = 0; 252 CvPADLIST (cp) = 0;
238 SvREFCNT_dec (cp); 253 SvREFCNT_dec (cp);
239#else 254#else
240 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 255 CvPADLIST (cv) = coro_clone_padlist (cv);
241#endif 256#endif
242 } 257 }
243} 258}
244 259
245static void 260static void
246put_padlist (pTHX_ CV *cv) 261put_padlist (CV *cv)
247{ 262{
248 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 263 MAGIC *mg = CORO_MAGIC (cv);
264 AV *av;
249 265
250 if (!mg) 266 if (!mg)
251 { 267 {
252 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 268 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
253 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 269 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
254 mg->mg_virtual = &vtbl_coro; 270 mg->mg_virtual = &vtbl_coro;
255 mg->mg_obj = (SV *)newAV (); 271 mg->mg_obj = (SV *)newAV ();
256 } 272 }
257 273
258 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); 274 av = (AV *)mg->mg_obj;
275
276 if (AvFILLp (av) >= AvMAX (av))
277 av_extend (av, AvMAX (av) + 1);
278
279 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
259} 280}
260 281
261#define SB do { 282#define SB do {
262#define SE } while (0) 283#define SE } while (0)
263 284
264#define LOAD(state) load_state(aTHX_ (state)); 285#define LOAD(state) load_state((state));
265#define SAVE(state,flags) save_state(aTHX_ (state),(flags)); 286#define SAVE(state,flags) save_state((state),(flags));
266 287
267#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 288#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE
268 289
269static void 290static void
270load_state(pTHX_ Coro__State c) 291load_state(Coro__State c)
271{ 292{
272 PL_dowarn = c->dowarn; 293#define VAR(name,type) PL_ ## name = c->name;
273 PL_in_eval = c->in_eval; 294# include "state.h"
274 295#undef VAR
275 PL_curstackinfo = c->curstackinfo;
276 PL_curstack = c->curstack;
277 PL_mainstack = c->mainstack;
278 PL_stack_sp = c->stack_sp;
279 PL_op = c->op;
280 PL_curpad = c->curpad;
281 PL_comppad = c->comppad;
282 PL_compcv = c->compcv;
283 PL_stack_base = c->stack_base;
284 PL_stack_max = c->stack_max;
285 PL_tmps_stack = c->tmps_stack;
286 PL_tmps_floor = c->tmps_floor;
287 PL_tmps_ix = c->tmps_ix;
288 PL_tmps_max = c->tmps_max;
289 PL_markstack = c->markstack;
290 PL_markstack_ptr = c->markstack_ptr;
291 PL_markstack_max = c->markstack_max;
292 PL_scopestack = c->scopestack;
293 PL_scopestack_ix = c->scopestack_ix;
294 PL_scopestack_max = c->scopestack_max;
295 PL_savestack = c->savestack;
296 PL_savestack_ix = c->savestack_ix;
297 PL_savestack_max = c->savestack_max;
298#if PERL_VERSION < 9
299 PL_retstack = c->retstack;
300 PL_retstack_ix = c->retstack_ix;
301 PL_retstack_max = c->retstack_max;
302#endif
303 PL_curpm = c->curpm;
304 PL_curcop = c->curcop;
305 PL_top_env = c->top_env;
306 296
307 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 297 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
308 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 298 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
309 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 299 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
310 300
313 CV *cv; 303 CV *cv;
314 304
315 /* now do the ugly restore mess */ 305 /* now do the ugly restore mess */
316 while ((cv = (CV *)POPs)) 306 while ((cv = (CV *)POPs))
317 { 307 {
318 AV *padlist = (AV *)POPs;
319
320 if (padlist)
321 {
322 put_padlist (aTHX_ cv); /* mark this padlist as available */ 308 put_padlist (cv); /* mark this padlist as available */
323 CvPADLIST(cv) = padlist; 309 CvDEPTH (cv) = PTR2IV (POPs);
324 } 310 CvPADLIST (cv) = (AV *)POPs;
325
326 ++CvDEPTH(cv);
327 } 311 }
328 312
329 PUTBACK; 313 PUTBACK;
330 } 314 }
331} 315}
332 316
333static void 317static void
334save_state(pTHX_ Coro__State c, int flags) 318save_state(Coro__State c, int flags)
335{ 319{
336 { 320 {
337 dSP; 321 dSP;
338 I32 cxix = cxstack_ix; 322 I32 cxix = cxstack_ix;
339 PERL_CONTEXT *ccstk = cxstack; 323 PERL_CONTEXT *ccstk = cxstack;
353 PERL_CONTEXT *cx = &ccstk[cxix--]; 337 PERL_CONTEXT *cx = &ccstk[cxix--];
354 338
355 if (CxTYPE(cx) == CXt_SUB) 339 if (CxTYPE(cx) == CXt_SUB)
356 { 340 {
357 CV *cv = cx->blk_sub.cv; 341 CV *cv = cx->blk_sub.cv;
342
358 if (CvDEPTH(cv)) 343 if (CvDEPTH (cv))
359 { 344 {
360 EXTEND (SP, CvDEPTH(cv)*2); 345 EXTEND (SP, 3);
361
362 while (--CvDEPTH(cv))
363 {
364 /* this tells the restore code to increment CvDEPTH */
365 PUSHs (Nullsv);
366 PUSHs ((SV *)cv);
367 }
368 346
369 PUSHs ((SV *)CvPADLIST(cv)); 347 PUSHs ((SV *)CvPADLIST(cv));
348 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
370 PUSHs ((SV *)cv); 349 PUSHs ((SV *)cv);
371 350
351 CvDEPTH (cv) = 0;
372 get_padlist (aTHX_ cv); 352 get_padlist (cv);
373 } 353 }
374 } 354 }
375#ifdef CXt_FORMAT 355#ifdef CXt_FORMAT
376 else if (CxTYPE(cx) == CXt_FORMAT) 356 else if (CxTYPE(cx) == CXt_FORMAT)
377 { 357 {
395 375
396 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 376 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
397 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 377 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
398 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 378 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
399 379
400 c->dowarn = PL_dowarn; 380#define VAR(name,type)c->name = PL_ ## name;
401 c->in_eval = PL_in_eval; 381# include "state.h"
402 382#undef VAR
403 c->curstackinfo = PL_curstackinfo;
404 c->curstack = PL_curstack;
405 c->mainstack = PL_mainstack;
406 c->stack_sp = PL_stack_sp;
407 c->op = PL_op;
408 c->curpad = PL_curpad;
409 c->comppad = PL_comppad;
410 c->compcv = PL_compcv;
411 c->stack_base = PL_stack_base;
412 c->stack_max = PL_stack_max;
413 c->tmps_stack = PL_tmps_stack;
414 c->tmps_floor = PL_tmps_floor;
415 c->tmps_ix = PL_tmps_ix;
416 c->tmps_max = PL_tmps_max;
417 c->markstack = PL_markstack;
418 c->markstack_ptr = PL_markstack_ptr;
419 c->markstack_max = PL_markstack_max;
420 c->scopestack = PL_scopestack;
421 c->scopestack_ix = PL_scopestack_ix;
422 c->scopestack_max = PL_scopestack_max;
423 c->savestack = PL_savestack;
424 c->savestack_ix = PL_savestack_ix;
425 c->savestack_max = PL_savestack_max;
426#if PERL_VERSION < 9
427 c->retstack = PL_retstack;
428 c->retstack_ix = PL_retstack_ix;
429 c->retstack_max = PL_retstack_max;
430#endif
431 c->curpm = PL_curpm;
432 c->curcop = PL_curcop;
433 c->top_env = PL_top_env;
434} 383}
435 384
436/* 385/*
437 * allocate various perl stacks. This is an exact copy 386 * allocate various perl stacks. This is an exact copy
438 * of perl.c:init_stacks, except that it uses less memory 387 * of perl.c:init_stacks, except that it uses less memory
439 * on the (sometimes correct) assumption that coroutines do 388 * on the (sometimes correct) assumption that coroutines do
440 * not usually need a lot of stackspace. 389 * not usually need a lot of stackspace.
441 */ 390 */
442static void 391static void
443coro_init_stacks (pTHX) 392coro_init_stacks ()
444{ 393{
445 LOCK;
446
447 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 394 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
448 PL_curstackinfo->si_type = PERLSI_MAIN; 395 PL_curstackinfo->si_type = PERLSI_MAIN;
449 PL_curstack = PL_curstackinfo->si_stack; 396 PL_curstack = PL_curstackinfo->si_stack;
450 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 397 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
451 398
452 PL_stack_base = AvARRAY(PL_curstack); 399 PL_stack_base = AvARRAY(PL_curstack);
453 PL_stack_sp = PL_stack_base; 400 PL_stack_sp = PL_stack_base;
454 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 401 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
455 402
456 New(50,PL_tmps_stack,96,SV*); 403 New(50,PL_tmps_stack,128,SV*);
457 PL_tmps_floor = -1; 404 PL_tmps_floor = -1;
458 PL_tmps_ix = -1; 405 PL_tmps_ix = -1;
459 PL_tmps_max = 96; 406 PL_tmps_max = 128;
460 407
461 New(54,PL_markstack,16,I32); 408 New(54,PL_markstack,32,I32);
462 PL_markstack_ptr = PL_markstack; 409 PL_markstack_ptr = PL_markstack;
463 PL_markstack_max = PL_markstack + 16; 410 PL_markstack_max = PL_markstack + 32;
464 411
465#ifdef SET_MARK_OFFSET 412#ifdef SET_MARK_OFFSET
466 SET_MARK_OFFSET; 413 SET_MARK_OFFSET;
467#endif 414#endif
468 415
469 New(54,PL_scopestack,16,I32); 416 New(54,PL_scopestack,32,I32);
470 PL_scopestack_ix = 0; 417 PL_scopestack_ix = 0;
471 PL_scopestack_max = 16; 418 PL_scopestack_max = 32;
472 419
473 New(54,PL_savestack,96,ANY); 420 New(54,PL_savestack,64,ANY);
474 PL_savestack_ix = 0; 421 PL_savestack_ix = 0;
475 PL_savestack_max = 96; 422 PL_savestack_max = 64;
476 423
477#if PERL_VERSION < 9 424#if !PERL_VERSION_ATLEAST (5,9,0)
478 New(54,PL_retstack,8,OP*); 425 New(54,PL_retstack,16,OP*);
479 PL_retstack_ix = 0; 426 PL_retstack_ix = 0;
480 PL_retstack_max = 8; 427 PL_retstack_max = 16;
481#endif 428#endif
482
483 UNLOCK;
484} 429}
485 430
486/* 431/*
487 * destroy the stacks, the callchain etc... 432 * destroy the stacks, the callchain etc...
488 */ 433 */
489static void 434static void
490destroy_stacks(pTHX) 435coro_destroy_stacks ()
491{ 436{
492 if (!IN_DESTRUCT) 437 if (!IN_DESTRUCT)
493 { 438 {
494 /* is this ugly, I ask? */ 439 /* is this ugly, I ask? */
495 LEAVE_SCOPE (0); 440 LEAVE_SCOPE (0);
526 471
527 Safefree (PL_tmps_stack); 472 Safefree (PL_tmps_stack);
528 Safefree (PL_markstack); 473 Safefree (PL_markstack);
529 Safefree (PL_scopestack); 474 Safefree (PL_scopestack);
530 Safefree (PL_savestack); 475 Safefree (PL_savestack);
531#if PERL_VERSION < 9 476#if !PERL_VERSION_ATLEAST (5,9,0)
532 Safefree (PL_retstack); 477 Safefree (PL_retstack);
533#endif 478#endif
534} 479}
535 480
536static void 481static void
537setup_coro (struct coro *coro) 482setup_coro (struct coro *coro)
538{ 483{
539 /* 484 /*
540 * emulate part of the perl startup here. 485 * emulate part of the perl startup here.
541 */ 486 */
542 dTHX;
543 dSP;
544 UNOP myop;
545 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE);
546 487
547 coro_init_stacks (aTHX); 488 coro_init_stacks ();
489
548 /*PL_curcop = 0;*/ 490 PL_curcop = 0;
549 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 491 PL_in_eval = 0;
492 PL_curpm = 0;
493
494 {
495 dSP;
496 LOGOP myop;
497
498 /* I have no idea why this is needed, but it is */
499 PUSHMARK (SP);
500
550 SvREFCNT_dec (GvAV (PL_defgv)); 501 SvREFCNT_dec (GvAV (PL_defgv));
551 GvAV (PL_defgv) = coro->args; coro->args = 0; 502 GvAV (PL_defgv) = coro->args; coro->args = 0;
552 503
553 SPAGAIN;
554
555 Zero (&myop, 1, UNOP); 504 Zero (&myop, 1, LOGOP);
556 myop.op_next = Nullop; 505 myop.op_next = Nullop;
557 myop.op_flags = OPf_WANT_VOID; 506 myop.op_flags = OPf_WANT_VOID;
558 507
559 PL_op = (OP *)&myop; 508 PL_op = (OP *)&myop;
560 509
561 PUSHMARK(SP); 510 PUSHMARK (SP);
562 XPUSHs (sub_init); 511 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
563 PUTBACK; 512 PUTBACK;
564 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 513 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
565 SPAGAIN; 514 SPAGAIN;
566 515
567 ENTER; /* necessary e.g. for dounwind */ 516 ENTER; /* necessary e.g. for dounwind */
517 }
568} 518}
569 519
570static void 520static void
571transfer_tail () 521free_coro_mortal ()
572{ 522{
573 if (coro_mortal) 523 if (coro_mortal)
574 { 524 {
575 SvREFCNT_dec (coro_mortal); 525 SvREFCNT_dec (coro_mortal);
576 coro_mortal = 0; 526 coro_mortal = 0;
577 } 527 }
528}
578 529
530static void NOINLINE
531prepare_cctx (coro_cctx *cctx)
532{
533 dSP;
534 LOGOP myop;
535
536 Zero (&myop, 1, LOGOP);
537 myop.op_next = PL_op;
538 myop.op_flags = OPf_WANT_VOID;
539
540 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
541
542 PUSHMARK (SP);
543 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
544 PUTBACK;
545 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX);
546 SPAGAIN;
547}
548
549static void
550coro_run (void *arg)
551{
552 /* coro_run is the alternative epilogue of transfer() */
579 UNLOCK; 553 UNLOCK;
580}
581 554
582static void
583coro_run (void *arg)
584{
585 /* 555 /*
586 * this is a _very_ stripped down perl interpreter ;) 556 * this is a _very_ stripped down perl interpreter ;)
587 */ 557 */
588 dTHX;
589
590 transfer_tail ();
591
592 PL_top_env = &PL_start_env; 558 PL_top_env = &PL_start_env;
593 PL_restartop = PL_op->op_next; 559
560 /* inject call to cctx_init */
561 prepare_cctx ((coro_cctx *)arg);
562
594 /* somebody will hit me for both perl_run and PL_restart_top */ 563 /* somebody will hit me for both perl_run and PL_restartop */
595 perl_run (aTHX_ PERL_GET_CONTEXT); 564 perl_run (PL_curinterp);
596 565
566 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
597 abort (); 567 abort ();
598} 568}
599 569
600static coro_stack * 570static coro_cctx *
601stack_new () 571cctx_new ()
602{ 572{
603 coro_stack *stack; 573 coro_cctx *cctx;
604 574
575 ++cctx_count;
576
605 New (0, stack, 1, coro_stack); 577 New (0, cctx, 1, coro_cctx);
606 578
607#if HAVE_MMAP 579#if HAVE_MMAP
608 580
609 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ 581 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
582 /* mmap suppsedly does allocate-on-write for us */
610 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 583 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
611 584
612 if (stack->sptr == (void *)-1) 585 if (cctx->sptr == (void *)-1)
613 { 586 {
614 fprintf (stderr, "FATAL: unable to mmap stack for coroutine\n"); 587 perror ("FATAL: unable to mmap stack for coroutine");
615 _exit (EXIT_FAILURE); 588 _exit (EXIT_FAILURE);
616 } 589 }
617 590
591# if STACKGUARD
618 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 592 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
593# endif
619 594
620#else 595#else
621 596
622 stack->ssize = STACKSIZE * (long)sizeof (long); 597 cctx->ssize = STACKSIZE * (long)sizeof (long);
623 New (0, stack->sptr, STACKSIZE, long); 598 New (0, cctx->sptr, STACKSIZE, long);
624 599
625 if (!stack->sptr) 600 if (!cctx->sptr)
626 { 601 {
627 fprintf (stderr, "FATAL: unable to malloc stack for coroutine\n"); 602 perror ("FATAL: unable to malloc stack for coroutine");
628 _exit (EXIT_FAILURE); 603 _exit (EXIT_FAILURE);
629 } 604 }
630 605
631#endif 606#endif
632 607
608#if USE_VALGRIND
609 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
610 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
611 cctx->ssize + (char *)cctx->sptr
612 );
613#endif
614
633 coro_create (&stack->cctx, coro_run, 0, stack->sptr, stack->ssize); 615 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
634 616
635 return stack; 617 return cctx;
636} 618}
637 619
638static void 620static void
639stack_free (coro_stack *stack) 621cctx_destroy (coro_cctx *cctx)
640{ 622{
641 if (!stack || stack == main_stack) 623 if (!cctx)
642 return; 624 return;
643 625
626 --cctx_count;
627
628#if USE_VALGRIND
629 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
630#endif
631
644#if HAVE_MMAP 632#if HAVE_MMAP
645 munmap (stack->sptr, stack->ssize); 633 munmap (cctx->sptr, cctx->ssize);
646#else 634#else
647 Safefree (stack->sptr); 635 Safefree (cctx->sptr);
648#endif 636#endif
649 637
650 Safefree (stack); 638 Safefree (cctx);
651} 639}
652 640
653static coro_stack *stack_first; 641static coro_cctx *
654 642cctx_get ()
655static void
656stack_get (struct coro *coro)
657{ 643{
644 coro_cctx *cctx;
645
658 if (stack_first) 646 if (cctx_first)
659 { 647 {
660 coro->stack = stack_first; 648 cctx = cctx_first;
661 stack_first = stack_first->next; 649 cctx_first = cctx->next;
650 --cctx_idle;
662 } 651 }
663 else 652 else
664 coro->stack = stack_new (); 653 {
665} 654 cctx = cctx_new ();
655 PL_op = PL_op->op_next;
656 }
666 657
658 return cctx;
659}
660
667static void 661static void
668stack_put (coro_stack *stack) 662cctx_put (coro_cctx *cctx)
669{ 663{
670 stack->next = stack_first; 664 /* free another cctx if overlimit */
671 stack_first = stack; 665 if (cctx_idle >= MAX_IDLE_CCTX)
666 {
667 coro_cctx *first = cctx_first;
668 cctx_first = first->next;
669 --cctx_idle;
670
671 assert (!first->inuse);
672 cctx_destroy (first);
673 }
674
675 ++cctx_idle;
676 cctx->next = cctx_first;
677 cctx_first = cctx;
672} 678}
673 679
674/* never call directly, always through the coro_state_transfer global variable */ 680/* never call directly, always through the coro_state_transfer global variable */
675static void 681static void NOINLINE
676transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 682transfer (struct coro *prev, struct coro *next, int flags)
677{ 683{
678 dSTACKLEVEL; 684 dSTACKLEVEL;
679 685
680 /* sometimes transfer is only called to set idle_sp */ 686 /* sometimes transfer is only called to set idle_sp */
681 if (!prev->stack->idle_sp) 687 if (flags == TRANSFER_SET_STACKLEVEL)
682 prev->stack->idle_sp = stacklevel; 688 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
683
684 LOCK;
685
686 if (prev != next) 689 else if (prev != next)
687 { 690 {
688 coro_stack *prev_stack = prev->stack; 691 coro_cctx *prev__cctx;
689 692
690 /* possibly "free" the stack */ 693 if (!prev->cctx)
691 if (prev_stack->idle_sp == stacklevel)
692 { 694 {
693 stack_put (prev_stack); 695 /* create a new empty context */
694 prev->stack = 0; 696 Newz (0, prev->cctx, 1, coro_cctx);
697 prev->cctx->inuse = 1;
698 prev->flags |= CF_RUNNING;
695 } 699 }
700
701 if (!prev->flags & CF_RUNNING)
702 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
703
704 if (next->flags & CF_RUNNING)
705 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
706
707 prev->flags &= ~CF_RUNNING;
708 next->flags |= CF_RUNNING;
709
710 LOCK;
696 711
697 if (next->mainstack) 712 if (next->mainstack)
698 { 713 {
699 /* coroutine already started */ 714 /* coroutine already started */
700 SAVE (prev, flags); 715 SAVE (prev, flags);
705 /* need to start coroutine */ 720 /* need to start coroutine */
706 /* first get rid of the old state */ 721 /* first get rid of the old state */
707 SAVE (prev, -1); 722 SAVE (prev, -1);
708 /* setup coroutine call */ 723 /* setup coroutine call */
709 setup_coro (next); 724 setup_coro (next);
710 /* need to change stack from main_stack to real one */ 725 /* need a new stack */
711 next->stack = 0; 726 assert (!next->stack);
712 } 727 }
713 728
729 prev__cctx = prev->cctx;
730
731 /* possibly "free" the cctx */
732 if (prev__cctx->idle_sp == STACKLEVEL)
733 {
734 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
735 assert (PL_top_env == prev__cctx->top_env);
736
737 cctx_put (prev__cctx);
738 prev->cctx = 0;
739 }
740
714 if (!next->stack) 741 if (!next->cctx)
715 stack_get (next); 742 next->cctx = cctx_get ();
716 743
717 if (prev_stack != next->stack) 744 if (prev__cctx != next->cctx)
745 {
746 assert ( prev__cctx->inuse);
747 assert (!next->cctx->inuse);
748
749 prev__cctx->inuse = 0;
750 next->cctx->inuse = 1;
751
752 prev__cctx->top_env = PL_top_env;
753 PL_top_env = next->cctx->top_env;
718 coro_transfer (&prev_stack->cctx, &next->stack->cctx); 754 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
755 }
756
757 free_coro_mortal ();
758
759 UNLOCK;
719 } 760 }
720
721 transfer_tail ();
722} 761}
723 762
724/* use this function pointer to call the above function */ 763struct transfer_args
725/* this is done to increase chances of the compiler not inlining the call */ 764{
726void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; 765 struct coro *prev, *next;
766 int flags;
767};
768
769#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags)
727 770
728static void 771static void
729coro_state_destroy (struct coro *coro) 772coro_state_destroy (struct coro *coro)
730{ 773{
731 if (coro->refcnt--) 774 if (coro->refcnt--)
732 return; 775 return;
776
777 if (coro->flags & CF_RUNNING)
778 croak ("FATAL: tried to destroy currently running coroutine");
733 779
734 if (coro->mainstack && coro->mainstack != main_mainstack) 780 if (coro->mainstack && coro->mainstack != main_mainstack)
735 { 781 {
736 struct coro temp; 782 struct coro temp;
737 783
738 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 784 SAVE ((&temp), TRANSFER_SAVE_ALL);
739 LOAD (aTHX_ coro); 785 LOAD (coro);
740 786
741 destroy_stacks (aTHX); 787 coro_destroy_stacks ();
742 788
743 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 789 LOAD ((&temp)); /* this will get rid of defsv etc.. */
744 790
745 coro->mainstack = 0; 791 coro->mainstack = 0;
746 } 792 }
747 793
748 stack_free (coro->stack); 794 cctx_destroy (coro->cctx);
749 SvREFCNT_dec (coro->args); 795 SvREFCNT_dec (coro->args);
750 Safefree (coro); 796 Safefree (coro);
751} 797}
752 798
753static int 799static int
754coro_state_clear (SV *sv, MAGIC *mg) 800coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
755{ 801{
756 struct coro *coro = (struct coro *)mg->mg_ptr; 802 struct coro *coro = (struct coro *)mg->mg_ptr;
757 mg->mg_ptr = 0; 803 mg->mg_ptr = 0;
758 804
759 coro_state_destroy (coro); 805 coro_state_destroy (coro);
760 806
761 return 0; 807 return 0;
762} 808}
763 809
764static int 810static int
765coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 811coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
766{ 812{
767 struct coro *coro = (struct coro *)mg->mg_ptr; 813 struct coro *coro = (struct coro *)mg->mg_ptr;
768 814
769 ++coro->refcnt; 815 ++coro->refcnt;
770 816
771 return 0; 817 return 0;
772} 818}
773 819
774static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 820static MGVTBL coro_state_vtbl = {
821 0, 0, 0, 0,
822 coro_state_clear,
823 0,
824#ifdef MGf_DUP
825 coro_state_dup,
826#else
827# define MGf_DUP 0
828#endif
829};
775 830
776static struct coro * 831static struct coro *
777SvSTATE (SV *coro) 832SvSTATE (SV *coro)
778{ 833{
779 HV *stash; 834 HV *stash;
794 assert (mg->mg_type == PERL_MAGIC_ext); 849 assert (mg->mg_type == PERL_MAGIC_ext);
795 return (struct coro *)mg->mg_ptr; 850 return (struct coro *)mg->mg_ptr;
796} 851}
797 852
798static void 853static void
854prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags)
855{
856 ta->prev = SvSTATE (prev);
857 ta->next = SvSTATE (next);
858 ta->flags = flags;
859}
860
861static void
799api_transfer (pTHX_ SV *prev, SV *next, int flags) 862api_transfer (SV *prev, SV *next, int flags)
800{ 863{
801 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 864 dTHX;
865 struct transfer_args ta;
866
867 prepare_transfer (&ta, prev, next, flags);
868 TRANSFER (ta);
802} 869}
803 870
804/** Coro ********************************************************************/ 871/** Coro ********************************************************************/
805 872
806#define PRIO_MAX 3 873#define PRIO_MAX 3
814static GV *coro_current, *coro_idle; 881static GV *coro_current, *coro_idle;
815static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 882static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
816static int coro_nready; 883static int coro_nready;
817 884
818static void 885static void
819coro_enq (pTHX_ SV *sv) 886coro_enq (SV *coro_sv)
820{ 887{
821 int prio;
822
823 if (SvTYPE (sv) != SVt_PVHV)
824 croak ("Coro::ready tried to enqueue something that is not a coroutine");
825
826 prio = SvSTATE (sv)->prio;
827
828 av_push (coro_ready [prio - PRIO_MIN], sv); 888 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
829 coro_nready++; 889 coro_nready++;
830} 890}
831 891
832static SV * 892static SV *
833coro_deq (pTHX_ int min_prio) 893coro_deq (int min_prio)
834{ 894{
835 int prio = PRIO_MAX - PRIO_MIN; 895 int prio = PRIO_MAX - PRIO_MIN;
836 896
837 min_prio -= PRIO_MIN; 897 min_prio -= PRIO_MIN;
838 if (min_prio < 0) 898 if (min_prio < 0)
846 } 906 }
847 907
848 return 0; 908 return 0;
849} 909}
850 910
851static void 911static int
852api_ready (SV *coro) 912api_ready (SV *coro_sv)
853{ 913{
854 dTHX; 914 struct coro *coro;
855 915
856 if (SvROK (coro)) 916 if (SvROK (coro_sv))
857 coro = SvRV (coro); 917 coro_sv = SvRV (coro_sv);
918
919 coro = SvSTATE (coro_sv);
920
921 if (coro->flags & CF_READY)
922 return 0;
923
924 if (coro->flags & CF_RUNNING)
925 croak ("Coro::ready called on currently running coroutine");
926
927 coro->flags |= CF_READY;
858 928
859 LOCK; 929 LOCK;
860 coro_enq (aTHX_ SvREFCNT_inc (coro)); 930 coro_enq (SvREFCNT_inc (coro_sv));
861 UNLOCK; 931 UNLOCK;
862}
863 932
933 return 1;
934}
935
864static void 936static int
865api_schedule (void) 937api_is_ready (SV *coro_sv)
866{ 938{
867 dTHX; 939 return !!SvSTATE (coro_sv)->flags & CF_READY;
940}
868 941
942static void
943prepare_schedule (struct transfer_args *ta)
944{
869 SV *prev, *next; 945 SV *current, *prev, *next;
946
870 SV *current = GvSV (coro_current); 947 current = GvSV (coro_current);
871 948
872 for (;;) 949 for (;;)
873 { 950 {
874 LOCK; 951 LOCK;
875
876 next = coro_deq (aTHX_ PRIO_MIN); 952 next = coro_deq (PRIO_MIN);
953 UNLOCK;
877 954
878 if (next) 955 if (next)
879 break; 956 break;
880
881 UNLOCK;
882 957
883 { 958 {
884 dSP; 959 dSP;
885 960
886 ENTER; 961 ENTER;
897 972
898 prev = SvRV (current); 973 prev = SvRV (current);
899 SvRV (current) = next; 974 SvRV (current) = next;
900 975
901 /* free this only after the transfer */ 976 /* free this only after the transfer */
977 LOCK;
978 free_coro_mortal ();
979 UNLOCK;
902 coro_mortal = prev; 980 coro_mortal = prev;
903 981
904 UNLOCK; 982 ta->prev = SvSTATE (prev);
983 ta->next = SvSTATE (next);
984 ta->flags = TRANSFER_SAVE_ALL;
905 985
906 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), TRANSFER_SAVE_ALL); 986 ta->next->flags &= ~CF_READY;
907} 987}
908 988
909static int coro_cede_self; 989static void
990prepare_cede (struct transfer_args *ta)
991{
992 api_ready (GvSV (coro_current));
993
994 prepare_schedule (ta);
995}
996
997static void
998api_schedule (void)
999{
1000 dTHX;
1001 struct transfer_args ta;
1002
1003 prepare_schedule (&ta);
1004 TRANSFER (ta);
1005}
910 1006
911static void 1007static void
912api_cede (void) 1008api_cede (void)
913{ 1009{
914 dTHX; 1010 dTHX;
915 SV *current = SvREFCNT_inc (SvRV (GvSV (coro_current))); 1011 struct transfer_args ta;
916 1012
917 LOCK; 1013 prepare_cede (&ta);
918 1014 TRANSFER (ta);
919 if (coro_cede_self)
920 {
921 AV *runqueue = coro_ready [PRIO_MAX - PRIO_MIN];
922 av_unshift (runqueue, 1);
923 av_store (runqueue, 0, current);
924 coro_nready++;
925 coro_cede_self = 0;
926 }
927 else
928 coro_enq (aTHX_ current);
929
930 UNLOCK;
931
932 api_schedule ();
933} 1015}
934 1016
935MODULE = Coro::State PACKAGE = Coro::State 1017MODULE = Coro::State PACKAGE = Coro::State
936 1018
937PROTOTYPES: DISABLE 1019PROTOTYPES: DISABLE
952 main_mainstack = PL_mainstack; 1034 main_mainstack = PL_mainstack;
953 1035
954 coroapi.ver = CORO_API_VERSION; 1036 coroapi.ver = CORO_API_VERSION;
955 coroapi.transfer = api_transfer; 1037 coroapi.transfer = api_transfer;
956 1038
957 Newz (0, main_stack, 1, coro_stack);
958 main_stack->idle_sp = (void *)-1;
959
960 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1039 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
961} 1040}
962 1041
963SV * 1042SV *
964new (char *klass, ...) 1043new (char *klass, ...)
973 1052
974 hv = newHV (); 1053 hv = newHV ();
975 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1054 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
976 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1055 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
977 1056
978 av_push (coro->args, newSVsv (RETVAL));
979 for (i = 1; i < items; i++) 1057 for (i = 1; i < items; i++)
980 av_push (coro->args, newSVsv (ST (i))); 1058 av_push (coro->args, newSVsv (ST (i)));
981
982 coro->stack = main_stack;
983 /*coro->mainstack = 0; *//*actual work is done inside transfer */
984 /*coro->stack = 0;*/
985} 1059}
986 OUTPUT: 1060 OUTPUT:
987 RETVAL 1061 RETVAL
988 1062
989void 1063void
990transfer (prev, next, flags) 1064_set_stacklevel (...)
991 SV *prev 1065 ALIAS:
992 SV *next 1066 Coro::State::transfer = 1
993 int flags 1067 Coro::schedule = 2
1068 Coro::cede = 3
1069 Coro::Cont::yield = 4
994 CODE: 1070 CODE:
995 PUTBACK;
996 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
997 SPAGAIN;
998
999int
1000prio (Coro::State coro, int newprio = 0)
1001 ALIAS:
1002 nice = 1
1003 CODE:
1004{ 1071{
1005 RETVAL = coro->prio; 1072 struct transfer_args ta;
1006 1073
1007 if (items > 1) 1074 switch (ix)
1008 { 1075 {
1076 case 0:
1077 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1078 ta.next = 0;
1079 ta.flags = TRANSFER_SET_STACKLEVEL;
1080 break;
1081
1082 case 1:
1009 if (ix) 1083 if (items != 3)
1010 newprio += coro->prio; 1084 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items);
1011 1085
1012 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1086 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1013 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1087 break;
1014 1088
1015 coro->prio = newprio; 1089 case 2:
1090 prepare_schedule (&ta);
1091 break;
1092
1093 case 3:
1094 prepare_cede (&ta);
1095 break;
1096
1097 case 4:
1098 {
1099 SV *yieldstack;
1100 SV *sv;
1101 AV *defav = GvAV (PL_defgv);
1102
1103 yieldstack = *hv_fetch (
1104 (HV *)SvRV (GvSV (coro_current)),
1105 "yieldstack", sizeof ("yieldstack") - 1,
1106 0
1107 );
1108
1109 /* set up @_ -- ugly */
1110 av_clear (defav);
1111 av_fill (defav, items - 1);
1112 while (items--)
1113 av_store (defav, items, SvREFCNT_inc (ST(items)));
1114
1115 sv = av_pop ((AV *)SvRV (yieldstack));
1116 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1117 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1118 ta.flags = 0;
1119 SvREFCNT_dec (sv);
1120 }
1121 break;
1122
1016 } 1123 }
1017}
1018 1124
1019void 1125 TRANSFER (ta);
1020_clear_idle_sp (Coro::State self) 1126}
1021 CODE:
1022 self->stack->idle_sp = 0;
1023 1127
1024void 1128void
1025_clone_state_from (SV *dst, SV *src) 1129_clone_state_from (SV *dst, SV *src)
1026 CODE: 1130 CODE:
1027{ 1131{
1038 int code 1142 int code
1039 PROTOTYPE: $ 1143 PROTOTYPE: $
1040 CODE: 1144 CODE:
1041 _exit (code); 1145 _exit (code);
1042 1146
1043MODULE = Coro::State PACKAGE = Coro::Cont 1147int
1148cctx_count ()
1149 CODE:
1150 RETVAL = cctx_count;
1151 OUTPUT:
1152 RETVAL
1044 1153
1045void 1154int
1046yield (...) 1155cctx_idle ()
1047 PROTOTYPE: @ 1156 CODE:
1048 CODE: 1157 RETVAL = cctx_idle;
1049{ 1158 OUTPUT:
1050 SV *yieldstack; 1159 RETVAL
1051 SV *sv;
1052 AV *defav = GvAV (PL_defgv);
1053 struct coro *prev, *next;
1054
1055 yieldstack = *hv_fetch (
1056 (HV *)SvRV (GvSV (coro_current)),
1057 "yieldstack", sizeof ("yieldstack") - 1,
1058 0
1059 );
1060
1061 /* set up @_ -- ugly */
1062 av_clear (defav);
1063 av_fill (defav, items - 1);
1064 while (items--)
1065 av_store (defav, items, SvREFCNT_inc (ST(items)));
1066
1067 sv = av_pop ((AV *)SvRV (yieldstack));
1068 prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1069 next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1070 SvREFCNT_dec (sv);
1071
1072 coro_state_transfer (aTHX_ prev, next, 0);
1073}
1074 1160
1075MODULE = Coro::State PACKAGE = Coro 1161MODULE = Coro::State PACKAGE = Coro
1076 1162
1077BOOT: 1163BOOT:
1078{ 1164{
1097 SV *sv = perl_get_sv("Coro::API", 1); 1183 SV *sv = perl_get_sv("Coro::API", 1);
1098 1184
1099 coroapi.schedule = api_schedule; 1185 coroapi.schedule = api_schedule;
1100 coroapi.cede = api_cede; 1186 coroapi.cede = api_cede;
1101 coroapi.ready = api_ready; 1187 coroapi.ready = api_ready;
1188 coroapi.is_ready = api_is_ready;
1102 coroapi.nready = &coro_nready; 1189 coroapi.nready = &coro_nready;
1103 coroapi.current = coro_current; 1190 coroapi.current = coro_current;
1104 1191
1105 GCoroAPI = &coroapi; 1192 GCoroAPI = &coroapi;
1106 sv_setiv (sv, (IV)&coroapi); 1193 sv_setiv (sv, (IV)&coroapi);
1107 SvREADONLY_on (sv); 1194 SvREADONLY_on (sv);
1108 } 1195 }
1109} 1196}
1110 1197
1111void 1198int
1199prio (Coro::State coro, int newprio = 0)
1200 ALIAS:
1201 nice = 1
1202 CODE:
1203{
1204 RETVAL = coro->prio;
1205
1206 if (items > 1)
1207 {
1208 if (ix)
1209 newprio += coro->prio;
1210
1211 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1212 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1213
1214 coro->prio = newprio;
1215 }
1216}
1217
1218SV *
1112ready (SV *self) 1219ready (SV *self)
1113 PROTOTYPE: $ 1220 PROTOTYPE: $
1114 CODE: 1221 CODE:
1115 api_ready (self); 1222 RETVAL = boolSV (api_ready (self));
1223 OUTPUT:
1224 RETVAL
1225
1226SV *
1227is_ready (SV *self)
1228 PROTOTYPE: $
1229 CODE:
1230 RETVAL = boolSV (api_is_ready (self));
1231 OUTPUT:
1232 RETVAL
1116 1233
1117int 1234int
1118nready (...) 1235nready (...)
1119 PROTOTYPE: 1236 PROTOTYPE:
1120 CODE: 1237 CODE:
1121 RETVAL = coro_nready; 1238 RETVAL = coro_nready;
1122 OUTPUT: 1239 OUTPUT:
1123 RETVAL 1240 RETVAL
1124 1241
1125void
1126schedule (...)
1127 PROTOTYPE:
1128 CODE:
1129 api_schedule ();
1130
1131void
1132_set_cede_self ()
1133 CODE:
1134 coro_cede_self = 1;
1135
1136void
1137cede (...)
1138 PROTOTYPE:
1139 CODE:
1140 api_cede ();
1141
1142MODULE = Coro::State PACKAGE = Coro::AIO 1242MODULE = Coro::State PACKAGE = Coro::AIO
1143 1243
1144SV * 1244SV *
1145_get_state () 1245_get_state ()
1146 CODE: 1246 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines