Files
skynet/skynet-src/skynet_env.c
2012-08-09 15:36:56 +08:00

54 lines
819 B
C

#include "skynet_env.h"
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <assert.h>
struct skynet_env {
int lock;
lua_State *L;
};
static struct skynet_env *E = NULL;
#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {}
#define UNLOCK(q) __sync_lock_release(&(q)->lock);
const char *
skynet_getenv(const char *key) {
LOCK(E)
lua_State *L = E->L;
lua_getglobal(L, key);
const char * result = lua_tostring(L, -1);
lua_pop(L, 1);
UNLOCK(E)
return result;
}
void
skynet_setenv(const char *key, const char *value) {
LOCK(E)
lua_State *L = E->L;
lua_getglobal(L, key);
assert(lua_isnil(L, -1));
lua_pop(L,1);
lua_pushstring(L,value);
lua_setglobal(L,key);
UNLOCK(E)
}
void
skynet_env_init() {
E = malloc(sizeof(*E));
E->lock = 0;
E->L = luaL_newstate();
}