浏览代码

lock/unlock may use L + better structure for internal debug stuff

Roberto Ierusalimschy 24 年之前
父节点
当前提交
426d3e43bd
共有 12 个文件被更改,包括 213 次插入224 次删除
  1. 103 103
      lapi.c
  2. 13 13
      ldebug.c
  3. 12 12
      ldo.c
  4. 16 1
      lgc.c
  5. 1 9
      lmem.h
  6. 2 3
      lobject.c
  7. 4 13
      lobject.h
  8. 30 45
      lstate.c
  9. 3 11
      lstate.h
  10. 19 4
      ltests.c
  11. 7 7
      ltm.c
  12. 3 3
      lvm.c

+ 103 - 103
lapi.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lapi.c,v 1.123 2001/02/01 13:56:49 roberto Exp roberto $
+** $Id: lapi.c,v 1.124 2001/02/01 16:03:38 roberto Exp roberto $
 ** Lua API
 ** See Copyright Notice in lua.h
 */
@@ -55,9 +55,9 @@ void luaA_pushobject (lua_State *L, const TObject *o) {
 
 LUA_API int lua_stackspace (lua_State *L) {
   int i;
-  LUA_LOCK;
+  LUA_LOCK(L);
   i = (L->stack_last - L->top);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return i;
 }
 
@@ -70,49 +70,49 @@ LUA_API int lua_stackspace (lua_State *L) {
 
 LUA_API int lua_gettop (lua_State *L) {
   int i;
-  LUA_LOCK;
+  LUA_LOCK(L);
   i = (L->top - L->Cbase);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return i;
 }
 
 
 LUA_API void lua_settop (lua_State *L, int index) {
-  LUA_LOCK;
+  LUA_LOCK(L);
   if (index >= 0)
     luaD_adjusttop(L, L->Cbase, index);
   else
     L->top = L->top+index+1;  /* index is negative */
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_remove (lua_State *L, int index) {
   StkId p;
-  LUA_LOCK;
+  LUA_LOCK(L);
   p = luaA_index(L, index);
   while (++p < L->top) setobj(p-1, p);
   L->top--;
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_insert (lua_State *L, int index) {
   StkId p;
   StkId q;
-  LUA_LOCK;
+  LUA_LOCK(L);
   p = luaA_index(L, index);
   for (q = L->top; q>p; q--) setobj(q, q-1);
   setobj(p, L->top);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_pushvalue (lua_State *L, int index) {
-  LUA_LOCK;
+  LUA_LOCK(L);
   setobj(L->top, luaA_index(L, index));
   api_incr_top(L);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
@@ -125,19 +125,19 @@ LUA_API void lua_pushvalue (lua_State *L, int index) {
 LUA_API int lua_type (lua_State *L, int index) {
   StkId o;
   int i;
-  LUA_LOCK;
+  LUA_LOCK(L);
   o = luaA_indexAcceptable(L, index);
   i = (o == NULL) ? LUA_TNONE : ttype(o);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return i;
 }
 
 
 LUA_API const char *lua_typename (lua_State *L, int t) {
   const char *s;
-  LUA_LOCK;
+  LUA_LOCK(L);
   s = (t == LUA_TNONE) ? "no value" : basictypename(G(L), t);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return s;
 }
 
@@ -145,10 +145,10 @@ LUA_API const char *lua_typename (lua_State *L, int t) {
 LUA_API const char *lua_xtype (lua_State *L, int index) {
   StkId o;
   const char *type;
-  LUA_LOCK;
+  LUA_LOCK(L);
   o = luaA_indexAcceptable(L, index);
   type = (o == NULL) ? "no value" : luaT_typename(G(L), o);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return type;
 }
 
@@ -156,20 +156,20 @@ LUA_API const char *lua_xtype (lua_State *L, int index) {
 LUA_API int lua_iscfunction (lua_State *L, int index) {
   StkId o;
   int i;
-  LUA_LOCK;
+  LUA_LOCK(L);
   o = luaA_indexAcceptable(L, index);
   i = (o == NULL) ? 0 : iscfunction(o);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return i;
 }
 
 LUA_API int lua_isnumber (lua_State *L, int index) {
   TObject *o;
   int i;
-  LUA_LOCK;
+  LUA_LOCK(L);
   o = luaA_indexAcceptable(L, index);
   i = (o == NULL) ? 0 : (tonumber(o) == 0);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return i;
 }
 
@@ -182,34 +182,34 @@ LUA_API int lua_isstring (lua_State *L, int index) {
 LUA_API int lua_tag (lua_State *L, int index) {
   StkId o;
   int i;
-  LUA_LOCK;
+  LUA_LOCK(L);
   o = luaA_indexAcceptable(L, index);
   i = (o == NULL) ? LUA_NOTAG : luaT_tag(o);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return i;
 }
 
 LUA_API int lua_equal (lua_State *L, int index1, int index2) {
   StkId o1, o2;
   int i;
-  LUA_LOCK;
+  LUA_LOCK(L);
   o1 = luaA_indexAcceptable(L, index1);
   o2 = luaA_indexAcceptable(L, index2);
   i = (o1 == NULL || o2 == NULL) ? 0  /* index out-of-range */
                                  : luaO_equalObj(o1, o2);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return i;
 }
 
 LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
   StkId o1, o2;
   int i;
-  LUA_LOCK;
+  LUA_LOCK(L);
   o1 = luaA_indexAcceptable(L, index1);
   o2 = luaA_indexAcceptable(L, index2);
   i = (o1 == NULL || o2 == NULL) ? 0  /* index out-of-range */
                                  : luaV_lessthan(L, o1, o2, L->top);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return i;
 }
 
@@ -218,58 +218,58 @@ LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
 LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
   StkId o;
   lua_Number n;
-  LUA_LOCK;
+  LUA_LOCK(L);
   o = luaA_indexAcceptable(L, index);
   n = (o == NULL || tonumber(o)) ? 0 : nvalue(o);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return n;
 }
 
 LUA_API const char *lua_tostring (lua_State *L, int index) {
   StkId o;
   const char *s;
-  LUA_LOCK;
+  LUA_LOCK(L);
   o = luaA_indexAcceptable(L, index);
   s = (o == NULL || tostring(L, o)) ? NULL : svalue(o);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return s;
 }
 
 LUA_API size_t lua_strlen (lua_State *L, int index) {
   StkId o;
   size_t l;
-  LUA_LOCK;
+  LUA_LOCK(L);
   o = luaA_indexAcceptable(L, index);
   l = (o == NULL || tostring(L, o)) ? 0 : tsvalue(o)->len;
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return l;
 }
 
 LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
   StkId o;
   lua_CFunction f;
-  LUA_LOCK;
+  LUA_LOCK(L);
   o = luaA_indexAcceptable(L, index);
   f = (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->f.c;
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return f;
 }
 
 LUA_API void *lua_touserdata (lua_State *L, int index) {
   StkId o;
   void *p;
-  LUA_LOCK;
+  LUA_LOCK(L);
   o = luaA_indexAcceptable(L, index);
   p = (o == NULL || ttype(o) != LUA_TUSERDATA) ? NULL :
                                                     tsvalue(o)->u.d.value;
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return p;
 }
 
 LUA_API const void *lua_topointer (lua_State *L, int index) {
   StkId o;
   const void *p;
-  LUA_LOCK;
+  LUA_LOCK(L);
   o = luaA_indexAcceptable(L, index);
   if (o == NULL) p = NULL;
   else {
@@ -285,7 +285,7 @@ LUA_API const void *lua_topointer (lua_State *L, int index) {
         break;
     }
   }
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return p;
 }
 
@@ -297,26 +297,26 @@ LUA_API const void *lua_topointer (lua_State *L, int index) {
 
 
 LUA_API void lua_pushnil (lua_State *L) {
-  LUA_LOCK;
+  LUA_LOCK(L);
   setnilvalue(L->top);
   api_incr_top(L);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
-  LUA_LOCK;
+  LUA_LOCK(L);
   setnvalue(L->top, n);
   api_incr_top(L);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
-  LUA_LOCK;
+  LUA_LOCK(L);
   setsvalue(L->top, luaS_newlstr(L, s, len));
   api_incr_top(L);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
@@ -329,20 +329,20 @@ LUA_API void lua_pushstring (lua_State *L, const char *s) {
 
 
 LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
-  LUA_LOCK;
+  LUA_LOCK(L);
   luaV_Cclosure(L, fn, n);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) {
-  LUA_LOCK;
+  LUA_LOCK(L);
   /* ORDER LUA_T */
   if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(G(L), tag)))
     luaO_verror(L, "invalid tag for a userdata (%d)", tag);
   setuvalue(L->top, luaS_createudata(L, u, tag));
   api_incr_top(L);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
@@ -353,54 +353,54 @@ LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) {
 
 
 LUA_API void lua_getglobal (lua_State *L, const char *name) {
-  LUA_LOCK;
+  LUA_LOCK(L);
   luaV_getglobal(L, luaS_new(L, name), L->top);
   api_incr_top(L);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_gettable (lua_State *L, int index) {
   StkId t;
-  LUA_LOCK;
+  LUA_LOCK(L);
   t = Index(L, index);
   luaV_gettable(L, t, L->top, L->top-1);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_rawget (lua_State *L, int index) {
   StkId t;
-  LUA_LOCK;
+  LUA_LOCK(L);
   t = Index(L, index);
   lua_assert(ttype(t) == LUA_TTABLE);
   setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
   StkId o;
-  LUA_LOCK;
+  LUA_LOCK(L);
   o = Index(L, index);
   lua_assert(ttype(o) == LUA_TTABLE);
   setobj(L->top, luaH_getnum(hvalue(o), n));
   api_incr_top(L);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_getglobals (lua_State *L) {
-  LUA_LOCK;
+  LUA_LOCK(L);
   sethvalue(L->top, L->gt);
   api_incr_top(L);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API int lua_getref (lua_State *L, int ref) {
   int status = 1;
-  LUA_LOCK;
+  LUA_LOCK(L);
   if (ref == LUA_REFNIL) {
     setnilvalue(L->top);
     api_incr_top(L);
@@ -412,16 +412,16 @@ LUA_API int lua_getref (lua_State *L, int ref) {
   }
   else
     status = 0;
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return status;
 }
 
 
 LUA_API void lua_newtable (lua_State *L) {
-  LUA_LOCK;
+  LUA_LOCK(L);
   sethvalue(L->top, luaH_new(L, 0));
   api_incr_top(L);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
@@ -432,58 +432,58 @@ LUA_API void lua_newtable (lua_State *L) {
 
 
 LUA_API void lua_setglobal (lua_State *L, const char *name) {
-  LUA_LOCK;
+  LUA_LOCK(L);
   luaV_setglobal(L, luaS_new(L, name), L->top);
   L->top--;  /* remove element from the top */
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_settable (lua_State *L, int index) {
   StkId t;
-  LUA_LOCK;
+  LUA_LOCK(L);
   t = Index(L, index);
   luaV_settable(L, t, L->top - 2, L->top);
   L->top -= 2;  /* pop index and value */
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_rawset (lua_State *L, int index) {
   StkId t;
-  LUA_LOCK;
+  LUA_LOCK(L);
   t = Index(L, index);
   lua_assert(ttype(t) == LUA_TTABLE);
   setobj(luaH_set(L, hvalue(t), L->top-2), (L->top-1));
   L->top -= 2;
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_rawseti (lua_State *L, int index, int n) {
   StkId o;
-  LUA_LOCK;
+  LUA_LOCK(L);
   o = Index(L, index);
   lua_assert(ttype(o) == LUA_TTABLE);
   setobj(luaH_setnum(L, hvalue(o), n), (L->top-1));
   L->top--;
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_setglobals (lua_State *L) {
   StkId newtable;
-  LUA_LOCK;
+  LUA_LOCK(L);
   newtable = --L->top;
   lua_assert(ttype(newtable) == LUA_TTABLE);
   L->gt = hvalue(newtable);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API int lua_ref (lua_State *L,  int lock) {
   int ref;
-  LUA_LOCK;
+  LUA_LOCK(L);
   if (ttype(L->top-1) == LUA_TNIL)
     ref = LUA_REFNIL;
   else {
@@ -500,7 +500,7 @@ LUA_API int lua_ref (lua_State *L,  int lock) {
     G(L)->refArray[ref].st = lock ? LOCK : HOLD;
   }
   L->top--;
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return ref;
 }
 
@@ -511,9 +511,9 @@ LUA_API int lua_ref (lua_State *L,  int lock) {
 */
 
 LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
-  LUA_LOCK;
+  LUA_LOCK(L);
   luaD_call(L, L->top-(nargs+1), nresults);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
@@ -527,28 +527,28 @@ LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
 
 LUA_API int lua_getgcthreshold (lua_State *L) {
   int threshold;
-  LUA_LOCK;
+  LUA_LOCK(L);
   threshold = GCscale(G(L)->GCthreshold);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return threshold;
 }
 
 LUA_API int lua_getgccount (lua_State *L) {
   int count;
-  LUA_LOCK;
+  LUA_LOCK(L);
   count = GCscale(G(L)->nblocks);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return count;
 }
 
 LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
-  LUA_LOCK;
+  LUA_LOCK(L);
   if (newthreshold > GCscale(ULONG_MAX))
     G(L)->GCthreshold = ULONG_MAX;
   else
     G(L)->GCthreshold = GCunscale(newthreshold);
   luaC_checkGC(L);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
@@ -558,7 +558,7 @@ LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
 
 LUA_API int lua_newtype (lua_State *L, const char *name, int basictype) {
   int tag;
-  LUA_LOCK;
+  LUA_LOCK(L);
   if (basictype != LUA_TNONE &&
       basictype != LUA_TTABLE &&
       basictype != LUA_TUSERDATA)
@@ -566,7 +566,7 @@ LUA_API int lua_newtype (lua_State *L, const char *name, int basictype) {
   tag = luaT_newtag(L, name, basictype);
   if (tag == LUA_TNONE)
     luaO_verror(L, "type name '%.30s' already exists", name);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return tag;
 }
 
@@ -574,7 +574,7 @@ LUA_API int lua_newtype (lua_State *L, const char *name, int basictype) {
 LUA_API int lua_type2tag (lua_State *L, const char *name) {
   int tag;
   const TObject *v;
-  LUA_LOCK;
+  LUA_LOCK(L);
   v = luaH_getstr(G(L)->type2tag, luaS_new(L, name));
   if (ttype(v) == LUA_TNIL)
     tag = LUA_TNONE;
@@ -582,14 +582,14 @@ LUA_API int lua_type2tag (lua_State *L, const char *name) {
     lua_assert(ttype(v) == LUA_TNUMBER);
     tag = (int)nvalue(v);
   }
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return tag;
 }
 
 
 LUA_API void lua_settag (lua_State *L, int tag) {
   int basictype;
-  LUA_LOCK;
+  LUA_LOCK(L);
   if (tag < 0 || tag >= G(L)->ntag)
     luaO_verror(L, "%d is not a valid tag", tag);
   basictype = G(L)->TMtable[tag].basictype;
@@ -607,25 +607,25 @@ LUA_API void lua_settag (lua_State *L, int tag) {
       luaO_verror(L, "cannot change the tag of a %.20s",
                   luaT_typename(G(L), L->top-1));
   }
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_error (lua_State *L, const char *s) {
-  LUA_LOCK;
+  LUA_LOCK(L);
   luaD_error(L, s);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_unref (lua_State *L, int ref) {
-  LUA_LOCK;
+  LUA_LOCK(L);
   if (ref >= 0) {
     lua_assert(ref < G(L)->nref && G(L)->refArray[ref].st < 0);
     G(L)->refArray[ref].st = G(L)->refFree;
     G(L)->refFree = ref;
   }
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
@@ -633,7 +633,7 @@ LUA_API int lua_next (lua_State *L, int index) {
   StkId t;
   Node *n;
   int more;
-  LUA_LOCK;
+  LUA_LOCK(L);
   t = luaA_index(L, index);
   lua_assert(ttype(t) == LUA_TTABLE);
   n = luaH_next(L, hvalue(t), luaA_index(L, -1));
@@ -647,7 +647,7 @@ LUA_API int lua_next (lua_State *L, int index) {
     L->top -= 1;  /* remove key */
     more = 0;
   }
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return more;
 }
 
@@ -656,7 +656,7 @@ LUA_API int lua_getn (lua_State *L, int index) {
   Hash *h;
   const TObject *value;
   int n;
-  LUA_LOCK;
+  LUA_LOCK(L);
   h = hvalue(luaA_index(L, index));
   value = luaH_getstr(h, luaS_newliteral(L, "n"));  /* = h.n */
   if (ttype(value) == LUA_TNUMBER)
@@ -674,32 +674,32 @@ LUA_API int lua_getn (lua_State *L, int index) {
     }
     n = (int)max;
   }
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return n;
 }
 
 
 LUA_API void lua_concat (lua_State *L, int n) {
   StkId top;
-  LUA_LOCK;
+  LUA_LOCK(L);
   top = L->top;
   luaV_strconc(L, n, top);
   L->top = top-(n-1);
   luaC_checkGC(L);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
   TString *ts;
   void *p;
-  LUA_LOCK;
+  LUA_LOCK(L);
   if (size == 0) size = 1;
   ts = luaS_newudata(L, size, NULL);
   setuvalue(L->top, ts);
   api_incr_top(L);
   p = ts->u.d.value;
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return p;
 }
 

+ 13 - 13
ldebug.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldebug.c,v 1.57 2001/01/26 11:45:51 roberto Exp roberto $
+** $Id: ldebug.c,v 1.58 2001/01/29 17:16:58 roberto Exp roberto $
 ** Debug Interface
 ** See Copyright Notice in lua.h
 */
@@ -42,20 +42,20 @@ static int isLmark (StkId o) {
 
 LUA_API lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
   lua_Hook oldhook;
-  LUA_LOCK;
+  LUA_LOCK(L);
   oldhook = L->callhook;
   L->callhook = func;
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return oldhook;
 }
 
 
 LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
   lua_Hook oldhook;
-  LUA_LOCK;
+  LUA_LOCK(L);
   oldhook = L->linehook;
   L->linehook = func;
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return oldhook;
 }
 
@@ -76,14 +76,14 @@ static StkId aux_stackedfunction (lua_State *L, int level, StkId top) {
 LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
   StkId f;
   int status;
-  LUA_LOCK;
+  LUA_LOCK(L);
   f = aux_stackedfunction(L, level, L->top);
   if (f == NULL) status = 0;  /* there is no such level */
   else {
     ar->_func = f;
     status = 1;
   }
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return status;
 }
 
@@ -162,7 +162,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
   const char *name;
   StkId f;
   Proto *fp;
-  LUA_LOCK;
+  LUA_LOCK(L);
   name = NULL;
   f = ar->_func;
   fp = getluaproto(f);
@@ -171,7 +171,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {
     if (name)
       luaA_pushobject(L, (f+1)+(n-1));  /* push value */
   }
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return name;
 }
 
@@ -180,7 +180,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
   const char *name;
   StkId f;
   Proto *fp;
-  LUA_LOCK;
+  LUA_LOCK(L);
   name = NULL;
   f = ar->_func;
   fp = getluaproto(f);
@@ -192,7 +192,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
     else
       setobj((f+1)+(n-1), L->top);
   }
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return name;
 }
 
@@ -272,7 +272,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
   StkId func;
   int isactive;
   int status = 1;
-  LUA_LOCK;
+  LUA_LOCK(L);
   isactive = (*what != '>');
   if (isactive)
     func = ar->_func;
@@ -309,7 +309,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
     }
   }
   if (!isactive) L->top--;  /* pop function */
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return status;
 }
 

+ 12 - 12
ldo.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldo.c,v 1.119 2001/01/29 19:34:02 roberto Exp roberto $
+** $Id: ldo.c,v 1.120 2001/02/01 17:40:48 roberto Exp roberto $
 ** Stack and Call structure of Lua
 ** See Copyright Notice in lua.h
 */
@@ -94,9 +94,9 @@ static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
   StkId old_top = L->Cbase = L->top;
   luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */
   L->allowhooks = 0;  /* cannot call hooks inside a hook */
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   (*hook)(L, ar);
-  LUA_LOCK;
+  LUA_LOCK(L);
   lua_assert(L->allowhooks == 0);
   L->allowhooks = 1;
   L->top = old_top;
@@ -135,9 +135,9 @@ static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
   luaD_checkstack(L, nup+LUA_MINSTACK);  /* ensure minimum stack size */
   for (n=0; n<nup; n++)  /* copy upvalues as extra arguments */
     setobj(L->top++, &cl->upvalue[n]);
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   n = (*cl->f.c)(L);  /* do the actual call */
-  LUA_LOCK;
+  LUA_LOCK(L);
   L->Cbase = old_Cbase;  /* restore old C base */
   return L->top - n;  /* return index of first result */
 }
@@ -219,13 +219,13 @@ LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
   StkId func;
   struct CallS c;
   int status;
-  LUA_LOCK;
+  LUA_LOCK(L);
   func = L->top - (nargs+1);  /* function to be called */
   c.func = func; c.nresults = nresults;
   status = luaD_runprotected(L, f_call, &c);
   if (status != 0)  /* an error occurred? */
     L->top = func;  /* remove parameters from the stack */
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return status;
 }
 
@@ -233,23 +233,23 @@ LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
 /*
 ** Execute a protected parser.
 */
-struct ParserS {  /* data to `f_parser' */
+struct SParser {  /* data to `f_parser' */
   ZIO *z;
   int bin;
 };
 
 static void f_parser (lua_State *L, void *ud) {
-  struct ParserS *p = (struct ParserS *)ud;
+  struct SParser *p = (struct SParser *)ud;
   Proto *tf = p->bin ? luaU_undump(L, p->z) : luaY_parser(L, p->z);
   luaV_Lclosure(L, tf, 0);
 }
 
 
 static int protectedparser (lua_State *L, ZIO *z, int bin) {
-  struct ParserS p;
+  struct SParser p;
   mem_int old_blocks;
   int status;
-  LUA_LOCK;
+  LUA_LOCK(L);
   p.z = z; p.bin = bin;
   luaC_checkGC(L);
   old_blocks = G(L)->nblocks;
@@ -261,7 +261,7 @@ static int protectedparser (lua_State *L, ZIO *z, int bin) {
   }
   else if (status == LUA_ERRRUN)  /* an error occurred: correct error code */
     status = LUA_ERRSYNTAX;
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return status;
 }
 

+ 16 - 1
lgc.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lgc.c,v 1.83 2001/01/29 19:34:02 roberto Exp roberto $
+** $Id: lgc.c,v 1.84 2001/02/01 17:40:48 roberto Exp roberto $
 ** Garbage Collector
 ** See Copyright Notice in lua.h
 */
@@ -17,6 +17,19 @@
 #include "ltm.h"
 
 
+/*
+** optional "lock" for GC
+** (when Lua calls GC tag methods it unlocks the regular lock)
+*/
+#ifndef LUA_LOCKGC
+#define LUA_LOCKGC(L)		{
+#endif
+
+#ifndef LUA_UNLOCKGC
+#define LUA_UNLOCKGC(L)		}
+#endif
+
+
 typedef struct GCState {
   Hash *tmark;  /* list of marked tables to be visited */
   Closure *cmark;  /* list of marked closures to be visited */
@@ -351,12 +364,14 @@ static void callgcTMudata (lua_State *L) {
 
 
 void luaC_collect (lua_State *L, int all) {
+  LUA_LOCKGC(L);
   collectudata(L, all);
   callgcTMudata(L);
   collectstrings(L, all);
   collecttable(L);
   collectproto(L);
   collectclosure(L);
+  LUA_UNLOCKGC(L);
 }
 
 

+ 1 - 9
lmem.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lmem.h,v 1.18 2000/12/26 18:46:09 roberto Exp roberto $
+** $Id: lmem.h,v 1.19 2000/12/28 12:55:41 roberto Exp roberto $
 ** Interface to Memory Manager
 ** See Copyright Notice in lua.h
 */
@@ -38,13 +38,5 @@ void *luaM_growaux (lua_State *L, void *block, int *size, int size_elem,
                                     (luint32)(n)*(luint32)sizeof(t)))
 
 
-#ifdef LUA_DEBUG
-extern mem_int memdebug_numblocks;
-extern mem_int memdebug_total;
-extern mem_int memdebug_maxmem;
-extern mem_int memdebug_memlimit;
-#endif
-
-
 #endif
 

+ 2 - 3
lobject.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lobject.c,v 1.62 2001/01/26 14:16:35 roberto Exp roberto $
+** $Id: lobject.c,v 1.63 2001/01/29 19:34:02 roberto Exp roberto $
 ** Some generic functions over Lua objects
 ** See Copyright Notice in lua.h
 */
@@ -19,8 +19,7 @@
 
 
 
-const char luaO_ttnil = LUA_TNIL;
-const TObject luaO_nilobject = {LUA_TNIL, {(void *)&luaO_ttnil}};
+const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
 
 
 /*

+ 4 - 13
lobject.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lobject.h,v 1.91 2001/01/29 19:34:02 roberto Exp roberto $
+** $Id: lobject.h,v 1.92 2001/02/01 17:40:48 roberto Exp roberto $
 ** Type definitions for Lua objects
 ** See Copyright Notice in lua.h
 */
@@ -12,19 +12,12 @@
 #include "lua.h"
 
 
-#ifdef LUA_DEBUG
-#undef NDEBUG
-#include <assert.h>
-#define lua_assert(c)		assert(c)
-#else
+#ifndef lua_assert
 #define lua_assert(c)		/* empty */
 #endif
 
 
-#ifdef LUA_DEBUG
-/* to avoid warnings, and make sure value is really unused */
-#define UNUSED(x)	(x=0, (void)(x))
-#else
+#ifndef UNUSED
 #define UNUSED(x)	((void)(x))	/* to avoid warnings */
 #endif
 
@@ -88,8 +81,7 @@ typedef struct lua_TObject {
   { TObject *_o=(obj); struct CallInfo *_v=(x); \
     _o->tt=LUA_TMARK; _o->value.v=_v; }
 
-#define setnilvalue(obj) \
-  { TObject *_o=(obj); _o->tt=LUA_TNIL; _o->value.v = (void *)&luaO_ttnil; }
+#define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
 
 #define setobj(obj1,obj2) \
   { TObject *o1=(obj1); const TObject *o2=(obj2); \
@@ -220,7 +212,6 @@ typedef struct CallInfo {
 } CallInfo;
 
 
-extern const char luaO_ttnil;  /* "address" of the nil value */
 extern const TObject luaO_nilobject;
 
 

+ 30 - 45
lstate.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.c,v 1.54 2001/01/25 16:45:36 roberto Exp roberto $
+** $Id: lstate.c,v 1.55 2001/01/26 11:45:51 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -19,33 +19,27 @@
 #include "ltm.h"
 
 
-#ifdef LUA_DEBUG
-static lua_State *lua_state = NULL;
-void luaB_opentests (lua_State *L);
-int islocked = 0;
-#endif
-
-
-/*
-** built-in implementation for ERRORMESSAGE. In a "correct" environment
-** ERRORMESSAGE should have an external definition, and so this function
-** would not be used.
-*/
-static int errormessage (lua_State *L) {
-  const char *s = lua_tostring(L, 1);
-  if (s == NULL) s = "(no message)";
-  fprintf(stderr, "error: %s\n", s);
-  return 0;
-}
-
-
 struct Sopen {
   int stacksize;
   lua_State *L;
 };
 
 
-static void close_state (lua_State *L);
+static void close_state (lua_State *L, lua_State *OL);
+
+
+/*
+** initialize ref array and registry
+*/
+#define INIT_REFSIZE	4
+
+static void ref_init (lua_State *L) {
+  G(L)->refArray = luaM_newvector(L, INIT_REFSIZE, struct Ref);
+  G(L)->sizeref = INIT_REFSIZE;
+  sethvalue(&G(L)->refArray[0].o, luaH_new(L, 0));
+  G(L)->refArray[0].st = LOCK;
+  G(L)->nref = 1;
+}
 
 
 /*
@@ -90,17 +84,8 @@ static void f_luaopen (lua_State *L, void *ud) {
     luaS_init(L);
     luaX_init(L);
     luaT_init(L);
+    ref_init(L);
     G(L)->GCthreshold = 4*G(L)->nblocks;
-    LUA_UNLOCK;  /* temporary exit to use the API */
-    lua_newtable(L);
-    lua_ref(L, 1);  /* create registry */
-    lua_register(L, LUA_ERRORMESSAGE, errormessage);
-#ifdef LUA_DEBUG
-    luaB_opentests(L);
-    if (lua_state == NULL) lua_state = L;  /* keep first state to be opened */
-    lua_assert(lua_gettop(L) == 0);
-#endif
-    LUA_LOCK;  /* go back inside */
   }
 }
 
@@ -108,7 +93,7 @@ static void f_luaopen (lua_State *L, void *ud) {
 LUA_API lua_State *lua_open (lua_State *OL, int stacksize) {
   struct Sopen so;
   lua_State *L;
-  LUA_LOCK;
+  if (OL) LUA_LOCK(OL);
   L = luaM_new(OL, lua_State);
   if (L) {  /* allocation OK? */
     L->G = NULL;
@@ -123,20 +108,17 @@ LUA_API lua_State *lua_open (lua_State *OL, int stacksize) {
     so.L = OL;
     if (luaD_runprotected(L, f_luaopen, &so) != 0) {
       /* memory allocation error: free partial state */
-      close_state(L);
+      close_state(L, OL);
       L = NULL;
     }
   }
-  LUA_UNLOCK;
+  if (OL) LUA_UNLOCK(OL);
   return L;
 }
 
 
-static void close_state (lua_State *L) {
-  lua_State *L1;
-  L1 = L->next;  /* any surviving thread (if there is one) */
-  if (L1 == L) L1 = NULL;  /* no surviving threads */
-  if (L1 != NULL) {  /* are there other threads? */
+static void close_state (lua_State *L, lua_State *OL) {
+  if (OL != NULL) {  /* are there other threads? */
     lua_assert(L->previous != L);
     L->previous->next = L->next;
     L->next->previous = L->previous;
@@ -152,15 +134,18 @@ static void close_state (lua_State *L) {
     luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char);
     luaM_freelem(NULL, L->G, global_State);
   }
-  luaM_freearray(L1, L->stack, L->stacksize, TObject);
-  luaM_freelem(L1, L, lua_State);
+  luaM_freearray(OL, L->stack, L->stacksize, TObject);
+  luaM_freelem(OL, L, lua_State);
 }
 
 LUA_API void lua_close (lua_State *L) {
+  lua_State *OL;
   lua_assert(L != lua_state || lua_gettop(L) == 0);
-  LUA_LOCK;
-  close_state(L);
-  LUA_UNLOCK;
+  LUA_LOCK(L);
+  OL = L->next;  /* any surviving thread (if there is one) */
+  if (OL == L) OL = NULL;  /* no surviving threads */
+  close_state(L, OL);
+  if (OL) LUA_UNLOCK(OL);  /* cannot unlock over a freed state */
   lua_assert(L != lua_state || memdebug_numblocks == 0);
   lua_assert(L != lua_state || memdebug_total == 0);
 }

+ 3 - 11
lstate.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.h,v 1.48 2001/01/26 11:45:51 roberto Exp roberto $
+** $Id: lstate.h,v 1.49 2001/02/01 17:40:48 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -12,24 +12,16 @@
 #include "luadebug.h"
 
 
-
-#ifdef LUA_DEBUG
-extern int islocked;
-#define LUA_LOCK	lua_assert(islocked++ == 0)
-#define LUA_UNLOCK	lua_assert(--islocked == 0)
-#endif
-
-
 /*
 ** macros that control all entries and exits from Lua core machine
 ** (mainly for thread syncronization)
 */
 #ifndef LUA_LOCK
-#define LUA_LOCK
+#define LUA_LOCK(L)	((void) 0)
 #endif
 
 #ifndef LUA_UNLOCK
-#define LUA_UNLOCK
+#define LUA_UNLOCK(L)	((void) 0)
 #endif
 
 /*

+ 19 - 4
ltests.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ltests.c,v 1.60 2001/01/29 17:16:58 roberto Exp roberto $
+** $Id: ltests.c,v 1.61 2001/02/01 16:03:38 roberto Exp roberto $
 ** Internal Module for Debugging of the Lua Implementation
 ** See Copyright Notice in lua.h
 */
@@ -28,14 +28,16 @@
 #include "lualib.h"
 
 
-void luaB_opentests (lua_State *L);
-
 
 /*
 ** The whole module only makes sense with LUA_DEBUG on
 */
 #ifdef LUA_DEBUG
 
+lua_State *lua_state = NULL;
+
+int islocked = 0;
+
 
 
 static void setnameval (lua_State *L, const char *name, int val) {
@@ -279,6 +281,7 @@ static int udataval (lua_State *L) {
 static int doonnewstack (lua_State *L) {
   lua_State *L1 = lua_open(L, luaL_check_int(L, 1));
   if (L1 == NULL) return 0;
+  *((int **)L1) = &islocked;  /* initialize the lock */
   lua_dostring(L1, luaL_check_string(L, 2));
   lua_pushnumber(L, 1);
   lua_close(L1);
@@ -288,8 +291,10 @@ static int doonnewstack (lua_State *L) {
 
 static int newstate (lua_State *L) {
   lua_State *L1 = lua_open(NULL, luaL_check_int(L, 1));
-  if (L1)
+  if (L1) {
+    *((int **)L1) = &islocked;  /* initialize the lock */
     lua_pushuserdata(L, L1);
+  }
   else
     lua_pushnil(L);
   return 1;
@@ -311,6 +316,7 @@ static int loadlib (lua_State *L) {
 static int closestate (lua_State *L) {
   luaL_checktype(L, 1, LUA_TUSERDATA);
   lua_close((lua_State *)lua_touserdata(L, 1));
+  LUA_UNLOCK(L);  /* close cannot unlock that */
   return 0;
 }
 
@@ -552,6 +558,9 @@ static const struct luaL_reg tests_funcs[] = {
 
 
 void luaB_opentests (lua_State *L) {
+  *((int **)L) = &islocked;  /* init lock */
+  lua_state = L;  /* keep first state to be opened */
+  /* open lib in a new table */
   lua_newtable(L);
   lua_getglobals(L);
   lua_pushvalue(L, -2);
@@ -559,6 +568,12 @@ void luaB_opentests (lua_State *L) {
   luaL_openl(L, tests_funcs);  /* open functions inside new table */
   lua_setglobals(L);  /* restore old table of globals */
   lua_setglobal(L, "T");  /* set new table as global T */
+  /* open other libraries */
+  lua_baselibopen(L);
+  lua_iolibopen(L);
+  lua_strlibopen(L);
+  lua_mathlibopen(L);
+  lua_dblibopen(L);
 }
 
 #endif

+ 7 - 7
ltm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ltm.c,v 1.63 2001/01/25 16:45:36 roberto Exp roberto $
+** $Id: ltm.c,v 1.64 2001/01/26 11:45:51 roberto Exp roberto $
 ** Tag methods
 ** See Copyright Notice in lua.h
 */
@@ -111,14 +111,14 @@ static void checktag (lua_State *L, int tag) {
 
 LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
   int e;
-  LUA_LOCK;
+  LUA_LOCK(L);
   checktag(L, tagto);
   checktag(L, tagfrom);
   for (e=0; e<TM_N; e++) {
     if (luaT_validevent(tagto, e))
       luaT_gettm(G(L), tagto, e) = luaT_gettm(G(L), tagfrom, e);
   }
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
   return tagto;
 }
 
@@ -156,7 +156,7 @@ const char *luaT_typename (global_State *G, const TObject *o) {
 
 LUA_API void lua_gettagmethod (lua_State *L, int t, const char *event) {
   int e;
-  LUA_LOCK;
+  LUA_LOCK(L);
   e = luaI_checkevent(L, event, t);
   checktag(L, t);
   if (luaT_validevent(t, e) && luaT_gettm(G(L), t, e)) {
@@ -165,13 +165,13 @@ LUA_API void lua_gettagmethod (lua_State *L, int t, const char *event) {
   else
     setnilvalue(L->top);
   incr_top;
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 
 
 LUA_API void lua_settagmethod (lua_State *L, int t, const char *event) {
   int e;
-  LUA_LOCK;
+  LUA_LOCK(L);
   e = luaI_checkevent(L, event, t);
   checktag(L, t);
   if (!luaT_validevent(t, e))
@@ -190,6 +190,6 @@ LUA_API void lua_settagmethod (lua_State *L, int t, const char *event) {
       luaD_error(L, "tag method must be a function (or nil)");
   }
   L->top--;
-  LUA_UNLOCK;
+  LUA_UNLOCK(L);
 }
 

+ 3 - 3
lvm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lvm.c,v 1.162 2001/02/01 16:03:38 roberto Exp roberto $
+** $Id: lvm.c,v 1.163 2001/02/01 17:39:55 roberto Exp roberto $
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -159,7 +159,7 @@ void luaV_settable (lua_State *L, StkId t, StkId key, StkId top) {
   }
   else {  /* try a `settable' tag method */
     Closure *tm = luaT_gettmbyObj(G(L), t, TM_SETTABLE);
-    L->top = top;
+    lua_assert(L->top == top);
     if (tm == NULL)  /* no tag method? */
       luaG_typeerror(L, t, "index");
     else {
@@ -206,7 +206,7 @@ void luaV_setglobal (lua_State *L, TString *s, StkId top) {
     setobj(oldvalue, top-1);  /* raw set */
   }
   else {  /* call tag method */
-    L->top = top;
+    lua_assert(L->top == top);
     luaD_checkstack(L, 3);
     setobj(top+2, top-1);  /* new value */
     setobj(top+1, oldvalue);  /* old value */