#include "skynet.h" #include "skynet_env.h" #include #include #include #include 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 = skynet_malloc(sizeof(*E)); E->lock = 0; E->L = luaL_newstate(); }