void lw_timer_start (lw_timer ctx, long interval) { lw_timer_stop (ctx); ctx->started = lw_true; lw_pump_add_user (ctx->pump); #ifdef USE_KQUEUE if (ctx->pump->def == &def_eventpump) { struct kevent event; EV_SET (&event, (uintptr_t) ctx, EVFILT_TIMER, EV_ADD | EV_ENABLE | EV_CLEAR, 0, interval, ctx); if (kevent (((lw_eventpump) ctx->pump)->queue, &event, 1, 0, 0, 0) == -1) { lwp_trace ("Timer: Failed to add timer to kqueue: %s", strerror (errno)); return; } } else { ctx->interval = interval; lw_thread_start (ctx->timer_thread, ctx); } #else #ifdef _lacewing_use_timerfd struct itimerspec spec; spec.it_interval.tv_sec = interval / 1000; spec.it_interval.tv_nsec = (interval % 1000) * 1000000; spec.it_value.tv_sec = 0; spec.it_value.tv_nsec = 1; timerfd_settime (ctx->fd, 0, &spec, 0); #else ctx->interval = interval; lw_thread_start (ctx->timer_thread, ctx); #endif #endif }
void lwp_addr_init (lw_addr ctx, const char * hostname, const char * service, long hints) { char * it; memset (ctx, 0, sizeof (*ctx)); ctx->resolver_thread = lw_thread_new ("resolver", (void *) resolver); ctx->hints = hints; ctx->hostname_to_free = ctx->hostname = strdup (hostname); while (isspace (*ctx->hostname)) ++ ctx->hostname; while (isspace (ctx->hostname [strlen (ctx->hostname) - 1])) ctx->hostname [strlen (ctx->hostname) - 1] = 0; for (it = ctx->hostname; *it; ++ it) { if (it [0] == ':' && it [1] == '/' && it [2] == '/') { *it = 0; service = ctx->hostname; ctx->hostname = it + 3; } if (*it == ':') { /* an explicit port overrides the service name */ service = it + 1; *it = 0; } } lwp_copy_string (ctx->service, service, sizeof (ctx->service)); lw_thread_join (ctx->resolver_thread); /* block if the thread is already running */ lw_thread_start (ctx->resolver_thread, ctx); }