add pthread lock

This commit is contained in:
Cloud Wu
2015-08-13 21:00:20 +08:00
parent 0ce9921c25
commit bf8f9b8654
18 changed files with 249 additions and 104 deletions

View File

@@ -1,5 +1,6 @@
#include "skynet.h"
#include "skynet_env.h"
#include "spinlock.h"
#include <lua.h>
#include <lauxlib.h>
@@ -8,18 +9,15 @@
#include <assert.h>
struct skynet_env {
int lock;
struct spinlock 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)
SPIN_LOCK(E)
lua_State *L = E->L;
@@ -27,14 +25,14 @@ skynet_getenv(const char *key) {
const char * result = lua_tostring(L, -1);
lua_pop(L, 1);
UNLOCK(E)
SPIN_UNLOCK(E)
return result;
}
void
skynet_setenv(const char *key, const char *value) {
LOCK(E)
SPIN_LOCK(E)
lua_State *L = E->L;
lua_getglobal(L, key);
@@ -43,12 +41,12 @@ skynet_setenv(const char *key, const char *value) {
lua_pushstring(L,value);
lua_setglobal(L,key);
UNLOCK(E)
SPIN_UNLOCK(E)
}
void
skynet_env_init() {
E = skynet_malloc(sizeof(*E));
E->lock = 0;
SPIN_INIT(E)
E->L = luaL_newstate();
}