| 1 |
|
| static pe_ring NQueue; |
| 2 |
|
| static int StarvePrio = PE_QUEUES - 2; |
| 3 |
|
| |
| 4 |
25 |
| static void boot_queue() { |
| 5 |
25 |
| HV *stash = gv_stashpv("Event", 1); |
| 6 |
25 |
| PE_RING_INIT(&NQueue, 0); |
| 7 |
25 |
| newCONSTSUB(stash, "QUEUES", newSViv(PE_QUEUES)); |
| 8 |
25 |
| newCONSTSUB(stash, "PRIO_NORMAL", newSViv(PE_PRIO_NORMAL)); |
| 9 |
25 |
| newCONSTSUB(stash, "PRIO_HIGH", newSViv(PE_PRIO_HIGH)); |
| 10 |
25 |
| } |
| 11 |
|
| |
| 12 |
346482 |
| /*inline*/ static void dequeEvent(pe_event *ev) { |
| 13 |
|
| assert(ev); |
| 14 |
346482 |
▶ | PE_RING_DETACH(&ev->que); |
Branch: 1 branch
| Branch | True | False | Total |
| PE_RING_DETACH(&ev->que); |
346482 |
0 |
346482 |
|
| 15 |
346482 |
| --ActiveWatchers; |
| 16 |
346482 |
| } |
| 17 |
|
| |
| 18 |
0 |
| static void db_show_queue() { |
| 19 |
|
| pe_event *ev; |
| 20 |
0 |
| ev = (pe_event*) NQueue.next->self; |
| 21 |
0 |
▶ | while (ev) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (ev) { |
0 |
0 |
0 |
|
| 22 |
0 |
| warn("0x%x : %d\n", ev, ev->prio); |
| 23 |
0 |
| ev = (pe_event*) ev->que.next->self; |
| 24 |
|
| } |
| 25 |
0 |
| } |
| 26 |
|
| |
| 27 |
346498 |
| static int prepare_event(pe_event *ev, char *forwhat) { |
| 28 |
|
| /* AVOID DIEING IN HERE!! */ |
| 29 |
|
| STRLEN n_a; |
| 30 |
346498 |
| pe_watcher *wa = ev->up; |
| 31 |
346498 |
▶ | if (!ev->callback) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ev->callback) { |
346495 |
3 |
346498 |
|
| 32 |
346495 |
▶ | if (WaPERLCB(wa)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (WaPERLCB(wa)) { |
346495 |
0 |
346495 |
|
| 33 |
346495 |
| ev->callback = SvREFCNT_inc(wa->callback); |
| 34 |
346495 |
| EvPERLCB_on(ev); |
| 35 |
|
| } else { |
| 36 |
0 |
| ev->callback = wa->callback; |
| 37 |
0 |
| ev->ext_data = wa->ext_data; |
| 38 |
0 |
| EvPERLCB_off(ev); |
| 39 |
|
| } |
| 40 |
|
| assert(ev->callback); |
| 41 |
|
| } |
| 42 |
|
| assert(!WaSUSPEND(wa)); |
| 43 |
|
| assert(WaREENTRANT(wa) || !wa->running); |
| 44 |
346498 |
▶ | if (!WaACTIVE(wa)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!WaACTIVE(wa)) { |
9 |
346489 |
346498 |
|
| 45 |
9 |
▶ | if (!WaRUNNOW(wa)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!WaRUNNOW(wa)) |
0 |
9 |
9 |
|
| 46 |
0 |
| warn("Event: event for !ACTIVE watcher '%s'", SvPV(wa->desc,n_a)); |
| 47 |
|
| } |
| 48 |
|
| else { |
| 49 |
346489 |
▶ | if (!WaREPEAT(wa)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!WaREPEAT(wa)) |
29 |
346460 |
346489 |
|
| 50 |
29 |
| pe_watcher_stop(wa, 0); |
| 51 |
346460 |
▶ | else if (WaINVOKE1(wa)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (WaINVOKE1(wa)) |
48 |
346412 |
346460 |
|
| 52 |
48 |
| pe_watcher_off(wa); |
| 53 |
|
| } |
| 54 |
346498 |
| WaRUNNOW_off(wa); /* race condition? XXX */ |
| 55 |
|
| if (WaDEBUGx(wa) >= 3) |
| 56 |
|
| warn("Event: %s '%s' prio=%d\n", forwhat, SvPV(wa->desc,n_a), ev->prio); |
| 57 |
346498 |
| return 1; |
| 58 |
|
| } |
| 59 |
|
| |
| 60 |
346483 |
| static void queueEvent(pe_event *ev) { /**INVOKE**/ |
| 61 |
|
| assert(ev->hits); |
| 62 |
346483 |
▶ | if (!PE_RING_EMPTY(&ev->que)) return; /* clump'd event already queued */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PE_RING_EMPTY(&ev->que)) return; /* clump'd event already queued */ |
0 |
346483 |
346483 |
|
| 63 |
346483 |
▶ | if (!prepare_event(ev, "queue")) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!prepare_event(ev, "queue")) return; |
0 |
346483 |
346483 |
|
| 64 |
|
| |
| 65 |
346483 |
▶ | if (ev->prio < 0) { /* invoke the event immediately! */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (ev->prio < 0) { /* invoke the event immediately! */ |
1 |
346482 |
346483 |
|
| 66 |
1 |
| ev->prio = 0; |
| 67 |
1 |
| pe_event_invoke(ev); |
| 68 |
1 |
| return; |
| 69 |
|
| } |
| 70 |
346482 |
▶ | if (ev->prio >= PE_QUEUES) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ev->prio >= PE_QUEUES) |
0 |
346482 |
346482 |
|
| 71 |
0 |
| ev->prio = PE_QUEUES-1; |
| 72 |
|
| |
| 73 |
|
| { |
| 74 |
|
| /* queue in reverse direction? XXX */ |
| 75 |
|
| /* warn("-- adding 0x%x/%d\n", ev, prio); db_show_queue();/**/ |
| 76 |
|
| pe_ring *rg; |
| 77 |
346482 |
| rg = NQueue.next; |
| 78 |
1252539 |
▶ | while (rg->self && ((pe_event*)rg->self)->prio <= ev->prio) |
Branch: 2 branches
| Branch | True | False | Total |
| while (rg->self && ((pe_event*)rg->self)->prio <= ev->prio) |
906062 |
346477 |
1252539 |
| while (rg->self && ((pe_event*)rg->self)->prio <= ev->prio) |
906057 |
5 |
906062 |
|
| 79 |
906057 |
| rg = rg->next; |
| 80 |
346482 |
| PE_RING_ADD_BEFORE(&ev->que, rg); |
| 81 |
|
| /* warn("=\n"); db_show_queue();/**/ |
| 82 |
346482 |
| ++ActiveWatchers; |
| 83 |
|
| } |
| 84 |
|
| } |
| 85 |
|
| |
| 86 |
580277 |
| static int pe_empty_queue(int maxprio) { /**INVOKE**/ |
| 87 |
|
| pe_event *ev; |
| 88 |
580277 |
| ev = (pe_event*) NQueue.next->self; |
| 89 |
580277 |
▶ | if (ev && ev->prio < maxprio) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ev && ev->prio < maxprio) { |
346524 |
233753 |
580277 |
| if (ev && ev->prio < maxprio) { |
345752 |
772 |
346524 |
|
| 90 |
345752 |
| dequeEvent(ev); |
| 91 |
345752 |
| pe_event_invoke(ev); |
| 92 |
345737 |
| return 1; |
| 93 |
|
| } |
| 94 |
234525 |
| return 0; |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
203941 |
| /*inline*/ static void pe_multiplex(NV tm) { |
| 98 |
203941 |
▶ | if (SvIVX(DebugLevel) >= 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvIVX(DebugLevel) >= 2) { |
0 |
203941 |
203941 |
|
| 99 |
0 |
| warn("Event: multiplex %.4fs %s%s\n", tm, |
| 100 |
0 |
▶ | PE_RING_EMPTY(&NQueue)?"":"QUEUE", |
Branch: 1 branch
| Branch | True | False | Total |
| PE_RING_EMPTY(&NQueue)?"":"QUEUE", |
0 |
0 |
0 |
|
| 101 |
0 |
▶ | PE_RING_EMPTY(&Idle)?"":"IDLE"); |
Branch: 1 branch
| Branch | True | False | Total |
| PE_RING_EMPTY(&Idle)?"":"IDLE"); |
0 |
0 |
0 |
|
| 102 |
|
| } |
| 103 |
203941 |
▶ | if (!Estat.on) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!Estat.on) |
203941 |
0 |
203941 |
|
| 104 |
203941 |
| pe_sys_multiplex(tm); |
| 105 |
|
| else { |
| 106 |
0 |
| void *st = Estat.enter(-1, 0); |
| 107 |
0 |
| pe_sys_multiplex(tm); |
| 108 |
0 |
| Estat.commit(st, 0); |
| 109 |
|
| } |
| 110 |
203941 |
| } |
| 111 |
|
| |
| 112 |
1 |
| static NV pe_map_prepare(NV tm) { |
| 113 |
1 |
| pe_qcallback *qcb = (pe_qcallback*) Prepare.prev->self; |
| 114 |
2 |
▶ | while (qcb) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (qcb) { |
1 |
1 |
2 |
|
| 115 |
1 |
▶ | if (qcb->is_perl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (qcb->is_perl) { |
1 |
0 |
1 |
|
| 116 |
|
| SV *got; |
| 117 |
|
| NV when; |
| 118 |
1 |
| dSP; |
| 119 |
1 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
1 |
1 |
|
| 120 |
1 |
| PUTBACK; |
| 121 |
1 |
| perl_call_sv((SV*)qcb->callback, G_SCALAR); |
| 122 |
1 |
| SPAGAIN; |
| 123 |
1 |
| got = POPs; |
| 124 |
1 |
| PUTBACK; |
| 125 |
1 |
▶ | when = SvNOK(got) ? SvNVX(got) : SvNV(got); |
Branch: 1 branch
| Branch | True | False | Total |
| when = SvNOK(got) ? SvNVX(got) : SvNV(got); |
0 |
1 |
1 |
|
| 126 |
1 |
▶ | if (when < tm) tm = when; |
Branch: 1 branch
| Branch | True | False | Total |
| if (when < tm) tm = when; |
0 |
1 |
1 |
|
| 127 |
|
| } |
| 128 |
|
| else { /* !is_perl */ |
| 129 |
0 |
| NV got = (* (NV(*)(void*)) qcb->callback)(qcb->ext_data); |
| 130 |
0 |
▶ | if (got < tm) tm = got; |
Branch: 1 branch
| Branch | True | False | Total |
| if (got < tm) tm = got; |
0 |
0 |
0 |
|
| 131 |
|
| } |
| 132 |
1 |
| qcb = (pe_qcallback*) qcb->ring.prev->self; |
| 133 |
|
| } |
| 134 |
1 |
| return tm; |
| 135 |
|
| } |
| 136 |
|
| |
| 137 |
3 |
| static void pe_queue_pending() { |
| 138 |
3 |
| NV tm = 0; |
| 139 |
3 |
▶ | if (!PE_RING_EMPTY(&Prepare)) tm = pe_map_prepare(tm); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PE_RING_EMPTY(&Prepare)) tm = pe_map_prepare(tm); |
1 |
2 |
3 |
|
| 140 |
|
| |
| 141 |
3 |
| pe_multiplex(0); |
| 142 |
|
| |
| 143 |
3 |
| pe_timeables_check(); |
| 144 |
3 |
▶ | if (!PE_RING_EMPTY(&Check)) pe_map_check(&Check); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PE_RING_EMPTY(&Check)) pe_map_check(&Check); |
1 |
2 |
3 |
|
| 145 |
|
| |
| 146 |
3 |
| pe_signal_asynccheck(); |
| 147 |
3 |
▶ | if (!PE_RING_EMPTY(&AsyncCheck)) pe_map_check(&AsyncCheck); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PE_RING_EMPTY(&AsyncCheck)) pe_map_check(&AsyncCheck); |
1 |
2 |
3 |
|
| 148 |
3 |
| } |
| 149 |
|
| |
| 150 |
376333 |
| static int one_event(NV tm) { /**INVOKE**/ |
| 151 |
|
| /*if (SvIVX(DebugLevel) >= 4) |
| 152 |
|
| warn("Event: ActiveWatchers=%d\n", ActiveWatchers); /**/ |
| 153 |
|
| |
| 154 |
376333 |
| pe_signal_asynccheck(); |
| 155 |
376333 |
▶ | if (!PE_RING_EMPTY(&AsyncCheck)) pe_map_check(&AsyncCheck); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PE_RING_EMPTY(&AsyncCheck)) pe_map_check(&AsyncCheck); |
0 |
376333 |
376333 |
|
| 156 |
|
| |
| 157 |
376333 |
▶ | if (pe_empty_queue(StarvePrio)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pe_empty_queue(StarvePrio)) return 1; |
172394 |
203938 |
376332 |
|
| 158 |
|
| |
| 159 |
203938 |
▶ | if (!PE_RING_EMPTY(&NQueue) || !PE_RING_EMPTY(&Idle)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!PE_RING_EMPTY(&NQueue) || !PE_RING_EMPTY(&Idle)) { |
203166 |
772 |
203938 |
| if (!PE_RING_EMPTY(&NQueue) || !PE_RING_EMPTY(&Idle)) { |
1 |
203165 |
203166 |
|
| 160 |
773 |
| tm = 0; |
| 161 |
|
| } |
| 162 |
|
| else { |
| 163 |
203165 |
| NV t1 = timeTillTimer(); |
| 164 |
203165 |
▶ | if (t1 < tm) tm = t1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (t1 < tm) tm = t1; |
203036 |
129 |
203165 |
|
| 165 |
|
| } |
| 166 |
203938 |
▶ | if (!PE_RING_EMPTY(&Prepare)) tm = pe_map_prepare(tm); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PE_RING_EMPTY(&Prepare)) tm = pe_map_prepare(tm); |
0 |
203938 |
203938 |
|
| 167 |
|
| |
| 168 |
203938 |
| pe_multiplex(tm); |
| 169 |
|
| |
| 170 |
203938 |
| pe_timeables_check(); |
| 171 |
203938 |
▶ | if (!PE_RING_EMPTY(&Check)) pe_map_check(&Check); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PE_RING_EMPTY(&Check)) pe_map_check(&Check); |
0 |
203938 |
203938 |
|
| 172 |
|
| |
| 173 |
203938 |
▶ | if (tm) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tm) { |
203165 |
773 |
203938 |
|
| 174 |
203165 |
| pe_signal_asynccheck(); |
| 175 |
203165 |
▶ | if (!PE_RING_EMPTY(&AsyncCheck)) pe_map_check(&AsyncCheck); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PE_RING_EMPTY(&AsyncCheck)) pe_map_check(&AsyncCheck); |
0 |
203165 |
203165 |
|
| 176 |
|
| } |
| 177 |
|
| |
| 178 |
203938 |
▶ | if (pe_empty_queue(PE_QUEUES)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pe_empty_queue(PE_QUEUES)) return 1; |
30584 |
173342 |
203926 |
|
| 179 |
|
| |
| 180 |
0 |
| while (1) { |
| 181 |
|
| pe_watcher *wa; |
| 182 |
|
| pe_event *ev; |
| 183 |
|
| pe_ring *lk; |
| 184 |
|
| |
| 185 |
30584 |
▶ | if (PE_RING_EMPTY(&Idle)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (PE_RING_EMPTY(&Idle)) return 0; |
30569 |
15 |
30584 |
|
| 186 |
|
| |
| 187 |
15 |
| lk = Idle.prev; |
| 188 |
15 |
▶ | PE_RING_DETACH(lk); |
Branch: 1 branch
| Branch | True | False | Total |
| PE_RING_DETACH(lk); |
15 |
0 |
15 |
|
| 189 |
15 |
| wa = (pe_watcher*) lk->self; |
| 190 |
|
| |
| 191 |
|
| /* idle is not an event so CLUMP is never an option but we still need |
| 192 |
|
| to create an event to pass info to the callback */ |
| 193 |
15 |
| ev = pe_event_allocate(wa); |
| 194 |
15 |
▶ | if (!prepare_event(ev, "idle")) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!prepare_event(ev, "idle")) continue; |
0 |
15 |
15 |
|
| 195 |
|
| /* can't queueEvent because we are already missed that */ |
| 196 |
15 |
| pe_event_invoke(ev); |
| 197 |
14 |
| return 1; |
| 198 |
|
| } |
| 199 |
|
| } |
| 200 |
|
| |
| 201 |
147 |
| static void pe_reentry() { |
| 202 |
|
| pe_watcher *wa; |
| 203 |
|
| struct pe_cbframe *frp; |
| 204 |
|
| |
| 205 |
147 |
| ENTER; /* for SAVE*() macro (see below) */ |
| 206 |
|
| |
| 207 |
147 |
▶ | if (CurCBFrame < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (CurCBFrame < 0) |
29 |
118 |
147 |
|
| 208 |
29 |
| return; |
| 209 |
|
| |
| 210 |
118 |
| frp = CBFrame + CurCBFrame; |
| 211 |
118 |
| wa = frp->ev->up; |
| 212 |
|
| assert(wa->running == frp->run_id); |
| 213 |
118 |
▶ | if (Estat.on) |
Branch: 1 branch
| Branch | True | False | Total |
| if (Estat.on) |
0 |
118 |
118 |
|
| 214 |
0 |
| Estat.suspend(frp->stats); /* reversed by pe_event_postCB? */ |
| 215 |
118 |
▶ | if (WaREPEAT(wa)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (WaREPEAT(wa)) { |
118 |
0 |
118 |
|
| 216 |
118 |
▶ | if (WaREENTRANT(wa)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (WaREENTRANT(wa)) { |
116 |
2 |
118 |
|
| 217 |
116 |
▶ | if (WaACTIVE(wa) && WaINVOKE1(wa)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (WaACTIVE(wa) && WaINVOKE1(wa)) |
116 |
0 |
116 |
| if (WaACTIVE(wa) && WaINVOKE1(wa)) |
1 |
115 |
116 |
|
| 218 |
1 |
| pe_watcher_on(wa, 1); |
| 219 |
|
| } else { |
| 220 |
2 |
▶ | if (!WaSUSPEND(wa)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!WaSUSPEND(wa)) { |
2 |
0 |
2 |
|
| 221 |
|
| /* temporarily suspend non-reentrant watcher until |
| 222 |
|
| callback is finished! */ |
| 223 |
2 |
| pe_watcher_suspend(wa); |
| 224 |
2 |
| SAVEDESTRUCTOR(_resume_watcher, wa); |
| 225 |
|
| } |
| 226 |
|
| } |
| 227 |
|
| } |
| 228 |
|
| } |
| 229 |
|
| |
| 230 |
21 |
| static int safe_one_event(NV maxtm) { |
| 231 |
|
| int got; |
| 232 |
21 |
| pe_check_recovery(); |
| 233 |
21 |
| pe_reentry(); |
| 234 |
21 |
| got = one_event(maxtm); |
| 235 |
11 |
| LEAVE; /* reentry */ |
| 236 |
11 |
| return got; |
| 237 |
|
| } |
| 238 |
|
| |
| 239 |
13 |
| static void pe_unloop(SV *why) { |
| 240 |
13 |
| SV *rsv = perl_get_sv("Event::Result", 0); |
| 241 |
|
| assert(rsv); |
| 242 |
13 |
| sv_setsv(rsv, why); |
| 243 |
13 |
▶ | if (--ExitLevel < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (--ExitLevel < 0) { |
0 |
13 |
13 |
|
| 244 |
0 |
| warn("Event::unloop() to %d", ExitLevel); |
| 245 |
|
| } |
| 246 |
13 |
| } |
| 247 |
|
| |
| 248 |
1 |
| static void pe_unloop_all(SV *why) { |
| 249 |
1 |
| SV *rsv = perl_get_sv("Event::TopResult", 0); |
| 250 |
|
| assert(rsv); |
| 251 |
1 |
| sv_setsv(rsv, why); |
| 252 |
1 |
| ExitLevel = 0; |
| 253 |
1 |
| } |