static void dns_cache_utm_fn(struct dns_ctx *ctx, int timeout, void *data) {
  eventer_t e = NULL, newe = NULL;
  if(ctx == NULL) e = eventer_remove(dns_cache_timeout);
  else {
    if(timeout < 0) e = eventer_remove(dns_cache_timeout);
    else {
      newe = eventer_in_s_us(dns_invoke_timeouts, dns_ctx, timeout, 0);
    }
  }
  if(e) eventer_free(e);
  if(newe) eventer_add(newe);
  dns_cache_timeout = newe;
}
Esempio n. 2
0
static int
mtev_lua_general_init(mtev_dso_generic_t *self) {
  const char * const *module;
  int (*f)(lua_State *);
  lua_general_conf_t *conf = get_config(self);
  lua_module_closure_t *lmc = pthread_getspecific(conf->key);

  if(lmc) return 0;

  if(!lmc) {
    lmc = calloc(1, sizeof(*lmc));
    lmc->self = self;
    mtev_hash_init(&lmc->state_coros);
    pthread_setspecific(conf->key, lmc);
  }

  if(!conf->module || !conf->function) {
    mtevL(nlerr, "lua_general cannot be used without module and function config\n");
    return -1;
  }

  lmc->resume = lua_general_resume;
  lmc->owner = pthread_self();
  lmc->eventer_id = eventer_is_loop(lmc->owner);
  lmc->lua_state = mtev_lua_open(self->hdr.name, lmc,
                                 conf->script_dir, conf->cpath);
  mtevL(nldeb, "lua_general opening state -> %p\n", lmc->lua_state);
  if(lmc->lua_state == NULL) {
    mtevL(mtev_error, "lua_general could not add general functions\n");
    return -1;
  }
  luaL_openlib(lmc->lua_state, "mtev", general_lua_funcs, 0);
  /* Load some preloads */

  for(module = conf->Cpreloads; module && *module; module++) {
    int len;
    char *symbol = NULL;
    len = strlen(*module) + strlen("luaopen_");
    symbol = malloc(len+1);
    if(!symbol) mtevL(nlerr, "Failed to preload %s: malloc error\n", *module);
    else {
      snprintf(symbol, len+1, "luaopen_%s", *module);
#ifdef RTLD_DEFAULT
      f = dlsym(RTLD_DEFAULT, symbol);
#else
      f = dlsym((void *)0, symbol);
#endif
      if(!f) mtevL(nlerr, "Failed to preload %s: %s not found\n", *module, symbol);
      else f(lmc->lua_state);
    }
    free(symbol);
  }

  lmc->pending = calloc(1, sizeof(*lmc->pending));
  mtev_hash_init(lmc->pending);

  if(conf->booted) return true;
  conf->booted = mtev_true;
  eventer_add_in_s_us(dispatch_general, self, 0, 0);

  if(conf->concurrent) {
    int i = 1;
    pthread_t tgt, thr;
    thr = eventer_choose_owner(i++);
    do {
      eventer_t e = eventer_in_s_us(dispatch_general, self, 0, 0);
      tgt = eventer_choose_owner(i++);
      eventer_set_owner(e, tgt);
      eventer_add(e);
    } while(!pthread_equal(thr,tgt));
  }
  return 0;
}