mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
add pthread lock
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user