ngx_lua_module开发(2012-11-17 9:30)

#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>


#define NGX_LUA_THREAD_N    16


typedef struct {
    lua_State  *l;
} ngx_lua_t;


typedef struct {
    lua_State  *l;
    int         ref;
} ngx_lua_thread_t;


static int  ngx_lua_thread_key;


ngx_lua_t *
ngx_lua_create(void)
{
    ngx_lua_t  *lua;
    lua_State  *l;

    lua = (ngx_lua_t *) malloc(sizeof(ngx_lua_t));
    if (lua == NULL) {
        return NULL;
    }

    memset(lua, 0, sizeof(ngx_lua_t));

    l = luaL_newstate();
    if (l == NULL) {
        free(lua);
        return NULL;
    }

    luaL_openlibs(l);

    lua_pushlightuserdata(l, &ngx_lua_thread_key);
    lua_newtable(l);
    lua_rawset(l, LUA_REGISTRYINDEX);

    lua->l = l;

    return lua;
}


void
ngx_lua_destroy(ngx_lua_t *lua)
{
    if (lua->l != NULL) {
        lua_close(lua->l);
    }

    free(lua);
}


ngx_lua_thread_t *
ngx_lua_thread_create(ngx_lua_t *lua)
{
    int                top, ref;
    lua_State         *l;
    ngx_lua_thread_t  *thr;

    thr = (ngx_lua_thread_t *) malloc(sizeof(ngx_lua_thread_t));
    if (thr == NULL) {
        return NULL;
    }

    memset(thr, 0, sizeof(ngx_lua_thread_t));

    top = lua_gettop(lua->l);

    lua_pushlightuserdata(lua->l, &ngx_lua_thread_key);
    lua_rawget(lua->l, LUA_REGISTRYINDEX);

    l = lua_newthread(lua->l);
    if (l == NULL) {
        lua_settop(lua->l, top);
        free(thr);
        return NULL;
    }

    lua_newtable(l);
    lua_pushvalue(l, -1);
    lua_setfield(l, -2, "_G");

    lua_newtable(l);
    lua_pushvalue(l, LUA_GLOBALSINDEX);
    lua_setfield(l, -2, "__index");
    lua_setmetatable(l, -2);

    lua_replace(l, LUA_GLOBALSINDEX);

    ref = luaL_ref(lua->l, -2);
    if (ref == LUA_REFNIL) {
        lua_settop(lua->l, top);
        free(thr);
        return NULL;
    }

    lua_settop(lua->l, top);

    thr->l = l;
    thr->ref = ref;

    return thr;
}


void
ngx_lua_thread_destroy(ngx_lua_t *lua, ngx_lua_thread_t *thr)
{
    int  top;

    top = lua_gettop(lua->l);

    lua_pushlightuserdata(lua->l, &ngx_lua_thread_key);
    lua_rawget(lua->l, LUA_REGISTRYINDEX);

    luaL_unref(lua->l, -1, thr->ref);

    lua_settop(lua->l, top);

    free(thr);
}


void
ngx_lua_thread_run(ngx_lua_thread_t *thr)
{
    int  rc;

    lua_getglobal(thr->l, "print");
    lua_pushfstring(thr->l, "%p", thr->l);

    rc = lua_resume(thr->l, 1);
}


int
main(int argc, char *argv[])
{
    int                n;
    ngx_lua_t         *lua;
    ngx_lua_thread_t  *thrs[NGX_LUA_THREAD_N], *thr;

    lua = ngx_lua_create();
    if (lua == NULL) {
        return 1;
    }

    printf("lua:%p\n", lua->l);

    lua_pushstring(lua->l, "test");

    for (n = 0; n < NGX_LUA_THREAD_N; n++) {
        thr = ngx_lua_thread_create(lua);
        if (thr == NULL) {
            ngx_lua_destroy(lua);
            return 1;
        }

        printf("lua:%p thr:%p\n", lua->l, thr->l);

        thrs[n] = thr;
    }

    printf("%s\n", lua_tostring(lua->l, -1));

    for (n = 0; n < NGX_LUA_THREAD_N; n++) {
        ngx_lua_thread_run(thrs[n]);
    }

    printf("%s\n", lua_tostring(lua->l, -1));

    for (n = 0; n < NGX_LUA_THREAD_N; n++) {
        ngx_lua_thread_destroy(lua, thrs[n]);
    }

    printf("%s\n", lua_tostring(lua->l, -1));

    ngx_lua_destroy(lua);

    return 0;
}

转载于:https://my.oschina.net/ngwsx/blog/89907

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值