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.363 by root, Tue Jul 14 00:55:10 2009 UTC vs.
Revision 1.442 by root, Tue Mar 4 06:13:24 2014 UTC

1/* this works around a bug in mingw32 providing a non-working setjmp */
2#define USE_NO_MINGW_SETJMP_TWO_ARGS
3
4#define NDEBUG 1 /* perl usually disables NDEBUG later */
5
1#include "libcoro/coro.c" 6#include "libcoro/coro.c"
2 7
3#define PERL_NO_GET_CONTEXT 8#define PERL_NO_GET_CONTEXT
4#define PERL_EXT 9#define PERL_EXT
5 10
8#include "XSUB.h" 13#include "XSUB.h"
9#include "perliol.h" 14#include "perliol.h"
10 15
11#include "schmorp.h" 16#include "schmorp.h"
12 17
18#define ECB_NO_THREADS 1
19#define ECB_NO_LIBM 1
20#include "ecb.h"
21
22#include <stddef.h>
13#include <stdio.h> 23#include <stdio.h>
14#include <errno.h> 24#include <errno.h>
15#include <assert.h> 25#include <assert.h>
16 26
17#ifdef WIN32 27#ifndef SVs_PADSTALE
28# define SVs_PADSTALE 0
29#endif
30
31#ifdef PadARRAY
32# define NEWPADAPI 1
33# define newPADLIST(var) (Newz (0, var, 1, PADLIST), Newx (PadlistARRAY (var), 2, PAD *))
34#else
35typedef AV PADNAMELIST;
36# if !PERL_VERSION_ATLEAST(5,8,0)
37typedef AV PADLIST;
38typedef AV PAD;
39# endif
40# define PadlistARRAY(pl) ((PAD **)AvARRAY (pl))
41# define PadlistMAX(pl) AvFILLp (pl)
42# define PadlistNAMES(pl) (*PadlistARRAY (pl))
43# define PadARRAY AvARRAY
44# define PadMAX AvFILLp
45# define newPADLIST(var) ((var) = newAV (), av_extend (var, 1))
46#endif
47
48/* 5.19.something has replaced SVt_BIND by SVt_INVLIST */
49/* we just alias it to SVt_IV, as that is sufficient for swap_sv for now */
50#if PERL_VERSION_ATLEAST(5,19,0)
51# define SVt_BIND SVt_IV
52#endif
53
54#if defined(_WIN32)
55# undef HAS_GETTIMEOFDAY
18# undef setjmp 56# undef setjmp
19# undef longjmp 57# undef longjmp
20# undef _exit 58# undef _exit
21# define setjmp _setjmp /* deep magic */ 59# define setjmp _setjmp /* deep magic */
22#else 60#else
23# include <inttypes.h> /* most portable stdint.h */ 61# include <inttypes.h> /* most portable stdint.h */
24#endif 62#endif
25 63
26#ifdef HAVE_MMAP
27# include <unistd.h>
28# include <sys/mman.h>
29# ifndef MAP_ANONYMOUS
30# ifdef MAP_ANON
31# define MAP_ANONYMOUS MAP_ANON
32# else
33# undef HAVE_MMAP
34# endif
35# endif
36# include <limits.h>
37# ifndef PAGESIZE
38# define PAGESIZE pagesize
39# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
40static long pagesize;
41# else
42# define BOOT_PAGESIZE (void)0
43# endif
44#else
45# define PAGESIZE 0
46# define BOOT_PAGESIZE (void)0
47#endif
48
49#if CORO_USE_VALGRIND
50# include <valgrind/valgrind.h>
51#endif
52
53/* the maximum number of idle cctx that will be pooled */ 64/* the maximum number of idle cctx that will be pooled */
54static int cctx_max_idle = 4; 65static int cctx_max_idle = 4;
55 66
56#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 67#if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0)
57# undef CORO_STACKGUARD 68# define HAS_SCOPESTACK_NAME 1
58#endif
59
60#ifndef CORO_STACKGUARD
61# define CORO_STACKGUARD 0
62#endif 69#endif
63 70
64/* prefer perl internal functions over our own? */ 71/* prefer perl internal functions over our own? */
65#ifndef CORO_PREFER_PERL_FUNCTIONS 72#ifndef CORO_PREFER_PERL_FUNCTIONS
66# define CORO_PREFER_PERL_FUNCTIONS 0 73# define CORO_PREFER_PERL_FUNCTIONS 0
76# define STACKLEVEL ((void *)&stacklevel) 83# define STACKLEVEL ((void *)&stacklevel)
77#endif 84#endif
78 85
79#define IN_DESTRUCT PL_dirty 86#define IN_DESTRUCT PL_dirty
80 87
81#if __GNUC__ >= 3
82# define attribute(x) __attribute__(x)
83# define expect(expr,value) __builtin_expect ((expr), (value))
84# define INLINE static inline
85#else
86# define attribute(x)
87# define expect(expr,value) (expr)
88# define INLINE static
89#endif
90
91#define expect_false(expr) expect ((expr) != 0, 0)
92#define expect_true(expr) expect ((expr) != 0, 1)
93
94#define NOINLINE attribute ((noinline))
95
96#include "CoroAPI.h" 88#include "CoroAPI.h"
97#define GCoroAPI (&coroapi) /* very sneaky */ 89#define GCoroAPI (&coroapi) /* very sneaky */
98 90
99#ifdef USE_ITHREADS 91#ifdef USE_ITHREADS
100# if CORO_PTHREAD 92# if CORO_PTHREAD
116static void (*u2time)(pTHX_ UV ret[2]); 108static void (*u2time)(pTHX_ UV ret[2]);
117 109
118/* we hijack an hopefully unused CV flag for our purposes */ 110/* we hijack an hopefully unused CV flag for our purposes */
119#define CVf_SLF 0x4000 111#define CVf_SLF 0x4000
120static OP *pp_slf (pTHX); 112static OP *pp_slf (pTHX);
113static void slf_destroy (pTHX_ struct coro *coro);
121 114
122static U32 cctx_gen; 115static U32 cctx_gen;
123static size_t cctx_stacksize = CORO_STACKSIZE; 116static size_t cctx_stacksize = CORO_STACKSIZE;
124static struct CoroAPI coroapi; 117static struct CoroAPI coroapi;
125static AV *main_mainstack; /* used to differentiate between $main and others */ 118static AV *main_mainstack; /* used to differentiate between $main and others */
142static SV *sv_pool_size; 135static SV *sv_pool_size;
143static SV *sv_async_pool_idle; /* description string */ 136static SV *sv_async_pool_idle; /* description string */
144static AV *av_async_pool; /* idle pool */ 137static AV *av_async_pool; /* idle pool */
145static SV *sv_Coro; /* class string */ 138static SV *sv_Coro; /* class string */
146static CV *cv_pool_handler; 139static CV *cv_pool_handler;
147static CV *cv_coro_state_new;
148 140
149/* Coro::AnyEvent */ 141/* Coro::AnyEvent */
150static SV *sv_activity; 142static SV *sv_activity;
151 143
152/* enable processtime/realtime profiling */ 144/* enable processtime/realtime profiling */
156static char times_valid; 148static char times_valid;
157 149
158static struct coro_cctx *cctx_first; 150static struct coro_cctx *cctx_first;
159static int cctx_count, cctx_idle; 151static int cctx_count, cctx_idle;
160 152
161enum { 153enum
154{
162 CC_MAPPED = 0x01, 155 CC_MAPPED = 0x01,
163 CC_NOREUSE = 0x02, /* throw this away after tracing */ 156 CC_NOREUSE = 0x02, /* throw this away after tracing */
164 CC_TRACE = 0x04, 157 CC_TRACE = 0x04,
165 CC_TRACE_SUB = 0x08, /* trace sub calls */ 158 CC_TRACE_SUB = 0x08, /* trace sub calls */
166 CC_TRACE_LINE = 0x10, /* trace each statement */ 159 CC_TRACE_LINE = 0x10, /* trace each statement */
171typedef struct coro_cctx 164typedef struct coro_cctx
172{ 165{
173 struct coro_cctx *next; 166 struct coro_cctx *next;
174 167
175 /* the stack */ 168 /* the stack */
176 void *sptr; 169 struct coro_stack stack;
177 size_t ssize;
178 170
179 /* cpu state */ 171 /* cpu state */
180 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 172 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
173#ifndef NDEBUG
181 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 174 JMPENV *idle_te; /* same as idle_sp, but for top_env */
175#endif
182 JMPENV *top_env; 176 JMPENV *top_env;
183 coro_context cctx; 177 coro_context cctx;
184 178
185 U32 gen; 179 U32 gen;
186#if CORO_USE_VALGRIND 180#if CORO_USE_VALGRIND
187 int valgrind_id; 181 int valgrind_id;
188#endif 182#endif
189 unsigned char flags; 183 unsigned char flags;
190} coro_cctx; 184} coro_cctx;
191 185
192coro_cctx *cctx_current; /* the currently running cctx */ 186static coro_cctx *cctx_current; /* the currently running cctx */
193 187
194/*****************************************************************************/ 188/*****************************************************************************/
195 189
190static MGVTBL coro_state_vtbl;
191
196enum { 192enum
193{
197 CF_RUNNING = 0x0001, /* coroutine is running */ 194 CF_RUNNING = 0x0001, /* coroutine is running */
198 CF_READY = 0x0002, /* coroutine is ready */ 195 CF_READY = 0x0002, /* coroutine is ready */
199 CF_NEW = 0x0004, /* has never been switched to */ 196 CF_NEW = 0x0004, /* has never been switched to */
200 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 197 CF_ZOMBIE = 0x0008, /* coroutine data has been freed */
201 CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */ 198 CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */
199 CF_NOCANCEL = 0x0020, /* cannot cancel, set slf_frame.data to 1 (hackish) */
202}; 200};
203 201
204/* the structure where most of the perl state is stored, overlaid on the cxstack */ 202/* the structure where most of the perl state is stored, overlaid on the cxstack */
205typedef struct 203typedef struct
206{ 204{
207 SV *defsv;
208 AV *defav;
209 SV *errsv;
210 SV *irsgv;
211 HV *hinthv;
212#define VAR(name,type) type name; 205 #define VARx(name,expr,type) type name;
213# include "state.h" 206 #include "state.h"
214#undef VAR
215} perl_slots; 207} perl_slots;
216 208
209/* how many context stack entries do we need for perl_slots */
217#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) 210#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
218 211
219/* this is a structure representing a perl-level coroutine */ 212/* this is a structure representing a perl-level coroutine */
220struct coro { 213struct coro
214{
221 /* the C coroutine allocated to this perl coroutine, if any */ 215 /* the C coroutine allocated to this perl coroutine, if any */
222 coro_cctx *cctx; 216 coro_cctx *cctx;
223 217
224 /* ready queue */ 218 /* ready queue */
225 struct coro *next_ready; 219 struct coro *next_ready;
227 /* state data */ 221 /* state data */
228 struct CoroSLF slf_frame; /* saved slf frame */ 222 struct CoroSLF slf_frame; /* saved slf frame */
229 AV *mainstack; 223 AV *mainstack;
230 perl_slots *slot; /* basically the saved sp */ 224 perl_slots *slot; /* basically the saved sp */
231 225
232 CV *startcv; /* the CV to execute */ 226 CV *startcv; /* the CV to execute */
233 AV *args; /* data associated with this coroutine (initial args) */ 227 AV *args; /* data associated with this coroutine (initial args) */
234 int refcnt; /* coroutines are refcounted, yes */
235 int flags; /* CF_ flags */ 228 int flags; /* CF_ flags */
236 HV *hv; /* the perl hash associated with this coro, if any */ 229 HV *hv; /* the perl hash associated with this coro, if any */
237 void (*on_destroy)(pTHX_ struct coro *coro);
238 230
239 /* statistics */ 231 /* statistics */
240 int usecount; /* number of transfers to this coro */ 232 int usecount; /* number of transfers to this coro */
241 233
242 /* coro process data */ 234 /* coro process data */
243 int prio; 235 int prio;
244 SV *except; /* exception to be thrown */ 236 SV *except; /* exception to be thrown */
245 SV *rouse_cb; 237 SV *rouse_cb; /* last rouse callback */
238 AV *on_destroy; /* callbacks or coros to notify on destroy */
239 AV *status; /* the exit status list */
246 240
247 /* async_pool */ 241 /* async_pool */
248 SV *saved_deffh; 242 SV *saved_deffh;
249 SV *invoke_cb; 243 SV *invoke_cb;
250 AV *invoke_av; 244 AV *invoke_av;
251 245
252 /* on_enter/on_leave */ 246 /* on_enter/on_leave */
253 AV *on_enter; 247 AV *on_enter;
254 AV *on_leave; 248 AV *on_leave;
255 249
250 /* swap_sv */
251 AV *swap_sv;
252
256 /* times */ 253 /* times */
257 coro_ts t_cpu, t_real; 254 coro_ts t_cpu, t_real;
258 255
259 /* linked list */ 256 /* linked list */
260 struct coro *next, *prev; 257 struct coro *next, *prev;
263typedef struct coro *Coro__State; 260typedef struct coro *Coro__State;
264typedef struct coro *Coro__State_or_hashref; 261typedef struct coro *Coro__State_or_hashref;
265 262
266/* the following variables are effectively part of the perl context */ 263/* the following variables are effectively part of the perl context */
267/* and get copied between struct coro and these variables */ 264/* and get copied between struct coro and these variables */
268/* the mainr easonw e don't support windows process emulation */ 265/* the main reason we don't support windows process emulation */
269static struct CoroSLF slf_frame; /* the current slf frame */ 266static struct CoroSLF slf_frame; /* the current slf frame */
270 267
271/** Coro ********************************************************************/ 268/** Coro ********************************************************************/
272 269
273#define CORO_PRIO_MAX 3 270#define CORO_PRIO_MAX 3
279 276
280/* for Coro.pm */ 277/* for Coro.pm */
281static SV *coro_current; 278static SV *coro_current;
282static SV *coro_readyhook; 279static SV *coro_readyhook;
283static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ 280static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
284static CV *cv_coro_run, *cv_coro_terminate; 281static CV *cv_coro_run;
285static struct coro *coro_first; 282static struct coro *coro_first;
286#define coro_nready coroapi.nready 283#define coro_nready coroapi.nready
287 284
285/** JIT *********************************************************************/
286
287#if CORO_JIT
288 /* APPLE doesn't have mmap though */
289 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__)
290 #ifndef CORO_JIT_TYPE
291 #if ECB_AMD64 && CORO_JIT_UNIXY
292 #define CORO_JIT_TYPE "amd64-unix"
293 #elif __i386 && CORO_JIT_UNIXY
294 #define CORO_JIT_TYPE "x86-unix"
295 #endif
296 #endif
297#endif
298
299#if !defined(CORO_JIT_TYPE) || _POSIX_MEMORY_PROTECTION <= 0
300 #undef CORO_JIT
301#endif
302
303#if CORO_JIT
304 typedef void (*load_save_perl_slots_type)(perl_slots *);
305 static load_save_perl_slots_type load_perl_slots, save_perl_slots;
306#endif
307
308/** Coro::Select ************************************************************/
309
310static OP *(*coro_old_pp_sselect) (pTHX);
311static SV *coro_select_select;
312
313/* horrible hack, but if it works... */
314static OP *
315coro_pp_sselect (pTHX)
316{
317 dSP;
318 PUSHMARK (SP - 4); /* fake argument list */
319 XPUSHs (coro_select_select);
320 PUTBACK;
321
322 /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */
323 PL_op->op_flags |= OPf_STACKED;
324 PL_op->op_private = 0;
325 return PL_ppaddr [OP_ENTERSUB](aTHX);
326}
327
328/** time stuff **************************************************************/
329
330#ifdef HAS_GETTIMEOFDAY
331
332ecb_inline void
333coro_u2time (pTHX_ UV ret[2])
334{
335 struct timeval tv;
336 gettimeofday (&tv, 0);
337
338 ret [0] = tv.tv_sec;
339 ret [1] = tv.tv_usec;
340}
341
342ecb_inline double
343coro_nvtime (void)
344{
345 struct timeval tv;
346 gettimeofday (&tv, 0);
347
348 return tv.tv_sec + tv.tv_usec * 1e-6;
349}
350
351ecb_inline void
352time_init (pTHX)
353{
354 nvtime = coro_nvtime;
355 u2time = coro_u2time;
356}
357
358#else
359
360ecb_inline void
361time_init (pTHX)
362{
363 SV **svp;
364
365 require_pv ("Time/HiRes.pm");
366
367 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
368
369 if (!svp) croak ("Time::HiRes is required, but missing. Caught");
370 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught");
371
372 nvtime = INT2PTR (double (*)(), SvIV (*svp));
373
374 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
375 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
376}
377
378#endif
379
288/** lowlevel stuff **********************************************************/ 380/** lowlevel stuff **********************************************************/
289 381
290static SV * 382static SV * ecb_noinline
291coro_get_sv (pTHX_ const char *name, int create) 383coro_get_sv (pTHX_ const char *name, int create)
292{ 384{
293#if PERL_VERSION_ATLEAST (5,10,0) 385#if PERL_VERSION_ATLEAST (5,10,0)
294 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 386 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
295 get_sv (name, create); 387 get_sv (name, create);
296#endif 388#endif
297 return get_sv (name, create); 389 return get_sv (name, create);
298} 390}
299 391
300static AV * 392static AV * ecb_noinline
301coro_get_av (pTHX_ const char *name, int create) 393coro_get_av (pTHX_ const char *name, int create)
302{ 394{
303#if PERL_VERSION_ATLEAST (5,10,0) 395#if PERL_VERSION_ATLEAST (5,10,0)
304 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 396 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
305 get_av (name, create); 397 get_av (name, create);
306#endif 398#endif
307 return get_av (name, create); 399 return get_av (name, create);
308} 400}
309 401
310static HV * 402static HV * ecb_noinline
311coro_get_hv (pTHX_ const char *name, int create) 403coro_get_hv (pTHX_ const char *name, int create)
312{ 404{
313#if PERL_VERSION_ATLEAST (5,10,0) 405#if PERL_VERSION_ATLEAST (5,10,0)
314 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 406 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
315 get_hv (name, create); 407 get_hv (name, create);
316#endif 408#endif
317 return get_hv (name, create); 409 return get_hv (name, create);
318} 410}
319 411
320INLINE void 412ecb_inline void
321coro_times_update () 413coro_times_update (void)
322{ 414{
323#ifdef coro_clock_gettime 415#ifdef coro_clock_gettime
324 struct timespec ts; 416 struct timespec ts;
325 417
326 ts.tv_sec = ts.tv_nsec = 0; 418 ts.tv_sec = ts.tv_nsec = 0;
338 time_real [0] = tv [0]; 430 time_real [0] = tv [0];
339 time_real [1] = tv [1] * 1000; 431 time_real [1] = tv [1] * 1000;
340#endif 432#endif
341} 433}
342 434
343INLINE void 435ecb_inline void
344coro_times_add (struct coro *c) 436coro_times_add (struct coro *c)
345{ 437{
346 c->t_real [1] += time_real [1]; 438 c->t_real [1] += time_real [1];
347 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } 439 if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; }
348 c->t_real [0] += time_real [0]; 440 c->t_real [0] += time_real [0];
350 c->t_cpu [1] += time_cpu [1]; 442 c->t_cpu [1] += time_cpu [1];
351 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } 443 if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; }
352 c->t_cpu [0] += time_cpu [0]; 444 c->t_cpu [0] += time_cpu [0];
353} 445}
354 446
355INLINE void 447ecb_inline void
356coro_times_sub (struct coro *c) 448coro_times_sub (struct coro *c)
357{ 449{
358 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } 450 if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; }
359 c->t_real [1] -= time_real [1]; 451 c->t_real [1] -= time_real [1];
360 c->t_real [0] -= time_real [0]; 452 c->t_real [0] -= time_real [0];
368/* magic glue */ 460/* magic glue */
369 461
370#define CORO_MAGIC_type_cv 26 462#define CORO_MAGIC_type_cv 26
371#define CORO_MAGIC_type_state PERL_MAGIC_ext 463#define CORO_MAGIC_type_state PERL_MAGIC_ext
372 464
373#define CORO_MAGIC_NN(sv, type) \ 465#define CORO_MAGIC_NN(sv, type) \
374 (expect_true (SvMAGIC (sv)->mg_type == type) \ 466 (ecb_expect_true (SvMAGIC (sv)->mg_type == type) \
375 ? SvMAGIC (sv) \ 467 ? SvMAGIC (sv) \
376 : mg_find (sv, type)) 468 : mg_find (sv, type))
377 469
378#define CORO_MAGIC(sv, type) \ 470#define CORO_MAGIC(sv, type) \
379 (expect_true (SvMAGIC (sv)) \ 471 (ecb_expect_true (SvMAGIC (sv)) \
380 ? CORO_MAGIC_NN (sv, type) \ 472 ? CORO_MAGIC_NN (sv, type) \
381 : 0) 473 : 0)
382 474
383#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 475#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
384#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) 476#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
385 477
386INLINE struct coro * 478ecb_inline MAGIC *
479SvSTATEhv_p (pTHX_ SV *coro)
480{
481 MAGIC *mg;
482
483 if (ecb_expect_true (
484 SvTYPE (coro) == SVt_PVHV
485 && (mg = CORO_MAGIC_state (coro))
486 && mg->mg_virtual == &coro_state_vtbl
487 ))
488 return mg;
489
490 return 0;
491}
492
493ecb_inline struct coro *
387SvSTATE_ (pTHX_ SV *coro) 494SvSTATE_ (pTHX_ SV *coro)
388{ 495{
389 HV *stash;
390 MAGIC *mg; 496 MAGIC *mg;
391 497
392 if (SvROK (coro)) 498 if (SvROK (coro))
393 coro = SvRV (coro); 499 coro = SvRV (coro);
394 500
395 if (expect_false (SvTYPE (coro) != SVt_PVHV)) 501 mg = SvSTATEhv_p (aTHX_ coro);
502 if (!mg)
396 croak ("Coro::State object required"); 503 croak ("Coro::State object required");
397 504
398 stash = SvSTASH (coro);
399 if (expect_false (stash != coro_stash && stash != coro_state_stash))
400 {
401 /* very slow, but rare, check */
402 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
403 croak ("Coro::State object required");
404 }
405
406 mg = CORO_MAGIC_state (coro);
407 return (struct coro *)mg->mg_ptr; 505 return (struct coro *)mg->mg_ptr;
408} 506}
409 507
410#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) 508#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
411 509
414#define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) 512#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
415 513
416/*****************************************************************************/ 514/*****************************************************************************/
417/* padlist management and caching */ 515/* padlist management and caching */
418 516
419static AV * 517ecb_inline PADLIST *
420coro_derive_padlist (pTHX_ CV *cv) 518coro_derive_padlist (pTHX_ CV *cv)
421{ 519{
422 AV *padlist = CvPADLIST (cv); 520 PADLIST *padlist = CvPADLIST (cv);
423 AV *newpadlist, *newpad; 521 PADLIST *newpadlist;
522 PAD *newpad;
523 PADOFFSET off = PadlistMAX (padlist) + 1;
424 524
425 newpadlist = newAV (); 525#if NEWPADAPI
526
527 /* if we had the original CvDEPTH, we might be able to steal the CvDEPTH+1 entry instead */
528 /* 20131102194744.GA6705@schmorp.de, 20131102195825.2013.qmail@lists-nntp.develooper.com */
529 while (!PadlistARRAY (padlist)[off - 1])
530 --off;
531
532 Perl_pad_push (aTHX_ padlist, off);
533
534 newpad = PadlistARRAY (padlist)[off];
535 PadlistARRAY (padlist)[off] = 0;
536
537#else
538
539#if PERL_VERSION_ATLEAST (5,10,0)
540 Perl_pad_push (aTHX_ padlist, off);
541#else
542 Perl_pad_push (aTHX_ padlist, off, 1);
543#endif
544
545 newpad = PadlistARRAY (padlist)[off];
546 PadlistMAX (padlist) = off - 1;
547
548#endif
549
550 newPADLIST (newpadlist);
551#if !PERL_VERSION_ATLEAST(5,15,3)
552 /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */
426 AvREAL_off (newpadlist); 553 AvREAL_off (newpadlist);
427#if PERL_VERSION_ATLEAST (5,10,0)
428 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
429#else
430 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
431#endif 554#endif
432 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
433 --AvFILLp (padlist);
434 555
435 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); 556 /* Already extended to 2 elements by newPADLIST. */
436 av_store (newpadlist, 1, (SV *)newpad); 557 PadlistMAX (newpadlist) = 1;
558 PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist));
559 PadlistARRAY (newpadlist)[1] = newpad;
437 560
438 return newpadlist; 561 return newpadlist;
439} 562}
440 563
441static void 564ecb_inline void
442free_padlist (pTHX_ AV *padlist) 565free_padlist (pTHX_ PADLIST *padlist)
443{ 566{
444 /* may be during global destruction */ 567 /* may be during global destruction */
445 if (!IN_DESTRUCT) 568 if (!IN_DESTRUCT)
446 { 569 {
447 I32 i = AvFILLp (padlist); 570 I32 i = PadlistMAX (padlist);
448 571
449 while (i > 0) /* special-case index 0 */ 572 while (i > 0) /* special-case index 0 */
450 { 573 {
451 /* we try to be extra-careful here */ 574 /* we try to be extra-careful here */
452 AV *av = (AV *)AvARRAY (padlist)[i--]; 575 PAD *pad = PadlistARRAY (padlist)[i--];
453 I32 j = AvFILLp (av);
454 576
577 if (pad)
578 {
579 I32 j = PadMAX (pad);
580
455 while (j >= 0) 581 while (j >= 0)
456 SvREFCNT_dec (AvARRAY (av)[j--]); 582 SvREFCNT_dec (PadARRAY (pad)[j--]);
457 583
458 AvFILLp (av) = -1; 584 PadMAX (pad) = -1;
459 SvREFCNT_dec (av); 585 SvREFCNT_dec (pad);
586 }
460 } 587 }
461 588
462 SvREFCNT_dec (AvARRAY (padlist)[0]); 589 SvREFCNT_dec (PadlistNAMES (padlist));
463 590
591#if NEWPADAPI
592 Safefree (PadlistARRAY (padlist));
593 Safefree (padlist);
594#else
464 AvFILLp (padlist) = -1; 595 AvFILLp (padlist) = -1;
596 AvREAL_off (padlist);
465 SvREFCNT_dec ((SV*)padlist); 597 SvREFCNT_dec ((SV*)padlist);
598#endif
466 } 599 }
467} 600}
468 601
469static int 602static int
470coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 603coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
471{ 604{
472 AV *padlist; 605 PADLIST *padlist;
473 AV *av = (AV *)mg->mg_obj; 606 PADLIST **padlists = (PADLIST **)(mg->mg_ptr + sizeof(size_t));
607 size_t len = *(size_t *)mg->mg_ptr;
474 608
475 /* perl manages to free our internal AV and _then_ call us */ 609 /* perl manages to free our internal AV and _then_ call us */
476 if (IN_DESTRUCT) 610 if (IN_DESTRUCT)
477 return 0; 611 return 0;
478 612
479 /* casting is fun. */ 613 while (len--)
480 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
481 free_padlist (aTHX_ padlist); 614 free_padlist (aTHX_ padlists[len]);
482
483 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
484 615
485 return 0; 616 return 0;
486} 617}
487 618
488static MGVTBL coro_cv_vtbl = { 619static MGVTBL coro_cv_vtbl = {
489 0, 0, 0, 0, 620 0, 0, 0, 0,
490 coro_cv_free 621 coro_cv_free
491}; 622};
492 623
493/* the next two functions merely cache the padlists */ 624/* the next two functions merely cache the padlists */
494static void 625ecb_inline void
495get_padlist (pTHX_ CV *cv) 626get_padlist (pTHX_ CV *cv)
496{ 627{
497 MAGIC *mg = CORO_MAGIC_cv (cv); 628 MAGIC *mg = CORO_MAGIC_cv (cv);
498 AV *av; 629 size_t *lenp;
499 630
500 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 631 if (ecb_expect_true (mg && *(lenp = (size_t *)mg->mg_ptr)))
501 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 632 CvPADLIST (cv) = ((PADLIST **)(mg->mg_ptr + sizeof(size_t)))[--*lenp];
502 else 633 else
503 { 634 {
504#if CORO_PREFER_PERL_FUNCTIONS 635#if CORO_PREFER_PERL_FUNCTIONS
505 /* this is probably cleaner? but also slower! */ 636 /* this is probably cleaner? but also slower! */
506 /* in practise, it seems to be less stable */ 637 /* in practise, it seems to be less stable */
512 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); 643 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
513#endif 644#endif
514 } 645 }
515} 646}
516 647
517static void 648ecb_inline void
518put_padlist (pTHX_ CV *cv) 649put_padlist (pTHX_ CV *cv)
519{ 650{
520 MAGIC *mg = CORO_MAGIC_cv (cv); 651 MAGIC *mg = CORO_MAGIC_cv (cv);
521 AV *av;
522 652
523 if (expect_false (!mg)) 653 if (ecb_expect_false (!mg))
654 {
524 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 655 mg = sv_magicext ((SV *)cv, 0, CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
656 Newz (0, mg->mg_ptr ,sizeof (size_t) + sizeof (PADLIST *), char);
657 mg->mg_len = 1; /* so mg_free frees mg_ptr */
658 }
659 else
660 Renew (mg->mg_ptr,
661 sizeof(size_t) + (*(size_t *)mg->mg_ptr + 1) * sizeof(PADLIST *),
662 char);
525 663
526 av = (AV *)mg->mg_obj; 664 ((PADLIST **)(mg->mg_ptr + sizeof (size_t))) [(*(size_t *)mg->mg_ptr)++] = CvPADLIST (cv);
527
528 if (expect_false (AvFILLp (av) >= AvMAX (av)))
529 av_extend (av, AvFILLp (av) + 1);
530
531 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
532} 665}
533 666
534/** load & save, init *******************************************************/ 667/** load & save, init *******************************************************/
668
669ecb_inline void
670swap_sv (SV *a, SV *b)
671{
672 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
673 SV tmp;
674
675 /* swap sv_any */
676 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
677
678 /* swap sv_flags */
679 SvFLAGS (&tmp) = SvFLAGS (a);
680 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
681 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
682
683#if PERL_VERSION_ATLEAST (5,10,0)
684 /* perl 5.10 and later complicates this _quite_ a bit, but it also
685 * is much faster, so no quarrels here. alternatively, we could
686 * sv_upgrade to avoid this.
687 */
688 {
689 /* swap sv_u */
690 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
691
692 /* if SvANY points to the head, we need to adjust the pointers,
693 * as the pointer for a still points to b, and maybe vice versa.
694 */
695 #define svany_in_head(type) \
696 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
697
698 if (svany_in_head (SvTYPE (a)))
699 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
700
701 if (svany_in_head (SvTYPE (b)))
702 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
703 }
704#endif
705}
706
707/* swap sv heads, at least logically */
708static void
709swap_svs (pTHX_ Coro__State c)
710{
711 int i;
712
713 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2)
714 swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]);
715}
716
717#define SWAP_SVS(coro) \
718 if (ecb_expect_false ((coro)->swap_sv)) \
719 swap_svs (aTHX_ (coro))
535 720
536static void 721static void
537on_enterleave_call (pTHX_ SV *cb); 722on_enterleave_call (pTHX_ SV *cb);
538 723
539static void 724static void
542 perl_slots *slot = c->slot; 727 perl_slots *slot = c->slot;
543 c->slot = 0; 728 c->slot = 0;
544 729
545 PL_mainstack = c->mainstack; 730 PL_mainstack = c->mainstack;
546 731
547 GvSV (PL_defgv) = slot->defsv; 732#if CORO_JIT
548 GvAV (PL_defgv) = slot->defav; 733 load_perl_slots (slot);
549 GvSV (PL_errgv) = slot->errsv; 734#else
550 GvSV (irsgv) = slot->irsgv;
551 GvHV (PL_hintgv) = slot->hinthv;
552
553 #define VAR(name,type) PL_ ## name = slot->name; 735 #define VARx(name,expr,type) expr = slot->name;
554 # include "state.h" 736 #include "state.h"
555 #undef VAR 737#endif
556 738
557 { 739 {
558 dSP; 740 dSP;
559 741
560 CV *cv; 742 CV *cv;
561 743
562 /* now do the ugly restore mess */ 744 /* now do the ugly restore mess */
563 while (expect_true (cv = (CV *)POPs)) 745 while (ecb_expect_true (cv = (CV *)POPs))
564 { 746 {
565 put_padlist (aTHX_ cv); /* mark this padlist as available */ 747 put_padlist (aTHX_ cv); /* mark this padlist as available */
566 CvDEPTH (cv) = PTR2IV (POPs); 748 CvDEPTH (cv) = PTR2IV (POPs);
567 CvPADLIST (cv) = (AV *)POPs; 749 CvPADLIST (cv) = (PADLIST *)POPs;
568 } 750 }
569 751
570 PUTBACK; 752 PUTBACK;
571 } 753 }
572 754
573 slf_frame = c->slf_frame; 755 slf_frame = c->slf_frame;
574 CORO_THROW = c->except; 756 CORO_THROW = c->except;
575 757
576 if (expect_false (enable_times)) 758 if (ecb_expect_false (enable_times))
577 { 759 {
578 if (expect_false (!times_valid)) 760 if (ecb_expect_false (!times_valid))
579 coro_times_update (); 761 coro_times_update ();
580 762
581 coro_times_sub (c); 763 coro_times_sub (c);
582 } 764 }
583 765
584 if (expect_false (c->on_enter)) 766 if (ecb_expect_false (c->on_enter))
585 { 767 {
586 int i; 768 int i;
587 769
588 for (i = 0; i <= AvFILLp (c->on_enter); ++i) 770 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
589 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); 771 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
590 } 772 }
773
774 SWAP_SVS (c);
591} 775}
592 776
593static void 777static void
594save_perl (pTHX_ Coro__State c) 778save_perl (pTHX_ Coro__State c)
595{ 779{
780 SWAP_SVS (c);
781
596 if (expect_false (c->on_leave)) 782 if (ecb_expect_false (c->on_leave))
597 { 783 {
598 int i; 784 int i;
599 785
600 for (i = AvFILLp (c->on_leave); i >= 0; --i) 786 for (i = AvFILLp (c->on_leave); i >= 0; --i)
601 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); 787 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
602 } 788 }
603 789
604 times_valid = 0; 790 times_valid = 0;
605 791
606 if (expect_false (enable_times)) 792 if (ecb_expect_false (enable_times))
607 { 793 {
608 coro_times_update (); times_valid = 1; 794 coro_times_update (); times_valid = 1;
609 coro_times_add (c); 795 coro_times_add (c);
610 } 796 }
611 797
625 811
626 XPUSHs (Nullsv); 812 XPUSHs (Nullsv);
627 /* this loop was inspired by pp_caller */ 813 /* this loop was inspired by pp_caller */
628 for (;;) 814 for (;;)
629 { 815 {
630 while (expect_true (cxix >= 0)) 816 while (ecb_expect_true (cxix >= 0))
631 { 817 {
632 PERL_CONTEXT *cx = &ccstk[cxix--]; 818 PERL_CONTEXT *cx = &ccstk[cxix--];
633 819
634 if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT)) 820 if (ecb_expect_true (CxTYPE (cx) == CXt_SUB) || ecb_expect_false (CxTYPE (cx) == CXt_FORMAT))
635 { 821 {
636 CV *cv = cx->blk_sub.cv; 822 CV *cv = cx->blk_sub.cv;
637 823
638 if (expect_true (CvDEPTH (cv))) 824 if (ecb_expect_true (CvDEPTH (cv)))
639 { 825 {
640 EXTEND (SP, 3); 826 EXTEND (SP, 3);
641 PUSHs ((SV *)CvPADLIST (cv)); 827 PUSHs ((SV *)CvPADLIST (cv));
642 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); 828 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
643 PUSHs ((SV *)cv); 829 PUSHs ((SV *)cv);
646 get_padlist (aTHX_ cv); 832 get_padlist (aTHX_ cv);
647 } 833 }
648 } 834 }
649 } 835 }
650 836
651 if (expect_true (top_si->si_type == PERLSI_MAIN)) 837 if (ecb_expect_true (top_si->si_type == PERLSI_MAIN))
652 break; 838 break;
653 839
654 top_si = top_si->si_prev; 840 top_si = top_si->si_prev;
655 ccstk = top_si->si_cxstack; 841 ccstk = top_si->si_cxstack;
656 cxix = top_si->si_cxix; 842 cxix = top_si->si_cxix;
658 844
659 PUTBACK; 845 PUTBACK;
660 } 846 }
661 847
662 /* allocate some space on the context stack for our purposes */ 848 /* allocate some space on the context stack for our purposes */
663 /* we manually unroll here, as usually 2 slots is enough */ 849 if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max))
664 if (SLOT_COUNT >= 1) CXINC;
665 if (SLOT_COUNT >= 2) CXINC;
666 if (SLOT_COUNT >= 3) CXINC;
667 { 850 {
668 unsigned int i; 851 unsigned int i;
852
669 for (i = 3; i < SLOT_COUNT; ++i) 853 for (i = 0; i < SLOT_COUNT; ++i)
670 CXINC; 854 CXINC;
671 } 855
672 cxstack_ix -= SLOT_COUNT; /* undo allocation */ 856 cxstack_ix -= SLOT_COUNT; /* undo allocation */
857 }
673 858
674 c->mainstack = PL_mainstack; 859 c->mainstack = PL_mainstack;
675 860
676 { 861 {
677 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 862 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
678 863
679 slot->defav = GvAV (PL_defgv); 864#if CORO_JIT
680 slot->defsv = DEFSV; 865 save_perl_slots (slot);
681 slot->errsv = ERRSV; 866#else
682 slot->irsgv = GvSV (irsgv);
683 slot->hinthv = GvHV (PL_hintgv);
684
685 #define VAR(name,type) slot->name = PL_ ## name; 867 #define VARx(name,expr,type) slot->name = expr;
686 # include "state.h" 868 #include "state.h"
687 #undef VAR 869#endif
688 } 870 }
689} 871}
690 872
691/* 873/*
692 * allocate various perl stacks. This is almost an exact copy 874 * allocate various perl stacks. This is almost an exact copy
698# define coro_init_stacks(thx) init_stacks () 880# define coro_init_stacks(thx) init_stacks ()
699#else 881#else
700static void 882static void
701coro_init_stacks (pTHX) 883coro_init_stacks (pTHX)
702{ 884{
703 PL_curstackinfo = new_stackinfo(32, 8); 885 PL_curstackinfo = new_stackinfo(32, 4 + SLOT_COUNT); /* 3 is minimum due to perl rounding down in scope.c:GROW() */
704 PL_curstackinfo->si_type = PERLSI_MAIN; 886 PL_curstackinfo->si_type = PERLSI_MAIN;
705 PL_curstack = PL_curstackinfo->si_stack; 887 PL_curstack = PL_curstackinfo->si_stack;
706 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 888 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
707 889
708 PL_stack_base = AvARRAY(PL_curstack); 890 PL_stack_base = AvARRAY(PL_curstack);
723#endif 905#endif
724 906
725 New(54,PL_scopestack,8,I32); 907 New(54,PL_scopestack,8,I32);
726 PL_scopestack_ix = 0; 908 PL_scopestack_ix = 0;
727 PL_scopestack_max = 8; 909 PL_scopestack_max = 8;
910#if HAS_SCOPESTACK_NAME
911 New(54,PL_scopestack_name,8,const char*);
912#endif
728 913
729 New(54,PL_savestack,24,ANY); 914 New(54,PL_savestack,24,ANY);
730 PL_savestack_ix = 0; 915 PL_savestack_ix = 0;
731 PL_savestack_max = 24; 916 PL_savestack_max = 24;
732 917
760 } 945 }
761 946
762 Safefree (PL_tmps_stack); 947 Safefree (PL_tmps_stack);
763 Safefree (PL_markstack); 948 Safefree (PL_markstack);
764 Safefree (PL_scopestack); 949 Safefree (PL_scopestack);
950#if HAS_SCOPESTACK_NAME
951 Safefree (PL_scopestack_name);
952#endif
765 Safefree (PL_savestack); 953 Safefree (PL_savestack);
766#if !PERL_VERSION_ATLEAST (5,10,0) 954#if !PERL_VERSION_ATLEAST (5,10,0)
767 Safefree (PL_retstack); 955 Safefree (PL_retstack);
768#endif 956#endif
769} 957}
815#endif 1003#endif
816 1004
817/* 1005/*
818 * This overrides the default magic get method of %SIG elements. 1006 * This overrides the default magic get method of %SIG elements.
819 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 1007 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
820 * and instead of trying to save and restore the hash elements, we just provide 1008 * and instead of trying to save and restore the hash elements (extremely slow),
821 * readback here. 1009 * we just provide our own readback here.
822 */ 1010 */
823static int 1011static int ecb_cold
824coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 1012coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
825{ 1013{
826 const char *s = MgPV_nolen_const (mg); 1014 const char *s = MgPV_nolen_const (mg);
827 1015
828 if (*s == '_') 1016 if (*s == '_')
829 { 1017 {
830 SV **svp = 0; 1018 SV **svp = 0;
831 1019
832 if (strEQ (s, "__DIE__" )) svp = &PL_diehook; 1020 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
833 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 1021 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
834 1022
835 if (svp) 1023 if (svp)
836 { 1024 {
837 sv_setsv (sv, *svp ? *svp : &PL_sv_undef); 1025 SV *ssv;
1026
1027 if (!*svp)
1028 ssv = &PL_sv_undef;
1029 else if (SvTYPE (*svp) == SVt_PVCV) /* perlio directly stores a CV in warnhook. ugh. */
1030 ssv = sv_2mortal (newRV_inc (*svp));
1031 else
1032 ssv = *svp;
1033
1034 sv_setsv (sv, ssv);
838 return 0; 1035 return 0;
839 } 1036 }
840 } 1037 }
841 1038
842 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; 1039 return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0;
843} 1040}
844 1041
845static int 1042static int ecb_cold
846coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) 1043coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg)
847{ 1044{
848 const char *s = MgPV_nolen_const (mg); 1045 const char *s = MgPV_nolen_const (mg);
849 1046
850 if (*s == '_') 1047 if (*s == '_')
864 } 1061 }
865 1062
866 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; 1063 return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0;
867} 1064}
868 1065
869static int 1066static int ecb_cold
870coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) 1067coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg)
871{ 1068{
872 const char *s = MgPV_nolen_const (mg); 1069 const char *s = MgPV_nolen_const (mg);
873 1070
874 if (*s == '_') 1071 if (*s == '_')
907slf_check_repeat (pTHX_ struct CoroSLF *frame) 1104slf_check_repeat (pTHX_ struct CoroSLF *frame)
908{ 1105{
909 return 1; 1106 return 1;
910} 1107}
911 1108
912static UNOP coro_setup_op; 1109static UNOP init_perl_op;
913 1110
914static void NOINLINE /* noinline to keep it out of the transfer fast path */ 1111ecb_noinline static void /* noinline to keep it out of the transfer fast path */
915coro_setup (pTHX_ struct coro *coro) 1112init_perl (pTHX_ struct coro *coro)
916{ 1113{
917 /* 1114 /*
918 * emulate part of the perl startup here. 1115 * emulate part of the perl startup here.
919 */ 1116 */
920 coro_init_stacks (aTHX); 1117 coro_init_stacks (aTHX);
927 PL_comppad_name_fill = 0; 1124 PL_comppad_name_fill = 0;
928 PL_comppad_name_floor = 0; 1125 PL_comppad_name_floor = 0;
929 PL_curpm = 0; 1126 PL_curpm = 0;
930 PL_curpad = 0; 1127 PL_curpad = 0;
931 PL_localizing = 0; 1128 PL_localizing = 0;
932 PL_dirty = 0;
933 PL_restartop = 0; 1129 PL_restartop = 0;
934#if PERL_VERSION_ATLEAST (5,10,0) 1130#if PERL_VERSION_ATLEAST (5,10,0)
935 PL_parser = 0; 1131 PL_parser = 0;
936#endif 1132#endif
937 PL_hints = 0; 1133 PL_hints = 0;
938 1134
939 /* recreate the die/warn hooks */ 1135 /* recreate the die/warn hooks */
940 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 1136 PL_diehook = SvREFCNT_inc (rv_diehook);
941 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); 1137 PL_warnhook = SvREFCNT_inc (rv_warnhook);
942 1138
943 GvSV (PL_defgv) = newSV (0); 1139 GvSV (PL_defgv) = newSV (0);
944 GvAV (PL_defgv) = coro->args; coro->args = 0; 1140 GvAV (PL_defgv) = coro->args; coro->args = 0;
945 GvSV (PL_errgv) = newSV (0); 1141 GvSV (PL_errgv) = newSV (0);
946 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 1142 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
947 GvHV (PL_hintgv) = 0; 1143 GvHV (PL_hintgv) = 0;
967 /* this newly created coroutine might be run on an existing cctx which most 1163 /* this newly created coroutine might be run on an existing cctx which most
968 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. 1164 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here.
969 */ 1165 */
970 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 1166 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
971 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 1167 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
1168 slf_frame.destroy = 0;
972 1169
973 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 1170 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
974 coro_setup_op.op_next = PL_op; 1171 init_perl_op.op_next = PL_op;
975 coro_setup_op.op_type = OP_ENTERSUB; 1172 init_perl_op.op_type = OP_ENTERSUB;
976 coro_setup_op.op_ppaddr = pp_slf; 1173 init_perl_op.op_ppaddr = pp_slf;
977 /* no flags etc. required, as an init function won't be called */ 1174 /* no flags etc. required, as an init function won't be called */
978 1175
979 PL_op = (OP *)&coro_setup_op; 1176 PL_op = (OP *)&init_perl_op;
980 1177
981 /* copy throw, in case it was set before coro_setup */ 1178 /* copy throw, in case it was set before init_perl */
982 CORO_THROW = coro->except; 1179 CORO_THROW = coro->except;
983 1180
1181 SWAP_SVS (coro);
1182
984 if (expect_false (enable_times)) 1183 if (ecb_expect_false (enable_times))
985 { 1184 {
986 coro_times_update (); 1185 coro_times_update ();
987 coro_times_sub (coro); 1186 coro_times_sub (coro);
988 } 1187 }
989} 1188}
1008 dounwind (-1); 1207 dounwind (-1);
1009 } 1208 }
1010} 1209}
1011 1210
1012static void 1211static void
1013coro_destruct_perl (pTHX_ struct coro *coro) 1212destroy_perl (pTHX_ struct coro *coro)
1014{ 1213{
1015 SV *svf [9]; 1214 SV *svf [9];
1016 1215
1017 { 1216 {
1217 SV *old_current = SvRV (coro_current);
1018 struct coro *current = SvSTATE_current; 1218 struct coro *current = SvSTATE (old_current);
1019 1219
1020 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); 1220 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1021 1221
1022 save_perl (aTHX_ current); 1222 save_perl (aTHX_ current);
1223
1224 /* this will cause transfer_check to croak on block*/
1225 SvRV_set (coro_current, (SV *)coro->hv);
1226
1023 load_perl (aTHX_ coro); 1227 load_perl (aTHX_ coro);
1024 1228
1025 coro_unwind_stacks (aTHX); 1229 coro_unwind_stacks (aTHX);
1230
1231 /* restore swapped sv's */
1232 SWAP_SVS (coro);
1233
1026 coro_destruct_stacks (aTHX); 1234 coro_destruct_stacks (aTHX);
1027 1235
1028 // now save some sv's to be free'd later 1236 /* now save some sv's to be free'd later */
1029 svf [0] = GvSV (PL_defgv); 1237 svf [0] = GvSV (PL_defgv);
1030 svf [1] = (SV *)GvAV (PL_defgv); 1238 svf [1] = (SV *)GvAV (PL_defgv);
1031 svf [2] = GvSV (PL_errgv); 1239 svf [2] = GvSV (PL_errgv);
1032 svf [3] = (SV *)PL_defoutgv; 1240 svf [3] = (SV *)PL_defoutgv;
1033 svf [4] = PL_rs; 1241 svf [4] = PL_rs;
1035 svf [6] = (SV *)GvHV (PL_hintgv); 1243 svf [6] = (SV *)GvHV (PL_hintgv);
1036 svf [7] = PL_diehook; 1244 svf [7] = PL_diehook;
1037 svf [8] = PL_warnhook; 1245 svf [8] = PL_warnhook;
1038 assert (9 == sizeof (svf) / sizeof (*svf)); 1246 assert (9 == sizeof (svf) / sizeof (*svf));
1039 1247
1248 SvRV_set (coro_current, old_current);
1249
1040 load_perl (aTHX_ current); 1250 load_perl (aTHX_ current);
1041 } 1251 }
1042 1252
1043 { 1253 {
1044 unsigned int i; 1254 unsigned int i;
1051 SvREFCNT_dec (coro->invoke_cb); 1261 SvREFCNT_dec (coro->invoke_cb);
1052 SvREFCNT_dec (coro->invoke_av); 1262 SvREFCNT_dec (coro->invoke_av);
1053 } 1263 }
1054} 1264}
1055 1265
1056INLINE void 1266ecb_inline void
1057free_coro_mortal (pTHX) 1267free_coro_mortal (pTHX)
1058{ 1268{
1059 if (expect_true (coro_mortal)) 1269 if (ecb_expect_true (coro_mortal))
1060 { 1270 {
1061 SvREFCNT_dec (coro_mortal); 1271 SvREFCNT_dec ((SV *)coro_mortal);
1062 coro_mortal = 0; 1272 coro_mortal = 0;
1063 } 1273 }
1064} 1274}
1065 1275
1066static int 1276static int
1199 1409
1200 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1410 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1201} 1411}
1202 1412
1203/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1413/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1204static void NOINLINE 1414static void ecb_noinline
1205cctx_prepare (pTHX) 1415cctx_prepare (pTHX)
1206{ 1416{
1207 PL_top_env = &PL_start_env; 1417 PL_top_env = &PL_start_env;
1208 1418
1209 if (cctx_current->flags & CC_TRACE) 1419 if (cctx_current->flags & CC_TRACE)
1220 slf_frame.prepare = slf_prepare_set_stacklevel; 1430 slf_frame.prepare = slf_prepare_set_stacklevel;
1221 slf_frame.check = slf_check_set_stacklevel; 1431 slf_frame.check = slf_check_set_stacklevel;
1222} 1432}
1223 1433
1224/* the tail of transfer: execute stuff we can only do after a transfer */ 1434/* the tail of transfer: execute stuff we can only do after a transfer */
1225INLINE void 1435ecb_inline void
1226transfer_tail (pTHX) 1436transfer_tail (pTHX)
1227{ 1437{
1228 free_coro_mortal (aTHX); 1438 free_coro_mortal (aTHX);
1439}
1440
1441/* try to exit the same way perl's main function would do */
1442/* we do not bother resetting the environment or other things *7
1443/* that are not, uhm, essential */
1444/* this obviously also doesn't work when perl is embedded */
1445static void ecb_noinline ecb_cold
1446perlish_exit (pTHX)
1447{
1448 int exitstatus = perl_destruct (PL_curinterp);
1449 perl_free (PL_curinterp);
1450 exit (exitstatus);
1229} 1451}
1230 1452
1231/* 1453/*
1232 * this is a _very_ stripped down perl interpreter ;) 1454 * this is a _very_ stripped down perl interpreter ;)
1233 */ 1455 */
1255 /* somebody or something will hit me for both perl_run and PL_restartop */ 1477 /* somebody or something will hit me for both perl_run and PL_restartop */
1256 PL_restartop = PL_op; 1478 PL_restartop = PL_op;
1257 perl_run (PL_curinterp); 1479 perl_run (PL_curinterp);
1258 /* 1480 /*
1259 * Unfortunately, there is no way to get at the return values of the 1481 * Unfortunately, there is no way to get at the return values of the
1260 * coro body here, as perl_run destroys these 1482 * coro body here, as perl_run destroys these. Likewise, we cannot catch
1483 * runtime errors here, as this is just a random interpreter, not a thread.
1261 */ 1484 */
1262 1485
1263 /* 1486 /*
1264 * If perl-run returns we assume exit() was being called or the coro 1487 * If perl-run returns we assume exit() was being called or the coro
1265 * fell off the end, which seems to be the only valid (non-bug) 1488 * fell off the end, which seems to be the only valid (non-bug)
1266 * reason for perl_run to return. We try to exit by jumping to the 1489 * reason for perl_run to return. We try to mimic whatever perl is normally
1267 * bootstrap-time "top" top_env, as we cannot restore the "main" 1490 * doing in that case. YMMV.
1268 * coroutine as Coro has no such concept.
1269 * This actually isn't valid with the pthread backend, but OSes requiring
1270 * that backend are too broken to do it in a standards-compliant way.
1271 */ 1491 */
1272 PL_top_env = main_top_env; 1492 perlish_exit (aTHX);
1273 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1274 } 1493 }
1275} 1494}
1276 1495
1277static coro_cctx * 1496static coro_cctx *
1278cctx_new () 1497cctx_new (void)
1279{ 1498{
1280 coro_cctx *cctx; 1499 coro_cctx *cctx;
1281 1500
1282 ++cctx_count; 1501 ++cctx_count;
1283 New (0, cctx, 1, coro_cctx); 1502 New (0, cctx, 1, coro_cctx);
1289 return cctx; 1508 return cctx;
1290} 1509}
1291 1510
1292/* create a new cctx only suitable as source */ 1511/* create a new cctx only suitable as source */
1293static coro_cctx * 1512static coro_cctx *
1294cctx_new_empty () 1513cctx_new_empty (void)
1295{ 1514{
1296 coro_cctx *cctx = cctx_new (); 1515 coro_cctx *cctx = cctx_new ();
1297 1516
1298 cctx->sptr = 0; 1517 cctx->stack.sptr = 0;
1299 coro_create (&cctx->cctx, 0, 0, 0, 0); 1518 coro_create (&cctx->cctx, 0, 0, 0, 0);
1300 1519
1301 return cctx; 1520 return cctx;
1302} 1521}
1303 1522
1304/* create a new cctx suitable as destination/running a perl interpreter */ 1523/* create a new cctx suitable as destination/running a perl interpreter */
1305static coro_cctx * 1524static coro_cctx *
1306cctx_new_run () 1525cctx_new_run (void)
1307{ 1526{
1308 coro_cctx *cctx = cctx_new (); 1527 coro_cctx *cctx = cctx_new ();
1309 void *stack_start;
1310 size_t stack_size;
1311 1528
1312#if HAVE_MMAP 1529 if (!coro_stack_alloc (&cctx->stack, cctx_stacksize))
1313 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1314 /* mmap supposedly does allocate-on-write for us */
1315 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1316
1317 if (cctx->sptr != (void *)-1)
1318 {
1319 #if CORO_STACKGUARD
1320 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1321 #endif
1322 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1323 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1324 cctx->flags |= CC_MAPPED;
1325 } 1530 {
1326 else
1327#endif
1328 {
1329 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1330 New (0, cctx->sptr, cctx_stacksize, long);
1331
1332 if (!cctx->sptr)
1333 {
1334 perror ("FATAL: unable to allocate stack for coroutine, exiting."); 1531 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
1335 _exit (EXIT_FAILURE); 1532 _exit (EXIT_FAILURE);
1336 }
1337
1338 stack_start = cctx->sptr;
1339 stack_size = cctx->ssize;
1340 } 1533 }
1341 1534
1342 #if CORO_USE_VALGRIND
1343 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1344 #endif
1345
1346 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); 1535 coro_create (&cctx->cctx, cctx_run, (void *)cctx, cctx->stack.sptr, cctx->stack.ssze);
1347 1536
1348 return cctx; 1537 return cctx;
1349} 1538}
1350 1539
1351static void 1540static void
1352cctx_destroy (coro_cctx *cctx) 1541cctx_destroy (coro_cctx *cctx)
1353{ 1542{
1354 if (!cctx) 1543 if (!cctx)
1355 return; 1544 return;
1356 1545
1357 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary? 1546 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));
1358 1547
1359 --cctx_count; 1548 --cctx_count;
1360 coro_destroy (&cctx->cctx); 1549 coro_destroy (&cctx->cctx);
1361 1550
1362 /* coro_transfer creates new, empty cctx's */ 1551 coro_stack_free (&cctx->stack);
1363 if (cctx->sptr)
1364 {
1365 #if CORO_USE_VALGRIND
1366 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1367 #endif
1368
1369#if HAVE_MMAP
1370 if (cctx->flags & CC_MAPPED)
1371 munmap (cctx->sptr, cctx->ssize);
1372 else
1373#endif
1374 Safefree (cctx->sptr);
1375 }
1376 1552
1377 Safefree (cctx); 1553 Safefree (cctx);
1378} 1554}
1379 1555
1380/* wether this cctx should be destructed */ 1556/* wether this cctx should be destructed */
1381#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) 1557#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1382 1558
1383static coro_cctx * 1559static coro_cctx *
1384cctx_get (pTHX) 1560cctx_get (pTHX)
1385{ 1561{
1386 while (expect_true (cctx_first)) 1562 while (ecb_expect_true (cctx_first))
1387 { 1563 {
1388 coro_cctx *cctx = cctx_first; 1564 coro_cctx *cctx = cctx_first;
1389 cctx_first = cctx->next; 1565 cctx_first = cctx->next;
1390 --cctx_idle; 1566 --cctx_idle;
1391 1567
1392 if (expect_true (!CCTX_EXPIRED (cctx))) 1568 if (ecb_expect_true (!CCTX_EXPIRED (cctx)))
1393 return cctx; 1569 return cctx;
1394 1570
1395 cctx_destroy (cctx); 1571 cctx_destroy (cctx);
1396 } 1572 }
1397 1573
1399} 1575}
1400 1576
1401static void 1577static void
1402cctx_put (coro_cctx *cctx) 1578cctx_put (coro_cctx *cctx)
1403{ 1579{
1404 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); 1580 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->stack.sptr));
1405 1581
1406 /* free another cctx if overlimit */ 1582 /* free another cctx if overlimit */
1407 if (expect_false (cctx_idle >= cctx_max_idle)) 1583 if (ecb_expect_false (cctx_idle >= cctx_max_idle))
1408 { 1584 {
1409 coro_cctx *first = cctx_first; 1585 coro_cctx *first = cctx_first;
1410 cctx_first = first->next; 1586 cctx_first = first->next;
1411 --cctx_idle; 1587 --cctx_idle;
1412 1588
1423static void 1599static void
1424transfer_check (pTHX_ struct coro *prev, struct coro *next) 1600transfer_check (pTHX_ struct coro *prev, struct coro *next)
1425{ 1601{
1426 /* TODO: throwing up here is considered harmful */ 1602 /* TODO: throwing up here is considered harmful */
1427 1603
1428 if (expect_true (prev != next)) 1604 if (ecb_expect_true (prev != next))
1429 { 1605 {
1430 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1606 if (ecb_expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1431 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); 1607 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1432 1608
1433 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED))) 1609 if (ecb_expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED)))
1434 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); 1610 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1435 1611
1436#if !PERL_VERSION_ATLEAST (5,10,0) 1612#if !PERL_VERSION_ATLEAST (5,10,0)
1437 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1613 if (ecb_expect_false (PL_lex_state != LEX_NOTPARSING))
1438 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); 1614 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1439#endif 1615#endif
1440 } 1616 }
1441} 1617}
1442 1618
1443/* always use the TRANSFER macro */ 1619/* always use the TRANSFER macro */
1444static void NOINLINE /* noinline so we have a fixed stackframe */ 1620static void ecb_noinline /* noinline so we have a fixed stackframe */
1445transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1621transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1446{ 1622{
1447 dSTACKLEVEL; 1623 dSTACKLEVEL;
1448 1624
1449 /* sometimes transfer is only called to set idle_sp */ 1625 /* sometimes transfer is only called to set idle_sp */
1450 if (expect_false (!prev)) 1626 if (ecb_expect_false (!prev))
1451 { 1627 {
1452 cctx_current->idle_sp = STACKLEVEL; 1628 cctx_current->idle_sp = STACKLEVEL;
1453 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1629 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1454 } 1630 }
1455 else if (expect_true (prev != next)) 1631 else if (ecb_expect_true (prev != next))
1456 { 1632 {
1457 coro_cctx *cctx_prev; 1633 coro_cctx *cctx_prev;
1458 1634
1459 if (expect_false (prev->flags & CF_NEW)) 1635 if (ecb_expect_false (prev->flags & CF_NEW))
1460 { 1636 {
1461 /* create a new empty/source context */ 1637 /* create a new empty/source context */
1462 prev->flags &= ~CF_NEW; 1638 prev->flags &= ~CF_NEW;
1463 prev->flags |= CF_RUNNING; 1639 prev->flags |= CF_RUNNING;
1464 } 1640 }
1467 next->flags |= CF_RUNNING; 1643 next->flags |= CF_RUNNING;
1468 1644
1469 /* first get rid of the old state */ 1645 /* first get rid of the old state */
1470 save_perl (aTHX_ prev); 1646 save_perl (aTHX_ prev);
1471 1647
1472 if (expect_false (next->flags & CF_NEW)) 1648 if (ecb_expect_false (next->flags & CF_NEW))
1473 { 1649 {
1474 /* need to start coroutine */ 1650 /* need to start coroutine */
1475 next->flags &= ~CF_NEW; 1651 next->flags &= ~CF_NEW;
1476 /* setup coroutine call */ 1652 /* setup coroutine call */
1477 coro_setup (aTHX_ next); 1653 init_perl (aTHX_ next);
1478 } 1654 }
1479 else 1655 else
1480 load_perl (aTHX_ next); 1656 load_perl (aTHX_ next);
1481 1657
1482 /* possibly untie and reuse the cctx */ 1658 /* possibly untie and reuse the cctx */
1483 if (expect_true ( 1659 if (ecb_expect_true (
1484 cctx_current->idle_sp == STACKLEVEL 1660 cctx_current->idle_sp == STACKLEVEL
1485 && !(cctx_current->flags & CC_TRACE) 1661 && !(cctx_current->flags & CC_TRACE)
1486 && !force_cctx 1662 && !force_cctx
1487 )) 1663 ))
1488 { 1664 {
1489 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1665 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1490 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); 1666 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1491 1667
1492 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ 1668 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1493 /* without this the next cctx_get might destroy the running cctx while still in use */ 1669 /* without this the next cctx_get might destroy the running cctx while still in use */
1494 if (expect_false (CCTX_EXPIRED (cctx_current))) 1670 if (ecb_expect_false (CCTX_EXPIRED (cctx_current)))
1495 if (expect_true (!next->cctx)) 1671 if (ecb_expect_true (!next->cctx))
1496 next->cctx = cctx_get (aTHX); 1672 next->cctx = cctx_get (aTHX);
1497 1673
1498 cctx_put (cctx_current); 1674 cctx_put (cctx_current);
1499 } 1675 }
1500 else 1676 else
1501 prev->cctx = cctx_current; 1677 prev->cctx = cctx_current;
1502 1678
1503 ++next->usecount; 1679 ++next->usecount;
1504 1680
1505 cctx_prev = cctx_current; 1681 cctx_prev = cctx_current;
1506 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); 1682 cctx_current = ecb_expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1507 1683
1508 next->cctx = 0; 1684 next->cctx = 0;
1509 1685
1510 if (expect_false (cctx_prev != cctx_current)) 1686 if (ecb_expect_false (cctx_prev != cctx_current))
1511 { 1687 {
1512 cctx_prev->top_env = PL_top_env; 1688 cctx_prev->top_env = PL_top_env;
1513 PL_top_env = cctx_current->top_env; 1689 PL_top_env = cctx_current->top_env;
1514 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); 1690 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1515 } 1691 }
1521#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) 1697#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx))
1522#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1698#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1523 1699
1524/** high level stuff ********************************************************/ 1700/** high level stuff ********************************************************/
1525 1701
1702/* this function is actually Coro, not Coro::State, but we call it from here */
1703/* because it is convenient - but it hasn't been declared yet for that reason */
1526static int 1704static void
1705coro_call_on_destroy (pTHX_ struct coro *coro);
1706
1707static void
1527coro_state_destroy (pTHX_ struct coro *coro) 1708coro_state_destroy (pTHX_ struct coro *coro)
1528{ 1709{
1529 if (coro->flags & CF_DESTROYED) 1710 if (coro->flags & CF_ZOMBIE)
1530 return 0; 1711 return;
1531 1712
1532 if (coro->on_destroy && !PL_dirty)
1533 coro->on_destroy (aTHX_ coro); 1713 slf_destroy (aTHX_ coro);
1534 1714
1535 coro->flags |= CF_DESTROYED; 1715 coro->flags |= CF_ZOMBIE;
1536 1716
1537 if (coro->flags & CF_READY) 1717 if (coro->flags & CF_READY)
1538 { 1718 {
1539 /* reduce nready, as destroying a ready coro effectively unreadies it */ 1719 /* reduce nready, as destroying a ready coro effectively unreadies it */
1540 /* alternative: look through all ready queues and remove the coro */ 1720 /* alternative: look through all ready queues and remove the coro */
1541 --coro_nready; 1721 --coro_nready;
1542 } 1722 }
1543 else 1723 else
1544 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1724 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1725
1726 if (coro->next) coro->next->prev = coro->prev;
1727 if (coro->prev) coro->prev->next = coro->next;
1728 if (coro == coro_first) coro_first = coro->next;
1545 1729
1546 if (coro->mainstack 1730 if (coro->mainstack
1547 && coro->mainstack != main_mainstack 1731 && coro->mainstack != main_mainstack
1548 && coro->slot 1732 && coro->slot
1549 && !PL_dirty) 1733 && !PL_dirty)
1550 coro_destruct_perl (aTHX_ coro); 1734 destroy_perl (aTHX_ coro);
1551 1735
1552 cctx_destroy (coro->cctx); 1736 cctx_destroy (coro->cctx);
1553 SvREFCNT_dec (coro->startcv); 1737 SvREFCNT_dec (coro->startcv);
1554 SvREFCNT_dec (coro->args); 1738 SvREFCNT_dec (coro->args);
1739 SvREFCNT_dec (coro->swap_sv);
1555 SvREFCNT_dec (CORO_THROW); 1740 SvREFCNT_dec (CORO_THROW);
1556 1741
1557 if (coro->next) coro->next->prev = coro->prev; 1742 coro_call_on_destroy (aTHX_ coro);
1558 if (coro->prev) coro->prev->next = coro->next;
1559 if (coro == coro_first) coro_first = coro->next;
1560 1743
1561 return 1; 1744 /* more destruction mayhem in coro_state_free */
1562} 1745}
1563 1746
1564static int 1747static int
1565coro_state_free (pTHX_ SV *sv, MAGIC *mg) 1748coro_state_free (pTHX_ SV *sv, MAGIC *mg)
1566{ 1749{
1567 struct coro *coro = (struct coro *)mg->mg_ptr; 1750 struct coro *coro = (struct coro *)mg->mg_ptr;
1568 mg->mg_ptr = 0; 1751 mg->mg_ptr = 0;
1569 1752
1570 coro->hv = 0;
1571
1572 if (--coro->refcnt < 0)
1573 {
1574 coro_state_destroy (aTHX_ coro); 1753 coro_state_destroy (aTHX_ coro);
1754 SvREFCNT_dec (coro->on_destroy);
1755 SvREFCNT_dec (coro->status);
1756
1575 Safefree (coro); 1757 Safefree (coro);
1576 }
1577 1758
1578 return 0; 1759 return 0;
1579} 1760}
1580 1761
1581static int 1762static int ecb_cold
1582coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) 1763coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
1583{ 1764{
1584 struct coro *coro = (struct coro *)mg->mg_ptr; 1765 /* called when perl clones the current process the slow way (windows process emulation) */
1585 1766 /* WE SIMply nuke the pointers in the copy, causing perl to croak */
1586 ++coro->refcnt; 1767 mg->mg_ptr = 0;
1768 mg->mg_virtual = 0;
1587 1769
1588 return 0; 1770 return 0;
1589} 1771}
1590 1772
1591static MGVTBL coro_state_vtbl = { 1773static MGVTBL coro_state_vtbl = {
1616 TRANSFER (ta, 1); 1798 TRANSFER (ta, 1);
1617} 1799}
1618 1800
1619/** Coro ********************************************************************/ 1801/** Coro ********************************************************************/
1620 1802
1621INLINE void 1803ecb_inline void
1622coro_enq (pTHX_ struct coro *coro) 1804coro_enq (pTHX_ struct coro *coro)
1623{ 1805{
1624 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; 1806 struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN];
1625 1807
1626 SvREFCNT_inc_NN (coro->hv); 1808 SvREFCNT_inc_NN (coro->hv);
1628 coro->next_ready = 0; 1810 coro->next_ready = 0;
1629 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; 1811 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1630 ready [1] = coro; 1812 ready [1] = coro;
1631} 1813}
1632 1814
1633INLINE struct coro * 1815ecb_inline struct coro *
1634coro_deq (pTHX) 1816coro_deq (pTHX)
1635{ 1817{
1636 int prio; 1818 int prio;
1637 1819
1638 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) 1820 for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; )
1648 } 1830 }
1649 1831
1650 return 0; 1832 return 0;
1651} 1833}
1652 1834
1835static void
1836invoke_sv_ready_hook_helper (void)
1837{
1838 dTHX;
1839 dSP;
1840
1841 ENTER;
1842 SAVETMPS;
1843
1844 PUSHMARK (SP);
1845 PUTBACK;
1846 call_sv (coro_readyhook, G_VOID | G_DISCARD);
1847
1848 FREETMPS;
1849 LEAVE;
1850}
1851
1653static int 1852static int
1654api_ready (pTHX_ SV *coro_sv) 1853api_ready (pTHX_ SV *coro_sv)
1655{ 1854{
1656 struct coro *coro;
1657 SV *sv_hook;
1658 void (*xs_hook)(void);
1659
1660 coro = SvSTATE (coro_sv); 1855 struct coro *coro = SvSTATE (coro_sv);
1661 1856
1662 if (coro->flags & CF_READY) 1857 if (coro->flags & CF_READY)
1663 return 0; 1858 return 0;
1664 1859
1665 coro->flags |= CF_READY; 1860 coro->flags |= CF_READY;
1666 1861
1667 sv_hook = coro_nready ? 0 : coro_readyhook;
1668 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1669
1670 coro_enq (aTHX_ coro); 1862 coro_enq (aTHX_ coro);
1671 ++coro_nready;
1672 1863
1673 if (sv_hook) 1864 if (!coro_nready++)
1674 { 1865 if (coroapi.readyhook)
1675 dSP; 1866 coroapi.readyhook ();
1676
1677 ENTER;
1678 SAVETMPS;
1679
1680 PUSHMARK (SP);
1681 PUTBACK;
1682 call_sv (sv_hook, G_VOID | G_DISCARD);
1683
1684 FREETMPS;
1685 LEAVE;
1686 }
1687
1688 if (xs_hook)
1689 xs_hook ();
1690 1867
1691 return 1; 1868 return 1;
1692} 1869}
1693 1870
1694static int 1871static int
1696{ 1873{
1697 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1874 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1698} 1875}
1699 1876
1700/* expects to own a reference to next->hv */ 1877/* expects to own a reference to next->hv */
1701INLINE void 1878ecb_inline void
1702prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) 1879prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1703{ 1880{
1704 SV *prev_sv = SvRV (coro_current); 1881 SV *prev_sv = SvRV (coro_current);
1705 1882
1706 ta->prev = SvSTATE_hv (prev_sv); 1883 ta->prev = SvSTATE_hv (prev_sv);
1719{ 1896{
1720 for (;;) 1897 for (;;)
1721 { 1898 {
1722 struct coro *next = coro_deq (aTHX); 1899 struct coro *next = coro_deq (aTHX);
1723 1900
1724 if (expect_true (next)) 1901 if (ecb_expect_true (next))
1725 { 1902 {
1726 /* cannot transfer to destroyed coros, skip and look for next */ 1903 /* cannot transfer to destroyed coros, skip and look for next */
1727 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED))) 1904 if (ecb_expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED)))
1728 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ 1905 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1729 else 1906 else
1730 { 1907 {
1731 next->flags &= ~CF_READY; 1908 next->flags &= ~CF_READY;
1732 --coro_nready; 1909 --coro_nready;
1739 { 1916 {
1740 /* nothing to schedule: call the idle handler */ 1917 /* nothing to schedule: call the idle handler */
1741 if (SvROK (sv_idle) 1918 if (SvROK (sv_idle)
1742 && SvOBJECT (SvRV (sv_idle))) 1919 && SvOBJECT (SvRV (sv_idle)))
1743 { 1920 {
1921 if (SvRV (sv_idle) == SvRV (coro_current))
1922 {
1923 require_pv ("Carp");
1924
1925 {
1926 dSP;
1927
1928 ENTER;
1929 SAVETMPS;
1930
1931 PUSHMARK (SP);
1932 XPUSHs (sv_2mortal (newSVpv ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught", 0)));
1933 PUTBACK;
1934 call_pv ("Carp::confess", G_VOID | G_DISCARD);
1935
1936 FREETMPS;
1937 LEAVE;
1938 }
1939 }
1940
1744 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ 1941 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1745 api_ready (aTHX_ SvRV (sv_idle)); 1942 api_ready (aTHX_ SvRV (sv_idle));
1746 --coro_nready; 1943 --coro_nready;
1747 } 1944 }
1748 else 1945 else
1749 { 1946 {
1947 /* TODO: deprecated, remove, cannot work reliably *//*D*/
1750 dSP; 1948 dSP;
1751 1949
1752 ENTER; 1950 ENTER;
1753 SAVETMPS; 1951 SAVETMPS;
1754 1952
1761 } 1959 }
1762 } 1960 }
1763 } 1961 }
1764} 1962}
1765 1963
1766INLINE void 1964ecb_inline void
1767prepare_cede (pTHX_ struct coro_transfer_args *ta) 1965prepare_cede (pTHX_ struct coro_transfer_args *ta)
1768{ 1966{
1769 api_ready (aTHX_ coro_current); 1967 api_ready (aTHX_ coro_current);
1770 prepare_schedule (aTHX_ ta); 1968 prepare_schedule (aTHX_ ta);
1771} 1969}
1772 1970
1773INLINE void 1971ecb_inline void
1774prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) 1972prepare_cede_notself (pTHX_ struct coro_transfer_args *ta)
1775{ 1973{
1776 SV *prev = SvRV (coro_current); 1974 SV *prev = SvRV (coro_current);
1777 1975
1778 if (coro_nready) 1976 if (coro_nready)
1808{ 2006{
1809 struct coro_transfer_args ta; 2007 struct coro_transfer_args ta;
1810 2008
1811 prepare_cede (aTHX_ &ta); 2009 prepare_cede (aTHX_ &ta);
1812 2010
1813 if (expect_true (ta.prev != ta.next)) 2011 if (ecb_expect_true (ta.prev != ta.next))
1814 { 2012 {
1815 TRANSFER (ta, 1); 2013 TRANSFER (ta, 1);
1816 return 1; 2014 return 1;
1817 } 2015 }
1818 else 2016 else
1861 coro->slot->runops = RUNOPS_DEFAULT; 2059 coro->slot->runops = RUNOPS_DEFAULT;
1862 } 2060 }
1863} 2061}
1864 2062
1865static void 2063static void
2064coro_push_av (pTHX_ AV *av, I32 gimme_v)
2065{
2066 if (AvFILLp (av) >= 0 && gimme_v != G_VOID)
2067 {
2068 dSP;
2069
2070 if (gimme_v == G_SCALAR)
2071 XPUSHs (AvARRAY (av)[AvFILLp (av)]);
2072 else
2073 {
2074 int i;
2075 EXTEND (SP, AvFILLp (av) + 1);
2076
2077 for (i = 0; i <= AvFILLp (av); ++i)
2078 PUSHs (AvARRAY (av)[i]);
2079 }
2080
2081 PUTBACK;
2082 }
2083}
2084
2085static void
2086coro_push_on_destroy (pTHX_ struct coro *coro, SV *cb)
2087{
2088 if (!coro->on_destroy)
2089 coro->on_destroy = newAV ();
2090
2091 av_push (coro->on_destroy, cb);
2092}
2093
2094static void
2095slf_destroy_join (pTHX_ struct CoroSLF *frame)
2096{
2097 SvREFCNT_dec ((SV *)((struct coro *)frame->data)->hv);
2098}
2099
2100static int
2101slf_check_join (pTHX_ struct CoroSLF *frame)
2102{
2103 struct coro *coro = (struct coro *)frame->data;
2104
2105 if (!coro->status)
2106 return 1;
2107
2108 frame->destroy = 0;
2109
2110 coro_push_av (aTHX_ coro->status, GIMME_V);
2111
2112 SvREFCNT_dec ((SV *)coro->hv);
2113
2114 return 0;
2115}
2116
2117static void
2118slf_init_join (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2119{
2120 struct coro *coro = SvSTATE (items > 0 ? arg [0] : &PL_sv_undef);
2121
2122 if (items > 1)
2123 croak ("join called with too many arguments");
2124
2125 if (coro->status)
2126 frame->prepare = prepare_nop;
2127 else
2128 {
2129 coro_push_on_destroy (aTHX_ coro, SvREFCNT_inc_NN (SvRV (coro_current)));
2130 frame->prepare = prepare_schedule;
2131 }
2132
2133 frame->check = slf_check_join;
2134 frame->destroy = slf_destroy_join;
2135 frame->data = (void *)coro;
2136 SvREFCNT_inc (coro->hv);
2137}
2138
2139static void
1866coro_call_on_destroy (pTHX_ struct coro *coro) 2140coro_call_on_destroy (pTHX_ struct coro *coro)
1867{ 2141{
1868 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); 2142 AV *od = coro->on_destroy;
1869 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1870 2143
1871 if (on_destroyp) 2144 if (!od)
1872 { 2145 return;
1873 AV *on_destroy = (AV *)SvRV (*on_destroyp);
1874 2146
2147 coro->on_destroy = 0;
2148 sv_2mortal ((SV *)od);
2149
1875 while (AvFILLp (on_destroy) >= 0) 2150 while (AvFILLp (od) >= 0)
2151 {
2152 SV *cb = sv_2mortal (av_pop (od));
2153
2154 /* coro hv's (and only hv's at the moment) are supported as well */
2155 if (SvSTATEhv_p (aTHX_ cb))
2156 api_ready (aTHX_ cb);
2157 else
1876 { 2158 {
1877 dSP; /* don't disturb outer sp */ 2159 dSP; /* don't disturb outer sp */
1878 SV *cb = av_pop (on_destroy);
1879
1880 PUSHMARK (SP); 2160 PUSHMARK (SP);
1881 2161
1882 if (statusp) 2162 if (coro->status)
1883 { 2163 {
1884 int i; 2164 PUTBACK;
1885 AV *status = (AV *)SvRV (*statusp); 2165 coro_push_av (aTHX_ coro->status, G_ARRAY);
1886 EXTEND (SP, AvFILLp (status) + 1); 2166 SPAGAIN;
1887
1888 for (i = 0; i <= AvFILLp (status); ++i)
1889 PUSHs (AvARRAY (status)[i]);
1890 } 2167 }
1891 2168
1892 PUTBACK; 2169 PUTBACK;
1893 call_sv (sv_2mortal (cb), G_VOID | G_DISCARD); 2170 call_sv (cb, G_VOID | G_DISCARD);
1894 } 2171 }
1895 } 2172 }
1896} 2173}
1897 2174
1898static void 2175static void
1899slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2176coro_set_status (pTHX_ struct coro *coro, SV **arg, int items)
1900{ 2177{
2178 AV *av;
2179
2180 if (coro->status)
2181 {
2182 av = coro->status;
2183 av_clear (av);
2184 }
2185 else
2186 av = coro->status = newAV ();
2187
2188 /* items are actually not so common, so optimise for this case */
2189 if (items)
2190 {
1901 int i; 2191 int i;
1902 HV *hv = (HV *)SvRV (coro_current);
1903 AV *av = newAV ();
1904 2192
1905 av_extend (av, items - 1); 2193 av_extend (av, items - 1);
2194
1906 for (i = 0; i < items; ++i) 2195 for (i = 0; i < items; ++i)
1907 av_push (av, SvREFCNT_inc_NN (arg [i])); 2196 av_push (av, SvREFCNT_inc_NN (arg [i]));
2197 }
2198}
1908 2199
1909 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); 2200static void
1910 2201slf_init_terminate_cancel_common (pTHX_ struct CoroSLF *frame, HV *coro_hv)
2202{
1911 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 2203 av_push (av_destroy, (SV *)newRV_inc ((SV *)coro_hv)); /* RVinc for perl */
1912 api_ready (aTHX_ sv_manager); 2204 api_ready (aTHX_ sv_manager);
1913 2205
1914 frame->prepare = prepare_schedule; 2206 frame->prepare = prepare_schedule;
1915 frame->check = slf_check_repeat; 2207 frame->check = slf_check_repeat;
1916 2208
1917 /* as a minor optimisation, we could unwind all stacks here */ 2209 /* as a minor optimisation, we could unwind all stacks here */
1918 /* but that puts extra pressure on pp_slf, and is not worth much */ 2210 /* but that puts extra pressure on pp_slf, and is not worth much */
1919 /*coro_unwind_stacks (aTHX);*/ 2211 /*coro_unwind_stacks (aTHX);*/
2212}
2213
2214static void
2215slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2216{
2217 HV *coro_hv = (HV *)SvRV (coro_current);
2218
2219 coro_set_status (aTHX_ SvSTATE ((SV *)coro_hv), arg, items);
2220 slf_init_terminate_cancel_common (aTHX_ frame, coro_hv);
2221}
2222
2223static void
2224slf_init_cancel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2225{
2226 HV *coro_hv;
2227 struct coro *coro;
2228
2229 if (items <= 0)
2230 croak ("Coro::cancel called without coro object,");
2231
2232 coro = SvSTATE (arg [0]);
2233 coro_hv = coro->hv;
2234
2235 coro_set_status (aTHX_ coro, arg + 1, items - 1);
2236
2237 if (ecb_expect_false (coro->flags & CF_NOCANCEL))
2238 {
2239 /* coro currently busy cancelling something, so just notify it */
2240 coro->slf_frame.data = (void *)coro;
2241
2242 frame->prepare = prepare_nop;
2243 frame->check = slf_check_nop;
2244 }
2245 else if (coro_hv == (HV *)SvRV (coro_current))
2246 {
2247 /* cancelling the current coro is allowed, and equals terminate */
2248 slf_init_terminate_cancel_common (aTHX_ frame, coro_hv);
2249 }
2250 else
2251 {
2252 struct coro *self = SvSTATE_current;
2253
2254 /* otherwise we cancel directly, purely for speed reasons
2255 * unfortunately, this requires some magic trickery, as
2256 * somebody else could cancel us, so we have to fight the cancellation.
2257 * this is ugly, and hopefully fully worth the extra speed.
2258 * besides, I can't get the slow-but-safe version working...
2259 */
2260 slf_frame.data = 0;
2261 self->flags |= CF_NOCANCEL;
2262 coro_state_destroy (aTHX_ coro);
2263 self->flags &= ~CF_NOCANCEL;
2264
2265 if (slf_frame.data)
2266 {
2267 /* while we were busy we have been cancelled, so terminate */
2268 slf_init_terminate_cancel_common (aTHX_ frame, self->hv);
2269 }
2270 else
2271 {
2272 frame->prepare = prepare_nop;
2273 frame->check = slf_check_nop;
2274 }
2275 }
2276}
2277
2278static int
2279slf_check_safe_cancel (pTHX_ struct CoroSLF *frame)
2280{
2281 frame->prepare = 0;
2282 coro_unwind_stacks (aTHX);
2283
2284 slf_init_terminate_cancel_common (aTHX_ frame, (HV *)SvRV (coro_current));
2285
2286 return 1;
2287}
2288
2289static int
2290safe_cancel (pTHX_ struct coro *coro, SV **arg, int items)
2291{
2292 if (coro->cctx)
2293 croak ("coro inside C callback, unable to cancel at this time, caught");
2294
2295 if (coro->flags & CF_NEW)
2296 {
2297 coro_set_status (aTHX_ coro, arg, items);
2298 coro_state_destroy (aTHX_ coro);
2299 }
2300 else
2301 {
2302 if (!coro->slf_frame.prepare)
2303 croak ("coro outside an SLF function, unable to cancel at this time, caught");
2304
2305 slf_destroy (aTHX_ coro);
2306
2307 coro_set_status (aTHX_ coro, arg, items);
2308 coro->slf_frame.prepare = prepare_nop;
2309 coro->slf_frame.check = slf_check_safe_cancel;
2310
2311 api_ready (aTHX_ (SV *)coro->hv);
2312 }
2313
2314 return 1;
1920} 2315}
1921 2316
1922/*****************************************************************************/ 2317/*****************************************************************************/
1923/* async pool handler */ 2318/* async pool handler */
1924 2319
1955slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2350slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1956{ 2351{
1957 HV *hv = (HV *)SvRV (coro_current); 2352 HV *hv = (HV *)SvRV (coro_current);
1958 struct coro *coro = SvSTATE_hv ((SV *)hv); 2353 struct coro *coro = SvSTATE_hv ((SV *)hv);
1959 2354
1960 if (expect_true (coro->saved_deffh)) 2355 if (ecb_expect_true (coro->saved_deffh))
1961 { 2356 {
1962 /* subsequent iteration */ 2357 /* subsequent iteration */
1963 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2358 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1964 coro->saved_deffh = 0; 2359 coro->saved_deffh = 0;
1965 2360
1966 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) 2361 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
1967 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2362 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1968 { 2363 {
1969 coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); 2364 slf_init_terminate_cancel_common (aTHX_ frame, hv);
1970 coro->invoke_av = newAV (); 2365 return;
1971
1972 frame->prepare = prepare_nop;
1973 } 2366 }
1974 else 2367 else
1975 { 2368 {
1976 av_clear (GvAV (PL_defgv)); 2369 av_clear (GvAV (PL_defgv));
1977 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); 2370 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0);
2027 2420
2028static int 2421static int
2029slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) 2422slf_check_rouse_wait (pTHX_ struct CoroSLF *frame)
2030{ 2423{
2031 SV *data = (SV *)frame->data; 2424 SV *data = (SV *)frame->data;
2032 2425
2033 if (CORO_THROW) 2426 if (CORO_THROW)
2034 return 0; 2427 return 0;
2035 2428
2036 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2429 if (SvTYPE (SvRV (data)) != SVt_PVAV)
2037 return 1; 2430 return 1;
2073 cb = sv_2mortal (coro->rouse_cb); 2466 cb = sv_2mortal (coro->rouse_cb);
2074 coro->rouse_cb = 0; 2467 coro->rouse_cb = 0;
2075 } 2468 }
2076 2469
2077 if (!SvROK (cb) 2470 if (!SvROK (cb)
2078 || SvTYPE (SvRV (cb)) != SVt_PVCV 2471 || SvTYPE (SvRV (cb)) != SVt_PVCV
2079 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) 2472 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
2080 croak ("Coro::rouse_wait called with illegal callback argument,"); 2473 croak ("Coro::rouse_wait called with illegal callback argument,");
2081 2474
2082 { 2475 {
2083 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ 2476 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */
2200static void 2593static void
2201slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2594slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2202{ 2595{
2203 frame->prepare = prepare_cede_notself; 2596 frame->prepare = prepare_cede_notself;
2204 frame->check = slf_check_nop; 2597 frame->check = slf_check_nop;
2598}
2599
2600/* "undo"/cancel a running slf call - used when cancelling a coro, mainly */
2601static void
2602slf_destroy (pTHX_ struct coro *coro)
2603{
2604 struct CoroSLF frame = coro->slf_frame;
2605
2606 /*
2607 * The on_destroy below most likely is from an SLF call.
2608 * Since by definition the SLF call will not finish when we destroy
2609 * the coro, we will have to force-finish it here, otherwise
2610 * cleanup functions cannot call SLF functions.
2611 */
2612 coro->slf_frame.prepare = 0;
2613
2614 /* this callback is reserved for slf functions needing to do cleanup */
2615 if (frame.destroy && frame.prepare && !PL_dirty)
2616 frame.destroy (aTHX_ &frame);
2205} 2617}
2206 2618
2207/* 2619/*
2208 * these not obviously related functions are all rolled into one 2620 * these not obviously related functions are all rolled into one
2209 * function to increase chances that they all will call transfer with the same 2621 * function to increase chances that they all will call transfer with the same
2216 I32 checkmark; /* mark SP to see how many elements check has pushed */ 2628 I32 checkmark; /* mark SP to see how many elements check has pushed */
2217 2629
2218 /* set up the slf frame, unless it has already been set-up */ 2630 /* set up the slf frame, unless it has already been set-up */
2219 /* the latter happens when a new coro has been started */ 2631 /* the latter happens when a new coro has been started */
2220 /* or when a new cctx was attached to an existing coroutine */ 2632 /* or when a new cctx was attached to an existing coroutine */
2221 if (expect_true (!slf_frame.prepare)) 2633 if (ecb_expect_true (!slf_frame.prepare))
2222 { 2634 {
2223 /* first iteration */ 2635 /* first iteration */
2224 dSP; 2636 dSP;
2225 SV **arg = PL_stack_base + TOPMARK + 1; 2637 SV **arg = PL_stack_base + TOPMARK + 1;
2226 int items = SP - arg; /* args without function object */ 2638 int items = SP - arg; /* args without function object */
2267 while (slf_frame.check (aTHX_ &slf_frame)); 2679 while (slf_frame.check (aTHX_ &slf_frame));
2268 2680
2269 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ 2681 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */
2270 2682
2271 /* exception handling */ 2683 /* exception handling */
2272 if (expect_false (CORO_THROW)) 2684 if (ecb_expect_false (CORO_THROW))
2273 { 2685 {
2274 SV *exception = sv_2mortal (CORO_THROW); 2686 SV *exception = sv_2mortal (CORO_THROW);
2275 2687
2276 CORO_THROW = 0; 2688 CORO_THROW = 0;
2277 sv_setsv (ERRSV, exception); 2689 sv_setsv (ERRSV, exception);
2278 croak (0); 2690 croak (0);
2279 } 2691 }
2280 2692
2281 /* return value handling - mostly like entersub */ 2693 /* return value handling - mostly like entersub */
2282 /* make sure we put something on the stack in scalar context */ 2694 /* make sure we put something on the stack in scalar context */
2283 if (GIMME_V == G_SCALAR) 2695 if (GIMME_V == G_SCALAR
2696 && ecb_expect_false (PL_stack_sp != PL_stack_base + checkmark + 1))
2284 { 2697 {
2285 dSP; 2698 dSP;
2286 SV **bot = PL_stack_base + checkmark; 2699 SV **bot = PL_stack_base + checkmark;
2287 2700
2288 if (sp == bot) /* too few, push undef */ 2701 if (sp == bot) /* too few, push undef */
2289 bot [1] = &PL_sv_undef; 2702 bot [1] = &PL_sv_undef;
2290 else if (sp != bot + 1) /* too many, take last one */ 2703 else /* too many, take last one */
2291 bot [1] = *sp; 2704 bot [1] = *sp;
2292 2705
2293 SP = bot + 1; 2706 SP = bot + 1;
2294 2707
2295 PUTBACK; 2708 PUTBACK;
2328 if (PL_op->op_flags & OPf_STACKED) 2741 if (PL_op->op_flags & OPf_STACKED)
2329 { 2742 {
2330 if (items > slf_arga) 2743 if (items > slf_arga)
2331 { 2744 {
2332 slf_arga = items; 2745 slf_arga = items;
2333 free (slf_argv); 2746 Safefree (slf_argv);
2334 slf_argv = malloc (slf_arga * sizeof (SV *)); 2747 New (0, slf_argv, slf_arga, SV *);
2335 } 2748 }
2336 2749
2337 slf_argc = items; 2750 slf_argc = items;
2338 2751
2339 for (i = 0; i < items; ++i) 2752 for (i = 0; i < items; ++i)
2402{ 2815{
2403 PerlIOBuf base; 2816 PerlIOBuf base;
2404 NV next, every; 2817 NV next, every;
2405} PerlIOCede; 2818} PerlIOCede;
2406 2819
2407static IV 2820static IV ecb_cold
2408PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) 2821PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
2409{ 2822{
2410 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 2823 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
2411 2824
2412 self->every = SvCUR (arg) ? SvNV (arg) : 0.01; 2825 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
2413 self->next = nvtime () + self->every; 2826 self->next = nvtime () + self->every;
2414 2827
2415 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); 2828 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
2416} 2829}
2417 2830
2418static SV * 2831static SV * ecb_cold
2419PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) 2832PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
2420{ 2833{
2421 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 2834 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
2422 2835
2423 return newSVnv (self->every); 2836 return newSVnv (self->every);
2535 SvREFCNT_dec (cb); 2948 SvREFCNT_dec (cb);
2536 } 2949 }
2537} 2950}
2538 2951
2539static void 2952static void
2540coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2953coro_semaphore_destroy (pTHX_ struct CoroSLF *frame)
2541{ 2954{
2542 /* call $sem->adjust (0) to possibly wake up some other waiters */ 2955 /* call $sem->adjust (0) to possibly wake up some other waiters */
2543 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); 2956 coro_semaphore_adjust (aTHX_ (AV *)frame->data, 0);
2544} 2957}
2545 2958
2546static int 2959static int
2547slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire) 2960slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire)
2548{ 2961{
2549 AV *av = (AV *)frame->data; 2962 AV *av = (AV *)frame->data;
2550 SV *count_sv = AvARRAY (av)[0]; 2963 SV *count_sv = AvARRAY (av)[0];
2964 SV *coro_hv = SvRV (coro_current);
2965
2966 frame->destroy = 0;
2551 2967
2552 /* if we are about to throw, don't actually acquire the lock, just throw */ 2968 /* if we are about to throw, don't actually acquire the lock, just throw */
2553 if (CORO_THROW) 2969 if (ecb_expect_false (CORO_THROW))
2970 {
2971 /* we still might be responsible for the semaphore, so wake up others */
2972 coro_semaphore_adjust (aTHX_ av, 0);
2973
2554 return 0; 2974 return 0;
2975 }
2555 else if (SvIVX (count_sv) > 0) 2976 else if (SvIVX (count_sv) > 0)
2556 { 2977 {
2557 SvSTATE_current->on_destroy = 0;
2558
2559 if (acquire) 2978 if (acquire)
2560 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2979 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2561 else 2980 else
2562 coro_semaphore_adjust (aTHX_ av, 0); 2981 coro_semaphore_adjust (aTHX_ av, 0);
2563 2982
2567 { 2986 {
2568 int i; 2987 int i;
2569 /* if we were woken up but can't down, we look through the whole */ 2988 /* if we were woken up but can't down, we look through the whole */
2570 /* waiters list and only add us if we aren't in there already */ 2989 /* waiters list and only add us if we aren't in there already */
2571 /* this avoids some degenerate memory usage cases */ 2990 /* this avoids some degenerate memory usage cases */
2572 2991 for (i = AvFILLp (av); i > 0; --i) /* i > 0 is not an off-by-one bug */
2573 for (i = 1; i <= AvFILLp (av); ++i)
2574 if (AvARRAY (av)[i] == SvRV (coro_current)) 2992 if (AvARRAY (av)[i] == coro_hv)
2575 return 1; 2993 return 1;
2576 2994
2577 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2995 av_push (av, SvREFCNT_inc (coro_hv));
2578 return 1; 2996 return 1;
2579 } 2997 }
2580} 2998}
2581 2999
2582static int 3000static int
2605 { 3023 {
2606 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 3024 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2607 3025
2608 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); 3026 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2609 frame->prepare = prepare_schedule; 3027 frame->prepare = prepare_schedule;
2610
2611 /* to avoid race conditions when a woken-up coro gets terminated */ 3028 /* to avoid race conditions when a woken-up coro gets terminated */
2612 /* we arrange for a temporary on_destroy that calls adjust (0) */ 3029 /* we arrange for a temporary on_destroy that calls adjust (0) */
2613 SvSTATE_current->on_destroy = coro_semaphore_on_destroy; 3030 frame->destroy = coro_semaphore_destroy;
2614 } 3031 }
2615} 3032}
2616 3033
2617static void 3034static void
2618slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 3035slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2700 { 3117 {
2701 SV *cb_cv = s_get_cv_croak (arg [1]); 3118 SV *cb_cv = s_get_cv_croak (arg [1]);
2702 av_push (av, SvREFCNT_inc_NN (cb_cv)); 3119 av_push (av, SvREFCNT_inc_NN (cb_cv));
2703 3120
2704 if (SvIVX (AvARRAY (av)[0])) 3121 if (SvIVX (AvARRAY (av)[0]))
2705 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ 3122 coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */
2706 3123
2707 frame->prepare = prepare_nop; 3124 frame->prepare = prepare_nop;
2708 frame->check = slf_check_nop; 3125 frame->check = slf_check_nop;
2709 } 3126 }
2710 else if (SvIVX (AvARRAY (av)[0])) 3127 else if (SvIVX (AvARRAY (av)[0]))
2787 /* it quickly returns */ 3204 /* it quickly returns */
2788 if (CORO_THROW) 3205 if (CORO_THROW)
2789 return 0; 3206 return 0;
2790 3207
2791 /* one element that is an RV? repeat! */ 3208 /* one element that is an RV? repeat! */
2792 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) 3209 if (AvFILLp (state) == 0 && SvTYPE (AvARRAY (state)[0]) != SVt_PV)
2793 return 1; 3210 return 1;
2794 3211
2795 /* restore status */ 3212 /* restore status */
2796 { 3213 {
2797 SV *data_sv = av_pop (state); 3214 SV *data_sv = av_pop (state);
2836 dSP; 3253 dSP;
2837 3254
2838 static SV *prio_cv; 3255 static SV *prio_cv;
2839 static SV *prio_sv; 3256 static SV *prio_sv;
2840 3257
2841 if (expect_false (!prio_cv)) 3258 if (ecb_expect_false (!prio_cv))
2842 { 3259 {
2843 prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0); 3260 prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0);
2844 prio_sv = newSViv (0); 3261 prio_sv = newSViv (0);
2845 } 3262 }
2846 3263
2872 /* now call the AIO function - we assume our request is uncancelable */ 3289 /* now call the AIO function - we assume our request is uncancelable */
2873 PUTBACK; 3290 PUTBACK;
2874 call_sv ((SV *)req, G_VOID | G_DISCARD); 3291 call_sv ((SV *)req, G_VOID | G_DISCARD);
2875 } 3292 }
2876 3293
2877 /* now that the requets is going, we loop toll we have a result */ 3294 /* now that the request is going, we loop till we have a result */
2878 frame->data = (void *)state; 3295 frame->data = (void *)state;
2879 frame->prepare = prepare_schedule; 3296 frame->prepare = prepare_schedule;
2880 frame->check = slf_check_aio_req; 3297 frame->check = slf_check_aio_req;
2881} 3298}
2882 3299
2892 3309
2893/*****************************************************************************/ 3310/*****************************************************************************/
2894 3311
2895#if CORO_CLONE 3312#if CORO_CLONE
2896# include "clone.c" 3313# include "clone.c"
3314#endif
3315
3316/*****************************************************************************/
3317
3318static SV *
3319coro_new (pTHX_ HV *stash, SV **argv, int argc, int is_coro)
3320{
3321 SV *coro_sv;
3322 struct coro *coro;
3323 MAGIC *mg;
3324 HV *hv;
3325 SV *cb;
3326 int i;
3327
3328 if (argc > 0)
3329 {
3330 cb = s_get_cv_croak (argv [0]);
3331
3332 if (!is_coro)
3333 {
3334 if (CvISXSUB (cb))
3335 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
3336
3337 if (!CvROOT (cb))
3338 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
3339 }
3340 }
3341
3342 Newz (0, coro, 1, struct coro);
3343 coro->args = newAV ();
3344 coro->flags = CF_NEW;
3345
3346 if (coro_first) coro_first->prev = coro;
3347 coro->next = coro_first;
3348 coro_first = coro;
3349
3350 coro->hv = hv = newHV ();
3351 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
3352 mg->mg_flags |= MGf_DUP;
3353 coro_sv = sv_bless (newRV_noinc ((SV *)hv), stash);
3354
3355 if (argc > 0)
3356 {
3357 av_extend (coro->args, argc + is_coro - 1);
3358
3359 if (is_coro)
3360 {
3361 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
3362 cb = (SV *)cv_coro_run;
3363 }
3364
3365 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
3366
3367 for (i = 1; i < argc; i++)
3368 av_push (coro->args, newSVsv (argv [i]));
3369 }
3370
3371 return coro_sv;
3372}
3373
3374#ifndef __cplusplus
3375ecb_cold XS(boot_Coro__State);
3376#endif
3377
3378#if CORO_JIT
3379
3380static void ecb_noinline ecb_cold
3381pushav_4uv (pTHX_ UV a, UV b, UV c, UV d)
3382{
3383 dSP;
3384 AV *av = newAV ();
3385
3386 av_store (av, 3, newSVuv (d));
3387 av_store (av, 2, newSVuv (c));
3388 av_store (av, 1, newSVuv (b));
3389 av_store (av, 0, newSVuv (a));
3390
3391 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
3392
3393 PUTBACK;
3394}
3395
3396static void ecb_noinline ecb_cold
3397jit_init (pTHX)
3398{
3399 dSP;
3400 SV *load, *save;
3401 char *map_base;
3402 char *load_ptr, *save_ptr;
3403 STRLEN load_len, save_len, map_len;
3404 int count;
3405
3406 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1);
3407
3408 PUSHMARK (SP);
3409 #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type));
3410 #include "state.h"
3411 count = call_pv ("Coro::State::_jit", G_ARRAY);
3412 SPAGAIN;
3413
3414 save = POPs; save_ptr = SvPVbyte (save, save_len);
3415 load = POPs; load_ptr = SvPVbyte (load, load_len);
3416
3417 map_len = load_len + save_len + 16;
3418
3419 map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3420
3421 assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED));
3422
3423 load_perl_slots = (load_save_perl_slots_type)map_base;
3424 memcpy (map_base, load_ptr, load_len);
3425
3426 map_base += (load_len + 15) & ~15;
3427
3428 save_perl_slots = (load_save_perl_slots_type)map_base;
3429 memcpy (map_base, save_ptr, save_len);
3430
3431 /* we are good citizens and try to make the page read-only, so the evil evil */
3432 /* hackers might have it a bit more difficult */
3433 mprotect (map_base, map_len, PROT_READ | PROT_EXEC);
3434
3435 PUTBACK;
3436 eval_pv ("undef &Coro::State::_jit", 1);
3437}
3438
2897#endif 3439#endif
2898 3440
2899MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 3441MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2900 3442
2901PROTOTYPES: DISABLE 3443PROTOTYPES: DISABLE
2905#ifdef USE_ITHREADS 3447#ifdef USE_ITHREADS
2906# if CORO_PTHREAD 3448# if CORO_PTHREAD
2907 coro_thx = PERL_GET_CONTEXT; 3449 coro_thx = PERL_GET_CONTEXT;
2908# endif 3450# endif
2909#endif 3451#endif
2910 BOOT_PAGESIZE; 3452 /* perl defines these to check for existance first, but why it doesn't */
3453 /* just create them one at init time is not clear to me, except for */
3454 /* programs trying to delete them, but... */
3455 /* anyway, we declare this as invalid and make sure they are initialised here */
3456 DEFSV;
3457 ERRSV;
2911 3458
2912 cctx_current = cctx_new_empty (); 3459 cctx_current = cctx_new_empty ();
2913 3460
2914 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 3461 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2915 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 3462 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2955 coroapi.prepare_nop = prepare_nop; 3502 coroapi.prepare_nop = prepare_nop;
2956 coroapi.prepare_schedule = prepare_schedule; 3503 coroapi.prepare_schedule = prepare_schedule;
2957 coroapi.prepare_cede = prepare_cede; 3504 coroapi.prepare_cede = prepare_cede;
2958 coroapi.prepare_cede_notself = prepare_cede_notself; 3505 coroapi.prepare_cede_notself = prepare_cede_notself;
2959 3506
2960 { 3507 time_init (aTHX);
2961 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
2962
2963 if (!svp) croak ("Time::HiRes is required");
2964 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
2965
2966 nvtime = INT2PTR (double (*)(), SvIV (*svp));
2967
2968 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
2969 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
2970 }
2971 3508
2972 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3509 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3510#if CORO_JIT
3511 PUTBACK;
3512 jit_init (aTHX);
3513 SPAGAIN;
3514#endif
2973} 3515}
2974 3516
2975SV * 3517SV *
2976new (char *klass, ...) 3518new (SV *klass, ...)
2977 ALIAS: 3519 ALIAS:
2978 Coro::new = 1 3520 Coro::new = 1
2979 CODE: 3521 CODE:
2980{ 3522 RETVAL = coro_new (aTHX_ ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix);
2981 struct coro *coro; 3523 OUTPUT:
2982 MAGIC *mg;
2983 HV *hv;
2984 SV *cb;
2985 int i;
2986
2987 if (items > 1)
2988 {
2989 cb = s_get_cv_croak (ST (1));
2990
2991 if (!ix)
2992 {
2993 if (CvISXSUB (cb))
2994 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2995
2996 if (!CvROOT (cb))
2997 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
2998 }
2999 }
3000
3001 Newz (0, coro, 1, struct coro);
3002 coro->args = newAV ();
3003 coro->flags = CF_NEW;
3004
3005 if (coro_first) coro_first->prev = coro;
3006 coro->next = coro_first;
3007 coro_first = coro;
3008
3009 coro->hv = hv = newHV ();
3010 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
3011 mg->mg_flags |= MGf_DUP;
3012 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
3013
3014 if (items > 1)
3015 {
3016 av_extend (coro->args, items - 1 + ix - 1);
3017
3018 if (ix)
3019 {
3020 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
3021 cb = (SV *)cv_coro_run;
3022 }
3023
3024 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
3025
3026 for (i = 2; i < items; i++)
3027 av_push (coro->args, newSVsv (ST (i)));
3028 }
3029}
3030 OUTPUT:
3031 RETVAL 3524 RETVAL
3032 3525
3033void 3526void
3034transfer (...) 3527transfer (...)
3035 PROTOTYPE: $$ 3528 PROTOTYPE: $$
3036 CODE: 3529 CODE:
3037 CORO_EXECUTE_SLF_XS (slf_init_transfer); 3530 CORO_EXECUTE_SLF_XS (slf_init_transfer);
3038
3039bool
3040_destroy (SV *coro_sv)
3041 CODE:
3042 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
3043 OUTPUT:
3044 RETVAL
3045
3046void
3047_exit (int code)
3048 PROTOTYPE: $
3049 CODE:
3050 _exit (code);
3051 3531
3052SV * 3532SV *
3053clone (Coro::State coro) 3533clone (Coro::State coro)
3054 CODE: 3534 CODE:
3055{ 3535{
3110void 3590void
3111list () 3591list ()
3112 PROTOTYPE: 3592 PROTOTYPE:
3113 PPCODE: 3593 PPCODE:
3114{ 3594{
3115 struct coro *coro; 3595 struct coro *coro;
3116 for (coro = coro_first; coro; coro = coro->next) 3596 for (coro = coro_first; coro; coro = coro->next)
3117 if (coro->hv) 3597 if (coro->hv)
3118 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 3598 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
3119} 3599}
3120 3600
3125 CODE: 3605 CODE:
3126{ 3606{
3127 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3607 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
3128 { 3608 {
3129 struct coro *current = SvSTATE_current; 3609 struct coro *current = SvSTATE_current;
3610 struct CoroSLF slf_save;
3130 3611
3131 if (current != coro) 3612 if (current != coro)
3132 { 3613 {
3133 PUTBACK; 3614 PUTBACK;
3134 save_perl (aTHX_ current); 3615 save_perl (aTHX_ current);
3135 load_perl (aTHX_ coro); 3616 load_perl (aTHX_ coro);
3617 /* the coro is most likely in an active SLF call.
3618 * while not strictly required (the code we execute is
3619 * not allowed to call any SLF functions), it's cleaner
3620 * to reinitialise the slf_frame and restore it later.
3621 * This might one day allow us to actually do SLF calls
3622 * from code executed here.
3623 */
3624 slf_save = slf_frame;
3625 slf_frame.prepare = 0;
3136 SPAGAIN; 3626 SPAGAIN;
3137 } 3627 }
3138 3628
3139 PUSHSTACK; 3629 PUSHSTACK;
3140 3630
3150 SPAGAIN; 3640 SPAGAIN;
3151 3641
3152 if (current != coro) 3642 if (current != coro)
3153 { 3643 {
3154 PUTBACK; 3644 PUTBACK;
3645 slf_frame = slf_save;
3155 save_perl (aTHX_ coro); 3646 save_perl (aTHX_ coro);
3156 load_perl (aTHX_ current); 3647 load_perl (aTHX_ current);
3157 SPAGAIN; 3648 SPAGAIN;
3158 } 3649 }
3159 } 3650 }
3164 PROTOTYPE: $ 3655 PROTOTYPE: $
3165 ALIAS: 3656 ALIAS:
3166 is_ready = CF_READY 3657 is_ready = CF_READY
3167 is_running = CF_RUNNING 3658 is_running = CF_RUNNING
3168 is_new = CF_NEW 3659 is_new = CF_NEW
3169 is_destroyed = CF_DESTROYED 3660 is_destroyed = CF_ZOMBIE
3661 is_zombie = CF_ZOMBIE
3170 is_suspended = CF_SUSPENDED 3662 is_suspended = CF_SUSPENDED
3171 CODE: 3663 CODE:
3172 RETVAL = boolSV (coro->flags & ix); 3664 RETVAL = boolSV (coro->flags & ix);
3173 OUTPUT: 3665 OUTPUT:
3174 RETVAL 3666 RETVAL
3175 3667
3176void 3668void
3177throw (Coro::State self, SV *throw = &PL_sv_undef) 3669throw (SV *self, SV *exception = &PL_sv_undef)
3178 PROTOTYPE: $;$ 3670 PROTOTYPE: $;$
3179 CODE: 3671 CODE:
3180{ 3672{
3673 struct coro *coro = SvSTATE (self);
3181 struct coro *current = SvSTATE_current; 3674 struct coro *current = SvSTATE_current;
3182 SV **throwp = self == current ? &CORO_THROW : &self->except; 3675 SV **exceptionp = coro == current ? &CORO_THROW : &coro->except;
3183 SvREFCNT_dec (*throwp); 3676 SvREFCNT_dec (*exceptionp);
3184 SvGETMAGIC (throw); 3677 SvGETMAGIC (exception);
3185 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3678 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3679
3680 api_ready (aTHX_ self);
3186} 3681}
3187 3682
3188void 3683void
3189api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3684api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3190 PROTOTYPE: $;$ 3685 PROTOTYPE: $;$
3245 3740
3246void 3741void
3247cancel (Coro::State self) 3742cancel (Coro::State self)
3248 CODE: 3743 CODE:
3249 coro_state_destroy (aTHX_ self); 3744 coro_state_destroy (aTHX_ self);
3250 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3251
3252 3745
3253SV * 3746SV *
3254enable_times (int enabled = enable_times) 3747enable_times (int enabled = enable_times)
3255 CODE: 3748 CODE:
3256{ 3749{
3269 3762
3270void 3763void
3271times (Coro::State self) 3764times (Coro::State self)
3272 PPCODE: 3765 PPCODE:
3273{ 3766{
3274 struct coro *current = SvSTATE (coro_current); 3767 struct coro *current = SvSTATE (coro_current);
3275 3768
3276 if (expect_false (current == self)) 3769 if (ecb_expect_false (current == self))
3277 { 3770 {
3278 coro_times_update (); 3771 coro_times_update ();
3279 coro_times_add (SvSTATE (coro_current)); 3772 coro_times_add (SvSTATE (coro_current));
3280 } 3773 }
3281 3774
3282 EXTEND (SP, 2); 3775 EXTEND (SP, 2);
3283 PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); 3776 PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9)));
3284 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); 3777 PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9)));
3285 3778
3286 if (expect_false (current == self)) 3779 if (ecb_expect_false (current == self))
3287 coro_times_sub (SvSTATE (coro_current)); 3780 coro_times_sub (SvSTATE (coro_current));
3288} 3781}
3782
3783void
3784swap_sv (Coro::State coro, SV *sv, SV *swapsv)
3785 CODE:
3786{
3787 struct coro *current = SvSTATE_current;
3788
3789 if (current == coro)
3790 SWAP_SVS (current);
3791
3792 if (!coro->swap_sv)
3793 coro->swap_sv = newAV ();
3794
3795 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv )));
3796 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv)));
3797
3798 if (current == coro)
3799 SWAP_SVS (current);
3800}
3801
3289 3802
3290MODULE = Coro::State PACKAGE = Coro 3803MODULE = Coro::State PACKAGE = Coro
3291 3804
3292BOOT: 3805BOOT:
3293{ 3806{
3294 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3807 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3295 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3808 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3296 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); 3809 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3297 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3298 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3810 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3299 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3811 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3300 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3812 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3301 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3813 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3302 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); 3814 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3303 3815
3304 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3816 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3305 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3817 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3306 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3818 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3307 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3819 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */
3308 3820
3309 coro_stash = gv_stashpv ("Coro", TRUE); 3821 coro_stash = gv_stashpv ("Coro", TRUE);
3310 3822
3311 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); 3823 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3312 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); 3824 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3331 sv_setiv (sv, (IV)&coroapi); 3843 sv_setiv (sv, (IV)&coroapi);
3332 SvREADONLY_on (sv); 3844 SvREADONLY_on (sv);
3333 } 3845 }
3334} 3846}
3335 3847
3848SV *
3849async (...)
3850 PROTOTYPE: &@
3851 CODE:
3852 RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1);
3853 api_ready (aTHX_ RETVAL);
3854 OUTPUT:
3855 RETVAL
3856
3857void
3858_destroy (Coro::State coro)
3859 CODE:
3860 /* used by the manager thread */
3861 coro_state_destroy (aTHX_ coro);
3862
3863void
3864on_destroy (Coro::State coro, SV *cb)
3865 CODE:
3866 coro_push_on_destroy (aTHX_ coro, newSVsv (cb));
3867
3868void
3869join (...)
3870 CODE:
3871 CORO_EXECUTE_SLF_XS (slf_init_join);
3872
3336void 3873void
3337terminate (...) 3874terminate (...)
3338 CODE: 3875 CODE:
3339 CORO_EXECUTE_SLF_XS (slf_init_terminate); 3876 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3877
3878void
3879cancel (...)
3880 CODE:
3881 CORO_EXECUTE_SLF_XS (slf_init_cancel);
3882
3883int
3884safe_cancel (Coro::State self, ...)
3885 C_ARGS: aTHX_ self, &ST (1), items - 1
3340 3886
3341void 3887void
3342schedule (...) 3888schedule (...)
3343 CODE: 3889 CODE:
3344 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3890 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3374_set_readyhook (SV *hook) 3920_set_readyhook (SV *hook)
3375 PROTOTYPE: $ 3921 PROTOTYPE: $
3376 CODE: 3922 CODE:
3377 SvREFCNT_dec (coro_readyhook); 3923 SvREFCNT_dec (coro_readyhook);
3378 SvGETMAGIC (hook); 3924 SvGETMAGIC (hook);
3925 if (SvOK (hook))
3926 {
3379 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; 3927 coro_readyhook = newSVsv (hook);
3928 CORO_READYHOOK = invoke_sv_ready_hook_helper;
3929 }
3930 else
3931 {
3932 coro_readyhook = 0;
3933 CORO_READYHOOK = 0;
3934 }
3380 3935
3381int 3936int
3382prio (Coro::State coro, int newprio = 0) 3937prio (Coro::State coro, int newprio = 0)
3383 PROTOTYPE: $;$ 3938 PROTOTYPE: $;$
3384 ALIAS: 3939 ALIAS:
3448 for (i = 1; i < items; ++i) 4003 for (i = 1; i < items; ++i)
3449 av_push (av, SvREFCNT_inc_NN (ST (i))); 4004 av_push (av, SvREFCNT_inc_NN (ST (i)));
3450 4005
3451 if ((SV *)hv == &PL_sv_undef) 4006 if ((SV *)hv == &PL_sv_undef)
3452 { 4007 {
3453 PUSHMARK (SP); 4008 SV *sv = coro_new (aTHX_ coro_stash, (SV **)&cv_pool_handler, 1, 1);
3454 EXTEND (SP, 2);
3455 PUSHs (sv_Coro);
3456 PUSHs ((SV *)cv_pool_handler);
3457 PUTBACK;
3458 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3459 SPAGAIN;
3460
3461 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); 4009 hv = (HV *)SvREFCNT_inc_NN (SvRV (sv));
4010 SvREFCNT_dec (sv);
3462 } 4011 }
3463 4012
3464 { 4013 {
3465 struct coro *coro = SvSTATE_hv (hv); 4014 struct coro *coro = SvSTATE_hv (hv);
3466 4015
3498 on_leave = 1 4047 on_leave = 1
3499 PROTOTYPE: & 4048 PROTOTYPE: &
3500 CODE: 4049 CODE:
3501{ 4050{
3502 struct coro *coro = SvSTATE_current; 4051 struct coro *coro = SvSTATE_current;
3503 AV **avp = ix ? &coro->on_leave : &coro->on_enter; 4052 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3504 4053
3505 block = s_get_cv_croak (block); 4054 block = s_get_cv_croak (block);
3506 4055
3507 if (!*avp) 4056 if (!*avp)
3508 *avp = newAV (); 4057 *avp = newAV ();
3528 4077
3529SV * 4078SV *
3530new (SV *klass, SV *count = 0) 4079new (SV *klass, SV *count = 0)
3531 CODE: 4080 CODE:
3532{ 4081{
3533 int semcnt = 1; 4082 int semcnt = 1;
3534 4083
3535 if (count) 4084 if (count)
3536 { 4085 {
3537 SvGETMAGIC (count); 4086 SvGETMAGIC (count);
3538 4087
3598 XSRETURN_NO; 4147 XSRETURN_NO;
3599} 4148}
3600 4149
3601void 4150void
3602waiters (SV *self) 4151waiters (SV *self)
3603 PPCODE: 4152 PPCODE:
3604{ 4153{
3605 AV *av = (AV *)SvRV (self); 4154 AV *av = (AV *)SvRV (self);
3606 int wcount = AvFILLp (av) + 1 - 1; 4155 int wcount = AvFILLp (av) + 1 - 1;
3607 4156
3608 if (GIMME_V == G_SCALAR) 4157 if (GIMME_V == G_SCALAR)
3617} 4166}
3618 4167
3619MODULE = Coro::State PACKAGE = Coro::SemaphoreSet 4168MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3620 4169
3621void 4170void
3622_may_delete (SV *sem, int count, int extra_refs) 4171_may_delete (SV *sem, int count, unsigned int extra_refs)
3623 PPCODE: 4172 PPCODE:
3624{ 4173{
3625 AV *av = (AV *)SvRV (sem); 4174 AV *av = (AV *)SvRV (sem);
3626 4175
3627 if (SvREFCNT ((SV *)av) == 1 + extra_refs 4176 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3628 && AvFILLp (av) == 0 /* no waiters, just count */ 4177 && AvFILLp (av) == 0 /* no waiters, just count */
3629 && SvIV (AvARRAY (av)[0]) == count) 4178 && SvIV (AvARRAY (av)[0]) == count)
3630 XSRETURN_YES; 4179 XSRETURN_YES;
3651 4200
3652void 4201void
3653broadcast (SV *self) 4202broadcast (SV *self)
3654 CODE: 4203 CODE:
3655{ 4204{
3656 AV *av = (AV *)SvRV (self); 4205 AV *av = (AV *)SvRV (self);
3657 coro_signal_wake (aTHX_ av, AvFILLp (av)); 4206 coro_signal_wake (aTHX_ av, AvFILLp (av));
3658} 4207}
3659 4208
3660void 4209void
3661send (SV *self) 4210send (SV *self)
3669 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ 4218 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */
3670} 4219}
3671 4220
3672IV 4221IV
3673awaited (SV *self) 4222awaited (SV *self)
3674 CODE: 4223 CODE:
3675 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; 4224 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1;
3676 OUTPUT: 4225 OUTPUT:
3677 RETVAL 4226 RETVAL
3678 4227
3679 4228
3684 4233
3685void 4234void
3686_schedule (...) 4235_schedule (...)
3687 CODE: 4236 CODE:
3688{ 4237{
3689 static int incede; 4238 static int incede;
3690 4239
3691 api_cede_notself (aTHX); 4240 api_cede_notself (aTHX);
3692 4241
3693 ++incede; 4242 ++incede;
3694 while (coro_nready >= incede && api_cede (aTHX)) 4243 while (coro_nready >= incede && api_cede (aTHX))
3717 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 4266 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3718 sv_setpv ((SV *)slf_cv, proto); 4267 sv_setpv ((SV *)slf_cv, proto);
3719 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 4268 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3720} 4269}
3721 4270
4271MODULE = Coro::State PACKAGE = Coro::Select
4272
4273void
4274patch_pp_sselect ()
4275 CODE:
4276 if (!coro_old_pp_sselect)
4277 {
4278 coro_select_select = (SV *)get_cv ("Coro::Select::select", 0);
4279 coro_old_pp_sselect = PL_ppaddr [OP_SSELECT];
4280 PL_ppaddr [OP_SSELECT] = coro_pp_sselect;
4281 }
4282
4283void
4284unpatch_pp_sselect ()
4285 CODE:
4286 if (coro_old_pp_sselect)
4287 {
4288 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
4289 coro_old_pp_sselect = 0;
4290 }
4291
4292MODULE = Coro::State PACKAGE = Coro::Util
4293
4294void
4295_exit (int code)
4296 CODE:
4297 _exit (code);
4298
4299NV
4300time ()
4301 CODE:
4302 RETVAL = nvtime (aTHX);
4303 OUTPUT:
4304 RETVAL
4305
4306NV
4307gettimeofday ()
4308 PPCODE:
4309{
4310 UV tv [2];
4311 u2time (aTHX_ tv);
4312 EXTEND (SP, 2);
4313 PUSHs (sv_2mortal (newSVuv (tv [0])));
4314 PUSHs (sv_2mortal (newSVuv (tv [1])));
4315}
4316

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines