Skip to content

Commit fe4130e

Browse files
committed
feature: ngx.thread.spawn(f, ...) can now take multiple arguments for the "light thread" coroutine's first resume.
1 parent 56b9b8a commit fe4130e

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

src/ngx_http_lua_uthread.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ ngx_http_lua_inject_uthread_api(ngx_log_t *log, lua_State *L)
3333
static int
3434
ngx_http_lua_uthread_spawn(lua_State *L)
3535
{
36+
int n;
3637
ngx_http_request_t *r;
3738
ngx_http_lua_ctx_t *ctx;
3839
ngx_http_lua_co_ctx_t *coctx = NULL;
3940

41+
n = lua_gettop(L);
42+
4043
ngx_http_lua_coroutine_create_helper(L, &r, &ctx, &coctx);
4144

4245
/* anchor the newly created coroutine into the Lua registry */
@@ -47,6 +50,11 @@ ngx_http_lua_uthread_spawn(lua_State *L)
4750
coctx->co_ref = luaL_ref(L, -2);
4851
lua_pop(L, 1);
4952

53+
if (n > 1) {
54+
lua_replace(L, 1);
55+
lua_xmove(L, coctx->co, n - 1);
56+
}
57+
5058
coctx->is_uthread = 1;
5159
ctx->uthreads++;
5260

src/ngx_http_lua_util.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,8 @@ ngx_http_lua_run_thread(lua_State *L, ngx_http_request_t *r,
10351035
"lua user thread resume");
10361036

10371037
ctx->co_op = NGX_HTTP_LUA_USER_CORO_NOP;
1038-
nrets = 0;
1038+
nrets = lua_gettop(ctx->cur_co_ctx->co) - 1;
1039+
dd("nrets = %d", nrets);
10391040

10401041
break;
10411042

t/093-uthread-spawn.t

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,3 +1095,37 @@ body: hello world)$
10951095
--- no_error_log
10961096
[error]
10971097

1098+
1099+
1100+
=== TEST 24: simple user thread with args
1101+
--- config
1102+
location /lua {
1103+
content_by_lua '
1104+
function f(a, b)
1105+
ngx.say("hello ", a, " and ", b)
1106+
end
1107+
1108+
ngx.say("before")
1109+
ngx.thread.spawn(f, "foo", 3.14)
1110+
ngx.say("after")
1111+
';
1112+
}
1113+
--- request
1114+
GET /lua
1115+
--- stap2 eval: $::StapScript
1116+
--- stap eval: $::GCScript
1117+
--- stap_out
1118+
create 2 in 1
1119+
spawn user thread 2 in 1
1120+
terminate 2: ok
1121+
terminate 1: ok
1122+
delete thread 2
1123+
delete thread 1
1124+
1125+
--- response_body
1126+
before
1127+
hello foo and 3.14
1128+
after
1129+
--- no_error_log
1130+
[error]
1131+

0 commit comments

Comments
 (0)