Ejemplo n.º 1
0
static int luv_queue_channel_send(lua_State* L)
{
	int type, ret;
	luv_msg_t* msg;
	luv_queue_t* queue = luv_queue_check_queue_t(L);
	if (lua_gettop(L) < 2) {
		luv_queue_lua_usage(L, queue_usage_send);
	}

	msg = (luv_msg_t*)malloc(sizeof(luv_msg_t));
	ret = luv_thread_arg_set(L, &msg->arg, 2, lua_gettop(L), 1);
	// printf("chan_send: %d\r\n", ret);

	ret = luv_queue_send(queue, msg, 0);
	if (!ret) {
		luv_queue_message_release(msg);

	} else {
		if (queue->async_cb != LUA_REFNIL) {
			uv_async_send(&(queue->async));
		}
	}

	lua_pushboolean(L, ret);
	return 1;
}
Ejemplo n.º 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) {
        /* 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);
}
Ejemplo n.º 3
0
static int luv_queue_work(lua_State* L) {
    luv_work_ctx_t* ctx;
    luv_work_t* work;
    int ret;
    int top = lua_gettop(L);
    ctx = luv_check_work_ctx(L, 1);   // ctx should ref up
    work = lua_newuserdata(L, sizeof(*work)); //work should ref up
    memset(work, 0, sizeof(*work));
    luv_thread_arg_set(L, &work->arg, 2, top);
    work->ctx = ctx;
    work->work.data = work;
    ret = uv_queue_work(luv_loop(L), &work->work, luv_work_cb, luv_after_work_cb);
    if (ret < 0) return luv_error(L, ret);

    lua_pushlightuserdata(L, work->ctx);
    lua_pushvalue(L, 1);
    lua_settable(L, LUA_REGISTRYINDEX);

    lua_pushlightuserdata(L, work);
    lua_pushvalue(L, -2);
    lua_settable(L, LUA_REGISTRYINDEX);

    lua_pushboolean(L, 1);
    return 1;
}
Ejemplo n.º 4
0
Archivo: thread.c Proyecto: luvit/luv
static int luv_new_thread(lua_State* L) {
  int ret;
  size_t len;
  const char* buff;
  luv_thread_t* thread;
  int cbidx = 1;
#if LUV_UV_VERSION_GEQ(1, 26, 0)
  uv_thread_options_t options;
  options.flags = UV_THREAD_NO_FLAGS;
#endif
  thread = (luv_thread_t*)lua_newuserdata(L, sizeof(*thread));
  memset(thread, 0, sizeof(*thread));
  luaL_getmetatable(L, "uv_thread");
  lua_setmetatable(L, -2);

#if LUV_UV_VERSION_GEQ(1, 26, 0)
  if (lua_type(L, 1) == LUA_TTABLE)
  {
    cbidx++;

    lua_getfield(L, 1, "stack_size");
    if (!lua_isnil(L, -1))
    {
      options.flags |= UV_THREAD_HAS_STACK_SIZE;
      if (lua_isnumber(L, -1)) {
        options.stack_size = lua_tointeger(L, -1);
      }
      else {
        return luaL_argerror(L, 1, "stack_size option must be a number if set");
      }
    }
    lua_pop(L, 1);
  }
#endif

  buff = luv_thread_dumped(L, cbidx, &len);

  //clear in luv_thread_gc or in child threads
  thread->argc = luv_thread_arg_set(L, &thread->arg, cbidx+1, lua_gettop(L) - 1, LUVF_THREAD_UHANDLE);
  thread->len = len;
  thread->code = (char*)malloc(thread->len);
  memcpy(thread->code, buff, len);

#if LUV_UV_VERSION_GEQ(1, 26, 0)
  ret = uv_thread_create_ex(&thread->handle, &options, luv_thread_cb, thread);
#else
  ret = uv_thread_create(&thread->handle, luv_thread_cb, thread);
#endif
  if (ret < 0) return luv_error(L, ret);

  return 1;
}
Ejemplo n.º 5
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)));
  }
}
Ejemplo n.º 6
0
static int luv_new_thread(lua_State* L) {
  int ret;
  size_t len;
  const char* buff;
  luv_thread_t* thread;
  thread = lua_newuserdata(L, sizeof(*thread));
  memset(thread, 0, sizeof(*thread));
  luaL_getmetatable(L, "uv_thread");
  lua_setmetatable(L, -2);

  buff = luv_thread_dumped(L, 1, &len);

  thread->argc = luv_thread_arg_set(L, &thread->arg, 2, lua_gettop(L) - 1, 1);
  thread->len = len;
  thread->code = malloc(thread->len);
  memcpy(thread->code, buff, len);

  ret = uv_thread_create(&thread->handle, luv_thread_cb, thread);
  if (ret < 0) return luv_error(L, ret);

  return 1;
}
Ejemplo n.º 7
0
static int luv_queue_work(lua_State* L) {
  int top = lua_gettop(L);
  luv_work_ctx_t* ctx = luv_check_work_ctx(L, 1);
  luv_work_t* work = malloc(sizeof(*work));
  int ret;

  luv_thread_arg_set(L, &work->arg, 2, top);
  work->ctx = ctx;
  work->work.data = work;
  ret = uv_queue_work(luv_loop(L), &work->work, luv_work_cb, luv_after_work_cb);
  if (ret < 0) {
    free(work);
    return luv_error(L, ret);
  }

  //ref up to ctx 
  lua_pushlightuserdata(L, work);
  lua_pushvalue(L, 1);
  lua_rawset(L, LUA_REGISTRYINDEX);

  lua_pushboolean(L, 1);
  return 1;
}