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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines