From 8dcc6bc5321f12f28c6738b68350e920983101fb Mon Sep 17 00:00:00 2001
From: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Wed, 14 Sep 2005 14:48:57 -0300
Subject: avoid the use of global lock

---
 ltests.c | 26 ++++++--------------------
 ltests.h | 17 ++++++++++-------
 2 files changed, 16 insertions(+), 27 deletions(-)

diff --git a/ltests.c b/ltests.c
index cd595865..7530b7f9 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
 /*
-** $Id: ltests.c,v 2.29 2005/08/26 17:32:05 roberto Exp roberto $
+** $Id: ltests.c,v 2.30 2005/08/26 17:36:32 roberto Exp roberto $
 ** Internal Module for Debugging of the Lua Implementation
 ** See Copyright Notice in lua.h
 */
@@ -737,7 +737,6 @@ static int loadlib (lua_State *L) {
 static int closestate (lua_State *L) {
   lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_checknumber(L, 1)));
   lua_close(L1);
-  lua_unlock(L);  /* close cannot unlock that */
   return 0;
 }
 
@@ -1121,39 +1120,26 @@ static const struct luaL_Reg tests_funcs[] = {
 };
 
 
-static void fim (void) {
-  if (!islocked)
-    lua_close(lua_state);
-  lua_assert(memcontrol.numblocks == 0);
-  lua_assert(memcontrol.total == 0);
-}
-
-
-static int l_panic (lua_State *L) {
-  UNUSED(L);
-  fprintf(stderr, "unable to recover; exiting\n");
-  return 0;
-}
-
-
 int luaB_opentests (lua_State *L) {
   void *ud;
   lua_assert(lua_getallocf(L, &ud) == debug_realloc);
   lua_assert(ud == cast(void *, &memcontrol));
-  lua_atpanic(L, l_panic);
   lua_state = L;  /* keep first state to be opened */
   luaL_register(L, "T", tests_funcs);
-  atexit(fim);
   return 0;
 }
 
 
 #undef main
 int main (int argc, char *argv[]) {
+  int ret;
   char *limit = getenv("MEMLIMIT");
   if (limit)
     memcontrol.memlimit = strtoul(limit, NULL, 10);
-  return l_main(argc, argv);
+  ret = l_main(argc, argv);
+  lua_assert(memcontrol.numblocks == 0);
+  lua_assert(memcontrol.total == 0);
+  return ret;
 }
 
 #endif
diff --git a/ltests.h b/ltests.h
index f609ad69..1d054bae 100644
--- a/ltests.h
+++ b/ltests.h
@@ -1,5 +1,5 @@
 /*
-** $Id: ltests.h,v 2.14 2005/05/03 19:01:17 roberto Exp roberto $
+** $Id: ltests.h,v 2.15 2005/06/06 13:30:25 roberto Exp roberto $
 ** Internal Header for Debugging of the Lua Implementation
 ** See Copyright Notice in lua.h
 */
@@ -55,16 +55,19 @@ int lua_checkpc (lua_State *L, pCallInfo ci);
 
 /* test for lock/unlock */
 #undef luai_userstateopen
+#undef luai_userstatethread
 #undef lua_lock
 #undef lua_unlock
 #undef LUAI_EXTRASPACE
 
-LUAI_DATA int islocked;
-#define LUAI_EXTRASPACE		sizeof(double)
-#define getlock(l)	(*(cast(int **, l) - 1))
-#define luai_userstateopen(l)	getlock(l) = &islocked;
-#define lua_lock(l)     lua_assert((*getlock(l))++ == 0)
-#define lua_unlock(l)   lua_assert(--(*getlock(l)) == 0)
+struct L_EXTRA { int lock; int *plock; };
+#define LUAI_EXTRASPACE		sizeof(struct L_EXTRA)
+#define getlock(l)	(cast(struct L_EXTRA *, l) - 1)
+#define luai_userstateopen(l)  \
+	(getlock(l)->lock = 0, getlock(l)->plock = &(getlock(l)->lock))
+#define luai_userstatethread(l,l1)  (getlock(l1)->plock = getlock(l)->plock)
+#define lua_lock(l)     lua_assert((*getlock(l)->plock)++ == 0)
+#define lua_unlock(l)   lua_assert(--(*getlock(l)->plock) == 0)
 
 
 int luaB_opentests (lua_State *L);
-- 
cgit v1.2.3-55-g6feb