void wait_for_event(struct state *state) { s64 event_usecs = script_time_to_live_time_usecs( state, state->event->time_usecs); DEBUGP("waiting until %lld -- now is %lld\n", event_usecs, now_usecs()); while (1) { const s64 wait_usecs = event_usecs - now_usecs(); if (wait_usecs <= 0) break; /* If we're waiting a long time, and we are on an OS * that we know has a fine-grained usleep(), then * usleep() instead of spinning on the CPU. */ #ifdef linux /* Since the scheduler may not wake us up precisely * when we tell it to, sleep until just before the * event we're waiting for and then spin. */ if (wait_usecs > MAX_SPIN_USECS) { run_unlock(state); usleep(wait_usecs - MAX_SPIN_USECS); run_lock(state); } #endif /* At this point we should only have a millisecond or * two to wait, so we spin. */ } check_event_time(state, now_usecs()); }
struct state *state_new(struct config *config, struct script *script, struct netdev *netdev) { struct state *state = calloc(1, sizeof(struct state)); if (pthread_mutex_init(&state->mutex, NULL) != 0) die_perror("pthread_mutex_init"); run_lock(state); state->config = config; state->script = script; state->netdev = netdev; state->packets = packets_new(); state->syscalls = syscalls_new(state); state->code = code_new(config); state->sockets = NULL; return state; }