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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines