| 783 | } |
| 784 | |
| 785 | ts_lua_http_ctx * |
| 786 | ts_lua_create_http_ctx(ts_lua_main_ctx *main_ctx, ts_lua_instance_conf *conf) |
| 787 | { |
| 788 | ts_lua_coroutine *crt; |
| 789 | ts_lua_http_ctx *http_ctx; |
| 790 | lua_State *L; |
| 791 | lua_State *l; |
| 792 | |
| 793 | L = main_ctx->lua; |
| 794 | |
| 795 | http_ctx = TSRalloc<ts_lua_http_ctx>(); |
| 796 | memset(http_ctx, 0, sizeof(*http_ctx)); |
| 797 | |
| 798 | // create coroutine for http_ctx |
| 799 | crt = &http_ctx->cinfo.routine; |
| 800 | l = lua_newthread(L); |
| 801 | |
| 802 | lua_pushlightuserdata(L, conf); |
| 803 | lua_rawget(L, LUA_REGISTRYINDEX); |
| 804 | |
| 805 | /* new globals table for coroutine */ |
| 806 | lua_newtable(l); |
| 807 | lua_pushvalue(l, -1); |
| 808 | lua_setfield(l, -2, "_G"); |
| 809 | lua_newtable(l); |
| 810 | lua_xmove(L, l, 1); |
| 811 | lua_setfield(l, -2, "__index"); |
| 812 | lua_setmetatable(l, -2); |
| 813 | |
| 814 | lua_replace(l, LUA_GLOBALSINDEX); |
| 815 | |
| 816 | // init coroutine |
| 817 | crt->ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 818 | crt->lua = l; |
| 819 | crt->mctx = main_ctx; |
| 820 | |
| 821 | // update thread stats |
| 822 | ts_lua_ctx_stats *const stats = main_ctx->stats; |
| 823 | |
| 824 | TSMutexLock(stats->mutexp); |
| 825 | ++stats->threads; |
| 826 | if (stats->threads_max < stats->threads) { |
| 827 | stats->threads_max = stats->threads; |
| 828 | } |
| 829 | TSMutexUnlock(stats->mutexp); |
| 830 | |
| 831 | http_ctx->instance_conf = conf; |
| 832 | |
| 833 | ts_lua_set_http_ctx(l, http_ctx); |
| 834 | ts_lua_create_context_table(l); |
| 835 | |
| 836 | return http_ctx; |
| 837 | } |
| 838 | |
| 839 | void |
| 840 | ts_lua_destroy_http_ctx(ts_lua_http_ctx *http_ctx) |
no test coverage detected