… | |
… | |
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 | |
… | |
… | |
194 | static CV *cv_pool_handler; |
205 | static CV *cv_pool_handler; |
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; |
|
|
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; |
199 | |
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 { |
… | |
… | |
293 | |
310 | |
294 | /* on_enter/on_leave */ |
311 | /* on_enter/on_leave */ |
295 | AV *on_enter; |
312 | AV *on_enter; |
296 | AV *on_leave; |
313 | AV *on_leave; |
297 | |
314 | |
|
|
315 | /* times */ |
|
|
316 | coro_ts t_cpu, t_real; |
|
|
317 | |
298 | /* linked list */ |
318 | /* linked list */ |
299 | struct coro *next, *prev; |
319 | struct coro *next, *prev; |
300 | }; |
320 | }; |
301 | |
321 | |
302 | typedef struct coro *Coro__State; |
322 | typedef struct coro *Coro__State; |
… | |
… | |
307 | /* the mainr easonw e don't support windows process emulation */ |
327 | /* the mainr easonw e don't support windows process emulation */ |
308 | static struct CoroSLF slf_frame; /* the current slf frame */ |
328 | static struct CoroSLF slf_frame; /* the current slf frame */ |
309 | |
329 | |
310 | /** Coro ********************************************************************/ |
330 | /** Coro ********************************************************************/ |
311 | |
331 | |
312 | #define PRIO_MAX 3 |
332 | #define CORO_PRIO_MAX 3 |
313 | #define PRIO_HIGH 1 |
333 | #define CORO_PRIO_HIGH 1 |
314 | #define PRIO_NORMAL 0 |
334 | #define CORO_PRIO_NORMAL 0 |
315 | #define PRIO_LOW -1 |
335 | #define CORO_PRIO_LOW -1 |
316 | #define PRIO_IDLE -3 |
336 | #define CORO_PRIO_IDLE -3 |
317 | #define PRIO_MIN -4 |
337 | #define CORO_PRIO_MIN -4 |
318 | |
338 | |
319 | /* for Coro.pm */ |
339 | /* for Coro.pm */ |
320 | static SV *coro_current; |
340 | static SV *coro_current; |
321 | static SV *coro_readyhook; |
341 | static SV *coro_readyhook; |
322 | static struct coro *coro_ready [PRIO_MAX - PRIO_MIN + 1][2]; /* head|tail */ |
342 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
323 | static CV *cv_coro_run, *cv_coro_terminate; |
343 | static CV *cv_coro_run, *cv_coro_terminate; |
324 | static struct coro *coro_first; |
344 | static struct coro *coro_first; |
325 | #define coro_nready coroapi.nready |
345 | #define coro_nready coroapi.nready |
326 | |
346 | |
327 | /** lowlevel stuff **********************************************************/ |
347 | /** lowlevel stuff **********************************************************/ |
… | |
… | |
366 | |
386 | |
367 | if (!cv) |
387 | if (!cv) |
368 | croak ("code reference expected"); |
388 | croak ("code reference expected"); |
369 | |
389 | |
370 | return cv; |
390 | return cv; |
|
|
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]; |
371 | } |
437 | } |
372 | |
438 | |
373 | /*****************************************************************************/ |
439 | /*****************************************************************************/ |
374 | /* magic glue */ |
440 | /* magic glue */ |
375 | |
441 | |
… | |
… | |
475 | static int |
541 | static int |
476 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
542 | coro_cv_free (pTHX_ SV *sv, MAGIC *mg) |
477 | { |
543 | { |
478 | AV *padlist; |
544 | AV *padlist; |
479 | 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; |
480 | |
550 | |
481 | /* casting is fun. */ |
551 | /* casting is fun. */ |
482 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
552 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
483 | free_padlist (aTHX_ padlist); |
553 | free_padlist (aTHX_ padlist); |
484 | |
554 | |
… | |
… | |
573 | } |
643 | } |
574 | |
644 | |
575 | slf_frame = c->slf_frame; |
645 | slf_frame = c->slf_frame; |
576 | CORO_THROW = c->except; |
646 | CORO_THROW = c->except; |
577 | |
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 | |
578 | if (expect_false (c->on_enter)) |
656 | if (expect_false (c->on_enter)) |
579 | { |
657 | { |
580 | int i; |
658 | int i; |
581 | |
659 | |
582 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
660 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
… | |
… | |
591 | { |
669 | { |
592 | int i; |
670 | int i; |
593 | |
671 | |
594 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
672 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
595 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[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); |
596 | } |
682 | } |
597 | |
683 | |
598 | c->except = CORO_THROW; |
684 | c->except = CORO_THROW; |
599 | c->slf_frame = slf_frame; |
685 | c->slf_frame = slf_frame; |
600 | |
686 | |
… | |
… | |
964 | |
1050 | |
965 | PL_op = (OP *)&coro_setup_op; |
1051 | PL_op = (OP *)&coro_setup_op; |
966 | |
1052 | |
967 | /* copy throw, in case it was set before coro_setup */ |
1053 | /* copy throw, in case it was set before coro_setup */ |
968 | CORO_THROW = coro->except; |
1054 | CORO_THROW = coro->except; |
|
|
1055 | |
|
|
1056 | if (expect_false (enable_times)) |
|
|
1057 | { |
|
|
1058 | coro_times_update (); |
|
|
1059 | coro_times_sub (coro); |
|
|
1060 | } |
969 | } |
1061 | } |
970 | |
1062 | |
971 | static void |
1063 | static void |
972 | coro_unwind_stacks (pTHX) |
1064 | coro_unwind_stacks (pTHX) |
973 | { |
1065 | { |
… | |
… | |
1622 | /** Coro ********************************************************************/ |
1714 | /** Coro ********************************************************************/ |
1623 | |
1715 | |
1624 | INLINE void |
1716 | INLINE void |
1625 | coro_enq (pTHX_ struct coro *coro) |
1717 | coro_enq (pTHX_ struct coro *coro) |
1626 | { |
1718 | { |
1627 | struct coro **ready = coro_ready [coro->prio - PRIO_MIN]; |
1719 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1628 | |
1720 | |
1629 | SvREFCNT_inc_NN (coro->hv); |
1721 | SvREFCNT_inc_NN (coro->hv); |
1630 | |
1722 | |
1631 | coro->next_ready = 0; |
1723 | coro->next_ready = 0; |
1632 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1724 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
… | |
… | |
1636 | INLINE struct coro * |
1728 | INLINE struct coro * |
1637 | coro_deq (pTHX) |
1729 | coro_deq (pTHX) |
1638 | { |
1730 | { |
1639 | int prio; |
1731 | int prio; |
1640 | |
1732 | |
1641 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1733 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
1642 | { |
1734 | { |
1643 | struct coro **ready = coro_ready [prio]; |
1735 | struct coro **ready = coro_ready [prio]; |
1644 | |
1736 | |
1645 | if (ready [0]) |
1737 | if (ready [0]) |
1646 | { |
1738 | { |
… | |
… | |
2965 | |
3057 | |
2966 | if (!svp) croak ("Time::HiRes is required"); |
3058 | if (!svp) croak ("Time::HiRes is required"); |
2967 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
3059 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
2968 | |
3060 | |
2969 | 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)); |
2970 | } |
3065 | } |
2971 | |
3066 | |
2972 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
3067 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
2973 | } |
3068 | } |
2974 | |
3069 | |
2975 | SV * |
3070 | SV * |
2976 | new (char *klass, ...) |
3071 | new (char *klass, ...) |
2977 | ALIAS: |
3072 | ALIAS: |
… | |
… | |
3179 | CODE: |
3274 | CODE: |
3180 | { |
3275 | { |
3181 | struct coro *current = SvSTATE_current; |
3276 | struct coro *current = SvSTATE_current; |
3182 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3277 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3183 | SvREFCNT_dec (*throwp); |
3278 | SvREFCNT_dec (*throwp); |
|
|
3279 | SvGETMAGIC (throw); |
3184 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3280 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3185 | } |
3281 | } |
3186 | |
3282 | |
3187 | void |
3283 | void |
3188 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3284 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
… | |
… | |
3247 | CODE: |
3343 | CODE: |
3248 | coro_state_destroy (aTHX_ self); |
3344 | coro_state_destroy (aTHX_ self); |
3249 | coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ |
3345 | coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ |
3250 | |
3346 | |
3251 | |
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 | } |
|
|
3384 | |
3252 | MODULE = Coro::State PACKAGE = Coro |
3385 | MODULE = Coro::State PACKAGE = Coro |
3253 | |
3386 | |
3254 | BOOT: |
3387 | BOOT: |
3255 | { |
3388 | { |
3256 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3389 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
… | |
… | |
3268 | 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); |
3269 | 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); |
3270 | |
3403 | |
3271 | coro_stash = gv_stashpv ("Coro", TRUE); |
3404 | coro_stash = gv_stashpv ("Coro", TRUE); |
3272 | |
3405 | |
3273 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
3406 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3274 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
3407 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3275 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
3408 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
3276 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
3409 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (CORO_PRIO_LOW)); |
3277 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
3410 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (CORO_PRIO_IDLE)); |
3278 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
3411 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (CORO_PRIO_MIN)); |
3279 | |
3412 | |
3280 | { |
3413 | { |
3281 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3414 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3282 | |
3415 | |
3283 | coroapi.schedule = api_schedule; |
3416 | coroapi.schedule = api_schedule; |
… | |
… | |
3335 | void |
3468 | void |
3336 | _set_readyhook (SV *hook) |
3469 | _set_readyhook (SV *hook) |
3337 | PROTOTYPE: $ |
3470 | PROTOTYPE: $ |
3338 | CODE: |
3471 | CODE: |
3339 | SvREFCNT_dec (coro_readyhook); |
3472 | SvREFCNT_dec (coro_readyhook); |
|
|
3473 | SvGETMAGIC (hook); |
3340 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
3474 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
3341 | |
3475 | |
3342 | int |
3476 | int |
3343 | prio (Coro::State coro, int newprio = 0) |
3477 | prio (Coro::State coro, int newprio = 0) |
3344 | PROTOTYPE: $;$ |
3478 | PROTOTYPE: $;$ |
… | |
… | |
3351 | if (items > 1) |
3485 | if (items > 1) |
3352 | { |
3486 | { |
3353 | if (ix) |
3487 | if (ix) |
3354 | newprio = coro->prio - newprio; |
3488 | newprio = coro->prio - newprio; |
3355 | |
3489 | |
3356 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
3490 | if (newprio < CORO_PRIO_MIN) newprio = CORO_PRIO_MIN; |
3357 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
3491 | if (newprio > CORO_PRIO_MAX) newprio = CORO_PRIO_MAX; |
3358 | |
3492 | |
3359 | coro->prio = newprio; |
3493 | coro->prio = newprio; |
3360 | } |
3494 | } |
3361 | } |
3495 | } |
3362 | OUTPUT: |
3496 | OUTPUT: |
… | |
… | |
3488 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
3622 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
3489 | |
3623 | |
3490 | SV * |
3624 | SV * |
3491 | new (SV *klass, SV *count = 0) |
3625 | new (SV *klass, SV *count = 0) |
3492 | 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 | |
3493 | RETVAL = sv_bless ( |
3638 | RETVAL = sv_bless ( |
3494 | coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), |
3639 | coro_waitarray_new (aTHX_ semcnt), |
3495 | GvSTASH (CvGV (cv)) |
3640 | GvSTASH (CvGV (cv)) |
3496 | ); |
3641 | ); |
|
|
3642 | } |
3497 | OUTPUT: |
3643 | OUTPUT: |
3498 | RETVAL |
3644 | RETVAL |
3499 | |
3645 | |
3500 | # helper for Coro::Channel and others |
3646 | # helper for Coro::Channel and others |
3501 | SV * |
3647 | SV * |