1 | /* this works around a bug in mingw32 providing a non-working setjmp */ |
1 | /* this works around a bug in mingw32 providing a non-working setjmp */ |
2 | #define USE_NO_MINGW_SETJMP_TWO_ARGS |
2 | #define USE_NO_MINGW_SETJMP_TWO_ARGS |
3 | |
3 | |
4 | #define NDEBUG 1 |
4 | #define NDEBUG 1 /* perl usually disables NDEBUG later */ |
5 | |
5 | |
6 | #include "libcoro/coro.c" |
6 | #include "libcoro/coro.c" |
7 | |
7 | |
8 | #define PERL_NO_GET_CONTEXT |
8 | #define PERL_NO_GET_CONTEXT |
9 | #define PERL_EXT |
9 | #define PERL_EXT |
… | |
… | |
13 | #include "XSUB.h" |
13 | #include "XSUB.h" |
14 | #include "perliol.h" |
14 | #include "perliol.h" |
15 | |
15 | |
16 | #include "schmorp.h" |
16 | #include "schmorp.h" |
17 | |
17 | |
|
|
18 | #define ECB_NO_THREADS 1 |
|
|
19 | #include "ecb.h" |
|
|
20 | |
|
|
21 | #include <stddef.h> |
18 | #include <stdio.h> |
22 | #include <stdio.h> |
19 | #include <errno.h> |
23 | #include <errno.h> |
20 | #include <assert.h> |
24 | #include <assert.h> |
21 | |
25 | |
22 | #ifndef SVs_PADSTALE |
26 | #ifndef SVs_PADSTALE |
23 | # define SVs_PADSTALE 0 |
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 |
|
|
34 | typedef AV PADNAMELIST; |
|
|
35 | # if !PERL_VERSION_ATLEAST(5,8,0) |
|
|
36 | typedef AV PADLIST; |
|
|
37 | typedef 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 |
24 | #endif |
51 | #endif |
25 | |
52 | |
26 | #if defined(_WIN32) |
53 | #if defined(_WIN32) |
27 | # undef HAS_GETTIMEOFDAY |
54 | # undef HAS_GETTIMEOFDAY |
28 | # undef setjmp |
55 | # undef setjmp |
… | |
… | |
31 | # define setjmp _setjmp /* deep magic */ |
58 | # define setjmp _setjmp /* deep magic */ |
32 | #else |
59 | #else |
33 | # include <inttypes.h> /* most portable stdint.h */ |
60 | # include <inttypes.h> /* most portable stdint.h */ |
34 | #endif |
61 | #endif |
35 | |
62 | |
36 | #ifdef HAVE_MMAP |
|
|
37 | # include <unistd.h> |
|
|
38 | # include <sys/mman.h> |
|
|
39 | # ifndef MAP_ANONYMOUS |
|
|
40 | # ifdef MAP_ANON |
|
|
41 | # define MAP_ANONYMOUS MAP_ANON |
|
|
42 | # else |
|
|
43 | # undef HAVE_MMAP |
|
|
44 | # endif |
|
|
45 | # endif |
|
|
46 | # include <limits.h> |
|
|
47 | # ifndef PAGESIZE |
|
|
48 | # define PAGESIZE pagesize |
|
|
49 | # define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) |
|
|
50 | static long pagesize; |
|
|
51 | # else |
|
|
52 | # define BOOT_PAGESIZE (void)0 |
|
|
53 | # endif |
|
|
54 | #else |
|
|
55 | # define PAGESIZE 0 |
|
|
56 | # define BOOT_PAGESIZE (void)0 |
|
|
57 | #endif |
|
|
58 | |
|
|
59 | #if CORO_USE_VALGRIND |
|
|
60 | # include <valgrind/valgrind.h> |
|
|
61 | #endif |
|
|
62 | |
|
|
63 | /* the maximum number of idle cctx that will be pooled */ |
63 | /* the maximum number of idle cctx that will be pooled */ |
64 | static int cctx_max_idle = 4; |
64 | static int cctx_max_idle = 4; |
65 | |
65 | |
66 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
66 | #if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0) |
67 | # undef CORO_STACKGUARD |
67 | # define HAS_SCOPESTACK_NAME 1 |
68 | #endif |
|
|
69 | |
|
|
70 | #ifndef CORO_STACKGUARD |
|
|
71 | # define CORO_STACKGUARD 0 |
|
|
72 | #endif |
68 | #endif |
73 | |
69 | |
74 | /* prefer perl internal functions over our own? */ |
70 | /* prefer perl internal functions over our own? */ |
75 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
71 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
76 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
72 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
… | |
… | |
86 | # define STACKLEVEL ((void *)&stacklevel) |
82 | # define STACKLEVEL ((void *)&stacklevel) |
87 | #endif |
83 | #endif |
88 | |
84 | |
89 | #define IN_DESTRUCT PL_dirty |
85 | #define IN_DESTRUCT PL_dirty |
90 | |
86 | |
91 | #if __GNUC__ >= 3 |
|
|
92 | # define attribute(x) __attribute__(x) |
|
|
93 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
|
|
94 | # define INLINE static inline |
|
|
95 | #else |
|
|
96 | # define attribute(x) |
|
|
97 | # define expect(expr,value) (expr) |
|
|
98 | # define INLINE static |
|
|
99 | #endif |
|
|
100 | |
|
|
101 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
102 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
103 | |
|
|
104 | #define NOINLINE attribute ((noinline)) |
|
|
105 | |
|
|
106 | #include "CoroAPI.h" |
87 | #include "CoroAPI.h" |
107 | #define GCoroAPI (&coroapi) /* very sneaky */ |
88 | #define GCoroAPI (&coroapi) /* very sneaky */ |
108 | |
89 | |
109 | #ifdef USE_ITHREADS |
90 | #ifdef USE_ITHREADS |
110 | # if CORO_PTHREAD |
91 | # if CORO_PTHREAD |
… | |
… | |
126 | static void (*u2time)(pTHX_ UV ret[2]); |
107 | static void (*u2time)(pTHX_ UV ret[2]); |
127 | |
108 | |
128 | /* we hijack an hopefully unused CV flag for our purposes */ |
109 | /* we hijack an hopefully unused CV flag for our purposes */ |
129 | #define CVf_SLF 0x4000 |
110 | #define CVf_SLF 0x4000 |
130 | static OP *pp_slf (pTHX); |
111 | static OP *pp_slf (pTHX); |
|
|
112 | static void slf_destroy (pTHX_ struct coro *coro); |
131 | |
113 | |
132 | static U32 cctx_gen; |
114 | static U32 cctx_gen; |
133 | static size_t cctx_stacksize = CORO_STACKSIZE; |
115 | static size_t cctx_stacksize = CORO_STACKSIZE; |
134 | static struct CoroAPI coroapi; |
116 | static struct CoroAPI coroapi; |
135 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
117 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
… | |
… | |
165 | static char times_valid; |
147 | static char times_valid; |
166 | |
148 | |
167 | static struct coro_cctx *cctx_first; |
149 | static struct coro_cctx *cctx_first; |
168 | static int cctx_count, cctx_idle; |
150 | static int cctx_count, cctx_idle; |
169 | |
151 | |
170 | enum { |
152 | enum |
|
|
153 | { |
171 | CC_MAPPED = 0x01, |
154 | CC_MAPPED = 0x01, |
172 | CC_NOREUSE = 0x02, /* throw this away after tracing */ |
155 | CC_NOREUSE = 0x02, /* throw this away after tracing */ |
173 | CC_TRACE = 0x04, |
156 | CC_TRACE = 0x04, |
174 | CC_TRACE_SUB = 0x08, /* trace sub calls */ |
157 | CC_TRACE_SUB = 0x08, /* trace sub calls */ |
175 | CC_TRACE_LINE = 0x10, /* trace each statement */ |
158 | CC_TRACE_LINE = 0x10, /* trace each statement */ |
… | |
… | |
180 | typedef struct coro_cctx |
163 | typedef struct coro_cctx |
181 | { |
164 | { |
182 | struct coro_cctx *next; |
165 | struct coro_cctx *next; |
183 | |
166 | |
184 | /* the stack */ |
167 | /* the stack */ |
185 | void *sptr; |
168 | struct coro_stack stack; |
186 | size_t ssize; |
|
|
187 | |
169 | |
188 | /* cpu state */ |
170 | /* cpu state */ |
189 | 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 |
190 | 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 |
191 | JMPENV *top_env; |
175 | JMPENV *top_env; |
192 | coro_context cctx; |
176 | coro_context cctx; |
193 | |
177 | |
194 | U32 gen; |
178 | U32 gen; |
195 | #if CORO_USE_VALGRIND |
179 | #if CORO_USE_VALGRIND |
… | |
… | |
200 | |
184 | |
201 | static coro_cctx *cctx_current; /* the currently running cctx */ |
185 | static coro_cctx *cctx_current; /* the currently running cctx */ |
202 | |
186 | |
203 | /*****************************************************************************/ |
187 | /*****************************************************************************/ |
204 | |
188 | |
|
|
189 | static MGVTBL coro_state_vtbl; |
|
|
190 | |
205 | enum { |
191 | enum |
|
|
192 | { |
206 | CF_RUNNING = 0x0001, /* coroutine is running */ |
193 | CF_RUNNING = 0x0001, /* coroutine is running */ |
207 | CF_READY = 0x0002, /* coroutine is ready */ |
194 | CF_READY = 0x0002, /* coroutine is ready */ |
208 | CF_NEW = 0x0004, /* has never been switched to */ |
195 | CF_NEW = 0x0004, /* has never been switched to */ |
209 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
196 | CF_ZOMBIE = 0x0008, /* coroutine data has been freed */ |
210 | CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */ |
197 | CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */ |
211 | CF_NOCANCEL = 0x0020, /* cannot cancel, set slf_frame.data to 1 (hackish) */ |
198 | CF_NOCANCEL = 0x0020, /* cannot cancel, set slf_frame.data to 1 (hackish) */ |
212 | }; |
199 | }; |
213 | |
200 | |
214 | /* 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 */ |
215 | typedef struct |
202 | typedef struct |
216 | { |
203 | { |
217 | SV *defsv; |
|
|
218 | AV *defav; |
|
|
219 | SV *errsv; |
|
|
220 | SV *irsgv; |
|
|
221 | HV *hinthv; |
|
|
222 | #define VAR(name,type) type name; |
204 | #define VARx(name,expr,type) type name; |
223 | # include "state.h" |
205 | #include "state.h" |
224 | #undef VAR |
|
|
225 | } perl_slots; |
206 | } perl_slots; |
226 | |
207 | |
|
|
208 | /* how many context stack entries do we need for perl_slots */ |
227 | #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)) |
228 | |
210 | |
229 | /* this is a structure representing a perl-level coroutine */ |
211 | /* this is a structure representing a perl-level coroutine */ |
230 | struct coro { |
212 | struct coro |
|
|
213 | { |
231 | /* the C coroutine allocated to this perl coroutine, if any */ |
214 | /* the C coroutine allocated to this perl coroutine, if any */ |
232 | coro_cctx *cctx; |
215 | coro_cctx *cctx; |
233 | |
216 | |
234 | /* ready queue */ |
217 | /* ready queue */ |
235 | struct coro *next_ready; |
218 | struct coro *next_ready; |
… | |
… | |
237 | /* state data */ |
220 | /* state data */ |
238 | struct CoroSLF slf_frame; /* saved slf frame */ |
221 | struct CoroSLF slf_frame; /* saved slf frame */ |
239 | AV *mainstack; |
222 | AV *mainstack; |
240 | perl_slots *slot; /* basically the saved sp */ |
223 | perl_slots *slot; /* basically the saved sp */ |
241 | |
224 | |
242 | CV *startcv; /* the CV to execute */ |
225 | CV *startcv; /* the CV to execute */ |
243 | AV *args; /* data associated with this coroutine (initial args) */ |
226 | AV *args; /* data associated with this coroutine (initial args) */ |
244 | int refcnt; /* coroutines are refcounted, yes */ |
|
|
245 | int flags; /* CF_ flags */ |
227 | int flags; /* CF_ flags */ |
246 | HV *hv; /* the perl hash associated with this coro, if any */ |
228 | HV *hv; /* the perl hash associated with this coro, if any */ |
247 | void (*on_destroy)(pTHX_ struct coro *coro); /* for temporary use by xs in critical sections */ |
|
|
248 | |
229 | |
249 | /* statistics */ |
230 | /* statistics */ |
250 | int usecount; /* number of transfers to this coro */ |
231 | int usecount; /* number of transfers to this coro */ |
251 | |
232 | |
252 | /* coro process data */ |
233 | /* coro process data */ |
253 | int prio; |
234 | int prio; |
254 | SV *except; /* exception to be thrown */ |
235 | SV *except; /* exception to be thrown */ |
255 | 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 */ |
256 | |
239 | |
257 | /* async_pool */ |
240 | /* async_pool */ |
258 | SV *saved_deffh; |
241 | SV *saved_deffh; |
259 | SV *invoke_cb; |
242 | SV *invoke_cb; |
260 | AV *invoke_av; |
243 | AV *invoke_av; |
… | |
… | |
292 | |
275 | |
293 | /* for Coro.pm */ |
276 | /* for Coro.pm */ |
294 | static SV *coro_current; |
277 | static SV *coro_current; |
295 | static SV *coro_readyhook; |
278 | static SV *coro_readyhook; |
296 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
279 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
297 | static CV *cv_coro_run, *cv_coro_terminate; |
280 | static CV *cv_coro_run; |
298 | static struct coro *coro_first; |
281 | static struct coro *coro_first; |
299 | #define coro_nready coroapi.nready |
282 | #define coro_nready coroapi.nready |
|
|
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 |
300 | |
306 | |
301 | /** Coro::Select ************************************************************/ |
307 | /** Coro::Select ************************************************************/ |
302 | |
308 | |
303 | static OP *(*coro_old_pp_sselect) (pTHX); |
309 | static OP *(*coro_old_pp_sselect) (pTHX); |
304 | static SV *coro_select_select; |
310 | static SV *coro_select_select; |
… | |
… | |
320 | |
326 | |
321 | /** time stuff **************************************************************/ |
327 | /** time stuff **************************************************************/ |
322 | |
328 | |
323 | #ifdef HAS_GETTIMEOFDAY |
329 | #ifdef HAS_GETTIMEOFDAY |
324 | |
330 | |
325 | static void |
331 | ecb_inline void |
326 | coro_u2time (pTHX_ UV ret[2]) |
332 | coro_u2time (pTHX_ UV ret[2]) |
327 | { |
333 | { |
328 | struct timeval tv; |
334 | struct timeval tv; |
329 | gettimeofday (&tv, 0); |
335 | gettimeofday (&tv, 0); |
330 | |
336 | |
331 | ret [0] = tv.tv_sec; |
337 | ret [0] = tv.tv_sec; |
332 | ret [1] = tv.tv_usec; |
338 | ret [1] = tv.tv_usec; |
333 | } |
339 | } |
334 | |
340 | |
335 | static double |
341 | ecb_inline double |
336 | coro_nvtime () |
342 | coro_nvtime (void) |
337 | { |
343 | { |
338 | struct timeval tv; |
344 | struct timeval tv; |
339 | gettimeofday (&tv, 0); |
345 | gettimeofday (&tv, 0); |
340 | |
346 | |
341 | return tv.tv_sec + tv.tv_usec * 1e-6; |
347 | return tv.tv_sec + tv.tv_usec * 1e-6; |
342 | } |
348 | } |
343 | |
349 | |
344 | static void |
350 | ecb_inline void |
345 | time_init (pTHX) |
351 | time_init (pTHX) |
346 | { |
352 | { |
347 | nvtime = coro_nvtime; |
353 | nvtime = coro_nvtime; |
348 | u2time = coro_u2time; |
354 | u2time = coro_u2time; |
349 | } |
355 | } |
350 | |
356 | |
351 | #else |
357 | #else |
352 | |
358 | |
353 | static void |
359 | ecb_inline void |
354 | time_init (pTHX) |
360 | time_init (pTHX) |
355 | { |
361 | { |
356 | SV **svp; |
362 | SV **svp; |
357 | |
363 | |
358 | require_pv ("Time/HiRes.pm"); |
364 | require_pv ("Time/HiRes.pm"); |
359 | |
365 | |
360 | svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
366 | svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
361 | |
367 | |
362 | if (!svp) croak ("Time::HiRes is required, but missing. Caught"); |
368 | if (!svp) croak ("Time::HiRes is required, but missing. Caught"); |
363 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught"); |
369 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught"); |
364 | |
370 | |
… | |
… | |
370 | |
376 | |
371 | #endif |
377 | #endif |
372 | |
378 | |
373 | /** lowlevel stuff **********************************************************/ |
379 | /** lowlevel stuff **********************************************************/ |
374 | |
380 | |
375 | static SV * |
381 | static SV * ecb_noinline |
376 | coro_get_sv (pTHX_ const char *name, int create) |
382 | coro_get_sv (pTHX_ const char *name, int create) |
377 | { |
383 | { |
378 | #if PERL_VERSION_ATLEAST (5,10,0) |
384 | #if PERL_VERSION_ATLEAST (5,10,0) |
379 | /* 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 */ |
380 | get_sv (name, create); |
386 | get_sv (name, create); |
381 | #endif |
387 | #endif |
382 | return get_sv (name, create); |
388 | return get_sv (name, create); |
383 | } |
389 | } |
384 | |
390 | |
385 | static AV * |
391 | static AV * ecb_noinline |
386 | coro_get_av (pTHX_ const char *name, int create) |
392 | coro_get_av (pTHX_ const char *name, int create) |
387 | { |
393 | { |
388 | #if PERL_VERSION_ATLEAST (5,10,0) |
394 | #if PERL_VERSION_ATLEAST (5,10,0) |
389 | /* 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 */ |
390 | get_av (name, create); |
396 | get_av (name, create); |
391 | #endif |
397 | #endif |
392 | return get_av (name, create); |
398 | return get_av (name, create); |
393 | } |
399 | } |
394 | |
400 | |
395 | static HV * |
401 | static HV * ecb_noinline |
396 | coro_get_hv (pTHX_ const char *name, int create) |
402 | coro_get_hv (pTHX_ const char *name, int create) |
397 | { |
403 | { |
398 | #if PERL_VERSION_ATLEAST (5,10,0) |
404 | #if PERL_VERSION_ATLEAST (5,10,0) |
399 | /* 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 */ |
400 | get_hv (name, create); |
406 | get_hv (name, create); |
401 | #endif |
407 | #endif |
402 | return get_hv (name, create); |
408 | return get_hv (name, create); |
403 | } |
409 | } |
404 | |
410 | |
405 | INLINE void |
411 | ecb_inline void |
406 | coro_times_update () |
412 | coro_times_update (void) |
407 | { |
413 | { |
408 | #ifdef coro_clock_gettime |
414 | #ifdef coro_clock_gettime |
409 | struct timespec ts; |
415 | struct timespec ts; |
410 | |
416 | |
411 | ts.tv_sec = ts.tv_nsec = 0; |
417 | ts.tv_sec = ts.tv_nsec = 0; |
… | |
… | |
423 | time_real [0] = tv [0]; |
429 | time_real [0] = tv [0]; |
424 | time_real [1] = tv [1] * 1000; |
430 | time_real [1] = tv [1] * 1000; |
425 | #endif |
431 | #endif |
426 | } |
432 | } |
427 | |
433 | |
428 | INLINE void |
434 | ecb_inline void |
429 | coro_times_add (struct coro *c) |
435 | coro_times_add (struct coro *c) |
430 | { |
436 | { |
431 | c->t_real [1] += time_real [1]; |
437 | c->t_real [1] += time_real [1]; |
432 | 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]; } |
433 | c->t_real [0] += time_real [0]; |
439 | c->t_real [0] += time_real [0]; |
… | |
… | |
435 | c->t_cpu [1] += time_cpu [1]; |
441 | c->t_cpu [1] += time_cpu [1]; |
436 | 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]; } |
437 | c->t_cpu [0] += time_cpu [0]; |
443 | c->t_cpu [0] += time_cpu [0]; |
438 | } |
444 | } |
439 | |
445 | |
440 | INLINE void |
446 | ecb_inline void |
441 | coro_times_sub (struct coro *c) |
447 | coro_times_sub (struct coro *c) |
442 | { |
448 | { |
443 | 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]; } |
444 | c->t_real [1] -= time_real [1]; |
450 | c->t_real [1] -= time_real [1]; |
445 | c->t_real [0] -= time_real [0]; |
451 | c->t_real [0] -= time_real [0]; |
… | |
… | |
453 | /* magic glue */ |
459 | /* magic glue */ |
454 | |
460 | |
455 | #define CORO_MAGIC_type_cv 26 |
461 | #define CORO_MAGIC_type_cv 26 |
456 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
462 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
457 | |
463 | |
458 | #define CORO_MAGIC_NN(sv, type) \ |
464 | #define CORO_MAGIC_NN(sv, type) \ |
459 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
465 | (ecb_expect_true (SvMAGIC (sv)->mg_type == type) \ |
460 | ? SvMAGIC (sv) \ |
466 | ? SvMAGIC (sv) \ |
461 | : mg_find (sv, type)) |
467 | : mg_find (sv, type)) |
462 | |
468 | |
463 | #define CORO_MAGIC(sv, type) \ |
469 | #define CORO_MAGIC(sv, type) \ |
464 | (expect_true (SvMAGIC (sv)) \ |
470 | (ecb_expect_true (SvMAGIC (sv)) \ |
465 | ? CORO_MAGIC_NN (sv, type) \ |
471 | ? CORO_MAGIC_NN (sv, type) \ |
466 | : 0) |
472 | : 0) |
467 | |
473 | |
468 | #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) |
469 | #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) |
470 | |
476 | |
471 | INLINE struct coro * |
477 | ecb_inline MAGIC * |
|
|
478 | SvSTATEhv_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 | |
|
|
492 | ecb_inline struct coro * |
472 | SvSTATE_ (pTHX_ SV *coro) |
493 | SvSTATE_ (pTHX_ SV *coro) |
473 | { |
494 | { |
474 | HV *stash; |
|
|
475 | MAGIC *mg; |
495 | MAGIC *mg; |
476 | |
496 | |
477 | if (SvROK (coro)) |
497 | if (SvROK (coro)) |
478 | coro = SvRV (coro); |
498 | coro = SvRV (coro); |
479 | |
499 | |
480 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
500 | mg = SvSTATEhv_p (aTHX_ coro); |
|
|
501 | if (!mg) |
481 | croak ("Coro::State object required"); |
502 | croak ("Coro::State object required"); |
482 | |
503 | |
483 | stash = SvSTASH (coro); |
|
|
484 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
485 | { |
|
|
486 | /* very slow, but rare, check */ |
|
|
487 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
488 | croak ("Coro::State object required"); |
|
|
489 | } |
|
|
490 | |
|
|
491 | mg = CORO_MAGIC_state (coro); |
|
|
492 | return (struct coro *)mg->mg_ptr; |
504 | return (struct coro *)mg->mg_ptr; |
493 | } |
505 | } |
494 | |
506 | |
495 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
507 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
496 | |
508 | |
… | |
… | |
499 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
511 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
500 | |
512 | |
501 | /*****************************************************************************/ |
513 | /*****************************************************************************/ |
502 | /* padlist management and caching */ |
514 | /* padlist management and caching */ |
503 | |
515 | |
504 | static AV * |
516 | ecb_inline PADLIST * |
505 | coro_derive_padlist (pTHX_ CV *cv) |
517 | coro_derive_padlist (pTHX_ CV *cv) |
506 | { |
518 | { |
507 | AV *padlist = CvPADLIST (cv); |
519 | PADLIST *padlist = CvPADLIST (cv); |
508 | AV *newpadlist, *newpad; |
520 | PADLIST *newpadlist; |
|
|
521 | PAD *newpad; |
|
|
522 | PADOFFSET off = PadlistMAX (padlist) + 1; |
509 | |
523 | |
510 | 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. */ |
511 | AvREAL_off (newpadlist); |
552 | AvREAL_off (newpadlist); |
512 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
513 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); |
|
|
514 | #else |
|
|
515 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
|
|
516 | #endif |
553 | #endif |
517 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
|
|
518 | --AvFILLp (padlist); |
|
|
519 | |
554 | |
520 | av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); |
555 | /* Already extended to 2 elements by newPADLIST. */ |
521 | av_store (newpadlist, 1, (SV *)newpad); |
556 | PadlistMAX (newpadlist) = 1; |
|
|
557 | PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist)); |
|
|
558 | PadlistARRAY (newpadlist)[1] = newpad; |
522 | |
559 | |
523 | return newpadlist; |
560 | return newpadlist; |
524 | } |
561 | } |
525 | |
562 | |
526 | static void |
563 | ecb_inline void |
527 | free_padlist (pTHX_ AV *padlist) |
564 | free_padlist (pTHX_ PADLIST *padlist) |
528 | { |
565 | { |
529 | /* may be during global destruction */ |
566 | /* may be during global destruction */ |
530 | if (!IN_DESTRUCT) |
567 | if (!IN_DESTRUCT) |
531 | { |
568 | { |
532 | I32 i = AvFILLp (padlist); |
569 | I32 i = PadlistMAX (padlist); |
533 | |
570 | |
534 | while (i > 0) /* special-case index 0 */ |
571 | while (i > 0) /* special-case index 0 */ |
535 | { |
572 | { |
536 | /* we try to be extra-careful here */ |
573 | /* we try to be extra-careful here */ |
537 | AV *av = (AV *)AvARRAY (padlist)[i--]; |
574 | PAD *pad = PadlistARRAY (padlist)[i--]; |
538 | I32 j = AvFILLp (av); |
|
|
539 | |
575 | |
|
|
576 | if (pad) |
|
|
577 | { |
|
|
578 | I32 j = PadMAX (pad); |
|
|
579 | |
540 | while (j >= 0) |
580 | while (j >= 0) |
541 | SvREFCNT_dec (AvARRAY (av)[j--]); |
581 | SvREFCNT_dec (PadARRAY (pad)[j--]); |
542 | |
582 | |
543 | AvFILLp (av) = -1; |
583 | PadMAX (pad) = -1; |
544 | SvREFCNT_dec (av); |
584 | SvREFCNT_dec (pad); |
|
|
585 | } |
545 | } |
586 | } |
546 | |
587 | |
547 | SvREFCNT_dec (AvARRAY (padlist)[0]); |
588 | SvREFCNT_dec (PadlistNAMES (padlist)); |
548 | |
589 | |
|
|
590 | #if NEWPADAPI |
|
|
591 | Safefree (PadlistARRAY (padlist)); |
|
|
592 | Safefree (padlist); |
|
|
593 | #else |
549 | AvFILLp (padlist) = -1; |
594 | AvFILLp (padlist) = -1; |
|
|
595 | AvREAL_off (padlist); |
550 | SvREFCNT_dec ((SV*)padlist); |
596 | SvREFCNT_dec ((SV*)padlist); |
|
|
597 | #endif |
551 | } |
598 | } |
552 | } |
599 | } |
553 | |
600 | |
554 | static int |
601 | static int |
555 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
602 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
556 | { |
603 | { |
557 | AV *padlist; |
604 | PADLIST *padlist; |
558 | 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; |
559 | |
607 | |
560 | /* perl manages to free our internal AV and _then_ call us */ |
608 | /* perl manages to free our internal AV and _then_ call us */ |
561 | if (IN_DESTRUCT) |
609 | if (IN_DESTRUCT) |
562 | return 0; |
610 | return 0; |
563 | |
611 | |
564 | /* casting is fun. */ |
612 | while (len--) |
565 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
|
|
566 | free_padlist (aTHX_ padlist); |
613 | free_padlist (aTHX_ padlists[len]); |
567 | |
|
|
568 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
|
|
569 | |
614 | |
570 | return 0; |
615 | return 0; |
571 | } |
616 | } |
572 | |
617 | |
573 | static MGVTBL coro_cv_vtbl = { |
618 | static MGVTBL coro_cv_vtbl = { |
574 | 0, 0, 0, 0, |
619 | 0, 0, 0, 0, |
575 | coro_cv_free |
620 | coro_cv_free |
576 | }; |
621 | }; |
577 | |
622 | |
578 | /* the next two functions merely cache the padlists */ |
623 | /* the next two functions merely cache the padlists */ |
579 | static void |
624 | ecb_inline void |
580 | get_padlist (pTHX_ CV *cv) |
625 | get_padlist (pTHX_ CV *cv) |
581 | { |
626 | { |
582 | MAGIC *mg = CORO_MAGIC_cv (cv); |
627 | MAGIC *mg = CORO_MAGIC_cv (cv); |
583 | AV *av; |
628 | size_t *lenp; |
584 | |
629 | |
585 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
630 | if (ecb_expect_true (mg && *(lenp = (size_t *)mg->mg_ptr))) |
586 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
631 | CvPADLIST (cv) = ((PADLIST **)(mg->mg_ptr + sizeof(size_t)))[--*lenp]; |
587 | else |
632 | else |
588 | { |
633 | { |
589 | #if CORO_PREFER_PERL_FUNCTIONS |
634 | #if CORO_PREFER_PERL_FUNCTIONS |
590 | /* this is probably cleaner? but also slower! */ |
635 | /* this is probably cleaner? but also slower! */ |
591 | /* in practise, it seems to be less stable */ |
636 | /* in practise, it seems to be less stable */ |
… | |
… | |
597 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
642 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
598 | #endif |
643 | #endif |
599 | } |
644 | } |
600 | } |
645 | } |
601 | |
646 | |
602 | static void |
647 | ecb_inline void |
603 | put_padlist (pTHX_ CV *cv) |
648 | put_padlist (pTHX_ CV *cv) |
604 | { |
649 | { |
605 | MAGIC *mg = CORO_MAGIC_cv (cv); |
650 | MAGIC *mg = CORO_MAGIC_cv (cv); |
606 | AV *av; |
|
|
607 | |
651 | |
608 | if (expect_false (!mg)) |
652 | if (ecb_expect_false (!mg)) |
|
|
653 | { |
609 | 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); |
610 | |
662 | |
611 | av = (AV *)mg->mg_obj; |
663 | ((PADLIST **)(mg->mg_ptr + sizeof (size_t))) [(*(size_t *)mg->mg_ptr)++] = CvPADLIST (cv); |
612 | |
|
|
613 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
|
|
614 | av_extend (av, AvFILLp (av) + 1); |
|
|
615 | |
|
|
616 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
|
|
617 | } |
664 | } |
618 | |
665 | |
619 | /** load & save, init *******************************************************/ |
666 | /** load & save, init *******************************************************/ |
620 | |
667 | |
|
|
668 | ecb_inline void |
|
|
669 | swap_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 | |
621 | /* swap sv heads, at least logically */ |
706 | /* swap sv heads, at least logically */ |
622 | static void |
707 | static void |
623 | swap_svs (pTHX_ Coro__State c) |
708 | swap_svs (pTHX_ Coro__State c) |
624 | { |
709 | { |
625 | int i; |
710 | int i; |
626 | |
711 | |
627 | for (i = 0; i <= AvFILLp (c->swap_sv); ) |
712 | for (i = 0; i <= AvFILLp (c->swap_sv); i += 2) |
628 | { |
713 | swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]); |
629 | SV *a = AvARRAY (c->swap_sv)[i++]; |
|
|
630 | SV *b = AvARRAY (c->swap_sv)[i++]; |
|
|
631 | |
|
|
632 | const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */ |
|
|
633 | SV tmp; |
|
|
634 | |
|
|
635 | /* swap sv_any */ |
|
|
636 | SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp); |
|
|
637 | |
|
|
638 | /* swap sv_flags */ |
|
|
639 | SvFLAGS (&tmp) = SvFLAGS (a); |
|
|
640 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
|
|
641 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
|
|
642 | |
|
|
643 | #if PERL_VERSION_ATLEAST (5,10,0) |
|
|
644 | /* perl 5.10 complicates this _quite_ a bit, but it also is |
|
|
645 | * much faster, so no quarrels here. alternatively, we could |
|
|
646 | * sv_upgrade to avoid this. |
|
|
647 | */ |
|
|
648 | { |
|
|
649 | /* swap sv_u */ |
|
|
650 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
|
|
651 | |
|
|
652 | /* if SvANY points to the head, we need to adjust the pointers, |
|
|
653 | * as the pointer for a still points to b, and maybe vice versa. |
|
|
654 | */ |
|
|
655 | #define svany_in_head(type) \ |
|
|
656 | (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type))) |
|
|
657 | |
|
|
658 | if (svany_in_head (SvTYPE (a))) |
|
|
659 | SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a); |
|
|
660 | |
|
|
661 | if (svany_in_head (SvTYPE (b))) |
|
|
662 | SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b); |
|
|
663 | } |
|
|
664 | #endif |
|
|
665 | } |
|
|
666 | } |
714 | } |
667 | |
715 | |
668 | #define SWAP_SVS(coro) \ |
716 | #define SWAP_SVS(coro) \ |
669 | if (expect_false ((coro)->swap_sv)) \ |
717 | if (ecb_expect_false ((coro)->swap_sv)) \ |
670 | swap_svs (aTHX_ (coro)) |
718 | swap_svs (aTHX_ (coro)) |
671 | |
719 | |
672 | static void |
720 | static void |
673 | on_enterleave_call (pTHX_ SV *cb); |
721 | on_enterleave_call (pTHX_ SV *cb); |
674 | |
722 | |
… | |
… | |
678 | perl_slots *slot = c->slot; |
726 | perl_slots *slot = c->slot; |
679 | c->slot = 0; |
727 | c->slot = 0; |
680 | |
728 | |
681 | PL_mainstack = c->mainstack; |
729 | PL_mainstack = c->mainstack; |
682 | |
730 | |
683 | GvSV (PL_defgv) = slot->defsv; |
731 | #if CORO_JIT |
684 | GvAV (PL_defgv) = slot->defav; |
732 | load_perl_slots (slot); |
685 | GvSV (PL_errgv) = slot->errsv; |
733 | #else |
686 | GvSV (irsgv) = slot->irsgv; |
|
|
687 | GvHV (PL_hintgv) = slot->hinthv; |
|
|
688 | |
|
|
689 | #define VAR(name,type) PL_ ## name = slot->name; |
734 | #define VARx(name,expr,type) expr = slot->name; |
690 | # include "state.h" |
735 | #include "state.h" |
691 | #undef VAR |
736 | #endif |
692 | |
737 | |
693 | { |
738 | { |
694 | dSP; |
739 | dSP; |
695 | |
740 | |
696 | CV *cv; |
741 | CV *cv; |
697 | |
742 | |
698 | /* now do the ugly restore mess */ |
743 | /* now do the ugly restore mess */ |
699 | while (expect_true (cv = (CV *)POPs)) |
744 | while (ecb_expect_true (cv = (CV *)POPs)) |
700 | { |
745 | { |
701 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
746 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
702 | CvDEPTH (cv) = PTR2IV (POPs); |
747 | CvDEPTH (cv) = PTR2IV (POPs); |
703 | CvPADLIST (cv) = (AV *)POPs; |
748 | CvPADLIST (cv) = (PADLIST *)POPs; |
704 | } |
749 | } |
705 | |
750 | |
706 | PUTBACK; |
751 | PUTBACK; |
707 | } |
752 | } |
708 | |
753 | |
709 | slf_frame = c->slf_frame; |
754 | slf_frame = c->slf_frame; |
710 | CORO_THROW = c->except; |
755 | CORO_THROW = c->except; |
711 | |
756 | |
712 | if (expect_false (enable_times)) |
757 | if (ecb_expect_false (enable_times)) |
713 | { |
758 | { |
714 | if (expect_false (!times_valid)) |
759 | if (ecb_expect_false (!times_valid)) |
715 | coro_times_update (); |
760 | coro_times_update (); |
716 | |
761 | |
717 | coro_times_sub (c); |
762 | coro_times_sub (c); |
718 | } |
763 | } |
719 | |
764 | |
720 | if (expect_false (c->on_enter)) |
765 | if (ecb_expect_false (c->on_enter)) |
721 | { |
766 | { |
722 | int i; |
767 | int i; |
723 | |
768 | |
724 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
769 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
725 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
770 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
… | |
… | |
731 | static void |
776 | static void |
732 | save_perl (pTHX_ Coro__State c) |
777 | save_perl (pTHX_ Coro__State c) |
733 | { |
778 | { |
734 | SWAP_SVS (c); |
779 | SWAP_SVS (c); |
735 | |
780 | |
736 | if (expect_false (c->on_leave)) |
781 | if (ecb_expect_false (c->on_leave)) |
737 | { |
782 | { |
738 | int i; |
783 | int i; |
739 | |
784 | |
740 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
785 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
741 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
786 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
742 | } |
787 | } |
743 | |
788 | |
744 | times_valid = 0; |
789 | times_valid = 0; |
745 | |
790 | |
746 | if (expect_false (enable_times)) |
791 | if (ecb_expect_false (enable_times)) |
747 | { |
792 | { |
748 | coro_times_update (); times_valid = 1; |
793 | coro_times_update (); times_valid = 1; |
749 | coro_times_add (c); |
794 | coro_times_add (c); |
750 | } |
795 | } |
751 | |
796 | |
… | |
… | |
765 | |
810 | |
766 | XPUSHs (Nullsv); |
811 | XPUSHs (Nullsv); |
767 | /* this loop was inspired by pp_caller */ |
812 | /* this loop was inspired by pp_caller */ |
768 | for (;;) |
813 | for (;;) |
769 | { |
814 | { |
770 | while (expect_true (cxix >= 0)) |
815 | while (ecb_expect_true (cxix >= 0)) |
771 | { |
816 | { |
772 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
817 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
773 | |
818 | |
774 | 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)) |
775 | { |
820 | { |
776 | CV *cv = cx->blk_sub.cv; |
821 | CV *cv = cx->blk_sub.cv; |
777 | |
822 | |
778 | if (expect_true (CvDEPTH (cv))) |
823 | if (ecb_expect_true (CvDEPTH (cv))) |
779 | { |
824 | { |
780 | EXTEND (SP, 3); |
825 | EXTEND (SP, 3); |
781 | PUSHs ((SV *)CvPADLIST (cv)); |
826 | PUSHs ((SV *)CvPADLIST (cv)); |
782 | PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); |
827 | PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); |
783 | PUSHs ((SV *)cv); |
828 | PUSHs ((SV *)cv); |
… | |
… | |
786 | get_padlist (aTHX_ cv); |
831 | get_padlist (aTHX_ cv); |
787 | } |
832 | } |
788 | } |
833 | } |
789 | } |
834 | } |
790 | |
835 | |
791 | if (expect_true (top_si->si_type == PERLSI_MAIN)) |
836 | if (ecb_expect_true (top_si->si_type == PERLSI_MAIN)) |
792 | break; |
837 | break; |
793 | |
838 | |
794 | top_si = top_si->si_prev; |
839 | top_si = top_si->si_prev; |
795 | ccstk = top_si->si_cxstack; |
840 | ccstk = top_si->si_cxstack; |
796 | cxix = top_si->si_cxix; |
841 | cxix = top_si->si_cxix; |
… | |
… | |
798 | |
843 | |
799 | PUTBACK; |
844 | PUTBACK; |
800 | } |
845 | } |
801 | |
846 | |
802 | /* allocate some space on the context stack for our purposes */ |
847 | /* allocate some space on the context stack for our purposes */ |
803 | /* we manually unroll here, as usually 2 slots is enough */ |
848 | if (ecb_expect_false (cxstack_ix + (int)SLOT_COUNT >= cxstack_max)) |
804 | if (SLOT_COUNT >= 1) CXINC; |
|
|
805 | if (SLOT_COUNT >= 2) CXINC; |
|
|
806 | if (SLOT_COUNT >= 3) CXINC; |
|
|
807 | { |
849 | { |
808 | unsigned int i; |
850 | unsigned int i; |
|
|
851 | |
809 | for (i = 3; i < SLOT_COUNT; ++i) |
852 | for (i = 0; i < SLOT_COUNT; ++i) |
810 | CXINC; |
853 | CXINC; |
811 | } |
854 | |
812 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
855 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
|
|
856 | } |
813 | |
857 | |
814 | c->mainstack = PL_mainstack; |
858 | c->mainstack = PL_mainstack; |
815 | |
859 | |
816 | { |
860 | { |
817 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
861 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
818 | |
862 | |
819 | slot->defav = GvAV (PL_defgv); |
863 | #if CORO_JIT |
820 | slot->defsv = DEFSV; |
864 | save_perl_slots (slot); |
821 | slot->errsv = ERRSV; |
865 | #else |
822 | slot->irsgv = GvSV (irsgv); |
|
|
823 | slot->hinthv = GvHV (PL_hintgv); |
|
|
824 | |
|
|
825 | #define VAR(name,type) slot->name = PL_ ## name; |
866 | #define VARx(name,expr,type) slot->name = expr; |
826 | # include "state.h" |
867 | #include "state.h" |
827 | #undef VAR |
868 | #endif |
828 | } |
869 | } |
829 | } |
870 | } |
830 | |
871 | |
831 | /* |
872 | /* |
832 | * allocate various perl stacks. This is almost an exact copy |
873 | * allocate various perl stacks. This is almost an exact copy |
… | |
… | |
838 | # define coro_init_stacks(thx) init_stacks () |
879 | # define coro_init_stacks(thx) init_stacks () |
839 | #else |
880 | #else |
840 | static void |
881 | static void |
841 | coro_init_stacks (pTHX) |
882 | coro_init_stacks (pTHX) |
842 | { |
883 | { |
843 | 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() */ |
844 | PL_curstackinfo->si_type = PERLSI_MAIN; |
885 | PL_curstackinfo->si_type = PERLSI_MAIN; |
845 | PL_curstack = PL_curstackinfo->si_stack; |
886 | PL_curstack = PL_curstackinfo->si_stack; |
846 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
887 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
847 | |
888 | |
848 | PL_stack_base = AvARRAY(PL_curstack); |
889 | PL_stack_base = AvARRAY(PL_curstack); |
… | |
… | |
863 | #endif |
904 | #endif |
864 | |
905 | |
865 | New(54,PL_scopestack,8,I32); |
906 | New(54,PL_scopestack,8,I32); |
866 | PL_scopestack_ix = 0; |
907 | PL_scopestack_ix = 0; |
867 | 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 |
868 | |
912 | |
869 | New(54,PL_savestack,24,ANY); |
913 | New(54,PL_savestack,24,ANY); |
870 | PL_savestack_ix = 0; |
914 | PL_savestack_ix = 0; |
871 | PL_savestack_max = 24; |
915 | PL_savestack_max = 24; |
872 | |
916 | |
… | |
… | |
900 | } |
944 | } |
901 | |
945 | |
902 | Safefree (PL_tmps_stack); |
946 | Safefree (PL_tmps_stack); |
903 | Safefree (PL_markstack); |
947 | Safefree (PL_markstack); |
904 | Safefree (PL_scopestack); |
948 | Safefree (PL_scopestack); |
|
|
949 | #if HAS_SCOPESTACK_NAME |
|
|
950 | Safefree (PL_scopestack_name); |
|
|
951 | #endif |
905 | Safefree (PL_savestack); |
952 | Safefree (PL_savestack); |
906 | #if !PERL_VERSION_ATLEAST (5,10,0) |
953 | #if !PERL_VERSION_ATLEAST (5,10,0) |
907 | Safefree (PL_retstack); |
954 | Safefree (PL_retstack); |
908 | #endif |
955 | #endif |
909 | } |
956 | } |
… | |
… | |
955 | #endif |
1002 | #endif |
956 | |
1003 | |
957 | /* |
1004 | /* |
958 | * This overrides the default magic get method of %SIG elements. |
1005 | * This overrides the default magic get method of %SIG elements. |
959 | * 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 |
960 | * 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), |
961 | * readback here. |
1008 | * we just provide our own readback here. |
962 | */ |
1009 | */ |
963 | static int |
1010 | static int ecb_cold |
964 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
1011 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
965 | { |
1012 | { |
966 | const char *s = MgPV_nolen_const (mg); |
1013 | const char *s = MgPV_nolen_const (mg); |
967 | |
1014 | |
968 | if (*s == '_') |
1015 | if (*s == '_') |
969 | { |
1016 | { |
970 | SV **svp = 0; |
1017 | SV **svp = 0; |
971 | |
1018 | |
972 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
1019 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
973 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
1020 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
974 | |
1021 | |
975 | if (svp) |
1022 | if (svp) |
976 | { |
1023 | { |
977 | 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); |
978 | return 0; |
1034 | return 0; |
979 | } |
1035 | } |
980 | } |
1036 | } |
981 | |
1037 | |
982 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
1038 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
983 | } |
1039 | } |
984 | |
1040 | |
985 | static int |
1041 | static int ecb_cold |
986 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
1042 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
987 | { |
1043 | { |
988 | const char *s = MgPV_nolen_const (mg); |
1044 | const char *s = MgPV_nolen_const (mg); |
989 | |
1045 | |
990 | if (*s == '_') |
1046 | if (*s == '_') |
… | |
… | |
1004 | } |
1060 | } |
1005 | |
1061 | |
1006 | return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; |
1062 | return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; |
1007 | } |
1063 | } |
1008 | |
1064 | |
1009 | static int |
1065 | static int ecb_cold |
1010 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1066 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1011 | { |
1067 | { |
1012 | const char *s = MgPV_nolen_const (mg); |
1068 | const char *s = MgPV_nolen_const (mg); |
1013 | |
1069 | |
1014 | if (*s == '_') |
1070 | if (*s == '_') |
… | |
… | |
1049 | return 1; |
1105 | return 1; |
1050 | } |
1106 | } |
1051 | |
1107 | |
1052 | static UNOP init_perl_op; |
1108 | static UNOP init_perl_op; |
1053 | |
1109 | |
1054 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
1110 | ecb_noinline static void /* noinline to keep it out of the transfer fast path */ |
1055 | init_perl (pTHX_ struct coro *coro) |
1111 | init_perl (pTHX_ struct coro *coro) |
1056 | { |
1112 | { |
1057 | /* |
1113 | /* |
1058 | * emulate part of the perl startup here. |
1114 | * emulate part of the perl startup here. |
1059 | */ |
1115 | */ |
… | |
… | |
1074 | PL_parser = 0; |
1130 | PL_parser = 0; |
1075 | #endif |
1131 | #endif |
1076 | PL_hints = 0; |
1132 | PL_hints = 0; |
1077 | |
1133 | |
1078 | /* recreate the die/warn hooks */ |
1134 | /* recreate the die/warn hooks */ |
1079 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
1135 | PL_diehook = SvREFCNT_inc (rv_diehook); |
1080 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
1136 | PL_warnhook = SvREFCNT_inc (rv_warnhook); |
1081 | |
1137 | |
1082 | GvSV (PL_defgv) = newSV (0); |
1138 | GvSV (PL_defgv) = newSV (0); |
1083 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1139 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
1084 | GvSV (PL_errgv) = newSV (0); |
1140 | GvSV (PL_errgv) = newSV (0); |
1085 | 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); |
1086 | GvHV (PL_hintgv) = 0; |
1142 | GvHV (PL_hintgv) = 0; |
… | |
… | |
1106 | /* 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 |
1107 | * 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. |
1108 | */ |
1164 | */ |
1109 | 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 */ |
1110 | 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; |
1111 | |
1168 | |
1112 | /* 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 */ |
1113 | init_perl_op.op_next = PL_op; |
1170 | init_perl_op.op_next = PL_op; |
1114 | init_perl_op.op_type = OP_ENTERSUB; |
1171 | init_perl_op.op_type = OP_ENTERSUB; |
1115 | init_perl_op.op_ppaddr = pp_slf; |
1172 | init_perl_op.op_ppaddr = pp_slf; |
… | |
… | |
1120 | /* copy throw, in case it was set before init_perl */ |
1177 | /* copy throw, in case it was set before init_perl */ |
1121 | CORO_THROW = coro->except; |
1178 | CORO_THROW = coro->except; |
1122 | |
1179 | |
1123 | SWAP_SVS (coro); |
1180 | SWAP_SVS (coro); |
1124 | |
1181 | |
1125 | if (expect_false (enable_times)) |
1182 | if (ecb_expect_false (enable_times)) |
1126 | { |
1183 | { |
1127 | coro_times_update (); |
1184 | coro_times_update (); |
1128 | coro_times_sub (coro); |
1185 | coro_times_sub (coro); |
1129 | } |
1186 | } |
1130 | } |
1187 | } |
… | |
… | |
1154 | destroy_perl (pTHX_ struct coro *coro) |
1211 | destroy_perl (pTHX_ struct coro *coro) |
1155 | { |
1212 | { |
1156 | SV *svf [9]; |
1213 | SV *svf [9]; |
1157 | |
1214 | |
1158 | { |
1215 | { |
|
|
1216 | SV *old_current = SvRV (coro_current); |
1159 | struct coro *current = SvSTATE_current; |
1217 | struct coro *current = SvSTATE (old_current); |
1160 | |
1218 | |
1161 | 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)); |
1162 | |
1220 | |
1163 | save_perl (aTHX_ current); |
1221 | save_perl (aTHX_ current); |
|
|
1222 | |
|
|
1223 | /* this will cause transfer_check to croak on block*/ |
1164 | /*SvRV_set (coro_current, (SV *)coro->hv);*/ |
1224 | SvRV_set (coro_current, (SV *)coro->hv); |
|
|
1225 | |
1165 | load_perl (aTHX_ coro); |
1226 | load_perl (aTHX_ coro); |
1166 | |
1227 | |
1167 | coro_unwind_stacks (aTHX); |
1228 | coro_unwind_stacks (aTHX); |
1168 | coro_destruct_stacks (aTHX); |
|
|
1169 | |
1229 | |
1170 | /* restore swapped sv's */ |
1230 | /* restore swapped sv's */ |
1171 | SWAP_SVS (coro); |
1231 | SWAP_SVS (coro); |
1172 | |
1232 | |
|
|
1233 | coro_destruct_stacks (aTHX); |
|
|
1234 | |
1173 | // now save some sv's to be free'd later |
1235 | /* now save some sv's to be free'd later */ |
1174 | svf [0] = GvSV (PL_defgv); |
1236 | svf [0] = GvSV (PL_defgv); |
1175 | svf [1] = (SV *)GvAV (PL_defgv); |
1237 | svf [1] = (SV *)GvAV (PL_defgv); |
1176 | svf [2] = GvSV (PL_errgv); |
1238 | svf [2] = GvSV (PL_errgv); |
1177 | svf [3] = (SV *)PL_defoutgv; |
1239 | svf [3] = (SV *)PL_defoutgv; |
1178 | svf [4] = PL_rs; |
1240 | svf [4] = PL_rs; |
… | |
… | |
1180 | svf [6] = (SV *)GvHV (PL_hintgv); |
1242 | svf [6] = (SV *)GvHV (PL_hintgv); |
1181 | svf [7] = PL_diehook; |
1243 | svf [7] = PL_diehook; |
1182 | svf [8] = PL_warnhook; |
1244 | svf [8] = PL_warnhook; |
1183 | assert (9 == sizeof (svf) / sizeof (*svf)); |
1245 | assert (9 == sizeof (svf) / sizeof (*svf)); |
1184 | |
1246 | |
1185 | /*SvRV_set (coro_current, (SV *)current->hv);*/ |
1247 | SvRV_set (coro_current, old_current); |
|
|
1248 | |
1186 | load_perl (aTHX_ current); |
1249 | load_perl (aTHX_ current); |
1187 | } |
1250 | } |
1188 | |
1251 | |
1189 | { |
1252 | { |
1190 | unsigned int i; |
1253 | unsigned int i; |
… | |
… | |
1197 | SvREFCNT_dec (coro->invoke_cb); |
1260 | SvREFCNT_dec (coro->invoke_cb); |
1198 | SvREFCNT_dec (coro->invoke_av); |
1261 | SvREFCNT_dec (coro->invoke_av); |
1199 | } |
1262 | } |
1200 | } |
1263 | } |
1201 | |
1264 | |
1202 | INLINE void |
1265 | ecb_inline void |
1203 | free_coro_mortal (pTHX) |
1266 | free_coro_mortal (pTHX) |
1204 | { |
1267 | { |
1205 | if (expect_true (coro_mortal)) |
1268 | if (ecb_expect_true (coro_mortal)) |
1206 | { |
1269 | { |
1207 | SvREFCNT_dec (coro_mortal); |
1270 | SvREFCNT_dec ((SV *)coro_mortal); |
1208 | coro_mortal = 0; |
1271 | coro_mortal = 0; |
1209 | } |
1272 | } |
1210 | } |
1273 | } |
1211 | |
1274 | |
1212 | static int |
1275 | static int |
… | |
… | |
1345 | |
1408 | |
1346 | 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 */ |
1347 | } |
1410 | } |
1348 | |
1411 | |
1349 | /* 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 */ |
1350 | static void NOINLINE |
1413 | static void ecb_noinline |
1351 | cctx_prepare (pTHX) |
1414 | cctx_prepare (pTHX) |
1352 | { |
1415 | { |
1353 | PL_top_env = &PL_start_env; |
1416 | PL_top_env = &PL_start_env; |
1354 | |
1417 | |
1355 | if (cctx_current->flags & CC_TRACE) |
1418 | if (cctx_current->flags & CC_TRACE) |
… | |
… | |
1366 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1429 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1367 | slf_frame.check = slf_check_set_stacklevel; |
1430 | slf_frame.check = slf_check_set_stacklevel; |
1368 | } |
1431 | } |
1369 | |
1432 | |
1370 | /* 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 */ |
1371 | INLINE void |
1434 | ecb_inline void |
1372 | transfer_tail (pTHX) |
1435 | transfer_tail (pTHX) |
1373 | { |
1436 | { |
1374 | 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 */ |
|
|
1444 | static void ecb_noinline ecb_cold |
|
|
1445 | perlish_exit (pTHX) |
|
|
1446 | { |
|
|
1447 | int exitstatus = perl_destruct (PL_curinterp); |
|
|
1448 | perl_free (PL_curinterp); |
|
|
1449 | exit (exitstatus); |
1375 | } |
1450 | } |
1376 | |
1451 | |
1377 | /* |
1452 | /* |
1378 | * this is a _very_ stripped down perl interpreter ;) |
1453 | * this is a _very_ stripped down perl interpreter ;) |
1379 | */ |
1454 | */ |
… | |
… | |
1408 | */ |
1483 | */ |
1409 | |
1484 | |
1410 | /* |
1485 | /* |
1411 | * 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 |
1412 | * 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) |
1413 | * 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 |
1414 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1489 | * doing in that case. YMMV. |
1415 | * coroutine as Coro has no such concept. |
|
|
1416 | * This actually isn't valid with the pthread backend, but OSes requiring |
|
|
1417 | * that backend are too broken to do it in a standards-compliant way. |
|
|
1418 | */ |
1490 | */ |
1419 | PL_top_env = main_top_env; |
1491 | perlish_exit (aTHX); |
1420 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
|
|
1421 | } |
1492 | } |
1422 | } |
1493 | } |
1423 | |
1494 | |
1424 | static coro_cctx * |
1495 | static coro_cctx * |
1425 | cctx_new () |
1496 | cctx_new (void) |
1426 | { |
1497 | { |
1427 | coro_cctx *cctx; |
1498 | coro_cctx *cctx; |
1428 | |
1499 | |
1429 | ++cctx_count; |
1500 | ++cctx_count; |
1430 | New (0, cctx, 1, coro_cctx); |
1501 | New (0, cctx, 1, coro_cctx); |
… | |
… | |
1436 | return cctx; |
1507 | return cctx; |
1437 | } |
1508 | } |
1438 | |
1509 | |
1439 | /* create a new cctx only suitable as source */ |
1510 | /* create a new cctx only suitable as source */ |
1440 | static coro_cctx * |
1511 | static coro_cctx * |
1441 | cctx_new_empty () |
1512 | cctx_new_empty (void) |
1442 | { |
1513 | { |
1443 | coro_cctx *cctx = cctx_new (); |
1514 | coro_cctx *cctx = cctx_new (); |
1444 | |
1515 | |
1445 | cctx->sptr = 0; |
1516 | cctx->stack.sptr = 0; |
1446 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
1517 | coro_create (&cctx->cctx, 0, 0, 0, 0); |
1447 | |
1518 | |
1448 | return cctx; |
1519 | return cctx; |
1449 | } |
1520 | } |
1450 | |
1521 | |
1451 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1522 | /* create a new cctx suitable as destination/running a perl interpreter */ |
1452 | static coro_cctx * |
1523 | static coro_cctx * |
1453 | cctx_new_run () |
1524 | cctx_new_run (void) |
1454 | { |
1525 | { |
1455 | coro_cctx *cctx = cctx_new (); |
1526 | coro_cctx *cctx = cctx_new (); |
1456 | void *stack_start; |
|
|
1457 | size_t stack_size; |
|
|
1458 | |
1527 | |
1459 | #if HAVE_MMAP |
1528 | if (!coro_stack_alloc (&cctx->stack, cctx_stacksize)) |
1460 | cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
|
|
1461 | /* mmap supposedly does allocate-on-write for us */ |
|
|
1462 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
|
|
1463 | |
|
|
1464 | if (cctx->sptr != (void *)-1) |
|
|
1465 | { |
|
|
1466 | #if CORO_STACKGUARD |
|
|
1467 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
|
|
1468 | #endif |
|
|
1469 | stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE; |
|
|
1470 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
|
|
1471 | cctx->flags |= CC_MAPPED; |
|
|
1472 | } |
1529 | { |
1473 | else |
|
|
1474 | #endif |
|
|
1475 | { |
|
|
1476 | cctx->ssize = cctx_stacksize * (long)sizeof (long); |
|
|
1477 | New (0, cctx->sptr, cctx_stacksize, long); |
|
|
1478 | |
|
|
1479 | if (!cctx->sptr) |
|
|
1480 | { |
|
|
1481 | perror ("FATAL: unable to allocate stack for coroutine, exiting."); |
1530 | perror ("FATAL: unable to allocate stack for coroutine, exiting."); |
1482 | _exit (EXIT_FAILURE); |
1531 | _exit (EXIT_FAILURE); |
1483 | } |
|
|
1484 | |
|
|
1485 | stack_start = cctx->sptr; |
|
|
1486 | stack_size = cctx->ssize; |
|
|
1487 | } |
1532 | } |
1488 | |
1533 | |
1489 | #if CORO_USE_VALGRIND |
|
|
1490 | cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size); |
|
|
1491 | #endif |
|
|
1492 | |
|
|
1493 | 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); |
1494 | |
1535 | |
1495 | return cctx; |
1536 | return cctx; |
1496 | } |
1537 | } |
1497 | |
1538 | |
1498 | static void |
1539 | static void |
… | |
… | |
1504 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current)); |
1545 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current)); |
1505 | |
1546 | |
1506 | --cctx_count; |
1547 | --cctx_count; |
1507 | coro_destroy (&cctx->cctx); |
1548 | coro_destroy (&cctx->cctx); |
1508 | |
1549 | |
1509 | /* coro_transfer creates new, empty cctx's */ |
1550 | coro_stack_free (&cctx->stack); |
1510 | if (cctx->sptr) |
|
|
1511 | { |
|
|
1512 | #if CORO_USE_VALGRIND |
|
|
1513 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
|
|
1514 | #endif |
|
|
1515 | |
|
|
1516 | #if HAVE_MMAP |
|
|
1517 | if (cctx->flags & CC_MAPPED) |
|
|
1518 | munmap (cctx->sptr, cctx->ssize); |
|
|
1519 | else |
|
|
1520 | #endif |
|
|
1521 | Safefree (cctx->sptr); |
|
|
1522 | } |
|
|
1523 | |
1551 | |
1524 | Safefree (cctx); |
1552 | Safefree (cctx); |
1525 | } |
1553 | } |
1526 | |
1554 | |
1527 | /* wether this cctx should be destructed */ |
1555 | /* wether this cctx should be destructed */ |
1528 | #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)) |
1529 | |
1557 | |
1530 | static coro_cctx * |
1558 | static coro_cctx * |
1531 | cctx_get (pTHX) |
1559 | cctx_get (pTHX) |
1532 | { |
1560 | { |
1533 | while (expect_true (cctx_first)) |
1561 | while (ecb_expect_true (cctx_first)) |
1534 | { |
1562 | { |
1535 | coro_cctx *cctx = cctx_first; |
1563 | coro_cctx *cctx = cctx_first; |
1536 | cctx_first = cctx->next; |
1564 | cctx_first = cctx->next; |
1537 | --cctx_idle; |
1565 | --cctx_idle; |
1538 | |
1566 | |
1539 | if (expect_true (!CCTX_EXPIRED (cctx))) |
1567 | if (ecb_expect_true (!CCTX_EXPIRED (cctx))) |
1540 | return cctx; |
1568 | return cctx; |
1541 | |
1569 | |
1542 | cctx_destroy (cctx); |
1570 | cctx_destroy (cctx); |
1543 | } |
1571 | } |
1544 | |
1572 | |
… | |
… | |
1546 | } |
1574 | } |
1547 | |
1575 | |
1548 | static void |
1576 | static void |
1549 | cctx_put (coro_cctx *cctx) |
1577 | cctx_put (coro_cctx *cctx) |
1550 | { |
1578 | { |
1551 | 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)); |
1552 | |
1580 | |
1553 | /* free another cctx if overlimit */ |
1581 | /* free another cctx if overlimit */ |
1554 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1582 | if (ecb_expect_false (cctx_idle >= cctx_max_idle)) |
1555 | { |
1583 | { |
1556 | coro_cctx *first = cctx_first; |
1584 | coro_cctx *first = cctx_first; |
1557 | cctx_first = first->next; |
1585 | cctx_first = first->next; |
1558 | --cctx_idle; |
1586 | --cctx_idle; |
1559 | |
1587 | |
… | |
… | |
1570 | static void |
1598 | static void |
1571 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1599 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1572 | { |
1600 | { |
1573 | /* TODO: throwing up here is considered harmful */ |
1601 | /* TODO: throwing up here is considered harmful */ |
1574 | |
1602 | |
1575 | if (expect_true (prev != next)) |
1603 | if (ecb_expect_true (prev != next)) |
1576 | { |
1604 | { |
1577 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1605 | if (ecb_expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1578 | 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,"); |
1579 | |
1607 | |
1580 | if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED))) |
1608 | if (ecb_expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED))) |
1581 | 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,"); |
1582 | |
1610 | |
1583 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1611 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1584 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1612 | if (ecb_expect_false (PL_lex_state != LEX_NOTPARSING)) |
1585 | 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,"); |
1586 | #endif |
1614 | #endif |
1587 | } |
1615 | } |
1588 | } |
1616 | } |
1589 | |
1617 | |
1590 | /* always use the TRANSFER macro */ |
1618 | /* always use the TRANSFER macro */ |
1591 | static void NOINLINE /* noinline so we have a fixed stackframe */ |
1619 | static void ecb_noinline /* noinline so we have a fixed stackframe */ |
1592 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1620 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1593 | { |
1621 | { |
1594 | dSTACKLEVEL; |
1622 | dSTACKLEVEL; |
1595 | |
1623 | |
1596 | /* sometimes transfer is only called to set idle_sp */ |
1624 | /* sometimes transfer is only called to set idle_sp */ |
1597 | if (expect_false (!prev)) |
1625 | if (ecb_expect_false (!prev)) |
1598 | { |
1626 | { |
1599 | cctx_current->idle_sp = STACKLEVEL; |
1627 | cctx_current->idle_sp = STACKLEVEL; |
1600 | 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 */ |
1601 | } |
1629 | } |
1602 | else if (expect_true (prev != next)) |
1630 | else if (ecb_expect_true (prev != next)) |
1603 | { |
1631 | { |
1604 | coro_cctx *cctx_prev; |
1632 | coro_cctx *cctx_prev; |
1605 | |
1633 | |
1606 | if (expect_false (prev->flags & CF_NEW)) |
1634 | if (ecb_expect_false (prev->flags & CF_NEW)) |
1607 | { |
1635 | { |
1608 | /* create a new empty/source context */ |
1636 | /* create a new empty/source context */ |
1609 | prev->flags &= ~CF_NEW; |
1637 | prev->flags &= ~CF_NEW; |
1610 | prev->flags |= CF_RUNNING; |
1638 | prev->flags |= CF_RUNNING; |
1611 | } |
1639 | } |
… | |
… | |
1614 | next->flags |= CF_RUNNING; |
1642 | next->flags |= CF_RUNNING; |
1615 | |
1643 | |
1616 | /* first get rid of the old state */ |
1644 | /* first get rid of the old state */ |
1617 | save_perl (aTHX_ prev); |
1645 | save_perl (aTHX_ prev); |
1618 | |
1646 | |
1619 | if (expect_false (next->flags & CF_NEW)) |
1647 | if (ecb_expect_false (next->flags & CF_NEW)) |
1620 | { |
1648 | { |
1621 | /* need to start coroutine */ |
1649 | /* need to start coroutine */ |
1622 | next->flags &= ~CF_NEW; |
1650 | next->flags &= ~CF_NEW; |
1623 | /* setup coroutine call */ |
1651 | /* setup coroutine call */ |
1624 | init_perl (aTHX_ next); |
1652 | init_perl (aTHX_ next); |
1625 | } |
1653 | } |
1626 | else |
1654 | else |
1627 | load_perl (aTHX_ next); |
1655 | load_perl (aTHX_ next); |
1628 | |
1656 | |
1629 | /* possibly untie and reuse the cctx */ |
1657 | /* possibly untie and reuse the cctx */ |
1630 | if (expect_true ( |
1658 | if (ecb_expect_true ( |
1631 | cctx_current->idle_sp == STACKLEVEL |
1659 | cctx_current->idle_sp == STACKLEVEL |
1632 | && !(cctx_current->flags & CC_TRACE) |
1660 | && !(cctx_current->flags & CC_TRACE) |
1633 | && !force_cctx |
1661 | && !force_cctx |
1634 | )) |
1662 | )) |
1635 | { |
1663 | { |
1636 | /* 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 */ |
1637 | 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)); |
1638 | |
1666 | |
1639 | /* 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. */ |
1640 | /* 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 */ |
1641 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1669 | if (ecb_expect_false (CCTX_EXPIRED (cctx_current))) |
1642 | if (expect_true (!next->cctx)) |
1670 | if (ecb_expect_true (!next->cctx)) |
1643 | next->cctx = cctx_get (aTHX); |
1671 | next->cctx = cctx_get (aTHX); |
1644 | |
1672 | |
1645 | cctx_put (cctx_current); |
1673 | cctx_put (cctx_current); |
1646 | } |
1674 | } |
1647 | else |
1675 | else |
1648 | prev->cctx = cctx_current; |
1676 | prev->cctx = cctx_current; |
1649 | |
1677 | |
1650 | ++next->usecount; |
1678 | ++next->usecount; |
1651 | |
1679 | |
1652 | cctx_prev = cctx_current; |
1680 | cctx_prev = cctx_current; |
1653 | cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1681 | cctx_current = ecb_expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1654 | |
1682 | |
1655 | next->cctx = 0; |
1683 | next->cctx = 0; |
1656 | |
1684 | |
1657 | if (expect_false (cctx_prev != cctx_current)) |
1685 | if (ecb_expect_false (cctx_prev != cctx_current)) |
1658 | { |
1686 | { |
1659 | cctx_prev->top_env = PL_top_env; |
1687 | cctx_prev->top_env = PL_top_env; |
1660 | PL_top_env = cctx_current->top_env; |
1688 | PL_top_env = cctx_current->top_env; |
1661 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1689 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1662 | } |
1690 | } |
… | |
… | |
1668 | #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)) |
1669 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1697 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
1670 | |
1698 | |
1671 | /** high level stuff ********************************************************/ |
1699 | /** high level stuff ********************************************************/ |
1672 | |
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 */ |
|
|
1703 | static void |
|
|
1704 | coro_call_on_destroy (pTHX_ struct coro *coro); |
|
|
1705 | |
1673 | static void |
1706 | static void |
1674 | coro_state_destroy (pTHX_ struct coro *coro) |
1707 | coro_state_destroy (pTHX_ struct coro *coro) |
1675 | { |
1708 | { |
1676 | if (coro->flags & CF_DESTROYED) |
1709 | if (coro->flags & CF_ZOMBIE) |
1677 | return; |
1710 | return; |
1678 | |
1711 | |
1679 | /* this callback is reserved for slf functions needing to do cleanup */ |
|
|
1680 | if (coro->on_destroy && !PL_dirty) |
|
|
1681 | coro->on_destroy (aTHX_ coro); |
1712 | slf_destroy (aTHX_ coro); |
1682 | |
1713 | |
1683 | /* |
|
|
1684 | * The on_destroy above most likely is from an SLF call. |
|
|
1685 | * Since by definition the SLF call will not finish when we destroy |
|
|
1686 | * the coro, we will have to force-finish it here, otherwise |
|
|
1687 | * cleanup functions cannot call SLF functions. |
|
|
1688 | */ |
|
|
1689 | coro->slf_frame.prepare = 0; |
|
|
1690 | |
|
|
1691 | coro->flags |= CF_DESTROYED; |
1714 | coro->flags |= CF_ZOMBIE; |
1692 | |
1715 | |
1693 | if (coro->flags & CF_READY) |
1716 | if (coro->flags & CF_READY) |
1694 | { |
1717 | { |
1695 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1718 | /* reduce nready, as destroying a ready coro effectively unreadies it */ |
1696 | /* alternative: look through all ready queues and remove the coro */ |
1719 | /* alternative: look through all ready queues and remove the coro */ |
1697 | --coro_nready; |
1720 | --coro_nready; |
1698 | } |
1721 | } |
1699 | else |
1722 | else |
1700 | 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; |
1701 | |
1728 | |
1702 | if (coro->mainstack |
1729 | if (coro->mainstack |
1703 | && coro->mainstack != main_mainstack |
1730 | && coro->mainstack != main_mainstack |
1704 | && coro->slot |
1731 | && coro->slot |
1705 | && !PL_dirty) |
1732 | && !PL_dirty) |
1706 | destroy_perl (aTHX_ coro); |
1733 | destroy_perl (aTHX_ coro); |
1707 | |
1734 | |
1708 | if (coro->next) coro->next->prev = coro->prev; |
|
|
1709 | if (coro->prev) coro->prev->next = coro->next; |
|
|
1710 | if (coro == coro_first) coro_first = coro->next; |
|
|
1711 | |
|
|
1712 | cctx_destroy (coro->cctx); |
1735 | cctx_destroy (coro->cctx); |
1713 | SvREFCNT_dec (coro->startcv); |
1736 | SvREFCNT_dec (coro->startcv); |
1714 | SvREFCNT_dec (coro->args); |
1737 | SvREFCNT_dec (coro->args); |
1715 | SvREFCNT_dec (coro->swap_sv); |
1738 | SvREFCNT_dec (coro->swap_sv); |
1716 | SvREFCNT_dec (CORO_THROW); |
1739 | SvREFCNT_dec (CORO_THROW); |
|
|
1740 | |
|
|
1741 | coro_call_on_destroy (aTHX_ coro); |
|
|
1742 | |
|
|
1743 | /* more destruction mayhem in coro_state_free */ |
1717 | } |
1744 | } |
1718 | |
1745 | |
1719 | static int |
1746 | static int |
1720 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
1747 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
1721 | { |
1748 | { |
1722 | struct coro *coro = (struct coro *)mg->mg_ptr; |
1749 | struct coro *coro = (struct coro *)mg->mg_ptr; |
1723 | mg->mg_ptr = 0; |
1750 | mg->mg_ptr = 0; |
1724 | |
1751 | |
1725 | coro->hv = 0; |
|
|
1726 | |
|
|
1727 | if (--coro->refcnt < 0) |
|
|
1728 | { |
|
|
1729 | coro_state_destroy (aTHX_ coro); |
1752 | coro_state_destroy (aTHX_ coro); |
|
|
1753 | SvREFCNT_dec (coro->on_destroy); |
|
|
1754 | SvREFCNT_dec (coro->status); |
|
|
1755 | |
1730 | Safefree (coro); |
1756 | Safefree (coro); |
1731 | } |
|
|
1732 | |
1757 | |
1733 | return 0; |
1758 | return 0; |
1734 | } |
1759 | } |
1735 | |
1760 | |
1736 | static int |
1761 | static int ecb_cold |
1737 | coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) |
1762 | coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) |
1738 | { |
1763 | { |
1739 | struct coro *coro = (struct coro *)mg->mg_ptr; |
1764 | /* called when perl clones the current process the slow way (windows process emulation) */ |
1740 | |
1765 | /* WE SIMply nuke the pointers in the copy, causing perl to croak */ |
1741 | ++coro->refcnt; |
1766 | mg->mg_ptr = 0; |
|
|
1767 | mg->mg_virtual = 0; |
1742 | |
1768 | |
1743 | return 0; |
1769 | return 0; |
1744 | } |
1770 | } |
1745 | |
1771 | |
1746 | static MGVTBL coro_state_vtbl = { |
1772 | static MGVTBL coro_state_vtbl = { |
… | |
… | |
1771 | TRANSFER (ta, 1); |
1797 | TRANSFER (ta, 1); |
1772 | } |
1798 | } |
1773 | |
1799 | |
1774 | /** Coro ********************************************************************/ |
1800 | /** Coro ********************************************************************/ |
1775 | |
1801 | |
1776 | INLINE void |
1802 | ecb_inline void |
1777 | coro_enq (pTHX_ struct coro *coro) |
1803 | coro_enq (pTHX_ struct coro *coro) |
1778 | { |
1804 | { |
1779 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1805 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1780 | |
1806 | |
1781 | SvREFCNT_inc_NN (coro->hv); |
1807 | SvREFCNT_inc_NN (coro->hv); |
… | |
… | |
1783 | coro->next_ready = 0; |
1809 | coro->next_ready = 0; |
1784 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1810 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1785 | ready [1] = coro; |
1811 | ready [1] = coro; |
1786 | } |
1812 | } |
1787 | |
1813 | |
1788 | INLINE struct coro * |
1814 | ecb_inline struct coro * |
1789 | coro_deq (pTHX) |
1815 | coro_deq (pTHX) |
1790 | { |
1816 | { |
1791 | int prio; |
1817 | int prio; |
1792 | |
1818 | |
1793 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
1819 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1846 | { |
1872 | { |
1847 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1873 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1848 | } |
1874 | } |
1849 | |
1875 | |
1850 | /* expects to own a reference to next->hv */ |
1876 | /* expects to own a reference to next->hv */ |
1851 | INLINE void |
1877 | ecb_inline void |
1852 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1878 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1853 | { |
1879 | { |
1854 | SV *prev_sv = SvRV (coro_current); |
1880 | SV *prev_sv = SvRV (coro_current); |
1855 | |
1881 | |
1856 | ta->prev = SvSTATE_hv (prev_sv); |
1882 | ta->prev = SvSTATE_hv (prev_sv); |
… | |
… | |
1869 | { |
1895 | { |
1870 | for (;;) |
1896 | for (;;) |
1871 | { |
1897 | { |
1872 | struct coro *next = coro_deq (aTHX); |
1898 | struct coro *next = coro_deq (aTHX); |
1873 | |
1899 | |
1874 | if (expect_true (next)) |
1900 | if (ecb_expect_true (next)) |
1875 | { |
1901 | { |
1876 | /* cannot transfer to destroyed coros, skip and look for next */ |
1902 | /* cannot transfer to destroyed coros, skip and look for next */ |
1877 | if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED))) |
1903 | if (ecb_expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED))) |
1878 | 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 */ |
1879 | else |
1905 | else |
1880 | { |
1906 | { |
1881 | next->flags &= ~CF_READY; |
1907 | next->flags &= ~CF_READY; |
1882 | --coro_nready; |
1908 | --coro_nready; |
… | |
… | |
1915 | } |
1941 | } |
1916 | } |
1942 | } |
1917 | } |
1943 | } |
1918 | } |
1944 | } |
1919 | |
1945 | |
1920 | INLINE void |
1946 | ecb_inline void |
1921 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1947 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1922 | { |
1948 | { |
1923 | api_ready (aTHX_ coro_current); |
1949 | api_ready (aTHX_ coro_current); |
1924 | prepare_schedule (aTHX_ ta); |
1950 | prepare_schedule (aTHX_ ta); |
1925 | } |
1951 | } |
1926 | |
1952 | |
1927 | INLINE void |
1953 | ecb_inline void |
1928 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1954 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1929 | { |
1955 | { |
1930 | SV *prev = SvRV (coro_current); |
1956 | SV *prev = SvRV (coro_current); |
1931 | |
1957 | |
1932 | if (coro_nready) |
1958 | if (coro_nready) |
… | |
… | |
1962 | { |
1988 | { |
1963 | struct coro_transfer_args ta; |
1989 | struct coro_transfer_args ta; |
1964 | |
1990 | |
1965 | prepare_cede (aTHX_ &ta); |
1991 | prepare_cede (aTHX_ &ta); |
1966 | |
1992 | |
1967 | if (expect_true (ta.prev != ta.next)) |
1993 | if (ecb_expect_true (ta.prev != ta.next)) |
1968 | { |
1994 | { |
1969 | TRANSFER (ta, 1); |
1995 | TRANSFER (ta, 1); |
1970 | return 1; |
1996 | return 1; |
1971 | } |
1997 | } |
1972 | else |
1998 | else |
… | |
… | |
2015 | coro->slot->runops = RUNOPS_DEFAULT; |
2041 | coro->slot->runops = RUNOPS_DEFAULT; |
2016 | } |
2042 | } |
2017 | } |
2043 | } |
2018 | |
2044 | |
2019 | static void |
2045 | static void |
|
|
2046 | coro_push_av (pTHX_ AV *av, I32 gimme_v) |
|
|
2047 | { |
|
|
2048 | if (AvFILLp (av) >= 0 && gimme_v != G_VOID) |
|
|
2049 | { |
|
|
2050 | dSP; |
|
|
2051 | |
|
|
2052 | if (gimme_v == G_SCALAR) |
|
|
2053 | XPUSHs (AvARRAY (av)[AvFILLp (av)]); |
|
|
2054 | else |
|
|
2055 | { |
|
|
2056 | int i; |
|
|
2057 | EXTEND (SP, AvFILLp (av) + 1); |
|
|
2058 | |
|
|
2059 | for (i = 0; i <= AvFILLp (av); ++i) |
|
|
2060 | PUSHs (AvARRAY (av)[i]); |
|
|
2061 | } |
|
|
2062 | |
|
|
2063 | PUTBACK; |
|
|
2064 | } |
|
|
2065 | } |
|
|
2066 | |
|
|
2067 | static void |
|
|
2068 | coro_push_on_destroy (pTHX_ struct coro *coro, SV *cb) |
|
|
2069 | { |
|
|
2070 | if (!coro->on_destroy) |
|
|
2071 | coro->on_destroy = newAV (); |
|
|
2072 | |
|
|
2073 | av_push (coro->on_destroy, cb); |
|
|
2074 | } |
|
|
2075 | |
|
|
2076 | static void |
|
|
2077 | slf_destroy_join (pTHX_ struct CoroSLF *frame) |
|
|
2078 | { |
|
|
2079 | SvREFCNT_dec ((SV *)((struct coro *)frame->data)->hv); |
|
|
2080 | } |
|
|
2081 | |
|
|
2082 | static int |
|
|
2083 | slf_check_join (pTHX_ struct CoroSLF *frame) |
|
|
2084 | { |
|
|
2085 | struct coro *coro = (struct coro *)frame->data; |
|
|
2086 | |
|
|
2087 | if (!coro->status) |
|
|
2088 | return 1; |
|
|
2089 | |
|
|
2090 | frame->destroy = 0; |
|
|
2091 | |
|
|
2092 | coro_push_av (aTHX_ coro->status, GIMME_V); |
|
|
2093 | |
|
|
2094 | SvREFCNT_dec ((SV *)coro->hv); |
|
|
2095 | |
|
|
2096 | return 0; |
|
|
2097 | } |
|
|
2098 | |
|
|
2099 | static void |
|
|
2100 | slf_init_join (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2101 | { |
|
|
2102 | struct coro *coro = SvSTATE (items > 0 ? arg [0] : &PL_sv_undef); |
|
|
2103 | |
|
|
2104 | if (items > 1) |
|
|
2105 | croak ("join called with too many arguments"); |
|
|
2106 | |
|
|
2107 | if (coro->status) |
|
|
2108 | frame->prepare = prepare_nop; |
|
|
2109 | else |
|
|
2110 | { |
|
|
2111 | coro_push_on_destroy (aTHX_ coro, SvREFCNT_inc_NN (SvRV (coro_current))); |
|
|
2112 | frame->prepare = prepare_schedule; |
|
|
2113 | } |
|
|
2114 | |
|
|
2115 | frame->check = slf_check_join; |
|
|
2116 | frame->destroy = slf_destroy_join; |
|
|
2117 | frame->data = (void *)coro; |
|
|
2118 | SvREFCNT_inc (coro->hv); |
|
|
2119 | } |
|
|
2120 | |
|
|
2121 | static void |
2020 | coro_call_on_destroy (pTHX_ struct coro *coro) |
2122 | coro_call_on_destroy (pTHX_ struct coro *coro) |
2021 | { |
2123 | { |
2022 | SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); |
2124 | AV *od = coro->on_destroy; |
2023 | |
2125 | |
2024 | if (on_destroyp) |
2126 | if (!od) |
2025 | { |
2127 | return; |
2026 | SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0); |
|
|
2027 | AV *on_destroy = sv_2mortal (SvREFCNT_inc ((AV *)SvRV (*on_destroyp))); |
|
|
2028 | AV *status = statusp ? sv_2mortal (SvREFCNT_inc ((AV *)SvRV (*statusp))) : 0; |
|
|
2029 | |
2128 | |
|
|
2129 | coro->on_destroy = 0; |
|
|
2130 | sv_2mortal ((SV *)od); |
|
|
2131 | |
2030 | while (AvFILLp (on_destroy) >= 0) |
2132 | while (AvFILLp (od) >= 0) |
|
|
2133 | { |
|
|
2134 | SV *cb = sv_2mortal (av_pop (od)); |
|
|
2135 | |
|
|
2136 | /* coro hv's (and only hv's at the moment) are supported as well */ |
|
|
2137 | if (SvSTATEhv_p (aTHX_ cb)) |
|
|
2138 | api_ready (aTHX_ cb); |
|
|
2139 | else |
2031 | { |
2140 | { |
2032 | dSP; /* don't disturb outer sp */ |
2141 | dSP; /* don't disturb outer sp */ |
2033 | SV *cb = av_pop (on_destroy); |
|
|
2034 | |
|
|
2035 | PUSHMARK (SP); |
2142 | PUSHMARK (SP); |
2036 | |
2143 | |
2037 | if (statusp) |
2144 | if (coro->status) |
2038 | { |
2145 | { |
2039 | int i; |
2146 | PUTBACK; |
2040 | EXTEND (SP, AvFILLp (status) + 1); |
2147 | coro_push_av (aTHX_ coro->status, G_ARRAY); |
2041 | |
2148 | SPAGAIN; |
2042 | for (i = 0; i <= AvFILLp (status); ++i) |
|
|
2043 | PUSHs (AvARRAY (status)[i]); |
|
|
2044 | } |
2149 | } |
2045 | |
2150 | |
2046 | PUTBACK; |
2151 | PUTBACK; |
2047 | call_sv (sv_2mortal (cb), G_VOID | G_DISCARD); |
2152 | call_sv (cb, G_VOID | G_DISCARD); |
2048 | } |
2153 | } |
2049 | } |
2154 | } |
2050 | } |
2155 | } |
2051 | |
2156 | |
2052 | static void |
2157 | static void |
2053 | coro_set_status (HV *coro_hv, SV **arg, int items) |
2158 | coro_set_status (pTHX_ struct coro *coro, SV **arg, int items) |
2054 | { |
2159 | { |
2055 | AV *av = newAV (); |
2160 | AV *av; |
|
|
2161 | |
|
|
2162 | if (coro->status) |
|
|
2163 | { |
|
|
2164 | av = coro->status; |
|
|
2165 | av_clear (av); |
|
|
2166 | } |
|
|
2167 | else |
|
|
2168 | av = coro->status = newAV (); |
2056 | |
2169 | |
2057 | /* items are actually not so common, so optimise for this case */ |
2170 | /* items are actually not so common, so optimise for this case */ |
2058 | if (items) |
2171 | if (items) |
2059 | { |
2172 | { |
2060 | int i; |
2173 | int i; |
… | |
… | |
2062 | av_extend (av, items - 1); |
2175 | av_extend (av, items - 1); |
2063 | |
2176 | |
2064 | for (i = 0; i < items; ++i) |
2177 | for (i = 0; i < items; ++i) |
2065 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
2178 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
2066 | } |
2179 | } |
2067 | |
|
|
2068 | hv_store (coro_hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
|
|
2069 | } |
2180 | } |
2070 | |
2181 | |
2071 | static void |
2182 | static void |
2072 | slf_init_terminate_cancel_common (pTHX_ struct CoroSLF *frame, HV *coro_hv) |
2183 | slf_init_terminate_cancel_common (pTHX_ struct CoroSLF *frame, HV *coro_hv) |
2073 | { |
2184 | { |
… | |
… | |
2085 | static void |
2196 | static void |
2086 | slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2197 | slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2087 | { |
2198 | { |
2088 | HV *coro_hv = (HV *)SvRV (coro_current); |
2199 | HV *coro_hv = (HV *)SvRV (coro_current); |
2089 | |
2200 | |
2090 | coro_set_status (coro_hv, arg, items); |
2201 | coro_set_status (aTHX_ SvSTATE ((SV *)coro_hv), arg, items); |
2091 | slf_init_terminate_cancel_common (frame, coro_hv); |
2202 | slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); |
2092 | } |
2203 | } |
2093 | |
2204 | |
2094 | static void |
2205 | static void |
2095 | slf_init_cancel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2206 | slf_init_cancel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2096 | { |
2207 | { |
… | |
… | |
2101 | croak ("Coro::cancel called without coro object,"); |
2212 | croak ("Coro::cancel called without coro object,"); |
2102 | |
2213 | |
2103 | coro = SvSTATE (arg [0]); |
2214 | coro = SvSTATE (arg [0]); |
2104 | coro_hv = coro->hv; |
2215 | coro_hv = coro->hv; |
2105 | |
2216 | |
2106 | coro_set_status (coro_hv, arg + 1, items - 1); |
2217 | coro_set_status (aTHX_ coro, arg + 1, items - 1); |
2107 | |
2218 | |
2108 | if (expect_false (coro->flags & CF_NOCANCEL)) |
2219 | if (ecb_expect_false (coro->flags & CF_NOCANCEL)) |
2109 | { |
2220 | { |
2110 | /* coro currently busy cancelling something, so just notify it */ |
2221 | /* coro currently busy cancelling something, so just notify it */ |
2111 | coro->slf_frame.data = (void *)coro; |
2222 | coro->slf_frame.data = (void *)coro; |
2112 | |
2223 | |
2113 | frame->prepare = prepare_nop; |
2224 | frame->prepare = prepare_nop; |
2114 | frame->check = slf_check_nop; |
2225 | frame->check = slf_check_nop; |
2115 | } |
2226 | } |
2116 | else if (coro_hv == (HV *)SvRV (coro_current)) |
2227 | else if (coro_hv == (HV *)SvRV (coro_current)) |
2117 | { |
2228 | { |
2118 | /* cancelling the current coro is allowed, and equals terminate */ |
2229 | /* cancelling the current coro is allowed, and equals terminate */ |
2119 | slf_init_terminate_cancel_common (frame, coro_hv); |
2230 | slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); |
2120 | } |
2231 | } |
2121 | else |
2232 | else |
2122 | { |
2233 | { |
2123 | struct coro *self = SvSTATE_current; |
2234 | struct coro *self = SvSTATE_current; |
2124 | |
2235 | |
… | |
… | |
2128 | * this is ugly, and hopefully fully worth the extra speed. |
2239 | * this is ugly, and hopefully fully worth the extra speed. |
2129 | * besides, I can't get the slow-but-safe version working... |
2240 | * besides, I can't get the slow-but-safe version working... |
2130 | */ |
2241 | */ |
2131 | slf_frame.data = 0; |
2242 | slf_frame.data = 0; |
2132 | self->flags |= CF_NOCANCEL; |
2243 | self->flags |= CF_NOCANCEL; |
2133 | |
|
|
2134 | coro_state_destroy (aTHX_ coro); |
2244 | coro_state_destroy (aTHX_ coro); |
2135 | coro_call_on_destroy (aTHX_ coro); |
|
|
2136 | |
|
|
2137 | self->flags &= ~CF_NOCANCEL; |
2245 | self->flags &= ~CF_NOCANCEL; |
2138 | |
2246 | |
2139 | if (slf_frame.data) |
2247 | if (slf_frame.data) |
2140 | { |
2248 | { |
2141 | /* while we were busy we have been cancelled, so terminate */ |
2249 | /* while we were busy we have been cancelled, so terminate */ |
2142 | slf_init_terminate_cancel_common (frame, self->hv); |
2250 | slf_init_terminate_cancel_common (aTHX_ frame, self->hv); |
2143 | } |
2251 | } |
2144 | else |
2252 | else |
2145 | { |
2253 | { |
2146 | frame->prepare = prepare_nop; |
2254 | frame->prepare = prepare_nop; |
2147 | frame->check = slf_check_nop; |
2255 | frame->check = slf_check_nop; |
2148 | } |
2256 | } |
2149 | } |
2257 | } |
2150 | } |
2258 | } |
2151 | |
2259 | |
|
|
2260 | static int |
|
|
2261 | slf_check_safe_cancel (pTHX_ struct CoroSLF *frame) |
|
|
2262 | { |
|
|
2263 | frame->prepare = 0; |
|
|
2264 | coro_unwind_stacks (aTHX); |
|
|
2265 | |
|
|
2266 | slf_init_terminate_cancel_common (aTHX_ frame, (HV *)SvRV (coro_current)); |
|
|
2267 | |
|
|
2268 | return 1; |
|
|
2269 | } |
|
|
2270 | |
|
|
2271 | static int |
|
|
2272 | safe_cancel (pTHX_ struct coro *coro, SV **arg, int items) |
|
|
2273 | { |
|
|
2274 | if (coro->cctx) |
|
|
2275 | croak ("coro inside C callback, unable to cancel at this time, caught"); |
|
|
2276 | |
|
|
2277 | if (coro->flags & CF_NEW) |
|
|
2278 | { |
|
|
2279 | coro_set_status (aTHX_ coro, arg, items); |
|
|
2280 | coro_state_destroy (aTHX_ coro); |
|
|
2281 | } |
|
|
2282 | else |
|
|
2283 | { |
|
|
2284 | if (!coro->slf_frame.prepare) |
|
|
2285 | croak ("coro outside an SLF function, unable to cancel at this time, caught"); |
|
|
2286 | |
|
|
2287 | slf_destroy (aTHX_ coro); |
|
|
2288 | |
|
|
2289 | coro_set_status (aTHX_ coro, arg, items); |
|
|
2290 | coro->slf_frame.prepare = prepare_nop; |
|
|
2291 | coro->slf_frame.check = slf_check_safe_cancel; |
|
|
2292 | |
|
|
2293 | api_ready (aTHX_ (SV *)coro->hv); |
|
|
2294 | } |
|
|
2295 | |
|
|
2296 | return 1; |
|
|
2297 | } |
|
|
2298 | |
2152 | /*****************************************************************************/ |
2299 | /*****************************************************************************/ |
2153 | /* async pool handler */ |
2300 | /* async pool handler */ |
2154 | |
2301 | |
2155 | static int |
2302 | static int |
2156 | slf_check_pool_handler (pTHX_ struct CoroSLF *frame) |
2303 | slf_check_pool_handler (pTHX_ struct CoroSLF *frame) |
… | |
… | |
2185 | slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2332 | slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2186 | { |
2333 | { |
2187 | HV *hv = (HV *)SvRV (coro_current); |
2334 | HV *hv = (HV *)SvRV (coro_current); |
2188 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2335 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2189 | |
2336 | |
2190 | if (expect_true (coro->saved_deffh)) |
2337 | if (ecb_expect_true (coro->saved_deffh)) |
2191 | { |
2338 | { |
2192 | /* subsequent iteration */ |
2339 | /* subsequent iteration */ |
2193 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2340 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2194 | coro->saved_deffh = 0; |
2341 | coro->saved_deffh = 0; |
2195 | |
2342 | |
2196 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
2343 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
2197 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2344 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
2198 | { |
2345 | { |
2199 | coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); |
2346 | slf_init_terminate_cancel_common (aTHX_ frame, hv); |
2200 | coro->invoke_av = newAV (); |
2347 | return; |
2201 | |
|
|
2202 | frame->prepare = prepare_nop; |
|
|
2203 | } |
2348 | } |
2204 | else |
2349 | else |
2205 | { |
2350 | { |
2206 | av_clear (GvAV (PL_defgv)); |
2351 | av_clear (GvAV (PL_defgv)); |
2207 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
2352 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
… | |
… | |
2257 | |
2402 | |
2258 | static int |
2403 | static int |
2259 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
2404 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
2260 | { |
2405 | { |
2261 | SV *data = (SV *)frame->data; |
2406 | SV *data = (SV *)frame->data; |
2262 | |
2407 | |
2263 | if (CORO_THROW) |
2408 | if (CORO_THROW) |
2264 | return 0; |
2409 | return 0; |
2265 | |
2410 | |
2266 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2411 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2267 | return 1; |
2412 | return 1; |
… | |
… | |
2303 | cb = sv_2mortal (coro->rouse_cb); |
2448 | cb = sv_2mortal (coro->rouse_cb); |
2304 | coro->rouse_cb = 0; |
2449 | coro->rouse_cb = 0; |
2305 | } |
2450 | } |
2306 | |
2451 | |
2307 | if (!SvROK (cb) |
2452 | if (!SvROK (cb) |
2308 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
2453 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
2309 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
2454 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
2310 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
2455 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
2311 | |
2456 | |
2312 | { |
2457 | { |
2313 | CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ |
2458 | CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ |
… | |
… | |
2430 | static void |
2575 | static void |
2431 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2576 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2432 | { |
2577 | { |
2433 | frame->prepare = prepare_cede_notself; |
2578 | frame->prepare = prepare_cede_notself; |
2434 | frame->check = slf_check_nop; |
2579 | frame->check = slf_check_nop; |
|
|
2580 | } |
|
|
2581 | |
|
|
2582 | /* "undo"/cancel a running slf call - used when cancelling a coro, mainly */ |
|
|
2583 | static void |
|
|
2584 | slf_destroy (pTHX_ struct coro *coro) |
|
|
2585 | { |
|
|
2586 | /* this callback is reserved for slf functions needing to do cleanup */ |
|
|
2587 | if (coro->slf_frame.destroy && coro->slf_frame.prepare && !PL_dirty) |
|
|
2588 | coro->slf_frame.destroy (aTHX_ &coro->slf_frame); |
|
|
2589 | |
|
|
2590 | /* |
|
|
2591 | * The on_destroy above most likely is from an SLF call. |
|
|
2592 | * Since by definition the SLF call will not finish when we destroy |
|
|
2593 | * the coro, we will have to force-finish it here, otherwise |
|
|
2594 | * cleanup functions cannot call SLF functions. |
|
|
2595 | */ |
|
|
2596 | coro->slf_frame.prepare = 0; |
2435 | } |
2597 | } |
2436 | |
2598 | |
2437 | /* |
2599 | /* |
2438 | * these not obviously related functions are all rolled into one |
2600 | * these not obviously related functions are all rolled into one |
2439 | * function to increase chances that they all will call transfer with the same |
2601 | * function to increase chances that they all will call transfer with the same |
… | |
… | |
2446 | I32 checkmark; /* mark SP to see how many elements check has pushed */ |
2608 | I32 checkmark; /* mark SP to see how many elements check has pushed */ |
2447 | |
2609 | |
2448 | /* set up the slf frame, unless it has already been set-up */ |
2610 | /* set up the slf frame, unless it has already been set-up */ |
2449 | /* the latter happens when a new coro has been started */ |
2611 | /* the latter happens when a new coro has been started */ |
2450 | /* or when a new cctx was attached to an existing coroutine */ |
2612 | /* or when a new cctx was attached to an existing coroutine */ |
2451 | if (expect_true (!slf_frame.prepare)) |
2613 | if (ecb_expect_true (!slf_frame.prepare)) |
2452 | { |
2614 | { |
2453 | /* first iteration */ |
2615 | /* first iteration */ |
2454 | dSP; |
2616 | dSP; |
2455 | SV **arg = PL_stack_base + TOPMARK + 1; |
2617 | SV **arg = PL_stack_base + TOPMARK + 1; |
2456 | int items = SP - arg; /* args without function object */ |
2618 | int items = SP - arg; /* args without function object */ |
… | |
… | |
2497 | while (slf_frame.check (aTHX_ &slf_frame)); |
2659 | while (slf_frame.check (aTHX_ &slf_frame)); |
2498 | |
2660 | |
2499 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
2661 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
2500 | |
2662 | |
2501 | /* exception handling */ |
2663 | /* exception handling */ |
2502 | if (expect_false (CORO_THROW)) |
2664 | if (ecb_expect_false (CORO_THROW)) |
2503 | { |
2665 | { |
2504 | SV *exception = sv_2mortal (CORO_THROW); |
2666 | SV *exception = sv_2mortal (CORO_THROW); |
2505 | |
2667 | |
2506 | CORO_THROW = 0; |
2668 | CORO_THROW = 0; |
2507 | sv_setsv (ERRSV, exception); |
2669 | sv_setsv (ERRSV, exception); |
2508 | croak (0); |
2670 | croak (0); |
2509 | } |
2671 | } |
2510 | |
2672 | |
2511 | /* return value handling - mostly like entersub */ |
2673 | /* return value handling - mostly like entersub */ |
2512 | /* make sure we put something on the stack in scalar context */ |
2674 | /* make sure we put something on the stack in scalar context */ |
2513 | if (GIMME_V == G_SCALAR) |
2675 | if (GIMME_V == G_SCALAR |
|
|
2676 | && ecb_expect_false (PL_stack_sp != PL_stack_base + checkmark + 1)) |
2514 | { |
2677 | { |
2515 | dSP; |
2678 | dSP; |
2516 | SV **bot = PL_stack_base + checkmark; |
2679 | SV **bot = PL_stack_base + checkmark; |
2517 | |
2680 | |
2518 | if (sp == bot) /* too few, push undef */ |
2681 | if (sp == bot) /* too few, push undef */ |
2519 | bot [1] = &PL_sv_undef; |
2682 | bot [1] = &PL_sv_undef; |
2520 | else if (sp != bot + 1) /* too many, take last one */ |
2683 | else /* too many, take last one */ |
2521 | bot [1] = *sp; |
2684 | bot [1] = *sp; |
2522 | |
2685 | |
2523 | SP = bot + 1; |
2686 | SP = bot + 1; |
2524 | |
2687 | |
2525 | PUTBACK; |
2688 | PUTBACK; |
… | |
… | |
2632 | { |
2795 | { |
2633 | PerlIOBuf base; |
2796 | PerlIOBuf base; |
2634 | NV next, every; |
2797 | NV next, every; |
2635 | } PerlIOCede; |
2798 | } PerlIOCede; |
2636 | |
2799 | |
2637 | static IV |
2800 | static IV ecb_cold |
2638 | PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
2801 | PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
2639 | { |
2802 | { |
2640 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
2803 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
2641 | |
2804 | |
2642 | self->every = SvCUR (arg) ? SvNV (arg) : 0.01; |
2805 | self->every = SvCUR (arg) ? SvNV (arg) : 0.01; |
2643 | self->next = nvtime () + self->every; |
2806 | self->next = nvtime () + self->every; |
2644 | |
2807 | |
2645 | return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); |
2808 | return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); |
2646 | } |
2809 | } |
2647 | |
2810 | |
2648 | static SV * |
2811 | static SV * ecb_cold |
2649 | PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
2812 | PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
2650 | { |
2813 | { |
2651 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
2814 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
2652 | |
2815 | |
2653 | return newSVnv (self->every); |
2816 | return newSVnv (self->every); |
… | |
… | |
2765 | SvREFCNT_dec (cb); |
2928 | SvREFCNT_dec (cb); |
2766 | } |
2929 | } |
2767 | } |
2930 | } |
2768 | |
2931 | |
2769 | static void |
2932 | static void |
2770 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
2933 | coro_semaphore_destroy (pTHX_ struct CoroSLF *frame) |
2771 | { |
2934 | { |
2772 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2935 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2773 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2936 | coro_semaphore_adjust (aTHX_ (AV *)frame->data, 0); |
2774 | } |
2937 | } |
2775 | |
2938 | |
2776 | static int |
2939 | static int |
2777 | slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire) |
2940 | slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire) |
2778 | { |
2941 | { |
2779 | AV *av = (AV *)frame->data; |
2942 | AV *av = (AV *)frame->data; |
2780 | SV *count_sv = AvARRAY (av)[0]; |
2943 | SV *count_sv = AvARRAY (av)[0]; |
|
|
2944 | SV *coro_hv = SvRV (coro_current); |
2781 | |
2945 | |
2782 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
2946 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
2783 | if (CORO_THROW) |
2947 | if (CORO_THROW) |
2784 | return 0; |
2948 | return 0; |
2785 | else if (SvIVX (count_sv) > 0) |
2949 | else if (SvIVX (count_sv) > 0) |
2786 | { |
2950 | { |
2787 | SvSTATE_current->on_destroy = 0; |
2951 | frame->destroy = 0; |
2788 | |
2952 | |
2789 | if (acquire) |
2953 | if (acquire) |
2790 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2954 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2791 | else |
2955 | else |
2792 | coro_semaphore_adjust (aTHX_ av, 0); |
2956 | coro_semaphore_adjust (aTHX_ av, 0); |
… | |
… | |
2797 | { |
2961 | { |
2798 | int i; |
2962 | int i; |
2799 | /* if we were woken up but can't down, we look through the whole */ |
2963 | /* if we were woken up but can't down, we look through the whole */ |
2800 | /* waiters list and only add us if we aren't in there already */ |
2964 | /* waiters list and only add us if we aren't in there already */ |
2801 | /* this avoids some degenerate memory usage cases */ |
2965 | /* this avoids some degenerate memory usage cases */ |
2802 | |
2966 | for (i = AvFILLp (av); i > 0; --i) /* i > 0 is not an off-by-one bug */ |
2803 | for (i = 1; i <= AvFILLp (av); ++i) |
|
|
2804 | if (AvARRAY (av)[i] == SvRV (coro_current)) |
2967 | if (AvARRAY (av)[i] == coro_hv) |
2805 | return 1; |
2968 | return 1; |
2806 | |
2969 | |
2807 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2970 | av_push (av, SvREFCNT_inc (coro_hv)); |
2808 | return 1; |
2971 | return 1; |
2809 | } |
2972 | } |
2810 | } |
2973 | } |
2811 | |
2974 | |
2812 | static int |
2975 | static int |
… | |
… | |
2835 | { |
2998 | { |
2836 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2999 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2837 | |
3000 | |
2838 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
3001 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2839 | frame->prepare = prepare_schedule; |
3002 | frame->prepare = prepare_schedule; |
2840 | |
|
|
2841 | /* to avoid race conditions when a woken-up coro gets terminated */ |
3003 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2842 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
3004 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
2843 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
3005 | frame->destroy = coro_semaphore_destroy; |
2844 | } |
3006 | } |
2845 | } |
3007 | } |
2846 | |
3008 | |
2847 | static void |
3009 | static void |
2848 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
3010 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
… | |
… | |
3017 | /* it quickly returns */ |
3179 | /* it quickly returns */ |
3018 | if (CORO_THROW) |
3180 | if (CORO_THROW) |
3019 | return 0; |
3181 | return 0; |
3020 | |
3182 | |
3021 | /* one element that is an RV? repeat! */ |
3183 | /* one element that is an RV? repeat! */ |
3022 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
3184 | if (AvFILLp (state) == 0 && SvTYPE (AvARRAY (state)[0]) != SVt_PV) |
3023 | return 1; |
3185 | return 1; |
3024 | |
3186 | |
3025 | /* restore status */ |
3187 | /* restore status */ |
3026 | { |
3188 | { |
3027 | SV *data_sv = av_pop (state); |
3189 | SV *data_sv = av_pop (state); |
… | |
… | |
3066 | dSP; |
3228 | dSP; |
3067 | |
3229 | |
3068 | static SV *prio_cv; |
3230 | static SV *prio_cv; |
3069 | static SV *prio_sv; |
3231 | static SV *prio_sv; |
3070 | |
3232 | |
3071 | if (expect_false (!prio_cv)) |
3233 | if (ecb_expect_false (!prio_cv)) |
3072 | { |
3234 | { |
3073 | prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0); |
3235 | prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0); |
3074 | prio_sv = newSViv (0); |
3236 | prio_sv = newSViv (0); |
3075 | } |
3237 | } |
3076 | |
3238 | |
… | |
… | |
3182 | } |
3344 | } |
3183 | |
3345 | |
3184 | return coro_sv; |
3346 | return coro_sv; |
3185 | } |
3347 | } |
3186 | |
3348 | |
|
|
3349 | #ifndef __cplusplus |
|
|
3350 | ecb_cold XS(boot_Coro__State); |
|
|
3351 | #endif |
|
|
3352 | |
|
|
3353 | #if CORO_JIT |
|
|
3354 | |
|
|
3355 | static void ecb_noinline ecb_cold |
|
|
3356 | pushav_4uv (pTHX_ UV a, UV b, UV c, UV d) |
|
|
3357 | { |
|
|
3358 | dSP; |
|
|
3359 | AV *av = newAV (); |
|
|
3360 | |
|
|
3361 | av_store (av, 3, newSVuv (d)); |
|
|
3362 | av_store (av, 2, newSVuv (c)); |
|
|
3363 | av_store (av, 1, newSVuv (b)); |
|
|
3364 | av_store (av, 0, newSVuv (a)); |
|
|
3365 | |
|
|
3366 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
3367 | |
|
|
3368 | PUTBACK; |
|
|
3369 | } |
|
|
3370 | |
|
|
3371 | static void ecb_noinline ecb_cold |
|
|
3372 | jit_init (pTHX) |
|
|
3373 | { |
|
|
3374 | dSP; |
|
|
3375 | SV *load, *save; |
|
|
3376 | char *map_base; |
|
|
3377 | char *load_ptr, *save_ptr; |
|
|
3378 | STRLEN load_len, save_len, map_len; |
|
|
3379 | int count; |
|
|
3380 | |
|
|
3381 | eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); |
|
|
3382 | |
|
|
3383 | PUSHMARK (SP); |
|
|
3384 | #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type)); |
|
|
3385 | #include "state.h" |
|
|
3386 | count = call_pv ("Coro::State::_jit", G_ARRAY); |
|
|
3387 | SPAGAIN; |
|
|
3388 | |
|
|
3389 | save = POPs; save_ptr = SvPVbyte (save, save_len); |
|
|
3390 | load = POPs; load_ptr = SvPVbyte (load, load_len); |
|
|
3391 | |
|
|
3392 | map_len = load_len + save_len + 16; |
|
|
3393 | |
|
|
3394 | map_base = mmap (0, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
|
|
3395 | |
|
|
3396 | assert (("Coro: unable to mmap jit code page, cannot continue.", map_base != (char *)MAP_FAILED)); |
|
|
3397 | |
|
|
3398 | load_perl_slots = (load_save_perl_slots_type)map_base; |
|
|
3399 | memcpy (map_base, load_ptr, load_len); |
|
|
3400 | |
|
|
3401 | map_base += (load_len + 15) & ~15; |
|
|
3402 | |
|
|
3403 | save_perl_slots = (load_save_perl_slots_type)map_base; |
|
|
3404 | memcpy (map_base, save_ptr, save_len); |
|
|
3405 | |
|
|
3406 | /* we are good citizens and try to make the page read-only, so the evil evil */ |
|
|
3407 | /* hackers might have it a bit more difficult */ |
|
|
3408 | mprotect (map_base, map_len, PROT_READ | PROT_EXEC); |
|
|
3409 | |
|
|
3410 | PUTBACK; |
|
|
3411 | eval_pv ("undef &Coro::State::_jit", 1); |
|
|
3412 | } |
|
|
3413 | |
|
|
3414 | #endif |
|
|
3415 | |
3187 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3416 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3188 | |
3417 | |
3189 | PROTOTYPES: DISABLE |
3418 | PROTOTYPES: DISABLE |
3190 | |
3419 | |
3191 | BOOT: |
3420 | BOOT: |
… | |
… | |
3193 | #ifdef USE_ITHREADS |
3422 | #ifdef USE_ITHREADS |
3194 | # if CORO_PTHREAD |
3423 | # if CORO_PTHREAD |
3195 | coro_thx = PERL_GET_CONTEXT; |
3424 | coro_thx = PERL_GET_CONTEXT; |
3196 | # endif |
3425 | # endif |
3197 | #endif |
3426 | #endif |
3198 | BOOT_PAGESIZE; |
3427 | /* perl defines these to check for existance first, but why it doesn't */ |
|
|
3428 | /* just create them one at init time is not clear to me, except for */ |
|
|
3429 | /* programs trying to delete them, but... */ |
|
|
3430 | /* anyway, we declare this as invalid and make sure they are initialised here */ |
|
|
3431 | DEFSV; |
|
|
3432 | ERRSV; |
3199 | |
3433 | |
3200 | cctx_current = cctx_new_empty (); |
3434 | cctx_current = cctx_new_empty (); |
3201 | |
3435 | |
3202 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3436 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
3203 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
3437 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
… | |
… | |
3246 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3480 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3247 | |
3481 | |
3248 | time_init (aTHX); |
3482 | time_init (aTHX); |
3249 | |
3483 | |
3250 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3484 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
|
|
3485 | #if CORO_JIT |
|
|
3486 | PUTBACK; |
|
|
3487 | jit_init (aTHX); |
|
|
3488 | SPAGAIN; |
|
|
3489 | #endif |
3251 | } |
3490 | } |
3252 | |
3491 | |
3253 | SV * |
3492 | SV * |
3254 | new (SV *klass, ...) |
3493 | new (SV *klass, ...) |
3255 | ALIAS: |
3494 | ALIAS: |
… | |
… | |
3262 | void |
3501 | void |
3263 | transfer (...) |
3502 | transfer (...) |
3264 | PROTOTYPE: $$ |
3503 | PROTOTYPE: $$ |
3265 | CODE: |
3504 | CODE: |
3266 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3505 | CORO_EXECUTE_SLF_XS (slf_init_transfer); |
3267 | |
|
|
3268 | void |
|
|
3269 | _exit (int code) |
|
|
3270 | PROTOTYPE: $ |
|
|
3271 | CODE: |
|
|
3272 | _exit (code); |
|
|
3273 | |
3506 | |
3274 | SV * |
3507 | SV * |
3275 | clone (Coro::State coro) |
3508 | clone (Coro::State coro) |
3276 | CODE: |
3509 | CODE: |
3277 | { |
3510 | { |
… | |
… | |
3332 | void |
3565 | void |
3333 | list () |
3566 | list () |
3334 | PROTOTYPE: |
3567 | PROTOTYPE: |
3335 | PPCODE: |
3568 | PPCODE: |
3336 | { |
3569 | { |
3337 | struct coro *coro; |
3570 | struct coro *coro; |
3338 | for (coro = coro_first; coro; coro = coro->next) |
3571 | for (coro = coro_first; coro; coro = coro->next) |
3339 | if (coro->hv) |
3572 | if (coro->hv) |
3340 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
3573 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
3341 | } |
3574 | } |
3342 | |
3575 | |
… | |
… | |
3397 | PROTOTYPE: $ |
3630 | PROTOTYPE: $ |
3398 | ALIAS: |
3631 | ALIAS: |
3399 | is_ready = CF_READY |
3632 | is_ready = CF_READY |
3400 | is_running = CF_RUNNING |
3633 | is_running = CF_RUNNING |
3401 | is_new = CF_NEW |
3634 | is_new = CF_NEW |
3402 | is_destroyed = CF_DESTROYED |
3635 | is_destroyed = CF_ZOMBIE |
|
|
3636 | is_zombie = CF_ZOMBIE |
3403 | is_suspended = CF_SUSPENDED |
3637 | is_suspended = CF_SUSPENDED |
3404 | CODE: |
3638 | CODE: |
3405 | RETVAL = boolSV (coro->flags & ix); |
3639 | RETVAL = boolSV (coro->flags & ix); |
3406 | OUTPUT: |
3640 | OUTPUT: |
3407 | RETVAL |
3641 | RETVAL |
3408 | |
3642 | |
3409 | void |
3643 | void |
3410 | throw (Coro::State self, SV *exception = &PL_sv_undef) |
3644 | throw (SV *self, SV *exception = &PL_sv_undef) |
3411 | PROTOTYPE: $;$ |
3645 | PROTOTYPE: $;$ |
3412 | CODE: |
3646 | CODE: |
3413 | { |
3647 | { |
|
|
3648 | struct coro *coro = SvSTATE (self); |
3414 | struct coro *current = SvSTATE_current; |
3649 | struct coro *current = SvSTATE_current; |
3415 | SV **exceptionp = self == current ? &CORO_THROW : &self->except; |
3650 | SV **exceptionp = coro == current ? &CORO_THROW : &coro->except; |
3416 | SvREFCNT_dec (*exceptionp); |
3651 | SvREFCNT_dec (*exceptionp); |
3417 | SvGETMAGIC (exception); |
3652 | SvGETMAGIC (exception); |
3418 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
3653 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
|
|
3654 | |
|
|
3655 | api_ready (aTHX_ self); |
3419 | } |
3656 | } |
3420 | |
3657 | |
3421 | void |
3658 | void |
3422 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3659 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3423 | PROTOTYPE: $;$ |
3660 | PROTOTYPE: $;$ |
… | |
… | |
3500 | |
3737 | |
3501 | void |
3738 | void |
3502 | times (Coro::State self) |
3739 | times (Coro::State self) |
3503 | PPCODE: |
3740 | PPCODE: |
3504 | { |
3741 | { |
3505 | struct coro *current = SvSTATE (coro_current); |
3742 | struct coro *current = SvSTATE (coro_current); |
3506 | |
3743 | |
3507 | if (expect_false (current == self)) |
3744 | if (ecb_expect_false (current == self)) |
3508 | { |
3745 | { |
3509 | coro_times_update (); |
3746 | coro_times_update (); |
3510 | coro_times_add (SvSTATE (coro_current)); |
3747 | coro_times_add (SvSTATE (coro_current)); |
3511 | } |
3748 | } |
3512 | |
3749 | |
3513 | EXTEND (SP, 2); |
3750 | EXTEND (SP, 2); |
3514 | PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); |
3751 | PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); |
3515 | PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); |
3752 | PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); |
3516 | |
3753 | |
3517 | if (expect_false (current == self)) |
3754 | if (ecb_expect_false (current == self)) |
3518 | coro_times_sub (SvSTATE (coro_current)); |
3755 | coro_times_sub (SvSTATE (coro_current)); |
3519 | } |
3756 | } |
3520 | |
3757 | |
3521 | void |
3758 | void |
3522 | swap_sv (Coro::State coro, SV *sv, SV *swapsv) |
3759 | swap_sv (Coro::State coro, SV *sv, SV *swapsv) |
… | |
… | |
3543 | BOOT: |
3780 | BOOT: |
3544 | { |
3781 | { |
3545 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3782 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3546 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3783 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3547 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3784 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3548 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
|
|
3549 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3785 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3550 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3786 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3551 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3787 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3552 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
3788 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
3553 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3789 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3554 | |
3790 | |
3555 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3791 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3556 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3792 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3557 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3793 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3558 | CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ |
3794 | CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ |
3559 | |
3795 | |
3560 | coro_stash = gv_stashpv ("Coro", TRUE); |
3796 | coro_stash = gv_stashpv ("Coro", TRUE); |
3561 | |
3797 | |
3562 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3798 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3563 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3799 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3564 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
3800 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
… | |
… | |
3596 | void |
3832 | void |
3597 | _destroy (Coro::State coro) |
3833 | _destroy (Coro::State coro) |
3598 | CODE: |
3834 | CODE: |
3599 | /* used by the manager thread */ |
3835 | /* used by the manager thread */ |
3600 | coro_state_destroy (aTHX_ coro); |
3836 | coro_state_destroy (aTHX_ coro); |
|
|
3837 | |
|
|
3838 | void |
|
|
3839 | on_destroy (Coro::State coro, SV *cb) |
|
|
3840 | CODE: |
3601 | coro_call_on_destroy (aTHX_ coro); |
3841 | coro_push_on_destroy (aTHX_ coro, newSVsv (cb)); |
|
|
3842 | |
|
|
3843 | void |
|
|
3844 | join (...) |
|
|
3845 | CODE: |
|
|
3846 | CORO_EXECUTE_SLF_XS (slf_init_join); |
3602 | |
3847 | |
3603 | void |
3848 | void |
3604 | terminate (...) |
3849 | terminate (...) |
3605 | CODE: |
3850 | CODE: |
3606 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3851 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3607 | |
3852 | |
3608 | void |
3853 | void |
3609 | cancel (...) |
3854 | cancel (...) |
3610 | CODE: |
3855 | CODE: |
3611 | CORO_EXECUTE_SLF_XS (slf_init_cancel); |
3856 | CORO_EXECUTE_SLF_XS (slf_init_cancel); |
|
|
3857 | |
|
|
3858 | int |
|
|
3859 | safe_cancel (Coro::State self, ...) |
|
|
3860 | C_ARGS: aTHX_ self, &ST (1), items - 1 |
3612 | |
3861 | |
3613 | void |
3862 | void |
3614 | schedule (...) |
3863 | schedule (...) |
3615 | CODE: |
3864 | CODE: |
3616 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
3865 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
… | |
… | |
3773 | on_leave = 1 |
4022 | on_leave = 1 |
3774 | PROTOTYPE: & |
4023 | PROTOTYPE: & |
3775 | CODE: |
4024 | CODE: |
3776 | { |
4025 | { |
3777 | struct coro *coro = SvSTATE_current; |
4026 | struct coro *coro = SvSTATE_current; |
3778 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
4027 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
3779 | |
4028 | |
3780 | block = s_get_cv_croak (block); |
4029 | block = s_get_cv_croak (block); |
3781 | |
4030 | |
3782 | if (!*avp) |
4031 | if (!*avp) |
3783 | *avp = newAV (); |
4032 | *avp = newAV (); |
… | |
… | |
3803 | |
4052 | |
3804 | SV * |
4053 | SV * |
3805 | new (SV *klass, SV *count = 0) |
4054 | new (SV *klass, SV *count = 0) |
3806 | CODE: |
4055 | CODE: |
3807 | { |
4056 | { |
3808 | int semcnt = 1; |
4057 | int semcnt = 1; |
3809 | |
4058 | |
3810 | if (count) |
4059 | if (count) |
3811 | { |
4060 | { |
3812 | SvGETMAGIC (count); |
4061 | SvGETMAGIC (count); |
3813 | |
4062 | |
… | |
… | |
3873 | XSRETURN_NO; |
4122 | XSRETURN_NO; |
3874 | } |
4123 | } |
3875 | |
4124 | |
3876 | void |
4125 | void |
3877 | waiters (SV *self) |
4126 | waiters (SV *self) |
3878 | PPCODE: |
4127 | PPCODE: |
3879 | { |
4128 | { |
3880 | AV *av = (AV *)SvRV (self); |
4129 | AV *av = (AV *)SvRV (self); |
3881 | int wcount = AvFILLp (av) + 1 - 1; |
4130 | int wcount = AvFILLp (av) + 1 - 1; |
3882 | |
4131 | |
3883 | if (GIMME_V == G_SCALAR) |
4132 | if (GIMME_V == G_SCALAR) |
… | |
… | |
3892 | } |
4141 | } |
3893 | |
4142 | |
3894 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
4143 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
3895 | |
4144 | |
3896 | void |
4145 | void |
3897 | _may_delete (SV *sem, int count, int extra_refs) |
4146 | _may_delete (SV *sem, int count, unsigned int extra_refs) |
3898 | PPCODE: |
4147 | PPCODE: |
3899 | { |
4148 | { |
3900 | AV *av = (AV *)SvRV (sem); |
4149 | AV *av = (AV *)SvRV (sem); |
3901 | |
4150 | |
3902 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
4151 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
3903 | && AvFILLp (av) == 0 /* no waiters, just count */ |
4152 | && AvFILLp (av) == 0 /* no waiters, just count */ |
3904 | && SvIV (AvARRAY (av)[0]) == count) |
4153 | && SvIV (AvARRAY (av)[0]) == count) |
3905 | XSRETURN_YES; |
4154 | XSRETURN_YES; |
… | |
… | |
3926 | |
4175 | |
3927 | void |
4176 | void |
3928 | broadcast (SV *self) |
4177 | broadcast (SV *self) |
3929 | CODE: |
4178 | CODE: |
3930 | { |
4179 | { |
3931 | AV *av = (AV *)SvRV (self); |
4180 | AV *av = (AV *)SvRV (self); |
3932 | coro_signal_wake (aTHX_ av, AvFILLp (av)); |
4181 | coro_signal_wake (aTHX_ av, AvFILLp (av)); |
3933 | } |
4182 | } |
3934 | |
4183 | |
3935 | void |
4184 | void |
3936 | send (SV *self) |
4185 | send (SV *self) |
… | |
… | |
3944 | SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ |
4193 | SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ |
3945 | } |
4194 | } |
3946 | |
4195 | |
3947 | IV |
4196 | IV |
3948 | awaited (SV *self) |
4197 | awaited (SV *self) |
3949 | CODE: |
4198 | CODE: |
3950 | RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; |
4199 | RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; |
3951 | OUTPUT: |
4200 | OUTPUT: |
3952 | RETVAL |
4201 | RETVAL |
3953 | |
4202 | |
3954 | |
4203 | |
… | |
… | |
3959 | |
4208 | |
3960 | void |
4209 | void |
3961 | _schedule (...) |
4210 | _schedule (...) |
3962 | CODE: |
4211 | CODE: |
3963 | { |
4212 | { |
3964 | static int incede; |
4213 | static int incede; |
3965 | |
4214 | |
3966 | api_cede_notself (aTHX); |
4215 | api_cede_notself (aTHX); |
3967 | |
4216 | |
3968 | ++incede; |
4217 | ++incede; |
3969 | while (coro_nready >= incede && api_cede (aTHX)) |
4218 | while (coro_nready >= incede && api_cede (aTHX)) |
… | |
… | |
4013 | { |
4262 | { |
4014 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
4263 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
4015 | coro_old_pp_sselect = 0; |
4264 | coro_old_pp_sselect = 0; |
4016 | } |
4265 | } |
4017 | |
4266 | |
|
|
4267 | MODULE = Coro::State PACKAGE = Coro::Util |
|
|
4268 | |
|
|
4269 | void |
|
|
4270 | _exit (int code) |
|
|
4271 | CODE: |
|
|
4272 | _exit (code); |
|
|
4273 | |
|
|
4274 | NV |
|
|
4275 | time () |
|
|
4276 | CODE: |
|
|
4277 | RETVAL = nvtime (aTHX); |
|
|
4278 | OUTPUT: |
|
|
4279 | RETVAL |
|
|
4280 | |
|
|
4281 | NV |
|
|
4282 | gettimeofday () |
|
|
4283 | PPCODE: |
|
|
4284 | { |
|
|
4285 | UV tv [2]; |
|
|
4286 | u2time (aTHX_ tv); |
|
|
4287 | EXTEND (SP, 2); |
|
|
4288 | PUSHs (sv_2mortal (newSVuv (tv [0]))); |
|
|
4289 | PUSHs (sv_2mortal (newSVuv (tv [1]))); |
|
|
4290 | } |
|
|
4291 | |