Skip to content

Commit

Permalink
Merge pull request luvit#335 from zhaozg/fix-steps
Browse files Browse the repository at this point in the history
Fix steps
  • Loading branch information
zhaozg committed Jun 4, 2019
2 parents afd2072 + 54a9cf8 commit 788fa44
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/async.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static uv_async_t* luv_check_async(lua_State* L, int index) {
static void luv_async_cb(uv_async_t* handle) {
lua_State* L = luv_state(handle->loop);
luv_handle_t* data = (luv_handle_t*)handle->data;
int n = luv_thread_arg_push(L, (const luv_thread_arg_t*)data->extra, 0);
int n = luv_thread_arg_push(L, (luv_thread_arg_t*)data->extra, 0);
luv_call_callback(L, data, LUV_ASYNC, n);
luv_thread_arg_clear(L, (luv_thread_arg_t*)data->extra, 0);
}
Expand Down
2 changes: 2 additions & 0 deletions src/lhandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ typedef struct {
void* extra;
} luv_handle_t;

#ifdef LUV_SOURCE
/* Setup the handle at the top of the stack */
static luv_handle_t* luv_setup_handle(lua_State* L);

Expand All @@ -69,5 +70,6 @@ static void luv_find_handle(lua_State* L, luv_handle_t* data);

/* Unref the handle from the lua world, allowing it to GC */
static void luv_unref_handle(lua_State* L, luv_handle_t* data);
#endif

#endif
2 changes: 2 additions & 0 deletions src/lreq.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ typedef struct {
void* data; /* extra data */
} luv_req_t;

#ifdef LUV_SOURCE
/* Used in the top of a setup function to check the arg
and ref the callback to an integer.
*/
Expand All @@ -39,5 +40,6 @@ static luv_req_t* luv_setup_req(lua_State* L, int ref);
static void luv_fulfill_req(lua_State* L, luv_req_t* data, int nargs);

static void luv_cleanup_req(lua_State* L, luv_req_t* data);
#endif

#endif
12 changes: 8 additions & 4 deletions src/lthreadpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@
#define LUV_THREAD_MAXNUM_ARG 9

typedef struct {
/* only support LUA_TNIL, LUA_TBOOLEAN, LUA_TLIGHTUSERDATA, LUA_TNUMBER, LUA_TSTRING*/
// support basic lua type LUA_TNIL, LUA_TBOOLEAN, LUA_TNUMBER, LUA_TSTRING
// and support uv_handle_t userdata
int type;
union
{
lua_Number num;
int boolean;
void* userdata;
void* userdata; // luv private scope uv_handle_t
struct {
const char* base;
size_t len;
} str;
} val;
int ref; // ref of uv_handle_t
} luv_val_t;

typedef struct {
Expand All @@ -47,10 +49,12 @@ typedef struct {
#endif

//LUV flags thread support userdata handle
#define LUVF_THREAD_UHANDLE 1
#define LUVF_THREAD_UHANDLE 1

#ifdef LUV_SOURCE
static int luv_thread_arg_set(lua_State* L, luv_thread_arg_t* args, int idx, int top, int flags);
static int luv_thread_arg_push(lua_State* L, const luv_thread_arg_t* args, int flags);
static int luv_thread_arg_push(lua_State* L, luv_thread_arg_t* args, int flags);
static void luv_thread_arg_clear(lua_State* L, luv_thread_arg_t* args, int flags);
#endif

#endif //LUV_LTHREADPOOL_H
2 changes: 2 additions & 0 deletions src/luv.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#if (LUA_VERSION_NUM != 503)
#include "c-api/compat-5.3.h"
#endif
#define LUV_SOURCE
#include "luv.h"

#include "util.c"
#include "lhandle.c"
#include "lreq.c"
Expand Down
2 changes: 2 additions & 0 deletions src/luv.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ LUALIB_API int luaopen_luv (lua_State *L);
#include "lhandle.h"
#include "lreq.h"

#ifdef LUV_SOURCE
/* From stream.c */
static uv_stream_t* luv_check_stream(lua_State* L, int index);
static void luv_alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
Expand All @@ -94,6 +95,7 @@ static int luv_sock_string_to_num(const char* string);
static const char* luv_sock_num_to_string(const int num);
static int luv_sig_string_to_num(const char* string);
static const char* luv_sig_num_to_string(const int num);
#endif

typedef lua_State* (*luv_acquire_vm)();
typedef void (*luv_release_vm)(lua_State* L);
Expand Down
62 changes: 25 additions & 37 deletions src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ static int luv_thread_arg_set(lua_State* L, luv_thread_arg_t* args, int idx, int
case LUA_TNUMBER:
arg->val.num = lua_tonumber(L, i);
break;
case LUA_TLIGHTUSERDATA:
arg->val.userdata = lua_touserdata(L, i);
break;
case LUA_TSTRING:
{
const char* p = lua_tolstring(L, i, &arg->val.str.len);
Expand All @@ -86,6 +83,7 @@ static int luv_thread_arg_set(lua_State* L, luv_thread_arg_t* args, int idx, int
case LUA_TUSERDATA:
if (flags & LUVF_THREAD_UHANDLE) {
arg->val.userdata = luv_check_handle(L, i);
arg->ref = LUA_NOREF;
break;
}

Expand All @@ -108,24 +106,22 @@ static void luv_thread_arg_clear(lua_State* L, luv_thread_arg_t* args, int flags
return;

for (i = 0; i < args->argc; i++) {
const luv_val_t* arg = args->argv + i;
luv_val_t* arg = args->argv + i;
switch (arg->type) {
case LUA_TSTRING:
free((void*)arg->val.str.base);
break;
case LUA_TUSERDATA:
if (flags & LUVF_THREAD_UHANDLE) {
//unref to metatable, avoid run __gc
lua_pushlightuserdata(L, arg->val.userdata);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_rawgeti(L, LUA_REGISTRYINDEX, arg->ref);
lua_pushnil(L);
lua_setmetatable(L, -2);
lua_pop(L, 1);

//unref
lua_pushlightuserdata(L, arg->val.userdata);
lua_pushnil(L);
lua_rawset(L, LUA_REGISTRYINDEX);
luaL_unref(L, LUA_REGISTRYINDEX, arg->ref);
arg->ref = LUA_NOREF;
break;
}
default:
Expand All @@ -136,41 +132,17 @@ static void luv_thread_arg_clear(lua_State* L, luv_thread_arg_t* args, int flags
args->argc = 0;
}

static void luv_thread_setup_handle(lua_State* L, uv_handle_t* handle) {
*(uv_handle_t**) lua_newuserdata(L, sizeof(void*)) = handle;

#define XX(uc, lc) case UV_##uc: \
luaL_getmetatable(L, "uv_"#lc); \
break;
switch (handle->type) {
UV_HANDLE_TYPE_MAP(XX)
default:
luaL_error(L, "Unknown handle type");
}
#undef XX

lua_setmetatable(L, -2);

//ref up of userdata parameter
lua_pushlightuserdata(L, handle);
lua_pushvalue(L, -2);
lua_rawset(L, LUA_REGISTRYINDEX);
}

static int luv_thread_arg_push(lua_State* L, const luv_thread_arg_t* args, int flags) {
static int luv_thread_arg_push(lua_State* L, luv_thread_arg_t* args, int flags) {
int i = 0;
while (i < args->argc) {
const luv_val_t* arg = args->argv + i;
luv_val_t* arg = args->argv + i;
switch (arg->type) {
case LUA_TNIL:
lua_pushnil(L);
break;
case LUA_TBOOLEAN:
lua_pushboolean(L, arg->val.boolean);
break;
case LUA_TLIGHTUSERDATA:
lua_pushlightuserdata(L, arg->val.userdata);
break;
case LUA_TNUMBER:
lua_pushnumber(L, arg->val.num);
break;
Expand All @@ -180,7 +152,23 @@ static int luv_thread_arg_push(lua_State* L, const luv_thread_arg_t* args, int f
case LUA_TUSERDATA:
if (flags & LUVF_THREAD_UHANDLE)
{
luv_thread_setup_handle(L, (uv_handle_t*)arg->val.userdata);
uv_handle_t* handle = (uv_handle_t*)arg->val.userdata;
*(uv_handle_t**) lua_newuserdata(L, sizeof(void*)) = handle;

#define XX(uc, lc) case UV_##uc: \
luaL_getmetatable(L, "uv_"#lc); \
break;
switch (handle->type) {
UV_HANDLE_TYPE_MAP(XX)
default:
luaL_error(L, "Unknown handle type");
}
#undef XX
lua_setmetatable(L, -2);

//ref up of userdata parameter
lua_pushvalue(L, -1);
arg->ref = luaL_ref(L, LUA_REGISTRYINDEX);
break;
}
default:
Expand Down Expand Up @@ -257,7 +245,7 @@ static void luv_thread_cb(void* varg) {
//push lua function, thread entry
if (luaL_loadbuffer(L, thd->code, thd->len, "=thread") == 0) {
int i, ret;

//push parameter for real thread function
i = luv_thread_arg_push(L, &thd->arg, LUVF_THREAD_UHANDLE);
assert(i == thd->arg.argc);
Expand Down
3 changes: 3 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
(((major)<<16 | (minor)<<8 | (patch)) >= UV_VERSION_HEX)

void luv_stack_dump(lua_State* L, const char* name);

#ifdef LUV_SOURCE
static int luv_error(lua_State* L, int ret);
static void luv_status(lua_State* L, int status);
#endif

#endif
17 changes: 8 additions & 9 deletions src/work.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ typedef struct {
size_t len;

uv_async_t async;
int async_cb; /* ref, run in main, call when async message received, NYI */
int async_cb; /* ref, run in main, call when async message received */
int after_work_cb; /* ref, run in main ,call after work cb*/
} luv_work_ctx_t;

Expand All @@ -32,6 +32,7 @@ typedef struct {
luv_work_ctx_t* ctx;

luv_thread_arg_t arg;
int ref;
} luv_work_t;

static uv_key_t L_key;
Expand Down Expand Up @@ -84,7 +85,7 @@ static void luv_work_cb(uv_work_t* req)
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)
{
Expand Down Expand Up @@ -172,9 +173,8 @@ static void luv_after_work_cb(uv_work_t* req, int status) {
lua_pop(L, 1);

//ref down to ctx, up in luv_queue_work()
lua_pushlightuserdata(L, work);
lua_pushnil(L);
lua_rawset(L, LUA_REGISTRYINDEX);
luaL_unref(L, LUA_REGISTRYINDEX, work->ref);
work->ref = LUA_NOREF;

luv_thread_arg_clear(NULL, &work->arg, 0);
free(work);
Expand Down Expand Up @@ -248,7 +248,7 @@ static int luv_queue_work(lua_State* L) {
luv_work_t* work = (luv_work_t*)malloc(sizeof(*work));
int ret;

luv_thread_arg_set(L, &work->arg, 2, top, 0); //clear in sub threads,luv_work_cb,
luv_thread_arg_set(L, &work->arg, 2, top, 0); //clear in sub threads,luv_work_cb
work->ctx = ctx;
work->work.data = work;
ret = uv_queue_work(luv_loop(L), &work->work, luv_work_cb, luv_after_work_cb);
Expand All @@ -257,10 +257,9 @@ static int luv_queue_work(lua_State* L) {
return luv_error(L, ret);
}

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

lua_pushboolean(L, 1);
return 1;
Expand Down

0 comments on commit 788fa44

Please sign in to comment.