… | |
… | |
133 | #else |
133 | #else |
134 | # define dSTACKLEVEL volatile void *stacklevel |
134 | # define dSTACKLEVEL volatile void *stacklevel |
135 | # define STACKLEVEL ((void *)&stacklevel) |
135 | # define STACKLEVEL ((void *)&stacklevel) |
136 | #endif |
136 | #endif |
137 | |
137 | |
138 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
138 | #define IN_DESTRUCT PL_dirty |
139 | |
139 | |
140 | #if __GNUC__ >= 3 |
140 | #if __GNUC__ >= 3 |
141 | # define attribute(x) __attribute__(x) |
141 | # define attribute(x) __attribute__(x) |
142 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
142 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
143 | # define INLINE static inline |
143 | # define INLINE static inline |
144 | #else |
144 | #else |
145 | # define attribute(x) |
145 | # define attribute(x) |
146 | # define expect(expr,value) (expr) |
146 | # define expect(expr,value) (expr) |
147 | # define INLINE static |
147 | # define INLINE static |
… | |
… | |
159 | # if CORO_PTHREAD |
159 | # if CORO_PTHREAD |
160 | static void *coro_thx; |
160 | static void *coro_thx; |
161 | # endif |
161 | # endif |
162 | #endif |
162 | #endif |
163 | |
163 | |
|
|
164 | #ifdef __linux |
|
|
165 | # include <time.h> /* for timespec */ |
|
|
166 | # include <syscall.h> /* for SYS_* */ |
|
|
167 | # ifdef SYS_clock_gettime |
|
|
168 | # define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
|
|
169 | # define CORO_CLOCK_MONOTONIC 1 |
|
|
170 | # define CORO_CLOCK_THREAD_CPUTIME_ID 3 |
|
|
171 | # endif |
|
|
172 | #endif |
|
|
173 | |
164 | static double (*nvtime)(); /* so why doesn't it take void? */ |
174 | static double (*nvtime)(); /* so why doesn't it take void? */ |
|
|
175 | static void (*u2time)(pTHX_ UV ret[2]); |
165 | |
176 | |
166 | /* we hijack an hopefully unused CV flag for our purposes */ |
177 | /* we hijack an hopefully unused CV flag for our purposes */ |
167 | #define CVf_SLF 0x4000 |
178 | #define CVf_SLF 0x4000 |
168 | static OP *pp_slf (pTHX); |
179 | static OP *pp_slf (pTHX); |
169 | |
180 | |
… | |
… | |
195 | static CV *cv_coro_state_new; |
206 | static CV *cv_coro_state_new; |
196 | |
207 | |
197 | /* Coro::AnyEvent */ |
208 | /* Coro::AnyEvent */ |
198 | static SV *sv_activity; |
209 | static SV *sv_activity; |
199 | |
210 | |
|
|
211 | /* enable processtime/realtime profiling */ |
|
|
212 | static char enable_times; |
|
|
213 | typedef U32 coro_ts[2]; |
|
|
214 | static coro_ts time_real, time_cpu; |
|
|
215 | static char times_valid; |
|
|
216 | |
200 | static struct coro_cctx *cctx_first; |
217 | static struct coro_cctx *cctx_first; |
201 | static int cctx_count, cctx_idle; |
218 | static int cctx_count, cctx_idle; |
202 | |
219 | |
203 | enum { |
220 | enum { |
204 | CC_MAPPED = 0x01, |
221 | CC_MAPPED = 0x01, |
… | |
… | |
238 | enum { |
255 | enum { |
239 | CF_RUNNING = 0x0001, /* coroutine is running */ |
256 | CF_RUNNING = 0x0001, /* coroutine is running */ |
240 | CF_READY = 0x0002, /* coroutine is ready */ |
257 | CF_READY = 0x0002, /* coroutine is ready */ |
241 | CF_NEW = 0x0004, /* has never been switched to */ |
258 | CF_NEW = 0x0004, /* has never been switched to */ |
242 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
259 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
|
|
260 | CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */ |
243 | }; |
261 | }; |
244 | |
262 | |
245 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
263 | /* the structure where most of the perl state is stored, overlaid on the cxstack */ |
246 | typedef struct |
264 | typedef struct |
247 | { |
265 | { |
… | |
… | |
260 | /* this is a structure representing a perl-level coroutine */ |
278 | /* this is a structure representing a perl-level coroutine */ |
261 | struct coro { |
279 | struct coro { |
262 | /* the C coroutine allocated to this perl coroutine, if any */ |
280 | /* the C coroutine allocated to this perl coroutine, if any */ |
263 | coro_cctx *cctx; |
281 | coro_cctx *cctx; |
264 | |
282 | |
|
|
283 | /* ready queue */ |
|
|
284 | struct coro *next_ready; |
|
|
285 | |
265 | /* state data */ |
286 | /* state data */ |
266 | struct CoroSLF slf_frame; /* saved slf frame */ |
287 | struct CoroSLF slf_frame; /* saved slf frame */ |
267 | AV *mainstack; |
288 | AV *mainstack; |
268 | perl_slots *slot; /* basically the saved sp */ |
289 | perl_slots *slot; /* basically the saved sp */ |
269 | |
290 | |
… | |
… | |
285 | /* async_pool */ |
306 | /* async_pool */ |
286 | SV *saved_deffh; |
307 | SV *saved_deffh; |
287 | SV *invoke_cb; |
308 | SV *invoke_cb; |
288 | AV *invoke_av; |
309 | AV *invoke_av; |
289 | |
310 | |
|
|
311 | /* on_enter/on_leave */ |
|
|
312 | AV *on_enter; |
|
|
313 | AV *on_leave; |
|
|
314 | |
|
|
315 | /* times */ |
|
|
316 | coro_ts t_cpu, t_real; |
|
|
317 | |
290 | /* linked list */ |
318 | /* linked list */ |
291 | struct coro *next, *prev; |
319 | struct coro *next, *prev; |
292 | }; |
320 | }; |
293 | |
321 | |
294 | typedef struct coro *Coro__State; |
322 | typedef struct coro *Coro__State; |
… | |
… | |
299 | /* the mainr easonw e don't support windows process emulation */ |
327 | /* the mainr easonw e don't support windows process emulation */ |
300 | static struct CoroSLF slf_frame; /* the current slf frame */ |
328 | static struct CoroSLF slf_frame; /* the current slf frame */ |
301 | |
329 | |
302 | /** Coro ********************************************************************/ |
330 | /** Coro ********************************************************************/ |
303 | |
331 | |
304 | #define PRIO_MAX 3 |
332 | #define CORO_PRIO_MAX 3 |
305 | #define PRIO_HIGH 1 |
333 | #define CORO_PRIO_HIGH 1 |
306 | #define PRIO_NORMAL 0 |
334 | #define CORO_PRIO_NORMAL 0 |
307 | #define PRIO_LOW -1 |
335 | #define CORO_PRIO_LOW -1 |
308 | #define PRIO_IDLE -3 |
336 | #define CORO_PRIO_IDLE -3 |
309 | #define PRIO_MIN -4 |
337 | #define CORO_PRIO_MIN -4 |
310 | |
338 | |
311 | /* for Coro.pm */ |
339 | /* for Coro.pm */ |
312 | static SV *coro_current; |
340 | static SV *coro_current; |
313 | static SV *coro_readyhook; |
341 | static SV *coro_readyhook; |
314 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
342 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
315 | static CV *cv_coro_run, *cv_coro_terminate; |
343 | static CV *cv_coro_run, *cv_coro_terminate; |
316 | static struct coro *coro_first; |
344 | static struct coro *coro_first; |
317 | #define coro_nready coroapi.nready |
345 | #define coro_nready coroapi.nready |
318 | |
346 | |
319 | /** lowlevel stuff **********************************************************/ |
347 | /** lowlevel stuff **********************************************************/ |
… | |
… | |
352 | INLINE CV * |
380 | INLINE CV * |
353 | coro_sv_2cv (pTHX_ SV *sv) |
381 | coro_sv_2cv (pTHX_ SV *sv) |
354 | { |
382 | { |
355 | HV *st; |
383 | HV *st; |
356 | GV *gvp; |
384 | GV *gvp; |
357 | return sv_2cv (sv, &st, &gvp, 0); |
385 | CV *cv = sv_2cv (sv, &st, &gvp, 0); |
|
|
386 | |
|
|
387 | if (!cv) |
|
|
388 | croak ("code reference expected"); |
|
|
389 | |
|
|
390 | return cv; |
358 | } |
391 | } |
|
|
392 | |
|
|
393 | INLINE void |
|
|
394 | coro_times_update () |
|
|
395 | { |
|
|
396 | #ifdef coro_clock_gettime |
|
|
397 | struct timespec ts; |
|
|
398 | |
|
|
399 | ts.tv_sec = ts.tv_nsec = 0; |
|
|
400 | coro_clock_gettime (CORO_CLOCK_THREAD_CPUTIME_ID, &ts); |
|
|
401 | time_cpu [0] = ts.tv_sec; time_cpu [1] = ts.tv_nsec; |
|
|
402 | |
|
|
403 | ts.tv_sec = ts.tv_nsec = 0; |
|
|
404 | coro_clock_gettime (CORO_CLOCK_MONOTONIC, &ts); |
|
|
405 | time_real [0] = ts.tv_sec; time_real [1] = ts.tv_nsec; |
|
|
406 | #else |
|
|
407 | UV tv[2]; |
|
|
408 | |
|
|
409 | u2time (aTHX_ &tv); |
|
|
410 | time_real [0] = tv [0]; |
|
|
411 | time_real [1] = tv [1] * 1000; |
|
|
412 | #endif |
|
|
413 | } |
|
|
414 | |
|
|
415 | INLINE void |
|
|
416 | coro_times_add (struct coro *c) |
|
|
417 | { |
|
|
418 | c->t_real [1] += time_real [1]; |
|
|
419 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
|
|
420 | c->t_real [0] += time_real [0]; |
|
|
421 | |
|
|
422 | c->t_cpu [1] += time_cpu [1]; |
|
|
423 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
|
|
424 | c->t_cpu [0] += time_cpu [0]; |
|
|
425 | } |
|
|
426 | |
|
|
427 | INLINE void |
|
|
428 | coro_times_sub (struct coro *c) |
|
|
429 | { |
|
|
430 | if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } |
|
|
431 | c->t_real [1] -= time_real [1]; |
|
|
432 | c->t_real [0] -= time_real [0]; |
|
|
433 | |
|
|
434 | if (c->t_cpu [1] < time_cpu [1]) { c->t_cpu [1] += 1000000000; --c->t_cpu [0]; } |
|
|
435 | c->t_cpu [1] -= time_cpu [1]; |
|
|
436 | c->t_cpu [0] -= time_cpu [0]; |
|
|
437 | } |
|
|
438 | |
|
|
439 | /*****************************************************************************/ |
|
|
440 | /* magic glue */ |
|
|
441 | |
|
|
442 | #define CORO_MAGIC_type_cv 26 |
|
|
443 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
444 | |
|
|
445 | #define CORO_MAGIC_NN(sv, type) \ |
|
|
446 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
|
|
447 | ? SvMAGIC (sv) \ |
|
|
448 | : mg_find (sv, type)) |
|
|
449 | |
|
|
450 | #define CORO_MAGIC(sv, type) \ |
|
|
451 | (expect_true (SvMAGIC (sv)) \ |
|
|
452 | ? CORO_MAGIC_NN (sv, type) \ |
|
|
453 | : 0) |
|
|
454 | |
|
|
455 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
456 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
|
|
457 | |
|
|
458 | INLINE struct coro * |
|
|
459 | SvSTATE_ (pTHX_ SV *coro) |
|
|
460 | { |
|
|
461 | HV *stash; |
|
|
462 | MAGIC *mg; |
|
|
463 | |
|
|
464 | if (SvROK (coro)) |
|
|
465 | coro = SvRV (coro); |
|
|
466 | |
|
|
467 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
468 | croak ("Coro::State object required"); |
|
|
469 | |
|
|
470 | stash = SvSTASH (coro); |
|
|
471 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
472 | { |
|
|
473 | /* very slow, but rare, check */ |
|
|
474 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
475 | croak ("Coro::State object required"); |
|
|
476 | } |
|
|
477 | |
|
|
478 | mg = CORO_MAGIC_state (coro); |
|
|
479 | return (struct coro *)mg->mg_ptr; |
|
|
480 | } |
|
|
481 | |
|
|
482 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
483 | |
|
|
484 | /* faster than SvSTATE, but expects a coroutine hv */ |
|
|
485 | #define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) |
|
|
486 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
|
|
487 | |
|
|
488 | /*****************************************************************************/ |
|
|
489 | /* padlist management and caching */ |
359 | |
490 | |
360 | static AV * |
491 | static AV * |
361 | coro_derive_padlist (pTHX_ CV *cv) |
492 | coro_derive_padlist (pTHX_ CV *cv) |
362 | { |
493 | { |
363 | AV *padlist = CvPADLIST (cv); |
494 | AV *padlist = CvPADLIST (cv); |
… | |
… | |
371 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
502 | Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); |
372 | #endif |
503 | #endif |
373 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
504 | newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; |
374 | --AvFILLp (padlist); |
505 | --AvFILLp (padlist); |
375 | |
506 | |
376 | av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); |
507 | av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); |
377 | av_store (newpadlist, 1, (SV *)newpad); |
508 | av_store (newpadlist, 1, (SV *)newpad); |
378 | |
509 | |
379 | return newpadlist; |
510 | return newpadlist; |
380 | } |
511 | } |
381 | |
512 | |
382 | static void |
513 | static void |
383 | free_padlist (pTHX_ AV *padlist) |
514 | free_padlist (pTHX_ AV *padlist) |
384 | { |
515 | { |
385 | /* may be during global destruction */ |
516 | /* may be during global destruction */ |
386 | if (SvREFCNT (padlist)) |
517 | if (!IN_DESTRUCT) |
387 | { |
518 | { |
388 | I32 i = AvFILLp (padlist); |
519 | I32 i = AvFILLp (padlist); |
389 | while (i >= 0) |
520 | |
|
|
521 | while (i > 0) /* special-case index 0 */ |
390 | { |
522 | { |
391 | SV **svp = av_fetch (padlist, i--, FALSE); |
523 | /* we try to be extra-careful here */ |
392 | if (svp) |
524 | AV *av = (AV *)AvARRAY (padlist)[i--]; |
393 | { |
525 | I32 j = AvFILLp (av); |
394 | SV *sv; |
526 | |
395 | while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) |
527 | while (j >= 0) |
|
|
528 | SvREFCNT_dec (AvARRAY (av)[j--]); |
|
|
529 | |
|
|
530 | AvFILLp (av) = -1; |
396 | SvREFCNT_dec (sv); |
531 | SvREFCNT_dec (av); |
397 | |
|
|
398 | SvREFCNT_dec (*svp); |
|
|
399 | } |
|
|
400 | } |
532 | } |
401 | |
533 | |
|
|
534 | SvREFCNT_dec (AvARRAY (padlist)[0]); |
|
|
535 | |
|
|
536 | AvFILLp (padlist) = -1; |
402 | SvREFCNT_dec ((SV*)padlist); |
537 | SvREFCNT_dec ((SV*)padlist); |
403 | } |
538 | } |
404 | } |
539 | } |
405 | |
540 | |
406 | static int |
541 | static int |
407 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
542 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
408 | { |
543 | { |
409 | AV *padlist; |
544 | AV *padlist; |
410 | AV *av = (AV *)mg->mg_obj; |
545 | AV *av = (AV *)mg->mg_obj; |
|
|
546 | |
|
|
547 | /* perl manages to free our internal AV and _then_ call us */ |
|
|
548 | if (IN_DESTRUCT) |
|
|
549 | return; |
411 | |
550 | |
412 | /* casting is fun. */ |
551 | /* casting is fun. */ |
413 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
552 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
414 | free_padlist (aTHX_ padlist); |
553 | free_padlist (aTHX_ padlist); |
415 | |
554 | |
416 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
555 | SvREFCNT_dec (av); /* sv_magicext increased the refcount */ |
417 | |
556 | |
418 | return 0; |
557 | return 0; |
419 | } |
558 | } |
420 | |
|
|
421 | #define CORO_MAGIC_type_cv 26 |
|
|
422 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
|
|
423 | |
559 | |
424 | static MGVTBL coro_cv_vtbl = { |
560 | static MGVTBL coro_cv_vtbl = { |
425 | 0, 0, 0, 0, |
561 | 0, 0, 0, 0, |
426 | coro_cv_free |
562 | coro_cv_free |
427 | }; |
563 | }; |
428 | |
|
|
429 | #define CORO_MAGIC_NN(sv, type) \ |
|
|
430 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
|
|
431 | ? SvMAGIC (sv) \ |
|
|
432 | : mg_find (sv, type)) |
|
|
433 | |
|
|
434 | #define CORO_MAGIC(sv, type) \ |
|
|
435 | (expect_true (SvMAGIC (sv)) \ |
|
|
436 | ? CORO_MAGIC_NN (sv, type) \ |
|
|
437 | : 0) |
|
|
438 | |
|
|
439 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
440 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
|
|
441 | |
|
|
442 | INLINE struct coro * |
|
|
443 | SvSTATE_ (pTHX_ SV *coro) |
|
|
444 | { |
|
|
445 | HV *stash; |
|
|
446 | MAGIC *mg; |
|
|
447 | |
|
|
448 | if (SvROK (coro)) |
|
|
449 | coro = SvRV (coro); |
|
|
450 | |
|
|
451 | if (expect_false (SvTYPE (coro) != SVt_PVHV)) |
|
|
452 | croak ("Coro::State object required"); |
|
|
453 | |
|
|
454 | stash = SvSTASH (coro); |
|
|
455 | if (expect_false (stash != coro_stash && stash != coro_state_stash)) |
|
|
456 | { |
|
|
457 | /* very slow, but rare, check */ |
|
|
458 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
459 | croak ("Coro::State object required"); |
|
|
460 | } |
|
|
461 | |
|
|
462 | mg = CORO_MAGIC_state (coro); |
|
|
463 | return (struct coro *)mg->mg_ptr; |
|
|
464 | } |
|
|
465 | |
|
|
466 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
467 | |
|
|
468 | /* faster than SvSTATE, but expects a coroutine hv */ |
|
|
469 | #define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr) |
|
|
470 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
|
|
471 | |
564 | |
472 | /* the next two functions merely cache the padlists */ |
565 | /* the next two functions merely cache the padlists */ |
473 | static void |
566 | static void |
474 | get_padlist (pTHX_ CV *cv) |
567 | get_padlist (pTHX_ CV *cv) |
475 | { |
568 | { |
… | |
… | |
503 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
596 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
504 | |
597 | |
505 | av = (AV *)mg->mg_obj; |
598 | av = (AV *)mg->mg_obj; |
506 | |
599 | |
507 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
600 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
508 | av_extend (av, AvMAX (av) + 1); |
601 | av_extend (av, AvFILLp (av) + 1); |
509 | |
602 | |
510 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
603 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
511 | } |
604 | } |
512 | |
605 | |
513 | /** load & save, init *******************************************************/ |
606 | /** load & save, init *******************************************************/ |
|
|
607 | |
|
|
608 | static void |
|
|
609 | on_enterleave_call (pTHX_ SV *cb); |
514 | |
610 | |
515 | static void |
611 | static void |
516 | load_perl (pTHX_ Coro__State c) |
612 | load_perl (pTHX_ Coro__State c) |
517 | { |
613 | { |
518 | perl_slots *slot = c->slot; |
614 | perl_slots *slot = c->slot; |
… | |
… | |
546 | PUTBACK; |
642 | PUTBACK; |
547 | } |
643 | } |
548 | |
644 | |
549 | slf_frame = c->slf_frame; |
645 | slf_frame = c->slf_frame; |
550 | CORO_THROW = c->except; |
646 | CORO_THROW = c->except; |
|
|
647 | |
|
|
648 | if (expect_false (enable_times)) |
|
|
649 | { |
|
|
650 | if (expect_false (!times_valid)) |
|
|
651 | coro_times_update (); |
|
|
652 | |
|
|
653 | coro_times_sub (c); |
|
|
654 | } |
|
|
655 | |
|
|
656 | if (expect_false (c->on_enter)) |
|
|
657 | { |
|
|
658 | int i; |
|
|
659 | |
|
|
660 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
|
|
661 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
|
|
662 | } |
551 | } |
663 | } |
552 | |
664 | |
553 | static void |
665 | static void |
554 | save_perl (pTHX_ Coro__State c) |
666 | save_perl (pTHX_ Coro__State c) |
555 | { |
667 | { |
|
|
668 | if (expect_false (c->on_leave)) |
|
|
669 | { |
|
|
670 | int i; |
|
|
671 | |
|
|
672 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
|
|
673 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
|
|
674 | } |
|
|
675 | |
|
|
676 | times_valid = 0; |
|
|
677 | |
|
|
678 | if (expect_false (enable_times)) |
|
|
679 | { |
|
|
680 | coro_times_update (); times_valid = 1; |
|
|
681 | coro_times_add (c); |
|
|
682 | } |
|
|
683 | |
556 | c->except = CORO_THROW; |
684 | c->except = CORO_THROW; |
557 | c->slf_frame = slf_frame; |
685 | c->slf_frame = slf_frame; |
558 | |
686 | |
559 | { |
687 | { |
560 | dSP; |
688 | dSP; |
… | |
… | |
573 | { |
701 | { |
574 | while (expect_true (cxix >= 0)) |
702 | while (expect_true (cxix >= 0)) |
575 | { |
703 | { |
576 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
704 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
577 | |
705 | |
578 | if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)) |
706 | if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT)) |
579 | { |
707 | { |
580 | CV *cv = cx->blk_sub.cv; |
708 | CV *cv = cx->blk_sub.cv; |
581 | |
709 | |
582 | if (expect_true (CvDEPTH (cv))) |
710 | if (expect_true (CvDEPTH (cv))) |
583 | { |
711 | { |
… | |
… | |
759 | #endif |
887 | #endif |
760 | |
888 | |
761 | /* |
889 | /* |
762 | * This overrides the default magic get method of %SIG elements. |
890 | * This overrides the default magic get method of %SIG elements. |
763 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
891 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
764 | * and instead of tryign to save and restore the hash elements, we just provide |
892 | * and instead of trying to save and restore the hash elements, we just provide |
765 | * readback here. |
893 | * readback here. |
766 | * We only do this when the hook is != 0, as they are often set to 0 temporarily, |
|
|
767 | * not expecting this to actually change the hook. This is a potential problem |
|
|
768 | * when a schedule happens then, but we ignore this. |
|
|
769 | */ |
894 | */ |
770 | static int |
895 | static int |
771 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
896 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
772 | { |
897 | { |
773 | const char *s = MgPV_nolen_const (mg); |
898 | const char *s = MgPV_nolen_const (mg); |
… | |
… | |
826 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
951 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
827 | |
952 | |
828 | if (svp) |
953 | if (svp) |
829 | { |
954 | { |
830 | SV *old = *svp; |
955 | SV *old = *svp; |
831 | *svp = newSVsv (sv); |
956 | *svp = SvOK (sv) ? newSVsv (sv) : 0; |
832 | SvREFCNT_dec (old); |
957 | SvREFCNT_dec (old); |
833 | return 0; |
958 | return 0; |
834 | } |
959 | } |
835 | } |
960 | } |
836 | |
961 | |
… | |
… | |
925 | |
1050 | |
926 | PL_op = (OP *)&coro_setup_op; |
1051 | PL_op = (OP *)&coro_setup_op; |
927 | |
1052 | |
928 | /* copy throw, in case it was set before coro_setup */ |
1053 | /* copy throw, in case it was set before coro_setup */ |
929 | CORO_THROW = coro->except; |
1054 | CORO_THROW = coro->except; |
930 | } |
|
|
931 | |
1055 | |
|
|
1056 | if (expect_false (enable_times)) |
|
|
1057 | { |
|
|
1058 | coro_times_update (); |
|
|
1059 | coro_times_sub (coro); |
|
|
1060 | } |
|
|
1061 | } |
|
|
1062 | |
932 | static void |
1063 | static void |
933 | coro_destruct (pTHX_ struct coro *coro) |
1064 | coro_unwind_stacks (pTHX) |
934 | { |
1065 | { |
935 | if (!IN_DESTRUCT) |
1066 | if (!IN_DESTRUCT) |
936 | { |
1067 | { |
937 | /* restore all saved variables and stuff */ |
1068 | /* restore all saved variables and stuff */ |
938 | LEAVE_SCOPE (0); |
1069 | LEAVE_SCOPE (0); |
… | |
… | |
946 | POPSTACK_TO (PL_mainstack); |
1077 | POPSTACK_TO (PL_mainstack); |
947 | |
1078 | |
948 | /* unwind main stack */ |
1079 | /* unwind main stack */ |
949 | dounwind (-1); |
1080 | dounwind (-1); |
950 | } |
1081 | } |
|
|
1082 | } |
951 | |
1083 | |
952 | SvREFCNT_dec (GvSV (PL_defgv)); |
1084 | static void |
953 | SvREFCNT_dec (GvAV (PL_defgv)); |
1085 | coro_destruct_perl (pTHX_ struct coro *coro) |
954 | SvREFCNT_dec (GvSV (PL_errgv)); |
1086 | { |
955 | SvREFCNT_dec (PL_defoutgv); |
1087 | SV *svf [9]; |
956 | SvREFCNT_dec (PL_rs); |
|
|
957 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
958 | SvREFCNT_dec (GvHV (PL_hintgv)); |
|
|
959 | |
1088 | |
960 | SvREFCNT_dec (PL_diehook); |
|
|
961 | SvREFCNT_dec (PL_warnhook); |
|
|
962 | |
1089 | { |
|
|
1090 | struct coro *current = SvSTATE_current; |
|
|
1091 | |
|
|
1092 | assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); |
|
|
1093 | |
|
|
1094 | save_perl (aTHX_ current); |
|
|
1095 | load_perl (aTHX_ coro); |
|
|
1096 | |
|
|
1097 | coro_unwind_stacks (aTHX); |
|
|
1098 | coro_destruct_stacks (aTHX); |
|
|
1099 | |
|
|
1100 | // now save some sv's to be free'd later |
|
|
1101 | svf [0] = GvSV (PL_defgv); |
|
|
1102 | svf [1] = (SV *)GvAV (PL_defgv); |
|
|
1103 | svf [2] = GvSV (PL_errgv); |
|
|
1104 | svf [3] = (SV *)PL_defoutgv; |
|
|
1105 | svf [4] = PL_rs; |
|
|
1106 | svf [5] = GvSV (irsgv); |
|
|
1107 | svf [6] = (SV *)GvHV (PL_hintgv); |
|
|
1108 | svf [7] = PL_diehook; |
|
|
1109 | svf [8] = PL_warnhook; |
|
|
1110 | assert (9 == sizeof (svf) / sizeof (*svf)); |
|
|
1111 | |
|
|
1112 | load_perl (aTHX_ current); |
|
|
1113 | } |
|
|
1114 | |
|
|
1115 | { |
|
|
1116 | int i; |
|
|
1117 | |
|
|
1118 | for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i) |
|
|
1119 | SvREFCNT_dec (svf [i]); |
|
|
1120 | |
963 | SvREFCNT_dec (coro->saved_deffh); |
1121 | SvREFCNT_dec (coro->saved_deffh); |
964 | SvREFCNT_dec (coro->rouse_cb); |
1122 | SvREFCNT_dec (coro->rouse_cb); |
965 | SvREFCNT_dec (coro->invoke_cb); |
1123 | SvREFCNT_dec (coro->invoke_cb); |
966 | SvREFCNT_dec (coro->invoke_av); |
1124 | SvREFCNT_dec (coro->invoke_av); |
967 | |
1125 | } |
968 | coro_destruct_stacks (aTHX); |
|
|
969 | } |
1126 | } |
970 | |
1127 | |
971 | INLINE void |
1128 | INLINE void |
972 | free_coro_mortal (pTHX) |
1129 | free_coro_mortal (pTHX) |
973 | { |
1130 | { |
… | |
… | |
1178 | /* |
1335 | /* |
1179 | * If perl-run returns we assume exit() was being called or the coro |
1336 | * If perl-run returns we assume exit() was being called or the coro |
1180 | * fell off the end, which seems to be the only valid (non-bug) |
1337 | * fell off the end, which seems to be the only valid (non-bug) |
1181 | * reason for perl_run to return. We try to exit by jumping to the |
1338 | * reason for perl_run to return. We try to exit by jumping to the |
1182 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1339 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1183 | * coroutine as Coro has no such concept |
1340 | * coroutine as Coro has no such concept. |
|
|
1341 | * This actually isn't valid with the pthread backend, but OSes requiring |
|
|
1342 | * that backend are too broken to do it in a standards-compliant way. |
1184 | */ |
1343 | */ |
1185 | PL_top_env = main_top_env; |
1344 | PL_top_env = main_top_env; |
1186 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1345 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1187 | } |
1346 | } |
1188 | } |
1347 | } |
… | |
… | |
1265 | cctx_destroy (coro_cctx *cctx) |
1424 | cctx_destroy (coro_cctx *cctx) |
1266 | { |
1425 | { |
1267 | if (!cctx) |
1426 | if (!cctx) |
1268 | return; |
1427 | return; |
1269 | |
1428 | |
1270 | assert (cctx != cctx_current);//D temporary |
1429 | assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary? |
1271 | |
1430 | |
1272 | --cctx_count; |
1431 | --cctx_count; |
1273 | coro_destroy (&cctx->cctx); |
1432 | coro_destroy (&cctx->cctx); |
1274 | |
1433 | |
1275 | /* coro_transfer creates new, empty cctx's */ |
1434 | /* coro_transfer creates new, empty cctx's */ |
… | |
… | |
1339 | /* TODO: throwing up here is considered harmful */ |
1498 | /* TODO: throwing up here is considered harmful */ |
1340 | |
1499 | |
1341 | if (expect_true (prev != next)) |
1500 | if (expect_true (prev != next)) |
1342 | { |
1501 | { |
1343 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1502 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1344 | croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); |
1503 | croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); |
1345 | |
1504 | |
1346 | if (expect_false (next->flags & CF_RUNNING)) |
|
|
1347 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
|
|
1348 | |
|
|
1349 | if (expect_false (next->flags & CF_DESTROYED)) |
1505 | if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED))) |
1350 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); |
1506 | croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); |
1351 | |
1507 | |
1352 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1508 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1353 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1509 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1354 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1510 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1355 | #endif |
1511 | #endif |
… | |
… | |
1393 | coro_setup (aTHX_ next); |
1549 | coro_setup (aTHX_ next); |
1394 | } |
1550 | } |
1395 | else |
1551 | else |
1396 | load_perl (aTHX_ next); |
1552 | load_perl (aTHX_ next); |
1397 | |
1553 | |
1398 | assert (!prev->cctx);//D temporary |
|
|
1399 | |
|
|
1400 | /* possibly untie and reuse the cctx */ |
1554 | /* possibly untie and reuse the cctx */ |
1401 | if (expect_true ( |
1555 | if (expect_true ( |
1402 | cctx_current->idle_sp == STACKLEVEL |
1556 | cctx_current->idle_sp == STACKLEVEL |
1403 | && !(cctx_current->flags & CC_TRACE) |
1557 | && !(cctx_current->flags & CC_TRACE) |
1404 | && !force_cctx |
1558 | && !force_cctx |
… | |
… | |
1445 | coro_state_destroy (pTHX_ struct coro *coro) |
1599 | coro_state_destroy (pTHX_ struct coro *coro) |
1446 | { |
1600 | { |
1447 | if (coro->flags & CF_DESTROYED) |
1601 | if (coro->flags & CF_DESTROYED) |
1448 | return 0; |
1602 | return 0; |
1449 | |
1603 | |
1450 | if (coro->on_destroy) |
1604 | if (coro->on_destroy && !PL_dirty) |
1451 | coro->on_destroy (aTHX_ coro); |
1605 | coro->on_destroy (aTHX_ coro); |
1452 | |
1606 | |
1453 | coro->flags |= CF_DESTROYED; |
1607 | coro->flags |= CF_DESTROYED; |
1454 | |
1608 | |
1455 | if (coro->flags & CF_READY) |
1609 | if (coro->flags & CF_READY) |
… | |
… | |
1459 | --coro_nready; |
1613 | --coro_nready; |
1460 | } |
1614 | } |
1461 | else |
1615 | else |
1462 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1616 | coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ |
1463 | |
1617 | |
1464 | if (coro->mainstack && coro->mainstack != main_mainstack) |
1618 | if (coro->mainstack |
1465 | { |
1619 | && coro->mainstack != main_mainstack |
1466 | struct coro temp; |
1620 | && coro->slot |
1467 | |
1621 | && !PL_dirty) |
1468 | assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); |
|
|
1469 | |
|
|
1470 | save_perl (aTHX_ &temp); |
|
|
1471 | load_perl (aTHX_ coro); |
|
|
1472 | |
|
|
1473 | coro_destruct (aTHX_ coro); |
1622 | coro_destruct_perl (aTHX_ coro); |
1474 | |
|
|
1475 | load_perl (aTHX_ &temp); |
|
|
1476 | |
|
|
1477 | coro->slot = 0; |
|
|
1478 | } |
|
|
1479 | |
1623 | |
1480 | cctx_destroy (coro->cctx); |
1624 | cctx_destroy (coro->cctx); |
1481 | SvREFCNT_dec (coro->startcv); |
1625 | SvREFCNT_dec (coro->startcv); |
1482 | SvREFCNT_dec (coro->args); |
1626 | SvREFCNT_dec (coro->args); |
1483 | SvREFCNT_dec (CORO_THROW); |
1627 | SvREFCNT_dec (CORO_THROW); |
… | |
… | |
1570 | /** Coro ********************************************************************/ |
1714 | /** Coro ********************************************************************/ |
1571 | |
1715 | |
1572 | INLINE void |
1716 | INLINE void |
1573 | coro_enq (pTHX_ struct coro *coro) |
1717 | coro_enq (pTHX_ struct coro *coro) |
1574 | { |
1718 | { |
1575 | av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); |
1719 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1576 | } |
|
|
1577 | |
1720 | |
1578 | INLINE SV * |
1721 | SvREFCNT_inc_NN (coro->hv); |
|
|
1722 | |
|
|
1723 | coro->next_ready = 0; |
|
|
1724 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
|
|
1725 | ready [1] = coro; |
|
|
1726 | } |
|
|
1727 | |
|
|
1728 | INLINE struct coro * |
1579 | coro_deq (pTHX) |
1729 | coro_deq (pTHX) |
1580 | { |
1730 | { |
1581 | int prio; |
1731 | int prio; |
1582 | |
1732 | |
1583 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1733 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
1584 | if (AvFILLp (coro_ready [prio]) >= 0) |
1734 | { |
1585 | return av_shift (coro_ready [prio]); |
1735 | struct coro **ready = coro_ready [prio]; |
|
|
1736 | |
|
|
1737 | if (ready [0]) |
|
|
1738 | { |
|
|
1739 | struct coro *coro = ready [0]; |
|
|
1740 | ready [0] = coro->next_ready; |
|
|
1741 | return coro; |
|
|
1742 | } |
|
|
1743 | } |
1586 | |
1744 | |
1587 | return 0; |
1745 | return 0; |
1588 | } |
1746 | } |
1589 | |
1747 | |
1590 | static int |
1748 | static int |
… | |
… | |
1654 | static void |
1812 | static void |
1655 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1813 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1656 | { |
1814 | { |
1657 | for (;;) |
1815 | for (;;) |
1658 | { |
1816 | { |
1659 | SV *next_sv = coro_deq (aTHX); |
1817 | struct coro *next = coro_deq (aTHX); |
1660 | |
1818 | |
1661 | if (expect_true (next_sv)) |
1819 | if (expect_true (next)) |
1662 | { |
1820 | { |
1663 | struct coro *next = SvSTATE_hv (next_sv); |
|
|
1664 | |
|
|
1665 | /* cannot transfer to destroyed coros, skip and look for next */ |
1821 | /* cannot transfer to destroyed coros, skip and look for next */ |
1666 | if (expect_false (next->flags & CF_DESTROYED)) |
1822 | if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED))) |
1667 | SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ |
1823 | SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ |
1668 | else |
1824 | else |
1669 | { |
1825 | { |
1670 | next->flags &= ~CF_READY; |
1826 | next->flags &= ~CF_READY; |
1671 | --coro_nready; |
1827 | --coro_nready; |
1672 | |
1828 | |
… | |
… | |
1850 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
2006 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1851 | api_ready (aTHX_ sv_manager); |
2007 | api_ready (aTHX_ sv_manager); |
1852 | |
2008 | |
1853 | frame->prepare = prepare_schedule; |
2009 | frame->prepare = prepare_schedule; |
1854 | frame->check = slf_check_repeat; |
2010 | frame->check = slf_check_repeat; |
|
|
2011 | |
|
|
2012 | /* as a minor optimisation, we could unwind all stacks here */ |
|
|
2013 | /* but that puts extra pressure on pp_slf, and is not worth much */ |
|
|
2014 | /*coro_unwind_stacks (aTHX);*/ |
1855 | } |
2015 | } |
1856 | |
2016 | |
1857 | /*****************************************************************************/ |
2017 | /*****************************************************************************/ |
1858 | /* async pool handler */ |
2018 | /* async pool handler */ |
1859 | |
2019 | |
… | |
… | |
1942 | SV *data = (SV *)GENSUB_ARG; |
2102 | SV *data = (SV *)GENSUB_ARG; |
1943 | |
2103 | |
1944 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2104 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
1945 | { |
2105 | { |
1946 | /* first call, set args */ |
2106 | /* first call, set args */ |
|
|
2107 | SV *coro = SvRV (data); |
1947 | AV *av = newAV (); |
2108 | AV *av = newAV (); |
1948 | SV *coro = SvRV (data); |
|
|
1949 | |
2109 | |
1950 | SvRV_set (data, (SV *)av); |
2110 | SvRV_set (data, (SV *)av); |
1951 | api_ready (aTHX_ coro); |
|
|
1952 | SvREFCNT_dec (coro); |
|
|
1953 | |
2111 | |
1954 | /* better take a full copy of the arguments */ |
2112 | /* better take a full copy of the arguments */ |
1955 | while (items--) |
2113 | while (items--) |
1956 | av_store (av, items, newSVsv (ST (items))); |
2114 | av_store (av, items, newSVsv (ST (items))); |
|
|
2115 | |
|
|
2116 | api_ready (aTHX_ coro); |
|
|
2117 | SvREFCNT_dec (coro); |
1957 | } |
2118 | } |
1958 | |
2119 | |
1959 | XSRETURN_EMPTY; |
2120 | XSRETURN_EMPTY; |
1960 | } |
2121 | } |
1961 | |
2122 | |
… | |
… | |
1978 | |
2139 | |
1979 | EXTEND (SP, AvFILLp (av) + 1); |
2140 | EXTEND (SP, AvFILLp (av) + 1); |
1980 | for (i = 0; i <= AvFILLp (av); ++i) |
2141 | for (i = 0; i <= AvFILLp (av); ++i) |
1981 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
2142 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
1982 | |
2143 | |
1983 | /* we have stolen the elements, so ste length to zero and free */ |
2144 | /* we have stolen the elements, so set length to zero and free */ |
1984 | AvFILLp (av) = -1; |
2145 | AvFILLp (av) = -1; |
1985 | av_undef (av); |
2146 | av_undef (av); |
1986 | |
2147 | |
1987 | PUTBACK; |
2148 | PUTBACK; |
1988 | } |
2149 | } |
… | |
… | |
2278 | |
2439 | |
2279 | PL_op->op_ppaddr = pp_slf; |
2440 | PL_op->op_ppaddr = pp_slf; |
2280 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2441 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2281 | |
2442 | |
2282 | PL_op = (OP *)&slf_restore; |
2443 | PL_op = (OP *)&slf_restore; |
|
|
2444 | } |
|
|
2445 | |
|
|
2446 | /*****************************************************************************/ |
|
|
2447 | /* dynamic wind */ |
|
|
2448 | |
|
|
2449 | static void |
|
|
2450 | on_enterleave_call (pTHX_ SV *cb) |
|
|
2451 | { |
|
|
2452 | dSP; |
|
|
2453 | |
|
|
2454 | PUSHSTACK; |
|
|
2455 | |
|
|
2456 | PUSHMARK (SP); |
|
|
2457 | PUTBACK; |
|
|
2458 | call_sv (cb, G_VOID | G_DISCARD); |
|
|
2459 | SPAGAIN; |
|
|
2460 | |
|
|
2461 | POPSTACK; |
|
|
2462 | } |
|
|
2463 | |
|
|
2464 | static SV * |
|
|
2465 | coro_avp_pop_and_free (pTHX_ AV **avp) |
|
|
2466 | { |
|
|
2467 | AV *av = *avp; |
|
|
2468 | SV *res = av_pop (av); |
|
|
2469 | |
|
|
2470 | if (AvFILLp (av) < 0) |
|
|
2471 | { |
|
|
2472 | *avp = 0; |
|
|
2473 | SvREFCNT_dec (av); |
|
|
2474 | } |
|
|
2475 | |
|
|
2476 | return res; |
|
|
2477 | } |
|
|
2478 | |
|
|
2479 | static void |
|
|
2480 | coro_pop_on_enter (pTHX_ void *coro) |
|
|
2481 | { |
|
|
2482 | SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter); |
|
|
2483 | SvREFCNT_dec (cb); |
|
|
2484 | } |
|
|
2485 | |
|
|
2486 | static void |
|
|
2487 | coro_pop_on_leave (pTHX_ void *coro) |
|
|
2488 | { |
|
|
2489 | SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave); |
|
|
2490 | on_enterleave_call (aTHX_ sv_2mortal (cb)); |
2283 | } |
2491 | } |
2284 | |
2492 | |
2285 | /*****************************************************************************/ |
2493 | /*****************************************************************************/ |
2286 | /* PerlIO::cede */ |
2494 | /* PerlIO::cede */ |
2287 | |
2495 | |
… | |
… | |
2361 | /* Coro::Semaphore & Coro::Signal */ |
2569 | /* Coro::Semaphore & Coro::Signal */ |
2362 | |
2570 | |
2363 | static SV * |
2571 | static SV * |
2364 | coro_waitarray_new (pTHX_ int count) |
2572 | coro_waitarray_new (pTHX_ int count) |
2365 | { |
2573 | { |
2366 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
2574 | /* a waitarray=semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
2367 | AV *av = newAV (); |
2575 | AV *av = newAV (); |
2368 | SV **ary; |
2576 | SV **ary; |
2369 | |
2577 | |
2370 | /* unfortunately, building manually saves memory */ |
2578 | /* unfortunately, building manually saves memory */ |
2371 | Newx (ary, 2, SV *); |
2579 | Newx (ary, 2, SV *); |
2372 | AvALLOC (av) = ary; |
2580 | AvALLOC (av) = ary; |
|
|
2581 | #if PERL_VERSION_ATLEAST (5,10,0) |
2373 | /*AvARRAY (av) = ary;*/ |
2582 | AvARRAY (av) = ary; |
|
|
2583 | #else |
2374 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
2584 | /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */ |
|
|
2585 | /* -DDEBUGGING flags this as a bug, despite it perfectly working */ |
|
|
2586 | SvPVX ((SV *)av) = (char *)ary; |
|
|
2587 | #endif |
2375 | AvMAX (av) = 1; |
2588 | AvMAX (av) = 1; |
2376 | AvFILLp (av) = 0; |
2589 | AvFILLp (av) = 0; |
2377 | ary [0] = newSViv (count); |
2590 | ary [0] = newSViv (count); |
2378 | |
2591 | |
2379 | return newRV_noinc ((SV *)av); |
2592 | return newRV_noinc ((SV *)av); |
… | |
… | |
2510 | { |
2723 | { |
2511 | /* callback form */ |
2724 | /* callback form */ |
2512 | AV *av = (AV *)SvRV (arg [0]); |
2725 | AV *av = (AV *)SvRV (arg [0]); |
2513 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
2726 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
2514 | |
2727 | |
2515 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
2728 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2516 | |
2729 | |
2517 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2730 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2518 | coro_semaphore_adjust (aTHX_ av, 0); |
2731 | coro_semaphore_adjust (aTHX_ av, 0); |
2519 | |
2732 | |
2520 | frame->prepare = prepare_nop; |
2733 | frame->prepare = prepare_nop; |
… | |
… | |
2544 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2757 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2545 | AvARRAY (av)[1] = cb; |
2758 | AvARRAY (av)[1] = cb; |
2546 | |
2759 | |
2547 | cb = av_shift (av); |
2760 | cb = av_shift (av); |
2548 | |
2761 | |
|
|
2762 | if (SvTYPE (cb) == SVt_PVCV) |
|
|
2763 | { |
|
|
2764 | dSP; |
|
|
2765 | PUSHMARK (SP); |
|
|
2766 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
2767 | PUTBACK; |
|
|
2768 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
|
|
2769 | } |
|
|
2770 | else |
|
|
2771 | { |
2549 | api_ready (aTHX_ cb); |
2772 | api_ready (aTHX_ cb); |
2550 | sv_setiv (cb, 0); /* signal waiter */ |
2773 | sv_setiv (cb, 0); /* signal waiter */ |
|
|
2774 | } |
|
|
2775 | |
2551 | SvREFCNT_dec (cb); |
2776 | SvREFCNT_dec (cb); |
2552 | |
2777 | |
2553 | --count; |
2778 | --count; |
2554 | } |
2779 | } |
2555 | } |
2780 | } |
… | |
… | |
2564 | static void |
2789 | static void |
2565 | slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2790 | slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2566 | { |
2791 | { |
2567 | AV *av = (AV *)SvRV (arg [0]); |
2792 | AV *av = (AV *)SvRV (arg [0]); |
2568 | |
2793 | |
|
|
2794 | if (items >= 2) |
|
|
2795 | { |
|
|
2796 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
|
|
2797 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
|
|
2798 | |
2569 | if (SvIVX (AvARRAY (av)[0])) |
2799 | if (SvIVX (AvARRAY (av)[0])) |
|
|
2800 | coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ |
|
|
2801 | |
|
|
2802 | frame->prepare = prepare_nop; |
|
|
2803 | frame->check = slf_check_nop; |
|
|
2804 | } |
|
|
2805 | else if (SvIVX (AvARRAY (av)[0])) |
2570 | { |
2806 | { |
2571 | SvIVX (AvARRAY (av)[0]) = 0; |
2807 | SvIVX (AvARRAY (av)[0]) = 0; |
2572 | frame->prepare = prepare_nop; |
2808 | frame->prepare = prepare_nop; |
2573 | frame->check = slf_check_nop; |
2809 | frame->check = slf_check_nop; |
2574 | } |
2810 | } |
2575 | else |
2811 | else |
2576 | { |
2812 | { |
2577 | SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ |
2813 | SV *waiter = newSVsv (coro_current); /* owned by signal av */ |
2578 | |
2814 | |
2579 | av_push (av, waiter); |
2815 | av_push (av, waiter); |
2580 | |
2816 | |
2581 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
2817 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
2582 | frame->prepare = prepare_schedule; |
2818 | frame->prepare = prepare_schedule; |
… | |
… | |
2821 | |
3057 | |
2822 | if (!svp) croak ("Time::HiRes is required"); |
3058 | if (!svp) croak ("Time::HiRes is required"); |
2823 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
3059 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
2824 | |
3060 | |
2825 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
3061 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
3062 | |
|
|
3063 | svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); |
|
|
3064 | u2time = INT2PTR (double (*)(), SvIV (*svp)); |
2826 | } |
3065 | } |
2827 | |
3066 | |
2828 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
3067 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
2829 | } |
3068 | } |
2830 | |
3069 | |
2831 | SV * |
3070 | SV * |
2832 | new (char *klass, ...) |
3071 | new (char *klass, ...) |
2833 | ALIAS: |
3072 | ALIAS: |
… | |
… | |
2908 | SV * |
3147 | SV * |
2909 | clone (Coro::State coro) |
3148 | clone (Coro::State coro) |
2910 | CODE: |
3149 | CODE: |
2911 | { |
3150 | { |
2912 | #if CORO_CLONE |
3151 | #if CORO_CLONE |
2913 | struct coro *ncoro = coro_clone (coro); |
3152 | struct coro *ncoro = coro_clone (aTHX_ coro); |
2914 | MAGIC *mg; |
3153 | MAGIC *mg; |
2915 | /* TODO: too much duplication */ |
3154 | /* TODO: too much duplication */ |
2916 | ncoro->hv = newHV (); |
3155 | ncoro->hv = newHV (); |
2917 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
3156 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
2918 | mg->mg_flags |= MGf_DUP; |
3157 | mg->mg_flags |= MGf_DUP; |
… | |
… | |
2980 | eval = 1 |
3219 | eval = 1 |
2981 | CODE: |
3220 | CODE: |
2982 | { |
3221 | { |
2983 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
3222 | if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) |
2984 | { |
3223 | { |
2985 | struct coro temp; |
3224 | struct coro *current = SvSTATE_current; |
2986 | |
3225 | |
2987 | if (!(coro->flags & CF_RUNNING)) |
3226 | if (current != coro) |
2988 | { |
3227 | { |
2989 | PUTBACK; |
3228 | PUTBACK; |
2990 | save_perl (aTHX_ &temp); |
3229 | save_perl (aTHX_ current); |
2991 | load_perl (aTHX_ coro); |
3230 | load_perl (aTHX_ coro); |
|
|
3231 | SPAGAIN; |
2992 | } |
3232 | } |
2993 | |
3233 | |
2994 | { |
|
|
2995 | dSP; |
|
|
2996 | ENTER; |
|
|
2997 | SAVETMPS; |
|
|
2998 | PUTBACK; |
|
|
2999 | PUSHSTACK; |
3234 | PUSHSTACK; |
|
|
3235 | |
3000 | PUSHMARK (SP); |
3236 | PUSHMARK (SP); |
|
|
3237 | PUTBACK; |
3001 | |
3238 | |
3002 | if (ix) |
3239 | if (ix) |
3003 | eval_sv (coderef, 0); |
3240 | eval_sv (coderef, 0); |
3004 | else |
3241 | else |
3005 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
3242 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
3006 | |
3243 | |
3007 | POPSTACK; |
3244 | POPSTACK; |
3008 | SPAGAIN; |
3245 | SPAGAIN; |
3009 | FREETMPS; |
|
|
3010 | LEAVE; |
|
|
3011 | PUTBACK; |
|
|
3012 | } |
|
|
3013 | |
3246 | |
3014 | if (!(coro->flags & CF_RUNNING)) |
3247 | if (current != coro) |
3015 | { |
3248 | { |
|
|
3249 | PUTBACK; |
3016 | save_perl (aTHX_ coro); |
3250 | save_perl (aTHX_ coro); |
3017 | load_perl (aTHX_ &temp); |
3251 | load_perl (aTHX_ current); |
3018 | SPAGAIN; |
3252 | SPAGAIN; |
3019 | } |
3253 | } |
3020 | } |
3254 | } |
3021 | } |
3255 | } |
3022 | |
3256 | |
… | |
… | |
3026 | ALIAS: |
3260 | ALIAS: |
3027 | is_ready = CF_READY |
3261 | is_ready = CF_READY |
3028 | is_running = CF_RUNNING |
3262 | is_running = CF_RUNNING |
3029 | is_new = CF_NEW |
3263 | is_new = CF_NEW |
3030 | is_destroyed = CF_DESTROYED |
3264 | is_destroyed = CF_DESTROYED |
|
|
3265 | is_suspended = CF_SUSPENDED |
3031 | CODE: |
3266 | CODE: |
3032 | RETVAL = boolSV (coro->flags & ix); |
3267 | RETVAL = boolSV (coro->flags & ix); |
3033 | OUTPUT: |
3268 | OUTPUT: |
3034 | RETVAL |
3269 | RETVAL |
3035 | |
3270 | |
… | |
… | |
3039 | CODE: |
3274 | CODE: |
3040 | { |
3275 | { |
3041 | struct coro *current = SvSTATE_current; |
3276 | struct coro *current = SvSTATE_current; |
3042 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3277 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3043 | SvREFCNT_dec (*throwp); |
3278 | SvREFCNT_dec (*throwp); |
|
|
3279 | SvGETMAGIC (throw); |
3044 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3280 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3045 | } |
3281 | } |
3046 | |
3282 | |
3047 | void |
3283 | void |
3048 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3284 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
… | |
… | |
3100 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
3336 | SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; |
3101 | |
3337 | |
3102 | SV *tmp = *src; *src = *dst; *dst = tmp; |
3338 | SV *tmp = *src; *src = *dst; *dst = tmp; |
3103 | } |
3339 | } |
3104 | |
3340 | |
|
|
3341 | void |
|
|
3342 | cancel (Coro::State self) |
|
|
3343 | CODE: |
|
|
3344 | coro_state_destroy (aTHX_ self); |
|
|
3345 | coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ |
|
|
3346 | |
|
|
3347 | |
|
|
3348 | SV * |
|
|
3349 | enable_times (int enabled = enable_times) |
|
|
3350 | CODE: |
|
|
3351 | { |
|
|
3352 | RETVAL = boolSV (enable_times); |
|
|
3353 | |
|
|
3354 | if (enabled != enable_times) |
|
|
3355 | { |
|
|
3356 | enable_times = enabled; |
|
|
3357 | |
|
|
3358 | coro_times_update (); |
|
|
3359 | (enabled ? coro_times_sub : coro_times_add)(SvSTATE (coro_current)); |
|
|
3360 | } |
|
|
3361 | } |
|
|
3362 | OUTPUT: |
|
|
3363 | RETVAL |
|
|
3364 | |
|
|
3365 | void |
|
|
3366 | times (Coro::State self) |
|
|
3367 | PPCODE: |
|
|
3368 | { |
|
|
3369 | struct coro *current = SvSTATE (coro_current); |
|
|
3370 | |
|
|
3371 | if (expect_false (current == self)) |
|
|
3372 | { |
|
|
3373 | coro_times_update (); |
|
|
3374 | coro_times_add (SvSTATE (coro_current)); |
|
|
3375 | } |
|
|
3376 | |
|
|
3377 | EXTEND (SP, 2); |
|
|
3378 | PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); |
|
|
3379 | PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); |
|
|
3380 | |
|
|
3381 | if (expect_false (current == self)) |
|
|
3382 | coro_times_sub (SvSTATE (coro_current)); |
|
|
3383 | } |
3105 | |
3384 | |
3106 | MODULE = Coro::State PACKAGE = Coro |
3385 | MODULE = Coro::State PACKAGE = Coro |
3107 | |
3386 | |
3108 | BOOT: |
3387 | BOOT: |
3109 | { |
3388 | { |
3110 | int i; |
|
|
3111 | |
|
|
3112 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3389 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3113 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3390 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3114 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
3391 | cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); |
3115 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3392 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3116 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3393 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3117 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3394 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3118 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3395 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3119 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
3396 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
… | |
… | |
3124 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3401 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3125 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3402 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3126 | |
3403 | |
3127 | coro_stash = gv_stashpv ("Coro", TRUE); |
3404 | coro_stash = gv_stashpv ("Coro", TRUE); |
3128 | |
3405 | |
3129 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
3406 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3130 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
3407 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3131 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
3408 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
3132 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
3409 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (CORO_PRIO_LOW)); |
3133 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
3410 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (CORO_PRIO_IDLE)); |
3134 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
3411 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (CORO_PRIO_MIN)); |
3135 | |
|
|
3136 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
|
|
3137 | coro_ready[i] = newAV (); |
|
|
3138 | |
3412 | |
3139 | { |
3413 | { |
3140 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3414 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3141 | |
3415 | |
3142 | coroapi.schedule = api_schedule; |
3416 | coroapi.schedule = api_schedule; |
… | |
… | |
3183 | cede_notself (...) |
3457 | cede_notself (...) |
3184 | CODE: |
3458 | CODE: |
3185 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3459 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3186 | |
3460 | |
3187 | void |
3461 | void |
3188 | _cancel (Coro::State self) |
|
|
3189 | CODE: |
|
|
3190 | coro_state_destroy (aTHX_ self); |
|
|
3191 | coro_call_on_destroy (aTHX_ self); |
|
|
3192 | |
|
|
3193 | void |
|
|
3194 | _set_current (SV *current) |
3462 | _set_current (SV *current) |
3195 | PROTOTYPE: $ |
3463 | PROTOTYPE: $ |
3196 | CODE: |
3464 | CODE: |
3197 | SvREFCNT_dec (SvRV (coro_current)); |
3465 | SvREFCNT_dec (SvRV (coro_current)); |
3198 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
3466 | SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); |
… | |
… | |
3200 | void |
3468 | void |
3201 | _set_readyhook (SV *hook) |
3469 | _set_readyhook (SV *hook) |
3202 | PROTOTYPE: $ |
3470 | PROTOTYPE: $ |
3203 | CODE: |
3471 | CODE: |
3204 | SvREFCNT_dec (coro_readyhook); |
3472 | SvREFCNT_dec (coro_readyhook); |
|
|
3473 | SvGETMAGIC (hook); |
3205 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
3474 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
3206 | |
3475 | |
3207 | int |
3476 | int |
3208 | prio (Coro::State coro, int newprio = 0) |
3477 | prio (Coro::State coro, int newprio = 0) |
3209 | PROTOTYPE: $;$ |
3478 | PROTOTYPE: $;$ |
… | |
… | |
3216 | if (items > 1) |
3485 | if (items > 1) |
3217 | { |
3486 | { |
3218 | if (ix) |
3487 | if (ix) |
3219 | newprio = coro->prio - newprio; |
3488 | newprio = coro->prio - newprio; |
3220 | |
3489 | |
3221 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
3490 | if (newprio < CORO_PRIO_MIN) newprio = CORO_PRIO_MIN; |
3222 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
3491 | if (newprio > CORO_PRIO_MAX) newprio = CORO_PRIO_MAX; |
3223 | |
3492 | |
3224 | coro->prio = newprio; |
3493 | coro->prio = newprio; |
3225 | } |
3494 | } |
3226 | } |
3495 | } |
3227 | OUTPUT: |
3496 | OUTPUT: |
… | |
… | |
3240 | PROTOTYPE: |
3509 | PROTOTYPE: |
3241 | CODE: |
3510 | CODE: |
3242 | RETVAL = coro_nready; |
3511 | RETVAL = coro_nready; |
3243 | OUTPUT: |
3512 | OUTPUT: |
3244 | RETVAL |
3513 | RETVAL |
|
|
3514 | |
|
|
3515 | void |
|
|
3516 | suspend (Coro::State self) |
|
|
3517 | PROTOTYPE: $ |
|
|
3518 | CODE: |
|
|
3519 | self->flags |= CF_SUSPENDED; |
|
|
3520 | |
|
|
3521 | void |
|
|
3522 | resume (Coro::State self) |
|
|
3523 | PROTOTYPE: $ |
|
|
3524 | CODE: |
|
|
3525 | self->flags &= ~CF_SUSPENDED; |
3245 | |
3526 | |
3246 | void |
3527 | void |
3247 | _pool_handler (...) |
3528 | _pool_handler (...) |
3248 | CODE: |
3529 | CODE: |
3249 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
3530 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
… | |
… | |
3304 | rouse_wait (...) |
3585 | rouse_wait (...) |
3305 | PROTOTYPE: ;$ |
3586 | PROTOTYPE: ;$ |
3306 | PPCODE: |
3587 | PPCODE: |
3307 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3588 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3308 | |
3589 | |
|
|
3590 | void |
|
|
3591 | on_enter (SV *block) |
|
|
3592 | ALIAS: |
|
|
3593 | on_leave = 1 |
|
|
3594 | PROTOTYPE: & |
|
|
3595 | CODE: |
|
|
3596 | { |
|
|
3597 | struct coro *coro = SvSTATE_current; |
|
|
3598 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
|
|
3599 | |
|
|
3600 | block = (SV *)coro_sv_2cv (aTHX_ block); |
|
|
3601 | |
|
|
3602 | if (!*avp) |
|
|
3603 | *avp = newAV (); |
|
|
3604 | |
|
|
3605 | av_push (*avp, SvREFCNT_inc (block)); |
|
|
3606 | |
|
|
3607 | if (!ix) |
|
|
3608 | on_enterleave_call (aTHX_ block); |
|
|
3609 | |
|
|
3610 | LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */ |
|
|
3611 | SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro); |
|
|
3612 | ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */ |
|
|
3613 | } |
|
|
3614 | |
3309 | |
3615 | |
3310 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3616 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3311 | |
3617 | |
3312 | BOOT: |
3618 | BOOT: |
3313 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
3619 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
… | |
… | |
3316 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
3622 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
3317 | |
3623 | |
3318 | SV * |
3624 | SV * |
3319 | new (SV *klass, SV *count = 0) |
3625 | new (SV *klass, SV *count = 0) |
3320 | CODE: |
3626 | CODE: |
|
|
3627 | { |
|
|
3628 | int semcnt = 1; |
|
|
3629 | |
|
|
3630 | if (count) |
|
|
3631 | { |
|
|
3632 | SvGETMAGIC (count); |
|
|
3633 | |
|
|
3634 | if (SvOK (count)) |
|
|
3635 | semcnt = SvIV (count); |
|
|
3636 | } |
|
|
3637 | |
3321 | RETVAL = sv_bless ( |
3638 | RETVAL = sv_bless ( |
3322 | coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), |
3639 | coro_waitarray_new (aTHX_ semcnt), |
3323 | GvSTASH (CvGV (cv)) |
3640 | GvSTASH (CvGV (cv)) |
3324 | ); |
3641 | ); |
|
|
3642 | } |
3325 | OUTPUT: |
3643 | OUTPUT: |
3326 | RETVAL |
3644 | RETVAL |
3327 | |
3645 | |
3328 | # helper for Coro::Channel |
3646 | # helper for Coro::Channel and others |
3329 | SV * |
3647 | SV * |
3330 | _alloc (int count) |
3648 | _alloc (int count) |
3331 | CODE: |
3649 | CODE: |
3332 | RETVAL = coro_waitarray_new (aTHX_ count); |
3650 | RETVAL = coro_waitarray_new (aTHX_ count); |
3333 | OUTPUT: |
3651 | OUTPUT: |
… | |
… | |
3391 | for (i = 1; i <= wcount; ++i) |
3709 | for (i = 1; i <= wcount; ++i) |
3392 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
3710 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
3393 | } |
3711 | } |
3394 | } |
3712 | } |
3395 | |
3713 | |
|
|
3714 | MODULE = Coro::State PACKAGE = Coro::SemaphoreSet |
|
|
3715 | |
|
|
3716 | void |
|
|
3717 | _may_delete (SV *sem, int count, int extra_refs) |
|
|
3718 | PPCODE: |
|
|
3719 | { |
|
|
3720 | AV *av = (AV *)SvRV (sem); |
|
|
3721 | |
|
|
3722 | if (SvREFCNT ((SV *)av) == 1 + extra_refs |
|
|
3723 | && AvFILLp (av) == 0 /* no waiters, just count */ |
|
|
3724 | && SvIV (AvARRAY (av)[0]) == count) |
|
|
3725 | XSRETURN_YES; |
|
|
3726 | |
|
|
3727 | XSRETURN_NO; |
|
|
3728 | } |
|
|
3729 | |
3396 | MODULE = Coro::State PACKAGE = Coro::Signal |
3730 | MODULE = Coro::State PACKAGE = Coro::Signal |
3397 | |
3731 | |
3398 | SV * |
3732 | SV * |
3399 | new (SV *klass) |
3733 | new (SV *klass) |
3400 | CODE: |
3734 | CODE: |