aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-06-12 15:43:13 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-06-12 15:43:13 -0300
commit89e8303f4ea5f1041f3633db7948e7aef17b5226 (patch)
treed3f6853fd3bc177c4ba728aa71051cd29ba57c89
parent89c301d180ef4ef3887fbf54fe2fd43a5c8d183a (diff)
downloadlua-89e8303f4ea5f1041f3633db7948e7aef17b5226.tar.gz
lua-89e8303f4ea5f1041f3633db7948e7aef17b5226.tar.bz2
lua-89e8303f4ea5f1041f3633db7948e7aef17b5226.zip
more robust treatment of GC tag methods (now they can create new
objects while running...)
-rw-r--r--lgc.c29
-rw-r--r--lgc.h4
-rw-r--r--lstate.c9
3 files changed, 16 insertions, 26 deletions
diff --git a/lgc.c b/lgc.c
index e7d533d3..d3c9490c 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.c,v 1.101 2001/06/07 15:01:21 roberto Exp roberto $ 2** $Id: lgc.c,v 1.102 2001/06/08 19:01:38 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*/
@@ -18,18 +18,6 @@
18#include "ltm.h" 18#include "ltm.h"
19 19
20 20
21/*
22** optional lock for GC
23** (when Lua calls GC tag methods it unlocks the regular lock)
24*/
25#ifndef lua_lockgc
26#define lua_lockgc(L) {
27#endif
28
29#ifndef lua_unlockgc
30#define lua_unlockgc(L) }
31#endif
32
33 21
34typedef struct GCState { 22typedef struct GCState {
35 Hash *tmark; /* list of marked tables to be visited */ 23 Hash *tmark; /* list of marked tables to be visited */
@@ -281,7 +269,7 @@ static void collecttable (lua_State *L) {
281} 269}
282 270
283 271
284static void collectudata (lua_State *L) { 272void luaC_collectudata (lua_State *L) {
285 Udata **p = &G(L)->rootudata; 273 Udata **p = &G(L)->rootudata;
286 Udata *next; 274 Udata *next;
287 while ((next = *p) != NULL) { 275 while ((next = *p) != NULL) {
@@ -351,9 +339,8 @@ static void callgcTM (lua_State *L, const TObject *obj) {
351} 339}
352 340
353 341
354static void callgcTMudata (lua_State *L) { 342void luaC_callgcTMudata (lua_State *L) {
355 int tag; 343 int tag;
356 G(L)->GCthreshold = 2*G(L)->nblocks; /* avoid GC during tag methods */
357 for (tag=G(L)->ntag-1; tag>=0; tag--) { /* for each tag (in reverse order) */ 344 for (tag=G(L)->ntag-1; tag>=0; tag--) { /* for each tag (in reverse order) */
358 Udata *udata; 345 Udata *udata;
359 while ((udata = G(L)->TMtable[tag].collected) != NULL) { 346 while ((udata = G(L)->TMtable[tag].collected) != NULL) {
@@ -368,14 +355,14 @@ static void callgcTMudata (lua_State *L) {
368 355
369 356
370void luaC_collect (lua_State *L, int all) { 357void luaC_collect (lua_State *L, int all) {
371 lua_lockgc(L); 358 luaC_collectudata(L);
372 collectudata(L);
373 callgcTMudata(L);
374 collectstrings(L, all); 359 collectstrings(L, all);
375 collecttable(L); 360 collecttable(L);
376 collectproto(L); 361 collectproto(L);
377 collectclosure(L); 362 collectclosure(L);
378 lua_unlockgc(L); 363 checkMbuffer(L);
364 G(L)->GCthreshold = 2*G(L)->nblocks; /* set new threshold */
365 luaC_callgcTMudata(L);
379} 366}
380 367
381 368
@@ -383,8 +370,6 @@ void luaC_collectgarbage (lua_State *L) {
383 markall(L); 370 markall(L);
384 invalidatetables(G(L)); 371 invalidatetables(G(L));
385 luaC_collect(L, 0); 372 luaC_collect(L, 0);
386 checkMbuffer(L);
387 G(L)->GCthreshold = 2*G(L)->nblocks; /* set new threshold */
388 callgcTM(L, &luaO_nilobject); 373 callgcTM(L, &luaO_nilobject);
389} 374}
390 375
diff --git a/lgc.h b/lgc.h
index 82f19b76..7268e4c2 100644
--- a/lgc.h
+++ b/lgc.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.h,v 1.9 2001/02/02 16:23:20 roberto Exp roberto $ 2** $Id: lgc.h,v 1.10 2001/06/05 19:27:32 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,6 +15,8 @@
15 luaC_collectgarbage(L) 15 luaC_collectgarbage(L)
16 16
17 17
18void luaC_collectudata (lua_State *L);
19void luaC_callgcTMudata (lua_State *L);
18void luaC_collect (lua_State *L, int all); 20void luaC_collect (lua_State *L, int all);
19void luaC_collectgarbage (lua_State *L); 21void luaC_collectgarbage (lua_State *L);
20 22
diff --git a/lstate.c b/lstate.c
index 194e92c1..65c1cde4 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 1.62 2001/04/17 17:35:54 roberto Exp roberto $ 2** $Id: lstate.c,v 1.63 2001/06/06 18:00:19 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*/
@@ -112,11 +112,14 @@ static void close_state (lua_State *L, lua_State *OL) {
112 L->next->previous = L->previous; 112 L->next->previous = L->previous;
113 } 113 }
114 else if (G(L)) { /* last thread; close global state */ 114 else if (G(L)) { /* last thread; close global state */
115 luaC_collect(L, 1); /* collect all elements */ 115 while (G(L)->rootudata) {
116 luaC_collectudata(L); /* collect all user data */
117 luaC_callgcTMudata(L); /* call their tag methods */
118 } /* repeat, as tag methods may create new userdata objects */
119 luaC_collect(L, 1); /* collect all other elements */
116 lua_assert(G(L)->rootproto == NULL); 120 lua_assert(G(L)->rootproto == NULL);
117 lua_assert(G(L)->rootcl == NULL); 121 lua_assert(G(L)->rootcl == NULL);
118 lua_assert(G(L)->roottable == NULL); 122 lua_assert(G(L)->roottable == NULL);
119 lua_assert(G(L)->rootudata == NULL);
120 luaS_freeall(L); 123 luaS_freeall(L);
121 luaM_freearray(L, G(L)->TMtable, G(L)->sizeTM, struct TM); 124 luaM_freearray(L, G(L)->TMtable, G(L)->sizeTM, struct TM);
122 luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, l_char); 125 luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, l_char);