From 6f207b15fb20f1c7d06224354cfdf5e32fdbba68 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 10 Feb 2003 15:32:50 -0200 Subject: [PATCH] resist errors in finalizers during lua_close --- lgc.c | 16 +++++----------- lgc.h | 5 +++-- lstate.c | 12 ++++++++++-- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/lgc.c b/lgc.c index fb3dfd09..626756a7 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 1.166 2002/12/04 17:38:31 roberto Exp roberto $ +** $Id: lgc.c,v 1.167 2002/12/19 11:11:55 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -110,7 +110,7 @@ static void marktmu (GCState *st) { /* move `dead' udata that need finalization to list `tmudata' */ -static void separateudata (lua_State *L) { +void luaC_separateudata (lua_State *L) { GCObject **p = &G(L)->rootudata; GCObject *curr; GCObject *collected = NULL; /* to collect udata with gc event */ @@ -411,7 +411,7 @@ static void do1gcTM (lua_State *L, Udata *udata) { } -static void callGCTM (lua_State *L) { +void luaC_callGCTM (lua_State *L) { lu_byte oldah = L->allowhook; L->allowhook = 0; /* stop debug hooks during GC tag methods */ L->top++; /* reserve space to keep udata while runs its gc method */ @@ -431,12 +431,6 @@ static void callGCTM (lua_State *L) { } -void luaC_callallgcTM (lua_State *L) { - separateudata(L); - callGCTM(L); /* call their GC tag methods */ -} - - void luaC_sweep (lua_State *L, int all) { if (all) all = 256; /* larger than any mark */ sweeplist(L, &G(L)->rootudata, all); @@ -469,7 +463,7 @@ static void mark (lua_State *L) { wkv = st.wkv; /* keys must be cleared after preserving udata */ st.wkv = NULL; st.wv = NULL; - separateudata(L); /* separate userdata to be preserved */ + luaC_separateudata(L); /* separate userdata to be preserved */ marktmu(&st); /* mark `preserved' userdata */ propagatemarks(&st); /* remark, to propagate `preserveness' */ cleartablekeys(wkv); @@ -485,7 +479,7 @@ void luaC_collectgarbage (lua_State *L) { mark(L); luaC_sweep(L, 0); checkSizes(L); - callGCTM(L); + luaC_callGCTM(L); } diff --git a/lgc.h b/lgc.h index c4a153e2..78adf69a 100644 --- a/lgc.h +++ b/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 1.16 2002/08/30 19:09:21 roberto Exp roberto $ +** $Id: lgc.h,v 1.17 2002/11/25 12:38:47 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -15,7 +15,8 @@ luaC_collectgarbage(L) -void luaC_callallgcTM (lua_State *L); +void luaC_separateudata (lua_State *L); +void luaC_callGCTM (lua_State *L); void luaC_sweep (lua_State *L, int all); void luaC_collectgarbage (lua_State *L); void luaC_link (lua_State *L, GCObject *o, lu_byte tt); diff --git a/lstate.c b/lstate.c index 2744a5de..a71a8abe 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 1.117 2002/12/04 17:38:31 roberto Exp roberto $ +** $Id: lstate.c,v 1.118 2002/12/19 13:21:08 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -197,10 +197,18 @@ LUA_API lua_State *lua_open (void) { } +static void callallgcTM (lua_State *L, void *ud) { + UNUSED(ud); + luaC_callGCTM(L); /* call GC metamethods for all udata */ +} + + LUA_API void lua_close (lua_State *L) { lua_lock(L); L = G(L)->mainthread; /* only the main thread can be closed */ - luaC_callallgcTM(L); /* call GC tag methods for all udata */ + luaC_separateudata(L); /* separate udata that have GC metamethods */ + /* repeat until no more errors */ + while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0) /* skip */; lua_assert(G(L)->tmudata == NULL); close_state(L); }