/* Like sleep, but it will timeout in 'usec' microseconds. */ void rendez_sleep_timeout(struct rendez *rv, int (*cond)(void*), void *arg, uint64_t usec) { int8_t irq_state = 0; struct alarm_waiter awaiter; struct cv_lookup_elm cle; struct timer_chain *pcpui_tchain = &per_cpu_info[core_id()].tchain; if (!usec) return; /* Doing this cond check early, but then unlocking again. Mostly just to * avoid weird issues with the CV lock and the alarm tchain lock. */ cv_lock_irqsave(&rv->cv, &irq_state); if (cond(arg)) { cv_unlock_irqsave(&rv->cv, &irq_state); return; } cv_unlock_irqsave(&rv->cv, &irq_state); /* The handler will call rendez_wake, but won't mess with the condition * state. It's enough to break us out of cv_wait() to see .on_tchain. */ init_awaiter(&awaiter, rendez_alarm_handler); awaiter.data = rv; set_awaiter_rel(&awaiter, usec); /* Set our alarm on this cpu's tchain. Note that when we sleep in cv_wait, * we could be migrated, and later on we could be unsetting the alarm * remotely. */ set_alarm(pcpui_tchain, &awaiter); cv_lock_irqsave(&rv->cv, &irq_state); __reg_abortable_cv(&cle, &rv->cv); /* We could wake early for a few reasons. Legit wakeups after a changed * condition (and we should exit), other alarms with different timeouts (and * we should go back to sleep), etc. Note it is possible for our alarm to * fire immediately upon setting it: before we even cv_lock. */ while (!cond(arg) && awaiter.on_tchain) { if (should_abort(&cle)) { cv_unlock_irqsave(&rv->cv, &irq_state); unset_alarm(pcpui_tchain, &awaiter); dereg_abortable_cv(&cle); error(EINTR, "syscall aborted"); } cv_wait(&rv->cv); cpu_relax(); } cv_unlock_irqsave(&rv->cv, &irq_state); dereg_abortable_cv(&cle); /* Turn off our alarm. If it already fired, this is a no-op. Note this * could be cross-core. */ unset_alarm(pcpui_tchain, &awaiter); }
/** * Called from TCP_REG when registering a new PCB: * the reason is to have the TCP timer only running when * there are active (or time-wait) PCBs. */ void tcp_timer_needed(void) { /* timer is off but needed again? */ if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) { /* enable and start timer */ tcpip_tcp_timer_active = true; tcp_waiter = sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, tcp_waiter); } else { if (tcp_waiter != NULL) { struct timer_chain *tchain = &per_cpu_info[core_id()].tchain; unset_alarm(tchain, tcp_waiter); } } }
void rcvr(int fd, int msglen, int interval, int nmsg) { int i, n, munged; uint16_t x; int64_t now; uint8_t *buf = malloc(BUFSIZE); struct icmphdr *icmp; Req *r; struct alarm_waiter waiter; init_awaiter(&waiter, alarm_abort_sysc); waiter.data = current_uthread; sum = 0; while(lostmsgs+rcvdmsgs < nmsg){ /* arm to wake ourselves if the read doesn't connect in time */ set_awaiter_rel(&waiter, 1000 * ((nmsg - lostmsgs - rcvdmsgs) * interval + waittime)); set_alarm(&waiter); n = read(fd, buf, BUFSIZE); /* cancel immediately, so future syscalls don't get aborted */ unset_alarm(&waiter); now = read_tsc(); if(n <= 0){ /* read interrupted - time to go */ /* Faking time being a minute in the future, so clean marks our * message as lost. Note this will also end up cancelling any other * pending replies that would have expired by then. Whatever. */ clean(0, now + MINUTETSC, NULL); continue; } if(n < msglen){ printf("bad len %d/%d\n", n, msglen); continue; } icmp = geticmp(buf); munged = 0; for(i = proto->iphdrsz + ICMP_HDRSIZE; i < msglen; i++) if(buf[i] != (uint8_t)i) munged++; if(munged) printf("corrupted reply\n"); x = nhgets(icmp->seq); if(icmp->type != proto->echoreply || icmp->code != 0) { printf("bad type/code/sequence %d/%d/%d (want %d/%d/%d)\n", icmp->type, icmp->code, x, proto->echoreply, 0, x); continue; } clean(x, now, buf); } spin_pdr_lock(&listlock); for(r = first; r; r = r->next) if(r->replied == 0) lostmsgs++; spin_pdr_unlock(&listlock); if(!quiet && lostmsgs) printf("%d out of %d messages lost\n", lostmsgs, lostmsgs+rcvdmsgs); }