Beispiel #1
0
static lua_module_closure_t *noit_lua_setup_lmc(noit_module_t *mod, const char **obj) {
  lua_module_closure_t *lmc;
  struct module_conf *mc;
  struct module_tls_conf *mtlsc;
  mc = mtev_image_get_userdata(&mod->hdr);
  if(obj) *obj = mc->object;
  lmc = pthread_getspecific(mc->c->key);
  if(lmc == NULL) {
    int rv;
    lmc = calloc(1, sizeof(*lmc));
    lmc->pending = calloc(1, sizeof(*lmc->pending));
    mtev_hash_init(lmc->pending);
    lmc->owner = pthread_self();
    lmc->resume = noit_lua_check_resume;
    pthread_setspecific(mc->c->key, lmc);
    mtevL(nldeb, "lua_state[%s]: new state\n", mod->hdr.name);
    lmc->lua_state = mtev_lua_open(mod->hdr.name, lmc,
                                   mc->c->script_dir, mc->c->cpath);
    require(lmc->lua_state, rv, noit);
  }
  mtlsc = __get_module_tls_conf(&mod->hdr);
  if(!mtlsc->loaded) {
    if(mod->hdr.onload(&mod->hdr) == -1) {
      return NULL;
    }
    mtlsc->loaded = 1;
  }
  mtevL(nldeb, "lua_state[%s]: %p\n", mod->hdr.name, lmc->lua_state);
  return lmc;
}
Beispiel #2
0
static int
mtev_lua_web_driver_init(mtev_dso_generic_t *self) {
  lua_web_conf_t *conf = get_config(self);
  lua_module_closure_t *lmc = &conf->lmc;
  lmc->resume = lua_web_resume;
  lmc->owner = pthread_self();
  lmc->lua_state = mtev_lua_open(self->hdr.name, lmc,
                                 conf->script_dir, conf->cpath);
  if(lmc->lua_state == NULL) return -1;
  lmc->pending = calloc(1, sizeof(*lmc->pending));
  dso_post_init_hook_register("web_lua", late_stage_rest_register, NULL);
  return 0;
}
Beispiel #3
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;
}