ngx_lua_module开发(2012-11-16 23:46)

#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;
    }

    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;
}

你可能感兴趣的:(c,nginx,Module,lua,luajit)