Example #1
0
static void luv_work_cb(uv_work_t* req)
{
    uv_thread_t tid = uv_thread_self();
    luv_work_t* work = req->data;
    luv_work_ctx_t* ctx = work->ctx;
    lua_State *L = uv_key_get(&L_key);
    int top;
    if (L == NULL) {
        /* should vm reuse in pool? */
        L = acquire_vm_cb();

        uv_key_set(&L_key, L);
    }
    top = lua_gettop(L);
    if (luaL_loadbuffer(L, ctx->code, ctx->len, "=pool") == 0)
    {
        int i = luv_thread_arg_push(L, &work->arg);
        if (lua_pcall(L, i, LUA_MULTRET, 0)) {
            fprintf(stderr, "Uncaught Error in thread: %s\n", lua_tostring(L, -1));
        }
        luv_thread_arg_set(L, &work->arg, top, lua_gettop(L));
    } else {
        fprintf(stderr, "Uncaught Error: %s\n", lua_tostring(L, -1));
    }

    //release_vm_cb(L);
}
Example #2
0
static void luv_work_cb(uv_work_t* req)
{
  uv_thread_t tid = uv_thread_self();
  luv_work_t* work = req->data;
  luv_work_ctx_t* ctx = work->ctx;
  lua_State *L = uv_key_get(&L_key);
  int top;
  if (L == NULL) {
    /* vm reuse in threadpool */
    L = acquire_vm_cb();
    uv_key_set(&L_key, L);
  }

  top = lua_gettop(L);
  lua_pushlstring(L, ctx->code, ctx->len);
  lua_rawget(L, LUA_REGISTRYINDEX);
  if (lua_isnil(L, -1))
  {
    lua_pop(L, 1);
    
    lua_pushlstring(L, ctx->code, ctx->len);
    if (luaL_loadbuffer(L, ctx->code, ctx->len, "=pool") != 0)
    {
      fprintf(stderr, "Uncaught Error: %s\n", lua_tostring(L, -1));
      lua_pop(L, 2);

      lua_pushnil(L);
    } else
    {
      lua_pushvalue(L, -1);
      lua_insert(L, lua_gettop(L) - 2);
      lua_rawset(L, LUA_REGISTRYINDEX);
    }
  }

  if (lua_isfunction(L, -1))
  {
    int i = luv_thread_arg_push(L, &work->arg);
    if (lua_pcall(L, i, LUA_MULTRET, 0)) {
      fprintf(stderr, "Uncaught Error in thread: %s\n", lua_tostring(L, -1));
    }
    luv_thread_arg_clear(&work->arg);
    luv_thread_arg_set(L, &work->arg, top + 1, lua_gettop(L));
    lua_settop(L, top);
  } else {
    fprintf(stderr, "Uncaught Error: %s can't be work entry\n", 
      lua_typename(L, lua_type(L,-1)));
  }
}