Skip to content

Commit 06d8430

Browse files
committed
ngx threads: replace list with rbtree
1 parent b1b1504 commit 06d8430

File tree

4 files changed

+166
-94
lines changed

4 files changed

+166
-94
lines changed

src/ngx_http_lua_common.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,13 @@ typedef struct {
440440
} ngx_http_lua_vm_state_t;
441441

442442

443+
typedef struct ngx_http_user_co_ctx_s {
444+
ngx_rbtree_t rbtree;
445+
ngx_rbtree_node_t sentinel;
446+
ngx_pool_t *pool;
447+
} ngx_http_user_co_ctx_t;
448+
449+
443450
typedef struct ngx_http_lua_ctx_s {
444451
/* for lua_coce_cache off: */
445452
ngx_http_lua_vm_state_t *vm_state;
@@ -449,8 +456,7 @@ typedef struct ngx_http_lua_ctx_s {
449456

450457
ngx_http_lua_co_ctx_t *cur_co_ctx; /* co ctx for the current coroutine */
451458

452-
/* FIXME: we should use rbtree here to prevent O(n) lookup overhead */
453-
ngx_list_t *user_co_ctx; /* coroutine contexts for user
459+
ngx_http_user_co_ctx_t *user_co_ctx; /* coroutine contexts for user
454460
coroutines */
455461

456462
ngx_http_lua_co_ctx_t entry_co_ctx; /* coroutine context for the

src/ngx_http_lua_coroutine.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,15 @@ ngx_http_lua_coroutine_create_helper(lua_State *L, ngx_http_request_t *r,
8686
* to main Lua thread
8787
*/
8888
co = lua_newthread(vm);
89+
if (co == NULL) {
90+
return luaL_error(L, "no memory");
91+
}
8992

9093
ngx_http_lua_probe_user_coroutine_create(r, L, co);
9194

9295
coctx = ngx_http_lua_get_co_ctx(co, ctx);
9396
if (coctx == NULL) {
94-
coctx = ngx_http_lua_create_co_ctx(r, ctx);
97+
coctx = ngx_http_lua_create_co_ctx(r, ctx, co);
9598
if (coctx == NULL) {
9699
return luaL_error(L, "no memory");
97100
}

0 commit comments

Comments
 (0)