use spinlock macro

This commit is contained in:
Cloud Wu
2015-10-12 13:07:09 +08:00
parent 878110f9f7
commit d7e4e43a1b
2 changed files with 11 additions and 11 deletions

View File

@@ -972,29 +972,28 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
// use clonefunction // use clonefunction
#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {} #include "spinlock.h"
#define UNLOCK(q) __sync_lock_release(&(q)->lock);
struct codecache { struct codecache {
int lock; struct spinlock lock;
lua_State *L; lua_State *L;
}; };
static struct codecache CC = { 0 , NULL }; static struct codecache CC;
static void static void
clearcache() { clearcache() {
if (CC.L == NULL) if (CC.L == NULL)
return; return;
LOCK(&CC) SPIN_LOCK(&CC)
lua_close(CC.L); lua_close(CC.L);
CC.L = luaL_newstate(); CC.L = luaL_newstate();
UNLOCK(&CC) SPIN_UNLOCK(&CC)
} }
static void static void
init() { init() {
CC.lock = 0; SPIN_INIT(&CC);
CC.L = luaL_newstate(); CC.L = luaL_newstate();
} }
@@ -1002,13 +1001,13 @@ static const void *
load(const char *key) { load(const char *key) {
if (CC.L == NULL) if (CC.L == NULL)
return NULL; return NULL;
LOCK(&CC) SPIN_LOCK(&CC)
lua_State *L = CC.L; lua_State *L = CC.L;
lua_pushstring(L, key); lua_pushstring(L, key);
lua_rawget(L, LUA_REGISTRYINDEX); lua_rawget(L, LUA_REGISTRYINDEX);
const void * result = lua_touserdata(L, -1); const void * result = lua_touserdata(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
UNLOCK(&CC) SPIN_UNLOCK(&CC)
return result; return result;
} }
@@ -1018,7 +1017,7 @@ save(const char *key, const void * proto) {
lua_State *L; lua_State *L;
const void * result = NULL; const void * result = NULL;
LOCK(&CC) SPIN_LOCK(&CC)
if (CC.L == NULL) { if (CC.L == NULL) {
init(); init();
L = CC.L; L = CC.L;
@@ -1036,7 +1035,7 @@ save(const char *key, const void * proto) {
lua_pop(L,2); lua_pop(L,2);
} }
} }
UNLOCK(&CC) SPIN_UNLOCK(&CC)
return result; return result;
} }

View File

@@ -34,6 +34,7 @@ spinlock_unlock(struct spinlock *lock) {
static inline void static inline void
spinlock_destroy(struct spinlock *lock) { spinlock_destroy(struct spinlock *lock) {
(void) lock;
} }
#else #else