aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2003-02-10 15:32:50 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2003-02-10 15:32:50 -0200
commit6f207b15fb20f1c7d06224354cfdf5e32fdbba68 (patch)
treec81ec4d1d2efe14db873dbb1a589c54d4be21ae2
parent3184314bf32ce146fad2e4adc302c840497d5cde (diff)
downloadlua-6f207b15fb20f1c7d06224354cfdf5e32fdbba68.tar.gz
lua-6f207b15fb20f1c7d06224354cfdf5e32fdbba68.tar.bz2
lua-6f207b15fb20f1c7d06224354cfdf5e32fdbba68.zip
resist errors in finalizers during lua_close
-rw-r--r--lgc.c16
-rw-r--r--lgc.h5
-rw-r--r--lstate.c12
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 @@
1/* 1/*
2** $Id: lgc.c,v 1.166 2002/12/04 17:38:31 roberto Exp roberto $ 2** $Id: lgc.c,v 1.167 2002/12/19 11:11:55 roberto Exp roberto $
3** Garbage Collector 3** Garbage Collector
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -110,7 +110,7 @@ static void marktmu (GCState *st) {
110 110
111 111
112/* move `dead' udata that need finalization to list `tmudata' */ 112/* move `dead' udata that need finalization to list `tmudata' */
113static void separateudata (lua_State *L) { 113void luaC_separateudata (lua_State *L) {
114 GCObject **p = &G(L)->rootudata; 114 GCObject **p = &G(L)->rootudata;
115 GCObject *curr; 115 GCObject *curr;
116 GCObject *collected = NULL; /* to collect udata with gc event */ 116 GCObject *collected = NULL; /* to collect udata with gc event */
@@ -411,7 +411,7 @@ static void do1gcTM (lua_State *L, Udata *udata) {
411} 411}
412 412
413 413
414static void callGCTM (lua_State *L) { 414void luaC_callGCTM (lua_State *L) {
415 lu_byte oldah = L->allowhook; 415 lu_byte oldah = L->allowhook;
416 L->allowhook = 0; /* stop debug hooks during GC tag methods */ 416 L->allowhook = 0; /* stop debug hooks during GC tag methods */
417 L->top++; /* reserve space to keep udata while runs its gc method */ 417 L->top++; /* reserve space to keep udata while runs its gc method */
@@ -431,12 +431,6 @@ static void callGCTM (lua_State *L) {
431} 431}
432 432
433 433
434void luaC_callallgcTM (lua_State *L) {
435 separateudata(L);
436 callGCTM(L); /* call their GC tag methods */
437}
438
439
440void luaC_sweep (lua_State *L, int all) { 434void luaC_sweep (lua_State *L, int all) {
441 if (all) all = 256; /* larger than any mark */ 435 if (all) all = 256; /* larger than any mark */
442 sweeplist(L, &G(L)->rootudata, all); 436 sweeplist(L, &G(L)->rootudata, all);
@@ -469,7 +463,7 @@ static void mark (lua_State *L) {
469 wkv = st.wkv; /* keys must be cleared after preserving udata */ 463 wkv = st.wkv; /* keys must be cleared after preserving udata */
470 st.wkv = NULL; 464 st.wkv = NULL;
471 st.wv = NULL; 465 st.wv = NULL;
472 separateudata(L); /* separate userdata to be preserved */ 466 luaC_separateudata(L); /* separate userdata to be preserved */
473 marktmu(&st); /* mark `preserved' userdata */ 467 marktmu(&st); /* mark `preserved' userdata */
474 propagatemarks(&st); /* remark, to propagate `preserveness' */ 468 propagatemarks(&st); /* remark, to propagate `preserveness' */
475 cleartablekeys(wkv); 469 cleartablekeys(wkv);
@@ -485,7 +479,7 @@ void luaC_collectgarbage (lua_State *L) {
485 mark(L); 479 mark(L);
486 luaC_sweep(L, 0); 480 luaC_sweep(L, 0);
487 checkSizes(L); 481 checkSizes(L);
488 callGCTM(L); 482 luaC_callGCTM(L);
489} 483}
490 484
491 485
diff --git a/lgc.h b/lgc.h
index c4a153e2..78adf69a 100644
--- a/lgc.h
+++ b/lgc.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.h,v 1.16 2002/08/30 19:09:21 roberto Exp roberto $ 2** $Id: lgc.h,v 1.17 2002/11/25 12:38:47 roberto Exp roberto $
3** Garbage Collector 3** Garbage Collector
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -15,7 +15,8 @@
15 luaC_collectgarbage(L) 15 luaC_collectgarbage(L)
16 16
17 17
18void luaC_callallgcTM (lua_State *L); 18void luaC_separateudata (lua_State *L);
19void luaC_callGCTM (lua_State *L);
19void luaC_sweep (lua_State *L, int all); 20void luaC_sweep (lua_State *L, int all);
20void luaC_collectgarbage (lua_State *L); 21void luaC_collectgarbage (lua_State *L);
21void luaC_link (lua_State *L, GCObject *o, lu_byte tt); 22void 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 @@
1/* 1/*
2** $Id: lstate.c,v 1.117 2002/12/04 17:38:31 roberto Exp roberto $ 2** $Id: lstate.c,v 1.118 2002/12/19 13:21:08 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -197,10 +197,18 @@ LUA_API lua_State *lua_open (void) {
197} 197}
198 198
199 199
200static void callallgcTM (lua_State *L, void *ud) {
201 UNUSED(ud);
202 luaC_callGCTM(L); /* call GC metamethods for all udata */
203}
204
205
200LUA_API void lua_close (lua_State *L) { 206LUA_API void lua_close (lua_State *L) {
201 lua_lock(L); 207 lua_lock(L);
202 L = G(L)->mainthread; /* only the main thread can be closed */ 208 L = G(L)->mainthread; /* only the main thread can be closed */
203 luaC_callallgcTM(L); /* call GC tag methods for all udata */ 209 luaC_separateudata(L); /* separate udata that have GC metamethods */
210 /* repeat until no more errors */
211 while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0) /* skip */;
204 lua_assert(G(L)->tmudata == NULL); 212 lua_assert(G(L)->tmudata == NULL);
205 close_state(L); 213 close_state(L);
206} 214}