| 1 |
|
| #ifndef FT_LOOP_H |
| 2 |
|
| #define FT_LOOP_H |
| 3 |
|
| |
| 4 |
|
| /* Fetch's minimal event loop: a readiness backend plus per-fd read/write |
| 5 |
|
| * callbacks and timers, with run_until(future) for re-entrant awaiting. This |
| 6 |
|
| * supplies the hm_loop_run + hm_cur_loop hook that ft_future.h's await path |
| 7 |
|
| * calls. Far smaller than Hyperman's server loop - no connections, PSGI, or |
| 8 |
|
| * idle/timeout sweeps; just IO readiness and timers. |
| 9 |
|
| * |
| 10 |
|
| * Must be included AFTER ft_future.h (which forward-declares struct hm_loop, |
| 11 |
|
| * declares hm_loop_run, and defines the shared hm_cur_loop) and after the |
| 12 |
|
| * backend .c files. */ |
| 13 |
|
| |
| 14 |
|
| #include <errno.h> |
| 15 |
|
| |
| 16 |
|
| typedef struct ft_timer { |
| 17 |
|
| SV *cb; |
| 18 |
|
| int oneshot; |
| 19 |
|
| UV id; /* the token the backend holds; never reused */ |
| 20 |
|
| double secs; /* as requested, for re-arming an interval */ |
| 21 |
|
| double due; /* monotonic deadline, for re-arming a oneshot */ |
| 22 |
|
| struct ft_timer *next; |
| 23 |
|
| } ft_timer; |
| 24 |
|
| |
| 25 |
|
| /* the monotonic clock the deadline backends already keep */ |
| 26 |
|
| #ifdef _WIN32 |
| 27 |
|
| #define ft_loop_now() hm_sel_now() |
| 28 |
|
| #else |
| 29 |
|
| #define ft_loop_now() hm_poll_now() |
| 30 |
|
| #endif |
| 31 |
|
| |
| 32 |
|
| struct hm_loop { /* name matches ft_future.h's forward decl */ |
| 33 |
|
| hm_backend *be; /* NULL between a fork and the child's first need */ |
| 34 |
|
| const char *be_name; /* the backend to rebuild with (a static string) */ |
| 35 |
|
| IV pid; /* the process whose kernel object `be` is */ |
| 36 |
|
| SV **rcb; /* [HM_MAXFD] read-ready callbacks */ |
| 37 |
|
| SV **wcb; /* [HM_MAXFD] write-ready callbacks */ |
| 38 |
|
| ft_timer *timers; /* live timers, for dispatch + cleanup */ |
| 39 |
|
| UV last_timer_id; /* monotonic; see ft_timer_token */ |
| 40 |
|
| int stop; |
| 41 |
|
| int nio; /* live fd interests, for the idle check */ |
| 42 |
|
| }; |
| 43 |
|
| |
| 44 |
|
| /* What the backend is given to identify a timer, and gives back when one |
| 45 |
|
| * fires. It is a counter, not the ft_timer address, because a cancelled timer |
| 46 |
|
| * can still have an event in flight - queued in the kernel, or already in a |
| 47 |
|
| * completion ring - and the freed struct is exactly the right size to be |
| 48 |
|
| * handed straight back to the next ft_add_timer. Then the stale event names a |
| 49 |
|
| * live timer and takes that down instead: the next request fails on a deadline |
| 50 |
|
| * that was never its own. A counter is never reused, so the lookup below |
| 51 |
|
| * simply does not find it and the event is dropped. */ |
| 52 |
|
| #define ft_timer_token(id) INT2PTR(void *, (UV)(id)) |
| 53 |
|
| |
| 54 |
|
| typedef struct hm_loop ft_loop; |
| 55 |
|
| /* hm_cur_loop is defined (static) in ft_future.h; we only read/write it. */ |
| 56 |
|
| |
| 57 |
24 |
| static ft_timer *ft_timer_of_token(ft_loop *l, void *token) { |
| 58 |
24 |
| UV id = PTR2UV(token); |
| 59 |
|
| ft_timer *t; |
| 60 |
69 |
▶ | for (t = l->timers; t; t = t->next) if (t->id == id) return t; |
Branch: 2 branches
| Branch | True | False | Total |
| for (t = l->timers; t; t = t->next) if (t->id == id) return t; |
24 |
45 |
69 |
| for (t = l->timers; t; t = t->next) if (t->id == id) return t; |
69 |
0 |
69 |
|
| 61 |
0 |
| return NULL; |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
47 |
| static ft_loop *ft_loop_new(pTHX_ const char *name) { |
| 65 |
47 |
| ft_loop *l = (ft_loop *)calloc(1, sizeof(ft_loop)); |
| 66 |
47 |
▶ | if (!l) croak("Fetch::Loop: out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!l) croak("Fetch::Loop: out of memory"); |
0 |
47 |
47 |
|
| 67 |
47 |
▶ | l->be = hm_backend_create(name && *name ? name : NULL); |
Branch: 2 branches
| Branch | True | False | Total |
| l->be = hm_backend_create(name && *name ? name : NULL); |
4 |
43 |
47 |
| l->be = hm_backend_create(name && *name ? name : NULL); |
4 |
0 |
4 |
|
| 68 |
47 |
▶ | if (!l->be) { free(l); croak("Fetch::Loop: no event backend available"); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!l->be) { free(l); croak("Fetch::Loop: no event backend available"); } |
2 |
45 |
47 |
|
| 69 |
45 |
| l->be_name = l->be->name; |
| 70 |
45 |
| l->pid = (IV)PerlProc_getpid(); |
| 71 |
45 |
| l->rcb = (SV **)calloc(HM_MAXFD, sizeof(SV *)); |
| 72 |
45 |
| l->wcb = (SV **)calloc(HM_MAXFD, sizeof(SV *)); |
| 73 |
45 |
▶ | if (!l->rcb || !l->wcb) croak("Fetch::Loop: out of memory"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!l->rcb || !l->wcb) croak("Fetch::Loop: out of memory"); |
45 |
0 |
45 |
| if (!l->rcb || !l->wcb) croak("Fetch::Loop: out of memory"); |
0 |
45 |
45 |
|
| 74 |
45 |
| return l; |
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
|
| /* A loop inherited across a fork must never reach its backend again. |
| 78 |
|
| * |
| 79 |
|
| * The backend's kernel object is SHARED with the parent, not copied. An epoll |
| 80 |
|
| * instance is one interest list for both processes, so a child "cleaning up" |
| 81 |
|
| * its inherited watchers deregisters the parent's sockets and the parent |
| 82 |
|
| * waits on an empty set forever. An io_uring is worse: the submission ring is |
| 83 |
|
| * shared memory, while liburing's cursors are per-process copies that stop |
| 84 |
|
| * agreeing the moment the parent submits again. The child's first |
| 85 |
|
| * io_uring_submit then hands the kernel a wrapped-negative count, the kernel |
| 86 |
|
| * pushes a full ring of stale entries, and the parent - now equally out of |
| 87 |
|
| * step - loops on io_uring_enter submitting 1024 dead requests per call |
| 88 |
|
| * until the machine runs out of memory. That is exactly what a forked test |
| 89 |
|
| * server did at exit, through global destruction of a Fetch it never used. |
| 90 |
|
| * |
| 91 |
|
| * So the first call that finds the pid changed disowns the loop: the |
| 92 |
|
| * inherited backend is released with `foreign` set (memory only, no |
| 93 |
|
| * descriptor, no ring) and a fresh one is built only if this process goes |
| 94 |
|
| * on to need one. A child that merely exits, running DESTROY on the way, |
| 95 |
|
| * never creates a ring just to tear it down. The watcher and timer tables |
| 96 |
|
| * are KEPT and mirrored onto the new backend when it is built: they are |
| 97 |
|
| * the child's copies of live connections, and a parked connection revived |
| 98 |
|
| * from the pool sends without re-arming, trusting that it is still |
| 99 |
|
| * READ-armed exactly as it was parked. */ |
| 100 |
|
| #define FT_LOOP_INHERITED(l) ((l)->pid != (IV)PerlProc_getpid()) |
| 101 |
|
| |
| 102 |
5 |
| static void ft_loop_disown(pTHX_ ft_loop *l) { |
| 103 |
5 |
| l->pid = (IV)PerlProc_getpid(); |
| 104 |
5 |
▶ | if (l->be) { l->be->foreign = 1; l->be->destroy(l->be); l->be = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (l->be) { l->be->foreign = 1; l->be->destroy(l->be); l->be = NULL; } |
5 |
0 |
5 |
|
| 105 |
5 |
| } |
| 106 |
|
| |
| 107 |
|
| /* Called on every entry that can reach the backend: hands back a backend |
| 108 |
|
| * this process owns, building one after a fork the first time it is needed. |
| 109 |
|
| * NULL only when nothing is needed (need == 0) and there is none. */ |
| 110 |
807 |
| static hm_backend *ft_loop_be(pTHX_ ft_loop *l, int need) { |
| 111 |
807 |
▶ | if (FT_LOOP_INHERITED(l)) ft_loop_disown(aTHX_ l); |
Branch: 1 branch
| Branch | True | False | Total |
| if (FT_LOOP_INHERITED(l)) ft_loop_disown(aTHX_ l); |
5 |
802 |
807 |
|
| 112 |
807 |
▶ | if (!l->be && need) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!l->be && need) { |
5 |
802 |
807 |
| if (!l->be && need) { |
3 |
2 |
5 |
|
| 113 |
|
| int fd; |
| 114 |
|
| ft_timer *t; |
| 115 |
|
| double now; |
| 116 |
3 |
| l->be = hm_backend_create(l->be_name); |
| 117 |
3 |
▶ | if (!l->be) l->be = hm_backend_create(NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!l->be) l->be = hm_backend_create(NULL); |
0 |
3 |
3 |
|
| 118 |
3 |
▶ | if (!l->be) croak("Fetch::Loop: no event backend available"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!l->be) croak("Fetch::Loop: no event backend available"); |
0 |
3 |
3 |
|
| 119 |
196611 |
▶ | for (fd = 0; fd < HM_MAXFD; fd++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (fd = 0; fd < HM_MAXFD; fd++) { |
196608 |
3 |
196611 |
|
| 120 |
196608 |
▶ | int mask = (l->rcb[fd] ? HM_EV_READ : 0) | (l->wcb[fd] ? HM_EV_WRITE : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| int mask = (l->rcb[fd] ? HM_EV_READ : 0) | (l->wcb[fd] ? HM_EV_WRITE : 0); |
0 |
196608 |
196608 |
|
| 121 |
196608 |
▶ | if (mask) l->be->add_io(l->be, fd, mask, 0); |
Branch: 1 branch
| Branch | True | False | Total |
| if (mask) l->be->add_io(l->be, fd, mask, 0); |
3 |
196605 |
196608 |
|
| 122 |
|
| } |
| 123 |
3 |
| now = ft_loop_now(); |
| 124 |
3 |
▶ | for (t = l->timers; t; t = t->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (t = l->timers; t; t = t->next) { |
0 |
3 |
3 |
|
| 125 |
0 |
▶ | double left = t->oneshot ? t->due - now : t->secs; |
Branch: 1 branch
| Branch | True | False | Total |
| double left = t->oneshot ? t->due - now : t->secs; |
0 |
0 |
0 |
|
| 126 |
0 |
▶ | if (left < 0) left = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (left < 0) left = 0; |
0 |
0 |
0 |
|
| 127 |
0 |
| l->be->add_timer(l->be, left, t->oneshot, ft_timer_token(t->id)); |
| 128 |
|
| } |
| 129 |
|
| } |
| 130 |
807 |
| return l->be; |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
45 |
| static void ft_loop_free(pTHX_ ft_loop *l) { |
| 134 |
|
| int fd; |
| 135 |
|
| ft_timer *t; |
| 136 |
45 |
▶ | if (!l) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!l) return; |
0 |
45 |
45 |
|
| 137 |
45 |
▶ | if (FT_LOOP_INHERITED(l)) ft_loop_disown(aTHX_ l); |
Branch: 1 branch
| Branch | True | False | Total |
| if (FT_LOOP_INHERITED(l)) ft_loop_disown(aTHX_ l); |
0 |
45 |
45 |
|
| 138 |
45 |
▶ | if (l->rcb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (l->rcb) { |
45 |
0 |
45 |
|
| 139 |
2949165 |
▶ | for (fd = 0; fd < HM_MAXFD; fd++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (fd = 0; fd < HM_MAXFD; fd++) { |
2949120 |
45 |
2949165 |
|
| 140 |
2949120 |
| SV *cb = l->rcb[fd]; |
| 141 |
2949120 |
▶ | if (cb) { l->rcb[fd] = NULL; SvREFCNT_dec(cb); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb) { l->rcb[fd] = NULL; SvREFCNT_dec(cb); } |
7 |
2949113 |
2949120 |
|
| 142 |
|
| } |
| 143 |
|
| } |
| 144 |
45 |
▶ | if (l->wcb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (l->wcb) { |
45 |
0 |
45 |
|
| 145 |
2949165 |
▶ | for (fd = 0; fd < HM_MAXFD; fd++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (fd = 0; fd < HM_MAXFD; fd++) { |
2949120 |
45 |
2949165 |
|
| 146 |
2949120 |
| SV *cb = l->wcb[fd]; |
| 147 |
2949120 |
▶ | if (cb) { l->wcb[fd] = NULL; SvREFCNT_dec(cb); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb) { l->wcb[fd] = NULL; SvREFCNT_dec(cb); } |
0 |
2949120 |
2949120 |
|
| 148 |
|
| } |
| 149 |
|
| } |
| 150 |
45 |
| t = l->timers; |
| 151 |
45 |
| l->timers = NULL; |
| 152 |
45 |
▶ | while (t) { ft_timer *n = t->next; if (t->cb) SvREFCNT_dec(t->cb); free(t); t = n; } |
Branch: 2 branches
| Branch | True | False | Total |
| while (t) { ft_timer *n = t->next; if (t->cb) SvREFCNT_dec(t->cb); free(t); t = n; } |
0 |
0 |
0 |
| while (t) { ft_timer *n = t->next; if (t->cb) SvREFCNT_dec(t->cb); free(t); t = n; } |
0 |
45 |
45 |
|
| 153 |
45 |
▶ | if (l->be) l->be->destroy(l->be); |
Branch: 1 branch
| Branch | True | False | Total |
| if (l->be) l->be->destroy(l->be); |
43 |
2 |
45 |
|
| 154 |
45 |
| free(l->rcb); |
| 155 |
45 |
| free(l->wcb); |
| 156 |
45 |
| free(l); |
| 157 |
|
| } |
| 158 |
|
| |
| 159 |
|
| /* call $cb->() discarding the result, trapping death into a warning */ |
| 160 |
307 |
| static void ft_call0(pTHX_ SV *cb) { |
| 161 |
|
| /* Fast path: the loop's own watchers are C closures (hm_closure XSUBs |
| 162 |
|
| * carrying hm_clos magic) - call the XSUB body directly, skipping |
| 163 |
|
| * pp_entersub, the eval frame and the Perl stack dance. These callbacks |
| 164 |
|
| * do not die; any user Perl code they reach (future on_ready callbacks) |
| 165 |
|
| * is eval-guarded where it runs, in hmf_pump. */ |
| 166 |
307 |
▶ | if (SvROK(cb)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(cb)) { |
307 |
0 |
307 |
|
| 167 |
307 |
| CV *cv = (CV *)SvRV(cb); |
| 168 |
307 |
▶ | if (SvTYPE((SV *)cv) == SVt_PVCV && CvISXSUB(cv) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTYPE((SV *)cv) == SVt_PVCV && CvISXSUB(cv) |
307 |
0 |
307 |
| if (SvTYPE((SV *)cv) == SVt_PVCV && CvISXSUB(cv) |
284 |
23 |
307 |
|
| 169 |
284 |
▶ | && hm_clos_of(aTHX_ cv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| && hm_clos_of(aTHX_ cv)) { |
284 |
0 |
284 |
|
| 170 |
284 |
| dSP; |
| 171 |
284 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
284 |
284 |
|
| 172 |
284 |
| PUTBACK; |
| 173 |
284 |
| (CvXSUB(cv))(aTHX_ cv); |
| 174 |
284 |
| return; |
| 175 |
|
| } |
| 176 |
|
| } |
| 177 |
|
| { |
| 178 |
23 |
| dSP; |
| 179 |
23 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
0 |
23 |
23 |
|
| 180 |
23 |
| call_sv(cb, G_DISCARD | G_EVAL); |
| 181 |
19 |
▶ | if (SvTRUE(ERRSV)) warn("Fetch: loop callback died: %s", SvPV_nolen(ERRSV)); |
Branch: 3 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) warn("Fetch: loop callback died: %s", SvPV_nolen(ERRSV)); |
19 |
0 |
19 |
| if (SvTRUE(ERRSV)) warn("Fetch: loop callback died: %s", SvPV_nolen(ERRSV)); |
0 |
19 |
19 |
| if (SvTRUE(ERRSV)) warn("Fetch: loop callback died: %s", SvPV_nolen(ERRSV)); |
0 |
0 |
0 |
|
| 182 |
19 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
19 |
19 |
|
| 183 |
|
| } |
| 184 |
|
| } |
| 185 |
|
| |
| 186 |
236 |
| static void ft_watch_io(pTHX_ ft_loop *l, int fd, int mask, SV *cb) { |
| 187 |
236 |
| hm_backend *be = ft_loop_be(aTHX_ l, 1); |
| 188 |
236 |
▶ | if (fd < 0 || fd >= HM_MAXFD) croak("watch_io: fd %d out of range", fd); |
Branch: 2 branches
| Branch | True | False | Total |
| if (fd < 0 || fd >= HM_MAXFD) croak("watch_io: fd %d out of range", fd); |
236 |
0 |
236 |
| if (fd < 0 || fd >= HM_MAXFD) croak("watch_io: fd %d out of range", fd); |
0 |
236 |
236 |
|
| 189 |
236 |
▶ | if (mask & HM_EV_READ) { if (l->rcb[fd]) SvREFCNT_dec(l->rcb[fd]); else l->nio++; l->rcb[fd] = SvREFCNT_inc(cb); } |
Branch: 2 branches
| Branch | True | False | Total |
| if (mask & HM_EV_READ) { if (l->rcb[fd]) SvREFCNT_dec(l->rcb[fd]); else l->nio++; l->rcb[fd] = SvREFCNT_inc(cb); } |
117 |
119 |
236 |
| if (mask & HM_EV_READ) { if (l->rcb[fd]) SvREFCNT_dec(l->rcb[fd]); else l->nio++; l->rcb[fd] = SvREFCNT_inc(cb); } |
0 |
117 |
117 |
|
| 190 |
236 |
▶ | if (mask & HM_EV_WRITE) { if (l->wcb[fd]) SvREFCNT_dec(l->wcb[fd]); else l->nio++; l->wcb[fd] = SvREFCNT_inc(cb); } |
Branch: 2 branches
| Branch | True | False | Total |
| if (mask & HM_EV_WRITE) { if (l->wcb[fd]) SvREFCNT_dec(l->wcb[fd]); else l->nio++; l->wcb[fd] = SvREFCNT_inc(cb); } |
119 |
117 |
236 |
| if (mask & HM_EV_WRITE) { if (l->wcb[fd]) SvREFCNT_dec(l->wcb[fd]); else l->nio++; l->wcb[fd] = SvREFCNT_inc(cb); } |
0 |
119 |
119 |
|
| 191 |
236 |
| be->add_io(be, fd, mask, 0); |
| 192 |
236 |
| } |
| 193 |
|
| |
| 194 |
229 |
| static void ft_unwatch_io(pTHX_ ft_loop *l, int fd, int mask) { |
| 195 |
229 |
| hm_backend *be = ft_loop_be(aTHX_ l, 0); |
| 196 |
229 |
▶ | if (fd < 0 || fd >= HM_MAXFD) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (fd < 0 || fd >= HM_MAXFD) return; |
229 |
0 |
229 |
| if (fd < 0 || fd >= HM_MAXFD) return; |
0 |
229 |
229 |
|
| 197 |
229 |
▶ | if (mask & HM_EV_READ) { if (l->rcb[fd]) { SvREFCNT_dec(l->rcb[fd]); l->rcb[fd] = NULL; l->nio--; } } |
Branch: 2 branches
| Branch | True | False | Total |
| if (mask & HM_EV_READ) { if (l->rcb[fd]) { SvREFCNT_dec(l->rcb[fd]); l->rcb[fd] = NULL; l->nio--; } } |
110 |
119 |
229 |
| if (mask & HM_EV_READ) { if (l->rcb[fd]) { SvREFCNT_dec(l->rcb[fd]); l->rcb[fd] = NULL; l->nio--; } } |
110 |
0 |
110 |
|
| 198 |
229 |
▶ | if (mask & HM_EV_WRITE) { if (l->wcb[fd]) { SvREFCNT_dec(l->wcb[fd]); l->wcb[fd] = NULL; l->nio--; } } |
Branch: 2 branches
| Branch | True | False | Total |
| if (mask & HM_EV_WRITE) { if (l->wcb[fd]) { SvREFCNT_dec(l->wcb[fd]); l->wcb[fd] = NULL; l->nio--; } } |
119 |
110 |
229 |
| if (mask & HM_EV_WRITE) { if (l->wcb[fd]) { SvREFCNT_dec(l->wcb[fd]); l->wcb[fd] = NULL; l->nio--; } } |
119 |
0 |
119 |
|
| 199 |
229 |
▶ | if (be) be->remove_io(be, fd, mask); |
Branch: 1 branch
| Branch | True | False | Total |
| if (be) be->remove_io(be, fd, mask); |
227 |
2 |
229 |
|
| 200 |
|
| } |
| 201 |
|
| |
| 202 |
40 |
| static ft_timer *ft_add_timer(pTHX_ ft_loop *l, double secs, SV *cb, int oneshot) { |
| 203 |
40 |
| hm_backend *be = ft_loop_be(aTHX_ l, 1); |
| 204 |
40 |
| ft_timer *t = (ft_timer *)calloc(1, sizeof(ft_timer)); |
| 205 |
40 |
▶ | if (!t) croak("Fetch: out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t) croak("Fetch: out of memory"); |
0 |
40 |
40 |
|
| 206 |
40 |
| t->cb = SvREFCNT_inc(cb); |
| 207 |
40 |
| t->oneshot = oneshot ? 1 : 0; |
| 208 |
40 |
| t->secs = secs; |
| 209 |
40 |
| t->due = ft_loop_now() + secs; |
| 210 |
40 |
| t->id = ++l->last_timer_id; |
| 211 |
40 |
| t->next = l->timers; |
| 212 |
40 |
| l->timers = t; |
| 213 |
40 |
| be->add_timer(be, secs, oneshot, ft_timer_token(t->id)); |
| 214 |
40 |
| return t; |
| 215 |
|
| } |
| 216 |
|
| |
| 217 |
40 |
| static void ft_timer_unlink(pTHX_ ft_loop *l, ft_timer *dead) { |
| 218 |
40 |
| ft_timer **pp = &l->timers; |
| 219 |
87 |
▶ | while (*pp) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (*pp) { |
87 |
0 |
87 |
|
| 220 |
87 |
▶ | if (*pp == dead) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*pp == dead) { |
40 |
47 |
87 |
|
| 221 |
40 |
| *pp = dead->next; |
| 222 |
40 |
▶ | if (dead->cb) SvREFCNT_dec(dead->cb); |
Branch: 1 branch
| Branch | True | False | Total |
| if (dead->cb) SvREFCNT_dec(dead->cb); |
40 |
0 |
40 |
|
| 223 |
40 |
| free(dead); |
| 224 |
40 |
| return; |
| 225 |
|
| } |
| 226 |
47 |
| pp = &(*pp)->next; |
| 227 |
|
| } |
| 228 |
|
| } |
| 229 |
|
| |
| 230 |
|
| /* Cancel a still-pending timer: drop it from the backend so it never fires, |
| 231 |
|
| * then unlink+free. (The fire path in hm_loop_run unlinks without del_timer, |
| 232 |
|
| * since a oneshot the backend has already dropped.) */ |
| 233 |
16 |
| static void ft_del_timer(pTHX_ ft_loop *l, ft_timer *t) { |
| 234 |
16 |
| hm_backend *be = ft_loop_be(aTHX_ l, 0); |
| 235 |
|
| ft_timer *p; |
| 236 |
16 |
▶ | if (!t) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t) return; |
0 |
16 |
16 |
|
| 237 |
|
| /* Only a timer still linked is ours to read: a connection copied into a |
| 238 |
|
| * forked child still points at a timer the disown above has freed. */ |
| 239 |
18 |
▶ | for (p = l->timers; p && p != t; p = p->next) ; |
Branch: 2 branches
| Branch | True | False | Total |
| for (p = l->timers; p && p != t; p = p->next) ; |
18 |
0 |
18 |
| for (p = l->timers; p && p != t; p = p->next) ; |
2 |
16 |
18 |
|
| 240 |
16 |
▶ | if (!p) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) return; |
0 |
16 |
16 |
|
| 241 |
16 |
▶ | if (be) be->del_timer(be, ft_timer_token(t->id)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (be) be->del_timer(be, ft_timer_token(t->id)); |
16 |
0 |
16 |
|
| 242 |
16 |
| ft_timer_unlink(aTHX_ l, t); |
| 243 |
|
| } |
| 244 |
|
| |
| 245 |
|
| /* Pump until `until` (a Fetch::Future SV) resolves, ->stop is set, or forever |
| 246 |
|
| * when until == NULL. Re-entrant: saves/restores hm_cur_loop and ->stop. */ |
| 247 |
144 |
| static void hm_loop_run(pTHX_ struct hm_loop *l, SV *until) { |
| 248 |
144 |
| ft_loop *saved = hm_cur_loop; |
| 249 |
144 |
| int saved_stop = l->stop; |
| 250 |
144 |
| hm_cur_loop = l; |
| 251 |
144 |
| l->stop = 0; |
| 252 |
|
| |
| 253 |
425 |
▶ | while (!l->stop) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!l->stop) { |
286 |
139 |
425 |
|
| 254 |
|
| hm_event evs[HM_MAXEV]; |
| 255 |
|
| hm_backend *be; |
| 256 |
|
| int n, i; |
| 257 |
|
| |
| 258 |
|
| /* Per turn, not per call: a callback may fork, and the child that |
| 259 |
|
| * carries on from inside it must not pump the parent's backend. */ |
| 260 |
286 |
| be = ft_loop_be(aTHX_ l, 1); |
| 261 |
|
| |
| 262 |
286 |
| hmf_pump(aTHX); /* drain future continuations */ |
| 263 |
286 |
▶ | if (until && hmf_state(aTHX_ until) != HMF_PENDING) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (until && hmf_state(aTHX_ until) != HMF_PENDING) break; |
279 |
7 |
286 |
| if (until && hmf_state(aTHX_ until) != HMF_PENDING) break; |
0 |
279 |
279 |
|
| 264 |
|
| |
| 265 |
|
| /* Nothing armed and nothing timed means nothing can ever wake us: |
| 266 |
|
| * wait_ev below blocks in the kernel with no deadline, which is not |
| 267 |
|
| * a stall a Perl-level alarm can even interrupt. Awaiting a future |
| 268 |
|
| * this loop was never going to resolve is the way to get here, so |
| 269 |
|
| * say that rather than hanging the process. */ |
| 270 |
286 |
▶ | if (until && !l->nio && !l->timers) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (until && !l->nio && !l->timers) { |
279 |
7 |
286 |
| if (until && !l->nio && !l->timers) { |
16 |
263 |
279 |
| if (until && !l->nio && !l->timers) { |
1 |
15 |
16 |
|
| 271 |
1 |
| l->stop = saved_stop; |
| 272 |
1 |
| hm_cur_loop = saved; |
| 273 |
1 |
| croak("Fetch::Future: awaited on a loop with no watchers - the " |
| 274 |
|
| "future belongs to a different loop"); |
| 275 |
|
| } |
| 276 |
|
| |
| 277 |
285 |
| n = be->wait_ev(be, evs, HM_MAXEV, -1.0); |
| 278 |
285 |
▶ | if (n < 0) { if (errno == EINTR) continue; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (n < 0) { if (errno == EINTR) continue; break; } |
0 |
285 |
285 |
| if (n < 0) { if (errno == EINTR) continue; break; } |
0 |
0 |
0 |
|
| 279 |
|
| |
| 280 |
452 |
▶ | for (i = 0; i < n && !l->stop; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < n && !l->stop; i++) { |
307 |
145 |
452 |
| for (i = 0; i < n && !l->stop; i++) { |
307 |
0 |
307 |
|
| 281 |
307 |
| hm_event *e = &evs[i]; |
| 282 |
307 |
▶ | if (e->kind & HM_EV_TIMER) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->kind & HM_EV_TIMER) { |
24 |
283 |
307 |
|
| 283 |
24 |
| ft_timer *t = ft_timer_of_token(l, e->udata); |
| 284 |
24 |
▶ | if (t && t->cb) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (t && t->cb) { |
24 |
0 |
24 |
| if (t && t->cb) { |
24 |
0 |
24 |
|
| 285 |
|
| SV *cb; |
| 286 |
24 |
| int one = t->oneshot; |
| 287 |
|
| /* The backend's clock is not the deadline. kqueue's |
| 288 |
|
| * EVFILT_TIMER on NetBSD is a callout in scheduler |
| 289 |
|
| * ticks, and in a VM those ticks run ahead of |
| 290 |
|
| * CLOCK_MONOTONIC: a 300 ms request timeout fired at |
| 291 |
|
| * 246 ms on a smoker. The deadline is `due` on the clock |
| 292 |
|
| * the request was timed on; a oneshot that arrives early |
| 293 |
|
| * is re-armed for the remainder (the backend has already |
| 294 |
|
| * dropped it, so the same token is free to re-add) and |
| 295 |
|
| * not reported. Each re-arm is for a shorter interval, so |
| 296 |
|
| * a backend that is always early still converges. Within |
| 297 |
|
| * a millisecond is the backends' own rounding. */ |
| 298 |
24 |
▶ | if (one) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (one) { |
24 |
0 |
24 |
|
| 299 |
24 |
| double left = t->due - ft_loop_now(); |
| 300 |
24 |
▶ | if (left > 1e-3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (left > 1e-3) { |
0 |
24 |
24 |
|
| 301 |
0 |
| be->add_timer(be, left, 1, ft_timer_token(t->id)); |
| 302 |
0 |
| continue; |
| 303 |
|
| } |
| 304 |
|
| } |
| 305 |
24 |
| cb = SvREFCNT_inc(t->cb); |
| 306 |
24 |
▶ | if (one) ft_timer_unlink(aTHX_ l, t); /* backend already dropped it */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (one) ft_timer_unlink(aTHX_ l, t); /* backend already dropped it */ |
24 |
0 |
24 |
|
| 307 |
24 |
| ft_call0(aTHX_ cb); |
| 308 |
20 |
| SvREFCNT_dec(cb); |
| 309 |
|
| } |
| 310 |
|
| } else { |
| 311 |
283 |
▶ | if ((e->kind & HM_EV_READ) && e->fd >= 0 && e->fd < HM_MAXFD && l->rcb[e->fd]) { |
Branch: 4 branches
| Branch | True | False | Total |
| if ((e->kind & HM_EV_READ) && e->fd >= 0 && e->fd < HM_MAXFD && l->rcb[e->fd]) { |
164 |
119 |
283 |
| if ((e->kind & HM_EV_READ) && e->fd >= 0 && e->fd < HM_MAXFD && l->rcb[e->fd]) { |
164 |
0 |
164 |
| if ((e->kind & HM_EV_READ) && e->fd >= 0 && e->fd < HM_MAXFD && l->rcb[e->fd]) { |
164 |
0 |
164 |
| if ((e->kind & HM_EV_READ) && e->fd >= 0 && e->fd < HM_MAXFD && l->rcb[e->fd]) { |
164 |
0 |
164 |
|
| 312 |
164 |
| SV *cb = SvREFCNT_inc(l->rcb[e->fd]); |
| 313 |
164 |
| ft_call0(aTHX_ cb); |
| 314 |
164 |
| SvREFCNT_dec(cb); |
| 315 |
|
| } |
| 316 |
283 |
▶ | if ((e->kind & HM_EV_WRITE) && e->fd >= 0 && e->fd < HM_MAXFD && l->wcb[e->fd]) { |
Branch: 4 branches
| Branch | True | False | Total |
| if ((e->kind & HM_EV_WRITE) && e->fd >= 0 && e->fd < HM_MAXFD && l->wcb[e->fd]) { |
119 |
164 |
283 |
| if ((e->kind & HM_EV_WRITE) && e->fd >= 0 && e->fd < HM_MAXFD && l->wcb[e->fd]) { |
119 |
0 |
119 |
| if ((e->kind & HM_EV_WRITE) && e->fd >= 0 && e->fd < HM_MAXFD && l->wcb[e->fd]) { |
119 |
0 |
119 |
| if ((e->kind & HM_EV_WRITE) && e->fd >= 0 && e->fd < HM_MAXFD && l->wcb[e->fd]) { |
119 |
0 |
119 |
|
| 317 |
119 |
| SV *cb = SvREFCNT_inc(l->wcb[e->fd]); |
| 318 |
119 |
| ft_call0(aTHX_ cb); |
| 319 |
119 |
| SvREFCNT_dec(cb); |
| 320 |
|
| } |
| 321 |
|
| } |
| 322 |
303 |
| hmf_pump(aTHX); |
| 323 |
303 |
▶ | if (until && hmf_state(aTHX_ until) != HMF_PENDING) { l->stop = 1; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (until && hmf_state(aTHX_ until) != HMF_PENDING) { l->stop = 1; break; } |
300 |
3 |
303 |
| if (until && hmf_state(aTHX_ until) != HMF_PENDING) { l->stop = 1; break; } |
136 |
164 |
300 |
|
| 324 |
|
| } |
| 325 |
|
| } |
| 326 |
|
| |
| 327 |
139 |
| l->stop = saved_stop; |
| 328 |
139 |
| hm_cur_loop = saved; |
| 329 |
139 |
| } |
| 330 |
|
| |
| 331 |
|
| #endif /* FT_LOOP_H */ |