void udelay_sched(uint64_t usec) { struct timer_chain *tchain = &per_cpu_info[core_id()].tchain; struct alarm_waiter a_waiter; init_awaiter(&a_waiter, 0); set_awaiter_rel(&a_waiter, usec); set_alarm(tchain, &a_waiter); sleep_on_awaiter(&a_waiter); }
/* 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); }
/** * Timer callback function that calls tcp_tmr() and reschedules itself. * * @param arg unused argument */ static void tcpip_tcp_timer(struct alarm_waiter *aw) { /* call TCP timer handler */ tcp_tmr(); if (aw == NULL) { // need to allocate a fresh waiter } /* timer still needed? */ if (tcp_active_pcbs || tcp_tw_pcbs) { struct timer_chain *tchain = &per_cpu_info[core_id()].tchain; set_awaiter_rel(aw, TCP_TMR_INTERVAL); set_alarm(tchain, aw); tcpip_tcp_timer_active = true; } else { /* disable timer */ tcpip_tcp_timer_active = false; } }
/** * Create a one-shot timer (aka timeout). Timeouts are processed in the * following cases: * @param msecs time in milliseconds after that the timer should expire * @param handler callback function to call when msecs have elapsed, this handler takes no arguments */ struct alarm_waiter* sys_timeout(uint32_t msecs, sys_timeout_handler func, struct alarm_waiter* waiter) { if (waiter == NULL) { waiter = kmalloc(sizeof(struct alarm_waiter), 0); } struct timer_chain *tchain = &per_cpu_info[core_id()].tchain; // initialize the waiter init_awaiter(waiter, func); // explicitly setting the waiter data to be null, since we do not need it in our handler waiter->data = NULL; // set waiting time set_awaiter_rel(waiter, 1000 * msecs); // attach the waiter to a chain to wait set_alarm(tchain, waiter); // XXX: when to destroy the waiter, handlers responsibility? return waiter; }
/* Helper: Sets up a timer tick on the calling core to go off 10 msec from now. * This assumes the calling core is an LL core, etc. */ static void set_ksched_alarm(void) { set_awaiter_rel(&ksched_waiter, TIMER_TICK_USEC); set_alarm(&per_cpu_info[core_id()].tchain, &ksched_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); }