xs/loop.xs

filter: Statementstmt 100% 56 / 56 Branchbran 71.1% 37 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.1% 93 / 108 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hyperman/hm_proxy.h ↑ Summary include/hyperman/hm_workerhook.h »
1 MODULE = Hyperman PACKAGE = Hyperman::Loop
2  
3 # The per-worker event loop, standalone. Callback watchers plus
4 # the Future-returning primitives; run is re-entrant (await pumps it).
5  
6 SV *
7 new(class, ...)
8 SV *class
9 CODE:
10 {
11 66 ▶ const char *bname = (items > 1 && SvOK(ST(1))) ? SvPV_nolen(ST(1)) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
const char *bname = (items > 1 && SvOK(ST(1))) ? SvPV_nolen(ST(1)) : NULL; 27 15 42
const char *bname = (items > 1 && SvOK(ST(1))) ? SvPV_nolen(ST(1)) : NULL; 24 3 27
12 hm_loop *loop;
13 int i;
14 PERL_UNUSED_VAR(class);
15 42 loop = hm_loop_new(aTHX_ bname);
16 42 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 4 38 42
# uncoverable condition right count:2 class:ignore_covered_err 4 0 4
# uncoverable condition right count:2 class:ignore_covered_err 4 0 4
17 # uncoverable condition right count:3 class:ignore_covered_err
18 if (!loop) croak("Hyperman::Loop: no usable event backend%s%s",
19 bname ? " named " : "", bname ? bname : "");
20 /* Key/value options after the backend name, so the loop takes the
21 38 ▶ * same settings the server does rather than only reading the
Branch: 1 branch
BranchTrueFalseTotal
* same settings the server does rather than only reading the 3 35 38
22 3 ▶ * environment. Odd counts croak instead of silently dropping one. */
Branch: 1 branch
BranchTrueFalseTotal
* environment. Odd counts croak instead of silently dropping one. */ 1 2 3
23 1 if (items > 2) {
24 3 ▶ if ((items - 2) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 2) % 2) 2 1 3
25 2 croak("Hyperman::Loop->new: odd number of options");
26 2 for (i = 2; i + 1 < items; i += 2) {
27 2 ▶ const char *key = SvPV_nolen(ST(i));
Branch: 1 branch
BranchTrueFalseTotal
const char *key = SvPV_nolen(ST(i)); 1 1 2
28 1 SV *val = ST(i + 1);
29 1 ▶ if (strEQ(key, "completion"))
Branch: 2 branches
BranchTrueFalseTotal
if (strEQ(key, "completion")) 1 0 1
if (strEQ(key, "completion")) 0 1 1
30 1 loop->completion =
31 # uncoverable condition left count:1 class:ignore_covered_err
32 # uncoverable condition right count:1 class:ignore_covered_err
33 36 # uncoverable condition left count:2 class:ignore_covered_err
34 36 (SvTRUE(val) && loop->be->submit_recv) ? 1 : 0;
35 else croak("Hyperman::Loop->new: unknown option '%s'", key);
36 }
37 }
38 loop->perl_owned = 1;
39 RETVAL = hm_loop_to_sv(aTHX_ loop);
40 }
41 OUTPUT:
42 RETVAL
43  
44 void
45 run(self)
46 SV *self
47 CODE:
48 17 hm_loop_run(aTHX_ hm_loop_from_sv(aTHX_ self), NULL);
49  
50 void
51 run_until(self, future)
52 SV *self
53 SV *future
54 CODE:
55 13 ▶ if (!hmf_is_future(aTHX_ future))
Branch: 1 branch
BranchTrueFalseTotal
if (!hmf_is_future(aTHX_ future)) 1 12 13
56 1 croak("run_until: not a Hyperman::Future");
57 12 hm_loop_run(aTHX_ hm_loop_from_sv(aTHX_ self), future);
58  
59 void
60 stop(self)
61 SV *self
62 CODE:
63 17 hm_loop_from_sv(aTHX_ self)->stop = 1;
64  
65 # Persistent callback watcher; returns a watcher handle for unwatch_io.
66 SV *
67 watch_io(self, fh, mode, cb)
68 SV *self
69 SV *fh
70 SV *mode
71 SV *cb
72 CODE:
73 {
74 4 hm_loop *loop = hm_loop_from_sv(aTHX_ self);
75 4 int fd = hm_fd_of(aTHX_ fh);
76 8 ▶ const char *m = SvOK(mode) ? SvPV_nolen(mode) : "r"; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
const char *m = SvOK(mode) ? SvPV_nolen(mode) : "r"; /* # uncoverable condition right count:1 class:ignore_covered_err */ 4 0 4
77 4 AV *handle = newAV();
78 4 hm_add_io_watch(aTHX_ loop, fd, m, cb, 1);
79 4 av_push(handle, newSViv(fd));
80 4 av_push(handle, newSVpv(m, 0));
81 4 RETVAL = newRV_noinc((SV *)handle);
82 }
83 OUTPUT:
84 RETVAL
85  
86 # Remove a watcher: takes the watch_io handle, or ($fh_or_fd, $mode).
87 void
88 unwatch_io(self, watcher, ...)
89 SV *self
90 SV *watcher
91 CODE:
92 {
93 6 hm_loop *loop = hm_loop_from_sv(aTHX_ self);
94 int fd;
95 6 const char *m = "r";
96 6 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 6 0 6
# uncoverable condition right count:1 class:ignore_covered_err 2 4 6
97 4 ▶ # uncoverable condition right count:3 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:3 class:ignore_covered_err 2 0 2
98 2 if (SvROK(watcher) && SvTYPE(SvRV(watcher)) == SVt_PVAV
99 2 && !SvOBJECT(SvRV(watcher))) {
100 2 AV *h = (AV *)SvRV(watcher);
101 2 ▶ SV **f = av_fetch(h, 0, 0);
Branch: 1 branch
BranchTrueFalseTotal
SV **f = av_fetch(h, 0, 0); 2 0 2
102 4 ▶ SV **mo = av_fetch(h, 1, 0);
Branch: 2 branches
BranchTrueFalseTotal
SV **mo = av_fetch(h, 1, 0); 2 0 2
SV **mo = av_fetch(h, 1, 0); 2 0 2
103 fd = f ? (int)SvIV(*f) : -1; /* # uncoverable condition right count:1 class:ignore_covered_err */
104 4 # uncoverable branch false class:ignore_covered_err
105 8 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 4 0 4
# uncoverable condition right count:1 class:ignore_covered_err 4 0 4
106 # uncoverable condition right count:2 class:ignore_covered_err
107 6 if (mo && SvOK(*mo)) m = SvPV_nolen(*mo);
108 6 ▶ } else {
Branch: 1 branch
BranchTrueFalseTotal
} else { 0 6 6
109 fd = hm_fd_of(aTHX_ watcher);
110 # uncoverable branch false class:ignore_covered_err
111 # uncoverable condition right count:1 class:ignore_covered_err
112 # uncoverable condition right count:2 class:ignore_covered_err
113 if (items > 2 && SvOK(ST(2))) m = SvPV_nolen(ST(2));
114 }
115 hm_del_io_watch(aTHX_ loop, fd,
116 m[0] == 'w' ? HM_EV_WRITE : HM_EV_READ); /* # uncoverable condition left count:1 class:ignore_covered_err */
117 }
118  
119 # One-shot callback timer.
120 void
121 timer(self, secs, cb)
122 SV *self
123 double secs
124 SV *cb
125 CODE:
126 33 hm_add_timer_watch(aTHX_ hm_loop_from_sv(aTHX_ self), secs, cb, HM_TW_CB);
127  
128 # ---- Future-returning primitives ----
129  
130 SV *
131 timer_f(self, secs)
132 SV *self
133 double secs
134 CODE:
135 2022 RETVAL = hmf_new(aTHX_ "Hyperman::Future");
136 2022 hm_add_timer_watch(aTHX_ hm_loop_from_sv(aTHX_ self), secs, RETVAL,
137 HM_TW_FUTURE);
138 OUTPUT:
139 RETVAL
140  
141 SV *
142 readable_f(self, fh)
143 SV *self
144 SV *fh
145 ALIAS:
146 writable_f = 1
147 CODE:
148 10 RETVAL = hmf_new(aTHX_ "Hyperman::Future");
149 10 ▶ hm_add_io_watch(aTHX_ hm_loop_from_sv(aTHX_ self),
Branch: 1 branch
BranchTrueFalseTotal
hm_add_io_watch(aTHX_ hm_loop_from_sv(aTHX_ self), 1 9 10
150 hm_fd_of(aTHX_ fh), ix ? "w" : "r", RETVAL, 0);
151 OUTPUT:
152 RETVAL
153  
154 void
155 defer(self, cb)
156 SV *self
157 SV *cb
158 CODE:
159 3 av_push(hm_loop_from_sv(aTHX_ self)->deferred, newSVsv(cb));
160  
161 # Make ->get/->await on pending futures pump this loop when it is not the
162 # currently running one (scripts and tests; workers need nothing).
163 SV *
164 install_await(self)
165 SV *self
166 CODE:
167 {
168 1 SV *cb = hm_closure(aTHX_ hm_xs_await_cb, self, NULL, NULL, NULL, 0, 0);
169 1 SV *aw = get_sv("Hyperman::Future::AWAIT", GV_ADD);
170 1 sv_setsv(aw, cb);
171 1 SvREFCNT_dec(cb);
172 1 RETVAL = SvREFCNT_inc(self);
173 }
174 OUTPUT:
175 RETVAL
176  
177 const char *
178 backend(self)
179 SV *self
180 CODE:
181 3 RETVAL = hm_loop_from_sv(aTHX_ self)->be->name;
182 OUTPUT:
183 RETVAL
184  
185 void
186 DESTROY(self)
187 SV *self
188 CODE:
189 {
190 908 hm_loop *loop = hm_loop_from_sv(aTHX_ self);
191 908 ▶ # uncoverable condition right count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:2 class:ignore_covered_err 36 872 908
# uncoverable condition right count:2 class:ignore_covered_err 36 0 36
# uncoverable condition right count:2 class:ignore_covered_err 36 0 36
192 36 # uncoverable condition right count:3 class:ignore_covered_err
193 if (loop->perl_owned && !loop->running && loop != hm_cur_loop)
194 hm_loop_free(aTHX_ loop);
195 }
« include/hyperman/hm_proxy.h ↑ Summary include/hyperman/hm_workerhook.h »