c/ev.c

filter: Statementstmt 75.8% 185 / 244 Branchbran 52.3% 68 / 130 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 67.6% 253 / 374 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« c/timeable.c ↑ Summary c/hook.c »
1 /* 100 levels will trigger a manditory warning from perl */
2 #define MAX_CB_NEST 95
3  
4 static NV QueueTime[PE_QUEUES];
5  
6 static pe_cbframe CBFrame[MAX_CB_NEST];
7 static int CurCBFrame = -1;
8  
9 pe_event_vtbl event_vtbl, ioevent_vtbl, datafulevent_vtbl;
10  
11 346498 static void pe_anyevent_init(pe_event *ev, pe_watcher *wa) {
12 assert(wa);
13 346498 ev->up = wa;
14 346498 ++wa->refcnt;
15 346498 ev->mysv = 0;
16 346498 PE_RING_INIT(&ev->peer, ev);
17 346498 PE_RING_UNSHIFT(&ev->peer, &wa->events);
18 346498 ev->hits = 0;
19 346498 ev->prio = wa->prio;
20 346498 ev->callback = 0;
21 346498 }
22  
23 346498 static void pe_anyevent_dtor(pe_event *ev) {
24 STRLEN n_a;
25 346498 pe_watcher *wa = ev->up;
26 if (WaDEBUGx(wa) >= 3)
27 warn("Event=0x%x '%s' destroyed (SV=0x%x)",
28 ev,
29 SvPV(wa->desc, n_a),
30 ev->mysv? SvRV(ev->mysv) : 0);
31 346498 ev->up = 0;
32 346498 ev->mysv = 0;
33 346498 ev->hits = 0;
34 346498 ▶ if (EvPERLCB(ev))
Branch: 1 branch
BranchTrueFalseTotal
if (EvPERLCB(ev)) 346498 0 346498
35 346498 SvREFCNT_dec(ev->callback);
36 346498 ev->callback = 0;
37 346498 ▶ PE_RING_DETACH(&ev->peer);
Branch: 1 branch
BranchTrueFalseTotal
PE_RING_DETACH(&ev->peer); 730 345768 346498
38 346498 ▶ PE_RING_DETACH(&ev->que);
Branch: 1 branch
BranchTrueFalseTotal
PE_RING_DETACH(&ev->que); 0 346498 346498
39 346498 --wa->refcnt;
40 346498 ▶ if (WaCANDESTROY(wa)) /* running */
Branch: 3 branches
BranchTrueFalseTotal
if (WaCANDESTROY(wa)) /* running */ 829 345669 346498
if (WaCANDESTROY(wa)) /* running */ 829 0 829
if (WaCANDESTROY(wa)) /* running */ 829 0 829
41 829 (*wa->vtbl->dtor)(wa);
42 346498 }
43  
44 0 static void pe_anyevent_set_cb(pe_event *ev, void *fptr, void *ext) {
45 0 ▶ if (EvPERLCB(ev))
Branch: 1 branch
BranchTrueFalseTotal
if (EvPERLCB(ev)) 0 0 0
46 0 SvREFCNT_dec(ev->callback);
47 0 EvPERLCB_off(ev);
48 0 ev->callback = fptr;
49 0 ev->ext_data = ext;
50 0 }
51  
52 3 static void pe_anyevent_set_perl_cb(pe_event *ev, SV *sv) {
53 3 SV *old = 0;
54 3 ▶ if (EvPERLCB(ev))
Branch: 1 branch
BranchTrueFalseTotal
if (EvPERLCB(ev)) 2 1 3
55 2 old = ev->callback;
56 3 ev->callback = SvREFCNT_inc(sv);
57 3 SvREFCNT_dec(old);
58 3 EvPERLCB_on(ev);
59 3 }
60  
61 /*****************************************************************/
62  
63 85 static pe_event *pe_event_allocate(pe_watcher *wa) {
64 pe_event *ev;
65 assert(wa);
66 85 ▶ if (PE_RING_EMPTY(&event_vtbl.freelist)) {
Branch: 1 branch
BranchTrueFalseTotal
if (PE_RING_EMPTY(&event_vtbl.freelist)) { 31 54 85
67 31 EvNew(0, ev, 1, pe_event);
68 31 ev->vtbl = &event_vtbl;
69 31 PE_RING_INIT(&ev->que, ev);
70 } else {
71 54 pe_ring *lk = event_vtbl.freelist.prev;
72 54 ▶ PE_RING_DETACH(lk);
Branch: 1 branch
BranchTrueFalseTotal
PE_RING_DETACH(lk); 54 0 54
73 54 ev = (pe_event*) lk->self;
74 }
75 85 pe_anyevent_init(ev, wa);
76 85 return ev;
77 }
78  
79 85 static void pe_event_dtor(pe_event *ev) {
80 85 pe_anyevent_dtor(ev);
81 85 PE_RING_UNSHIFT(&ev->que, &event_vtbl.freelist);
82 85 }
83  
84 346498 static void pe_event_release(pe_event *ev) {
85 346498 ▶ if (!ev->mysv)
Branch: 1 branch
BranchTrueFalseTotal
if (!ev->mysv) 730 345768 346498
86 730 (*ev->vtbl->dtor)(ev);
87 else {
88 345768 SvREFCNT_dec(ev->mysv);
89 345768 ev->mysv=0;
90 }
91 346498 }
92  
93 0 EKEYMETH(_event_hits) {
94 0 ▶ if (!nval) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nval) { 0 0 0
95 0 dSP;
96 0 ▶ XPUSHs(sv_2mortal(newSViv(ev->hits)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(ev->hits))); 0 0 0
97 0 PUTBACK;
98 } else
99 0 croak("'e_hits' is read-only");
100 0 }
101  
102 0 EKEYMETH(_event_prio) {
103 0 ▶ if (!nval) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nval) { 0 0 0
104 0 dSP;
105 0 ▶ XPUSHs(sv_2mortal(newSViv(ev->prio)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(ev->prio))); 0 0 0
106 0 PUTBACK;
107 } else
108 0 croak("'e_prio' is read-only");
109 0 }
110  
111 /*------------------------------------------------------*/
112  
113 346410 static pe_event *pe_ioevent_allocate(pe_watcher *wa) {
114 pe_ioevent *ev;
115 assert(wa);
116 346410 ▶ if (PE_RING_EMPTY(&ioevent_vtbl.freelist)) {
Branch: 1 branch
BranchTrueFalseTotal
if (PE_RING_EMPTY(&ioevent_vtbl.freelist)) { 1241 345169 346410
117 1241 EvNew(1, ev, 1, pe_ioevent);
118 1241 ev->base.vtbl = &ioevent_vtbl;
119 1241 PE_RING_INIT(&ev->base.que, ev);
120 } else {
121 345169 pe_ring *lk = ioevent_vtbl.freelist.prev;
122 345169 ▶ PE_RING_DETACH(lk);
Branch: 1 branch
BranchTrueFalseTotal
PE_RING_DETACH(lk); 345169 0 345169
123 345169 ev = (pe_ioevent*) lk->self;
124 }
125 346410 pe_anyevent_init(&ev->base, wa);
126 346410 ev->got = 0;
127 346410 return &ev->base;
128 }
129  
130 346410 static void pe_ioevent_dtor(pe_event *ev) {
131 346410 pe_anyevent_dtor(ev);
132 346410 PE_RING_UNSHIFT(&ev->que, &ioevent_vtbl.freelist);
133 346410 }
134  
135 0 EKEYMETH(_event_got) {
136 0 pe_ioevent *io = (pe_ioevent *)ev;
137 0 ▶ if (!nval) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nval) { 0 0 0
138 0 dSP;
139 0 ▶ XPUSHs(sv_2mortal(events_mask_2sv(io->got)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(events_mask_2sv(io->got))); 0 0 0
140 0 PUTBACK;
141 } else
142 0 croak("'e_got' is read-only");
143 0 }
144  
145 /*------------------------------------------------------*/
146  
147 3 static pe_event *pe_datafulevent_allocate(pe_watcher *wa) {
148 pe_datafulevent *ev;
149 assert(wa);
150 3 ▶ if (PE_RING_EMPTY(&datafulevent_vtbl.freelist)) {
Branch: 1 branch
BranchTrueFalseTotal
if (PE_RING_EMPTY(&datafulevent_vtbl.freelist)) { 3 0 3
151 3 EvNew(15, ev, 1, pe_datafulevent);
152 3 ev->base.vtbl = &datafulevent_vtbl;
153 3 PE_RING_INIT(&ev->base.que, ev);
154 } else {
155 0 pe_ring *lk = datafulevent_vtbl.freelist.prev;
156 0 ▶ PE_RING_DETACH(lk);
Branch: 1 branch
BranchTrueFalseTotal
PE_RING_DETACH(lk); 0 0 0
157 0 ev = (pe_datafulevent*) lk->self;
158 }
159 3 pe_anyevent_init(&ev->base, wa);
160 3 ev->data = &PL_sv_undef;
161 3 return &ev->base;
162 }
163  
164 3 static void pe_datafulevent_dtor(pe_event *ev) {
165 3 pe_datafulevent *de = (pe_datafulevent *)ev;
166 3 SvREFCNT_dec(de->data);
167 3 pe_anyevent_dtor(ev);
168 3 PE_RING_UNSHIFT(&ev->que, &datafulevent_vtbl.freelist);
169 3 }
170  
171 0 EKEYMETH(_event_data) {
172 0 pe_datafulevent *de = (pe_datafulevent *)ev;
173 0 ▶ if (!nval) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nval) { 0 0 0
174 0 dSP;
175 0 ▶ XPUSHs(de->data);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(de->data); 0 0 0
176 0 PUTBACK;
177 } else
178 0 croak("'e_data' is read-only");
179 0 }
180  
181 /*------------------------------------------------------*/
182  
183 345768 static void pe_event_postCB(pe_cbframe *fp) {
184 345768 pe_event *ev = fp->ev;
185 345768 pe_watcher *wa = ev->up;
186 345768 --CurCBFrame;
187 345768 ▶ if (WaACTIVE(wa) && WaINVOKE1(wa) && WaREPEAT(wa))
Branch: 3 branches
BranchTrueFalseTotal
if (WaACTIVE(wa) && WaINVOKE1(wa) && WaREPEAT(wa)) 344903 865 345768
if (WaACTIVE(wa) && WaINVOKE1(wa) && WaREPEAT(wa)) 51 344852 344903
if (WaACTIVE(wa) && WaINVOKE1(wa) && WaREPEAT(wa)) 48 3 51
188 48 pe_watcher_on(wa, 1);
189 345768 ▶ if (Estat.on) {
Branch: 1 branch
BranchTrueFalseTotal
if (Estat.on) { 0 345768 345768
190 0 ▶ if (fp->stats) {
Branch: 1 branch
BranchTrueFalseTotal
if (fp->stats) { 0 0 0
191 0 Estat.scrub(fp->stats, wa);
192 0 fp->stats = 0;
193 }
194 0 ▶ if (CurCBFrame >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (CurCBFrame >= 0) { 0 0 0
195 0 pe_cbframe *pfp = CBFrame + CurCBFrame;
196 0 ▶ if (!pfp->stats)
Branch: 1 branch
BranchTrueFalseTotal
if (!pfp->stats) 0 0 0
197 0 pfp->stats = Estat.enter(CurCBFrame, pfp->ev->up->max_cb_tm);
198 else
199 0 Estat.resume(pfp->stats);
200 }
201 }
202 /* this must be last because it can destroy the watcher */
203 345768 pe_event_release(ev);
204 345768 }
205  
206 7 static void pe_callback_died(pe_cbframe *fp) {
207 7 dSP;
208 STRLEN n_a;
209 7 pe_watcher *wa = fp->ev->up;
210 7 SV *eval = perl_get_sv("Event::DIED", 1);
211 14 ▶ SV *err = (sv_true(ERRSV)?
Branch: 1 branch
BranchTrueFalseTotal
SV *err = (sv_true(ERRSV)? 7 0 7
212 7 ▶ sv_mortalcopy(ERRSV):
Branch: 2 branches
BranchTrueFalseTotal
sv_mortalcopy(ERRSV): 7 0 7
sv_mortalcopy(ERRSV): 7 0 7
213 0 sv_2mortal(newSVpv("?",0)));
214 if (WaDEBUGx(wa) >= 4)
215 warn("Event: '%s' died with: %s\n", SvPV(wa->desc,n_a),
216 SvPV(ERRSV,n_a));
217 7 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 7 7
218 7 ▶ XPUSHs(event_2sv(fp->ev));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(event_2sv(fp->ev)); 0 7 7
219 7 ▶ XPUSHs(err);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(err); 0 7 7
220 7 PUTBACK;
221 7 perl_call_sv(eval, G_EVAL|G_DISCARD);
222 7 ▶ if (sv_true(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_true(ERRSV)) { 7 0 7
if (sv_true(ERRSV)) { 1 6 7
223 3 ▶ warn("Event: '%s' died and then $Event::DIED died with: %s\n",
Branch: 1 branch
BranchTrueFalseTotal
warn("Event: '%s' died and then $Event::DIED died with: %s\n", 1 0 1
224 1 ▶ SvPV(wa->desc,n_a), SvPV(ERRSV,n_a));
Branch: 1 branch
BranchTrueFalseTotal
SvPV(wa->desc,n_a), SvPV(ERRSV,n_a)); 1 0 1
225 1 ▶ sv_setpv(ERRSV, "");
Branch: 1 branch
BranchTrueFalseTotal
sv_setpv(ERRSV, ""); 1 0 1
226 }
227 7 }
228  
229 2 static void _resume_watcher(void *vp) {
230 2 pe_watcher *wa = (pe_watcher *)vp;
231 2 pe_watcher_resume(wa);
232 2 }
233  
234 345915 static void pe_check_recovery() {
235 /* NO ASSERTIONS HERE! EVAL CONTEXT IS VERY MESSY */
236 int alert;
237 struct pe_cbframe *fp;
238 345915 ▶ if (CurCBFrame < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (CurCBFrame < 0) 345674 241 345915
239 345674 return;
240  
241 241 alert=0;
242 257 ▶ while (CurCBFrame >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (CurCBFrame >= 0) { 252 5 257
243 252 fp = CBFrame + CurCBFrame;
244 252 ▶ if (fp->ev->up->running == fp->run_id)
Branch: 1 branch
BranchTrueFalseTotal
if (fp->ev->up->running == fp->run_id) 236 16 252
245 236 break;
246 16 ▶ if (!alert) {
Branch: 1 branch
BranchTrueFalseTotal
if (!alert) { 6 10 16
247 6 alert=1;
248 /* exception detected; alert the militia! */
249 6 pe_callback_died(fp);
250 }
251 16 pe_event_postCB(fp);
252 }
253 }
254  
255 345768 static void pe_event_invoke(pe_event *ev) {
256 STRLEN n_a;
257 int Dbg;
258 345768 pe_watcher *wa = ev->up;
259 struct pe_cbframe *frp;
260  
261 345768 pe_check_recovery();
262  
263 /* SETUP */
264 345768 ENTER;
265 345768 SAVEINT(wa->running);
266 345768 ▶ PE_RING_DETACH(&ev->peer);
Branch: 1 branch
BranchTrueFalseTotal
PE_RING_DETACH(&ev->peer); 345768 0 345768
267 345768 frp = &CBFrame[++CurCBFrame];
268 345768 frp->ev = ev;
269 345768 frp->run_id = ++wa->running;
270 345768 ▶ if (Estat.on)
Branch: 1 branch
BranchTrueFalseTotal
if (Estat.on) 0 345768 345768
271 0 frp->stats = Estat.enter(CurCBFrame, wa->max_cb_tm);
272 assert(ev->prio >= 0 && ev->prio < PE_QUEUES);
273 345768 QueueTime[ev->prio] = wa->cbtime = NVtime();
274 /* SETUP */
275  
276 345768 ▶ if (CurCBFrame+1 >= MAX_CB_NEST) {
Branch: 1 branch
BranchTrueFalseTotal
if (CurCBFrame+1 >= MAX_CB_NEST) { 1 345767 345768
277 1 ExitLevel = 0;
278 1 croak("Deep recursion detected; invoking unloop_all()\n");
279 }
280  
281 345767 Dbg = WaDEBUGx(wa);
282 345767 ▶ if (Dbg) {
Branch: 1 branch
BranchTrueFalseTotal
if (Dbg) { 0 345767 345767
283 /*
284 SV *cvb = perl_get_sv("Carp::Verbose", 1);
285 if (!SvIV(cvb)) {
286 SAVEIV(SvIVX(cvb));
287 SvIVX(cvb) = 1;
288 }
289 */
290  
291 0 ▶ if (Dbg >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (Dbg >= 2) 0 0 0
292 0 warn("Event: [%d]invoking '%s' (prio %d)\n",
293 0 CurCBFrame, SvPV(wa->desc,n_a),ev->prio);
294 }
295  
296 345767 ▶ if (!PE_RING_EMPTY(&Callback)) pe_map_check(&Callback);
Branch: 1 branch
BranchTrueFalseTotal
if (!PE_RING_EMPTY(&Callback)) pe_map_check(&Callback); 1 345766 345767
297  
298 345767 ▶ if (EvPERLCB(ev)) {
Branch: 1 branch
BranchTrueFalseTotal
if (EvPERLCB(ev)) { 345767 0 345767
299 345767 SV *cb = SvRV((SV*)ev->callback);
300 345767 ▶ int pcflags = G_VOID | (SvIVX(Eval)? G_EVAL : 0);
Branch: 1 branch
BranchTrueFalseTotal
int pcflags = G_VOID | (SvIVX(Eval)? G_EVAL : 0); 1 345766 345767
301 int retcnt;
302 345767 SV *evsv = event_2sv(ev);
303 345767 dSP;
304 345767 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 345767 345767
305 345767 ▶ if (SvTYPE(cb) == SVt_PVCV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(cb) == SVt_PVCV) { 345761 6 345767
306 345761 ▶ XPUSHs(evsv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(evsv); 0 345761 345761
307 345761 PUTBACK;
308 345761 retcnt = perl_call_sv((SV*) ev->callback, pcflags);
309 } else {
310 6 AV *av = (AV*)cb;
311 assert(SvTYPE(cb) == SVt_PVAV);
312 6 ▶ XPUSHs(*av_fetch(av, 0, 0));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(*av_fetch(av, 0, 0)); 0 6 6
313 6 ▶ XPUSHs(evsv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(evsv); 0 6 6
314 6 PUTBACK;
315 12 retcnt = perl_call_method(SvPV(*av_fetch(av, 1, 0),n_a), pcflags);
316 }
317 345752 SPAGAIN;
318 345752 SP -= retcnt;
319 345752 PUTBACK;
320 345752 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 345752 0 345752
if (SvTRUE(ERRSV)) { 1 345751 345752
321 1 ▶ if (pcflags & G_EVAL)
Branch: 1 branch
BranchTrueFalseTotal
if (pcflags & G_EVAL) 1 0 1
322 1 pe_callback_died(frp);
323 else
324 0 ▶ sv_setsv(ERRSV, &PL_sv_no);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_no); 0 0 0
325 }
326 } else {
327 assert(ev->callback);
328 0 (* (void(*)(pe_event*)) ev->callback)(ev);
329 }
330  
331 345752 LEAVE;
332  
333 345752 ▶ if (Estat.on) {
Branch: 1 branch
BranchTrueFalseTotal
if (Estat.on) { 0 345752 345752
334 0 ▶ if (frp->stats) /* maybe in transition */
Branch: 1 branch
BranchTrueFalseTotal
if (frp->stats) /* maybe in transition */ 0 0 0
335 0 Estat.commit(frp->stats, wa);
336 0 frp->stats=0;
337 }
338 345752 ▶ if (Dbg >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (Dbg >= 3) 0 345752 345752
339 0 warn("Event: completed '%s'\n", SvPV(wa->desc, n_a));
340 345752 pe_event_postCB(frp);
341 345752 }
342  
343 25 static void boot_pe_event() {
344 pe_event_vtbl *vt;
345  
346 25 vt = &event_vtbl;
347 25 vt->new_event = pe_event_allocate;
348 25 vt->dtor = pe_event_dtor;
349 25 vt->stash = gv_stashpv("Event::Event", 1);
350 25 PE_RING_INIT(&vt->freelist, 0);
351  
352 25 vt = &ioevent_vtbl;
353 25 memcpy(vt, &event_vtbl, sizeof(pe_event_vtbl));
354 25 vt->stash = gv_stashpv("Event::Event::Io", 1);
355 25 vt->new_event = pe_ioevent_allocate;
356 25 vt->dtor = pe_ioevent_dtor;
357 25 PE_RING_INIT(&vt->freelist, 0);
358  
359 25 vt = &datafulevent_vtbl;
360 25 memcpy(vt, &event_vtbl, sizeof(pe_event_vtbl));
361 25 vt->stash = gv_stashpv("Event::Event::Dataful", 1);
362 25 vt->new_event = pe_datafulevent_allocate;
363 25 vt->dtor = pe_datafulevent_dtor;
364 25 PE_RING_INIT(&vt->freelist, 0);
365  
366 25 memset(QueueTime, 0, sizeof(QueueTime));
367 25 }
« c/timeable.c ↑ Summary c/hook.c »